file.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  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/random.h>
  24. #include <linux/aio.h>
  25. #include "f2fs.h"
  26. #include "node.h"
  27. #include "segment.h"
  28. #include "xattr.h"
  29. #include "acl.h"
  30. #include "gc.h"
  31. #include "trace.h"
  32. #include <trace/events/f2fs.h>
  33. static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
  34. struct vm_fault *vmf)
  35. {
  36. struct page *page = vmf->page;
  37. struct inode *inode = file_inode(vma->vm_file);
  38. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  39. struct dnode_of_data dn;
  40. int err;
  41. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  42. f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
  43. /* block allocation */
  44. f2fs_lock_op(sbi);
  45. set_new_dnode(&dn, inode, NULL, NULL, 0);
  46. err = f2fs_reserve_block(&dn, page->index);
  47. if (err) {
  48. f2fs_unlock_op(sbi);
  49. goto out;
  50. }
  51. f2fs_put_dnode(&dn);
  52. f2fs_unlock_op(sbi);
  53. f2fs_balance_fs(sbi, dn.node_changed);
  54. file_update_time(vma->vm_file);
  55. lock_page(page);
  56. if (unlikely(page->mapping != inode->i_mapping ||
  57. page_offset(page) > i_size_read(inode) ||
  58. !PageUptodate(page))) {
  59. unlock_page(page);
  60. err = -EFAULT;
  61. goto out;
  62. }
  63. /*
  64. * check to see if the page is mapped already (no holes)
  65. */
  66. if (PageMappedToDisk(page))
  67. goto mapped;
  68. /* page is wholly or partially inside EOF */
  69. if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
  70. i_size_read(inode)) {
  71. unsigned offset;
  72. offset = i_size_read(inode) & ~PAGE_MASK;
  73. zero_user_segment(page, offset, PAGE_SIZE);
  74. }
  75. set_page_dirty(page);
  76. if (!PageUptodate(page))
  77. SetPageUptodate(page);
  78. trace_f2fs_vm_page_mkwrite(page, DATA);
  79. mapped:
  80. /* fill the page */
  81. f2fs_wait_on_page_writeback(page, DATA, false);
  82. /* wait for GCed encrypted page writeback */
  83. if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
  84. f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
  85. out:
  86. f2fs_update_time(sbi, REQ_TIME);
  87. return block_page_mkwrite_return(err);
  88. }
  89. static const struct vm_operations_struct f2fs_file_vm_ops = {
  90. .fault = filemap_fault,
  91. .page_mkwrite = f2fs_vm_page_mkwrite,
  92. .remap_pages = generic_file_remap_pages,
  93. };
  94. static int get_parent_ino(struct inode *inode, nid_t *pino)
  95. {
  96. struct dentry *dentry;
  97. inode = igrab(inode);
  98. dentry = d_find_any_alias(inode);
  99. iput(inode);
  100. if (!dentry)
  101. return 0;
  102. if (update_dent_inode(inode, inode, &dentry->d_name)) {
  103. dput(dentry);
  104. return 0;
  105. }
  106. *pino = parent_ino(dentry);
  107. dput(dentry);
  108. return 1;
  109. }
  110. static inline bool need_do_checkpoint(struct inode *inode)
  111. {
  112. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  113. bool need_cp = false;
  114. if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
  115. need_cp = true;
  116. else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
  117. need_cp = true;
  118. else if (file_wrong_pino(inode))
  119. need_cp = true;
  120. else if (!space_for_roll_forward(sbi))
  121. need_cp = true;
  122. else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
  123. need_cp = true;
  124. else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
  125. need_cp = true;
  126. else if (test_opt(sbi, FASTBOOT))
  127. need_cp = true;
  128. else if (sbi->active_logs == 2)
  129. need_cp = true;
  130. return need_cp;
  131. }
  132. static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
  133. {
  134. struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
  135. bool ret = false;
  136. /* But we need to avoid that there are some inode updates */
  137. if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
  138. ret = true;
  139. f2fs_put_page(i, 0);
  140. return ret;
  141. }
  142. static void try_to_fix_pino(struct inode *inode)
  143. {
  144. struct f2fs_inode_info *fi = F2FS_I(inode);
  145. nid_t pino;
  146. down_write(&fi->i_sem);
  147. fi->xattr_ver = 0;
  148. if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
  149. get_parent_ino(inode, &pino)) {
  150. f2fs_i_pino_write(inode, pino);
  151. file_got_pino(inode);
  152. }
  153. up_write(&fi->i_sem);
  154. }
  155. static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
  156. int datasync, bool atomic)
  157. {
  158. struct inode *inode = file->f_mapping->host;
  159. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  160. nid_t ino = inode->i_ino;
  161. int ret = 0;
  162. bool need_cp = false;
  163. struct writeback_control wbc = {
  164. .sync_mode = WB_SYNC_ALL,
  165. .nr_to_write = LONG_MAX,
  166. .for_reclaim = 0,
  167. };
  168. if (unlikely(f2fs_readonly(inode->i_sb)))
  169. return 0;
  170. trace_f2fs_sync_file_enter(inode);
  171. /* if fdatasync is triggered, let's do in-place-update */
  172. if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
  173. set_inode_flag(inode, FI_NEED_IPU);
  174. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  175. clear_inode_flag(inode, FI_NEED_IPU);
  176. if (ret) {
  177. trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
  178. return ret;
  179. }
  180. /* if the inode is dirty, let's recover all the time */
  181. if (!f2fs_skip_inode_update(inode, datasync)) {
  182. f2fs_write_inode(inode, NULL);
  183. goto go_write;
  184. }
  185. /*
  186. * if there is no written data, don't waste time to write recovery info.
  187. */
  188. if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
  189. !exist_written_data(sbi, ino, APPEND_INO)) {
  190. /* it may call write_inode just prior to fsync */
  191. if (need_inode_page_update(sbi, ino))
  192. goto go_write;
  193. if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
  194. exist_written_data(sbi, ino, UPDATE_INO))
  195. goto flush_out;
  196. goto out;
  197. }
  198. go_write:
  199. /*
  200. * Both of fdatasync() and fsync() are able to be recovered from
  201. * sudden-power-off.
  202. */
  203. down_read(&F2FS_I(inode)->i_sem);
  204. need_cp = need_do_checkpoint(inode);
  205. up_read(&F2FS_I(inode)->i_sem);
  206. if (need_cp) {
  207. /* all the dirty node pages should be flushed for POR */
  208. ret = f2fs_sync_fs(inode->i_sb, 1);
  209. /*
  210. * We've secured consistency through sync_fs. Following pino
  211. * will be used only for fsynced inodes after checkpoint.
  212. */
  213. try_to_fix_pino(inode);
  214. clear_inode_flag(inode, FI_APPEND_WRITE);
  215. clear_inode_flag(inode, FI_UPDATE_WRITE);
  216. goto out;
  217. }
  218. sync_nodes:
  219. ret = fsync_node_pages(sbi, inode, &wbc, atomic);
  220. if (ret)
  221. goto out;
  222. /* if cp_error was enabled, we should avoid infinite loop */
  223. if (unlikely(f2fs_cp_error(sbi))) {
  224. ret = -EIO;
  225. goto out;
  226. }
  227. if (need_inode_block_update(sbi, ino)) {
  228. f2fs_mark_inode_dirty_sync(inode, true);
  229. f2fs_write_inode(inode, NULL);
  230. goto sync_nodes;
  231. }
  232. ret = wait_on_node_pages_writeback(sbi, ino);
  233. if (ret)
  234. goto out;
  235. /* once recovery info is written, don't need to tack this */
  236. remove_ino_entry(sbi, ino, APPEND_INO);
  237. clear_inode_flag(inode, FI_APPEND_WRITE);
  238. flush_out:
  239. remove_ino_entry(sbi, ino, UPDATE_INO);
  240. clear_inode_flag(inode, FI_UPDATE_WRITE);
  241. ret = f2fs_issue_flush(sbi);
  242. f2fs_update_time(sbi, REQ_TIME);
  243. out:
  244. trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
  245. f2fs_trace_ios(NULL, 1);
  246. return ret;
  247. }
  248. int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  249. {
  250. return f2fs_do_sync_file(file, start, end, datasync, false);
  251. }
  252. static pgoff_t __get_first_dirty_index(struct address_space *mapping,
  253. pgoff_t pgofs, int whence)
  254. {
  255. struct pagevec pvec;
  256. int nr_pages;
  257. if (whence != SEEK_DATA)
  258. return 0;
  259. /* find first dirty page index */
  260. pagevec_init(&pvec, 0);
  261. nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
  262. PAGECACHE_TAG_DIRTY, 1);
  263. pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
  264. pagevec_release(&pvec);
  265. return pgofs;
  266. }
  267. static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
  268. int whence)
  269. {
  270. switch (whence) {
  271. case SEEK_DATA:
  272. if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
  273. (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
  274. return true;
  275. break;
  276. case SEEK_HOLE:
  277. if (blkaddr == NULL_ADDR)
  278. return true;
  279. break;
  280. }
  281. return false;
  282. }
  283. static inline int unsigned_offsets(struct file *file)
  284. {
  285. return file->f_mode & FMODE_UNSIGNED_OFFSET;
  286. }
  287. static loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
  288. {
  289. if (offset < 0 && !unsigned_offsets(file))
  290. return -EINVAL;
  291. if (offset > maxsize)
  292. return -EINVAL;
  293. if (offset != file->f_pos) {
  294. file->f_pos = offset;
  295. file->f_version = 0;
  296. }
  297. return offset;
  298. }
  299. static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
  300. {
  301. struct inode *inode = file->f_mapping->host;
  302. loff_t maxbytes = inode->i_sb->s_maxbytes;
  303. struct dnode_of_data dn;
  304. pgoff_t pgofs, end_offset, dirty;
  305. loff_t data_ofs = offset;
  306. loff_t isize;
  307. int err = 0;
  308. inode_lock(inode);
  309. isize = i_size_read(inode);
  310. if (offset >= isize)
  311. goto fail;
  312. /* handle inline data case */
  313. if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
  314. if (whence == SEEK_HOLE)
  315. data_ofs = isize;
  316. goto found;
  317. }
  318. pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
  319. dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
  320. for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
  321. set_new_dnode(&dn, inode, NULL, NULL, 0);
  322. err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
  323. if (err && err != -ENOENT) {
  324. goto fail;
  325. } else if (err == -ENOENT) {
  326. /* direct node does not exists */
  327. if (whence == SEEK_DATA) {
  328. pgofs = get_next_page_offset(&dn, pgofs);
  329. continue;
  330. } else {
  331. goto found;
  332. }
  333. }
  334. end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
  335. /* find data/hole in dnode block */
  336. for (; dn.ofs_in_node < end_offset;
  337. dn.ofs_in_node++, pgofs++,
  338. data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
  339. block_t blkaddr;
  340. blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
  341. if (__found_offset(blkaddr, dirty, pgofs, whence)) {
  342. f2fs_put_dnode(&dn);
  343. goto found;
  344. }
  345. }
  346. f2fs_put_dnode(&dn);
  347. }
  348. if (whence == SEEK_DATA)
  349. goto fail;
  350. found:
  351. if (whence == SEEK_HOLE && data_ofs > isize)
  352. data_ofs = isize;
  353. inode_unlock(inode);
  354. return vfs_setpos(file, data_ofs, maxbytes);
  355. fail:
  356. inode_unlock(inode);
  357. return -ENXIO;
  358. }
  359. static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
  360. {
  361. struct inode *inode = file->f_mapping->host;
  362. loff_t maxbytes = inode->i_sb->s_maxbytes;
  363. switch (whence) {
  364. case SEEK_SET:
  365. case SEEK_CUR:
  366. case SEEK_END:
  367. return generic_file_llseek_size(file, offset, whence,
  368. maxbytes);
  369. case SEEK_DATA:
  370. case SEEK_HOLE:
  371. if (offset < 0)
  372. return -ENXIO;
  373. return f2fs_seek_block(file, offset, whence);
  374. }
  375. return -EINVAL;
  376. }
  377. static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
  378. {
  379. struct inode *inode = file_inode(file);
  380. int err;
  381. if (f2fs_encrypted_inode(inode)) {
  382. err = fscrypt_get_encryption_info(inode);
  383. if (err)
  384. return 0;
  385. if (!f2fs_encrypted_inode(inode))
  386. return -ENOKEY;
  387. }
  388. /* we don't need to use inline_data strictly */
  389. err = f2fs_convert_inline_inode(inode);
  390. if (err)
  391. return err;
  392. file_accessed(file);
  393. vma->vm_ops = &f2fs_file_vm_ops;
  394. return 0;
  395. }
  396. static int f2fs_file_open(struct inode *inode, struct file *filp)
  397. {
  398. int ret = generic_file_open(inode, filp);
  399. struct dentry *dir;
  400. if (!ret && f2fs_encrypted_inode(inode)) {
  401. ret = fscrypt_get_encryption_info(inode);
  402. if (ret)
  403. return -EACCES;
  404. if (!fscrypt_has_encryption_key(inode))
  405. return -ENOKEY;
  406. }
  407. dir = dget_parent(file_dentry(filp));
  408. if (f2fs_encrypted_inode(d_inode(dir)) &&
  409. !fscrypt_has_permitted_context(d_inode(dir), inode)) {
  410. dput(dir);
  411. return -EPERM;
  412. }
  413. dput(dir);
  414. return ret;
  415. }
  416. int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
  417. {
  418. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  419. struct f2fs_node *raw_node;
  420. int nr_free = 0, ofs = dn->ofs_in_node, len = count;
  421. __le32 *addr;
  422. raw_node = F2FS_NODE(dn->node_page);
  423. addr = blkaddr_in_node(raw_node) + ofs;
  424. for (; count > 0; count--, addr++, dn->ofs_in_node++) {
  425. block_t blkaddr = le32_to_cpu(*addr);
  426. if (blkaddr == NULL_ADDR)
  427. continue;
  428. dn->data_blkaddr = NULL_ADDR;
  429. set_data_blkaddr(dn);
  430. invalidate_blocks(sbi, blkaddr);
  431. if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
  432. clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
  433. nr_free++;
  434. }
  435. if (nr_free) {
  436. pgoff_t fofs;
  437. /*
  438. * once we invalidate valid blkaddr in range [ofs, ofs + count],
  439. * we will invalidate all blkaddr in the whole range.
  440. */
  441. fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
  442. dn->inode) + ofs;
  443. f2fs_update_extent_cache_range(dn, fofs, 0, len);
  444. dec_valid_block_count(sbi, dn->inode, nr_free);
  445. }
  446. dn->ofs_in_node = ofs;
  447. f2fs_update_time(sbi, REQ_TIME);
  448. trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
  449. dn->ofs_in_node, nr_free);
  450. return nr_free;
  451. }
  452. void truncate_data_blocks(struct dnode_of_data *dn)
  453. {
  454. truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
  455. }
  456. static int truncate_partial_data_page(struct inode *inode, u64 from,
  457. bool cache_only)
  458. {
  459. unsigned offset = from & (PAGE_SIZE - 1);
  460. pgoff_t index = from >> PAGE_SHIFT;
  461. struct address_space *mapping = inode->i_mapping;
  462. struct page *page;
  463. if (!offset && !cache_only)
  464. return 0;
  465. if (cache_only) {
  466. page = find_lock_page(mapping, index);
  467. if (page && PageUptodate(page))
  468. goto truncate_out;
  469. f2fs_put_page(page, 1);
  470. return 0;
  471. }
  472. page = get_lock_data_page(inode, index, true);
  473. if (IS_ERR(page))
  474. return 0;
  475. truncate_out:
  476. f2fs_wait_on_page_writeback(page, DATA, true);
  477. zero_user(page, offset, PAGE_SIZE - offset);
  478. if (!cache_only || !f2fs_encrypted_inode(inode) ||
  479. !S_ISREG(inode->i_mode))
  480. set_page_dirty(page);
  481. f2fs_put_page(page, 1);
  482. return 0;
  483. }
  484. int truncate_blocks(struct inode *inode, u64 from, bool lock)
  485. {
  486. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  487. unsigned int blocksize = inode->i_sb->s_blocksize;
  488. struct dnode_of_data dn;
  489. pgoff_t free_from;
  490. int count = 0, err = 0;
  491. struct page *ipage;
  492. bool truncate_page = false;
  493. trace_f2fs_truncate_blocks_enter(inode, from);
  494. free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
  495. if (free_from >= sbi->max_file_blocks)
  496. goto free_partial;
  497. if (lock)
  498. f2fs_lock_op(sbi);
  499. ipage = get_node_page(sbi, inode->i_ino);
  500. if (IS_ERR(ipage)) {
  501. err = PTR_ERR(ipage);
  502. goto out;
  503. }
  504. if (f2fs_has_inline_data(inode)) {
  505. if (truncate_inline_inode(ipage, from))
  506. set_page_dirty(ipage);
  507. f2fs_put_page(ipage, 1);
  508. truncate_page = true;
  509. goto out;
  510. }
  511. set_new_dnode(&dn, inode, ipage, NULL, 0);
  512. err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
  513. if (err) {
  514. if (err == -ENOENT)
  515. goto free_next;
  516. goto out;
  517. }
  518. count = ADDRS_PER_PAGE(dn.node_page, inode);
  519. count -= dn.ofs_in_node;
  520. f2fs_bug_on(sbi, count < 0);
  521. if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
  522. truncate_data_blocks_range(&dn, count);
  523. free_from += count;
  524. }
  525. f2fs_put_dnode(&dn);
  526. free_next:
  527. err = truncate_inode_blocks(inode, free_from);
  528. out:
  529. if (lock)
  530. f2fs_unlock_op(sbi);
  531. free_partial:
  532. /* lastly zero out the first data page */
  533. if (!err)
  534. err = truncate_partial_data_page(inode, from, truncate_page);
  535. trace_f2fs_truncate_blocks_exit(inode, err);
  536. return err;
  537. }
  538. int f2fs_truncate(struct inode *inode)
  539. {
  540. int err;
  541. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  542. S_ISLNK(inode->i_mode)))
  543. return 0;
  544. trace_f2fs_truncate(inode);
  545. /* we should check inline_data size */
  546. if (!f2fs_may_inline_data(inode)) {
  547. err = f2fs_convert_inline_inode(inode);
  548. if (err)
  549. return err;
  550. }
  551. err = truncate_blocks(inode, i_size_read(inode), true);
  552. if (err)
  553. return err;
  554. inode->i_mtime = inode->i_ctime = current_time(inode);
  555. f2fs_mark_inode_dirty_sync(inode, false);
  556. return 0;
  557. }
  558. int f2fs_getattr(struct vfsmount *mnt,
  559. struct dentry *dentry, struct kstat *stat)
  560. {
  561. struct inode *inode = d_inode(dentry);
  562. generic_fillattr(inode, stat);
  563. stat->blocks <<= 3;
  564. return 0;
  565. }
  566. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  567. static void __setattr_copy(struct inode *inode, const struct iattr *attr)
  568. {
  569. unsigned int ia_valid = attr->ia_valid;
  570. if (ia_valid & ATTR_UID)
  571. inode->i_uid = attr->ia_uid;
  572. if (ia_valid & ATTR_GID)
  573. inode->i_gid = attr->ia_gid;
  574. if (ia_valid & ATTR_ATIME)
  575. inode->i_atime = timespec_trunc(attr->ia_atime,
  576. inode->i_sb->s_time_gran);
  577. if (ia_valid & ATTR_MTIME)
  578. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  579. inode->i_sb->s_time_gran);
  580. if (ia_valid & ATTR_CTIME)
  581. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  582. inode->i_sb->s_time_gran);
  583. if (ia_valid & ATTR_MODE) {
  584. umode_t mode = attr->ia_mode;
  585. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  586. mode &= ~S_ISGID;
  587. set_acl_inode(inode, mode);
  588. }
  589. }
  590. #else
  591. #define __setattr_copy setattr_copy
  592. #endif
  593. int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
  594. {
  595. struct inode *inode = d_inode(dentry);
  596. int err;
  597. bool size_changed = false;
  598. err = inode_change_ok(inode, attr);
  599. if (err)
  600. return err;
  601. if (attr->ia_valid & ATTR_SIZE) {
  602. if (f2fs_encrypted_inode(inode) &&
  603. fscrypt_get_encryption_info(inode))
  604. return -EACCES;
  605. if (attr->ia_size <= i_size_read(inode)) {
  606. truncate_setsize(inode, attr->ia_size);
  607. err = f2fs_truncate(inode);
  608. if (err)
  609. return err;
  610. } else {
  611. /*
  612. * do not trim all blocks after i_size if target size is
  613. * larger than i_size.
  614. */
  615. truncate_setsize(inode, attr->ia_size);
  616. /* should convert inline inode here */
  617. if (!f2fs_may_inline_data(inode)) {
  618. err = f2fs_convert_inline_inode(inode);
  619. if (err)
  620. return err;
  621. }
  622. inode->i_mtime = inode->i_ctime = current_time(inode);
  623. }
  624. size_changed = true;
  625. }
  626. __setattr_copy(inode, attr);
  627. if (attr->ia_valid & ATTR_MODE) {
  628. err = f2fs_acl_chmod(inode);
  629. if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
  630. inode->i_mode = F2FS_I(inode)->i_acl_mode;
  631. clear_inode_flag(inode, FI_ACL_MODE);
  632. }
  633. }
  634. /* file size may changed here */
  635. f2fs_mark_inode_dirty_sync(inode, size_changed);
  636. /* inode change will produce dirty node pages flushed by checkpoint */
  637. f2fs_balance_fs(F2FS_I_SB(inode), true);
  638. return err;
  639. }
  640. const struct inode_operations f2fs_file_inode_operations = {
  641. .getattr = f2fs_getattr,
  642. .setattr = f2fs_setattr,
  643. .get_acl = f2fs_get_acl,
  644. #ifdef CONFIG_F2FS_FS_XATTR
  645. .setxattr = generic_setxattr,
  646. .getxattr = generic_getxattr,
  647. .listxattr = f2fs_listxattr,
  648. .removexattr = generic_removexattr,
  649. #endif
  650. .fiemap = f2fs_fiemap,
  651. };
  652. static int fill_zero(struct inode *inode, pgoff_t index,
  653. loff_t start, loff_t len)
  654. {
  655. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  656. struct page *page;
  657. if (!len)
  658. return 0;
  659. f2fs_balance_fs(sbi, true);
  660. f2fs_lock_op(sbi);
  661. page = get_new_data_page(inode, NULL, index, false);
  662. f2fs_unlock_op(sbi);
  663. if (IS_ERR(page))
  664. return PTR_ERR(page);
  665. f2fs_wait_on_page_writeback(page, DATA, true);
  666. zero_user(page, start, len);
  667. set_page_dirty(page);
  668. f2fs_put_page(page, 1);
  669. return 0;
  670. }
  671. int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
  672. {
  673. int err;
  674. while (pg_start < pg_end) {
  675. struct dnode_of_data dn;
  676. pgoff_t end_offset, count;
  677. set_new_dnode(&dn, inode, NULL, NULL, 0);
  678. err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
  679. if (err) {
  680. if (err == -ENOENT) {
  681. pg_start++;
  682. continue;
  683. }
  684. return err;
  685. }
  686. end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
  687. count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
  688. f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
  689. truncate_data_blocks_range(&dn, count);
  690. f2fs_put_dnode(&dn);
  691. pg_start += count;
  692. }
  693. return 0;
  694. }
  695. static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
  696. {
  697. pgoff_t pg_start, pg_end;
  698. loff_t off_start, off_end;
  699. int ret;
  700. ret = f2fs_convert_inline_inode(inode);
  701. if (ret)
  702. return ret;
  703. pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
  704. pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
  705. off_start = offset & (PAGE_SIZE - 1);
  706. off_end = (offset + len) & (PAGE_SIZE - 1);
  707. if (pg_start == pg_end) {
  708. ret = fill_zero(inode, pg_start, off_start,
  709. off_end - off_start);
  710. if (ret)
  711. return ret;
  712. } else {
  713. if (off_start) {
  714. ret = fill_zero(inode, pg_start++, off_start,
  715. PAGE_SIZE - off_start);
  716. if (ret)
  717. return ret;
  718. }
  719. if (off_end) {
  720. ret = fill_zero(inode, pg_end, 0, off_end);
  721. if (ret)
  722. return ret;
  723. }
  724. if (pg_start < pg_end) {
  725. struct address_space *mapping = inode->i_mapping;
  726. loff_t blk_start, blk_end;
  727. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  728. f2fs_balance_fs(sbi, true);
  729. blk_start = (loff_t)pg_start << PAGE_SHIFT;
  730. blk_end = (loff_t)pg_end << PAGE_SHIFT;
  731. truncate_inode_pages_range(mapping, blk_start,
  732. blk_end - 1);
  733. f2fs_lock_op(sbi);
  734. ret = truncate_hole(inode, pg_start, pg_end);
  735. f2fs_unlock_op(sbi);
  736. }
  737. }
  738. return ret;
  739. }
  740. static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
  741. int *do_replace, pgoff_t off, pgoff_t len)
  742. {
  743. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  744. struct dnode_of_data dn;
  745. int ret, done, i;
  746. next_dnode:
  747. set_new_dnode(&dn, inode, NULL, NULL, 0);
  748. ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
  749. if (ret && ret != -ENOENT) {
  750. return ret;
  751. } else if (ret == -ENOENT) {
  752. if (dn.max_level == 0)
  753. return -ENOENT;
  754. done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
  755. blkaddr += done;
  756. do_replace += done;
  757. goto next;
  758. }
  759. done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
  760. dn.ofs_in_node, len);
  761. for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
  762. *blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
  763. if (!is_checkpointed_data(sbi, *blkaddr)) {
  764. if (test_opt(sbi, LFS)) {
  765. f2fs_put_dnode(&dn);
  766. return -ENOTSUPP;
  767. }
  768. /* do not invalidate this block address */
  769. f2fs_update_data_blkaddr(&dn, NULL_ADDR);
  770. *do_replace = 1;
  771. }
  772. }
  773. f2fs_put_dnode(&dn);
  774. next:
  775. len -= done;
  776. off += done;
  777. if (len)
  778. goto next_dnode;
  779. return 0;
  780. }
  781. static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
  782. int *do_replace, pgoff_t off, int len)
  783. {
  784. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  785. struct dnode_of_data dn;
  786. int ret, i;
  787. for (i = 0; i < len; i++, do_replace++, blkaddr++) {
  788. if (*do_replace == 0)
  789. continue;
  790. set_new_dnode(&dn, inode, NULL, NULL, 0);
  791. ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
  792. if (ret) {
  793. dec_valid_block_count(sbi, inode, 1);
  794. invalidate_blocks(sbi, *blkaddr);
  795. } else {
  796. f2fs_update_data_blkaddr(&dn, *blkaddr);
  797. }
  798. f2fs_put_dnode(&dn);
  799. }
  800. return 0;
  801. }
  802. static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
  803. block_t *blkaddr, int *do_replace,
  804. pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
  805. {
  806. struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
  807. pgoff_t i = 0;
  808. int ret;
  809. while (i < len) {
  810. if (blkaddr[i] == NULL_ADDR && !full) {
  811. i++;
  812. continue;
  813. }
  814. if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
  815. struct dnode_of_data dn;
  816. struct node_info ni;
  817. size_t new_size;
  818. pgoff_t ilen;
  819. set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
  820. ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
  821. if (ret)
  822. return ret;
  823. get_node_info(sbi, dn.nid, &ni);
  824. ilen = min((pgoff_t)
  825. ADDRS_PER_PAGE(dn.node_page, dst_inode) -
  826. dn.ofs_in_node, len - i);
  827. do {
  828. dn.data_blkaddr = datablock_addr(dn.node_page,
  829. dn.ofs_in_node);
  830. truncate_data_blocks_range(&dn, 1);
  831. if (do_replace[i]) {
  832. f2fs_i_blocks_write(src_inode,
  833. 1, false);
  834. f2fs_i_blocks_write(dst_inode,
  835. 1, true);
  836. f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
  837. blkaddr[i], ni.version, true, false);
  838. do_replace[i] = 0;
  839. }
  840. dn.ofs_in_node++;
  841. i++;
  842. new_size = (dst + i) << PAGE_SHIFT;
  843. if (dst_inode->i_size < new_size)
  844. f2fs_i_size_write(dst_inode, new_size);
  845. } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
  846. f2fs_put_dnode(&dn);
  847. } else {
  848. struct page *psrc, *pdst;
  849. psrc = get_lock_data_page(src_inode, src + i, true);
  850. if (IS_ERR(psrc))
  851. return PTR_ERR(psrc);
  852. pdst = get_new_data_page(dst_inode, NULL, dst + i,
  853. true);
  854. if (IS_ERR(pdst)) {
  855. f2fs_put_page(psrc, 1);
  856. return PTR_ERR(pdst);
  857. }
  858. f2fs_copy_page(psrc, pdst);
  859. set_page_dirty(pdst);
  860. f2fs_put_page(pdst, 1);
  861. f2fs_put_page(psrc, 1);
  862. ret = truncate_hole(src_inode, src + i, src + i + 1);
  863. if (ret)
  864. return ret;
  865. i++;
  866. }
  867. }
  868. return 0;
  869. }
  870. static int __exchange_data_block(struct inode *src_inode,
  871. struct inode *dst_inode, pgoff_t src, pgoff_t dst,
  872. pgoff_t len, bool full)
  873. {
  874. block_t *src_blkaddr;
  875. int *do_replace;
  876. pgoff_t olen;
  877. int ret;
  878. while (len) {
  879. olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
  880. src_blkaddr = f2fs_kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
  881. if (!src_blkaddr)
  882. return -ENOMEM;
  883. do_replace = f2fs_kvzalloc(sizeof(int) * olen, GFP_KERNEL);
  884. if (!do_replace) {
  885. f2fs_kvfree(src_blkaddr);
  886. return -ENOMEM;
  887. }
  888. ret = __read_out_blkaddrs(src_inode, src_blkaddr,
  889. do_replace, src, olen);
  890. if (ret)
  891. goto roll_back;
  892. ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
  893. do_replace, src, dst, olen, full);
  894. if (ret)
  895. goto roll_back;
  896. src += olen;
  897. dst += olen;
  898. len -= olen;
  899. f2fs_kvfree(src_blkaddr);
  900. f2fs_kvfree(do_replace);
  901. }
  902. return 0;
  903. roll_back:
  904. __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
  905. f2fs_kvfree(src_blkaddr);
  906. f2fs_kvfree(do_replace);
  907. return ret;
  908. }
  909. static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
  910. {
  911. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  912. pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
  913. int ret;
  914. f2fs_balance_fs(sbi, true);
  915. f2fs_lock_op(sbi);
  916. f2fs_drop_extent_tree(inode);
  917. ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
  918. f2fs_unlock_op(sbi);
  919. return ret;
  920. }
  921. static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
  922. {
  923. pgoff_t pg_start, pg_end;
  924. loff_t new_size;
  925. int ret;
  926. if (offset + len >= i_size_read(inode))
  927. return -EINVAL;
  928. /* collapse range should be aligned to block size of f2fs. */
  929. if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
  930. return -EINVAL;
  931. ret = f2fs_convert_inline_inode(inode);
  932. if (ret)
  933. return ret;
  934. pg_start = offset >> PAGE_SHIFT;
  935. pg_end = (offset + len) >> PAGE_SHIFT;
  936. /* write out all dirty pages from offset */
  937. ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  938. if (ret)
  939. return ret;
  940. truncate_pagecache(inode, 0, offset);
  941. ret = f2fs_do_collapse(inode, pg_start, pg_end);
  942. if (ret)
  943. return ret;
  944. /* write out all moved pages, if possible */
  945. filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  946. truncate_pagecache(inode, 0, offset);
  947. new_size = i_size_read(inode) - len;
  948. truncate_pagecache(inode, 0, new_size);
  949. ret = truncate_blocks(inode, new_size, true);
  950. if (!ret)
  951. f2fs_i_size_write(inode, new_size);
  952. return ret;
  953. }
  954. static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
  955. pgoff_t end)
  956. {
  957. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  958. pgoff_t index = start;
  959. unsigned int ofs_in_node = dn->ofs_in_node;
  960. blkcnt_t count = 0;
  961. int ret;
  962. for (; index < end; index++, dn->ofs_in_node++) {
  963. if (datablock_addr(dn->node_page, dn->ofs_in_node) == NULL_ADDR)
  964. count++;
  965. }
  966. dn->ofs_in_node = ofs_in_node;
  967. ret = reserve_new_blocks(dn, count);
  968. if (ret)
  969. return ret;
  970. dn->ofs_in_node = ofs_in_node;
  971. for (index = start; index < end; index++, dn->ofs_in_node++) {
  972. dn->data_blkaddr =
  973. datablock_addr(dn->node_page, dn->ofs_in_node);
  974. /*
  975. * reserve_new_blocks will not guarantee entire block
  976. * allocation.
  977. */
  978. if (dn->data_blkaddr == NULL_ADDR) {
  979. ret = -ENOSPC;
  980. break;
  981. }
  982. if (dn->data_blkaddr != NEW_ADDR) {
  983. invalidate_blocks(sbi, dn->data_blkaddr);
  984. dn->data_blkaddr = NEW_ADDR;
  985. set_data_blkaddr(dn);
  986. }
  987. }
  988. f2fs_update_extent_cache_range(dn, start, 0, index - start);
  989. return ret;
  990. }
  991. static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
  992. int mode)
  993. {
  994. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  995. struct address_space *mapping = inode->i_mapping;
  996. pgoff_t index, pg_start, pg_end;
  997. loff_t new_size = i_size_read(inode);
  998. loff_t off_start, off_end;
  999. int ret = 0;
  1000. ret = inode_newsize_ok(inode, (len + offset));
  1001. if (ret)
  1002. return ret;
  1003. ret = f2fs_convert_inline_inode(inode);
  1004. if (ret)
  1005. return ret;
  1006. ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
  1007. if (ret)
  1008. return ret;
  1009. truncate_pagecache_range(inode, offset, offset + len - 1);
  1010. pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
  1011. pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
  1012. off_start = offset & (PAGE_SIZE - 1);
  1013. off_end = (offset + len) & (PAGE_SIZE - 1);
  1014. if (pg_start == pg_end) {
  1015. ret = fill_zero(inode, pg_start, off_start,
  1016. off_end - off_start);
  1017. if (ret)
  1018. return ret;
  1019. if (offset + len > new_size)
  1020. new_size = offset + len;
  1021. new_size = max_t(loff_t, new_size, offset + len);
  1022. } else {
  1023. if (off_start) {
  1024. ret = fill_zero(inode, pg_start++, off_start,
  1025. PAGE_SIZE - off_start);
  1026. if (ret)
  1027. return ret;
  1028. new_size = max_t(loff_t, new_size,
  1029. (loff_t)pg_start << PAGE_SHIFT);
  1030. }
  1031. for (index = pg_start; index < pg_end;) {
  1032. struct dnode_of_data dn;
  1033. unsigned int end_offset;
  1034. pgoff_t end;
  1035. f2fs_lock_op(sbi);
  1036. set_new_dnode(&dn, inode, NULL, NULL, 0);
  1037. ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
  1038. if (ret) {
  1039. f2fs_unlock_op(sbi);
  1040. goto out;
  1041. }
  1042. end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
  1043. end = min(pg_end, end_offset - dn.ofs_in_node + index);
  1044. ret = f2fs_do_zero_range(&dn, index, end);
  1045. f2fs_put_dnode(&dn);
  1046. f2fs_unlock_op(sbi);
  1047. f2fs_balance_fs(sbi, dn.node_changed);
  1048. if (ret)
  1049. goto out;
  1050. index = end;
  1051. new_size = max_t(loff_t, new_size,
  1052. (loff_t)index << PAGE_SHIFT);
  1053. }
  1054. if (off_end) {
  1055. ret = fill_zero(inode, pg_end, 0, off_end);
  1056. if (ret)
  1057. goto out;
  1058. new_size = max_t(loff_t, new_size, offset + len);
  1059. }
  1060. }
  1061. out:
  1062. if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
  1063. f2fs_i_size_write(inode, new_size);
  1064. return ret;
  1065. }
  1066. static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
  1067. {
  1068. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1069. pgoff_t nr, pg_start, pg_end, delta, idx;
  1070. loff_t new_size;
  1071. int ret = 0;
  1072. new_size = i_size_read(inode) + len;
  1073. if (new_size > inode->i_sb->s_maxbytes)
  1074. return -EFBIG;
  1075. if (offset >= i_size_read(inode))
  1076. return -EINVAL;
  1077. /* insert range should be aligned to block size of f2fs. */
  1078. if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
  1079. return -EINVAL;
  1080. ret = f2fs_convert_inline_inode(inode);
  1081. if (ret)
  1082. return ret;
  1083. f2fs_balance_fs(sbi, true);
  1084. ret = truncate_blocks(inode, i_size_read(inode), true);
  1085. if (ret)
  1086. return ret;
  1087. /* write out all dirty pages from offset */
  1088. ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  1089. if (ret)
  1090. return ret;
  1091. truncate_pagecache(inode, 0, offset);
  1092. pg_start = offset >> PAGE_SHIFT;
  1093. pg_end = (offset + len) >> PAGE_SHIFT;
  1094. delta = pg_end - pg_start;
  1095. idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
  1096. while (!ret && idx > pg_start) {
  1097. nr = idx - pg_start;
  1098. if (nr > delta)
  1099. nr = delta;
  1100. idx -= nr;
  1101. f2fs_lock_op(sbi);
  1102. f2fs_drop_extent_tree(inode);
  1103. ret = __exchange_data_block(inode, inode, idx,
  1104. idx + delta, nr, false);
  1105. f2fs_unlock_op(sbi);
  1106. }
  1107. /* write out all moved pages, if possible */
  1108. filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  1109. truncate_pagecache(inode, 0, offset);
  1110. if (!ret)
  1111. f2fs_i_size_write(inode, new_size);
  1112. return ret;
  1113. }
  1114. static int expand_inode_data(struct inode *inode, loff_t offset,
  1115. loff_t len, int mode)
  1116. {
  1117. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1118. struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
  1119. pgoff_t pg_end;
  1120. loff_t new_size = i_size_read(inode);
  1121. loff_t off_end;
  1122. int err;
  1123. err = inode_newsize_ok(inode, (len + offset));
  1124. if (err)
  1125. return err;
  1126. err = f2fs_convert_inline_inode(inode);
  1127. if (err)
  1128. return err;
  1129. f2fs_balance_fs(sbi, true);
  1130. pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
  1131. off_end = (offset + len) & (PAGE_SIZE - 1);
  1132. map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
  1133. map.m_len = pg_end - map.m_lblk;
  1134. if (off_end)
  1135. map.m_len++;
  1136. err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
  1137. if (err) {
  1138. pgoff_t last_off;
  1139. if (!map.m_len)
  1140. return err;
  1141. last_off = map.m_lblk + map.m_len - 1;
  1142. /* update new size to the failed position */
  1143. new_size = (last_off == pg_end) ? offset + len:
  1144. (loff_t)(last_off + 1) << PAGE_SHIFT;
  1145. } else {
  1146. new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
  1147. }
  1148. if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
  1149. f2fs_i_size_write(inode, new_size);
  1150. return err;
  1151. }
  1152. #ifndef FALLOC_FL_COLLAPSE_RANGE
  1153. #define FALLOC_FL_COLLAPSE_RANGE 0X08
  1154. #endif
  1155. #ifndef FALLOC_FL_ZERO_RANGE
  1156. #define FALLOC_FL_ZERO_RANGE 0X10
  1157. #endif
  1158. #ifndef FALLOC_FL_INSERT_RANGE
  1159. #define FALLOC_FL_INSERT_RANGE 0X20
  1160. #endif
  1161. static long f2fs_fallocate(struct file *file, int mode,
  1162. loff_t offset, loff_t len)
  1163. {
  1164. struct inode *inode = file_inode(file);
  1165. long ret = 0;
  1166. /* f2fs only support ->fallocate for regular file */
  1167. if (!S_ISREG(inode->i_mode))
  1168. return -EINVAL;
  1169. if (f2fs_encrypted_inode(inode) &&
  1170. (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
  1171. return -EOPNOTSUPP;
  1172. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
  1173. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
  1174. FALLOC_FL_INSERT_RANGE))
  1175. return -EOPNOTSUPP;
  1176. inode_lock(inode);
  1177. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1178. if (offset >= inode->i_size)
  1179. goto out;
  1180. ret = punch_hole(inode, offset, len);
  1181. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  1182. ret = f2fs_collapse_range(inode, offset, len);
  1183. } else if (mode & FALLOC_FL_ZERO_RANGE) {
  1184. ret = f2fs_zero_range(inode, offset, len, mode);
  1185. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  1186. ret = f2fs_insert_range(inode, offset, len);
  1187. } else {
  1188. ret = expand_inode_data(inode, offset, len, mode);
  1189. }
  1190. if (!ret) {
  1191. inode->i_mtime = inode->i_ctime = current_time(inode);
  1192. f2fs_mark_inode_dirty_sync(inode, false);
  1193. if (mode & FALLOC_FL_KEEP_SIZE)
  1194. file_set_keep_isize(inode);
  1195. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1196. }
  1197. out:
  1198. inode_unlock(inode);
  1199. trace_f2fs_fallocate(inode, mode, offset, len, ret);
  1200. return ret;
  1201. }
  1202. static int f2fs_release_file(struct inode *inode, struct file *filp)
  1203. {
  1204. /*
  1205. * f2fs_relase_file is called at every close calls. So we should
  1206. * not drop any inmemory pages by close called by other process.
  1207. */
  1208. if (!(filp->f_mode & FMODE_WRITE) ||
  1209. atomic_read(&inode->i_writecount) != 1)
  1210. return 0;
  1211. /* some remained atomic pages should discarded */
  1212. if (f2fs_is_atomic_file(inode))
  1213. drop_inmem_pages(inode);
  1214. if (f2fs_is_volatile_file(inode)) {
  1215. clear_inode_flag(inode, FI_VOLATILE_FILE);
  1216. set_inode_flag(inode, FI_DROP_CACHE);
  1217. filemap_fdatawrite(inode->i_mapping);
  1218. clear_inode_flag(inode, FI_DROP_CACHE);
  1219. }
  1220. return 0;
  1221. }
  1222. #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
  1223. #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
  1224. static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
  1225. {
  1226. if (S_ISDIR(mode))
  1227. return flags;
  1228. else if (S_ISREG(mode))
  1229. return flags & F2FS_REG_FLMASK;
  1230. else
  1231. return flags & F2FS_OTHER_FLMASK;
  1232. }
  1233. static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
  1234. {
  1235. struct inode *inode = file_inode(filp);
  1236. struct f2fs_inode_info *fi = F2FS_I(inode);
  1237. unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
  1238. return put_user(flags, (int __user *)arg);
  1239. }
  1240. static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
  1241. {
  1242. struct inode *inode = file_inode(filp);
  1243. struct f2fs_inode_info *fi = F2FS_I(inode);
  1244. unsigned int flags;
  1245. unsigned int oldflags;
  1246. int ret;
  1247. if (!inode_owner_or_capable(inode))
  1248. return -EACCES;
  1249. if (get_user(flags, (int __user *)arg))
  1250. return -EFAULT;
  1251. ret = mnt_want_write_file(filp);
  1252. if (ret)
  1253. return ret;
  1254. flags = f2fs_mask_flags(inode->i_mode, flags);
  1255. inode_lock(inode);
  1256. oldflags = fi->i_flags;
  1257. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  1258. if (!capable(CAP_LINUX_IMMUTABLE)) {
  1259. inode_unlock(inode);
  1260. ret = -EPERM;
  1261. goto out;
  1262. }
  1263. }
  1264. flags = flags & FS_FL_USER_MODIFIABLE;
  1265. flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
  1266. fi->i_flags = flags;
  1267. inode_unlock(inode);
  1268. inode->i_ctime = current_time(inode);
  1269. f2fs_set_inode_flags(inode);
  1270. out:
  1271. mnt_drop_write_file(filp);
  1272. return ret;
  1273. }
  1274. static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
  1275. {
  1276. struct inode *inode = file_inode(filp);
  1277. return put_user(inode->i_generation, (int __user *)arg);
  1278. }
  1279. static int f2fs_ioc_start_atomic_write(struct file *filp)
  1280. {
  1281. struct inode *inode = file_inode(filp);
  1282. int ret;
  1283. if (!inode_owner_or_capable(inode))
  1284. return -EACCES;
  1285. ret = mnt_want_write_file(filp);
  1286. if (ret)
  1287. return ret;
  1288. inode_lock(inode);
  1289. if (f2fs_is_atomic_file(inode))
  1290. goto out;
  1291. ret = f2fs_convert_inline_inode(inode);
  1292. if (ret)
  1293. goto out;
  1294. set_inode_flag(inode, FI_ATOMIC_FILE);
  1295. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1296. if (!get_dirty_pages(inode))
  1297. goto out;
  1298. f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
  1299. "Unexpected flush for atomic writes: ino=%lu, npages=%lld",
  1300. inode->i_ino, get_dirty_pages(inode));
  1301. ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
  1302. if (ret)
  1303. clear_inode_flag(inode, FI_ATOMIC_FILE);
  1304. out:
  1305. inode_unlock(inode);
  1306. mnt_drop_write_file(filp);
  1307. return ret;
  1308. }
  1309. static int f2fs_ioc_commit_atomic_write(struct file *filp)
  1310. {
  1311. struct inode *inode = file_inode(filp);
  1312. int ret;
  1313. if (!inode_owner_or_capable(inode))
  1314. return -EACCES;
  1315. ret = mnt_want_write_file(filp);
  1316. if (ret)
  1317. return ret;
  1318. inode_lock(inode);
  1319. if (f2fs_is_volatile_file(inode))
  1320. goto err_out;
  1321. if (f2fs_is_atomic_file(inode)) {
  1322. clear_inode_flag(inode, FI_ATOMIC_FILE);
  1323. ret = commit_inmem_pages(inode);
  1324. if (ret) {
  1325. set_inode_flag(inode, FI_ATOMIC_FILE);
  1326. goto err_out;
  1327. }
  1328. }
  1329. ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
  1330. err_out:
  1331. inode_unlock(inode);
  1332. mnt_drop_write_file(filp);
  1333. return ret;
  1334. }
  1335. static int f2fs_ioc_start_volatile_write(struct file *filp)
  1336. {
  1337. struct inode *inode = file_inode(filp);
  1338. int ret;
  1339. if (!inode_owner_or_capable(inode))
  1340. return -EACCES;
  1341. ret = mnt_want_write_file(filp);
  1342. if (ret)
  1343. return ret;
  1344. inode_lock(inode);
  1345. if (f2fs_is_volatile_file(inode))
  1346. goto out;
  1347. ret = f2fs_convert_inline_inode(inode);
  1348. if (ret)
  1349. goto out;
  1350. set_inode_flag(inode, FI_VOLATILE_FILE);
  1351. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1352. out:
  1353. inode_unlock(inode);
  1354. mnt_drop_write_file(filp);
  1355. return ret;
  1356. }
  1357. static int f2fs_ioc_release_volatile_write(struct file *filp)
  1358. {
  1359. struct inode *inode = file_inode(filp);
  1360. int ret;
  1361. if (!inode_owner_or_capable(inode))
  1362. return -EACCES;
  1363. ret = mnt_want_write_file(filp);
  1364. if (ret)
  1365. return ret;
  1366. inode_lock(inode);
  1367. if (!f2fs_is_volatile_file(inode))
  1368. goto out;
  1369. if (!f2fs_is_first_block_written(inode)) {
  1370. ret = truncate_partial_data_page(inode, 0, true);
  1371. goto out;
  1372. }
  1373. ret = punch_hole(inode, 0, F2FS_BLKSIZE);
  1374. out:
  1375. inode_unlock(inode);
  1376. mnt_drop_write_file(filp);
  1377. return ret;
  1378. }
  1379. static int f2fs_ioc_abort_volatile_write(struct file *filp)
  1380. {
  1381. struct inode *inode = file_inode(filp);
  1382. int ret;
  1383. if (!inode_owner_or_capable(inode))
  1384. return -EACCES;
  1385. ret = mnt_want_write_file(filp);
  1386. if (ret)
  1387. return ret;
  1388. inode_lock(inode);
  1389. if (f2fs_is_atomic_file(inode))
  1390. drop_inmem_pages(inode);
  1391. if (f2fs_is_volatile_file(inode)) {
  1392. clear_inode_flag(inode, FI_VOLATILE_FILE);
  1393. ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
  1394. }
  1395. inode_unlock(inode);
  1396. mnt_drop_write_file(filp);
  1397. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1398. return ret;
  1399. }
  1400. static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
  1401. {
  1402. struct inode *inode = file_inode(filp);
  1403. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1404. struct super_block *sb = sbi->sb;
  1405. __u32 in;
  1406. int ret;
  1407. if (!capable(CAP_SYS_ADMIN))
  1408. return -EPERM;
  1409. if (get_user(in, (__u32 __user *)arg))
  1410. return -EFAULT;
  1411. ret = mnt_want_write_file(filp);
  1412. if (ret)
  1413. return ret;
  1414. switch (in) {
  1415. case F2FS_GOING_DOWN_FULLSYNC:
  1416. sb = freeze_bdev(sb->s_bdev);
  1417. if (sb && !IS_ERR(sb)) {
  1418. f2fs_stop_checkpoint(sbi, false);
  1419. thaw_bdev(sb->s_bdev, sb);
  1420. }
  1421. break;
  1422. case F2FS_GOING_DOWN_METASYNC:
  1423. /* do checkpoint only */
  1424. f2fs_sync_fs(sb, 1);
  1425. f2fs_stop_checkpoint(sbi, false);
  1426. break;
  1427. case F2FS_GOING_DOWN_NOSYNC:
  1428. f2fs_stop_checkpoint(sbi, false);
  1429. break;
  1430. case F2FS_GOING_DOWN_METAFLUSH:
  1431. sync_meta_pages(sbi, META, LONG_MAX);
  1432. f2fs_stop_checkpoint(sbi, false);
  1433. break;
  1434. default:
  1435. ret = -EINVAL;
  1436. goto out;
  1437. }
  1438. f2fs_update_time(sbi, REQ_TIME);
  1439. out:
  1440. mnt_drop_write_file(filp);
  1441. return ret;
  1442. }
  1443. static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
  1444. {
  1445. struct inode *inode = file_inode(filp);
  1446. struct super_block *sb = inode->i_sb;
  1447. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  1448. struct fstrim_range range;
  1449. int ret;
  1450. if (!capable(CAP_SYS_ADMIN))
  1451. return -EPERM;
  1452. if (!blk_queue_discard(q))
  1453. return -EOPNOTSUPP;
  1454. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  1455. sizeof(range)))
  1456. return -EFAULT;
  1457. ret = mnt_want_write_file(filp);
  1458. if (ret)
  1459. return ret;
  1460. range.minlen = max((unsigned int)range.minlen,
  1461. q->limits.discard_granularity);
  1462. ret = f2fs_trim_fs(F2FS_SB(sb), &range);
  1463. mnt_drop_write_file(filp);
  1464. if (ret < 0)
  1465. return ret;
  1466. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  1467. sizeof(range)))
  1468. return -EFAULT;
  1469. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1470. return 0;
  1471. }
  1472. static bool uuid_is_nonzero(__u8 u[16])
  1473. {
  1474. int i;
  1475. for (i = 0; i < 16; i++)
  1476. if (u[i])
  1477. return true;
  1478. return false;
  1479. }
  1480. static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
  1481. {
  1482. struct fscrypt_policy policy;
  1483. struct inode *inode = file_inode(filp);
  1484. int ret;
  1485. if (copy_from_user(&policy, (struct fscrypt_policy __user *)arg,
  1486. sizeof(policy)))
  1487. return -EFAULT;
  1488. ret = mnt_want_write_file(filp);
  1489. if (ret)
  1490. return ret;
  1491. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  1492. ret = fscrypt_process_policy(inode, &policy);
  1493. mnt_drop_write_file(filp);
  1494. return ret;
  1495. }
  1496. static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
  1497. {
  1498. struct fscrypt_policy policy;
  1499. struct inode *inode = file_inode(filp);
  1500. int err;
  1501. err = fscrypt_get_policy(inode, &policy);
  1502. if (err)
  1503. return err;
  1504. if (copy_to_user((struct fscrypt_policy __user *)arg, &policy, sizeof(policy)))
  1505. return -EFAULT;
  1506. return 0;
  1507. }
  1508. static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
  1509. {
  1510. struct inode *inode = file_inode(filp);
  1511. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1512. int err;
  1513. if (!f2fs_sb_has_crypto(inode->i_sb))
  1514. return -EOPNOTSUPP;
  1515. if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
  1516. goto got_it;
  1517. err = mnt_want_write_file(filp);
  1518. if (err)
  1519. return err;
  1520. /* update superblock with uuid */
  1521. generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
  1522. err = f2fs_commit_super(sbi, false);
  1523. if (err) {
  1524. /* undo new data */
  1525. memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
  1526. mnt_drop_write_file(filp);
  1527. return err;
  1528. }
  1529. mnt_drop_write_file(filp);
  1530. got_it:
  1531. if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
  1532. 16))
  1533. return -EFAULT;
  1534. return 0;
  1535. }
  1536. static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
  1537. {
  1538. struct inode *inode = file_inode(filp);
  1539. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1540. __u32 sync;
  1541. int ret;
  1542. if (!capable(CAP_SYS_ADMIN))
  1543. return -EPERM;
  1544. if (get_user(sync, (__u32 __user *)arg))
  1545. return -EFAULT;
  1546. if (f2fs_readonly(sbi->sb))
  1547. return -EROFS;
  1548. ret = mnt_want_write_file(filp);
  1549. if (ret)
  1550. return ret;
  1551. if (!sync) {
  1552. if (!mutex_trylock(&sbi->gc_mutex)) {
  1553. ret = -EBUSY;
  1554. goto out;
  1555. }
  1556. } else {
  1557. mutex_lock(&sbi->gc_mutex);
  1558. }
  1559. ret = f2fs_gc(sbi, sync, true);
  1560. out:
  1561. mnt_drop_write_file(filp);
  1562. return ret;
  1563. }
  1564. static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
  1565. {
  1566. struct inode *inode = file_inode(filp);
  1567. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1568. int ret;
  1569. if (!capable(CAP_SYS_ADMIN))
  1570. return -EPERM;
  1571. if (f2fs_readonly(sbi->sb))
  1572. return -EROFS;
  1573. ret = mnt_want_write_file(filp);
  1574. if (ret)
  1575. return ret;
  1576. ret = f2fs_sync_fs(sbi->sb, 1);
  1577. mnt_drop_write_file(filp);
  1578. return ret;
  1579. }
  1580. static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
  1581. struct file *filp,
  1582. struct f2fs_defragment *range)
  1583. {
  1584. struct inode *inode = file_inode(filp);
  1585. struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
  1586. struct extent_info ei;
  1587. pgoff_t pg_start, pg_end;
  1588. unsigned int blk_per_seg = sbi->blocks_per_seg;
  1589. unsigned int total = 0, sec_num;
  1590. unsigned int pages_per_sec = sbi->segs_per_sec * blk_per_seg;
  1591. block_t blk_end = 0;
  1592. bool fragmented = false;
  1593. int err;
  1594. /* if in-place-update policy is enabled, don't waste time here */
  1595. if (need_inplace_update(inode))
  1596. return -EINVAL;
  1597. pg_start = range->start >> PAGE_SHIFT;
  1598. pg_end = (range->start + range->len) >> PAGE_SHIFT;
  1599. f2fs_balance_fs(sbi, true);
  1600. inode_lock(inode);
  1601. /* writeback all dirty pages in the range */
  1602. err = filemap_write_and_wait_range(inode->i_mapping, range->start,
  1603. range->start + range->len - 1);
  1604. if (err)
  1605. goto out;
  1606. /*
  1607. * lookup mapping info in extent cache, skip defragmenting if physical
  1608. * block addresses are continuous.
  1609. */
  1610. if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
  1611. if (ei.fofs + ei.len >= pg_end)
  1612. goto out;
  1613. }
  1614. map.m_lblk = pg_start;
  1615. /*
  1616. * lookup mapping info in dnode page cache, skip defragmenting if all
  1617. * physical block addresses are continuous even if there are hole(s)
  1618. * in logical blocks.
  1619. */
  1620. while (map.m_lblk < pg_end) {
  1621. map.m_len = pg_end - map.m_lblk;
  1622. err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
  1623. if (err)
  1624. goto out;
  1625. if (!(map.m_flags & F2FS_MAP_FLAGS)) {
  1626. map.m_lblk++;
  1627. continue;
  1628. }
  1629. if (blk_end && blk_end != map.m_pblk) {
  1630. fragmented = true;
  1631. break;
  1632. }
  1633. blk_end = map.m_pblk + map.m_len;
  1634. map.m_lblk += map.m_len;
  1635. }
  1636. if (!fragmented)
  1637. goto out;
  1638. map.m_lblk = pg_start;
  1639. map.m_len = pg_end - pg_start;
  1640. sec_num = (map.m_len + pages_per_sec - 1) / pages_per_sec;
  1641. /*
  1642. * make sure there are enough free section for LFS allocation, this can
  1643. * avoid defragment running in SSR mode when free section are allocated
  1644. * intensively
  1645. */
  1646. if (has_not_enough_free_secs(sbi, 0, sec_num)) {
  1647. err = -EAGAIN;
  1648. goto out;
  1649. }
  1650. while (map.m_lblk < pg_end) {
  1651. pgoff_t idx;
  1652. int cnt = 0;
  1653. do_map:
  1654. map.m_len = pg_end - map.m_lblk;
  1655. err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
  1656. if (err)
  1657. goto clear_out;
  1658. if (!(map.m_flags & F2FS_MAP_FLAGS)) {
  1659. map.m_lblk++;
  1660. continue;
  1661. }
  1662. set_inode_flag(inode, FI_DO_DEFRAG);
  1663. idx = map.m_lblk;
  1664. while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
  1665. struct page *page;
  1666. page = get_lock_data_page(inode, idx, true);
  1667. if (IS_ERR(page)) {
  1668. err = PTR_ERR(page);
  1669. goto clear_out;
  1670. }
  1671. set_page_dirty(page);
  1672. f2fs_put_page(page, 1);
  1673. idx++;
  1674. cnt++;
  1675. total++;
  1676. }
  1677. map.m_lblk = idx;
  1678. if (idx < pg_end && cnt < blk_per_seg)
  1679. goto do_map;
  1680. clear_inode_flag(inode, FI_DO_DEFRAG);
  1681. err = filemap_fdatawrite(inode->i_mapping);
  1682. if (err)
  1683. goto out;
  1684. }
  1685. clear_out:
  1686. clear_inode_flag(inode, FI_DO_DEFRAG);
  1687. out:
  1688. inode_unlock(inode);
  1689. if (!err)
  1690. range->len = (u64)total << PAGE_SHIFT;
  1691. return err;
  1692. }
  1693. static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
  1694. {
  1695. struct inode *inode = file_inode(filp);
  1696. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1697. struct f2fs_defragment range;
  1698. int err;
  1699. if (!capable(CAP_SYS_ADMIN))
  1700. return -EPERM;
  1701. if (!S_ISREG(inode->i_mode))
  1702. return -EINVAL;
  1703. err = mnt_want_write_file(filp);
  1704. if (err)
  1705. return err;
  1706. if (f2fs_readonly(sbi->sb)) {
  1707. err = -EROFS;
  1708. goto out;
  1709. }
  1710. if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
  1711. sizeof(range))) {
  1712. err = -EFAULT;
  1713. goto out;
  1714. }
  1715. /* verify alignment of offset & size */
  1716. if (range.start & (F2FS_BLKSIZE - 1) ||
  1717. range.len & (F2FS_BLKSIZE - 1)) {
  1718. err = -EINVAL;
  1719. goto out;
  1720. }
  1721. err = f2fs_defragment_range(sbi, filp, &range);
  1722. f2fs_update_time(sbi, REQ_TIME);
  1723. if (err < 0)
  1724. goto out;
  1725. if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
  1726. sizeof(range)))
  1727. err = -EFAULT;
  1728. out:
  1729. mnt_drop_write_file(filp);
  1730. return err;
  1731. }
  1732. long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1733. {
  1734. switch (cmd) {
  1735. case F2FS_IOC_GETFLAGS:
  1736. return f2fs_ioc_getflags(filp, arg);
  1737. case F2FS_IOC_SETFLAGS:
  1738. return f2fs_ioc_setflags(filp, arg);
  1739. case F2FS_IOC_GETVERSION:
  1740. return f2fs_ioc_getversion(filp, arg);
  1741. case F2FS_IOC_START_ATOMIC_WRITE:
  1742. return f2fs_ioc_start_atomic_write(filp);
  1743. case F2FS_IOC_COMMIT_ATOMIC_WRITE:
  1744. return f2fs_ioc_commit_atomic_write(filp);
  1745. case F2FS_IOC_START_VOLATILE_WRITE:
  1746. return f2fs_ioc_start_volatile_write(filp);
  1747. case F2FS_IOC_RELEASE_VOLATILE_WRITE:
  1748. return f2fs_ioc_release_volatile_write(filp);
  1749. case F2FS_IOC_ABORT_VOLATILE_WRITE:
  1750. return f2fs_ioc_abort_volatile_write(filp);
  1751. case F2FS_IOC_SHUTDOWN:
  1752. return f2fs_ioc_shutdown(filp, arg);
  1753. case FITRIM:
  1754. return f2fs_ioc_fitrim(filp, arg);
  1755. case F2FS_IOC_SET_ENCRYPTION_POLICY:
  1756. return f2fs_ioc_set_encryption_policy(filp, arg);
  1757. case F2FS_IOC_GET_ENCRYPTION_POLICY:
  1758. return f2fs_ioc_get_encryption_policy(filp, arg);
  1759. case F2FS_IOC_GET_ENCRYPTION_PWSALT:
  1760. return f2fs_ioc_get_encryption_pwsalt(filp, arg);
  1761. case F2FS_IOC_GARBAGE_COLLECT:
  1762. return f2fs_ioc_gc(filp, arg);
  1763. case F2FS_IOC_WRITE_CHECKPOINT:
  1764. return f2fs_ioc_write_checkpoint(filp, arg);
  1765. case F2FS_IOC_DEFRAGMENT:
  1766. return f2fs_ioc_defragment(filp, arg);
  1767. default:
  1768. return -ENOTTY;
  1769. }
  1770. }
  1771. static ssize_t f2fs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1772. unsigned long nr_segs, loff_t pos)
  1773. {
  1774. struct file *file = iocb->ki_filp;
  1775. struct inode *inode = file_inode(file);
  1776. size_t count;
  1777. struct blk_plug plug;
  1778. ssize_t ret;
  1779. if (f2fs_encrypted_inode(inode) &&
  1780. !fscrypt_has_encryption_key(inode) &&
  1781. fscrypt_get_encryption_info(inode))
  1782. return -EACCES;
  1783. ret = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
  1784. if (ret)
  1785. return ret;
  1786. inode_lock(inode);
  1787. ret = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1788. if (!ret) {
  1789. int err = f2fs_preallocate_blocks(inode, pos, count,
  1790. iocb->ki_filp->f_flags & O_DIRECT);
  1791. if (err) {
  1792. inode_unlock(inode);
  1793. return err;
  1794. }
  1795. blk_start_plug(&plug);
  1796. ret = __generic_file_aio_write(iocb, iov, nr_segs,
  1797. &iocb->ki_pos);
  1798. blk_finish_plug(&plug);
  1799. }
  1800. inode_unlock(inode);
  1801. if (ret > 0 || ret == -EIOCBQUEUED) {
  1802. ssize_t err;
  1803. err = generic_write_sync(file, iocb->ki_pos - ret, ret);
  1804. if (err < 0 && ret > 0)
  1805. ret = err;
  1806. }
  1807. return ret;
  1808. }
  1809. #ifdef CONFIG_COMPAT
  1810. long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1811. {
  1812. switch (cmd) {
  1813. case F2FS_IOC32_GETFLAGS:
  1814. cmd = F2FS_IOC_GETFLAGS;
  1815. break;
  1816. case F2FS_IOC32_SETFLAGS:
  1817. cmd = F2FS_IOC_SETFLAGS;
  1818. break;
  1819. case F2FS_IOC32_GETVERSION:
  1820. cmd = F2FS_IOC_GETVERSION;
  1821. break;
  1822. case F2FS_IOC_START_ATOMIC_WRITE:
  1823. case F2FS_IOC_COMMIT_ATOMIC_WRITE:
  1824. case F2FS_IOC_START_VOLATILE_WRITE:
  1825. case F2FS_IOC_RELEASE_VOLATILE_WRITE:
  1826. case F2FS_IOC_ABORT_VOLATILE_WRITE:
  1827. case F2FS_IOC_SHUTDOWN:
  1828. case F2FS_IOC_SET_ENCRYPTION_POLICY:
  1829. case F2FS_IOC_GET_ENCRYPTION_PWSALT:
  1830. case F2FS_IOC_GET_ENCRYPTION_POLICY:
  1831. case F2FS_IOC_GARBAGE_COLLECT:
  1832. case F2FS_IOC_WRITE_CHECKPOINT:
  1833. case F2FS_IOC_DEFRAGMENT:
  1834. break;
  1835. default:
  1836. return -ENOIOCTLCMD;
  1837. }
  1838. return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  1839. }
  1840. #endif
  1841. static ssize_t f2fs_file_splice_write(struct pipe_inode_info *pipe,
  1842. struct file *out,
  1843. loff_t *ppos, size_t len, unsigned int flags)
  1844. {
  1845. struct address_space *mapping = out->f_mapping;
  1846. struct inode *inode = mapping->host;
  1847. int ret;
  1848. ret = generic_write_checks(out, ppos, &len, S_ISBLK(inode->i_mode));
  1849. if (ret)
  1850. return ret;
  1851. ret = f2fs_preallocate_blocks(inode, *ppos, len, false);
  1852. if (ret)
  1853. return ret;
  1854. return generic_file_splice_write(pipe, out, ppos, len, flags);
  1855. }
  1856. const struct file_operations f2fs_file_operations = {
  1857. .llseek = f2fs_llseek,
  1858. .read = do_sync_read,
  1859. .write = do_sync_write,
  1860. .aio_read = generic_file_aio_read,
  1861. .aio_write = f2fs_file_aio_write,
  1862. .open = f2fs_file_open,
  1863. .release = f2fs_release_file,
  1864. .mmap = f2fs_file_mmap,
  1865. .fsync = f2fs_sync_file,
  1866. .fallocate = f2fs_fallocate,
  1867. .unlocked_ioctl = f2fs_ioctl,
  1868. #ifdef CONFIG_COMPAT
  1869. .compat_ioctl = f2fs_compat_ioctl,
  1870. #endif
  1871. .splice_read = generic_file_splice_read,
  1872. .splice_write = f2fs_file_splice_write,
  1873. };