data.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  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/writeback.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/pagevec.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/bio.h>
  20. #include <linux/prefetch.h>
  21. #include <linux/uio.h>
  22. #include <linux/cleancache.h>
  23. #include "f2fs.h"
  24. #include "node.h"
  25. #include "segment.h"
  26. #include "trace.h"
  27. #include <trace/events/f2fs.h>
  28. static void f2fs_read_end_io(struct bio *bio)
  29. {
  30. struct bio_vec *bvec;
  31. int i;
  32. if (f2fs_bio_encrypted(bio)) {
  33. if (bio->bi_error) {
  34. f2fs_release_crypto_ctx(bio->bi_private);
  35. } else {
  36. f2fs_end_io_crypto_work(bio->bi_private, bio);
  37. return;
  38. }
  39. }
  40. bio_for_each_segment_all(bvec, bio, i) {
  41. struct page *page = bvec->bv_page;
  42. if (!bio->bi_error) {
  43. SetPageUptodate(page);
  44. } else {
  45. ClearPageUptodate(page);
  46. SetPageError(page);
  47. }
  48. unlock_page(page);
  49. }
  50. bio_put(bio);
  51. }
  52. static void f2fs_write_end_io(struct bio *bio)
  53. {
  54. struct f2fs_sb_info *sbi = bio->bi_private;
  55. struct bio_vec *bvec;
  56. int i;
  57. bio_for_each_segment_all(bvec, bio, i) {
  58. struct page *page = bvec->bv_page;
  59. f2fs_restore_and_release_control_page(&page);
  60. if (unlikely(bio->bi_error)) {
  61. set_page_dirty(page);
  62. set_bit(AS_EIO, &page->mapping->flags);
  63. f2fs_stop_checkpoint(sbi);
  64. }
  65. end_page_writeback(page);
  66. dec_page_count(sbi, F2FS_WRITEBACK);
  67. }
  68. if (!get_pages(sbi, F2FS_WRITEBACK) &&
  69. !list_empty(&sbi->cp_wait.task_list))
  70. wake_up(&sbi->cp_wait);
  71. bio_put(bio);
  72. }
  73. /*
  74. * Low-level block read/write IO operations.
  75. */
  76. static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
  77. int npages, bool is_read)
  78. {
  79. struct bio *bio;
  80. bio = f2fs_bio_alloc(npages);
  81. bio->bi_bdev = sbi->sb->s_bdev;
  82. bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
  83. bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
  84. bio->bi_private = is_read ? NULL : sbi;
  85. return bio;
  86. }
  87. static void __submit_merged_bio(struct f2fs_bio_info *io)
  88. {
  89. struct f2fs_io_info *fio = &io->fio;
  90. if (!io->bio)
  91. return;
  92. if (is_read_io(fio->rw))
  93. trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
  94. else
  95. trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
  96. submit_bio(fio->rw, io->bio);
  97. io->bio = NULL;
  98. }
  99. void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
  100. enum page_type type, int rw)
  101. {
  102. enum page_type btype = PAGE_TYPE_OF_BIO(type);
  103. struct f2fs_bio_info *io;
  104. io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
  105. down_write(&io->io_rwsem);
  106. /* change META to META_FLUSH in the checkpoint procedure */
  107. if (type >= META_FLUSH) {
  108. io->fio.type = META_FLUSH;
  109. if (test_opt(sbi, NOBARRIER))
  110. io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
  111. else
  112. io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
  113. }
  114. __submit_merged_bio(io);
  115. up_write(&io->io_rwsem);
  116. }
  117. /*
  118. * Fill the locked page with data located in the block address.
  119. * Return unlocked page.
  120. */
  121. int f2fs_submit_page_bio(struct f2fs_io_info *fio)
  122. {
  123. struct bio *bio;
  124. struct page *page = fio->encrypted_page ? fio->encrypted_page : fio->page;
  125. if (!f2fs_is_valid_blkaddr(fio->sbi, fio->blk_addr,
  126. __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
  127. return -EFAULT;
  128. trace_f2fs_submit_page_bio(page, fio);
  129. f2fs_trace_ios(fio, 0);
  130. /* Allocate a new bio */
  131. bio = __bio_alloc(fio->sbi, fio->blk_addr, 1, is_read_io(fio->rw));
  132. if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
  133. bio_put(bio);
  134. return -EFAULT;
  135. }
  136. submit_bio(fio->rw, bio);
  137. return 0;
  138. }
  139. void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
  140. {
  141. struct f2fs_sb_info *sbi = fio->sbi;
  142. enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
  143. struct f2fs_bio_info *io;
  144. bool is_read = is_read_io(fio->rw);
  145. struct page *bio_page;
  146. io = is_read ? &sbi->read_io : &sbi->write_io[btype];
  147. verify_block_addr(fio, fio->blk_addr);
  148. down_write(&io->io_rwsem);
  149. if (!is_read)
  150. inc_page_count(sbi, F2FS_WRITEBACK);
  151. if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
  152. io->fio.rw != fio->rw))
  153. __submit_merged_bio(io);
  154. alloc_new:
  155. if (io->bio == NULL) {
  156. int bio_blocks = MAX_BIO_BLOCKS(sbi);
  157. io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
  158. io->fio = *fio;
  159. }
  160. bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
  161. if (bio_add_page(io->bio, bio_page, PAGE_CACHE_SIZE, 0) <
  162. PAGE_CACHE_SIZE) {
  163. __submit_merged_bio(io);
  164. goto alloc_new;
  165. }
  166. io->last_block_in_bio = fio->blk_addr;
  167. f2fs_trace_ios(fio, 0);
  168. up_write(&io->io_rwsem);
  169. trace_f2fs_submit_page_mbio(fio->page, fio);
  170. }
  171. /*
  172. * Lock ordering for the change of data block address:
  173. * ->data_page
  174. * ->node_page
  175. * update block addresses in the node page
  176. */
  177. void set_data_blkaddr(struct dnode_of_data *dn)
  178. {
  179. struct f2fs_node *rn;
  180. __le32 *addr_array;
  181. struct page *node_page = dn->node_page;
  182. unsigned int ofs_in_node = dn->ofs_in_node;
  183. f2fs_wait_on_page_writeback(node_page, NODE);
  184. rn = F2FS_NODE(node_page);
  185. /* Get physical address of data block */
  186. addr_array = blkaddr_in_node(rn);
  187. addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
  188. set_page_dirty(node_page);
  189. }
  190. int reserve_new_block(struct dnode_of_data *dn)
  191. {
  192. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  193. if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
  194. return -EPERM;
  195. if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
  196. return -ENOSPC;
  197. trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
  198. dn->data_blkaddr = NEW_ADDR;
  199. set_data_blkaddr(dn);
  200. mark_inode_dirty(dn->inode);
  201. sync_inode_page(dn);
  202. return 0;
  203. }
  204. int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
  205. {
  206. bool need_put = dn->inode_page ? false : true;
  207. int err;
  208. err = get_dnode_of_data(dn, index, ALLOC_NODE);
  209. if (err)
  210. return err;
  211. if (dn->data_blkaddr == NULL_ADDR)
  212. err = reserve_new_block(dn);
  213. if (err || need_put)
  214. f2fs_put_dnode(dn);
  215. return err;
  216. }
  217. int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
  218. {
  219. struct extent_info ei;
  220. struct inode *inode = dn->inode;
  221. if (f2fs_lookup_extent_cache(inode, index, &ei)) {
  222. dn->data_blkaddr = ei.blk + index - ei.fofs;
  223. return 0;
  224. }
  225. return f2fs_reserve_block(dn, index);
  226. }
  227. struct page *get_read_data_page(struct inode *inode, pgoff_t index,
  228. int rw, bool for_write)
  229. {
  230. struct address_space *mapping = inode->i_mapping;
  231. struct dnode_of_data dn;
  232. struct page *page;
  233. struct extent_info ei;
  234. int err;
  235. struct f2fs_io_info fio = {
  236. .sbi = F2FS_I_SB(inode),
  237. .type = DATA,
  238. .rw = rw,
  239. .encrypted_page = NULL,
  240. };
  241. if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
  242. return read_mapping_page(mapping, index, NULL);
  243. page = f2fs_grab_cache_page(mapping, index, for_write);
  244. if (!page)
  245. return ERR_PTR(-ENOMEM);
  246. if (f2fs_lookup_extent_cache(inode, index, &ei)) {
  247. dn.data_blkaddr = ei.blk + index - ei.fofs;
  248. goto got_it;
  249. }
  250. set_new_dnode(&dn, inode, NULL, NULL, 0);
  251. err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
  252. if (err)
  253. goto put_err;
  254. f2fs_put_dnode(&dn);
  255. if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
  256. err = -ENOENT;
  257. goto put_err;
  258. }
  259. got_it:
  260. if (PageUptodate(page)) {
  261. unlock_page(page);
  262. return page;
  263. }
  264. /*
  265. * A new dentry page is allocated but not able to be written, since its
  266. * new inode page couldn't be allocated due to -ENOSPC.
  267. * In such the case, its blkaddr can be remained as NEW_ADDR.
  268. * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
  269. */
  270. if (dn.data_blkaddr == NEW_ADDR) {
  271. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  272. SetPageUptodate(page);
  273. unlock_page(page);
  274. return page;
  275. }
  276. fio.blk_addr = dn.data_blkaddr;
  277. fio.page = page;
  278. err = f2fs_submit_page_bio(&fio);
  279. if (err)
  280. goto put_err;
  281. return page;
  282. put_err:
  283. f2fs_put_page(page, 1);
  284. return ERR_PTR(err);
  285. }
  286. struct page *find_data_page(struct inode *inode, pgoff_t index)
  287. {
  288. struct address_space *mapping = inode->i_mapping;
  289. struct page *page;
  290. page = find_get_page(mapping, index);
  291. if (page && PageUptodate(page))
  292. return page;
  293. f2fs_put_page(page, 0);
  294. page = get_read_data_page(inode, index, READ_SYNC, false);
  295. if (IS_ERR(page))
  296. return page;
  297. if (PageUptodate(page))
  298. return page;
  299. wait_on_page_locked(page);
  300. if (unlikely(!PageUptodate(page))) {
  301. f2fs_put_page(page, 0);
  302. return ERR_PTR(-EIO);
  303. }
  304. return page;
  305. }
  306. /*
  307. * If it tries to access a hole, return an error.
  308. * Because, the callers, functions in dir.c and GC, should be able to know
  309. * whether this page exists or not.
  310. */
  311. struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
  312. bool for_write)
  313. {
  314. struct address_space *mapping = inode->i_mapping;
  315. struct page *page;
  316. repeat:
  317. page = get_read_data_page(inode, index, READ_SYNC, for_write);
  318. if (IS_ERR(page))
  319. return page;
  320. /* wait for read completion */
  321. lock_page(page);
  322. if (unlikely(!PageUptodate(page))) {
  323. f2fs_put_page(page, 1);
  324. return ERR_PTR(-EIO);
  325. }
  326. if (unlikely(page->mapping != mapping)) {
  327. f2fs_put_page(page, 1);
  328. goto repeat;
  329. }
  330. return page;
  331. }
  332. /*
  333. * Caller ensures that this data page is never allocated.
  334. * A new zero-filled data page is allocated in the page cache.
  335. *
  336. * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
  337. * f2fs_unlock_op().
  338. * Note that, ipage is set only by make_empty_dir, and if any error occur,
  339. * ipage should be released by this function.
  340. */
  341. struct page *get_new_data_page(struct inode *inode,
  342. struct page *ipage, pgoff_t index, bool new_i_size)
  343. {
  344. struct address_space *mapping = inode->i_mapping;
  345. struct page *page;
  346. struct dnode_of_data dn;
  347. int err;
  348. repeat:
  349. page = f2fs_grab_cache_page(mapping, index, true);
  350. if (!page) {
  351. /*
  352. * before exiting, we should make sure ipage will be released
  353. * if any error occur.
  354. */
  355. f2fs_put_page(ipage, 1);
  356. return ERR_PTR(-ENOMEM);
  357. }
  358. set_new_dnode(&dn, inode, ipage, NULL, 0);
  359. err = f2fs_reserve_block(&dn, index);
  360. if (err) {
  361. f2fs_put_page(page, 1);
  362. return ERR_PTR(err);
  363. }
  364. if (!ipage)
  365. f2fs_put_dnode(&dn);
  366. if (PageUptodate(page))
  367. goto got_it;
  368. if (dn.data_blkaddr == NEW_ADDR) {
  369. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  370. SetPageUptodate(page);
  371. } else {
  372. f2fs_put_page(page, 1);
  373. page = get_read_data_page(inode, index, READ_SYNC, true);
  374. if (IS_ERR(page))
  375. goto repeat;
  376. /* wait for read completion */
  377. lock_page(page);
  378. }
  379. got_it:
  380. if (new_i_size && i_size_read(inode) <
  381. ((loff_t)(index + 1) << PAGE_CACHE_SHIFT)) {
  382. i_size_write(inode, ((loff_t)(index + 1) << PAGE_CACHE_SHIFT));
  383. /* Only the directory inode sets new_i_size */
  384. set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
  385. }
  386. return page;
  387. }
  388. static int __allocate_data_block(struct dnode_of_data *dn)
  389. {
  390. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  391. struct f2fs_inode_info *fi = F2FS_I(dn->inode);
  392. struct f2fs_summary sum;
  393. struct node_info ni;
  394. int seg = CURSEG_WARM_DATA;
  395. pgoff_t fofs;
  396. if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
  397. return -EPERM;
  398. dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
  399. if (dn->data_blkaddr == NEW_ADDR)
  400. goto alloc;
  401. if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
  402. return -ENOSPC;
  403. alloc:
  404. get_node_info(sbi, dn->nid, &ni);
  405. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  406. if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
  407. seg = CURSEG_DIRECT_IO;
  408. allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
  409. &sum, seg);
  410. set_data_blkaddr(dn);
  411. /* update i_size */
  412. fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
  413. dn->ofs_in_node;
  414. if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT))
  415. i_size_write(dn->inode,
  416. ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT));
  417. /* direct IO doesn't use extent cache to maximize the performance */
  418. f2fs_drop_largest_extent(dn->inode, fofs);
  419. return 0;
  420. }
  421. static void __allocate_data_blocks(struct inode *inode, loff_t offset,
  422. size_t count)
  423. {
  424. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  425. struct dnode_of_data dn;
  426. u64 start = F2FS_BYTES_TO_BLK(offset);
  427. u64 len = F2FS_BYTES_TO_BLK(count);
  428. bool allocated;
  429. u64 end_offset;
  430. while (len) {
  431. f2fs_balance_fs(sbi);
  432. f2fs_lock_op(sbi);
  433. /* When reading holes, we need its node page */
  434. set_new_dnode(&dn, inode, NULL, NULL, 0);
  435. if (get_dnode_of_data(&dn, start, ALLOC_NODE))
  436. goto out;
  437. allocated = false;
  438. end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  439. while (dn.ofs_in_node < end_offset && len) {
  440. block_t blkaddr;
  441. if (unlikely(f2fs_cp_error(sbi)))
  442. goto sync_out;
  443. blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
  444. if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR) {
  445. if (__allocate_data_block(&dn))
  446. goto sync_out;
  447. allocated = true;
  448. }
  449. len--;
  450. start++;
  451. dn.ofs_in_node++;
  452. }
  453. if (allocated)
  454. sync_inode_page(&dn);
  455. f2fs_put_dnode(&dn);
  456. f2fs_unlock_op(sbi);
  457. }
  458. return;
  459. sync_out:
  460. if (allocated)
  461. sync_inode_page(&dn);
  462. f2fs_put_dnode(&dn);
  463. out:
  464. f2fs_unlock_op(sbi);
  465. return;
  466. }
  467. /*
  468. * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
  469. * f2fs_map_blocks structure.
  470. * If original data blocks are allocated, then give them to blockdev.
  471. * Otherwise,
  472. * a. preallocate requested block addresses
  473. * b. do not use extent cache for better performance
  474. * c. give the block addresses to blockdev
  475. */
  476. static int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
  477. int create, int flag)
  478. {
  479. unsigned int maxblocks = map->m_len;
  480. struct dnode_of_data dn;
  481. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  482. int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
  483. pgoff_t pgofs, end_offset;
  484. int err = 0, ofs = 1;
  485. struct extent_info ei;
  486. bool allocated = false;
  487. map->m_len = 0;
  488. map->m_flags = 0;
  489. /* it only supports block size == page size */
  490. pgofs = (pgoff_t)map->m_lblk;
  491. if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
  492. map->m_pblk = ei.blk + pgofs - ei.fofs;
  493. map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
  494. map->m_flags = F2FS_MAP_MAPPED;
  495. goto out;
  496. }
  497. if (create)
  498. f2fs_lock_op(F2FS_I_SB(inode));
  499. /* When reading holes, we need its node page */
  500. set_new_dnode(&dn, inode, NULL, NULL, 0);
  501. err = get_dnode_of_data(&dn, pgofs, mode);
  502. if (err) {
  503. if (err == -ENOENT)
  504. err = 0;
  505. goto unlock_out;
  506. }
  507. if (__is_valid_data_blkaddr(dn.data_blkaddr) &&
  508. !f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr, DATA_GENERIC)) {
  509. err = -EFAULT;
  510. goto sync_out;
  511. }
  512. if (!is_valid_data_blkaddr(sbi, dn.data_blkaddr)) {
  513. if (create) {
  514. if (unlikely(f2fs_cp_error(sbi))) {
  515. err = -EIO;
  516. goto put_out;
  517. }
  518. err = __allocate_data_block(&dn);
  519. if (err)
  520. goto put_out;
  521. allocated = true;
  522. map->m_flags = F2FS_MAP_NEW;
  523. } else {
  524. if (flag != F2FS_GET_BLOCK_FIEMAP ||
  525. dn.data_blkaddr != NEW_ADDR) {
  526. if (flag == F2FS_GET_BLOCK_BMAP)
  527. err = -ENOENT;
  528. goto put_out;
  529. }
  530. /*
  531. * preallocated unwritten block should be mapped
  532. * for fiemap.
  533. */
  534. if (dn.data_blkaddr == NEW_ADDR)
  535. map->m_flags = F2FS_MAP_UNWRITTEN;
  536. }
  537. }
  538. map->m_flags |= F2FS_MAP_MAPPED;
  539. map->m_pblk = dn.data_blkaddr;
  540. map->m_len = 1;
  541. end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  542. dn.ofs_in_node++;
  543. pgofs++;
  544. get_next:
  545. if (dn.ofs_in_node >= end_offset) {
  546. if (allocated)
  547. sync_inode_page(&dn);
  548. allocated = false;
  549. f2fs_put_dnode(&dn);
  550. set_new_dnode(&dn, inode, NULL, NULL, 0);
  551. err = get_dnode_of_data(&dn, pgofs, mode);
  552. if (err) {
  553. if (err == -ENOENT)
  554. err = 0;
  555. goto unlock_out;
  556. }
  557. end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  558. }
  559. if (maxblocks > map->m_len) {
  560. block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
  561. if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
  562. if (create) {
  563. if (unlikely(f2fs_cp_error(sbi))) {
  564. err = -EIO;
  565. goto sync_out;
  566. }
  567. err = __allocate_data_block(&dn);
  568. if (err)
  569. goto sync_out;
  570. allocated = true;
  571. map->m_flags |= F2FS_MAP_NEW;
  572. blkaddr = dn.data_blkaddr;
  573. } else {
  574. /*
  575. * we only merge preallocated unwritten blocks
  576. * for fiemap.
  577. */
  578. if (flag != F2FS_GET_BLOCK_FIEMAP ||
  579. blkaddr != NEW_ADDR)
  580. goto sync_out;
  581. }
  582. }
  583. /* Give more consecutive addresses for the readahead */
  584. if ((map->m_pblk != NEW_ADDR &&
  585. blkaddr == (map->m_pblk + ofs)) ||
  586. (map->m_pblk == NEW_ADDR &&
  587. blkaddr == NEW_ADDR)) {
  588. ofs++;
  589. dn.ofs_in_node++;
  590. pgofs++;
  591. map->m_len++;
  592. goto get_next;
  593. }
  594. }
  595. sync_out:
  596. if (allocated)
  597. sync_inode_page(&dn);
  598. put_out:
  599. f2fs_put_dnode(&dn);
  600. unlock_out:
  601. if (create)
  602. f2fs_unlock_op(F2FS_I_SB(inode));
  603. out:
  604. trace_f2fs_map_blocks(inode, map, err);
  605. return err;
  606. }
  607. static int __get_data_block(struct inode *inode, sector_t iblock,
  608. struct buffer_head *bh, int create, int flag)
  609. {
  610. struct f2fs_map_blocks map;
  611. int ret;
  612. map.m_lblk = iblock;
  613. map.m_len = bh->b_size >> inode->i_blkbits;
  614. ret = f2fs_map_blocks(inode, &map, create, flag);
  615. if (!ret) {
  616. map_bh(bh, inode->i_sb, map.m_pblk);
  617. bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
  618. bh->b_size = (u64)map.m_len << inode->i_blkbits;
  619. }
  620. return ret;
  621. }
  622. static int get_data_block(struct inode *inode, sector_t iblock,
  623. struct buffer_head *bh_result, int create, int flag)
  624. {
  625. return __get_data_block(inode, iblock, bh_result, create, flag);
  626. }
  627. static int get_data_block_dio(struct inode *inode, sector_t iblock,
  628. struct buffer_head *bh_result, int create)
  629. {
  630. return __get_data_block(inode, iblock, bh_result, create,
  631. F2FS_GET_BLOCK_DIO);
  632. }
  633. static int get_data_block_bmap(struct inode *inode, sector_t iblock,
  634. struct buffer_head *bh_result, int create)
  635. {
  636. return __get_data_block(inode, iblock, bh_result, create,
  637. F2FS_GET_BLOCK_BMAP);
  638. }
  639. static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
  640. {
  641. return (offset >> inode->i_blkbits);
  642. }
  643. static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
  644. {
  645. return (blk << inode->i_blkbits);
  646. }
  647. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  648. u64 start, u64 len)
  649. {
  650. struct buffer_head map_bh;
  651. sector_t start_blk, last_blk;
  652. loff_t isize = i_size_read(inode);
  653. u64 logical = 0, phys = 0, size = 0;
  654. u32 flags = 0;
  655. bool past_eof = false, whole_file = false;
  656. int ret = 0;
  657. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  658. if (ret)
  659. return ret;
  660. if (f2fs_has_inline_data(inode)) {
  661. ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
  662. if (ret != -EAGAIN)
  663. return ret;
  664. }
  665. mutex_lock(&inode->i_mutex);
  666. if (len >= isize) {
  667. whole_file = true;
  668. len = isize;
  669. }
  670. if (logical_to_blk(inode, len) == 0)
  671. len = blk_to_logical(inode, 1);
  672. start_blk = logical_to_blk(inode, start);
  673. last_blk = logical_to_blk(inode, start + len - 1);
  674. next:
  675. memset(&map_bh, 0, sizeof(struct buffer_head));
  676. map_bh.b_size = len;
  677. ret = get_data_block(inode, start_blk, &map_bh, 0,
  678. F2FS_GET_BLOCK_FIEMAP);
  679. if (ret)
  680. goto out;
  681. /* HOLE */
  682. if (!buffer_mapped(&map_bh)) {
  683. start_blk++;
  684. if (!past_eof && blk_to_logical(inode, start_blk) >= isize)
  685. past_eof = 1;
  686. if (past_eof && size) {
  687. flags |= FIEMAP_EXTENT_LAST;
  688. ret = fiemap_fill_next_extent(fieinfo, logical,
  689. phys, size, flags);
  690. } else if (size) {
  691. ret = fiemap_fill_next_extent(fieinfo, logical,
  692. phys, size, flags);
  693. size = 0;
  694. }
  695. /* if we have holes up to/past EOF then we're done */
  696. if (start_blk > last_blk || past_eof || ret)
  697. goto out;
  698. } else {
  699. if (start_blk > last_blk && !whole_file) {
  700. ret = fiemap_fill_next_extent(fieinfo, logical,
  701. phys, size, flags);
  702. goto out;
  703. }
  704. /*
  705. * if size != 0 then we know we already have an extent
  706. * to add, so add it.
  707. */
  708. if (size) {
  709. ret = fiemap_fill_next_extent(fieinfo, logical,
  710. phys, size, flags);
  711. if (ret)
  712. goto out;
  713. }
  714. logical = blk_to_logical(inode, start_blk);
  715. phys = blk_to_logical(inode, map_bh.b_blocknr);
  716. size = map_bh.b_size;
  717. flags = 0;
  718. if (buffer_unwritten(&map_bh))
  719. flags = FIEMAP_EXTENT_UNWRITTEN;
  720. start_blk += logical_to_blk(inode, size);
  721. /*
  722. * If we are past the EOF, then we need to make sure as
  723. * soon as we find a hole that the last extent we found
  724. * is marked with FIEMAP_EXTENT_LAST
  725. */
  726. if (!past_eof && logical + size >= isize)
  727. past_eof = true;
  728. }
  729. cond_resched();
  730. if (fatal_signal_pending(current))
  731. ret = -EINTR;
  732. else
  733. goto next;
  734. out:
  735. if (ret == 1)
  736. ret = 0;
  737. mutex_unlock(&inode->i_mutex);
  738. return ret;
  739. }
  740. struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
  741. unsigned nr_pages)
  742. {
  743. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  744. struct f2fs_crypto_ctx *ctx = NULL;
  745. struct block_device *bdev = sbi->sb->s_bdev;
  746. struct bio *bio;
  747. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
  748. return ERR_PTR(-EFAULT);
  749. if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
  750. ctx = f2fs_get_crypto_ctx(inode);
  751. if (IS_ERR(ctx))
  752. return ERR_CAST(ctx);
  753. /* wait the page to be moved by cleaning */
  754. f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
  755. }
  756. bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
  757. if (!bio) {
  758. if (ctx)
  759. f2fs_release_crypto_ctx(ctx);
  760. return ERR_PTR(-ENOMEM);
  761. }
  762. bio->bi_bdev = bdev;
  763. bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blkaddr);
  764. bio->bi_end_io = f2fs_read_end_io;
  765. bio->bi_private = ctx;
  766. return bio;
  767. }
  768. /*
  769. * This function was originally taken from fs/mpage.c, and customized for f2fs.
  770. * Major change was from block_size == page_size in f2fs by default.
  771. */
  772. static int f2fs_mpage_readpages(struct address_space *mapping,
  773. struct list_head *pages, struct page *page,
  774. unsigned nr_pages)
  775. {
  776. struct bio *bio = NULL;
  777. unsigned page_idx;
  778. sector_t last_block_in_bio = 0;
  779. struct inode *inode = mapping->host;
  780. const unsigned blkbits = inode->i_blkbits;
  781. const unsigned blocksize = 1 << blkbits;
  782. sector_t block_in_file;
  783. sector_t last_block;
  784. sector_t last_block_in_file;
  785. sector_t block_nr;
  786. struct f2fs_map_blocks map;
  787. map.m_pblk = 0;
  788. map.m_lblk = 0;
  789. map.m_len = 0;
  790. map.m_flags = 0;
  791. for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
  792. prefetchw(&page->flags);
  793. if (pages) {
  794. page = list_entry(pages->prev, struct page, lru);
  795. list_del(&page->lru);
  796. if (add_to_page_cache_lru(page, mapping,
  797. page->index, GFP_KERNEL))
  798. goto next_page;
  799. }
  800. block_in_file = (sector_t)page->index;
  801. last_block = block_in_file + nr_pages;
  802. last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
  803. blkbits;
  804. if (last_block > last_block_in_file)
  805. last_block = last_block_in_file;
  806. /*
  807. * Map blocks using the previous result first.
  808. */
  809. if ((map.m_flags & F2FS_MAP_MAPPED) &&
  810. block_in_file > map.m_lblk &&
  811. block_in_file < (map.m_lblk + map.m_len))
  812. goto got_it;
  813. /*
  814. * Then do more f2fs_map_blocks() calls until we are
  815. * done with this page.
  816. */
  817. map.m_flags = 0;
  818. if (block_in_file < last_block) {
  819. map.m_lblk = block_in_file;
  820. map.m_len = last_block - block_in_file;
  821. if (f2fs_map_blocks(inode, &map, 0,
  822. F2FS_GET_BLOCK_READ))
  823. goto set_error_page;
  824. }
  825. got_it:
  826. if ((map.m_flags & F2FS_MAP_MAPPED)) {
  827. block_nr = map.m_pblk + block_in_file - map.m_lblk;
  828. SetPageMappedToDisk(page);
  829. if (!PageUptodate(page) && !cleancache_get_page(page)) {
  830. SetPageUptodate(page);
  831. goto confused;
  832. }
  833. if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
  834. DATA_GENERIC))
  835. goto set_error_page;
  836. } else {
  837. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  838. SetPageUptodate(page);
  839. unlock_page(page);
  840. goto next_page;
  841. }
  842. /*
  843. * This page will go to BIO. Do we need to send this
  844. * BIO off first?
  845. */
  846. if (bio && (last_block_in_bio != block_nr - 1)) {
  847. submit_and_realloc:
  848. submit_bio(READ, bio);
  849. bio = NULL;
  850. }
  851. if (bio == NULL) {
  852. bio = f2fs_grab_bio(inode, block_nr, nr_pages);
  853. if (IS_ERR(bio))
  854. goto set_error_page;
  855. }
  856. if (bio_add_page(bio, page, blocksize, 0) < blocksize)
  857. goto submit_and_realloc;
  858. last_block_in_bio = block_nr;
  859. goto next_page;
  860. set_error_page:
  861. SetPageError(page);
  862. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  863. unlock_page(page);
  864. goto next_page;
  865. confused:
  866. if (bio) {
  867. submit_bio(READ, bio);
  868. bio = NULL;
  869. }
  870. unlock_page(page);
  871. next_page:
  872. if (pages)
  873. page_cache_release(page);
  874. }
  875. BUG_ON(pages && !list_empty(pages));
  876. if (bio)
  877. submit_bio(READ, bio);
  878. return 0;
  879. }
  880. static int f2fs_read_data_page(struct file *file, struct page *page)
  881. {
  882. struct inode *inode = page->mapping->host;
  883. int ret = -EAGAIN;
  884. trace_f2fs_readpage(page, DATA);
  885. /* If the file has inline data, try to read it directly */
  886. if (f2fs_has_inline_data(inode))
  887. ret = f2fs_read_inline_data(inode, page);
  888. if (ret == -EAGAIN)
  889. ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
  890. return ret;
  891. }
  892. static int f2fs_read_data_pages(struct file *file,
  893. struct address_space *mapping,
  894. struct list_head *pages, unsigned nr_pages)
  895. {
  896. struct inode *inode = file->f_mapping->host;
  897. struct page *page = list_entry(pages->prev, struct page, lru);
  898. trace_f2fs_readpages(inode, page, nr_pages);
  899. /* If the file has inline data, skip readpages */
  900. if (f2fs_has_inline_data(inode))
  901. return 0;
  902. return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
  903. }
  904. int do_write_data_page(struct f2fs_io_info *fio)
  905. {
  906. struct page *page = fio->page;
  907. struct inode *inode = page->mapping->host;
  908. struct dnode_of_data dn;
  909. int err = 0;
  910. set_new_dnode(&dn, inode, NULL, NULL, 0);
  911. err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
  912. if (err)
  913. return err;
  914. fio->blk_addr = dn.data_blkaddr;
  915. /* This page is already truncated */
  916. if (fio->blk_addr == NULL_ADDR) {
  917. ClearPageUptodate(page);
  918. goto out_writepage;
  919. }
  920. if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
  921. /* wait for GCed encrypted page writeback */
  922. f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
  923. fio->blk_addr);
  924. fio->encrypted_page = f2fs_encrypt(inode, fio->page);
  925. if (IS_ERR(fio->encrypted_page)) {
  926. err = PTR_ERR(fio->encrypted_page);
  927. goto out_writepage;
  928. }
  929. }
  930. set_page_writeback(page);
  931. if (__is_valid_data_blkaddr(fio->blk_addr) &&
  932. !f2fs_is_valid_blkaddr(fio->sbi, fio->blk_addr,
  933. DATA_GENERIC)) {
  934. err = -EFAULT;
  935. goto out_writepage;
  936. }
  937. /*
  938. * If current allocation needs SSR,
  939. * it had better in-place writes for updated data.
  940. */
  941. if (unlikely(is_valid_data_blkaddr(fio->sbi, fio->blk_addr) &&
  942. !is_cold_data(page) &&
  943. need_inplace_update(inode))) {
  944. rewrite_data_page(fio);
  945. set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
  946. trace_f2fs_do_write_data_page(page, IPU);
  947. } else {
  948. write_data_page(&dn, fio);
  949. set_data_blkaddr(&dn);
  950. f2fs_update_extent_cache(&dn);
  951. trace_f2fs_do_write_data_page(page, OPU);
  952. set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
  953. if (page->index == 0)
  954. set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
  955. }
  956. out_writepage:
  957. f2fs_put_dnode(&dn);
  958. return err;
  959. }
  960. static int f2fs_write_data_page(struct page *page,
  961. struct writeback_control *wbc)
  962. {
  963. struct inode *inode = page->mapping->host;
  964. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  965. loff_t i_size = i_size_read(inode);
  966. const pgoff_t end_index = ((unsigned long long) i_size)
  967. >> PAGE_CACHE_SHIFT;
  968. unsigned offset = 0;
  969. bool need_balance_fs = false;
  970. int err = 0;
  971. struct f2fs_io_info fio = {
  972. .sbi = sbi,
  973. .type = DATA,
  974. .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
  975. .page = page,
  976. .encrypted_page = NULL,
  977. };
  978. trace_f2fs_writepage(page, DATA);
  979. if (page->index < end_index)
  980. goto write;
  981. /*
  982. * If the offset is out-of-range of file size,
  983. * this page does not have to be written to disk.
  984. */
  985. offset = i_size & (PAGE_CACHE_SIZE - 1);
  986. if ((page->index >= end_index + 1) || !offset)
  987. goto out;
  988. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  989. write:
  990. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  991. goto redirty_out;
  992. if (f2fs_is_drop_cache(inode))
  993. goto out;
  994. if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
  995. available_free_memory(sbi, BASE_CHECK))
  996. goto redirty_out;
  997. /* Dentry blocks are controlled by checkpoint */
  998. if (S_ISDIR(inode->i_mode)) {
  999. if (unlikely(f2fs_cp_error(sbi)))
  1000. goto redirty_out;
  1001. err = do_write_data_page(&fio);
  1002. goto done;
  1003. }
  1004. /* we should bypass data pages to proceed the kworkder jobs */
  1005. if (unlikely(f2fs_cp_error(sbi))) {
  1006. SetPageError(page);
  1007. goto out;
  1008. }
  1009. if (!wbc->for_reclaim)
  1010. need_balance_fs = true;
  1011. else if (has_not_enough_free_secs(sbi, 0))
  1012. goto redirty_out;
  1013. err = -EAGAIN;
  1014. f2fs_lock_op(sbi);
  1015. if (f2fs_has_inline_data(inode))
  1016. err = f2fs_write_inline_data(inode, page);
  1017. if (err == -EAGAIN)
  1018. err = do_write_data_page(&fio);
  1019. f2fs_unlock_op(sbi);
  1020. done:
  1021. if (err && err != -ENOENT)
  1022. goto redirty_out;
  1023. clear_cold_data(page);
  1024. out:
  1025. inode_dec_dirty_pages(inode);
  1026. if (err)
  1027. ClearPageUptodate(page);
  1028. unlock_page(page);
  1029. if (need_balance_fs)
  1030. f2fs_balance_fs(sbi);
  1031. if (wbc->for_reclaim)
  1032. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  1033. return 0;
  1034. redirty_out:
  1035. redirty_page_for_writepage(wbc, page);
  1036. return AOP_WRITEPAGE_ACTIVATE;
  1037. }
  1038. static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
  1039. void *data)
  1040. {
  1041. struct address_space *mapping = data;
  1042. int ret = mapping->a_ops->writepage(page, wbc);
  1043. mapping_set_error(mapping, ret);
  1044. return ret;
  1045. }
  1046. /*
  1047. * This function was copied from write_cche_pages from mm/page-writeback.c.
  1048. * The major change is making write step of cold data page separately from
  1049. * warm/hot data page.
  1050. */
  1051. static int f2fs_write_cache_pages(struct address_space *mapping,
  1052. struct writeback_control *wbc, writepage_t writepage,
  1053. void *data)
  1054. {
  1055. int ret = 0;
  1056. int done = 0;
  1057. struct pagevec pvec;
  1058. int nr_pages;
  1059. pgoff_t uninitialized_var(writeback_index);
  1060. pgoff_t index;
  1061. pgoff_t end; /* Inclusive */
  1062. pgoff_t done_index;
  1063. int cycled;
  1064. int range_whole = 0;
  1065. int tag;
  1066. int step = 0;
  1067. pagevec_init(&pvec, 0);
  1068. next:
  1069. if (wbc->range_cyclic) {
  1070. writeback_index = mapping->writeback_index; /* prev offset */
  1071. index = writeback_index;
  1072. if (index == 0)
  1073. cycled = 1;
  1074. else
  1075. cycled = 0;
  1076. end = -1;
  1077. } else {
  1078. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  1079. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1080. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  1081. range_whole = 1;
  1082. cycled = 1; /* ignore range_cyclic tests */
  1083. }
  1084. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  1085. tag = PAGECACHE_TAG_TOWRITE;
  1086. else
  1087. tag = PAGECACHE_TAG_DIRTY;
  1088. retry:
  1089. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  1090. tag_pages_for_writeback(mapping, index, end);
  1091. done_index = index;
  1092. while (!done && (index <= end)) {
  1093. int i;
  1094. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  1095. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
  1096. if (nr_pages == 0)
  1097. break;
  1098. for (i = 0; i < nr_pages; i++) {
  1099. struct page *page = pvec.pages[i];
  1100. if (page->index > end) {
  1101. done = 1;
  1102. break;
  1103. }
  1104. done_index = page->index;
  1105. lock_page(page);
  1106. if (unlikely(page->mapping != mapping)) {
  1107. continue_unlock:
  1108. unlock_page(page);
  1109. continue;
  1110. }
  1111. if (!PageDirty(page)) {
  1112. /* someone wrote it for us */
  1113. goto continue_unlock;
  1114. }
  1115. if (step == is_cold_data(page))
  1116. goto continue_unlock;
  1117. if (PageWriteback(page)) {
  1118. if (wbc->sync_mode != WB_SYNC_NONE)
  1119. f2fs_wait_on_page_writeback(page, DATA);
  1120. else
  1121. goto continue_unlock;
  1122. }
  1123. BUG_ON(PageWriteback(page));
  1124. if (!clear_page_dirty_for_io(page))
  1125. goto continue_unlock;
  1126. ret = (*writepage)(page, wbc, data);
  1127. if (unlikely(ret)) {
  1128. if (ret == AOP_WRITEPAGE_ACTIVATE) {
  1129. unlock_page(page);
  1130. ret = 0;
  1131. } else {
  1132. done_index = page->index + 1;
  1133. done = 1;
  1134. break;
  1135. }
  1136. }
  1137. if (--wbc->nr_to_write <= 0 &&
  1138. wbc->sync_mode == WB_SYNC_NONE) {
  1139. done = 1;
  1140. break;
  1141. }
  1142. }
  1143. pagevec_release(&pvec);
  1144. cond_resched();
  1145. }
  1146. if (step < 1) {
  1147. step++;
  1148. goto next;
  1149. }
  1150. if (!cycled && !done) {
  1151. cycled = 1;
  1152. index = 0;
  1153. end = writeback_index - 1;
  1154. goto retry;
  1155. }
  1156. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  1157. mapping->writeback_index = done_index;
  1158. return ret;
  1159. }
  1160. static int f2fs_write_data_pages(struct address_space *mapping,
  1161. struct writeback_control *wbc)
  1162. {
  1163. struct inode *inode = mapping->host;
  1164. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1165. bool locked = false;
  1166. int ret;
  1167. long diff;
  1168. trace_f2fs_writepages(mapping->host, wbc, DATA);
  1169. /* deal with chardevs and other special file */
  1170. if (!mapping->a_ops->writepage)
  1171. return 0;
  1172. /* skip writing if there is no dirty page in this inode */
  1173. if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
  1174. return 0;
  1175. if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
  1176. get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
  1177. available_free_memory(sbi, DIRTY_DENTS))
  1178. goto skip_write;
  1179. /* during POR, we don't need to trigger writepage at all. */
  1180. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1181. goto skip_write;
  1182. diff = nr_pages_to_write(sbi, DATA, wbc);
  1183. if (!S_ISDIR(inode->i_mode)) {
  1184. mutex_lock(&sbi->writepages);
  1185. locked = true;
  1186. }
  1187. ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
  1188. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  1189. if (locked)
  1190. mutex_unlock(&sbi->writepages);
  1191. remove_dirty_dir_inode(inode);
  1192. wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
  1193. return ret;
  1194. skip_write:
  1195. wbc->pages_skipped += get_dirty_pages(inode);
  1196. return 0;
  1197. }
  1198. static void f2fs_write_failed(struct address_space *mapping, loff_t to)
  1199. {
  1200. struct inode *inode = mapping->host;
  1201. if (to > inode->i_size) {
  1202. truncate_pagecache(inode, inode->i_size);
  1203. truncate_blocks(inode, inode->i_size, true);
  1204. }
  1205. }
  1206. static int f2fs_write_begin(struct file *file, struct address_space *mapping,
  1207. loff_t pos, unsigned len, unsigned flags,
  1208. struct page **pagep, void **fsdata)
  1209. {
  1210. struct inode *inode = mapping->host;
  1211. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1212. struct page *page = NULL;
  1213. struct page *ipage;
  1214. pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
  1215. struct dnode_of_data dn;
  1216. int err = 0;
  1217. trace_f2fs_write_begin(inode, pos, len, flags);
  1218. f2fs_balance_fs(sbi);
  1219. /*
  1220. * We should check this at this moment to avoid deadlock on inode page
  1221. * and #0 page. The locking rule for inline_data conversion should be:
  1222. * lock_page(page #0) -> lock_page(inode_page)
  1223. */
  1224. if (index != 0) {
  1225. err = f2fs_convert_inline_inode(inode);
  1226. if (err)
  1227. goto fail;
  1228. }
  1229. repeat:
  1230. /*
  1231. * Do not use grab_cache_page_write_begin() to avoid deadlock due to
  1232. * wait_for_stable_page. Will wait that below with our IO control.
  1233. */
  1234. page = pagecache_get_page(mapping, index,
  1235. FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
  1236. if (!page) {
  1237. err = -ENOMEM;
  1238. goto fail;
  1239. }
  1240. *pagep = page;
  1241. f2fs_lock_op(sbi);
  1242. /* check inline_data */
  1243. ipage = get_node_page(sbi, inode->i_ino);
  1244. if (IS_ERR(ipage)) {
  1245. err = PTR_ERR(ipage);
  1246. goto unlock_fail;
  1247. }
  1248. set_new_dnode(&dn, inode, ipage, ipage, 0);
  1249. if (f2fs_has_inline_data(inode)) {
  1250. if (pos + len <= MAX_INLINE_DATA) {
  1251. read_inline_data(page, ipage);
  1252. set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
  1253. sync_inode_page(&dn);
  1254. goto put_next;
  1255. }
  1256. err = f2fs_convert_inline_page(&dn, page);
  1257. if (err)
  1258. goto put_fail;
  1259. }
  1260. err = f2fs_get_block(&dn, index);
  1261. if (err)
  1262. goto put_fail;
  1263. put_next:
  1264. f2fs_put_dnode(&dn);
  1265. f2fs_unlock_op(sbi);
  1266. f2fs_wait_on_page_writeback(page, DATA);
  1267. /* wait for GCed encrypted page writeback */
  1268. if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
  1269. f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
  1270. if (len == PAGE_CACHE_SIZE)
  1271. goto out_update;
  1272. if (PageUptodate(page))
  1273. goto out_clear;
  1274. if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
  1275. unsigned start = pos & (PAGE_CACHE_SIZE - 1);
  1276. unsigned end = start + len;
  1277. /* Reading beyond i_size is simple: memset to zero */
  1278. zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
  1279. goto out_update;
  1280. }
  1281. if (dn.data_blkaddr == NEW_ADDR) {
  1282. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  1283. } else {
  1284. struct bio *bio;
  1285. bio = f2fs_grab_bio(inode, dn.data_blkaddr, 1);
  1286. if (IS_ERR(bio)) {
  1287. err = PTR_ERR(bio);
  1288. goto fail;
  1289. }
  1290. if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
  1291. bio_put(bio);
  1292. err = -EFAULT;
  1293. goto fail;
  1294. }
  1295. submit_bio(READ_SYNC, bio);
  1296. lock_page(page);
  1297. if (unlikely(!PageUptodate(page))) {
  1298. err = -EIO;
  1299. goto fail;
  1300. }
  1301. if (unlikely(page->mapping != mapping)) {
  1302. f2fs_put_page(page, 1);
  1303. goto repeat;
  1304. }
  1305. }
  1306. out_update:
  1307. SetPageUptodate(page);
  1308. out_clear:
  1309. clear_cold_data(page);
  1310. return 0;
  1311. put_fail:
  1312. f2fs_put_dnode(&dn);
  1313. unlock_fail:
  1314. f2fs_unlock_op(sbi);
  1315. fail:
  1316. f2fs_put_page(page, 1);
  1317. f2fs_write_failed(mapping, pos + len);
  1318. return err;
  1319. }
  1320. static int f2fs_write_end(struct file *file,
  1321. struct address_space *mapping,
  1322. loff_t pos, unsigned len, unsigned copied,
  1323. struct page *page, void *fsdata)
  1324. {
  1325. struct inode *inode = page->mapping->host;
  1326. trace_f2fs_write_end(inode, pos, len, copied);
  1327. set_page_dirty(page);
  1328. if (pos + copied > i_size_read(inode)) {
  1329. i_size_write(inode, pos + copied);
  1330. mark_inode_dirty(inode);
  1331. update_inode_page(inode);
  1332. }
  1333. f2fs_put_page(page, 1);
  1334. return copied;
  1335. }
  1336. static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
  1337. loff_t offset)
  1338. {
  1339. unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
  1340. if (offset & blocksize_mask)
  1341. return -EINVAL;
  1342. if (iov_iter_alignment(iter) & blocksize_mask)
  1343. return -EINVAL;
  1344. return 0;
  1345. }
  1346. static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
  1347. loff_t offset)
  1348. {
  1349. struct file *file = iocb->ki_filp;
  1350. struct address_space *mapping = file->f_mapping;
  1351. struct inode *inode = mapping->host;
  1352. size_t count = iov_iter_count(iter);
  1353. int err;
  1354. /* we don't need to use inline_data strictly */
  1355. if (f2fs_has_inline_data(inode)) {
  1356. err = f2fs_convert_inline_inode(inode);
  1357. if (err)
  1358. return err;
  1359. }
  1360. if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
  1361. return 0;
  1362. err = check_direct_IO(inode, iter, offset);
  1363. if (err)
  1364. return err;
  1365. trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
  1366. if (iov_iter_rw(iter) == WRITE) {
  1367. __allocate_data_blocks(inode, offset, count);
  1368. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
  1369. err = -EIO;
  1370. goto out;
  1371. }
  1372. }
  1373. err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block_dio);
  1374. out:
  1375. if (err < 0 && iov_iter_rw(iter) == WRITE)
  1376. f2fs_write_failed(mapping, offset + count);
  1377. trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
  1378. return err;
  1379. }
  1380. void f2fs_invalidate_page(struct page *page, unsigned int offset,
  1381. unsigned int length)
  1382. {
  1383. struct inode *inode = page->mapping->host;
  1384. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1385. if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
  1386. (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
  1387. return;
  1388. if (PageDirty(page)) {
  1389. if (inode->i_ino == F2FS_META_INO(sbi))
  1390. dec_page_count(sbi, F2FS_DIRTY_META);
  1391. else if (inode->i_ino == F2FS_NODE_INO(sbi))
  1392. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1393. else
  1394. inode_dec_dirty_pages(inode);
  1395. }
  1396. /* This is atomic written page, keep Private */
  1397. if (IS_ATOMIC_WRITTEN_PAGE(page))
  1398. return;
  1399. ClearPagePrivate(page);
  1400. }
  1401. int f2fs_release_page(struct page *page, gfp_t wait)
  1402. {
  1403. /* If this is dirty page, keep PagePrivate */
  1404. if (PageDirty(page))
  1405. return 0;
  1406. /* This is atomic written page, keep Private */
  1407. if (IS_ATOMIC_WRITTEN_PAGE(page))
  1408. return 0;
  1409. ClearPagePrivate(page);
  1410. return 1;
  1411. }
  1412. static int f2fs_set_data_page_dirty(struct page *page)
  1413. {
  1414. struct address_space *mapping = page->mapping;
  1415. struct inode *inode = mapping->host;
  1416. trace_f2fs_set_page_dirty(page, DATA);
  1417. SetPageUptodate(page);
  1418. if (f2fs_is_atomic_file(inode)) {
  1419. if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
  1420. register_inmem_page(inode, page);
  1421. return 1;
  1422. }
  1423. /*
  1424. * Previously, this page has been registered, we just
  1425. * return here.
  1426. */
  1427. return 0;
  1428. }
  1429. if (!PageDirty(page)) {
  1430. __set_page_dirty_nobuffers(page);
  1431. update_dirty_page(inode, page);
  1432. return 1;
  1433. }
  1434. return 0;
  1435. }
  1436. static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
  1437. {
  1438. struct inode *inode = mapping->host;
  1439. if (f2fs_has_inline_data(inode))
  1440. return 0;
  1441. /* make sure allocating whole blocks */
  1442. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  1443. filemap_write_and_wait(mapping);
  1444. return generic_block_bmap(mapping, block, get_data_block_bmap);
  1445. }
  1446. const struct address_space_operations f2fs_dblock_aops = {
  1447. .readpage = f2fs_read_data_page,
  1448. .readpages = f2fs_read_data_pages,
  1449. .writepage = f2fs_write_data_page,
  1450. .writepages = f2fs_write_data_pages,
  1451. .write_begin = f2fs_write_begin,
  1452. .write_end = f2fs_write_end,
  1453. .set_page_dirty = f2fs_set_data_page_dirty,
  1454. .invalidatepage = f2fs_invalidate_page,
  1455. .releasepage = f2fs_release_page,
  1456. .direct_IO = f2fs_direct_IO,
  1457. .bmap = f2fs_bmap,
  1458. };