inode.c 23 KB

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