inode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * inode.c - NILFS inode operations.
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Ryusuke Konishi <ryusuke@osrg.net>
  21. *
  22. */
  23. #include <linux/buffer_head.h>
  24. #include <linux/gfp.h>
  25. #include <linux/mpage.h>
  26. #include <linux/writeback.h>
  27. #include <linux/uio.h>
  28. #include "nilfs.h"
  29. #include "btnode.h"
  30. #include "segment.h"
  31. #include "page.h"
  32. #include "mdt.h"
  33. #include "cpfile.h"
  34. #include "ifile.h"
  35. /**
  36. * nilfs_get_block() - get a file block on the filesystem (callback function)
  37. * @inode - inode struct of the target file
  38. * @blkoff - file block number
  39. * @bh_result - buffer head to be mapped on
  40. * @create - indicate whether allocating the block or not when it has not
  41. * been allocated yet.
  42. *
  43. * This function does not issue actual read request of the specified data
  44. * block. It is done by VFS.
  45. */
  46. int nilfs_get_block(struct inode *inode, sector_t blkoff,
  47. struct buffer_head *bh_result, int create)
  48. {
  49. struct nilfs_inode_info *ii = NILFS_I(inode);
  50. __u64 blknum = 0;
  51. int err = 0, ret;
  52. struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
  53. unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
  54. down_read(&NILFS_MDT(dat)->mi_sem);
  55. ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
  56. up_read(&NILFS_MDT(dat)->mi_sem);
  57. if (ret >= 0) { /* found */
  58. map_bh(bh_result, inode->i_sb, blknum);
  59. if (ret > 0)
  60. bh_result->b_size = (ret << inode->i_blkbits);
  61. goto out;
  62. }
  63. /* data block was not found */
  64. if (ret == -ENOENT && create) {
  65. struct nilfs_transaction_info ti;
  66. bh_result->b_blocknr = 0;
  67. err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
  68. if (unlikely(err))
  69. goto out;
  70. err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
  71. (unsigned long)bh_result);
  72. if (unlikely(err != 0)) {
  73. if (err == -EEXIST) {
  74. /*
  75. * The get_block() function could be called
  76. * from multiple callers for an inode.
  77. * However, the page having this block must
  78. * be locked in this case.
  79. */
  80. printk(KERN_WARNING
  81. "nilfs_get_block: a race condition "
  82. "while inserting a data block. "
  83. "(inode number=%lu, file block "
  84. "offset=%llu)\n",
  85. inode->i_ino,
  86. (unsigned long long)blkoff);
  87. err = 0;
  88. } else if (err == -EINVAL) {
  89. nilfs_error(inode->i_sb, __func__,
  90. "broken bmap (inode=%lu)\n",
  91. inode->i_ino);
  92. err = -EIO;
  93. }
  94. nilfs_transaction_abort(inode->i_sb);
  95. goto out;
  96. }
  97. nilfs_mark_inode_dirty(inode);
  98. nilfs_transaction_commit(inode->i_sb); /* never fails */
  99. /* Error handling should be detailed */
  100. set_buffer_new(bh_result);
  101. map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
  102. to proper value */
  103. } else if (ret == -ENOENT) {
  104. /* not found is not error (e.g. hole); must return without
  105. the mapped state flag. */
  106. ;
  107. } else {
  108. err = ret;
  109. }
  110. out:
  111. return err;
  112. }
  113. /**
  114. * nilfs_readpage() - implement readpage() method of nilfs_aops {}
  115. * address_space_operations.
  116. * @file - file struct of the file to be read
  117. * @page - the page to be read
  118. */
  119. static int nilfs_readpage(struct file *file, struct page *page)
  120. {
  121. return mpage_readpage(page, nilfs_get_block);
  122. }
  123. /**
  124. * nilfs_readpages() - implement readpages() method of nilfs_aops {}
  125. * address_space_operations.
  126. * @file - file struct of the file to be read
  127. * @mapping - address_space struct used for reading multiple pages
  128. * @pages - the pages to be read
  129. * @nr_pages - number of pages to be read
  130. */
  131. static int nilfs_readpages(struct file *file, struct address_space *mapping,
  132. struct list_head *pages, unsigned nr_pages)
  133. {
  134. return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
  135. }
  136. static int nilfs_writepages(struct address_space *mapping,
  137. struct writeback_control *wbc)
  138. {
  139. struct inode *inode = mapping->host;
  140. int err = 0;
  141. if (wbc->sync_mode == WB_SYNC_ALL)
  142. err = nilfs_construct_dsync_segment(inode->i_sb, inode,
  143. wbc->range_start,
  144. wbc->range_end);
  145. return err;
  146. }
  147. static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
  148. {
  149. struct inode *inode = page->mapping->host;
  150. int err;
  151. redirty_page_for_writepage(wbc, page);
  152. unlock_page(page);
  153. if (wbc->sync_mode == WB_SYNC_ALL) {
  154. err = nilfs_construct_segment(inode->i_sb);
  155. if (unlikely(err))
  156. return err;
  157. } else if (wbc->for_reclaim)
  158. nilfs_flush_segment(inode->i_sb, inode->i_ino);
  159. return 0;
  160. }
  161. static int nilfs_set_page_dirty(struct page *page)
  162. {
  163. int ret = __set_page_dirty_buffers(page);
  164. if (ret) {
  165. struct inode *inode = page->mapping->host;
  166. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  167. unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
  168. nilfs_set_file_dirty(sbi, inode, nr_dirty);
  169. }
  170. return ret;
  171. }
  172. static int nilfs_write_begin(struct file *file, struct address_space *mapping,
  173. loff_t pos, unsigned len, unsigned flags,
  174. struct page **pagep, void **fsdata)
  175. {
  176. struct inode *inode = mapping->host;
  177. int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
  178. if (unlikely(err))
  179. return err;
  180. err = block_write_begin(mapping, pos, len, flags, pagep,
  181. nilfs_get_block);
  182. if (unlikely(err)) {
  183. loff_t isize = mapping->host->i_size;
  184. if (pos + len > isize)
  185. vmtruncate(mapping->host, isize);
  186. nilfs_transaction_abort(inode->i_sb);
  187. }
  188. return err;
  189. }
  190. static int nilfs_write_end(struct file *file, struct address_space *mapping,
  191. loff_t pos, unsigned len, unsigned copied,
  192. struct page *page, void *fsdata)
  193. {
  194. struct inode *inode = mapping->host;
  195. unsigned start = pos & (PAGE_CACHE_SIZE - 1);
  196. unsigned nr_dirty;
  197. int err;
  198. nr_dirty = nilfs_page_count_clean_buffers(page, start,
  199. start + copied);
  200. copied = generic_write_end(file, mapping, pos, len, copied, page,
  201. fsdata);
  202. nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty);
  203. err = nilfs_transaction_commit(inode->i_sb);
  204. return err ? : copied;
  205. }
  206. static ssize_t
  207. nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  208. loff_t offset, unsigned long nr_segs)
  209. {
  210. struct file *file = iocb->ki_filp;
  211. struct inode *inode = file->f_mapping->host;
  212. ssize_t size;
  213. if (rw == WRITE)
  214. return 0;
  215. /* Needs synchronization with the cleaner */
  216. size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  217. offset, nr_segs, nilfs_get_block, NULL);
  218. /*
  219. * In case of error extending write may have instantiated a few
  220. * blocks outside i_size. Trim these off again.
  221. */
  222. if (unlikely((rw & WRITE) && size < 0)) {
  223. loff_t isize = i_size_read(inode);
  224. loff_t end = offset + iov_length(iov, nr_segs);
  225. if (end > isize)
  226. vmtruncate(inode, isize);
  227. }
  228. return size;
  229. }
  230. const struct address_space_operations nilfs_aops = {
  231. .writepage = nilfs_writepage,
  232. .readpage = nilfs_readpage,
  233. .sync_page = block_sync_page,
  234. .writepages = nilfs_writepages,
  235. .set_page_dirty = nilfs_set_page_dirty,
  236. .readpages = nilfs_readpages,
  237. .write_begin = nilfs_write_begin,
  238. .write_end = nilfs_write_end,
  239. /* .releasepage = nilfs_releasepage, */
  240. .invalidatepage = block_invalidatepage,
  241. .direct_IO = nilfs_direct_IO,
  242. .is_partially_uptodate = block_is_partially_uptodate,
  243. };
  244. struct inode *nilfs_new_inode(struct inode *dir, int mode)
  245. {
  246. struct super_block *sb = dir->i_sb;
  247. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  248. struct inode *inode;
  249. struct nilfs_inode_info *ii;
  250. int err = -ENOMEM;
  251. ino_t ino;
  252. inode = new_inode(sb);
  253. if (unlikely(!inode))
  254. goto failed;
  255. mapping_set_gfp_mask(inode->i_mapping,
  256. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  257. ii = NILFS_I(inode);
  258. ii->i_state = 1 << NILFS_I_NEW;
  259. err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
  260. if (unlikely(err))
  261. goto failed_ifile_create_inode;
  262. /* reference count of i_bh inherits from nilfs_mdt_read_block() */
  263. atomic_inc(&sbi->s_inodes_count);
  264. inode_init_owner(inode, dir, mode);
  265. inode->i_ino = ino;
  266. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  267. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  268. err = nilfs_bmap_read(ii->i_bmap, NULL);
  269. if (err < 0)
  270. goto failed_bmap;
  271. set_bit(NILFS_I_BMAP, &ii->i_state);
  272. /* No lock is needed; iget() ensures it. */
  273. }
  274. ii->i_flags = NILFS_I(dir)->i_flags;
  275. if (S_ISLNK(mode))
  276. ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
  277. if (!S_ISDIR(mode))
  278. ii->i_flags &= ~NILFS_DIRSYNC_FL;
  279. /* ii->i_file_acl = 0; */
  280. /* ii->i_dir_acl = 0; */
  281. ii->i_dir_start_lookup = 0;
  282. ii->i_cno = 0;
  283. nilfs_set_inode_flags(inode);
  284. spin_lock(&sbi->s_next_gen_lock);
  285. inode->i_generation = sbi->s_next_generation++;
  286. spin_unlock(&sbi->s_next_gen_lock);
  287. insert_inode_hash(inode);
  288. err = nilfs_init_acl(inode, dir);
  289. if (unlikely(err))
  290. goto failed_acl; /* never occur. When supporting
  291. nilfs_init_acl(), proper cancellation of
  292. above jobs should be considered */
  293. return inode;
  294. failed_acl:
  295. failed_bmap:
  296. inode->i_nlink = 0;
  297. iput(inode); /* raw_inode will be deleted through
  298. generic_delete_inode() */
  299. goto failed;
  300. failed_ifile_create_inode:
  301. make_bad_inode(inode);
  302. iput(inode); /* if i_nlink == 1, generic_forget_inode() will be
  303. called */
  304. failed:
  305. return ERR_PTR(err);
  306. }
  307. void nilfs_free_inode(struct inode *inode)
  308. {
  309. struct super_block *sb = inode->i_sb;
  310. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  311. /* XXX: check error code? Is there any thing I can do? */
  312. (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
  313. atomic_dec(&sbi->s_inodes_count);
  314. }
  315. void nilfs_set_inode_flags(struct inode *inode)
  316. {
  317. unsigned int flags = NILFS_I(inode)->i_flags;
  318. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
  319. S_DIRSYNC);
  320. if (flags & NILFS_SYNC_FL)
  321. inode->i_flags |= S_SYNC;
  322. if (flags & NILFS_APPEND_FL)
  323. inode->i_flags |= S_APPEND;
  324. if (flags & NILFS_IMMUTABLE_FL)
  325. inode->i_flags |= S_IMMUTABLE;
  326. #ifndef NILFS_ATIME_DISABLE
  327. if (flags & NILFS_NOATIME_FL)
  328. #endif
  329. inode->i_flags |= S_NOATIME;
  330. if (flags & NILFS_DIRSYNC_FL)
  331. inode->i_flags |= S_DIRSYNC;
  332. mapping_set_gfp_mask(inode->i_mapping,
  333. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  334. }
  335. int nilfs_read_inode_common(struct inode *inode,
  336. struct nilfs_inode *raw_inode)
  337. {
  338. struct nilfs_inode_info *ii = NILFS_I(inode);
  339. int err;
  340. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  341. inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
  342. inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
  343. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  344. inode->i_size = le64_to_cpu(raw_inode->i_size);
  345. inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  346. inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
  347. inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  348. inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  349. inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
  350. inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  351. if (inode->i_nlink == 0 && inode->i_mode == 0)
  352. return -EINVAL; /* this inode is deleted */
  353. inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
  354. ii->i_flags = le32_to_cpu(raw_inode->i_flags);
  355. #if 0
  356. ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  357. ii->i_dir_acl = S_ISREG(inode->i_mode) ?
  358. 0 : le32_to_cpu(raw_inode->i_dir_acl);
  359. #endif
  360. ii->i_dir_start_lookup = 0;
  361. ii->i_cno = 0;
  362. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  363. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  364. S_ISLNK(inode->i_mode)) {
  365. err = nilfs_bmap_read(ii->i_bmap, raw_inode);
  366. if (err < 0)
  367. return err;
  368. set_bit(NILFS_I_BMAP, &ii->i_state);
  369. /* No lock is needed; iget() ensures it. */
  370. }
  371. return 0;
  372. }
  373. static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
  374. struct inode *inode)
  375. {
  376. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  377. struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
  378. struct buffer_head *bh;
  379. struct nilfs_inode *raw_inode;
  380. int err;
  381. down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  382. err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
  383. if (unlikely(err))
  384. goto bad_inode;
  385. raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
  386. err = nilfs_read_inode_common(inode, raw_inode);
  387. if (err)
  388. goto failed_unmap;
  389. if (S_ISREG(inode->i_mode)) {
  390. inode->i_op = &nilfs_file_inode_operations;
  391. inode->i_fop = &nilfs_file_operations;
  392. inode->i_mapping->a_ops = &nilfs_aops;
  393. } else if (S_ISDIR(inode->i_mode)) {
  394. inode->i_op = &nilfs_dir_inode_operations;
  395. inode->i_fop = &nilfs_dir_operations;
  396. inode->i_mapping->a_ops = &nilfs_aops;
  397. } else if (S_ISLNK(inode->i_mode)) {
  398. inode->i_op = &nilfs_symlink_inode_operations;
  399. inode->i_mapping->a_ops = &nilfs_aops;
  400. } else {
  401. inode->i_op = &nilfs_special_inode_operations;
  402. init_special_inode(
  403. inode, inode->i_mode,
  404. huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
  405. }
  406. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  407. brelse(bh);
  408. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  409. nilfs_set_inode_flags(inode);
  410. return 0;
  411. failed_unmap:
  412. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  413. brelse(bh);
  414. bad_inode:
  415. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  416. return err;
  417. }
  418. struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
  419. {
  420. struct inode *inode;
  421. int err;
  422. inode = iget_locked(sb, ino);
  423. if (unlikely(!inode))
  424. return ERR_PTR(-ENOMEM);
  425. if (!(inode->i_state & I_NEW))
  426. return inode;
  427. err = __nilfs_read_inode(sb, ino, inode);
  428. if (unlikely(err)) {
  429. iget_failed(inode);
  430. return ERR_PTR(err);
  431. }
  432. unlock_new_inode(inode);
  433. return inode;
  434. }
  435. void nilfs_write_inode_common(struct inode *inode,
  436. struct nilfs_inode *raw_inode, int has_bmap)
  437. {
  438. struct nilfs_inode_info *ii = NILFS_I(inode);
  439. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  440. raw_inode->i_uid = cpu_to_le32(inode->i_uid);
  441. raw_inode->i_gid = cpu_to_le32(inode->i_gid);
  442. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  443. raw_inode->i_size = cpu_to_le64(inode->i_size);
  444. raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  445. raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  446. raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  447. raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  448. raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
  449. raw_inode->i_flags = cpu_to_le32(ii->i_flags);
  450. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  451. if (has_bmap)
  452. nilfs_bmap_write(ii->i_bmap, raw_inode);
  453. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  454. raw_inode->i_device_code =
  455. cpu_to_le64(huge_encode_dev(inode->i_rdev));
  456. /* When extending inode, nilfs->ns_inode_size should be checked
  457. for substitutions of appended fields */
  458. }
  459. void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
  460. {
  461. ino_t ino = inode->i_ino;
  462. struct nilfs_inode_info *ii = NILFS_I(inode);
  463. struct super_block *sb = inode->i_sb;
  464. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  465. struct nilfs_inode *raw_inode;
  466. raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
  467. if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
  468. memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
  469. set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
  470. nilfs_write_inode_common(inode, raw_inode, 0);
  471. /* XXX: call with has_bmap = 0 is a workaround to avoid
  472. deadlock of bmap. This delays update of i_bmap to just
  473. before writing */
  474. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
  475. }
  476. #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
  477. static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
  478. unsigned long from)
  479. {
  480. unsigned long b;
  481. int ret;
  482. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  483. return;
  484. repeat:
  485. ret = nilfs_bmap_last_key(ii->i_bmap, &b);
  486. if (ret == -ENOENT)
  487. return;
  488. else if (ret < 0)
  489. goto failed;
  490. if (b < from)
  491. return;
  492. b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
  493. ret = nilfs_bmap_truncate(ii->i_bmap, b);
  494. nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
  495. if (!ret || (ret == -ENOMEM &&
  496. nilfs_bmap_truncate(ii->i_bmap, b) == 0))
  497. goto repeat;
  498. failed:
  499. if (ret == -EINVAL)
  500. nilfs_error(ii->vfs_inode.i_sb, __func__,
  501. "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
  502. else
  503. nilfs_warning(ii->vfs_inode.i_sb, __func__,
  504. "failed to truncate bmap (ino=%lu, err=%d)",
  505. ii->vfs_inode.i_ino, ret);
  506. }
  507. void nilfs_truncate(struct inode *inode)
  508. {
  509. unsigned long blkoff;
  510. unsigned int blocksize;
  511. struct nilfs_transaction_info ti;
  512. struct super_block *sb = inode->i_sb;
  513. struct nilfs_inode_info *ii = NILFS_I(inode);
  514. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  515. return;
  516. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  517. return;
  518. blocksize = sb->s_blocksize;
  519. blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
  520. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  521. block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
  522. nilfs_truncate_bmap(ii, blkoff);
  523. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  524. if (IS_SYNC(inode))
  525. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  526. nilfs_mark_inode_dirty(inode);
  527. nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
  528. nilfs_transaction_commit(sb);
  529. /* May construct a logical segment and may fail in sync mode.
  530. But truncate has no return value. */
  531. }
  532. static void nilfs_clear_inode(struct inode *inode)
  533. {
  534. struct nilfs_inode_info *ii = NILFS_I(inode);
  535. /*
  536. * Free resources allocated in nilfs_read_inode(), here.
  537. */
  538. BUG_ON(!list_empty(&ii->i_dirty));
  539. brelse(ii->i_bh);
  540. ii->i_bh = NULL;
  541. if (test_bit(NILFS_I_BMAP, &ii->i_state))
  542. nilfs_bmap_clear(ii->i_bmap);
  543. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  544. }
  545. void nilfs_evict_inode(struct inode *inode)
  546. {
  547. struct nilfs_transaction_info ti;
  548. struct super_block *sb = inode->i_sb;
  549. struct nilfs_inode_info *ii = NILFS_I(inode);
  550. if (inode->i_nlink || unlikely(is_bad_inode(inode))) {
  551. if (inode->i_data.nrpages)
  552. truncate_inode_pages(&inode->i_data, 0);
  553. end_writeback(inode);
  554. nilfs_clear_inode(inode);
  555. return;
  556. }
  557. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  558. if (inode->i_data.nrpages)
  559. truncate_inode_pages(&inode->i_data, 0);
  560. nilfs_truncate_bmap(ii, 0);
  561. nilfs_mark_inode_dirty(inode);
  562. end_writeback(inode);
  563. nilfs_clear_inode(inode);
  564. nilfs_free_inode(inode);
  565. /* nilfs_free_inode() marks inode buffer dirty */
  566. if (IS_SYNC(inode))
  567. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  568. nilfs_transaction_commit(sb);
  569. /* May construct a logical segment and may fail in sync mode.
  570. But delete_inode has no return value. */
  571. }
  572. int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
  573. {
  574. struct nilfs_transaction_info ti;
  575. struct inode *inode = dentry->d_inode;
  576. struct super_block *sb = inode->i_sb;
  577. int err;
  578. err = inode_change_ok(inode, iattr);
  579. if (err)
  580. return err;
  581. err = nilfs_transaction_begin(sb, &ti, 0);
  582. if (unlikely(err))
  583. return err;
  584. if ((iattr->ia_valid & ATTR_SIZE) &&
  585. iattr->ia_size != i_size_read(inode)) {
  586. err = vmtruncate(inode, iattr->ia_size);
  587. if (unlikely(err))
  588. goto out_err;
  589. }
  590. setattr_copy(inode, iattr);
  591. mark_inode_dirty(inode);
  592. if (iattr->ia_valid & ATTR_MODE) {
  593. err = nilfs_acl_chmod(inode);
  594. if (unlikely(err))
  595. goto out_err;
  596. }
  597. return nilfs_transaction_commit(sb);
  598. out_err:
  599. nilfs_transaction_abort(sb);
  600. return err;
  601. }
  602. int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
  603. struct buffer_head **pbh)
  604. {
  605. struct nilfs_inode_info *ii = NILFS_I(inode);
  606. int err;
  607. spin_lock(&sbi->s_inode_lock);
  608. if (ii->i_bh == NULL) {
  609. spin_unlock(&sbi->s_inode_lock);
  610. err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
  611. pbh);
  612. if (unlikely(err))
  613. return err;
  614. spin_lock(&sbi->s_inode_lock);
  615. if (ii->i_bh == NULL)
  616. ii->i_bh = *pbh;
  617. else {
  618. brelse(*pbh);
  619. *pbh = ii->i_bh;
  620. }
  621. } else
  622. *pbh = ii->i_bh;
  623. get_bh(*pbh);
  624. spin_unlock(&sbi->s_inode_lock);
  625. return 0;
  626. }
  627. int nilfs_inode_dirty(struct inode *inode)
  628. {
  629. struct nilfs_inode_info *ii = NILFS_I(inode);
  630. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  631. int ret = 0;
  632. if (!list_empty(&ii->i_dirty)) {
  633. spin_lock(&sbi->s_inode_lock);
  634. ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
  635. test_bit(NILFS_I_BUSY, &ii->i_state);
  636. spin_unlock(&sbi->s_inode_lock);
  637. }
  638. return ret;
  639. }
  640. int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
  641. unsigned nr_dirty)
  642. {
  643. struct nilfs_inode_info *ii = NILFS_I(inode);
  644. atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
  645. if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
  646. return 0;
  647. spin_lock(&sbi->s_inode_lock);
  648. if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
  649. !test_bit(NILFS_I_BUSY, &ii->i_state)) {
  650. /* Because this routine may race with nilfs_dispose_list(),
  651. we have to check NILFS_I_QUEUED here, too. */
  652. if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
  653. /* This will happen when somebody is freeing
  654. this inode. */
  655. nilfs_warning(sbi->s_super, __func__,
  656. "cannot get inode (ino=%lu)\n",
  657. inode->i_ino);
  658. spin_unlock(&sbi->s_inode_lock);
  659. return -EINVAL; /* NILFS_I_DIRTY may remain for
  660. freeing inode */
  661. }
  662. list_del(&ii->i_dirty);
  663. list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
  664. set_bit(NILFS_I_QUEUED, &ii->i_state);
  665. }
  666. spin_unlock(&sbi->s_inode_lock);
  667. return 0;
  668. }
  669. int nilfs_mark_inode_dirty(struct inode *inode)
  670. {
  671. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  672. struct buffer_head *ibh;
  673. int err;
  674. err = nilfs_load_inode_block(sbi, inode, &ibh);
  675. if (unlikely(err)) {
  676. nilfs_warning(inode->i_sb, __func__,
  677. "failed to reget inode block.\n");
  678. return err;
  679. }
  680. nilfs_update_inode(inode, ibh);
  681. nilfs_mdt_mark_buffer_dirty(ibh);
  682. nilfs_mdt_mark_dirty(sbi->s_ifile);
  683. brelse(ibh);
  684. return 0;
  685. }
  686. /**
  687. * nilfs_dirty_inode - reflect changes on given inode to an inode block.
  688. * @inode: inode of the file to be registered.
  689. *
  690. * nilfs_dirty_inode() loads a inode block containing the specified
  691. * @inode and copies data from a nilfs_inode to a corresponding inode
  692. * entry in the inode block. This operation is excluded from the segment
  693. * construction. This function can be called both as a single operation
  694. * and as a part of indivisible file operations.
  695. */
  696. void nilfs_dirty_inode(struct inode *inode)
  697. {
  698. struct nilfs_transaction_info ti;
  699. if (is_bad_inode(inode)) {
  700. nilfs_warning(inode->i_sb, __func__,
  701. "tried to mark bad_inode dirty. ignored.\n");
  702. dump_stack();
  703. return;
  704. }
  705. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  706. nilfs_mark_inode_dirty(inode);
  707. nilfs_transaction_commit(inode->i_sb); /* never fails */
  708. }