inode.c 29 KB

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