xfs_extfree_item.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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_ag.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. void
  35. xfs_efi_item_free(xfs_efi_log_item_t *efip)
  36. {
  37. int nexts = efip->efi_format.efi_nextents;
  38. if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
  39. kmem_free(efip);
  40. } else {
  41. kmem_zone_free(xfs_efi_zone, efip);
  42. }
  43. }
  44. /*
  45. * This returns the number of iovecs needed to log the given efi item.
  46. * We only need 1 iovec for an efi item. It just logs the efi_log_format
  47. * structure.
  48. */
  49. /*ARGSUSED*/
  50. STATIC uint
  51. xfs_efi_item_size(xfs_efi_log_item_t *efip)
  52. {
  53. return 1;
  54. }
  55. /*
  56. * This is called to fill in the vector of log iovecs for the
  57. * given efi log item. We use only 1 iovec, and we point that
  58. * at the efi_log_format structure embedded in the efi item.
  59. * It is at this point that we assert that all of the extent
  60. * slots in the efi item have been filled.
  61. */
  62. STATIC void
  63. xfs_efi_item_format(xfs_efi_log_item_t *efip,
  64. xfs_log_iovec_t *log_vector)
  65. {
  66. uint size;
  67. ASSERT(efip->efi_next_extent == efip->efi_format.efi_nextents);
  68. efip->efi_format.efi_type = XFS_LI_EFI;
  69. size = sizeof(xfs_efi_log_format_t);
  70. size += (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
  71. efip->efi_format.efi_size = 1;
  72. log_vector->i_addr = (xfs_caddr_t)&(efip->efi_format);
  73. log_vector->i_len = size;
  74. XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFI_FORMAT);
  75. ASSERT(size >= sizeof(xfs_efi_log_format_t));
  76. }
  77. /*
  78. * Pinning has no meaning for an efi item, so just return.
  79. */
  80. /*ARGSUSED*/
  81. STATIC void
  82. xfs_efi_item_pin(xfs_efi_log_item_t *efip)
  83. {
  84. return;
  85. }
  86. /*
  87. * While EFIs cannot really be pinned, the unpin operation is the
  88. * last place at which the EFI is manipulated during a transaction.
  89. * Here we coordinate with xfs_efi_cancel() to determine who gets to
  90. * free the EFI.
  91. */
  92. /*ARGSUSED*/
  93. STATIC void
  94. xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
  95. {
  96. struct xfs_ail *ailp = efip->efi_item.li_ailp;
  97. spin_lock(&ailp->xa_lock);
  98. if (efip->efi_flags & XFS_EFI_CANCELED) {
  99. /* xfs_trans_ail_delete() drops the AIL lock. */
  100. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)efip);
  101. xfs_efi_item_free(efip);
  102. } else {
  103. efip->efi_flags |= XFS_EFI_COMMITTED;
  104. spin_unlock(&ailp->xa_lock);
  105. }
  106. }
  107. /*
  108. * like unpin only we have to also clear the xaction descriptor
  109. * pointing the log item if we free the item. This routine duplicates
  110. * unpin because efi_flags is protected by the AIL lock. Freeing
  111. * the descriptor and then calling unpin would force us to drop the AIL
  112. * lock which would open up a race condition.
  113. */
  114. STATIC void
  115. xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp)
  116. {
  117. struct xfs_ail *ailp = efip->efi_item.li_ailp;
  118. xfs_log_item_desc_t *lidp;
  119. spin_lock(&ailp->xa_lock);
  120. if (efip->efi_flags & XFS_EFI_CANCELED) {
  121. /*
  122. * free the xaction descriptor pointing to this item
  123. */
  124. lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip);
  125. xfs_trans_free_item(tp, lidp);
  126. /* xfs_trans_ail_delete() drops the AIL lock. */
  127. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)efip);
  128. xfs_efi_item_free(efip);
  129. } else {
  130. efip->efi_flags |= XFS_EFI_COMMITTED;
  131. spin_unlock(&ailp->xa_lock);
  132. }
  133. }
  134. /*
  135. * Efi items have no locking or pushing. However, since EFIs are
  136. * pulled from the AIL when their corresponding EFDs are committed
  137. * to disk, their situation is very similar to being pinned. Return
  138. * XFS_ITEM_PINNED so that the caller will eventually flush the log.
  139. * This should help in getting the EFI out of the AIL.
  140. */
  141. /*ARGSUSED*/
  142. STATIC uint
  143. xfs_efi_item_trylock(xfs_efi_log_item_t *efip)
  144. {
  145. return XFS_ITEM_PINNED;
  146. }
  147. /*
  148. * Efi items have no locking, so just return.
  149. */
  150. /*ARGSUSED*/
  151. STATIC void
  152. xfs_efi_item_unlock(xfs_efi_log_item_t *efip)
  153. {
  154. if (efip->efi_item.li_flags & XFS_LI_ABORTED)
  155. xfs_efi_item_free(efip);
  156. return;
  157. }
  158. /*
  159. * The EFI is logged only once and cannot be moved in the log, so
  160. * simply return the lsn at which it's been logged. The canceled
  161. * flag is not paid any attention here. Checking for that is delayed
  162. * until the EFI is unpinned.
  163. */
  164. /*ARGSUSED*/
  165. STATIC xfs_lsn_t
  166. xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
  167. {
  168. return lsn;
  169. }
  170. /*
  171. * There isn't much you can do to push on an efi item. It is simply
  172. * stuck waiting for all of its corresponding efd items to be
  173. * committed to disk.
  174. */
  175. /*ARGSUSED*/
  176. STATIC void
  177. xfs_efi_item_push(xfs_efi_log_item_t *efip)
  178. {
  179. return;
  180. }
  181. /*
  182. * The EFI dependency tracking op doesn't do squat. It can't because
  183. * it doesn't know where the free extent is coming from. The dependency
  184. * tracking has to be handled by the "enclosing" metadata object. For
  185. * example, for inodes, the inode is locked throughout the extent freeing
  186. * so the dependency should be recorded there.
  187. */
  188. /*ARGSUSED*/
  189. STATIC void
  190. xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
  191. {
  192. return;
  193. }
  194. /*
  195. * This is the ops vector shared by all efi log items.
  196. */
  197. static struct xfs_item_ops xfs_efi_item_ops = {
  198. .iop_size = (uint(*)(xfs_log_item_t*))xfs_efi_item_size,
  199. .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
  200. xfs_efi_item_format,
  201. .iop_pin = (void(*)(xfs_log_item_t*))xfs_efi_item_pin,
  202. .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin,
  203. .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
  204. xfs_efi_item_unpin_remove,
  205. .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock,
  206. .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efi_item_unlock,
  207. .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
  208. xfs_efi_item_committed,
  209. .iop_push = (void(*)(xfs_log_item_t*))xfs_efi_item_push,
  210. .iop_pushbuf = NULL,
  211. .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
  212. xfs_efi_item_committing
  213. };
  214. /*
  215. * Allocate and initialize an efi item with the given number of extents.
  216. */
  217. xfs_efi_log_item_t *
  218. xfs_efi_init(xfs_mount_t *mp,
  219. uint nextents)
  220. {
  221. xfs_efi_log_item_t *efip;
  222. uint size;
  223. ASSERT(nextents > 0);
  224. if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
  225. size = (uint)(sizeof(xfs_efi_log_item_t) +
  226. ((nextents - 1) * sizeof(xfs_extent_t)));
  227. efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP);
  228. } else {
  229. efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
  230. KM_SLEEP);
  231. }
  232. efip->efi_item.li_type = XFS_LI_EFI;
  233. efip->efi_item.li_ops = &xfs_efi_item_ops;
  234. efip->efi_item.li_mountp = mp;
  235. efip->efi_item.li_ailp = mp->m_ail;
  236. efip->efi_format.efi_nextents = nextents;
  237. efip->efi_format.efi_id = (__psint_t)(void*)efip;
  238. return (efip);
  239. }
  240. /*
  241. * Copy an EFI format buffer from the given buf, and into the destination
  242. * EFI format structure.
  243. * The given buffer can be in 32 bit or 64 bit form (which has different padding),
  244. * one of which will be the native format for this kernel.
  245. * It will handle the conversion of formats if necessary.
  246. */
  247. int
  248. xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
  249. {
  250. xfs_efi_log_format_t *src_efi_fmt = (xfs_efi_log_format_t *)buf->i_addr;
  251. uint i;
  252. uint len = sizeof(xfs_efi_log_format_t) +
  253. (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
  254. uint len32 = sizeof(xfs_efi_log_format_32_t) +
  255. (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
  256. uint len64 = sizeof(xfs_efi_log_format_64_t) +
  257. (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
  258. if (buf->i_len == len) {
  259. memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
  260. return 0;
  261. } else if (buf->i_len == len32) {
  262. xfs_efi_log_format_32_t *src_efi_fmt_32 =
  263. (xfs_efi_log_format_32_t *)buf->i_addr;
  264. dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
  265. dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
  266. dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
  267. dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
  268. for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
  269. dst_efi_fmt->efi_extents[i].ext_start =
  270. src_efi_fmt_32->efi_extents[i].ext_start;
  271. dst_efi_fmt->efi_extents[i].ext_len =
  272. src_efi_fmt_32->efi_extents[i].ext_len;
  273. }
  274. return 0;
  275. } else if (buf->i_len == len64) {
  276. xfs_efi_log_format_64_t *src_efi_fmt_64 =
  277. (xfs_efi_log_format_64_t *)buf->i_addr;
  278. dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
  279. dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
  280. dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
  281. dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
  282. for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
  283. dst_efi_fmt->efi_extents[i].ext_start =
  284. src_efi_fmt_64->efi_extents[i].ext_start;
  285. dst_efi_fmt->efi_extents[i].ext_len =
  286. src_efi_fmt_64->efi_extents[i].ext_len;
  287. }
  288. return 0;
  289. }
  290. return EFSCORRUPTED;
  291. }
  292. /*
  293. * This is called by the efd item code below to release references to
  294. * the given efi item. Each efd calls this with the number of
  295. * extents that it has logged, and when the sum of these reaches
  296. * the total number of extents logged by this efi item we can free
  297. * the efi item.
  298. *
  299. * Freeing the efi item requires that we remove it from the AIL.
  300. * We'll use the AIL lock to protect our counters as well as
  301. * the removal from the AIL.
  302. */
  303. void
  304. xfs_efi_release(xfs_efi_log_item_t *efip,
  305. uint nextents)
  306. {
  307. struct xfs_ail *ailp = efip->efi_item.li_ailp;
  308. int extents_left;
  309. ASSERT(efip->efi_next_extent > 0);
  310. ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
  311. spin_lock(&ailp->xa_lock);
  312. ASSERT(efip->efi_next_extent >= nextents);
  313. efip->efi_next_extent -= nextents;
  314. extents_left = efip->efi_next_extent;
  315. if (extents_left == 0) {
  316. /* xfs_trans_ail_delete() drops the AIL lock. */
  317. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)efip);
  318. xfs_efi_item_free(efip);
  319. } else {
  320. spin_unlock(&ailp->xa_lock);
  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);
  329. } else {
  330. kmem_zone_free(xfs_efd_zone, efdp);
  331. }
  332. }
  333. /*
  334. * This returns the number of iovecs needed to log the given efd item.
  335. * We only need 1 iovec for an efd item. It just logs the efd_log_format
  336. * structure.
  337. */
  338. /*ARGSUSED*/
  339. STATIC uint
  340. xfs_efd_item_size(xfs_efd_log_item_t *efdp)
  341. {
  342. return 1;
  343. }
  344. /*
  345. * This is called to fill in the vector of log iovecs for the
  346. * given efd log item. We use only 1 iovec, and we point that
  347. * at the efd_log_format structure embedded in the efd item.
  348. * It is at this point that we assert that all of the extent
  349. * slots in the efd item have been filled.
  350. */
  351. STATIC void
  352. xfs_efd_item_format(xfs_efd_log_item_t *efdp,
  353. xfs_log_iovec_t *log_vector)
  354. {
  355. uint size;
  356. ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
  357. efdp->efd_format.efd_type = XFS_LI_EFD;
  358. size = sizeof(xfs_efd_log_format_t);
  359. size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
  360. efdp->efd_format.efd_size = 1;
  361. log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format);
  362. log_vector->i_len = size;
  363. XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFD_FORMAT);
  364. ASSERT(size >= sizeof(xfs_efd_log_format_t));
  365. }
  366. /*
  367. * Pinning has no meaning for an efd item, so just return.
  368. */
  369. /*ARGSUSED*/
  370. STATIC void
  371. xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
  372. {
  373. return;
  374. }
  375. /*
  376. * Since pinning has no meaning for an efd item, unpinning does
  377. * not either.
  378. */
  379. /*ARGSUSED*/
  380. STATIC void
  381. xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
  382. {
  383. return;
  384. }
  385. /*ARGSUSED*/
  386. STATIC void
  387. xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
  388. {
  389. return;
  390. }
  391. /*
  392. * Efd items have no locking, so just return success.
  393. */
  394. /*ARGSUSED*/
  395. STATIC uint
  396. xfs_efd_item_trylock(xfs_efd_log_item_t *efdp)
  397. {
  398. return XFS_ITEM_LOCKED;
  399. }
  400. /*
  401. * Efd items have no locking or pushing, so return failure
  402. * so that the caller doesn't bother with us.
  403. */
  404. /*ARGSUSED*/
  405. STATIC void
  406. xfs_efd_item_unlock(xfs_efd_log_item_t *efdp)
  407. {
  408. if (efdp->efd_item.li_flags & XFS_LI_ABORTED)
  409. xfs_efd_item_free(efdp);
  410. return;
  411. }
  412. /*
  413. * When the efd item is committed to disk, all we need to do
  414. * is delete our reference to our partner efi item and then
  415. * free ourselves. Since we're freeing ourselves we must
  416. * return -1 to keep the transaction code from further referencing
  417. * this item.
  418. */
  419. /*ARGSUSED*/
  420. STATIC xfs_lsn_t
  421. xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn)
  422. {
  423. /*
  424. * If we got a log I/O error, it's always the case that the LR with the
  425. * EFI got unpinned and freed before the EFD got aborted.
  426. */
  427. if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
  428. xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
  429. xfs_efd_item_free(efdp);
  430. return (xfs_lsn_t)-1;
  431. }
  432. /*
  433. * There isn't much you can do to push on an efd item. It is simply
  434. * stuck waiting for the log to be flushed to disk.
  435. */
  436. /*ARGSUSED*/
  437. STATIC void
  438. xfs_efd_item_push(xfs_efd_log_item_t *efdp)
  439. {
  440. return;
  441. }
  442. /*
  443. * The EFD dependency tracking op doesn't do squat. It can't because
  444. * it doesn't know where the free extent is coming from. The dependency
  445. * tracking has to be handled by the "enclosing" metadata object. For
  446. * example, for inodes, the inode is locked throughout the extent freeing
  447. * so the dependency should be recorded there.
  448. */
  449. /*ARGSUSED*/
  450. STATIC void
  451. xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
  452. {
  453. return;
  454. }
  455. /*
  456. * This is the ops vector shared by all efd log items.
  457. */
  458. static struct xfs_item_ops xfs_efd_item_ops = {
  459. .iop_size = (uint(*)(xfs_log_item_t*))xfs_efd_item_size,
  460. .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
  461. xfs_efd_item_format,
  462. .iop_pin = (void(*)(xfs_log_item_t*))xfs_efd_item_pin,
  463. .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin,
  464. .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
  465. xfs_efd_item_unpin_remove,
  466. .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock,
  467. .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efd_item_unlock,
  468. .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
  469. xfs_efd_item_committed,
  470. .iop_push = (void(*)(xfs_log_item_t*))xfs_efd_item_push,
  471. .iop_pushbuf = NULL,
  472. .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
  473. xfs_efd_item_committing
  474. };
  475. /*
  476. * Allocate and initialize an efd item with the given number of extents.
  477. */
  478. xfs_efd_log_item_t *
  479. xfs_efd_init(xfs_mount_t *mp,
  480. xfs_efi_log_item_t *efip,
  481. uint nextents)
  482. {
  483. xfs_efd_log_item_t *efdp;
  484. uint size;
  485. ASSERT(nextents > 0);
  486. if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
  487. size = (uint)(sizeof(xfs_efd_log_item_t) +
  488. ((nextents - 1) * sizeof(xfs_extent_t)));
  489. efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP);
  490. } else {
  491. efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
  492. KM_SLEEP);
  493. }
  494. efdp->efd_item.li_type = XFS_LI_EFD;
  495. efdp->efd_item.li_ops = &xfs_efd_item_ops;
  496. efdp->efd_item.li_mountp = mp;
  497. efdp->efd_item.li_ailp = mp->m_ail;
  498. efdp->efd_efip = efip;
  499. efdp->efd_format.efd_nextents = nextents;
  500. efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
  501. return (efdp);
  502. }