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