inode.c 23 KB

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