xfs_fsmap.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_btree.h"
  16. #include "xfs_rmap_btree.h"
  17. #include "xfs_trace.h"
  18. #include "xfs_rmap.h"
  19. #include "xfs_alloc.h"
  20. #include "xfs_bit.h"
  21. #include <linux/fsmap.h>
  22. #include "xfs_fsmap.h"
  23. #include "xfs_refcount.h"
  24. #include "xfs_refcount_btree.h"
  25. #include "xfs_alloc_btree.h"
  26. #include "xfs_rtbitmap.h"
  27. #include "xfs_ag.h"
  28. /* Convert an xfs_fsmap to an fsmap. */
  29. static void
  30. xfs_fsmap_from_internal(
  31. struct fsmap *dest,
  32. struct xfs_fsmap *src)
  33. {
  34. dest->fmr_device = src->fmr_device;
  35. dest->fmr_flags = src->fmr_flags;
  36. dest->fmr_physical = BBTOB(src->fmr_physical);
  37. dest->fmr_owner = src->fmr_owner;
  38. dest->fmr_offset = BBTOB(src->fmr_offset);
  39. dest->fmr_length = BBTOB(src->fmr_length);
  40. dest->fmr_reserved[0] = 0;
  41. dest->fmr_reserved[1] = 0;
  42. dest->fmr_reserved[2] = 0;
  43. }
  44. /* Convert an fsmap to an xfs_fsmap. */
  45. static void
  46. xfs_fsmap_to_internal(
  47. struct xfs_fsmap *dest,
  48. struct fsmap *src)
  49. {
  50. dest->fmr_device = src->fmr_device;
  51. dest->fmr_flags = src->fmr_flags;
  52. dest->fmr_physical = BTOBBT(src->fmr_physical);
  53. dest->fmr_owner = src->fmr_owner;
  54. dest->fmr_offset = BTOBBT(src->fmr_offset);
  55. dest->fmr_length = BTOBBT(src->fmr_length);
  56. }
  57. /* Convert an fsmap owner into an rmapbt owner. */
  58. static int
  59. xfs_fsmap_owner_to_rmap(
  60. struct xfs_rmap_irec *dest,
  61. const struct xfs_fsmap *src)
  62. {
  63. if (!(src->fmr_flags & FMR_OF_SPECIAL_OWNER)) {
  64. dest->rm_owner = src->fmr_owner;
  65. return 0;
  66. }
  67. switch (src->fmr_owner) {
  68. case 0: /* "lowest owner id possible" */
  69. case -1ULL: /* "highest owner id possible" */
  70. dest->rm_owner = src->fmr_owner;
  71. break;
  72. case XFS_FMR_OWN_FREE:
  73. dest->rm_owner = XFS_RMAP_OWN_NULL;
  74. break;
  75. case XFS_FMR_OWN_UNKNOWN:
  76. dest->rm_owner = XFS_RMAP_OWN_UNKNOWN;
  77. break;
  78. case XFS_FMR_OWN_FS:
  79. dest->rm_owner = XFS_RMAP_OWN_FS;
  80. break;
  81. case XFS_FMR_OWN_LOG:
  82. dest->rm_owner = XFS_RMAP_OWN_LOG;
  83. break;
  84. case XFS_FMR_OWN_AG:
  85. dest->rm_owner = XFS_RMAP_OWN_AG;
  86. break;
  87. case XFS_FMR_OWN_INOBT:
  88. dest->rm_owner = XFS_RMAP_OWN_INOBT;
  89. break;
  90. case XFS_FMR_OWN_INODES:
  91. dest->rm_owner = XFS_RMAP_OWN_INODES;
  92. break;
  93. case XFS_FMR_OWN_REFC:
  94. dest->rm_owner = XFS_RMAP_OWN_REFC;
  95. break;
  96. case XFS_FMR_OWN_COW:
  97. dest->rm_owner = XFS_RMAP_OWN_COW;
  98. break;
  99. case XFS_FMR_OWN_DEFECTIVE: /* not implemented */
  100. /* fall through */
  101. default:
  102. return -EINVAL;
  103. }
  104. return 0;
  105. }
  106. /* Convert an rmapbt owner into an fsmap owner. */
  107. static int
  108. xfs_fsmap_owner_from_rmap(
  109. struct xfs_fsmap *dest,
  110. const struct xfs_rmap_irec *src)
  111. {
  112. dest->fmr_flags = 0;
  113. if (!XFS_RMAP_NON_INODE_OWNER(src->rm_owner)) {
  114. dest->fmr_owner = src->rm_owner;
  115. return 0;
  116. }
  117. dest->fmr_flags |= FMR_OF_SPECIAL_OWNER;
  118. switch (src->rm_owner) {
  119. case XFS_RMAP_OWN_FS:
  120. dest->fmr_owner = XFS_FMR_OWN_FS;
  121. break;
  122. case XFS_RMAP_OWN_LOG:
  123. dest->fmr_owner = XFS_FMR_OWN_LOG;
  124. break;
  125. case XFS_RMAP_OWN_AG:
  126. dest->fmr_owner = XFS_FMR_OWN_AG;
  127. break;
  128. case XFS_RMAP_OWN_INOBT:
  129. dest->fmr_owner = XFS_FMR_OWN_INOBT;
  130. break;
  131. case XFS_RMAP_OWN_INODES:
  132. dest->fmr_owner = XFS_FMR_OWN_INODES;
  133. break;
  134. case XFS_RMAP_OWN_REFC:
  135. dest->fmr_owner = XFS_FMR_OWN_REFC;
  136. break;
  137. case XFS_RMAP_OWN_COW:
  138. dest->fmr_owner = XFS_FMR_OWN_COW;
  139. break;
  140. case XFS_RMAP_OWN_NULL: /* "free" */
  141. dest->fmr_owner = XFS_FMR_OWN_FREE;
  142. break;
  143. default:
  144. ASSERT(0);
  145. return -EFSCORRUPTED;
  146. }
  147. return 0;
  148. }
  149. /* getfsmap query state */
  150. struct xfs_getfsmap_info {
  151. struct xfs_fsmap_head *head;
  152. struct fsmap *fsmap_recs; /* mapping records */
  153. struct xfs_buf *agf_bp; /* AGF, for refcount queries */
  154. struct xfs_perag *pag; /* AG info, if applicable */
  155. xfs_daddr_t next_daddr; /* next daddr we expect */
  156. /* daddr of low fsmap key when we're using the rtbitmap */
  157. xfs_daddr_t low_daddr;
  158. /* daddr of high fsmap key, or the last daddr on the device */
  159. xfs_daddr_t end_daddr;
  160. u64 missing_owner; /* owner of holes */
  161. u32 dev; /* device id */
  162. /*
  163. * Low rmap key for the query. If low.rm_blockcount is nonzero, this
  164. * is the second (or later) call to retrieve the recordset in pieces.
  165. * xfs_getfsmap_rec_before_start will compare all records retrieved
  166. * by the rmapbt query to filter out any records that start before
  167. * the last record.
  168. */
  169. struct xfs_rmap_irec low;
  170. struct xfs_rmap_irec high; /* high rmap key */
  171. bool last; /* last extent? */
  172. };
  173. /* Associate a device with a getfsmap handler. */
  174. struct xfs_getfsmap_dev {
  175. u32 dev;
  176. int (*fn)(struct xfs_trans *tp,
  177. const struct xfs_fsmap *keys,
  178. struct xfs_getfsmap_info *info);
  179. sector_t nr_sectors;
  180. };
  181. /* Compare two getfsmap device handlers. */
  182. static int
  183. xfs_getfsmap_dev_compare(
  184. const void *p1,
  185. const void *p2)
  186. {
  187. const struct xfs_getfsmap_dev *d1 = p1;
  188. const struct xfs_getfsmap_dev *d2 = p2;
  189. return d1->dev - d2->dev;
  190. }
  191. /* Decide if this mapping is shared. */
  192. STATIC int
  193. xfs_getfsmap_is_shared(
  194. struct xfs_trans *tp,
  195. struct xfs_getfsmap_info *info,
  196. const struct xfs_rmap_irec *rec,
  197. bool *stat)
  198. {
  199. struct xfs_mount *mp = tp->t_mountp;
  200. struct xfs_btree_cur *cur;
  201. xfs_agblock_t fbno;
  202. xfs_extlen_t flen;
  203. int error;
  204. *stat = false;
  205. if (!xfs_has_reflink(mp))
  206. return 0;
  207. /* rt files will have no perag structure */
  208. if (!info->pag)
  209. return 0;
  210. /* Are there any shared blocks here? */
  211. flen = 0;
  212. cur = xfs_refcountbt_init_cursor(mp, tp, info->agf_bp, info->pag);
  213. error = xfs_refcount_find_shared(cur, rec->rm_startblock,
  214. rec->rm_blockcount, &fbno, &flen, false);
  215. xfs_btree_del_cursor(cur, error);
  216. if (error)
  217. return error;
  218. *stat = flen > 0;
  219. return 0;
  220. }
  221. static inline void
  222. xfs_getfsmap_format(
  223. struct xfs_mount *mp,
  224. struct xfs_fsmap *xfm,
  225. struct xfs_getfsmap_info *info)
  226. {
  227. struct fsmap *rec;
  228. trace_xfs_getfsmap_mapping(mp, xfm);
  229. rec = &info->fsmap_recs[info->head->fmh_entries++];
  230. xfs_fsmap_from_internal(rec, xfm);
  231. }
  232. static inline bool
  233. xfs_getfsmap_rec_before_start(
  234. struct xfs_getfsmap_info *info,
  235. const struct xfs_rmap_irec *rec,
  236. xfs_daddr_t rec_daddr)
  237. {
  238. if (info->low_daddr != XFS_BUF_DADDR_NULL)
  239. return rec_daddr < info->low_daddr;
  240. if (info->low.rm_blockcount)
  241. return xfs_rmap_compare(rec, &info->low) < 0;
  242. return false;
  243. }
  244. /*
  245. * Format a reverse mapping for getfsmap, having translated rm_startblock
  246. * into the appropriate daddr units. Pass in a nonzero @len_daddr if the
  247. * length could be larger than rm_blockcount in struct xfs_rmap_irec.
  248. */
  249. STATIC int
  250. xfs_getfsmap_helper(
  251. struct xfs_trans *tp,
  252. struct xfs_getfsmap_info *info,
  253. const struct xfs_rmap_irec *rec,
  254. xfs_daddr_t rec_daddr,
  255. xfs_daddr_t len_daddr)
  256. {
  257. struct xfs_fsmap fmr;
  258. struct xfs_mount *mp = tp->t_mountp;
  259. bool shared;
  260. int error;
  261. if (fatal_signal_pending(current))
  262. return -EINTR;
  263. if (len_daddr == 0)
  264. len_daddr = XFS_FSB_TO_BB(mp, rec->rm_blockcount);
  265. /*
  266. * Filter out records that start before our startpoint, if the
  267. * caller requested that.
  268. */
  269. if (xfs_getfsmap_rec_before_start(info, rec, rec_daddr)) {
  270. rec_daddr += len_daddr;
  271. if (info->next_daddr < rec_daddr)
  272. info->next_daddr = rec_daddr;
  273. return 0;
  274. }
  275. /*
  276. * For an info->last query, we're looking for a gap between the last
  277. * mapping emitted and the high key specified by userspace. If the
  278. * user's query spans less than 1 fsblock, then info->high and
  279. * info->low will have the same rm_startblock, which causes rec_daddr
  280. * and next_daddr to be the same. Therefore, use the end_daddr that
  281. * we calculated from userspace's high key to synthesize the record.
  282. * Note that if the btree query found a mapping, there won't be a gap.
  283. */
  284. if (info->last && info->end_daddr != XFS_BUF_DADDR_NULL)
  285. rec_daddr = info->end_daddr + 1;
  286. /* Are we just counting mappings? */
  287. if (info->head->fmh_count == 0) {
  288. if (info->head->fmh_entries == UINT_MAX)
  289. return -ECANCELED;
  290. if (rec_daddr > info->next_daddr)
  291. info->head->fmh_entries++;
  292. if (info->last)
  293. return 0;
  294. info->head->fmh_entries++;
  295. rec_daddr += len_daddr;
  296. if (info->next_daddr < rec_daddr)
  297. info->next_daddr = rec_daddr;
  298. return 0;
  299. }
  300. /*
  301. * If the record starts past the last physical block we saw,
  302. * then we've found a gap. Report the gap as being owned by
  303. * whatever the caller specified is the missing owner.
  304. */
  305. if (rec_daddr > info->next_daddr) {
  306. if (info->head->fmh_entries >= info->head->fmh_count)
  307. return -ECANCELED;
  308. fmr.fmr_device = info->dev;
  309. fmr.fmr_physical = info->next_daddr;
  310. fmr.fmr_owner = info->missing_owner;
  311. fmr.fmr_offset = 0;
  312. fmr.fmr_length = rec_daddr - info->next_daddr;
  313. fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
  314. xfs_getfsmap_format(mp, &fmr, info);
  315. }
  316. if (info->last)
  317. goto out;
  318. /* Fill out the extent we found */
  319. if (info->head->fmh_entries >= info->head->fmh_count)
  320. return -ECANCELED;
  321. trace_xfs_fsmap_mapping(mp, info->dev,
  322. info->pag ? info->pag->pag_agno : NULLAGNUMBER, rec);
  323. fmr.fmr_device = info->dev;
  324. fmr.fmr_physical = rec_daddr;
  325. error = xfs_fsmap_owner_from_rmap(&fmr, rec);
  326. if (error)
  327. return error;
  328. fmr.fmr_offset = XFS_FSB_TO_BB(mp, rec->rm_offset);
  329. fmr.fmr_length = len_daddr;
  330. if (rec->rm_flags & XFS_RMAP_UNWRITTEN)
  331. fmr.fmr_flags |= FMR_OF_PREALLOC;
  332. if (rec->rm_flags & XFS_RMAP_ATTR_FORK)
  333. fmr.fmr_flags |= FMR_OF_ATTR_FORK;
  334. if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
  335. fmr.fmr_flags |= FMR_OF_EXTENT_MAP;
  336. if (fmr.fmr_flags == 0) {
  337. error = xfs_getfsmap_is_shared(tp, info, rec, &shared);
  338. if (error)
  339. return error;
  340. if (shared)
  341. fmr.fmr_flags |= FMR_OF_SHARED;
  342. }
  343. xfs_getfsmap_format(mp, &fmr, info);
  344. out:
  345. rec_daddr += len_daddr;
  346. if (info->next_daddr < rec_daddr)
  347. info->next_daddr = rec_daddr;
  348. return 0;
  349. }
  350. /* Transform a rmapbt irec into a fsmap */
  351. STATIC int
  352. xfs_getfsmap_datadev_helper(
  353. struct xfs_btree_cur *cur,
  354. const struct xfs_rmap_irec *rec,
  355. void *priv)
  356. {
  357. struct xfs_mount *mp = cur->bc_mp;
  358. struct xfs_getfsmap_info *info = priv;
  359. xfs_fsblock_t fsb;
  360. xfs_daddr_t rec_daddr;
  361. fsb = XFS_AGB_TO_FSB(mp, cur->bc_ag.pag->pag_agno, rec->rm_startblock);
  362. rec_daddr = XFS_FSB_TO_DADDR(mp, fsb);
  363. return xfs_getfsmap_helper(cur->bc_tp, info, rec, rec_daddr, 0);
  364. }
  365. /* Transform a bnobt irec into a fsmap */
  366. STATIC int
  367. xfs_getfsmap_datadev_bnobt_helper(
  368. struct xfs_btree_cur *cur,
  369. const struct xfs_alloc_rec_incore *rec,
  370. void *priv)
  371. {
  372. struct xfs_mount *mp = cur->bc_mp;
  373. struct xfs_getfsmap_info *info = priv;
  374. struct xfs_rmap_irec irec;
  375. xfs_daddr_t rec_daddr;
  376. rec_daddr = XFS_AGB_TO_DADDR(mp, cur->bc_ag.pag->pag_agno,
  377. rec->ar_startblock);
  378. irec.rm_startblock = rec->ar_startblock;
  379. irec.rm_blockcount = rec->ar_blockcount;
  380. irec.rm_owner = XFS_RMAP_OWN_NULL; /* "free" */
  381. irec.rm_offset = 0;
  382. irec.rm_flags = 0;
  383. return xfs_getfsmap_helper(cur->bc_tp, info, &irec, rec_daddr, 0);
  384. }
  385. /* Set rmap flags based on the getfsmap flags */
  386. static void
  387. xfs_getfsmap_set_irec_flags(
  388. struct xfs_rmap_irec *irec,
  389. const struct xfs_fsmap *fmr)
  390. {
  391. irec->rm_flags = 0;
  392. if (fmr->fmr_flags & FMR_OF_ATTR_FORK)
  393. irec->rm_flags |= XFS_RMAP_ATTR_FORK;
  394. if (fmr->fmr_flags & FMR_OF_EXTENT_MAP)
  395. irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
  396. if (fmr->fmr_flags & FMR_OF_PREALLOC)
  397. irec->rm_flags |= XFS_RMAP_UNWRITTEN;
  398. }
  399. static inline bool
  400. rmap_not_shareable(struct xfs_mount *mp, const struct xfs_rmap_irec *r)
  401. {
  402. if (!xfs_has_reflink(mp))
  403. return true;
  404. if (XFS_RMAP_NON_INODE_OWNER(r->rm_owner))
  405. return true;
  406. if (r->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK |
  407. XFS_RMAP_UNWRITTEN))
  408. return true;
  409. return false;
  410. }
  411. /* Execute a getfsmap query against the regular data device. */
  412. STATIC int
  413. __xfs_getfsmap_datadev(
  414. struct xfs_trans *tp,
  415. const struct xfs_fsmap *keys,
  416. struct xfs_getfsmap_info *info,
  417. int (*query_fn)(struct xfs_trans *,
  418. struct xfs_getfsmap_info *,
  419. struct xfs_btree_cur **,
  420. void *),
  421. void *priv)
  422. {
  423. struct xfs_mount *mp = tp->t_mountp;
  424. struct xfs_perag *pag;
  425. struct xfs_btree_cur *bt_cur = NULL;
  426. xfs_fsblock_t start_fsb;
  427. xfs_fsblock_t end_fsb;
  428. xfs_agnumber_t start_ag;
  429. xfs_agnumber_t end_ag;
  430. uint64_t eofs;
  431. int error = 0;
  432. eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  433. if (keys[0].fmr_physical >= eofs)
  434. return 0;
  435. start_fsb = XFS_DADDR_TO_FSB(mp, keys[0].fmr_physical);
  436. end_fsb = XFS_DADDR_TO_FSB(mp, min(eofs - 1, keys[1].fmr_physical));
  437. /*
  438. * Convert the fsmap low/high keys to AG based keys. Initialize
  439. * low to the fsmap low key and max out the high key to the end
  440. * of the AG.
  441. */
  442. info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
  443. error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
  444. if (error)
  445. return error;
  446. info->low.rm_blockcount = XFS_BB_TO_FSBT(mp, keys[0].fmr_length);
  447. xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
  448. /* Adjust the low key if we are continuing from where we left off. */
  449. if (info->low.rm_blockcount == 0) {
  450. /* No previous record from which to continue */
  451. } else if (rmap_not_shareable(mp, &info->low)) {
  452. /* Last record seen was an unshareable extent */
  453. info->low.rm_owner = 0;
  454. info->low.rm_offset = 0;
  455. start_fsb += info->low.rm_blockcount;
  456. if (XFS_FSB_TO_DADDR(mp, start_fsb) >= eofs)
  457. return 0;
  458. } else {
  459. /* Last record seen was a shareable file data extent */
  460. info->low.rm_offset += info->low.rm_blockcount;
  461. }
  462. info->low.rm_startblock = XFS_FSB_TO_AGBNO(mp, start_fsb);
  463. info->high.rm_startblock = -1U;
  464. info->high.rm_owner = ULLONG_MAX;
  465. info->high.rm_offset = ULLONG_MAX;
  466. info->high.rm_blockcount = 0;
  467. info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
  468. start_ag = XFS_FSB_TO_AGNO(mp, start_fsb);
  469. end_ag = XFS_FSB_TO_AGNO(mp, end_fsb);
  470. for_each_perag_range(mp, start_ag, end_ag, pag) {
  471. /*
  472. * Set the AG high key from the fsmap high key if this
  473. * is the last AG that we're querying.
  474. */
  475. info->pag = pag;
  476. if (pag->pag_agno == end_ag) {
  477. info->high.rm_startblock = XFS_FSB_TO_AGBNO(mp,
  478. end_fsb);
  479. info->high.rm_offset = XFS_BB_TO_FSBT(mp,
  480. keys[1].fmr_offset);
  481. error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
  482. if (error)
  483. break;
  484. xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
  485. }
  486. if (bt_cur) {
  487. xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
  488. bt_cur = NULL;
  489. xfs_trans_brelse(tp, info->agf_bp);
  490. info->agf_bp = NULL;
  491. }
  492. error = xfs_alloc_read_agf(pag, tp, 0, &info->agf_bp);
  493. if (error)
  494. break;
  495. trace_xfs_fsmap_low_key(mp, info->dev, pag->pag_agno,
  496. &info->low);
  497. trace_xfs_fsmap_high_key(mp, info->dev, pag->pag_agno,
  498. &info->high);
  499. error = query_fn(tp, info, &bt_cur, priv);
  500. if (error)
  501. break;
  502. /*
  503. * Set the AG low key to the start of the AG prior to
  504. * moving on to the next AG.
  505. */
  506. if (pag->pag_agno == start_ag)
  507. memset(&info->low, 0, sizeof(info->low));
  508. /*
  509. * If this is the last AG, report any gap at the end of it
  510. * before we drop the reference to the perag when the loop
  511. * terminates.
  512. */
  513. if (pag->pag_agno == end_ag) {
  514. info->last = true;
  515. error = query_fn(tp, info, &bt_cur, priv);
  516. if (error)
  517. break;
  518. }
  519. info->pag = NULL;
  520. }
  521. if (bt_cur)
  522. xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
  523. XFS_BTREE_NOERROR);
  524. if (info->agf_bp) {
  525. xfs_trans_brelse(tp, info->agf_bp);
  526. info->agf_bp = NULL;
  527. }
  528. if (info->pag) {
  529. xfs_perag_rele(info->pag);
  530. info->pag = NULL;
  531. } else if (pag) {
  532. /* loop termination case */
  533. xfs_perag_rele(pag);
  534. }
  535. return error;
  536. }
  537. /* Actually query the rmap btree. */
  538. STATIC int
  539. xfs_getfsmap_datadev_rmapbt_query(
  540. struct xfs_trans *tp,
  541. struct xfs_getfsmap_info *info,
  542. struct xfs_btree_cur **curpp,
  543. void *priv)
  544. {
  545. /* Report any gap at the end of the last AG. */
  546. if (info->last)
  547. return xfs_getfsmap_datadev_helper(*curpp, &info->high, info);
  548. /* Allocate cursor for this AG and query_range it. */
  549. *curpp = xfs_rmapbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
  550. info->pag);
  551. return xfs_rmap_query_range(*curpp, &info->low, &info->high,
  552. xfs_getfsmap_datadev_helper, info);
  553. }
  554. /* Execute a getfsmap query against the regular data device rmapbt. */
  555. STATIC int
  556. xfs_getfsmap_datadev_rmapbt(
  557. struct xfs_trans *tp,
  558. const struct xfs_fsmap *keys,
  559. struct xfs_getfsmap_info *info)
  560. {
  561. info->missing_owner = XFS_FMR_OWN_FREE;
  562. return __xfs_getfsmap_datadev(tp, keys, info,
  563. xfs_getfsmap_datadev_rmapbt_query, NULL);
  564. }
  565. /* Actually query the bno btree. */
  566. STATIC int
  567. xfs_getfsmap_datadev_bnobt_query(
  568. struct xfs_trans *tp,
  569. struct xfs_getfsmap_info *info,
  570. struct xfs_btree_cur **curpp,
  571. void *priv)
  572. {
  573. struct xfs_alloc_rec_incore *key = priv;
  574. /* Report any gap at the end of the last AG. */
  575. if (info->last)
  576. return xfs_getfsmap_datadev_bnobt_helper(*curpp, &key[1], info);
  577. /* Allocate cursor for this AG and query_range it. */
  578. *curpp = xfs_bnobt_init_cursor(tp->t_mountp, tp, info->agf_bp,
  579. info->pag);
  580. key->ar_startblock = info->low.rm_startblock;
  581. key[1].ar_startblock = info->high.rm_startblock;
  582. return xfs_alloc_query_range(*curpp, key, &key[1],
  583. xfs_getfsmap_datadev_bnobt_helper, info);
  584. }
  585. /* Execute a getfsmap query against the regular data device's bnobt. */
  586. STATIC int
  587. xfs_getfsmap_datadev_bnobt(
  588. struct xfs_trans *tp,
  589. const struct xfs_fsmap *keys,
  590. struct xfs_getfsmap_info *info)
  591. {
  592. struct xfs_alloc_rec_incore akeys[2];
  593. memset(akeys, 0, sizeof(akeys));
  594. info->missing_owner = XFS_FMR_OWN_UNKNOWN;
  595. return __xfs_getfsmap_datadev(tp, keys, info,
  596. xfs_getfsmap_datadev_bnobt_query, &akeys[0]);
  597. }
  598. /* Execute a getfsmap query against the log device. */
  599. STATIC int
  600. xfs_getfsmap_logdev(
  601. struct xfs_trans *tp,
  602. const struct xfs_fsmap *keys,
  603. struct xfs_getfsmap_info *info)
  604. {
  605. struct xfs_mount *mp = tp->t_mountp;
  606. struct xfs_rmap_irec rmap;
  607. xfs_daddr_t rec_daddr, len_daddr;
  608. xfs_fsblock_t start_fsb, end_fsb;
  609. uint64_t eofs;
  610. eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
  611. if (keys[0].fmr_physical >= eofs)
  612. return 0;
  613. start_fsb = XFS_BB_TO_FSBT(mp,
  614. keys[0].fmr_physical + keys[0].fmr_length);
  615. end_fsb = XFS_BB_TO_FSB(mp, min(eofs - 1, keys[1].fmr_physical));
  616. /* Adjust the low key if we are continuing from where we left off. */
  617. if (keys[0].fmr_length > 0)
  618. info->low_daddr = XFS_FSB_TO_BB(mp, start_fsb);
  619. trace_xfs_fsmap_low_key_linear(mp, info->dev, start_fsb);
  620. trace_xfs_fsmap_high_key_linear(mp, info->dev, end_fsb);
  621. if (start_fsb > 0)
  622. return 0;
  623. /* Fabricate an rmap entry for the external log device. */
  624. rmap.rm_startblock = 0;
  625. rmap.rm_blockcount = mp->m_sb.sb_logblocks;
  626. rmap.rm_owner = XFS_RMAP_OWN_LOG;
  627. rmap.rm_offset = 0;
  628. rmap.rm_flags = 0;
  629. rec_daddr = XFS_FSB_TO_BB(mp, rmap.rm_startblock);
  630. len_daddr = XFS_FSB_TO_BB(mp, rmap.rm_blockcount);
  631. return xfs_getfsmap_helper(tp, info, &rmap, rec_daddr, len_daddr);
  632. }
  633. #ifdef CONFIG_XFS_RT
  634. /* Transform a rtbitmap "record" into a fsmap */
  635. STATIC int
  636. xfs_getfsmap_rtdev_rtbitmap_helper(
  637. struct xfs_mount *mp,
  638. struct xfs_trans *tp,
  639. const struct xfs_rtalloc_rec *rec,
  640. void *priv)
  641. {
  642. struct xfs_getfsmap_info *info = priv;
  643. struct xfs_rmap_irec irec;
  644. xfs_rtblock_t rtbno;
  645. xfs_daddr_t rec_daddr, len_daddr;
  646. rtbno = xfs_rtx_to_rtb(mp, rec->ar_startext);
  647. rec_daddr = XFS_FSB_TO_BB(mp, rtbno);
  648. irec.rm_startblock = rtbno;
  649. rtbno = xfs_rtx_to_rtb(mp, rec->ar_extcount);
  650. len_daddr = XFS_FSB_TO_BB(mp, rtbno);
  651. irec.rm_blockcount = rtbno;
  652. irec.rm_owner = XFS_RMAP_OWN_NULL; /* "free" */
  653. irec.rm_offset = 0;
  654. irec.rm_flags = 0;
  655. return xfs_getfsmap_helper(tp, info, &irec, rec_daddr, len_daddr);
  656. }
  657. /* Execute a getfsmap query against the realtime device rtbitmap. */
  658. STATIC int
  659. xfs_getfsmap_rtdev_rtbitmap(
  660. struct xfs_trans *tp,
  661. const struct xfs_fsmap *keys,
  662. struct xfs_getfsmap_info *info)
  663. {
  664. struct xfs_rtalloc_rec ahigh = { 0 };
  665. struct xfs_mount *mp = tp->t_mountp;
  666. xfs_rtblock_t start_rtb;
  667. xfs_rtblock_t end_rtb;
  668. xfs_rtxnum_t high;
  669. uint64_t eofs;
  670. int error;
  671. eofs = XFS_FSB_TO_BB(mp, xfs_rtx_to_rtb(mp, mp->m_sb.sb_rextents));
  672. if (keys[0].fmr_physical >= eofs)
  673. return 0;
  674. start_rtb = XFS_BB_TO_FSBT(mp,
  675. keys[0].fmr_physical + keys[0].fmr_length);
  676. end_rtb = XFS_BB_TO_FSB(mp, min(eofs - 1, keys[1].fmr_physical));
  677. info->missing_owner = XFS_FMR_OWN_UNKNOWN;
  678. /* Adjust the low key if we are continuing from where we left off. */
  679. if (keys[0].fmr_length > 0) {
  680. info->low_daddr = XFS_FSB_TO_BB(mp, start_rtb);
  681. if (info->low_daddr >= eofs)
  682. return 0;
  683. }
  684. trace_xfs_fsmap_low_key_linear(mp, info->dev, start_rtb);
  685. trace_xfs_fsmap_high_key_linear(mp, info->dev, end_rtb);
  686. xfs_rtbitmap_lock_shared(mp, XFS_RBMLOCK_BITMAP);
  687. /*
  688. * Set up query parameters to return free rtextents covering the range
  689. * we want.
  690. */
  691. high = xfs_rtb_to_rtxup(mp, end_rtb);
  692. error = xfs_rtalloc_query_range(mp, tp, xfs_rtb_to_rtx(mp, start_rtb),
  693. high, xfs_getfsmap_rtdev_rtbitmap_helper, info);
  694. if (error)
  695. goto err;
  696. /*
  697. * Report any gaps at the end of the rtbitmap by simulating a null
  698. * rmap starting at the block after the end of the query range.
  699. */
  700. info->last = true;
  701. ahigh.ar_startext = min(mp->m_sb.sb_rextents, high);
  702. error = xfs_getfsmap_rtdev_rtbitmap_helper(mp, tp, &ahigh, info);
  703. if (error)
  704. goto err;
  705. err:
  706. xfs_rtbitmap_unlock_shared(mp, XFS_RBMLOCK_BITMAP);
  707. return error;
  708. }
  709. #endif /* CONFIG_XFS_RT */
  710. /* Do we recognize the device? */
  711. STATIC bool
  712. xfs_getfsmap_is_valid_device(
  713. struct xfs_mount *mp,
  714. struct xfs_fsmap *fm)
  715. {
  716. if (fm->fmr_device == 0 || fm->fmr_device == UINT_MAX ||
  717. fm->fmr_device == new_encode_dev(mp->m_ddev_targp->bt_dev))
  718. return true;
  719. if (mp->m_logdev_targp &&
  720. fm->fmr_device == new_encode_dev(mp->m_logdev_targp->bt_dev))
  721. return true;
  722. if (mp->m_rtdev_targp &&
  723. fm->fmr_device == new_encode_dev(mp->m_rtdev_targp->bt_dev))
  724. return true;
  725. return false;
  726. }
  727. /* Ensure that the low key is less than the high key. */
  728. STATIC bool
  729. xfs_getfsmap_check_keys(
  730. struct xfs_fsmap *low_key,
  731. struct xfs_fsmap *high_key)
  732. {
  733. if (low_key->fmr_flags & (FMR_OF_SPECIAL_OWNER | FMR_OF_EXTENT_MAP)) {
  734. if (low_key->fmr_offset)
  735. return false;
  736. }
  737. if (high_key->fmr_flags != -1U &&
  738. (high_key->fmr_flags & (FMR_OF_SPECIAL_OWNER |
  739. FMR_OF_EXTENT_MAP))) {
  740. if (high_key->fmr_offset && high_key->fmr_offset != -1ULL)
  741. return false;
  742. }
  743. if (high_key->fmr_length && high_key->fmr_length != -1ULL)
  744. return false;
  745. if (low_key->fmr_device > high_key->fmr_device)
  746. return false;
  747. if (low_key->fmr_device < high_key->fmr_device)
  748. return true;
  749. if (low_key->fmr_physical > high_key->fmr_physical)
  750. return false;
  751. if (low_key->fmr_physical < high_key->fmr_physical)
  752. return true;
  753. if (low_key->fmr_owner > high_key->fmr_owner)
  754. return false;
  755. if (low_key->fmr_owner < high_key->fmr_owner)
  756. return true;
  757. if (low_key->fmr_offset > high_key->fmr_offset)
  758. return false;
  759. if (low_key->fmr_offset < high_key->fmr_offset)
  760. return true;
  761. return false;
  762. }
  763. /*
  764. * There are only two devices if we didn't configure RT devices at build time.
  765. */
  766. #ifdef CONFIG_XFS_RT
  767. #define XFS_GETFSMAP_DEVS 3
  768. #else
  769. #define XFS_GETFSMAP_DEVS 2
  770. #endif /* CONFIG_XFS_RT */
  771. /*
  772. * Get filesystem's extents as described in head, and format for output. Fills
  773. * in the supplied records array until there are no more reverse mappings to
  774. * return or head.fmh_entries == head.fmh_count. In the second case, this
  775. * function returns -ECANCELED to indicate that more records would have been
  776. * returned.
  777. *
  778. * Key to Confusion
  779. * ----------------
  780. * There are multiple levels of keys and counters at work here:
  781. * xfs_fsmap_head.fmh_keys -- low and high fsmap keys passed in;
  782. * these reflect fs-wide sector addrs.
  783. * dkeys -- fmh_keys used to query each device;
  784. * these are fmh_keys but w/ the low key
  785. * bumped up by fmr_length.
  786. * xfs_getfsmap_info.next_daddr -- next disk addr we expect to see; this
  787. * is how we detect gaps in the fsmap
  788. records and report them.
  789. * xfs_getfsmap_info.low/high -- per-AG low/high keys computed from
  790. * dkeys; used to query the metadata.
  791. */
  792. STATIC int
  793. xfs_getfsmap(
  794. struct xfs_mount *mp,
  795. struct xfs_fsmap_head *head,
  796. struct fsmap *fsmap_recs)
  797. {
  798. struct xfs_trans *tp = NULL;
  799. struct xfs_fsmap dkeys[2]; /* per-dev keys */
  800. struct xfs_getfsmap_dev handlers[XFS_GETFSMAP_DEVS];
  801. struct xfs_getfsmap_info info = {
  802. .fsmap_recs = fsmap_recs,
  803. .head = head,
  804. };
  805. bool use_rmap;
  806. int i;
  807. int error = 0;
  808. if (head->fmh_iflags & ~FMH_IF_VALID)
  809. return -EINVAL;
  810. if (!xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[0]) ||
  811. !xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[1]))
  812. return -EINVAL;
  813. if (!xfs_getfsmap_check_keys(&head->fmh_keys[0], &head->fmh_keys[1]))
  814. return -EINVAL;
  815. use_rmap = xfs_has_rmapbt(mp) &&
  816. has_capability_noaudit(current, CAP_SYS_ADMIN);
  817. head->fmh_entries = 0;
  818. /* Set up our device handlers. */
  819. memset(handlers, 0, sizeof(handlers));
  820. handlers[0].nr_sectors = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  821. handlers[0].dev = new_encode_dev(mp->m_ddev_targp->bt_dev);
  822. if (use_rmap)
  823. handlers[0].fn = xfs_getfsmap_datadev_rmapbt;
  824. else
  825. handlers[0].fn = xfs_getfsmap_datadev_bnobt;
  826. if (mp->m_logdev_targp != mp->m_ddev_targp) {
  827. handlers[1].nr_sectors = XFS_FSB_TO_BB(mp,
  828. mp->m_sb.sb_logblocks);
  829. handlers[1].dev = new_encode_dev(mp->m_logdev_targp->bt_dev);
  830. handlers[1].fn = xfs_getfsmap_logdev;
  831. }
  832. #ifdef CONFIG_XFS_RT
  833. if (mp->m_rtdev_targp) {
  834. handlers[2].nr_sectors = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
  835. handlers[2].dev = new_encode_dev(mp->m_rtdev_targp->bt_dev);
  836. handlers[2].fn = xfs_getfsmap_rtdev_rtbitmap;
  837. }
  838. #endif /* CONFIG_XFS_RT */
  839. xfs_sort(handlers, XFS_GETFSMAP_DEVS, sizeof(struct xfs_getfsmap_dev),
  840. xfs_getfsmap_dev_compare);
  841. /*
  842. * To continue where we left off, we allow userspace to use the
  843. * last mapping from a previous call as the low key of the next.
  844. * This is identified by a non-zero length in the low key. We
  845. * have to increment the low key in this scenario to ensure we
  846. * don't return the same mapping again, and instead return the
  847. * very next mapping.
  848. *
  849. * If the low key mapping refers to file data, the same physical
  850. * blocks could be mapped to several other files/offsets.
  851. * According to rmapbt record ordering, the minimal next
  852. * possible record for the block range is the next starting
  853. * offset in the same inode. Therefore, each fsmap backend bumps
  854. * the file offset to continue the search appropriately. For
  855. * all other low key mapping types (attr blocks, metadata), each
  856. * fsmap backend bumps the physical offset as there can be no
  857. * other mapping for the same physical block range.
  858. */
  859. dkeys[0] = head->fmh_keys[0];
  860. memset(&dkeys[1], 0xFF, sizeof(struct xfs_fsmap));
  861. info.next_daddr = head->fmh_keys[0].fmr_physical +
  862. head->fmh_keys[0].fmr_length;
  863. /* For each device we support... */
  864. for (i = 0; i < XFS_GETFSMAP_DEVS; i++) {
  865. /* Is this device within the range the user asked for? */
  866. if (!handlers[i].fn)
  867. continue;
  868. if (head->fmh_keys[0].fmr_device > handlers[i].dev)
  869. continue;
  870. if (head->fmh_keys[1].fmr_device < handlers[i].dev)
  871. break;
  872. /*
  873. * If this device number matches the high key, we have to pass
  874. * the high key to the handler to limit the query results, and
  875. * set the end_daddr so that we can synthesize records at the
  876. * end of the query range or device.
  877. */
  878. if (handlers[i].dev == head->fmh_keys[1].fmr_device) {
  879. dkeys[1] = head->fmh_keys[1];
  880. info.end_daddr = min(handlers[i].nr_sectors - 1,
  881. dkeys[1].fmr_physical);
  882. } else {
  883. info.end_daddr = handlers[i].nr_sectors - 1;
  884. }
  885. /*
  886. * If the device number exceeds the low key, zero out the low
  887. * key so that we get everything from the beginning.
  888. */
  889. if (handlers[i].dev > head->fmh_keys[0].fmr_device)
  890. memset(&dkeys[0], 0, sizeof(struct xfs_fsmap));
  891. /*
  892. * Grab an empty transaction so that we can use its recursive
  893. * buffer locking abilities to detect cycles in the rmapbt
  894. * without deadlocking.
  895. */
  896. error = xfs_trans_alloc_empty(mp, &tp);
  897. if (error)
  898. break;
  899. info.dev = handlers[i].dev;
  900. info.last = false;
  901. info.pag = NULL;
  902. info.low_daddr = XFS_BUF_DADDR_NULL;
  903. info.low.rm_blockcount = 0;
  904. error = handlers[i].fn(tp, dkeys, &info);
  905. if (error)
  906. break;
  907. xfs_trans_cancel(tp);
  908. tp = NULL;
  909. info.next_daddr = 0;
  910. }
  911. if (tp)
  912. xfs_trans_cancel(tp);
  913. head->fmh_oflags = FMH_OF_DEV_T;
  914. return error;
  915. }
  916. int
  917. xfs_ioc_getfsmap(
  918. struct xfs_inode *ip,
  919. struct fsmap_head __user *arg)
  920. {
  921. struct xfs_fsmap_head xhead = {0};
  922. struct fsmap_head head;
  923. struct fsmap *recs;
  924. unsigned int count;
  925. __u32 last_flags = 0;
  926. bool done = false;
  927. int error;
  928. if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
  929. return -EFAULT;
  930. if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
  931. memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
  932. sizeof(head.fmh_keys[0].fmr_reserved)) ||
  933. memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
  934. sizeof(head.fmh_keys[1].fmr_reserved)))
  935. return -EINVAL;
  936. /*
  937. * Use an internal memory buffer so that we don't have to copy fsmap
  938. * data to userspace while holding locks. Start by trying to allocate
  939. * up to 128k for the buffer, but fall back to a single page if needed.
  940. */
  941. count = min_t(unsigned int, head.fmh_count,
  942. 131072 / sizeof(struct fsmap));
  943. recs = kvcalloc(count, sizeof(struct fsmap), GFP_KERNEL);
  944. if (!recs) {
  945. count = min_t(unsigned int, head.fmh_count,
  946. PAGE_SIZE / sizeof(struct fsmap));
  947. recs = kvcalloc(count, sizeof(struct fsmap), GFP_KERNEL);
  948. if (!recs)
  949. return -ENOMEM;
  950. }
  951. xhead.fmh_iflags = head.fmh_iflags;
  952. xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
  953. xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
  954. trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
  955. trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
  956. head.fmh_entries = 0;
  957. do {
  958. struct fsmap __user *user_recs;
  959. struct fsmap *last_rec;
  960. user_recs = &arg->fmh_recs[head.fmh_entries];
  961. xhead.fmh_entries = 0;
  962. xhead.fmh_count = min_t(unsigned int, count,
  963. head.fmh_count - head.fmh_entries);
  964. /* Run query, record how many entries we got. */
  965. error = xfs_getfsmap(ip->i_mount, &xhead, recs);
  966. switch (error) {
  967. case 0:
  968. /*
  969. * There are no more records in the result set. Copy
  970. * whatever we got to userspace and break out.
  971. */
  972. done = true;
  973. break;
  974. case -ECANCELED:
  975. /*
  976. * The internal memory buffer is full. Copy whatever
  977. * records we got to userspace and go again if we have
  978. * not yet filled the userspace buffer.
  979. */
  980. error = 0;
  981. break;
  982. default:
  983. goto out_free;
  984. }
  985. head.fmh_entries += xhead.fmh_entries;
  986. head.fmh_oflags = xhead.fmh_oflags;
  987. /*
  988. * If the caller wanted a record count or there aren't any
  989. * new records to return, we're done.
  990. */
  991. if (head.fmh_count == 0 || xhead.fmh_entries == 0)
  992. break;
  993. /* Copy all the records we got out to userspace. */
  994. if (copy_to_user(user_recs, recs,
  995. xhead.fmh_entries * sizeof(struct fsmap))) {
  996. error = -EFAULT;
  997. goto out_free;
  998. }
  999. /* Remember the last record flags we copied to userspace. */
  1000. last_rec = &recs[xhead.fmh_entries - 1];
  1001. last_flags = last_rec->fmr_flags;
  1002. /* Set up the low key for the next iteration. */
  1003. xfs_fsmap_to_internal(&xhead.fmh_keys[0], last_rec);
  1004. trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
  1005. } while (!done && head.fmh_entries < head.fmh_count);
  1006. /*
  1007. * If there are no more records in the query result set and we're not
  1008. * in counting mode, mark the last record returned with the LAST flag.
  1009. */
  1010. if (done && head.fmh_count > 0 && head.fmh_entries > 0) {
  1011. struct fsmap __user *user_rec;
  1012. last_flags |= FMR_OF_LAST;
  1013. user_rec = &arg->fmh_recs[head.fmh_entries - 1];
  1014. if (copy_to_user(&user_rec->fmr_flags, &last_flags,
  1015. sizeof(last_flags))) {
  1016. error = -EFAULT;
  1017. goto out_free;
  1018. }
  1019. }
  1020. /* copy back header */
  1021. if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) {
  1022. error = -EFAULT;
  1023. goto out_free;
  1024. }
  1025. out_free:
  1026. kvfree(recs);
  1027. return error;
  1028. }