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>