xfs_trans_item.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (c) 2000-2002,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_trans_priv.h"
  25. /* XXX: from here down needed until struct xfs_trans has its own ailp */
  26. #include "xfs_bit.h"
  27. #include "xfs_buf_item.h"
  28. #include "xfs_sb.h"
  29. #include "xfs_ag.h"
  30. #include "xfs_dir2.h"
  31. #include "xfs_mount.h"
  32. STATIC int xfs_trans_unlock_chunk(xfs_log_item_chunk_t *,
  33. int, int, xfs_lsn_t);
  34. /*
  35. * This is called to add the given log item to the transaction's
  36. * list of log items. It must find a free log item descriptor
  37. * or allocate a new one and add the item to that descriptor.
  38. * The function returns a pointer to item descriptor used to point
  39. * to the new item. The log item will now point to its new descriptor
  40. * with its li_desc field.
  41. */
  42. xfs_log_item_desc_t *
  43. xfs_trans_add_item(xfs_trans_t *tp, xfs_log_item_t *lip)
  44. {
  45. xfs_log_item_desc_t *lidp;
  46. xfs_log_item_chunk_t *licp;
  47. int i=0;
  48. /*
  49. * If there are no free descriptors, allocate a new chunk
  50. * of them and put it at the front of the chunk list.
  51. */
  52. if (tp->t_items_free == 0) {
  53. licp = (xfs_log_item_chunk_t*)
  54. kmem_alloc(sizeof(xfs_log_item_chunk_t), KM_SLEEP);
  55. ASSERT(licp != NULL);
  56. /*
  57. * Initialize the chunk, and then
  58. * claim the first slot in the newly allocated chunk.
  59. */
  60. xfs_lic_init(licp);
  61. xfs_lic_claim(licp, 0);
  62. licp->lic_unused = 1;
  63. xfs_lic_init_slot(licp, 0);
  64. lidp = xfs_lic_slot(licp, 0);
  65. /*
  66. * Link in the new chunk and update the free count.
  67. */
  68. licp->lic_next = tp->t_items.lic_next;
  69. tp->t_items.lic_next = licp;
  70. tp->t_items_free = XFS_LIC_NUM_SLOTS - 1;
  71. /*
  72. * Initialize the descriptor and the generic portion
  73. * of the log item.
  74. *
  75. * Point the new slot at this item and return it.
  76. * Also point the log item at its currently active
  77. * descriptor and set the item's mount pointer.
  78. */
  79. lidp->lid_item = lip;
  80. lidp->lid_flags = 0;
  81. lidp->lid_size = 0;
  82. lip->li_desc = lidp;
  83. lip->li_mountp = tp->t_mountp;
  84. lip->li_ailp = tp->t_mountp->m_ail;
  85. return lidp;
  86. }
  87. /*
  88. * Find the free descriptor. It is somewhere in the chunklist
  89. * of descriptors.
  90. */
  91. licp = &tp->t_items;
  92. while (licp != NULL) {
  93. if (xfs_lic_vacancy(licp)) {
  94. if (licp->lic_unused <= XFS_LIC_MAX_SLOT) {
  95. i = licp->lic_unused;
  96. ASSERT(xfs_lic_isfree(licp, i));
  97. break;
  98. }
  99. for (i = 0; i <= XFS_LIC_MAX_SLOT; i++) {
  100. if (xfs_lic_isfree(licp, i))
  101. break;
  102. }
  103. ASSERT(i <= XFS_LIC_MAX_SLOT);
  104. break;
  105. }
  106. licp = licp->lic_next;
  107. }
  108. ASSERT(licp != NULL);
  109. /*
  110. * If we find a free descriptor, claim it,
  111. * initialize it, and return it.
  112. */
  113. xfs_lic_claim(licp, i);
  114. if (licp->lic_unused <= i) {
  115. licp->lic_unused = i + 1;
  116. xfs_lic_init_slot(licp, i);
  117. }
  118. lidp = xfs_lic_slot(licp, i);
  119. tp->t_items_free--;
  120. lidp->lid_item = lip;
  121. lidp->lid_flags = 0;
  122. lidp->lid_size = 0;
  123. lip->li_desc = lidp;
  124. lip->li_mountp = tp->t_mountp;
  125. lip->li_ailp = tp->t_mountp->m_ail;
  126. return lidp;
  127. }
  128. /*
  129. * Free the given descriptor.
  130. *
  131. * This requires setting the bit in the chunk's free mask corresponding
  132. * to the given slot.
  133. */
  134. void
  135. xfs_trans_free_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp)
  136. {
  137. uint slot;
  138. xfs_log_item_chunk_t *licp;
  139. xfs_log_item_chunk_t **licpp;
  140. slot = xfs_lic_desc_to_slot(lidp);
  141. licp = xfs_lic_desc_to_chunk(lidp);
  142. xfs_lic_relse(licp, slot);
  143. lidp->lid_item->li_desc = NULL;
  144. tp->t_items_free++;
  145. /*
  146. * If there are no more used items in the chunk and this is not
  147. * the chunk embedded in the transaction structure, then free
  148. * the chunk. First pull it from the chunk list and then
  149. * free it back to the heap. We didn't bother with a doubly
  150. * linked list here because the lists should be very short
  151. * and this is not a performance path. It's better to save
  152. * the memory of the extra pointer.
  153. *
  154. * Also decrement the transaction structure's count of free items
  155. * by the number in a chunk since we are freeing an empty chunk.
  156. */
  157. if (xfs_lic_are_all_free(licp) && (licp != &(tp->t_items))) {
  158. licpp = &(tp->t_items.lic_next);
  159. while (*licpp != licp) {
  160. ASSERT(*licpp != NULL);
  161. licpp = &((*licpp)->lic_next);
  162. }
  163. *licpp = licp->lic_next;
  164. kmem_free(licp);
  165. tp->t_items_free -= XFS_LIC_NUM_SLOTS;
  166. }
  167. }
  168. /*
  169. * This is called to find the descriptor corresponding to the given
  170. * log item. It returns a pointer to the descriptor.
  171. * The log item MUST have a corresponding descriptor in the given
  172. * transaction. This routine does not return NULL, it panics.
  173. *
  174. * The descriptor pointer is kept in the log item's li_desc field.
  175. * Just return it.
  176. */
  177. /*ARGSUSED*/
  178. xfs_log_item_desc_t *
  179. xfs_trans_find_item(xfs_trans_t *tp, xfs_log_item_t *lip)
  180. {
  181. ASSERT(lip->li_desc != NULL);
  182. return lip->li_desc;
  183. }
  184. /*
  185. * Return a pointer to the first descriptor in the chunk list.
  186. * This does not return NULL if there are none, it panics.
  187. *
  188. * The first descriptor must be in either the first or second chunk.
  189. * This is because the only chunk allowed to be empty is the first.
  190. * All others are freed when they become empty.
  191. *
  192. * At some point this and xfs_trans_next_item() should be optimized
  193. * to quickly look at the mask to determine if there is anything to
  194. * look at.
  195. */
  196. xfs_log_item_desc_t *
  197. xfs_trans_first_item(xfs_trans_t *tp)
  198. {
  199. xfs_log_item_chunk_t *licp;
  200. int i;
  201. licp = &tp->t_items;
  202. /*
  203. * If it's not in the first chunk, skip to the second.
  204. */
  205. if (xfs_lic_are_all_free(licp)) {
  206. licp = licp->lic_next;
  207. }
  208. /*
  209. * Return the first non-free descriptor in the chunk.
  210. */
  211. ASSERT(!xfs_lic_are_all_free(licp));
  212. for (i = 0; i < licp->lic_unused; i++) {
  213. if (xfs_lic_isfree(licp, i)) {
  214. continue;
  215. }
  216. return xfs_lic_slot(licp, i);
  217. }
  218. cmn_err(CE_WARN, "xfs_trans_first_item() -- no first item");
  219. return NULL;
  220. }
  221. /*
  222. * Given a descriptor, return the next descriptor in the chunk list.
  223. * This returns NULL if there are no more used descriptors in the list.
  224. *
  225. * We do this by first locating the chunk in which the descriptor resides,
  226. * and then scanning forward in the chunk and the list for the next
  227. * used descriptor.
  228. */
  229. /*ARGSUSED*/
  230. xfs_log_item_desc_t *
  231. xfs_trans_next_item(xfs_trans_t *tp, xfs_log_item_desc_t *lidp)
  232. {
  233. xfs_log_item_chunk_t *licp;
  234. int i;
  235. licp = xfs_lic_desc_to_chunk(lidp);
  236. /*
  237. * First search the rest of the chunk. The for loop keeps us
  238. * from referencing things beyond the end of the chunk.
  239. */
  240. for (i = (int)xfs_lic_desc_to_slot(lidp) + 1; i < licp->lic_unused; i++) {
  241. if (xfs_lic_isfree(licp, i)) {
  242. continue;
  243. }
  244. return xfs_lic_slot(licp, i);
  245. }
  246. /*
  247. * Now search the next chunk. It must be there, because the
  248. * next chunk would have been freed if it were empty.
  249. * If there is no next chunk, return NULL.
  250. */
  251. if (licp->lic_next == NULL) {
  252. return NULL;
  253. }
  254. licp = licp->lic_next;
  255. ASSERT(!xfs_lic_are_all_free(licp));
  256. for (i = 0; i < licp->lic_unused; i++) {
  257. if (xfs_lic_isfree(licp, i)) {
  258. continue;
  259. }
  260. return xfs_lic_slot(licp, i);
  261. }
  262. ASSERT(0);
  263. /* NOTREACHED */
  264. return NULL; /* keep gcc quite */
  265. }
  266. /*
  267. * This is called to unlock all of the items of a transaction and to free
  268. * all the descriptors of that transaction.
  269. *
  270. * It walks the list of descriptors and unlocks each item. It frees
  271. * each chunk except that embedded in the transaction as it goes along.
  272. */
  273. void
  274. xfs_trans_free_items(
  275. xfs_trans_t *tp,
  276. xfs_lsn_t commit_lsn,
  277. int flags)
  278. {
  279. xfs_log_item_chunk_t *licp;
  280. xfs_log_item_chunk_t *next_licp;
  281. int abort;
  282. abort = flags & XFS_TRANS_ABORT;
  283. licp = &tp->t_items;
  284. /*
  285. * Special case the embedded chunk so we don't free it below.
  286. */
  287. if (!xfs_lic_are_all_free(licp)) {
  288. (void) xfs_trans_unlock_chunk(licp, 1, abort, commit_lsn);
  289. xfs_lic_all_free(licp);
  290. licp->lic_unused = 0;
  291. }
  292. licp = licp->lic_next;
  293. /*
  294. * Unlock each item in each chunk and free the chunks.
  295. */
  296. while (licp != NULL) {
  297. ASSERT(!xfs_lic_are_all_free(licp));
  298. (void) xfs_trans_unlock_chunk(licp, 1, abort, commit_lsn);
  299. next_licp = licp->lic_next;
  300. kmem_free(licp);
  301. licp = next_licp;
  302. }
  303. /*
  304. * Reset the transaction structure's free item count.
  305. */
  306. tp->t_items_free = XFS_LIC_NUM_SLOTS;
  307. tp->t_items.lic_next = NULL;
  308. }
  309. /*
  310. * This is called to unlock the items associated with a transaction.
  311. * Items which were not logged should be freed.
  312. * Those which were logged must still be tracked so they can be unpinned
  313. * when the transaction commits.
  314. */
  315. void
  316. xfs_trans_unlock_items(xfs_trans_t *tp, xfs_lsn_t commit_lsn)
  317. {
  318. xfs_log_item_chunk_t *licp;
  319. xfs_log_item_chunk_t *next_licp;
  320. xfs_log_item_chunk_t **licpp;
  321. int freed;
  322. freed = 0;
  323. licp = &tp->t_items;
  324. /*
  325. * Special case the embedded chunk so we don't free.
  326. */
  327. if (!xfs_lic_are_all_free(licp)) {
  328. freed = xfs_trans_unlock_chunk(licp, 0, 0, commit_lsn);
  329. }
  330. licpp = &(tp->t_items.lic_next);
  331. licp = licp->lic_next;
  332. /*
  333. * Unlock each item in each chunk, free non-dirty descriptors,
  334. * and free empty chunks.
  335. */
  336. while (licp != NULL) {
  337. ASSERT(!xfs_lic_are_all_free(licp));
  338. freed += xfs_trans_unlock_chunk(licp, 0, 0, commit_lsn);
  339. next_licp = licp->lic_next;
  340. if (xfs_lic_are_all_free(licp)) {
  341. *licpp = next_licp;
  342. kmem_free(licp);
  343. freed -= XFS_LIC_NUM_SLOTS;
  344. } else {
  345. licpp = &(licp->lic_next);
  346. }
  347. ASSERT(*licpp == next_licp);
  348. licp = next_licp;
  349. }
  350. /*
  351. * Fix the free descriptor count in the transaction.
  352. */
  353. tp->t_items_free += freed;
  354. }
  355. /*
  356. * Unlock each item pointed to by a descriptor in the given chunk.
  357. * Stamp the commit lsn into each item if necessary.
  358. * Free descriptors pointing to items which are not dirty if freeing_chunk
  359. * is zero. If freeing_chunk is non-zero, then we need to unlock all
  360. * items in the chunk.
  361. *
  362. * Return the number of descriptors freed.
  363. */
  364. STATIC int
  365. xfs_trans_unlock_chunk(
  366. xfs_log_item_chunk_t *licp,
  367. int freeing_chunk,
  368. int abort,
  369. xfs_lsn_t commit_lsn)
  370. {
  371. xfs_log_item_desc_t *lidp;
  372. xfs_log_item_t *lip;
  373. int i;
  374. int freed;
  375. freed = 0;
  376. lidp = licp->lic_descs;
  377. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  378. if (xfs_lic_isfree(licp, i)) {
  379. continue;
  380. }
  381. lip = lidp->lid_item;
  382. lip->li_desc = NULL;
  383. if (commit_lsn != NULLCOMMITLSN)
  384. IOP_COMMITTING(lip, commit_lsn);
  385. if (abort)
  386. lip->li_flags |= XFS_LI_ABORTED;
  387. IOP_UNLOCK(lip);
  388. /*
  389. * Free the descriptor if the item is not dirty
  390. * within this transaction and the caller is not
  391. * going to just free the entire thing regardless.
  392. */
  393. if (!(freeing_chunk) &&
  394. (!(lidp->lid_flags & XFS_LID_DIRTY) || abort)) {
  395. xfs_lic_relse(licp, i);
  396. freed++;
  397. }
  398. }
  399. return freed;
  400. }