xfs_extfree_item.c 13 KB

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