inode.c 32 KB

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