inode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. /* data block was not found */
  61. if (ret == -ENOENT && create) {
  62. struct nilfs_transaction_info ti;
  63. bh_result->b_blocknr = 0;
  64. err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
  65. if (unlikely(err))
  66. goto out;
  67. err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
  68. (unsigned long)bh_result);
  69. if (unlikely(err != 0)) {
  70. if (err == -EEXIST) {
  71. /*
  72. * The get_block() function could be called
  73. * from multiple callers for an inode.
  74. * However, the page having this block must
  75. * be locked in this case.
  76. */
  77. printk(KERN_WARNING
  78. "nilfs_get_block: a race condition "
  79. "while inserting a data block. "
  80. "(inode number=%lu, file block "
  81. "offset=%llu)\n",
  82. inode->i_ino,
  83. (unsigned long long)blkoff);
  84. err = 0;
  85. } else if (err == -EINVAL) {
  86. nilfs_error(inode->i_sb, __func__,
  87. "broken bmap (inode=%lu)\n",
  88. inode->i_ino);
  89. err = -EIO;
  90. }
  91. nilfs_transaction_abort(inode->i_sb);
  92. goto out;
  93. }
  94. nilfs_transaction_commit(inode->i_sb); /* never fails */
  95. /* Error handling should be detailed */
  96. set_buffer_new(bh_result);
  97. map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
  98. to proper value */
  99. } else if (ret == -ENOENT) {
  100. /* not found is not error (e.g. hole); must return without
  101. the mapped state flag. */
  102. ;
  103. } else {
  104. err = ret;
  105. }
  106. out:
  107. return err;
  108. }
  109. /**
  110. * nilfs_readpage() - implement readpage() method of nilfs_aops {}
  111. * address_space_operations.
  112. * @file - file struct of the file to be read
  113. * @page - the page to be read
  114. */
  115. static int nilfs_readpage(struct file *file, struct page *page)
  116. {
  117. return mpage_readpage(page, nilfs_get_block);
  118. }
  119. /**
  120. * nilfs_readpages() - implement readpages() method of nilfs_aops {}
  121. * address_space_operations.
  122. * @file - file struct of the file to be read
  123. * @mapping - address_space struct used for reading multiple pages
  124. * @pages - the pages to be read
  125. * @nr_pages - number of pages to be read
  126. */
  127. static int nilfs_readpages(struct file *file, struct address_space *mapping,
  128. struct list_head *pages, unsigned nr_pages)
  129. {
  130. return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
  131. }
  132. static int nilfs_writepages(struct address_space *mapping,
  133. struct writeback_control *wbc)
  134. {
  135. struct inode *inode = mapping->host;
  136. int err = 0;
  137. if (wbc->sync_mode == WB_SYNC_ALL)
  138. err = nilfs_construct_dsync_segment(inode->i_sb, inode,
  139. wbc->range_start,
  140. wbc->range_end);
  141. return err;
  142. }
  143. static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
  144. {
  145. struct inode *inode = page->mapping->host;
  146. int err;
  147. redirty_page_for_writepage(wbc, page);
  148. unlock_page(page);
  149. if (wbc->sync_mode == WB_SYNC_ALL) {
  150. err = nilfs_construct_segment(inode->i_sb);
  151. if (unlikely(err))
  152. return err;
  153. } else if (wbc->for_reclaim)
  154. nilfs_flush_segment(inode->i_sb, inode->i_ino);
  155. return 0;
  156. }
  157. static int nilfs_set_page_dirty(struct page *page)
  158. {
  159. int ret = __set_page_dirty_buffers(page);
  160. if (ret) {
  161. struct inode *inode = page->mapping->host;
  162. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  163. unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
  164. nilfs_set_file_dirty(sbi, inode, nr_dirty);
  165. }
  166. return ret;
  167. }
  168. static int nilfs_write_begin(struct file *file, struct address_space *mapping,
  169. loff_t pos, unsigned len, unsigned flags,
  170. struct page **pagep, void **fsdata)
  171. {
  172. struct inode *inode = mapping->host;
  173. int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
  174. if (unlikely(err))
  175. return err;
  176. *pagep = NULL;
  177. err = block_write_begin(file, mapping, pos, len, flags, pagep,
  178. fsdata, nilfs_get_block);
  179. if (unlikely(err))
  180. nilfs_transaction_abort(inode->i_sb);
  181. return err;
  182. }
  183. static int nilfs_write_end(struct file *file, struct address_space *mapping,
  184. loff_t pos, unsigned len, unsigned copied,
  185. struct page *page, void *fsdata)
  186. {
  187. struct inode *inode = mapping->host;
  188. unsigned start = pos & (PAGE_CACHE_SIZE - 1);
  189. unsigned nr_dirty;
  190. int err;
  191. nr_dirty = nilfs_page_count_clean_buffers(page, start,
  192. start + copied);
  193. copied = generic_write_end(file, mapping, pos, len, copied, page,
  194. fsdata);
  195. nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty);
  196. err = nilfs_transaction_commit(inode->i_sb);
  197. return err ? : copied;
  198. }
  199. static ssize_t
  200. nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  201. loff_t offset, unsigned long nr_segs)
  202. {
  203. struct file *file = iocb->ki_filp;
  204. struct inode *inode = file->f_mapping->host;
  205. ssize_t size;
  206. if (rw == WRITE)
  207. return 0;
  208. /* Needs synchronization with the cleaner */
  209. size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  210. offset, nr_segs, nilfs_get_block, NULL);
  211. return size;
  212. }
  213. struct address_space_operations nilfs_aops = {
  214. .writepage = nilfs_writepage,
  215. .readpage = nilfs_readpage,
  216. /* .sync_page = nilfs_sync_page, */
  217. .writepages = nilfs_writepages,
  218. .set_page_dirty = nilfs_set_page_dirty,
  219. .readpages = nilfs_readpages,
  220. .write_begin = nilfs_write_begin,
  221. .write_end = nilfs_write_end,
  222. /* .releasepage = nilfs_releasepage, */
  223. .invalidatepage = block_invalidatepage,
  224. .direct_IO = nilfs_direct_IO,
  225. };
  226. struct inode *nilfs_new_inode(struct inode *dir, int mode)
  227. {
  228. struct super_block *sb = dir->i_sb;
  229. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  230. struct inode *inode;
  231. struct nilfs_inode_info *ii;
  232. int err = -ENOMEM;
  233. ino_t ino;
  234. inode = new_inode(sb);
  235. if (unlikely(!inode))
  236. goto failed;
  237. mapping_set_gfp_mask(inode->i_mapping,
  238. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  239. ii = NILFS_I(inode);
  240. ii->i_state = 1 << NILFS_I_NEW;
  241. err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
  242. if (unlikely(err))
  243. goto failed_ifile_create_inode;
  244. /* reference count of i_bh inherits from nilfs_mdt_read_block() */
  245. atomic_inc(&sbi->s_inodes_count);
  246. inode->i_uid = current_fsuid();
  247. if (dir->i_mode & S_ISGID) {
  248. inode->i_gid = dir->i_gid;
  249. if (S_ISDIR(mode))
  250. mode |= S_ISGID;
  251. } else
  252. inode->i_gid = current_fsgid();
  253. inode->i_mode = mode;
  254. inode->i_ino = ino;
  255. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  256. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  257. err = nilfs_bmap_read(ii->i_bmap, NULL);
  258. if (err < 0)
  259. goto failed_bmap;
  260. set_bit(NILFS_I_BMAP, &ii->i_state);
  261. /* No lock is needed; iget() ensures it. */
  262. }
  263. ii->i_flags = NILFS_I(dir)->i_flags;
  264. if (S_ISLNK(mode))
  265. ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
  266. if (!S_ISDIR(mode))
  267. ii->i_flags &= ~NILFS_DIRSYNC_FL;
  268. /* ii->i_file_acl = 0; */
  269. /* ii->i_dir_acl = 0; */
  270. ii->i_dtime = 0;
  271. ii->i_dir_start_lookup = 0;
  272. #ifdef CONFIG_NILFS_FS_POSIX_ACL
  273. ii->i_acl = NULL;
  274. ii->i_default_acl = NULL;
  275. #endif
  276. ii->i_cno = 0;
  277. nilfs_set_inode_flags(inode);
  278. spin_lock(&sbi->s_next_gen_lock);
  279. inode->i_generation = sbi->s_next_generation++;
  280. spin_unlock(&sbi->s_next_gen_lock);
  281. insert_inode_hash(inode);
  282. err = nilfs_init_acl(inode, dir);
  283. if (unlikely(err))
  284. goto failed_acl; /* never occur. When supporting
  285. nilfs_init_acl(), proper cancellation of
  286. above jobs should be considered */
  287. mark_inode_dirty(inode);
  288. return inode;
  289. failed_acl:
  290. failed_bmap:
  291. inode->i_nlink = 0;
  292. iput(inode); /* raw_inode will be deleted through
  293. generic_delete_inode() */
  294. goto failed;
  295. failed_ifile_create_inode:
  296. make_bad_inode(inode);
  297. iput(inode); /* if i_nlink == 1, generic_forget_inode() will be
  298. called */
  299. failed:
  300. return ERR_PTR(err);
  301. }
  302. void nilfs_free_inode(struct inode *inode)
  303. {
  304. struct super_block *sb = inode->i_sb;
  305. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  306. clear_inode(inode);
  307. /* XXX: check error code? Is there any thing I can do? */
  308. (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
  309. atomic_dec(&sbi->s_inodes_count);
  310. }
  311. void nilfs_set_inode_flags(struct inode *inode)
  312. {
  313. unsigned int flags = NILFS_I(inode)->i_flags;
  314. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
  315. S_DIRSYNC);
  316. if (flags & NILFS_SYNC_FL)
  317. inode->i_flags |= S_SYNC;
  318. if (flags & NILFS_APPEND_FL)
  319. inode->i_flags |= S_APPEND;
  320. if (flags & NILFS_IMMUTABLE_FL)
  321. inode->i_flags |= S_IMMUTABLE;
  322. #ifndef NILFS_ATIME_DISABLE
  323. if (flags & NILFS_NOATIME_FL)
  324. #endif
  325. inode->i_flags |= S_NOATIME;
  326. if (flags & NILFS_DIRSYNC_FL)
  327. inode->i_flags |= S_DIRSYNC;
  328. mapping_set_gfp_mask(inode->i_mapping,
  329. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  330. }
  331. int nilfs_read_inode_common(struct inode *inode,
  332. struct nilfs_inode *raw_inode)
  333. {
  334. struct nilfs_inode_info *ii = NILFS_I(inode);
  335. int err;
  336. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  337. inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
  338. inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
  339. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  340. inode->i_size = le64_to_cpu(raw_inode->i_size);
  341. inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  342. inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
  343. inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  344. inode->i_atime.tv_nsec = 0;
  345. inode->i_ctime.tv_nsec = 0;
  346. inode->i_mtime.tv_nsec = 0;
  347. ii->i_dtime = le64_to_cpu(raw_inode->i_dtime);
  348. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ii->i_dtime))
  349. return -EINVAL; /* this inode is deleted */
  350. inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
  351. ii->i_flags = le32_to_cpu(raw_inode->i_flags);
  352. #if 0
  353. ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  354. ii->i_dir_acl = S_ISREG(inode->i_mode) ?
  355. 0 : le32_to_cpu(raw_inode->i_dir_acl);
  356. #endif
  357. ii->i_cno = 0;
  358. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  359. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  360. S_ISLNK(inode->i_mode)) {
  361. err = nilfs_bmap_read(ii->i_bmap, raw_inode);
  362. if (err < 0)
  363. return err;
  364. set_bit(NILFS_I_BMAP, &ii->i_state);
  365. /* No lock is needed; iget() ensures it. */
  366. }
  367. return 0;
  368. }
  369. static int nilfs_read_sketch_inode(struct inode *inode)
  370. {
  371. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  372. int err = 0;
  373. if (sbi->s_snapshot_cno) {
  374. struct the_nilfs *nilfs = sbi->s_nilfs;
  375. struct buffer_head *bh_cp;
  376. struct nilfs_checkpoint *raw_cp;
  377. err = nilfs_cpfile_get_checkpoint(
  378. nilfs->ns_cpfile, sbi->s_snapshot_cno, 0, &raw_cp,
  379. &bh_cp);
  380. if (likely(!err)) {
  381. if (!nilfs_checkpoint_sketch(raw_cp))
  382. inode->i_size = 0;
  383. nilfs_cpfile_put_checkpoint(
  384. nilfs->ns_cpfile, sbi->s_snapshot_cno, bh_cp);
  385. }
  386. inode->i_flags |= S_NOCMTIME;
  387. }
  388. return err;
  389. }
  390. static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
  391. struct inode *inode)
  392. {
  393. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  394. struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
  395. struct buffer_head *bh;
  396. struct nilfs_inode *raw_inode;
  397. int err;
  398. down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  399. err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
  400. if (unlikely(err))
  401. goto bad_inode;
  402. raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
  403. #ifdef CONFIG_NILFS_FS_POSIX_ACL
  404. ii->i_acl = NILFS_ACL_NOT_CACHED;
  405. ii->i_default_acl = NILFS_ACL_NOT_CACHED;
  406. #endif
  407. if (nilfs_read_inode_common(inode, raw_inode))
  408. goto failed_unmap;
  409. if (S_ISREG(inode->i_mode)) {
  410. inode->i_op = &nilfs_file_inode_operations;
  411. inode->i_fop = &nilfs_file_operations;
  412. inode->i_mapping->a_ops = &nilfs_aops;
  413. if (unlikely(inode->i_ino == NILFS_SKETCH_INO)) {
  414. err = nilfs_read_sketch_inode(inode);
  415. if (unlikely(err))
  416. goto failed_unmap;
  417. }
  418. } else if (S_ISDIR(inode->i_mode)) {
  419. inode->i_op = &nilfs_dir_inode_operations;
  420. inode->i_fop = &nilfs_dir_operations;
  421. inode->i_mapping->a_ops = &nilfs_aops;
  422. } else if (S_ISLNK(inode->i_mode)) {
  423. inode->i_op = &nilfs_symlink_inode_operations;
  424. inode->i_mapping->a_ops = &nilfs_aops;
  425. } else {
  426. inode->i_op = &nilfs_special_inode_operations;
  427. init_special_inode(
  428. inode, inode->i_mode,
  429. new_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
  430. }
  431. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  432. brelse(bh);
  433. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  434. nilfs_set_inode_flags(inode);
  435. return 0;
  436. failed_unmap:
  437. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  438. brelse(bh);
  439. bad_inode:
  440. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  441. return err;
  442. }
  443. struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
  444. {
  445. struct inode *inode;
  446. int err;
  447. inode = iget_locked(sb, ino);
  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_blocks = cpu_to_le64(inode->i_blocks);
  472. raw_inode->i_dtime = cpu_to_le64(ii->i_dtime);
  473. raw_inode->i_flags = cpu_to_le32(ii->i_flags);
  474. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  475. if (has_bmap)
  476. nilfs_bmap_write(ii->i_bmap, raw_inode);
  477. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  478. raw_inode->i_device_code =
  479. cpu_to_le64(new_encode_dev(inode->i_rdev));
  480. /* When extending inode, nilfs->ns_inode_size should be checked
  481. for substitutions of appended fields */
  482. }
  483. void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
  484. {
  485. ino_t ino = inode->i_ino;
  486. struct nilfs_inode_info *ii = NILFS_I(inode);
  487. struct super_block *sb = inode->i_sb;
  488. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  489. struct nilfs_inode *raw_inode;
  490. raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
  491. /* The buffer is guarded with lock_buffer() by the caller */
  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_set_file_dirty(NILFS_SB(sb), inode, 0);
  552. nilfs_transaction_commit(sb);
  553. /* May construct a logical segment and may fail in sync mode.
  554. But truncate has no return value. */
  555. }
  556. void nilfs_delete_inode(struct inode *inode)
  557. {
  558. struct nilfs_transaction_info ti;
  559. struct super_block *sb = inode->i_sb;
  560. struct nilfs_inode_info *ii = NILFS_I(inode);
  561. if (unlikely(is_bad_inode(inode))) {
  562. if (inode->i_data.nrpages)
  563. truncate_inode_pages(&inode->i_data, 0);
  564. clear_inode(inode);
  565. return;
  566. }
  567. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  568. if (inode->i_data.nrpages)
  569. truncate_inode_pages(&inode->i_data, 0);
  570. nilfs_truncate_bmap(ii, 0);
  571. nilfs_free_inode(inode);
  572. /* nilfs_free_inode() marks inode buffer dirty */
  573. if (IS_SYNC(inode))
  574. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  575. nilfs_transaction_commit(sb);
  576. /* May construct a logical segment and may fail in sync mode.
  577. But delete_inode has no return value. */
  578. }
  579. int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
  580. {
  581. struct nilfs_transaction_info ti;
  582. struct inode *inode = dentry->d_inode;
  583. struct super_block *sb = inode->i_sb;
  584. int err;
  585. err = inode_change_ok(inode, iattr);
  586. if (err)
  587. return err;
  588. err = nilfs_transaction_begin(sb, &ti, 0);
  589. if (unlikely(err))
  590. return err;
  591. err = inode_setattr(inode, iattr);
  592. if (!err && (iattr->ia_valid & ATTR_MODE))
  593. err = nilfs_acl_chmod(inode);
  594. if (likely(!err))
  595. err = nilfs_transaction_commit(sb);
  596. else
  597. nilfs_transaction_abort(sb);
  598. return err;
  599. }
  600. int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
  601. struct buffer_head **pbh)
  602. {
  603. struct nilfs_inode_info *ii = NILFS_I(inode);
  604. int err;
  605. spin_lock(&sbi->s_inode_lock);
  606. /* Caller of this function MUST lock s_inode_lock */
  607. if (ii->i_bh == NULL) {
  608. spin_unlock(&sbi->s_inode_lock);
  609. err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
  610. pbh);
  611. if (unlikely(err))
  612. return err;
  613. spin_lock(&sbi->s_inode_lock);
  614. if (ii->i_bh == NULL)
  615. ii->i_bh = *pbh;
  616. else {
  617. brelse(*pbh);
  618. *pbh = ii->i_bh;
  619. }
  620. } else
  621. *pbh = ii->i_bh;
  622. get_bh(*pbh);
  623. spin_unlock(&sbi->s_inode_lock);
  624. return 0;
  625. }
  626. int nilfs_inode_dirty(struct inode *inode)
  627. {
  628. struct nilfs_inode_info *ii = NILFS_I(inode);
  629. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  630. int ret = 0;
  631. if (!list_empty(&ii->i_dirty)) {
  632. spin_lock(&sbi->s_inode_lock);
  633. ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
  634. test_bit(NILFS_I_BUSY, &ii->i_state);
  635. spin_unlock(&sbi->s_inode_lock);
  636. }
  637. return ret;
  638. }
  639. int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
  640. unsigned nr_dirty)
  641. {
  642. struct nilfs_inode_info *ii = NILFS_I(inode);
  643. atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
  644. if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state) ||
  645. unlikely(inode->i_ino == NILFS_SKETCH_INO))
  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. lock_buffer(ibh);
  681. nilfs_update_inode(inode, ibh);
  682. unlock_buffer(ibh);
  683. nilfs_mdt_mark_buffer_dirty(ibh);
  684. nilfs_mdt_mark_dirty(sbi->s_ifile);
  685. brelse(ibh);
  686. return 0;
  687. }
  688. /**
  689. * nilfs_dirty_inode - reflect changes on given inode to an inode block.
  690. * @inode: inode of the file to be registered.
  691. *
  692. * nilfs_dirty_inode() loads a inode block containing the specified
  693. * @inode and copies data from a nilfs_inode to a corresponding inode
  694. * entry in the inode block. This operation is excluded from the segment
  695. * construction. This function can be called both as a single operation
  696. * and as a part of indivisible file operations.
  697. */
  698. void nilfs_dirty_inode(struct inode *inode)
  699. {
  700. struct nilfs_transaction_info ti;
  701. if (is_bad_inode(inode)) {
  702. nilfs_warning(inode->i_sb, __func__,
  703. "tried to mark bad_inode dirty. ignored.\n");
  704. dump_stack();
  705. return;
  706. }
  707. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  708. if (likely(inode->i_ino != NILFS_SKETCH_INO))
  709. nilfs_mark_inode_dirty(inode);
  710. nilfs_transaction_commit(inode->i_sb); /* never fails */
  711. }