xfs_extfree_item.c 13 KB

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