xfs_rmap_item.c 14 KB

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