inode.c 22 KB

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