libnilfsgc: insert sanity check for snapshot numbers

Snapshot numbers obtained through nilfs_get_cpinfo() ioctl must be
arranged in ascending order.  Otherwise, the Live/Dead determination
algorithm malfunctions.

This inserts a sanity check on the order of snapshot numbers in
nilfs_get_snapshot function to prevent GC from corrupting the file
system.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
libnilfsgc: try the last hit snapshot number in live/dead determination

For regular files, duration of a block correlates with its
predecessor.  By exploiting this nature, we can reduce the number of
comparisons with snapshot numbers and duration of blocks.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Imported from util-linux-2.13-pre5 tarball.

[modification applied for umount.nilfs2.c]

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
build-sys: remove DEFAULT_INCLUDES workaround

The automake stuff uses "-I.". as a default gcc option for
includes. This is a problem for source code where is local includes
with a same name like system includes (e.g.  mntent.h,
paths.h). Possible workaround is overwrite the automake
DEFAULT_INCLUDES variable. But this solution produces warnings. The
best way (this patch) is probably rename the files and remove
DEFAULT_INCLUDES.

[modified for mount.nilfs2.c and umount.nilfs2.c]

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: fix mtab_lock

* the lock function uses F_SETLK / F_SETLKW as a conditional wait.
  It's more reliable and better for performance to close the
  MOUNTED_LOCK file in unlock_mtab(), otherwise concurrent process will
  be wait by while () { link() } loop instead on fcntl(F_SETLKW).

  Thanks to Jeff Moyer <moyer@redhat.com> who found the problem two
  year ago.

* when open(MOUNTED_LOCK) failed, we need to try everything again, but
  the original code didn't zeroize "we_created_lockfile" and the old
  version in particular case left lock_mtab() without locked /etc/mtab.
  This is nasty bug.

* the original locking code had bad performance due too long sleep
  (1s),  between attempts. Now we're more aggressive and we use
  5000us. The result is that more processes is able to lock mtab in
  short time slice.

  Thanks to Peter Rockai <prockai@redhat.com> who found the problem
  and suggest a first version of the code with usleep.

* now we don't count number of attempts anymore, but we count sum of
  time which we spend in the mtab_lock(). The number of attempts is
  not important (and it also depends on CPU performance, load,
  scheduler, ...), the important thing is how long we spend with
  locking. Now time  limit is 30s.

[include statements was modified to apply autoconf header check]

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: relative atime support

Add the "relatime" (relative atime) option support to mount.  Relative
atime only updates the atime if the previous atime is older than the
mtime or ctime.  Like noatime, but useful for applications like mutt
that need to know when a file has been read since it was last
modified.

[the patch was applied to mount_opts.c instead of mount.c]

Cc: Adrian Bunk <bunk@stusta.de>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Karel Zak <kzak@redhat.com>

Signed-off-by: Valerie Henson <val_henson@linux.intel.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: fix memory usage in update_mtab

The update_mtab deallocates memory which was allocated by caller. It's nice
opportunity for double-free errors.

The patch fix a memory leak if we have to abort before mc0 are freed.  The
patch also fix a memory leak when we deallocate old (umounted) entry.

[modified for nilfs-utils/sbin/mount/fstab.c]

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Martin Schlemmer <azarah@gentoo.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: fix list logic in update_mtab

If mtab does not contain the new entry, then only mc0->prev is updated
to point to absent, but not the old mc0->prev's nxt pointer.  Because
we then use the nxt pointers to write the new mtab, absent is not
added to the new mtab.

(Note: fortunately, the mount doesn't use the update_mtab() for new
mounts, but for remount, move and umount only -- kzak)

If mtab is empty, absent->prev should be set to mc0, and not
mc0->prev, as it will be NULL.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Martin Schlemmer <azarah@gentoo.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: should set proper permissions on locktime

When creating the "/etc/mtab~" lockfile (specifically 'linktargetfile'  in the
lock_mtab function), the file is created with incorrect permissions ('000')
which necessitates root to leverage CAP_DAC_OVERRIDE. If proper file modes (it
would appear 0600 would be sufficient) were used in the open this would
function properly with CAP_DAC_OVERRIDE revoked.

  $ sysctl -w kernel.cap-bound=0xf7fd7df5
  $ mount -t tmpfs /dev/swap /mnt
  can't open lock file /etc/mtab~: Permission denied (use -n flag to override)

Signed-off-by: Flávio Leitner <fleitner@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: improve chmod & chown usage and clean up gcc warnings (fstab.c)

Fix strict gcc warnings in tailf that come from using:
  ("-Wall -Wp,-D_FORTIFY_SOURCE=2")

fstab.c:770: warning: ignoring return value of 'chown', declared with attribute warn_unused_result

The patch makes chmod() and chown() mandatory. We cannot rename()
temporary mtab to the final mtab when owner is not the same user as
owner of the original mtab. It's security risk.

Signed-off-by: Randy Dunlap <randy.dunlap@oracle.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: use atexit() rather than (*at_die)()

[modified for mount.nilfs2.c and umount.nilfs2.c]

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: mtab created multiple times with -a option

When /etc/mtab does not exist and mount is called with -a, for every
mount point that is mounted a root-fs record is added to mtab. This is
because get_mtab_info() sets the flag mtab_does_not_exist to 1 when it
doesn't find /etc/mtab. However, if it actually finds /etc/mtab, the
variable is not reset to 0. So on every subsequent call to
get_mtab_info() (as it is the case when mounting several mount points
with the -a option), mount will think that /etc/mtab does not exist,
even if it was created in the meantime by mount itself.

Reported-By: Jonas Kramer <jkramer@nex.scrapping.cc>
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Add comment on backported bug fixes from util-linux-2.17.2

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs_cleanerd: try to shrink protected segments when hit them

nilfs_cleanerd may stop garbage collection even if it has room for
reclaming more segments.  This issue is reproducible with the
following steps:

 - make snapshots
 - change all snapshots back to checkpoints.
 - run GC with nilfs-clean -p1
 - remove all data from the current file system tree.
 - run GC again with nilfs-clean -p1

In this situation, cleanerd hits segments protected by the file
system; one of log cursors written in super blocks was not updated and
prevents garbage collection.

This fixes the issue by trying to update the log cursor when GC failed
due to the protected segments.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: v2.1.3 release

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
AUTHORS: add description on maintainer of RPM packages

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: use realloc for xstrconcat functions

Everywhere in code is

	s = xstrconcat3(s, t, u);

that's nonsense use malloc(new_s); free(s); return new_s;

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
mount: use growable string for options

The parse_opt() routine uses fixed size of string for mount options.
This is useless for future selinux options where is not well defined
size of selinux context name.

The patch also makes code more readable and all option-string
operations share same code.

[modified for mount.nilfs2.c]

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: add own pathnames header file

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
nilfs-utils: cleanup usage of _PATH_*

The macros MOUNTED and MNTTAB are deprecated. This patch standardize
and consolidate all path macros and remove mount_paths.h.

[applied to all programs of nilfs-utils]

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>