inode.c 24 KB

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