-
Notifications
You must be signed in to change notification settings - Fork 6
/
nilfs-resize.c
1648 lines (1469 loc) · 45.1 KB
/
nilfs-resize.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
/*
* nilfs-resize.c - resize nilfs2 volume
*
* Licensed under GPLv2: the complete text of the GNU General Public License
* can be found in COPYING file of the nilfs-utils package.
*
* Copyright (C) 2011-2012 Nippon Telegraph and Telephone Corporation.
* Written by Ryusuke Konishi <konishi.ryusuke@gmail.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#if HAVE_LINUX_TYPES_H
#include <linux/types.h>
#endif /* HAVE_LINUX_TYPES_H */
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif /* HAVE_FCNTL_H */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#if HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif /* HAVE_SYS_IOCTL_H */
#if HAVE_LIMITS_H
#include <limits.h>
#endif /* HAVE_LIMITS_H */
#if HAVE_STRING_H
#include <string.h>
#endif /* HAVE_SYS_STRING_H */
#if HAVE_SYSLOG_H
#include <syslog.h> /* LOG_ERR, and so forth */
#endif /* HAVE_SYSLOG_H */
#if HAVE_TIME_H
#include <time.h> /* timespec, nanosleep() */
#endif /* HAVE_TIME_H */
#include <sys/stat.h>
#include <assert.h>
#include <stdarg.h> /* va_start, va_end, vfprintf */
#include <errno.h>
#include <signal.h>
#include "nilfs.h"
#include "compat.h"
#include "util.h"
#include "nilfs_gc.h"
extern int check_mount(const char *device);
#ifdef _GNU_SOURCE
#include <getopt.h>
static const struct option long_option[] = {
{"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'},
{"yes", no_argument, NULL, 'y'},
{"assume-yes", no_argument, NULL, 'y'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
};
#define NILFS_RESIZE_USAGE \
"Usage: %s [options] device [size]\n" \
" -h, --help\t\tdisplay this help and exit\n" \
" -v, --verbose\t\tverbose mode\n" \
" -y, --yes,--assume-yes\n" \
" \t\tAssume Yes to all queries and do not prompt\n" \
" -V, --version\t\tdisplay version and exit\n"
#else
#define NILFS_RESIZE_USAGE \
"Usage: %s [-h] [-v] [-y] [-V] device [size]\n"
#endif /* _GNU_SOURCE */
/* get device size through ioctl */
#ifndef BLKGETSIZE64
#define BLKGETSIZE64 _IOR(0x12, 114, size_t)
#endif
/* options */
static char *progname;
static int show_version_only;
static int verbose;
static int assume_yes;
static int show_progress = 1;
/* global variables */
static __u64 devsize;
static unsigned int sector_size = 512;
static struct nilfs_sustat sustat;
static __u64 trunc_start, trunc_end;
#define NILFS_RESIZE_NSUINFO 256
#define NILFS_RESIZE_NSEGNUMS 256
static struct nilfs_suinfo suinfo[NILFS_RESIZE_NSUINFO];
static __u64 segnums[NILFS_RESIZE_NSEGNUMS];
/* filesystem parameters */
static __u32 blocksize;
static __u32 blocksize_bits;
static __u64 fs_devsize;
static __u32 blocks_per_segment;
static __u32 rsvsegs_percentage; /* reserved segment percentage */
static __u64 first_data_block;
static __u64 free_blocks_count;
/* cleaner parameter for shrink */
static int nsegments_per_clean = 2;
static struct timespec clean_interval = { 0, 100000000 }; /* 100 msec */
/* progress meter */
static int pm_width = 60;
static int pm_barwidth;
static int pm_labelwidth = 10;
static __u64 pm_max;
static __u64 pm_done;
static int pm_curpos;
static int pm_in_progress; /* 0: off, 1: on, -1: interrupted */
static const char *pm_label;
/**
* nilfs_resize_logger - logger function to pass to libraries used
* @priority: log level
* @fmt: format string
* @...: variable arguments that provide values for the conversion
* descriptors in the format string
*
* This function provides a message output function specific to the resize
* command to relay and display message output from libraries.
*
* Here, messages with @priority greater than or equal to %LOG_ERR are
* redirected to standard error and a newline is inserted at the end of the
* message.
*/
static void nilfs_resize_logger(int priority, const char *fmt, ...)
{
va_list args;
if (priority > LOG_ERR)
return;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
/**
* nilfs_resize_progress_show - show progress bar on standard error output
*
* This function displays a progress bar on the standard error output showing
* the current progress and flushes the file descriptor.
*/
static void nilfs_resize_progress_show(void)
{
int i, len = strlen(pm_label);
int delta;
fwrite(pm_label, sizeof(char), min_t(int, len, pm_labelwidth), stderr);
fputs(" |", stderr);
for (i = 0; i < pm_curpos; i++)
fputc('*', stderr);
for (; i < pm_barwidth; i++)
fputc('-', stderr);
fputc('|', stderr);
delta = pm_barwidth + 1 - pm_curpos;
for (i = 0; i < delta; i++)
fputc('\b', stderr);
fflush(stderr);
}
/**
* nilfs_resize_progress_init - initialize the progress bar and display the
* initial bar
* @maxval: maximum progress value
* @label: label name displayed to the left of the progress bar
*
* This function initializes each variable representing the internal state
* of the progress bar, displays the progress bar in its initial state with
* a progress value of 0, and sets &pm_in_progress to one indicating that
* the progress bar is being displayed.
*/
static void nilfs_resize_progress_init(__u64 maxval, const char *label)
{
pm_max = maxval;
pm_done = 0;
pm_curpos = 0;
pm_barwidth = max_t(int, pm_width - pm_labelwidth - 3, 10);
pm_label = label;
nilfs_resize_progress_show();
pm_in_progress = 1;
}
/**
* nilfs_resize_progress_update - update progress bar display
* @done: progress value
*
* This function does nothing if &pm_in_progress is off value (0).
* Otherwise, updates the progress bar with the progress value given by
* @done and sets the progress value in &pm_done and the updated cursor
* position in &pm_curpos.
* If the progress bar display is interrupted by other messages
* (&pm_in_progress < 0), it calls nilfs_resize_progress_show() to redisplay
* the entire progress bar. Changing the cursor position is done locally
* using combination of character output and the backspace character code.
*/
static void nilfs_resize_progress_update(__u64 done)
{
int pos, delta;
int i;
if (!pm_in_progress)
return;
if (pm_in_progress < 0) {
/* resuming */
nilfs_resize_progress_show();
pm_in_progress = 1;
}
pos = pm_barwidth * min_t(__u64, done, pm_max) / pm_max;
if (pos > pm_curpos) {
delta = pos - pm_curpos;
for (i = 0; i < delta; i++)
fputc('*', stderr);
} else if (pos < pm_curpos) {
delta = pm_curpos - pos;
for (i = 0; i < delta; i++)
fputc('\b', stderr);
for (i = 0; i < delta; i++)
fputc('-', stderr);
for (i = 0; i < delta; i++)
fputc('\b', stderr);
}
fflush(stderr);
pm_curpos = pos;
pm_done = done;
}
/**
* nilfs_resize_progress_inc - increase/decrease progress
* @n: difference in progress (positive values are incremented, negative
* values are decremented)
*
* This function increases the progress by @n (decreases it if it is a
* negative value) and updates the progress bar display with
* nilfs_resize_progress_update().
*/
static void nilfs_resize_progress_inc(long n)
{
nilfs_resize_progress_update(pm_done + n);
}
/**
* nilfs_resize_progress_exit - stop updating progress bar
*
* This function does nothing if the progress bar is not in display mode.
* If not, it sets &pm_in_progress to display disabled state (0).
* If the progress bar is being updated and is not interrupted by other
* messages, it outputs a newline code to the standard error output to
* advance the line.
*/
static void nilfs_resize_progress_exit(void)
{
if (!pm_in_progress)
return;
if (pm_in_progress > 0)
fputc('\n', stderr);
pm_in_progress = 0;
}
/**
* myprintf - dedicated message output function (for coexistence with
* progress bar)
* @fmt: format string
* @...: variable arguments that provide values for the conversion
* descriptors in the format string
*
* This function is used to print a message concurrently with the progress
* bar. The message is relayed to standard error.
* If the progress bar is being updated, it outputs a new line code to
* change the line, and sets the progress bar status to "interrupted"
* (&pm_in_progress = -1).
*/
static void myprintf(const char *fmt, ...)
{
va_list args;
if (pm_in_progress > 0) {
fputc('\n', stderr);
pm_in_progress = -1;
}
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
static void nilfs_resize_update_super(const struct nilfs_super_block *sb)
{
blocksize_bits = le32_to_cpu(sb->s_log_block_size) + 10;
blocksize = 1UL << blocksize_bits;
blocks_per_segment = le32_to_cpu(sb->s_blocks_per_segment);
fs_devsize = le64_to_cpu(sb->s_dev_size);
rsvsegs_percentage = le32_to_cpu(sb->s_r_segments_percentage);
first_data_block = le64_to_cpu(sb->s_first_data_block);
free_blocks_count = le64_to_cpu(sb->s_free_blocks_count);
}
static int nilfs_resize_reload_super(struct nilfs *nilfs)
{
struct nilfs_super_block *sb;
sb = nilfs_sb_read(nilfs->n_devfd);
if (!sb) {
myprintf("Error: cannot read super block: %s\n",
strerror(errno));
return -1;
}
nilfs_resize_update_super(sb);
free(sb);
return 0;
}
/**
* nilfs_resize_update_sustat - update nilfs segment usage statistics
* @nilfs: nilfs object
*
* This is a wrapper function for nilfs_get_sustat() and stores segment
* usage statistics in the global variable &sustat. This uses myprintf()
* to output an error message when errors occur.
*
* Return: 0 on success, -1 on failure.
*/
static int nilfs_resize_update_sustat(struct nilfs *nilfs)
{
if (nilfs_get_sustat(nilfs, &sustat) < 0) {
myprintf("Error: failed to get segment usage status: %s\n",
strerror(errno));
return -1;
}
return 0;
}
static int nilfs_resize_segment_is_protected(struct nilfs *nilfs, __u64 segnum)
{
void *segment;
__u64 segseq;
__u64 protseq = sustat.ss_prot_seq;
int ret = 0;
/* need updating sustat before testing */
if (nilfs_get_segment(nilfs, segnum, &segment) < 0) {
myprintf("Error: cannot read segment: %s\n", strerror(errno));
return -1;
}
segseq = nilfs_get_segment_seqnum(nilfs, segment, segnum);
if (cnt64_ge(segseq, protseq))
ret = 1;
nilfs_put_segment(nilfs, segment);
return ret;
}
/**
* nilfs_resize_lock_cleaner - block signals and lock cleaner
* @nilfs: nilfs object
* @sigset: place to store the old signal set
*
* This is a wrapper function for sigprocmask() and nilfs_lock_cleaner(),
* and uses myprintf() to output an error message when errors occur.
* On success, %SIGINT and %SIGTERM are blocked, and the cleaner is locked.
*
* Return: 0 on success, -1 on failure.
*/
static int nilfs_resize_lock_cleaner(struct nilfs *nilfs, sigset_t *sigset)
{
sigset_t newset;
sigemptyset(&newset);
sigaddset(&newset, SIGINT);
sigaddset(&newset, SIGTERM);
if (sigprocmask(SIG_BLOCK, &newset, sigset) < 0) {
myprintf("Error: cannot block signals: %s\n", strerror(errno));
return -1;
}
if (nilfs_lock_cleaner(nilfs) < 0) {
myprintf("Error: failed to lock cleaner: %s\n",
strerror(errno));
sigprocmask(SIG_SETMASK, sigset, NULL);
return -1;
}
return 0;
}
/**
* nilfs_resize_unlock_cleaner - unlock cleaner and unblock signals
* @nilfs: nilfs object
* @sigset: place to store the original signal set
*/
static void nilfs_resize_unlock_cleaner(struct nilfs *nilfs,
const sigset_t *sigset)
{
nilfs_unlock_cleaner(nilfs);
sigprocmask(SIG_SETMASK, sigset, NULL);
}
/**
* nilfs_resize_calc_nrsvsegs - calculate the number of reserved segments
* @nsegs: number of segments
*/
static __u64 nilfs_resize_calc_nrsvsegs(__u64 nsegs)
{
return max_t(__u64, NILFS_MIN_NRSVSEGS,
DIV_ROUND_UP(nsegs * rsvsegs_percentage, 100));
}
/**
* nilfs_resize_check_free_space - check if free space remains after resizing
* @nilfs: nilfs object
* @newnsegs: number of segments
*
* This function checks whether there is free space for at least the number
* of reserved segments when resizing to the number of segments specified by
* @newnsegs, and outputs an error message if not.
*
* In verbose mode, this function outputs information about the remaining
* space if there is enough space.
*
* Return: 0 if there is enough space, -1 otherwise.
*/
static int nilfs_resize_check_free_space(struct nilfs *nilfs, __u64 newnsegs)
{
unsigned long long nrsvsegs, nsegs, nbytes;
nrsvsegs = nilfs_resize_calc_nrsvsegs(newnsegs);
if (sustat.ss_ncleansegs < sustat.ss_nsegs - newnsegs + nrsvsegs) {
unsigned long long nsegs, nbytes;
nsegs = (sustat.ss_nsegs - newnsegs) - sustat.ss_ncleansegs
+ nrsvsegs;
nbytes = (nsegs * blocks_per_segment) << blocksize_bits;
myprintf("Error: the filesystem does not have enough free space.\n"
" At least %llu more segments (%llu bytes) are required.\n",
nsegs, nbytes);
return -1;
} else if (verbose) {
nsegs = sustat.ss_ncleansegs - (sustat.ss_nsegs - newnsegs)
- nrsvsegs;
nbytes = (nsegs * blocks_per_segment) << blocksize_bits;
myprintf("%llu free segments (%llu bytes) will be left after shrinkage.\n",
nsegs, nbytes);
}
return 0;
}
/**
* nilfs_resize_restore_alloc_range - restore the allocatable range of segments
* @nilfs: nilfs object
*
* This function uses the saved original layout information to reset the
* range of allocatable segments back to before the start of shrinking.
*/
static void nilfs_resize_restore_alloc_range(struct nilfs *nilfs)
{
if (nilfs_resize_update_sustat(nilfs) < 0)
return;
nilfs_set_alloc_range(nilfs, 0, fs_devsize);
}
/**
* nilfs_resize_find_movable_segments - find movable segments within a
* specified range
* @nilfs: nilfs object
* @start: starting segment number of search range (inclusive)
* @end: ending segment number of search range (inclusive)
* @segnumv: array of 64-bit integers that stores the discovered segment
* numbers
* @maxsegnums: maximum number of segments to search (maximum number of
* segment numbers that can be stored in @segnumv)
*
* This function searches for segments that are reclaimable and not
* protected by superblock log pointers within the range of the segment
* sequence specified by [@start, @end], and stores their numbers in
* @segnumv.
*
* Return: on success, the number of movable segments discovered, -1 on
* error.
*/
static ssize_t
nilfs_resize_find_movable_segments(struct nilfs *nilfs, __u64 start,
__u64 end, __u64 *segnumv,
unsigned long maxsegnums)
{
__u64 segnum, *snp;
unsigned long rest, count;
ssize_t nsi, i;
int ret;
assert(start <= end);
segnum = start;
rest = min_t(__u64, maxsegnums, end - start + 1);
for (snp = segnumv; rest > 0 && segnum <= end; ) {
count = min_t(unsigned long, rest, NILFS_RESIZE_NSUINFO);
nsi = nilfs_get_suinfo(nilfs, segnum, suinfo, count);
if (nsi < 0) {
myprintf("Error: operation failed during searching movable segments: %s\n",
strerror(errno));
return -1;
}
for (i = 0; i < nsi; i++, segnum++) {
if (!nilfs_suinfo_reclaimable(&suinfo[i]))
continue;
if (nilfs_suinfo_empty(&suinfo[i])) {
/* Scrapped segments can be removed */
*snp++ = segnum;
rest--;
continue;
}
ret = nilfs_resize_segment_is_protected(nilfs, segnum);
if (ret < 0)
return -1;
else if (ret)
continue;
*snp++ = segnum;
rest--;
}
}
return snp - segnumv; /* return the number of found segments */
}
#if 0
/**
* nilfs_resize_get_latest_segment - find latest segment within a specified
* range
* @nilfs: nilfs object
* @start: starting segment number of search range (inclusive)
* @end: ending segment number of search range (inclusive)
* @segnump place to store the number of the most recently updated
* segment
*
* This function searches for dirty (in-use) and non-error segments in
* the range specified by [@start, @end] of the segment sequence, and
* stores the number of the most recently updated segment in @segnump.
*
* Return: 1 on success, 0 if no segments were in use and not in error,
* -1 on error.
*/
static int
nilfs_resize_get_latest_segment(struct nilfs *nilfs, __u64 start, __u64 end,
__u64 *segnump)
{
__u64 segnum, latest_segnum = (~(__u64)0);
unsigned long count;
ssize_t nsi, i;
__u64 latest_time = 0;
int ret = 0;
assert(start <= end);
for (segnum = start; segnum <= end; segnum += nsi) {
count = min_t(unsigned long, end - segnum + 1,
NILFS_RESIZE_NSUINFO);
nsi = nilfs_get_suinfo(nilfs, segnum, suinfo, count);
if (nsi < 0) {
myprintf("Error: operation failed during searching latest segment: %s\n",
strerror(errno));
return -1;
}
assert(nsi > 0);
for (i = 0; i < nsi; i++) {
if (nilfs_suinfo_dirty(&suinfo[i]) &&
!nilfs_suinfo_error(&suinfo[i]) &&
suinfo[i].sui_lastmod > latest_time) {
latest_time = suinfo[i].sui_lastmod;
latest_segnum = segnum + i;
}
}
}
if (latest_time > 0) {
*segnump = latest_segnum;
ret = 1;
}
return ret;
}
#endif
/**
* nilfs_resize_find_active_segments - find active (grabbed by log writer)
* segments within a specified range
* @nilfs: nilfs object
* @start: starting segment number of search range (inclusive)
* @end: ending segment number of search range (inclusive)
* @segnumv: array of 64-bit integers that stores the discovered segment
* numbers
* @maxsegnums: maximum number of segments to search (maximum number of
* segment numbers that can be stored in @segnumv)
*
* This function searches for segments that are reclaimable and not
* protected by superblock log pointers within the range of the segment
* sequence specified by [@start, @end], and stores their numbers in
* @segnumv.
*
* Return: on success, the number of movable segments discovered, -1 on
* error.
*/
static ssize_t
nilfs_resize_find_active_segments(struct nilfs *nilfs, __u64 start, __u64 end,
__u64 *segnumv, unsigned long maxsegnums)
{
__u64 segnum, *snp;
unsigned long rest, count;
ssize_t nsi, i;
assert(start <= end);
segnum = start;
rest = min_t(__u64, maxsegnums, end - start + 1);
for (snp = segnumv; rest > 0 && segnum <= end; ) {
count = min_t(unsigned long, rest, NILFS_RESIZE_NSUINFO);
nsi = nilfs_get_suinfo(nilfs, segnum, suinfo, count);
if (nsi < 0) {
myprintf("Error: operation failed during searching active segments: %s\n",
strerror(errno));
return -1;
}
for (i = 0; i < nsi; i++, segnum++) {
if (nilfs_suinfo_active(&suinfo[i]) &&
!nilfs_suinfo_error(&suinfo[i])) {
*snp++ = segnum;
rest--;
}
}
}
return snp - segnumv; /* return the number of found segments */
}
/**
* nilfs_resize_find_inuse_segments - find reclaimable segments within a
* specified range
* @nilfs: nilfs object
* @start: starting segment number of search range (inclusive)
* @end: ending segment number of search range (inclusive)
* @segnumv: array of 64-bit integers that stores the discovered segment
* numbers
* @maxsegnums: maximum number of segments to search (maximum number of
* segment numbers that can be stored in @segnumv)
*
* This function searches for reclaimable (dirty, non-error, and non-active)
* segments within the range of the segment sequence specified by
* [@start, @end], and stores their numbers in @segnumv.
*
* Return: on success, the number of reclaimable segments discovered, -1 on
* error.
*/
static ssize_t
nilfs_resize_find_inuse_segments(struct nilfs *nilfs, __u64 start, __u64 end,
__u64 *segnumv, unsigned long maxsegnums)
{
__u64 segnum, *snp;
unsigned long rest, count;
ssize_t nsi, i;
assert(start <= end);
segnum = start;
rest = min_t(__u64, maxsegnums, end - start + 1);
for (snp = segnumv; rest > 0 && segnum <= end; ) {
count = min_t(unsigned long, rest, NILFS_RESIZE_NSUINFO);
nsi = nilfs_get_suinfo(nilfs, segnum, suinfo, count);
if (nsi < 0) {
myprintf("Error: operation failed during searching in-use segments: %s\n",
strerror(errno));
return -1;
}
for (i = 0; i < nsi; i++, segnum++) {
if (nilfs_suinfo_reclaimable(&suinfo[i])) {
*snp++ = segnum;
rest--;
}
}
}
return snp - segnumv; /* return the number of found segments */
}
/**
* nilfs_resize_count_inuse_segments - count the number of reclaimable
* segments within a specified range
* @nilfs: nilfs object
* @start: starting segment number of search range (inclusive)
* @end: ending segment number of search range (inclusive)
*
* This function counts and returns the number of reclaimable (dirty,
* non-error, and non-active) segments within the range of the segment
* sequence specified by [@start, @end].
*
* Return: number of reclaimable segments on success, -1 on error.
*/
static ssize_t
nilfs_resize_count_inuse_segments(struct nilfs *nilfs, __u64 start, __u64 end)
{
__u64 segnum;
unsigned long rest, count;
ssize_t nsi, i;
ssize_t nfound = 0;
assert(start <= end);
segnum = start;
rest = end - start + 1;
while (rest > 0 && segnum <= end) {
count = min_t(unsigned long, rest, NILFS_RESIZE_NSUINFO);
nsi = nilfs_get_suinfo(nilfs, segnum, suinfo, count);
if (nsi < 0) {
myprintf("Error: operation failed during counting in-use segments: %s\n",
strerror(errno));
return -1;
}
for (i = 0; i < nsi; i++, segnum++) {
if (nilfs_suinfo_reclaimable(&suinfo[i])) {
nfound++;
rest--;
}
}
}
return nfound; /* return the number of found segments */
}
#define NILFS_RESIZE_SEGMENT_PROTECTED 0x01
#define NILFS_RESIZE_SEGMENT_UNRECLAIMABLE 0x02
/**
* nilfs_resize_verify_failure - check the cause of movement failure for a
* segment number array
* @nilfs: nilfs object
* @segnumv: array of segment numbers
* @nsegs: number of segment numbers stored in @segnumv
*
* This function examines the usage of each segment specified by the segment
* number array @segnumv, determines why they are not movable (unreclaimable
* and/or protected), and returns its summary.
*
* Return: OR value of the following reason bit flags.
* * %NILFS_RESIZE_SEGMENT_PROTECTED - Protected segments exist
* * %NILFS_RESIZE_SEGMENT_UNRECLAIMABLE - Unreclaimable segments exist
*/
static int nilfs_resize_verify_failure(struct nilfs *nilfs,
__u64 *segnumv, unsigned long nsegs)
{
struct nilfs_suinfo si;
int reason = 0;
int i, ret;
for (i = 0; i < nsegs; i++) {
if (nilfs_get_suinfo(nilfs, segnumv[i], &si, 1) == 1) {
if (!nilfs_suinfo_reclaimable(&si))
reason |= NILFS_RESIZE_SEGMENT_UNRECLAIMABLE;
else if (nilfs_suinfo_empty(&si))
continue; /* Scrapped segment */
if (nilfs_suinfo_active(&si)) {
/*
* Active segments may not have been written
* either, so we determine them to be protected
* without reading the segment summary.
*/
reason |= NILFS_RESIZE_SEGMENT_PROTECTED;
continue;
}
}
ret = nilfs_resize_segment_is_protected(nilfs, segnumv[i]);
if (ret > 0)
reason |= NILFS_RESIZE_SEGMENT_PROTECTED;
}
return reason;
}
/**
* nilfs_resize_move_segments - attempt to move segments and find out why
* some segments cannot be moved
* @nilfs: nilfs object
* @segnumv: array of segment numbers
* @nsegs: number of segment numbers stored in @segnumv
* @reason: place to store the reason as an OR value of bit flags, if
* there are segments that failed to move
*
* This function attempts to move each segment specified by the segment
* number array @segnumv, and if a successfully moved (evicted) segment
* is in the area to be truncated, it calls nilfs_resize_progress_inc()
* to advance the displayed truncation progress by the number of successful
* segments.
*
* If there are segments that fail to be moved and @reason is not %NULL, it
* uses nilfs_resize_verify_failure() to check the reason for the failure
* and stores it in @reason as an OR value of the following bit flags:
* * %NILFS_RESIZE_SEGMENT_PROTECTED - Protected segments exist
* * %NILFS_RESIZE_SEGMENT_UNRECLAIMABLE - Unreclaimable segments exist
*
* Return: Number of segments moved on success, -1 on error.
*/
static ssize_t nilfs_resize_move_segments(struct nilfs *nilfs,
__u64 *segnumv, unsigned long nsegs,
int *reason)
{
unsigned long rest = nsegs, nc;
__u64 *snp = segnumv;
ssize_t nmoved = 0, i, nhits;
int rv = 0;
int ret;
for (snp = segnumv; rest > 0; snp += nc, rest -= nc) {
nc = min_t(unsigned long, rest, nsegments_per_clean);
if (nilfs_resize_update_sustat(nilfs) < 0)
return -1;
ret = nilfs_reclaim_segment(nilfs, snp, nc,
sustat.ss_prot_seq, 0);
if (ret < 0)
return -1;
nmoved += ret;
/* updating progress bar */
for (i = 0, nhits = 0; i < ret; i++) {
if (snp[i] >= trunc_start && snp[i] <= trunc_end)
nhits++;
}
if (nhits)
nilfs_resize_progress_inc(nhits);
/* check reason of gc failure */
if (ret < nc && reason)
rv |= nilfs_resize_verify_failure(nilfs, snp + ret,
nc - ret);
nanosleep(&clean_interval, NULL);
}
if (reason)
*reason = rv;
return nmoved;
}
static int __nilfs_resize_try_update_log_cursor(struct nilfs *nilfs)
{
nilfs_cno_t cno;
int ret = -1;
nilfs_sync(nilfs, &cno);
if (nilfs_freeze(nilfs) == 0 && nilfs_thaw(nilfs) == 0)
ret = 0;
nilfs_resize_update_sustat(nilfs);
return ret;
}
/**
* nilfs_resize_try_to_update_log_cursor - attempt to update log cursors
* @nilfs: nilfs object
* @reason: string representing the trigger for updating log cursors
*
* This function updates "log cursors", the starting points of segment
* chains pointed to by the two superblocks (the starting segments of log
* tracking during recovery mount). Updates are performed using sync,
* freeze and thaw ioctls, which are effective for converging these
* cursors.
* Then, to obtain the latest information on the segments protected by the
* log cursors, it updates the segment usage statistics &sustat by calling
* nilfs_resize_update_sustat().
*
* In verbose mode, this function outputs a message containing the reason
* for the execution before updating log cursors, and a message containing
* the result after updating them.
*
* Return: 0 on success, -1 on failure.
*/
static int nilfs_resize_try_update_log_cursor(struct nilfs *nilfs,
const char *reason)
{
int ret;
if (verbose)
myprintf("%s.\nTrying to update log cursor ..", reason);
ret = __nilfs_resize_try_update_log_cursor(nilfs);
if (verbose)
myprintf(ret == 0 ? " ok.\n" : " failed.\n");
return ret;
}
/**
* nilfs_resize_reclaim_nibble - somehow move a specified number of segments
* within a specified range or in front of it
* @nilfs: nilfs object
* @start: starting segment number of the segment range to reclaim
* @end: ending segment number of the segment range to reclaim
* @count: number of segments to attempt to reclaim
* @unprotect: flag whether to attempt to unprotect protected segments by
* updating log cursors
*
* This function moves up to @count movable segments in the range specified
* by [@start, @end]. If there are not enough movable segments in the range,
* it tries to move movable segments in the [0, @start) range to force
* active or protected segments out of the area to be truncated.
* If no movable segments are found in both ranges, it attempts to update
* log cursors once.
*
* If the @unprotect flag is true and no attempt has been made to update
* log cursors, an attempt will be made even if the same or more segments
* have been moved.
*
* Return: 0 if the specified number of segments can be moved, -1 if not.
*/
static int nilfs_resize_reclaim_nibble(struct nilfs *nilfs,
unsigned long long start,
unsigned long long end,
unsigned long count, int unprotect)
{
__u64 segnumv[2], segnum;
ssize_t nfound, nmoved = 0;
unsigned long nc;
unsigned long long end2 = end;
int log_cursor_updated = 0;
int ret;
segnum = start;
retry:
while (segnum <= end) {
ssize_t nm;
nc = min_t(unsigned long, count - nmoved, ARRAY_SIZE(segnumv));
nfound = nilfs_resize_find_movable_segments(
nilfs, segnum, end, segnumv, nc);
if (nfound < 0)
goto failed;
if (nfound == 0)
break;
segnum = segnumv[nfound - 1] + 1; /* set next segnum */
nm = nilfs_resize_move_segments(nilfs, segnumv, nfound, NULL);
if (nm < 0)
goto failed;
nmoved += nm;
if (nmoved >= count) {
if (unprotect && !log_cursor_updated) {
nilfs_resize_try_update_log_cursor(
nilfs, "Hit protected segment");
}
return 0;
}
}
if (end >= start) {
segnum = 0;
end = start - 1;
goto retry;
}
if (!log_cursor_updated) {
ret = nilfs_resize_try_update_log_cursor(
nilfs, "No movable segment");
if (!ret) {
segnum = start;
end = end2;
log_cursor_updated = 1;
goto retry;
}
}
myprintf("Error: couldn't move any segments.\n");
return -1;
failed: