xfs_extfree_item.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_buf_item.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_dir.h"
  27. #include "xfs_dmapi.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_trans_priv.h"
  30. #include "xfs_extfree_item.h"
  31. kmem_zone_t *xfs_efi_zone;
  32. kmem_zone_t *xfs_efd_zone;
  33. STATIC void xfs_efi_item_unlock(xfs_efi_log_item_t *);
  34. STATIC void xfs_efi_item_abort(xfs_efi_log_item_t *);
  35. STATIC void xfs_efd_item_abort(xfs_efd_log_item_t *);
  36. void
  37. xfs_efi_item_free(xfs_efi_log_item_t *efip)
  38. {
  39. int nexts = efip->efi_format.efi_nextents;
  40. if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
  41. kmem_free(efip, sizeof(xfs_efi_log_item_t) +
  42. (nexts - 1) * sizeof(xfs_extent_t));
  43. } else {
  44. kmem_zone_free(xfs_efi_zone, efip);
  45. }
  46. }
  47. /*
  48. * This returns the number of iovecs needed to log the given efi item.
  49. * We only need 1 iovec for an efi item. It just logs the efi_log_format
  50. * structure.
  51. */
  52. /*ARGSUSED*/
  53. STATIC uint
  54. xfs_efi_item_size(xfs_efi_log_item_t *efip)
  55. {
  56. return 1;
  57. }
  58. /*
  59. * This is called to fill in the vector of log iovecs for the
  60. * given efi log item. We use only 1 iovec, and we point that
  61. * at the efi_log_format structure embedded in the efi item.
  62. * It is at this point that we assert that all of the extent
  63. * slots in the efi item have been filled.
  64. */
  65. STATIC void
  66. xfs_efi_item_format(xfs_efi_log_item_t *efip,
  67. xfs_log_iovec_t *log_vector)
  68. {
  69. uint size;
  70. ASSERT(efip->efi_next_extent == efip->efi_format.efi_nextents);
  71. efip->efi_format.efi_type = XFS_LI_EFI;
  72. size = sizeof(xfs_efi_log_format_t);
  73. size += (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
  74. efip->efi_format.efi_size = 1;
  75. log_vector->i_addr = (xfs_caddr_t)&(efip->efi_format);
  76. log_vector->i_len = size;
  77. XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFI_FORMAT);
  78. ASSERT(size >= sizeof(xfs_efi_log_format_t));
  79. }
  80. /*
  81. * Pinning has no meaning for an efi item, so just return.
  82. */
  83. /*ARGSUSED*/
  84. STATIC void
  85. xfs_efi_item_pin(xfs_efi_log_item_t *efip)
  86. {
  87. return;
  88. }
  89. /*
  90. * While EFIs cannot really be pinned, the unpin operation is the
  91. * last place at which the EFI is manipulated during a transaction.
  92. * Here we coordinate with xfs_efi_cancel() to determine who gets to
  93. * free the EFI.
  94. */
  95. /*ARGSUSED*/
  96. STATIC void
  97. xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
  98. {
  99. xfs_mount_t *mp;
  100. SPLDECL(s);
  101. mp = efip->efi_item.li_mountp;
  102. AIL_LOCK(mp, s);
  103. if (efip->efi_flags & XFS_EFI_CANCELED) {
  104. /*
  105. * xfs_trans_delete_ail() drops the AIL lock.
  106. */
  107. xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
  108. xfs_efi_item_free(efip);
  109. } else {
  110. efip->efi_flags |= XFS_EFI_COMMITTED;
  111. AIL_UNLOCK(mp, s);
  112. }
  113. }
  114. /*
  115. * like unpin only we have to also clear the xaction descriptor
  116. * pointing the log item if we free the item. This routine duplicates
  117. * unpin because efi_flags is protected by the AIL lock. Freeing
  118. * the descriptor and then calling unpin would force us to drop the AIL
  119. * lock which would open up a race condition.
  120. */
  121. STATIC void
  122. xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp)
  123. {
  124. xfs_mount_t *mp;
  125. xfs_log_item_desc_t *lidp;
  126. SPLDECL(s);
  127. mp = efip->efi_item.li_mountp;
  128. AIL_LOCK(mp, s);
  129. if (efip->efi_flags & XFS_EFI_CANCELED) {
  130. /*
  131. * free the xaction descriptor pointing to this item
  132. */
  133. lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip);
  134. xfs_trans_free_item(tp, lidp);
  135. /*
  136. * pull the item off the AIL.
  137. * xfs_trans_delete_ail() drops the AIL lock.
  138. */
  139. xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
  140. xfs_efi_item_free(efip);
  141. } else {
  142. efip->efi_flags |= XFS_EFI_COMMITTED;
  143. AIL_UNLOCK(mp, s);
  144. }
  145. }
  146. /*
  147. * Efi items have no locking or pushing. However, since EFIs are
  148. * pulled from the AIL when their corresponding EFDs are committed
  149. * to disk, their situation is very similar to being pinned. Return
  150. * XFS_ITEM_PINNED so that the caller will eventually flush the log.
  151. * This should help in getting the EFI out of the AIL.
  152. */
  153. /*ARGSUSED*/
  154. STATIC uint
  155. xfs_efi_item_trylock(xfs_efi_log_item_t *efip)
  156. {
  157. return XFS_ITEM_PINNED;
  158. }
  159. /*
  160. * Efi items have no locking, so just return.
  161. */
  162. /*ARGSUSED*/
  163. STATIC void
  164. xfs_efi_item_unlock(xfs_efi_log_item_t *efip)
  165. {
  166. if (efip->efi_item.li_flags & XFS_LI_ABORTED)
  167. xfs_efi_item_abort(efip);
  168. return;
  169. }
  170. /*
  171. * The EFI is logged only once and cannot be moved in the log, so
  172. * simply return the lsn at which it's been logged. The canceled
  173. * flag is not paid any attention here. Checking for that is delayed
  174. * until the EFI is unpinned.
  175. */
  176. /*ARGSUSED*/
  177. STATIC xfs_lsn_t
  178. xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
  179. {
  180. return lsn;
  181. }
  182. /*
  183. * This is called when the transaction logging the EFI is aborted.
  184. * Free up the EFI and return. No need to clean up the slot for
  185. * the item in the transaction. That was done by the unpin code
  186. * which is called prior to this routine in the abort/fs-shutdown path.
  187. */
  188. STATIC void
  189. xfs_efi_item_abort(xfs_efi_log_item_t *efip)
  190. {
  191. xfs_efi_item_free(efip);
  192. }
  193. /*
  194. * There isn't much you can do to push on an efi item. It is simply
  195. * stuck waiting for all of its corresponding efd items to be
  196. * committed to disk.
  197. */
  198. /*ARGSUSED*/
  199. STATIC void
  200. xfs_efi_item_push(xfs_efi_log_item_t *efip)
  201. {
  202. return;
  203. }
  204. /*
  205. * The EFI dependency tracking op doesn't do squat. It can't because
  206. * it doesn't know where the free extent is coming from. The dependency
  207. * tracking has to be handled by the "enclosing" metadata object. For
  208. * example, for inodes, the inode is locked throughout the extent freeing
  209. * so the dependency should be recorded there.
  210. */
  211. /*ARGSUSED*/
  212. STATIC void
  213. xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
  214. {
  215. return;
  216. }
  217. /*
  218. * This is the ops vector shared by all efi log items.
  219. */
  220. STATIC struct xfs_item_ops xfs_efi_item_ops = {
  221. .iop_size = (uint(*)(xfs_log_item_t*))xfs_efi_item_size,
  222. .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
  223. xfs_efi_item_format,
  224. .iop_pin = (void(*)(xfs_log_item_t*))xfs_efi_item_pin,
  225. .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin,
  226. .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
  227. xfs_efi_item_unpin_remove,
  228. .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock,
  229. .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efi_item_unlock,
  230. .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
  231. xfs_efi_item_committed,
  232. .iop_push = (void(*)(xfs_log_item_t*))xfs_efi_item_push,
  233. .iop_abort = (void(*)(xfs_log_item_t*))xfs_efi_item_abort,
  234. .iop_pushbuf = NULL,
  235. .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
  236. xfs_efi_item_committing
  237. };
  238. /*
  239. * Allocate and initialize an efi item with the given number of extents.
  240. */
  241. xfs_efi_log_item_t *
  242. xfs_efi_init(xfs_mount_t *mp,
  243. uint nextents)
  244. {
  245. xfs_efi_log_item_t *efip;
  246. uint size;
  247. ASSERT(nextents > 0);
  248. if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
  249. size = (uint)(sizeof(xfs_efi_log_item_t) +
  250. ((nextents - 1) * sizeof(xfs_extent_t)));
  251. efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP);
  252. } else {
  253. efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
  254. KM_SLEEP);
  255. }
  256. efip->efi_item.li_type = XFS_LI_EFI;
  257. efip->efi_item.li_ops = &xfs_efi_item_ops;
  258. efip->efi_item.li_mountp = mp;
  259. efip->efi_format.efi_nextents = nextents;
  260. efip->efi_format.efi_id = (__psint_t)(void*)efip;
  261. return (efip);
  262. }
  263. /*
  264. * This is called by the efd item code below to release references to
  265. * the given efi item. Each efd calls this with the number of
  266. * extents that it has logged, and when the sum of these reaches
  267. * the total number of extents logged by this efi item we can free
  268. * the efi item.
  269. *
  270. * Freeing the efi item requires that we remove it from the AIL.
  271. * We'll use the AIL lock to protect our counters as well as
  272. * the removal from the AIL.
  273. */
  274. void
  275. xfs_efi_release(xfs_efi_log_item_t *efip,
  276. uint nextents)
  277. {
  278. xfs_mount_t *mp;
  279. int extents_left;
  280. SPLDECL(s);
  281. mp = efip->efi_item.li_mountp;
  282. ASSERT(efip->efi_next_extent > 0);
  283. ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
  284. AIL_LOCK(mp, s);
  285. ASSERT(efip->efi_next_extent >= nextents);
  286. efip->efi_next_extent -= nextents;
  287. extents_left = efip->efi_next_extent;
  288. if (extents_left == 0) {
  289. /*
  290. * xfs_trans_delete_ail() drops the AIL lock.
  291. */
  292. xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
  293. xfs_efi_item_free(efip);
  294. } else {
  295. AIL_UNLOCK(mp, s);
  296. }
  297. }
  298. /*
  299. * This is called when the transaction that should be committing the
  300. * EFD corresponding to the given EFI is aborted. The committed and
  301. * canceled flags are used to coordinate the freeing of the EFI and
  302. * the references by the transaction that committed it.
  303. */
  304. STATIC void
  305. xfs_efi_cancel(
  306. xfs_efi_log_item_t *efip)
  307. {
  308. xfs_mount_t *mp;
  309. SPLDECL(s);
  310. mp = efip->efi_item.li_mountp;
  311. AIL_LOCK(mp, s);
  312. if (efip->efi_flags & XFS_EFI_COMMITTED) {
  313. /*
  314. * xfs_trans_delete_ail() drops the AIL lock.
  315. */
  316. xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
  317. xfs_efi_item_free(efip);
  318. } else {
  319. efip->efi_flags |= XFS_EFI_CANCELED;
  320. AIL_UNLOCK(mp, s);
  321. }
  322. }
  323. STATIC void
  324. xfs_efd_item_free(xfs_efd_log_item_t *efdp)
  325. {
  326. int nexts = efdp->efd_format.efd_nextents;
  327. if (nexts > XFS_EFD_MAX_FAST_EXTENTS) {
  328. kmem_free(efdp, sizeof(xfs_efd_log_item_t) +
  329. (nexts - 1) * sizeof(xfs_extent_t));
  330. } else {
  331. kmem_zone_free(xfs_efd_zone, efdp);
  332. }
  333. }
  334. /*
  335. * This returns the number of iovecs needed to log the given efd item.
  336. * We only need 1 iovec for an efd item. It just logs the efd_log_format
  337. * structure.
  338. */
  339. /*ARGSUSED*/
  340. STATIC uint
  341. xfs_efd_item_size(xfs_efd_log_item_t *efdp)
  342. {
  343. return 1;
  344. }
  345. /*
  346. * This is called to fill in the vector of log iovecs for the
  347. * given efd log item. We use only 1 iovec, and we point that
  348. * at the efd_log_format structure embedded in the efd item.
  349. * It is at this point that we assert that all of the extent
  350. * slots in the efd item have been filled.
  351. */
  352. STATIC void
  353. xfs_efd_item_format(xfs_efd_log_item_t *efdp,
  354. xfs_log_iovec_t *log_vector)
  355. {
  356. uint size;
  357. ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
  358. efdp->efd_format.efd_type = XFS_LI_EFD;
  359. size = sizeof(xfs_efd_log_format_t);
  360. size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
  361. efdp->efd_format.efd_size = 1;
  362. log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format);
  363. log_vector->i_len = size;
  364. XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFD_FORMAT);
  365. ASSERT(size >= sizeof(xfs_efd_log_format_t));
  366. }
  367. /*
  368. * Pinning has no meaning for an efd item, so just return.
  369. */
  370. /*ARGSUSED*/
  371. STATIC void
  372. xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
  373. {
  374. return;
  375. }
  376. /*
  377. * Since pinning has no meaning for an efd item, unpinning does
  378. * not either.
  379. */
  380. /*ARGSUSED*/
  381. STATIC void
  382. xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
  383. {
  384. return;
  385. }
  386. /*ARGSUSED*/
  387. STATIC void
  388. xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
  389. {
  390. return;
  391. }
  392. /*
  393. * Efd items have no locking, so just return success.
  394. */
  395. /*ARGSUSED*/
  396. STATIC uint
  397. xfs_efd_item_trylock(xfs_efd_log_item_t *efdp)
  398. {
  399. return XFS_ITEM_LOCKED;
  400. }
  401. /*
  402. * Efd items have no locking or pushing, so return failure
  403. * so that the caller doesn't bother with us.
  404. */
  405. /*ARGSUSED*/
  406. STATIC void
  407. xfs_efd_item_unlock(xfs_efd_log_item_t *efdp)
  408. {
  409. if (efdp->efd_item.li_flags & XFS_LI_ABORTED)
  410. xfs_efd_item_abort(efdp);
  411. return;
  412. }
  413. /*
  414. * When the efd item is committed to disk, all we need to do
  415. * is delete our reference to our partner efi item and then
  416. * free ourselves. Since we're freeing ourselves we must
  417. * return -1 to keep the transaction code from further referencing
  418. * this item.
  419. */
  420. /*ARGSUSED*/
  421. STATIC xfs_lsn_t
  422. xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn)
  423. {
  424. /*
  425. * If we got a log I/O error, it's always the case that the LR with the
  426. * EFI got unpinned and freed before the EFD got aborted.
  427. */
  428. if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
  429. xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
  430. xfs_efd_item_free(efdp);
  431. return (xfs_lsn_t)-1;
  432. }
  433. /*
  434. * The transaction of which this EFD is a part has been aborted.
  435. * Inform its companion EFI of this fact and then clean up after
  436. * ourselves. No need to clean up the slot for the item in the
  437. * transaction. That was done by the unpin code which is called
  438. * prior to this routine in the abort/fs-shutdown path.
  439. */
  440. STATIC void
  441. xfs_efd_item_abort(xfs_efd_log_item_t *efdp)
  442. {
  443. /*
  444. * If we got a log I/O error, it's always the case that the LR with the
  445. * EFI got unpinned and freed before the EFD got aborted. So don't
  446. * reference the EFI at all in that case.
  447. */
  448. if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
  449. xfs_efi_cancel(efdp->efd_efip);
  450. xfs_efd_item_free(efdp);
  451. }
  452. /*
  453. * There isn't much you can do to push on an efd item. It is simply
  454. * stuck waiting for the log to be flushed to disk.
  455. */
  456. /*ARGSUSED*/
  457. STATIC void
  458. xfs_efd_item_push(xfs_efd_log_item_t *efdp)
  459. {
  460. return;
  461. }
  462. /*
  463. * The EFD dependency tracking op doesn't do squat. It can't because
  464. * it doesn't know where the free extent is coming from. The dependency
  465. * tracking has to be handled by the "enclosing" metadata object. For
  466. * example, for inodes, the inode is locked throughout the extent freeing
  467. * so the dependency should be recorded there.
  468. */
  469. /*ARGSUSED*/
  470. STATIC void
  471. xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
  472. {
  473. return;
  474. }
  475. /*
  476. * This is the ops vector shared by all efd log items.
  477. */
  478. STATIC struct xfs_item_ops xfs_efd_item_ops = {
  479. .iop_size = (uint(*)(xfs_log_item_t*))xfs_efd_item_size,
  480. .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
  481. xfs_efd_item_format,
  482. .iop_pin = (void(*)(xfs_log_item_t*))xfs_efd_item_pin,
  483. .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin,
  484. .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
  485. xfs_efd_item_unpin_remove,
  486. .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock,
  487. .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efd_item_unlock,
  488. .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
  489. xfs_efd_item_committed,
  490. .iop_push = (void(*)(xfs_log_item_t*))xfs_efd_item_push,
  491. .iop_abort = (void(*)(xfs_log_item_t*))xfs_efd_item_abort,
  492. .iop_pushbuf = NULL,
  493. .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
  494. xfs_efd_item_committing
  495. };
  496. /*
  497. * Allocate and initialize an efd item with the given number of extents.
  498. */
  499. xfs_efd_log_item_t *
  500. xfs_efd_init(xfs_mount_t *mp,
  501. xfs_efi_log_item_t *efip,
  502. uint nextents)
  503. {
  504. xfs_efd_log_item_t *efdp;
  505. uint size;
  506. ASSERT(nextents > 0);
  507. if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
  508. size = (uint)(sizeof(xfs_efd_log_item_t) +
  509. ((nextents - 1) * sizeof(xfs_extent_t)));
  510. efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP);
  511. } else {
  512. efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
  513. KM_SLEEP);
  514. }
  515. efdp->efd_item.li_type = XFS_LI_EFD;
  516. efdp->efd_item.li_ops = &xfs_efd_item_ops;
  517. efdp->efd_item.li_mountp = mp;
  518. efdp->efd_efip = efip;
  519. efdp->efd_format.efd_nextents = nextents;
  520. efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
  521. return (efdp);
  522. }