cleanerd: fix wrong cleaner speed of manual clean mode

The number of segments per clean is wrong for manual clean mode used
by nilfs-clean command.  This fixes the issue.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: add checkpatch script

This import checkpatch script from a linux kernel and adds description
of patch submission rules to README file.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: remove trailing whitespaces

This fixes trailing whitespace issues as follows which are reported by
checkpatch script:

ERROR: trailing whitespace
+^I^I$

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: fix coding style issues: space required after that ','

This fixes the following coding style issues:

 ERROR: space required after that ',' (ctx:VxV)
 #51: FILE: bin/lssu.c:51:
 +       {"index",required_argument, NULL, 'i'},
                 ^
 ERROR: space required after that ',' (ctx:VxV)
 #52: FILE: bin/lssu.c:52:
 +       {"lines",required_argument, NULL, 'n'},
                 ^
 ERROR: space required after that ',' (ctx:VxV)
 #88: FILE: sbin/mkfs/mkfs.c:88:
 +#define ROUNDUP_DIV(n,m)   (((n) - 1) / (m) + 1)
                       ^
 ERROR: space required after that ',' (ctx:VxV)
 #89: FILE: sbin/mkfs/mkfs.c:89:
 +#define max_t(type,x,y) \
                   ^
 ERROR: space required after that ',' (ctx:VxV)
 #89: FILE: sbin/mkfs/mkfs.c:89:
 +#define max_t(type,x,y) \
                      ^
 ERROR: space required after that ',' (ctx:VxV)
 #307: FILE: sbin/mkfs/mkfs.c:307:
 +#define BLKDISCARD     _IO(0x12,119)
                                ^
 ERROR: space required after that ',' (ctx:VxV)
 #311: FILE: sbin/mkfs/mkfs.c:311:
 +#define BLKDISCARDZEROES _IO(0x12,124)
                                  ^
 ERROR: space required after that ',' (ctx:VxV)
 #19: FILE: sbin/mkfs/bitops.c:19:
 +int ext2fs_set_bit(int nr,void * addr)
                          ^
 ERROR: space required after that ',' (ctx:VxV)
 #55: FILE: sbin/mkfs/mkfs.h:55:
 +extern int ext2fs_set_bit(int nr,void * addr);
                                  ^
 ...

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: fix coding style issues: space required before the open parenthesis

