inode.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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_dir_start_lookup = 0;
  271. #ifdef CONFIG_NILFS_FS_POSIX_ACL
  272. ii->i_acl = NULL;
  273. ii->i_default_acl = NULL;
  274. #endif
  275. ii->i_cno = 0;
  276. nilfs_set_inode_flags(inode);
  277. spin_lock(&sbi->s_next_gen_lock);
  278. inode->i_generation = sbi->s_next_generation++;
  279. spin_unlock(&sbi->s_next_gen_lock);
  280. insert_inode_hash(inode);
  281. err = nilfs_init_acl(inode, dir);
  282. if (unlikely(err))
  283. goto failed_acl; /* never occur. When supporting
  284. nilfs_init_acl(), proper cancellation of
  285. above jobs should be considered */
  286. mark_inode_dirty(inode);
  287. return inode;
  288. failed_acl:
  289. failed_bmap:
  290. inode->i_nlink = 0;
  291. iput(inode); /* raw_inode will be deleted through
  292. generic_delete_inode() */
  293. goto failed;
  294. failed_ifile_create_inode:
  295. make_bad_inode(inode);
  296. iput(inode); /* if i_nlink == 1, generic_forget_inode() will be
  297. called */
  298. failed:
  299. return ERR_PTR(err);
  300. }
  301. void nilfs_free_inode(struct inode *inode)
  302. {
  303. struct super_block *sb = inode->i_sb;
  304. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  305. clear_inode(inode);
  306. /* XXX: check error code? Is there any thing I can do? */
  307. (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
  308. atomic_dec(&sbi->s_inodes_count);
  309. }
  310. void nilfs_set_inode_flags(struct inode *inode)
  311. {
  312. unsigned int flags = NILFS_I(inode)->i_flags;
  313. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
  314. S_DIRSYNC);
  315. if (flags & NILFS_SYNC_FL)
  316. inode->i_flags |= S_SYNC;
  317. if (flags & NILFS_APPEND_FL)
  318. inode->i_flags |= S_APPEND;
  319. if (flags & NILFS_IMMUTABLE_FL)
  320. inode->i_flags |= S_IMMUTABLE;
  321. #ifndef NILFS_ATIME_DISABLE
  322. if (flags & NILFS_NOATIME_FL)
  323. #endif
  324. inode->i_flags |= S_NOATIME;
  325. if (flags & NILFS_DIRSYNC_FL)
  326. inode->i_flags |= S_DIRSYNC;
  327. mapping_set_gfp_mask(inode->i_mapping,
  328. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  329. }
  330. int nilfs_read_inode_common(struct inode *inode,
  331. struct nilfs_inode *raw_inode)
  332. {
  333. struct nilfs_inode_info *ii = NILFS_I(inode);
  334. int err;
  335. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  336. inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
  337. inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
  338. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  339. inode->i_size = le64_to_cpu(raw_inode->i_size);
  340. inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  341. inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
  342. inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  343. inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  344. inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
  345. inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  346. if (inode->i_nlink == 0 && inode->i_mode == 0)
  347. return -EINVAL; /* this inode is deleted */
  348. inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
  349. ii->i_flags = le32_to_cpu(raw_inode->i_flags);
  350. #if 0
  351. ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  352. ii->i_dir_acl = S_ISREG(inode->i_mode) ?
  353. 0 : le32_to_cpu(raw_inode->i_dir_acl);
  354. #endif
  355. ii->i_cno = 0;
  356. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  357. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  358. S_ISLNK(inode->i_mode)) {
  359. err = nilfs_bmap_read(ii->i_bmap, raw_inode);
  360. if (err < 0)
  361. return err;
  362. set_bit(NILFS_I_BMAP, &ii->i_state);
  363. /* No lock is needed; iget() ensures it. */
  364. }
  365. return 0;
  366. }
  367. static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
  368. struct inode *inode)
  369. {
  370. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  371. struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
  372. struct buffer_head *bh;
  373. struct nilfs_inode *raw_inode;
  374. int err;
  375. down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  376. err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
  377. if (unlikely(err))
  378. goto bad_inode;
  379. raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
  380. #ifdef CONFIG_NILFS_FS_POSIX_ACL
  381. ii->i_acl = NILFS_ACL_NOT_CACHED;
  382. ii->i_default_acl = NILFS_ACL_NOT_CACHED;
  383. #endif
  384. if (nilfs_read_inode_common(inode, raw_inode))
  385. goto failed_unmap;
  386. if (S_ISREG(inode->i_mode)) {
  387. inode->i_op = &nilfs_file_inode_operations;
  388. inode->i_fop = &nilfs_file_operations;
  389. inode->i_mapping->a_ops = &nilfs_aops;
  390. } else if (S_ISDIR(inode->i_mode)) {
  391. inode->i_op = &nilfs_dir_inode_operations;
  392. inode->i_fop = &nilfs_dir_operations;
  393. inode->i_mapping->a_ops = &nilfs_aops;
  394. } else if (S_ISLNK(inode->i_mode)) {
  395. inode->i_op = &nilfs_symlink_inode_operations;
  396. inode->i_mapping->a_ops = &nilfs_aops;
  397. } else {
  398. inode->i_op = &nilfs_special_inode_operations;
  399. init_special_inode(
  400. inode, inode->i_mode,
  401. new_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
  402. }
  403. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  404. brelse(bh);
  405. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  406. nilfs_set_inode_flags(inode);
  407. return 0;
  408. failed_unmap:
  409. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
  410. brelse(bh);
  411. bad_inode:
  412. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  413. return err;
  414. }
  415. struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
  416. {
  417. struct inode *inode;
  418. int err;
  419. inode = iget_locked(sb, ino);
  420. if (unlikely(!inode))
  421. return ERR_PTR(-ENOMEM);
  422. if (!(inode->i_state & I_NEW))
  423. return inode;
  424. err = __nilfs_read_inode(sb, ino, inode);
  425. if (unlikely(err)) {
  426. iget_failed(inode);
  427. return ERR_PTR(err);
  428. }
  429. unlock_new_inode(inode);
  430. return inode;
  431. }
  432. void nilfs_write_inode_common(struct inode *inode,
  433. struct nilfs_inode *raw_inode, int has_bmap)
  434. {
  435. struct nilfs_inode_info *ii = NILFS_I(inode);
  436. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  437. raw_inode->i_uid = cpu_to_le32(inode->i_uid);
  438. raw_inode->i_gid = cpu_to_le32(inode->i_gid);
  439. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  440. raw_inode->i_size = cpu_to_le64(inode->i_size);
  441. raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  442. raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  443. raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  444. raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  445. raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
  446. raw_inode->i_flags = cpu_to_le32(ii->i_flags);
  447. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  448. if (has_bmap)
  449. nilfs_bmap_write(ii->i_bmap, raw_inode);
  450. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  451. raw_inode->i_device_code =
  452. cpu_to_le64(new_encode_dev(inode->i_rdev));
  453. /* When extending inode, nilfs->ns_inode_size should be checked
  454. for substitutions of appended fields */
  455. }
  456. void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
  457. {
  458. ino_t ino = inode->i_ino;
  459. struct nilfs_inode_info *ii = NILFS_I(inode);
  460. struct super_block *sb = inode->i_sb;
  461. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  462. struct nilfs_inode *raw_inode;
  463. raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
  464. /* The buffer is guarded with lock_buffer() by the caller */
  465. if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
  466. memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
  467. set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
  468. nilfs_write_inode_common(inode, raw_inode, 0);
  469. /* XXX: call with has_bmap = 0 is a workaround to avoid
  470. deadlock of bmap. This delays update of i_bmap to just
  471. before writing */
  472. nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
  473. }
  474. #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
  475. static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
  476. unsigned long from)
  477. {
  478. unsigned long b;
  479. int ret;
  480. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  481. return;
  482. repeat:
  483. ret = nilfs_bmap_last_key(ii->i_bmap, &b);
  484. if (ret == -ENOENT)
  485. return;
  486. else if (ret < 0)
  487. goto failed;
  488. if (b < from)
  489. return;
  490. b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
  491. ret = nilfs_bmap_truncate(ii->i_bmap, b);
  492. nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
  493. if (!ret || (ret == -ENOMEM &&
  494. nilfs_bmap_truncate(ii->i_bmap, b) == 0))
  495. goto repeat;
  496. failed:
  497. if (ret == -EINVAL)
  498. nilfs_error(ii->vfs_inode.i_sb, __func__,
  499. "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
  500. else
  501. nilfs_warning(ii->vfs_inode.i_sb, __func__,
  502. "failed to truncate bmap (ino=%lu, err=%d)",
  503. ii->vfs_inode.i_ino, ret);
  504. }
  505. void nilfs_truncate(struct inode *inode)
  506. {
  507. unsigned long blkoff;
  508. unsigned int blocksize;
  509. struct nilfs_transaction_info ti;
  510. struct super_block *sb = inode->i_sb;
  511. struct nilfs_inode_info *ii = NILFS_I(inode);
  512. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  513. return;
  514. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  515. return;
  516. blocksize = sb->s_blocksize;
  517. blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
  518. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  519. block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
  520. nilfs_truncate_bmap(ii, blkoff);
  521. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  522. if (IS_SYNC(inode))
  523. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  524. nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
  525. nilfs_transaction_commit(sb);
  526. /* May construct a logical segment and may fail in sync mode.
  527. But truncate has no return value. */
  528. }
  529. void nilfs_delete_inode(struct inode *inode)
  530. {
  531. struct nilfs_transaction_info ti;
  532. struct super_block *sb = inode->i_sb;
  533. struct nilfs_inode_info *ii = NILFS_I(inode);
  534. if (unlikely(is_bad_inode(inode))) {
  535. if (inode->i_data.nrpages)
  536. truncate_inode_pages(&inode->i_data, 0);
  537. clear_inode(inode);
  538. return;
  539. }
  540. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  541. if (inode->i_data.nrpages)
  542. truncate_inode_pages(&inode->i_data, 0);
  543. nilfs_truncate_bmap(ii, 0);
  544. nilfs_free_inode(inode);
  545. /* nilfs_free_inode() marks inode buffer dirty */
  546. if (IS_SYNC(inode))
  547. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  548. nilfs_transaction_commit(sb);
  549. /* May construct a logical segment and may fail in sync mode.
  550. But delete_inode has no return value. */
  551. }
  552. int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
  553. {
  554. struct nilfs_transaction_info ti;
  555. struct inode *inode = dentry->d_inode;
  556. struct super_block *sb = inode->i_sb;
  557. int err;
  558. err = inode_change_ok(inode, iattr);
  559. if (err)
  560. return err;
  561. err = nilfs_transaction_begin(sb, &ti, 0);
  562. if (unlikely(err))
  563. return err;
  564. err = inode_setattr(inode, iattr);
  565. if (!err && (iattr->ia_valid & ATTR_MODE))
  566. err = nilfs_acl_chmod(inode);
  567. if (likely(!err))
  568. err = nilfs_transaction_commit(sb);
  569. else
  570. nilfs_transaction_abort(sb);
  571. return err;
  572. }
  573. int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
  574. struct buffer_head **pbh)
  575. {
  576. struct nilfs_inode_info *ii = NILFS_I(inode);
  577. int err;
  578. spin_lock(&sbi->s_inode_lock);
  579. /* Caller of this function MUST lock s_inode_lock */
  580. if (ii->i_bh == NULL) {
  581. spin_unlock(&sbi->s_inode_lock);
  582. err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
  583. pbh);
  584. if (unlikely(err))
  585. return err;
  586. spin_lock(&sbi->s_inode_lock);
  587. if (ii->i_bh == NULL)
  588. ii->i_bh = *pbh;
  589. else {
  590. brelse(*pbh);
  591. *pbh = ii->i_bh;
  592. }
  593. } else
  594. *pbh = ii->i_bh;
  595. get_bh(*pbh);
  596. spin_unlock(&sbi->s_inode_lock);
  597. return 0;
  598. }
  599. int nilfs_inode_dirty(struct inode *inode)
  600. {
  601. struct nilfs_inode_info *ii = NILFS_I(inode);
  602. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  603. int ret = 0;
  604. if (!list_empty(&ii->i_dirty)) {
  605. spin_lock(&sbi->s_inode_lock);
  606. ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
  607. test_bit(NILFS_I_BUSY, &ii->i_state);
  608. spin_unlock(&sbi->s_inode_lock);
  609. }
  610. return ret;
  611. }
  612. int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
  613. unsigned nr_dirty)
  614. {
  615. struct nilfs_inode_info *ii = NILFS_I(inode);
  616. atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
  617. if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
  618. return 0;
  619. spin_lock(&sbi->s_inode_lock);
  620. if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
  621. !test_bit(NILFS_I_BUSY, &ii->i_state)) {
  622. /* Because this routine may race with nilfs_dispose_list(),
  623. we have to check NILFS_I_QUEUED here, too. */
  624. if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
  625. /* This will happen when somebody is freeing
  626. this inode. */
  627. nilfs_warning(sbi->s_super, __func__,
  628. "cannot get inode (ino=%lu)\n",
  629. inode->i_ino);
  630. spin_unlock(&sbi->s_inode_lock);
  631. return -EINVAL; /* NILFS_I_DIRTY may remain for
  632. freeing inode */
  633. }
  634. list_del(&ii->i_dirty);
  635. list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
  636. set_bit(NILFS_I_QUEUED, &ii->i_state);
  637. }
  638. spin_unlock(&sbi->s_inode_lock);
  639. return 0;
  640. }
  641. int nilfs_mark_inode_dirty(struct inode *inode)
  642. {
  643. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  644. struct buffer_head *ibh;
  645. int err;
  646. err = nilfs_load_inode_block(sbi, inode, &ibh);
  647. if (unlikely(err)) {
  648. nilfs_warning(inode->i_sb, __func__,
  649. "failed to reget inode block.\n");
  650. return err;
  651. }
  652. lock_buffer(ibh);
  653. nilfs_update_inode(inode, ibh);
  654. unlock_buffer(ibh);
  655. nilfs_mdt_mark_buffer_dirty(ibh);
  656. nilfs_mdt_mark_dirty(sbi->s_ifile);
  657. brelse(ibh);
  658. return 0;
  659. }
  660. /**
  661. * nilfs_dirty_inode - reflect changes on given inode to an inode block.
  662. * @inode: inode of the file to be registered.
  663. *
  664. * nilfs_dirty_inode() loads a inode block containing the specified
  665. * @inode and copies data from a nilfs_inode to a corresponding inode
  666. * entry in the inode block. This operation is excluded from the segment
  667. * construction. This function can be called both as a single operation
  668. * and as a part of indivisible file operations.
  669. */
  670. void nilfs_dirty_inode(struct inode *inode)
  671. {
  672. struct nilfs_transaction_info ti;
  673. if (is_bad_inode(inode)) {
  674. nilfs_warning(inode->i_sb, __func__,
  675. "tried to mark bad_inode dirty. ignored.\n");
  676. dump_stack();
  677. return;
  678. }
  679. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  680. nilfs_mark_inode_dirty(inode);
  681. nilfs_transaction_commit(inode->i_sb); /* never fails */
  682. }