inode.c 25 KB

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