inode.c 33 KB

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