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