file.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  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 "f2fs.h"
  25. #include "node.h"
  26. #include "segment.h"
  27. #include "xattr.h"
  28. #include "acl.h"
  29. #include "gc.h"
  30. #include "trace.h"
  31. #include <trace/events/f2fs.h>
  32. static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
  33. struct vm_fault *vmf)
  34. {
  35. struct page *page = vmf->page;
  36. struct inode *inode = file_inode(vma->vm_file);
  37. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  38. struct dnode_of_data dn;
  39. int err;
  40. f2fs_balance_fs(sbi);
  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. file_update_time(vma->vm_file);
  54. lock_page(page);
  55. if (unlikely(page->mapping != inode->i_mapping ||
  56. page_offset(page) > i_size_read(inode) ||
  57. !PageUptodate(page))) {
  58. unlock_page(page);
  59. err = -EFAULT;
  60. goto out;
  61. }
  62. /*
  63. * check to see if the page is mapped already (no holes)
  64. */
  65. if (PageMappedToDisk(page))
  66. goto mapped;
  67. /* page is wholly or partially inside EOF */
  68. if (((loff_t)(page->index + 1) << PAGE_CACHE_SHIFT) >
  69. i_size_read(inode)) {
  70. unsigned offset;
  71. offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
  72. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  73. }
  74. set_page_dirty(page);
  75. SetPageUptodate(page);
  76. trace_f2fs_vm_page_mkwrite(page, DATA);
  77. mapped:
  78. /* fill the page */
  79. f2fs_wait_on_page_writeback(page, DATA);
  80. /* wait for GCed encrypted page writeback */
  81. if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
  82. f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
  83. /* if gced page is attached, don't write to cold segment */
  84. clear_cold_data(page);
  85. out:
  86. sb_end_pagefault(inode->i_sb);
  87. return block_page_mkwrite_return(err);
  88. }
  89. static const struct vm_operations_struct f2fs_file_vm_ops = {
  90. .fault = filemap_fault,
  91. .map_pages = filemap_map_pages,
  92. .page_mkwrite = f2fs_vm_page_mkwrite,
  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 (file_enc_name(inode) && need_dentry_mark(sbi, inode->i_ino))
  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. fi->i_pino = pino;
  151. file_got_pino(inode);
  152. up_write(&fi->i_sem);
  153. mark_inode_dirty_sync(inode);
  154. f2fs_write_inode(inode, NULL);
  155. } else {
  156. up_write(&fi->i_sem);
  157. }
  158. }
  159. int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  160. {
  161. struct inode *inode = file->f_mapping->host;
  162. struct f2fs_inode_info *fi = F2FS_I(inode);
  163. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  164. nid_t ino = inode->i_ino;
  165. int ret = 0;
  166. bool need_cp = false;
  167. struct writeback_control wbc = {
  168. .sync_mode = WB_SYNC_ALL,
  169. .nr_to_write = LONG_MAX,
  170. .for_reclaim = 0,
  171. };
  172. if (unlikely(f2fs_readonly(inode->i_sb)))
  173. return 0;
  174. trace_f2fs_sync_file_enter(inode);
  175. if (S_ISDIR(inode->i_mode))
  176. goto go_write;
  177. /* if fdatasync is triggered, let's do in-place-update */
  178. if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
  179. set_inode_flag(fi, FI_NEED_IPU);
  180. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  181. clear_inode_flag(fi, FI_NEED_IPU);
  182. if (ret) {
  183. trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
  184. return ret;
  185. }
  186. /* if the inode is dirty, let's recover all the time */
  187. if (!datasync) {
  188. f2fs_write_inode(inode, NULL);
  189. goto go_write;
  190. }
  191. /*
  192. * if there is no written data, don't waste time to write recovery info.
  193. */
  194. if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
  195. !exist_written_data(sbi, ino, APPEND_INO)) {
  196. /* it may call write_inode just prior to fsync */
  197. if (need_inode_page_update(sbi, ino))
  198. goto go_write;
  199. if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
  200. exist_written_data(sbi, ino, UPDATE_INO))
  201. goto flush_out;
  202. goto out;
  203. }
  204. go_write:
  205. /* guarantee free sections for fsync */
  206. f2fs_balance_fs(sbi);
  207. /*
  208. * Both of fdatasync() and fsync() are able to be recovered from
  209. * sudden-power-off.
  210. */
  211. down_read(&fi->i_sem);
  212. need_cp = need_do_checkpoint(inode);
  213. up_read(&fi->i_sem);
  214. if (need_cp) {
  215. /* all the dirty node pages should be flushed for POR */
  216. ret = f2fs_sync_fs(inode->i_sb, 1);
  217. /*
  218. * We've secured consistency through sync_fs. Following pino
  219. * will be used only for fsynced inodes after checkpoint.
  220. */
  221. try_to_fix_pino(inode);
  222. clear_inode_flag(fi, FI_APPEND_WRITE);
  223. clear_inode_flag(fi, FI_UPDATE_WRITE);
  224. goto out;
  225. }
  226. sync_nodes:
  227. sync_node_pages(sbi, ino, &wbc);
  228. /* if cp_error was enabled, we should avoid infinite loop */
  229. if (unlikely(f2fs_cp_error(sbi)))
  230. goto out;
  231. if (need_inode_block_update(sbi, ino)) {
  232. mark_inode_dirty_sync(inode);
  233. f2fs_write_inode(inode, NULL);
  234. goto sync_nodes;
  235. }
  236. ret = wait_on_node_pages_writeback(sbi, ino);
  237. if (ret)
  238. goto out;
  239. /* once recovery info is written, don't need to tack this */
  240. remove_dirty_inode(sbi, ino, APPEND_INO);
  241. clear_inode_flag(fi, FI_APPEND_WRITE);
  242. flush_out:
  243. remove_dirty_inode(sbi, ino, UPDATE_INO);
  244. clear_inode_flag(fi, FI_UPDATE_WRITE);
  245. ret = f2fs_issue_flush(sbi);
  246. out:
  247. trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
  248. f2fs_trace_ios(NULL, 1);
  249. return ret;
  250. }
  251. static pgoff_t __get_first_dirty_index(struct address_space *mapping,
  252. pgoff_t pgofs, int whence)
  253. {
  254. struct pagevec pvec;
  255. int nr_pages;
  256. if (whence != SEEK_DATA)
  257. return 0;
  258. /* find first dirty page index */
  259. pagevec_init(&pvec, 0);
  260. nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
  261. PAGECACHE_TAG_DIRTY, 1);
  262. pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
  263. pagevec_release(&pvec);
  264. return pgofs;
  265. }
  266. static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
  267. pgoff_t dirty, pgoff_t pgofs, int whence)
  268. {
  269. switch (whence) {
  270. case SEEK_DATA:
  271. if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
  272. is_valid_data_blkaddr(sbi, blkaddr))
  273. return true;
  274. break;
  275. case SEEK_HOLE:
  276. if (blkaddr == NULL_ADDR)
  277. return true;
  278. break;
  279. }
  280. return false;
  281. }
  282. static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
  283. {
  284. struct inode *inode = file->f_mapping->host;
  285. loff_t maxbytes = inode->i_sb->s_maxbytes;
  286. struct dnode_of_data dn;
  287. pgoff_t pgofs, end_offset, dirty;
  288. loff_t data_ofs = offset;
  289. loff_t isize;
  290. int err = 0;
  291. mutex_lock(&inode->i_mutex);
  292. isize = i_size_read(inode);
  293. if (offset >= isize)
  294. goto fail;
  295. /* handle inline data case */
  296. if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
  297. if (whence == SEEK_HOLE)
  298. data_ofs = isize;
  299. goto found;
  300. }
  301. pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
  302. dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
  303. for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) {
  304. set_new_dnode(&dn, inode, NULL, NULL, 0);
  305. err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
  306. if (err && err != -ENOENT) {
  307. goto fail;
  308. } else if (err == -ENOENT) {
  309. /* direct node does not exists */
  310. if (whence == SEEK_DATA) {
  311. pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
  312. F2FS_I(inode));
  313. continue;
  314. } else {
  315. goto found;
  316. }
  317. }
  318. end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  319. /* find data/hole in dnode block */
  320. for (; dn.ofs_in_node < end_offset;
  321. dn.ofs_in_node++, pgofs++,
  322. data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) {
  323. block_t blkaddr;
  324. blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
  325. if (__is_valid_data_blkaddr(blkaddr) &&
  326. !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
  327. blkaddr, DATA_GENERIC)) {
  328. f2fs_put_dnode(&dn);
  329. goto fail;
  330. }
  331. if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
  332. pgofs, whence)) {
  333. f2fs_put_dnode(&dn);
  334. goto found;
  335. }
  336. }
  337. f2fs_put_dnode(&dn);
  338. }
  339. if (whence == SEEK_DATA)
  340. goto fail;
  341. found:
  342. if (whence == SEEK_HOLE && data_ofs > isize)
  343. data_ofs = isize;
  344. mutex_unlock(&inode->i_mutex);
  345. return vfs_setpos(file, data_ofs, maxbytes);
  346. fail:
  347. mutex_unlock(&inode->i_mutex);
  348. return -ENXIO;
  349. }
  350. static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
  351. {
  352. struct inode *inode = file->f_mapping->host;
  353. loff_t maxbytes = inode->i_sb->s_maxbytes;
  354. switch (whence) {
  355. case SEEK_SET:
  356. case SEEK_CUR:
  357. case SEEK_END:
  358. return generic_file_llseek_size(file, offset, whence,
  359. maxbytes, i_size_read(inode));
  360. case SEEK_DATA:
  361. case SEEK_HOLE:
  362. if (offset < 0)
  363. return -ENXIO;
  364. return f2fs_seek_block(file, offset, whence);
  365. }
  366. return -EINVAL;
  367. }
  368. static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
  369. {
  370. struct inode *inode = file_inode(file);
  371. if (f2fs_encrypted_inode(inode)) {
  372. int err = f2fs_get_encryption_info(inode);
  373. if (err)
  374. return 0;
  375. }
  376. /* we don't need to use inline_data strictly */
  377. if (f2fs_has_inline_data(inode)) {
  378. int err = f2fs_convert_inline_inode(inode);
  379. if (err)
  380. return err;
  381. }
  382. file_accessed(file);
  383. vma->vm_ops = &f2fs_file_vm_ops;
  384. return 0;
  385. }
  386. static int f2fs_file_open(struct inode *inode, struct file *filp)
  387. {
  388. int ret = generic_file_open(inode, filp);
  389. if (!ret && f2fs_encrypted_inode(inode)) {
  390. ret = f2fs_get_encryption_info(inode);
  391. if (ret)
  392. ret = -EACCES;
  393. }
  394. return ret;
  395. }
  396. int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
  397. {
  398. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  399. struct f2fs_node *raw_node;
  400. int nr_free = 0, ofs = dn->ofs_in_node, len = count;
  401. __le32 *addr;
  402. raw_node = F2FS_NODE(dn->node_page);
  403. addr = blkaddr_in_node(raw_node) + ofs;
  404. for (; count > 0; count--, addr++, dn->ofs_in_node++) {
  405. block_t blkaddr = le32_to_cpu(*addr);
  406. if (blkaddr == NULL_ADDR)
  407. continue;
  408. dn->data_blkaddr = NULL_ADDR;
  409. set_data_blkaddr(dn);
  410. if (__is_valid_data_blkaddr(blkaddr) &&
  411. !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
  412. continue;
  413. invalidate_blocks(sbi, blkaddr);
  414. if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
  415. clear_inode_flag(F2FS_I(dn->inode),
  416. FI_FIRST_BLOCK_WRITTEN);
  417. nr_free++;
  418. }
  419. if (nr_free) {
  420. pgoff_t fofs;
  421. /*
  422. * once we invalidate valid blkaddr in range [ofs, ofs + count],
  423. * we will invalidate all blkaddr in the whole range.
  424. */
  425. fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
  426. F2FS_I(dn->inode)) + ofs;
  427. f2fs_update_extent_cache_range(dn, fofs, 0, len);
  428. dec_valid_block_count(sbi, dn->inode, nr_free);
  429. set_page_dirty(dn->node_page);
  430. sync_inode_page(dn);
  431. }
  432. dn->ofs_in_node = ofs;
  433. trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
  434. dn->ofs_in_node, nr_free);
  435. return nr_free;
  436. }
  437. void truncate_data_blocks(struct dnode_of_data *dn)
  438. {
  439. truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
  440. }
  441. static int truncate_partial_data_page(struct inode *inode, u64 from,
  442. bool cache_only)
  443. {
  444. unsigned offset = from & (PAGE_CACHE_SIZE - 1);
  445. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  446. struct address_space *mapping = inode->i_mapping;
  447. struct page *page;
  448. if (!offset && !cache_only)
  449. return 0;
  450. if (cache_only) {
  451. page = f2fs_grab_cache_page(mapping, index, false);
  452. if (page && PageUptodate(page))
  453. goto truncate_out;
  454. f2fs_put_page(page, 1);
  455. return 0;
  456. }
  457. page = get_lock_data_page(inode, index, true);
  458. if (IS_ERR(page))
  459. return 0;
  460. truncate_out:
  461. f2fs_wait_on_page_writeback(page, DATA);
  462. zero_user(page, offset, PAGE_CACHE_SIZE - offset);
  463. if (!cache_only || !f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode))
  464. set_page_dirty(page);
  465. f2fs_put_page(page, 1);
  466. return 0;
  467. }
  468. int truncate_blocks(struct inode *inode, u64 from, bool lock)
  469. {
  470. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  471. unsigned int blocksize = inode->i_sb->s_blocksize;
  472. struct dnode_of_data dn;
  473. pgoff_t free_from;
  474. int count = 0, err = 0;
  475. struct page *ipage;
  476. bool truncate_page = false;
  477. trace_f2fs_truncate_blocks_enter(inode, from);
  478. free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
  479. if (lock)
  480. f2fs_lock_op(sbi);
  481. ipage = get_node_page(sbi, inode->i_ino);
  482. if (IS_ERR(ipage)) {
  483. err = PTR_ERR(ipage);
  484. goto out;
  485. }
  486. if (f2fs_has_inline_data(inode)) {
  487. if (truncate_inline_inode(ipage, from))
  488. set_page_dirty(ipage);
  489. f2fs_put_page(ipage, 1);
  490. truncate_page = true;
  491. goto out;
  492. }
  493. set_new_dnode(&dn, inode, ipage, NULL, 0);
  494. err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
  495. if (err) {
  496. if (err == -ENOENT)
  497. goto free_next;
  498. goto out;
  499. }
  500. count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  501. count -= dn.ofs_in_node;
  502. f2fs_bug_on(sbi, count < 0);
  503. if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
  504. truncate_data_blocks_range(&dn, count);
  505. free_from += count;
  506. }
  507. f2fs_put_dnode(&dn);
  508. free_next:
  509. err = truncate_inode_blocks(inode, free_from);
  510. out:
  511. if (lock)
  512. f2fs_unlock_op(sbi);
  513. /* lastly zero out the first data page */
  514. if (!err)
  515. err = truncate_partial_data_page(inode, from, truncate_page);
  516. trace_f2fs_truncate_blocks_exit(inode, err);
  517. return err;
  518. }
  519. int f2fs_truncate(struct inode *inode, bool lock)
  520. {
  521. int err;
  522. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  523. S_ISLNK(inode->i_mode)))
  524. return 0;
  525. trace_f2fs_truncate(inode);
  526. /* we should check inline_data size */
  527. if (f2fs_has_inline_data(inode) && !f2fs_may_inline_data(inode)) {
  528. err = f2fs_convert_inline_inode(inode);
  529. if (err)
  530. return err;
  531. }
  532. err = truncate_blocks(inode, i_size_read(inode), lock);
  533. if (err)
  534. return err;
  535. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  536. mark_inode_dirty(inode);
  537. return 0;
  538. }
  539. int f2fs_getattr(struct vfsmount *mnt,
  540. struct dentry *dentry, struct kstat *stat)
  541. {
  542. struct inode *inode = d_inode(dentry);
  543. generic_fillattr(inode, stat);
  544. stat->blocks <<= 3;
  545. return 0;
  546. }
  547. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  548. static void __setattr_copy(struct inode *inode, const struct iattr *attr)
  549. {
  550. struct f2fs_inode_info *fi = F2FS_I(inode);
  551. unsigned int ia_valid = attr->ia_valid;
  552. if (ia_valid & ATTR_UID)
  553. inode->i_uid = attr->ia_uid;
  554. if (ia_valid & ATTR_GID)
  555. inode->i_gid = attr->ia_gid;
  556. if (ia_valid & ATTR_ATIME)
  557. inode->i_atime = timespec_trunc(attr->ia_atime,
  558. inode->i_sb->s_time_gran);
  559. if (ia_valid & ATTR_MTIME)
  560. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  561. inode->i_sb->s_time_gran);
  562. if (ia_valid & ATTR_CTIME)
  563. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  564. inode->i_sb->s_time_gran);
  565. if (ia_valid & ATTR_MODE) {
  566. umode_t mode = attr->ia_mode;
  567. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  568. mode &= ~S_ISGID;
  569. set_acl_inode(fi, mode);
  570. }
  571. }
  572. #else
  573. #define __setattr_copy setattr_copy
  574. #endif
  575. int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
  576. {
  577. struct inode *inode = d_inode(dentry);
  578. struct f2fs_inode_info *fi = F2FS_I(inode);
  579. int err;
  580. err = inode_change_ok(inode, attr);
  581. if (err)
  582. return err;
  583. if (attr->ia_valid & ATTR_SIZE) {
  584. if (f2fs_encrypted_inode(inode) &&
  585. f2fs_get_encryption_info(inode))
  586. return -EACCES;
  587. if (attr->ia_size <= i_size_read(inode)) {
  588. truncate_setsize(inode, attr->ia_size);
  589. err = f2fs_truncate(inode, true);
  590. if (err)
  591. return err;
  592. f2fs_balance_fs(F2FS_I_SB(inode));
  593. } else {
  594. /*
  595. * do not trim all blocks after i_size if target size is
  596. * larger than i_size.
  597. */
  598. truncate_setsize(inode, attr->ia_size);
  599. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  600. }
  601. }
  602. __setattr_copy(inode, attr);
  603. if (attr->ia_valid & ATTR_MODE) {
  604. err = posix_acl_chmod(inode, get_inode_mode(inode));
  605. if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
  606. inode->i_mode = fi->i_acl_mode;
  607. clear_inode_flag(fi, FI_ACL_MODE);
  608. }
  609. }
  610. mark_inode_dirty(inode);
  611. return err;
  612. }
  613. const struct inode_operations f2fs_file_inode_operations = {
  614. .getattr = f2fs_getattr,
  615. .setattr = f2fs_setattr,
  616. .get_acl = f2fs_get_acl,
  617. .set_acl = f2fs_set_acl,
  618. #ifdef CONFIG_F2FS_FS_XATTR
  619. .setxattr = generic_setxattr,
  620. .getxattr = generic_getxattr,
  621. .listxattr = f2fs_listxattr,
  622. .removexattr = generic_removexattr,
  623. #endif
  624. .fiemap = f2fs_fiemap,
  625. };
  626. static int fill_zero(struct inode *inode, pgoff_t index,
  627. loff_t start, loff_t len)
  628. {
  629. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  630. struct page *page;
  631. if (!len)
  632. return 0;
  633. f2fs_balance_fs(sbi);
  634. f2fs_lock_op(sbi);
  635. page = get_new_data_page(inode, NULL, index, false);
  636. f2fs_unlock_op(sbi);
  637. if (IS_ERR(page))
  638. return PTR_ERR(page);
  639. f2fs_wait_on_page_writeback(page, DATA);
  640. zero_user(page, start, len);
  641. set_page_dirty(page);
  642. f2fs_put_page(page, 1);
  643. return 0;
  644. }
  645. int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
  646. {
  647. int err;
  648. while (pg_start < pg_end) {
  649. struct dnode_of_data dn;
  650. pgoff_t end_offset, count;
  651. set_new_dnode(&dn, inode, NULL, NULL, 0);
  652. err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
  653. if (err) {
  654. if (err == -ENOENT) {
  655. pg_start++;
  656. continue;
  657. }
  658. return err;
  659. }
  660. end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  661. count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
  662. f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
  663. truncate_data_blocks_range(&dn, count);
  664. f2fs_put_dnode(&dn);
  665. pg_start += count;
  666. }
  667. return 0;
  668. }
  669. static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
  670. {
  671. pgoff_t pg_start, pg_end;
  672. loff_t off_start, off_end;
  673. int ret = 0;
  674. if (f2fs_has_inline_data(inode)) {
  675. ret = f2fs_convert_inline_inode(inode);
  676. if (ret)
  677. return ret;
  678. }
  679. pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
  680. pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
  681. off_start = offset & (PAGE_CACHE_SIZE - 1);
  682. off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
  683. if (pg_start == pg_end) {
  684. ret = fill_zero(inode, pg_start, off_start,
  685. off_end - off_start);
  686. if (ret)
  687. return ret;
  688. } else {
  689. if (off_start) {
  690. ret = fill_zero(inode, pg_start++, off_start,
  691. PAGE_CACHE_SIZE - off_start);
  692. if (ret)
  693. return ret;
  694. }
  695. if (off_end) {
  696. ret = fill_zero(inode, pg_end, 0, off_end);
  697. if (ret)
  698. return ret;
  699. }
  700. if (pg_start < pg_end) {
  701. struct address_space *mapping = inode->i_mapping;
  702. loff_t blk_start, blk_end;
  703. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  704. f2fs_balance_fs(sbi);
  705. blk_start = (loff_t)pg_start << PAGE_CACHE_SHIFT;
  706. blk_end = (loff_t)pg_end << PAGE_CACHE_SHIFT;
  707. truncate_inode_pages_range(mapping, blk_start,
  708. blk_end - 1);
  709. f2fs_lock_op(sbi);
  710. ret = truncate_hole(inode, pg_start, pg_end);
  711. f2fs_unlock_op(sbi);
  712. }
  713. }
  714. return ret;
  715. }
  716. static int __exchange_data_block(struct inode *inode, pgoff_t src,
  717. pgoff_t dst, bool full)
  718. {
  719. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  720. struct dnode_of_data dn;
  721. block_t new_addr;
  722. bool do_replace = false;
  723. int ret;
  724. set_new_dnode(&dn, inode, NULL, NULL, 0);
  725. ret = get_dnode_of_data(&dn, src, LOOKUP_NODE_RA);
  726. if (ret && ret != -ENOENT) {
  727. return ret;
  728. } else if (ret == -ENOENT) {
  729. new_addr = NULL_ADDR;
  730. } else {
  731. new_addr = dn.data_blkaddr;
  732. if (!is_checkpointed_data(sbi, new_addr)) {
  733. dn.data_blkaddr = NULL_ADDR;
  734. /* do not invalidate this block address */
  735. set_data_blkaddr(&dn);
  736. f2fs_update_extent_cache(&dn);
  737. do_replace = true;
  738. }
  739. f2fs_put_dnode(&dn);
  740. }
  741. if (new_addr == NULL_ADDR)
  742. return full ? truncate_hole(inode, dst, dst + 1) : 0;
  743. if (do_replace) {
  744. struct page *ipage = get_node_page(sbi, inode->i_ino);
  745. struct node_info ni;
  746. if (IS_ERR(ipage)) {
  747. ret = PTR_ERR(ipage);
  748. goto err_out;
  749. }
  750. set_new_dnode(&dn, inode, ipage, NULL, 0);
  751. ret = f2fs_reserve_block(&dn, dst);
  752. if (ret)
  753. goto err_out;
  754. truncate_data_blocks_range(&dn, 1);
  755. get_node_info(sbi, dn.nid, &ni);
  756. f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr,
  757. ni.version, true);
  758. f2fs_put_dnode(&dn);
  759. } else {
  760. struct page *psrc, *pdst;
  761. psrc = get_lock_data_page(inode, src, true);
  762. if (IS_ERR(psrc))
  763. return PTR_ERR(psrc);
  764. pdst = get_new_data_page(inode, NULL, dst, false);
  765. if (IS_ERR(pdst)) {
  766. f2fs_put_page(psrc, 1);
  767. return PTR_ERR(pdst);
  768. }
  769. f2fs_copy_page(psrc, pdst);
  770. set_page_dirty(pdst);
  771. f2fs_put_page(pdst, 1);
  772. f2fs_put_page(psrc, 1);
  773. return truncate_hole(inode, src, src + 1);
  774. }
  775. return 0;
  776. err_out:
  777. if (!get_dnode_of_data(&dn, src, LOOKUP_NODE)) {
  778. dn.data_blkaddr = new_addr;
  779. set_data_blkaddr(&dn);
  780. f2fs_update_extent_cache(&dn);
  781. f2fs_put_dnode(&dn);
  782. }
  783. return ret;
  784. }
  785. static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
  786. {
  787. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  788. pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
  789. int ret = 0;
  790. for (; end < nrpages; start++, end++) {
  791. f2fs_balance_fs(sbi);
  792. f2fs_lock_op(sbi);
  793. ret = __exchange_data_block(inode, end, start, true);
  794. f2fs_unlock_op(sbi);
  795. if (ret)
  796. break;
  797. }
  798. return ret;
  799. }
  800. static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
  801. {
  802. pgoff_t pg_start, pg_end;
  803. loff_t new_size;
  804. int ret;
  805. if (offset + len >= i_size_read(inode))
  806. return -EINVAL;
  807. /* collapse range should be aligned to block size of f2fs. */
  808. if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
  809. return -EINVAL;
  810. f2fs_balance_fs(F2FS_I_SB(inode));
  811. if (f2fs_has_inline_data(inode)) {
  812. ret = f2fs_convert_inline_inode(inode);
  813. if (ret)
  814. return ret;
  815. }
  816. pg_start = offset >> PAGE_CACHE_SHIFT;
  817. pg_end = (offset + len) >> PAGE_CACHE_SHIFT;
  818. /* write out all dirty pages from offset */
  819. ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  820. if (ret)
  821. return ret;
  822. truncate_pagecache(inode, offset);
  823. ret = f2fs_do_collapse(inode, pg_start, pg_end);
  824. if (ret)
  825. return ret;
  826. /* write out all moved pages, if possible */
  827. filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  828. truncate_pagecache(inode, offset);
  829. new_size = i_size_read(inode) - len;
  830. truncate_pagecache(inode, new_size);
  831. ret = truncate_blocks(inode, new_size, true);
  832. if (!ret)
  833. i_size_write(inode, new_size);
  834. return ret;
  835. }
  836. static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
  837. int mode)
  838. {
  839. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  840. struct address_space *mapping = inode->i_mapping;
  841. pgoff_t index, pg_start, pg_end;
  842. loff_t new_size = i_size_read(inode);
  843. loff_t off_start, off_end;
  844. int ret = 0;
  845. ret = inode_newsize_ok(inode, (len + offset));
  846. if (ret)
  847. return ret;
  848. f2fs_balance_fs(sbi);
  849. if (f2fs_has_inline_data(inode)) {
  850. ret = f2fs_convert_inline_inode(inode);
  851. if (ret)
  852. return ret;
  853. }
  854. ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
  855. if (ret)
  856. return ret;
  857. truncate_pagecache_range(inode, offset, offset + len - 1);
  858. pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
  859. pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
  860. off_start = offset & (PAGE_CACHE_SIZE - 1);
  861. off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
  862. if (pg_start == pg_end) {
  863. ret = fill_zero(inode, pg_start, off_start,
  864. off_end - off_start);
  865. if (ret)
  866. return ret;
  867. if (offset + len > new_size)
  868. new_size = offset + len;
  869. new_size = max_t(loff_t, new_size, offset + len);
  870. } else {
  871. if (off_start) {
  872. ret = fill_zero(inode, pg_start++, off_start,
  873. PAGE_CACHE_SIZE - off_start);
  874. if (ret)
  875. return ret;
  876. new_size = max_t(loff_t, new_size,
  877. (loff_t)pg_start << PAGE_CACHE_SHIFT);
  878. }
  879. for (index = pg_start; index < pg_end; index++) {
  880. struct dnode_of_data dn;
  881. struct page *ipage;
  882. f2fs_lock_op(sbi);
  883. ipage = get_node_page(sbi, inode->i_ino);
  884. if (IS_ERR(ipage)) {
  885. ret = PTR_ERR(ipage);
  886. f2fs_unlock_op(sbi);
  887. goto out;
  888. }
  889. set_new_dnode(&dn, inode, ipage, NULL, 0);
  890. ret = f2fs_reserve_block(&dn, index);
  891. if (ret) {
  892. f2fs_unlock_op(sbi);
  893. goto out;
  894. }
  895. if (dn.data_blkaddr != NEW_ADDR) {
  896. invalidate_blocks(sbi, dn.data_blkaddr);
  897. dn.data_blkaddr = NEW_ADDR;
  898. set_data_blkaddr(&dn);
  899. dn.data_blkaddr = NULL_ADDR;
  900. f2fs_update_extent_cache(&dn);
  901. }
  902. f2fs_put_dnode(&dn);
  903. f2fs_unlock_op(sbi);
  904. new_size = max_t(loff_t, new_size,
  905. (loff_t)(index + 1) << PAGE_CACHE_SHIFT);
  906. }
  907. if (off_end) {
  908. ret = fill_zero(inode, pg_end, 0, off_end);
  909. if (ret)
  910. goto out;
  911. new_size = max_t(loff_t, new_size, offset + len);
  912. }
  913. }
  914. out:
  915. if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) {
  916. i_size_write(inode, new_size);
  917. mark_inode_dirty(inode);
  918. update_inode_page(inode);
  919. }
  920. return ret;
  921. }
  922. static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
  923. {
  924. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  925. pgoff_t pg_start, pg_end, delta, nrpages, idx;
  926. loff_t new_size;
  927. int ret = 0;
  928. new_size = i_size_read(inode) + len;
  929. if (new_size > inode->i_sb->s_maxbytes)
  930. return -EFBIG;
  931. if (offset >= i_size_read(inode))
  932. return -EINVAL;
  933. /* insert range should be aligned to block size of f2fs. */
  934. if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
  935. return -EINVAL;
  936. f2fs_balance_fs(sbi);
  937. if (f2fs_has_inline_data(inode)) {
  938. ret = f2fs_convert_inline_inode(inode);
  939. if (ret)
  940. return ret;
  941. }
  942. ret = truncate_blocks(inode, i_size_read(inode), true);
  943. if (ret)
  944. return ret;
  945. /* write out all dirty pages from offset */
  946. ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  947. if (ret)
  948. return ret;
  949. truncate_pagecache(inode, offset);
  950. pg_start = offset >> PAGE_CACHE_SHIFT;
  951. pg_end = (offset + len) >> PAGE_CACHE_SHIFT;
  952. delta = pg_end - pg_start;
  953. nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
  954. for (idx = nrpages - 1; idx >= pg_start && idx != -1; idx--) {
  955. f2fs_lock_op(sbi);
  956. ret = __exchange_data_block(inode, idx, idx + delta, false);
  957. f2fs_unlock_op(sbi);
  958. if (ret)
  959. break;
  960. }
  961. /* write out all moved pages, if possible */
  962. filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
  963. truncate_pagecache(inode, offset);
  964. if (!ret)
  965. i_size_write(inode, new_size);
  966. return ret;
  967. }
  968. static int expand_inode_data(struct inode *inode, loff_t offset,
  969. loff_t len, int mode)
  970. {
  971. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  972. pgoff_t index, pg_start, pg_end;
  973. loff_t new_size = i_size_read(inode);
  974. loff_t off_start, off_end;
  975. int ret = 0;
  976. f2fs_balance_fs(sbi);
  977. ret = inode_newsize_ok(inode, (len + offset));
  978. if (ret)
  979. return ret;
  980. if (f2fs_has_inline_data(inode)) {
  981. ret = f2fs_convert_inline_inode(inode);
  982. if (ret)
  983. return ret;
  984. }
  985. pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
  986. pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
  987. off_start = offset & (PAGE_CACHE_SIZE - 1);
  988. off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
  989. f2fs_lock_op(sbi);
  990. for (index = pg_start; index <= pg_end; index++) {
  991. struct dnode_of_data dn;
  992. if (index == pg_end && !off_end)
  993. goto noalloc;
  994. set_new_dnode(&dn, inode, NULL, NULL, 0);
  995. ret = f2fs_reserve_block(&dn, index);
  996. if (ret)
  997. break;
  998. noalloc:
  999. if (pg_start == pg_end)
  1000. new_size = offset + len;
  1001. else if (index == pg_start && off_start)
  1002. new_size = (loff_t)(index + 1) << PAGE_CACHE_SHIFT;
  1003. else if (index == pg_end)
  1004. new_size = ((loff_t)index << PAGE_CACHE_SHIFT) +
  1005. off_end;
  1006. else
  1007. new_size += PAGE_CACHE_SIZE;
  1008. }
  1009. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  1010. i_size_read(inode) < new_size) {
  1011. i_size_write(inode, new_size);
  1012. mark_inode_dirty(inode);
  1013. update_inode_page(inode);
  1014. }
  1015. f2fs_unlock_op(sbi);
  1016. return ret;
  1017. }
  1018. static long f2fs_fallocate(struct file *file, int mode,
  1019. loff_t offset, loff_t len)
  1020. {
  1021. struct inode *inode = file_inode(file);
  1022. long ret = 0;
  1023. /* f2fs only support ->fallocate for regular file */
  1024. if (!S_ISREG(inode->i_mode))
  1025. return -EINVAL;
  1026. if (f2fs_encrypted_inode(inode) &&
  1027. (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
  1028. return -EOPNOTSUPP;
  1029. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
  1030. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
  1031. FALLOC_FL_INSERT_RANGE))
  1032. return -EOPNOTSUPP;
  1033. mutex_lock(&inode->i_mutex);
  1034. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1035. if (offset >= inode->i_size)
  1036. goto out;
  1037. ret = punch_hole(inode, offset, len);
  1038. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  1039. ret = f2fs_collapse_range(inode, offset, len);
  1040. } else if (mode & FALLOC_FL_ZERO_RANGE) {
  1041. ret = f2fs_zero_range(inode, offset, len, mode);
  1042. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  1043. ret = f2fs_insert_range(inode, offset, len);
  1044. } else {
  1045. ret = expand_inode_data(inode, offset, len, mode);
  1046. }
  1047. if (!ret) {
  1048. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1049. mark_inode_dirty(inode);
  1050. }
  1051. out:
  1052. mutex_unlock(&inode->i_mutex);
  1053. trace_f2fs_fallocate(inode, mode, offset, len, ret);
  1054. return ret;
  1055. }
  1056. static int f2fs_release_file(struct inode *inode, struct file *filp)
  1057. {
  1058. /* some remained atomic pages should discarded */
  1059. if (f2fs_is_atomic_file(inode))
  1060. commit_inmem_pages(inode, true);
  1061. if (f2fs_is_volatile_file(inode)) {
  1062. set_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
  1063. filemap_fdatawrite(inode->i_mapping);
  1064. clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
  1065. }
  1066. return 0;
  1067. }
  1068. #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
  1069. #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
  1070. static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
  1071. {
  1072. if (S_ISDIR(mode))
  1073. return flags;
  1074. else if (S_ISREG(mode))
  1075. return flags & F2FS_REG_FLMASK;
  1076. else
  1077. return flags & F2FS_OTHER_FLMASK;
  1078. }
  1079. static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
  1080. {
  1081. struct inode *inode = file_inode(filp);
  1082. struct f2fs_inode_info *fi = F2FS_I(inode);
  1083. unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
  1084. return put_user(flags, (int __user *)arg);
  1085. }
  1086. static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
  1087. {
  1088. struct inode *inode = file_inode(filp);
  1089. struct f2fs_inode_info *fi = F2FS_I(inode);
  1090. unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
  1091. unsigned int oldflags;
  1092. int ret;
  1093. ret = mnt_want_write_file(filp);
  1094. if (ret)
  1095. return ret;
  1096. if (!inode_owner_or_capable(inode)) {
  1097. ret = -EACCES;
  1098. goto out;
  1099. }
  1100. if (get_user(flags, (int __user *)arg)) {
  1101. ret = -EFAULT;
  1102. goto out;
  1103. }
  1104. flags = f2fs_mask_flags(inode->i_mode, flags);
  1105. mutex_lock(&inode->i_mutex);
  1106. oldflags = fi->i_flags;
  1107. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  1108. if (!capable(CAP_LINUX_IMMUTABLE)) {
  1109. mutex_unlock(&inode->i_mutex);
  1110. ret = -EPERM;
  1111. goto out;
  1112. }
  1113. }
  1114. flags = flags & FS_FL_USER_MODIFIABLE;
  1115. flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
  1116. fi->i_flags = flags;
  1117. mutex_unlock(&inode->i_mutex);
  1118. f2fs_set_inode_flags(inode);
  1119. inode->i_ctime = CURRENT_TIME;
  1120. mark_inode_dirty(inode);
  1121. out:
  1122. mnt_drop_write_file(filp);
  1123. return ret;
  1124. }
  1125. static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
  1126. {
  1127. struct inode *inode = file_inode(filp);
  1128. return put_user(inode->i_generation, (int __user *)arg);
  1129. }
  1130. static int f2fs_ioc_start_atomic_write(struct file *filp)
  1131. {
  1132. struct inode *inode = file_inode(filp);
  1133. int ret;
  1134. if (!inode_owner_or_capable(inode))
  1135. return -EACCES;
  1136. f2fs_balance_fs(F2FS_I_SB(inode));
  1137. if (f2fs_is_atomic_file(inode))
  1138. return 0;
  1139. ret = f2fs_convert_inline_inode(inode);
  1140. if (ret)
  1141. return ret;
  1142. set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
  1143. return 0;
  1144. }
  1145. static int f2fs_ioc_commit_atomic_write(struct file *filp)
  1146. {
  1147. struct inode *inode = file_inode(filp);
  1148. int ret;
  1149. if (!inode_owner_or_capable(inode))
  1150. return -EACCES;
  1151. if (f2fs_is_volatile_file(inode))
  1152. return 0;
  1153. ret = mnt_want_write_file(filp);
  1154. if (ret)
  1155. return ret;
  1156. if (f2fs_is_atomic_file(inode)) {
  1157. clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
  1158. ret = commit_inmem_pages(inode, false);
  1159. if (ret)
  1160. goto err_out;
  1161. }
  1162. ret = f2fs_sync_file(filp, 0, LLONG_MAX, 0);
  1163. err_out:
  1164. mnt_drop_write_file(filp);
  1165. return ret;
  1166. }
  1167. static int f2fs_ioc_start_volatile_write(struct file *filp)
  1168. {
  1169. struct inode *inode = file_inode(filp);
  1170. int ret;
  1171. if (!inode_owner_or_capable(inode))
  1172. return -EACCES;
  1173. if (f2fs_is_volatile_file(inode))
  1174. return 0;
  1175. ret = f2fs_convert_inline_inode(inode);
  1176. if (ret)
  1177. return ret;
  1178. set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
  1179. return 0;
  1180. }
  1181. static int f2fs_ioc_release_volatile_write(struct file *filp)
  1182. {
  1183. struct inode *inode = file_inode(filp);
  1184. if (!inode_owner_or_capable(inode))
  1185. return -EACCES;
  1186. if (!f2fs_is_volatile_file(inode))
  1187. return 0;
  1188. if (!f2fs_is_first_block_written(inode))
  1189. return truncate_partial_data_page(inode, 0, true);
  1190. return punch_hole(inode, 0, F2FS_BLKSIZE);
  1191. }
  1192. static int f2fs_ioc_abort_volatile_write(struct file *filp)
  1193. {
  1194. struct inode *inode = file_inode(filp);
  1195. int ret;
  1196. if (!inode_owner_or_capable(inode))
  1197. return -EACCES;
  1198. ret = mnt_want_write_file(filp);
  1199. if (ret)
  1200. return ret;
  1201. f2fs_balance_fs(F2FS_I_SB(inode));
  1202. clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
  1203. clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
  1204. commit_inmem_pages(inode, true);
  1205. mnt_drop_write_file(filp);
  1206. return ret;
  1207. }
  1208. static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
  1209. {
  1210. struct inode *inode = file_inode(filp);
  1211. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1212. struct super_block *sb = sbi->sb;
  1213. __u32 in;
  1214. if (!capable(CAP_SYS_ADMIN))
  1215. return -EPERM;
  1216. if (get_user(in, (__u32 __user *)arg))
  1217. return -EFAULT;
  1218. switch (in) {
  1219. case F2FS_GOING_DOWN_FULLSYNC:
  1220. sb = freeze_bdev(sb->s_bdev);
  1221. if (sb && !IS_ERR(sb)) {
  1222. f2fs_stop_checkpoint(sbi);
  1223. thaw_bdev(sb->s_bdev, sb);
  1224. }
  1225. break;
  1226. case F2FS_GOING_DOWN_METASYNC:
  1227. /* do checkpoint only */
  1228. f2fs_sync_fs(sb, 1);
  1229. f2fs_stop_checkpoint(sbi);
  1230. break;
  1231. case F2FS_GOING_DOWN_NOSYNC:
  1232. f2fs_stop_checkpoint(sbi);
  1233. break;
  1234. case F2FS_GOING_DOWN_METAFLUSH:
  1235. sync_meta_pages(sbi, META, LONG_MAX);
  1236. f2fs_stop_checkpoint(sbi);
  1237. break;
  1238. default:
  1239. return -EINVAL;
  1240. }
  1241. return 0;
  1242. }
  1243. static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
  1244. {
  1245. struct inode *inode = file_inode(filp);
  1246. struct super_block *sb = inode->i_sb;
  1247. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  1248. struct fstrim_range range;
  1249. int ret;
  1250. if (!capable(CAP_SYS_ADMIN))
  1251. return -EPERM;
  1252. if (!blk_queue_discard(q))
  1253. return -EOPNOTSUPP;
  1254. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  1255. sizeof(range)))
  1256. return -EFAULT;
  1257. range.minlen = max((unsigned int)range.minlen,
  1258. q->limits.discard_granularity);
  1259. ret = f2fs_trim_fs(F2FS_SB(sb), &range);
  1260. if (ret < 0)
  1261. return ret;
  1262. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  1263. sizeof(range)))
  1264. return -EFAULT;
  1265. return 0;
  1266. }
  1267. static bool uuid_is_nonzero(__u8 u[16])
  1268. {
  1269. int i;
  1270. for (i = 0; i < 16; i++)
  1271. if (u[i])
  1272. return true;
  1273. return false;
  1274. }
  1275. static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
  1276. {
  1277. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1278. struct f2fs_encryption_policy policy;
  1279. struct inode *inode = file_inode(filp);
  1280. int err;
  1281. if (copy_from_user(&policy, (struct f2fs_encryption_policy __user *)arg,
  1282. sizeof(policy)))
  1283. return -EFAULT;
  1284. err = mnt_want_write_file(filp);
  1285. if (err)
  1286. return err;
  1287. mutex_lock(&inode->i_mutex);
  1288. err = f2fs_process_policy(&policy, inode);
  1289. mutex_unlock(&inode->i_mutex);
  1290. mnt_drop_write_file(filp);
  1291. return err;
  1292. #else
  1293. return -EOPNOTSUPP;
  1294. #endif
  1295. }
  1296. static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
  1297. {
  1298. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1299. struct f2fs_encryption_policy policy;
  1300. struct inode *inode = file_inode(filp);
  1301. int err;
  1302. err = f2fs_get_policy(inode, &policy);
  1303. if (err)
  1304. return err;
  1305. if (copy_to_user((struct f2fs_encryption_policy __user *)arg, &policy,
  1306. sizeof(policy)))
  1307. return -EFAULT;
  1308. return 0;
  1309. #else
  1310. return -EOPNOTSUPP;
  1311. #endif
  1312. }
  1313. static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
  1314. {
  1315. struct inode *inode = file_inode(filp);
  1316. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1317. int err;
  1318. if (!f2fs_sb_has_crypto(inode->i_sb))
  1319. return -EOPNOTSUPP;
  1320. if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
  1321. goto got_it;
  1322. err = mnt_want_write_file(filp);
  1323. if (err)
  1324. return err;
  1325. /* update superblock with uuid */
  1326. generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
  1327. err = f2fs_commit_super(sbi, false);
  1328. mnt_drop_write_file(filp);
  1329. if (err) {
  1330. /* undo new data */
  1331. memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
  1332. return err;
  1333. }
  1334. got_it:
  1335. if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
  1336. 16))
  1337. return -EFAULT;
  1338. return 0;
  1339. }
  1340. static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
  1341. {
  1342. struct inode *inode = file_inode(filp);
  1343. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1344. __u32 sync;
  1345. if (!capable(CAP_SYS_ADMIN))
  1346. return -EPERM;
  1347. if (get_user(sync, (__u32 __user *)arg))
  1348. return -EFAULT;
  1349. if (f2fs_readonly(sbi->sb))
  1350. return -EROFS;
  1351. if (!sync) {
  1352. if (!mutex_trylock(&sbi->gc_mutex))
  1353. return -EBUSY;
  1354. } else {
  1355. mutex_lock(&sbi->gc_mutex);
  1356. }
  1357. return f2fs_gc(sbi, sync);
  1358. }
  1359. static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
  1360. {
  1361. struct inode *inode = file_inode(filp);
  1362. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1363. struct cp_control cpc;
  1364. if (!capable(CAP_SYS_ADMIN))
  1365. return -EPERM;
  1366. if (f2fs_readonly(sbi->sb))
  1367. return -EROFS;
  1368. cpc.reason = __get_cp_reason(sbi);
  1369. mutex_lock(&sbi->gc_mutex);
  1370. write_checkpoint(sbi, &cpc);
  1371. mutex_unlock(&sbi->gc_mutex);
  1372. return 0;
  1373. }
  1374. long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1375. {
  1376. switch (cmd) {
  1377. case F2FS_IOC_GETFLAGS:
  1378. return f2fs_ioc_getflags(filp, arg);
  1379. case F2FS_IOC_SETFLAGS:
  1380. return f2fs_ioc_setflags(filp, arg);
  1381. case F2FS_IOC_GETVERSION:
  1382. return f2fs_ioc_getversion(filp, arg);
  1383. case F2FS_IOC_START_ATOMIC_WRITE:
  1384. return f2fs_ioc_start_atomic_write(filp);
  1385. case F2FS_IOC_COMMIT_ATOMIC_WRITE:
  1386. return f2fs_ioc_commit_atomic_write(filp);
  1387. case F2FS_IOC_START_VOLATILE_WRITE:
  1388. return f2fs_ioc_start_volatile_write(filp);
  1389. case F2FS_IOC_RELEASE_VOLATILE_WRITE:
  1390. return f2fs_ioc_release_volatile_write(filp);
  1391. case F2FS_IOC_ABORT_VOLATILE_WRITE:
  1392. return f2fs_ioc_abort_volatile_write(filp);
  1393. case F2FS_IOC_SHUTDOWN:
  1394. return f2fs_ioc_shutdown(filp, arg);
  1395. case FITRIM:
  1396. return f2fs_ioc_fitrim(filp, arg);
  1397. case F2FS_IOC_SET_ENCRYPTION_POLICY:
  1398. return f2fs_ioc_set_encryption_policy(filp, arg);
  1399. case F2FS_IOC_GET_ENCRYPTION_POLICY:
  1400. return f2fs_ioc_get_encryption_policy(filp, arg);
  1401. case F2FS_IOC_GET_ENCRYPTION_PWSALT:
  1402. return f2fs_ioc_get_encryption_pwsalt(filp, arg);
  1403. case F2FS_IOC_GARBAGE_COLLECT:
  1404. return f2fs_ioc_gc(filp, arg);
  1405. case F2FS_IOC_WRITE_CHECKPOINT:
  1406. return f2fs_ioc_write_checkpoint(filp, arg);
  1407. default:
  1408. return -ENOTTY;
  1409. }
  1410. }
  1411. static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1412. {
  1413. struct inode *inode = file_inode(iocb->ki_filp);
  1414. if (f2fs_encrypted_inode(inode) &&
  1415. !f2fs_has_encryption_key(inode) &&
  1416. f2fs_get_encryption_info(inode))
  1417. return -EACCES;
  1418. return generic_file_write_iter(iocb, from);
  1419. }
  1420. #ifdef CONFIG_COMPAT
  1421. long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1422. {
  1423. switch (cmd) {
  1424. case F2FS_IOC32_GETFLAGS:
  1425. cmd = F2FS_IOC_GETFLAGS;
  1426. break;
  1427. case F2FS_IOC32_SETFLAGS:
  1428. cmd = F2FS_IOC_SETFLAGS;
  1429. break;
  1430. default:
  1431. return -ENOIOCTLCMD;
  1432. }
  1433. return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  1434. }
  1435. #endif
  1436. const struct file_operations f2fs_file_operations = {
  1437. .llseek = f2fs_llseek,
  1438. .read_iter = generic_file_read_iter,
  1439. .write_iter = f2fs_file_write_iter,
  1440. .open = f2fs_file_open,
  1441. .release = f2fs_release_file,
  1442. .mmap = f2fs_file_mmap,
  1443. .fsync = f2fs_sync_file,
  1444. .fallocate = f2fs_fallocate,
  1445. .unlocked_ioctl = f2fs_ioctl,
  1446. #ifdef CONFIG_COMPAT
  1447. .compat_ioctl = f2fs_compat_ioctl,
  1448. #endif
  1449. .splice_read = generic_file_splice_read,
  1450. .splice_write = iter_file_splice_write,
  1451. };