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