xfs_extfree_item.c 14 KB

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