inode.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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/pagemap.h>
  27. #include <linux/writeback.h>
  28. #include <linux/uio.h>
  29. #include "nilfs.h"
  30. #include "btnode.h"
  31. #include "segment.h"
  32. #include "page.h"
  33. #include "mdt.h"
  34. #include "cpfile.h"
  35. #include "ifile.h"
  36. /**
  37. * struct nilfs_iget_args - arguments used during comparison between inodes
  38. * @ino: inode number
  39. * @cno: checkpoint number
  40. * @root: pointer on NILFS root object (mounted checkpoint)
  41. * @for_gc: inode for GC flag
  42. */
  43. struct nilfs_iget_args {
  44. u64 ino;
  45. __u64 cno;
  46. struct nilfs_root *root;
  47. int for_gc;
  48. };
  49. static int nilfs_iget_test(struct inode *inode, void *opaque);
  50. void nilfs_inode_add_blocks(struct inode *inode, int n)
  51. {
  52. struct nilfs_root *root = NILFS_I(inode)->i_root;
  53. inode_add_bytes(inode, i_blocksize(inode) * n);
  54. if (root)
  55. atomic64_add(n, &root->blocks_count);
  56. }
  57. void nilfs_inode_sub_blocks(struct inode *inode, int n)
  58. {
  59. struct nilfs_root *root = NILFS_I(inode)->i_root;
  60. inode_sub_bytes(inode, i_blocksize(inode) * n);
  61. if (root)
  62. atomic64_sub(n, &root->blocks_count);
  63. }
  64. /**
  65. * nilfs_get_block() - get a file block on the filesystem (callback function)
  66. * @inode - inode struct of the target file
  67. * @blkoff - file block number
  68. * @bh_result - buffer head to be mapped on
  69. * @create - indicate whether allocating the block or not when it has not
  70. * been allocated yet.
  71. *
  72. * This function does not issue actual read request of the specified data
  73. * block. It is done by VFS.
  74. */
  75. int nilfs_get_block(struct inode *inode, sector_t blkoff,
  76. struct buffer_head *bh_result, int create)
  77. {
  78. struct nilfs_inode_info *ii = NILFS_I(inode);
  79. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  80. __u64 blknum = 0;
  81. int err = 0, ret;
  82. unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
  83. down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  84. ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
  85. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  86. if (ret >= 0) { /* found */
  87. map_bh(bh_result, inode->i_sb, blknum);
  88. if (ret > 0)
  89. bh_result->b_size = (ret << inode->i_blkbits);
  90. goto out;
  91. }
  92. /* data block was not found */
  93. if (ret == -ENOENT && create) {
  94. struct nilfs_transaction_info ti;
  95. bh_result->b_blocknr = 0;
  96. err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
  97. if (unlikely(err))
  98. goto out;
  99. err = nilfs_bmap_insert(ii->i_bmap, blkoff,
  100. (unsigned long)bh_result);
  101. if (unlikely(err != 0)) {
  102. if (err == -EEXIST) {
  103. /*
  104. * The get_block() function could be called
  105. * from multiple callers for an inode.
  106. * However, the page having this block must
  107. * be locked in this case.
  108. */
  109. printk(KERN_WARNING
  110. "nilfs_get_block: a race condition "
  111. "while inserting a data block. "
  112. "(inode number=%lu, file block "
  113. "offset=%llu)\n",
  114. inode->i_ino,
  115. (unsigned long long)blkoff);
  116. err = 0;
  117. }
  118. nilfs_transaction_abort(inode->i_sb);
  119. goto out;
  120. }
  121. nilfs_mark_inode_dirty_sync(inode);
  122. nilfs_transaction_commit(inode->i_sb); /* never fails */
  123. /* Error handling should be detailed */
  124. set_buffer_new(bh_result);
  125. set_buffer_delay(bh_result);
  126. map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
  127. to proper value */
  128. } else if (ret == -ENOENT) {
  129. /* not found is not error (e.g. hole); must return without
  130. the mapped state flag. */
  131. ;
  132. } else {
  133. err = ret;
  134. }
  135. out:
  136. return err;
  137. }
  138. /**
  139. * nilfs_readpage() - implement readpage() method of nilfs_aops {}
  140. * address_space_operations.
  141. * @file - file struct of the file to be read
  142. * @page - the page to be read
  143. */
  144. static int nilfs_readpage(struct file *file, struct page *page)
  145. {
  146. return mpage_readpage(page, nilfs_get_block);
  147. }
  148. /**
  149. * nilfs_readpages() - implement readpages() method of nilfs_aops {}
  150. * address_space_operations.
  151. * @file - file struct of the file to be read
  152. * @mapping - address_space struct used for reading multiple pages
  153. * @pages - the pages to be read
  154. * @nr_pages - number of pages to be read
  155. */
  156. static int nilfs_readpages(struct file *file, struct address_space *mapping,
  157. struct list_head *pages, unsigned nr_pages)
  158. {
  159. return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
  160. }
  161. static int nilfs_writepages(struct address_space *mapping,
  162. struct writeback_control *wbc)
  163. {
  164. struct inode *inode = mapping->host;
  165. int err = 0;
  166. if (inode->i_sb->s_flags & MS_RDONLY) {
  167. nilfs_clear_dirty_pages(mapping, false);
  168. return -EROFS;
  169. }
  170. if (wbc->sync_mode == WB_SYNC_ALL)
  171. err = nilfs_construct_dsync_segment(inode->i_sb, inode,
  172. wbc->range_start,
  173. wbc->range_end);
  174. return err;
  175. }
  176. static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
  177. {
  178. struct inode *inode = page->mapping->host;
  179. int err;
  180. if (inode->i_sb->s_flags & MS_RDONLY) {
  181. /*
  182. * It means that filesystem was remounted in read-only
  183. * mode because of error or metadata corruption. But we
  184. * have dirty pages that try to be flushed in background.
  185. * So, here we simply discard this dirty page.
  186. */
  187. nilfs_clear_dirty_page(page, false);
  188. unlock_page(page);
  189. return -EROFS;
  190. }
  191. redirty_page_for_writepage(wbc, page);
  192. unlock_page(page);
  193. if (wbc->sync_mode == WB_SYNC_ALL) {
  194. err = nilfs_construct_segment(inode->i_sb);
  195. if (unlikely(err))
  196. return err;
  197. } else if (wbc->for_reclaim)
  198. nilfs_flush_segment(inode->i_sb, inode->i_ino);
  199. return 0;
  200. }
  201. static int nilfs_set_page_dirty(struct page *page)
  202. {
  203. struct inode *inode = page->mapping->host;
  204. int ret = __set_page_dirty_nobuffers(page);
  205. if (page_has_buffers(page)) {
  206. unsigned nr_dirty = 0;
  207. struct buffer_head *bh, *head;
  208. /*
  209. * This page is locked by callers, and no other thread
  210. * concurrently marks its buffers dirty since they are
  211. * only dirtied through routines in fs/buffer.c in
  212. * which call sites of mark_buffer_dirty are protected
  213. * by page lock.
  214. */
  215. bh = head = page_buffers(page);
  216. do {
  217. /* Do not mark hole blocks dirty */
  218. if (buffer_dirty(bh) || !buffer_mapped(bh))
  219. continue;
  220. set_buffer_dirty(bh);
  221. nr_dirty++;
  222. } while (bh = bh->b_this_page, bh != head);
  223. if (nr_dirty)
  224. nilfs_set_file_dirty(inode, nr_dirty);
  225. } else if (ret) {
  226. unsigned nr_dirty = 1 << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  227. nilfs_set_file_dirty(inode, nr_dirty);
  228. }
  229. return ret;
  230. }
  231. void nilfs_write_failed(struct address_space *mapping, loff_t to)
  232. {
  233. struct inode *inode = mapping->host;
  234. if (to > inode->i_size) {
  235. truncate_pagecache(inode, inode->i_size);
  236. nilfs_truncate(inode);
  237. }
  238. }
  239. static int nilfs_write_begin(struct file *file, struct address_space *mapping,
  240. loff_t pos, unsigned len, unsigned flags,
  241. struct page **pagep, void **fsdata)
  242. {
  243. struct inode *inode = mapping->host;
  244. int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
  245. if (unlikely(err))
  246. return err;
  247. err = block_write_begin(mapping, pos, len, flags, pagep,
  248. nilfs_get_block);
  249. if (unlikely(err)) {
  250. nilfs_write_failed(mapping, pos + len);
  251. nilfs_transaction_abort(inode->i_sb);
  252. }
  253. return err;
  254. }
  255. static int nilfs_write_end(struct file *file, struct address_space *mapping,
  256. loff_t pos, unsigned len, unsigned copied,
  257. struct page *page, void *fsdata)
  258. {
  259. struct inode *inode = mapping->host;
  260. unsigned start = pos & (PAGE_CACHE_SIZE - 1);
  261. unsigned nr_dirty;
  262. int err;
  263. nr_dirty = nilfs_page_count_clean_buffers(page, start,
  264. start + copied);
  265. copied = generic_write_end(file, mapping, pos, len, copied, page,
  266. fsdata);
  267. nilfs_set_file_dirty(inode, nr_dirty);
  268. err = nilfs_transaction_commit(inode->i_sb);
  269. return err ? : copied;
  270. }
  271. static ssize_t
  272. nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset)
  273. {
  274. struct inode *inode = file_inode(iocb->ki_filp);
  275. if (iov_iter_rw(iter) == WRITE)
  276. return 0;
  277. /* Needs synchronization with the cleaner */
  278. return blockdev_direct_IO(iocb, inode, iter, offset, nilfs_get_block);
  279. }
  280. const struct address_space_operations nilfs_aops = {
  281. .writepage = nilfs_writepage,
  282. .readpage = nilfs_readpage,
  283. .writepages = nilfs_writepages,
  284. .set_page_dirty = nilfs_set_page_dirty,
  285. .readpages = nilfs_readpages,
  286. .write_begin = nilfs_write_begin,
  287. .write_end = nilfs_write_end,
  288. /* .releasepage = nilfs_releasepage, */
  289. .invalidatepage = block_invalidatepage,
  290. .direct_IO = nilfs_direct_IO,
  291. .is_partially_uptodate = block_is_partially_uptodate,
  292. };
  293. static int nilfs_insert_inode_locked(struct inode *inode,
  294. struct nilfs_root *root,
  295. unsigned long ino)
  296. {
  297. struct nilfs_iget_args args = {
  298. .ino = ino, .root = root, .cno = 0, .for_gc = 0
  299. };
  300. return insert_inode_locked4(inode, ino, nilfs_iget_test, &args);
  301. }
  302. struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
  303. {
  304. struct super_block *sb = dir->i_sb;
  305. struct the_nilfs *nilfs = sb->s_fs_info;
  306. struct inode *inode;
  307. struct nilfs_inode_info *ii;
  308. struct nilfs_root *root;
  309. int err = -ENOMEM;
  310. ino_t ino;
  311. inode = new_inode(sb);
  312. if (unlikely(!inode))
  313. goto failed;
  314. mapping_set_gfp_mask(inode->i_mapping,
  315. mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
  316. root = NILFS_I(dir)->i_root;
  317. ii = NILFS_I(inode);
  318. ii->i_state = 1 << NILFS_I_NEW;
  319. ii->i_root = root;
  320. err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh);
  321. if (unlikely(err))
  322. goto failed_ifile_create_inode;
  323. /* reference count of i_bh inherits from nilfs_mdt_read_block() */
  324. atomic64_inc(&root->inodes_count);
  325. inode_init_owner(inode, dir, mode);
  326. inode->i_ino = ino;
  327. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  328. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  329. err = nilfs_bmap_read(ii->i_bmap, NULL);
  330. if (err < 0)
  331. goto failed_after_creation;
  332. set_bit(NILFS_I_BMAP, &ii->i_state);
  333. /* No lock is needed; iget() ensures it. */
  334. }
  335. ii->i_flags = nilfs_mask_flags(
  336. mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED);
  337. /* ii->i_file_acl = 0; */
  338. /* ii->i_dir_acl = 0; */
  339. ii->i_dir_start_lookup = 0;
  340. nilfs_set_inode_flags(inode);
  341. spin_lock(&nilfs->ns_next_gen_lock);
  342. inode->i_generation = nilfs->ns_next_generation++;
  343. spin_unlock(&nilfs->ns_next_gen_lock);
  344. if (nilfs_insert_inode_locked(inode, root, ino) < 0) {
  345. err = -EIO;
  346. goto failed_after_creation;
  347. }
  348. err = nilfs_init_acl(inode, dir);
  349. if (unlikely(err))
  350. goto failed_after_creation; /* never occur. When supporting
  351. nilfs_init_acl(), proper cancellation of
  352. above jobs should be considered */
  353. return inode;
  354. failed_after_creation:
  355. clear_nlink(inode);
  356. unlock_new_inode(inode);
  357. iput(inode); /* raw_inode will be deleted through
  358. nilfs_evict_inode() */
  359. goto failed;
  360. failed_ifile_create_inode:
  361. make_bad_inode(inode);
  362. iput(inode); /* if i_nlink == 1, generic_forget_inode() will be
  363. called */
  364. failed:
  365. return ERR_PTR(err);
  366. }
  367. void nilfs_set_inode_flags(struct inode *inode)
  368. {
  369. unsigned int flags = NILFS_I(inode)->i_flags;
  370. unsigned int new_fl = 0;
  371. if (flags & FS_SYNC_FL)
  372. new_fl |= S_SYNC;
  373. if (flags & FS_APPEND_FL)
  374. new_fl |= S_APPEND;
  375. if (flags & FS_IMMUTABLE_FL)
  376. new_fl |= S_IMMUTABLE;
  377. if (flags & FS_NOATIME_FL)
  378. new_fl |= S_NOATIME;
  379. if (flags & FS_DIRSYNC_FL)
  380. new_fl |= S_DIRSYNC;
  381. inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE |
  382. S_NOATIME | S_DIRSYNC);
  383. }
  384. int nilfs_read_inode_common(struct inode *inode,
  385. struct nilfs_inode *raw_inode)
  386. {
  387. struct nilfs_inode_info *ii = NILFS_I(inode);
  388. int err;
  389. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  390. i_uid_write(inode, le32_to_cpu(raw_inode->i_uid));
  391. i_gid_write(inode, le32_to_cpu(raw_inode->i_gid));
  392. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  393. inode->i_size = le64_to_cpu(raw_inode->i_size);
  394. inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  395. inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
  396. inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  397. inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  398. inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
  399. inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  400. if (inode->i_nlink == 0)
  401. return -ESTALE; /* this inode is deleted */
  402. inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
  403. ii->i_flags = le32_to_cpu(raw_inode->i_flags);
  404. #if 0
  405. ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  406. ii->i_dir_acl = S_ISREG(inode->i_mode) ?
  407. 0 : le32_to_cpu(raw_inode->i_dir_acl);
  408. #endif
  409. ii->i_dir_start_lookup = 0;
  410. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  411. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  412. S_ISLNK(inode->i_mode)) {
  413. err = nilfs_bmap_read(ii->i_bmap, raw_inode);
  414. if (err < 0)
  415. return err;
  416. set_bit(NILFS_I_BMAP, &ii->i_state);
  417. /* No lock is needed; iget() ensures it. */
  418. }
  419. return 0;
  420. }
  421. static int __nilfs_read_inode(struct super_block *sb,
  422. struct nilfs_root *root, unsigned long ino,
  423. struct inode *inode)
  424. {
  425. struct the_nilfs *nilfs = sb->s_fs_info;
  426. struct buffer_head *bh;
  427. struct nilfs_inode *raw_inode;
  428. int err;
  429. down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  430. err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh);
  431. if (unlikely(err))
  432. goto bad_inode;
  433. raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh);
  434. err = nilfs_read_inode_common(inode, raw_inode);
  435. if (err)
  436. goto failed_unmap;
  437. if (S_ISREG(inode->i_mode)) {
  438. inode->i_op = &nilfs_file_inode_operations;
  439. inode->i_fop = &nilfs_file_operations;
  440. inode->i_mapping->a_ops = &nilfs_aops;
  441. } else if (S_ISDIR(inode->i_mode)) {
  442. inode->i_op = &nilfs_dir_inode_operations;
  443. inode->i_fop = &nilfs_dir_operations;
  444. inode->i_mapping->a_ops = &nilfs_aops;
  445. } else if (S_ISLNK(inode->i_mode)) {
  446. inode->i_op = &nilfs_symlink_inode_operations;
  447. inode->i_mapping->a_ops = &nilfs_aops;
  448. } else {
  449. inode->i_op = &nilfs_special_inode_operations;
  450. init_special_inode(
  451. inode, inode->i_mode,
  452. huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
  453. }
  454. nilfs_ifile_unmap_inode(root->ifile, ino, bh);
  455. brelse(bh);
  456. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  457. nilfs_set_inode_flags(inode);
  458. mapping_set_gfp_mask(inode->i_mapping,
  459. mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
  460. return 0;
  461. failed_unmap:
  462. nilfs_ifile_unmap_inode(root->ifile, ino, bh);
  463. brelse(bh);
  464. bad_inode:
  465. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  466. return err;
  467. }
  468. static int nilfs_iget_test(struct inode *inode, void *opaque)
  469. {
  470. struct nilfs_iget_args *args = opaque;
  471. struct nilfs_inode_info *ii;
  472. if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
  473. return 0;
  474. ii = NILFS_I(inode);
  475. if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
  476. return !args->for_gc;
  477. return args->for_gc && args->cno == ii->i_cno;
  478. }
  479. static int nilfs_iget_set(struct inode *inode, void *opaque)
  480. {
  481. struct nilfs_iget_args *args = opaque;
  482. inode->i_ino = args->ino;
  483. if (args->for_gc) {
  484. NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE;
  485. NILFS_I(inode)->i_cno = args->cno;
  486. NILFS_I(inode)->i_root = NULL;
  487. } else {
  488. if (args->root && args->ino == NILFS_ROOT_INO)
  489. nilfs_get_root(args->root);
  490. NILFS_I(inode)->i_root = args->root;
  491. }
  492. return 0;
  493. }
  494. struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root,
  495. unsigned long ino)
  496. {
  497. struct nilfs_iget_args args = {
  498. .ino = ino, .root = root, .cno = 0, .for_gc = 0
  499. };
  500. return ilookup5(sb, ino, nilfs_iget_test, &args);
  501. }
  502. struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root,
  503. unsigned long ino)
  504. {
  505. struct nilfs_iget_args args = {
  506. .ino = ino, .root = root, .cno = 0, .for_gc = 0
  507. };
  508. return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
  509. }
  510. struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
  511. unsigned long ino)
  512. {
  513. struct inode *inode;
  514. int err;
  515. inode = nilfs_iget_locked(sb, root, ino);
  516. if (unlikely(!inode))
  517. return ERR_PTR(-ENOMEM);
  518. if (!(inode->i_state & I_NEW))
  519. return inode;
  520. err = __nilfs_read_inode(sb, root, ino, inode);
  521. if (unlikely(err)) {
  522. iget_failed(inode);
  523. return ERR_PTR(err);
  524. }
  525. unlock_new_inode(inode);
  526. return inode;
  527. }
  528. struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
  529. __u64 cno)
  530. {
  531. struct nilfs_iget_args args = {
  532. .ino = ino, .root = NULL, .cno = cno, .for_gc = 1
  533. };
  534. struct inode *inode;
  535. int err;
  536. inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
  537. if (unlikely(!inode))
  538. return ERR_PTR(-ENOMEM);
  539. if (!(inode->i_state & I_NEW))
  540. return inode;
  541. err = nilfs_init_gcinode(inode);
  542. if (unlikely(err)) {
  543. iget_failed(inode);
  544. return ERR_PTR(err);
  545. }
  546. unlock_new_inode(inode);
  547. return inode;
  548. }
  549. void nilfs_write_inode_common(struct inode *inode,
  550. struct nilfs_inode *raw_inode, int has_bmap)
  551. {
  552. struct nilfs_inode_info *ii = NILFS_I(inode);
  553. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  554. raw_inode->i_uid = cpu_to_le32(i_uid_read(inode));
  555. raw_inode->i_gid = cpu_to_le32(i_gid_read(inode));
  556. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  557. raw_inode->i_size = cpu_to_le64(inode->i_size);
  558. raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  559. raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  560. raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  561. raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  562. raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
  563. raw_inode->i_flags = cpu_to_le32(ii->i_flags);
  564. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  565. if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) {
  566. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  567. /* zero-fill unused portion in the case of super root block */
  568. raw_inode->i_xattr = 0;
  569. raw_inode->i_pad = 0;
  570. memset((void *)raw_inode + sizeof(*raw_inode), 0,
  571. nilfs->ns_inode_size - sizeof(*raw_inode));
  572. }
  573. if (has_bmap)
  574. nilfs_bmap_write(ii->i_bmap, raw_inode);
  575. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  576. raw_inode->i_device_code =
  577. cpu_to_le64(huge_encode_dev(inode->i_rdev));
  578. /* When extending inode, nilfs->ns_inode_size should be checked
  579. for substitutions of appended fields */
  580. }
  581. void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags)
  582. {
  583. ino_t ino = inode->i_ino;
  584. struct nilfs_inode_info *ii = NILFS_I(inode);
  585. struct inode *ifile = ii->i_root->ifile;
  586. struct nilfs_inode *raw_inode;
  587. raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
  588. if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
  589. memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
  590. if (flags & I_DIRTY_DATASYNC)
  591. set_bit(NILFS_I_INODE_SYNC, &ii->i_state);
  592. nilfs_write_inode_common(inode, raw_inode, 0);
  593. /* XXX: call with has_bmap = 0 is a workaround to avoid
  594. deadlock of bmap. This delays update of i_bmap to just
  595. before writing */
  596. nilfs_ifile_unmap_inode(ifile, ino, ibh);
  597. }
  598. #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
  599. static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
  600. unsigned long from)
  601. {
  602. __u64 b;
  603. int ret;
  604. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  605. return;
  606. repeat:
  607. ret = nilfs_bmap_last_key(ii->i_bmap, &b);
  608. if (ret == -ENOENT)
  609. return;
  610. else if (ret < 0)
  611. goto failed;
  612. if (b < from)
  613. return;
  614. b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
  615. ret = nilfs_bmap_truncate(ii->i_bmap, b);
  616. nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
  617. if (!ret || (ret == -ENOMEM &&
  618. nilfs_bmap_truncate(ii->i_bmap, b) == 0))
  619. goto repeat;
  620. failed:
  621. nilfs_warning(ii->vfs_inode.i_sb, __func__,
  622. "failed to truncate bmap (ino=%lu, err=%d)",
  623. ii->vfs_inode.i_ino, ret);
  624. }
  625. void nilfs_truncate(struct inode *inode)
  626. {
  627. unsigned long blkoff;
  628. unsigned int blocksize;
  629. struct nilfs_transaction_info ti;
  630. struct super_block *sb = inode->i_sb;
  631. struct nilfs_inode_info *ii = NILFS_I(inode);
  632. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  633. return;
  634. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  635. return;
  636. blocksize = sb->s_blocksize;
  637. blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
  638. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  639. block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
  640. nilfs_truncate_bmap(ii, blkoff);
  641. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  642. if (IS_SYNC(inode))
  643. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  644. nilfs_mark_inode_dirty(inode);
  645. nilfs_set_file_dirty(inode, 0);
  646. nilfs_transaction_commit(sb);
  647. /* May construct a logical segment and may fail in sync mode.
  648. But truncate has no return value. */
  649. }
  650. static void nilfs_clear_inode(struct inode *inode)
  651. {
  652. struct nilfs_inode_info *ii = NILFS_I(inode);
  653. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  654. /*
  655. * Free resources allocated in nilfs_read_inode(), here.
  656. */
  657. BUG_ON(!list_empty(&ii->i_dirty));
  658. brelse(ii->i_bh);
  659. ii->i_bh = NULL;
  660. if (mdi && mdi->mi_palloc_cache)
  661. nilfs_palloc_destroy_cache(inode);
  662. if (test_bit(NILFS_I_BMAP, &ii->i_state))
  663. nilfs_bmap_clear(ii->i_bmap);
  664. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  665. if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
  666. nilfs_put_root(ii->i_root);
  667. }
  668. void nilfs_evict_inode(struct inode *inode)
  669. {
  670. struct nilfs_transaction_info ti;
  671. struct super_block *sb = inode->i_sb;
  672. struct nilfs_inode_info *ii = NILFS_I(inode);
  673. int ret;
  674. if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
  675. truncate_inode_pages_final(&inode->i_data);
  676. clear_inode(inode);
  677. nilfs_clear_inode(inode);
  678. return;
  679. }
  680. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  681. truncate_inode_pages_final(&inode->i_data);
  682. /* TODO: some of the following operations may fail. */
  683. nilfs_truncate_bmap(ii, 0);
  684. nilfs_mark_inode_dirty(inode);
  685. clear_inode(inode);
  686. ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
  687. if (!ret)
  688. atomic64_dec(&ii->i_root->inodes_count);
  689. nilfs_clear_inode(inode);
  690. if (IS_SYNC(inode))
  691. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  692. nilfs_transaction_commit(sb);
  693. /* May construct a logical segment and may fail in sync mode.
  694. But delete_inode has no return value. */
  695. }
  696. int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
  697. {
  698. struct nilfs_transaction_info ti;
  699. struct inode *inode = d_inode(dentry);
  700. struct super_block *sb = inode->i_sb;
  701. int err;
  702. err = inode_change_ok(inode, iattr);
  703. if (err)
  704. return err;
  705. err = nilfs_transaction_begin(sb, &ti, 0);
  706. if (unlikely(err))
  707. return err;
  708. if ((iattr->ia_valid & ATTR_SIZE) &&
  709. iattr->ia_size != i_size_read(inode)) {
  710. inode_dio_wait(inode);
  711. truncate_setsize(inode, iattr->ia_size);
  712. nilfs_truncate(inode);
  713. }
  714. setattr_copy(inode, iattr);
  715. mark_inode_dirty(inode);
  716. if (iattr->ia_valid & ATTR_MODE) {
  717. err = nilfs_acl_chmod(inode);
  718. if (unlikely(err))
  719. goto out_err;
  720. }
  721. return nilfs_transaction_commit(sb);
  722. out_err:
  723. nilfs_transaction_abort(sb);
  724. return err;
  725. }
  726. int nilfs_permission(struct inode *inode, int mask)
  727. {
  728. struct nilfs_root *root = NILFS_I(inode)->i_root;
  729. if ((mask & MAY_WRITE) && root &&
  730. root->cno != NILFS_CPTREE_CURRENT_CNO)
  731. return -EROFS; /* snapshot is not writable */
  732. return generic_permission(inode, mask);
  733. }
  734. int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
  735. {
  736. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  737. struct nilfs_inode_info *ii = NILFS_I(inode);
  738. int err;
  739. spin_lock(&nilfs->ns_inode_lock);
  740. if (ii->i_bh == NULL) {
  741. spin_unlock(&nilfs->ns_inode_lock);
  742. err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
  743. inode->i_ino, pbh);
  744. if (unlikely(err))
  745. return err;
  746. spin_lock(&nilfs->ns_inode_lock);
  747. if (ii->i_bh == NULL)
  748. ii->i_bh = *pbh;
  749. else {
  750. brelse(*pbh);
  751. *pbh = ii->i_bh;
  752. }
  753. } else
  754. *pbh = ii->i_bh;
  755. get_bh(*pbh);
  756. spin_unlock(&nilfs->ns_inode_lock);
  757. return 0;
  758. }
  759. int nilfs_inode_dirty(struct inode *inode)
  760. {
  761. struct nilfs_inode_info *ii = NILFS_I(inode);
  762. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  763. int ret = 0;
  764. if (!list_empty(&ii->i_dirty)) {
  765. spin_lock(&nilfs->ns_inode_lock);
  766. ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
  767. test_bit(NILFS_I_BUSY, &ii->i_state);
  768. spin_unlock(&nilfs->ns_inode_lock);
  769. }
  770. return ret;
  771. }
  772. int nilfs_set_file_dirty(struct inode *inode, unsigned nr_dirty)
  773. {
  774. struct nilfs_inode_info *ii = NILFS_I(inode);
  775. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  776. atomic_add(nr_dirty, &nilfs->ns_ndirtyblks);
  777. if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
  778. return 0;
  779. spin_lock(&nilfs->ns_inode_lock);
  780. if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
  781. !test_bit(NILFS_I_BUSY, &ii->i_state)) {
  782. /* Because this routine may race with nilfs_dispose_list(),
  783. we have to check NILFS_I_QUEUED here, too. */
  784. if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
  785. /* This will happen when somebody is freeing
  786. this inode. */
  787. nilfs_warning(inode->i_sb, __func__,
  788. "cannot get inode (ino=%lu)\n",
  789. inode->i_ino);
  790. spin_unlock(&nilfs->ns_inode_lock);
  791. return -EINVAL; /* NILFS_I_DIRTY may remain for
  792. freeing inode */
  793. }
  794. list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files);
  795. set_bit(NILFS_I_QUEUED, &ii->i_state);
  796. }
  797. spin_unlock(&nilfs->ns_inode_lock);
  798. return 0;
  799. }
  800. int __nilfs_mark_inode_dirty(struct inode *inode, int flags)
  801. {
  802. struct buffer_head *ibh;
  803. int err;
  804. err = nilfs_load_inode_block(inode, &ibh);
  805. if (unlikely(err)) {
  806. nilfs_warning(inode->i_sb, __func__,
  807. "failed to reget inode block.\n");
  808. return err;
  809. }
  810. nilfs_update_inode(inode, ibh, flags);
  811. mark_buffer_dirty(ibh);
  812. nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
  813. brelse(ibh);
  814. return 0;
  815. }
  816. /**
  817. * nilfs_dirty_inode - reflect changes on given inode to an inode block.
  818. * @inode: inode of the file to be registered.
  819. *
  820. * nilfs_dirty_inode() loads a inode block containing the specified
  821. * @inode and copies data from a nilfs_inode to a corresponding inode
  822. * entry in the inode block. This operation is excluded from the segment
  823. * construction. This function can be called both as a single operation
  824. * and as a part of indivisible file operations.
  825. */
  826. void nilfs_dirty_inode(struct inode *inode, int flags)
  827. {
  828. struct nilfs_transaction_info ti;
  829. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  830. if (is_bad_inode(inode)) {
  831. nilfs_warning(inode->i_sb, __func__,
  832. "tried to mark bad_inode dirty. ignored.\n");
  833. dump_stack();
  834. return;
  835. }
  836. if (mdi) {
  837. nilfs_mdt_mark_dirty(inode);
  838. return;
  839. }
  840. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  841. __nilfs_mark_inode_dirty(inode, flags);
  842. nilfs_transaction_commit(inode->i_sb); /* never fails */
  843. }
  844. int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  845. __u64 start, __u64 len)
  846. {
  847. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  848. __u64 logical = 0, phys = 0, size = 0;
  849. __u32 flags = 0;
  850. loff_t isize;
  851. sector_t blkoff, end_blkoff;
  852. sector_t delalloc_blkoff;
  853. unsigned long delalloc_blklen;
  854. unsigned int blkbits = inode->i_blkbits;
  855. int ret, n;
  856. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  857. if (ret)
  858. return ret;
  859. mutex_lock(&inode->i_mutex);
  860. isize = i_size_read(inode);
  861. blkoff = start >> blkbits;
  862. end_blkoff = (start + len - 1) >> blkbits;
  863. delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
  864. &delalloc_blkoff);
  865. do {
  866. __u64 blkphy;
  867. unsigned int maxblocks;
  868. if (delalloc_blklen && blkoff == delalloc_blkoff) {
  869. if (size) {
  870. /* End of the current extent */
  871. ret = fiemap_fill_next_extent(
  872. fieinfo, logical, phys, size, flags);
  873. if (ret)
  874. break;
  875. }
  876. if (blkoff > end_blkoff)
  877. break;
  878. flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
  879. logical = blkoff << blkbits;
  880. phys = 0;
  881. size = delalloc_blklen << blkbits;
  882. blkoff = delalloc_blkoff + delalloc_blklen;
  883. delalloc_blklen = nilfs_find_uncommitted_extent(
  884. inode, blkoff, &delalloc_blkoff);
  885. continue;
  886. }
  887. /*
  888. * Limit the number of blocks that we look up so as
  889. * not to get into the next delayed allocation extent.
  890. */
  891. maxblocks = INT_MAX;
  892. if (delalloc_blklen)
  893. maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
  894. maxblocks);
  895. blkphy = 0;
  896. down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  897. n = nilfs_bmap_lookup_contig(
  898. NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
  899. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  900. if (n < 0) {
  901. int past_eof;
  902. if (unlikely(n != -ENOENT))
  903. break; /* error */
  904. /* HOLE */
  905. blkoff++;
  906. past_eof = ((blkoff << blkbits) >= isize);
  907. if (size) {
  908. /* End of the current extent */
  909. if (past_eof)
  910. flags |= FIEMAP_EXTENT_LAST;
  911. ret = fiemap_fill_next_extent(
  912. fieinfo, logical, phys, size, flags);
  913. if (ret)
  914. break;
  915. size = 0;
  916. }
  917. if (blkoff > end_blkoff || past_eof)
  918. break;
  919. } else {
  920. if (size) {
  921. if (phys && blkphy << blkbits == phys + size) {
  922. /* The current extent goes on */
  923. size += n << blkbits;
  924. } else {
  925. /* Terminate the current extent */
  926. ret = fiemap_fill_next_extent(
  927. fieinfo, logical, phys, size,
  928. flags);
  929. if (ret || blkoff > end_blkoff)
  930. break;
  931. /* Start another extent */
  932. flags = FIEMAP_EXTENT_MERGED;
  933. logical = blkoff << blkbits;
  934. phys = blkphy << blkbits;
  935. size = n << blkbits;
  936. }
  937. } else {
  938. /* Start a new extent */
  939. flags = FIEMAP_EXTENT_MERGED;
  940. logical = blkoff << blkbits;
  941. phys = blkphy << blkbits;
  942. size = n << blkbits;
  943. }
  944. blkoff += n;
  945. }
  946. cond_resched();
  947. } while (true);
  948. /* If ret is 1 then we just hit the end of the extent array */
  949. if (ret == 1)
  950. ret = 0;
  951. mutex_unlock(&inode->i_mutex);
  952. return ret;
  953. }