file.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104
  1. /*
  2. * fs/f2fs/file.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/stat.h>
  14. #include <linux/buffer_head.h>
  15. #include <linux/writeback.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/falloc.h>
  18. #include <linux/types.h>
  19. #include <linux/compat.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/mount.h>
  22. #include <linux/pagevec.h>
  23. #include <linux/uio.h>
  24. #include <linux/uuid.h>
  25. #include <linux/file.h>
  26. #include "f2fs.h"
  27. #include "node.h"
  28. #include "segment.h"
  29. #include "xattr.h"
  30. #include "acl.h"
  31. #include "gc.h"
  32. #include "trace.h"
  33. #include <trace/events/f2fs.h>
  34. static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
  35. {
  36. struct inode *inode = file_inode(vmf->vma->vm_file);
  37. vm_fault_t ret;
  38. down_read(&F2FS_I(inode)->i_mmap_sem);
  39. ret = filemap_fault(vmf);
  40. up_read(&F2FS_I(inode)->i_mmap_sem);
  41. return ret;
  42. }
  43. static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
  44. {
  45. struct page *page = vmf->page;
  46. struct inode *inode = file_inode(vmf->vma->vm_file);
  47. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  48. struct dnode_of_data dn;
  49. int err;
  50. if (unlikely(f2fs_cp_error(sbi))) {
  51. err = -EIO;
  52. goto err;
  53. }
  54. sb_start_pagefault(inode->i_sb);
  55. f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
  56. /* block allocation */
  57. f2fs_lock_op(sbi);
  58. set_new_dnode(&dn, inode, NULL, NULL, 0);
  59. err = f2fs_reserve_block(&dn, page->index);
  60. if (err) {
  61. f2fs_unlock_op(sbi);
  62. goto out;
  63. }
  64. f2fs_put_dnode(&dn);
  65. f2fs_unlock_op(sbi);
  66. f2fs_balance_fs(sbi, dn.node_changed);
  67. file_update_time(vmf->vma->vm_file);
  68. down_read(&F2FS_I(inode)->i_mmap_sem);
  69. lock_page(page);
  70. if (unlikely(page->mapping != inode->i_mapping ||
  71. page_offset(page) > i_size_read(inode) ||
  72. !PageUptodate(page))) {
  73. unlock_page(page);
  74. err = -EFAULT;
  75. goto out_sem;
  76. }
  77. /*
  78. * check to see if the page is mapped already (no holes)
  79. */
  80. if (PageMappedToDisk(page))
  81. goto mapped;
  82. /* page is wholly or partially inside EOF */
  83. if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
  84. i_size_read(inode)) {
  85. loff_t offset;
  86. offset = i_size_read(inode) & ~PAGE_MASK;
  87. zero_user_segment(page, offset, PAGE_SIZE);
  88. }
  89. set_page_dirty(page);
  90. if (!PageUptodate(page))
  91. SetPageUptodate(page);
  92. f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
  93. trace_f2fs_vm_page_mkwrite(page, DATA);
  94. mapped:
  95. /* fill the page */
  96. f2fs_wait_on_page_writeback(page, DATA, false);
  97. /* wait for GCed page writeback via META_MAPPING */
  98. f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
  99. out_sem:
  100. up_read(&F2FS_I(inode)->i_mmap_sem);
  101. out:
  102. sb_end_pagefault(inode->i_sb);
  103. f2fs_update_time(sbi, REQ_TIME);
  104. err:
  105. return block_page_mkwrite_return(err);
  106. }
  107. static const struct vm_operations_struct f2fs_file_vm_ops = {
  108. .fault = f2fs_filemap_fault,
  109. .map_pages = filemap_map_pages,
  110. .page_mkwrite = f2fs_vm_page_mkwrite,
  111. };
  112. static int get_parent_ino(struct inode *inode, nid_t *pino)
  113. {
  114. struct dentry *dentry;
  115. inode = igrab(inode);
  116. dentry = d_find_any_alias(inode);
  117. iput(inode);
  118. if (!dentry)
  119. return 0;
  120. *pino = parent_ino(dentry);
  121. dput(dentry);
  122. return 1;
  123. }
  124. static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
  125. {
  126. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  127. enum cp_reason_type cp_reason = CP_NO_NEEDED;
  128. if (!S_ISREG(inode->i_mode))
  129. cp_reason = CP_NON_REGULAR;
  130. else if (inode->i_nlink != 1)
  131. cp_reason = CP_HARDLINK;
  132. else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
  133. cp_reason = CP_SB_NEED_CP;
  134. else if (file_wrong_pino(inode))
  135. cp_reason = CP_WRONG_PINO;
  136. else if (!f2fs_space_for_roll_forward(sbi))
  137. cp_reason = CP_NO_SPC_ROLL;
  138. else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
  139. cp_reason = CP_NODE_NEED_CP;
  140. else if (test_opt(sbi, FASTBOOT))
  141. cp_reason = CP_FASTBOOT_MODE;
  142. else if (F2FS_OPTION(sbi).active_logs == 2)
  143. cp_reason = CP_SPEC_LOG_NUM;
  144. else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
  145. f2fs_need_dentry_mark(sbi, inode->i_ino) &&
  146. f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
  147. TRANS_DIR_INO))
  148. cp_reason = CP_RECOVER_DIR;
  149. return cp_reason;
  150. }
  151. static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
  152. {
  153. struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
  154. bool ret = false;
  155. /* But we need to avoid that there are some inode updates */
  156. if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
  157. ret = true;
  158. f2fs_put_page(i, 0);
  159. return ret;
  160. }
  161. static void try_to_fix_pino(struct inode *inode)
  162. {
  163. struct f2fs_inode_info *fi = F2FS_I(inode);
  164. nid_t pino;
  165. down_write(&fi->i_sem);
  166. if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
  167. get_parent_ino(inode, &pino)) {
  168. f2fs_i_pino_write(inode, pino);
  169. file_got_pino(inode);
  170. }
  171. up_write(&fi->i_sem);
  172. }
  173. static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
  174. int datasync, bool atomic)
  175. {
  176. struct inode *inode = file->f_mapping->host;
  177. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  178. nid_t ino = inode->i_ino;
  179. int ret = 0;
  180. enum cp_reason_type cp_reason = 0;
  181. struct writeback_control wbc = {
  182. .sync_mode = WB_SYNC_ALL,
  183. .nr_to_write = LONG_MAX,
  184. .for_reclaim = 0,
  185. };
  186. unsigned int seq_id = 0;
  187. if (unlikely(f2fs_readonly(inode->i_sb)))
  188. return 0;
  189. trace_f2fs_sync_file_enter(inode);
  190. if (S_ISDIR(inode->i_mode))
  191. goto go_write;
  192. /* if fdatasync is triggered, let's do in-place-update */
  193. if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
  194. set_inode_flag(inode, FI_NEED_IPU);
  195. ret = file_write_and_wait_range(file, start, end);
  196. clear_inode_flag(inode, FI_NEED_IPU);
  197. if (ret) {
  198. trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
  199. return ret;
  200. }
  201. /* if the inode is dirty, let's recover all the time */
  202. if (!f2fs_skip_inode_update(inode, datasync)) {
  203. f2fs_write_inode(inode, NULL);
  204. goto go_write;
  205. }
  206. /*
  207. * if there is no written data, don't waste time to write recovery info.
  208. */
  209. if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
  210. !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
  211. /* it may call write_inode just prior to fsync */
  212. if (need_inode_page_update(sbi, ino))
  213. goto go_write;
  214. if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
  215. f2fs_exist_written_data(sbi, ino, UPDATE_INO))
  216. goto flush_out;
  217. goto out;
  218. }
  219. go_write:
  220. /*
  221. * Both of fdatasync() and fsync() are able to be recovered from
  222. * sudden-power-off.
  223. */
  224. down_read(&F2FS_I(inode)->i_sem);
  225. cp_reason = need_do_checkpoint(inode);
  226. up_read(&F2FS_I(inode)->i_sem);
  227. if (cp_reason) {
  228. /* all the dirty node pages should be flushed for POR */
  229. ret = f2fs_sync_fs(inode->i_sb, 1);
  230. /*
  231. * We've secured consistency through sync_fs. Following pino
  232. * will be used only for fsynced inodes after checkpoint.
  233. */
  234. try_to_fix_pino(inode);
  235. clear_inode_flag(inode, FI_APPEND_WRITE);
  236. clear_inode_flag(inode, FI_UPDATE_WRITE);
  237. goto out;
  238. }
  239. sync_nodes:
  240. atomic_inc(&sbi->wb_sync_req[NODE]);
  241. ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
  242. atomic_dec(&sbi->wb_sync_req[NODE]);
  243. if (ret)
  244. goto out;
  245. /* if cp_error was enabled, we should avoid infinite loop */
  246. if (unlikely(f2fs_cp_error(sbi))) {
  247. ret = -EIO;
  248. goto out;
  249. }
  250. if (f2fs_need_inode_block_update(sbi, ino)) {
  251. f2fs_mark_inode_dirty_sync(inode, true);
  252. f2fs_write_inode(inode, NULL);
  253. goto sync_nodes;
  254. }
  255. /*
  256. * If it's atomic_write, it's just fine to keep write ordering. So
  257. * here we don't need to wait for node write completion, since we use
  258. * node chain which serializes node blocks. If one of node writes are
  259. * reordered, we can see simply broken chain, resulting in stopping
  260. * roll-forward recovery. It means we'll recover all or none node blocks
  261. * given fsync mark.
  262. */
  263. if (!atomic) {
  264. ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
  265. if (ret)
  266. goto out;
  267. }
  268. /* once recovery info is written, don't need to tack this */
  269. f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
  270. clear_inode_flag(inode, FI_APPEND_WRITE);
  271. flush_out:
  272. if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
  273. ret = f2fs_issue_flush(sbi, inode->i_ino);
  274. if (!ret) {
  275. f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
  276. clear_inode_flag(inode, FI_UPDATE_WRITE);
  277. f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
  278. }
  279. f2fs_update_time(sbi, REQ_TIME);
  280. out:
  281. trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
  282. f2fs_trace_ios(NULL, 1);
  283. return ret;
  284. }
  285. int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  286. {
  287. if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
  288. return -EIO;
  289. return f2fs_do_sync_file(file, start, end, datasync, false);
  290. }
  291. static pgoff_t __get_first_dirty_index(struct address_space *mapping,
  292. pgoff_t pgofs, int whence)
  293. {
  294. struct page *page;
  295. int nr_pages;
  296. if (whence != SEEK_DATA)
  297. return 0;
  298. /* find first dirty page index */
  299. nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
  300. 1, &page);
  301. if (!nr_pages)
  302. return ULONG_MAX;
  303. pgofs = page->index;
  304. put_page(page);
  305. return pgofs;
  306. }
  307. static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
  308. pgoff_t dirty, pgoff_t pgofs, int whence)
  309. {
  310. switch (whence) {
  311. case SEEK_DATA:
  312. if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
  313. is_valid_data_blkaddr(sbi, blkaddr))
  314. return true;
  315. break;
  316. case SEEK_HOLE:
  317. if (blkaddr == NULL_ADDR)
  318. return true;
  319. break;
  320. }
  321. return false;
  322. }
  323. static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
  324. {
  325. struct inode *inode = file->f_mapping->host;
  326. loff_t maxbytes = inode->i_sb->s_maxbytes;
  327. struct dnode_of_data dn;
  328. pgoff_t pgofs, end_offset, dirty;
  329. loff_t data_ofs = offset;
  330. loff_t isize;
  331. int err = 0;
  332. inode_lock(inode);
  333. isize = i_size_read(inode);
  334. if (offset >= isize)
  335. goto fail;
  336. /* handle inline data case */
  337. if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
  338. if (whence == SEEK_HOLE)
  339. data_ofs = isize;
  340. goto found;
  341. }
  342. pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
  343. dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
  344. for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
  345. set_new_dnode(&dn, inode, NULL, NULL, 0);
  346. err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
  347. if (err && err != -ENOENT) {
  348. goto fail;
  349. } else if (err == -ENOENT) {
  350. /* direct node does not exists */
  351. if (whence == SEEK_DATA) {
  352. pgofs = f2fs_get_next_page_offset(&dn, pgofs);
  353. continue;
  354. } else {
  355. goto found;
  356. }
  357. }
  358. end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
  359. /* find data/hole in dnode block */
  360. for (; dn.ofs_in_node < end_offset;
  361. dn.ofs_in_node++, pgofs++,
  362. data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
  363. block_t blkaddr;
  364. blkaddr = datablock_addr(dn.inode,
  365. dn.node_page, dn.ofs_in_node);
  366. if (__is_valid_data_blkaddr(blkaddr) &&
  367. !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
  368. blkaddr, DATA_GENERIC)) {
  369. f2fs_put_dnode(&dn);
  370. goto fail;
  371. }
  372. if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
  373. pgofs, whence)) {
  374. f2fs_put_dnode(&dn);
  375. goto found;
  376. }
  377. }
  378. f2fs_put_dnode(&dn);
  379. }
  380. if (whence == SEEK_DATA)
  381. goto fail;
  382. found:
  383. if (whence == SEEK_HOLE && data_ofs > isize)
  384. data_ofs = isize;
  385. inode_unlock(inode);
  386. return vfs_setpos(file, data_ofs, maxbytes);
  387. fail:
  388. inode_unlock(inode);
  389. return -ENXIO;
  390. }
  391. static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
  392. {
  393. struct inode *inode = file->f_mapping->host;
  394. loff_t maxbytes = inode->i_sb->s_maxbytes;
  395. switch (whence) {
  396. case SEEK_SET:
  397. case SEEK_CUR:
  398. case SEEK_END:
  399. return generic_file_llseek_size(file, offset, whence,
  400. maxbytes, i_size_read(inode));
  401. case SEEK_DATA:
  402. case SEEK_HOLE:
  403. if (offset < 0)
  404. return -ENXIO;
  405. return f2fs_seek_block(file, offset, whence);
  406. }
  407. return -EINVAL;
  408. }
  409. static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
  410. {
  411. struct inode *inode = file_inode(file);
  412. int err;
  413. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
  414. return -EIO;
  415. /* we don't need to use inline_data strictly */
  416. err = f2fs_convert_inline_inode(inode);
  417. if (err)
  418. return err;
  419. file_accessed(file);
  420. vma->vm_ops = &f2fs_file_vm_ops;
  421. return 0;
  422. }
  423. static int f2fs_file_open(struct inode *inode, struct file *filp)
  424. {
  425. int err = fscrypt_file_open(inode, filp);
  426. if (err)
  427. return err;
  428. filp->f_mode |= FMODE_NOWAIT;
  429. return dquot_file_open(inode, filp);
  430. }
  431. void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
  432. {
  433. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  434. struct f2fs_node *raw_node;
  435. int nr_free = 0, ofs = dn->ofs_in_node, len = count;
  436. __le32 *addr;
  437. int base = 0;
  438. if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
  439. base = get_extra_isize(dn->inode);
  440. raw_node = F2FS_NODE(dn->node_page);
  441. addr = blkaddr_in_node(raw_node) + base + ofs;
  442. for (; count > 0; count--, addr++, dn->ofs_in_node++) {
  443. block_t blkaddr = le32_to_cpu(*addr);
  444. if (blkaddr == NULL_ADDR)
  445. continue;
  446. dn->data_blkaddr = NULL_ADDR;
  447. f2fs_set_data_blkaddr(dn);
  448. if (__is_valid_data_blkaddr(blkaddr) &&
  449. !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
  450. continue;
  451. f2fs_invalidate_blocks(sbi, blkaddr);
  452. if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
  453. clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
  454. nr_free++;
  455. }
  456. if (nr_free) {
  457. pgoff_t fofs;
  458. /*
  459. * once we invalidate valid blkaddr in range [ofs, ofs + count],
  460. * we will invalidate all blkaddr in the whole range.
  461. */
  462. fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
  463. dn->inode) + ofs;
  464. f2fs_update_extent_cache_range(dn, fofs, 0, len);
  465. dec_valid_block_count(sbi, dn->inode, nr_free);
  466. }
  467. dn->ofs_in_node = ofs;
  468. f2fs_update_time(sbi, REQ_TIME);
  469. trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
  470. dn->ofs_in_node, nr_free);
  471. }
  472. void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
  473. {
  474. f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
  475. }
  476. static int truncate_partial_data_page(struct inode *inode, u64 from,
  477. bool cache_only)
  478. {
  479. loff_t offset = from & (PAGE_SIZE - 1);
  480. pgoff_t index = from >> PAGE_SHIFT;
  481. struct address_space *mapping = inode->i_mapping;
  482. struct page *page;
  483. if (!offset && !cache_only)
  484. return 0;
  485. if (cache_only) {
  486. page = find_lock_page(mapping, index);
  487. if (page && PageUptodate(page))
  488. goto truncate_out;
  489. f2fs_put_page(page, 1);
  490. return 0;
  491. }
  492. page = f2fs_get_lock_data_page(inode, index, true);
  493. if (IS_ERR(page))
  494. return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
  495. truncate_out:
  496. f2fs_wait_on_page_writeback(page, DATA, true);
  497. zero_user(page, offset, PAGE_SIZE - offset);
  498. /* An encrypted inode should have a key and truncate the last page. */
  499. f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
  500. if (!cache_only)
  501. set_page_dirty(page);
  502. f2fs_put_page(page, 1);
  503. return 0;
  504. }
  505. int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
  506. {
  507. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  508. struct dnode_of_data dn;
  509. pgoff_t free_from;
  510. int count = 0, err = 0;
  511. struct page *ipage;
  512. bool truncate_page = false;
  513. trace_f2fs_truncate_blocks_enter(inode, from);
  514. free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
  515. if (free_from >= sbi->max_file_blocks)
  516. goto free_partial;
  517. if (lock)
  518. f2fs_lock_op(sbi);
  519. ipage = f2fs_get_node_page(sbi, inode->i_ino);
  520. if (IS_ERR(ipage)) {
  521. err = PTR_ERR(ipage);
  522. goto out;
  523. }
  524. if (f2fs_has_inline_data(inode)) {
  525. f2fs_truncate_inline_inode(inode, ipage, from);
  526. f2fs_put_page(ipage, 1);
  527. truncate_page = true;
  528. goto out;
  529. }
  530. set_new_dnode(&dn, inode, ipage, NULL, 0);
  531. err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
  532. if (err) {
  533. if (err == -ENOENT)
  534. goto free_next;
  535. goto out;
  536. }
  537. count = ADDRS_PER_PAGE(dn.node_page, inode);
  538. count -= dn.ofs_in_node;
  539. f2fs_bug_on(sbi, count < 0);
  540. if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
  541. f2fs_truncate_data_blocks_range(&dn, count);
  542. free_from += count;
  543. }
  544. f2fs_put_dnode(&dn);
  545. free_next:
  546. err = f2fs_truncate_inode_blocks(inode, free_from);
  547. out:
  548. if (lock)
  549. f2fs_unlock_op(sbi);
  550. free_partial:
  551. /* lastly zero out the first data page */
  552. if (!err)
  553. err = truncate_partial_data_page(inode, from, truncate_page);
  554. trace_f2fs_truncate_blocks_exit(inode, err);
  555. return err;
  556. }
  557. int f2fs_truncate(struct inode *inode)
  558. {
  559. int err;
  560. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
  561. return -EIO;
  562. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  563. S_ISLNK(inode->i_mode)))
  564. return 0;
  565. trace_f2fs_truncate(inode);
  566. if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
  567. f2fs_show_injection_info(FAULT_TRUNCATE);
  568. return -EIO;
  569. }
  570. /* we should check inline_data size */
  571. if (!f2fs_may_inline_data(inode)) {
  572. err = f2fs_convert_inline_inode(inode);
  573. if (err)
  574. return err;
  575. }
  576. err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
  577. if (err)
  578. return err;
  579. inode->i_mtime = inode->i_ctime = current_time(inode);
  580. f2fs_mark_inode_dirty_sync(inode, false);
  581. return 0;
  582. }
  583. int f2fs_getattr(const struct path *path, struct kstat *stat,
  584. u32 request_mask, unsigned int query_flags)
  585. {
  586. struct inode *inode = d_inode(path->dentry);
  587. struct f2fs_inode_info *fi = F2FS_I(inode);
  588. struct f2fs_inode *ri;
  589. unsigned int flags;
  590. if (f2fs_has_extra_attr(inode) &&
  591. f2fs_sb_has_inode_crtime(inode->i_sb) &&
  592. F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
  593. stat->result_mask |= STATX_BTIME;
  594. stat->btime.tv_sec = fi->i_crtime.tv_sec;
  595. stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
  596. }
  597. flags = fi->i_flags & F2FS_FL_USER_VISIBLE;
  598. if (flags & F2FS_APPEND_FL)
  599. stat->attributes |= STATX_ATTR_APPEND;
  600. if (flags & F2FS_COMPR_FL)
  601. stat->attributes |= STATX_ATTR_COMPRESSED;
  602. if (f2fs_encrypted_inode(inode))
  603. stat->attributes |= STATX_ATTR_ENCRYPTED;
  604. if (flags & F2FS_IMMUTABLE_FL)
  605. stat->attributes |= STATX_ATTR_IMMUTABLE;
  606. if (flags & F2FS_NODUMP_FL)
  607. stat->attributes |= STATX_ATTR_NODUMP;
  608. stat->attributes_mask |= (STATX_ATTR_APPEND |
  609. STATX_ATTR_COMPRESSED |
  610. STATX_ATTR_ENCRYPTED |
  611. STATX_ATTR_IMMUTABLE |
  612. STATX_ATTR_NODUMP);
  613. generic_fillattr(inode, stat);
  614. /* we need to show initial sectors used for inline_data/dentries */
  615. if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
  616. f2fs_has_inline_dentry(inode))
  617. stat->blocks += (stat->size + 511) >> 9;
  618. return 0;
  619. }
  620. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  621. static void __setattr_copy(struct inode *inode, const struct iattr *attr)
  622. {
  623. unsigned int ia_valid = attr->ia_valid;
  624. if (ia_valid & ATTR_UID)
  625. inode->i_uid = attr->ia_uid;
  626. if (ia_valid & ATTR_GID)
  627. inode->i_gid = attr->ia_gid;
  628. if (ia_valid & ATTR_ATIME)
  629. inode->i_atime = timespec64_trunc(attr->ia_atime,
  630. inode->i_sb->s_time_gran);
  631. if (ia_valid & ATTR_MTIME)
  632. inode->i_mtime = timespec64_trunc(attr->ia_mtime,
  633. inode->i_sb->s_time_gran);
  634. if (ia_valid & ATTR_CTIME)
  635. inode->i_ctime = timespec64_trunc(attr->ia_ctime,
  636. inode->i_sb->s_time_gran);
  637. if (ia_valid & ATTR_MODE) {
  638. umode_t mode = attr->ia_mode;
  639. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  640. mode &= ~S_ISGID;
  641. set_acl_inode(inode, mode);
  642. }
  643. }
  644. #else
  645. #define __setattr_copy setattr_copy
  646. #endif
  647. int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
  648. {
  649. struct inode *inode = d_inode(dentry);
  650. int err;
  651. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
  652. return -EIO;
  653. err = setattr_prepare(dentry, attr);
  654. if (err)
  655. return err;
  656. err = fscrypt_prepare_setattr(dentry, attr);
  657. if (err)
  658. return err;
  659. if (is_quota_modification(inode, attr)) {
  660. err = dquot_initialize(inode);
  661. if (err)
  662. return err;
  663. }
  664. if ((attr->ia_valid & ATTR_UID &&
  665. !uid_eq(attr->ia_uid, inode->i_uid)) ||
  666. (attr->ia_valid & ATTR_GID &&
  667. !gid_eq(attr->ia_gid, inode->i_gid))) {
  668. err = dquot_transfer(inode, attr);
  669. if (err)
  670. return err;
  671. }
  672. if (attr->ia_valid & ATTR_SIZE) {
  673. bool to_smaller = (attr->ia_size <= i_size_read(inode));
  674. down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  675. down_write(&F2FS_I(inode)->i_mmap_sem);
  676. truncate_setsize(inode, attr->ia_size);
  677. if (to_smaller)
  678. err = f2fs_truncate(inode);
  679. /*
  680. * do not trim all blocks after i_size if target size is
  681. * larger than i_size.
  682. */
  683. up_write(&F2FS_I(inode)->i_mmap_sem);
  684. up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  685. if (err)
  686. return err;
  687. if (!to_smaller) {
  688. /* should convert inline inode here */
  689. if (!f2fs_may_inline_data(inode)) {
  690. err = f2fs_convert_inline_inode(inode);
  691. if (err)
  692. return err;
  693. }
  694. inode->i_mtime = inode->i_ctime = current_time(inode);
  695. }
  696. down_write(&F2FS_I(inode)->i_sem);
  697. F2FS_I(inode)->last_disk_size = i_size_read(inode);
  698. up_write(&F2FS_I(inode)->i_sem);
  699. }
  700. __setattr_copy(inode, attr);
  701. if (attr->ia_valid & ATTR_MODE) {
  702. err = posix_acl_chmod(inode, f2fs_get_inode_mode(inode));
  703. if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
  704. inode->i_mode = F2FS_I(inode)->i_acl_mode;
  705. clear_inode_flag(inode, FI_ACL_MODE);
  706. }
  707. }
  708. /* file size may changed here */
  709. f2fs_mark_inode_dirty_sync(inode, true);
  710. /* inode change will produce dirty node pages flushed by checkpoint */
  711. f2fs_balance_fs(F2FS_I_SB(inode), true);
  712. return err;
  713. }
  714. const struct inode_operations f2fs_file_inode_operations = {
  715. .getattr = f2fs_getattr,
  716. .setattr = f2fs_setattr,
  717. .get_acl = f2fs_get_acl,
  718. .set_acl = f2fs_set_acl,
  719. #ifdef CONFIG_F2FS_FS_XATTR
  720. .listxattr = f2fs_listxattr,
  721. #endif
  722. .fiemap = f2fs_fiemap,
  723. };
  724. static int fill_zero(struct inode *inode, pgoff_t index,
  725. loff_t start, loff_t len)
  726. {
  727. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  728. struct page *page;
  729. if (!len)
  730. return 0;
  731. f2fs_balance_fs(sbi, true);
  732. f2fs_lock_op(sbi);
  733. page = f2fs_get_new_data_page(inode, NULL, index, false);
  734. f2fs_unlock_op(sbi);
  735. if (IS_ERR(page))
  736. return PTR_ERR(page);
  737. f2fs_wait_on_page_writeback(page, DATA, true);
  738. zero_user(page, start, len);
  739. set_page_dirty(page);
  740. f2fs_put_page(page, 1);
  741. return 0;
  742. }
  743. int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
  744. {
  745. int err;
  746. while (pg_start < pg_end) {
  747. struct dnode_of_data dn;
  748. pgoff_t end_offset, count;
  749. set_new_dnode(&dn, inode, NULL, NULL, 0);
  750. err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
  751. if (err) {
  752. if (err == -ENOENT) {
  753. pg_start = f2fs_get_next_page_offset(&dn,
  754. pg_start);
  755. continue;
  756. }
  757. return err;
  758. }
  759. end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
  760. count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
  761. f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
  762. f2fs_truncate_data_blocks_range(&dn, count);
  763. f2fs_put_dnode(&dn);
  764. pg_start += count;
  765. }
  766. return 0;
  767. }
  768. static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
  769. {
  770. pgoff_t pg_start, pg_end;
  771. loff_t off_start, off_end;
  772. int ret;
  773. ret = f2fs_convert_inline_inode(inode);
  774. if (ret)
  775. return ret;
  776. pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
  777. pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
  778. off_start = offset & (PAGE_SIZE - 1);
  779. off_end = (offset + len) & (PAGE_SIZE - 1);
  780. if (pg_start == pg_end) {
  781. ret = fill_zero(inode, pg_start, off_start,
  782. off_end - off_start);
  783. if (ret)
  784. return ret;
  785. } else {
  786. if (off_start) {
  787. ret = fill_zero(inode, pg_start++, off_start,
  788. PAGE_SIZE - off_start);
  789. if (ret)
  790. return ret;
  791. }
  792. if (off_end) {
  793. ret = fill_zero(inode, pg_end, 0, off_end);
  794. if (ret)
  795. return ret;
  796. }
  797. if (pg_start < pg_end) {
  798. struct address_space *mapping = inode->i_mapping;
  799. loff_t blk_start, blk_end;
  800. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  801. f2fs_balance_fs(sbi, true);
  802. blk_start = (loff_t)pg_start << PAGE_SHIFT;
  803. blk_end = (loff_t)pg_end << PAGE_SHIFT;
  804. down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  805. down_write(&F2FS_I(inode)->i_mmap_sem);
  806. truncate_inode_pages_range(mapping, blk_start,
  807. blk_end - 1);
  808. f2fs_lock_op(sbi);
  809. ret = f2fs_truncate_hole(inode, pg_start, pg_end);
  810. f2fs_unlock_op(sbi);
  811. up_write(&F2FS_I(inode)->i_mmap_sem);
  812. up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  813. }
  814. }
  815. return ret;
  816. }
  817. static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
  818. int *do_replace, pgoff_t off, pgoff_t len)
  819. {
  820. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  821. struct dnode_of_data dn;
  822. int ret, done, i;
  823. next_dnode:
  824. set_new_dnode(&dn, inode, NULL, NULL, 0);
  825. ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
  826. if (ret && ret != -ENOENT) {
  827. return ret;
  828. } else if (ret == -ENOENT) {
  829. if (dn.max_level == 0)
  830. return -ENOENT;
  831. done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
  832. blkaddr += done;
  833. do_replace += done;
  834. goto next;
  835. }
  836. done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
  837. dn.ofs_in_node, len);
  838. for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
  839. *blkaddr = datablock_addr(dn.inode,
  840. dn.node_page, dn.ofs_in_node);
  841. if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
  842. if (test_opt(sbi, LFS)) {
  843. f2fs_put_dnode(&dn);
  844. return -ENOTSUPP;
  845. }
  846. /* do not invalidate this block address */
  847. f2fs_update_data_blkaddr(&dn, NULL_ADDR);
  848. *do_replace = 1;
  849. }
  850. }
  851. f2fs_put_dnode(&dn);
  852. next:
  853. len -= done;
  854. off += done;
  855. if (len)
  856. goto next_dnode;
  857. return 0;
  858. }
  859. static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
  860. int *do_replace, pgoff_t off, int len)
  861. {
  862. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  863. struct dnode_of_data dn;
  864. int ret, i;
  865. for (i = 0; i < len; i++, do_replace++, blkaddr++) {
  866. if (*do_replace == 0)
  867. continue;
  868. set_new_dnode(&dn, inode, NULL, NULL, 0);
  869. ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
  870. if (ret) {
  871. dec_valid_block_count(sbi, inode, 1);
  872. f2fs_invalidate_blocks(sbi, *blkaddr);
  873. } else {
  874. f2fs_update_data_blkaddr(&dn, *blkaddr);
  875. }
  876. f2fs_put_dnode(&dn);
  877. }
  878. return 0;
  879. }
  880. static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
  881. block_t *blkaddr, int *do_replace,
  882. pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
  883. {
  884. struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
  885. pgoff_t i = 0;
  886. int ret;
  887. while (i < len) {
  888. if (blkaddr[i] == NULL_ADDR && !full) {
  889. i++;
  890. continue;
  891. }
  892. if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
  893. struct dnode_of_data dn;
  894. struct node_info ni;
  895. size_t new_size;
  896. pgoff_t ilen;
  897. set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
  898. ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
  899. if (ret)
  900. return ret;
  901. ret = f2fs_get_node_info(sbi, dn.nid, &ni);
  902. if (ret) {
  903. f2fs_put_dnode(&dn);
  904. return ret;
  905. }
  906. ilen = min((pgoff_t)
  907. ADDRS_PER_PAGE(dn.node_page, dst_inode) -
  908. dn.ofs_in_node, len - i);
  909. do {
  910. dn.data_blkaddr = datablock_addr(dn.inode,
  911. dn.node_page, dn.ofs_in_node);
  912. f2fs_truncate_data_blocks_range(&dn, 1);
  913. if (do_replace[i]) {
  914. f2fs_i_blocks_write(src_inode,
  915. 1, false, false);
  916. f2fs_i_blocks_write(dst_inode,
  917. 1, true, false);
  918. f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
  919. blkaddr[i], ni.version, true, false);
  920. do_replace[i] = 0;
  921. }
  922. dn.ofs_in_node++;
  923. i++;
  924. new_size = (loff_t)(dst + i) << PAGE_SHIFT;
  925. if (dst_inode->i_size < new_size)
  926. f2fs_i_size_write(dst_inode, new_size);
  927. } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
  928. f2fs_put_dnode(&dn);
  929. } else {
  930. struct page *psrc, *pdst;
  931. psrc = f2fs_get_lock_data_page(src_inode,
  932. src + i, true);
  933. if (IS_ERR(psrc))
  934. return PTR_ERR(psrc);
  935. pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
  936. true);
  937. if (IS_ERR(pdst)) {
  938. f2fs_put_page(psrc, 1);
  939. return PTR_ERR(pdst);
  940. }
  941. f2fs_copy_page(psrc, pdst);
  942. set_page_dirty(pdst);
  943. f2fs_put_page(pdst, 1);
  944. f2fs_put_page(psrc, 1);
  945. ret = f2fs_truncate_hole(src_inode,
  946. src + i, src + i + 1);
  947. if (ret)
  948. return ret;
  949. i++;
  950. }
  951. }
  952. return 0;
  953. }
  954. static int __exchange_data_block(struct inode *src_inode,
  955. struct inode *dst_inode, pgoff_t src, pgoff_t dst,
  956. pgoff_t len, bool full)
  957. {
  958. block_t *src_blkaddr;
  959. int *do_replace;
  960. pgoff_t olen;
  961. int ret;
  962. while (len) {
  963. olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
  964. src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
  965. array_size(olen, sizeof(block_t)),
  966. GFP_KERNEL);
  967. if (!src_blkaddr)
  968. return -ENOMEM;
  969. do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
  970. array_size(olen, sizeof(int)),
  971. GFP_KERNEL);
  972. if (!do_replace) {
  973. kvfree(src_blkaddr);
  974. return -ENOMEM;
  975. }
  976. ret = __read_out_blkaddrs(src_inode, src_blkaddr,
  977. do_replace, src, olen);
  978. if (ret)
  979. goto roll_back;
  980. ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
  981. do_replace, src, dst, olen, full);
  982. if (ret)
  983. goto roll_back;
  984. src += olen;
  985. dst += olen;
  986. len -= olen;
  987. kvfree(src_blkaddr);
  988. kvfree(do_replace);
  989. }
  990. return 0;
  991. roll_back:
  992. __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
  993. kvfree(src_blkaddr);
  994. kvfree(do_replace);
  995. return ret;
  996. }
  997. static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
  998. {
  999. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1000. pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
  1001. pgoff_t start = offset >> PAGE_SHIFT;
  1002. pgoff_t end = (offset + len) >> PAGE_SHIFT;
  1003. int ret;
  1004. f2fs_balance_fs(sbi, true);
  1005. /* avoid gc operation during block exchange */
  1006. down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1007. down_write(&F2FS_I(inode)->i_mmap_sem);
  1008. f2fs_lock_op(sbi);
  1009. f2fs_drop_extent_tree(inode);
  1010. truncate_pagecache(inode, offset);
  1011. ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
  1012. f2fs_unlock_op(sbi);
  1013. up_write(&F2FS_I(inode)->i_mmap_sem);
  1014. up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1015. return ret;
  1016. }
  1017. static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
  1018. {
  1019. loff_t new_size;
  1020. int ret;
  1021. if (offset + len >= i_size_read(inode))
  1022. return -EINVAL;
  1023. /* collapse range should be aligned to block size of f2fs. */
  1024. if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
  1025. return -EINVAL;
  1026. ret = f2fs_convert_inline_inode(inode);
  1027. if (ret)
  1028. return ret;
  1029. /* write out all dirty pages from offset */
  1030. ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  1031. if (ret)
  1032. return ret;
  1033. ret = f2fs_do_collapse(inode, offset, len);
  1034. if (ret)
  1035. return ret;
  1036. /* write out all moved pages, if possible */
  1037. down_write(&F2FS_I(inode)->i_mmap_sem);
  1038. filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  1039. truncate_pagecache(inode, offset);
  1040. new_size = i_size_read(inode) - len;
  1041. truncate_pagecache(inode, new_size);
  1042. ret = f2fs_truncate_blocks(inode, new_size, true);
  1043. up_write(&F2FS_I(inode)->i_mmap_sem);
  1044. if (!ret)
  1045. f2fs_i_size_write(inode, new_size);
  1046. return ret;
  1047. }
  1048. static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
  1049. pgoff_t end)
  1050. {
  1051. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  1052. pgoff_t index = start;
  1053. unsigned int ofs_in_node = dn->ofs_in_node;
  1054. blkcnt_t count = 0;
  1055. int ret;
  1056. for (; index < end; index++, dn->ofs_in_node++) {
  1057. if (datablock_addr(dn->inode, dn->node_page,
  1058. dn->ofs_in_node) == NULL_ADDR)
  1059. count++;
  1060. }
  1061. dn->ofs_in_node = ofs_in_node;
  1062. ret = f2fs_reserve_new_blocks(dn, count);
  1063. if (ret)
  1064. return ret;
  1065. dn->ofs_in_node = ofs_in_node;
  1066. for (index = start; index < end; index++, dn->ofs_in_node++) {
  1067. dn->data_blkaddr = datablock_addr(dn->inode,
  1068. dn->node_page, dn->ofs_in_node);
  1069. /*
  1070. * f2fs_reserve_new_blocks will not guarantee entire block
  1071. * allocation.
  1072. */
  1073. if (dn->data_blkaddr == NULL_ADDR) {
  1074. ret = -ENOSPC;
  1075. break;
  1076. }
  1077. if (dn->data_blkaddr != NEW_ADDR) {
  1078. f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
  1079. dn->data_blkaddr = NEW_ADDR;
  1080. f2fs_set_data_blkaddr(dn);
  1081. }
  1082. }
  1083. f2fs_update_extent_cache_range(dn, start, 0, index - start);
  1084. return ret;
  1085. }
  1086. static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
  1087. int mode)
  1088. {
  1089. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1090. struct address_space *mapping = inode->i_mapping;
  1091. pgoff_t index, pg_start, pg_end;
  1092. loff_t new_size = i_size_read(inode);
  1093. loff_t off_start, off_end;
  1094. int ret = 0;
  1095. ret = inode_newsize_ok(inode, (len + offset));
  1096. if (ret)
  1097. return ret;
  1098. ret = f2fs_convert_inline_inode(inode);
  1099. if (ret)
  1100. return ret;
  1101. ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
  1102. if (ret)
  1103. return ret;
  1104. pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
  1105. pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
  1106. off_start = offset & (PAGE_SIZE - 1);
  1107. off_end = (offset + len) & (PAGE_SIZE - 1);
  1108. if (pg_start == pg_end) {
  1109. ret = fill_zero(inode, pg_start, off_start,
  1110. off_end - off_start);
  1111. if (ret)
  1112. return ret;
  1113. new_size = max_t(loff_t, new_size, offset + len);
  1114. } else {
  1115. if (off_start) {
  1116. ret = fill_zero(inode, pg_start++, off_start,
  1117. PAGE_SIZE - off_start);
  1118. if (ret)
  1119. return ret;
  1120. new_size = max_t(loff_t, new_size,
  1121. (loff_t)pg_start << PAGE_SHIFT);
  1122. }
  1123. for (index = pg_start; index < pg_end;) {
  1124. struct dnode_of_data dn;
  1125. unsigned int end_offset;
  1126. pgoff_t end;
  1127. down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1128. down_write(&F2FS_I(inode)->i_mmap_sem);
  1129. truncate_pagecache_range(inode,
  1130. (loff_t)index << PAGE_SHIFT,
  1131. ((loff_t)pg_end << PAGE_SHIFT) - 1);
  1132. f2fs_lock_op(sbi);
  1133. set_new_dnode(&dn, inode, NULL, NULL, 0);
  1134. ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
  1135. if (ret) {
  1136. f2fs_unlock_op(sbi);
  1137. up_write(&F2FS_I(inode)->i_mmap_sem);
  1138. up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1139. goto out;
  1140. }
  1141. end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
  1142. end = min(pg_end, end_offset - dn.ofs_in_node + index);
  1143. ret = f2fs_do_zero_range(&dn, index, end);
  1144. f2fs_put_dnode(&dn);
  1145. f2fs_unlock_op(sbi);
  1146. up_write(&F2FS_I(inode)->i_mmap_sem);
  1147. up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1148. f2fs_balance_fs(sbi, dn.node_changed);
  1149. if (ret)
  1150. goto out;
  1151. index = end;
  1152. new_size = max_t(loff_t, new_size,
  1153. (loff_t)index << PAGE_SHIFT);
  1154. }
  1155. if (off_end) {
  1156. ret = fill_zero(inode, pg_end, 0, off_end);
  1157. if (ret)
  1158. goto out;
  1159. new_size = max_t(loff_t, new_size, offset + len);
  1160. }
  1161. }
  1162. out:
  1163. if (new_size > i_size_read(inode)) {
  1164. if (mode & FALLOC_FL_KEEP_SIZE)
  1165. file_set_keep_isize(inode);
  1166. else
  1167. f2fs_i_size_write(inode, new_size);
  1168. }
  1169. return ret;
  1170. }
  1171. static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
  1172. {
  1173. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1174. pgoff_t nr, pg_start, pg_end, delta, idx;
  1175. loff_t new_size;
  1176. int ret = 0;
  1177. new_size = i_size_read(inode) + len;
  1178. ret = inode_newsize_ok(inode, new_size);
  1179. if (ret)
  1180. return ret;
  1181. if (offset >= i_size_read(inode))
  1182. return -EINVAL;
  1183. /* insert range should be aligned to block size of f2fs. */
  1184. if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
  1185. return -EINVAL;
  1186. ret = f2fs_convert_inline_inode(inode);
  1187. if (ret)
  1188. return ret;
  1189. f2fs_balance_fs(sbi, true);
  1190. down_write(&F2FS_I(inode)->i_mmap_sem);
  1191. ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
  1192. up_write(&F2FS_I(inode)->i_mmap_sem);
  1193. if (ret)
  1194. return ret;
  1195. /* write out all dirty pages from offset */
  1196. ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  1197. if (ret)
  1198. return ret;
  1199. pg_start = offset >> PAGE_SHIFT;
  1200. pg_end = (offset + len) >> PAGE_SHIFT;
  1201. delta = pg_end - pg_start;
  1202. idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
  1203. /* avoid gc operation during block exchange */
  1204. down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1205. down_write(&F2FS_I(inode)->i_mmap_sem);
  1206. truncate_pagecache(inode, offset);
  1207. while (!ret && idx > pg_start) {
  1208. nr = idx - pg_start;
  1209. if (nr > delta)
  1210. nr = delta;
  1211. idx -= nr;
  1212. f2fs_lock_op(sbi);
  1213. f2fs_drop_extent_tree(inode);
  1214. ret = __exchange_data_block(inode, inode, idx,
  1215. idx + delta, nr, false);
  1216. f2fs_unlock_op(sbi);
  1217. }
  1218. up_write(&F2FS_I(inode)->i_mmap_sem);
  1219. up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1220. /* write out all moved pages, if possible */
  1221. down_write(&F2FS_I(inode)->i_mmap_sem);
  1222. filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  1223. truncate_pagecache(inode, offset);
  1224. up_write(&F2FS_I(inode)->i_mmap_sem);
  1225. if (!ret)
  1226. f2fs_i_size_write(inode, new_size);
  1227. return ret;
  1228. }
  1229. static int expand_inode_data(struct inode *inode, loff_t offset,
  1230. loff_t len, int mode)
  1231. {
  1232. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1233. struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
  1234. .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE };
  1235. pgoff_t pg_end;
  1236. loff_t new_size = i_size_read(inode);
  1237. loff_t off_end;
  1238. int err;
  1239. err = inode_newsize_ok(inode, (len + offset));
  1240. if (err)
  1241. return err;
  1242. err = f2fs_convert_inline_inode(inode);
  1243. if (err)
  1244. return err;
  1245. f2fs_balance_fs(sbi, true);
  1246. pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
  1247. off_end = (offset + len) & (PAGE_SIZE - 1);
  1248. map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
  1249. map.m_len = pg_end - map.m_lblk;
  1250. if (off_end)
  1251. map.m_len++;
  1252. err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
  1253. if (err) {
  1254. pgoff_t last_off;
  1255. if (!map.m_len)
  1256. return err;
  1257. last_off = map.m_lblk + map.m_len - 1;
  1258. /* update new size to the failed position */
  1259. new_size = (last_off == pg_end) ? offset + len :
  1260. (loff_t)(last_off + 1) << PAGE_SHIFT;
  1261. } else {
  1262. new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
  1263. }
  1264. if (new_size > i_size_read(inode)) {
  1265. if (mode & FALLOC_FL_KEEP_SIZE)
  1266. file_set_keep_isize(inode);
  1267. else
  1268. f2fs_i_size_write(inode, new_size);
  1269. }
  1270. return err;
  1271. }
  1272. static long f2fs_fallocate(struct file *file, int mode,
  1273. loff_t offset, loff_t len)
  1274. {
  1275. struct inode *inode = file_inode(file);
  1276. long ret = 0;
  1277. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
  1278. return -EIO;
  1279. /* f2fs only support ->fallocate for regular file */
  1280. if (!S_ISREG(inode->i_mode))
  1281. return -EINVAL;
  1282. if (f2fs_encrypted_inode(inode) &&
  1283. (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
  1284. return -EOPNOTSUPP;
  1285. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
  1286. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
  1287. FALLOC_FL_INSERT_RANGE))
  1288. return -EOPNOTSUPP;
  1289. inode_lock(inode);
  1290. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1291. if (offset >= inode->i_size)
  1292. goto out;
  1293. ret = punch_hole(inode, offset, len);
  1294. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  1295. ret = f2fs_collapse_range(inode, offset, len);
  1296. } else if (mode & FALLOC_FL_ZERO_RANGE) {
  1297. ret = f2fs_zero_range(inode, offset, len, mode);
  1298. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  1299. ret = f2fs_insert_range(inode, offset, len);
  1300. } else {
  1301. ret = expand_inode_data(inode, offset, len, mode);
  1302. }
  1303. if (!ret) {
  1304. inode->i_mtime = inode->i_ctime = current_time(inode);
  1305. f2fs_mark_inode_dirty_sync(inode, false);
  1306. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1307. }
  1308. out:
  1309. inode_unlock(inode);
  1310. trace_f2fs_fallocate(inode, mode, offset, len, ret);
  1311. return ret;
  1312. }
  1313. static int f2fs_release_file(struct inode *inode, struct file *filp)
  1314. {
  1315. /*
  1316. * f2fs_relase_file is called at every close calls. So we should
  1317. * not drop any inmemory pages by close called by other process.
  1318. */
  1319. if (!(filp->f_mode & FMODE_WRITE) ||
  1320. atomic_read(&inode->i_writecount) != 1)
  1321. return 0;
  1322. /* some remained atomic pages should discarded */
  1323. if (f2fs_is_atomic_file(inode))
  1324. f2fs_drop_inmem_pages(inode);
  1325. if (f2fs_is_volatile_file(inode)) {
  1326. set_inode_flag(inode, FI_DROP_CACHE);
  1327. filemap_fdatawrite(inode->i_mapping);
  1328. clear_inode_flag(inode, FI_DROP_CACHE);
  1329. clear_inode_flag(inode, FI_VOLATILE_FILE);
  1330. stat_dec_volatile_write(inode);
  1331. }
  1332. return 0;
  1333. }
  1334. static int f2fs_file_flush(struct file *file, fl_owner_t id)
  1335. {
  1336. struct inode *inode = file_inode(file);
  1337. /*
  1338. * If the process doing a transaction is crashed, we should do
  1339. * roll-back. Otherwise, other reader/write can see corrupted database
  1340. * until all the writers close its file. Since this should be done
  1341. * before dropping file lock, it needs to do in ->flush.
  1342. */
  1343. if (f2fs_is_atomic_file(inode) &&
  1344. F2FS_I(inode)->inmem_task == current)
  1345. f2fs_drop_inmem_pages(inode);
  1346. return 0;
  1347. }
  1348. static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
  1349. {
  1350. struct inode *inode = file_inode(filp);
  1351. struct f2fs_inode_info *fi = F2FS_I(inode);
  1352. unsigned int flags = fi->i_flags;
  1353. if (f2fs_encrypted_inode(inode))
  1354. flags |= F2FS_ENCRYPT_FL;
  1355. if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
  1356. flags |= F2FS_INLINE_DATA_FL;
  1357. flags &= F2FS_FL_USER_VISIBLE;
  1358. return put_user(flags, (int __user *)arg);
  1359. }
  1360. static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
  1361. {
  1362. struct f2fs_inode_info *fi = F2FS_I(inode);
  1363. unsigned int oldflags;
  1364. /* Is it quota file? Do not allow user to mess with it */
  1365. if (IS_NOQUOTA(inode))
  1366. return -EPERM;
  1367. flags = f2fs_mask_flags(inode->i_mode, flags);
  1368. oldflags = fi->i_flags;
  1369. if ((flags ^ oldflags) & (F2FS_APPEND_FL | F2FS_IMMUTABLE_FL))
  1370. if (!capable(CAP_LINUX_IMMUTABLE))
  1371. return -EPERM;
  1372. flags = flags & F2FS_FL_USER_MODIFIABLE;
  1373. flags |= oldflags & ~F2FS_FL_USER_MODIFIABLE;
  1374. fi->i_flags = flags;
  1375. if (fi->i_flags & F2FS_PROJINHERIT_FL)
  1376. set_inode_flag(inode, FI_PROJ_INHERIT);
  1377. else
  1378. clear_inode_flag(inode, FI_PROJ_INHERIT);
  1379. inode->i_ctime = current_time(inode);
  1380. f2fs_set_inode_flags(inode);
  1381. f2fs_mark_inode_dirty_sync(inode, true);
  1382. return 0;
  1383. }
  1384. static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
  1385. {
  1386. struct inode *inode = file_inode(filp);
  1387. unsigned int flags;
  1388. int ret;
  1389. if (!inode_owner_or_capable(inode))
  1390. return -EACCES;
  1391. if (get_user(flags, (int __user *)arg))
  1392. return -EFAULT;
  1393. ret = mnt_want_write_file(filp);
  1394. if (ret)
  1395. return ret;
  1396. inode_lock(inode);
  1397. ret = __f2fs_ioc_setflags(inode, flags);
  1398. inode_unlock(inode);
  1399. mnt_drop_write_file(filp);
  1400. return ret;
  1401. }
  1402. static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
  1403. {
  1404. struct inode *inode = file_inode(filp);
  1405. return put_user(inode->i_generation, (int __user *)arg);
  1406. }
  1407. static int f2fs_ioc_start_atomic_write(struct file *filp)
  1408. {
  1409. struct inode *inode = file_inode(filp);
  1410. int ret;
  1411. if (!inode_owner_or_capable(inode))
  1412. return -EACCES;
  1413. if (!S_ISREG(inode->i_mode))
  1414. return -EINVAL;
  1415. ret = mnt_want_write_file(filp);
  1416. if (ret)
  1417. return ret;
  1418. inode_lock(inode);
  1419. if (f2fs_is_atomic_file(inode)) {
  1420. if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST))
  1421. ret = -EINVAL;
  1422. goto out;
  1423. }
  1424. ret = f2fs_convert_inline_inode(inode);
  1425. if (ret)
  1426. goto out;
  1427. down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1428. /*
  1429. * Should wait end_io to count F2FS_WB_CP_DATA correctly by
  1430. * f2fs_is_atomic_file.
  1431. */
  1432. if (get_dirty_pages(inode))
  1433. f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
  1434. "Unexpected flush for atomic writes: ino=%lu, npages=%u",
  1435. inode->i_ino, get_dirty_pages(inode));
  1436. ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
  1437. if (ret) {
  1438. up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1439. goto out;
  1440. }
  1441. set_inode_flag(inode, FI_ATOMIC_FILE);
  1442. clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
  1443. up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  1444. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1445. F2FS_I(inode)->inmem_task = current;
  1446. stat_inc_atomic_write(inode);
  1447. stat_update_max_atomic_write(inode);
  1448. out:
  1449. inode_unlock(inode);
  1450. mnt_drop_write_file(filp);
  1451. return ret;
  1452. }
  1453. static int f2fs_ioc_commit_atomic_write(struct file *filp)
  1454. {
  1455. struct inode *inode = file_inode(filp);
  1456. int ret;
  1457. if (!inode_owner_or_capable(inode))
  1458. return -EACCES;
  1459. ret = mnt_want_write_file(filp);
  1460. if (ret)
  1461. return ret;
  1462. f2fs_balance_fs(F2FS_I_SB(inode), true);
  1463. inode_lock(inode);
  1464. if (f2fs_is_volatile_file(inode)) {
  1465. ret = -EINVAL;
  1466. goto err_out;
  1467. }
  1468. if (f2fs_is_atomic_file(inode)) {
  1469. ret = f2fs_commit_inmem_pages(inode);
  1470. if (ret)
  1471. goto err_out;
  1472. ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
  1473. if (!ret) {
  1474. clear_inode_flag(inode, FI_ATOMIC_FILE);
  1475. F2FS_I(inode)->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
  1476. stat_dec_atomic_write(inode);
  1477. }
  1478. } else {
  1479. ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
  1480. }
  1481. err_out:
  1482. if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
  1483. clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
  1484. ret = -EINVAL;
  1485. }
  1486. inode_unlock(inode);
  1487. mnt_drop_write_file(filp);
  1488. return ret;
  1489. }
  1490. static int f2fs_ioc_start_volatile_write(struct file *filp)
  1491. {
  1492. struct inode *inode = file_inode(filp);
  1493. int ret;
  1494. if (!inode_owner_or_capable(inode))
  1495. return -EACCES;
  1496. if (!S_ISREG(inode->i_mode))
  1497. return -EINVAL;
  1498. ret = mnt_want_write_file(filp);
  1499. if (ret)
  1500. return ret;
  1501. inode_lock(inode);
  1502. if (f2fs_is_volatile_file(inode))
  1503. goto out;
  1504. ret = f2fs_convert_inline_inode(inode);
  1505. if (ret)
  1506. goto out;
  1507. stat_inc_volatile_write(inode);
  1508. stat_update_max_volatile_write(inode);
  1509. set_inode_flag(inode, FI_VOLATILE_FILE);
  1510. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1511. out:
  1512. inode_unlock(inode);
  1513. mnt_drop_write_file(filp);
  1514. return ret;
  1515. }
  1516. static int f2fs_ioc_release_volatile_write(struct file *filp)
  1517. {
  1518. struct inode *inode = file_inode(filp);
  1519. int ret;
  1520. if (!inode_owner_or_capable(inode))
  1521. return -EACCES;
  1522. ret = mnt_want_write_file(filp);
  1523. if (ret)
  1524. return ret;
  1525. inode_lock(inode);
  1526. if (!f2fs_is_volatile_file(inode))
  1527. goto out;
  1528. if (!f2fs_is_first_block_written(inode)) {
  1529. ret = truncate_partial_data_page(inode, 0, true);
  1530. goto out;
  1531. }
  1532. ret = punch_hole(inode, 0, F2FS_BLKSIZE);
  1533. out:
  1534. inode_unlock(inode);
  1535. mnt_drop_write_file(filp);
  1536. return ret;
  1537. }
  1538. static int f2fs_ioc_abort_volatile_write(struct file *filp)
  1539. {
  1540. struct inode *inode = file_inode(filp);
  1541. int ret;
  1542. if (!inode_owner_or_capable(inode))
  1543. return -EACCES;
  1544. ret = mnt_want_write_file(filp);
  1545. if (ret)
  1546. return ret;
  1547. inode_lock(inode);
  1548. if (f2fs_is_atomic_file(inode))
  1549. f2fs_drop_inmem_pages(inode);
  1550. if (f2fs_is_volatile_file(inode)) {
  1551. clear_inode_flag(inode, FI_VOLATILE_FILE);
  1552. stat_dec_volatile_write(inode);
  1553. ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
  1554. }
  1555. clear_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
  1556. inode_unlock(inode);
  1557. mnt_drop_write_file(filp);
  1558. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1559. return ret;
  1560. }
  1561. static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
  1562. {
  1563. struct inode *inode = file_inode(filp);
  1564. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1565. struct super_block *sb = sbi->sb;
  1566. __u32 in;
  1567. int ret = 0;
  1568. if (!capable(CAP_SYS_ADMIN))
  1569. return -EPERM;
  1570. if (get_user(in, (__u32 __user *)arg))
  1571. return -EFAULT;
  1572. if (in != F2FS_GOING_DOWN_FULLSYNC) {
  1573. ret = mnt_want_write_file(filp);
  1574. if (ret)
  1575. return ret;
  1576. }
  1577. switch (in) {
  1578. case F2FS_GOING_DOWN_FULLSYNC:
  1579. sb = freeze_bdev(sb->s_bdev);
  1580. if (IS_ERR(sb)) {
  1581. ret = PTR_ERR(sb);
  1582. goto out;
  1583. }
  1584. if (sb) {
  1585. f2fs_stop_checkpoint(sbi, false);
  1586. set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
  1587. thaw_bdev(sb->s_bdev, sb);
  1588. }
  1589. break;
  1590. case F2FS_GOING_DOWN_METASYNC:
  1591. /* do checkpoint only */
  1592. ret = f2fs_sync_fs(sb, 1);
  1593. if (ret)
  1594. goto out;
  1595. f2fs_stop_checkpoint(sbi, false);
  1596. set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
  1597. break;
  1598. case F2FS_GOING_DOWN_NOSYNC:
  1599. f2fs_stop_checkpoint(sbi, false);
  1600. set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
  1601. break;
  1602. case F2FS_GOING_DOWN_METAFLUSH:
  1603. f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
  1604. f2fs_stop_checkpoint(sbi, false);
  1605. set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
  1606. break;
  1607. default:
  1608. ret = -EINVAL;
  1609. goto out;
  1610. }
  1611. f2fs_stop_gc_thread(sbi);
  1612. f2fs_stop_discard_thread(sbi);
  1613. f2fs_drop_discard_cmd(sbi);
  1614. clear_opt(sbi, DISCARD);
  1615. f2fs_update_time(sbi, REQ_TIME);
  1616. out:
  1617. if (in != F2FS_GOING_DOWN_FULLSYNC)
  1618. mnt_drop_write_file(filp);
  1619. return ret;
  1620. }
  1621. static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
  1622. {
  1623. struct inode *inode = file_inode(filp);
  1624. struct super_block *sb = inode->i_sb;
  1625. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  1626. struct fstrim_range range;
  1627. int ret;
  1628. if (!capable(CAP_SYS_ADMIN))
  1629. return -EPERM;
  1630. if (!f2fs_hw_support_discard(F2FS_SB(sb)))
  1631. return -EOPNOTSUPP;
  1632. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  1633. sizeof(range)))
  1634. return -EFAULT;
  1635. ret = mnt_want_write_file(filp);
  1636. if (ret)
  1637. return ret;
  1638. range.minlen = max((unsigned int)range.minlen,
  1639. q->limits.discard_granularity);
  1640. ret = f2fs_trim_fs(F2FS_SB(sb), &range);
  1641. mnt_drop_write_file(filp);
  1642. if (ret < 0)
  1643. return ret;
  1644. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  1645. sizeof(range)))
  1646. return -EFAULT;
  1647. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1648. return 0;
  1649. }
  1650. static bool uuid_is_nonzero(__u8 u[16])
  1651. {
  1652. int i;
  1653. for (i = 0; i < 16; i++)
  1654. if (u[i])
  1655. return true;
  1656. return false;
  1657. }
  1658. static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
  1659. {
  1660. struct inode *inode = file_inode(filp);
  1661. if (!f2fs_sb_has_encrypt(inode->i_sb))
  1662. return -EOPNOTSUPP;
  1663. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1664. return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
  1665. }
  1666. static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
  1667. {
  1668. if (!f2fs_sb_has_encrypt(file_inode(filp)->i_sb))
  1669. return -EOPNOTSUPP;
  1670. return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
  1671. }
  1672. static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
  1673. {
  1674. struct inode *inode = file_inode(filp);
  1675. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1676. int err;
  1677. if (!f2fs_sb_has_encrypt(inode->i_sb))
  1678. return -EOPNOTSUPP;
  1679. err = mnt_want_write_file(filp);
  1680. if (err)
  1681. return err;
  1682. down_write(&sbi->sb_lock);
  1683. if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
  1684. goto got_it;
  1685. /* update superblock with uuid */
  1686. generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
  1687. err = f2fs_commit_super(sbi, false);
  1688. if (err) {
  1689. /* undo new data */
  1690. memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
  1691. goto out_err;
  1692. }
  1693. got_it:
  1694. if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
  1695. 16))
  1696. err = -EFAULT;
  1697. out_err:
  1698. up_write(&sbi->sb_lock);
  1699. mnt_drop_write_file(filp);
  1700. return err;
  1701. }
  1702. static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
  1703. {
  1704. struct inode *inode = file_inode(filp);
  1705. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1706. __u32 sync;
  1707. int ret;
  1708. if (!capable(CAP_SYS_ADMIN))
  1709. return -EPERM;
  1710. if (get_user(sync, (__u32 __user *)arg))
  1711. return -EFAULT;
  1712. if (f2fs_readonly(sbi->sb))
  1713. return -EROFS;
  1714. ret = mnt_want_write_file(filp);
  1715. if (ret)
  1716. return ret;
  1717. if (!sync) {
  1718. if (!mutex_trylock(&sbi->gc_mutex)) {
  1719. ret = -EBUSY;
  1720. goto out;
  1721. }
  1722. } else {
  1723. mutex_lock(&sbi->gc_mutex);
  1724. }
  1725. ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
  1726. out:
  1727. mnt_drop_write_file(filp);
  1728. return ret;
  1729. }
  1730. static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
  1731. {
  1732. struct inode *inode = file_inode(filp);
  1733. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1734. struct f2fs_gc_range range;
  1735. u64 end;
  1736. int ret;
  1737. if (!capable(CAP_SYS_ADMIN))
  1738. return -EPERM;
  1739. if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
  1740. sizeof(range)))
  1741. return -EFAULT;
  1742. if (f2fs_readonly(sbi->sb))
  1743. return -EROFS;
  1744. end = range.start + range.len;
  1745. if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) {
  1746. return -EINVAL;
  1747. }
  1748. ret = mnt_want_write_file(filp);
  1749. if (ret)
  1750. return ret;
  1751. do_more:
  1752. if (!range.sync) {
  1753. if (!mutex_trylock(&sbi->gc_mutex)) {
  1754. ret = -EBUSY;
  1755. goto out;
  1756. }
  1757. } else {
  1758. mutex_lock(&sbi->gc_mutex);
  1759. }
  1760. ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
  1761. range.start += BLKS_PER_SEC(sbi);
  1762. if (range.start <= end)
  1763. goto do_more;
  1764. out:
  1765. mnt_drop_write_file(filp);
  1766. return ret;
  1767. }
  1768. static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
  1769. {
  1770. struct inode *inode = file_inode(filp);
  1771. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1772. int ret;
  1773. if (!capable(CAP_SYS_ADMIN))
  1774. return -EPERM;
  1775. if (f2fs_readonly(sbi->sb))
  1776. return -EROFS;
  1777. ret = mnt_want_write_file(filp);
  1778. if (ret)
  1779. return ret;
  1780. ret = f2fs_sync_fs(sbi->sb, 1);
  1781. mnt_drop_write_file(filp);
  1782. return ret;
  1783. }
  1784. static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
  1785. struct file *filp,
  1786. struct f2fs_defragment *range)
  1787. {
  1788. struct inode *inode = file_inode(filp);
  1789. struct f2fs_map_blocks map = { .m_next_extent = NULL,
  1790. .m_seg_type = NO_CHECK_TYPE };
  1791. struct extent_info ei = {0, 0, 0};
  1792. pgoff_t pg_start, pg_end, next_pgofs;
  1793. unsigned int blk_per_seg = sbi->blocks_per_seg;
  1794. unsigned int total = 0, sec_num;
  1795. block_t blk_end = 0;
  1796. bool fragmented = false;
  1797. int err;
  1798. /* if in-place-update policy is enabled, don't waste time here */
  1799. if (f2fs_should_update_inplace(inode, NULL))
  1800. return -EINVAL;
  1801. pg_start = range->start >> PAGE_SHIFT;
  1802. pg_end = (range->start + range->len) >> PAGE_SHIFT;
  1803. f2fs_balance_fs(sbi, true);
  1804. inode_lock(inode);
  1805. /* writeback all dirty pages in the range */
  1806. err = filemap_write_and_wait_range(inode->i_mapping, range->start,
  1807. range->start + range->len - 1);
  1808. if (err)
  1809. goto out;
  1810. /*
  1811. * lookup mapping info in extent cache, skip defragmenting if physical
  1812. * block addresses are continuous.
  1813. */
  1814. if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
  1815. if (ei.fofs + ei.len >= pg_end)
  1816. goto out;
  1817. }
  1818. map.m_lblk = pg_start;
  1819. map.m_next_pgofs = &next_pgofs;
  1820. /*
  1821. * lookup mapping info in dnode page cache, skip defragmenting if all
  1822. * physical block addresses are continuous even if there are hole(s)
  1823. * in logical blocks.
  1824. */
  1825. while (map.m_lblk < pg_end) {
  1826. map.m_len = pg_end - map.m_lblk;
  1827. err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
  1828. if (err)
  1829. goto out;
  1830. if (!(map.m_flags & F2FS_MAP_FLAGS)) {
  1831. map.m_lblk = next_pgofs;
  1832. continue;
  1833. }
  1834. if (blk_end && blk_end != map.m_pblk)
  1835. fragmented = true;
  1836. /* record total count of block that we're going to move */
  1837. total += map.m_len;
  1838. blk_end = map.m_pblk + map.m_len;
  1839. map.m_lblk += map.m_len;
  1840. }
  1841. if (!fragmented)
  1842. goto out;
  1843. sec_num = (total + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
  1844. /*
  1845. * make sure there are enough free section for LFS allocation, this can
  1846. * avoid defragment running in SSR mode when free section are allocated
  1847. * intensively
  1848. */
  1849. if (has_not_enough_free_secs(sbi, 0, sec_num)) {
  1850. err = -EAGAIN;
  1851. goto out;
  1852. }
  1853. map.m_lblk = pg_start;
  1854. map.m_len = pg_end - pg_start;
  1855. total = 0;
  1856. while (map.m_lblk < pg_end) {
  1857. pgoff_t idx;
  1858. int cnt = 0;
  1859. do_map:
  1860. map.m_len = pg_end - map.m_lblk;
  1861. err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
  1862. if (err)
  1863. goto clear_out;
  1864. if (!(map.m_flags & F2FS_MAP_FLAGS)) {
  1865. map.m_lblk = next_pgofs;
  1866. continue;
  1867. }
  1868. set_inode_flag(inode, FI_DO_DEFRAG);
  1869. idx = map.m_lblk;
  1870. while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
  1871. struct page *page;
  1872. page = f2fs_get_lock_data_page(inode, idx, true);
  1873. if (IS_ERR(page)) {
  1874. err = PTR_ERR(page);
  1875. goto clear_out;
  1876. }
  1877. set_page_dirty(page);
  1878. f2fs_put_page(page, 1);
  1879. idx++;
  1880. cnt++;
  1881. total++;
  1882. }
  1883. map.m_lblk = idx;
  1884. if (idx < pg_end && cnt < blk_per_seg)
  1885. goto do_map;
  1886. clear_inode_flag(inode, FI_DO_DEFRAG);
  1887. err = filemap_fdatawrite(inode->i_mapping);
  1888. if (err)
  1889. goto out;
  1890. }
  1891. clear_out:
  1892. clear_inode_flag(inode, FI_DO_DEFRAG);
  1893. out:
  1894. inode_unlock(inode);
  1895. if (!err)
  1896. range->len = (u64)total << PAGE_SHIFT;
  1897. return err;
  1898. }
  1899. static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
  1900. {
  1901. struct inode *inode = file_inode(filp);
  1902. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1903. struct f2fs_defragment range;
  1904. int err;
  1905. if (!capable(CAP_SYS_ADMIN))
  1906. return -EPERM;
  1907. if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
  1908. return -EINVAL;
  1909. if (f2fs_readonly(sbi->sb))
  1910. return -EROFS;
  1911. if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
  1912. sizeof(range)))
  1913. return -EFAULT;
  1914. /* verify alignment of offset & size */
  1915. if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
  1916. return -EINVAL;
  1917. if (unlikely((range.start + range.len) >> PAGE_SHIFT >
  1918. sbi->max_file_blocks))
  1919. return -EINVAL;
  1920. err = mnt_want_write_file(filp);
  1921. if (err)
  1922. return err;
  1923. err = f2fs_defragment_range(sbi, filp, &range);
  1924. mnt_drop_write_file(filp);
  1925. f2fs_update_time(sbi, REQ_TIME);
  1926. if (err < 0)
  1927. return err;
  1928. if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
  1929. sizeof(range)))
  1930. return -EFAULT;
  1931. return 0;
  1932. }
  1933. static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
  1934. struct file *file_out, loff_t pos_out, size_t len)
  1935. {
  1936. struct inode *src = file_inode(file_in);
  1937. struct inode *dst = file_inode(file_out);
  1938. struct f2fs_sb_info *sbi = F2FS_I_SB(src);
  1939. size_t olen = len, dst_max_i_size = 0;
  1940. size_t dst_osize;
  1941. int ret;
  1942. if (file_in->f_path.mnt != file_out->f_path.mnt ||
  1943. src->i_sb != dst->i_sb)
  1944. return -EXDEV;
  1945. if (unlikely(f2fs_readonly(src->i_sb)))
  1946. return -EROFS;
  1947. if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
  1948. return -EINVAL;
  1949. if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
  1950. return -EOPNOTSUPP;
  1951. if (src == dst) {
  1952. if (pos_in == pos_out)
  1953. return 0;
  1954. if (pos_out > pos_in && pos_out < pos_in + len)
  1955. return -EINVAL;
  1956. }
  1957. inode_lock(src);
  1958. if (src != dst) {
  1959. ret = -EBUSY;
  1960. if (!inode_trylock(dst))
  1961. goto out;
  1962. }
  1963. ret = -EINVAL;
  1964. if (pos_in + len > src->i_size || pos_in + len < pos_in)
  1965. goto out_unlock;
  1966. if (len == 0)
  1967. olen = len = src->i_size - pos_in;
  1968. if (pos_in + len == src->i_size)
  1969. len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
  1970. if (len == 0) {
  1971. ret = 0;
  1972. goto out_unlock;
  1973. }
  1974. dst_osize = dst->i_size;
  1975. if (pos_out + olen > dst->i_size)
  1976. dst_max_i_size = pos_out + olen;
  1977. /* verify the end result is block aligned */
  1978. if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
  1979. !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
  1980. !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
  1981. goto out_unlock;
  1982. ret = f2fs_convert_inline_inode(src);
  1983. if (ret)
  1984. goto out_unlock;
  1985. ret = f2fs_convert_inline_inode(dst);
  1986. if (ret)
  1987. goto out_unlock;
  1988. /* write out all dirty pages from offset */
  1989. ret = filemap_write_and_wait_range(src->i_mapping,
  1990. pos_in, pos_in + len);
  1991. if (ret)
  1992. goto out_unlock;
  1993. ret = filemap_write_and_wait_range(dst->i_mapping,
  1994. pos_out, pos_out + len);
  1995. if (ret)
  1996. goto out_unlock;
  1997. f2fs_balance_fs(sbi, true);
  1998. down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
  1999. if (src != dst) {
  2000. ret = -EBUSY;
  2001. if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
  2002. goto out_src;
  2003. }
  2004. f2fs_lock_op(sbi);
  2005. ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
  2006. pos_out >> F2FS_BLKSIZE_BITS,
  2007. len >> F2FS_BLKSIZE_BITS, false);
  2008. if (!ret) {
  2009. if (dst_max_i_size)
  2010. f2fs_i_size_write(dst, dst_max_i_size);
  2011. else if (dst_osize != dst->i_size)
  2012. f2fs_i_size_write(dst, dst_osize);
  2013. }
  2014. f2fs_unlock_op(sbi);
  2015. if (src != dst)
  2016. up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
  2017. out_src:
  2018. up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
  2019. out_unlock:
  2020. if (src != dst)
  2021. inode_unlock(dst);
  2022. out:
  2023. inode_unlock(src);
  2024. return ret;
  2025. }
  2026. static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
  2027. {
  2028. struct f2fs_move_range range;
  2029. struct fd dst;
  2030. int err;
  2031. if (!(filp->f_mode & FMODE_READ) ||
  2032. !(filp->f_mode & FMODE_WRITE))
  2033. return -EBADF;
  2034. if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
  2035. sizeof(range)))
  2036. return -EFAULT;
  2037. dst = fdget(range.dst_fd);
  2038. if (!dst.file)
  2039. return -EBADF;
  2040. if (!(dst.file->f_mode & FMODE_WRITE)) {
  2041. err = -EBADF;
  2042. goto err_out;
  2043. }
  2044. err = mnt_want_write_file(filp);
  2045. if (err)
  2046. goto err_out;
  2047. err = f2fs_move_file_range(filp, range.pos_in, dst.file,
  2048. range.pos_out, range.len);
  2049. mnt_drop_write_file(filp);
  2050. if (err)
  2051. goto err_out;
  2052. if (copy_to_user((struct f2fs_move_range __user *)arg,
  2053. &range, sizeof(range)))
  2054. err = -EFAULT;
  2055. err_out:
  2056. fdput(dst);
  2057. return err;
  2058. }
  2059. static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
  2060. {
  2061. struct inode *inode = file_inode(filp);
  2062. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2063. struct sit_info *sm = SIT_I(sbi);
  2064. unsigned int start_segno = 0, end_segno = 0;
  2065. unsigned int dev_start_segno = 0, dev_end_segno = 0;
  2066. struct f2fs_flush_device range;
  2067. int ret;
  2068. if (!capable(CAP_SYS_ADMIN))
  2069. return -EPERM;
  2070. if (f2fs_readonly(sbi->sb))
  2071. return -EROFS;
  2072. if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
  2073. sizeof(range)))
  2074. return -EFAULT;
  2075. if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
  2076. sbi->segs_per_sec != 1) {
  2077. f2fs_msg(sbi->sb, KERN_WARNING,
  2078. "Can't flush %u in %d for segs_per_sec %u != 1\n",
  2079. range.dev_num, sbi->s_ndevs,
  2080. sbi->segs_per_sec);
  2081. return -EINVAL;
  2082. }
  2083. ret = mnt_want_write_file(filp);
  2084. if (ret)
  2085. return ret;
  2086. if (range.dev_num != 0)
  2087. dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
  2088. dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
  2089. start_segno = sm->last_victim[FLUSH_DEVICE];
  2090. if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
  2091. start_segno = dev_start_segno;
  2092. end_segno = min(start_segno + range.segments, dev_end_segno);
  2093. while (start_segno < end_segno) {
  2094. if (!mutex_trylock(&sbi->gc_mutex)) {
  2095. ret = -EBUSY;
  2096. goto out;
  2097. }
  2098. sm->last_victim[GC_CB] = end_segno + 1;
  2099. sm->last_victim[GC_GREEDY] = end_segno + 1;
  2100. sm->last_victim[ALLOC_NEXT] = end_segno + 1;
  2101. ret = f2fs_gc(sbi, true, true, start_segno);
  2102. if (ret == -EAGAIN)
  2103. ret = 0;
  2104. else if (ret < 0)
  2105. break;
  2106. start_segno++;
  2107. }
  2108. out:
  2109. mnt_drop_write_file(filp);
  2110. return ret;
  2111. }
  2112. static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
  2113. {
  2114. struct inode *inode = file_inode(filp);
  2115. u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
  2116. /* Must validate to set it with SQLite behavior in Android. */
  2117. sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
  2118. return put_user(sb_feature, (u32 __user *)arg);
  2119. }
  2120. #ifdef CONFIG_QUOTA
  2121. static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
  2122. {
  2123. struct inode *inode = file_inode(filp);
  2124. struct f2fs_inode_info *fi = F2FS_I(inode);
  2125. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2126. struct super_block *sb = sbi->sb;
  2127. struct dquot *transfer_to[MAXQUOTAS] = {};
  2128. struct page *ipage;
  2129. kprojid_t kprojid;
  2130. int err;
  2131. if (!f2fs_sb_has_project_quota(sb)) {
  2132. if (projid != F2FS_DEF_PROJID)
  2133. return -EOPNOTSUPP;
  2134. else
  2135. return 0;
  2136. }
  2137. if (!f2fs_has_extra_attr(inode))
  2138. return -EOPNOTSUPP;
  2139. kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
  2140. if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
  2141. return 0;
  2142. err = -EPERM;
  2143. /* Is it quota file? Do not allow user to mess with it */
  2144. if (IS_NOQUOTA(inode))
  2145. return err;
  2146. ipage = f2fs_get_node_page(sbi, inode->i_ino);
  2147. if (IS_ERR(ipage))
  2148. return PTR_ERR(ipage);
  2149. if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
  2150. i_projid)) {
  2151. err = -EOVERFLOW;
  2152. f2fs_put_page(ipage, 1);
  2153. return err;
  2154. }
  2155. f2fs_put_page(ipage, 1);
  2156. err = dquot_initialize(inode);
  2157. if (err)
  2158. return err;
  2159. transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
  2160. if (!IS_ERR(transfer_to[PRJQUOTA])) {
  2161. err = __dquot_transfer(inode, transfer_to);
  2162. dqput(transfer_to[PRJQUOTA]);
  2163. if (err)
  2164. goto out_dirty;
  2165. }
  2166. F2FS_I(inode)->i_projid = kprojid;
  2167. inode->i_ctime = current_time(inode);
  2168. out_dirty:
  2169. f2fs_mark_inode_dirty_sync(inode, true);
  2170. return err;
  2171. }
  2172. #else
  2173. static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
  2174. {
  2175. if (projid != F2FS_DEF_PROJID)
  2176. return -EOPNOTSUPP;
  2177. return 0;
  2178. }
  2179. #endif
  2180. /* Transfer internal flags to xflags */
  2181. static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
  2182. {
  2183. __u32 xflags = 0;
  2184. if (iflags & F2FS_SYNC_FL)
  2185. xflags |= FS_XFLAG_SYNC;
  2186. if (iflags & F2FS_IMMUTABLE_FL)
  2187. xflags |= FS_XFLAG_IMMUTABLE;
  2188. if (iflags & F2FS_APPEND_FL)
  2189. xflags |= FS_XFLAG_APPEND;
  2190. if (iflags & F2FS_NODUMP_FL)
  2191. xflags |= FS_XFLAG_NODUMP;
  2192. if (iflags & F2FS_NOATIME_FL)
  2193. xflags |= FS_XFLAG_NOATIME;
  2194. if (iflags & F2FS_PROJINHERIT_FL)
  2195. xflags |= FS_XFLAG_PROJINHERIT;
  2196. return xflags;
  2197. }
  2198. #define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
  2199. FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
  2200. FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
  2201. /* Transfer xflags flags to internal */
  2202. static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
  2203. {
  2204. unsigned long iflags = 0;
  2205. if (xflags & FS_XFLAG_SYNC)
  2206. iflags |= F2FS_SYNC_FL;
  2207. if (xflags & FS_XFLAG_IMMUTABLE)
  2208. iflags |= F2FS_IMMUTABLE_FL;
  2209. if (xflags & FS_XFLAG_APPEND)
  2210. iflags |= F2FS_APPEND_FL;
  2211. if (xflags & FS_XFLAG_NODUMP)
  2212. iflags |= F2FS_NODUMP_FL;
  2213. if (xflags & FS_XFLAG_NOATIME)
  2214. iflags |= F2FS_NOATIME_FL;
  2215. if (xflags & FS_XFLAG_PROJINHERIT)
  2216. iflags |= F2FS_PROJINHERIT_FL;
  2217. return iflags;
  2218. }
  2219. static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
  2220. {
  2221. struct inode *inode = file_inode(filp);
  2222. struct f2fs_inode_info *fi = F2FS_I(inode);
  2223. struct fsxattr fa;
  2224. memset(&fa, 0, sizeof(struct fsxattr));
  2225. fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
  2226. F2FS_FL_USER_VISIBLE);
  2227. if (f2fs_sb_has_project_quota(inode->i_sb))
  2228. fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
  2229. fi->i_projid);
  2230. if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
  2231. return -EFAULT;
  2232. return 0;
  2233. }
  2234. static int f2fs_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
  2235. {
  2236. /*
  2237. * Project Quota ID state is only allowed to change from within the init
  2238. * namespace. Enforce that restriction only if we are trying to change
  2239. * the quota ID state. Everything else is allowed in user namespaces.
  2240. */
  2241. if (current_user_ns() == &init_user_ns)
  2242. return 0;
  2243. if (__kprojid_val(F2FS_I(inode)->i_projid) != fa->fsx_projid)
  2244. return -EINVAL;
  2245. if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL) {
  2246. if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
  2247. return -EINVAL;
  2248. } else {
  2249. if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
  2250. return -EINVAL;
  2251. }
  2252. return 0;
  2253. }
  2254. static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
  2255. {
  2256. struct inode *inode = file_inode(filp);
  2257. struct f2fs_inode_info *fi = F2FS_I(inode);
  2258. struct fsxattr fa;
  2259. unsigned int flags;
  2260. int err;
  2261. if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
  2262. return -EFAULT;
  2263. /* Make sure caller has proper permission */
  2264. if (!inode_owner_or_capable(inode))
  2265. return -EACCES;
  2266. if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
  2267. return -EOPNOTSUPP;
  2268. flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
  2269. if (f2fs_mask_flags(inode->i_mode, flags) != flags)
  2270. return -EOPNOTSUPP;
  2271. err = mnt_want_write_file(filp);
  2272. if (err)
  2273. return err;
  2274. inode_lock(inode);
  2275. err = f2fs_ioctl_check_project(inode, &fa);
  2276. if (err)
  2277. goto out;
  2278. flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
  2279. (flags & F2FS_FL_XFLAG_VISIBLE);
  2280. err = __f2fs_ioc_setflags(inode, flags);
  2281. if (err)
  2282. goto out;
  2283. err = f2fs_ioc_setproject(filp, fa.fsx_projid);
  2284. out:
  2285. inode_unlock(inode);
  2286. mnt_drop_write_file(filp);
  2287. return err;
  2288. }
  2289. int f2fs_pin_file_control(struct inode *inode, bool inc)
  2290. {
  2291. struct f2fs_inode_info *fi = F2FS_I(inode);
  2292. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2293. /* Use i_gc_failures for normal file as a risk signal. */
  2294. if (inc)
  2295. f2fs_i_gc_failures_write(inode,
  2296. fi->i_gc_failures[GC_FAILURE_PIN] + 1);
  2297. if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
  2298. f2fs_msg(sbi->sb, KERN_WARNING,
  2299. "%s: Enable GC = ino %lx after %x GC trials\n",
  2300. __func__, inode->i_ino,
  2301. fi->i_gc_failures[GC_FAILURE_PIN]);
  2302. clear_inode_flag(inode, FI_PIN_FILE);
  2303. return -EAGAIN;
  2304. }
  2305. return 0;
  2306. }
  2307. static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
  2308. {
  2309. struct inode *inode = file_inode(filp);
  2310. __u32 pin;
  2311. int ret = 0;
  2312. if (!inode_owner_or_capable(inode))
  2313. return -EACCES;
  2314. if (get_user(pin, (__u32 __user *)arg))
  2315. return -EFAULT;
  2316. if (!S_ISREG(inode->i_mode))
  2317. return -EINVAL;
  2318. if (f2fs_readonly(F2FS_I_SB(inode)->sb))
  2319. return -EROFS;
  2320. ret = mnt_want_write_file(filp);
  2321. if (ret)
  2322. return ret;
  2323. inode_lock(inode);
  2324. if (f2fs_should_update_outplace(inode, NULL)) {
  2325. ret = -EINVAL;
  2326. goto out;
  2327. }
  2328. if (!pin) {
  2329. clear_inode_flag(inode, FI_PIN_FILE);
  2330. f2fs_i_gc_failures_write(inode, 0);
  2331. goto done;
  2332. }
  2333. if (f2fs_pin_file_control(inode, false)) {
  2334. ret = -EAGAIN;
  2335. goto out;
  2336. }
  2337. ret = f2fs_convert_inline_inode(inode);
  2338. if (ret)
  2339. goto out;
  2340. set_inode_flag(inode, FI_PIN_FILE);
  2341. ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
  2342. done:
  2343. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  2344. out:
  2345. inode_unlock(inode);
  2346. mnt_drop_write_file(filp);
  2347. return ret;
  2348. }
  2349. static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
  2350. {
  2351. struct inode *inode = file_inode(filp);
  2352. __u32 pin = 0;
  2353. if (is_inode_flag_set(inode, FI_PIN_FILE))
  2354. pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
  2355. return put_user(pin, (u32 __user *)arg);
  2356. }
  2357. int f2fs_precache_extents(struct inode *inode)
  2358. {
  2359. struct f2fs_inode_info *fi = F2FS_I(inode);
  2360. struct f2fs_map_blocks map;
  2361. pgoff_t m_next_extent;
  2362. loff_t end;
  2363. int err;
  2364. if (is_inode_flag_set(inode, FI_NO_EXTENT))
  2365. return -EOPNOTSUPP;
  2366. map.m_lblk = 0;
  2367. map.m_next_pgofs = NULL;
  2368. map.m_next_extent = &m_next_extent;
  2369. map.m_seg_type = NO_CHECK_TYPE;
  2370. end = F2FS_I_SB(inode)->max_file_blocks;
  2371. while (map.m_lblk < end) {
  2372. map.m_len = end - map.m_lblk;
  2373. down_write(&fi->i_gc_rwsem[WRITE]);
  2374. err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
  2375. up_write(&fi->i_gc_rwsem[WRITE]);
  2376. if (err)
  2377. return err;
  2378. map.m_lblk = m_next_extent;
  2379. }
  2380. return err;
  2381. }
  2382. static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
  2383. {
  2384. return f2fs_precache_extents(file_inode(filp));
  2385. }
  2386. long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  2387. {
  2388. if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
  2389. return -EIO;
  2390. switch (cmd) {
  2391. case F2FS_IOC_GETFLAGS:
  2392. return f2fs_ioc_getflags(filp, arg);
  2393. case F2FS_IOC_SETFLAGS:
  2394. return f2fs_ioc_setflags(filp, arg);
  2395. case F2FS_IOC_GETVERSION:
  2396. return f2fs_ioc_getversion(filp, arg);
  2397. case F2FS_IOC_START_ATOMIC_WRITE:
  2398. return f2fs_ioc_start_atomic_write(filp);
  2399. case F2FS_IOC_COMMIT_ATOMIC_WRITE:
  2400. return f2fs_ioc_commit_atomic_write(filp);
  2401. case F2FS_IOC_START_VOLATILE_WRITE:
  2402. return f2fs_ioc_start_volatile_write(filp);
  2403. case F2FS_IOC_RELEASE_VOLATILE_WRITE:
  2404. return f2fs_ioc_release_volatile_write(filp);
  2405. case F2FS_IOC_ABORT_VOLATILE_WRITE:
  2406. return f2fs_ioc_abort_volatile_write(filp);
  2407. case F2FS_IOC_SHUTDOWN:
  2408. return f2fs_ioc_shutdown(filp, arg);
  2409. case FITRIM:
  2410. return f2fs_ioc_fitrim(filp, arg);
  2411. case F2FS_IOC_SET_ENCRYPTION_POLICY:
  2412. return f2fs_ioc_set_encryption_policy(filp, arg);
  2413. case F2FS_IOC_GET_ENCRYPTION_POLICY:
  2414. return f2fs_ioc_get_encryption_policy(filp, arg);
  2415. case F2FS_IOC_GET_ENCRYPTION_PWSALT:
  2416. return f2fs_ioc_get_encryption_pwsalt(filp, arg);
  2417. case F2FS_IOC_GARBAGE_COLLECT:
  2418. return f2fs_ioc_gc(filp, arg);
  2419. case F2FS_IOC_GARBAGE_COLLECT_RANGE:
  2420. return f2fs_ioc_gc_range(filp, arg);
  2421. case F2FS_IOC_WRITE_CHECKPOINT:
  2422. return f2fs_ioc_write_checkpoint(filp, arg);
  2423. case F2FS_IOC_DEFRAGMENT:
  2424. return f2fs_ioc_defragment(filp, arg);
  2425. case F2FS_IOC_MOVE_RANGE:
  2426. return f2fs_ioc_move_range(filp, arg);
  2427. case F2FS_IOC_FLUSH_DEVICE:
  2428. return f2fs_ioc_flush_device(filp, arg);
  2429. case F2FS_IOC_GET_FEATURES:
  2430. return f2fs_ioc_get_features(filp, arg);
  2431. case F2FS_IOC_FSGETXATTR:
  2432. return f2fs_ioc_fsgetxattr(filp, arg);
  2433. case F2FS_IOC_FSSETXATTR:
  2434. return f2fs_ioc_fssetxattr(filp, arg);
  2435. case F2FS_IOC_GET_PIN_FILE:
  2436. return f2fs_ioc_get_pin_file(filp, arg);
  2437. case F2FS_IOC_SET_PIN_FILE:
  2438. return f2fs_ioc_set_pin_file(filp, arg);
  2439. case F2FS_IOC_PRECACHE_EXTENTS:
  2440. return f2fs_ioc_precache_extents(filp, arg);
  2441. default:
  2442. return -ENOTTY;
  2443. }
  2444. }
  2445. static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  2446. {
  2447. struct file *file = iocb->ki_filp;
  2448. struct inode *inode = file_inode(file);
  2449. ssize_t ret;
  2450. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
  2451. return -EIO;
  2452. if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
  2453. return -EINVAL;
  2454. if (!inode_trylock(inode)) {
  2455. if (iocb->ki_flags & IOCB_NOWAIT)
  2456. return -EAGAIN;
  2457. inode_lock(inode);
  2458. }
  2459. ret = generic_write_checks(iocb, from);
  2460. if (ret > 0) {
  2461. bool preallocated = false;
  2462. size_t target_size = 0;
  2463. int err;
  2464. if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
  2465. set_inode_flag(inode, FI_NO_PREALLOC);
  2466. if ((iocb->ki_flags & IOCB_NOWAIT) &&
  2467. (iocb->ki_flags & IOCB_DIRECT)) {
  2468. if (!f2fs_overwrite_io(inode, iocb->ki_pos,
  2469. iov_iter_count(from)) ||
  2470. f2fs_has_inline_data(inode) ||
  2471. f2fs_force_buffered_io(inode, WRITE)) {
  2472. clear_inode_flag(inode,
  2473. FI_NO_PREALLOC);
  2474. inode_unlock(inode);
  2475. return -EAGAIN;
  2476. }
  2477. } else {
  2478. preallocated = true;
  2479. target_size = iocb->ki_pos + iov_iter_count(from);
  2480. err = f2fs_preallocate_blocks(iocb, from);
  2481. if (err) {
  2482. clear_inode_flag(inode, FI_NO_PREALLOC);
  2483. inode_unlock(inode);
  2484. return err;
  2485. }
  2486. }
  2487. ret = __generic_file_write_iter(iocb, from);
  2488. clear_inode_flag(inode, FI_NO_PREALLOC);
  2489. /* if we couldn't write data, we should deallocate blocks. */
  2490. if (preallocated && i_size_read(inode) < target_size)
  2491. f2fs_truncate(inode);
  2492. if (ret > 0)
  2493. f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
  2494. }
  2495. inode_unlock(inode);
  2496. if (ret > 0)
  2497. ret = generic_write_sync(iocb, ret);
  2498. return ret;
  2499. }
  2500. #ifdef CONFIG_COMPAT
  2501. long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2502. {
  2503. switch (cmd) {
  2504. case F2FS_IOC32_GETFLAGS:
  2505. cmd = F2FS_IOC_GETFLAGS;
  2506. break;
  2507. case F2FS_IOC32_SETFLAGS:
  2508. cmd = F2FS_IOC_SETFLAGS;
  2509. break;
  2510. case F2FS_IOC32_GETVERSION:
  2511. cmd = F2FS_IOC_GETVERSION;
  2512. break;
  2513. case F2FS_IOC_START_ATOMIC_WRITE:
  2514. case F2FS_IOC_COMMIT_ATOMIC_WRITE:
  2515. case F2FS_IOC_START_VOLATILE_WRITE:
  2516. case F2FS_IOC_RELEASE_VOLATILE_WRITE:
  2517. case F2FS_IOC_ABORT_VOLATILE_WRITE:
  2518. case F2FS_IOC_SHUTDOWN:
  2519. case F2FS_IOC_SET_ENCRYPTION_POLICY:
  2520. case F2FS_IOC_GET_ENCRYPTION_PWSALT:
  2521. case F2FS_IOC_GET_ENCRYPTION_POLICY:
  2522. case F2FS_IOC_GARBAGE_COLLECT:
  2523. case F2FS_IOC_GARBAGE_COLLECT_RANGE:
  2524. case F2FS_IOC_WRITE_CHECKPOINT:
  2525. case F2FS_IOC_DEFRAGMENT:
  2526. case F2FS_IOC_MOVE_RANGE:
  2527. case F2FS_IOC_FLUSH_DEVICE:
  2528. case F2FS_IOC_GET_FEATURES:
  2529. case F2FS_IOC_FSGETXATTR:
  2530. case F2FS_IOC_FSSETXATTR:
  2531. case F2FS_IOC_GET_PIN_FILE:
  2532. case F2FS_IOC_SET_PIN_FILE:
  2533. case F2FS_IOC_PRECACHE_EXTENTS:
  2534. break;
  2535. default:
  2536. return -ENOIOCTLCMD;
  2537. }
  2538. return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  2539. }
  2540. #endif
  2541. const struct file_operations f2fs_file_operations = {
  2542. .llseek = f2fs_llseek,
  2543. .read_iter = generic_file_read_iter,
  2544. .write_iter = f2fs_file_write_iter,
  2545. .open = f2fs_file_open,
  2546. .release = f2fs_release_file,
  2547. .mmap = f2fs_file_mmap,
  2548. .flush = f2fs_file_flush,
  2549. .fsync = f2fs_sync_file,
  2550. .fallocate = f2fs_fallocate,
  2551. .unlocked_ioctl = f2fs_ioctl,
  2552. #ifdef CONFIG_COMPAT
  2553. .compat_ioctl = f2fs_compat_ioctl,
  2554. #endif
  2555. .splice_read = generic_file_splice_read,
  2556. .splice_write = iter_file_splice_write,
  2557. };