file.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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 "f2fs.h"
  24. #include "node.h"
  25. #include "segment.h"
  26. #include "xattr.h"
  27. #include "acl.h"
  28. #include "trace.h"
  29. #include <trace/events/f2fs.h>
  30. static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
  31. struct vm_fault *vmf)
  32. {
  33. struct page *page = vmf->page;
  34. struct inode *inode = file_inode(vma->vm_file);
  35. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  36. struct dnode_of_data dn;
  37. int err;
  38. f2fs_balance_fs(sbi);
  39. sb_start_pagefault(inode->i_sb);
  40. f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
  41. /* block allocation */
  42. f2fs_lock_op(sbi);
  43. set_new_dnode(&dn, inode, NULL, NULL, 0);
  44. err = f2fs_reserve_block(&dn, page->index);
  45. if (err) {
  46. f2fs_unlock_op(sbi);
  47. goto out;
  48. }
  49. f2fs_put_dnode(&dn);
  50. f2fs_unlock_op(sbi);
  51. file_update_time(vma->vm_file);
  52. lock_page(page);
  53. if (unlikely(page->mapping != inode->i_mapping ||
  54. page_offset(page) > i_size_read(inode) ||
  55. !PageUptodate(page))) {
  56. unlock_page(page);
  57. err = -EFAULT;
  58. goto out;
  59. }
  60. /*
  61. * check to see if the page is mapped already (no holes)
  62. */
  63. if (PageMappedToDisk(page))
  64. goto mapped;
  65. /* page is wholly or partially inside EOF */
  66. if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
  67. unsigned offset;
  68. offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
  69. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  70. }
  71. set_page_dirty(page);
  72. SetPageUptodate(page);
  73. trace_f2fs_vm_page_mkwrite(page, DATA);
  74. mapped:
  75. /* fill the page */
  76. f2fs_wait_on_page_writeback(page, DATA);
  77. out:
  78. sb_end_pagefault(inode->i_sb);
  79. return block_page_mkwrite_return(err);
  80. }
  81. static const struct vm_operations_struct f2fs_file_vm_ops = {
  82. .fault = filemap_fault,
  83. .map_pages = filemap_map_pages,
  84. .page_mkwrite = f2fs_vm_page_mkwrite,
  85. };
  86. static int get_parent_ino(struct inode *inode, nid_t *pino)
  87. {
  88. struct dentry *dentry;
  89. inode = igrab(inode);
  90. dentry = d_find_any_alias(inode);
  91. iput(inode);
  92. if (!dentry)
  93. return 0;
  94. if (update_dent_inode(inode, &dentry->d_name)) {
  95. dput(dentry);
  96. return 0;
  97. }
  98. *pino = parent_ino(dentry);
  99. dput(dentry);
  100. return 1;
  101. }
  102. static inline bool need_do_checkpoint(struct inode *inode)
  103. {
  104. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  105. bool need_cp = false;
  106. if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
  107. need_cp = true;
  108. else if (file_wrong_pino(inode))
  109. need_cp = true;
  110. else if (!space_for_roll_forward(sbi))
  111. need_cp = true;
  112. else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
  113. need_cp = true;
  114. else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
  115. need_cp = true;
  116. else if (test_opt(sbi, FASTBOOT))
  117. need_cp = true;
  118. else if (sbi->active_logs == 2)
  119. need_cp = true;
  120. return need_cp;
  121. }
  122. static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
  123. {
  124. struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
  125. bool ret = false;
  126. /* But we need to avoid that there are some inode updates */
  127. if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
  128. ret = true;
  129. f2fs_put_page(i, 0);
  130. return ret;
  131. }
  132. static void try_to_fix_pino(struct inode *inode)
  133. {
  134. struct f2fs_inode_info *fi = F2FS_I(inode);
  135. nid_t pino;
  136. down_write(&fi->i_sem);
  137. fi->xattr_ver = 0;
  138. if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
  139. get_parent_ino(inode, &pino)) {
  140. fi->i_pino = pino;
  141. file_got_pino(inode);
  142. up_write(&fi->i_sem);
  143. mark_inode_dirty_sync(inode);
  144. f2fs_write_inode(inode, NULL);
  145. } else {
  146. up_write(&fi->i_sem);
  147. }
  148. }
  149. int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  150. {
  151. struct inode *inode = file->f_mapping->host;
  152. struct f2fs_inode_info *fi = F2FS_I(inode);
  153. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  154. nid_t ino = inode->i_ino;
  155. int ret = 0;
  156. bool need_cp = false;
  157. struct writeback_control wbc = {
  158. .sync_mode = WB_SYNC_ALL,
  159. .nr_to_write = LONG_MAX,
  160. .for_reclaim = 0,
  161. };
  162. if (unlikely(f2fs_readonly(inode->i_sb)))
  163. return 0;
  164. trace_f2fs_sync_file_enter(inode);
  165. /* if fdatasync is triggered, let's do in-place-update */
  166. if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
  167. set_inode_flag(fi, FI_NEED_IPU);
  168. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  169. clear_inode_flag(fi, FI_NEED_IPU);
  170. if (ret) {
  171. trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
  172. return ret;
  173. }
  174. /* if the inode is dirty, let's recover all the time */
  175. if (!datasync && is_inode_flag_set(fi, FI_DIRTY_INODE)) {
  176. update_inode_page(inode);
  177. goto go_write;
  178. }
  179. /*
  180. * if there is no written data, don't waste time to write recovery info.
  181. */
  182. if (!is_inode_flag_set(fi, FI_APPEND_WRITE) &&
  183. !exist_written_data(sbi, ino, APPEND_INO)) {
  184. /* it may call write_inode just prior to fsync */
  185. if (need_inode_page_update(sbi, ino))
  186. goto go_write;
  187. if (is_inode_flag_set(fi, FI_UPDATE_WRITE) ||
  188. exist_written_data(sbi, ino, UPDATE_INO))
  189. goto flush_out;
  190. goto out;
  191. }
  192. go_write:
  193. /* guarantee free sections for fsync */
  194. f2fs_balance_fs(sbi);
  195. /*
  196. * Both of fdatasync() and fsync() are able to be recovered from
  197. * sudden-power-off.
  198. */
  199. down_read(&fi->i_sem);
  200. need_cp = need_do_checkpoint(inode);
  201. up_read(&fi->i_sem);
  202. if (need_cp) {
  203. /* all the dirty node pages should be flushed for POR */
  204. ret = f2fs_sync_fs(inode->i_sb, 1);
  205. /*
  206. * We've secured consistency through sync_fs. Following pino
  207. * will be used only for fsynced inodes after checkpoint.
  208. */
  209. try_to_fix_pino(inode);
  210. goto out;
  211. }
  212. sync_nodes:
  213. sync_node_pages(sbi, ino, &wbc);
  214. /* if cp_error was enabled, we should avoid infinite loop */
  215. if (unlikely(f2fs_cp_error(sbi)))
  216. goto out;
  217. if (need_inode_block_update(sbi, ino)) {
  218. mark_inode_dirty_sync(inode);
  219. f2fs_write_inode(inode, NULL);
  220. goto sync_nodes;
  221. }
  222. ret = wait_on_node_pages_writeback(sbi, ino);
  223. if (ret)
  224. goto out;
  225. /* once recovery info is written, don't need to tack this */
  226. remove_dirty_inode(sbi, ino, APPEND_INO);
  227. clear_inode_flag(fi, FI_APPEND_WRITE);
  228. flush_out:
  229. remove_dirty_inode(sbi, ino, UPDATE_INO);
  230. clear_inode_flag(fi, FI_UPDATE_WRITE);
  231. ret = f2fs_issue_flush(sbi);
  232. out:
  233. trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
  234. f2fs_trace_ios(NULL, NULL, 1);
  235. return ret;
  236. }
  237. static pgoff_t __get_first_dirty_index(struct address_space *mapping,
  238. pgoff_t pgofs, int whence)
  239. {
  240. struct pagevec pvec;
  241. int nr_pages;
  242. if (whence != SEEK_DATA)
  243. return 0;
  244. /* find first dirty page index */
  245. pagevec_init(&pvec, 0);
  246. nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
  247. PAGECACHE_TAG_DIRTY, 1);
  248. pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX;
  249. pagevec_release(&pvec);
  250. return pgofs;
  251. }
  252. static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
  253. int whence)
  254. {
  255. switch (whence) {
  256. case SEEK_DATA:
  257. if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
  258. (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
  259. return true;
  260. break;
  261. case SEEK_HOLE:
  262. if (blkaddr == NULL_ADDR)
  263. return true;
  264. break;
  265. }
  266. return false;
  267. }
  268. static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
  269. {
  270. struct inode *inode = file->f_mapping->host;
  271. loff_t maxbytes = inode->i_sb->s_maxbytes;
  272. struct dnode_of_data dn;
  273. pgoff_t pgofs, end_offset, dirty;
  274. loff_t data_ofs = offset;
  275. loff_t isize;
  276. int err = 0;
  277. mutex_lock(&inode->i_mutex);
  278. isize = i_size_read(inode);
  279. if (offset >= isize)
  280. goto fail;
  281. /* handle inline data case */
  282. if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
  283. if (whence == SEEK_HOLE)
  284. data_ofs = isize;
  285. goto found;
  286. }
  287. pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT);
  288. dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
  289. for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) {
  290. set_new_dnode(&dn, inode, NULL, NULL, 0);
  291. err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
  292. if (err && err != -ENOENT) {
  293. goto fail;
  294. } else if (err == -ENOENT) {
  295. /* direct node does not exists */
  296. if (whence == SEEK_DATA) {
  297. pgofs = PGOFS_OF_NEXT_DNODE(pgofs,
  298. F2FS_I(inode));
  299. continue;
  300. } else {
  301. goto found;
  302. }
  303. }
  304. end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  305. /* find data/hole in dnode block */
  306. for (; dn.ofs_in_node < end_offset;
  307. dn.ofs_in_node++, pgofs++,
  308. data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) {
  309. block_t blkaddr;
  310. blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
  311. if (__found_offset(blkaddr, dirty, pgofs, whence)) {
  312. f2fs_put_dnode(&dn);
  313. goto found;
  314. }
  315. }
  316. f2fs_put_dnode(&dn);
  317. }
  318. if (whence == SEEK_DATA)
  319. goto fail;
  320. found:
  321. if (whence == SEEK_HOLE && data_ofs > isize)
  322. data_ofs = isize;
  323. mutex_unlock(&inode->i_mutex);
  324. return vfs_setpos(file, data_ofs, maxbytes);
  325. fail:
  326. mutex_unlock(&inode->i_mutex);
  327. return -ENXIO;
  328. }
  329. static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
  330. {
  331. struct inode *inode = file->f_mapping->host;
  332. loff_t maxbytes = inode->i_sb->s_maxbytes;
  333. switch (whence) {
  334. case SEEK_SET:
  335. case SEEK_CUR:
  336. case SEEK_END:
  337. return generic_file_llseek_size(file, offset, whence,
  338. maxbytes, i_size_read(inode));
  339. case SEEK_DATA:
  340. case SEEK_HOLE:
  341. if (offset < 0)
  342. return -ENXIO;
  343. return f2fs_seek_block(file, offset, whence);
  344. }
  345. return -EINVAL;
  346. }
  347. static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
  348. {
  349. struct inode *inode = file_inode(file);
  350. /* we don't need to use inline_data strictly */
  351. if (f2fs_has_inline_data(inode)) {
  352. int err = f2fs_convert_inline_inode(inode);
  353. if (err)
  354. return err;
  355. }
  356. file_accessed(file);
  357. vma->vm_ops = &f2fs_file_vm_ops;
  358. return 0;
  359. }
  360. int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
  361. {
  362. int nr_free = 0, ofs = dn->ofs_in_node;
  363. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  364. struct f2fs_node *raw_node;
  365. __le32 *addr;
  366. raw_node = F2FS_NODE(dn->node_page);
  367. addr = blkaddr_in_node(raw_node) + ofs;
  368. for (; count > 0; count--, addr++, dn->ofs_in_node++) {
  369. block_t blkaddr = le32_to_cpu(*addr);
  370. if (blkaddr == NULL_ADDR)
  371. continue;
  372. dn->data_blkaddr = NULL_ADDR;
  373. update_extent_cache(dn);
  374. invalidate_blocks(sbi, blkaddr);
  375. nr_free++;
  376. }
  377. if (nr_free) {
  378. dec_valid_block_count(sbi, dn->inode, nr_free);
  379. set_page_dirty(dn->node_page);
  380. sync_inode_page(dn);
  381. }
  382. dn->ofs_in_node = ofs;
  383. trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
  384. dn->ofs_in_node, nr_free);
  385. return nr_free;
  386. }
  387. void truncate_data_blocks(struct dnode_of_data *dn)
  388. {
  389. truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
  390. }
  391. static int truncate_partial_data_page(struct inode *inode, u64 from)
  392. {
  393. unsigned offset = from & (PAGE_CACHE_SIZE - 1);
  394. struct page *page;
  395. if (!offset)
  396. return 0;
  397. page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false);
  398. if (IS_ERR(page))
  399. return 0;
  400. lock_page(page);
  401. if (unlikely(!PageUptodate(page) ||
  402. page->mapping != inode->i_mapping))
  403. goto out;
  404. f2fs_wait_on_page_writeback(page, DATA);
  405. zero_user(page, offset, PAGE_CACHE_SIZE - offset);
  406. set_page_dirty(page);
  407. out:
  408. f2fs_put_page(page, 1);
  409. return 0;
  410. }
  411. int truncate_blocks(struct inode *inode, u64 from, bool lock)
  412. {
  413. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  414. unsigned int blocksize = inode->i_sb->s_blocksize;
  415. struct dnode_of_data dn;
  416. pgoff_t free_from;
  417. int count = 0, err = 0;
  418. struct page *ipage;
  419. trace_f2fs_truncate_blocks_enter(inode, from);
  420. free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
  421. if (lock)
  422. f2fs_lock_op(sbi);
  423. ipage = get_node_page(sbi, inode->i_ino);
  424. if (IS_ERR(ipage)) {
  425. err = PTR_ERR(ipage);
  426. goto out;
  427. }
  428. if (f2fs_has_inline_data(inode)) {
  429. f2fs_put_page(ipage, 1);
  430. goto out;
  431. }
  432. set_new_dnode(&dn, inode, ipage, NULL, 0);
  433. err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
  434. if (err) {
  435. if (err == -ENOENT)
  436. goto free_next;
  437. goto out;
  438. }
  439. count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
  440. count -= dn.ofs_in_node;
  441. f2fs_bug_on(sbi, count < 0);
  442. if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
  443. truncate_data_blocks_range(&dn, count);
  444. free_from += count;
  445. }
  446. f2fs_put_dnode(&dn);
  447. free_next:
  448. err = truncate_inode_blocks(inode, free_from);
  449. out:
  450. if (lock)
  451. f2fs_unlock_op(sbi);
  452. /* lastly zero out the first data page */
  453. if (!err)
  454. err = truncate_partial_data_page(inode, from);
  455. trace_f2fs_truncate_blocks_exit(inode, err);
  456. return err;
  457. }
  458. void f2fs_truncate(struct inode *inode)
  459. {
  460. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  461. S_ISLNK(inode->i_mode)))
  462. return;
  463. trace_f2fs_truncate(inode);
  464. /* we should check inline_data size */
  465. if (f2fs_has_inline_data(inode) && !f2fs_may_inline(inode)) {
  466. if (f2fs_convert_inline_inode(inode))
  467. return;
  468. }
  469. if (!truncate_blocks(inode, i_size_read(inode), true)) {
  470. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  471. mark_inode_dirty(inode);
  472. }
  473. }
  474. int f2fs_getattr(struct vfsmount *mnt,
  475. struct dentry *dentry, struct kstat *stat)
  476. {
  477. struct inode *inode = dentry->d_inode;
  478. generic_fillattr(inode, stat);
  479. stat->blocks <<= 3;
  480. return 0;
  481. }
  482. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  483. static void __setattr_copy(struct inode *inode, const struct iattr *attr)
  484. {
  485. struct f2fs_inode_info *fi = F2FS_I(inode);
  486. unsigned int ia_valid = attr->ia_valid;
  487. if (ia_valid & ATTR_UID)
  488. inode->i_uid = attr->ia_uid;
  489. if (ia_valid & ATTR_GID)
  490. inode->i_gid = attr->ia_gid;
  491. if (ia_valid & ATTR_ATIME)
  492. inode->i_atime = timespec_trunc(attr->ia_atime,
  493. inode->i_sb->s_time_gran);
  494. if (ia_valid & ATTR_MTIME)
  495. inode->i_mtime = timespec_trunc(attr->ia_mtime,
  496. inode->i_sb->s_time_gran);
  497. if (ia_valid & ATTR_CTIME)
  498. inode->i_ctime = timespec_trunc(attr->ia_ctime,
  499. inode->i_sb->s_time_gran);
  500. if (ia_valid & ATTR_MODE) {
  501. umode_t mode = attr->ia_mode;
  502. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  503. mode &= ~S_ISGID;
  504. set_acl_inode(fi, mode);
  505. }
  506. }
  507. #else
  508. #define __setattr_copy setattr_copy
  509. #endif
  510. int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
  511. {
  512. struct inode *inode = dentry->d_inode;
  513. struct f2fs_inode_info *fi = F2FS_I(inode);
  514. int err;
  515. err = inode_change_ok(inode, attr);
  516. if (err)
  517. return err;
  518. if (attr->ia_valid & ATTR_SIZE) {
  519. if (attr->ia_size != i_size_read(inode)) {
  520. truncate_setsize(inode, attr->ia_size);
  521. f2fs_truncate(inode);
  522. f2fs_balance_fs(F2FS_I_SB(inode));
  523. } else {
  524. /*
  525. * giving a chance to truncate blocks past EOF which
  526. * are fallocated with FALLOC_FL_KEEP_SIZE.
  527. */
  528. f2fs_truncate(inode);
  529. }
  530. }
  531. __setattr_copy(inode, attr);
  532. if (attr->ia_valid & ATTR_MODE) {
  533. err = posix_acl_chmod(inode, get_inode_mode(inode));
  534. if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
  535. inode->i_mode = fi->i_acl_mode;
  536. clear_inode_flag(fi, FI_ACL_MODE);
  537. }
  538. }
  539. mark_inode_dirty(inode);
  540. return err;
  541. }
  542. const struct inode_operations f2fs_file_inode_operations = {
  543. .getattr = f2fs_getattr,
  544. .setattr = f2fs_setattr,
  545. .get_acl = f2fs_get_acl,
  546. .set_acl = f2fs_set_acl,
  547. #ifdef CONFIG_F2FS_FS_XATTR
  548. .setxattr = generic_setxattr,
  549. .getxattr = generic_getxattr,
  550. .listxattr = f2fs_listxattr,
  551. .removexattr = generic_removexattr,
  552. #endif
  553. .fiemap = f2fs_fiemap,
  554. };
  555. static void fill_zero(struct inode *inode, pgoff_t index,
  556. loff_t start, loff_t len)
  557. {
  558. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  559. struct page *page;
  560. if (!len)
  561. return;
  562. f2fs_balance_fs(sbi);
  563. f2fs_lock_op(sbi);
  564. page = get_new_data_page(inode, NULL, index, false);
  565. f2fs_unlock_op(sbi);
  566. if (!IS_ERR(page)) {
  567. f2fs_wait_on_page_writeback(page, DATA);
  568. zero_user(page, start, len);
  569. set_page_dirty(page);
  570. f2fs_put_page(page, 1);
  571. }
  572. }
  573. int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
  574. {
  575. pgoff_t index;
  576. int err;
  577. for (index = pg_start; index < pg_end; index++) {
  578. struct dnode_of_data dn;
  579. set_new_dnode(&dn, inode, NULL, NULL, 0);
  580. err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
  581. if (err) {
  582. if (err == -ENOENT)
  583. continue;
  584. return err;
  585. }
  586. if (dn.data_blkaddr != NULL_ADDR)
  587. truncate_data_blocks_range(&dn, 1);
  588. f2fs_put_dnode(&dn);
  589. }
  590. return 0;
  591. }
  592. static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
  593. {
  594. pgoff_t pg_start, pg_end;
  595. loff_t off_start, off_end;
  596. int ret = 0;
  597. if (!S_ISREG(inode->i_mode))
  598. return -EOPNOTSUPP;
  599. /* skip punching hole beyond i_size */
  600. if (offset >= inode->i_size)
  601. return ret;
  602. if (f2fs_has_inline_data(inode)) {
  603. ret = f2fs_convert_inline_inode(inode);
  604. if (ret)
  605. return ret;
  606. }
  607. pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
  608. pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
  609. off_start = offset & (PAGE_CACHE_SIZE - 1);
  610. off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
  611. if (pg_start == pg_end) {
  612. fill_zero(inode, pg_start, off_start,
  613. off_end - off_start);
  614. } else {
  615. if (off_start)
  616. fill_zero(inode, pg_start++, off_start,
  617. PAGE_CACHE_SIZE - off_start);
  618. if (off_end)
  619. fill_zero(inode, pg_end, 0, off_end);
  620. if (pg_start < pg_end) {
  621. struct address_space *mapping = inode->i_mapping;
  622. loff_t blk_start, blk_end;
  623. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  624. f2fs_balance_fs(sbi);
  625. blk_start = pg_start << PAGE_CACHE_SHIFT;
  626. blk_end = pg_end << PAGE_CACHE_SHIFT;
  627. truncate_inode_pages_range(mapping, blk_start,
  628. blk_end - 1);
  629. f2fs_lock_op(sbi);
  630. ret = truncate_hole(inode, pg_start, pg_end);
  631. f2fs_unlock_op(sbi);
  632. }
  633. }
  634. return ret;
  635. }
  636. static int expand_inode_data(struct inode *inode, loff_t offset,
  637. loff_t len, int mode)
  638. {
  639. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  640. pgoff_t index, pg_start, pg_end;
  641. loff_t new_size = i_size_read(inode);
  642. loff_t off_start, off_end;
  643. int ret = 0;
  644. f2fs_balance_fs(sbi);
  645. ret = inode_newsize_ok(inode, (len + offset));
  646. if (ret)
  647. return ret;
  648. if (f2fs_has_inline_data(inode)) {
  649. ret = f2fs_convert_inline_inode(inode);
  650. if (ret)
  651. return ret;
  652. }
  653. pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
  654. pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
  655. off_start = offset & (PAGE_CACHE_SIZE - 1);
  656. off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
  657. f2fs_lock_op(sbi);
  658. for (index = pg_start; index <= pg_end; index++) {
  659. struct dnode_of_data dn;
  660. if (index == pg_end && !off_end)
  661. goto noalloc;
  662. set_new_dnode(&dn, inode, NULL, NULL, 0);
  663. ret = f2fs_reserve_block(&dn, index);
  664. if (ret)
  665. break;
  666. noalloc:
  667. if (pg_start == pg_end)
  668. new_size = offset + len;
  669. else if (index == pg_start && off_start)
  670. new_size = (index + 1) << PAGE_CACHE_SHIFT;
  671. else if (index == pg_end)
  672. new_size = (index << PAGE_CACHE_SHIFT) + off_end;
  673. else
  674. new_size += PAGE_CACHE_SIZE;
  675. }
  676. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  677. i_size_read(inode) < new_size) {
  678. i_size_write(inode, new_size);
  679. mark_inode_dirty(inode);
  680. update_inode_page(inode);
  681. }
  682. f2fs_unlock_op(sbi);
  683. return ret;
  684. }
  685. static long f2fs_fallocate(struct file *file, int mode,
  686. loff_t offset, loff_t len)
  687. {
  688. struct inode *inode = file_inode(file);
  689. long ret;
  690. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  691. return -EOPNOTSUPP;
  692. mutex_lock(&inode->i_mutex);
  693. if (mode & FALLOC_FL_PUNCH_HOLE)
  694. ret = punch_hole(inode, offset, len);
  695. else
  696. ret = expand_inode_data(inode, offset, len, mode);
  697. if (!ret) {
  698. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  699. mark_inode_dirty(inode);
  700. }
  701. mutex_unlock(&inode->i_mutex);
  702. trace_f2fs_fallocate(inode, mode, offset, len, ret);
  703. return ret;
  704. }
  705. static int f2fs_release_file(struct inode *inode, struct file *filp)
  706. {
  707. /* some remained atomic pages should discarded */
  708. if (f2fs_is_atomic_file(inode))
  709. commit_inmem_pages(inode, true);
  710. if (f2fs_is_volatile_file(inode)) {
  711. set_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
  712. filemap_fdatawrite(inode->i_mapping);
  713. clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
  714. }
  715. return 0;
  716. }
  717. #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
  718. #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
  719. static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
  720. {
  721. if (S_ISDIR(mode))
  722. return flags;
  723. else if (S_ISREG(mode))
  724. return flags & F2FS_REG_FLMASK;
  725. else
  726. return flags & F2FS_OTHER_FLMASK;
  727. }
  728. static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
  729. {
  730. struct inode *inode = file_inode(filp);
  731. struct f2fs_inode_info *fi = F2FS_I(inode);
  732. unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
  733. return put_user(flags, (int __user *)arg);
  734. }
  735. static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
  736. {
  737. struct inode *inode = file_inode(filp);
  738. struct f2fs_inode_info *fi = F2FS_I(inode);
  739. unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
  740. unsigned int oldflags;
  741. int ret;
  742. ret = mnt_want_write_file(filp);
  743. if (ret)
  744. return ret;
  745. if (!inode_owner_or_capable(inode)) {
  746. ret = -EACCES;
  747. goto out;
  748. }
  749. if (get_user(flags, (int __user *)arg)) {
  750. ret = -EFAULT;
  751. goto out;
  752. }
  753. flags = f2fs_mask_flags(inode->i_mode, flags);
  754. mutex_lock(&inode->i_mutex);
  755. oldflags = fi->i_flags;
  756. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  757. if (!capable(CAP_LINUX_IMMUTABLE)) {
  758. mutex_unlock(&inode->i_mutex);
  759. ret = -EPERM;
  760. goto out;
  761. }
  762. }
  763. flags = flags & FS_FL_USER_MODIFIABLE;
  764. flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
  765. fi->i_flags = flags;
  766. mutex_unlock(&inode->i_mutex);
  767. f2fs_set_inode_flags(inode);
  768. inode->i_ctime = CURRENT_TIME;
  769. mark_inode_dirty(inode);
  770. out:
  771. mnt_drop_write_file(filp);
  772. return ret;
  773. }
  774. static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
  775. {
  776. struct inode *inode = file_inode(filp);
  777. return put_user(inode->i_generation, (int __user *)arg);
  778. }
  779. static int f2fs_ioc_start_atomic_write(struct file *filp)
  780. {
  781. struct inode *inode = file_inode(filp);
  782. if (!inode_owner_or_capable(inode))
  783. return -EACCES;
  784. f2fs_balance_fs(F2FS_I_SB(inode));
  785. if (f2fs_is_atomic_file(inode))
  786. return 0;
  787. set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
  788. return f2fs_convert_inline_inode(inode);
  789. }
  790. static int f2fs_ioc_commit_atomic_write(struct file *filp)
  791. {
  792. struct inode *inode = file_inode(filp);
  793. int ret;
  794. if (!inode_owner_or_capable(inode))
  795. return -EACCES;
  796. if (f2fs_is_volatile_file(inode))
  797. return 0;
  798. ret = mnt_want_write_file(filp);
  799. if (ret)
  800. return ret;
  801. if (f2fs_is_atomic_file(inode))
  802. commit_inmem_pages(inode, false);
  803. ret = f2fs_sync_file(filp, 0, LONG_MAX, 0);
  804. mnt_drop_write_file(filp);
  805. clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
  806. return ret;
  807. }
  808. static int f2fs_ioc_start_volatile_write(struct file *filp)
  809. {
  810. struct inode *inode = file_inode(filp);
  811. if (!inode_owner_or_capable(inode))
  812. return -EACCES;
  813. if (f2fs_is_volatile_file(inode))
  814. return 0;
  815. set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
  816. return f2fs_convert_inline_inode(inode);
  817. }
  818. static int f2fs_ioc_release_volatile_write(struct file *filp)
  819. {
  820. struct inode *inode = file_inode(filp);
  821. if (!inode_owner_or_capable(inode))
  822. return -EACCES;
  823. if (!f2fs_is_volatile_file(inode))
  824. return 0;
  825. punch_hole(inode, 0, F2FS_BLKSIZE);
  826. return 0;
  827. }
  828. static int f2fs_ioc_abort_volatile_write(struct file *filp)
  829. {
  830. struct inode *inode = file_inode(filp);
  831. int ret;
  832. if (!inode_owner_or_capable(inode))
  833. return -EACCES;
  834. ret = mnt_want_write_file(filp);
  835. if (ret)
  836. return ret;
  837. f2fs_balance_fs(F2FS_I_SB(inode));
  838. if (f2fs_is_atomic_file(inode)) {
  839. commit_inmem_pages(inode, false);
  840. clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
  841. }
  842. if (f2fs_is_volatile_file(inode)) {
  843. clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
  844. filemap_fdatawrite(inode->i_mapping);
  845. set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
  846. }
  847. mnt_drop_write_file(filp);
  848. return ret;
  849. }
  850. static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
  851. {
  852. struct inode *inode = file_inode(filp);
  853. struct super_block *sb = inode->i_sb;
  854. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  855. struct fstrim_range range;
  856. int ret;
  857. if (!capable(CAP_SYS_ADMIN))
  858. return -EPERM;
  859. if (!blk_queue_discard(q))
  860. return -EOPNOTSUPP;
  861. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  862. sizeof(range)))
  863. return -EFAULT;
  864. range.minlen = max((unsigned int)range.minlen,
  865. q->limits.discard_granularity);
  866. ret = f2fs_trim_fs(F2FS_SB(sb), &range);
  867. if (ret < 0)
  868. return ret;
  869. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  870. sizeof(range)))
  871. return -EFAULT;
  872. return 0;
  873. }
  874. long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  875. {
  876. switch (cmd) {
  877. case F2FS_IOC_GETFLAGS:
  878. return f2fs_ioc_getflags(filp, arg);
  879. case F2FS_IOC_SETFLAGS:
  880. return f2fs_ioc_setflags(filp, arg);
  881. case F2FS_IOC_GETVERSION:
  882. return f2fs_ioc_getversion(filp, arg);
  883. case F2FS_IOC_START_ATOMIC_WRITE:
  884. return f2fs_ioc_start_atomic_write(filp);
  885. case F2FS_IOC_COMMIT_ATOMIC_WRITE:
  886. return f2fs_ioc_commit_atomic_write(filp);
  887. case F2FS_IOC_START_VOLATILE_WRITE:
  888. return f2fs_ioc_start_volatile_write(filp);
  889. case F2FS_IOC_RELEASE_VOLATILE_WRITE:
  890. return f2fs_ioc_release_volatile_write(filp);
  891. case F2FS_IOC_ABORT_VOLATILE_WRITE:
  892. return f2fs_ioc_abort_volatile_write(filp);
  893. case FITRIM:
  894. return f2fs_ioc_fitrim(filp, arg);
  895. default:
  896. return -ENOTTY;
  897. }
  898. }
  899. #ifdef CONFIG_COMPAT
  900. long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  901. {
  902. switch (cmd) {
  903. case F2FS_IOC32_GETFLAGS:
  904. cmd = F2FS_IOC_GETFLAGS;
  905. break;
  906. case F2FS_IOC32_SETFLAGS:
  907. cmd = F2FS_IOC_SETFLAGS;
  908. break;
  909. default:
  910. return -ENOIOCTLCMD;
  911. }
  912. return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  913. }
  914. #endif
  915. const struct file_operations f2fs_file_operations = {
  916. .llseek = f2fs_llseek,
  917. .read = new_sync_read,
  918. .write = new_sync_write,
  919. .read_iter = generic_file_read_iter,
  920. .write_iter = generic_file_write_iter,
  921. .open = generic_file_open,
  922. .release = f2fs_release_file,
  923. .mmap = f2fs_file_mmap,
  924. .fsync = f2fs_sync_file,
  925. .fallocate = f2fs_fallocate,
  926. .unlocked_ioctl = f2fs_ioctl,
  927. #ifdef CONFIG_COMPAT
  928. .compat_ioctl = f2fs_compat_ioctl,
  929. #endif
  930. .splice_read = generic_file_splice_read,
  931. .splice_write = iter_file_splice_write,
  932. };