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