file.c 53 KB

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