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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/vnode.h>
#include <sys/errno.h>
#include <sys/sysmacros.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/conf.h>
#include <sys/proc.h>
#include <sys/cmn_err.h>
#include <sys/fssnap_if.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_filio.h>
#include <sys/fs/ufs_log.h>
#include <sys/fs/ufs_bio.h>
#include <sys/inttypes.h>
#include <sys/callb.h>
/*
* Kernel threads for logging
* Currently only one for rolling the log (one per log).
*/
#define LUFS_DEFAULT_NUM_ROLL_BUFS 16
#define LUFS_DEFAULT_MIN_ROLL_BUFS 4
#define LUFS_DEFAULT_MAX_ROLL_BUFS 64
/*
* Macros
*/
#define logmap_need_roll(logmap) ((logmap)->mtm_nme > logmap_maxnme)
#define ldl_empty(ul) ((ul)->un_head_lof == (ul)->un_tail_lof)
/*
* Tunables
*/
uint32_t lufs_num_roll_bufs = LUFS_DEFAULT_NUM_ROLL_BUFS;
uint32_t lufs_min_roll_bufs = LUFS_DEFAULT_MIN_ROLL_BUFS;
uint32_t lufs_max_roll_bufs = LUFS_DEFAULT_MAX_ROLL_BUFS;
long logmap_maxnme = 1536;
int trans_roll_tics = 0;
uint64_t trans_roll_new_delta = 0;
uint64_t lrr_wait = 0;
/*
* Key for thread specific data for the roll thread to
* bypass snapshot throttling
*/
uint_t bypass_snapshot_throttle_key;
/*
* externs
*/
extern kmutex_t ml_scan;
extern kcondvar_t ml_scan_cv;
extern int maxphys;
static void
trans_roll_wait(mt_map_t *logmap, callb_cpr_t *cprinfop)
{
mutex_enter(&logmap->mtm_mutex);
logmap->mtm_ref = 0;
if (logmap->mtm_flags & MTM_FORCE_ROLL) {
cv_broadcast(&logmap->mtm_from_roll_cv);
}
logmap->mtm_flags &= ~(MTM_FORCE_ROLL | MTM_ROLLING);
CALLB_CPR_SAFE_BEGIN(cprinfop);
(void) cv_reltimedwait(&logmap->mtm_to_roll_cv, &logmap->mtm_mutex,
trans_roll_tics, TR_CLOCK_TICK);
CALLB_CPR_SAFE_END(cprinfop, &logmap->mtm_mutex);
logmap->mtm_flags |= MTM_ROLLING;
mutex_exit(&logmap->mtm_mutex);
}
/*
* returns the number of 8K buffers to use for rolling the log
*/
static uint32_t
log_roll_buffers()
{
/*
* sanity validate the tunable lufs_num_roll_bufs
*/
if (lufs_num_roll_bufs < lufs_min_roll_bufs) {
return (lufs_min_roll_bufs);
}
if (lufs_num_roll_bufs > lufs_max_roll_bufs) {
return (lufs_max_roll_bufs);
}
return (lufs_num_roll_bufs);
}
/*
* Find something to roll, then if we don't have cached roll buffers
* covering all the deltas in that MAPBLOCK then read the master
* and overlay the deltas.
* returns;
* 0 if sucessful
* 1 on finding nothing to roll
* 2 on error
*/
int
log_roll_read(ml_unit_t *ul, rollbuf_t *rbs, int nmblk, caddr_t roll_bufs,
int *retnbuf)
{
offset_t mof;
buf_t *bp;
rollbuf_t *rbp;
mt_map_t *logmap = ul->un_logmap;
daddr_t mblkno;
int i;
int error;
int nbuf;
/*
* Make sure there is really something to roll
*/
mof = 0;
if (!logmap_next_roll(logmap, &mof)) {
return (1);
}
/*
* build some master blocks + deltas to roll forward
*/
rw_enter(&logmap->mtm_rwlock, RW_READER);
nbuf = 0;
do {
mof = mof & (offset_t)MAPBLOCKMASK;
mblkno = lbtodb(mof);
/*
* Check for the case of a new delta to a set up buffer
*/
for (i = 0, rbp = rbs; i < nbuf; ++i, ++rbp) {
if (P2ALIGN(rbp->rb_bh.b_blkno,
MAPBLOCKSIZE / DEV_BSIZE) == mblkno) {
trans_roll_new_delta++;
/* Flush out the current set of buffers */
goto flush_bufs;
}
}
/*
* Work out what to roll next. If it isn't cached then read
* it asynchronously from the master.
*/
bp = &rbp->rb_bh;
bp->b_blkno = mblkno;
bp->b_flags = B_READ;
bp->b_un.b_addr = roll_bufs + (nbuf << MAPBLOCKSHIFT);
bp->b_bufsize = MAPBLOCKSIZE;
if (top_read_roll(rbp, ul)) {
/* logmap deltas were in use */
if (nbuf == 0) {
/*
* On first buffer wait for the logmap user
* to finish by grabbing the logmap lock
* exclusively rather than spinning
*/
rw_exit(&logmap->mtm_rwlock);
lrr_wait++;
rw_enter(&logmap->mtm_rwlock, RW_WRITER);
rw_exit(&logmap->mtm_rwlock);
return (1);
}
/* we have at least one buffer - flush it */
goto flush_bufs;
}
if ((bp->b_flags & B_INVAL) == 0) {
nbuf++;
}
mof += MAPBLOCKSIZE;
} while ((nbuf < nmblk) && logmap_next_roll(logmap, &mof));
/*
* If there was nothing to roll cycle back
*/
if (nbuf == 0) {
rw_exit(&logmap->mtm_rwlock);
return (1);
}
flush_bufs:
/*
* For each buffer, if it isn't cached then wait for the read to
* finish and overlay the deltas.
*/
for (error = 0, i = 0, rbp = rbs; i < nbuf; ++i, ++rbp) {
if (!rbp->rb_crb) {
bp = &rbp->rb_bh;
if (trans_not_wait(bp)) {
ldl_seterror(ul,
"Error reading master during ufs log roll");
error = 1;
}
/*
* sync read the data from the log
*/
if (ldl_read(ul, bp->b_un.b_addr,
ldbtob(bp->b_blkno) & (offset_t)MAPBLOCKMASK,
MAPBLOCKSIZE, rbp->rb_age)) {
error = 1;
}
}
/*
* reset the age bit in the age list
*/
logmap_list_put_roll(logmap, rbp->rb_age);
if (ul->un_flags & LDL_ERROR) {
error = 1;
}
}
rw_exit(&logmap->mtm_rwlock);
if (error)
return (2);
*retnbuf = nbuf;
return (0);
}
/*
* Write out a cached roll buffer
*/
void
log_roll_write_crb(ufsvfs_t *ufsvfsp, rollbuf_t *rbp)
{
crb_t *crb = rbp->rb_crb;
buf_t *bp = &rbp->rb_bh;
bp->b_blkno = lbtodb(crb->c_mof);
bp->b_un.b_addr = crb->c_buf;
bp->b_bcount = crb->c_nb;
bp->b_bufsize = crb->c_nb;
ASSERT((crb->c_nb & DEV_BMASK) == 0);
bp->b_flags = B_WRITE;
logstats.ls_rwrites.value.ui64++;
/* if snapshots are enabled, call it */
if (ufsvfsp->vfs_snapshot) {
fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
} else {
(void) bdev_strategy(bp);
}
}
/*
* Write out a set of non cached roll buffers
*/
void
log_roll_write_bufs(ufsvfs_t *ufsvfsp, rollbuf_t *rbp)
{
buf_t *bp = &rbp->rb_bh;
buf_t *bp2;
rbsecmap_t secmap = rbp->rb_secmap;
int j, k;
ASSERT(secmap);
ASSERT((bp->b_flags & B_INVAL) == 0);
do { /* for each contiguous block of sectors */
/* find start of next sector to write */
for (j = 0; j < 16; ++j) {
if (secmap & UINT16_C(1))
break;
secmap >>= 1;
}
bp->b_un.b_addr += (j << DEV_BSHIFT);
bp->b_blkno += j;
/* calculate number of sectors */
secmap >>= 1;
j++;
for (k = 1; j < 16; ++j) {
if ((secmap & UINT16_C(1)) == 0)
break;
secmap >>= 1;
k++;
}
bp->b_bcount = k << DEV_BSHIFT;
bp->b_flags = B_WRITE;
logstats.ls_rwrites.value.ui64++;
/* if snapshots are enabled, call it */
if (ufsvfsp->vfs_snapshot)
fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
else
(void) bdev_strategy(bp);
if (secmap) {
/*
* Allocate another buf_t to handle
* the next write in this MAPBLOCK
* Chain them via b_list.
*/
bp2 = kmem_alloc(sizeof (buf_t), KM_SLEEP);
bp->b_list = bp2;
bioinit(bp2);
bp2->b_iodone = trans_not_done;
bp2->b_bufsize = MAPBLOCKSIZE;
bp2->b_edev = bp->b_edev;
bp2->b_un.b_addr =
bp->b_un.b_addr + bp->b_bcount;
bp2->b_blkno = bp->b_blkno + k;
bp = bp2;
}
} while (secmap);
}
/*
* Asynchronously roll the deltas, using the sector map
* in each rollbuf_t.
*/
int
log_roll_write(ml_unit_t *ul, rollbuf_t *rbs, int nbuf)
{
ufsvfs_t *ufsvfsp = ul->un_ufsvfs;
rollbuf_t *rbp;
buf_t *bp, *bp2;
rollbuf_t *head, *prev, *rbp2;
/*
* Order the buffers by blkno
*/
ASSERT(nbuf > 0);
#ifdef lint
prev = rbs;
#endif
for (head = rbs, rbp = rbs + 1; rbp < rbs + nbuf; rbp++) {
for (rbp2 = head; rbp2; prev = rbp2, rbp2 = rbp2->rb_next) {
if (rbp->rb_bh.b_blkno < rbp2->rb_bh.b_blkno) {
if (rbp2 == head) {
rbp->rb_next = head;
head = rbp;
} else {
prev->rb_next = rbp;
rbp->rb_next = rbp2;
}
break;
}
}
if (rbp2 == NULL) {
prev->rb_next = rbp;
rbp->rb_next = NULL;
}
}
/*
* issue the in-order writes
*/
for (rbp = head; rbp; rbp = rbp2) {
if (rbp->rb_crb) {
log_roll_write_crb(ufsvfsp, rbp);
} else {
log_roll_write_bufs(ufsvfsp, rbp);
}
/* null out the rb_next link for next set of rolling */
rbp2 = rbp->rb_next;
rbp->rb_next = NULL;
}
/*
* wait for all the writes to finish
*/
for (rbp = rbs; rbp < rbs + nbuf; rbp++) {
bp = &rbp->rb_bh;
if (trans_not_wait(bp)) {
ldl_seterror(ul,
"Error writing master during ufs log roll");
}
/*
* Now wait for all the "cloned" buffer writes (if any)
* and free those headers
*/
bp2 = bp->b_list;
bp->b_list = NULL;
while (bp2) {
if (trans_not_wait(bp2)) {
ldl_seterror(ul,
"Error writing master during ufs log roll");
}
bp = bp2;
bp2 = bp2->b_list;
kmem_free(bp, sizeof (buf_t));
}
}
if (ul->un_flags & LDL_ERROR)
return (1);
return (0);
}
void
trans_roll(ml_unit_t *ul)
{
callb_cpr_t cprinfo;
mt_map_t *logmap = ul->un_logmap;
rollbuf_t *rbs;
rollbuf_t *rbp;
buf_t *bp;
caddr_t roll_bufs;
uint32_t nmblk;
int i;
int doingforceroll;
int nbuf;
CALLB_CPR_INIT(&cprinfo, &logmap->mtm_mutex, callb_generic_cpr,
"trans_roll");
/*
* We do not want the roll thread's writes to be
* throttled by the snapshot.
* If they are throttled then we can have a deadlock
* between the roll thread and the snapshot taskq thread:
* roll thread wants the throttling semaphore and
* the snapshot taskq thread cannot release the semaphore
* because it is writing to the log and the log is full.
*/
(void) tsd_set(bypass_snapshot_throttle_key, (void*)1);
/*
* setup some roll parameters
*/
if (trans_roll_tics == 0)
trans_roll_tics = 5 * hz;
nmblk = log_roll_buffers();
/*
* allocate the buffers and buffer headers
*/
roll_bufs = kmem_alloc(nmblk * MAPBLOCKSIZE, KM_SLEEP);
rbs = kmem_alloc(nmblk * sizeof (rollbuf_t), KM_SLEEP);
/*
* initialize the buffer headers
*/
for (i = 0, rbp = rbs; i < nmblk; ++i, ++rbp) {
rbp->rb_next = NULL;
bp = &rbp->rb_bh;
bioinit(bp);
bp->b_edev = ul->un_dev;
bp->b_iodone = trans_not_done;
bp->b_bufsize = MAPBLOCKSIZE;
}
doingforceroll = 0;
again:
/*
* LOOP FOREVER
*/
/*
* exit on demand
*/
mutex_enter(&logmap->mtm_mutex);
if ((ul->un_flags & LDL_ERROR) || (logmap->mtm_flags & MTM_ROLL_EXIT)) {
kmem_free(rbs, nmblk * sizeof (rollbuf_t));
kmem_free(roll_bufs, nmblk * MAPBLOCKSIZE);
logmap->mtm_flags &= ~(MTM_FORCE_ROLL | MTM_ROLL_RUNNING |
MTM_ROLL_EXIT | MTM_ROLLING);
cv_broadcast(&logmap->mtm_from_roll_cv);
CALLB_CPR_EXIT(&cprinfo);
thread_exit();
/* NOTREACHED */
}
/*
* MT_SCAN debug mode
* don't roll except in FORCEROLL situations
*/
if (logmap->mtm_debug & MT_SCAN)
if ((logmap->mtm_flags & MTM_FORCE_ROLL) == 0) {
mutex_exit(&logmap->mtm_mutex);
trans_roll_wait(logmap, &cprinfo);
goto again;
}
ASSERT(logmap->mtm_trimlof == 0);
/*
* If we've finished a force roll cycle then wakeup any
* waiters.
*/
if (doingforceroll) {
doingforceroll = 0;
logmap->mtm_flags &= ~MTM_FORCE_ROLL;
mutex_exit(&logmap->mtm_mutex);
cv_broadcast(&logmap->mtm_from_roll_cv);
} else {
mutex_exit(&logmap->mtm_mutex);
}
/*
* If someone wants us to roll something; then do it
*/
if (logmap->mtm_flags & MTM_FORCE_ROLL) {
doingforceroll = 1;
goto rollsomething;
}
/*
* Log is busy, check if logmap is getting full.
*/
if (logmap_need_roll(logmap)) {
goto rollsomething;
}
/*
* Check if the log is idle and is not empty
*/
if (!logmap->mtm_ref && !ldl_empty(ul)) {
goto rollsomething;
}
/*
* Log is busy, check if its getting full
*/
if (ldl_need_roll(ul)) {
goto rollsomething;
}
/*
* nothing to do; wait a bit and then start over
*/
trans_roll_wait(logmap, &cprinfo);
goto again;
/*
* ROLL SOMETHING
*/
rollsomething:
/*
* Use the cached roll buffers, or read the master
* and overlay the deltas
*/
switch (log_roll_read(ul, rbs, nmblk, roll_bufs, &nbuf)) {
case 1: trans_roll_wait(logmap, &cprinfo);
/* FALLTHROUGH */
case 2: goto again;
/* default case is success */
}
/*
* Asynchronously write out the deltas
*/
if (log_roll_write(ul, rbs, nbuf))
goto again;
/*
* free up the deltas in the logmap
*/
for (i = 0, rbp = rbs; i < nbuf; ++i, ++rbp) {
bp = &rbp->rb_bh;
logmap_remove_roll(logmap,
ldbtob(bp->b_blkno) & (offset_t)MAPBLOCKMASK, MAPBLOCKSIZE);
}
/*
* free up log space; if possible
*/
logmap_sethead(logmap, ul);
/*
* LOOP
*/
goto again;
}
|