Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux...
[linux-2.6] / fs / xfs / xfs_log.c
1 /*
2  * Copyright (c) 2000-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_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_error.h"
31 #include "xfs_log_priv.h"
32 #include "xfs_buf_item.h"
33 #include "xfs_bmap_btree.h"
34 #include "xfs_alloc_btree.h"
35 #include "xfs_ialloc_btree.h"
36 #include "xfs_log_recover.h"
37 #include "xfs_trans_priv.h"
38 #include "xfs_dir2_sf.h"
39 #include "xfs_attr_sf.h"
40 #include "xfs_dinode.h"
41 #include "xfs_inode.h"
42 #include "xfs_rw.h"
43
44 kmem_zone_t     *xfs_log_ticket_zone;
45
46 #define xlog_write_adv_cnt(ptr, len, off, bytes) \
47         { (ptr) += (bytes); \
48           (len) -= (bytes); \
49           (off) += (bytes);}
50
51 /* Local miscellaneous function prototypes */
52 STATIC int       xlog_bdstrat_cb(struct xfs_buf *);
53 STATIC int       xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
54                                     xlog_in_core_t **, xfs_lsn_t *);
55 STATIC xlog_t *  xlog_alloc_log(xfs_mount_t     *mp,
56                                 xfs_buftarg_t   *log_target,
57                                 xfs_daddr_t     blk_offset,
58                                 int             num_bblks);
59 STATIC int       xlog_space_left(xlog_t *log, int cycle, int bytes);
60 STATIC int       xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
61 STATIC void      xlog_dealloc_log(xlog_t *log);
62 STATIC int       xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
63                             int nentries, xfs_log_ticket_t tic,
64                             xfs_lsn_t *start_lsn,
65                             xlog_in_core_t **commit_iclog,
66                             uint flags);
67
68 /* local state machine functions */
69 STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
70 STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
71 STATIC int  xlog_state_get_iclog_space(xlog_t           *log,
72                                        int              len,
73                                        xlog_in_core_t   **iclog,
74                                        xlog_ticket_t    *ticket,
75                                        int              *continued_write,
76                                        int              *logoffsetp);
77 STATIC int  xlog_state_release_iclog(xlog_t             *log,
78                                      xlog_in_core_t     *iclog);
79 STATIC void xlog_state_switch_iclogs(xlog_t             *log,
80                                      xlog_in_core_t *iclog,
81                                      int                eventual_size);
82 STATIC int  xlog_state_sync(xlog_t                      *log,
83                             xfs_lsn_t                   lsn,
84                             uint                        flags,
85                             int                         *log_flushed);
86 STATIC int  xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
87 STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
88
89 /* local functions to manipulate grant head */
90 STATIC int  xlog_grant_log_space(xlog_t         *log,
91                                  xlog_ticket_t  *xtic);
92 STATIC void xlog_grant_push_ail(xfs_mount_t     *mp,
93                                 int             need_bytes);
94 STATIC void xlog_regrant_reserve_log_space(xlog_t        *log,
95                                            xlog_ticket_t *ticket);
96 STATIC int xlog_regrant_write_log_space(xlog_t          *log,
97                                          xlog_ticket_t  *ticket);
98 STATIC void xlog_ungrant_log_space(xlog_t        *log,
99                                    xlog_ticket_t *ticket);
100
101
102 /* local ticket functions */
103 STATIC xlog_ticket_t    *xlog_ticket_get(xlog_t *log,
104                                          int    unit_bytes,
105                                          int    count,
106                                          char   clientid,
107                                          uint   flags);
108 STATIC void             xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
109
110 #if defined(DEBUG)
111 STATIC void     xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
112 STATIC void     xlog_verify_grant_head(xlog_t *log, int equals);
113 STATIC void     xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
114                                   int count, boolean_t syncing);
115 STATIC void     xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
116                                      xfs_lsn_t tail_lsn);
117 #else
118 #define xlog_verify_dest_ptr(a,b)
119 #define xlog_verify_grant_head(a,b)
120 #define xlog_verify_iclog(a,b,c,d)
121 #define xlog_verify_tail_lsn(a,b,c)
122 #endif
123
124 STATIC int      xlog_iclogs_empty(xlog_t *log);
125
126 #if defined(XFS_LOG_TRACE)
127
128 #define XLOG_TRACE_LOGGRANT_SIZE        2048
129 #define XLOG_TRACE_ICLOG_SIZE           256
130
131 void
132 xlog_trace_loggrant_alloc(xlog_t *log)
133 {
134         log->l_grant_trace = ktrace_alloc(XLOG_TRACE_LOGGRANT_SIZE, KM_NOFS);
135 }
136
137 void
138 xlog_trace_loggrant_dealloc(xlog_t *log)
139 {
140         ktrace_free(log->l_grant_trace);
141 }
142
143 void
144 xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
145 {
146         unsigned long cnts;
147
148         /* ticket counts are 1 byte each */
149         cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
150
151         ktrace_enter(log->l_grant_trace,
152                      (void *)tic,
153                      (void *)log->l_reserve_headq,
154                      (void *)log->l_write_headq,
155                      (void *)((unsigned long)log->l_grant_reserve_cycle),
156                      (void *)((unsigned long)log->l_grant_reserve_bytes),
157                      (void *)((unsigned long)log->l_grant_write_cycle),
158                      (void *)((unsigned long)log->l_grant_write_bytes),
159                      (void *)((unsigned long)log->l_curr_cycle),
160                      (void *)((unsigned long)log->l_curr_block),
161                      (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
162                      (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
163                      (void *)string,
164                      (void *)((unsigned long)tic->t_trans_type),
165                      (void *)cnts,
166                      (void *)((unsigned long)tic->t_curr_res),
167                      (void *)((unsigned long)tic->t_unit_res));
168 }
169
170 void
171 xlog_trace_iclog_alloc(xlog_in_core_t *iclog)
172 {
173         iclog->ic_trace = ktrace_alloc(XLOG_TRACE_ICLOG_SIZE, KM_NOFS);
174 }
175
176 void
177 xlog_trace_iclog_dealloc(xlog_in_core_t *iclog)
178 {
179         ktrace_free(iclog->ic_trace);
180 }
181
182 void
183 xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
184 {
185         ktrace_enter(iclog->ic_trace,
186                      (void *)((unsigned long)state),
187                      (void *)((unsigned long)current_pid()),
188                      (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
189                      (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
190                      (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
191                      (void *)NULL, (void *)NULL);
192 }
193 #else
194
195 #define xlog_trace_loggrant_alloc(log)
196 #define xlog_trace_loggrant_dealloc(log)
197 #define xlog_trace_loggrant(log,tic,string)
198
199 #define xlog_trace_iclog_alloc(iclog)
200 #define xlog_trace_iclog_dealloc(iclog)
201 #define xlog_trace_iclog(iclog,state)
202
203 #endif /* XFS_LOG_TRACE */
204
205
206 static void
207 xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
208 {
209         if (*qp) {
210                 tic->t_next         = (*qp);
211                 tic->t_prev         = (*qp)->t_prev;
212                 (*qp)->t_prev->t_next = tic;
213                 (*qp)->t_prev       = tic;
214         } else {
215                 tic->t_prev = tic->t_next = tic;
216                 *qp = tic;
217         }
218
219         tic->t_flags |= XLOG_TIC_IN_Q;
220 }
221
222 static void
223 xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
224 {
225         if (tic == tic->t_next) {
226                 *qp = NULL;
227         } else {
228                 *qp = tic->t_next;
229                 tic->t_next->t_prev = tic->t_prev;
230                 tic->t_prev->t_next = tic->t_next;
231         }
232
233         tic->t_next = tic->t_prev = NULL;
234         tic->t_flags &= ~XLOG_TIC_IN_Q;
235 }
236
237 static void
238 xlog_grant_sub_space(struct log *log, int bytes)
239 {
240         log->l_grant_write_bytes -= bytes;
241         if (log->l_grant_write_bytes < 0) {
242                 log->l_grant_write_bytes += log->l_logsize;
243                 log->l_grant_write_cycle--;
244         }
245
246         log->l_grant_reserve_bytes -= bytes;
247         if ((log)->l_grant_reserve_bytes < 0) {
248                 log->l_grant_reserve_bytes += log->l_logsize;
249                 log->l_grant_reserve_cycle--;
250         }
251
252 }
253
254 static void
255 xlog_grant_add_space_write(struct log *log, int bytes)
256 {
257         int tmp = log->l_logsize - log->l_grant_write_bytes;
258         if (tmp > bytes)
259                 log->l_grant_write_bytes += bytes;
260         else {
261                 log->l_grant_write_cycle++;
262                 log->l_grant_write_bytes = bytes - tmp;
263         }
264 }
265
266 static void
267 xlog_grant_add_space_reserve(struct log *log, int bytes)
268 {
269         int tmp = log->l_logsize - log->l_grant_reserve_bytes;
270         if (tmp > bytes)
271                 log->l_grant_reserve_bytes += bytes;
272         else {
273                 log->l_grant_reserve_cycle++;
274                 log->l_grant_reserve_bytes = bytes - tmp;
275         }
276 }
277
278 static inline void
279 xlog_grant_add_space(struct log *log, int bytes)
280 {
281         xlog_grant_add_space_write(log, bytes);
282         xlog_grant_add_space_reserve(log, bytes);
283 }
284
285 static void
286 xlog_tic_reset_res(xlog_ticket_t *tic)
287 {
288         tic->t_res_num = 0;
289         tic->t_res_arr_sum = 0;
290         tic->t_res_num_ophdrs = 0;
291 }
292
293 static void
294 xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
295 {
296         if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
297                 /* add to overflow and start again */
298                 tic->t_res_o_flow += tic->t_res_arr_sum;
299                 tic->t_res_num = 0;
300                 tic->t_res_arr_sum = 0;
301         }
302
303         tic->t_res_arr[tic->t_res_num].r_len = len;
304         tic->t_res_arr[tic->t_res_num].r_type = type;
305         tic->t_res_arr_sum += len;
306         tic->t_res_num++;
307 }
308
309 /*
310  * NOTES:
311  *
312  *      1. currblock field gets updated at startup and after in-core logs
313  *              marked as with WANT_SYNC.
314  */
315
316 /*
317  * This routine is called when a user of a log manager ticket is done with
318  * the reservation.  If the ticket was ever used, then a commit record for
319  * the associated transaction is written out as a log operation header with
320  * no data.  The flag XLOG_TIC_INITED is set when the first write occurs with
321  * a given ticket.  If the ticket was one with a permanent reservation, then
322  * a few operations are done differently.  Permanent reservation tickets by
323  * default don't release the reservation.  They just commit the current
324  * transaction with the belief that the reservation is still needed.  A flag
325  * must be passed in before permanent reservations are actually released.
326  * When these type of tickets are not released, they need to be set into
327  * the inited state again.  By doing this, a start record will be written
328  * out when the next write occurs.
329  */
330 xfs_lsn_t
331 xfs_log_done(xfs_mount_t        *mp,
332              xfs_log_ticket_t   xtic,
333              void               **iclog,
334              uint               flags)
335 {
336         xlog_t          *log    = mp->m_log;
337         xlog_ticket_t   *ticket = (xfs_log_ticket_t) xtic;
338         xfs_lsn_t       lsn     = 0;
339
340         if (XLOG_FORCED_SHUTDOWN(log) ||
341             /*
342              * If nothing was ever written, don't write out commit record.
343              * If we get an error, just continue and give back the log ticket.
344              */
345             (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
346              (xlog_commit_record(mp, ticket,
347                                  (xlog_in_core_t **)iclog, &lsn)))) {
348                 lsn = (xfs_lsn_t) -1;
349                 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
350                         flags |= XFS_LOG_REL_PERM_RESERV;
351                 }
352         }
353
354
355         if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
356             (flags & XFS_LOG_REL_PERM_RESERV)) {
357                 /*
358                  * Release ticket if not permanent reservation or a specific
359                  * request has been made to release a permanent reservation.
360                  */
361                 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
362                 xlog_ungrant_log_space(log, ticket);
363                 xlog_ticket_put(log, ticket);
364         } else {
365                 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
366                 xlog_regrant_reserve_log_space(log, ticket);
367                 /* If this ticket was a permanent reservation and we aren't
368                  * trying to release it, reset the inited flags; so next time
369                  * we write, a start record will be written out.
370                  */
371                 ticket->t_flags |= XLOG_TIC_INITED;
372         }
373
374         return lsn;
375 }       /* xfs_log_done */
376
377
378 /*
379  * Force the in-core log to disk.  If flags == XFS_LOG_SYNC,
380  *      the force is done synchronously.
381  *
382  * Asynchronous forces are implemented by setting the WANT_SYNC
383  * bit in the appropriate in-core log and then returning.
384  *
385  * Synchronous forces are implemented with a signal variable. All callers
386  * to force a given lsn to disk will wait on a the sv attached to the
387  * specific in-core log.  When given in-core log finally completes its
388  * write to disk, that thread will wake up all threads waiting on the
389  * sv.
390  */
391 int
392 _xfs_log_force(
393         xfs_mount_t     *mp,
394         xfs_lsn_t       lsn,
395         uint            flags,
396         int             *log_flushed)
397 {
398         xlog_t          *log = mp->m_log;
399         int             dummy;
400
401         if (!log_flushed)
402                 log_flushed = &dummy;
403
404         ASSERT(flags & XFS_LOG_FORCE);
405
406         XFS_STATS_INC(xs_log_force);
407
408         if (log->l_flags & XLOG_IO_ERROR)
409                 return XFS_ERROR(EIO);
410         if (lsn == 0)
411                 return xlog_state_sync_all(log, flags, log_flushed);
412         else
413                 return xlog_state_sync(log, lsn, flags, log_flushed);
414 }       /* _xfs_log_force */
415
416 /*
417  * Wrapper for _xfs_log_force(), to be used when caller doesn't care
418  * about errors or whether the log was flushed or not. This is the normal
419  * interface to use when trying to unpin items or move the log forward.
420  */
421 void
422 xfs_log_force(
423         xfs_mount_t     *mp,
424         xfs_lsn_t       lsn,
425         uint            flags)
426 {
427         int     error;
428         error = _xfs_log_force(mp, lsn, flags, NULL);
429         if (error) {
430                 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
431                         "error %d returned.", error);
432         }
433 }
434
435
436 /*
437  * Attaches a new iclog I/O completion callback routine during
438  * transaction commit.  If the log is in error state, a non-zero
439  * return code is handed back and the caller is responsible for
440  * executing the callback at an appropriate time.
441  */
442 int
443 xfs_log_notify(xfs_mount_t        *mp,          /* mount of partition */
444                void               *iclog_hndl,  /* iclog to hang callback off */
445                xfs_log_callback_t *cb)
446 {
447         xlog_in_core_t    *iclog = (xlog_in_core_t *)iclog_hndl;
448         int     abortflg;
449
450         spin_lock(&iclog->ic_callback_lock);
451         abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
452         if (!abortflg) {
453                 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
454                               (iclog->ic_state == XLOG_STATE_WANT_SYNC));
455                 cb->cb_next = NULL;
456                 *(iclog->ic_callback_tail) = cb;
457                 iclog->ic_callback_tail = &(cb->cb_next);
458         }
459         spin_unlock(&iclog->ic_callback_lock);
460         return abortflg;
461 }       /* xfs_log_notify */
462
463 int
464 xfs_log_release_iclog(xfs_mount_t *mp,
465                       void        *iclog_hndl)
466 {
467         xlog_t *log = mp->m_log;
468         xlog_in_core_t    *iclog = (xlog_in_core_t *)iclog_hndl;
469
470         if (xlog_state_release_iclog(log, iclog)) {
471                 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
472                 return EIO;
473         }
474
475         return 0;
476 }
477
478 /*
479  *  1. Reserve an amount of on-disk log space and return a ticket corresponding
480  *      to the reservation.
481  *  2. Potentially, push buffers at tail of log to disk.
482  *
483  * Each reservation is going to reserve extra space for a log record header.
484  * When writes happen to the on-disk log, we don't subtract the length of the
485  * log record header from any reservation.  By wasting space in each
486  * reservation, we prevent over allocation problems.
487  */
488 int
489 xfs_log_reserve(xfs_mount_t      *mp,
490                 int              unit_bytes,
491                 int              cnt,
492                 xfs_log_ticket_t *ticket,
493                 __uint8_t        client,
494                 uint             flags,
495                 uint             t_type)
496 {
497         xlog_t          *log = mp->m_log;
498         xlog_ticket_t   *internal_ticket;
499         int             retval = 0;
500
501         ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
502         ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
503
504         if (XLOG_FORCED_SHUTDOWN(log))
505                 return XFS_ERROR(EIO);
506
507         XFS_STATS_INC(xs_try_logspace);
508
509         if (*ticket != NULL) {
510                 ASSERT(flags & XFS_LOG_PERM_RESERV);
511                 internal_ticket = (xlog_ticket_t *)*ticket;
512                 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
513                 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
514                 retval = xlog_regrant_write_log_space(log, internal_ticket);
515         } else {
516                 /* may sleep if need to allocate more tickets */
517                 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
518                                                   client, flags);
519                 if (!internal_ticket)
520                         return XFS_ERROR(ENOMEM);
521                 internal_ticket->t_trans_type = t_type;
522                 *ticket = internal_ticket;
523                 xlog_trace_loggrant(log, internal_ticket, 
524                         (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
525                         "xfs_log_reserve: create new ticket (permanent trans)" :
526                         "xfs_log_reserve: create new ticket");
527                 xlog_grant_push_ail(mp,
528                                     (internal_ticket->t_unit_res *
529                                      internal_ticket->t_cnt));
530                 retval = xlog_grant_log_space(log, internal_ticket);
531         }
532
533         return retval;
534 }       /* xfs_log_reserve */
535
536
537 /*
538  * Mount a log filesystem
539  *
540  * mp           - ubiquitous xfs mount point structure
541  * log_target   - buftarg of on-disk log device
542  * blk_offset   - Start block # where block size is 512 bytes (BBSIZE)
543  * num_bblocks  - Number of BBSIZE blocks in on-disk log
544  *
545  * Return error or zero.
546  */
547 int
548 xfs_log_mount(
549         xfs_mount_t     *mp,
550         xfs_buftarg_t   *log_target,
551         xfs_daddr_t     blk_offset,
552         int             num_bblks)
553 {
554         int             error;
555
556         if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
557                 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
558         else {
559                 cmn_err(CE_NOTE,
560                         "!Mounting filesystem \"%s\" in no-recovery mode.  Filesystem will be inconsistent.",
561                         mp->m_fsname);
562                 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
563         }
564
565         mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
566         if (!mp->m_log) {
567                 cmn_err(CE_WARN, "XFS: Log allocation failed: No memory!");
568                 error = ENOMEM;
569                 goto out;
570         }
571
572         /*
573          * Initialize the AIL now we have a log.
574          */
575         spin_lock_init(&mp->m_ail_lock);
576         error = xfs_trans_ail_init(mp);
577         if (error) {
578                 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
579                 goto error;
580         }
581
582         /*
583          * skip log recovery on a norecovery mount.  pretend it all
584          * just worked.
585          */
586         if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
587                 int     readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
588
589                 if (readonly)
590                         mp->m_flags &= ~XFS_MOUNT_RDONLY;
591
592                 error = xlog_recover(mp->m_log);
593
594                 if (readonly)
595                         mp->m_flags |= XFS_MOUNT_RDONLY;
596                 if (error) {
597                         cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
598                         goto error;
599                 }
600         }
601
602         /* Normal transactions can now occur */
603         mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
604
605         /* End mounting message in xfs_log_mount_finish */
606         return 0;
607 error:
608         xfs_log_unmount_dealloc(mp);
609 out:
610         return error;
611 }       /* xfs_log_mount */
612
613 /*
614  * Finish the recovery of the file system.  This is separate from
615  * the xfs_log_mount() call, because it depends on the code in
616  * xfs_mountfs() to read in the root and real-time bitmap inodes
617  * between calling xfs_log_mount() and here.
618  *
619  * mp           - ubiquitous xfs mount point structure
620  */
621 int
622 xfs_log_mount_finish(xfs_mount_t *mp)
623 {
624         int     error;
625
626         if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
627                 error = xlog_recover_finish(mp->m_log);
628         else {
629                 error = 0;
630                 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
631         }
632
633         return error;
634 }
635
636 /*
637  * Unmount processing for the log.
638  */
639 int
640 xfs_log_unmount(xfs_mount_t *mp)
641 {
642         int             error;
643
644         error = xfs_log_unmount_write(mp);
645         xfs_log_unmount_dealloc(mp);
646         return error;
647 }
648
649 /*
650  * Final log writes as part of unmount.
651  *
652  * Mark the filesystem clean as unmount happens.  Note that during relocation
653  * this routine needs to be executed as part of source-bag while the
654  * deallocation must not be done until source-end.
655  */
656
657 /*
658  * Unmount record used to have a string "Unmount filesystem--" in the
659  * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
660  * We just write the magic number now since that particular field isn't
661  * currently architecture converted and "nUmount" is a bit foo.
662  * As far as I know, there weren't any dependencies on the old behaviour.
663  */
664
665 int
666 xfs_log_unmount_write(xfs_mount_t *mp)
667 {
668         xlog_t           *log = mp->m_log;
669         xlog_in_core_t   *iclog;
670 #ifdef DEBUG
671         xlog_in_core_t   *first_iclog;
672 #endif
673         xfs_log_iovec_t  reg[1];
674         xfs_log_ticket_t tic = NULL;
675         xfs_lsn_t        lsn;
676         int              error;
677
678         /* the data section must be 32 bit size aligned */
679         struct {
680             __uint16_t magic;
681             __uint16_t pad1;
682             __uint32_t pad2; /* may as well make it 64 bits */
683         } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
684
685         /*
686          * Don't write out unmount record on read-only mounts.
687          * Or, if we are doing a forced umount (typically because of IO errors).
688          */
689         if (mp->m_flags & XFS_MOUNT_RDONLY)
690                 return 0;
691
692         error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
693         ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
694
695 #ifdef DEBUG
696         first_iclog = iclog = log->l_iclog;
697         do {
698                 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
699                         ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
700                         ASSERT(iclog->ic_offset == 0);
701                 }
702                 iclog = iclog->ic_next;
703         } while (iclog != first_iclog);
704 #endif
705         if (! (XLOG_FORCED_SHUTDOWN(log))) {
706                 reg[0].i_addr = (void*)&magic;
707                 reg[0].i_len  = sizeof(magic);
708                 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
709
710                 error = xfs_log_reserve(mp, 600, 1, &tic,
711                                         XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
712                 if (!error) {
713                         /* remove inited flag */
714                         ((xlog_ticket_t *)tic)->t_flags = 0;
715                         error = xlog_write(mp, reg, 1, tic, &lsn,
716                                            NULL, XLOG_UNMOUNT_TRANS);
717                         /*
718                          * At this point, we're umounting anyway,
719                          * so there's no point in transitioning log state
720                          * to IOERROR. Just continue...
721                          */
722                 }
723
724                 if (error) {
725                         xfs_fs_cmn_err(CE_ALERT, mp,
726                                 "xfs_log_unmount: unmount record failed");
727                 }
728
729
730                 spin_lock(&log->l_icloglock);
731                 iclog = log->l_iclog;
732                 atomic_inc(&iclog->ic_refcnt);
733                 spin_unlock(&log->l_icloglock);
734                 xlog_state_want_sync(log, iclog);
735                 error = xlog_state_release_iclog(log, iclog);
736
737                 spin_lock(&log->l_icloglock);
738                 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
739                       iclog->ic_state == XLOG_STATE_DIRTY)) {
740                         if (!XLOG_FORCED_SHUTDOWN(log)) {
741                                 sv_wait(&iclog->ic_force_wait, PMEM,
742                                         &log->l_icloglock, s);
743                         } else {
744                                 spin_unlock(&log->l_icloglock);
745                         }
746                 } else {
747                         spin_unlock(&log->l_icloglock);
748                 }
749                 if (tic) {
750                         xlog_trace_loggrant(log, tic, "unmount rec");
751                         xlog_ungrant_log_space(log, tic);
752                         xlog_ticket_put(log, tic);
753                 }
754         } else {
755                 /*
756                  * We're already in forced_shutdown mode, couldn't
757                  * even attempt to write out the unmount transaction.
758                  *
759                  * Go through the motions of sync'ing and releasing
760                  * the iclog, even though no I/O will actually happen,
761                  * we need to wait for other log I/Os that may already
762                  * be in progress.  Do this as a separate section of
763                  * code so we'll know if we ever get stuck here that
764                  * we're in this odd situation of trying to unmount
765                  * a file system that went into forced_shutdown as
766                  * the result of an unmount..
767                  */
768                 spin_lock(&log->l_icloglock);
769                 iclog = log->l_iclog;
770                 atomic_inc(&iclog->ic_refcnt);
771                 spin_unlock(&log->l_icloglock);
772
773                 xlog_state_want_sync(log, iclog);
774                 error =  xlog_state_release_iclog(log, iclog);
775
776                 spin_lock(&log->l_icloglock);
777
778                 if ( ! (   iclog->ic_state == XLOG_STATE_ACTIVE
779                         || iclog->ic_state == XLOG_STATE_DIRTY
780                         || iclog->ic_state == XLOG_STATE_IOERROR) ) {
781
782                                 sv_wait(&iclog->ic_force_wait, PMEM,
783                                         &log->l_icloglock, s);
784                 } else {
785                         spin_unlock(&log->l_icloglock);
786                 }
787         }
788
789         return error;
790 }       /* xfs_log_unmount_write */
791
792 /*
793  * Deallocate log structures for unmount/relocation.
794  *
795  * We need to stop the aild from running before we destroy
796  * and deallocate the log as the aild references the log.
797  */
798 void
799 xfs_log_unmount_dealloc(xfs_mount_t *mp)
800 {
801         xfs_trans_ail_destroy(mp);
802         xlog_dealloc_log(mp->m_log);
803 }
804
805 /*
806  * Write region vectors to log.  The write happens using the space reservation
807  * of the ticket (tic).  It is not a requirement that all writes for a given
808  * transaction occur with one call to xfs_log_write().
809  */
810 int
811 xfs_log_write(xfs_mount_t *     mp,
812               xfs_log_iovec_t   reg[],
813               int               nentries,
814               xfs_log_ticket_t  tic,
815               xfs_lsn_t         *start_lsn)
816 {
817         int     error;
818         xlog_t *log = mp->m_log;
819
820         if (XLOG_FORCED_SHUTDOWN(log))
821                 return XFS_ERROR(EIO);
822
823         if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
824                 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
825         }
826         return error;
827 }       /* xfs_log_write */
828
829
830 void
831 xfs_log_move_tail(xfs_mount_t   *mp,
832                   xfs_lsn_t     tail_lsn)
833 {
834         xlog_ticket_t   *tic;
835         xlog_t          *log = mp->m_log;
836         int             need_bytes, free_bytes, cycle, bytes;
837
838         if (XLOG_FORCED_SHUTDOWN(log))
839                 return;
840
841         if (tail_lsn == 0) {
842                 /* needed since sync_lsn is 64 bits */
843                 spin_lock(&log->l_icloglock);
844                 tail_lsn = log->l_last_sync_lsn;
845                 spin_unlock(&log->l_icloglock);
846         }
847
848         spin_lock(&log->l_grant_lock);
849
850         /* Also an invalid lsn.  1 implies that we aren't passing in a valid
851          * tail_lsn.
852          */
853         if (tail_lsn != 1) {
854                 log->l_tail_lsn = tail_lsn;
855         }
856
857         if ((tic = log->l_write_headq)) {
858 #ifdef DEBUG
859                 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
860                         panic("Recovery problem");
861 #endif
862                 cycle = log->l_grant_write_cycle;
863                 bytes = log->l_grant_write_bytes;
864                 free_bytes = xlog_space_left(log, cycle, bytes);
865                 do {
866                         ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
867
868                         if (free_bytes < tic->t_unit_res && tail_lsn != 1)
869                                 break;
870                         tail_lsn = 0;
871                         free_bytes -= tic->t_unit_res;
872                         sv_signal(&tic->t_wait);
873                         tic = tic->t_next;
874                 } while (tic != log->l_write_headq);
875         }
876         if ((tic = log->l_reserve_headq)) {
877 #ifdef DEBUG
878                 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
879                         panic("Recovery problem");
880 #endif
881                 cycle = log->l_grant_reserve_cycle;
882                 bytes = log->l_grant_reserve_bytes;
883                 free_bytes = xlog_space_left(log, cycle, bytes);
884                 do {
885                         if (tic->t_flags & XLOG_TIC_PERM_RESERV)
886                                 need_bytes = tic->t_unit_res*tic->t_cnt;
887                         else
888                                 need_bytes = tic->t_unit_res;
889                         if (free_bytes < need_bytes && tail_lsn != 1)
890                                 break;
891                         tail_lsn = 0;
892                         free_bytes -= need_bytes;
893                         sv_signal(&tic->t_wait);
894                         tic = tic->t_next;
895                 } while (tic != log->l_reserve_headq);
896         }
897         spin_unlock(&log->l_grant_lock);
898 }       /* xfs_log_move_tail */
899
900 /*
901  * Determine if we have a transaction that has gone to disk
902  * that needs to be covered. Log activity needs to be idle (no AIL and
903  * nothing in the iclogs). And, we need to be in the right state indicating
904  * something has gone out.
905  */
906 int
907 xfs_log_need_covered(xfs_mount_t *mp)
908 {
909         int             needed = 0, gen;
910         xlog_t          *log = mp->m_log;
911
912         if (!xfs_fs_writable(mp))
913                 return 0;
914
915         spin_lock(&log->l_icloglock);
916         if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
917                 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
918                         && !xfs_trans_first_ail(mp, &gen)
919                         && xlog_iclogs_empty(log)) {
920                 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
921                         log->l_covered_state = XLOG_STATE_COVER_DONE;
922                 else {
923                         ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
924                         log->l_covered_state = XLOG_STATE_COVER_DONE2;
925                 }
926                 needed = 1;
927         }
928         spin_unlock(&log->l_icloglock);
929         return needed;
930 }
931
932 /******************************************************************************
933  *
934  *      local routines
935  *
936  ******************************************************************************
937  */
938
939 /* xfs_trans_tail_ail returns 0 when there is nothing in the list.
940  * The log manager must keep track of the last LR which was committed
941  * to disk.  The lsn of this LR will become the new tail_lsn whenever
942  * xfs_trans_tail_ail returns 0.  If we don't do this, we run into
943  * the situation where stuff could be written into the log but nothing
944  * was ever in the AIL when asked.  Eventually, we panic since the
945  * tail hits the head.
946  *
947  * We may be holding the log iclog lock upon entering this routine.
948  */
949 xfs_lsn_t
950 xlog_assign_tail_lsn(xfs_mount_t *mp)
951 {
952         xfs_lsn_t tail_lsn;
953         xlog_t    *log = mp->m_log;
954
955         tail_lsn = xfs_trans_tail_ail(mp);
956         spin_lock(&log->l_grant_lock);
957         if (tail_lsn != 0) {
958                 log->l_tail_lsn = tail_lsn;
959         } else {
960                 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
961         }
962         spin_unlock(&log->l_grant_lock);
963
964         return tail_lsn;
965 }       /* xlog_assign_tail_lsn */
966
967
968 /*
969  * Return the space in the log between the tail and the head.  The head
970  * is passed in the cycle/bytes formal parms.  In the special case where
971  * the reserve head has wrapped passed the tail, this calculation is no
972  * longer valid.  In this case, just return 0 which means there is no space
973  * in the log.  This works for all places where this function is called
974  * with the reserve head.  Of course, if the write head were to ever
975  * wrap the tail, we should blow up.  Rather than catch this case here,
976  * we depend on other ASSERTions in other parts of the code.   XXXmiken
977  *
978  * This code also handles the case where the reservation head is behind
979  * the tail.  The details of this case are described below, but the end
980  * result is that we return the size of the log as the amount of space left.
981  */
982 STATIC int
983 xlog_space_left(xlog_t *log, int cycle, int bytes)
984 {
985         int free_bytes;
986         int tail_bytes;
987         int tail_cycle;
988
989         tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
990         tail_cycle = CYCLE_LSN(log->l_tail_lsn);
991         if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
992                 free_bytes = log->l_logsize - (bytes - tail_bytes);
993         } else if ((tail_cycle + 1) < cycle) {
994                 return 0;
995         } else if (tail_cycle < cycle) {
996                 ASSERT(tail_cycle == (cycle - 1));
997                 free_bytes = tail_bytes - bytes;
998         } else {
999                 /*
1000                  * The reservation head is behind the tail.
1001                  * In this case we just want to return the size of the
1002                  * log as the amount of space left.
1003                  */
1004                 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
1005                         "xlog_space_left: head behind tail\n"
1006                         "  tail_cycle = %d, tail_bytes = %d\n"
1007                         "  GH   cycle = %d, GH   bytes = %d",
1008                         tail_cycle, tail_bytes, cycle, bytes);
1009                 ASSERT(0);
1010                 free_bytes = log->l_logsize;
1011         }
1012         return free_bytes;
1013 }       /* xlog_space_left */
1014
1015
1016 /*
1017  * Log function which is called when an io completes.
1018  *
1019  * The log manager needs its own routine, in order to control what
1020  * happens with the buffer after the write completes.
1021  */
1022 void
1023 xlog_iodone(xfs_buf_t *bp)
1024 {
1025         xlog_in_core_t  *iclog;
1026         xlog_t          *l;
1027         int             aborted;
1028
1029         iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1030         ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
1031         XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1032         aborted = 0;
1033
1034         /*
1035          * Some versions of cpp barf on the recursive definition of
1036          * ic_log -> hic_fields.ic_log and expand ic_log twice when
1037          * it is passed through two macros.  Workaround broken cpp.
1038          */
1039         l = iclog->ic_log;
1040
1041         /*
1042          * If the _XFS_BARRIER_FAILED flag was set by a lower
1043          * layer, it means the underlying device no longer supports
1044          * barrier I/O. Warn loudly and turn off barriers.
1045          */
1046         if (bp->b_flags & _XFS_BARRIER_FAILED) {
1047                 bp->b_flags &= ~_XFS_BARRIER_FAILED;
1048                 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
1049                 xfs_fs_cmn_err(CE_WARN, l->l_mp,
1050                                 "xlog_iodone: Barriers are no longer supported"
1051                                 " by device. Disabling barriers\n");
1052                 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
1053         }
1054
1055         /*
1056          * Race to shutdown the filesystem if we see an error.
1057          */
1058         if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1059                         XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1060                 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1061                 XFS_BUF_STALE(bp);
1062                 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
1063                 /*
1064                  * This flag will be propagated to the trans-committed
1065                  * callback routines to let them know that the log-commit
1066                  * didn't succeed.
1067                  */
1068                 aborted = XFS_LI_ABORTED;
1069         } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1070                 aborted = XFS_LI_ABORTED;
1071         }
1072
1073         /* log I/O is always issued ASYNC */
1074         ASSERT(XFS_BUF_ISASYNC(bp));
1075         xlog_state_done_syncing(iclog, aborted);
1076         /*
1077          * do not reference the buffer (bp) here as we could race
1078          * with it being freed after writing the unmount record to the
1079          * log.
1080          */
1081
1082 }       /* xlog_iodone */
1083
1084 /*
1085  * The bdstrat callback function for log bufs. This gives us a central
1086  * place to trap bufs in case we get hit by a log I/O error and need to
1087  * shutdown. Actually, in practice, even when we didn't get a log error,
1088  * we transition the iclogs to IOERROR state *after* flushing all existing
1089  * iclogs to disk. This is because we don't want anymore new transactions to be
1090  * started or completed afterwards.
1091  */
1092 STATIC int
1093 xlog_bdstrat_cb(struct xfs_buf *bp)
1094 {
1095         xlog_in_core_t *iclog;
1096
1097         iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1098
1099         if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1100           /* note for irix bstrat will need  struct bdevsw passed
1101            * Fix the following macro if the code ever is merged
1102            */
1103             XFS_bdstrat(bp);
1104                 return 0;
1105         }
1106
1107         xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1108         XFS_BUF_ERROR(bp, EIO);
1109         XFS_BUF_STALE(bp);
1110         xfs_biodone(bp);
1111         return XFS_ERROR(EIO);
1112
1113
1114 }
1115
1116 /*
1117  * Return size of each in-core log record buffer.
1118  *
1119  * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
1120  *
1121  * If the filesystem blocksize is too large, we may need to choose a
1122  * larger size since the directory code currently logs entire blocks.
1123  */
1124
1125 STATIC void
1126 xlog_get_iclog_buffer_size(xfs_mount_t  *mp,
1127                            xlog_t       *log)
1128 {
1129         int size;
1130         int xhdrs;
1131
1132         if (mp->m_logbufs <= 0)
1133                 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1134         else
1135                 log->l_iclog_bufs = mp->m_logbufs;
1136
1137         /*
1138          * Buffer size passed in from mount system call.
1139          */
1140         if (mp->m_logbsize > 0) {
1141                 size = log->l_iclog_size = mp->m_logbsize;
1142                 log->l_iclog_size_log = 0;
1143                 while (size != 1) {
1144                         log->l_iclog_size_log++;
1145                         size >>= 1;
1146                 }
1147
1148                 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
1149                         /* # headers = size / 32K
1150                          * one header holds cycles from 32K of data
1151                          */
1152
1153                         xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1154                         if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1155                                 xhdrs++;
1156                         log->l_iclog_hsize = xhdrs << BBSHIFT;
1157                         log->l_iclog_heads = xhdrs;
1158                 } else {
1159                         ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1160                         log->l_iclog_hsize = BBSIZE;
1161                         log->l_iclog_heads = 1;
1162                 }
1163                 goto done;
1164         }
1165
1166         /* All machines use 32KB buffers by default. */
1167         log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1168         log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1169
1170         /* the default log size is 16k or 32k which is one header sector */
1171         log->l_iclog_hsize = BBSIZE;
1172         log->l_iclog_heads = 1;
1173
1174         /*
1175          * For 16KB, we use 3 32KB buffers.  For 32KB block sizes, we use
1176          * 4 32KB buffers.  For 64KB block sizes, we use 8 32KB buffers.
1177          */
1178         if (mp->m_sb.sb_blocksize >= 16*1024) {
1179                 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1180                 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1181                 if (mp->m_logbufs <= 0) {
1182                         switch (mp->m_sb.sb_blocksize) {
1183                             case 16*1024:                       /* 16 KB */
1184                                 log->l_iclog_bufs = 3;
1185                                 break;
1186                             case 32*1024:                       /* 32 KB */
1187                                 log->l_iclog_bufs = 4;
1188                                 break;
1189                             case 64*1024:                       /* 64 KB */
1190                                 log->l_iclog_bufs = 8;
1191                                 break;
1192                             default:
1193                                 xlog_panic("XFS: Invalid blocksize");
1194                                 break;
1195                         }
1196                 }
1197         }
1198
1199 done:   /* are we being asked to make the sizes selected above visible? */
1200         if (mp->m_logbufs == 0)
1201                 mp->m_logbufs = log->l_iclog_bufs;
1202         if (mp->m_logbsize == 0)
1203                 mp->m_logbsize = log->l_iclog_size;
1204 }       /* xlog_get_iclog_buffer_size */
1205
1206
1207 /*
1208  * This routine initializes some of the log structure for a given mount point.
1209  * Its primary purpose is to fill in enough, so recovery can occur.  However,
1210  * some other stuff may be filled in too.
1211  */
1212 STATIC xlog_t *
1213 xlog_alloc_log(xfs_mount_t      *mp,
1214                xfs_buftarg_t    *log_target,
1215                xfs_daddr_t      blk_offset,
1216                int              num_bblks)
1217 {
1218         xlog_t                  *log;
1219         xlog_rec_header_t       *head;
1220         xlog_in_core_t          **iclogp;
1221         xlog_in_core_t          *iclog, *prev_iclog=NULL;
1222         xfs_buf_t               *bp;
1223         int                     i;
1224         int                     iclogsize;
1225
1226         log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
1227         if (!log)
1228                 return NULL;
1229
1230         log->l_mp          = mp;
1231         log->l_targ        = log_target;
1232         log->l_logsize     = BBTOB(num_bblks);
1233         log->l_logBBstart  = blk_offset;
1234         log->l_logBBsize   = num_bblks;
1235         log->l_covered_state = XLOG_STATE_COVER_IDLE;
1236         log->l_flags       |= XLOG_ACTIVE_RECOVERY;
1237
1238         log->l_prev_block  = -1;
1239         log->l_tail_lsn    = xlog_assign_lsn(1, 0);
1240         /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1241         log->l_last_sync_lsn = log->l_tail_lsn;
1242         log->l_curr_cycle  = 1;     /* 0 is bad since this is initial value */
1243         log->l_grant_reserve_cycle = 1;
1244         log->l_grant_write_cycle = 1;
1245
1246         if (xfs_sb_version_hassector(&mp->m_sb)) {
1247                 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1248                 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1249                 /* for larger sector sizes, must have v2 or external log */
1250                 ASSERT(log->l_sectbb_log == 0 ||
1251                         log->l_logBBstart == 0 ||
1252                         xfs_sb_version_haslogv2(&mp->m_sb));
1253                 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1254         }
1255         log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1256
1257         xlog_get_iclog_buffer_size(mp, log);
1258
1259         bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1260         if (!bp)
1261                 goto out_free_log;
1262         XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1263         XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1264         XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1265         ASSERT(XFS_BUF_ISBUSY(bp));
1266         ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1267         log->l_xbuf = bp;
1268
1269         spin_lock_init(&log->l_icloglock);
1270         spin_lock_init(&log->l_grant_lock);
1271         sv_init(&log->l_flush_wait, 0, "flush_wait");
1272
1273         xlog_trace_loggrant_alloc(log);
1274         /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1275         ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1276
1277         iclogp = &log->l_iclog;
1278         /*
1279          * The amount of memory to allocate for the iclog structure is
1280          * rather funky due to the way the structure is defined.  It is
1281          * done this way so that we can use different sizes for machines
1282          * with different amounts of memory.  See the definition of
1283          * xlog_in_core_t in xfs_log_priv.h for details.
1284          */
1285         iclogsize = log->l_iclog_size;
1286         ASSERT(log->l_iclog_size >= 4096);
1287         for (i=0; i < log->l_iclog_bufs; i++) {
1288                 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1289                 if (!*iclogp)
1290                         goto out_free_iclog;
1291
1292                 iclog = *iclogp;
1293                 iclog->ic_prev = prev_iclog;
1294                 prev_iclog = iclog;
1295
1296                 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1297                 if (!bp)
1298                         goto out_free_iclog;
1299                 if (!XFS_BUF_CPSEMA(bp))
1300                         ASSERT(0);
1301                 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1302                 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1303                 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1304                 iclog->ic_bp = bp;
1305                 iclog->hic_data = bp->b_addr;
1306 #ifdef DEBUG
1307                 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1308 #endif
1309                 head = &iclog->ic_header;
1310                 memset(head, 0, sizeof(xlog_rec_header_t));
1311                 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1312                 head->h_version = cpu_to_be32(
1313                         xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
1314                 head->h_size = cpu_to_be32(log->l_iclog_size);
1315                 /* new fields */
1316                 head->h_fmt = cpu_to_be32(XLOG_FMT);
1317                 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1318
1319                 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1320                 iclog->ic_state = XLOG_STATE_ACTIVE;
1321                 iclog->ic_log = log;
1322                 atomic_set(&iclog->ic_refcnt, 0);
1323                 spin_lock_init(&iclog->ic_callback_lock);
1324                 iclog->ic_callback_tail = &(iclog->ic_callback);
1325                 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1326
1327                 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1328                 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1329                 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1330                 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
1331
1332                 xlog_trace_iclog_alloc(iclog);
1333
1334                 iclogp = &iclog->ic_next;
1335         }
1336         *iclogp = log->l_iclog;                 /* complete ring */
1337         log->l_iclog->ic_prev = prev_iclog;     /* re-write 1st prev ptr */
1338
1339         return log;
1340
1341 out_free_iclog:
1342         for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1343                 prev_iclog = iclog->ic_next;
1344                 if (iclog->ic_bp) {
1345                         sv_destroy(&iclog->ic_force_wait);
1346                         sv_destroy(&iclog->ic_write_wait);
1347                         xfs_buf_free(iclog->ic_bp);
1348                         xlog_trace_iclog_dealloc(iclog);
1349                 }
1350                 kmem_free(iclog);
1351         }
1352         spinlock_destroy(&log->l_icloglock);
1353         spinlock_destroy(&log->l_grant_lock);
1354         xlog_trace_loggrant_dealloc(log);
1355         xfs_buf_free(log->l_xbuf);
1356 out_free_log:
1357         kmem_free(log);
1358         return NULL;
1359 }       /* xlog_alloc_log */
1360
1361
1362 /*
1363  * Write out the commit record of a transaction associated with the given
1364  * ticket.  Return the lsn of the commit record.
1365  */
1366 STATIC int
1367 xlog_commit_record(xfs_mount_t  *mp,
1368                    xlog_ticket_t *ticket,
1369                    xlog_in_core_t **iclog,
1370                    xfs_lsn_t    *commitlsnp)
1371 {
1372         int             error;
1373         xfs_log_iovec_t reg[1];
1374
1375         reg[0].i_addr = NULL;
1376         reg[0].i_len = 0;
1377         XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
1378
1379         ASSERT_ALWAYS(iclog);
1380         if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1381                                iclog, XLOG_COMMIT_TRANS))) {
1382                 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1383         }
1384         return error;
1385 }       /* xlog_commit_record */
1386
1387
1388 /*
1389  * Push on the buffer cache code if we ever use more than 75% of the on-disk
1390  * log space.  This code pushes on the lsn which would supposedly free up
1391  * the 25% which we want to leave free.  We may need to adopt a policy which
1392  * pushes on an lsn which is further along in the log once we reach the high
1393  * water mark.  In this manner, we would be creating a low water mark.
1394  */
1395 STATIC void
1396 xlog_grant_push_ail(xfs_mount_t *mp,
1397                     int         need_bytes)
1398 {
1399     xlog_t      *log = mp->m_log;       /* pointer to the log */
1400     xfs_lsn_t   tail_lsn;               /* lsn of the log tail */
1401     xfs_lsn_t   threshold_lsn = 0;      /* lsn we'd like to be at */
1402     int         free_blocks;            /* free blocks left to write to */
1403     int         free_bytes;             /* free bytes left to write to */
1404     int         threshold_block;        /* block in lsn we'd like to be at */
1405     int         threshold_cycle;        /* lsn cycle we'd like to be at */
1406     int         free_threshold;
1407
1408     ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1409
1410     spin_lock(&log->l_grant_lock);
1411     free_bytes = xlog_space_left(log,
1412                                  log->l_grant_reserve_cycle,
1413                                  log->l_grant_reserve_bytes);
1414     tail_lsn = log->l_tail_lsn;
1415     free_blocks = BTOBBT(free_bytes);
1416
1417     /*
1418      * Set the threshold for the minimum number of free blocks in the
1419      * log to the maximum of what the caller needs, one quarter of the
1420      * log, and 256 blocks.
1421      */
1422     free_threshold = BTOBB(need_bytes);
1423     free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1424     free_threshold = MAX(free_threshold, 256);
1425     if (free_blocks < free_threshold) {
1426         threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1427         threshold_cycle = CYCLE_LSN(tail_lsn);
1428         if (threshold_block >= log->l_logBBsize) {
1429             threshold_block -= log->l_logBBsize;
1430             threshold_cycle += 1;
1431         }
1432         threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
1433
1434         /* Don't pass in an lsn greater than the lsn of the last
1435          * log record known to be on disk.
1436          */
1437         if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1438             threshold_lsn = log->l_last_sync_lsn;
1439     }
1440     spin_unlock(&log->l_grant_lock);
1441
1442     /*
1443      * Get the transaction layer to kick the dirty buffers out to
1444      * disk asynchronously. No point in trying to do this if
1445      * the filesystem is shutting down.
1446      */
1447     if (threshold_lsn &&
1448         !XLOG_FORCED_SHUTDOWN(log))
1449             xfs_trans_push_ail(mp, threshold_lsn);
1450 }       /* xlog_grant_push_ail */
1451
1452
1453 /*
1454  * Flush out the in-core log (iclog) to the on-disk log in an asynchronous 
1455  * fashion.  Previously, we should have moved the current iclog
1456  * ptr in the log to point to the next available iclog.  This allows further
1457  * write to continue while this code syncs out an iclog ready to go.
1458  * Before an in-core log can be written out, the data section must be scanned
1459  * to save away the 1st word of each BBSIZE block into the header.  We replace
1460  * it with the current cycle count.  Each BBSIZE block is tagged with the
1461  * cycle count because there in an implicit assumption that drives will
1462  * guarantee that entire 512 byte blocks get written at once.  In other words,
1463  * we can't have part of a 512 byte block written and part not written.  By
1464  * tagging each block, we will know which blocks are valid when recovering
1465  * after an unclean shutdown.
1466  *
1467  * This routine is single threaded on the iclog.  No other thread can be in
1468  * this routine with the same iclog.  Changing contents of iclog can there-
1469  * fore be done without grabbing the state machine lock.  Updating the global
1470  * log will require grabbing the lock though.
1471  *
1472  * The entire log manager uses a logical block numbering scheme.  Only
1473  * log_sync (and then only bwrite()) know about the fact that the log may
1474  * not start with block zero on a given device.  The log block start offset
1475  * is added immediately before calling bwrite().
1476  */
1477
1478 STATIC int
1479 xlog_sync(xlog_t                *log,
1480           xlog_in_core_t        *iclog)
1481 {
1482         xfs_caddr_t     dptr;           /* pointer to byte sized element */
1483         xfs_buf_t       *bp;
1484         int             i;
1485         uint            count;          /* byte count of bwrite */
1486         uint            count_init;     /* initial count before roundup */
1487         int             roundoff;       /* roundoff to BB or stripe */
1488         int             split = 0;      /* split write into two regions */
1489         int             error;
1490         int             v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
1491
1492         XFS_STATS_INC(xs_log_writes);
1493         ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1494
1495         /* Add for LR header */
1496         count_init = log->l_iclog_hsize + iclog->ic_offset;
1497
1498         /* Round out the log write size */
1499         if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1500                 /* we have a v2 stripe unit to use */
1501                 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1502         } else {
1503                 count = BBTOB(BTOBB(count_init));
1504         }
1505         roundoff = count - count_init;
1506         ASSERT(roundoff >= 0);
1507         ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 && 
1508                 roundoff < log->l_mp->m_sb.sb_logsunit)
1509                 || 
1510                 (log->l_mp->m_sb.sb_logsunit <= 1 && 
1511                  roundoff < BBTOB(1)));
1512
1513         /* move grant heads by roundoff in sync */
1514         spin_lock(&log->l_grant_lock);
1515         xlog_grant_add_space(log, roundoff);
1516         spin_unlock(&log->l_grant_lock);
1517
1518         /* put cycle number in every block */
1519         xlog_pack_data(log, iclog, roundoff); 
1520
1521         /* real byte length */
1522         if (v2) {
1523                 iclog->ic_header.h_len =
1524                         cpu_to_be32(iclog->ic_offset + roundoff);
1525         } else {
1526                 iclog->ic_header.h_len =
1527                         cpu_to_be32(iclog->ic_offset);
1528         }
1529
1530         bp = iclog->ic_bp;
1531         ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1532         XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1533         XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
1534
1535         XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1536
1537         /* Do we need to split this write into 2 parts? */
1538         if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1539                 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1540                 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1541                 iclog->ic_bwritecnt = 2;        /* split into 2 writes */
1542         } else {
1543                 iclog->ic_bwritecnt = 1;
1544         }
1545         XFS_BUF_SET_COUNT(bp, count);
1546         XFS_BUF_SET_FSPRIVATE(bp, iclog);       /* save for later */
1547         XFS_BUF_ZEROFLAGS(bp);
1548         XFS_BUF_BUSY(bp);
1549         XFS_BUF_ASYNC(bp);
1550         /*
1551          * Do an ordered write for the log block.
1552          * Its unnecessary to flush the first split block in the log wrap case.
1553          */
1554         if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
1555                 XFS_BUF_ORDERED(bp);
1556
1557         ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1558         ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1559
1560         xlog_verify_iclog(log, iclog, count, B_TRUE);
1561
1562         /* account for log which doesn't start at block #0 */
1563         XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1564         /*
1565          * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1566          * is shutting down.
1567          */
1568         XFS_BUF_WRITE(bp);
1569
1570         if ((error = XFS_bwrite(bp))) {
1571                 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1572                                   XFS_BUF_ADDR(bp));
1573                 return error;
1574         }
1575         if (split) {
1576                 bp = iclog->ic_log->l_xbuf;
1577                 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1578                                                         (unsigned long)1);
1579                 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1580                 XFS_BUF_SET_ADDR(bp, 0);             /* logical 0 */
1581                 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1582                                             (__psint_t)count), split);
1583                 XFS_BUF_SET_FSPRIVATE(bp, iclog);
1584                 XFS_BUF_ZEROFLAGS(bp);
1585                 XFS_BUF_BUSY(bp);
1586                 XFS_BUF_ASYNC(bp);
1587                 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1588                         XFS_BUF_ORDERED(bp);
1589                 dptr = XFS_BUF_PTR(bp);
1590                 /*
1591                  * Bump the cycle numbers at the start of each block
1592                  * since this part of the buffer is at the start of
1593                  * a new cycle.  Watch out for the header magic number
1594                  * case, though.
1595                  */
1596                 for (i = 0; i < split; i += BBSIZE) {
1597                         be32_add_cpu((__be32 *)dptr, 1);
1598                         if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
1599                                 be32_add_cpu((__be32 *)dptr, 1);
1600                         dptr += BBSIZE;
1601                 }
1602
1603                 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1604                 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1605
1606                 /* account for internal log which doesn't start at block #0 */
1607                 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1608                 XFS_BUF_WRITE(bp);
1609                 if ((error = XFS_bwrite(bp))) {
1610                         xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1611                                           bp, XFS_BUF_ADDR(bp));
1612                         return error;
1613                 }
1614         }
1615         return 0;
1616 }       /* xlog_sync */
1617
1618
1619 /*
1620  * Deallocate a log structure
1621  */
1622 STATIC void
1623 xlog_dealloc_log(xlog_t *log)
1624 {
1625         xlog_in_core_t  *iclog, *next_iclog;
1626         int             i;
1627
1628         iclog = log->l_iclog;
1629         for (i=0; i<log->l_iclog_bufs; i++) {
1630                 sv_destroy(&iclog->ic_force_wait);
1631                 sv_destroy(&iclog->ic_write_wait);
1632                 xfs_buf_free(iclog->ic_bp);
1633                 xlog_trace_iclog_dealloc(iclog);
1634                 next_iclog = iclog->ic_next;
1635                 kmem_free(iclog);
1636                 iclog = next_iclog;
1637         }
1638         spinlock_destroy(&log->l_icloglock);
1639         spinlock_destroy(&log->l_grant_lock);
1640
1641         xfs_buf_free(log->l_xbuf);
1642         xlog_trace_loggrant_dealloc(log);
1643         log->l_mp->m_log = NULL;
1644         kmem_free(log);
1645 }       /* xlog_dealloc_log */
1646
1647 /*
1648  * Update counters atomically now that memcpy is done.
1649  */
1650 /* ARGSUSED */
1651 static inline void
1652 xlog_state_finish_copy(xlog_t           *log,
1653                        xlog_in_core_t   *iclog,
1654                        int              record_cnt,
1655                        int              copy_bytes)
1656 {
1657         spin_lock(&log->l_icloglock);
1658
1659         be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
1660         iclog->ic_offset += copy_bytes;
1661
1662         spin_unlock(&log->l_icloglock);
1663 }       /* xlog_state_finish_copy */
1664
1665
1666
1667
1668 /*
1669  * print out info relating to regions written which consume
1670  * the reservation
1671  */
1672 STATIC void
1673 xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1674 {
1675         uint i;
1676         uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1677
1678         /* match with XLOG_REG_TYPE_* in xfs_log.h */
1679         static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1680             "bformat",
1681             "bchunk",
1682             "efi_format",
1683             "efd_format",
1684             "iformat",
1685             "icore",
1686             "iext",
1687             "ibroot",
1688             "ilocal",
1689             "iattr_ext",
1690             "iattr_broot",
1691             "iattr_local",
1692             "qformat",
1693             "dquot",
1694             "quotaoff",
1695             "LR header",
1696             "unmount",
1697             "commit",
1698             "trans header"
1699         };
1700         static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1701             "SETATTR_NOT_SIZE",
1702             "SETATTR_SIZE",
1703             "INACTIVE",
1704             "CREATE",
1705             "CREATE_TRUNC",
1706             "TRUNCATE_FILE",
1707             "REMOVE",
1708             "LINK",
1709             "RENAME",
1710             "MKDIR",
1711             "RMDIR",
1712             "SYMLINK",
1713             "SET_DMATTRS",
1714             "GROWFS",
1715             "STRAT_WRITE",
1716             "DIOSTRAT",
1717             "WRITE_SYNC",
1718             "WRITEID",
1719             "ADDAFORK",
1720             "ATTRINVAL",
1721             "ATRUNCATE",
1722             "ATTR_SET",
1723             "ATTR_RM",
1724             "ATTR_FLAG",
1725             "CLEAR_AGI_BUCKET",
1726             "QM_SBCHANGE",
1727             "DUMMY1",
1728             "DUMMY2",
1729             "QM_QUOTAOFF",
1730             "QM_DQALLOC",
1731             "QM_SETQLIM",
1732             "QM_DQCLUSTER",
1733             "QM_QINOCREATE",
1734             "QM_QUOTAOFF_END",
1735             "SB_UNIT",
1736             "FSYNC_TS",
1737             "GROWFSRT_ALLOC",
1738             "GROWFSRT_ZERO",
1739             "GROWFSRT_FREE",
1740             "SWAPEXT"
1741         };
1742
1743         xfs_fs_cmn_err(CE_WARN, mp,
1744                         "xfs_log_write: reservation summary:\n"
1745                         "  trans type  = %s (%u)\n"
1746                         "  unit res    = %d bytes\n"
1747                         "  current res = %d bytes\n"
1748                         "  total reg   = %u bytes (o/flow = %u bytes)\n"
1749                         "  ophdrs      = %u (ophdr space = %u bytes)\n"
1750                         "  ophdr + reg = %u bytes\n"
1751                         "  num regions = %u\n",
1752                         ((ticket->t_trans_type <= 0 ||
1753                           ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1754                           "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1755                         ticket->t_trans_type,
1756                         ticket->t_unit_res,
1757                         ticket->t_curr_res,
1758                         ticket->t_res_arr_sum, ticket->t_res_o_flow,
1759                         ticket->t_res_num_ophdrs, ophdr_spc,
1760                         ticket->t_res_arr_sum + 
1761                         ticket->t_res_o_flow + ophdr_spc,
1762                         ticket->t_res_num);
1763
1764         for (i = 0; i < ticket->t_res_num; i++) {
1765                 uint r_type = ticket->t_res_arr[i].r_type; 
1766                 cmn_err(CE_WARN,
1767                             "region[%u]: %s - %u bytes\n",
1768                             i, 
1769                             ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1770                             "bad-rtype" : res_type_str[r_type-1]),
1771                             ticket->t_res_arr[i].r_len);
1772         }
1773 }
1774
1775 /*
1776  * Write some region out to in-core log
1777  *
1778  * This will be called when writing externally provided regions or when
1779  * writing out a commit record for a given transaction.
1780  *
1781  * General algorithm:
1782  *      1. Find total length of this write.  This may include adding to the
1783  *              lengths passed in.
1784  *      2. Check whether we violate the tickets reservation.
1785  *      3. While writing to this iclog
1786  *          A. Reserve as much space in this iclog as can get
1787  *          B. If this is first write, save away start lsn
1788  *          C. While writing this region:
1789  *              1. If first write of transaction, write start record
1790  *              2. Write log operation header (header per region)
1791  *              3. Find out if we can fit entire region into this iclog
1792  *              4. Potentially, verify destination memcpy ptr
1793  *              5. Memcpy (partial) region
1794  *              6. If partial copy, release iclog; otherwise, continue
1795  *                      copying more regions into current iclog
1796  *      4. Mark want sync bit (in simulation mode)
1797  *      5. Release iclog for potential flush to on-disk log.
1798  *
1799  * ERRORS:
1800  * 1.   Panic if reservation is overrun.  This should never happen since
1801  *      reservation amounts are generated internal to the filesystem.
1802  * NOTES:
1803  * 1. Tickets are single threaded data structures.
1804  * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1805  *      syncing routine.  When a single log_write region needs to span
1806  *      multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1807  *      on all log operation writes which don't contain the end of the
1808  *      region.  The XLOG_END_TRANS bit is used for the in-core log
1809  *      operation which contains the end of the continued log_write region.
1810  * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1811  *      we don't really know exactly how much space will be used.  As a result,
1812  *      we don't update ic_offset until the end when we know exactly how many
1813  *      bytes have been written out.
1814  */
1815 STATIC int
1816 xlog_write(xfs_mount_t *        mp,
1817            xfs_log_iovec_t      reg[],
1818            int                  nentries,
1819            xfs_log_ticket_t     tic,
1820            xfs_lsn_t            *start_lsn,
1821            xlog_in_core_t       **commit_iclog,
1822            uint                 flags)
1823 {
1824     xlog_t           *log = mp->m_log;
1825     xlog_ticket_t    *ticket = (xlog_ticket_t *)tic;
1826     xlog_in_core_t   *iclog = NULL;  /* ptr to current in-core log */
1827     xlog_op_header_t *logop_head;    /* ptr to log operation header */
1828     __psint_t        ptr;            /* copy address into data region */
1829     int              len;            /* # xlog_write() bytes 2 still copy */
1830     int              index;          /* region index currently copying */
1831     int              log_offset;     /* offset (from 0) into data region */
1832     int              start_rec_copy; /* # bytes to copy for start record */
1833     int              partial_copy;   /* did we split a region? */
1834     int              partial_copy_len;/* # bytes copied if split region */
1835     int              need_copy;      /* # bytes need to memcpy this region */
1836     int              copy_len;       /* # bytes actually memcpy'ing */
1837     int              copy_off;       /* # bytes from entry start */
1838     int              contwr;         /* continued write of in-core log? */
1839     int              error;
1840     int              record_cnt = 0, data_cnt = 0;
1841
1842     partial_copy_len = partial_copy = 0;
1843
1844     /* Calculate potential maximum space.  Each region gets its own
1845      * xlog_op_header_t and may need to be double word aligned.
1846      */
1847     len = 0;
1848     if (ticket->t_flags & XLOG_TIC_INITED) {    /* acct for start rec of xact */
1849         len += sizeof(xlog_op_header_t);
1850         ticket->t_res_num_ophdrs++;
1851     }
1852
1853     for (index = 0; index < nentries; index++) {
1854         len += sizeof(xlog_op_header_t);            /* each region gets >= 1 */
1855         ticket->t_res_num_ophdrs++;
1856         len += reg[index].i_len;
1857         xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
1858     }
1859     contwr = *start_lsn = 0;
1860
1861     if (ticket->t_curr_res < len) {
1862         xlog_print_tic_res(mp, ticket);
1863 #ifdef DEBUG
1864         xlog_panic(
1865                 "xfs_log_write: reservation ran out. Need to up reservation");
1866 #else
1867         /* Customer configurable panic */
1868         xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1869                 "xfs_log_write: reservation ran out. Need to up reservation");
1870         /* If we did not panic, shutdown the filesystem */
1871         xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1872 #endif
1873     } else
1874         ticket->t_curr_res -= len;
1875
1876     for (index = 0; index < nentries; ) {
1877         if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1878                                                &contwr, &log_offset)))
1879                 return error;
1880
1881         ASSERT(log_offset <= iclog->ic_size - 1);
1882         ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1883
1884         /* start_lsn is the first lsn written to. That's all we need. */
1885         if (! *start_lsn)
1886             *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
1887
1888         /* This loop writes out as many regions as can fit in the amount
1889          * of space which was allocated by xlog_state_get_iclog_space().
1890          */
1891         while (index < nentries) {
1892             ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1893             ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1894             start_rec_copy = 0;
1895
1896             /* If first write for transaction, insert start record.
1897              * We can't be trying to commit if we are inited.  We can't
1898              * have any "partial_copy" if we are inited.
1899              */
1900             if (ticket->t_flags & XLOG_TIC_INITED) {
1901                 logop_head              = (xlog_op_header_t *)ptr;
1902                 logop_head->oh_tid      = cpu_to_be32(ticket->t_tid);
1903                 logop_head->oh_clientid = ticket->t_clientid;
1904                 logop_head->oh_len      = 0;
1905                 logop_head->oh_flags    = XLOG_START_TRANS;
1906                 logop_head->oh_res2     = 0;
1907                 ticket->t_flags         &= ~XLOG_TIC_INITED;    /* clear bit */
1908                 record_cnt++;
1909
1910                 start_rec_copy = sizeof(xlog_op_header_t);
1911                 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1912             }
1913
1914             /* Copy log operation header directly into data section */
1915             logop_head                  = (xlog_op_header_t *)ptr;
1916             logop_head->oh_tid          = cpu_to_be32(ticket->t_tid);
1917             logop_head->oh_clientid     = ticket->t_clientid;
1918             logop_head->oh_res2         = 0;
1919
1920             /* header copied directly */
1921             xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1922
1923             /* are we copying a commit or unmount record? */
1924             logop_head->oh_flags = flags;
1925
1926             /*
1927              * We've seen logs corrupted with bad transaction client
1928              * ids.  This makes sure that XFS doesn't generate them on.
1929              * Turn this into an EIO and shut down the filesystem.
1930              */
1931             switch (logop_head->oh_clientid)  {
1932             case XFS_TRANSACTION:
1933             case XFS_VOLUME:
1934             case XFS_LOG:
1935                 break;
1936             default:
1937                 xfs_fs_cmn_err(CE_WARN, mp,
1938                     "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1939                     logop_head->oh_clientid, tic);
1940                 return XFS_ERROR(EIO);
1941             }
1942
1943             /* Partial write last time? => (partial_copy != 0)
1944              * need_copy is the amount we'd like to copy if everything could
1945              * fit in the current memcpy.
1946              */
1947             need_copy = reg[index].i_len - partial_copy_len;
1948
1949             copy_off = partial_copy_len;
1950             if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1951                 copy_len = need_copy;
1952                 logop_head->oh_len = cpu_to_be32(copy_len);
1953                 if (partial_copy)
1954                     logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1955                 partial_copy_len = partial_copy = 0;
1956             } else {                                        /* partial write */
1957                 copy_len = iclog->ic_size - log_offset;
1958                 logop_head->oh_len = cpu_to_be32(copy_len);
1959                 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1960                 if (partial_copy)
1961                         logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1962                 partial_copy_len += copy_len;
1963                 partial_copy++;
1964                 len += sizeof(xlog_op_header_t); /* from splitting of region */
1965                 /* account for new log op header */
1966                 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1967                 ticket->t_res_num_ophdrs++;
1968             }
1969             xlog_verify_dest_ptr(log, ptr);
1970
1971             /* copy region */
1972             ASSERT(copy_len >= 0);
1973             memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1974             xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1975
1976             /* make copy_len total bytes copied, including headers */
1977             copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1978             record_cnt++;
1979             data_cnt += contwr ? copy_len : 0;
1980             if (partial_copy) {                 /* copied partial region */
1981                     /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1982                     xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1983                     record_cnt = data_cnt = 0;
1984                     if ((error = xlog_state_release_iclog(log, iclog)))
1985                             return error;
1986                     break;                      /* don't increment index */
1987             } else {                            /* copied entire region */
1988                 index++;
1989                 partial_copy_len = partial_copy = 0;
1990
1991                 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1992                     xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1993                     record_cnt = data_cnt = 0;
1994                     xlog_state_want_sync(log, iclog);
1995                     if (commit_iclog) {
1996                         ASSERT(flags & XLOG_COMMIT_TRANS);
1997                         *commit_iclog = iclog;
1998                     } else if ((error = xlog_state_release_iclog(log, iclog)))
1999                            return error;
2000                     if (index == nentries)
2001                             return 0;           /* we are done */
2002                     else
2003                             break;
2004                 }
2005             } /* if (partial_copy) */
2006         } /* while (index < nentries) */
2007     } /* for (index = 0; index < nentries; ) */
2008     ASSERT(len == 0);
2009
2010     xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2011     if (commit_iclog) {
2012         ASSERT(flags & XLOG_COMMIT_TRANS);
2013         *commit_iclog = iclog;
2014         return 0;
2015     }
2016     return xlog_state_release_iclog(log, iclog);
2017 }       /* xlog_write */
2018
2019
2020 /*****************************************************************************
2021  *
2022  *              State Machine functions
2023  *
2024  *****************************************************************************
2025  */
2026
2027 /* Clean iclogs starting from the head.  This ordering must be
2028  * maintained, so an iclog doesn't become ACTIVE beyond one that
2029  * is SYNCING.  This is also required to maintain the notion that we use
2030  * a ordered wait queue to hold off would be writers to the log when every
2031  * iclog is trying to sync to disk.
2032  *
2033  * State Change: DIRTY -> ACTIVE
2034  */
2035 STATIC void
2036 xlog_state_clean_log(xlog_t *log)
2037 {
2038         xlog_in_core_t  *iclog;
2039         int changed = 0;
2040
2041         iclog = log->l_iclog;
2042         do {
2043                 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2044                         iclog->ic_state = XLOG_STATE_ACTIVE;
2045                         iclog->ic_offset       = 0;
2046                         ASSERT(iclog->ic_callback == NULL);
2047                         /*
2048                          * If the number of ops in this iclog indicate it just
2049                          * contains the dummy transaction, we can
2050                          * change state into IDLE (the second time around).
2051                          * Otherwise we should change the state into
2052                          * NEED a dummy.
2053                          * We don't need to cover the dummy.
2054                          */
2055                         if (!changed &&
2056                            (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2057                                         XLOG_COVER_OPS)) {
2058                                 changed = 1;
2059                         } else {
2060                                 /*
2061                                  * We have two dirty iclogs so start over
2062                                  * This could also be num of ops indicates
2063                                  * this is not the dummy going out.
2064                                  */
2065                                 changed = 2;
2066                         }
2067                         iclog->ic_header.h_num_logops = 0;
2068                         memset(iclog->ic_header.h_cycle_data, 0,
2069                               sizeof(iclog->ic_header.h_cycle_data));
2070                         iclog->ic_header.h_lsn = 0;
2071                 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2072                         /* do nothing */;
2073                 else
2074                         break;  /* stop cleaning */
2075                 iclog = iclog->ic_next;
2076         } while (iclog != log->l_iclog);
2077
2078         /* log is locked when we are called */
2079         /*
2080          * Change state for the dummy log recording.
2081          * We usually go to NEED. But we go to NEED2 if the changed indicates
2082          * we are done writing the dummy record.
2083          * If we are done with the second dummy recored (DONE2), then
2084          * we go to IDLE.
2085          */
2086         if (changed) {
2087                 switch (log->l_covered_state) {
2088                 case XLOG_STATE_COVER_IDLE:
2089                 case XLOG_STATE_COVER_NEED:
2090                 case XLOG_STATE_COVER_NEED2:
2091                         log->l_covered_state = XLOG_STATE_COVER_NEED;
2092                         break;
2093
2094                 case XLOG_STATE_COVER_DONE:
2095                         if (changed == 1)
2096                                 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2097                         else
2098                                 log->l_covered_state = XLOG_STATE_COVER_NEED;
2099                         break;
2100
2101                 case XLOG_STATE_COVER_DONE2:
2102                         if (changed == 1)
2103                                 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2104                         else
2105                                 log->l_covered_state = XLOG_STATE_COVER_NEED;
2106                         break;
2107
2108                 default:
2109                         ASSERT(0);
2110                 }
2111         }
2112 }       /* xlog_state_clean_log */
2113
2114 STATIC xfs_lsn_t
2115 xlog_get_lowest_lsn(
2116         xlog_t          *log)
2117 {
2118         xlog_in_core_t  *lsn_log;
2119         xfs_lsn_t       lowest_lsn, lsn;
2120
2121         lsn_log = log->l_iclog;
2122         lowest_lsn = 0;
2123         do {
2124             if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2125                 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
2126                 if ((lsn && !lowest_lsn) ||
2127                     (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2128                         lowest_lsn = lsn;
2129                 }
2130             }
2131             lsn_log = lsn_log->ic_next;
2132         } while (lsn_log != log->l_iclog);
2133         return lowest_lsn;
2134 }
2135
2136
2137 STATIC void
2138 xlog_state_do_callback(
2139         xlog_t          *log,
2140         int             aborted,
2141         xlog_in_core_t  *ciclog)
2142 {
2143         xlog_in_core_t     *iclog;
2144         xlog_in_core_t     *first_iclog;        /* used to know when we've
2145                                                  * processed all iclogs once */
2146         xfs_log_callback_t *cb, *cb_next;
2147         int                flushcnt = 0;
2148         xfs_lsn_t          lowest_lsn;
2149         int                ioerrors;    /* counter: iclogs with errors */
2150         int                loopdidcallbacks; /* flag: inner loop did callbacks*/
2151         int                funcdidcallbacks; /* flag: function did callbacks */
2152         int                repeats;     /* for issuing console warnings if
2153                                          * looping too many times */
2154         int                wake = 0;
2155
2156         spin_lock(&log->l_icloglock);
2157         first_iclog = iclog = log->l_iclog;
2158         ioerrors = 0;
2159         funcdidcallbacks = 0;
2160         repeats = 0;
2161
2162         do {
2163                 /*
2164                  * Scan all iclogs starting with the one pointed to by the
2165                  * log.  Reset this starting point each time the log is
2166                  * unlocked (during callbacks).
2167                  *
2168                  * Keep looping through iclogs until one full pass is made
2169                  * without running any callbacks.
2170                  */
2171                 first_iclog = log->l_iclog;
2172                 iclog = log->l_iclog;
2173                 loopdidcallbacks = 0;
2174                 repeats++;
2175
2176                 do {
2177
2178                         /* skip all iclogs in the ACTIVE & DIRTY states */
2179                         if (iclog->ic_state &
2180                             (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2181                                 iclog = iclog->ic_next;
2182                                 continue;
2183                         }
2184
2185                         /*
2186                          * Between marking a filesystem SHUTDOWN and stopping
2187                          * the log, we do flush all iclogs to disk (if there
2188                          * wasn't a log I/O error). So, we do want things to
2189                          * go smoothly in case of just a SHUTDOWN  w/o a
2190                          * LOG_IO_ERROR.
2191                          */
2192                         if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2193                                 /*
2194                                  * Can only perform callbacks in order.  Since
2195                                  * this iclog is not in the DONE_SYNC/
2196                                  * DO_CALLBACK state, we skip the rest and
2197                                  * just try to clean up.  If we set our iclog
2198                                  * to DO_CALLBACK, we will not process it when
2199                                  * we retry since a previous iclog is in the
2200                                  * CALLBACK and the state cannot change since
2201                                  * we are holding the l_icloglock.
2202                                  */
2203                                 if (!(iclog->ic_state &
2204                                         (XLOG_STATE_DONE_SYNC |
2205                                                  XLOG_STATE_DO_CALLBACK))) {
2206                                         if (ciclog && (ciclog->ic_state ==
2207                                                         XLOG_STATE_DONE_SYNC)) {
2208                                                 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2209                                         }
2210                                         break;
2211                                 }
2212                                 /*
2213                                  * We now have an iclog that is in either the
2214                                  * DO_CALLBACK or DONE_SYNC states. The other
2215                                  * states (WANT_SYNC, SYNCING, or CALLBACK were
2216                                  * caught by the above if and are going to
2217                                  * clean (i.e. we aren't doing their callbacks)
2218                                  * see the above if.
2219                                  */
2220
2221                                 /*
2222                                  * We will do one more check here to see if we
2223                                  * have chased our tail around.
2224                                  */
2225
2226                                 lowest_lsn = xlog_get_lowest_lsn(log);
2227                                 if (lowest_lsn &&
2228                                     XFS_LSN_CMP(lowest_lsn,
2229                                                 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
2230                                         iclog = iclog->ic_next;
2231                                         continue; /* Leave this iclog for
2232                                                    * another thread */
2233                                 }
2234
2235                                 iclog->ic_state = XLOG_STATE_CALLBACK;
2236
2237                                 spin_unlock(&log->l_icloglock);
2238
2239                                 /* l_last_sync_lsn field protected by
2240                                  * l_grant_lock. Don't worry about iclog's lsn.
2241                                  * No one else can be here except us.
2242                                  */
2243                                 spin_lock(&log->l_grant_lock);
2244                                 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2245                                        be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2246                                 log->l_last_sync_lsn =
2247                                         be64_to_cpu(iclog->ic_header.h_lsn);
2248                                 spin_unlock(&log->l_grant_lock);
2249
2250                         } else {
2251                                 spin_unlock(&log->l_icloglock);
2252                                 ioerrors++;
2253                         }
2254
2255                         /*
2256                          * Keep processing entries in the callback list until
2257                          * we come around and it is empty.  We need to
2258                          * atomically see that the list is empty and change the
2259                          * state to DIRTY so that we don't miss any more
2260                          * callbacks being added.
2261                          */
2262                         spin_lock(&iclog->ic_callback_lock);
2263                         cb = iclog->ic_callback;
2264                         while (cb) {
2265                                 iclog->ic_callback_tail = &(iclog->ic_callback);
2266                                 iclog->ic_callback = NULL;
2267                                 spin_unlock(&iclog->ic_callback_lock);
2268
2269                                 /* perform callbacks in the order given */
2270                                 for (; cb; cb = cb_next) {
2271                                         cb_next = cb->cb_next;
2272                                         cb->cb_func(cb->cb_arg, aborted);
2273                                 }
2274                                 spin_lock(&iclog->ic_callback_lock);
2275                                 cb = iclog->ic_callback;
2276                         }
2277
2278                         loopdidcallbacks++;
2279                         funcdidcallbacks++;
2280
2281                         spin_lock(&log->l_icloglock);
2282                         ASSERT(iclog->ic_callback == NULL);
2283                         spin_unlock(&iclog->ic_callback_lock);
2284                         if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2285                                 iclog->ic_state = XLOG_STATE_DIRTY;
2286
2287                         /*
2288                          * Transition from DIRTY to ACTIVE if applicable.
2289                          * NOP if STATE_IOERROR.
2290                          */
2291                         xlog_state_clean_log(log);
2292
2293                         /* wake up threads waiting in xfs_log_force() */
2294                         sv_broadcast(&iclog->ic_force_wait);
2295
2296                         iclog = iclog->ic_next;
2297                 } while (first_iclog != iclog);
2298
2299                 if (repeats > 5000) {
2300                         flushcnt += repeats;
2301                         repeats = 0;
2302                         xfs_fs_cmn_err(CE_WARN, log->l_mp,
2303                                 "%s: possible infinite loop (%d iterations)",
2304                                 __func__, flushcnt);
2305                 }
2306         } while (!ioerrors && loopdidcallbacks);
2307
2308         /*
2309          * make one last gasp attempt to see if iclogs are being left in
2310          * limbo..
2311          */
2312 #ifdef DEBUG
2313         if (funcdidcallbacks) {
2314                 first_iclog = iclog = log->l_iclog;
2315                 do {
2316                         ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2317                         /*
2318                          * Terminate the loop if iclogs are found in states
2319                          * which will cause other threads to clean up iclogs.
2320                          *
2321                          * SYNCING - i/o completion will go through logs
2322                          * DONE_SYNC - interrupt thread should be waiting for
2323                          *              l_icloglock
2324                          * IOERROR - give up hope all ye who enter here
2325                          */
2326                         if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2327                             iclog->ic_state == XLOG_STATE_SYNCING ||
2328                             iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2329                             iclog->ic_state == XLOG_STATE_IOERROR )
2330                                 break;
2331                         iclog = iclog->ic_next;
2332                 } while (first_iclog != iclog);
2333         }
2334 #endif
2335
2336         if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2337                 wake = 1;
2338         spin_unlock(&log->l_icloglock);
2339
2340         if (wake)
2341                 sv_broadcast(&log->l_flush_wait);
2342 }
2343
2344
2345 /*
2346  * Finish transitioning this iclog to the dirty state.
2347  *
2348  * Make sure that we completely execute this routine only when this is
2349  * the last call to the iclog.  There is a good chance that iclog flushes,
2350  * when we reach the end of the physical log, get turned into 2 separate
2351  * calls to bwrite.  Hence, one iclog flush could generate two calls to this
2352  * routine.  By using the reference count bwritecnt, we guarantee that only
2353  * the second completion goes through.
2354  *
2355  * Callbacks could take time, so they are done outside the scope of the
2356  * global state machine log lock.
2357  */
2358 STATIC void
2359 xlog_state_done_syncing(
2360         xlog_in_core_t  *iclog,
2361         int             aborted)
2362 {
2363         xlog_t             *log = iclog->ic_log;
2364
2365         spin_lock(&log->l_icloglock);
2366
2367         ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2368                iclog->ic_state == XLOG_STATE_IOERROR);
2369         ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
2370         ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2371
2372
2373         /*
2374          * If we got an error, either on the first buffer, or in the case of
2375          * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2376          * and none should ever be attempted to be written to disk
2377          * again.
2378          */
2379         if (iclog->ic_state != XLOG_STATE_IOERROR) {
2380                 if (--iclog->ic_bwritecnt == 1) {
2381                         spin_unlock(&log->l_icloglock);
2382                         return;
2383                 }
2384                 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2385         }
2386
2387         /*
2388          * Someone could be sleeping prior to writing out the next
2389          * iclog buffer, we wake them all, one will get to do the
2390          * I/O, the others get to wait for the result.
2391          */
2392         sv_broadcast(&iclog->ic_write_wait);
2393         spin_unlock(&log->l_icloglock);
2394         xlog_state_do_callback(log, aborted, iclog);    /* also cleans log */
2395 }       /* xlog_state_done_syncing */
2396
2397
2398 /*
2399  * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2400  * sleep.  We wait on the flush queue on the head iclog as that should be
2401  * the first iclog to complete flushing. Hence if all iclogs are syncing,
2402  * we will wait here and all new writes will sleep until a sync completes.
2403  *
2404  * The in-core logs are used in a circular fashion. They are not used
2405  * out-of-order even when an iclog past the head is free.
2406  *
2407  * return:
2408  *      * log_offset where xlog_write() can start writing into the in-core
2409  *              log's data space.
2410  *      * in-core log pointer to which xlog_write() should write.
2411  *      * boolean indicating this is a continued write to an in-core log.
2412  *              If this is the last write, then the in-core log's offset field
2413  *              needs to be incremented, depending on the amount of data which
2414  *              is copied.
2415  */
2416 STATIC int
2417 xlog_state_get_iclog_space(xlog_t         *log,
2418                            int            len,
2419                            xlog_in_core_t **iclogp,
2420                            xlog_ticket_t  *ticket,
2421                            int            *continued_write,
2422                            int            *logoffsetp)
2423 {
2424         int               log_offset;
2425         xlog_rec_header_t *head;
2426         xlog_in_core_t    *iclog;
2427         int               error;
2428
2429 restart:
2430         spin_lock(&log->l_icloglock);
2431         if (XLOG_FORCED_SHUTDOWN(log)) {
2432                 spin_unlock(&log->l_icloglock);
2433                 return XFS_ERROR(EIO);
2434         }
2435
2436         iclog = log->l_iclog;
2437         if (iclog->ic_state != XLOG_STATE_ACTIVE) {
2438                 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2439                 XFS_STATS_INC(xs_log_noiclogs);
2440
2441                 /* Wait for log writes to have flushed */
2442                 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
2443                 goto restart;
2444         }
2445
2446         head = &iclog->ic_header;
2447
2448         atomic_inc(&iclog->ic_refcnt);  /* prevents sync */
2449         log_offset = iclog->ic_offset;
2450
2451         /* On the 1st write to an iclog, figure out lsn.  This works
2452          * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2453          * committing to.  If the offset is set, that's how many blocks
2454          * must be written.
2455          */
2456         if (log_offset == 0) {
2457                 ticket->t_curr_res -= log->l_iclog_hsize;
2458                 xlog_tic_add_region(ticket,
2459                                     log->l_iclog_hsize,
2460                                     XLOG_REG_TYPE_LRHEADER);
2461                 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2462                 head->h_lsn = cpu_to_be64(
2463                         xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
2464                 ASSERT(log->l_curr_block >= 0);
2465         }
2466
2467         /* If there is enough room to write everything, then do it.  Otherwise,
2468          * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2469          * bit is on, so this will get flushed out.  Don't update ic_offset
2470          * until you know exactly how many bytes get copied.  Therefore, wait
2471          * until later to update ic_offset.
2472          *
2473          * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2474          * can fit into remaining data section.
2475          */
2476         if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2477                 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2478
2479                 /*
2480                  * If I'm the only one writing to this iclog, sync it to disk.
2481                  * We need to do an atomic compare and decrement here to avoid
2482                  * racing with concurrent atomic_dec_and_lock() calls in
2483                  * xlog_state_release_iclog() when there is more than one
2484                  * reference to the iclog.
2485                  */
2486                 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2487                         /* we are the only one */
2488                         spin_unlock(&log->l_icloglock);
2489                         error = xlog_state_release_iclog(log, iclog);
2490                         if (error)
2491                                 return error;
2492                 } else {
2493                         spin_unlock(&log->l_icloglock);
2494                 }
2495                 goto restart;
2496         }
2497
2498         /* Do we have enough room to write the full amount in the remainder
2499          * of this iclog?  Or must we continue a write on the next iclog and
2500          * mark this iclog as completely taken?  In the case where we switch
2501          * iclogs (to mark it taken), this particular iclog will release/sync
2502          * to disk in xlog_write().
2503          */
2504         if (len <= iclog->ic_size - iclog->ic_offset) {
2505                 *continued_write = 0;
2506                 iclog->ic_offset += len;
2507         } else {
2508                 *continued_write = 1;
2509                 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2510         }
2511         *iclogp = iclog;
2512
2513         ASSERT(iclog->ic_offset <= iclog->ic_size);
2514         spin_unlock(&log->l_icloglock);
2515
2516         *logoffsetp = log_offset;
2517         return 0;
2518 }       /* xlog_state_get_iclog_space */
2519
2520 /*
2521  * Atomically get the log space required for a log ticket.
2522  *
2523  * Once a ticket gets put onto the reserveq, it will only return after
2524  * the needed reservation is satisfied.
2525  */
2526 STATIC int
2527 xlog_grant_log_space(xlog_t        *log,
2528                      xlog_ticket_t *tic)
2529 {
2530         int              free_bytes;
2531         int              need_bytes;
2532 #ifdef DEBUG
2533         xfs_lsn_t        tail_lsn;
2534 #endif
2535
2536
2537 #ifdef DEBUG
2538         if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2539                 panic("grant Recovery problem");
2540 #endif
2541
2542         /* Is there space or do we need to sleep? */
2543         spin_lock(&log->l_grant_lock);
2544         xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2545
2546         /* something is already sleeping; insert new transaction at end */
2547         if (log->l_reserve_headq) {
2548                 xlog_ins_ticketq(&log->l_reserve_headq, tic);
2549                 xlog_trace_loggrant(log, tic,
2550                                     "xlog_grant_log_space: sleep 1");
2551                 /*
2552                  * Gotta check this before going to sleep, while we're
2553                  * holding the grant lock.
2554                  */
2555                 if (XLOG_FORCED_SHUTDOWN(log))
2556                         goto error_return;
2557
2558                 XFS_STATS_INC(xs_sleep_logspace);
2559                 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2560                 /*
2561                  * If we got an error, and the filesystem is shutting down,
2562                  * we'll catch it down below. So just continue...
2563                  */
2564                 xlog_trace_loggrant(log, tic,
2565                                     "xlog_grant_log_space: wake 1");
2566                 spin_lock(&log->l_grant_lock);
2567         }
2568         if (tic->t_flags & XFS_LOG_PERM_RESERV)
2569                 need_bytes = tic->t_unit_res*tic->t_ocnt;
2570         else
2571                 need_bytes = tic->t_unit_res;
2572
2573 redo:
2574         if (XLOG_FORCED_SHUTDOWN(log))
2575                 goto error_return;
2576
2577         free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2578                                      log->l_grant_reserve_bytes);
2579         if (free_bytes < need_bytes) {
2580                 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2581                         xlog_ins_ticketq(&log->l_reserve_headq, tic);
2582                 xlog_trace_loggrant(log, tic,
2583                                     "xlog_grant_log_space: sleep 2");
2584                 XFS_STATS_INC(xs_sleep_logspace);
2585                 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2586
2587                 if (XLOG_FORCED_SHUTDOWN(log)) {
2588                         spin_lock(&log->l_grant_lock);
2589                         goto error_return;
2590                 }
2591
2592                 xlog_trace_loggrant(log, tic,
2593                                     "xlog_grant_log_space: wake 2");
2594                 xlog_grant_push_ail(log->l_mp, need_bytes);
2595                 spin_lock(&log->l_grant_lock);
2596                 goto redo;
2597         } else if (tic->t_flags & XLOG_TIC_IN_Q)
2598                 xlog_del_ticketq(&log->l_reserve_headq, tic);
2599
2600         /* we've got enough space */
2601         xlog_grant_add_space(log, need_bytes);
2602 #ifdef DEBUG
2603         tail_lsn = log->l_tail_lsn;
2604         /*
2605          * Check to make sure the grant write head didn't just over lap the
2606          * tail.  If the cycles are the same, we can't be overlapping.
2607          * Otherwise, make sure that the cycles differ by exactly one and
2608          * check the byte count.
2609          */
2610         if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2611                 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2612                 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2613         }
2614 #endif
2615         xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2616         xlog_verify_grant_head(log, 1);
2617         spin_unlock(&log->l_grant_lock);
2618         return 0;
2619
2620  error_return:
2621         if (tic->t_flags & XLOG_TIC_IN_Q)
2622                 xlog_del_ticketq(&log->l_reserve_headq, tic);
2623         xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2624         /*
2625          * If we are failing, make sure the ticket doesn't have any
2626          * current reservations. We don't want to add this back when
2627          * the ticket/transaction gets cancelled.
2628          */
2629         tic->t_curr_res = 0;
2630         tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2631         spin_unlock(&log->l_grant_lock);
2632         return XFS_ERROR(EIO);
2633 }       /* xlog_grant_log_space */
2634
2635
2636 /*
2637  * Replenish the byte reservation required by moving the grant write head.
2638  *
2639  *
2640  */
2641 STATIC int
2642 xlog_regrant_write_log_space(xlog_t        *log,
2643                              xlog_ticket_t *tic)
2644 {
2645         int             free_bytes, need_bytes;
2646         xlog_ticket_t   *ntic;
2647 #ifdef DEBUG
2648         xfs_lsn_t       tail_lsn;
2649 #endif
2650
2651         tic->t_curr_res = tic->t_unit_res;
2652         xlog_tic_reset_res(tic);
2653
2654         if (tic->t_cnt > 0)
2655                 return 0;
2656
2657 #ifdef DEBUG
2658         if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2659                 panic("regrant Recovery problem");
2660 #endif
2661
2662         spin_lock(&log->l_grant_lock);
2663         xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2664
2665         if (XLOG_FORCED_SHUTDOWN(log))
2666                 goto error_return;
2667
2668         /* If there are other waiters on the queue then give them a
2669          * chance at logspace before us. Wake up the first waiters,
2670          * if we do not wake up all the waiters then go to sleep waiting
2671          * for more free space, otherwise try to get some space for
2672          * this transaction.
2673          */
2674
2675         if ((ntic = log->l_write_headq)) {
2676                 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2677                                              log->l_grant_write_bytes);
2678                 do {
2679                         ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2680
2681                         if (free_bytes < ntic->t_unit_res)
2682                                 break;
2683                         free_bytes -= ntic->t_unit_res;
2684                         sv_signal(&ntic->t_wait);
2685                         ntic = ntic->t_next;
2686                 } while (ntic != log->l_write_headq);
2687
2688                 if (ntic != log->l_write_headq) {
2689                         if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2690                                 xlog_ins_ticketq(&log->l_write_headq, tic);
2691
2692                         xlog_trace_loggrant(log, tic,
2693                                     "xlog_regrant_write_log_space: sleep 1");
2694                         XFS_STATS_INC(xs_sleep_logspace);
2695                         sv_wait(&tic->t_wait, PINOD|PLTWAIT,
2696                                 &log->l_grant_lock, s);
2697
2698                         /* If we're shutting down, this tic is already
2699                          * off the queue */
2700                         if (XLOG_FORCED_SHUTDOWN(log)) {
2701                                 spin_lock(&log->l_grant_lock);
2702                                 goto error_return;
2703                         }
2704
2705                         xlog_trace_loggrant(log, tic,
2706                                     "xlog_regrant_write_log_space: wake 1");
2707                         xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2708                         spin_lock(&log->l_grant_lock);
2709                 }
2710         }
2711
2712         need_bytes = tic->t_unit_res;
2713
2714 redo:
2715         if (XLOG_FORCED_SHUTDOWN(log))
2716                 goto error_return;
2717
2718         free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2719                                      log->l_grant_write_bytes);
2720         if (free_bytes < need_bytes) {
2721                 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
2722                         xlog_ins_ticketq(&log->l_write_headq, tic);
2723                 XFS_STATS_INC(xs_sleep_logspace);
2724                 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
2725
2726                 /* If we're shutting down, this tic is already off the queue */
2727                 if (XLOG_FORCED_SHUTDOWN(log)) {
2728                         spin_lock(&log->l_grant_lock);
2729                         goto error_return;
2730                 }
2731
2732                 xlog_trace_loggrant(log, tic,
2733                                     "xlog_regrant_write_log_space: wake 2");
2734                 xlog_grant_push_ail(log->l_mp, need_bytes);
2735                 spin_lock(&log->l_grant_lock);
2736                 goto redo;
2737         } else if (tic->t_flags & XLOG_TIC_IN_Q)
2738                 xlog_del_ticketq(&log->l_write_headq, tic);
2739
2740         /* we've got enough space */
2741         xlog_grant_add_space_write(log, need_bytes);
2742 #ifdef DEBUG
2743         tail_lsn = log->l_tail_lsn;
2744         if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2745                 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2746                 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2747         }
2748 #endif
2749
2750         xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2751         xlog_verify_grant_head(log, 1);
2752         spin_unlock(&log->l_grant_lock);
2753         return 0;
2754
2755
2756  error_return:
2757         if (tic->t_flags & XLOG_TIC_IN_Q)
2758                 xlog_del_ticketq(&log->l_reserve_headq, tic);
2759         xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2760         /*
2761          * If we are failing, make sure the ticket doesn't have any
2762          * current reservations. We don't want to add this back when
2763          * the ticket/transaction gets cancelled.
2764          */
2765         tic->t_curr_res = 0;
2766         tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2767         spin_unlock(&log->l_grant_lock);
2768         return XFS_ERROR(EIO);
2769 }       /* xlog_regrant_write_log_space */
2770
2771
2772 /* The first cnt-1 times through here we don't need to
2773  * move the grant write head because the permanent
2774  * reservation has reserved cnt times the unit amount.
2775  * Release part of current permanent unit reservation and
2776  * reset current reservation to be one units worth.  Also
2777  * move grant reservation head forward.
2778  */
2779 STATIC void
2780 xlog_regrant_reserve_log_space(xlog_t        *log,
2781                                xlog_ticket_t *ticket)
2782 {
2783         xlog_trace_loggrant(log, ticket,
2784                             "xlog_regrant_reserve_log_space: enter");
2785         if (ticket->t_cnt > 0)
2786                 ticket->t_cnt--;
2787
2788         spin_lock(&log->l_grant_lock);
2789         xlog_grant_sub_space(log, ticket->t_curr_res);
2790         ticket->t_curr_res = ticket->t_unit_res;
2791         xlog_tic_reset_res(ticket);
2792         xlog_trace_loggrant(log, ticket,
2793                             "xlog_regrant_reserve_log_space: sub current res");
2794         xlog_verify_grant_head(log, 1);
2795
2796         /* just return if we still have some of the pre-reserved space */
2797         if (ticket->t_cnt > 0) {
2798                 spin_unlock(&log->l_grant_lock);
2799                 return;
2800         }
2801
2802         xlog_grant_add_space_reserve(log, ticket->t_unit_res);
2803         xlog_trace_loggrant(log, ticket,
2804                             "xlog_regrant_reserve_log_space: exit");
2805         xlog_verify_grant_head(log, 0);
2806         spin_unlock(&log->l_grant_lock);
2807         ticket->t_curr_res = ticket->t_unit_res;
2808         xlog_tic_reset_res(ticket);
2809 }       /* xlog_regrant_reserve_log_space */
2810
2811
2812 /*
2813  * Give back the space left from a reservation.
2814  *
2815  * All the information we need to make a correct determination of space left
2816  * is present.  For non-permanent reservations, things are quite easy.  The
2817  * count should have been decremented to zero.  We only need to deal with the
2818  * space remaining in the current reservation part of the ticket.  If the
2819  * ticket contains a permanent reservation, there may be left over space which
2820  * needs to be released.  A count of N means that N-1 refills of the current
2821  * reservation can be done before we need to ask for more space.  The first
2822  * one goes to fill up the first current reservation.  Once we run out of
2823  * space, the count will stay at zero and the only space remaining will be
2824  * in the current reservation field.
2825  */
2826 STATIC void
2827 xlog_ungrant_log_space(xlog_t        *log,
2828                        xlog_ticket_t *ticket)
2829 {
2830         if (ticket->t_cnt > 0)
2831                 ticket->t_cnt--;
2832
2833         spin_lock(&log->l_grant_lock);
2834         xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2835
2836         xlog_grant_sub_space(log, ticket->t_curr_res);
2837
2838         xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2839
2840         /* If this is a permanent reservation ticket, we may be able to free
2841          * up more space based on the remaining count.
2842          */
2843         if (ticket->t_cnt > 0) {
2844                 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
2845                 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
2846         }
2847
2848         xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2849         xlog_verify_grant_head(log, 1);
2850         spin_unlock(&log->l_grant_lock);
2851         xfs_log_move_tail(log->l_mp, 1);
2852 }       /* xlog_ungrant_log_space */
2853
2854
2855 /*
2856  * Flush iclog to disk if this is the last reference to the given iclog and
2857  * the WANT_SYNC bit is set.
2858  *
2859  * When this function is entered, the iclog is not necessarily in the
2860  * WANT_SYNC state.  It may be sitting around waiting to get filled.
2861  *
2862  *
2863  */
2864 STATIC int
2865 xlog_state_release_iclog(
2866         xlog_t          *log,
2867         xlog_in_core_t  *iclog)
2868 {
2869         int             sync = 0;       /* do we sync? */
2870
2871         if (iclog->ic_state & XLOG_STATE_IOERROR)
2872                 return XFS_ERROR(EIO);
2873
2874         ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2875         if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2876                 return 0;
2877
2878         if (iclog->ic_state & XLOG_STATE_IOERROR) {
2879                 spin_unlock(&log->l_icloglock);
2880                 return XFS_ERROR(EIO);
2881         }
2882         ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2883                iclog->ic_state == XLOG_STATE_WANT_SYNC);
2884
2885         if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2886                 /* update tail before writing to iclog */
2887                 xlog_assign_tail_lsn(log->l_mp);
2888                 sync++;
2889                 iclog->ic_state = XLOG_STATE_SYNCING;
2890                 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
2891                 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2892                 /* cycle incremented when incrementing curr_block */
2893         }
2894         spin_unlock(&log->l_icloglock);
2895
2896         /*
2897          * We let the log lock go, so it's possible that we hit a log I/O
2898          * error or some other SHUTDOWN condition that marks the iclog
2899          * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2900          * this iclog has consistent data, so we ignore IOERROR
2901          * flags after this point.
2902          */
2903         if (sync)
2904                 return xlog_sync(log, iclog);
2905         return 0;
2906 }       /* xlog_state_release_iclog */
2907
2908
2909 /*
2910  * This routine will mark the current iclog in the ring as WANT_SYNC
2911  * and move the current iclog pointer to the next iclog in the ring.
2912  * When this routine is called from xlog_state_get_iclog_space(), the
2913  * exact size of the iclog has not yet been determined.  All we know is
2914  * that every data block.  We have run out of space in this log record.
2915  */
2916 STATIC void
2917 xlog_state_switch_iclogs(xlog_t         *log,
2918                          xlog_in_core_t *iclog,
2919                          int            eventual_size)
2920 {
2921         ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2922         if (!eventual_size)
2923                 eventual_size = iclog->ic_offset;
2924         iclog->ic_state = XLOG_STATE_WANT_SYNC;
2925         iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
2926         log->l_prev_block = log->l_curr_block;
2927         log->l_prev_cycle = log->l_curr_cycle;
2928
2929         /* roll log?: ic_offset changed later */
2930         log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2931
2932         /* Round up to next log-sunit */
2933         if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
2934             log->l_mp->m_sb.sb_logsunit > 1) {
2935                 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2936                 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2937         }
2938
2939         if (log->l_curr_block >= log->l_logBBsize) {
2940                 log->l_curr_cycle++;
2941                 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2942                         log->l_curr_cycle++;
2943                 log->l_curr_block -= log->l_logBBsize;
2944                 ASSERT(log->l_curr_block >= 0);
2945         }
2946         ASSERT(iclog == log->l_iclog);
2947         log->l_iclog = iclog->ic_next;
2948 }       /* xlog_state_switch_iclogs */
2949
2950
2951 /*
2952  * Write out all data in the in-core log as of this exact moment in time.
2953  *
2954  * Data may be written to the in-core log during this call.  However,
2955  * we don't guarantee this data will be written out.  A change from past
2956  * implementation means this routine will *not* write out zero length LRs.
2957  *
2958  * Basically, we try and perform an intelligent scan of the in-core logs.
2959  * If we determine there is no flushable data, we just return.  There is no
2960  * flushable data if:
2961  *
2962  *      1. the current iclog is active and has no data; the previous iclog
2963  *              is in the active or dirty state.
2964  *      2. the current iclog is drity, and the previous iclog is in the
2965  *              active or dirty state.
2966  *
2967  * We may sleep if:
2968  *
2969  *      1. the current iclog is not in the active nor dirty state.
2970  *      2. the current iclog dirty, and the previous iclog is not in the
2971  *              active nor dirty state.
2972  *      3. the current iclog is active, and there is another thread writing
2973  *              to this particular iclog.
2974  *      4. a) the current iclog is active and has no other writers
2975  *         b) when we return from flushing out this iclog, it is still
2976  *              not in the active nor dirty state.
2977  */
2978 STATIC int
2979 xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
2980 {
2981         xlog_in_core_t  *iclog;
2982         xfs_lsn_t       lsn;
2983
2984         spin_lock(&log->l_icloglock);
2985
2986         iclog = log->l_iclog;
2987         if (iclog->ic_state & XLOG_STATE_IOERROR) {
2988                 spin_unlock(&log->l_icloglock);
2989                 return XFS_ERROR(EIO);
2990         }
2991
2992         /* If the head iclog is not active nor dirty, we just attach
2993          * ourselves to the head and go to sleep.
2994          */
2995         if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2996             iclog->ic_state == XLOG_STATE_DIRTY) {
2997                 /*
2998                  * If the head is dirty or (active and empty), then
2999                  * we need to look at the previous iclog.  If the previous
3000                  * iclog is active or dirty we are done.  There is nothing
3001                  * to sync out.  Otherwise, we attach ourselves to the
3002                  * previous iclog and go to sleep.
3003                  */
3004                 if (iclog->ic_state == XLOG_STATE_DIRTY ||
3005                     (atomic_read(&iclog->ic_refcnt) == 0
3006                      && iclog->ic_offset == 0)) {
3007                         iclog = iclog->ic_prev;
3008                         if (iclog->ic_state == XLOG_STATE_ACTIVE ||
3009                             iclog->ic_state == XLOG_STATE_DIRTY)
3010                                 goto no_sleep;
3011                         else
3012                                 goto maybe_sleep;
3013                 } else {
3014                         if (atomic_read(&iclog->ic_refcnt) == 0) {
3015                                 /* We are the only one with access to this
3016                                  * iclog.  Flush it out now.  There should
3017                                  * be a roundoff of zero to show that someone
3018                                  * has already taken care of the roundoff from
3019                                  * the previous sync.
3020                                  */
3021                                 atomic_inc(&iclog->ic_refcnt);
3022                                 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
3023                                 xlog_state_switch_iclogs(log, iclog, 0);
3024                                 spin_unlock(&log->l_icloglock);
3025
3026                                 if (xlog_state_release_iclog(log, iclog))
3027                                         return XFS_ERROR(EIO);
3028                                 *log_flushed = 1;
3029                                 spin_lock(&log->l_icloglock);
3030                                 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
3031                                     iclog->ic_state != XLOG_STATE_DIRTY)
3032                                         goto maybe_sleep;
3033                                 else
3034                                         goto no_sleep;
3035                         } else {
3036                                 /* Someone else is writing to this iclog.
3037                                  * Use its call to flush out the data.  However,
3038                                  * the other thread may not force out this LR,
3039                                  * so we mark it WANT_SYNC.
3040                                  */
3041                                 xlog_state_switch_iclogs(log, iclog, 0);
3042                                 goto maybe_sleep;
3043                         }
3044                 }
3045         }
3046
3047         /* By the time we come around again, the iclog could've been filled
3048          * which would give it another lsn.  If we have a new lsn, just
3049          * return because the relevant data has been flushed.
3050          */
3051 maybe_sleep:
3052         if (flags & XFS_LOG_SYNC) {
3053                 /*
3054                  * We must check if we're shutting down here, before
3055                  * we wait, while we're holding the l_icloglock.
3056                  * Then we check again after waking up, in case our
3057                  * sleep was disturbed by a bad news.
3058                  */
3059                 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3060                         spin_unlock(&log->l_icloglock);
3061                         return XFS_ERROR(EIO);
3062                 }
3063                 XFS_STATS_INC(xs_log_force_sleep);
3064                 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
3065                 /*
3066                  * No need to grab the log lock here since we're
3067                  * only deciding whether or not to return EIO
3068                  * and the memory read should be atomic.
3069                  */
3070                 if (iclog->ic_state & XLOG_STATE_IOERROR)
3071                         return XFS_ERROR(EIO);
3072                 *log_flushed = 1;
3073
3074         } else {
3075
3076 no_sleep:
3077                 spin_unlock(&log->l_icloglock);
3078         }
3079         return 0;
3080 }       /* xlog_state_sync_all */
3081
3082
3083 /*
3084  * Used by code which implements synchronous log forces.
3085  *
3086  * Find in-core log with lsn.
3087  *      If it is in the DIRTY state, just return.
3088  *      If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3089  *              state and go to sleep or return.
3090  *      If it is in any other state, go to sleep or return.
3091  *
3092  * If filesystem activity goes to zero, the iclog will get flushed only by
3093  * bdflush().
3094  */
3095 STATIC int
3096 xlog_state_sync(xlog_t    *log,
3097                 xfs_lsn_t lsn,
3098                 uint      flags,
3099                 int       *log_flushed)
3100 {
3101     xlog_in_core_t      *iclog;
3102     int                 already_slept = 0;
3103
3104 try_again:
3105     spin_lock(&log->l_icloglock);
3106     iclog = log->l_iclog;
3107
3108     if (iclog->ic_state & XLOG_STATE_IOERROR) {
3109             spin_unlock(&log->l_icloglock);
3110             return XFS_ERROR(EIO);
3111     }
3112
3113     do {
3114         if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3115                 iclog = iclog->ic_next;
3116                 continue;
3117         }
3118
3119         if (iclog->ic_state == XLOG_STATE_DIRTY) {
3120                 spin_unlock(&log->l_icloglock);
3121                 return 0;
3122         }
3123
3124         if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3125                 /*
3126                  * We sleep here if we haven't already slept (e.g.
3127                  * this is the first time we've looked at the correct
3128                  * iclog buf) and the buffer before us is going to
3129                  * be sync'ed. The reason for this is that if we
3130                  * are doing sync transactions here, by waiting for
3131                  * the previous I/O to complete, we can allow a few
3132                  * more transactions into this iclog before we close
3133                  * it down.
3134                  *
3135                  * Otherwise, we mark the buffer WANT_SYNC, and bump
3136                  * up the refcnt so we can release the log (which drops
3137                  * the ref count).  The state switch keeps new transaction
3138                  * commits from using this buffer.  When the current commits
3139                  * finish writing into the buffer, the refcount will drop to
3140                  * zero and the buffer will go out then.
3141                  */
3142                 if (!already_slept &&
3143                     (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3144                                                  XLOG_STATE_SYNCING))) {
3145                         ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3146                         XFS_STATS_INC(xs_log_force_sleep);
3147                         sv_wait(&iclog->ic_prev->ic_write_wait, PSWP,
3148                                 &log->l_icloglock, s);
3149                         *log_flushed = 1;
3150                         already_slept = 1;
3151                         goto try_again;
3152                 } else {
3153                         atomic_inc(&iclog->ic_refcnt);
3154                         xlog_state_switch_iclogs(log, iclog, 0);
3155                         spin_unlock(&log->l_icloglock);
3156                         if (xlog_state_release_iclog(log, iclog))
3157                                 return XFS_ERROR(EIO);
3158                         *log_flushed = 1;
3159                         spin_lock(&log->l_icloglock);
3160                 }
3161         }
3162
3163         if ((flags & XFS_LOG_SYNC) && /* sleep */
3164             !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3165
3166                 /*
3167                  * Don't wait on completion if we know that we've
3168                  * gotten a log write error.
3169                  */
3170                 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3171                         spin_unlock(&log->l_icloglock);
3172                         return XFS_ERROR(EIO);
3173                 }
3174                 XFS_STATS_INC(xs_log_force_sleep);
3175                 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3176                 /*
3177                  * No need to grab the log lock here since we're
3178                  * only deciding whether or not to return EIO
3179                  * and the memory read should be atomic.
3180                  */
3181                 if (iclog->ic_state & XLOG_STATE_IOERROR)
3182                         return XFS_ERROR(EIO);
3183                 *log_flushed = 1;
3184         } else {                /* just return */
3185                 spin_unlock(&log->l_icloglock);
3186         }
3187         return 0;
3188
3189     } while (iclog != log->l_iclog);
3190
3191     spin_unlock(&log->l_icloglock);
3192     return 0;
3193 }       /* xlog_state_sync */
3194
3195
3196 /*
3197  * Called when we want to mark the current iclog as being ready to sync to
3198  * disk.
3199  */
3200 STATIC void
3201 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3202 {
3203         spin_lock(&log->l_icloglock);
3204
3205         if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3206                 xlog_state_switch_iclogs(log, iclog, 0);
3207         } else {
3208                 ASSERT(iclog->ic_state &
3209                         (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3210         }
3211
3212         spin_unlock(&log->l_icloglock);
3213 }       /* xlog_state_want_sync */
3214
3215
3216
3217 /*****************************************************************************
3218  *
3219  *              TICKET functions
3220  *
3221  *****************************************************************************
3222  */
3223
3224 /*
3225  * Free a used ticket.
3226  */
3227 STATIC void
3228 xlog_ticket_put(xlog_t          *log,
3229                 xlog_ticket_t   *ticket)
3230 {
3231         sv_destroy(&ticket->t_wait);
3232         kmem_zone_free(xfs_log_ticket_zone, ticket);
3233 }       /* xlog_ticket_put */
3234
3235
3236 /*
3237  * Allocate and initialise a new log ticket.
3238  */
3239 STATIC xlog_ticket_t *
3240 xlog_ticket_get(xlog_t          *log,
3241                 int             unit_bytes,
3242                 int             cnt,
3243                 char            client,
3244                 uint            xflags)
3245 {
3246         xlog_ticket_t   *tic;
3247         uint            num_headers;
3248
3249         tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3250         if (!tic)
3251                 return NULL;
3252
3253         /*
3254          * Permanent reservations have up to 'cnt'-1 active log operations
3255          * in the log.  A unit in this case is the amount of space for one
3256          * of these log operations.  Normal reservations have a cnt of 1
3257          * and their unit amount is the total amount of space required.
3258          *
3259          * The following lines of code account for non-transaction data
3260          * which occupy space in the on-disk log.
3261          *
3262          * Normal form of a transaction is:
3263          * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3264          * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3265          *
3266          * We need to account for all the leadup data and trailer data
3267          * around the transaction data.
3268          * And then we need to account for the worst case in terms of using
3269          * more space.
3270          * The worst case will happen if:
3271          * - the placement of the transaction happens to be such that the
3272          *   roundoff is at its maximum
3273          * - the transaction data is synced before the commit record is synced
3274          *   i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3275          *   Therefore the commit record is in its own Log Record.
3276          *   This can happen as the commit record is called with its
3277          *   own region to xlog_write().
3278          *   This then means that in the worst case, roundoff can happen for
3279          *   the commit-rec as well.
3280          *   The commit-rec is smaller than padding in this scenario and so it is
3281          *   not added separately.
3282          */
3283
3284         /* for trans header */
3285         unit_bytes += sizeof(xlog_op_header_t);
3286         unit_bytes += sizeof(xfs_trans_header_t);
3287
3288         /* for start-rec */
3289         unit_bytes += sizeof(xlog_op_header_t);
3290
3291         /* for LR headers */
3292         num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3293         unit_bytes += log->l_iclog_hsize * num_headers;
3294
3295         /* for commit-rec LR header - note: padding will subsume the ophdr */
3296         unit_bytes += log->l_iclog_hsize;
3297
3298         /* for split-recs - ophdrs added when data split over LRs */
3299         unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3300
3301         /* for roundoff padding for transaction data and one for commit record */
3302         if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
3303             log->l_mp->m_sb.sb_logsunit > 1) {
3304                 /* log su roundoff */
3305                 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3306         } else {
3307                 /* BB roundoff */
3308                 unit_bytes += 2*BBSIZE;
3309         }
3310
3311         tic->t_unit_res         = unit_bytes;
3312         tic->t_curr_res         = unit_bytes;
3313         tic->t_cnt              = cnt;
3314         tic->t_ocnt             = cnt;
3315         tic->t_tid              = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3316         tic->t_clientid         = client;
3317         tic->t_flags            = XLOG_TIC_INITED;
3318         tic->t_trans_type       = 0;
3319         if (xflags & XFS_LOG_PERM_RESERV)
3320                 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3321         sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
3322
3323         xlog_tic_reset_res(tic);
3324
3325         return tic;
3326 }       /* xlog_ticket_get */
3327
3328
3329 /******************************************************************************
3330  *
3331  *              Log debug routines
3332  *
3333  ******************************************************************************
3334  */
3335 #if defined(DEBUG)
3336 /*
3337  * Make sure that the destination ptr is within the valid data region of
3338  * one of the iclogs.  This uses backup pointers stored in a different
3339  * part of the log in case we trash the log structure.
3340  */
3341 void
3342 xlog_verify_dest_ptr(xlog_t     *log,
3343                      __psint_t  ptr)
3344 {
3345         int i;
3346         int good_ptr = 0;
3347
3348         for (i=0; i < log->l_iclog_bufs; i++) {
3349                 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3350                     ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3351                         good_ptr++;
3352         }
3353         if (! good_ptr)
3354                 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3355 }       /* xlog_verify_dest_ptr */
3356
3357 STATIC void
3358 xlog_verify_grant_head(xlog_t *log, int equals)
3359 {
3360     if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3361         if (equals)
3362             ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3363         else
3364             ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3365     } else {
3366         ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3367         ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3368     }
3369 }       /* xlog_verify_grant_head */
3370
3371 /* check if it will fit */
3372 STATIC void
3373 xlog_verify_tail_lsn(xlog_t         *log,
3374                      xlog_in_core_t *iclog,
3375                      xfs_lsn_t      tail_lsn)
3376 {
3377     int blocks;
3378
3379     if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3380         blocks =
3381             log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3382         if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3383             xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3384     } else {
3385         ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3386
3387         if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3388             xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3389
3390         blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3391         if (blocks < BTOBB(iclog->ic_offset) + 1)
3392             xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3393     }
3394 }       /* xlog_verify_tail_lsn */
3395
3396 /*
3397  * Perform a number of checks on the iclog before writing to disk.
3398  *
3399  * 1. Make sure the iclogs are still circular
3400  * 2. Make sure we have a good magic number
3401  * 3. Make sure we don't have magic numbers in the data
3402  * 4. Check fields of each log operation header for:
3403  *      A. Valid client identifier
3404  *      B. tid ptr value falls in valid ptr space (user space code)
3405  *      C. Length in log record header is correct according to the
3406  *              individual operation headers within record.
3407  * 5. When a bwrite will occur within 5 blocks of the front of the physical
3408  *      log, check the preceding blocks of the physical log to make sure all
3409  *      the cycle numbers agree with the current cycle number.
3410  */
3411 STATIC void
3412 xlog_verify_iclog(xlog_t         *log,
3413                   xlog_in_core_t *iclog,
3414                   int            count,
3415                   boolean_t      syncing)
3416 {
3417         xlog_op_header_t        *ophead;
3418         xlog_in_core_t          *icptr;
3419         xlog_in_core_2_t        *xhdr;
3420         xfs_caddr_t             ptr;
3421         xfs_caddr_t             base_ptr;
3422         __psint_t               field_offset;
3423         __uint8_t               clientid;
3424         int                     len, i, j, k, op_len;
3425         int                     idx;
3426
3427         /* check validity of iclog pointers */
3428         spin_lock(&log->l_icloglock);
3429         icptr = log->l_iclog;
3430         for (i=0; i < log->l_iclog_bufs; i++) {
3431                 if (icptr == NULL)
3432                         xlog_panic("xlog_verify_iclog: invalid ptr");
3433                 icptr = icptr->ic_next;
3434         }
3435         if (icptr != log->l_iclog)
3436                 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3437         spin_unlock(&log->l_icloglock);
3438
3439         /* check log magic numbers */
3440         if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
3441                 xlog_panic("xlog_verify_iclog: invalid magic num");
3442
3443         ptr = (xfs_caddr_t) &iclog->ic_header;
3444         for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
3445              ptr += BBSIZE) {
3446                 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
3447                         xlog_panic("xlog_verify_iclog: unexpected magic num");
3448         }
3449
3450         /* check fields */
3451         len = be32_to_cpu(iclog->ic_header.h_num_logops);
3452         ptr = iclog->ic_datap;
3453         base_ptr = ptr;
3454         ophead = (xlog_op_header_t *)ptr;
3455         xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3456         for (i = 0; i < len; i++) {
3457                 ophead = (xlog_op_header_t *)ptr;
3458
3459                 /* clientid is only 1 byte */
3460                 field_offset = (__psint_t)
3461                                ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3462                 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3463                         clientid = ophead->oh_clientid;
3464                 } else {
3465                         idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3466                         if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3467                                 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3468                                 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3469                                 clientid = xlog_get_client_id(
3470                                         xhdr[j].hic_xheader.xh_cycle_data[k]);
3471                         } else {
3472                                 clientid = xlog_get_client_id(
3473                                         iclog->ic_header.h_cycle_data[idx]);
3474                         }
3475                 }
3476                 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3477                         cmn_err(CE_WARN, "xlog_verify_iclog: "
3478                                 "invalid clientid %d op 0x%p offset 0x%lx",
3479                                 clientid, ophead, (unsigned long)field_offset);
3480
3481                 /* check length */
3482                 field_offset = (__psint_t)
3483                                ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3484                 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3485                         op_len = be32_to_cpu(ophead->oh_len);
3486                 } else {
3487                         idx = BTOBBT((__psint_t)&ophead->oh_len -
3488                                     (__psint_t)iclog->ic_datap);
3489                         if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3490                                 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3491                                 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3492                                 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
3493                         } else {
3494                                 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
3495                         }
3496                 }
3497                 ptr += sizeof(xlog_op_header_t) + op_len;
3498         }
3499 }       /* xlog_verify_iclog */
3500 #endif
3501
3502 /*
3503  * Mark all iclogs IOERROR. l_icloglock is held by the caller.
3504  */
3505 STATIC int
3506 xlog_state_ioerror(
3507         xlog_t  *log)
3508 {
3509         xlog_in_core_t  *iclog, *ic;
3510
3511         iclog = log->l_iclog;
3512         if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3513                 /*
3514                  * Mark all the incore logs IOERROR.
3515                  * From now on, no log flushes will result.
3516                  */
3517                 ic = iclog;
3518                 do {
3519                         ic->ic_state = XLOG_STATE_IOERROR;
3520                         ic = ic->ic_next;
3521                 } while (ic != iclog);
3522                 return 0;
3523         }
3524         /*
3525          * Return non-zero, if state transition has already happened.
3526          */
3527         return 1;
3528 }
3529
3530 /*
3531  * This is called from xfs_force_shutdown, when we're forcibly
3532  * shutting down the filesystem, typically because of an IO error.
3533  * Our main objectives here are to make sure that:
3534  *      a. the filesystem gets marked 'SHUTDOWN' for all interested
3535  *         parties to find out, 'atomically'.
3536  *      b. those who're sleeping on log reservations, pinned objects and
3537  *          other resources get woken up, and be told the bad news.
3538  *      c. nothing new gets queued up after (a) and (b) are done.
3539  *      d. if !logerror, flush the iclogs to disk, then seal them off
3540  *         for business.
3541  */
3542 int
3543 xfs_log_force_umount(
3544         struct xfs_mount        *mp,
3545         int                     logerror)
3546 {
3547         xlog_ticket_t   *tic;
3548         xlog_t          *log;
3549         int             retval;
3550         int             dummy;
3551
3552         log = mp->m_log;
3553
3554         /*
3555          * If this happens during log recovery, don't worry about
3556          * locking; the log isn't open for business yet.
3557          */
3558         if (!log ||
3559             log->l_flags & XLOG_ACTIVE_RECOVERY) {
3560                 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3561                 XFS_BUF_DONE(mp->m_sb_bp);
3562                 return 0;
3563         }
3564
3565         /*
3566          * Somebody could've already done the hard work for us.
3567          * No need to get locks for this.
3568          */
3569         if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3570                 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3571                 return 1;
3572         }
3573         retval = 0;
3574         /*
3575          * We must hold both the GRANT lock and the LOG lock,
3576          * before we mark the filesystem SHUTDOWN and wake
3577          * everybody up to tell the bad news.
3578          */
3579         spin_lock(&log->l_icloglock);
3580         spin_lock(&log->l_grant_lock);
3581         mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3582         XFS_BUF_DONE(mp->m_sb_bp);
3583         /*
3584          * This flag is sort of redundant because of the mount flag, but
3585          * it's good to maintain the separation between the log and the rest
3586          * of XFS.
3587          */
3588         log->l_flags |= XLOG_IO_ERROR;
3589
3590         /*
3591          * If we hit a log error, we want to mark all the iclogs IOERROR
3592          * while we're still holding the loglock.
3593          */
3594         if (logerror)
3595                 retval = xlog_state_ioerror(log);
3596         spin_unlock(&log->l_icloglock);
3597
3598         /*
3599          * We don't want anybody waiting for log reservations
3600          * after this. That means we have to wake up everybody
3601          * queued up on reserve_headq as well as write_headq.
3602          * In addition, we make sure in xlog_{re}grant_log_space
3603          * that we don't enqueue anything once the SHUTDOWN flag
3604          * is set, and this action is protected by the GRANTLOCK.
3605          */
3606         if ((tic = log->l_reserve_headq)) {
3607                 do {
3608                         sv_signal(&tic->t_wait);
3609                         tic = tic->t_next;
3610                 } while (tic != log->l_reserve_headq);
3611         }
3612
3613         if ((tic = log->l_write_headq)) {
3614                 do {
3615                         sv_signal(&tic->t_wait);
3616                         tic = tic->t_next;
3617                 } while (tic != log->l_write_headq);
3618         }
3619         spin_unlock(&log->l_grant_lock);
3620
3621         if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3622                 ASSERT(!logerror);
3623                 /*
3624                  * Force the incore logs to disk before shutting the
3625                  * log down completely.
3626                  */
3627                 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
3628                 spin_lock(&log->l_icloglock);
3629                 retval = xlog_state_ioerror(log);
3630                 spin_unlock(&log->l_icloglock);
3631         }
3632         /*
3633          * Wake up everybody waiting on xfs_log_force.
3634          * Callback all log item committed functions as if the
3635          * log writes were completed.
3636          */
3637         xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3638
3639 #ifdef XFSERRORDEBUG
3640         {
3641                 xlog_in_core_t  *iclog;
3642
3643                 spin_lock(&log->l_icloglock);
3644                 iclog = log->l_iclog;
3645                 do {
3646                         ASSERT(iclog->ic_callback == 0);
3647                         iclog = iclog->ic_next;
3648                 } while (iclog != log->l_iclog);
3649                 spin_unlock(&log->l_icloglock);
3650         }
3651 #endif
3652         /* return non-zero if log IOERROR transition had already happened */
3653         return retval;
3654 }
3655
3656 STATIC int
3657 xlog_iclogs_empty(xlog_t *log)
3658 {
3659         xlog_in_core_t  *iclog;
3660
3661         iclog = log->l_iclog;
3662         do {
3663                 /* endianness does not matter here, zero is zero in
3664                  * any language.
3665                  */
3666                 if (iclog->ic_header.h_num_logops)
3667                         return 0;
3668                 iclog = iclog->ic_next;
3669         } while (iclog != log->l_iclog);
3670         return 1;
3671 }