data.c 46 KB

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