forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xfs_log.c
3705 lines (3298 loc) · 106 KB
/
xfs_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000-2005 Silicon Graphics, Inc.
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_types.h"
#include "xfs_bit.h"
#include "xfs_log.h"
#include "xfs_inum.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_dir2.h"
#include "xfs_dmapi.h"
#include "xfs_mount.h"
#include "xfs_error.h"
#include "xfs_log_priv.h"
#include "xfs_buf_item.h"
#include "xfs_bmap_btree.h"
#include "xfs_alloc_btree.h"
#include "xfs_ialloc_btree.h"
#include "xfs_log_recover.h"
#include "xfs_trans_priv.h"
#include "xfs_dir2_sf.h"
#include "xfs_attr_sf.h"
#include "xfs_dinode.h"
#include "xfs_inode.h"
#include "xfs_rw.h"
#define xlog_write_adv_cnt(ptr, len, off, bytes) \
{ (ptr) += (bytes); \
(len) -= (bytes); \
(off) += (bytes);}
/* Local miscellaneous function prototypes */
STATIC int xlog_bdstrat_cb(struct xfs_buf *);
STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
xlog_in_core_t **, xfs_lsn_t *);
STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
xfs_buftarg_t *log_target,
xfs_daddr_t blk_offset,
int num_bblks);
STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
STATIC void xlog_dealloc_log(xlog_t *log);
STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
int nentries, xfs_log_ticket_t tic,
xfs_lsn_t *start_lsn,
xlog_in_core_t **commit_iclog,
uint flags);
/* local state machine functions */
STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
STATIC int xlog_state_get_iclog_space(xlog_t *log,
int len,
xlog_in_core_t **iclog,
xlog_ticket_t *ticket,
int *continued_write,
int *logoffsetp);
STATIC void xlog_state_put_ticket(xlog_t *log,
xlog_ticket_t *tic);
STATIC int xlog_state_release_iclog(xlog_t *log,
xlog_in_core_t *iclog);
STATIC void xlog_state_switch_iclogs(xlog_t *log,
xlog_in_core_t *iclog,
int eventual_size);
STATIC int xlog_state_sync(xlog_t *log,
xfs_lsn_t lsn,
uint flags,
int *log_flushed);
STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
/* local functions to manipulate grant head */
STATIC int xlog_grant_log_space(xlog_t *log,
xlog_ticket_t *xtic);
STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
int need_bytes);
STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
xlog_ticket_t *ticket);
STATIC int xlog_regrant_write_log_space(xlog_t *log,
xlog_ticket_t *ticket);
STATIC void xlog_ungrant_log_space(xlog_t *log,
xlog_ticket_t *ticket);
/* local ticket functions */
STATIC void xlog_state_ticket_alloc(xlog_t *log);
STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
int unit_bytes,
int count,
char clientid,
uint flags);
STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
#if defined(DEBUG)
STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
int count, boolean_t syncing);
STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
xfs_lsn_t tail_lsn);
#else
#define xlog_verify_dest_ptr(a,b)
#define xlog_verify_grant_head(a,b)
#define xlog_verify_iclog(a,b,c,d)
#define xlog_verify_tail_lsn(a,b,c)
#endif
STATIC int xlog_iclogs_empty(xlog_t *log);
#if defined(XFS_LOG_TRACE)
void
xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
{
unsigned long cnts;
if (!log->l_grant_trace) {
log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
if (!log->l_grant_trace)
return;
}
/* ticket counts are 1 byte each */
cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
ktrace_enter(log->l_grant_trace,
(void *)tic,
(void *)log->l_reserve_headq,
(void *)log->l_write_headq,
(void *)((unsigned long)log->l_grant_reserve_cycle),
(void *)((unsigned long)log->l_grant_reserve_bytes),
(void *)((unsigned long)log->l_grant_write_cycle),
(void *)((unsigned long)log->l_grant_write_bytes),
(void *)((unsigned long)log->l_curr_cycle),
(void *)((unsigned long)log->l_curr_block),
(void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
(void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
(void *)string,
(void *)((unsigned long)tic->t_trans_type),
(void *)cnts,
(void *)((unsigned long)tic->t_curr_res),
(void *)((unsigned long)tic->t_unit_res));
}
void
xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
{
if (!iclog->ic_trace)
iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
ktrace_enter(iclog->ic_trace,
(void *)((unsigned long)state),
(void *)((unsigned long)current_pid()),
(void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
(void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
(void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
(void *)NULL, (void *)NULL);
}
#else
#define xlog_trace_loggrant(log,tic,string)
#define xlog_trace_iclog(iclog,state)
#endif /* XFS_LOG_TRACE */
static void
xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
{
if (*qp) {
tic->t_next = (*qp);
tic->t_prev = (*qp)->t_prev;
(*qp)->t_prev->t_next = tic;
(*qp)->t_prev = tic;
} else {
tic->t_prev = tic->t_next = tic;
*qp = tic;
}
tic->t_flags |= XLOG_TIC_IN_Q;
}
static void
xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
{
if (tic == tic->t_next) {
*qp = NULL;
} else {
*qp = tic->t_next;
tic->t_next->t_prev = tic->t_prev;
tic->t_prev->t_next = tic->t_next;
}
tic->t_next = tic->t_prev = NULL;
tic->t_flags &= ~XLOG_TIC_IN_Q;
}
static void
xlog_grant_sub_space(struct log *log, int bytes)
{
log->l_grant_write_bytes -= bytes;
if (log->l_grant_write_bytes < 0) {
log->l_grant_write_bytes += log->l_logsize;
log->l_grant_write_cycle--;
}
log->l_grant_reserve_bytes -= bytes;
if ((log)->l_grant_reserve_bytes < 0) {
log->l_grant_reserve_bytes += log->l_logsize;
log->l_grant_reserve_cycle--;
}
}
static void
xlog_grant_add_space_write(struct log *log, int bytes)
{
log->l_grant_write_bytes += bytes;
if (log->l_grant_write_bytes > log->l_logsize) {
log->l_grant_write_bytes -= log->l_logsize;
log->l_grant_write_cycle++;
}
}
static void
xlog_grant_add_space_reserve(struct log *log, int bytes)
{
log->l_grant_reserve_bytes += bytes;
if (log->l_grant_reserve_bytes > log->l_logsize) {
log->l_grant_reserve_bytes -= log->l_logsize;
log->l_grant_reserve_cycle++;
}
}
static inline void
xlog_grant_add_space(struct log *log, int bytes)
{
xlog_grant_add_space_write(log, bytes);
xlog_grant_add_space_reserve(log, bytes);
}
/*
* NOTES:
*
* 1. currblock field gets updated at startup and after in-core logs
* marked as with WANT_SYNC.
*/
/*
* This routine is called when a user of a log manager ticket is done with
* the reservation. If the ticket was ever used, then a commit record for
* the associated transaction is written out as a log operation header with
* no data. The flag XLOG_TIC_INITED is set when the first write occurs with
* a given ticket. If the ticket was one with a permanent reservation, then
* a few operations are done differently. Permanent reservation tickets by
* default don't release the reservation. They just commit the current
* transaction with the belief that the reservation is still needed. A flag
* must be passed in before permanent reservations are actually released.
* When these type of tickets are not released, they need to be set into
* the inited state again. By doing this, a start record will be written
* out when the next write occurs.
*/
xfs_lsn_t
xfs_log_done(xfs_mount_t *mp,
xfs_log_ticket_t xtic,
void **iclog,
uint flags)
{
xlog_t *log = mp->m_log;
xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
xfs_lsn_t lsn = 0;
if (XLOG_FORCED_SHUTDOWN(log) ||
/*
* If nothing was ever written, don't write out commit record.
* If we get an error, just continue and give back the log ticket.
*/
(((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
(xlog_commit_record(mp, ticket,
(xlog_in_core_t **)iclog, &lsn)))) {
lsn = (xfs_lsn_t) -1;
if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
flags |= XFS_LOG_REL_PERM_RESERV;
}
}
if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
(flags & XFS_LOG_REL_PERM_RESERV)) {
/*
* Release ticket if not permanent reservation or a specific
* request has been made to release a permanent reservation.
*/
xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
xlog_ungrant_log_space(log, ticket);
xlog_state_put_ticket(log, ticket);
} else {
xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
xlog_regrant_reserve_log_space(log, ticket);
}
/* If this ticket was a permanent reservation and we aren't
* trying to release it, reset the inited flags; so next time
* we write, a start record will be written out.
*/
if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
(flags & XFS_LOG_REL_PERM_RESERV) == 0)
ticket->t_flags |= XLOG_TIC_INITED;
return lsn;
} /* xfs_log_done */
/*
* Force the in-core log to disk. If flags == XFS_LOG_SYNC,
* the force is done synchronously.
*
* Asynchronous forces are implemented by setting the WANT_SYNC
* bit in the appropriate in-core log and then returning.
*
* Synchronous forces are implemented with a semaphore. All callers
* to force a given lsn to disk will wait on a semaphore attached to the
* specific in-core log. When given in-core log finally completes its
* write to disk, that thread will wake up all threads waiting on the
* semaphore.
*/
int
_xfs_log_force(
xfs_mount_t *mp,
xfs_lsn_t lsn,
uint flags,
int *log_flushed)
{
xlog_t *log = mp->m_log;
int dummy;
if (!log_flushed)
log_flushed = &dummy;
ASSERT(flags & XFS_LOG_FORCE);
XFS_STATS_INC(xs_log_force);
if (log->l_flags & XLOG_IO_ERROR)
return XFS_ERROR(EIO);
if (lsn == 0)
return xlog_state_sync_all(log, flags, log_flushed);
else
return xlog_state_sync(log, lsn, flags, log_flushed);
} /* xfs_log_force */
/*
* Attaches a new iclog I/O completion callback routine during
* transaction commit. If the log is in error state, a non-zero
* return code is handed back and the caller is responsible for
* executing the callback at an appropriate time.
*/
int
xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
void *iclog_hndl, /* iclog to hang callback off */
xfs_log_callback_t *cb)
{
xlog_t *log = mp->m_log;
xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
int abortflg, spl;
cb->cb_next = NULL;
spl = LOG_LOCK(log);
abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
if (!abortflg) {
ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
(iclog->ic_state == XLOG_STATE_WANT_SYNC));
cb->cb_next = NULL;
*(iclog->ic_callback_tail) = cb;
iclog->ic_callback_tail = &(cb->cb_next);
}
LOG_UNLOCK(log, spl);
return abortflg;
} /* xfs_log_notify */
int
xfs_log_release_iclog(xfs_mount_t *mp,
void *iclog_hndl)
{
xlog_t *log = mp->m_log;
xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
if (xlog_state_release_iclog(log, iclog)) {
xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
return EIO;
}
return 0;
}
/*
* 1. Reserve an amount of on-disk log space and return a ticket corresponding
* to the reservation.
* 2. Potentially, push buffers at tail of log to disk.
*
* Each reservation is going to reserve extra space for a log record header.
* When writes happen to the on-disk log, we don't subtract the length of the
* log record header from any reservation. By wasting space in each
* reservation, we prevent over allocation problems.
*/
int
xfs_log_reserve(xfs_mount_t *mp,
int unit_bytes,
int cnt,
xfs_log_ticket_t *ticket,
__uint8_t client,
uint flags,
uint t_type)
{
xlog_t *log = mp->m_log;
xlog_ticket_t *internal_ticket;
int retval = 0;
ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
if (XLOG_FORCED_SHUTDOWN(log))
return XFS_ERROR(EIO);
XFS_STATS_INC(xs_try_logspace);
if (*ticket != NULL) {
ASSERT(flags & XFS_LOG_PERM_RESERV);
internal_ticket = (xlog_ticket_t *)*ticket;
xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
retval = xlog_regrant_write_log_space(log, internal_ticket);
} else {
/* may sleep if need to allocate more tickets */
internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
client, flags);
internal_ticket->t_trans_type = t_type;
*ticket = internal_ticket;
xlog_trace_loggrant(log, internal_ticket,
(internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
"xfs_log_reserve: create new ticket (permanent trans)" :
"xfs_log_reserve: create new ticket");
xlog_grant_push_ail(mp,
(internal_ticket->t_unit_res *
internal_ticket->t_cnt));
retval = xlog_grant_log_space(log, internal_ticket);
}
return retval;
} /* xfs_log_reserve */
/*
* Mount a log filesystem
*
* mp - ubiquitous xfs mount point structure
* log_target - buftarg of on-disk log device
* blk_offset - Start block # where block size is 512 bytes (BBSIZE)
* num_bblocks - Number of BBSIZE blocks in on-disk log
*
* Return error or zero.
*/
int
xfs_log_mount(xfs_mount_t *mp,
xfs_buftarg_t *log_target,
xfs_daddr_t blk_offset,
int num_bblks)
{
if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
else {
cmn_err(CE_NOTE,
"!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
mp->m_fsname);
ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
}
mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
/*
* skip log recovery on a norecovery mount. pretend it all
* just worked.
*/
if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
bhv_vfs_t *vfsp = XFS_MTOVFS(mp);
int error, readonly = (vfsp->vfs_flag & VFS_RDONLY);
if (readonly)
vfsp->vfs_flag &= ~VFS_RDONLY;
error = xlog_recover(mp->m_log);
if (readonly)
vfsp->vfs_flag |= VFS_RDONLY;
if (error) {
cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
xlog_dealloc_log(mp->m_log);
return error;
}
}
/* Normal transactions can now occur */
mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
/* End mounting message in xfs_log_mount_finish */
return 0;
} /* xfs_log_mount */
/*
* Finish the recovery of the file system. This is separate from
* the xfs_log_mount() call, because it depends on the code in
* xfs_mountfs() to read in the root and real-time bitmap inodes
* between calling xfs_log_mount() and here.
*
* mp - ubiquitous xfs mount point structure
*/
int
xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
{
int error;
if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
error = xlog_recover_finish(mp->m_log, mfsi_flags);
else {
error = 0;
ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
}
return error;
}
/*
* Unmount processing for the log.
*/
int
xfs_log_unmount(xfs_mount_t *mp)
{
int error;
error = xfs_log_unmount_write(mp);
xfs_log_unmount_dealloc(mp);
return error;
}
/*
* Final log writes as part of unmount.
*
* Mark the filesystem clean as unmount happens. Note that during relocation
* this routine needs to be executed as part of source-bag while the
* deallocation must not be done until source-end.
*/
/*
* Unmount record used to have a string "Unmount filesystem--" in the
* data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
* We just write the magic number now since that particular field isn't
* currently architecture converted and "nUmount" is a bit foo.
* As far as I know, there weren't any dependencies on the old behaviour.
*/
int
xfs_log_unmount_write(xfs_mount_t *mp)
{
xlog_t *log = mp->m_log;
xlog_in_core_t *iclog;
#ifdef DEBUG
xlog_in_core_t *first_iclog;
#endif
xfs_log_iovec_t reg[1];
xfs_log_ticket_t tic = NULL;
xfs_lsn_t lsn;
int error;
SPLDECL(s);
/* the data section must be 32 bit size aligned */
struct {
__uint16_t magic;
__uint16_t pad1;
__uint32_t pad2; /* may as well make it 64 bits */
} magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
/*
* Don't write out unmount record on read-only mounts.
* Or, if we are doing a forced umount (typically because of IO errors).
*/
if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
return 0;
xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
#ifdef DEBUG
first_iclog = iclog = log->l_iclog;
do {
if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
ASSERT(iclog->ic_offset == 0);
}
iclog = iclog->ic_next;
} while (iclog != first_iclog);
#endif
if (! (XLOG_FORCED_SHUTDOWN(log))) {
reg[0].i_addr = (void*)&magic;
reg[0].i_len = sizeof(magic);
XLOG_VEC_SET_TYPE(®[0], XLOG_REG_TYPE_UNMOUNT);
error = xfs_log_reserve(mp, 600, 1, &tic,
XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
if (!error) {
/* remove inited flag */
((xlog_ticket_t *)tic)->t_flags = 0;
error = xlog_write(mp, reg, 1, tic, &lsn,
NULL, XLOG_UNMOUNT_TRANS);
/*
* At this point, we're umounting anyway,
* so there's no point in transitioning log state
* to IOERROR. Just continue...
*/
}
if (error) {
xfs_fs_cmn_err(CE_ALERT, mp,
"xfs_log_unmount: unmount record failed");
}
s = LOG_LOCK(log);
iclog = log->l_iclog;
iclog->ic_refcnt++;
LOG_UNLOCK(log, s);
xlog_state_want_sync(log, iclog);
(void) xlog_state_release_iclog(log, iclog);
s = LOG_LOCK(log);
if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
iclog->ic_state == XLOG_STATE_DIRTY)) {
if (!XLOG_FORCED_SHUTDOWN(log)) {
sv_wait(&iclog->ic_forcesema, PMEM,
&log->l_icloglock, s);
} else {
LOG_UNLOCK(log, s);
}
} else {
LOG_UNLOCK(log, s);
}
if (tic) {
xlog_trace_loggrant(log, tic, "unmount rec");
xlog_ungrant_log_space(log, tic);
xlog_state_put_ticket(log, tic);
}
} else {
/*
* We're already in forced_shutdown mode, couldn't
* even attempt to write out the unmount transaction.
*
* Go through the motions of sync'ing and releasing
* the iclog, even though no I/O will actually happen,
* we need to wait for other log I/Os that may already
* be in progress. Do this as a separate section of
* code so we'll know if we ever get stuck here that
* we're in this odd situation of trying to unmount
* a file system that went into forced_shutdown as
* the result of an unmount..
*/
s = LOG_LOCK(log);
iclog = log->l_iclog;
iclog->ic_refcnt++;
LOG_UNLOCK(log, s);
xlog_state_want_sync(log, iclog);
(void) xlog_state_release_iclog(log, iclog);
s = LOG_LOCK(log);
if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
|| iclog->ic_state == XLOG_STATE_DIRTY
|| iclog->ic_state == XLOG_STATE_IOERROR) ) {
sv_wait(&iclog->ic_forcesema, PMEM,
&log->l_icloglock, s);
} else {
LOG_UNLOCK(log, s);
}
}
return 0;
} /* xfs_log_unmount_write */
/*
* Deallocate log structures for unmount/relocation.
*/
void
xfs_log_unmount_dealloc(xfs_mount_t *mp)
{
xlog_dealloc_log(mp->m_log);
}
/*
* Write region vectors to log. The write happens using the space reservation
* of the ticket (tic). It is not a requirement that all writes for a given
* transaction occur with one call to xfs_log_write().
*/
int
xfs_log_write(xfs_mount_t * mp,
xfs_log_iovec_t reg[],
int nentries,
xfs_log_ticket_t tic,
xfs_lsn_t *start_lsn)
{
int error;
xlog_t *log = mp->m_log;
if (XLOG_FORCED_SHUTDOWN(log))
return XFS_ERROR(EIO);
if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
}
return error;
} /* xfs_log_write */
void
xfs_log_move_tail(xfs_mount_t *mp,
xfs_lsn_t tail_lsn)
{
xlog_ticket_t *tic;
xlog_t *log = mp->m_log;
int need_bytes, free_bytes, cycle, bytes;
SPLDECL(s);
if (XLOG_FORCED_SHUTDOWN(log))
return;
ASSERT(!XFS_FORCED_SHUTDOWN(mp));
if (tail_lsn == 0) {
/* needed since sync_lsn is 64 bits */
s = LOG_LOCK(log);
tail_lsn = log->l_last_sync_lsn;
LOG_UNLOCK(log, s);
}
s = GRANT_LOCK(log);
/* Also an invalid lsn. 1 implies that we aren't passing in a valid
* tail_lsn.
*/
if (tail_lsn != 1) {
log->l_tail_lsn = tail_lsn;
}
if ((tic = log->l_write_headq)) {
#ifdef DEBUG
if (log->l_flags & XLOG_ACTIVE_RECOVERY)
panic("Recovery problem");
#endif
cycle = log->l_grant_write_cycle;
bytes = log->l_grant_write_bytes;
free_bytes = xlog_space_left(log, cycle, bytes);
do {
ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
if (free_bytes < tic->t_unit_res && tail_lsn != 1)
break;
tail_lsn = 0;
free_bytes -= tic->t_unit_res;
sv_signal(&tic->t_sema);
tic = tic->t_next;
} while (tic != log->l_write_headq);
}
if ((tic = log->l_reserve_headq)) {
#ifdef DEBUG
if (log->l_flags & XLOG_ACTIVE_RECOVERY)
panic("Recovery problem");
#endif
cycle = log->l_grant_reserve_cycle;
bytes = log->l_grant_reserve_bytes;
free_bytes = xlog_space_left(log, cycle, bytes);
do {
if (tic->t_flags & XLOG_TIC_PERM_RESERV)
need_bytes = tic->t_unit_res*tic->t_cnt;
else
need_bytes = tic->t_unit_res;
if (free_bytes < need_bytes && tail_lsn != 1)
break;
tail_lsn = 0;
free_bytes -= need_bytes;
sv_signal(&tic->t_sema);
tic = tic->t_next;
} while (tic != log->l_reserve_headq);
}
GRANT_UNLOCK(log, s);
} /* xfs_log_move_tail */
/*
* Determine if we have a transaction that has gone to disk
* that needs to be covered. Log activity needs to be idle (no AIL and
* nothing in the iclogs). And, we need to be in the right state indicating
* something has gone out.
*/
int
xfs_log_need_covered(xfs_mount_t *mp)
{
SPLDECL(s);
int needed = 0, gen;
xlog_t *log = mp->m_log;
bhv_vfs_t *vfsp = XFS_MTOVFS(mp);
if (vfs_test_for_freeze(vfsp) || XFS_FORCED_SHUTDOWN(mp) ||
(vfsp->vfs_flag & VFS_RDONLY))
return 0;
s = LOG_LOCK(log);
if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
(log->l_covered_state == XLOG_STATE_COVER_NEED2))
&& !xfs_trans_first_ail(mp, &gen)
&& xlog_iclogs_empty(log)) {
if (log->l_covered_state == XLOG_STATE_COVER_NEED)
log->l_covered_state = XLOG_STATE_COVER_DONE;
else {
ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
log->l_covered_state = XLOG_STATE_COVER_DONE2;
}
needed = 1;
}
LOG_UNLOCK(log, s);
return needed;
}
/******************************************************************************
*
* local routines
*
******************************************************************************
*/
/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
* The log manager must keep track of the last LR which was committed
* to disk. The lsn of this LR will become the new tail_lsn whenever
* xfs_trans_tail_ail returns 0. If we don't do this, we run into
* the situation where stuff could be written into the log but nothing
* was ever in the AIL when asked. Eventually, we panic since the
* tail hits the head.
*
* We may be holding the log iclog lock upon entering this routine.
*/
xfs_lsn_t
xlog_assign_tail_lsn(xfs_mount_t *mp)
{
xfs_lsn_t tail_lsn;
SPLDECL(s);
xlog_t *log = mp->m_log;
tail_lsn = xfs_trans_tail_ail(mp);
s = GRANT_LOCK(log);
if (tail_lsn != 0) {
log->l_tail_lsn = tail_lsn;
} else {
tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
}
GRANT_UNLOCK(log, s);
return tail_lsn;
} /* xlog_assign_tail_lsn */
/*
* Return the space in the log between the tail and the head. The head
* is passed in the cycle/bytes formal parms. In the special case where
* the reserve head has wrapped passed the tail, this calculation is no
* longer valid. In this case, just return 0 which means there is no space
* in the log. This works for all places where this function is called
* with the reserve head. Of course, if the write head were to ever
* wrap the tail, we should blow up. Rather than catch this case here,
* we depend on other ASSERTions in other parts of the code. XXXmiken
*
* This code also handles the case where the reservation head is behind
* the tail. The details of this case are described below, but the end
* result is that we return the size of the log as the amount of space left.
*/
int
xlog_space_left(xlog_t *log, int cycle, int bytes)
{
int free_bytes;
int tail_bytes;
int tail_cycle;
tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
tail_cycle = CYCLE_LSN(log->l_tail_lsn);
if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
free_bytes = log->l_logsize - (bytes - tail_bytes);
} else if ((tail_cycle + 1) < cycle) {
return 0;
} else if (tail_cycle < cycle) {
ASSERT(tail_cycle == (cycle - 1));
free_bytes = tail_bytes - bytes;
} else {
/*
* The reservation head is behind the tail.
* In this case we just want to return the size of the
* log as the amount of space left.
*/
xfs_fs_cmn_err(CE_ALERT, log->l_mp,
"xlog_space_left: head behind tail\n"
" tail_cycle = %d, tail_bytes = %d\n"
" GH cycle = %d, GH bytes = %d",
tail_cycle, tail_bytes, cycle, bytes);
ASSERT(0);
free_bytes = log->l_logsize;
}
return free_bytes;
} /* xlog_space_left */
/*
* Log function which is called when an io completes.
*
* The log manager needs its own routine, in order to control what
* happens with the buffer after the write completes.
*/
void
xlog_iodone(xfs_buf_t *bp)
{
xlog_in_core_t *iclog;
xlog_t *l;
int aborted;
iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
aborted = 0;
/*
* Some versions of cpp barf on the recursive definition of
* ic_log -> hic_fields.ic_log and expand ic_log twice when
* it is passed through two macros. Workaround broken cpp.
*/
l = iclog->ic_log;
/*
* Race to shutdown the filesystem if we see an error.
*/
if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
XFS_BUF_STALE(bp);
xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
/*
* This flag will be propagated to the trans-committed
* callback routines to let them know that the log-commit
* didn't succeed.
*/
aborted = XFS_LI_ABORTED;
} else if (iclog->ic_state & XLOG_STATE_IOERROR) {
aborted = XFS_LI_ABORTED;
}
xlog_state_done_syncing(iclog, aborted);
if (!(XFS_BUF_ISASYNC(bp))) {
/*
* Corresponding psema() will be done in bwrite(). If we don't
* vsema() here, panic.
*/
XFS_BUF_V_IODONESEMA(bp);
}
} /* xlog_iodone */
/*
* The bdstrat callback function for log bufs. This gives us a central
* place to trap bufs in case we get hit by a log I/O error and need to
* shutdown. Actually, in practice, even when we didn't get a log error,
* we transition the iclogs to IOERROR state *after* flushing all existing
* iclogs to disk. This is because we don't want anymore new transactions to be
* started or completed afterwards.
*/
STATIC int
xlog_bdstrat_cb(struct xfs_buf *bp)
{
xlog_in_core_t *iclog;
iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
/* note for irix bstrat will need struct bdevsw passed
* Fix the following macro if the code ever is merged
*/
XFS_bdstrat(bp);
return 0;