xfs_rmap_item.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Copyright (C) 2016 Oracle. All Rights Reserved.
  3. *
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it would be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write the Free Software Foundation,
  18. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "xfs.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_bit.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_defer.h"
  28. #include "xfs_trans.h"
  29. #include "xfs_trans_priv.h"
  30. #include "xfs_buf_item.h"
  31. #include "xfs_rmap_item.h"
  32. #include "xfs_log.h"
  33. #include "xfs_rmap.h"
  34. kmem_zone_t *xfs_rui_zone;
  35. kmem_zone_t *xfs_rud_zone;
  36. static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
  37. {
  38. return container_of(lip, struct xfs_rui_log_item, rui_item);
  39. }
  40. void
  41. xfs_rui_item_free(
  42. struct xfs_rui_log_item *ruip)
  43. {
  44. if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
  45. kmem_free(ruip);
  46. else
  47. kmem_zone_free(xfs_rui_zone, ruip);
  48. }
  49. STATIC void
  50. xfs_rui_item_size(
  51. struct xfs_log_item *lip,
  52. int *nvecs,
  53. int *nbytes)
  54. {
  55. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  56. *nvecs += 1;
  57. *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
  58. }
  59. /*
  60. * This is called to fill in the vector of log iovecs for the
  61. * given rui log item. We use only 1 iovec, and we point that
  62. * at the rui_log_format structure embedded in the rui item.
  63. * It is at this point that we assert that all of the extent
  64. * slots in the rui item have been filled.
  65. */
  66. STATIC void
  67. xfs_rui_item_format(
  68. struct xfs_log_item *lip,
  69. struct xfs_log_vec *lv)
  70. {
  71. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  72. struct xfs_log_iovec *vecp = NULL;
  73. ASSERT(atomic_read(&ruip->rui_next_extent) ==
  74. ruip->rui_format.rui_nextents);
  75. ruip->rui_format.rui_type = XFS_LI_RUI;
  76. ruip->rui_format.rui_size = 1;
  77. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
  78. xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
  79. }
  80. /*
  81. * Pinning has no meaning for an rui item, so just return.
  82. */
  83. STATIC void
  84. xfs_rui_item_pin(
  85. struct xfs_log_item *lip)
  86. {
  87. }
  88. /*
  89. * The unpin operation is the last place an RUI is manipulated in the log. It is
  90. * either inserted in the AIL or aborted in the event of a log I/O error. In
  91. * either case, the RUI transaction has been successfully committed to make it
  92. * this far. Therefore, we expect whoever committed the RUI to either construct
  93. * and commit the RUD or drop the RUD's reference in the event of error. Simply
  94. * drop the log's RUI reference now that the log is done with it.
  95. */
  96. STATIC void
  97. xfs_rui_item_unpin(
  98. struct xfs_log_item *lip,
  99. int remove)
  100. {
  101. struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
  102. xfs_rui_release(ruip);
  103. }
  104. /*
  105. * RUI items have no locking or pushing. However, since RUIs are pulled from
  106. * the AIL when their corresponding RUDs are committed to disk, their situation
  107. * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
  108. * will eventually flush the log. This should help in getting the RUI out of
  109. * the AIL.
  110. */
  111. STATIC uint
  112. xfs_rui_item_push(
  113. struct xfs_log_item *lip,
  114. struct list_head *buffer_list)
  115. {
  116. return XFS_ITEM_PINNED;
  117. }
  118. /*
  119. * The RUI has been either committed or aborted if the transaction has been
  120. * cancelled. If the transaction was cancelled, an RUD isn't going to be
  121. * constructed and thus we free the RUI here directly.
  122. */
  123. STATIC void
  124. xfs_rui_item_unlock(
  125. struct xfs_log_item *lip)
  126. {
  127. if (lip->li_flags & XFS_LI_ABORTED)
  128. xfs_rui_item_free(RUI_ITEM(lip));
  129. }
  130. /*
  131. * The RUI is logged only once and cannot be moved in the log, so simply return
  132. * the lsn at which it's been logged.
  133. */
  134. STATIC xfs_lsn_t
  135. xfs_rui_item_committed(
  136. struct xfs_log_item *lip,
  137. xfs_lsn_t lsn)
  138. {
  139. return lsn;
  140. }
  141. /*
  142. * The RUI dependency tracking op doesn't do squat. It can't because
  143. * it doesn't know where the free extent is coming from. The dependency
  144. * tracking has to be handled by the "enclosing" metadata object. For
  145. * example, for inodes, the inode is locked throughout the extent freeing
  146. * so the dependency should be recorded there.
  147. */
  148. STATIC void
  149. xfs_rui_item_committing(
  150. struct xfs_log_item *lip,
  151. xfs_lsn_t lsn)
  152. {
  153. }
  154. /*
  155. * This is the ops vector shared by all rui log items.
  156. */
  157. static const struct xfs_item_ops xfs_rui_item_ops = {
  158. .iop_size = xfs_rui_item_size,
  159. .iop_format = xfs_rui_item_format,
  160. .iop_pin = xfs_rui_item_pin,
  161. .iop_unpin = xfs_rui_item_unpin,
  162. .iop_unlock = xfs_rui_item_unlock,
  163. .iop_committed = xfs_rui_item_committed,
  164. .iop_push = xfs_rui_item_push,
  165. .iop_committing = xfs_rui_item_committing,
  166. };
  167. /*
  168. * Allocate and initialize an rui item with the given number of extents.
  169. */
  170. struct xfs_rui_log_item *
  171. xfs_rui_init(
  172. struct xfs_mount *mp,
  173. uint nextents)
  174. {
  175. struct xfs_rui_log_item *ruip;
  176. ASSERT(nextents > 0);
  177. if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
  178. ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), KM_SLEEP);
  179. else
  180. ruip = kmem_zone_zalloc(xfs_rui_zone, KM_SLEEP);
  181. xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
  182. ruip->rui_format.rui_nextents = nextents;
  183. ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
  184. atomic_set(&ruip->rui_next_extent, 0);
  185. atomic_set(&ruip->rui_refcount, 2);
  186. return ruip;
  187. }
  188. /*
  189. * Copy an RUI format buffer from the given buf, and into the destination
  190. * RUI format structure. The RUI/RUD items were designed not to need any
  191. * special alignment handling.
  192. */
  193. int
  194. xfs_rui_copy_format(
  195. struct xfs_log_iovec *buf,
  196. struct xfs_rui_log_format *dst_rui_fmt)
  197. {
  198. struct xfs_rui_log_format *src_rui_fmt;
  199. uint len;
  200. src_rui_fmt = buf->i_addr;
  201. len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
  202. if (buf->i_len != len)
  203. return -EFSCORRUPTED;
  204. memcpy(dst_rui_fmt, src_rui_fmt, len);
  205. return 0;
  206. }
  207. /*
  208. * Freeing the RUI requires that we remove it from the AIL if it has already
  209. * been placed there. However, the RUI may not yet have been placed in the AIL
  210. * when called by xfs_rui_release() from RUD processing due to the ordering of
  211. * committed vs unpin operations in bulk insert operations. Hence the reference
  212. * count to ensure only the last caller frees the RUI.
  213. */
  214. void
  215. xfs_rui_release(
  216. struct xfs_rui_log_item *ruip)
  217. {
  218. if (atomic_dec_and_test(&ruip->rui_refcount)) {
  219. xfs_trans_ail_remove(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR);
  220. xfs_rui_item_free(ruip);
  221. }
  222. }
  223. static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
  224. {
  225. return container_of(lip, struct xfs_rud_log_item, rud_item);
  226. }
  227. STATIC void
  228. xfs_rud_item_size(
  229. struct xfs_log_item *lip,
  230. int *nvecs,
  231. int *nbytes)
  232. {
  233. *nvecs += 1;
  234. *nbytes += sizeof(struct xfs_rud_log_format);
  235. }
  236. /*
  237. * This is called to fill in the vector of log iovecs for the
  238. * given rud log item. We use only 1 iovec, and we point that
  239. * at the rud_log_format structure embedded in the rud item.
  240. * It is at this point that we assert that all of the extent
  241. * slots in the rud item have been filled.
  242. */
  243. STATIC void
  244. xfs_rud_item_format(
  245. struct xfs_log_item *lip,
  246. struct xfs_log_vec *lv)
  247. {
  248. struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
  249. struct xfs_log_iovec *vecp = NULL;
  250. rudp->rud_format.rud_type = XFS_LI_RUD;
  251. rudp->rud_format.rud_size = 1;
  252. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
  253. sizeof(struct xfs_rud_log_format));
  254. }
  255. /*
  256. * Pinning has no meaning for an rud item, so just return.
  257. */
  258. STATIC void
  259. xfs_rud_item_pin(
  260. struct xfs_log_item *lip)
  261. {
  262. }
  263. /*
  264. * Since pinning has no meaning for an rud item, unpinning does
  265. * not either.
  266. */
  267. STATIC void
  268. xfs_rud_item_unpin(
  269. struct xfs_log_item *lip,
  270. int remove)
  271. {
  272. }
  273. /*
  274. * There isn't much you can do to push on an rud item. It is simply stuck
  275. * waiting for the log to be flushed to disk.
  276. */
  277. STATIC uint
  278. xfs_rud_item_push(
  279. struct xfs_log_item *lip,
  280. struct list_head *buffer_list)
  281. {
  282. return XFS_ITEM_PINNED;
  283. }
  284. /*
  285. * The RUD is either committed or aborted if the transaction is cancelled. If
  286. * the transaction is cancelled, drop our reference to the RUI and free the
  287. * RUD.
  288. */
  289. STATIC void
  290. xfs_rud_item_unlock(
  291. struct xfs_log_item *lip)
  292. {
  293. struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
  294. if (lip->li_flags & XFS_LI_ABORTED) {
  295. xfs_rui_release(rudp->rud_ruip);
  296. kmem_zone_free(xfs_rud_zone, rudp);
  297. }
  298. }
  299. /*
  300. * When the rud item is committed to disk, all we need to do is delete our
  301. * reference to our partner rui item and then free ourselves. Since we're
  302. * freeing ourselves we must return -1 to keep the transaction code from
  303. * further referencing this item.
  304. */
  305. STATIC xfs_lsn_t
  306. xfs_rud_item_committed(
  307. struct xfs_log_item *lip,
  308. xfs_lsn_t lsn)
  309. {
  310. struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
  311. /*
  312. * Drop the RUI reference regardless of whether the RUD has been
  313. * aborted. Once the RUD transaction is constructed, it is the sole
  314. * responsibility of the RUD to release the RUI (even if the RUI is
  315. * aborted due to log I/O error).
  316. */
  317. xfs_rui_release(rudp->rud_ruip);
  318. kmem_zone_free(xfs_rud_zone, rudp);
  319. return (xfs_lsn_t)-1;
  320. }
  321. /*
  322. * The RUD dependency tracking op doesn't do squat. It can't because
  323. * it doesn't know where the free extent is coming from. The dependency
  324. * tracking has to be handled by the "enclosing" metadata object. For
  325. * example, for inodes, the inode is locked throughout the extent freeing
  326. * so the dependency should be recorded there.
  327. */
  328. STATIC void
  329. xfs_rud_item_committing(
  330. struct xfs_log_item *lip,
  331. xfs_lsn_t lsn)
  332. {
  333. }
  334. /*
  335. * This is the ops vector shared by all rud log items.
  336. */
  337. static const struct xfs_item_ops xfs_rud_item_ops = {
  338. .iop_size = xfs_rud_item_size,
  339. .iop_format = xfs_rud_item_format,
  340. .iop_pin = xfs_rud_item_pin,
  341. .iop_unpin = xfs_rud_item_unpin,
  342. .iop_unlock = xfs_rud_item_unlock,
  343. .iop_committed = xfs_rud_item_committed,
  344. .iop_push = xfs_rud_item_push,
  345. .iop_committing = xfs_rud_item_committing,
  346. };
  347. /*
  348. * Allocate and initialize an rud item with the given number of extents.
  349. */
  350. struct xfs_rud_log_item *
  351. xfs_rud_init(
  352. struct xfs_mount *mp,
  353. struct xfs_rui_log_item *ruip)
  354. {
  355. struct xfs_rud_log_item *rudp;
  356. rudp = kmem_zone_zalloc(xfs_rud_zone, KM_SLEEP);
  357. xfs_log_item_init(mp, &rudp->rud_item, XFS_LI_RUD, &xfs_rud_item_ops);
  358. rudp->rud_ruip = ruip;
  359. rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
  360. return rudp;
  361. }
  362. /*
  363. * Process an rmap update intent item that was recovered from the log.
  364. * We need to update the rmapbt.
  365. */
  366. int
  367. xfs_rui_recover(
  368. struct xfs_mount *mp,
  369. struct xfs_rui_log_item *ruip)
  370. {
  371. int i;
  372. int error = 0;
  373. struct xfs_map_extent *rmap;
  374. xfs_fsblock_t startblock_fsb;
  375. bool op_ok;
  376. struct xfs_rud_log_item *rudp;
  377. enum xfs_rmap_intent_type type;
  378. int whichfork;
  379. xfs_exntst_t state;
  380. struct xfs_trans *tp;
  381. struct xfs_btree_cur *rcur = NULL;
  382. ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags));
  383. /*
  384. * First check the validity of the extents described by the
  385. * RUI. If any are bad, then assume that all are bad and
  386. * just toss the RUI.
  387. */
  388. for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
  389. rmap = &ruip->rui_format.rui_extents[i];
  390. startblock_fsb = XFS_BB_TO_FSB(mp,
  391. XFS_FSB_TO_DADDR(mp, rmap->me_startblock));
  392. switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
  393. case XFS_RMAP_EXTENT_MAP:
  394. case XFS_RMAP_EXTENT_MAP_SHARED:
  395. case XFS_RMAP_EXTENT_UNMAP:
  396. case XFS_RMAP_EXTENT_UNMAP_SHARED:
  397. case XFS_RMAP_EXTENT_CONVERT:
  398. case XFS_RMAP_EXTENT_CONVERT_SHARED:
  399. case XFS_RMAP_EXTENT_ALLOC:
  400. case XFS_RMAP_EXTENT_FREE:
  401. op_ok = true;
  402. break;
  403. default:
  404. op_ok = false;
  405. break;
  406. }
  407. if (!op_ok || startblock_fsb == 0 ||
  408. rmap->me_len == 0 ||
  409. startblock_fsb >= mp->m_sb.sb_dblocks ||
  410. rmap->me_len >= mp->m_sb.sb_agblocks ||
  411. (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) {
  412. /*
  413. * This will pull the RUI from the AIL and
  414. * free the memory associated with it.
  415. */
  416. set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
  417. xfs_rui_release(ruip);
  418. return -EIO;
  419. }
  420. }
  421. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  422. if (error)
  423. return error;
  424. rudp = xfs_trans_get_rud(tp, ruip);
  425. for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
  426. rmap = &ruip->rui_format.rui_extents[i];
  427. state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
  428. XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
  429. whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
  430. XFS_ATTR_FORK : XFS_DATA_FORK;
  431. switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
  432. case XFS_RMAP_EXTENT_MAP:
  433. type = XFS_RMAP_MAP;
  434. break;
  435. case XFS_RMAP_EXTENT_MAP_SHARED:
  436. type = XFS_RMAP_MAP_SHARED;
  437. break;
  438. case XFS_RMAP_EXTENT_UNMAP:
  439. type = XFS_RMAP_UNMAP;
  440. break;
  441. case XFS_RMAP_EXTENT_UNMAP_SHARED:
  442. type = XFS_RMAP_UNMAP_SHARED;
  443. break;
  444. case XFS_RMAP_EXTENT_CONVERT:
  445. type = XFS_RMAP_CONVERT;
  446. break;
  447. case XFS_RMAP_EXTENT_CONVERT_SHARED:
  448. type = XFS_RMAP_CONVERT_SHARED;
  449. break;
  450. case XFS_RMAP_EXTENT_ALLOC:
  451. type = XFS_RMAP_ALLOC;
  452. break;
  453. case XFS_RMAP_EXTENT_FREE:
  454. type = XFS_RMAP_FREE;
  455. break;
  456. default:
  457. error = -EFSCORRUPTED;
  458. goto abort_error;
  459. }
  460. error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
  461. rmap->me_owner, whichfork,
  462. rmap->me_startoff, rmap->me_startblock,
  463. rmap->me_len, state, &rcur);
  464. if (error)
  465. goto abort_error;
  466. }
  467. xfs_rmap_finish_one_cleanup(tp, rcur, error);
  468. set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
  469. error = xfs_trans_commit(tp);
  470. return error;
  471. abort_error:
  472. xfs_rmap_finish_one_cleanup(tp, rcur, error);
  473. xfs_trans_cancel(tp);
  474. return error;
  475. }