This fixes the following coding style issues:

 ERROR: space required before the open parenthesis '('
 #154: FILE: sbin/mount/sundries.c:154:
 +     while(1) {

 ERROR: space required before the open parenthesis '('
 #45: FILE: sbin/mount/mount_mntent.c:45:
 +       while(1) {

 ERROR: space required before the open parenthesis '('
 #92: FILE: sbin/mount/mount_mntent.c:92:
 +       while(s != ss) {

 ERROR: space required before the open parenthesis '('
 #211: FILE: sbin/mount/mount_mntent.c:211:
 +               while(isdigit(*s)) s++;

 ERROR: space required before the open parenthesis '('
 #214: FILE: sbin/mount/mount_mntent.c:214:
 +       if(*s && !is_space_or_tab(*s))

 ERROR: space required before the open parenthesis '('
 #218: FILE: sbin/mount/mount_mntent.c:218:
 +       if(isdigit(*s)) {

 ERROR: space required before the open parenthesis '('
 #220: FILE: sbin/mount/mount_mntent.c:220:
 +               while(isdigit(*s)) s++;

 ERROR: space required before the open parenthesis '('
 #223: FILE: sbin/mount/mount_mntent.c:223:
 +       if(*s && !is_space_or_tab(*s))

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: remove space chars before tabs or start of a line

This fixes the following coding style issues:

 WARNING: please, no spaces at the start of a line
 #1159: FILE: sbin/mkfs/mkfs.c:1159:
 +        if (argc > 0) {$
 ...
 WARNING: please, no spaces at the start of a line
 #1163: FILE: sbin/mkfs/mkfs.c:1163:
 +        }$

 WARNING: please, no space before tabs
 #1252: FILE: sbin/mkfs/mkfs.c:1252:
 +^Iraw_inode->i_blocks = ^I^Icpu_to_le64(nilfs.files[ino]->nblocks);$

 WARNING: please, no space before tabs
 #29: FILE: sbin/mkfs/mkfs.h:29:
 +#define NILFS_DISKHDR_SIZE ^I^I4096 /* HDD header (MBR+superblock) */$

 WARNING: please, no space before tabs
 #65: FILE: sbin/mkfs/mkfs.h:65:
 +#define BLKGETSIZE64 ^I_IOR(0x12, 114, size_t)$

 WARNING: please, no space before tabs
 #94: FILE: sbin/nilfs-resize/nilfs-resize.c:94:
 +#define BLKGETSIZE64 ^I_IOR(0x12, 114, size_t)$

 WARNING: please, no space before tabs
 #98: FILE: sbin/nilfs-resize/nilfs-resize.c:98:
 +#define FIFREEZE ^I_IOWR('X', 119, int)$

 WARNING: please, no space before tabs
 #525: FILE: sbin/cleanerd/cleanerd.c:525:
 +#define FIFREEZE ^I_IOWR('X', 119, int)$

 WARNING: please, no spaces at the start of a line
 #334: FILE: sbin/mount/fstab.c:334:
 +     die(EX_USER, "%s", strsignal(sig));$

 WARNING: please, no spaces at the start of a line
 #504: FILE: sbin/mount/mount.nilfs2.c:504:
 +        mtab_ok = check_mtab();$

 WARNING: please, no spaces at the start of a line
 #109: FILE: sbin/mount/mount_opts.c:109:
 +  { "relatime",^I0, 0, MS_RELATIME },   /* Update access times relative to$

 WARNING: please, no spaces at the start of a line
 #111: FILE: sbin/mount/mount_opts.c:111:
 +  { "norelatime", 0, 1, MS_RELATIME }, /* Update access time without regard$

 WARNING: please, no space before tabs
 #412: FILE: sbin/mount/umount.nilfs2.c:412:
 +^I^I^I} else if (errno != EBUSY) { ^I/* hmm ... */$

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: fix coding stype issues related to position of open brace

This fixes the following checkpatch errors:

 ERROR: that open brace { should be on the previous line
 #1141: FILE: sbin/mkfs/mkfs.c:1141:
 +       if ((optind == argc) && !show_version_only)
 +       {

 ERROR: that open brace { should be on the previous line
 #191: FILE: sbin/nilfs-tune/nilfs-tune.c:191:
 +       if (optind == argc)
 +       {

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: fix coding syle issues: "foo * bar" should be "foo *bar"

This fixes the following checkpatch errors:

 ERROR: "foo * bar" should be "foo *bar"
 #19: FILE: sbin/mkfs/bitops.c:19:
 +int ext2fs_set_bit(int nr, void * addr)

 ERROR: "foo * bar" should be "foo *bar"
 #31: FILE: sbin/mkfs/bitops.c:31:
 +int ext2fs_clear_bit(int nr, void * addr)

 ERROR: "foo * bar" should be "foo *bar"
 #43: FILE: sbin/mkfs/bitops.c:43:
 +int ext2fs_test_bit(int nr, const void * addr)

 ERROR: "foo * bar" should be "foo *bar"
 #55: FILE: sbin/mkfs/mkfs.h:55:
 +extern int ext2fs_set_bit(int nr, void * addr);

 ERROR: "foo * bar" should be "foo *bar"
 #56: FILE: sbin/mkfs/mkfs.h:56:
 +extern int ext2fs_clear_bit(int nr, void * addr);

 ERROR: "foo * bar" should be "foo *bar"
 #57: FILE: sbin/mkfs/mkfs.h:57:
 +extern int ext2fs_test_bit(int nr, const void * addr);

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: fix coding style issues related to trailing statements

This fixes the following checkpatch errors:

 ERROR: trailing statements should be on next line
 #97: FILE: lib/feature.c:97:
 +       for (i = 0; mask >>= 1; i++);

 ERROR: trailing statements should be on next line
 #469: FILE: sbin/mkfs/mkfs.c:469:
 +       for (n = 0; i > 1; i >>= 1) n++;

 ERROR: trailing statements should be on next line
 #1095: FILE: sbin/nilfs-resize/nilfs-resize.c:1095:
 +               case 's': size <<= 9; break;

 ERROR: trailing statements should be on next line
 #1096: FILE: sbin/nilfs-resize/nilfs-resize.c:1096:
 +               case 'K': size <<= 10; break;

 ERROR: trailing statements should be on next line
 #1097: FILE: sbin/nilfs-resize/nilfs-resize.c:1097:
 +               case 'M': size <<= 20; break;

 ERROR: trailing statements should be on next line
 #1098: FILE: sbin/nilfs-resize/nilfs-resize.c:1098:
 +               case 'G': size <<= 30; break;

 ERROR: trailing statements should be on next line
 #1099: FILE: sbin/nilfs-resize/nilfs-resize.c:1099:
 +               case 'T': size <<= 40; break;

 ERROR: trailing statements should be on next line
 #1100: FILE: sbin/nilfs-resize/nilfs-resize.c:1100:
 +               case 'P': size <<= 50; break;

 ERROR: trailing statements should be on next line
 #692: FILE: sbin/cleanerd/cldconfig.c:692:
 +                       while (((c = fgetc(fp)) != '\n') && (c != EOF)) ;

 ERROR: trailing statements should be on next line
 #234: FILE: sbin/mount/sundries.c:234:
 +         if (!this_len) continue; /* if two ',' appear in a row */

 ERROR: trailing statements should be on next line
 #211: FILE: sbin/mount/mount_mntent.c:211:
 +               while (isdigit(*s)) s++;

 ERROR: trailing statements should be on next line
 #220: FILE: sbin/mount/mount_mntent.c:220:
 +               while (isdigit(*s)) s++;

 ERROR: trailing statements should be on next line
 #261: FILE: sbin/mount/umount.nilfs2.c:261:
 +       else while (argc--)

 ERROR: trailing statements should be on next line
 #342: FILE: sbin/nilfs-clean/nilfs-clean.c:342:
 +               case 's': break;

 ERROR: trailing statements should be on next line
 #343: FILE: sbin/nilfs-clean/nilfs-clean.c:343:
 +               case 'm': period *= 60; break;

 ERROR: trailing statements should be on next line
 #344: FILE: sbin/nilfs-clean/nilfs-clean.c:344:
 +               case 'h': period *= 3600; break;

 ERROR: trailing statements should be on next line
 #345: FILE: sbin/nilfs-clean/nilfs-clean.c:345:
 +               case 'd': period *= 86400; break;

 ERROR: trailing statements should be on next line
 #346: FILE: sbin/nilfs-clean/nilfs-clean.c:346:
 +               case 'w': period *= 604800; break;

 ERROR: trailing statements should be on next line
 #347: FILE: sbin/nilfs-clean/nilfs-clean.c:347:
 +               case 'M': period *= 2678400; break;

 ERROR: trailing statements should be on next line
 #348: FILE: sbin/nilfs-clean/nilfs-clean.c:348:
 +               case 'Y': period *= 31536000; break;

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: do not use C99 style comments

This fixes the following checkpatch errors:

 ERROR: do not use C99 // comments
 #1711: FILE: sbin/mkfs/mkfs.c:1711:
 +       //commit_ifile();

 ERROR: do not use C99 // comments
 #1714: FILE: sbin/mkfs/mkfs.c:1714:
 +       //commit_dat();

 ERROR: do not use C99 // comments
 #37: FILE: sbin/mount/sundries.c:37:
 +//#include "nfsmount.h"

 ERROR: do not use C99 // comments
 #485: FILE: sbin/mount/umount.nilfs2.c:485:
 +// writemtab:

 ERROR: do not use C99 // comments
 #86: FILE: include/nilfs.h:86:
 +typedef __u64 sector_t;        // XXX: __u64 ??

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: use tabs where possible for code indent

This fixes the following checkpatch error:

 ERROR: code indent should use tabs where possible
 #46: FILE: sbin/mkfs/mkfs.h:46:
 +^I^I^I^I^I        (non-reserved) segments */$

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: do not use assignment in if condition

This fixes the following checkpatch errors:

 ERROR: do not use assignment in if condition
 #147: FILE: bin/rmcp.c:147:
 +       if ((progname = strrchr(argv[0], '/')) == NULL)

 ERROR: do not use assignment in if condition
 #85: FILE: bin/mkcp.c:85:
 +       if ((progname = strrchr(argv[0], '/')) == NULL)

 ERROR: do not use assignment in if condition
 #183: FILE: bin/dumpseg.c:183:
 +       if ((progname = strrchr(argv[0], '/')) == NULL)

 ERROR: do not use assignment in if condition
 #239: FILE: bin/dumpseg.c:239:
 +               if ((segsize = nilfs_get_segment(nilfs, segnum, &seg)) < 0) {

 ERROR: do not use assignment in if condition
 #90: FILE: bin/chcp.c:90:
 +       if ((progname = strrchr(argv[0], '/')) == NULL)

 ERROR: do not use assignment in if condition
 #306: FILE: lib/gc.c:306:
 +               if ((n = nilfs_get_vinfo(nilfs, vinfo, j)) < 0)

 ERROR: do not use assignment in if condition
 #345: FILE: lib/gc.c:345:
 +               if ((n = nilfs_get_cpinfo(

 ERROR: do not use assignment in if condition
 #465: FILE: lib/gc.c:465:
 +       if ((n = nilfs_get_snapshot(nilfs, &ss)) < 0)

 ERROR: do not use assignment in if condition
 #559: FILE: lib/gc.c:559:
 +               if ((n = nilfs_get_bdescs(nilfs, bdescs + i, count)) < 0)

 ERROR: do not use assignment in if condition
 #127: FILE: lib/nilfs.c:127:
 +               if ((q = strchr(p, MNTOPT_SEP)) != NULL) {

 ERROR: do not use assignment in if condition
 #248: FILE: lib/nilfs.c:248:
 +       if ((pagesize = sysconf(_SC_PAGESIZE)) < 0)

 ERROR: do not use assignment in if condition
 #668: FILE: lib/nilfs.c:668:
 +       if (((ret = ioctl(nilfs->n_iocfd, NILFS_IOCTL_SYNC, cnop)) < 0) &&

 ERROR: do not use assignment in if condition
 #742: FILE: lib/nilfs.c:742:
 +               if ((segment = mmap(0, segsize, PROT_READ, MAP_SHARED,

 ERROR: do not use assignment in if condition
 #747: FILE: lib/nilfs.c:747:
 +               if ((segment = malloc(segsize)) == NULL)

 ERROR: do not use assignment in if condition
 #42: FILE: lib/vector.c:42:
 +       if ((vector = malloc(sizeof(struct nilfs_vector))) == NULL)

 ERROR: do not use assignment in if condition
 #45: FILE: lib/vector.c:45:
 +       if ((vector->v_data = malloc(elemsize * NILFS_VECTOR_INIT_MAXELEMS)) == NULL) {

 ERROR: do not use assignment in if condition
 #91: FILE: lib/vector.c:91:
 +               if ((data = realloc(vector->v_data,

 ERROR: do not use assignment in if condition
 #622: FILE: sbin/mkfs/mkfs.c:622:
 +       if ((fd = open(device, O_RDWR)) < 0)

 ERROR: do not use assignment in if condition
 #1059: FILE: sbin/mkfs/mkfs.c:1059:
 +               if ((cbufp = rindex(cbuf, '\n')) != NULL)

 ERROR: do not use assignment in if condition
 #1152: FILE: sbin/nilfs-resize/nilfs-resize.c:1152:
 +       if ((progname = strrchr(argv[0], '/')) != NULL)

 ERROR: do not use assignment in if condition
 #685: FILE: sbin/cleanerd/cldconfig.c:685:
 +       if ((fp = fopen(conffile, "r")) == NULL)

 ERROR: do not use assignment in if condition
 #695: FILE: sbin/cleanerd/cldconfig.c:695:
 +               if ((ntoks = tokenize(line, tokens, NTOKENS_MAX)) == 0)

 ERROR: do not use assignment in if condition
 #697: FILE: sbin/cleanerd/cldconfig.c:697:
 +               if ((ret = nilfs_cldconfig_handle_keyword(

 ERROR: do not use assignment in if condition
 #584: FILE: sbin/cleanerd/cleanerd.c:584:
 +       if ((smv = nilfs_vector_create(sizeof(struct nilfs_segimp))) == NULL)

 ERROR: do not use assignment in if condition
 #606: FILE: sbin/cleanerd/cleanerd.c:606:
 +               if ((n = nilfs_get_suinfo(nilfs, segnum, si, count)) < 0) {

 ERROR: do not use assignment in if condition
 #611: FILE: sbin/cleanerd/cleanerd.c:611:
 +                       if (nilfs_suinfo_reclaimable(&si[i]) &&

 ERROR: do not use assignment in if condition
 #660: FILE: sbin/cleanerd/cleanerd.c:660:
 +               if ((pid = fork()) < 0)

 ERROR: do not use assignment in if condition
 #310: FILE: sbin/mount/fstab.c:310:
 +               if ((opts = mc->m.mnt_opts) != NULL

 ERROR: do not use assignment in if condition
 #278: FILE: sbin/mount/umount.nilfs2.c:278:
 +       if ((fd = open (device, O_RDONLY)) < 0) {

 ERROR: do not use assignment in if condition
 #391: FILE: sbin/mount/umount.nilfs2.c:391:
 +               if (!read_only_mount_point(mc) &&

 ERROR: do not use assignment in if condition
 #471: FILE: sbin/mount/umount.nilfs2.c:471:
 +                       if ((mc = getmntoptfile (spec)) != NULL)

 ERROR: do not use assignment in if condition
 #494: FILE: sbin/nilfs-clean/nilfs-clean.c:494:
 +       if ((progname = strrchr(argv[0], '/')) != NULL)

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
fstab.c: do not use assignment in if codition

This fixes the following checkpatch error:

 ERROR: do not use assignment in if condition
 #458: FILE: sbin/mount/mount_opts.c:458:
 +               if (opts

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: do not use braces for single statement blocks

This fixes the following checkpatch warnings:

 WARNING: braces {} are not necessary for single statement blocks
 #843: FILE: sbin/nilfs-resize/nilfs-resize.c:843:
 +       if (err == ENOTTY) {
 +               myprintf("       This kernel does not support the resize API.\n");
 +       }

 WARNING: braces {} are not necessary for single statement blocks
 #934: FILE: sbin/nilfs-resize/nilfs-resize.c:934:
 +               if (verbose) {
 +                       myprintf("Truncating segments.\n");
 +               }

 WARNING: braces {} are not necessary for any arm of this statement
 #906: FILE: sbin/cleanerd/cleanerd.c:906:
 +       if (cleanerd->running == 0) {
 [...]
 +       } else if (cleanerd->running > 0) {
 [...]
 +       } else {
 [...]

 WARNING: braces {} are not necessary for single statement blocks
 #1253: FILE: sbin/cleanerd/cleanerd.c:1253:
 +       if (cleanerd->mm_nrestpasses == 0 && cleanerd->mm_nrestsegs == 0) {
 +               nilfs_cleanerd_manual_end(cleanerd);
 +       }

 WARNING: braces {} are not necessary for any arm of this statement
 #187: FILE: sbin/mount/sundries.c:187:
 +         if (r) {
 [...]
 +         } else {
 [...]

 WARNING: braces {} are not necessary for any arm of this statement
 #228: FILE: sbin/mount/sundries.c:228:
 +         if (r) {
 [...]
 +         } else {
 [...]

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
cleanerd: adjust the OOM killer

cleanerd is a very important process in a system which uses nilfs. So
it should adjust the OOM killer for reducing possibility of the
killing as much as possible.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mkfs: check sizes of important structs at build time

Current nilfs_check_ondisk_sizes() checks sizes of important structs
at run time. The checking should be done at build time. This patch
adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to
static_assert() of C++11. If an argument is true, the macro causes a
bulid error.

Below is an example of BUILD_BUG_ON(). When the checked conditions are
true like below:

/* intentional change for testing BUILD_BUG_ON() */

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)
{
	BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);
...

build process of mkfs.o causes errors like this:

gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I../..  -I../../include  -Wall -g -O2 -MT mkfs.o -MD -MP -MF .deps/mkfs.Tpo -c -o mkfs.o mkfs.c
mkfs.c: In function 'nilfs_check_ondisk_sizes':
mkfs.c:429:2: error: negative width in bit-field '<anonymous>'
mkfs.c:430:2: error: negative width in bit-field '<anonymous>'
mkfs.c:431:2: error: negative width in bit-field '<anonymous>'
mkfs.c:432:2: error: negative width in bit-field '<anonymous>'
mkfs.c:433:2: error: negative width in bit-field '<anonymous>'
mkfs.c:434:2: error: negative width in bit-field '<anonymous>'
mkfs.c:435:2: error: negative width in bit-field '<anonymous>'

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
cleanerd: ignore nofork option

The nofork option aims to reduce call of fork(), but it is not
effective. This patch lets cleanerd ignore the option simply even if
it is passed.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
cleanerd: call _exit(2) twice for ensuring not being a session leader

Current daemonize() function of cleanerd call _exit(2) only once
during its process of becoming a daemon process.  But in the linux
environment, a daemon should call _exit(2) twice for ensuring not
being a session leader.  If a process don't do that, unexpected SIGHUP
can be sent to the process (though it happens rarely). The signal
would be confusing event for cleanerd of nilfs.  This patch removes
this potential problem.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: don't pass -n option to cleanerd

This patch lets mount.nilfs2 not pass the -n option to cleanerd,
because it is obsoleted.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
man: remove a description of -n option

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>