xfs_extfree_item.c 15 KB

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