inode.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. .is_partially_uptodate = block_is_partially_uptodate,
  226. };
  227. struct inode *nilfs_new_inode(struct inode *dir, int mode)
  228. {
  229. struct super_block *sb = dir->i_sb;
  230. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  231. struct inode *inode;
  232. struct nilfs_inode_info *ii;
  233. int err = -ENOMEM;
  234. ino_t ino;
  235. inode = new_inode(sb);
  236. if (unlikely(!inode))
  237. goto failed;
  238. mapping_set_gfp_mask(inode->i_mapping,
  239. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  240. ii = NILFS_I(inode);
  241. ii->i_state = 1 << NILFS_I_NEW;
  242. err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
  243. if (unlikely(err))
  244. goto failed_ifile_create_inode;
  245. /* reference count of i_bh inherits from nilfs_mdt_read_block() */
  246. atomic_inc(&sbi->s_inodes_count);
  247. inode->i_uid = current_fsuid();
  248. if (dir->i_mode & S_ISGID) {
  249. inode->i_gid = dir->i_gid;
  250. if (S_ISDIR(mode))
  251. mode |= S_ISGID;
  252. } else
  253. inode->i_gid = current_fsgid();
  254. inode->i_mode = mode;
  255. inode->i_ino = ino;
  256. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  257. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  258. err = nilfs_bmap_read(ii->i_bmap, NULL);
  259. if (err < 0)
  260. goto failed_bmap;
  261. set_bit(NILFS_I_BMAP, &ii->i_state);
  262. /* No lock is needed; iget() ensures it. */
  263. }
  264. ii->i_flags = NILFS_I(dir)->i_flags;
  265. if (S_ISLNK(mode))
  266. ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
  267. if (!S_ISDIR(mode))
  268. ii->i_flags &= ~NILFS_DIRSYNC_FL;
  269. /* ii->i_file_acl = 0; */
  270. /* ii->i_dir_acl = 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 = le32_to_cpu(raw_inode->i_mtime_nsec);
  345. inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
  346. inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  347. if (inode->i_nlink == 0 && inode->i_mode == 0)
  348. return -EINVAL; /* this inode is deleted */
  349. inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
  350. ii->i_flags = le32_to_cpu(raw_inode->i_flags);
  351. #if 0
  352. ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  353. ii->i_dir_acl = S_ISREG(inode->i_mode) ?
  354. 0 : le32_to_cpu(raw_inode->i_dir_acl);
  355. #endif
  356. ii->i_cno = 0;
  357. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  358. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  359. S_ISLNK(inode->i_mode)) {
  360. err = nilfs_bmap_read(ii->i_bmap, raw_inode);
  361. if (err < 0)
  362. return err;
  363. set_bit(NILFS_I_BMAP, &ii->i_state);
  364. /* No lock is needed; iget() ensures it. */
  365. }
  366. return 0;
  367. }
  368. static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
  369. struct inode *inode)
  370. {
  371. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  372. struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
  373. struct buffer_head *bh;
  374. struct nilfs_inode *raw_inode;
  375. int err;
  376. down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  377. err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
  378. if (unlikely(err))
  379. goto bad_inode;
  380. raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
  381. #ifdef CONFIG_NILFS_FS_POSIX_ACL
  382. ii->i_acl = NILFS_ACL_NOT_CACHED;
  383. ii->i_default_acl = NILFS_ACL_NOT_CACHED;
  384. #endif
  385. if (nilfs_read_inode_common(inode, raw_inode))
  386. goto failed_unmap;
  387. if (S_ISREG(inode->i_mode)) {
  388. inode->i_op = &nilfs_file_inode_operations;
  389. inode->i_fop = &nilfs_file_operations;
  390. inode->i_mapping->a_ops = &nilfs_aops;
  391. } else if (S_ISDIR(inode->i_mode)) {
  392. inode->i_op = &nilfs_dir_inode_operations;
  393. inode->i_fop = &nilfs_dir_operations;
  394. inode->i_mapping->a_ops = &nilfs_aops;
  395. } else if (S_ISLNK(inode->i_mode)) {
  396. inode->i_op = &nilfs_symlink_inode_operations;
  397. inode->i_mapping->a_ops = &nilfs_aops;
  398. } else {
  399. inode->i_op = &nilfs_special_inode_operations;
  400. init_special_inode(
  401. inode, inode->i_mode,
  402. new_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
  403. }
  404. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  405. brelse(bh);
  406. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  407. nilfs_set_inode_flags(inode);
  408. return 0;
  409. failed_unmap:
  410. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  411. brelse(bh);
  412. bad_inode:
  413. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  414. return err;
  415. }
  416. struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
  417. {
  418. struct inode *inode;
  419. int err;
  420. inode = iget_locked(sb, ino);
  421. if (unlikely(!inode))
  422. return ERR_PTR(-ENOMEM);
  423. if (!(inode->i_state & I_NEW))
  424. return inode;
  425. err = __nilfs_read_inode(sb, ino, inode);
  426. if (unlikely(err)) {
  427. iget_failed(inode);
  428. return ERR_PTR(err);
  429. }
  430. unlock_new_inode(inode);
  431. return inode;
  432. }
  433. void nilfs_write_inode_common(struct inode *inode,
  434. struct nilfs_inode *raw_inode, int has_bmap)
  435. {
  436. struct nilfs_inode_info *ii = NILFS_I(inode);
  437. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  438. raw_inode->i_uid = cpu_to_le32(inode->i_uid);
  439. raw_inode->i_gid = cpu_to_le32(inode->i_gid);
  440. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  441. raw_inode->i_size = cpu_to_le64(inode->i_size);
  442. raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  443. raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  444. raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  445. raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  446. raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
  447. raw_inode->i_flags = cpu_to_le32(ii->i_flags);
  448. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  449. if (has_bmap)
  450. nilfs_bmap_write(ii->i_bmap, raw_inode);
  451. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  452. raw_inode->i_device_code =
  453. cpu_to_le64(new_encode_dev(inode->i_rdev));
  454. /* When extending inode, nilfs->ns_inode_size should be checked
  455. for substitutions of appended fields */
  456. }
  457. void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
  458. {
  459. ino_t ino = inode->i_ino;
  460. struct nilfs_inode_info *ii = NILFS_I(inode);
  461. struct super_block *sb = inode->i_sb;
  462. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  463. struct nilfs_inode *raw_inode;
  464. raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
  465. /* The buffer is guarded with lock_buffer() by the caller */
  466. if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
  467. memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
  468. set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
  469. nilfs_write_inode_common(inode, raw_inode, 0);
  470. /* XXX: call with has_bmap = 0 is a workaround to avoid
  471. deadlock of bmap. This delays update of i_bmap to just
  472. before writing */
  473. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
  474. }
  475. #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
  476. static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
  477. unsigned long from)
  478. {
  479. unsigned long b;
  480. int ret;
  481. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  482. return;
  483. repeat:
  484. ret = nilfs_bmap_last_key(ii->i_bmap, &b);
  485. if (ret == -ENOENT)
  486. return;
  487. else if (ret < 0)
  488. goto failed;
  489. if (b < from)
  490. return;
  491. b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
  492. ret = nilfs_bmap_truncate(ii->i_bmap, b);
  493. nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
  494. if (!ret || (ret == -ENOMEM &&
  495. nilfs_bmap_truncate(ii->i_bmap, b) == 0))
  496. goto repeat;
  497. failed:
  498. if (ret == -EINVAL)
  499. nilfs_error(ii->vfs_inode.i_sb, __func__,
  500. "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
  501. else
  502. nilfs_warning(ii->vfs_inode.i_sb, __func__,
  503. "failed to truncate bmap (ino=%lu, err=%d)",
  504. ii->vfs_inode.i_ino, ret);
  505. }
  506. void nilfs_truncate(struct inode *inode)
  507. {
  508. unsigned long blkoff;
  509. unsigned int blocksize;
  510. struct nilfs_transaction_info ti;
  511. struct super_block *sb = inode->i_sb;
  512. struct nilfs_inode_info *ii = NILFS_I(inode);
  513. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  514. return;
  515. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  516. return;
  517. blocksize = sb->s_blocksize;
  518. blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
  519. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  520. block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
  521. nilfs_truncate_bmap(ii, blkoff);
  522. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  523. if (IS_SYNC(inode))
  524. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  525. nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
  526. nilfs_transaction_commit(sb);
  527. /* May construct a logical segment and may fail in sync mode.
  528. But truncate has no return value. */
  529. }
  530. void nilfs_delete_inode(struct inode *inode)
  531. {
  532. struct nilfs_transaction_info ti;
  533. struct super_block *sb = inode->i_sb;
  534. struct nilfs_inode_info *ii = NILFS_I(inode);
  535. if (unlikely(is_bad_inode(inode))) {
  536. if (inode->i_data.nrpages)
  537. truncate_inode_pages(&inode->i_data, 0);
  538. clear_inode(inode);
  539. return;
  540. }
  541. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  542. if (inode->i_data.nrpages)
  543. truncate_inode_pages(&inode->i_data, 0);
  544. nilfs_truncate_bmap(ii, 0);
  545. nilfs_free_inode(inode);
  546. /* nilfs_free_inode() marks inode buffer dirty */
  547. if (IS_SYNC(inode))
  548. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  549. nilfs_transaction_commit(sb);
  550. /* May construct a logical segment and may fail in sync mode.
  551. But delete_inode has no return value. */
  552. }
  553. int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
  554. {
  555. struct nilfs_transaction_info ti;
  556. struct inode *inode = dentry->d_inode;
  557. struct super_block *sb = inode->i_sb;
  558. int err;
  559. err = inode_change_ok(inode, iattr);
  560. if (err)
  561. return err;
  562. err = nilfs_transaction_begin(sb, &ti, 0);
  563. if (unlikely(err))
  564. return err;
  565. err = inode_setattr(inode, iattr);
  566. if (!err && (iattr->ia_valid & ATTR_MODE))
  567. err = nilfs_acl_chmod(inode);
  568. if (likely(!err))
  569. err = nilfs_transaction_commit(sb);
  570. else
  571. nilfs_transaction_abort(sb);
  572. return err;
  573. }
  574. int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
  575. struct buffer_head **pbh)
  576. {
  577. struct nilfs_inode_info *ii = NILFS_I(inode);
  578. int err;
  579. spin_lock(&sbi->s_inode_lock);
  580. /* Caller of this function MUST lock s_inode_lock */
  581. if (ii->i_bh == NULL) {
  582. spin_unlock(&sbi->s_inode_lock);
  583. err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
  584. pbh);
  585. if (unlikely(err))
  586. return err;
  587. spin_lock(&sbi->s_inode_lock);
  588. if (ii->i_bh == NULL)
  589. ii->i_bh = *pbh;
  590. else {
  591. brelse(*pbh);
  592. *pbh = ii->i_bh;
  593. }
  594. } else
  595. *pbh = ii->i_bh;
  596. get_bh(*pbh);
  597. spin_unlock(&sbi->s_inode_lock);
  598. return 0;
  599. }
  600. int nilfs_inode_dirty(struct inode *inode)
  601. {
  602. struct nilfs_inode_info *ii = NILFS_I(inode);
  603. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  604. int ret = 0;
  605. if (!list_empty(&ii->i_dirty)) {
  606. spin_lock(&sbi->s_inode_lock);
  607. ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
  608. test_bit(NILFS_I_BUSY, &ii->i_state);
  609. spin_unlock(&sbi->s_inode_lock);
  610. }
  611. return ret;
  612. }
  613. int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
  614. unsigned nr_dirty)
  615. {
  616. struct nilfs_inode_info *ii = NILFS_I(inode);
  617. atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
  618. if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
  619. return 0;
  620. spin_lock(&sbi->s_inode_lock);
  621. if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
  622. !test_bit(NILFS_I_BUSY, &ii->i_state)) {
  623. /* Because this routine may race with nilfs_dispose_list(),
  624. we have to check NILFS_I_QUEUED here, too. */
  625. if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
  626. /* This will happen when somebody is freeing
  627. this inode. */
  628. nilfs_warning(sbi->s_super, __func__,
  629. "cannot get inode (ino=%lu)\n",
  630. inode->i_ino);
  631. spin_unlock(&sbi->s_inode_lock);
  632. return -EINVAL; /* NILFS_I_DIRTY may remain for
  633. freeing inode */
  634. }
  635. list_del(&ii->i_dirty);
  636. list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
  637. set_bit(NILFS_I_QUEUED, &ii->i_state);
  638. }
  639. spin_unlock(&sbi->s_inode_lock);
  640. return 0;
  641. }
  642. int nilfs_mark_inode_dirty(struct inode *inode)
  643. {
  644. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  645. struct buffer_head *ibh;
  646. int err;
  647. err = nilfs_load_inode_block(sbi, inode, &ibh);
  648. if (unlikely(err)) {
  649. nilfs_warning(inode->i_sb, __func__,
  650. "failed to reget inode block.\n");
  651. return err;
  652. }
  653. lock_buffer(ibh);
  654. nilfs_update_inode(inode, ibh);
  655. unlock_buffer(ibh);
  656. nilfs_mdt_mark_buffer_dirty(ibh);
  657. nilfs_mdt_mark_dirty(sbi->s_ifile);
  658. brelse(ibh);
  659. return 0;
  660. }
  661. /**
  662. * nilfs_dirty_inode - reflect changes on given inode to an inode block.
  663. * @inode: inode of the file to be registered.
  664. *
  665. * nilfs_dirty_inode() loads a inode block containing the specified
  666. * @inode and copies data from a nilfs_inode to a corresponding inode
  667. * entry in the inode block. This operation is excluded from the segment
  668. * construction. This function can be called both as a single operation
  669. * and as a part of indivisible file operations.
  670. */
  671. void nilfs_dirty_inode(struct inode *inode)
  672. {
  673. struct nilfs_transaction_info ti;
  674. if (is_bad_inode(inode)) {
  675. nilfs_warning(inode->i_sb, __func__,
  676. "tried to mark bad_inode dirty. ignored.\n");
  677. dump_stack();
  678. return;
  679. }
  680. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  681. nilfs_mark_inode_dirty(inode);
  682. nilfs_transaction_commit(inode->i_sb); /* never fails */
  683. }