xfs_trans_item.c 11 KB

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