xfs_extfree_item.c 16 KB

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