blob: fc39b166d403e609e356008f05c23fcb682d8b28 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_dmapi.h"
26#include "xfs_mount.h"
27#include "xfs_trans_priv.h"
28#include "xfs_error.h"
29
30STATIC void xfs_ail_insert(xfs_ail_entry_t *, xfs_log_item_t *);
31STATIC xfs_log_item_t * xfs_ail_delete(xfs_ail_entry_t *, xfs_log_item_t *);
32STATIC xfs_log_item_t * xfs_ail_min(xfs_ail_entry_t *);
33STATIC xfs_log_item_t * xfs_ail_next(xfs_ail_entry_t *, xfs_log_item_t *);
34
35#ifdef DEBUG
36STATIC void xfs_ail_check(xfs_ail_entry_t *);
37#else
38#define xfs_ail_check(a)
39#endif /* DEBUG */
40
41
42/*
43 * This is called by the log manager code to determine the LSN
44 * of the tail of the log. This is exactly the LSN of the first
45 * item in the AIL. If the AIL is empty, then this function
46 * returns 0.
47 *
48 * We need the AIL lock in order to get a coherent read of the
49 * lsn of the last item in the AIL.
50 */
51xfs_lsn_t
52xfs_trans_tail_ail(
53 xfs_mount_t *mp)
54{
55 xfs_lsn_t lsn;
56 xfs_log_item_t *lip;
57 SPLDECL(s);
58
59 AIL_LOCK(mp,s);
60 lip = xfs_ail_min(&(mp->m_ail));
61 if (lip == NULL) {
62 lsn = (xfs_lsn_t)0;
63 } else {
64 lsn = lip->li_lsn;
65 }
66 AIL_UNLOCK(mp, s);
67
68 return lsn;
69}
70
71/*
72 * xfs_trans_push_ail
73 *
74 * This routine is called to move the tail of the AIL
75 * forward. It does this by trying to flush items in the AIL
76 * whose lsns are below the given threshold_lsn.
77 *
78 * The routine returns the lsn of the tail of the log.
79 */
80xfs_lsn_t
81xfs_trans_push_ail(
82 xfs_mount_t *mp,
83 xfs_lsn_t threshold_lsn)
84{
85 xfs_lsn_t lsn;
86 xfs_log_item_t *lip;
87 int gen;
88 int restarts;
89 int lock_result;
90 int flush_log;
91 SPLDECL(s);
92
93#define XFS_TRANS_PUSH_AIL_RESTARTS 10
94
95 AIL_LOCK(mp,s);
96 lip = xfs_trans_first_ail(mp, &gen);
97 if (lip == NULL || XFS_FORCED_SHUTDOWN(mp)) {
98 /*
99 * Just return if the AIL is empty.
100 */
101 AIL_UNLOCK(mp, s);
102 return (xfs_lsn_t)0;
103 }
104
105 XFS_STATS_INC(xs_push_ail);
106
107 /*
108 * While the item we are looking at is below the given threshold
109 * try to flush it out. Make sure to limit the number of times
110 * we allow xfs_trans_next_ail() to restart scanning from the
111 * beginning of the list. We'd like not to stop until we've at least
112 * tried to push on everything in the AIL with an LSN less than
113 * the given threshold. However, we may give up before that if
114 * we realize that we've been holding the AIL_LOCK for 'too long',
115 * blocking interrupts. Currently, too long is < 500us roughly.
116 */
117 flush_log = 0;
118 restarts = 0;
119 while (((restarts < XFS_TRANS_PUSH_AIL_RESTARTS) &&
120 (XFS_LSN_CMP(lip->li_lsn, threshold_lsn) < 0))) {
121 /*
122 * If we can lock the item without sleeping, unlock
123 * the AIL lock and flush the item. Then re-grab the
124 * AIL lock so we can look for the next item on the
125 * AIL. Since we unlock the AIL while we flush the
126 * item, the next routine may start over again at the
127 * the beginning of the list if anything has changed.
128 * That is what the generation count is for.
129 *
130 * If we can't lock the item, either its holder will flush
131 * it or it is already being flushed or it is being relogged.
132 * In any of these case it is being taken care of and we
133 * can just skip to the next item in the list.
134 */
135 lock_result = IOP_TRYLOCK(lip);
136 switch (lock_result) {
137 case XFS_ITEM_SUCCESS:
138 AIL_UNLOCK(mp, s);
139 XFS_STATS_INC(xs_push_ail_success);
140 IOP_PUSH(lip);
141 AIL_LOCK(mp,s);
142 break;
143
144 case XFS_ITEM_PUSHBUF:
145 AIL_UNLOCK(mp, s);
146 XFS_STATS_INC(xs_push_ail_pushbuf);
147#ifdef XFSRACEDEBUG
148 delay_for_intr();
149 delay(300);
150#endif
151 ASSERT(lip->li_ops->iop_pushbuf);
152 ASSERT(lip);
153 IOP_PUSHBUF(lip);
154 AIL_LOCK(mp,s);
155 break;
156
157 case XFS_ITEM_PINNED:
158 XFS_STATS_INC(xs_push_ail_pinned);
159 flush_log = 1;
160 break;
161
162 case XFS_ITEM_LOCKED:
163 XFS_STATS_INC(xs_push_ail_locked);
164 break;
165
166 case XFS_ITEM_FLUSHING:
167 XFS_STATS_INC(xs_push_ail_flushing);
168 break;
169
170 default:
171 ASSERT(0);
172 break;
173 }
174
175 lip = xfs_trans_next_ail(mp, lip, &gen, &restarts);
176 if (lip == NULL) {
177 break;
178 }
179 if (XFS_FORCED_SHUTDOWN(mp)) {
180 /*
181 * Just return if we shut down during the last try.
182 */
183 AIL_UNLOCK(mp, s);
184 return (xfs_lsn_t)0;
185 }
186
187 }
188
189 if (flush_log) {
190 /*
191 * If something we need to push out was pinned, then
192 * push out the log so it will become unpinned and
193 * move forward in the AIL.
194 */
195 AIL_UNLOCK(mp, s);
196 XFS_STATS_INC(xs_push_ail_flush);
197 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
198 AIL_LOCK(mp, s);
199 }
200
201 lip = xfs_ail_min(&(mp->m_ail));
202 if (lip == NULL) {
203 lsn = (xfs_lsn_t)0;
204 } else {
205 lsn = lip->li_lsn;
206 }
207
208 AIL_UNLOCK(mp, s);
209 return lsn;
210} /* xfs_trans_push_ail */
211
212
213/*
214 * This is to be called when an item is unlocked that may have
215 * been in the AIL. It will wake up the first member of the AIL
216 * wait list if this item's unlocking might allow it to progress.
217 * If the item is in the AIL, then we need to get the AIL lock
218 * while doing our checking so we don't race with someone going
219 * to sleep waiting for this event in xfs_trans_push_ail().
220 */
221void
222xfs_trans_unlocked_item(
223 xfs_mount_t *mp,
224 xfs_log_item_t *lip)
225{
226 xfs_log_item_t *min_lip;
227
228 /*
229 * If we're forcibly shutting down, we may have
230 * unlocked log items arbitrarily. The last thing
231 * we want to do is to move the tail of the log
232 * over some potentially valid data.
233 */
234 if (!(lip->li_flags & XFS_LI_IN_AIL) ||
235 XFS_FORCED_SHUTDOWN(mp)) {
236 return;
237 }
238
239 /*
240 * This is the one case where we can call into xfs_ail_min()
241 * without holding the AIL lock because we only care about the
242 * case where we are at the tail of the AIL. If the object isn't
243 * at the tail, it doesn't matter what result we get back. This
244 * is slightly racy because since we were just unlocked, we could
245 * go to sleep between the call to xfs_ail_min and the call to
246 * xfs_log_move_tail, have someone else lock us, commit to us disk,
247 * move us out of the tail of the AIL, and then we wake up. However,
248 * the call to xfs_log_move_tail() doesn't do anything if there's
249 * not enough free space to wake people up so we're safe calling it.
250 */
251 min_lip = xfs_ail_min(&mp->m_ail);
252
253 if (min_lip == lip)
254 xfs_log_move_tail(mp, 1);
255} /* xfs_trans_unlocked_item */
256
257
258/*
259 * Update the position of the item in the AIL with the new
260 * lsn. If it is not yet in the AIL, add it. Otherwise, move
261 * it to its new position by removing it and re-adding it.
262 *
263 * Wakeup anyone with an lsn less than the item's lsn. If the item
264 * we move in the AIL is the minimum one, update the tail lsn in the
265 * log manager.
266 *
267 * Increment the AIL's generation count to indicate that the tree
268 * has changed.
269 *
270 * This function must be called with the AIL lock held. The lock
271 * is dropped before returning, so the caller must pass in the
272 * cookie returned by AIL_LOCK.
273 */
274void
275xfs_trans_update_ail(
276 xfs_mount_t *mp,
277 xfs_log_item_t *lip,
278 xfs_lsn_t lsn,
Josh Triplett22d91f62006-09-28 11:04:07 +1000279 unsigned long s) __releases(mp->m_ail_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 xfs_ail_entry_t *ailp;
282 xfs_log_item_t *dlip=NULL;
283 xfs_log_item_t *mlip; /* ptr to minimum lip */
284
285 ailp = &(mp->m_ail);
286 mlip = xfs_ail_min(ailp);
287
288 if (lip->li_flags & XFS_LI_IN_AIL) {
289 dlip = xfs_ail_delete(ailp, lip);
290 ASSERT(dlip == lip);
291 } else {
292 lip->li_flags |= XFS_LI_IN_AIL;
293 }
294
295 lip->li_lsn = lsn;
296
297 xfs_ail_insert(ailp, lip);
298 mp->m_ail_gen++;
299
300 if (mlip == dlip) {
301 mlip = xfs_ail_min(&(mp->m_ail));
302 AIL_UNLOCK(mp, s);
303 xfs_log_move_tail(mp, mlip->li_lsn);
304 } else {
305 AIL_UNLOCK(mp, s);
306 }
307
308
309} /* xfs_trans_update_ail */
310
311/*
312 * Delete the given item from the AIL. It must already be in
313 * the AIL.
314 *
315 * Wakeup anyone with an lsn less than item's lsn. If the item
316 * we delete in the AIL is the minimum one, update the tail lsn in the
317 * log manager.
318 *
319 * Clear the IN_AIL flag from the item, reset its lsn to 0, and
320 * bump the AIL's generation count to indicate that the tree
321 * has changed.
322 *
323 * This function must be called with the AIL lock held. The lock
324 * is dropped before returning, so the caller must pass in the
325 * cookie returned by AIL_LOCK.
326 */
327void
328xfs_trans_delete_ail(
329 xfs_mount_t *mp,
330 xfs_log_item_t *lip,
Josh Triplett22d91f62006-09-28 11:04:07 +1000331 unsigned long s) __releases(mp->m_ail_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 xfs_ail_entry_t *ailp;
334 xfs_log_item_t *dlip;
335 xfs_log_item_t *mlip;
336
337 if (lip->li_flags & XFS_LI_IN_AIL) {
338 ailp = &(mp->m_ail);
339 mlip = xfs_ail_min(ailp);
340 dlip = xfs_ail_delete(ailp, lip);
341 ASSERT(dlip == lip);
342
343
344 lip->li_flags &= ~XFS_LI_IN_AIL;
345 lip->li_lsn = 0;
346 mp->m_ail_gen++;
347
348 if (mlip == dlip) {
349 mlip = xfs_ail_min(&(mp->m_ail));
350 AIL_UNLOCK(mp, s);
351 xfs_log_move_tail(mp, (mlip ? mlip->li_lsn : 0));
352 } else {
353 AIL_UNLOCK(mp, s);
354 }
355 }
356 else {
357 /*
358 * If the file system is not being shutdown, we are in
359 * serious trouble if we get to this stage.
360 */
361 if (XFS_FORCED_SHUTDOWN(mp))
362 AIL_UNLOCK(mp, s);
363 else {
364 xfs_cmn_err(XFS_PTAG_AILDELETE, CE_ALERT, mp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000365 "%s: attempting to delete a log item that is not in the AIL",
366 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 AIL_UNLOCK(mp, s);
Nathan Scott7d04a332006-06-09 14:58:38 +1000368 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370 }
371}
372
373
374
375/*
376 * Return the item in the AIL with the smallest lsn.
377 * Return the current tree generation number for use
378 * in calls to xfs_trans_next_ail().
379 */
380xfs_log_item_t *
381xfs_trans_first_ail(
382 xfs_mount_t *mp,
383 int *gen)
384{
385 xfs_log_item_t *lip;
386
387 lip = xfs_ail_min(&(mp->m_ail));
388 *gen = (int)mp->m_ail_gen;
389
390 return (lip);
391}
392
393/*
394 * If the generation count of the tree has not changed since the
395 * caller last took something from the AIL, then return the elmt
396 * in the tree which follows the one given. If the count has changed,
397 * then return the minimum elmt of the AIL and bump the restarts counter
398 * if one is given.
399 */
400xfs_log_item_t *
401xfs_trans_next_ail(
402 xfs_mount_t *mp,
403 xfs_log_item_t *lip,
404 int *gen,
405 int *restarts)
406{
407 xfs_log_item_t *nlip;
408
409 ASSERT(mp && lip && gen);
410 if (mp->m_ail_gen == *gen) {
411 nlip = xfs_ail_next(&(mp->m_ail), lip);
412 } else {
413 nlip = xfs_ail_min(&(mp->m_ail));
414 *gen = (int)mp->m_ail_gen;
415 if (restarts != NULL) {
416 XFS_STATS_INC(xs_push_ail_restarts);
417 (*restarts)++;
418 }
419 }
420
421 return (nlip);
422}
423
424
425/*
426 * The active item list (AIL) is a doubly linked list of log
427 * items sorted by ascending lsn. The base of the list is
428 * a forw/back pointer pair embedded in the xfs mount structure.
429 * The base is initialized with both pointers pointing to the
430 * base. This case always needs to be distinguished, because
431 * the base has no lsn to look at. We almost always insert
432 * at the end of the list, so on inserts we search from the
433 * end of the list to find where the new item belongs.
434 */
435
436/*
437 * Initialize the doubly linked list to point only to itself.
438 */
439void
440xfs_trans_ail_init(
441 xfs_mount_t *mp)
442{
443 mp->m_ail.ail_forw = (xfs_log_item_t*)&(mp->m_ail);
444 mp->m_ail.ail_back = (xfs_log_item_t*)&(mp->m_ail);
445}
446
447/*
448 * Insert the given log item into the AIL.
449 * We almost always insert at the end of the list, so on inserts
450 * we search from the end of the list to find where the
451 * new item belongs.
452 */
453STATIC void
454xfs_ail_insert(
455 xfs_ail_entry_t *base,
456 xfs_log_item_t *lip)
457/* ARGSUSED */
458{
459 xfs_log_item_t *next_lip;
460
461 /*
462 * If the list is empty, just insert the item.
463 */
464 if (base->ail_back == (xfs_log_item_t*)base) {
465 base->ail_forw = lip;
466 base->ail_back = lip;
467 lip->li_ail.ail_forw = (xfs_log_item_t*)base;
468 lip->li_ail.ail_back = (xfs_log_item_t*)base;
469 return;
470 }
471
472 next_lip = base->ail_back;
473 while ((next_lip != (xfs_log_item_t*)base) &&
474 (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) > 0)) {
475 next_lip = next_lip->li_ail.ail_back;
476 }
477 ASSERT((next_lip == (xfs_log_item_t*)base) ||
478 (XFS_LSN_CMP(next_lip->li_lsn, lip->li_lsn) <= 0));
479 lip->li_ail.ail_forw = next_lip->li_ail.ail_forw;
480 lip->li_ail.ail_back = next_lip;
481 next_lip->li_ail.ail_forw = lip;
482 lip->li_ail.ail_forw->li_ail.ail_back = lip;
483
484 xfs_ail_check(base);
485 return;
486}
487
488/*
489 * Delete the given item from the AIL. Return a pointer to the item.
490 */
491/*ARGSUSED*/
492STATIC xfs_log_item_t *
493xfs_ail_delete(
494 xfs_ail_entry_t *base,
495 xfs_log_item_t *lip)
496/* ARGSUSED */
497{
498 lip->li_ail.ail_forw->li_ail.ail_back = lip->li_ail.ail_back;
499 lip->li_ail.ail_back->li_ail.ail_forw = lip->li_ail.ail_forw;
500 lip->li_ail.ail_forw = NULL;
501 lip->li_ail.ail_back = NULL;
502
503 xfs_ail_check(base);
504 return lip;
505}
506
507/*
508 * Return a pointer to the first item in the AIL.
509 * If the AIL is empty, then return NULL.
510 */
511STATIC xfs_log_item_t *
512xfs_ail_min(
513 xfs_ail_entry_t *base)
514/* ARGSUSED */
515{
516 register xfs_log_item_t *forw = base->ail_forw;
517 if (forw == (xfs_log_item_t*)base) {
518 return NULL;
519 }
520 return forw;
521}
522
523/*
524 * Return a pointer to the item which follows
525 * the given item in the AIL. If the given item
526 * is the last item in the list, then return NULL.
527 */
528STATIC xfs_log_item_t *
529xfs_ail_next(
530 xfs_ail_entry_t *base,
531 xfs_log_item_t *lip)
532/* ARGSUSED */
533{
534 if (lip->li_ail.ail_forw == (xfs_log_item_t*)base) {
535 return NULL;
536 }
537 return lip->li_ail.ail_forw;
538
539}
540
541#ifdef DEBUG
542/*
543 * Check that the list is sorted as it should be.
544 */
545STATIC void
546xfs_ail_check(
547 xfs_ail_entry_t *base)
548{
549 xfs_log_item_t *lip;
550 xfs_log_item_t *prev_lip;
551
552 lip = base->ail_forw;
553 if (lip == (xfs_log_item_t*)base) {
554 /*
555 * Make sure the pointers are correct when the list
556 * is empty.
557 */
558 ASSERT(base->ail_back == (xfs_log_item_t*)base);
559 return;
560 }
561
562 /*
563 * Walk the list checking forward and backward pointers,
564 * lsn ordering, and that every entry has the XFS_LI_IN_AIL
565 * flag set.
566 */
567 prev_lip = (xfs_log_item_t*)base;
568 while (lip != (xfs_log_item_t*)base) {
569 if (prev_lip != (xfs_log_item_t*)base) {
570 ASSERT(prev_lip->li_ail.ail_forw == lip);
571 ASSERT(XFS_LSN_CMP(prev_lip->li_lsn, lip->li_lsn) <= 0);
572 }
573 ASSERT(lip->li_ail.ail_back == prev_lip);
574 ASSERT((lip->li_flags & XFS_LI_IN_AIL) != 0);
575 prev_lip = lip;
576 lip = lip->li_ail.ail_forw;
577 }
578 ASSERT(lip == (xfs_log_item_t*)base);
579 ASSERT(base->ail_back == prev_lip);
580}
581#endif /* DEBUG */