xfs_extfree_item.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 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
  50. * test_and_clear_bit(XFS_EFI_COMMITTED) to ensure only the last caller frees
  51. * the EFI.
  52. */
  53. STATIC void
  54. __xfs_efi_release(
  55. struct xfs_efi_log_item *efip)
  56. {
  57. struct xfs_ail *ailp = efip->efi_item.li_ailp;
  58. if (!test_and_clear_bit(XFS_EFI_COMMITTED, &efip->efi_flags)) {
  59. spin_lock(&ailp->xa_lock);
  60. /* xfs_trans_ail_delete() drops the AIL lock. */
  61. xfs_trans_ail_delete(ailp, &efip->efi_item);
  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
  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. STATIC uint
  140. xfs_efi_item_trylock(
  141. struct xfs_log_item *lip)
  142. {
  143. return XFS_ITEM_PINNED;
  144. }
  145. /*
  146. * Efi items have no locking, so just return.
  147. */
  148. STATIC void
  149. xfs_efi_item_unlock(
  150. struct xfs_log_item *lip)
  151. {
  152. if (lip->li_flags & XFS_LI_ABORTED)
  153. xfs_efi_item_free(EFI_ITEM(lip));
  154. }
  155. /*
  156. * The EFI is logged only once and cannot be moved in the log, so simply return
  157. * the lsn at which it's been logged. For bulk transaction committed
  158. * processing, the EFI may be processed but not yet unpinned prior to the EFD
  159. * being processed. Set the XFS_EFI_COMMITTED flag so this case can be detected
  160. * when processing the EFD.
  161. */
  162. STATIC xfs_lsn_t
  163. xfs_efi_item_committed(
  164. struct xfs_log_item *lip,
  165. xfs_lsn_t lsn)
  166. {
  167. struct xfs_efi_log_item *efip = EFI_ITEM(lip);
  168. set_bit(XFS_EFI_COMMITTED, &efip->efi_flags);
  169. return lsn;
  170. }
  171. /*
  172. * There isn't much you can do to push on an efi item. It is simply
  173. * stuck waiting for all of its corresponding efd items to be
  174. * committed to disk.
  175. */
  176. STATIC void
  177. xfs_efi_item_push(
  178. struct xfs_log_item *lip)
  179. {
  180. }
  181. /*
  182. * The EFI dependency tracking op doesn't do squat. It can't because
  183. * it doesn't know where the free extent is coming from. The dependency
  184. * tracking has to be handled by the "enclosing" metadata object. For
  185. * example, for inodes, the inode is locked throughout the extent freeing
  186. * so the dependency should be recorded there.
  187. */
  188. STATIC void
  189. xfs_efi_item_committing(
  190. struct xfs_log_item *lip,
  191. xfs_lsn_t lsn)
  192. {
  193. }
  194. /*
  195. * This is the ops vector shared by all efi log items.
  196. */
  197. static struct xfs_item_ops xfs_efi_item_ops = {
  198. .iop_size = xfs_efi_item_size,
  199. .iop_format = xfs_efi_item_format,
  200. .iop_pin = xfs_efi_item_pin,
  201. .iop_unpin = xfs_efi_item_unpin,
  202. .iop_trylock = xfs_efi_item_trylock,
  203. .iop_unlock = xfs_efi_item_unlock,
  204. .iop_committed = xfs_efi_item_committed,
  205. .iop_push = xfs_efi_item_push,
  206. .iop_committing = xfs_efi_item_committing
  207. };
  208. /*
  209. * Allocate and initialize an efi item with the given number of extents.
  210. */
  211. struct xfs_efi_log_item *
  212. xfs_efi_init(
  213. struct xfs_mount *mp,
  214. uint nextents)
  215. {
  216. struct xfs_efi_log_item *efip;
  217. uint size;
  218. ASSERT(nextents > 0);
  219. if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
  220. size = (uint)(sizeof(xfs_efi_log_item_t) +
  221. ((nextents - 1) * sizeof(xfs_extent_t)));
  222. efip = kmem_zalloc(size, KM_SLEEP);
  223. } else {
  224. efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
  225. }
  226. xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
  227. efip->efi_format.efi_nextents = nextents;
  228. efip->efi_format.efi_id = (__psint_t)(void*)efip;
  229. atomic_set(&efip->efi_next_extent, 0);
  230. return efip;
  231. }
  232. /*
  233. * Copy an EFI format buffer from the given buf, and into the destination
  234. * EFI format structure.
  235. * The given buffer can be in 32 bit or 64 bit form (which has different padding),
  236. * one of which will be the native format for this kernel.
  237. * It will handle the conversion of formats if necessary.
  238. */
  239. int
  240. xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
  241. {
  242. xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
  243. uint i;
  244. uint len = sizeof(xfs_efi_log_format_t) +
  245. (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
  246. uint len32 = sizeof(xfs_efi_log_format_32_t) +
  247. (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
  248. uint len64 = sizeof(xfs_efi_log_format_64_t) +
  249. (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
  250. if (buf->i_len == len) {
  251. memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
  252. return 0;
  253. } else if (buf->i_len == len32) {
  254. xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
  255. dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
  256. dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
  257. dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
  258. dst_efi_fmt->efi_id = src_efi_fmt_32->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_32->efi_extents[i].ext_start;
  262. dst_efi_fmt->efi_extents[i].ext_len =
  263. src_efi_fmt_32->efi_extents[i].ext_len;
  264. }
  265. return 0;
  266. } else if (buf->i_len == len64) {
  267. xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
  268. dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
  269. dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
  270. dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
  271. dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
  272. for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
  273. dst_efi_fmt->efi_extents[i].ext_start =
  274. src_efi_fmt_64->efi_extents[i].ext_start;
  275. dst_efi_fmt->efi_extents[i].ext_len =
  276. src_efi_fmt_64->efi_extents[i].ext_len;
  277. }
  278. return 0;
  279. }
  280. return EFSCORRUPTED;
  281. }
  282. /*
  283. * This is called by the efd item code below to release references to the given
  284. * efi item. Each efd calls this with the number of extents that it has
  285. * logged, and when the sum of these reaches the total number of extents logged
  286. * by this efi item we can free the efi item.
  287. */
  288. void
  289. xfs_efi_release(xfs_efi_log_item_t *efip,
  290. uint nextents)
  291. {
  292. ASSERT(atomic_read(&efip->efi_next_extent) >= nextents);
  293. if (atomic_sub_and_test(nextents, &efip->efi_next_extent))
  294. __xfs_efi_release(efip);
  295. }
  296. static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
  297. {
  298. return container_of(lip, struct xfs_efd_log_item, efd_item);
  299. }
  300. STATIC void
  301. xfs_efd_item_free(struct xfs_efd_log_item *efdp)
  302. {
  303. if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
  304. kmem_free(efdp);
  305. else
  306. kmem_zone_free(xfs_efd_zone, efdp);
  307. }
  308. /*
  309. * This returns the number of iovecs needed to log the given efd item.
  310. * We only need 1 iovec for an efd item. It just logs the efd_log_format
  311. * structure.
  312. */
  313. STATIC uint
  314. xfs_efd_item_size(
  315. struct xfs_log_item *lip)
  316. {
  317. return 1;
  318. }
  319. /*
  320. * This is called to fill in the vector of log iovecs for the
  321. * given efd log item. We use only 1 iovec, and we point that
  322. * at the efd_log_format structure embedded in the efd item.
  323. * It is at this point that we assert that all of the extent
  324. * slots in the efd item have been filled.
  325. */
  326. STATIC void
  327. xfs_efd_item_format(
  328. struct xfs_log_item *lip,
  329. struct xfs_log_iovec *log_vector)
  330. {
  331. struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
  332. uint size;
  333. ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
  334. efdp->efd_format.efd_type = XFS_LI_EFD;
  335. size = sizeof(xfs_efd_log_format_t);
  336. size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
  337. efdp->efd_format.efd_size = 1;
  338. log_vector->i_addr = &efdp->efd_format;
  339. log_vector->i_len = size;
  340. log_vector->i_type = XLOG_REG_TYPE_EFD_FORMAT;
  341. ASSERT(size >= sizeof(xfs_efd_log_format_t));
  342. }
  343. /*
  344. * Pinning has no meaning for an efd item, so just return.
  345. */
  346. STATIC void
  347. xfs_efd_item_pin(
  348. struct xfs_log_item *lip)
  349. {
  350. }
  351. /*
  352. * Since pinning has no meaning for an efd item, unpinning does
  353. * not either.
  354. */
  355. STATIC void
  356. xfs_efd_item_unpin(
  357. struct xfs_log_item *lip,
  358. int remove)
  359. {
  360. }
  361. /*
  362. * Efd items have no locking, so just return success.
  363. */
  364. STATIC uint
  365. xfs_efd_item_trylock(
  366. struct xfs_log_item *lip)
  367. {
  368. return XFS_ITEM_LOCKED;
  369. }
  370. /*
  371. * Efd items have no locking or pushing, so return failure
  372. * so that the caller doesn't bother with us.
  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. * There isn't much you can do to push on an efd item. It is simply
  405. * stuck waiting for the log to be flushed to disk.
  406. */
  407. STATIC void
  408. xfs_efd_item_push(
  409. struct xfs_log_item *lip)
  410. {
  411. }
  412. /*
  413. * The EFD dependency tracking op doesn't do squat. It can't because
  414. * it doesn't know where the free extent is coming from. The dependency
  415. * tracking has to be handled by the "enclosing" metadata object. For
  416. * example, for inodes, the inode is locked throughout the extent freeing
  417. * so the dependency should be recorded there.
  418. */
  419. STATIC void
  420. xfs_efd_item_committing(
  421. struct xfs_log_item *lip,
  422. xfs_lsn_t lsn)
  423. {
  424. }
  425. /*
  426. * This is the ops vector shared by all efd log items.
  427. */
  428. static struct xfs_item_ops xfs_efd_item_ops = {
  429. .iop_size = xfs_efd_item_size,
  430. .iop_format = xfs_efd_item_format,
  431. .iop_pin = xfs_efd_item_pin,
  432. .iop_unpin = xfs_efd_item_unpin,
  433. .iop_trylock = xfs_efd_item_trylock,
  434. .iop_unlock = xfs_efd_item_unlock,
  435. .iop_committed = xfs_efd_item_committed,
  436. .iop_push = xfs_efd_item_push,
  437. .iop_committing = xfs_efd_item_committing
  438. };
  439. /*
  440. * Allocate and initialize an efd item with the given number of extents.
  441. */
  442. struct xfs_efd_log_item *
  443. xfs_efd_init(
  444. struct xfs_mount *mp,
  445. struct xfs_efi_log_item *efip,
  446. uint nextents)
  447. {
  448. struct xfs_efd_log_item *efdp;
  449. uint size;
  450. ASSERT(nextents > 0);
  451. if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
  452. size = (uint)(sizeof(xfs_efd_log_item_t) +
  453. ((nextents - 1) * sizeof(xfs_extent_t)));
  454. efdp = kmem_zalloc(size, KM_SLEEP);
  455. } else {
  456. efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
  457. }
  458. xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
  459. efdp->efd_efip = efip;
  460. efdp->efd_format.efd_nextents = nextents;
  461. efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
  462. return efdp;
  463. }