aboutsummaryrefslogtreecommitdiff
path: root/libdiskfs
diff options
context:
space:
mode:
Diffstat (limited to 'libdiskfs')
-rw-r--r--libdiskfs/Makefile74
-rw-r--r--libdiskfs/boot-start.c650
-rw-r--r--libdiskfs/conch-fetch.c71
-rw-r--r--libdiskfs/conch-set.c49
-rw-r--r--libdiskfs/console.c71
-rw-r--r--libdiskfs/dead-name.c47
-rw-r--r--libdiskfs/demuxer.c49
-rw-r--r--libdiskfs/dir-chg.c82
-rw-r--r--libdiskfs/dir-clear.c70
-rw-r--r--libdiskfs/dir-init.c68
-rw-r--r--libdiskfs/dir-link.c133
-rw-r--r--libdiskfs/dir-lookup.c561
-rw-r--r--libdiskfs/dir-mkdir.c70
-rw-r--r--libdiskfs/dir-mkfile.c91
-rw-r--r--libdiskfs/dir-readdir.c61
-rw-r--r--libdiskfs/dir-rename.c239
-rw-r--r--libdiskfs/dir-renamed.c241
-rw-r--r--libdiskfs/dir-rmdir.c94
-rw-r--r--libdiskfs/dir-unlink.c98
-rw-r--r--libdiskfs/direnter.c48
-rw-r--r--libdiskfs/dirremove.c45
-rw-r--r--libdiskfs/dirrewrite.c50
-rw-r--r--libdiskfs/disk-pager.c131
-rw-r--r--libdiskfs/diskfs-pager.h73
-rw-r--r--libdiskfs/diskfs.h1076
-rw-r--r--libdiskfs/extern-inline.c20
-rw-r--r--libdiskfs/extra-version.c24
-rw-r--r--libdiskfs/fhandle.h36
-rw-r--r--libdiskfs/file-access.c44
-rw-r--r--libdiskfs/file-chauthor.c42
-rw-r--r--libdiskfs/file-chflags.c40
-rw-r--r--libdiskfs/file-chg.c71
-rw-r--r--libdiskfs/file-chmod.c57
-rw-r--r--libdiskfs/file-chown.c64
-rw-r--r--libdiskfs/file-exec.c212
-rw-r--r--libdiskfs/file-get-children.c95
-rw-r--r--libdiskfs/file-get-fs-opts.c54
-rw-r--r--libdiskfs/file-get-source.c37
-rw-r--r--libdiskfs/file-get-trans.c138
-rw-r--r--libdiskfs/file-get-transcntl.c48
-rw-r--r--libdiskfs/file-getcontrol.c51
-rw-r--r--libdiskfs/file-getfh.c60
-rw-r--r--libdiskfs/file-getlinknode.c39
-rw-r--r--libdiskfs/file-lock-stat.c38
-rw-r--r--libdiskfs/file-lock.c36
-rw-r--r--libdiskfs/file-reparent.c70
-rw-r--r--libdiskfs/file-set-size.c55
-rw-r--r--libdiskfs/file-set-trans.c215
-rw-r--r--libdiskfs/file-statfs.c50
-rw-r--r--libdiskfs/file-sync.c42
-rw-r--r--libdiskfs/file-syncfs.c61
-rw-r--r--libdiskfs/file-utimes.c57
-rw-r--r--libdiskfs/fsmutations.h39
-rw-r--r--libdiskfs/fsys-forward.c37
-rw-r--r--libdiskfs/fsys-getfile.c104
-rw-r--r--libdiskfs/fsys-getroot.c207
-rw-r--r--libdiskfs/fsys-goaway.c51
-rw-r--r--libdiskfs/fsys-options.c112
-rw-r--r--libdiskfs/fsys-startup.c36
-rw-r--r--libdiskfs/fsys-syncfs.c70
-rw-r--r--libdiskfs/get-source.c33
-rw-r--r--libdiskfs/ifsock.c131
-rw-r--r--libdiskfs/init-first.c65
-rw-r--r--libdiskfs/init-init.c111
-rw-r--r--libdiskfs/init-main.c78
-rw-r--r--libdiskfs/init-startup.c224
-rw-r--r--libdiskfs/io-async-icky.c33
-rw-r--r--libdiskfs/io-async.c30
-rw-r--r--libdiskfs/io-duplicate.c46
-rw-r--r--libdiskfs/io-get-conch.c44
-rw-r--r--libdiskfs/io-identity.c67
-rw-r--r--libdiskfs/io-map-cntl.c53
-rw-r--r--libdiskfs/io-map.c72
-rw-r--r--libdiskfs/io-modes-get.c33
-rw-r--r--libdiskfs/io-modes-off.c35
-rw-r--r--libdiskfs/io-modes-on.c35
-rw-r--r--libdiskfs/io-modes-set.c36
-rw-r--r--libdiskfs/io-owner-get.c36
-rw-r--r--libdiskfs/io-owner-mod.c36
-rw-r--r--libdiskfs/io-pathconf.c80
-rw-r--r--libdiskfs/io-prenotify.c72
-rw-r--r--libdiskfs/io-read.c109
-rw-r--r--libdiskfs/io-readable.c46
-rw-r--r--libdiskfs/io-reauthenticate.c66
-rw-r--r--libdiskfs/io-rel-conch.c44
-rw-r--r--libdiskfs/io-restrict-auth.c55
-rw-r--r--libdiskfs/io-revoke.c59
-rw-r--r--libdiskfs/io-seek.c61
-rw-r--r--libdiskfs/io-select.c40
-rw-r--r--libdiskfs/io-sigio.c36
-rw-r--r--libdiskfs/io-stat.c51
-rw-r--r--libdiskfs/io-stubs.c62
-rw-r--r--libdiskfs/io-version.c42
-rw-r--r--libdiskfs/io-write.c93
-rw-r--r--libdiskfs/lithp.h31
-rw-r--r--libdiskfs/lookup.c234
-rw-r--r--libdiskfs/name-cache.c376
-rw-r--r--libdiskfs/node-create.c163
-rw-r--r--libdiskfs/node-drop.c98
-rw-r--r--libdiskfs/node-make.c75
-rw-r--r--libdiskfs/node-nput.c81
-rw-r--r--libdiskfs/node-nputl.c38
-rw-r--r--libdiskfs/node-nref.c40
-rw-r--r--libdiskfs/node-nrefl.c30
-rw-r--r--libdiskfs/node-nrele.c65
-rw-r--r--libdiskfs/node-nrelel.c39
-rw-r--r--libdiskfs/node-rdwr.c77
-rw-r--r--libdiskfs/node-times.c74
-rw-r--r--libdiskfs/node-update.c32
-rw-r--r--libdiskfs/opts-append-std.c66
-rw-r--r--libdiskfs/opts-common.c59
-rw-r--r--libdiskfs/opts-get.c28
-rw-r--r--libdiskfs/opts-runtime.c24
-rw-r--r--libdiskfs/opts-set.c34
-rw-r--r--libdiskfs/opts-std-runtime.c161
-rw-r--r--libdiskfs/opts-std-startup.c178
-rw-r--r--libdiskfs/opts-version.c47
-rw-r--r--libdiskfs/peropen-make.c72
-rw-r--r--libdiskfs/peropen-rele.c49
-rw-r--r--libdiskfs/priv.h141
-rw-r--r--libdiskfs/protid-make.c68
-rw-r--r--libdiskfs/protid-rele.c35
-rw-r--r--libdiskfs/rdwr-internal.c74
-rw-r--r--libdiskfs/readonly-changed.c31
-rw-r--r--libdiskfs/readonly.c111
-rw-r--r--libdiskfs/remount.c47
-rw-r--r--libdiskfs/shutdown.c102
-rw-r--r--libdiskfs/sync-default.c23
-rw-r--r--libdiskfs/sync-interval.c136
-rw-r--r--libdiskfs/trans-callback.c91
-rw-r--r--libdiskfs/validate-author.c27
-rw-r--r--libdiskfs/validate-flags.c27
-rw-r--r--libdiskfs/validate-group.c27
-rw-r--r--libdiskfs/validate-mode.c27
-rw-r--r--libdiskfs/validate-owner.c27
-rw-r--r--libdiskfs/validate-rdev.c27
136 files changed, 11960 insertions, 0 deletions
diff --git a/libdiskfs/Makefile b/libdiskfs/Makefile
new file mode 100644
index 00000000..996e86a0
--- /dev/null
+++ b/libdiskfs/Makefile
@@ -0,0 +1,74 @@
+# Copyright (C) 1994,95,96,97,98,99,2000,01,2006,2012
+# Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2, or (at
+# your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+dir := libdiskfs
+makemode := library
+
+libname = libdiskfs
+FSSRCS= dir-chg.c dir-link.c dir-lookup.c dir-mkdir.c dir-mkfile.c \
+ dir-readdir.c dir-rename.c dir-rmdir.c dir-unlink.c \
+ file-access.c file-chauthor.c file-chflags.c file-chg.c \
+ file-chmod.c file-chown.c file-exec.c file-get-fs-opts.c \
+ file-get-trans.c file-get-transcntl.c file-getcontrol.c \
+ file-getfh.c file-getlinknode.c file-lock-stat.c \
+ file-lock.c file-set-size.c file-set-trans.c file-statfs.c \
+ file-sync.c file-syncfs.c file-utimes.c file-reparent.c
+IOSRCS= io-async-icky.c io-async.c io-duplicate.c io-get-conch.c io-revoke.c \
+ io-map-cntl.c io-map.c io-modes-get.c io-modes-off.c \
+ io-modes-on.c io-modes-set.c io-owner-mod.c io-owner-get.c \
+ io-pathconf.c io-prenotify.c io-read.c io-readable.c io-identity.c \
+ io-reauthenticate.c io-rel-conch.c io-restrict-auth.c io-seek.c \
+ io-select.c io-stat.c io-stubs.c io-write.c io-version.c io-sigio.c
+FSYSSRCS=fsys-getroot.c fsys-goaway.c fsys-startup.c fsys-getfile.c \
+ fsys-options.c fsys-syncfs.c fsys-forward.c \
+ file-get-children.c file-get-source.c
+IFSOCKSRCS=ifsock.c
+OTHERSRCS = conch-fetch.c conch-set.c dir-clear.c dir-init.c dir-renamed.c \
+ extern-inline.c \
+ node-create.c node-drop.c node-make.c node-rdwr.c node-update.c \
+ node-nref.c node-nput.c node-nrele.c node-nrefl.c node-nputl.c \
+ node-nrelel.c \
+ peropen-make.c peropen-rele.c protid-make.c protid-rele.c \
+ init-init.c init-startup.c init-first.c init-main.c \
+ rdwr-internal.c boot-start.c demuxer.c node-times.c shutdown.c \
+ sync-interval.c sync-default.c \
+ opts-set.c opts-get.c opts-std-startup.c opts-std-runtime.c \
+ opts-append-std.c opts-common.c opts-runtime.c opts-version.c \
+ trans-callback.c readonly.c readonly-changed.c \
+ remount.c console.c disk-pager.c \
+ name-cache.c direnter.c dirrewrite.c dirremove.c lookup.c dead-name.c \
+ validate-mode.c validate-group.c validate-author.c validate-flags.c \
+ validate-rdev.c validate-owner.c extra-version.c get-source.c
+SRCS = $(OTHERSRCS) $(FSSRCS) $(IOSRCS) $(FSYSSRCS) $(IFSOCKSRCS)
+installhdrs = diskfs.h diskfs-pager.h
+
+MIGSTUBS = fsServer.o ioServer.o fsysServer.o exec_startupServer.o \
+ fsys_replyUser.o fs_notifyUser.o ifsockServer.o \
+ startup_notifyServer.o
+OBJS = $(sort $(SRCS:.c=.o) $(MIGSTUBS))
+
+HURDLIBS = fshelp iohelp store ports shouldbeinlibc pager
+LDLIBS += -lpthread
+
+fsys-MIGSFLAGS = -imacros $(srcdir)/fsmutations.h -DREPLY_PORTS
+fs-MIGSFLAGS = -imacros $(srcdir)/fsmutations.h
+io-MIGSFLAGS = -imacros $(srcdir)/fsmutations.h
+ifsock-MIGSFLAGS = -imacros $(srcdir)/fsmutations.h
+exec_startup-MIGSFLAGS = -imacros $(srcdir)/fsmutations.h
+MIGCOMSFLAGS = -prefix diskfs_
+
+include ../Makeconf
diff --git a/libdiskfs/boot-start.c b/libdiskfs/boot-start.c
new file mode 100644
index 00000000..4cc7bb8d
--- /dev/null
+++ b/libdiskfs/boot-start.c
@@ -0,0 +1,650 @@
+/*
+ Copyright (C) 1993,94,95,96,97,98,99,2000,01,02,10,11
+ Free Software Foundation, Inc.
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include <stdio.h>
+#include <hurd.h>
+#include <hurd/fsys.h>
+#include <hurd/exec.h>
+#include <hurd/startup.h>
+#include <hurd/paths.h>
+#include <fcntl.h>
+#include <device/device.h>
+#include <sys/reboot.h>
+#include <string.h>
+#include <argz.h>
+#include <error.h>
+#include <pids.h>
+#include "exec_S.h"
+#include "exec_startup_S.h"
+#include "fsys_S.h"
+#include "fsys_reply_U.h"
+
+static mach_port_t diskfs_exec_ctl;
+extern task_t diskfs_exec_server_task;
+static task_t parent_task = MACH_PORT_NULL;
+
+static pthread_mutex_t execstartlock;
+static pthread_cond_t execstarted;
+
+const char *diskfs_boot_init_program = _HURD_INIT;
+
+static void start_execserver ();
+
+char **diskfs_argv = 0;
+
+static mach_port_t
+get_console ()
+{
+ mach_port_t device_master, console;
+ error_t err = get_privileged_ports (0, &device_master);
+
+ if (err)
+ return MACH_PORT_NULL;
+
+ err = device_open (device_master, D_WRITE | D_READ, "console", &console);
+ if (err)
+ return MACH_PORT_NULL;
+
+ return console;
+}
+
+/* Make sure we have the privileged ports. */
+void
+_diskfs_boot_privports (void)
+{
+ assert (diskfs_boot_filesystem ());
+ if (_hurd_host_priv == MACH_PORT_NULL)
+ {
+ /* We are the boot command run by the real bootstrap filesystem.
+ We get the privileged ports from it as init would. */
+ mach_port_t bootstrap;
+ error_t err = task_get_bootstrap_port (mach_task_self (), &bootstrap);
+ assert_perror (err);
+ err = fsys_getpriv (bootstrap, &_hurd_host_priv, &_hurd_device_master,
+ &parent_task);
+ mach_port_deallocate (mach_task_self (), bootstrap);
+ assert_perror (err);
+ }
+}
+
+/* Once diskfs_root_node is set, call this if we are a bootstrap
+ filesystem. */
+void
+diskfs_start_bootstrap ()
+{
+ mach_port_t root_pt, startup_pt, bootpt;
+ retry_type retry;
+ char pathbuf[1024];
+ string_t retry_name;
+ mach_port_t portarray[INIT_PORT_MAX];
+ mach_port_t fdarray[3]; /* XXX */
+ task_t newt;
+ error_t err;
+ char *exec_argv, *exec_env;
+ const char *initname;
+ size_t exec_argvlen, exec_envlen;
+ struct port_info *bootinfo;
+ struct protid *rootpi;
+ struct peropen *rootpo;
+ mach_port_t diskfs_exec;
+ unsigned int init_lookups = 0;
+
+ /* Create the port for current and root directory. */
+ err = diskfs_make_peropen (diskfs_root_node, O_READ | O_EXEC, 0,
+ &rootpo);
+ assert_perror (err);
+
+ err = diskfs_create_protid (rootpo, 0, &rootpi);
+ assert_perror (err);
+
+ /* Get us a send right to copy around. */
+ root_pt = ports_get_send_right (rootpi);
+ ports_port_deref (rootpi);
+
+ if (diskfs_exec_server_task == MACH_PORT_NULL)
+ {
+ /* We are the boot command run by the real bootstrap filesystem.
+ Our parent (the real bootstrap filesystem) provides us a root
+ directory where we look up /servers/exec like any non-bootstrap
+ filesystem would. */
+ assert (_hurd_ports);
+ assert (_hurd_ports[INIT_PORT_CRDIR].port != MACH_PORT_NULL);
+ diskfs_exec = file_name_lookup (_SERVERS_EXEC, 0, 0);
+ if (diskfs_exec == MACH_PORT_NULL)
+ error (1, errno, "%s", _SERVERS_EXEC);
+ else
+ {
+#ifndef NDEBUG
+ /* Make sure this is really a port to another server. */
+ struct port_info *pi = ports_lookup_port (diskfs_port_bucket,
+ diskfs_exec, 0);
+ assert (!pi);
+#endif
+ }
+
+ /* Here we assume the parent has already printed:
+ Hurd server bootstrap: bootfs[bootdev] exec ourfs
+ */
+ printf ("\nContinuing on new root filesystem %s:", diskfs_disk_name);
+ fflush (stdout);
+ }
+ else
+ {
+ uid_t idlist[] = {0, 0, 0};
+ file_t execnode;
+
+ printf ("Hurd server bootstrap: %s[%s]",
+ program_invocation_short_name, diskfs_disk_name);
+ fflush (stdout);
+
+ /* Get the execserver going and wait for its fsys_startup */
+ pthread_mutex_init (&execstartlock, NULL);
+ pthread_cond_init (&execstarted, NULL);
+ pthread_mutex_lock (&execstartlock);
+ start_execserver ();
+ pthread_cond_wait (&execstarted, &execstartlock);
+ pthread_mutex_unlock (&execstartlock);
+ assert (diskfs_exec_ctl != MACH_PORT_NULL);
+
+ /* Contact the exec server. */
+ err = fsys_getroot (diskfs_exec_ctl, root_pt, MACH_MSG_TYPE_COPY_SEND,
+ idlist, 3, idlist, 3, 0,
+ &retry, retry_name, &diskfs_exec);
+ assert_perror (err);
+ assert (retry == FS_RETRY_NORMAL);
+ assert (retry_name[0] == '\0');
+ assert (diskfs_exec != MACH_PORT_NULL);
+
+ /* Attempt to set the active translator for the exec server so that
+ filesystems other than the bootstrap can find it. */
+ err = dir_lookup (root_pt, _SERVERS_EXEC, O_NOTRANS, 0,
+ &retry, pathbuf, &execnode);
+ if (err)
+ {
+ error (0, err, "cannot set translator on %s", _SERVERS_EXEC);
+ mach_port_deallocate (mach_task_self (), diskfs_exec_ctl);
+ }
+ else
+ {
+ assert (retry == FS_RETRY_NORMAL);
+ assert (retry_name[0] == '\0');
+ assert (execnode != MACH_PORT_NULL);
+ err = file_set_translator (execnode, 0, FS_TRANS_SET, 0, 0, 0,
+ diskfs_exec_ctl, MACH_MSG_TYPE_COPY_SEND);
+ mach_port_deallocate (mach_task_self (), diskfs_exec_ctl);
+ mach_port_deallocate (mach_task_self (), execnode);
+ assert_perror (err);
+ }
+ diskfs_exec_ctl = MACH_PORT_NULL; /* Not used after this. */
+ }
+
+ /* Cache the exec server port for file_exec to use. */
+ _hurd_port_set (&_diskfs_exec_portcell, diskfs_exec);
+
+ if (_diskfs_boot_command)
+ {
+ /* We have a boot command line to run instead of init. */
+ err = argz_create (_diskfs_boot_command, &exec_argv, &exec_argvlen);
+ assert_perror (err);
+ initname = exec_argv;
+ while (*initname == '/')
+ initname++;
+ }
+ else
+ {
+ /* Choose the name of the startup server to execute. */
+ initname = diskfs_boot_init_program;
+ while (*initname == '/')
+ initname++;
+
+ int len = asprintf (&exec_argv, "/%s%c", initname, '\0');
+ assert (len != -1);
+ exec_argvlen = (size_t) len;
+ err = argz_add_sep (&exec_argv, &exec_argvlen,
+ diskfs_boot_command_line, ' ');
+ assert_perror (err);
+
+ initname = exec_argv + 1;
+ }
+
+ lookup_init:
+ err = dir_lookup (root_pt, initname, O_READ, 0,
+ &retry, pathbuf, &startup_pt);
+ init_lookups++;
+ if (err)
+ {
+ printf ("\nCannot find startup program `%s': %s\n",
+ initname, strerror (err));
+ fflush (stdout);
+ free (exec_argv);
+ assert_perror (err); /* XXX this won't reboot properly */
+ }
+ else if (retry == FS_RETRY_MAGICAL && pathbuf[0] == '/')
+ {
+ assert (sysconf (_SC_SYMLOOP_MAX) < 0 ||
+ init_lookups < sysconf (_SC_SYMLOOP_MAX));
+
+ /* INITNAME is a symlink with an absolute target, so try again. */
+ initname = strdupa (pathbuf);
+ goto lookup_init;
+ }
+
+ assert (retry == FS_RETRY_NORMAL);
+ assert (pathbuf[0] == '\0');
+
+ err = ports_create_port (diskfs_initboot_class, diskfs_port_bucket,
+ sizeof (struct port_info), &bootinfo);
+ assert_perror (err);
+ bootpt = ports_get_send_right (bootinfo);
+ ports_port_deref (bootinfo);
+
+ portarray[INIT_PORT_CRDIR] = root_pt;
+ portarray[INIT_PORT_CWDIR] = root_pt;
+ portarray[INIT_PORT_AUTH] = MACH_PORT_NULL;
+ portarray[INIT_PORT_PROC] = MACH_PORT_NULL;
+ portarray[INIT_PORT_CTTYID] = MACH_PORT_NULL;
+ portarray[INIT_PORT_BOOTSTRAP] = bootpt;
+
+ fdarray[0] = fdarray[1] = fdarray[2] = get_console (); /* XXX */
+
+ err = argz_create (environ, &exec_env, &exec_envlen);
+ assert_perror (err);
+
+ err = task_create (mach_task_self (),
+#ifdef KERN_INVALID_LEDGER
+ NULL, 0, /* OSF Mach */
+#endif
+ 0, &newt);
+ assert_perror (err);
+ if (_diskfs_boot_pause)
+ {
+ printf ("pausing for %s...\n", exec_argv);
+ getc (stdin);
+ }
+ printf (" %s", basename (exec_argv));
+ fflush (stdout);
+ err = exec_exec (diskfs_exec, startup_pt, MACH_MSG_TYPE_COPY_SEND,
+ newt, 0, exec_argv, exec_argvlen, exec_env, exec_envlen,
+ fdarray, MACH_MSG_TYPE_COPY_SEND, 3,
+ portarray, MACH_MSG_TYPE_COPY_SEND, INIT_PORT_MAX,
+ /* Supply no intarray, since we have no info for it.
+ With none supplied, it will use the defaults. */
+ NULL, 0, 0, 0, 0, 0);
+ free (exec_argv);
+ free (exec_env);
+ mach_port_deallocate (mach_task_self (), root_pt);
+ mach_port_deallocate (mach_task_self (), startup_pt);
+ mach_port_deallocate (mach_task_self (), bootpt);
+ assert_perror (err);
+}
+
+/* We look like an execserver to the execserver itself; it makes this
+ call (as does any task) to get its state. We can't give it all of
+ its ports (we'll provide those with a later call to exec_init). */
+kern_return_t
+diskfs_S_exec_startup_get_info (struct bootinfo *upt,
+ vm_address_t *user_entry,
+ vm_address_t *phdr_data,
+ vm_size_t *phdr_size,
+ vm_address_t *base_addr,
+ vm_size_t *stack_size,
+ int *flags,
+ char **argvP,
+ mach_msg_type_number_t *argvlen,
+ char **envpP __attribute__ ((unused)),
+ mach_msg_type_number_t *envplen,
+ mach_port_t **dtableP,
+ mach_msg_type_name_t *dtablepoly,
+ mach_msg_type_number_t *dtablelen,
+ mach_port_t **portarrayP,
+ mach_msg_type_name_t *portarraypoly,
+ mach_msg_type_number_t *portarraylen,
+ int **intarrayP,
+ mach_msg_type_number_t *intarraylen)
+{
+ error_t err;
+ mach_port_t *portarray, *dtable;
+ mach_port_t rootport;
+ struct protid *rootpi;
+ struct peropen *rootpo;
+
+ if (! upt)
+ return EOPNOTSUPP;
+
+ *user_entry = 0;
+ *phdr_data = *base_addr = 0;
+ *phdr_size = *stack_size = 0;
+
+ /* We have no args for it. Tell it to look on its stack
+ for the args placed there by the boot loader. */
+ *argvlen = *envplen = 0;
+ *flags = EXEC_STACK_ARGS;
+
+ if (*portarraylen < INIT_PORT_MAX)
+ *portarrayP = mmap (0, INIT_PORT_MAX * sizeof (mach_port_t),
+ PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ portarray = *portarrayP;
+ *portarraylen = INIT_PORT_MAX;
+
+ if (*dtablelen < 3)
+ *dtableP = mmap (0, 3 * sizeof (mach_port_t), PROT_READ|PROT_WRITE,
+ MAP_ANON, 0, 0);
+ dtable = *dtableP;
+ *dtablelen = 3;
+ dtable[0] = dtable[1] = dtable[2] = get_console (); /* XXX */
+
+ *intarrayP = NULL;
+ *intarraylen = 0;
+
+ err = diskfs_make_peropen (diskfs_root_node, O_READ | O_EXEC, 0, &rootpo);
+ assert_perror (err);
+
+ err = diskfs_create_protid (rootpo, 0, &rootpi);
+ assert_perror (err);
+
+ rootport = ports_get_right (rootpi);
+ ports_port_deref (rootpi);
+ portarray[INIT_PORT_CWDIR] = rootport;
+ portarray[INIT_PORT_CRDIR] = rootport;
+ portarray[INIT_PORT_AUTH] = MACH_PORT_NULL;
+ portarray[INIT_PORT_PROC] = MACH_PORT_NULL;
+ portarray[INIT_PORT_CTTYID] = MACH_PORT_NULL;
+ portarray[INIT_PORT_BOOTSTRAP] = upt->pi.port_right; /* use the same port */
+
+ *portarraypoly = MACH_MSG_TYPE_MAKE_SEND;
+
+ *dtablepoly = MACH_MSG_TYPE_COPY_SEND;
+
+ return 0;
+}
+
+/* Called by S_fsys_startup for execserver bootstrap. The execserver
+ is able to function without a real node, hence this fraud. */
+error_t
+diskfs_execboot_fsys_startup (mach_port_t port, int flags,
+ mach_port_t ctl,
+ mach_port_t *real,
+ mach_msg_type_name_t *realpoly)
+{
+ error_t err;
+ string_t pathbuf;
+ enum retry_type retry;
+ struct port_info *pt;
+ struct protid *rootpi;
+ struct peropen *rootpo;
+ mach_port_t rootport;
+
+ if (!(pt = ports_lookup_port (diskfs_port_bucket, port,
+ diskfs_execboot_class)))
+ return EOPNOTSUPP;
+
+ err = diskfs_make_peropen (diskfs_root_node, flags, 0, &rootpo);
+ assert_perror (err);
+ err = diskfs_create_protid (rootpo, 0, &rootpi);
+ assert_perror (err);
+ rootport = ports_get_send_right (rootpi);
+ ports_port_deref (rootpi);
+
+ err = dir_lookup (rootport, _SERVERS_EXEC, flags|O_NOTRANS, 0,
+ &retry, pathbuf, real);
+ assert_perror (err);
+ assert (retry == FS_RETRY_NORMAL);
+ assert (pathbuf[0] == '\0');
+ *realpoly = MACH_MSG_TYPE_MOVE_SEND;
+
+ mach_port_deallocate (mach_task_self (), rootport);
+
+ diskfs_exec_ctl = ctl;
+
+ pthread_mutex_lock (&execstartlock);
+ pthread_cond_signal (&execstarted);
+ pthread_mutex_unlock (&execstartlock);
+ ports_port_deref (pt);
+ return 0;
+}
+
+/* Called by init to get the privileged ports as described
+ in <hurd/fsys.defs>. */
+kern_return_t
+diskfs_S_fsys_getpriv (struct diskfs_control *init_bootstrap_port,
+ mach_port_t reply, mach_msg_type_name_t reply_type,
+ mach_port_t *host_priv, mach_msg_type_name_t *hp_type,
+ mach_port_t *dev_master, mach_msg_type_name_t *dm_type,
+ mach_port_t *fstask, mach_msg_type_name_t *task_type)
+{
+ error_t err;
+
+ if (!init_bootstrap_port
+ || init_bootstrap_port->pi.class != diskfs_initboot_class)
+ return EOPNOTSUPP;
+
+ err = get_privileged_ports (host_priv, dev_master);
+ if (!err)
+ {
+ *fstask = mach_task_self ();
+ *hp_type = *dm_type = MACH_MSG_TYPE_MOVE_SEND;
+ *task_type = MACH_MSG_TYPE_COPY_SEND;
+ }
+
+ return err;
+}
+
+/* Called by init to give us ports to the procserver and authserver as
+ described in <hurd/fsys.defs>. */
+kern_return_t
+diskfs_S_fsys_init (struct diskfs_control *pt,
+ mach_port_t reply, mach_msg_type_name_t replytype,
+ mach_port_t procserver,
+ mach_port_t authhandle)
+{
+ static int initdone = 0;
+ mach_port_t host, startup;
+ error_t err;
+ mach_port_t root_pt;
+ struct protid *rootpi;
+ struct peropen *rootpo;
+
+ if (!pt
+ || pt->pi.class != diskfs_initboot_class)
+ return EOPNOTSUPP;
+
+ if (initdone)
+ return EOPNOTSUPP;
+ initdone = 1;
+
+ /* init is single-threaded, so we must reply to its RPC before doing
+ anything which might attempt to send an RPC to init. */
+ fsys_init_reply (reply, replytype, 0);
+
+ /* Allocate our references here; _hurd_init will consume a reference
+ for the library itself. */
+ err = mach_port_mod_refs (mach_task_self (),
+ procserver, MACH_PORT_RIGHT_SEND, +1);
+ assert_perror (err);
+ err = mach_port_mod_refs (mach_task_self (),
+ authhandle, MACH_PORT_RIGHT_SEND, +1);
+ assert_perror (err);
+
+ if (diskfs_auth_server_port != MACH_PORT_NULL)
+ mach_port_deallocate (mach_task_self (), diskfs_auth_server_port);
+ diskfs_auth_server_port = authhandle;
+
+ if (diskfs_exec_server_task != MACH_PORT_NULL)
+ {
+ process_t execprocess;
+ err = proc_task2proc (procserver, diskfs_exec_server_task, &execprocess);
+ assert_perror (err);
+
+ /* Declare that the exec server is our child. */
+ proc_child (procserver, diskfs_exec_server_task);
+ proc_mark_exec (execprocess);
+
+ /* Don't start this until now so that exec is fully authenticated
+ with proc. */
+ HURD_PORT_USE (&_diskfs_exec_portcell,
+ exec_init (port, authhandle,
+ execprocess, MACH_MSG_TYPE_COPY_SEND));
+ mach_port_deallocate (mach_task_self (), execprocess);
+
+ /* We don't need this anymore. */
+ mach_port_deallocate (mach_task_self (), diskfs_exec_server_task);
+ diskfs_exec_server_task = MACH_PORT_NULL;
+ }
+ else
+ {
+ mach_port_t bootstrap;
+ process_t parent_proc;
+
+ assert (parent_task != MACH_PORT_NULL);
+
+ /* Tell the proc server that our parent task is our child. This
+ makes the process hierarchy fail to represent the real order of
+ who created whom, but it sets the owner and authentication ids to
+ root. It doesn't really matter that the parent fs task be
+ authenticated, but the exec server needs to be authenticated to
+ complete the boot handshakes with init. The exec server gets its
+ privilege by the parent fs doing proc_child (code above) after
+ we send it fsys_init (below). */
+
+ err = proc_child (procserver, parent_task);
+ assert_perror (err);
+
+ /* Get the parent's proc server port so we can send it in the fsys_init
+ RPC just as init would. */
+ err = proc_task2proc (procserver, parent_task, &parent_proc);
+ assert_perror (err);
+
+ /* We don't need this anymore. */
+ mach_port_deallocate (mach_task_self (), parent_task);
+ parent_task = MACH_PORT_NULL;
+
+ proc_mark_exec (parent_proc);
+
+ /* Give our parent (the real bootstrap filesystem) an fsys_init
+ RPC of its own, as init would have sent it. */
+ err = task_get_bootstrap_port (mach_task_self (), &bootstrap);
+ assert_perror (err);
+ err = fsys_init (bootstrap, parent_proc, MACH_MSG_TYPE_COPY_SEND,
+ authhandle);
+ mach_port_deallocate (mach_task_self (), parent_proc);
+ mach_port_deallocate (mach_task_self (), bootstrap);
+ assert_perror (err);
+ }
+
+ /* Get a port to the root directory to put in the library's
+ data structures. */
+ err = diskfs_make_peropen (diskfs_root_node, O_READ|O_EXEC, 0, &rootpo);
+ assert_perror (err);
+ err = diskfs_create_protid (rootpo, 0, &rootpi);
+ assert_perror (err);
+ root_pt = ports_get_send_right (rootpi);
+ ports_port_deref (rootpi);
+
+ /* We need two send rights, for the crdir and cwdir slots. */
+ mach_port_mod_refs (mach_task_self (), root_pt,
+ MACH_PORT_RIGHT_SEND, +1);
+
+ if (_hurd_ports)
+ {
+ /* We already have a portarray, because somebody responded to
+ exec_startup on our initial bootstrap port, even though we are
+ supposedly the bootstrap program. The losing `boot' that runs on
+ UX does this. */
+ _hurd_port_set (&_hurd_ports[INIT_PORT_PROC], procserver); /* Consume. */
+ _hurd_port_set (&_hurd_ports[INIT_PORT_AUTH], authhandle); /* Consume. */
+ _hurd_port_set (&_hurd_ports[INIT_PORT_CRDIR], root_pt); /* Consume. */
+ _hurd_port_set (&_hurd_ports[INIT_PORT_CWDIR], root_pt); /* Consume. */
+ _hurd_proc_init (diskfs_argv, NULL, 0);
+ }
+ else
+ {
+ /* We have no portarray or intarray because there was
+ no exec_startup data; _hurd_init was never called.
+ We now have the crucial ports, so create a portarray
+ and call _hurd_init. */
+ mach_port_t *portarray;
+ unsigned int i;
+ portarray = mmap (0, INIT_PORT_MAX * sizeof *portarray,
+ PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ if (MACH_PORT_NULL != (mach_port_t) 0)
+ for (i = 0; i < INIT_PORT_MAX; ++i)
+ portarray[i] = MACH_PORT_NULL;
+ portarray[INIT_PORT_PROC] = procserver;
+ portarray[INIT_PORT_AUTH] = authhandle;
+ portarray[INIT_PORT_CRDIR] = root_pt;
+ portarray[INIT_PORT_CWDIR] = root_pt;
+ _hurd_init (0, diskfs_argv, portarray, INIT_PORT_MAX, NULL, 0);
+ }
+
+ err = get_privileged_ports (&host, 0);
+ if (err)
+ return err;
+
+ proc_register_version (procserver, host, diskfs_server_name, "",
+ diskfs_server_version);
+
+ err = proc_getmsgport (procserver, HURD_PID_STARTUP, &startup);
+ if (!err)
+ {
+ startup_essential_task (startup, mach_task_self (), MACH_PORT_NULL,
+ diskfs_server_name, host);
+ mach_port_deallocate (mach_task_self (), startup);
+ }
+
+ mach_port_deallocate (mach_task_self (), host);
+ mach_port_deallocate (mach_task_self (), procserver);
+
+ _diskfs_init_completed ();
+
+ return MIG_NO_REPLY; /* Already replied above. */
+}
+
+/* Start the execserver running (when we are a bootstrap filesystem). */
+static void
+start_execserver (void)
+{
+ error_t err;
+ mach_port_t right;
+ extern task_t diskfs_exec_server_task; /* Set in opts-std-startup.c. */
+ struct port_info *execboot_info;
+
+ assert (diskfs_exec_server_task != MACH_PORT_NULL);
+
+ err = ports_create_port (diskfs_execboot_class, diskfs_port_bucket,
+ sizeof (struct port_info), &execboot_info);
+ assert_perror (err);
+ right = ports_get_send_right (execboot_info);
+ ports_port_deref (execboot_info);
+ task_set_special_port (diskfs_exec_server_task, TASK_BOOTSTRAP_PORT, right);
+ mach_port_deallocate (mach_task_self (), right);
+
+ if (_diskfs_boot_pause)
+ {
+ printf ("pausing for exec\n");
+ getc (stdin);
+ }
+ task_resume (diskfs_exec_server_task);
+
+ printf (" exec");
+ fflush (stdout);
+}
diff --git a/libdiskfs/conch-fetch.c b/libdiskfs/conch-fetch.c
new file mode 100644
index 00000000..43a0e870
--- /dev/null
+++ b/libdiskfs/conch-fetch.c
@@ -0,0 +1,71 @@
+/*
+ Copyright (C) 1994, 1995, 1996, 1999 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include <hurd/iohelp.h>
+
+/* Update our copy of the relevant fields from a shared page. Callers
+ must have the share lock on the shared page as well as the inode
+ toplock. This is called by the conch management facilities of
+ libioserver as well as by us. */
+void
+iohelp_fetch_shared_data (void *arg)
+{
+ struct protid *cred = arg;
+ int mod = 0;
+
+ /* Don't allow the user to grow the file past the alloc size. */
+ if (cred->mapped->file_size > cred->po->np->allocsize)
+ cred->mapped->file_size = cred->po->np->allocsize;
+
+ /* Don't allow the user to truncate the file this way. */
+ if (cred->mapped->file_size < cred->po->np->dn_stat.st_size)
+ cred->mapped->file_size = cred->po->np->dn_stat.st_size;
+ else if (cred->po->np->dn_stat.st_size != cred->mapped->file_size)
+ {
+ /* The user can validly set the size, but block the attempt
+ if we are readonly. */
+ if (diskfs_check_readonly ())
+ cred->mapped->file_size = cred->po->np->dn_stat.st_size;
+ else
+ {
+ cred->po->np->dn_stat.st_size = cred->mapped->file_size;
+ cred->po->np->dn_set_ctime = 1;
+ mod = 1;
+ }
+ }
+
+ cred->po->filepointer = cred->mapped->xx_file_pointer;
+
+ if (!diskfs_check_readonly ())
+ {
+ if (cred->mapped->written)
+ {
+ cred->po->np->dn_set_mtime = 1;
+ mod = 1;
+ }
+ if (cred->mapped->accessed && ! _diskfs_noatime)
+ {
+ cred->po->np->dn_set_atime = 1;
+ mod = 1;
+ }
+ }
+ cred->mapped->written = 0;
+ cred->mapped->accessed = 0;
+ if (diskfs_synchronous && mod)
+ diskfs_node_update (cred->po->np, 1);
+}
diff --git a/libdiskfs/conch-set.c b/libdiskfs/conch-set.c
new file mode 100644
index 00000000..86d3905d
--- /dev/null
+++ b/libdiskfs/conch-set.c
@@ -0,0 +1,49 @@
+/*
+ Copyright (C) 1994, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include <hurd/iohelp.h>
+#include <fcntl.h>
+
+/* Write current values into the shared page. Callers must have the
+ share lock on the shared page, as well as the inode toplock.
+ This is called by the conch management facilities of libioserver
+ as well as by us. */
+void
+iohelp_put_shared_data (void *arg)
+{
+ struct protid *cred = arg;
+
+ cred->mapped->append_mode = (cred->po->openstat & O_APPEND);
+ cred->mapped->eof_notify = 0;
+ cred->mapped->do_sigio = (cred->po->openstat & O_FSYNC);
+ cred->mapped->use_file_size = 1;
+ cred->mapped->use_read_size = 0;
+ cred->mapped->optimal_transfer_size = cred->po->np->dn_stat.st_blksize;
+ cred->mapped->seekable = 1;
+ cred->mapped->use_prenotify_size = 1;
+ cred->mapped->use_postnotify_size = 0;
+ cred->mapped->use_readnotify_size = 0;
+ cred->mapped->prenotify_size = cred->po->np->allocsize;
+
+ cred->mapped->xx_file_pointer = cred->po->filepointer;
+ cred->mapped->rd_file_pointer = -1;
+ cred->mapped->wr_file_pointer = -1;
+ cred->mapped->file_size = cred->po->np->dn_stat.st_size;
+ cred->mapped->written = 0;
+ cred->mapped->accessed = 0;
+}
diff --git a/libdiskfs/console.c b/libdiskfs/console.c
new file mode 100644
index 00000000..a4c3a1af
--- /dev/null
+++ b/libdiskfs/console.c
@@ -0,0 +1,71 @@
+/* Redirect stdio to the console if possible
+
+ Copyright (C) 1995,96,98,99,2001 Free Software Foundation, Inc.
+ Written by Miles Bader <miles@gnu.org>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <assert.h>
+
+#include <mach/mach.h>
+#include <device/device.h>
+#include <hurd.h>
+
+#include "priv.h"
+
+/* Make sure errors go somewhere reasonable. */
+void
+diskfs_console_stdio ()
+{
+ if (getpid () > 0)
+ {
+ if (write (2, "", 0) == 0)
+ /* We have a working stderr from our parent (e.g. settrans -a).
+ Just use it. */
+ dup2 (2, 1);
+ else
+ {
+ int fd = open ("/dev/console", O_RDWR);
+
+ dup2 (fd, 0);
+ dup2 (fd, 1);
+ dup2 (fd, 2);
+ if (fd > 2)
+ close (fd);
+ }
+ }
+ else
+ {
+ mach_port_t dev, cons;
+ error_t err;
+ if (diskfs_boot_filesystem ())
+ _diskfs_boot_privports ();
+ err = get_privileged_ports (NULL, &dev);
+ assert_perror (err);
+ err = device_open (dev, D_READ|D_WRITE, "console", &cons);
+ mach_port_deallocate (mach_task_self (), dev);
+ assert_perror (err);
+ stdin = mach_open_devstream (cons, "r");
+ stdout = stderr = mach_open_devstream (cons, "w");
+ mach_port_deallocate (mach_task_self (), cons);
+ }
+}
diff --git a/libdiskfs/dead-name.c b/libdiskfs/dead-name.c
new file mode 100644
index 00000000..6ca208e4
--- /dev/null
+++ b/libdiskfs/dead-name.c
@@ -0,0 +1,47 @@
+/* Handle dead name notifications on ports
+ Copyright (C) 1996 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+void
+ports_dead_name (void *notify, mach_port_t dead_name)
+{
+ struct protid *pi = ports_lookup_port (diskfs_port_bucket, dead_name,
+ diskfs_protid_class);
+ struct node *np;
+
+ if (pi)
+ {
+ np = pi->po->np;
+ pthread_mutex_lock (&np->lock);
+ if (dead_name == np->sockaddr)
+ {
+ mach_port_deallocate (mach_task_self (), np->sockaddr);
+ np->sockaddr = MACH_PORT_NULL;
+ diskfs_nput (np);
+ }
+ else
+ pthread_mutex_unlock (&np->lock);
+ }
+
+ fshelp_remove_active_translator (dead_name);
+
+ ports_interrupt_notified_rpcs (notify, dead_name, MACH_NOTIFY_DEAD_NAME);
+}
diff --git a/libdiskfs/demuxer.c b/libdiskfs/demuxer.c
new file mode 100644
index 00000000..4a1c4fb4
--- /dev/null
+++ b/libdiskfs/demuxer.c
@@ -0,0 +1,49 @@
+/* Demultiplexer for diskfs library
+ Copyright (C) 1994, 1995, 1996, 2013 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+#include "io_S.h"
+#include "fs_S.h"
+#include "../libports/notify_S.h"
+#include "fsys_S.h"
+#include "../libports/interrupt_S.h"
+#include "ifsock_S.h"
+#include "startup_notify_S.h"
+#include "exec_startup_S.h"
+
+int
+diskfs_demuxer (mach_msg_header_t *inp,
+ mach_msg_header_t *outp)
+{
+ mig_routine_t routine;
+ if ((routine = diskfs_io_server_routine (inp)) ||
+ (routine = diskfs_fs_server_routine (inp)) ||
+ (routine = ports_notify_server_routine (inp)) ||
+ (routine = diskfs_fsys_server_routine (inp)) ||
+ (routine = ports_interrupt_server_routine (inp)) ||
+ (diskfs_shortcut_ifsock ?
+ (routine = diskfs_ifsock_server_routine (inp)) : 0) ||
+ (routine = diskfs_startup_notify_server_routine (inp)) ||
+ (routine = diskfs_exec_startup_server_routine (inp)))
+ {
+ (*routine) (inp, outp);
+ return TRUE;
+ }
+ else
+ return FALSE;
+}
diff --git a/libdiskfs/dir-chg.c b/libdiskfs/dir-chg.c
new file mode 100644
index 00000000..ed8f40c1
--- /dev/null
+++ b/libdiskfs/dir-chg.c
@@ -0,0 +1,82 @@
+/* Notifications of directory changes.
+ Copyright (C) 1994, 1995, 1998, 2001 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include "fs_notify_U.h"
+
+kern_return_t
+diskfs_S_dir_notice_changes (struct protid *cred,
+ mach_port_t notify)
+{
+ error_t err;
+ struct modreq *req;
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ pthread_mutex_lock (&np->lock);
+ if (!S_ISDIR (np->dn_stat.st_mode))
+ {
+ pthread_mutex_unlock (&np->lock);
+ return ENOTDIR;
+ }
+ err = dir_changed (notify, np->dirmod_tick, DIR_CHANGED_NULL, "");
+ if (err)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return err;
+ }
+ req = malloc (sizeof (struct modreq));
+ if (! req)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return ENOMEM;
+ }
+ req->port = notify;
+ req->next = np->dirmod_reqs;
+ np->dirmod_reqs = req;
+ pthread_mutex_unlock (&np->lock);
+ return 0;
+}
+
+void
+diskfs_notice_dirchange (struct node *dp, enum dir_changed_type type,
+ const char *name)
+{
+ error_t err;
+ struct modreq **preq;
+
+ dp->dirmod_tick++;
+ preq = &dp->dirmod_reqs;
+ while (*preq)
+ {
+ struct modreq *req = *preq;
+ err = dir_changed (req->port, dp->dirmod_tick, type, name);
+ if (err && err != MACH_SEND_TIMED_OUT)
+ {
+ /* Remove notify port. */
+ *preq = req->next;
+ mach_port_deallocate (mach_task_self (), req->port);
+ free (req);
+ }
+ else
+ preq = &req->next;
+ }
+}
diff --git a/libdiskfs/dir-clear.c b/libdiskfs/dir-clear.c
new file mode 100644
index 00000000..7cf32358
--- /dev/null
+++ b/libdiskfs/dir-clear.c
@@ -0,0 +1,70 @@
+/*
+ Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+/* Clear the `.' and `..' entries from directory DP. Its parent is PDP,
+ and the user responsible for this is identified by CRED. Both
+ directories must be locked. */
+error_t
+diskfs_clear_directory (struct node *dp,
+ struct node *pdp,
+ struct protid *cred)
+{
+ error_t err;
+ struct dirstat *ds = alloca (diskfs_dirstat_size);
+ struct node *np;
+
+ /* Find and remove the `.' entry. */
+ err = diskfs_lookup (dp, ".", REMOVE, &np, ds, cred);
+ assert (err != ENOENT);
+ if (!err)
+ {
+ assert (np == dp);
+ err = diskfs_dirremove (dp, np, ".", ds);
+ diskfs_nrele (np);
+ }
+ else
+ diskfs_drop_dirstat (dp, ds);
+ if (err)
+ return err;
+
+ /* Decrement the link count */
+ dp->dn_stat.st_nlink--;
+ dp->dn_set_ctime = 1;
+
+ /* Find and remove the `..' entry. */
+ err = diskfs_lookup (dp, "..", REMOVE | SPEC_DOTDOT, &np, ds, cred);
+ assert (err != ENOENT);
+ if (!err)
+ {
+ assert (np == pdp);
+ err = diskfs_dirremove (dp, np, "..", ds);
+ }
+ else
+ diskfs_drop_dirstat (dp, ds);
+ if (err)
+ return err;
+
+ /* Decrement the link count on the parent */
+ pdp->dn_stat.st_nlink--;
+ pdp->dn_set_ctime = 1;
+
+ diskfs_truncate (dp, 0);
+
+ return err;
+}
diff --git a/libdiskfs/dir-init.c b/libdiskfs/dir-init.c
new file mode 100644
index 00000000..4efded07
--- /dev/null
+++ b/libdiskfs/dir-init.c
@@ -0,0 +1,68 @@
+/*
+ Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+/* Locked node DP is a new directory; add whatever links are necessary
+ to give it structure; its parent is the (locked) node PDP.
+ This routine may not call diskfs_lookup on PDP. The new directory
+ must be clear within the meaning of diskfs_dirempty.
+ CRED identifies the user making the call. */
+error_t
+diskfs_init_dir (struct node *dp, struct node *pdp, struct protid *cred)
+{
+ struct dirstat *ds = alloca (diskfs_dirstat_size);
+ struct node *foo;
+ error_t err;
+
+ /* Fabricate a protid that represents root credentials. */
+ static uid_t zero = 0;
+ static struct idvec vec = {&zero, 1, 1};
+ static struct iouser user = {&vec, &vec, 0};
+ struct protid lookupcred = {{0, 0, 0, 0}, &user, cred->po, 0, 0};
+
+ /* New links */
+ if (pdp->dn_stat.st_nlink == diskfs_link_max - 1)
+ return EMLINK;
+
+ dp->dn_stat.st_nlink++; /* for `.' */
+ dp->dn_set_ctime = 1;
+ err = diskfs_lookup (dp, ".", CREATE, &foo, ds, &lookupcred);
+ assert (err == ENOENT);
+ err = diskfs_direnter (dp, ".", dp, ds, cred);
+ if (err)
+ {
+ dp->dn_stat.st_nlink--;
+ dp->dn_set_ctime = 1;
+ return err;
+ }
+
+ pdp->dn_stat.st_nlink++; /* for `..' */
+ pdp->dn_set_ctime = 1;
+ err = diskfs_lookup (dp, "..", CREATE, &foo, ds, &lookupcred);
+ assert (err == ENOENT);
+ err = diskfs_direnter (dp, "..", pdp, ds, cred);
+ if (err)
+ {
+ pdp->dn_stat.st_nlink--;
+ pdp->dn_set_ctime = 1;
+ return err;
+ }
+
+ diskfs_node_update (dp, diskfs_synchronous);
+ return 0;
+}
diff --git a/libdiskfs/dir-link.c b/libdiskfs/dir-link.c
new file mode 100644
index 00000000..ca5dd561
--- /dev/null
+++ b/libdiskfs/dir-link.c
@@ -0,0 +1,133 @@
+/* libdiskfs implementation of fs.defs: dir_link
+ Copyright (C) 1992,93,94,95,96,97,99 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement dir_link as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_dir_link (struct protid *dircred,
+ struct protid *filecred,
+ char *name,
+ int excl)
+{
+ struct node *np; /* node being linked */
+ struct node *tnp; /* node being deleted implicitly */
+ struct node *dnp; /* directory of new entry */
+ struct dirstat *ds = alloca (diskfs_dirstat_size);
+ error_t err;
+
+ if (!dircred)
+ return EOPNOTSUPP;
+
+ if (diskfs_check_readonly ())
+ return EROFS;
+
+ if (!filecred)
+ return EXDEV;
+
+ np = filecred->po->np;
+ pthread_mutex_lock (&np->lock);
+ if (S_ISDIR (np->dn_stat.st_mode))
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EPERM;
+ }
+ pthread_mutex_unlock (&np->lock);
+
+ dnp = dircred->po->np;
+ pthread_mutex_lock (&dnp->lock);
+
+ /* Lookup new location */
+ err = diskfs_lookup (dnp, name, RENAME, &tnp, ds, dircred);
+ if (!err && excl)
+ {
+ err = EEXIST;
+ diskfs_nput (tnp);
+ }
+ if (err && err != ENOENT)
+ {
+ if (err == EAGAIN)
+ err = EINVAL;
+ diskfs_drop_dirstat (dnp, ds);
+ pthread_mutex_unlock (&dnp->lock);
+ return err;
+ }
+
+ if (np == tnp)
+ {
+ diskfs_drop_dirstat (dnp, ds);
+ pthread_mutex_unlock (&dnp->lock);
+ pthread_mutex_unlock (&tnp->lock);
+ mach_port_deallocate (mach_task_self (), filecred->pi.port_right);
+ return 0;
+ }
+
+ if (tnp && S_ISDIR (tnp->dn_stat.st_mode))
+ {
+ diskfs_drop_dirstat (dnp, ds);
+ pthread_mutex_unlock (&dnp->lock);
+ pthread_mutex_unlock (&tnp->lock);
+ return EISDIR;
+ }
+
+ /* Create new entry for NP */
+
+ /* This is safe because NP is not a directory (thus not DNP) and
+ not TNP and is a leaf. */
+ pthread_mutex_lock (&np->lock);
+
+ /* Increment link count */
+ if (np->dn_stat.st_nlink == diskfs_link_max - 1)
+ {
+ diskfs_drop_dirstat (dnp, ds);
+ pthread_mutex_unlock (&np->lock);
+ pthread_mutex_unlock (&dnp->lock);
+ return EMLINK;
+ }
+ np->dn_stat.st_nlink++;
+ np->dn_set_ctime = 1;
+ diskfs_node_update (np, diskfs_synchronous);
+
+ /* Attach it */
+ if (tnp)
+ {
+ assert (!excl);
+ err = diskfs_dirrewrite (dnp, tnp, np, name, ds);
+ if (!err)
+ {
+ /* Deallocate link on TNP */
+ tnp->dn_stat.st_nlink--;
+ tnp->dn_set_ctime = 1;
+ if (diskfs_synchronous)
+ diskfs_node_update (tnp, 1);
+ }
+ diskfs_nput (tnp);
+ }
+ else
+ err = diskfs_direnter (dnp, name, np, ds, dircred);
+
+ if (diskfs_synchronous)
+ diskfs_node_update (dnp, 1);
+
+ pthread_mutex_unlock (&dnp->lock);
+ pthread_mutex_unlock (&np->lock);
+ if (!err)
+ /* MiG won't do this for us, which it ought to. */
+ mach_port_deallocate (mach_task_self (), filecred->pi.port_right);
+ return err;
+}
diff --git a/libdiskfs/dir-lookup.c b/libdiskfs/dir-lookup.c
new file mode 100644
index 00000000..3950bf9c
--- /dev/null
+++ b/libdiskfs/dir-lookup.c
@@ -0,0 +1,561 @@
+/* libdiskfs implementation of fs.defs:dir_lookup
+ Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ 2002, 2008, 2013, 2014 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/file.h>
+#include <hurd/fsys.h>
+#include <hurd/paths.h>
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement dir_lookup as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_dir_lookup (struct protid *dircred,
+ char *path,
+ int flags,
+ mode_t mode,
+ enum retry_type *retry,
+ char *retryname,
+ file_t *returned_port,
+ mach_msg_type_name_t *returned_port_poly)
+{
+ struct node *dnp;
+ struct node *np;
+ int nsymlink = 0;
+ char *nextname;
+ char *relpath;
+ int nextnamelen;
+ error_t error = 0;
+ char *pathbuf = 0;
+ int pathbuflen = 0;
+ int newnamelen;
+ int create, excl;
+ int lastcomp = 0;
+ int newnode = 0;
+ struct dirstat *ds = 0;
+ int mustbedir = 0;
+ size_t amt;
+ int type;
+ struct protid *newpi = 0;
+ struct peropen *newpo = 0;
+
+ if (!dircred)
+ return EOPNOTSUPP;
+
+ flags &= O_HURD;
+
+ create = (flags & O_CREAT);
+ excl = (flags & O_EXCL);
+
+ /* Skip leading slashes */
+ while (path[0] == '/')
+ path++;
+
+ /* Preserve the path relative to diruser->po->path. */
+ relpath = strdup (path);
+ if (! relpath)
+ return ENOMEM;
+
+ /* Keep a pointer to the start of the path for length
+ calculations. */
+ char *path_start = path;
+
+ *returned_port_poly = MACH_MSG_TYPE_MAKE_SEND;
+ *retry = FS_RETRY_NORMAL;
+ retryname[0] = '\0';
+
+ if (path[0] == '\0')
+ {
+ /* Set things up in the state expected by the code from gotit: on. */
+ dnp = 0;
+ np = dircred->po->np;
+ pthread_mutex_lock (&np->lock);
+ diskfs_nref (np);
+ goto gotit;
+ }
+
+ dnp = dircred->po->np;
+
+ pthread_mutex_lock (&dnp->lock);
+ np = 0;
+
+ diskfs_nref (dnp); /* acquire a reference for later diskfs_nput */
+
+ do
+ {
+ assert (!lastcomp);
+
+ /* Find the name of the next pathname component */
+ nextname = index (path, '/');
+
+ if (nextname)
+ {
+ *nextname++ = '\0';
+ while (*nextname == '/')
+ nextname++;
+ if (*nextname == '\0')
+ {
+ /* These are the rules for filenames ending in /. */
+ nextname = 0;
+ lastcomp = 1;
+ mustbedir = 1;
+ create = 0;
+ }
+ else
+ lastcomp = 0;
+ }
+ else
+ lastcomp = 1;
+
+ np = 0;
+
+ /* diskfs_lookup the next pathname component */
+ if (lastcomp && create)
+ {
+ if (!ds)
+ ds = alloca (diskfs_dirstat_size);
+ error = diskfs_lookup (dnp, path, CREATE, &np, ds, dircred);
+ }
+ else
+ error = diskfs_lookup (dnp, path, LOOKUP, &np, 0, dircred);
+
+ if (lastcomp && create && excl && (!error || error == EAGAIN))
+ error = EEXIST;
+
+ /* If we get an error we're done */
+ if (error == EAGAIN)
+ {
+ if (dnp == dircred->po->shadow_root)
+ /* We're at the root of a shadow tree. */
+ {
+ if (dircred->po->shadow_root_parent == MACH_PORT_NULL)
+ {
+ /* This is a shadow root with no parent, meaning
+ we should treat it as a virtual root disconnected
+ from its real .. directory. */
+ error = 0;
+ np = dnp;
+ diskfs_nref (np);
+ }
+ else
+ {
+ /* Punt the client up to the shadow root parent. */
+ *retry = FS_RETRY_REAUTH;
+ *returned_port = dircred->po->shadow_root_parent;
+ *returned_port_poly = MACH_MSG_TYPE_COPY_SEND;
+ if (! lastcomp)
+ strcpy (retryname, nextname);
+ error = 0;
+ goto out;
+ }
+ }
+ else if (dircred->po->root_parent != MACH_PORT_NULL)
+ /* We're at a real translator root; even if DIRCRED->po has a
+ shadow root, we can get here if its in a directory that was
+ renamed out from under it... */
+ {
+ *retry = FS_RETRY_REAUTH;
+ *returned_port = dircred->po->root_parent;
+ *returned_port_poly = MACH_MSG_TYPE_COPY_SEND;
+ if (!lastcomp)
+ strcpy (retryname, nextname);
+ error = 0;
+ goto out;
+ }
+ else
+ /* We're at a REAL root, as in there's no way up from here. */
+ {
+ error = 0;
+ np = dnp;
+ diskfs_nref (np);
+ }
+ }
+
+ /* Create the new node if necessary */
+ if (lastcomp && create)
+ {
+ if (error == ENOENT)
+ {
+ mode &= ~(S_IFMT | S_ISPARE | S_ISVTX | S_ITRANS);
+ mode |= S_IFREG;
+ error = diskfs_create_node (dnp, path, mode, &np, dircred, ds);
+ if (diskfs_synchronous)
+ {
+ diskfs_file_update (dnp, 1);
+ diskfs_file_update (np, 1);
+ }
+ newnode = 1;
+ }
+ else
+ diskfs_drop_dirstat (dnp, ds);
+ }
+
+ if (error)
+ goto out;
+
+ /* If this is translated, start the translator (if necessary)
+ and return. */
+ if ((((flags & O_NOTRANS) == 0) || !lastcomp)
+ && ((np->dn_stat.st_mode & S_IPTRANS)
+ || S_ISFIFO (np->dn_stat.st_mode)
+ || S_ISCHR (np->dn_stat.st_mode)
+ || S_ISBLK (np->dn_stat.st_mode)
+ || fshelp_translated (&np->transbox)))
+ {
+ mach_port_t dirport;
+ struct iouser *user;
+
+ /* A callback function for short-circuited translators.
+ Symlink & ifsock are handled elsewhere. */
+ error_t short_circuited_callback1 (void *cookie1, void *cookie2,
+ uid_t *uid, gid_t *gid,
+ char **argz, size_t *argz_len)
+ {
+ struct node *node = cookie1;
+
+ switch (node->dn_stat.st_mode & S_IFMT)
+ {
+ case S_IFCHR:
+ case S_IFBLK:
+ asprintf (argz, "%s%c%d%c%d",
+ (S_ISCHR (node->dn_stat.st_mode)
+ ? _HURD_CHRDEV : _HURD_BLKDEV),
+ 0, major (node->dn_stat.st_rdev),
+ 0, minor (node->dn_stat.st_rdev));
+ *argz_len = strlen (*argz) + 1;
+ *argz_len += strlen (*argz + *argz_len) + 1;
+ *argz_len += strlen (*argz + *argz_len) + 1;
+ break;
+ case S_IFIFO:
+ asprintf (argz, "%s", _HURD_FIFO);
+ *argz_len = strlen (*argz) + 1;
+ break;
+ default:
+ return ENOENT;
+ }
+
+ *uid = node->dn_stat.st_uid;
+ *gid = node->dn_stat.st_gid;
+
+ return 0;
+ }
+
+ /* Create an unauthenticated port for DNP, and then
+ unlock it. */
+ error = iohelp_create_empty_iouser (&user);
+ if (! error)
+ {
+ error = diskfs_make_peropen (dnp, 0, dircred->po, &newpo);
+ if (! error)
+ {
+ error = diskfs_create_protid (newpo, user, &newpi);
+ if (! error)
+ newpo = 0;
+ }
+
+ iohelp_free_iouser (user);
+ }
+
+ if (error)
+ goto out;
+
+ dirport = ports_get_send_right (newpi);
+ if (np != dnp)
+ pthread_mutex_unlock (&dnp->lock);
+
+ /* Check if an active translator is currently running. If
+ not, fshelp_fetch_root will start one. In that case, we
+ need to register it in the list of active
+ translators. */
+ boolean_t register_translator =
+ np->transbox.active == MACH_PORT_NULL;
+
+ error = fshelp_fetch_root (&np->transbox, dircred->po,
+ dirport, dircred->user,
+ lastcomp ? flags : 0,
+ ((np->dn_stat.st_mode & S_IPTRANS)
+ ? _diskfs_translator_callback1
+ : short_circuited_callback1),
+ _diskfs_translator_callback2,
+ retry, retryname, returned_port);
+
+ /* fetch_root copies DIRPORT for success, so we always should
+ deallocate our send right. */
+ mach_port_deallocate (mach_task_self (), dirport);
+
+ if (error != ENOENT)
+ {
+ *returned_port_poly = MACH_MSG_TYPE_MOVE_SEND;
+ if (!lastcomp && !error)
+ {
+ char *end = strchr (retryname, '\0');
+ *end++ = '/';
+ strcpy (end, nextname);
+ }
+
+ if (register_translator)
+ {
+ char *translator_path = strdupa (relpath);
+ if (nextname != NULL)
+ {
+ /* This was not the last path component.
+ NEXTNAME points to the next component, locate
+ the end of the current component and use it
+ to trim TRANSLATOR_PATH. */
+ char *end = nextname;
+ while (*end != 0)
+ end--;
+ translator_path[end - path_start] = '\0';
+ }
+
+ error = fshelp_set_active_translator (&newpi->pi,
+ translator_path,
+ np->transbox.active);
+ if (error)
+ goto out;
+ }
+
+ goto out;
+ }
+
+ ports_port_deref (newpi);
+ newpi = NULL;
+
+ /* ENOENT means there was a hiccup, and the translator
+ vanished while NP was unlocked inside fshelp_fetch_root.
+ Reacquire the locks, and continue as normal. */
+ error = 0;
+ if (np != dnp)
+ {
+ if (!strcmp (path, ".."))
+ pthread_mutex_lock (&dnp->lock);
+ else
+ {
+ if (pthread_mutex_trylock (&dnp->lock))
+ {
+ pthread_mutex_unlock (&np->lock);
+ pthread_mutex_lock (&dnp->lock);
+ pthread_mutex_lock (&np->lock);
+ }
+ }
+ }
+ }
+
+ if (S_ISLNK (np->dn_stat.st_mode)
+ && (!lastcomp
+ || mustbedir /* "foo/" must see that foo points to a dir */
+ || !(flags & (O_NOLINK|O_NOTRANS))))
+ {
+ /* Handle symlink interpretation */
+
+ if (nsymlink++ > diskfs_maxsymlinks)
+ {
+ error = ELOOP;
+ goto out;
+ }
+
+ nextnamelen = nextname ? strlen (nextname) + 1 : 0;
+ newnamelen = nextnamelen + np->dn_stat.st_size + 1 + 1;
+ if (pathbuflen < newnamelen)
+ {
+ pathbuf = alloca (newnamelen);
+ pathbuflen = newnamelen;
+ }
+
+ if (diskfs_read_symlink_hook)
+ error = (*diskfs_read_symlink_hook)(np, pathbuf);
+ if (!diskfs_read_symlink_hook || error == EINVAL)
+ {
+ error = diskfs_node_rdwr (np, pathbuf,
+ 0, np->dn_stat.st_size, 0,
+ dircred, &amt);
+ if (!error)
+ assert (amt == np->dn_stat.st_size);
+ }
+ if (error)
+ goto out;
+
+ if (np->dn_stat.st_size == 0) /* symlink to "" */
+ path = nextname;
+ else
+ {
+ if (nextname)
+ {
+ pathbuf[np->dn_stat.st_size] = '/';
+ memcpy (pathbuf + np->dn_stat.st_size + 1,
+ nextname, nextnamelen - 1);
+ }
+ pathbuf[nextnamelen + np->dn_stat.st_size] = '\0';
+
+ if (pathbuf[0] == '/')
+ {
+ /* Punt to the caller. */
+ *retry = FS_RETRY_MAGICAL;
+ *returned_port = MACH_PORT_NULL;
+ memcpy (retryname, pathbuf,
+ nextnamelen + np->dn_stat.st_size + 1);
+ if (mustbedir)
+ {
+ retryname[nextnamelen + np->dn_stat.st_size] = '/';
+ retryname[nextnamelen + np->dn_stat.st_size + 1] = '\0';
+ }
+ goto out;
+ }
+
+ path = pathbuf;
+ }
+
+ if (lastcomp)
+ lastcomp = 0;
+
+ diskfs_nput (np);
+ np = 0;
+
+ if (path == 0) /* symlink to "" was the last component */
+ {
+ np = dnp;
+ dnp = 0;
+ break;
+ }
+ }
+ else
+ {
+ /* Handle normal nodes */
+ path = nextname;
+ if (np == dnp)
+ diskfs_nrele (dnp);
+ else
+ diskfs_nput (dnp);
+ if (!lastcomp)
+ {
+ dnp = np;
+ np = 0;
+ }
+ else
+ dnp = 0;
+ }
+ } while (path && *path);
+
+ /* At this point, np is the node to return. If newnode is set, then
+ we just created this node. */
+
+ gotit:
+ type = np->dn_stat.st_mode & S_IFMT;
+
+ if (mustbedir && type != S_IFDIR)
+ {
+ error = ENOTDIR;
+ goto out;
+ }
+
+ if (!newnode)
+ /* Check permissions on existing nodes, but not new ones. */
+ {
+ if (((type == S_IFSOCK || type == S_IFBLK || type == S_IFCHR ||
+ type == S_IFIFO)
+ && (flags & (O_READ|O_WRITE|O_EXEC)))
+ || (type == S_IFLNK && (flags & (O_WRITE|O_EXEC))))
+ error = EACCES;
+
+ if (!error && (flags & O_READ))
+ error = fshelp_access (&np->dn_stat, S_IREAD, dircred->user);
+
+ if (!error && (flags & O_EXEC))
+ error = fshelp_access (&np->dn_stat, S_IEXEC, dircred->user);
+
+ if (!error && (flags & O_WRITE))
+ {
+ if (type == S_IFDIR)
+ error = EISDIR;
+ else if (diskfs_check_readonly ())
+ error = EROFS;
+ else
+ error = fshelp_access (&np->dn_stat, S_IWRITE, dircred->user);
+ }
+
+ if (error)
+ goto out;
+ }
+
+ if ((flags & O_NOATIME)
+ && (fshelp_isowner (&np->dn_stat, dircred->user) == EPERM))
+ flags &= ~O_NOATIME;
+
+ error = diskfs_make_peropen (np, (flags &~OPENONLY_STATE_MODES),
+ dircred->po, &newpo);
+
+ if (! error)
+ error = diskfs_create_protid (newpo, dircred->user, &newpi);
+
+ if (! error)
+ {
+ newpo = 0;
+ if (flags & O_EXLOCK)
+ error = fshelp_acquire_lock (&np->userlock, &newpi->po->lock_status,
+ &np->lock, LOCK_EX);
+ else if (flags & O_SHLOCK)
+ error = fshelp_acquire_lock (&np->userlock, &newpi->po->lock_status,
+ &np->lock, LOCK_SH);
+ }
+
+ if (! error)
+ {
+ free (newpi->po->path);
+ if (dircred->po->path == NULL)
+ {
+ /* dircred is the root directory. */
+ newpi->po->path = relpath;
+ relpath = NULL; /* Do not free relpath. */
+ }
+ else
+ {
+ newpi->po->path = NULL;
+ asprintf (&newpi->po->path, "%s/%s", dircred->po->path, relpath);
+ }
+
+ if (! newpi->po->path)
+ error = errno;
+
+ *returned_port = ports_get_right (newpi);
+ ports_port_deref (newpi);
+ newpi = 0;
+ }
+
+ out:
+ if (np)
+ {
+ if (dnp == np)
+ diskfs_nrele (np);
+ else
+ diskfs_nput (np);
+ }
+ if (dnp)
+ diskfs_nput (dnp);
+
+ if (newpi)
+ ports_port_deref (newpi);
+ if (newpo)
+ diskfs_release_peropen (newpo);
+
+ free (relpath);
+
+ return error;
+}
diff --git a/libdiskfs/dir-mkdir.c b/libdiskfs/dir-mkdir.c
new file mode 100644
index 00000000..1ddb8087
--- /dev/null
+++ b/libdiskfs/dir-mkdir.c
@@ -0,0 +1,70 @@
+/* libdiskfs implementation of fs.defs: dir_mkdir
+ Copyright (C) 1992,93,94,95,96,97,2002 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement dir_mkdir as found in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_dir_mkdir (struct protid *dircred,
+ char *name,
+ mode_t mode)
+{
+ struct node *dnp;
+ struct node *np = 0;
+ struct dirstat *ds = alloca (diskfs_dirstat_size);
+ int error;
+
+ if (!dircred)
+ return EOPNOTSUPP;
+
+ dnp = dircred->po->np;
+ if (diskfs_check_readonly ())
+ return EROFS;
+
+ pthread_mutex_lock (&dnp->lock);
+
+ error = diskfs_lookup (dnp, name, CREATE, 0, ds, dircred);
+ if (error == EAGAIN)
+ error = EEXIST;
+ if (!error)
+ error = EEXIST;
+
+ if (error != ENOENT)
+ {
+ diskfs_drop_dirstat (dnp, ds);
+ pthread_mutex_unlock (&dnp->lock);
+ return error;
+ }
+
+ mode &= ~(S_ISPARE | S_IFMT | S_ITRANS);
+ mode |= S_IFDIR;
+
+ error = diskfs_create_node (dnp, name, mode, &np, dircred, ds);
+
+ if (diskfs_synchronous)
+ {
+ diskfs_file_update (dnp, 1);
+ diskfs_file_update (np, 1);
+ }
+
+ if (!error)
+ diskfs_nput (np);
+
+ pthread_mutex_unlock (&dnp->lock);
+ return error;
+}
diff --git a/libdiskfs/dir-mkfile.c b/libdiskfs/dir-mkfile.c
new file mode 100644
index 00000000..a38d89a5
--- /dev/null
+++ b/libdiskfs/dir-mkfile.c
@@ -0,0 +1,91 @@
+/* libdiskfs implementation of fs.defs: dir_mkfile
+ Copyright (C) 1994,95,96,97,2002 Free Software Foundation, Inc.
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <fcntl.h>
+
+/* Implement dir_mkfile as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_dir_mkfile (struct protid *cred,
+ int flags,
+ mode_t mode,
+ mach_port_t *newnode,
+ mach_msg_type_name_t *newnodetype)
+{
+ struct node *dnp, *np;
+ error_t err;
+ struct protid *newpi;
+ struct peropen *newpo;
+
+ if (!cred)
+ return EOPNOTSUPP;
+ if (diskfs_check_readonly ())
+ return EROFS;
+ dnp = cred->po->np;
+ pthread_mutex_lock (&dnp->lock);
+ if (!S_ISDIR (dnp->dn_stat.st_mode))
+ {
+ pthread_mutex_unlock (&dnp->lock);
+ return ENOTDIR;
+ }
+ err = fshelp_access (&dnp->dn_stat, S_IWRITE, cred->user);
+ if (err)
+ {
+ pthread_mutex_unlock (&dnp->lock);
+ return err;
+ }
+
+ mode &= ~(S_IFMT | S_ISPARE | S_ISVTX | S_ITRANS);
+ mode |= S_IFREG;
+ err = diskfs_create_node (dnp, 0, mode, &np, cred, 0);
+ pthread_mutex_unlock (&dnp->lock);
+
+ if (diskfs_synchronous)
+ {
+ diskfs_file_update (dnp, 1);
+ diskfs_file_update (np, 1);
+ }
+
+ if (err)
+ return err;
+
+ flags &= ~OPENONLY_STATE_MODES; /* These bits are all meaningless here. */
+
+ err = diskfs_make_peropen (np, flags, cred->po, &newpo);
+ if (! err)
+ {
+ err = diskfs_create_protid (newpo, cred->user, &newpi);
+ if (err)
+ diskfs_release_peropen (newpo);
+ }
+
+ if (! err)
+ {
+ *newnode = ports_get_right (newpi);
+ *newnodetype = MACH_MSG_TYPE_MAKE_SEND;
+ ports_port_deref (newpi);
+ }
+
+ diskfs_nput (np);
+
+ return err;
+}
diff --git a/libdiskfs/dir-readdir.c b/libdiskfs/dir-readdir.c
new file mode 100644
index 00000000..1393e26f
--- /dev/null
+++ b/libdiskfs/dir-readdir.c
@@ -0,0 +1,61 @@
+/*
+ Copyright (C) 1993,94,96,99,2002 Free Software Foundation, Inc.
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include <fcntl.h>
+#include "priv.h"
+#include "fs_S.h"
+
+kern_return_t
+diskfs_S_dir_readdir (struct protid *cred,
+ char **data,
+ size_t *datacnt,
+ boolean_t *data_dealloc,
+ int entry,
+ int nentries,
+ vm_size_t bufsiz,
+ int *amt)
+{
+ error_t err;
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ pthread_mutex_lock (&np->lock);
+
+ if ((cred->po->openstat & O_READ) == 0)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EBADF;
+ }
+
+ if ((np->dn_stat.st_mode & S_IFMT) != S_IFDIR)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return ENOTDIR;
+ }
+
+ err = diskfs_get_directs (np, entry, nentries, data, datacnt, bufsiz, amt);
+ *data_dealloc = 1; /* XXX */
+ pthread_mutex_unlock (&np->lock);
+ return err;
+}
diff --git a/libdiskfs/dir-rename.c b/libdiskfs/dir-rename.c
new file mode 100644
index 00000000..ff9dead0
--- /dev/null
+++ b/libdiskfs/dir-rename.c
@@ -0,0 +1,239 @@
+/* libdiskfs implementation of fs.defs: dir_rename
+
+ Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 2007
+ Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <string.h>
+
+/* To avoid races in checkpath, and to prevent a directory from being
+ simultaneously renamed by two processes, we serialize all renames of
+ directores with this lock */
+static pthread_mutex_t renamedirlock = PTHREAD_MUTEX_INITIALIZER;
+
+/* Implement dir_rename as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_dir_rename (struct protid *fromcred,
+ char *fromname,
+ struct protid *tocred,
+ char *toname,
+ int excl)
+{
+ struct node *fdp, *tdp, *fnp, *tnp, *tmpnp;
+ error_t err;
+ struct dirstat *ds = alloca (diskfs_dirstat_size);
+
+ if (!fromcred)
+ return EOPNOTSUPP;
+
+ /* Verify that tocred really is a port to us. */
+ if (! tocred)
+ return EXDEV;
+
+ if (!strcmp (fromname, ".") || !strcmp (fromname, "..")
+ || !strcmp (toname, ".") || !strcmp (toname, ".."))
+ return EINVAL;
+
+ if (tocred->po->shadow_root != fromcred->po->shadow_root)
+ /* Same translator, but in different shadow trees. */
+ return EXDEV;
+
+ if (diskfs_check_readonly ())
+ return EROFS;
+
+ fdp = fromcred->po->np;
+ tdp = tocred->po->np;
+
+ try_again:
+ /* Acquire the source; hold a reference to it. This
+ will prevent anyone from deleting it before we create
+ the new link. */
+ pthread_mutex_lock (&fdp->lock);
+ err = diskfs_lookup (fdp, fromname, LOOKUP, &fnp, 0, fromcred);
+ pthread_mutex_unlock (&fdp->lock);
+ if (err == EAGAIN)
+ err = EINVAL;
+ if (err)
+ return err;
+
+ if (S_ISDIR (fnp->dn_stat.st_mode))
+ {
+ pthread_mutex_unlock (&fnp->lock);
+ if (pthread_mutex_trylock (&renamedirlock))
+ {
+ diskfs_nrele (fnp);
+ pthread_mutex_lock (&renamedirlock);
+ goto try_again;
+ }
+ err = diskfs_rename_dir (fdp, fnp, fromname, tdp, toname, fromcred,
+ tocred);
+ if (diskfs_synchronous)
+ {
+ pthread_mutex_lock (&fdp->lock);
+ diskfs_file_update (fdp, 1);
+ pthread_mutex_unlock (&fdp->lock);
+
+ pthread_mutex_lock (&fnp->lock);
+ diskfs_file_update (fnp, 1);
+ pthread_mutex_unlock (&fnp->lock);
+
+ pthread_mutex_lock (&tdp->lock);
+ diskfs_file_update (tdp, 1);
+ pthread_mutex_unlock (&tdp->lock);
+ }
+
+ diskfs_nrele (fnp);
+ pthread_mutex_unlock (&renamedirlock);
+ if (!err)
+ /* MiG won't do this for us, which it ought to. */
+ mach_port_deallocate (mach_task_self (), tocred->pi.port_right);
+ return err;
+ }
+
+ pthread_mutex_unlock (&fnp->lock);
+
+ /* We now hold no locks */
+
+ /* Link the node into the new directory. */
+ pthread_mutex_lock (&tdp->lock);
+
+ err = diskfs_lookup (tdp, toname, RENAME, &tnp, ds, tocred);
+ if (err == EAGAIN)
+ err = EINVAL;
+ else if (!err && excl)
+ {
+ err = EEXIST;
+ diskfs_nput (tnp);
+ }
+ if (err && err != ENOENT)
+ {
+ diskfs_drop_dirstat (tdp, ds);
+ diskfs_nrele (fnp);
+ pthread_mutex_unlock (&tdp->lock);
+ return err;
+ }
+
+ /* rename("foo", "link-to-foo") is guaranteed to return 0 and
+ do nothing by Posix. */
+ if (tnp == fnp)
+ {
+ diskfs_drop_dirstat (tdp, ds);
+ diskfs_nrele (fnp);
+ diskfs_nput (tnp);
+ pthread_mutex_unlock (&tdp->lock);
+ mach_port_deallocate (mach_task_self (), tocred->pi.port_right);
+ return 0;
+ }
+
+ /* rename("foo", dir) should fail. */
+ if (tnp && S_ISDIR (tnp->dn_stat.st_mode))
+ {
+ diskfs_drop_dirstat (tdp, ds);
+ diskfs_nrele (fnp);
+ diskfs_nput (tnp);
+ pthread_mutex_unlock (&tdp->lock);
+ return EISDIR;
+ }
+
+ pthread_mutex_lock (&fnp->lock);
+
+ /* Increment the link count for the upcoming link */
+ if (fnp->dn_stat.st_nlink == diskfs_link_max - 1)
+ {
+ diskfs_drop_dirstat (tdp, ds);
+ diskfs_nput (fnp);
+ if (tnp)
+ diskfs_nput (tnp);
+ pthread_mutex_unlock (&tdp->lock);
+ return EMLINK;
+ }
+ fnp->dn_stat.st_nlink++;
+ fnp->dn_set_ctime = 1;
+ diskfs_node_update (fnp, diskfs_synchronous);
+
+ if (tnp)
+ {
+ err = diskfs_dirrewrite (tdp, tnp, fnp, toname, ds);
+ if (!err)
+ {
+ tnp->dn_stat.st_nlink--;
+ tnp->dn_set_ctime = 1;
+ if (diskfs_synchronous)
+ diskfs_node_update (tnp, 1);
+ }
+ diskfs_nput (tnp);
+ }
+ else
+ err = diskfs_direnter (tdp, toname, fnp, ds, tocred);
+
+ if (diskfs_synchronous)
+ diskfs_node_update (tdp, 1);
+
+ pthread_mutex_unlock (&tdp->lock);
+ pthread_mutex_unlock (&fnp->lock);
+ if (err)
+ {
+ diskfs_nrele (fnp);
+ return err;
+ }
+
+ /* We now hold no locks */
+
+ /* Now we remove the source. Unfortunately, we haven't held
+ fdp locked (nor could we), so someone else might have already
+ removed it. */
+ pthread_mutex_lock (&fdp->lock);
+ err = diskfs_lookup (fdp, fromname, REMOVE, &tmpnp, ds, fromcred);
+ if (err)
+ {
+ diskfs_drop_dirstat (tdp, ds);
+ pthread_mutex_unlock (&fdp->lock);
+ diskfs_nrele (fnp);
+ return err;
+ }
+
+ if (tmpnp != fnp)
+ {
+ /* This is no longer the node being renamed, so just return. */
+ diskfs_drop_dirstat (tdp, ds);
+ diskfs_nput (tmpnp);
+ diskfs_nrele (fnp);
+ pthread_mutex_unlock (&fdp->lock);
+ mach_port_deallocate (mach_task_self (), tocred->pi.port_right);
+ return 0;
+ }
+
+ diskfs_nrele (tmpnp);
+
+ err = diskfs_dirremove (fdp, fnp, fromname, ds);
+ if (diskfs_synchronous)
+ diskfs_node_update (fdp, 1);
+
+ fnp->dn_stat.st_nlink--;
+ fnp->dn_set_ctime = 1;
+
+ if (diskfs_synchronous)
+ diskfs_node_update (fnp, 1);
+
+ diskfs_nput (fnp);
+ pthread_mutex_unlock (&fdp->lock);
+ if (!err)
+ mach_port_deallocate (mach_task_self (), tocred->pi.port_right);
+
+ return err;
+}
diff --git a/libdiskfs/dir-renamed.c b/libdiskfs/dir-renamed.c
new file mode 100644
index 00000000..9e37e234
--- /dev/null
+++ b/libdiskfs/dir-renamed.c
@@ -0,0 +1,241 @@
+/*
+ Copyright (C) 1994,95,96,97,98,99,2001,2003 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+
+/* Check if source directory is in the path of the target directory.
+ We get target locked, source unlocked but with a reference. When
+ we return, nothing is locked, and target has lost its reference.
+ This routine assumes that no renames of directories will happen
+ while it is running; as a result, ufs_rename serializes all renames
+ of directories. */
+static error_t
+checkpath(struct node *source,
+ struct node *target,
+ struct protid *cred)
+{
+ error_t err;
+ struct node *np;
+
+ for (np = target, err = 0;
+ /* nothing */;
+ /* This special lookup does a diskfs_nput on its first argument
+ when it succeeds. */
+ err = diskfs_lookup (np, "..", LOOKUP | SPEC_DOTDOT, &np, 0, cred))
+ {
+ if (err)
+ {
+ diskfs_nput (np);
+ return err;
+ }
+
+ if (np == source)
+ {
+ diskfs_nput (np);
+ return EINVAL;
+ }
+
+ if (np == diskfs_root_node || np == cred->po->shadow_root)
+ {
+ diskfs_nput (np);
+ return 0;
+ }
+ }
+}
+
+/* Rename directory node FNP (whose parent is FDP, and which has name
+ FROMNAME in that directory) to have name TONAME inside directory
+ TDP. None of these nodes are locked, and none should be locked
+ upon return. This routine is serialized, so it doesn't have to be
+ reentrant. Directories will never be renamed except by this
+ routine. FROMCRED and TOCRED are the users responsible for
+ FDP/FNP and TDP respectively. */
+error_t
+diskfs_rename_dir (struct node *fdp, struct node *fnp, const char *fromname,
+ struct node *tdp, const char *toname,
+ struct protid *fromcred, struct protid *tocred)
+{
+ error_t err;
+ struct node *tnp, *tmpnp;
+ void *buf = alloca (diskfs_dirstat_size);
+ struct dirstat *ds;
+ struct dirstat *tmpds;
+
+ pthread_mutex_lock (&tdp->lock);
+ diskfs_nref (tdp); /* reference and lock will get consumed by
+ checkpath */
+ err = checkpath (fnp, tdp, tocred);
+
+ if (err)
+ return err;
+
+ /* Now, lock the parent directories. This is legal because tdp is not
+ a child of fnp (guaranteed by checkpath above). */
+ pthread_mutex_lock (&fdp->lock);
+ if (fdp != tdp)
+ pthread_mutex_lock (&tdp->lock);
+
+ /* 1: Lookup target; if it exists, make sure it's an empty directory. */
+ ds = buf;
+ err = diskfs_lookup (tdp, toname, RENAME, &tnp, ds, tocred);
+ assert (err != EAGAIN); /* <-> assert (TONAME != "..") */
+
+ if (tnp == fnp)
+ {
+ diskfs_drop_dirstat (tdp, ds);
+ diskfs_nput (tnp);
+ pthread_mutex_unlock (&tdp->lock);
+ if (fdp != tdp)
+ pthread_mutex_unlock (&fdp->lock);
+ return 0;
+ }
+
+ /* Check permissions to remove FROMNAME and lock FNP. */
+ tmpds = alloca (diskfs_dirstat_size);
+ err = diskfs_lookup (fdp, fromname, REMOVE, &tmpnp, tmpds, fromcred);
+ assert (!tmpnp || tmpnp == fnp);
+ if (tmpnp)
+ diskfs_nrele (tmpnp);
+ diskfs_drop_dirstat (fdp, tmpds);
+ if (err)
+ goto out;
+
+ if (tnp)
+ {
+ if (! S_ISDIR(tnp->dn_stat.st_mode))
+ err = ENOTDIR;
+ else if (!diskfs_dirempty (tnp, tocred))
+ err = ENOTEMPTY;
+ }
+
+ if (err && err != ENOENT)
+ goto out;
+
+ /* 2: Set our .. to point to the new parent */
+ if (fdp != tdp)
+ {
+ if (tdp->dn_stat.st_nlink == diskfs_link_max - 1)
+ {
+ err = EMLINK;
+ goto out;
+ }
+ tdp->dn_stat.st_nlink++;
+ tdp->dn_set_ctime = 1;
+ if (diskfs_synchronous)
+ diskfs_node_update (tdp, 1);
+
+ tmpds = alloca (diskfs_dirstat_size);
+ err = diskfs_lookup (fnp, "..", RENAME | SPEC_DOTDOT,
+ &tmpnp, tmpds, fromcred);
+ assert (err != ENOENT);
+ if (err)
+ {
+ diskfs_drop_dirstat (fnp, tmpds);
+ goto out;
+ }
+ assert (tmpnp == fdp);
+
+ err = diskfs_dirrewrite (fnp, fdp, tdp, "..", tmpds);
+ if (diskfs_synchronous)
+ diskfs_file_update (fnp, 1);
+ if (err)
+ goto out;
+
+ fdp->dn_stat.st_nlink--;
+ fdp->dn_set_ctime = 1;
+ if (diskfs_synchronous)
+ diskfs_node_update (fdp, 1);
+ }
+
+
+ /* 3: Increment the link count on the node being moved and rewrite
+ tdp. */
+ if (fnp->dn_stat.st_nlink == diskfs_link_max - 1)
+ {
+ pthread_mutex_unlock (&fnp->lock);
+ diskfs_drop_dirstat (tdp, ds);
+ pthread_mutex_unlock (&tdp->lock);
+ if (tnp)
+ diskfs_nput (tnp);
+ return EMLINK;
+ }
+ fnp->dn_stat.st_nlink++;
+ fnp->dn_set_ctime = 1;
+ diskfs_node_update (fnp, diskfs_synchronous);
+
+ if (tnp)
+ {
+ err = diskfs_dirrewrite (tdp, tnp, fnp, toname, ds);
+ ds = 0;
+ if (!err)
+ {
+ tnp->dn_stat.st_nlink--;
+ tnp->dn_set_ctime = 1;
+ }
+ diskfs_clear_directory (tnp, tdp, tocred);
+ if (diskfs_synchronous)
+ diskfs_file_update (tnp, 1);
+ }
+ else
+ {
+ err = diskfs_direnter (tdp, toname, fnp, ds, tocred);
+ if (diskfs_synchronous)
+ diskfs_file_update (tdp, 1);
+ }
+
+ if (err)
+ goto out;
+
+ /* 4: Remove the entry in fdp. */
+ ds = buf;
+ pthread_mutex_unlock (&fnp->lock);
+ err = diskfs_lookup (fdp, fromname, REMOVE, &tmpnp, ds, fromcred);
+ assert (!tmpnp || tmpnp == fnp);
+ if (tmpnp)
+ diskfs_nrele (tmpnp);
+ if (err)
+ {
+ assert (!tmpnp);
+ /* diskfs_lookup has not locked fnp then, do not unlock it. */
+ fnp = NULL;
+ goto out;
+ }
+
+ diskfs_dirremove (fdp, fnp, fromname, ds);
+ ds = 0;
+ fnp->dn_stat.st_nlink--;
+ fnp->dn_set_ctime = 1;
+ if (diskfs_synchronous)
+ {
+ diskfs_file_update (fdp, 1);
+ diskfs_node_update (fnp, 1);
+ }
+
+ out:
+ if (tdp)
+ pthread_mutex_unlock (&tdp->lock);
+ if (tnp)
+ diskfs_nput (tnp);
+ if (fdp && fdp != tdp)
+ pthread_mutex_unlock (&fdp->lock);
+ if (fnp)
+ pthread_mutex_unlock (&fnp->lock);
+ if (ds)
+ diskfs_drop_dirstat (tdp, ds);
+ return err;
+}
diff --git a/libdiskfs/dir-rmdir.c b/libdiskfs/dir-rmdir.c
new file mode 100644
index 00000000..83ec37b3
--- /dev/null
+++ b/libdiskfs/dir-rmdir.c
@@ -0,0 +1,94 @@
+/* libdsikfs implementation of fs.defs: dir_rmdir
+ Copyright (C) 1992,93,94,95,96,97,99 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <hurd/fsys.h>
+
+/* Implement dir_rmdir as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_dir_rmdir (struct protid *dircred,
+ char *name)
+{
+ struct node *dnp;
+ struct node *np;
+ struct dirstat *ds = alloca (diskfs_dirstat_size);
+ error_t error;
+
+ /* This routine cleans up the state we have after calling diskfs_lookup.
+ After that call, all returns are done with `return done (ERROR, NP);'. */
+ inline error_t done (error_t error, struct node *np)
+ {
+ if (np)
+ diskfs_nput (np);
+
+ if (ds)
+ diskfs_drop_dirstat (dnp, ds);
+ pthread_mutex_unlock (&dnp->lock);
+
+ return error;
+ }
+
+ if (!dircred)
+ return EOPNOTSUPP;
+
+ dnp = dircred->po->np;
+ if (diskfs_check_readonly ())
+ return EROFS;
+
+ pthread_mutex_lock (&dnp->lock);
+
+ error = diskfs_lookup (dnp, name, REMOVE, &np, ds, dircred);
+ if (error)
+ return done (error == EAGAIN ? ENOTEMPTY : error, 0);
+
+ if (dnp == np)
+ {
+ /* Attempt to rmdir(".") */
+ diskfs_nrele (np);
+ diskfs_drop_dirstat (dnp, ds);
+ pthread_mutex_unlock (&dnp->lock);
+ return EINVAL;
+ }
+
+ if ((np->dn_stat.st_mode & S_IPTRANS) || fshelp_translated (&np->transbox))
+ /* Attempt to rmdir a translated node. */
+ return done (EBUSY, np);
+
+ if (!S_ISDIR (np->dn_stat.st_mode))
+ return done (ENOTDIR, np);
+
+ if (!diskfs_dirempty (np, dircred))
+ return done (ENOTEMPTY, np);
+
+ /* Here we go! */
+ error = diskfs_dirremove (dnp, np, name, ds);
+ ds = 0;
+
+ if (!error)
+ {
+ np->dn_stat.st_nlink--;
+ np->dn_set_ctime = 1;
+ diskfs_clear_directory (np, dnp, dircred);
+ if (diskfs_synchronous)
+ diskfs_file_update (np, 1);
+ }
+ if (diskfs_synchronous)
+ diskfs_file_update (dnp, 1);
+
+ return done (error, np);
+}
diff --git a/libdiskfs/dir-unlink.c b/libdiskfs/dir-unlink.c
new file mode 100644
index 00000000..cf02c227
--- /dev/null
+++ b/libdiskfs/dir-unlink.c
@@ -0,0 +1,98 @@
+/* libdiskfs implementation of fs.defs: dir_unlink
+ Copyright (C) 1992,93,94,95,96,97,2002 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <hurd/fsys.h>
+
+/* Implement dir_unlink as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_dir_unlink (struct protid *dircred,
+ char *name)
+{
+ struct node *dnp;
+ struct node *np;
+ struct dirstat *ds = alloca (diskfs_dirstat_size);
+ error_t err;
+ mach_port_t control = MACH_PORT_NULL;
+
+ if (!dircred)
+ return EOPNOTSUPP;
+
+ dnp = dircred->po->np;
+ if (diskfs_check_readonly ())
+ return EROFS;
+
+ pthread_mutex_lock (&dnp->lock);
+
+ err = diskfs_lookup (dnp, name, REMOVE, &np, ds, dircred);
+ if (err == EAGAIN)
+ err = EPERM; /* 1003.1-1996 5.5.1.4 */
+ if (err)
+ {
+ diskfs_drop_dirstat (dnp, ds);
+ pthread_mutex_unlock (&dnp->lock);
+ return err;
+ }
+
+ /* This isn't the BSD behavior, but it is Posix compliant and saves
+ us on several race conditions.*/
+ if (S_ISDIR(np->dn_stat.st_mode))
+ {
+ if (np == dnp) /* gotta catch '.' */
+ diskfs_nrele (np);
+ else
+ diskfs_nput (np);
+ diskfs_drop_dirstat (dnp, ds);
+ pthread_mutex_unlock (&dnp->lock);
+ return EPERM; /* 1003.1-1996 5.5.1.4 */
+ }
+
+ err = diskfs_dirremove (dnp, np, name, ds);
+ if (diskfs_synchronous)
+ diskfs_node_update (dnp, 1);
+ if (err)
+ {
+ diskfs_nput (np);
+ pthread_mutex_unlock (&dnp->lock);
+ return err;
+ }
+
+ np->dn_stat.st_nlink--;
+ np->dn_set_ctime = 1;
+ if (diskfs_synchronous)
+ diskfs_node_update (np, 1);
+
+ if (np->dn_stat.st_nlink == 0)
+ fshelp_fetch_control (&np->transbox, &control);
+
+ /* This check is necessary because we might get here on an error while
+ checking the mode on something which happens to be `.'. */
+ if (np == dnp)
+ diskfs_nrele (np);
+ else
+ diskfs_nput (np);
+ pthread_mutex_unlock (&dnp->lock);
+
+ if (control)
+ {
+ fsys_goaway (control, FSYS_GOAWAY_UNLINK);
+ mach_port_deallocate (mach_task_self (), control);
+ }
+
+ return err;
+}
diff --git a/libdiskfs/direnter.c b/libdiskfs/direnter.c
new file mode 100644
index 00000000..cb9b76ca
--- /dev/null
+++ b/libdiskfs/direnter.c
@@ -0,0 +1,48 @@
+/* Wrapper for diskfs_direnter_hard
+ Copyright (C) 1996, 1998 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+
+#include "priv.h"
+
+/* Add NP to directory DP under the name NAME. This will only be
+ called after an unsuccessful call to diskfs_lookup of type CREATE
+ or RENAME; DP has been locked continuously since that call and DS
+ is as that call set it, NP is locked. CRED identifies the user
+ responsible for the call (to be used only to validate directory
+ growth). This function is a wrapper for diskfs_direnter_hard. */
+error_t
+diskfs_direnter (struct node *dp,
+ const char *name,
+ struct node *np,
+ struct dirstat *ds,
+ struct protid *cred)
+{
+ error_t err;
+
+ err = diskfs_direnter_hard (dp, name, np, ds, cred);
+ if (err)
+ return err;
+
+ if (dp->dirmod_reqs)
+ diskfs_notice_dirchange (dp, DIR_CHANGED_NEW, name);
+
+ diskfs_enter_lookup_cache (dp, np, name);
+ return 0;
+}
diff --git a/libdiskfs/dirremove.c b/libdiskfs/dirremove.c
new file mode 100644
index 00000000..239daa72
--- /dev/null
+++ b/libdiskfs/dirremove.c
@@ -0,0 +1,45 @@
+/* Wrapper for diskfs_dirremove_hard
+ Copyright (C) 1996, 1998 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+/* This will only be called after a successful call to diskfs_lookup
+ of type REMOVE; this call should remove the name found from the
+ directory DS. DP has been locked continuously since the call to
+ diskfs_lookup and DS is as that call set it. This routine should
+ call diskfs_notice_dirchange if DP->dirmod_reqs is nonzero. This
+ function is a wrapper for diskfs_dirremove_hard. The entry being
+ removed has name NAME and refers to NP. */
+error_t
+diskfs_dirremove (struct node *dp,
+ struct node *np,
+ const char *name,
+ struct dirstat *ds)
+{
+ error_t err;
+
+ diskfs_purge_lookup_cache (dp, np);
+
+ err = diskfs_dirremove_hard (dp, ds);
+
+ if (!err && dp->dirmod_reqs)
+ diskfs_notice_dirchange (dp, DIR_CHANGED_UNLINK, name);
+ return err;
+}
diff --git a/libdiskfs/dirrewrite.c b/libdiskfs/dirrewrite.c
new file mode 100644
index 00000000..8f713960
--- /dev/null
+++ b/libdiskfs/dirrewrite.c
@@ -0,0 +1,50 @@
+/* Wrapper for diskfs_dirrewrite_hard
+ Copyright (C) 1996, 1998 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+
+#include "priv.h"
+
+/* This will only be called after a successful call to diskfs_lookup
+ of type RENAME; this call should change the name found in directory
+ DP to point to node NP instead of its previous referent, OLDNP. DP
+ has been locked continuously since the call to diskfs_lookup and DS
+ is as that call set it; NP is locked. This routine should call
+ diskfs_notice_dirchange if DP->dirmod_reqs is nonzero. NAME is the
+ name of OLDNP inside DP; it is this reference which is being
+ rewritten. This function is a wrapper for diskfs_dirrewrite_hard. */
+error_t diskfs_dirrewrite (struct node *dp,
+ struct node *oldnp,
+ struct node *np,
+ const char *name,
+ struct dirstat *ds)
+{
+ error_t err;
+
+ diskfs_purge_lookup_cache (dp, oldnp);
+
+ err = diskfs_dirrewrite_hard (dp, np, ds);
+ if (err)
+ return err;
+
+ if (dp->dirmod_reqs)
+ diskfs_notice_dirchange (dp, DIR_CHANGED_RENUMBER, name);
+ diskfs_enter_lookup_cache (dp, np, name);
+ return 0;
+}
diff --git a/libdiskfs/disk-pager.c b/libdiskfs/disk-pager.c
new file mode 100644
index 00000000..9a0d9d88
--- /dev/null
+++ b/libdiskfs/disk-pager.c
@@ -0,0 +1,131 @@
+/* Map the disk image and handle faults accessing it.
+ Copyright (C) 1996,97,99,2001,02 Free Software Foundation, Inc.
+ Written by Roland McGrath.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "diskfs-pager.h"
+#include <hurd/sigpreempt.h>
+#include <error.h>
+
+__thread struct disk_image_user *diskfs_exception_diu;
+
+struct pager *diskfs_disk_pager;
+
+static void fault_handler (int sig, long int sigcode, struct sigcontext *scp);
+static struct hurd_signal_preemptor preemptor =
+ {
+ signals: sigmask (SIGSEGV) | sigmask (SIGBUS),
+ preemptor: NULL,
+ handler: (sighandler_t) &fault_handler,
+ };
+
+/* A top-level function for the paging thread that just services paging
+ requests. */
+static void *
+service_paging_requests (void *arg)
+{
+ struct port_bucket *pager_bucket = arg;
+ for (;;)
+ ports_manage_port_operations_multithread (pager_bucket,
+ pager_demuxer,
+ 1000 * 60 * 2,
+ 1000 * 60 * 10, 0);
+ return NULL;
+}
+
+void
+diskfs_start_disk_pager (struct user_pager_info *upi,
+ struct port_bucket *pager_bucket,
+ int may_cache, int notify_on_evict,
+ size_t size, void **image)
+{
+ pthread_t thread;
+ error_t err;
+ mach_port_t disk_pager_port;
+
+ /* Make a thread to service paging requests. */
+ err = pthread_create (&thread, NULL, service_paging_requests, pager_bucket);
+ if (!err)
+ pthread_detach (thread);
+ else
+ {
+ errno = err;
+ perror ("pthread_create");
+ }
+
+ /* Create the pager. */
+ diskfs_disk_pager = pager_create (upi, pager_bucket,
+ may_cache, MEMORY_OBJECT_COPY_NONE,
+ notify_on_evict);
+ if (diskfs_disk_pager == NULL)
+ error (2, errno, "creating diskfs_disk_pager failed");
+
+ /* Get a port to the disk pager. */
+ disk_pager_port = pager_get_port (diskfs_disk_pager);
+ mach_port_insert_right (mach_task_self (), disk_pager_port, disk_pager_port,
+ MACH_MSG_TYPE_MAKE_SEND);
+
+ /* Now map the disk image. */
+ err = vm_map (mach_task_self (), (vm_address_t *)image, size,
+ 0, 1, disk_pager_port, 0, 0,
+ VM_PROT_READ | (diskfs_readonly ? 0 : VM_PROT_WRITE),
+ VM_PROT_READ | VM_PROT_WRITE,
+ VM_INHERIT_NONE);
+ if (err)
+ error (2, err, "cannot vm_map whole disk");
+
+ /* Set up the signal preemptor to catch faults on the disk image. */
+ preemptor.first = (vm_address_t) *image;
+ preemptor.last = ((vm_address_t) *image + size);
+ hurd_preempt_signals (&preemptor);
+
+ /* We have the mapping; we no longer need the send right. */
+ mach_port_deallocate (mach_task_self (), disk_pager_port);
+}
+
+static void
+fault_handler (int sig, long int sigcode, struct sigcontext *scp)
+{
+ error_t err;
+
+#ifndef NDEBUG
+ if (diskfs_exception_diu == NULL)
+ {
+ error (0, 0,
+ "BUG: unexpected fault on disk image (%d, %#lx) in [%#lx,%#lx)"
+ " eip %#zx err %#x",
+ sig, sigcode,
+ preemptor.first, preemptor.last,
+ scp->sc_pc, scp->sc_error);
+ assert (scp->sc_error == EKERN_MEMORY_ERROR);
+ err = pager_get_error (diskfs_disk_pager, sigcode);
+ assert (err);
+ assert_perror (err);
+ }
+#endif
+
+ /* Clear the record, since the faulting thread will not. */
+ diskfs_exception_diu = NULL;
+
+ /* Fetch the error code from the pager. */
+ assert (scp->sc_error == EKERN_MEMORY_ERROR);
+ err = pager_get_error (diskfs_disk_pager, sigcode);
+ assert (err);
+
+ /* Make `diskfault_catch' return the error code. */
+ longjmp (diskfs_exception_diu->env, err);
+}
diff --git a/libdiskfs/diskfs-pager.h b/libdiskfs/diskfs-pager.h
new file mode 100644
index 00000000..a253069b
--- /dev/null
+++ b/libdiskfs/diskfs-pager.h
@@ -0,0 +1,73 @@
+/* Map the disk image and handle faults accessing it.
+ Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+ Written by Roland McGrath.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef _HURD_DISKFS_PAGER_H
+#define _HURD_DISKFS_PAGER_H 1
+
+#include <hurd/pager.h>
+#include <hurd/ports.h>
+#include <setjmp.h>
+#include <pthread.h>
+#include <errno.h>
+#include <assert.h>
+#include <stdlib.h>
+
+extern __thread struct disk_image_user *diskfs_exception_diu;
+
+/* Start a pager for the whole disk, and store it in DISKFS_DISK_PAGER,
+ preparing a signal preemptor so that the `diskfs_catch_exception' macro
+ below works. SIZE should be the size of the image to map, and the address
+ mapped is returned in IMAGE. INFO, PAGER_BUCKET, & MAY_CACHE are passed
+ to `pager_create'. */
+extern void diskfs_start_disk_pager (struct user_pager_info *info,
+ struct port_bucket *pager_bucket,
+ int may_cache, int notify_on_evict,
+ size_t size, void **image);
+
+extern struct pager *diskfs_disk_pager;
+
+struct disk_image_user
+ {
+ jmp_buf env;
+ struct disk_image_user *next;
+ };
+
+/* Return zero now. Return a second time with a nonzero error_t
+ if this thread faults accessing `disk_image' before calling
+ `diskfs_end_catch_exception' (below). */
+#define diskfs_catch_exception() \
+({ \
+ struct disk_image_user *diu = alloca (sizeof *diu); \
+ error_t err; \
+ diu->next = diskfs_exception_diu; \
+ err = setjmp (diu->env); \
+ if (err == 0) \
+ diskfs_exception_diu = diu; \
+ err; \
+})
+
+/* No longer handle faults on `disk_image' in this thread.
+ Any unexpected fault hereafter will crash the program. */
+#define diskfs_end_catch_exception() \
+({ \
+ struct disk_image_user *diu = diskfs_exception_diu; \
+ diskfs_exception_diu = diu->next; \
+})
+
+
+#endif /* hurd/diskfs-pager.h */
diff --git a/libdiskfs/diskfs.h b/libdiskfs/diskfs.h
new file mode 100644
index 00000000..e3285271
--- /dev/null
+++ b/libdiskfs/diskfs.h
@@ -0,0 +1,1076 @@
+/* Definitions for fileserver helper functions
+
+ Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2007, 2008,
+ 2009, 2013 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef _HURD_DISKFS
+#define _HURD_DISKFS
+
+#include <assert.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <hurd/ports.h>
+#include <hurd/fshelp.h>
+#include <hurd/iohelp.h>
+#include <idvec.h>
+#include <features.h>
+#include <refcount.h>
+
+#ifdef DISKFS_DEFINE_EXTERN_INLINE
+#define DISKFS_EXTERN_INLINE
+#else
+#define DISKFS_EXTERN_INLINE __extern_inline
+#endif
+
+/* Each user port referring to a file points to one of these
+ (with the aid of the ports library). */
+struct protid
+{
+ struct port_info pi; /* libports info block */
+
+ /* User identification */
+ struct iouser *user;
+
+ /* Object this refers to */
+ struct peropen *po;
+
+ /* Shared memory I/O information. */
+ memory_object_t shared_object;
+ struct shared_io *mapped;
+};
+
+/* One of these is created for each node opened by dir_lookup. */
+struct peropen
+{
+ off_t filepointer;
+ int lock_status;
+ refcount_t refcnt;
+ int openstat;
+
+ struct node *np;
+
+ /* The parent of the translator's root node. */
+ mach_port_t root_parent;
+
+ /* If this node is in a shadow tree, the parent of its root. */
+ mach_port_t shadow_root_parent;
+ /* If in a shadow tree, its root node in this translator. */
+ struct node *shadow_root;
+
+ /* Path relative to the root of the translator. */
+ char *path;
+};
+
+/* A unique one of these exists for each node currently in use (and
+ possibly for some not currently in use, but with links) in the
+ filesystem. */
+struct node
+{
+ struct node *next, **prevp;
+
+ struct disknode *dn;
+
+ io_statbuf_t dn_stat;
+
+ /* Stat has been modified if one of the following four fields
+ is nonzero. Also, if one of the dn_set_?time fields is nonzero,
+ the appropriate dn_stat.st_?tim field needs to be updated. */
+ int dn_set_ctime;
+ int dn_set_atime;
+ int dn_set_mtime;
+ int dn_stat_dirty;
+
+ pthread_mutex_t lock;
+
+ int references; /* hard references */
+ int light_references; /* light references */
+
+ mach_port_t sockaddr; /* address for S_IFSOCK shortcut */
+
+ int owner;
+
+ struct transbox transbox;
+
+ struct lock_box userlock;
+
+ struct conch conch;
+
+ struct modreq *dirmod_reqs;
+ unsigned int dirmod_tick;
+
+ struct modreq *filemod_reqs;
+ unsigned int filemod_tick;
+
+ loff_t allocsize;
+
+ ino64_t cache_id;
+
+ int author_tracks_uid;
+};
+
+struct diskfs_control
+{
+ struct port_info pi;
+};
+
+struct bootinfo
+{
+ struct port_info pi;
+};
+
+/* Possibly lookup types for diskfs_lookup call */
+enum lookup_type
+{
+ LOOKUP,
+ CREATE,
+ REMOVE,
+ RENAME,
+};
+
+/* Pending directory and file modification request */
+struct modreq
+{
+ mach_port_t port;
+ struct modreq *next;
+};
+
+
+/* Special flag for diskfs_lookup. */
+#define SPEC_DOTDOT 0x10000000
+
+struct argp; /* opaque in this file */
+struct argp_child; /* opaque in this file */
+struct store; /* opaque in this file */
+struct store_parsed; /* opaque in this file */
+
+/* Declarations of variables the library sets. */
+
+extern mach_port_t diskfs_default_pager; /* send right */
+extern auth_t diskfs_auth_server_port; /* send right */
+
+/* The io_identity identity port for the filesystem. */
+extern mach_port_t diskfs_fsys_identity;
+
+/* The command line diskfs was started, set by the default argument parser.
+ If you don't use it, set this yourself. This is only used for bootstrap
+ file systems, to give the procserver. */
+extern char **diskfs_argv;
+
+/* When this is a bootstrap filesystem, the multiboot kernel command
+ line passed from the kernel. If not a bootstrap filesystem, it is
+ 0. As such, it can be used to distinguish between the two cases.
+ Note: this is only valid after the arguments have been parsed by,
+ for example, diskfs_init_main. */
+extern const char *diskfs_boot_command_line;
+#define diskfs_boot_filesystem() (diskfs_boot_command_line != 0)
+
+/* When this is a bootstrap filesystem, nonzero if starting each bootstrap
+ program should pause for a keystroke, for debugging purposes. */
+extern int _diskfs_boot_pause;
+
+/* Name of the init program run when this is a bootstrap filesystem. */
+extern const char *diskfs_boot_init_program;
+
+/* Hold this lock while do fsys level operations. Innocuous users can just
+ hold a reader lock, and anyone who's going to do nasty things that would
+ screw anyone else should hold a writer lock. */
+extern pthread_rwlock_t diskfs_fsys_lock;
+
+extern volatile struct mapped_time_value *diskfs_mtime;
+
+/* True iff we should do every operation synchronously. It
+ is the format-specific code's responsibility to keep allocation
+ information permanently in sync if this is set; the rest will
+ be done by format independent code. */
+extern int diskfs_synchronous;
+
+extern pthread_spinlock_t diskfs_node_refcnt_lock;
+
+extern int pager_port_type;
+
+/* Whether the filesystem is currently writable or not. */
+extern int diskfs_readonly;
+
+
+struct pager;
+
+/* Port classes we manage */
+extern struct port_class *diskfs_protid_class;
+extern struct port_class *diskfs_control_class;
+extern struct port_class *diskfs_execboot_class;
+extern struct port_class *diskfs_initboot_class;
+extern struct port_class *diskfs_shutdown_notification_class;
+
+extern struct port_bucket *diskfs_port_bucket;
+
+
+
+/* Declarations of things the user must or may define. */
+
+/* The user must define this type. This should hold information
+ between calls to diskfs_lookup and diskfs_dir{enter,rewrite,rename}
+ so that those calls work as described below. */
+struct dirstat;
+
+/* The user must define this variable; it should be the size in bytes
+ of a struct dirstat. */
+extern const size_t diskfs_dirstat_size;
+
+/* The user must define this variable; it is the maximum number of
+ links to any one file. The implementation of dir_rename does not know
+ how to succeed if this is only one; on such formats you need to
+ reimplement dir_rename yourself. */
+extern int diskfs_link_max;
+
+/* The user must define this variable; it is the maximum length of
+ a single pathname component (i.e. file name within directory).
+ The filesystem code does not use this for anything, but it is
+ returned to user queries for _PC_NAME_MAX. */
+extern int diskfs_name_max;
+
+/* The user must define this variable; it is the maximum number of
+ symlinks to be traversed within a single call to dir_lookup.
+ If this is exceeded, dir_lookup will return ELOOP. */
+extern int diskfs_maxsymlinks;
+
+/* This variable is defined by diskfs; the user should set it if
+ the filesystem media cannot be made writeable. */
+extern int diskfs_hard_readonly;
+
+/* The user must define this variable. Set this to be the node
+ of root of the filesystem. */
+extern struct node *diskfs_root_node;
+
+/* The user must define this variable. Set this to the name of the
+ filesystem server. */
+extern char *diskfs_server_name;
+
+/* The user must define this variables. Set this to be the server
+ version number. */
+extern char *diskfs_server_version;
+
+/* The user may define this variable. Set this to be any additional
+ version specification that should be printed for --version. */
+extern char *diskfs_extra_version;
+
+/* The user may define this variable. This should be nonzero iff the
+ filesystem format supports shortcutting symlink translation.
+ The library guarantees that users will not be able to read or write
+ the contents of the node directly, and the library will only do so
+ if the symlink hook functions return EINVAL or are not defined.
+ The library knows that the dn_stat.st_size field is the length of
+ the symlink, even if the hook functions are used. */
+int diskfs_shortcut_symlink;
+
+/* The user may define this variable. This should be nonzero iff the
+ filesystem format supports shortcutting chrdev translation. */
+int diskfs_shortcut_chrdev;
+
+/* The user may define this variable. This should be nonzero iff the
+ filesystem format supports shortcutting blkdev translation. */
+int diskfs_shortcut_blkdev;
+
+/* The user may define this variable. This should be nonzero iff the
+ filesystem format supports shortcutting fifo translation. */
+int diskfs_shortcut_fifo;
+
+/* The user may define this variable. This should be nonzero iff the
+ filesystem format supports shortcutting ifsock translation. */
+int diskfs_shortcut_ifsock;
+
+/* The user may define this variable, otherwise it has a default value of 30.
+ diskfs_set_sync_interval is called with this value when the first diskfs
+ thread is started up (in diskfs_spawn_first_threa). */
+extern int diskfs_default_sync_interval;
+
+/* The user must define this variable, which should be a string that somehow
+ identifies the particular disk this filesystem is interpreting. It is
+ generally only used to print messages or to distinguish instances of the
+ same filesystem type from one another. If this filesystem accesses no
+ external media, then define this to be 0. */
+extern char *diskfs_disk_name;
+
+/* The user must define this function. Set *STATFSBUF with
+ appropriate values to reflect the current state of the filesystem.
+ The buffer will be initialized to all zeros by the caller;
+ the caller will set f_namelen to diskfs_name_max. */
+error_t diskfs_set_statfs (fsys_statfsbuf_t *statfsbuf);
+
+/* The user must define this function. Lookup in directory DP (which
+ is locked) the name NAME. TYPE will either be LOOKUP, CREATE,
+ RENAME, or REMOVE. CRED identifies the user making the call.
+
+ If the name is found, return zero, and (if NP is nonzero) set *NP
+ to point to the node for it, locked. If the name is not found,
+ return ENOENT, and (if NP is nonzero) set *NP to zero. If NP is
+ zero, then the node found must not be locked, even transitorily.
+ Lookups for REMOVE and RENAME (which must often check permissions
+ on the node being found) will always set NP.
+
+ If DS is nonzero then:
+ For LOOKUP: set *DS to be ignored by diskfs_drop_dirstat.
+ For CREATE: on success, set *DS to be ignored by diskfs_drop_dirstat.
+ on failure, set *DS for a future call to diskfs_direnter.
+ For RENAME: on success, set *DS for a future call to diskfs_dirrewrite.
+ on failure, set *DS for a future call to diskfs_direnter.
+ For REMOVE: on success, set *DS for a future call to diskfs_dirremove.
+ on failure, set *DS to be ignored by diskfs_drop_dirstat.
+ The caller of this function guarantees that if DS is nonzero, then
+ either the appropriate call listed above or diskfs_drop_dirstat will
+ be called with DS before the directory DP is unlocked, and guarantees
+ that no lookup calls will be made on this directory between this
+ lookup and the use (or descruction) of *DS.
+
+ If you use the library's versions of diskfs_rename_dir,
+ diskfs_clear_directory, and diskfs_init_dir, then lookups for `..'
+ might have the flag SPEC_DOTDOT or'd in. This has the following special
+ meaning:
+ For LOOKUP: DP should be unlocked and its reference dropped before
+ returning.
+ For RENAME and REMOVE: The node being found (*NP) is already held
+ locked, so don't lock it or add a reference to it.
+ (SPEC_DOTDOT will not be given with CREATE.)
+
+ Return ENOENT if NAME isn't in the directory.
+ Return EAGAIN if NAME refers to the `..' of this filesystem's root.
+ Return EIO if appropriate.
+*/
+error_t diskfs_lookup_hard (struct node *dp,
+ const char *name, enum lookup_type type,
+ struct node **np, struct dirstat *ds,
+ struct protid *cred);
+
+/* The user must define this function. Add NP to directory DP
+ under the name NAME. This will only be called after an
+ unsuccessful call to diskfs_lookup of type CREATE or RENAME; DP
+ has been locked continuously since that call and DS is as that call
+ set it, NP is locked. CRED identifies the user responsible
+ for the call (to be used only to validate directory growth). */
+error_t diskfs_direnter_hard (struct node *dp, const char *name,
+ struct node *np, struct dirstat *ds,
+ struct protid *cred);
+
+/* The user must define this function. This will only be called after
+ a successful call to diskfs_lookup of type RENAME; this call should change
+ the name found in directory DP to point to node NP instead of its previous
+ referent. DP has been locked continuously since the call to diskfs_lookup
+ and DS is as that call set it; NP is locked. */
+error_t diskfs_dirrewrite_hard (struct node *dp, struct node *np,
+ struct dirstat *ds);
+
+/* The user must define this function. This will only be called after a
+ successful call to diskfs_lookup of type REMOVE; this call should remove
+ the name found from the directory DS. DP has been locked continuously since
+ the call to diskfs_lookup and DS is as that call set it. */
+error_t diskfs_dirremove_hard (struct node *dp, struct dirstat *ds);
+
+/* The user must define this function. Initialize DS such that
+ diskfs_drop_dirstat will ignore it. */
+void diskfs_null_dirstat (struct dirstat *ds);
+
+/* The user must define this function. DS has been set by a previous
+ call to diskfs_lookup on directory DP; this function is
+ guaranteed to be called if none of
+ diskfs_dir{enter,rename,rewrite} is, and should free any state
+ retained by a struct dirstat. DP has been locked continuously since
+ the call to diskfs_lookup. */
+error_t diskfs_drop_dirstat (struct node *dp, struct dirstat *ds);
+
+/* The user must define this function. Return N directory entries
+ starting at ENTRY from locked directory node DP. Fill *DATA with
+ the entries; that pointer currently points to *DATACNT bytes. If
+ it isn't big enough, vm_allocate into *DATA. Set *DATACNT with the
+ total size used. Fill AMT with the number of entries copied.
+ Regardless, never copy more than BUFSIZ bytes. If BUFSIZ is 0,
+ then there is no limit on *DATACNT; if N is -1, then there is no limit
+ on AMT. */
+error_t diskfs_get_directs (struct node *dp, int entry, int n,
+ char **data, size_t *datacnt,
+ vm_size_t bufsiz, int *amt);
+
+/* The user must define this function. For locked node NP (for which
+ diskfs_node_translated is true) look up the name of its translator.
+ Store the name into newly malloced storage; set *NAMELEN to the
+ total length. */
+error_t diskfs_get_translator (struct node *np, char **namep, u_int *namelen);
+
+/* The user must define this function. For locked node NP, set
+ the name of the translating program to be NAME, length NAMELEN. CRED
+ identifies the user responsible for the call. */
+error_t diskfs_set_translator (struct node *np,
+ const char *name, u_int namelen,
+ struct protid *cred);
+
+/* The user must define this function. Truncate locked node NP to be SIZE
+ bytes long. (If NP is already less than or equal to SIZE bytes
+ long, do nothing.) If this is a symlink (and diskfs_shortcut_symlink
+ is set) then this should clear the symlink, even if
+ diskfs_create_symlink_hook stores the link target elsewhere. */
+error_t diskfs_truncate (struct node *np, loff_t size);
+
+/* The user must define this function. Grow the disk allocated to locked node
+ NP to be at least SIZE bytes, and set NP->allocsize to the actual
+ allocated size. (If the allocated size is already SIZE bytes, do
+ nothing.) CRED identifies the user responsible for the call. */
+error_t diskfs_grow (struct node *np, loff_t size, struct protid *cred);
+
+/* The user must define this function. Write to disk (synchronously
+ iff WAIT is nonzero) from format-specific buffers any non-paged
+ metadata. If CLEAN is nonzero, then after this is written the
+ filesystem will be absolutely clean, and the non-paged metadata can
+ so indicate. */
+error_t diskfs_set_hypermetadata (int wait, int clean);
+
+/* The user must define this function. Allocate a new node to be of
+ mode MODE in locked directory DP (don't actually set the mode or
+ modify the dir, that will be done by the caller); the user
+ responsible for the request can be identified with CRED. Set *NP
+ to be the newly allocated node. */
+error_t diskfs_alloc_node (struct node *dp, mode_t mode, struct node **np);
+
+/* Free node NP; the on disk copy has already been synced with
+ diskfs_node_update (where NP->dn_stat.st_mode was 0). It's
+ mode used to be MODE. */
+void diskfs_free_node (struct node *np, mode_t mode);
+
+/* Node NP has no more references; free local state, including *NP
+ if it isn't to be retained. diskfs_node_refcnt_lock is held. */
+void diskfs_node_norefs (struct node *np);
+
+/* The user must define this function. Node NP has some light
+ references, but has just lost its last hard references. Take steps
+ so that if any light references can be freed, they are. NP is locked
+ as is the pager refcount lock. This function will be called after
+ diskfs_lost_hardrefs. */
+void diskfs_try_dropping_softrefs (struct node *np);
+
+/* The user must define this funcction. Node NP has some light
+ references but has just lost its last hard reference. NP is locked. */
+void diskfs_lost_hardrefs (struct node *np);
+
+/* The user must define this function. Node NP has just acquired
+ a hard reference where it had none previously. It is thus now
+ OK again to have light references without real users. NP is
+ locked. */
+void diskfs_new_hardrefs (struct node *np);
+
+/* The user must define this function. Return non-zero if locked
+ directory DP is empty. If the user does not redefine
+ diskfs_clear_directory and diskfs_init_directory, then `empty'
+ means `possesses entries labelled . and .. only'. CRED
+ identifies the user making the call (if this user can't search
+ the directory, then this routine should fail). */
+int diskfs_dirempty (struct node *dp, struct protid *cred);
+
+/* The user may define this function. Return 0 if NP's mode can be
+ changed to MODE; otherwise return an error code. It must always be
+ possible to clear the mode; diskfs will not ask for permission
+ before doing so. */
+error_t diskfs_validate_mode_change (struct node *np, mode_t mode);
+
+/* The user may define this function. Return 0 if NP's owner can be
+ changed to UID; otherwise return an error code. */
+error_t diskfs_validate_owner_change (struct node *np, uid_t uid);
+
+/* The user may define this function. Return 0 if NP's group can be
+ changed to GID; otherwise return an error code. */
+error_t diskfs_validate_group_change (struct node *np, gid_t gid);
+
+/* The user may define this function. Return 0 if NP's author can be
+ changed to AUTHOR; otherwise return an error code. */
+error_t diskfs_validate_author_change (struct node *np, uid_t author);
+
+/* The user may define this function. Return 0 if NP's flags can be
+ changed to FLAGS; otherwise return an error code. It must always
+ be possible to clear the flags. */
+error_t diskfs_validate_flags_change (struct node *np, int flags);
+
+/* The user may define this function. Return 0 if NP's rdev can be
+ changed to RDEV; otherwise return an error code. */
+error_t diskfs_validate_rdev_change (struct node *np, dev_t rdev);
+
+/* The user must define this function. Sync the info in NP->dn_stat
+ and any associated format-specific information to disk. If WAIT is true,
+ then return only after the physicial media has been completely updated. */
+void diskfs_write_disknode (struct node *np, int wait);
+
+/* The user must define this function. Sync the file contents and all
+ associated meta data of file NP to disk (generally this will involve
+ calling diskfs_node_update for much of the metadata). If WAIT is true,
+ then return only after the physical media has been completely updated. */
+void diskfs_file_update (struct node *np, int wait);
+
+/* The user must define this function. For each active node, call
+ FUN. The node is to be locked around the call to FUN. If FUN
+ returns non-zero for any node, then immediately stop, and return
+ that value. */
+error_t diskfs_node_iterate (error_t (*fun)(struct node *));
+
+/* The user must define this function. Sync all the pagers and any
+ data belonging on disk except for the hypermetadata. If WAIT is true,
+ then return only after the physicial media has been completely updated. */
+void diskfs_sync_everything (int wait);
+
+/* Shutdown all pagers; this is done when the filesystem is exiting and is
+ irreversable. */
+void diskfs_shutdown_pager ();
+
+/* The user must define this function. Return a memory object port (send
+ right) for the file contents of NP. PROT is the maximum allowable
+ access. On errors, return MACH_PORT_NULL and set errno. */
+mach_port_t diskfs_get_filemap (struct node *np, vm_prot_t prot);
+
+/* The user must define this function. Return true if there are pager
+ ports exported that might be in use by users. If this returns false, then
+ further pager creation is also blocked. */
+int diskfs_pager_users ();
+
+/* Return the bitwise or of the maximum prot parameter (the second arg to
+ diskfs_get_filemap) for all active user pagers. */
+vm_prot_t diskfs_max_user_pager_prot ();
+
+/* The user must define this function. Return a `struct pager *' suitable
+ for use as an argument to diskfs_register_memory_fault_area that
+ refers to the pager returned by diskfs_get_filemap for node NP.
+ NP is locked. */
+struct pager *diskfs_get_filemap_pager_struct (struct node *np);
+
+/* The user may define this function. It is called when the disk has been
+ changed from read-only to read-write mode or vice-versa. READONLY is the
+ new state (which is also reflected in DISKFS_READONLY). This function is
+ also called during initial startup if the filesystem is to be writable. */
+void diskfs_readonly_changed (int readonly);
+
+/* The user must define this function. It must invalidate all cached global
+ state, and re-read it as necessary from disk, without writing anything.
+ It is always called with DISKFS_READONLY true. diskfs_node_reload is
+ subsequently called on all active nodes, so this call needn't re-read any
+ node-specific data. */
+error_t diskfs_reload_global_state ();
+
+/* The user must define this function. It must re-read all data specific to
+ NODE from disk, without writing anything. It is always called with
+ DISKFS_READONLY true. */
+error_t diskfs_node_reload (struct node *node);
+
+/* If this function is nonzero (and diskfs_shortcut_symlink is set) it
+ is called to set a symlink. If it returns EINVAL or isn't set,
+ then the normal method (writing the contents into the file data) is
+ used. If it returns any other error, it is returned to the user. */
+error_t (*diskfs_create_symlink_hook)(struct node *np, const char *target);
+
+/* If this function is nonzero (and diskfs_shortcut_symlink is set) it
+ is called to read the contents of a symlink. If it returns EINVAL or
+ isn't set, then the normal method (reading from the file data) is
+ used. If it returns any other error, it is returned to the user. */
+error_t (*diskfs_read_symlink_hook)(struct node *np, char *target);
+
+/* The user may define this function. The function must set source to
+ the source of CRED. The function may return an EOPNOTSUPP to
+ indicate that the concept of a source device is not applicable. The
+ default function always returns EOPNOTSUPP. */
+error_t diskfs_get_source (struct protid *cred,
+ char *source, size_t source_len);
+
+/* The library exports the following functions for general use */
+
+/* Call this after arguments have been parsed to initialize the library.
+ You must call this before calling any other diskfs functions, and after
+ parsing diskfs options. */
+error_t diskfs_init_diskfs (void);
+
+/* Call this once the filesystem is fully initialized, to advertise the new
+ filesystem control port to our parent filesystem. If BOOTSTRAP is set,
+ the diskfs will call fsys_startup on that port as appropriate and return
+ the REALNODE returned in that call; otherwise we return MACH_PORT_NULL.
+ FLAGS specifies how to open REALNODE (from the O_* set). */
+mach_port_t diskfs_startup_diskfs (mach_port_t bootstrap, int flags);
+
+/* Call this after all format-specific initialization is done (except
+ for setting diskfs_root_node); at this point the pagers should be
+ ready to go. DEMUXER is the demuxer to user. Normally, this is
+ just diskfs_demuxer. */
+void diskfs_spawn_first_thread (ports_demuxer_type demuxer);
+
+/* Once diskfs_root_node is set, call this if we are a bootstrap
+ filesystem. If you call this, then the library will call
+ diskfs_init_completed once it has a valid proc and auth port. */
+void diskfs_start_bootstrap ();
+
+/* Node NP now has no more references; clean all state. The
+ _diskfs_node_refcnt_lock must be held, and will be released
+ upon return. NP must be locked. */
+void diskfs_drop_node (struct node *np);
+
+/* Set on disk fields from NP->dn_stat; update ctime, atime, and mtime
+ if necessary. If WAIT is true, then return only after the physical
+ media has been completely updated. */
+void diskfs_node_update (struct node *np, int wait);
+
+/* Add a hard reference to a node. If there were no hard
+ references previously, then the node cannot be locked
+ (because you must hold a hard reference to hold the lock). */
+void diskfs_nref (struct node *np);
+
+/* Unlock node NP and release a hard reference; if this is the last
+ hard reference and there are no links to the file then request
+ soft references to be dropped. */
+void diskfs_nput (struct node *np);
+
+/* Release a hard reference on NP. If NP is locked by anyone, then
+ this cannot be the last hard reference (because you must hold a
+ hard reference in order to hold the lock). If this is the last
+ hard reference and there are no links, then request soft references
+ to be dropped. */
+void diskfs_nrele (struct node *np);
+
+/* Add a light reference to a node. */
+void diskfs_nref_light (struct node *np);
+
+/* Unlock node NP and release a light reference */
+void diskfs_nput_light (struct node *np);
+
+/* Release a light reference on NP. If NP is locked by anyone, then
+ this cannot be the last reference (because you must hold a
+ hard reference in order to hold the lock). */
+void diskfs_nrele_light (struct node *np);
+
+/* Reading and writing of files. this is called by other filesystem
+ routines and handles extension of files automatically. NP is the
+ node to be read or written, and must be locked. DATA will be
+ written or filled. OFF identifies where in thi fel the I/O is to
+ take place (-1 is not allowed). AMT is the size of DATA and tells
+ how much to copy. DIR is 1 for writing and 0 for reading. CRED is
+ the user doing the access (only used to validate attempted file
+ extension). For reads, *AMTREAD is filled with the amount actually
+ read. */
+error_t
+diskfs_node_rdwr (struct node *np, char *data, loff_t off,
+ size_t amt, int dir, struct protid *cred,
+ size_t *amtread);
+
+
+/* Send notifications to users who have requested them with
+ dir_notice_changes for directory DP. The type of modification and
+ affected name are TYPE and NAME respectively. This should be
+ called by diskfs_direnter, diskfs_dirremove, and diskfs_dirrewrite,
+ and anything else that changes the directory, after the change is
+ fully completed. */
+void
+diskfs_notice_dirchange (struct node *dp, enum dir_changed_type type,
+ const char *name);
+
+/* Send notifications to users who have requested them with
+ file_notice_changes for file NP. The type of modification is TYPE.
+ START and END identify the affected region of the file's data.
+ This should be called after the change is fully completed. */
+void
+diskfs_notice_filechange (struct node *np, enum file_changed_type type,
+ loff_t start, loff_t end);
+
+/* Create a new node structure with DS as its physical disknode.
+ The new node will have one hard reference and no light references. */
+struct node *diskfs_make_node (struct disknode *dn);
+
+/* Create a new node structure. Also allocate SIZE bytes for the
+ disknode. The address of the disknode can be obtained using
+ diskfs_node_disknode. The new node will have one hard reference
+ and no light references. */
+struct node *diskfs_make_node_alloc (size_t size);
+
+/* To avoid breaking the ABI whenever sizeof (struct node) changes, we
+ explicitly provide the size. The following two functions will use
+ this value for offset calculations. */
+extern const size_t _diskfs_sizeof_struct_node;
+
+/* Return the address of the disknode for NODE. NODE must have been
+ allocated using diskfs_make_node_alloc. */
+static inline struct disknode *
+diskfs_node_disknode (struct node *node)
+{
+ return (struct disknode *) ((char *) node + _diskfs_sizeof_struct_node);
+}
+
+/* Return the address of the node for DISKNODE. DISKNODE must have
+ been allocated using diskfs_make_node_alloc. */
+static inline struct node *
+diskfs_disknode_node (struct disknode *disknode)
+{
+ return (struct node *) ((char *) disknode - _diskfs_sizeof_struct_node);
+}
+
+
+/* The library also exports the following functions; they are not generally
+ useful unless you are redefining other functions the library provides. */
+
+/* Lookup in directory DP (which is locked) the name NAME. TYPE will
+ either be LOOKUP, CREATE, RENAME, or REMOVE. CRED identifies the
+ user making the call.
+
+ NAME will have leading and trailing slashes stripped. It is an
+ error if there are internal slashes. NAME will be modified in
+ place if there are slashes in it; it is therefore an error to
+ specify a constant NAME which contains slashes.
+
+ If the name is found, return zero, and (if NP is nonzero) set *NP
+ to point to the node for it, locked. If the name is not found,
+ return ENOENT, and (if NP is nonzero) set *NP to zero. If NP is
+ zero, then the node found must not be locked, even transitorily.
+ Lookups for REMOVE and RENAME (which must often check permissions
+ on the node being found) will always set NP.
+
+ If DS is nonzero then:
+ For LOOKUP: set *DS to be ignored by diskfs_drop_dirstat.
+ For CREATE: on success, set *DS to be ignored by diskfs_drop_dirstat.
+ on failure, set *DS for a future call to diskfs_direnter.
+ For RENAME: on success, set *DS for a future call to diskfs_dirrewrite.
+ on failure, set *DS for a future call to diskfs_direnter.
+ For REMOVE: on success, set *DS for a future call to diskfs_dirremove.
+ on failure, set *DS to be ignored by diskfs_drop_dirstat.
+ The caller of this function guarantees that if DS is nonzero, then
+ either the appropriate call listed above or diskfs_drop_dirstat will
+ be called with DS before the directory DP is unlocked, and guarantees
+ that no lookup calls will be made on this directory between this
+ lookup and the use (or descruction) of *DS.
+
+ If you use the library's versions of diskfs_rename_dir,
+ diskfs_clear_directory, and diskfs_init_dir, then lookups for `..'
+ might have the flag SPEC_DOTDOT or'd in. This has the following special
+ meaning:
+ For LOOKUP: DP should be unlocked and its reference dropped before
+ returning.
+ For RENAME and REMOVE: The node being found (*NP) is already held
+ locked, so don't lock it or add a reference to it.
+ (SPEC_DOTDOT will not be given with CREATE.)
+
+ Return ENOTDIR if DP is not a directory.
+ Return EACCES if CRED isn't allowed to search DP.
+ Return EACCES if completing the operation will require writing
+ the directory and diskfs_checkdirmod won't allow the modification.
+ Return ENOENT if NAME isn't in the directory.
+ Return EAGAIN if NAME refers to the `..' of this filesystem's root.
+ Return EIO if appropriate.
+
+ This function is a wrapper for diskfs_lookup_hard.
+*/
+error_t diskfs_lookup (struct node *dp,
+ const char *name, enum lookup_type type,
+ struct node **np, struct dirstat *ds,
+ struct protid *cred);
+
+/* Add NP to directory DP under the name NAME. This will only be
+ called after an unsuccessful call to diskfs_lookup of type CREATE
+ or RENAME; DP has been locked continuously since that call and DS
+ is as that call set it, NP is locked. CRED identifies the user
+ responsible for the call (to be used only to validate directory
+ growth). This function is a wrapper for diskfs_direnter_hard. */
+error_t
+diskfs_direnter (struct node *dp, const char *name, struct node *np,
+ struct dirstat *ds, struct protid *cred);
+
+/* This will only be called after a successful call to diskfs_lookup
+ of type RENAME; this call should change the name found in directory
+ DP to point to node NP instead of its previous referent, OLDNP. DP
+ has been locked continuously since the call to diskfs_lookup and DS
+ is as that call set it; NP is locked. This routine should call
+ diskfs_notice_dirchange if DP->dirmod_reqs is nonzero. NAME is the
+ name of OLDNP inside DP; it is this reference which is being
+ rewritten. This function is a wrapper for diskfs_dirrewrite_hard. */
+error_t diskfs_dirrewrite (struct node *dp, struct node *oldnp,
+ struct node *np, const char *name,
+ struct dirstat *ds);
+
+/* This will only be called after a successful call to diskfs_lookup
+ of type REMOVE; this call should remove the name found from the
+ directory DS. DP has been locked continuously since the call to
+ diskfs_lookup and DS is as that call set it. This routine should
+ call diskfs_notice_dirchange if DP->dirmod_reqs is nonzero. This
+ function is a wrapper for diskfs_dirremove_hard. The entry being
+ removed has name NAME and refers to NP. */
+error_t diskfs_dirremove (struct node *dp, struct node *np,
+ const char *name, struct dirstat *ds);
+
+/* Return the node corresponding to CACHE_ID in *NPP. */
+error_t diskfs_cached_lookup (ino64_t cache_id, struct node **npp);
+
+/* Create a new node. Give it MODE; if that includes IFDIR, also
+ initialize `.' and `..' in the new directory. Return the node in NPP.
+ CRED identifies the user responsible for the call. If NAME is nonzero,
+ then link the new node into DIR with name NAME; DS is the result of a
+ prior diskfs_lookup for creation (and DIR has been held locked since).
+ DIR must always be provided as at least a hint for disk allocation
+ strategies. */
+error_t
+diskfs_create_node (struct node *dir, const char *name, mode_t mode,
+ struct node **newnode, struct protid *cred,
+ struct dirstat *ds);
+
+/* Create and return a protid for an existing peropen PO in CRED,
+ referring to user USER. */
+error_t diskfs_create_protid (struct peropen *po, struct iouser *user,
+ struct protid **cred);
+
+/* Build and return in CRED a protid which has no user identification, for
+ peropen PO. */
+error_t diskfs_start_protid (struct peropen *po, struct protid **cred);
+
+/* Finish building protid CRED started with diskfs_start_protid;
+ the user to install is USER. */
+void diskfs_finish_protid (struct protid *cred, struct iouser *user);
+
+extern struct protid * diskfs_begin_using_protid_port (file_t port);
+extern struct diskfs_control * diskfs_begin_using_control_port (fsys_t port);
+extern struct bootinfo *diskfs_begin_using_bootinfo_port (exec_startup_t port);
+
+extern void diskfs_end_using_protid_port (struct protid *cred);
+extern void diskfs_end_using_control_port (struct diskfs_control *cred);
+extern void diskfs_end_using_bootinfo (struct bootinfo *upt);
+
+#if defined(__USE_EXTERN_INLINES) || defined(DISKFS_DEFINE_EXTERN_INLINE)
+
+/* Called by MiG to translate ports into struct protid *.
+ fsmutations.h arranges for this to happen for the io and
+ fs interfaces. */
+DISKFS_EXTERN_INLINE struct protid *
+diskfs_begin_using_protid_port (file_t port)
+{
+ return ports_lookup_port (diskfs_port_bucket, port, diskfs_protid_class);
+}
+
+/* And for the fsys interface. */
+DISKFS_EXTERN_INLINE struct diskfs_control *
+diskfs_begin_using_control_port (fsys_t port)
+{
+ return ports_lookup_port (diskfs_port_bucket, port, NULL);
+}
+
+/* And for the exec_startup interface. */
+DISKFS_EXTERN_INLINE struct bootinfo *
+diskfs_begin_using_bootinfo_port (exec_startup_t port)
+{
+ return ports_lookup_port (diskfs_port_bucket, port, diskfs_execboot_class);
+}
+
+
+/* Called by MiG after server routines have been run; this
+ balances begin_using_protid_port, and is arranged for the io
+ and fs interfaces by fsmutations.h. */
+DISKFS_EXTERN_INLINE void
+diskfs_end_using_protid_port (struct protid *cred)
+{
+ if (cred)
+ ports_port_deref (cred);
+}
+
+/* And for the fsys interface. */
+DISKFS_EXTERN_INLINE void
+diskfs_end_using_control_port (struct diskfs_control *cred)
+{
+ if (cred)
+ ports_port_deref (cred);
+}
+
+/* And for the exec_startup interface. */
+DISKFS_EXTERN_INLINE void
+diskfs_end_using_bootinfo (struct bootinfo *b)
+{
+ if (b)
+ ports_port_deref (b);
+}
+
+#endif /* Use extern inlines. */
+
+/* Called when a protid CRED has no more references. (Because references\
+ to protids are maintained by the port management library, this is
+ installed in the clean routines list.) The ports library will
+ free the structure for us. */
+void diskfs_protid_rele (void *arg);
+
+/* Create a new peropen structure on node NP with open flags FLAGS in
+ *PPO. The initial values for the root_parent, shadow_root, and
+ shadow_root_parent fields are copied from CONTEXT if it's non-zero,
+ otherwise they are zeroed. */
+error_t
+diskfs_make_peropen (struct node *np, int flags,
+ struct peropen *context, struct peropen **ppo);
+
+/* Decrement the reference count on a peropen structure. */
+void diskfs_release_peropen (struct peropen *po);
+
+/* Node NP has just been found in DIR with NAME. If NP is null, that
+ means that this name has been confirmed as absent in the directory. */
+void diskfs_enter_lookup_cache (struct node *dir, struct node *np,
+ const char *name);
+
+/* Purge all references in the cache to NP as a node inside
+ directory DP. */
+void diskfs_purge_lookup_cache (struct node *dp, struct node *np);
+
+/* Scan the cache looking for NAME inside DIR. If we don't know
+ anything entry at all, then return 0. If the entry is confirmed to
+ not exist, then return -1. Otherwise, return NP for the entry, with
+ a newly allocated reference. */
+struct node *diskfs_check_lookup_cache (struct node *dir, const char *name);
+
+/* Rename directory node FNP (whose parent is FDP, and which has name
+ FROMNAME in that directory) to have name TONAME inside directory
+ TDP. None of these nodes are locked, and none should be locked
+ upon return. This routine is serialized, so it doesn't have to be
+ reentrant. Directories will never be renamed except by this
+ routine. FROMCRED and TOCRED are the users responsible for
+ FDP/FNP and TDP respectively. This routine assumes the usual
+ convention where `.' and `..' are represented by ordinary links;
+ if that is not true for your format, you have to redefine this
+ function.*/
+error_t
+diskfs_rename_dir (struct node *fdp, struct node *fnp, const char *fromname,
+ struct node *tdp, const char *toname,
+ struct protid *fromcred, struct protid *tocred);
+
+/* Clear the `.' and `..' entries from directory DP. Its parent is
+ PDP, and the user responsible for this is identified by CRED. Both
+ directories must be locked. This routine assumes the usual
+ convention where `.' and `..' are represented by ordinary links; if
+ that is not true for your format, you have to redefine this
+ function. */
+error_t diskfs_clear_directory (struct node *dp, struct node *pdp,
+ struct protid *cred);
+
+/* Locked node DP is a new directory; add whatever links are necessary
+ to give it structure; its parent is the (locked) node PDP.
+ This routine may not call diskfs_lookup on PDP. The new directory
+ must be clear within the meaning of diskfs_dirempty. This routine
+ assumes the usual convention where `.' and `..' are represented by
+ ordinary links; if that is not true for your format, you have to
+ redefine this function. CRED identifies the user making the call. */
+error_t
+diskfs_init_dir (struct node *dp, struct node *pdp, struct protid *cred);
+
+/* If disk is not readonly and the noatime option is not enabled, set
+ NP->dn_set_atime. */
+void diskfs_set_node_atime (struct node *np);
+
+/* If NP->dn_set_ctime is set, then modify NP->dn_stat.st_ctim
+ appropriately; do the analogous operation for atime and mtime as well. */
+void diskfs_set_node_times (struct node *np);
+
+/* Shutdown the filesystem; flags are as for fsys_goaway. */
+error_t diskfs_shutdown (int flags);
+
+/* Change an active filesystem between read-only and writable modes, setting
+ the global variable DISKFS_READONLY to reflect the current mode. If an
+ error is returned, nothing will have changed. DISKFS_FSYS_LOCK should be
+ held while calling this routine. */
+error_t diskfs_set_readonly (int readonly);
+
+/* Re-read all incore data structures from disk. This will only work if
+ DISKFS_READONLY is true. DISKFS_FSYS_LOCK should be held while calling
+ this routine. */
+error_t diskfs_remount ();
+
+/* Called by S_fsys_startup for execserver bootstrap. The execserver
+ is able to function without a real node, hence this fraud. Arguments
+ are all as for fsys_startup in <hurd/fsys.defs>. */
+error_t diskfs_execboot_fsys_startup (mach_port_t port, int flags,
+ mach_port_t ctl, mach_port_t *real,
+ mach_msg_type_name_t *realpoly);
+
+/* Establish a thread to sync the filesystem every INTERVAL seconds, or
+ never, if INTERVAL is zero. If an error occurs creating the thread, it is
+ returned, otherwise 0. Subsequent calls will create a new thread and
+ (eventually) get rid of the old one; the old thread won't do any more
+ syncs, regardless. */
+error_t diskfs_set_sync_interval (int interval);
+
+/* Parse and execute the runtime options in ARGZ & ARGZ_LEN. EINVAL is
+ returned if some option is unrecognized. The default definition of this
+ routine will parse them using DISKFS_RUNTIME_ARGP, which see. */
+error_t diskfs_set_options (const char *argz, size_t argz_len);
+
+/* Append to the malloced string *ARGZ of length *ARGZ_LEN a NUL-separated
+ list of the arguments to this translator. The default definition of this
+ routine simply calls diskfs_append_std_options. */
+error_t diskfs_append_args (char **argz, size_t *argz_len);
+
+/* If this is defined or set to an argp structure, it will be used by the
+ default diskfs_set_options to handle runtime option parsing. The default
+ definition is initialized to a pointer to DISKFS_STD_RUNTIME_ARGP. */
+extern struct argp *diskfs_runtime_argp;
+
+/* An argp for the standard diskfs runtime options. The default definition
+ of DISKFS_RUNTIME_ARGP points to this, although if the user redefines
+ that, he may chain this onto his argp as well. */
+extern const struct argp diskfs_std_runtime_argp;
+
+/* An argp structure for the standard diskfs command line arguments. The
+ user may call argp_parse on this to parse the command line, chain it onto
+ the end of his own argp structure, or ignore it completely. */
+extern const struct argp diskfs_startup_argp;
+
+/* An argp structure for the standard diskfs command line arguments plus a
+ store specification. The address of a location in which to return the
+ resulting struct store_parsed structure should be passed as the input
+ argument to argp_parse; see the declaration for STORE_ARGP in
+ <hurd/store.h> for more information. */
+extern const struct argp diskfs_store_startup_argp;
+
+/* *Appends* to ARGZ & ARGZ_LEN '\0'-separated options describing the standard
+ diskfs option state (note that unlike diskfs_get_options, ARGZ & ARGZ_LEN
+ must already have a sane value). */
+error_t diskfs_append_std_options (char **argz, size_t *argz_len);
+
+/* Demultiplex incoming messages on ports created by libdiskfs. */
+int diskfs_demuxer (mach_msg_header_t *, mach_msg_header_t *);
+
+/* Check if the filesystem is readonly before an operation that
+ writes it. Return 1 if readonly, zero otherwise. */
+int diskfs_check_readonly (void);
+
+/* The diskfs library provides functions to demultiplex the fs, io,
+ fsys, interrupt, and notify interfaces. All the server routines
+ have the prefix `diskfs_S_'; `in' arguments of type file_t or io_t
+ appear as `struct protid *' to the stub. */
+
+
+/* All-in-one initialization function for diskfs filesystems using
+ libstore. This parses arguments using STARTUP_ARGP (defaulting to
+ diskfs_store_startup_argp if it's null; note that the ARGP_IN_ORDER
+ flag is always used); it calls diskfs_init_diskfs; it opens the
+ store with store_parsed_open, and sets diskfs_hard_readonly and
+ diskfs_readonly if the store is unwritable; it calls
+ diskfs_spawn_first_thread; finally, it returns the store and its
+ description in *STORE and *STORE_PARSED, and the bootstrap port in
+ *BOOTSTRAP. The caller should pass *BOOTSTRAP to
+ diskfs_startup_diskfs after setting diskfs_root_node.
+ (See <argp.h> and <hurd/store.h>.)
+
+ This call cannot return failure; if it encounters a fatal problem,
+ it prints a diagnostic on stderr (or the console) and exits the
+ program. */
+struct store *diskfs_init_main (struct argp *startup_argp,
+ int argc, char **argv,
+ struct store_parsed **store_parsed,
+ mach_port_t *bootstrap);
+
+/* The following are optional convenience routines and global variable, which
+ can be used by any user program that uses a mach device to hold the
+ underlying filesystem. */
+
+/* Make errors go somewhere reasonable. */
+void diskfs_console_stdio ();
+
+#endif /* hurd/diskfs.h */
diff --git a/libdiskfs/extern-inline.c b/libdiskfs/extern-inline.c
new file mode 100644
index 00000000..43de88d6
--- /dev/null
+++ b/libdiskfs/extern-inline.c
@@ -0,0 +1,20 @@
+/* Run time callable functions for extern inlines.
+ Copyright (C) 2001 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#define DISKFS_DEFINE_EXTERN_INLINE
+
+#include "diskfs.h"
diff --git a/libdiskfs/extra-version.c b/libdiskfs/extra-version.c
new file mode 100644
index 00000000..b1d78084
--- /dev/null
+++ b/libdiskfs/extra-version.c
@@ -0,0 +1,24 @@
+/* Default value for diskfs_extra_version
+ Copyright (C) 1996 Free Software Foundation, Inc.
+ Written by Thomas Bushnell, n/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+
+#include "priv.h"
+
+char *diskfs_extra_version = "";
diff --git a/libdiskfs/fhandle.h b/libdiskfs/fhandle.h
new file mode 100644
index 00000000..bd827d84
--- /dev/null
+++ b/libdiskfs/fhandle.h
@@ -0,0 +1,36 @@
+/* File handle type (for nfs server support)
+
+ Copyright (C) 1997,99 Free Software Foundation, Inc.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd; see the file COPYING. If not, write to
+ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef __FHANDLE_H__
+#define __FHANDLE_H__
+
+/* Must be exactly 28 bytes long */
+union diskfs_fhandle
+{
+ unsigned char bytes[28];
+ struct
+ {
+ int pad1;
+ int cache_id;
+ unsigned int gen;
+ } data;
+};
+
+#endif /* __FHANDLE_H__ */
diff --git a/libdiskfs/file-access.c b/libdiskfs/file-access.c
new file mode 100644
index 00000000..f7e129aa
--- /dev/null
+++ b/libdiskfs/file-access.c
@@ -0,0 +1,44 @@
+/*
+ Copyright (C) 1994 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <fcntl.h>
+
+kern_return_t
+diskfs_S_file_check_access (struct protid *cred,
+ int *type)
+{
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ pthread_mutex_lock (&np->lock);
+ *type = 0;
+ if (fshelp_access (&np->dn_stat, S_IREAD, cred->user) == 0)
+ *type |= O_READ;
+ if (fshelp_access (&np->dn_stat, S_IWRITE, cred->user) == 0)
+ *type |= O_WRITE;
+ if (fshelp_access (&np->dn_stat, S_IEXEC, cred->user) == 0)
+ *type |= O_EXEC;
+
+ pthread_mutex_unlock (&np->lock);
+
+ return 0;
+}
diff --git a/libdiskfs/file-chauthor.c b/libdiskfs/file-chauthor.c
new file mode 100644
index 00000000..6e49c53f
--- /dev/null
+++ b/libdiskfs/file-chauthor.c
@@ -0,0 +1,42 @@
+/* libdithkfth implementation of fth.defth: file_chauthor
+ Copyright (C) 1992, 1993, 1994, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "lithp.h"
+
+#include "priv.h"
+#include fth_TH_dot_h
+
+/* Implement file_chauthor as dethcribed in <hurd/fth.defth>. */
+kern_return_t
+dithkfth_TH_file_chauthor (struct protid *cred,
+ uid_t author)
+{
+ CHANGE_NODE_FIELD (cred,
+ ({
+ err = fthhelp_ithowner (&np->dn_thtat, cred->uther);
+ if (!err)
+ err = dithkfth_validate_author_change (np, author);
+ if (!err)
+ {
+ np->dn_thtat.tht_author = author;
+ np->dn_thet_theetime = 1;
+ if (np->filemod_reqs)
+ diskfs_notice_filechange(np, FILE_CHANGED_META,
+ 0, 0);
+ }
+ }));
+}
diff --git a/libdiskfs/file-chflags.c b/libdiskfs/file-chflags.c
new file mode 100644
index 00000000..01dc495c
--- /dev/null
+++ b/libdiskfs/file-chflags.c
@@ -0,0 +1,40 @@
+/* libdiskfs implementation of fs.defs:file_chflags
+ Copyright (C) 1992, 1993, 1994, 1996, 1998 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement file_chflags as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_chflags (struct protid *cred,
+ int flags)
+{
+ CHANGE_NODE_FIELD (cred,
+ ({
+ err = fshelp_isowner (&np->dn_stat, cred->user);
+ if (!err)
+ err = diskfs_validate_flags_change (np, flags);
+ if (!err)
+ {
+ np->dn_stat.st_flags = flags;
+ np->dn_set_ctime = 1;
+ }
+ if (!err && np->filemod_reqs)
+ diskfs_notice_filechange(np, FILE_CHANGED_META,
+ 0, 0);
+ }));
+}
diff --git a/libdiskfs/file-chg.c b/libdiskfs/file-chg.c
new file mode 100644
index 00000000..9da43e79
--- /dev/null
+++ b/libdiskfs/file-chg.c
@@ -0,0 +1,71 @@
+/*
+ Copyright (C) 1994, 1995 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_notify_S.h"
+#include "fs_notify_U.h"
+
+kern_return_t
+diskfs_S_file_notice_changes (struct protid *cred, mach_port_t notify)
+{
+ error_t err;
+ struct modreq *req;
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ pthread_mutex_lock (&np->lock);
+ err = file_changed (notify, np->filemod_tick, FILE_CHANGED_NULL, 0, 0);
+ if (err)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return err;
+ }
+ req = malloc (sizeof (struct modreq));
+ req->port = notify;
+ req->next = np->filemod_reqs;
+ np->filemod_reqs = req;
+ pthread_mutex_unlock (&np->lock);
+ return 0;
+}
+
+void
+diskfs_notice_filechange (struct node *dp, enum file_changed_type type,
+ off_t start, off_t end)
+{
+ error_t err;
+ struct modreq **preq;
+
+ dp->filemod_tick++;
+ preq = &dp->filemod_reqs;
+ while (*preq)
+ {
+ struct modreq *req = *preq;
+ err = file_changed (req->port, dp->filemod_tick, type, start, end);
+ if (err && err != MACH_SEND_TIMED_OUT)
+ {
+ /* Remove notify port. */
+ *preq = req->next;
+ mach_port_deallocate (mach_task_self (), req->port);
+ free (req);
+ }
+ else
+ preq = &req->next;
+ }
+}
diff --git a/libdiskfs/file-chmod.c b/libdiskfs/file-chmod.c
new file mode 100644
index 00000000..df262ea3
--- /dev/null
+++ b/libdiskfs/file-chmod.c
@@ -0,0 +1,57 @@
+/* libdiskfs implementation of fs.defs: file_chmod
+ Copyright (C) 1992,93,94,96,97,2002 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement file_chmod as described in <hurd/fs.defs>. */
+error_t
+diskfs_S_file_chmod (struct protid *cred,
+ mode_t mode)
+{
+ mode &= ~(S_IFMT | S_ISPARE | S_ITRANS);
+
+ CHANGE_NODE_FIELD (cred,
+ ({
+ if (!(err = fshelp_isowner (&np->dn_stat, cred->user)))
+ {
+ if (!idvec_contains (cred->user->uids, 0))
+ {
+ if (!S_ISDIR (np->dn_stat.st_mode))
+ mode &= ~S_ISVTX;
+ if (!idvec_contains (cred->user->gids,
+ np->dn_stat.st_gid))
+ mode &= ~S_ISGID;
+ if (!idvec_contains (cred->user->uids,
+ np->dn_stat.st_uid))
+ mode &= ~S_ISUID;
+ }
+ mode |= (np->dn_stat.st_mode
+ & (S_IFMT | S_ISPARE | S_ITRANS));
+ err = diskfs_validate_mode_change (np, mode);
+ if (!err)
+ {
+ np->dn_stat.st_mode = mode;
+ np->dn_set_ctime = 1;
+ if (np->filemod_reqs)
+ diskfs_notice_filechange (np,
+ FILE_CHANGED_META,
+ 0, 0);
+ }
+ }
+ }));
+}
diff --git a/libdiskfs/file-chown.c b/libdiskfs/file-chown.c
new file mode 100644
index 00000000..ecb851f2
--- /dev/null
+++ b/libdiskfs/file-chown.c
@@ -0,0 +1,64 @@
+/* libdiskfs implementetation of fs.defs: file_chown
+ Copyright (C) 1992, 1993, 1994, 1996, 1999 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement file_chown as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_chown (struct protid *cred,
+ uid_t uid,
+ gid_t gid)
+{
+ if (uid == (uid_t) -1 && gid == (gid_t) -1) /* No change requested. */
+ return 0;
+
+ CHANGE_NODE_FIELD (cred,
+ ({
+ err = fshelp_isowner (&np->dn_stat, cred->user);
+ if (err
+ || (((uid != (uid_t) -1
+ && !idvec_contains (cred->user->uids, uid))
+ || (gid != (gid_t) -1
+ && !idvec_contains (cred->user->gids, gid)))
+ && !idvec_contains (cred->user->uids, 0)))
+ err = EPERM;
+ else
+ {
+ if (uid != (uid_t) -1)
+ err = diskfs_validate_owner_change (np, uid);
+ if (!err && gid != (gid_t) -1)
+ err = diskfs_validate_group_change (np, gid);
+ if (!err)
+ {
+ if (uid != (uid_t) -1)
+ {
+ np->dn_stat.st_uid = uid;
+ if (np->author_tracks_uid)
+ np->dn_stat.st_author = uid;
+ }
+ if (gid != (gid_t) -1)
+ np->dn_stat.st_gid = gid;
+ np->dn_set_ctime = 1;
+ if (np->filemod_reqs)
+ diskfs_notice_filechange(np,
+ FILE_CHANGED_META,
+ 0, 0);
+ }
+ }
+ }));
+}
diff --git a/libdiskfs/file-exec.c b/libdiskfs/file-exec.c
new file mode 100644
index 00000000..9572dbed
--- /dev/null
+++ b/libdiskfs/file-exec.c
@@ -0,0 +1,212 @@
+/* File execution (file_exec RPC) for diskfs servers, using exec server.
+ Copyright (C) 1993,94,95,96,97,98,2000,02 Free Software Foundation, Inc.
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <hurd/exec.h>
+#include <hurd/paths.h>
+#include <string.h>
+#include <idvec.h>
+
+kern_return_t
+diskfs_S_file_exec (struct protid *cred,
+ task_t task,
+ int flags,
+ char *argv,
+ size_t argvlen,
+ char *envp,
+ size_t envplen,
+ mach_port_t *fds,
+ size_t fdslen,
+ mach_port_t *portarray,
+ size_t portarraylen,
+ int *intarray,
+ size_t intarraylen,
+ mach_port_t *deallocnames,
+ size_t deallocnameslen,
+ mach_port_t *destroynames,
+ size_t destroynameslen)
+{
+ struct node *np;
+ uid_t uid;
+ gid_t gid;
+ mode_t mode;
+ int suid, sgid;
+ struct protid *newpi;
+ struct peropen *newpo;
+ error_t err = 0;
+ mach_port_t execserver;
+ int cached_exec;
+ struct hurd_userlink ulink;
+ mach_port_t right;
+
+#define RETURN(code) do { err = (code); goto out; } while (0)
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ /* Get a light reference to the cached exec server port. */
+ execserver = _hurd_port_get (&_diskfs_exec_portcell, &ulink);
+ cached_exec = (execserver != MACH_PORT_NULL);
+ if (execserver == MACH_PORT_NULL)
+ {
+ /* No cached port. Look up the canonical naming point. */
+ execserver = file_name_lookup (_SERVERS_EXEC, 0, 0);
+ if (execserver == MACH_PORT_NULL)
+ return EOPNOTSUPP; /* No exec server, no exec. */
+ else
+ {
+ /* Install the newly-gotten exec server port for other
+ threads to use, then get a light reference for this call. */
+ _hurd_port_set (&_diskfs_exec_portcell, execserver);
+ execserver = _hurd_port_get (&_diskfs_exec_portcell, &ulink);
+ }
+ }
+
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+ mode = np->dn_stat.st_mode;
+ uid = np->dn_stat.st_uid;
+ gid = np->dn_stat.st_gid;
+ pthread_mutex_unlock (&np->lock);
+
+ if (_diskfs_noexec)
+ RETURN (EACCES);
+
+ if ((cred->po->openstat & O_EXEC) == 0)
+ RETURN (EBADF);
+
+ if (!((mode & (S_IXUSR|S_IXGRP|S_IXOTH))
+ || ((mode & S_IUSEUNK) && (mode & (S_IEXEC << S_IUNKSHIFT)))))
+ RETURN (EACCES);
+
+ if ((mode & S_IFMT) == S_IFDIR)
+ RETURN (EACCES);
+
+ suid = mode & S_ISUID;
+ sgid = mode & S_ISGID;
+ if (!_diskfs_nosuid && (suid || sgid))
+ {
+ int secure = 0;
+ error_t get_file_ids (struct idvec *uids, struct idvec *gids)
+ {
+ error_t err = idvec_merge (uids, cred->user->uids);
+ if (! err)
+ err = idvec_merge (gids, cred->user->gids);
+ return err;
+ }
+ err =
+ fshelp_exec_reauth (suid, uid, sgid, gid,
+ diskfs_auth_server_port, get_file_ids,
+ portarray, portarraylen, fds, fdslen, &secure);
+ if (secure)
+ flags |= EXEC_SECURE | EXEC_NEWTASK;
+ }
+
+ /* If the user can't read the file, then we should use a new task,
+ which would be inaccessible to the user. Actually, this doesn't
+ work, because the proc server will still give out the task port
+ to the user. Too many things depend on that that it can't be
+ changed. So this vague attempt isn't even worth trying. */
+#if 0
+ if (fshelp_access (&np->dn_stat, S_IREAD, cred->user))
+ flags |= EXEC_NEWTASK;
+#endif
+
+ if (! err)
+ /* Make a new peropen for the exec server to access the file, since any
+ seeking the exec server might want to do should not affect the
+ original peropen on which file_exec was called. (The new protid for
+ this peropen clones the caller's iouser to preserve the caller's
+ authentication credentials.) The new peropen's openmodes must have
+ O_READ even if the caller had only O_EXEC privilege, so the exec
+ server can read the executable file. We also include O_EXEC so that
+ the exec server can turn this peropen into a file descriptor in the
+ target process and permit it to exec its /dev/fd/N pseudo-file. */
+ {
+ err = diskfs_make_peropen (np, O_READ|O_EXEC, cred->po, &newpo);
+ if (! err)
+ {
+ err = diskfs_create_protid (newpo, cred->user, &newpi);
+ if (err)
+ diskfs_release_peropen (newpo);
+ }
+ }
+
+ if (! err)
+ {
+ do
+ {
+ right = ports_get_send_right (newpi);
+ err = exec_exec (execserver,
+ right, MACH_MSG_TYPE_COPY_SEND,
+ task, flags, argv, argvlen, envp, envplen,
+ fds, MACH_MSG_TYPE_COPY_SEND, fdslen,
+ portarray, MACH_MSG_TYPE_COPY_SEND, portarraylen,
+ intarray, intarraylen,
+ deallocnames, deallocnameslen,
+ destroynames, destroynameslen);
+ mach_port_deallocate (mach_task_self (), right);
+ if (err == MACH_SEND_INVALID_DEST)
+ {
+ if (cached_exec)
+ {
+ /* We were using a previously looked-up exec server port.
+ Try looking up a new one before giving an error. */
+ cached_exec = 0;
+ _hurd_port_free (&_diskfs_exec_portcell, &ulink, execserver);
+
+ execserver = file_name_lookup (_SERVERS_EXEC, 0, 0);
+ if (execserver == MACH_PORT_NULL)
+ err = EOPNOTSUPP;
+ else
+ {
+ _hurd_port_set (&_diskfs_exec_portcell, execserver);
+ execserver = _hurd_port_get (&_diskfs_exec_portcell,
+ &ulink);
+ }
+ }
+ else
+ err = EOPNOTSUPP;
+ }
+ } while (err == MACH_SEND_INVALID_DEST);
+ ports_port_deref (newpi);
+ }
+
+ if (! err)
+ {
+ unsigned int i;
+
+ mach_port_deallocate (mach_task_self (), task);
+ for (i = 0; i < fdslen; i++)
+ mach_port_deallocate (mach_task_self (), fds[i]);
+ for (i = 0; i < portarraylen; i++)
+ mach_port_deallocate (mach_task_self (), portarray[i]);
+ }
+
+ out:
+ _hurd_port_free (&_diskfs_exec_portcell, &ulink, execserver);
+
+ return err;
+}
diff --git a/libdiskfs/file-get-children.c b/libdiskfs/file-get-children.c
new file mode 100644
index 00000000..4581e4e0
--- /dev/null
+++ b/libdiskfs/file-get-children.c
@@ -0,0 +1,95 @@
+/* file_get_children
+
+ Copyright (C) 2013 Free Software Foundation, Inc.
+
+ Written by Justus Winter <4winter@informatik.uni-hamburg.de>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+#include <argz.h>
+
+/* Return any active translators bound to nodes of the receiving
+ filesystem. CHILDREN is an argz vector containing file names
+ relative to the root of the receiving translator. */
+error_t
+diskfs_S_file_get_children (struct protid *cred,
+ char **children,
+ mach_msg_type_number_t *children_len)
+{
+ error_t err;
+ if (! cred
+ || cred->pi.bucket != diskfs_port_bucket
+ || cred->pi.class != diskfs_protid_class)
+ return EOPNOTSUPP;
+
+ /* check_access performs the same permission check as is normally
+ done, i.e. it checks that all but the last path components are
+ executable by the requesting user and that the last component is
+ readable. */
+ error_t check_access (const char *path)
+ {
+ error_t err;
+ char *elements = NULL;
+ size_t elements_len = 0;
+
+ err = argz_create_sep (path, '/', &elements, &elements_len);
+ if (err)
+ return err;
+
+ struct node *dp = diskfs_root_node;
+
+ for (char *entry = elements;
+ entry;
+ entry = argz_next (elements, elements_len, entry))
+ {
+ struct node *next;
+ err = diskfs_lookup (dp, entry, LOOKUP, &next, NULL, cred);
+
+ if (dp != diskfs_root_node)
+ diskfs_nput (dp);
+
+ if (err)
+ return err;
+
+ dp = next;
+ }
+
+ err = fshelp_access (&dp->dn_stat, S_IRUSR, cred->user);
+ diskfs_nput (dp);
+ return err;
+ }
+
+
+ char *c = NULL;
+ size_t c_len = 0;
+
+ err = fshelp_get_active_translators (&c, &c_len, check_access);
+ if (err)
+ goto errout;
+
+ err = iohelp_return_malloced_buffer (c, c_len, children, children_len);
+ if (err)
+ goto errout;
+
+ c = NULL; /* c was freed by iohelp_return_malloced_buffer. */
+
+ errout:
+ free (c);
+ return err;
+}
diff --git a/libdiskfs/file-get-fs-opts.c b/libdiskfs/file-get-fs-opts.c
new file mode 100644
index 00000000..d7593118
--- /dev/null
+++ b/libdiskfs/file-get-fs-opts.c
@@ -0,0 +1,54 @@
+/* Get run-time file system options
+
+ Copyright (C) 1995,96,98,2002 Free Software Foundation, Inc.
+ Written by Miles Bader <miles@gnu.org>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <errno.h>
+#include <string.h>
+#include <argz.h>
+#include "priv.h"
+#include "fs_S.h"
+
+error_t
+diskfs_S_file_get_fs_options (struct protid *cred,
+ char **data, size_t *data_len)
+{
+ error_t err;
+ char *argz = 0;
+ size_t argz_len = 0;
+
+ if (! cred)
+ return EOPNOTSUPP;
+
+ err = argz_add (&argz, &argz_len, program_invocation_name);
+ if (err)
+ return err;
+
+ pthread_rwlock_rdlock (&diskfs_fsys_lock);
+ err = diskfs_append_args (&argz, &argz_len);
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+
+ if (! err)
+ /* Move ARGZ from a malloced buffer into a vm_alloced one. */
+ err = iohelp_return_malloced_buffer (argz, argz_len, data, data_len);
+ else
+ free (argz);
+
+ return err;
+}
diff --git a/libdiskfs/file-get-source.c b/libdiskfs/file-get-source.c
new file mode 100644
index 00000000..b5c31845
--- /dev/null
+++ b/libdiskfs/file-get-source.c
@@ -0,0 +1,37 @@
+/* file_get_source
+
+ Copyright (C) 2013 Free Software Foundation, Inc.
+
+ Written by Justus Winter <4winter@informatik.uni-hamburg.de>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Return information about the source of the receiving
+ filesystem. */
+error_t
+diskfs_S_file_get_source (struct protid *cred,
+ char *source)
+{
+ if (! cred
+ || cred->pi.bucket != diskfs_port_bucket
+ || cred->pi.class != diskfs_protid_class)
+ return EOPNOTSUPP;
+
+ return diskfs_get_source (cred, source, 1024 /* XXX */);
+}
diff --git a/libdiskfs/file-get-trans.c b/libdiskfs/file-get-trans.c
new file mode 100644
index 00000000..db5bbdad
--- /dev/null
+++ b/libdiskfs/file-get-trans.c
@@ -0,0 +1,138 @@
+/* libdiskfs implementation of fs.defs: file_get_translator
+ Copyright (C) 1992,93,94,95,96,98,99,2002 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include <hurd/paths.h>
+#include <string.h>
+#include <stdio.h>
+#include "fs_S.h"
+
+/* Implement file_get_translator as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_get_translator (struct protid *cred,
+ char **trans,
+ size_t *translen)
+{
+ struct node *np;
+ error_t err = 0;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+
+ /* First look for short-circuited translators. */
+ if (S_ISLNK (np->dn_stat.st_mode))
+ {
+ unsigned int len = sizeof _HURD_SYMLINK + np->dn_stat.st_size + 1;
+ size_t amt;
+ assert (diskfs_shortcut_symlink);
+ if (len > *translen)
+ *trans = mmap (0, len, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ bcopy (_HURD_SYMLINK, *trans, sizeof _HURD_SYMLINK);
+
+ if (diskfs_read_symlink_hook)
+ err = (*diskfs_read_symlink_hook) (np,
+ *trans + sizeof _HURD_SYMLINK);
+ if (!diskfs_read_symlink_hook || err == EINVAL)
+ {
+ err = diskfs_node_rdwr (np, *trans + sizeof _HURD_SYMLINK,
+ 0, np->dn_stat.st_size, 0, cred, &amt);
+ if (!err)
+ assert (amt == np->dn_stat.st_size);
+ }
+ if (!err)
+ {
+ (*trans)[sizeof _HURD_SYMLINK + np->dn_stat.st_size] = '\0';
+ *translen = len;
+ }
+ else if (len > *translen)
+ munmap (trans, len);
+ }
+ else if (S_ISCHR (np->dn_stat.st_mode) || S_ISBLK (np->dn_stat.st_mode))
+ {
+ char *buf;
+ unsigned int buflen;
+
+ if (S_ISCHR (np->dn_stat.st_mode))
+ assert (diskfs_shortcut_chrdev);
+ else
+ assert (diskfs_shortcut_blkdev);
+
+ buflen = asprintf (&buf, "%s%c%d%c%d",
+ (S_ISCHR (np->dn_stat.st_mode)
+ ? _HURD_CHRDEV
+ : _HURD_BLKDEV),
+ '\0', (np->dn_stat.st_rdev >> 8) & 0377,
+ '\0', (np->dn_stat.st_rdev) & 0377);
+ buflen++; /* terminating nul */
+
+ if (buflen > *translen)
+ *trans = mmap (0, buflen, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ bcopy (buf, *trans, buflen);
+ free (buf);
+ *translen = buflen;
+ err = 0;
+ }
+ else if (S_ISFIFO (np->dn_stat.st_mode))
+ {
+ unsigned int len;
+
+ len = sizeof _HURD_FIFO;
+ if (len > *translen)
+ *trans = mmap (0, len, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ bcopy (_HURD_FIFO, *trans, sizeof _HURD_FIFO);
+ *translen = len;
+ err = 0;
+ }
+ else if (S_ISSOCK (np->dn_stat.st_mode))
+ {
+ unsigned int len;
+
+ len = sizeof _HURD_IFSOCK;
+ if (len > *translen)
+ *trans = mmap (0, len, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ bcopy (_HURD_IFSOCK, *trans, sizeof _HURD_IFSOCK);
+ *translen = len;
+ err = 0;
+ }
+ else
+ {
+ if (! (np->dn_stat.st_mode & S_IPTRANS))
+ err = EINVAL;
+ else
+ {
+ char *string;
+ u_int len;
+ err = diskfs_get_translator (np, &string, &len);
+ if (!err)
+ {
+ if (len > *translen)
+ *trans = mmap (0, len, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ bcopy (string, *trans, len);
+ *translen = len;
+ free (string);
+ }
+ }
+ }
+
+ pthread_mutex_unlock (&np->lock);
+
+ return err;
+}
diff --git a/libdiskfs/file-get-transcntl.c b/libdiskfs/file-get-transcntl.c
new file mode 100644
index 00000000..311d23ef
--- /dev/null
+++ b/libdiskfs/file-get-transcntl.c
@@ -0,0 +1,48 @@
+/* libkdiskfs implementation of fs.defs: file_get_translator_cntl
+ Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement file_get_translator_cntl as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_get_translator_cntl (struct protid *cred,
+ mach_port_t *ctl,
+ mach_msg_type_name_t *ctltype)
+{
+ struct node *np;
+ error_t err;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+
+ err = fshelp_isowner (&np->dn_stat, cred->user);
+ if (!err)
+ err = fshelp_fetch_control (&np->transbox, ctl);
+ if (!err && *ctl == MACH_PORT_NULL)
+ err = ENXIO;
+ if (!err)
+ *ctltype = MACH_MSG_TYPE_MOVE_SEND;
+
+ pthread_mutex_unlock (&np->lock);
+
+ return err;
+}
diff --git a/libdiskfs/file-getcontrol.c b/libdiskfs/file-getcontrol.c
new file mode 100644
index 00000000..fc6f777e
--- /dev/null
+++ b/libdiskfs/file-getcontrol.c
@@ -0,0 +1,51 @@
+/* libdiskfs implementation of fs.defs:file_getcontrol.c
+ Copyright (C) 1992,93,94,95,96,2001 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <hurd/fshelp.h>
+
+/* Implement file_getcontrol as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_getcontrol (struct protid *cred,
+ mach_port_t *control,
+ mach_msg_type_name_t *controltype)
+{
+ int error;
+ struct port_info *newpi;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ error = fshelp_iscontroller (&diskfs_root_node->dn_stat, cred->user);
+ if (error)
+ return error;
+
+ error = ports_create_port (diskfs_control_class, diskfs_port_bucket,
+ sizeof (struct port_info), &newpi);
+ if (error)
+ return error;
+
+ pthread_spin_lock (&_diskfs_control_lock);
+ _diskfs_ncontrol_ports++;
+ pthread_spin_unlock (&_diskfs_control_lock);
+ *control = ports_get_right (newpi);
+ *controltype = MACH_MSG_TYPE_MAKE_SEND;
+ ports_port_deref (newpi);
+
+ return 0;
+}
diff --git a/libdiskfs/file-getfh.c b/libdiskfs/file-getfh.c
new file mode 100644
index 00000000..035705b5
--- /dev/null
+++ b/libdiskfs/file-getfh.c
@@ -0,0 +1,60 @@
+/* Return a file handle (for nfs server support)
+
+ Copyright (C) 1997,99,2002 Free Software Foundation, Inc.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd; see the file COPYING. If not, write to
+ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <string.h>
+
+#include "priv.h"
+#include "fs_S.h"
+#include "fhandle.h"
+
+/* Return an NFS file handle for CRED in FH & FN_LEN. */
+error_t
+diskfs_S_file_getfh (struct protid *cred, char **fh, size_t *fh_len)
+{
+ struct node *node;
+ union diskfs_fhandle *f;
+
+ if (! cred)
+ return EOPNOTSUPP;
+
+ if (! idvec_contains (cred->user->uids, 0))
+ return EPERM;
+
+ assert (sizeof *f == sizeof f->bytes);
+
+ node = cred->po->np;
+
+ pthread_mutex_lock (&node->lock);
+
+ if (*fh_len < sizeof (union diskfs_fhandle))
+ *fh = mmap (0, sizeof (union diskfs_fhandle), PROT_READ|PROT_WRITE,
+ MAP_ANON, 0, 0);
+ *fh_len = sizeof *f;
+
+ f = (union diskfs_fhandle *) *fh;
+
+ bzero (f, sizeof *f);
+ f->data.cache_id = node->cache_id;
+ f->data.gen = node->dn_stat.st_gen;
+
+ pthread_mutex_unlock (&node->lock);
+
+ return 0;
+}
diff --git a/libdiskfs/file-getlinknode.c b/libdiskfs/file-getlinknode.c
new file mode 100644
index 00000000..4b346c96
--- /dev/null
+++ b/libdiskfs/file-getlinknode.c
@@ -0,0 +1,39 @@
+/* libdiskfs implementation of fs.defs: file_getlinknode
+ Copyright (C) 1992, 1993, 1994 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement file_getlinknode as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_getlinknode (struct protid *cred,
+ file_t *port,
+ mach_msg_type_name_t *portpoly)
+{
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ if (np == diskfs_root_node)
+ return EBUSY;
+
+ *port = ports_get_right (cred);
+ *portpoly = MACH_MSG_TYPE_MAKE_SEND;
+ return 0;
+}
diff --git a/libdiskfs/file-lock-stat.c b/libdiskfs/file-lock-stat.c
new file mode 100644
index 00000000..4c371e29
--- /dev/null
+++ b/libdiskfs/file-lock-stat.c
@@ -0,0 +1,38 @@
+/*
+ Copyright (C) 1994, 1995 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+kern_return_t
+diskfs_S_file_lock_stat (struct protid *cred,
+ int *mystatus,
+ int *otherstatus)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ pthread_mutex_lock (&cred->po->np->lock);
+ *mystatus = cred->po->lock_status;
+ *otherstatus = cred->po->np->userlock.type;
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return 0;
+}
diff --git a/libdiskfs/file-lock.c b/libdiskfs/file-lock.c
new file mode 100644
index 00000000..bff1df7b
--- /dev/null
+++ b/libdiskfs/file-lock.c
@@ -0,0 +1,36 @@
+/*
+ Copyright (C) 1993, 1994 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+kern_return_t
+diskfs_S_file_lock (struct protid *cred, int flags)
+{
+ error_t err;
+ if (!cred)
+ return EOPNOTSUPP;
+ pthread_mutex_lock (&cred->po->np->lock);
+ err = fshelp_acquire_lock (&cred->po->np->userlock, &cred->po->lock_status,
+ &cred->po->np->lock, flags);
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return err;
+}
diff --git a/libdiskfs/file-reparent.c b/libdiskfs/file-reparent.c
new file mode 100644
index 00000000..da6a51d2
--- /dev/null
+++ b/libdiskfs/file-reparent.c
@@ -0,0 +1,70 @@
+/* Reparent a file
+
+ Copyright (C) 1997,2002 Free Software Foundation
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd; see the file COPYING. If not, write to
+ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+error_t
+diskfs_S_file_reparent (struct protid *cred, mach_port_t parent,
+ mach_port_t *new, mach_msg_type_name_t *new_type)
+{
+ error_t err;
+ struct node *node;
+ struct protid *new_cred;
+ struct peropen *new_po;
+
+ if (! cred)
+ return EOPNOTSUPP;
+
+ node = cred->po->np;
+
+ pthread_mutex_lock (&node->lock);
+ err = diskfs_make_peropen (node, cred->po->openstat, cred->po, &new_po);
+ if (! err)
+ {
+ err = diskfs_create_protid (new_po, cred->user, &new_cred);
+ if (err)
+ diskfs_release_peropen (new_po);
+ }
+ pthread_mutex_unlock (&node->lock);
+
+ if (! err)
+ {
+ /* Remove old shadow root state. */
+ if (new_cred->po->shadow_root && new_cred->po->shadow_root != node)
+ diskfs_nrele (new_cred->po->shadow_root);
+ if (new_cred->po->shadow_root_parent)
+ mach_port_deallocate (mach_task_self (),
+ new_cred->po->shadow_root_parent);
+
+ /* And install PARENT instead. */
+ new_cred->po->shadow_root = node;
+ new_cred->po->shadow_root_parent = parent;
+
+ *new = ports_get_right (new_cred);
+ *new_type = MACH_MSG_TYPE_MAKE_SEND;
+
+ ports_port_deref (new_cred);
+ }
+
+ return err;
+}
diff --git a/libdiskfs/file-set-size.c b/libdiskfs/file-set-size.c
new file mode 100644
index 00000000..fe2125ae
--- /dev/null
+++ b/libdiskfs/file-set-size.c
@@ -0,0 +1,55 @@
+/* libdiskfs implementation of fs.defs: file_set_size
+ Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <fcntl.h>
+
+/* Implement file_set_size as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_set_size (struct protid *cred,
+ off_t size)
+{
+ CHANGE_NODE_FIELD (cred,
+ ({
+ if (!(cred->po->openstat & O_WRITE) || (size < 0))
+ err = EINVAL;
+ else if (size < np->dn_stat.st_size)
+ {
+ err = diskfs_truncate (np, size);
+ if (!err && np->filemod_reqs)
+ diskfs_notice_filechange (np,
+ FILE_CHANGED_TRUNCATE,
+ 0, size);
+ }
+ else if (size > np->dn_stat.st_size)
+ {
+ err = diskfs_grow (np, size, cred);
+ if (! err)
+ {
+ np->dn_stat.st_size = size;
+ np->dn_set_ctime = np->dn_set_mtime = 1;
+ if (np->filemod_reqs)
+ diskfs_notice_filechange (np,
+ FILE_CHANGED_EXTEND,
+ 0, size);
+ }
+ }
+ else
+ err = 0; /* Setting to same size. */
+ }));
+}
diff --git a/libdiskfs/file-set-trans.c b/libdiskfs/file-set-trans.c
new file mode 100644
index 00000000..6e1a61d2
--- /dev/null
+++ b/libdiskfs/file-set-trans.c
@@ -0,0 +1,215 @@
+/* libdiskfs implementation of fs.defs: file_set_translator
+ Copyright (C) 1992,93,94,95,96,99,2001,02,13,14
+ Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <hurd/paths.h>
+#include <hurd/fsys.h>
+
+/* Implement file_set_translator as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_set_translator (struct protid *cred,
+ int passive_flags,
+ int active_flags,
+ int killtrans_flags,
+ char *passive,
+ size_t passivelen,
+ fsys_t active)
+{
+ struct node *np;
+ error_t err;
+ mach_port_t control = MACH_PORT_NULL;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ if (!(passive_flags & FS_TRANS_SET) && !(active_flags & FS_TRANS_SET))
+ return 0;
+
+ if ((passive_flags & FS_TRANS_SET) && diskfs_check_readonly ())
+ return EROFS;
+
+ if (passivelen && passive[passivelen - 1])
+ return EINVAL;
+
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+
+ err = fshelp_isowner (&np->dn_stat, cred->user);
+ if (err)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return err;
+ }
+
+ if ((active_flags & FS_TRANS_SET)
+ && ! (active_flags & FS_TRANS_ORPHAN))
+ {
+ err = fshelp_fetch_control (&np->transbox, &control);
+ if (err)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return err;
+ }
+
+ if ((control != MACH_PORT_NULL) && ((active_flags & FS_TRANS_EXCL) == 0))
+ {
+ pthread_mutex_unlock (&np->lock);
+ err = fsys_goaway (control, killtrans_flags);
+ mach_port_deallocate (mach_task_self (), control);
+ if (err && (err != MIG_SERVER_DIED)
+ && (err != MACH_SEND_INVALID_DEST))
+ return err;
+ err = 0;
+ pthread_mutex_lock (&np->lock);
+ }
+ else if (control != MACH_PORT_NULL)
+ mach_port_deallocate (mach_task_self (), control);
+ }
+
+ /* Handle exclusive passive bit *first*. */
+ if ((passive_flags & FS_TRANS_SET)
+ && (passive_flags & FS_TRANS_EXCL)
+ && (np->dn_stat.st_mode & S_IPTRANS))
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EBUSY;
+ }
+
+ if (active_flags & FS_TRANS_SET)
+ {
+ err = fshelp_set_active (&np->transbox, active,
+ active_flags & FS_TRANS_EXCL);
+ if (err)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return err;
+ }
+ }
+
+ /* Set passive translator */
+ if (passive_flags & FS_TRANS_SET)
+ {
+ if (!(passive_flags & FS_TRANS_FORCE))
+ {
+ /* Handle the short-circuited translators */
+ mode_t newmode = 0;
+
+ if (diskfs_shortcut_symlink && !strcmp (passive, _HURD_SYMLINK))
+ newmode = S_IFLNK;
+ else if (diskfs_shortcut_chrdev && !(strcmp (passive, _HURD_CHRDEV)))
+ newmode = S_IFCHR;
+ else if (diskfs_shortcut_blkdev && !strcmp (passive, _HURD_BLKDEV))
+ newmode = S_IFBLK;
+ else if (diskfs_shortcut_fifo && !strcmp (passive, _HURD_FIFO))
+ newmode = S_IFIFO;
+ else if (diskfs_shortcut_ifsock && !strcmp (passive, _HURD_IFSOCK))
+ newmode = S_IFSOCK;
+
+ if (newmode)
+ {
+ if (S_ISDIR (np->dn_stat.st_mode))
+ {
+ /* We can't allow this, because if the mode of the directory
+ changes, the links will be lost. Perhaps it might be
+ allowed for empty directories, but that's too much of a
+ pain. */
+ pthread_mutex_unlock (&np->lock);
+ return EISDIR;
+ }
+ if (newmode == S_IFBLK || newmode == S_IFCHR)
+ {
+ /* Find the device number from the arguments
+ of the translator. */
+ int major, minor;
+ char *arg;
+
+ arg = passive + strlen (passive) + 1;
+ assert (arg <= passive + passivelen);
+ if (arg == passive + passivelen)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EINVAL;
+ }
+ major = strtol (arg, 0, 0);
+
+ arg = arg + strlen (arg) + 1;
+ assert (arg < passive + passivelen);
+ if (arg == passive + passivelen)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EINVAL;
+ }
+ minor = strtol (arg, 0, 0);
+
+ err = diskfs_validate_rdev_change (np,
+ makedev (major, minor));
+ if (err)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return err;
+ }
+ np->dn_stat.st_rdev = makedev (major, minor);
+ }
+
+ diskfs_truncate (np, 0);
+ if (newmode == S_IFLNK)
+ {
+ char *arg = passive + strlen (passive) + 1;
+ assert (arg <= passive + passivelen);
+ if (arg == passive + passivelen)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EINVAL;
+ }
+
+ if (diskfs_create_symlink_hook)
+ err = (*diskfs_create_symlink_hook)(np, arg);
+ if (!diskfs_create_symlink_hook || err == EINVAL)
+ /* Store the argument in the file as the
+ target of the link */
+ err = diskfs_node_rdwr (np, arg, 0, strlen (arg),
+ 1, cred, 0);
+ if (err)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return err;
+ }
+ }
+ newmode = (np->dn_stat.st_mode & ~S_IFMT) | newmode;
+ err = diskfs_validate_mode_change (np, newmode);
+ if (!err)
+ {
+ np->dn_stat.st_mode = newmode;
+ diskfs_node_update (np, diskfs_synchronous);
+ }
+ pthread_mutex_unlock (&np->lock);
+ return err;
+ }
+ }
+ err = diskfs_set_translator (np, passive, passivelen, cred);
+ }
+
+ pthread_mutex_unlock (&np->lock);
+
+ if (! err && cred->po->path && active_flags & FS_TRANS_SET)
+ err = fshelp_set_active_translator (&cred->pi, cred->po->path, active);
+
+ return err;
+}
diff --git a/libdiskfs/file-statfs.c b/libdiskfs/file-statfs.c
new file mode 100644
index 00000000..817b0115
--- /dev/null
+++ b/libdiskfs/file-statfs.c
@@ -0,0 +1,50 @@
+/* libdiskfs implementation of fs.defs: file_statfs
+ Copyright (C) 1992,93,94,98,2000 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <string.h>
+#include <sys/statvfs.h>
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement file_getcontrol as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_statfs (struct protid *file,
+ fsys_statfsbuf_t *statbuf)
+{
+ if (!file)
+ return EOPNOTSUPP;
+
+ /* Start will all zeros, so the fs can skip fields for which
+ it has no information to contribute. */
+ bzero (statbuf, sizeof *statbuf);
+
+ if (diskfs_readonly)
+ statbuf->f_flag |= ST_RDONLY;
+ if (_diskfs_nosuid)
+ statbuf->f_flag |= ST_NOSUID;
+ if (_diskfs_noexec)
+ statbuf->f_flag |= ST_NOEXEC;
+ if (diskfs_synchronous)
+ statbuf->f_flag |= ST_SYNCHRONOUS;
+
+ diskfs_set_statfs (statbuf);
+
+ statbuf->f_namelen = diskfs_name_max;
+
+ return 0;
+}
diff --git a/libdiskfs/file-sync.c b/libdiskfs/file-sync.c
new file mode 100644
index 00000000..20fa005c
--- /dev/null
+++ b/libdiskfs/file-sync.c
@@ -0,0 +1,42 @@
+/* libdiskfs implementation of fs.defs: file_seek
+ Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement file_sync as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_sync (struct protid *cred,
+ int wait,
+ int omitmetadata)
+{
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ if (diskfs_synchronous)
+ wait = 1;
+
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+ iohelp_get_conch (&np->conch);
+ pthread_mutex_unlock (&np->lock);
+ diskfs_file_update (np, wait);
+ return 0;
+}
diff --git a/libdiskfs/file-syncfs.c b/libdiskfs/file-syncfs.c
new file mode 100644
index 00000000..b7d20a88
--- /dev/null
+++ b/libdiskfs/file-syncfs.c
@@ -0,0 +1,61 @@
+/* libdiskfs implementation of fs.defs: file_syncfs
+ Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+#include <hurd/fsys.h>
+
+/* Implement file_syncfs as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_syncfs (struct protid *cred,
+ int wait,
+ int dochildren)
+{
+ error_t
+ helper (struct node *np)
+ {
+ error_t error;
+ mach_port_t control;
+
+ error = fshelp_fetch_control (&np->transbox, &control);
+ pthread_mutex_unlock (&np->lock);
+ if (!error && (control != MACH_PORT_NULL))
+ {
+ fsys_syncfs (control, wait, 1);
+ mach_port_deallocate (mach_task_self (), control);
+ }
+ pthread_mutex_lock (&np->lock);
+ return 0;
+ }
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ if (dochildren)
+ diskfs_node_iterate (helper);
+
+ if (diskfs_synchronous)
+ wait = 1;
+
+ if (! diskfs_readonly)
+ {
+ diskfs_sync_everything (wait);
+ diskfs_set_hypermetadata (wait, 0);
+ }
+
+ return 0;
+}
diff --git a/libdiskfs/file-utimes.c b/libdiskfs/file-utimes.c
new file mode 100644
index 00000000..39fac504
--- /dev/null
+++ b/libdiskfs/file-utimes.c
@@ -0,0 +1,57 @@
+/* libdiskfs implementation of fs.defs: file_utimes
+ Copyright (C) 1992, 1993, 1994, 1998, 1999 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fs_S.h"
+
+/* Implement file_utimes as described in <hurd/fs.defs>. */
+kern_return_t
+diskfs_S_file_utimes (struct protid *cred,
+ time_value_t atime,
+ time_value_t mtime)
+{
+ CHANGE_NODE_FIELD (cred,
+ ({
+ if (!(err = fshelp_isowner (&np->dn_stat, cred->user)))
+ {
+ if (atime.microseconds == -1)
+ np->dn_set_atime = 1;
+ else
+ {
+ np->dn_stat.st_atim.tv_sec = atime.seconds;
+ np->dn_stat.st_atim.tv_nsec = atime.microseconds * 1000;
+ np->dn_set_atime = 0;
+ }
+
+ if (mtime.microseconds == -1)
+ np->dn_set_mtime = 1;
+ else
+ {
+ np->dn_stat.st_mtim.tv_sec = mtime.seconds;
+ np->dn_stat.st_mtim.tv_nsec = mtime.microseconds * 1000;
+ np->dn_set_mtime = 0;
+ }
+
+ np->dn_set_ctime = 1;
+
+ if (np->filemod_reqs)
+ diskfs_notice_filechange (np,
+ FILE_CHANGED_META,
+ 0, 0);
+ }
+ }));
+}
diff --git a/libdiskfs/fsmutations.h b/libdiskfs/fsmutations.h
new file mode 100644
index 00000000..3f9362b9
--- /dev/null
+++ b/libdiskfs/fsmutations.h
@@ -0,0 +1,39 @@
+/*
+ Copyright (C) 1994,2001 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Only CPP macro definitions should go in this file. */
+
+#define FILE_INTRAN protid_t diskfs_begin_using_protid_port (file_t)
+#define FILE_DESTRUCTOR diskfs_end_using_protid_port (protid_t)
+
+#define IO_INTRAN protid_t diskfs_begin_using_protid_port (io_t)
+#define IO_DESTRUCTOR diskfs_end_using_protid_port (protid_t)
+
+#define FSYS_INTRAN control_t diskfs_begin_using_control_port (fsys_t)
+#define FSYS_DESTRUCTOR diskfs_end_using_control_port (control_t)
+
+#define FILE_IMPORTS import "libdiskfs/priv.h";
+#define IO_IMPORTS import "libdiskfs/priv.h";
+#define FSYS_IMPORTS import "libdiskfs/priv.h";
+#define IFSOCK_IMPORTS import "libdiskfs/priv.h";
+
+#define EXEC_STARTUP_INTRAN \
+ bootinfo_t diskfs_begin_using_bootinfo_port (exec_startup_t)
+#define EXEC_STARTUP_DESTRUCTOR \
+ diskfs_end_using_bootinfo (bootinfo_t)
+#define EXEC_STARTUP_IMPORTS \
+ import "libdiskfs/priv.h";
diff --git a/libdiskfs/fsys-forward.c b/libdiskfs/fsys-forward.c
new file mode 100644
index 00000000..87441573
--- /dev/null
+++ b/libdiskfs/fsys-forward.c
@@ -0,0 +1,37 @@
+/* fsys_forward
+
+ Copyright (C) 1995 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd; see the file COPYING. If not, write to
+ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fsys_S.h"
+
+/* Ask SERVER to provide fsys translation service for us. REQUESTOR is
+ the bootstrap port supplied to the original translator, and ARGV are
+ the command line arguments. If the recipient accepts the request, he
+ (or some delegate) should send fsys_startup to REQUESTOR to start the
+ filesystem up. */
+error_t
+diskfs_S_fsys_forward (mach_port_t server,
+ mach_port_t reply, mach_msg_type_name_t reply_type,
+ mach_port_t requestor, char *argz, size_t argz_len)
+{
+ return EOPNOTSUPP;
+}
diff --git a/libdiskfs/fsys-getfile.c b/libdiskfs/fsys-getfile.c
new file mode 100644
index 00000000..9dd5d732
--- /dev/null
+++ b/libdiskfs/fsys-getfile.c
@@ -0,0 +1,104 @@
+/* Return the file for a given handle (for nfs server support)
+
+ Copyright (C) 1997,99,2001,02 Free Software Foundation, Inc.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd; see the file COPYING. If not, write to
+ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <fcntl.h>
+
+#include "priv.h"
+#include "fsys_S.h"
+#include "fhandle.h"
+
+/* Return in FILE & FILE_TYPE the file in FSYS corresponding to the NFS file
+ handle HANDLE & HANDLE_LEN. */
+error_t
+diskfs_S_fsys_getfile (struct diskfs_control *pt,
+ mach_port_t reply, mach_msg_type_name_t reply_type,
+ uid_t *uids, mach_msg_type_number_t nuids,
+ gid_t *gids, mach_msg_type_number_t ngids,
+ char *handle, mach_msg_type_number_t handle_len,
+ mach_port_t *file, mach_msg_type_name_t *file_type)
+{
+ int flags;
+ error_t err;
+ struct node *node;
+ const union diskfs_fhandle *f;
+ struct protid *new_cred;
+ struct peropen *new_po;
+ struct iouser *user;
+
+ if (!pt
+ || pt->pi.class != diskfs_control_class)
+ return EOPNOTSUPP;
+
+ if (handle_len != sizeof *f)
+ {
+ return EINVAL;
+ }
+
+ f = (const union diskfs_fhandle *) handle;
+
+ err = diskfs_cached_lookup (f->data.cache_id, &node);
+ if (err)
+ {
+ return err;
+ }
+
+ if (node->dn_stat.st_gen != f->data.gen)
+ {
+ diskfs_nput (node);
+ return ESTALE;
+ }
+
+ err = iohelp_create_complex_iouser (&user, uids, nuids, gids, ngids);
+ if (err)
+ {
+ diskfs_nput (node);
+ return err;
+ }
+
+ flags = 0;
+ if (! fshelp_access (&node->dn_stat, S_IREAD, user))
+ flags |= O_READ;
+ if (! fshelp_access (&node->dn_stat, S_IEXEC, user))
+ flags |= O_EXEC;
+ if (! fshelp_access (&node->dn_stat, S_IWRITE, user)
+ && ! S_ISDIR (node->dn_stat.st_mode)
+ && ! diskfs_check_readonly ())
+ flags |= O_WRITE;
+
+ err = diskfs_make_peropen (node, flags, 0, &new_po);
+ if (! err)
+ {
+ err = diskfs_create_protid (new_po, user, &new_cred);
+ if (err)
+ diskfs_release_peropen (new_po);
+ }
+
+ iohelp_free_iouser (user);
+
+ diskfs_nput (node);
+
+ if (! err)
+ {
+ *file = ports_get_right (new_cred);
+ *file_type = MACH_MSG_TYPE_MAKE_SEND;
+ }
+
+ return err;
+}
diff --git a/libdiskfs/fsys-getroot.c b/libdiskfs/fsys-getroot.c
new file mode 100644
index 00000000..10793c87
--- /dev/null
+++ b/libdiskfs/fsys-getroot.c
@@ -0,0 +1,207 @@
+/*
+ Copyright (C) 1993,94,95,96,97,98,2002 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include "fsys_S.h"
+#include <hurd/fsys.h>
+#include <fcntl.h>
+
+/* Implement fsys_getroot as described in <hurd/fsys.defs>. */
+kern_return_t
+diskfs_S_fsys_getroot (struct diskfs_control *pt,
+ mach_port_t reply,
+ mach_msg_type_name_t replytype,
+ mach_port_t dotdot,
+ uid_t *uids,
+ size_t nuids,
+ uid_t *gids,
+ size_t ngids,
+ int flags,
+ retry_type *retry,
+ char *retryname,
+ file_t *returned_port,
+ mach_msg_type_name_t *returned_port_poly)
+{
+ error_t err = 0;
+ mode_t type;
+ struct protid *newpi;
+ struct peropen *newpo;
+ struct iouser user;
+ struct peropen peropen_context =
+ {
+ root_parent: dotdot,
+ shadow_root_parent: MACH_PORT_NULL,
+ shadow_root: _diskfs_chroot_directory ? diskfs_root_node : NULL, /* XXX */
+ path: NULL,
+ };
+
+ if (!pt
+ || pt->pi.class != diskfs_control_class)
+ return EOPNOTSUPP;
+
+ flags &= O_HURD;
+
+ user.uids = make_idvec ();
+ user.gids = make_idvec ();
+ idvec_set_ids (user.uids, uids, nuids);
+ idvec_set_ids (user.gids, gids, ngids);
+#define drop_idvec() idvec_free (user.gids); idvec_free (user.uids)
+
+ pthread_rwlock_rdlock (&diskfs_fsys_lock);
+ pthread_mutex_lock (&diskfs_root_node->lock);
+
+ /* This code is similar (but not the same as) the code in
+ dir-lookup.c that does the same thing. Perhaps a way should
+ be found to share the logic. */
+
+ type = diskfs_root_node->dn_stat.st_mode & S_IFMT;
+
+ if (((diskfs_root_node->dn_stat.st_mode & S_IPTRANS)
+ || fshelp_translated (&diskfs_root_node->transbox))
+ && !(flags & O_NOTRANS))
+ {
+ err = fshelp_fetch_root (&diskfs_root_node->transbox,
+ &peropen_context, dotdot, &user, flags,
+ _diskfs_translator_callback1,
+ _diskfs_translator_callback2,
+ retry, retryname, returned_port);
+ if (err != ENOENT)
+ {
+ pthread_mutex_unlock (&diskfs_root_node->lock);
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ drop_idvec ();
+ if (!err)
+ *returned_port_poly = MACH_MSG_TYPE_MOVE_SEND;
+ return err;
+ }
+
+ /* ENOENT means the translator was removed in the interim. */
+ err = 0;
+ }
+
+ if (type == S_IFLNK && !(flags & (O_NOLINK | O_NOTRANS)))
+ {
+ /* Handle symlink interpretation */
+ char pathbuf[diskfs_root_node->dn_stat.st_size + 1];
+
+ if (diskfs_read_symlink_hook)
+ err = (*diskfs_read_symlink_hook) (diskfs_root_node, pathbuf);
+ if (!diskfs_read_symlink_hook || err == EINVAL)
+ {
+ size_t amt = 0;
+ err = diskfs_node_rdwr (diskfs_root_node, pathbuf, 0,
+ diskfs_root_node->dn_stat.st_size, 0,
+ 0, &amt);
+ pathbuf[amt] = '\0';
+ }
+
+ pthread_mutex_unlock (&diskfs_root_node->lock);
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ if (err)
+ {
+ drop_idvec ();
+ return err;
+ }
+
+ if (pathbuf[0] == '/')
+ {
+ *retry = FS_RETRY_MAGICAL;
+ *returned_port = MACH_PORT_NULL;
+ *returned_port_poly = MACH_MSG_TYPE_COPY_SEND;
+ strcpy (retryname, pathbuf);
+ mach_port_deallocate (mach_task_self (), dotdot);
+ drop_idvec ();
+ return 0;
+ }
+ else
+ {
+ *retry = FS_RETRY_REAUTH;
+ *returned_port = dotdot;
+ *returned_port_poly = MACH_MSG_TYPE_MOVE_SEND;
+ strcpy (retryname, pathbuf);
+ drop_idvec ();
+ return 0;
+ }
+ }
+
+ if ((type == S_IFSOCK || type == S_IFBLK
+ || type == S_IFCHR || type == S_IFIFO)
+ && (flags & (O_READ|O_WRITE|O_EXEC)))
+ err = EOPNOTSUPP;
+
+ if (!err && (flags & O_READ))
+ err = fshelp_access (&diskfs_root_node->dn_stat, S_IREAD, &user);
+
+ if (!err && (flags & O_EXEC))
+ err = fshelp_access (&diskfs_root_node->dn_stat, S_IEXEC, &user);
+
+ if (!err && (flags & (O_WRITE)))
+ {
+ if (type == S_IFDIR)
+ err = EISDIR;
+ else if (diskfs_check_readonly ())
+ err = EROFS;
+ else
+ err = fshelp_access (&diskfs_root_node->dn_stat,
+ S_IWRITE, &user);
+ }
+
+ if (err)
+ {
+ pthread_mutex_unlock (&diskfs_root_node->lock);
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ drop_idvec ();
+ return err;
+ }
+
+ if ((flags & O_NOATIME)
+ && (fshelp_isowner (&diskfs_root_node->dn_stat, &user)
+ == EPERM))
+ flags &= ~O_NOATIME;
+
+ flags &= ~OPENONLY_STATE_MODES;
+
+ err = diskfs_make_peropen (diskfs_root_node, flags,
+ &peropen_context, &newpo);
+ if (! err)
+ {
+ err = diskfs_create_protid (newpo, &user, &newpi);
+ if (err)
+ diskfs_release_peropen (newpo);
+ }
+
+ if (! err)
+ {
+ mach_port_deallocate (mach_task_self (), dotdot);
+ *retry = FS_RETRY_NORMAL;
+ *retryname = '\0';
+ *returned_port = ports_get_right (newpi);
+ *returned_port_poly = MACH_MSG_TYPE_MAKE_SEND;
+ ports_port_deref (newpi);
+ }
+
+ pthread_mutex_unlock (&diskfs_root_node->lock);
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+
+ drop_idvec ();
+
+ return err;
+}
diff --git a/libdiskfs/fsys-goaway.c b/libdiskfs/fsys-goaway.c
new file mode 100644
index 00000000..b9103873
--- /dev/null
+++ b/libdiskfs/fsys-goaway.c
@@ -0,0 +1,51 @@
+/*
+ Copyright (C) 1993, 1994, 1995 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include "fsys_S.h"
+#include "fsys_reply_U.h"
+
+/* Implement fsys_goaway as described in <hurd/fsys.defs>. */
+error_t
+diskfs_S_fsys_goaway (struct diskfs_control *pt,
+ mach_port_t reply,
+ mach_msg_type_name_t reply_type,
+ int flags)
+{
+ error_t ret;
+
+ if (!pt
+ || pt->pi.class != diskfs_control_class)
+ return EOPNOTSUPP;
+
+ /* XXX FSYS_GOAWAY_NOWAIT not implemented. */
+
+ ret = diskfs_shutdown (flags);
+
+ if (ret == 0)
+ {
+ /* We are supposed to exit, but first notify the caller. */
+ fsys_goaway_reply (reply, reply_type, 0);
+ exit (0);
+ }
+
+ return ret;
+}
diff --git a/libdiskfs/fsys-options.c b/libdiskfs/fsys-options.c
new file mode 100644
index 00000000..b366d143
--- /dev/null
+++ b/libdiskfs/fsys-options.c
@@ -0,0 +1,112 @@
+/* Parse run-time options
+
+ Copyright (C) 1995, 1996, 1998 Free Software Foundation, Inc.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <errno.h>
+#include <argz.h>
+#include <hurd/fsys.h>
+#include <string.h>
+
+#include "priv.h"
+#include "fsys_S.h"
+
+/* Implement fsys_set_options as described in <hurd/fsys.defs>. */
+kern_return_t
+diskfs_S_fsys_set_options (struct diskfs_control *pt,
+ mach_port_t reply,
+ mach_msg_type_name_t replytype,
+ char *data, mach_msg_type_number_t len,
+ int do_children)
+{
+ error_t err = 0;
+
+ error_t
+ helper (struct node *np)
+ {
+ error_t error;
+ mach_port_t control;
+
+ error = fshelp_fetch_control (&np->transbox, &control);
+ pthread_mutex_unlock (&np->lock);
+ if (!error && (control != MACH_PORT_NULL))
+ {
+ error = fsys_set_options (control, data, len, do_children);
+ mach_port_deallocate (mach_task_self (), control);
+ }
+ else
+ error = 0;
+ pthread_mutex_lock (&np->lock);
+
+ if ((error == MIG_SERVER_DIED) || (error == MACH_SEND_INVALID_DEST))
+ error = 0;
+ return error;
+ }
+
+ if (!pt
+ || pt->pi.class != diskfs_control_class)
+ return EOPNOTSUPP;
+
+ if (do_children)
+ {
+ pthread_rwlock_wrlock (&diskfs_fsys_lock);
+ err = diskfs_node_iterate (helper);
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ }
+
+ if (!err)
+ {
+ pthread_rwlock_wrlock (&diskfs_fsys_lock);
+ err = diskfs_set_options (data, len);
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ }
+
+ return err;
+}
+
+/* Implement fsys_get_options as described in <hurd/fsys.defs>. */
+error_t
+diskfs_S_fsys_get_options (struct diskfs_control *port,
+ mach_port_t reply,
+ mach_msg_type_name_t replytype,
+ char **data, mach_msg_type_number_t *data_len)
+{
+ char *argz = 0;
+ size_t argz_len = 0;
+ error_t err;
+
+ if (!port
+ || port->pi.class != diskfs_control_class)
+ return EOPNOTSUPP;
+
+ err = argz_add (&argz, &argz_len, program_invocation_name);
+ if (err)
+ return err;
+
+ pthread_rwlock_rdlock (&diskfs_fsys_lock);
+ err = diskfs_append_args (&argz, &argz_len);
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+
+ if (! err)
+ /* Move ARGZ from a malloced buffer into a vm_alloced one. */
+ err = iohelp_return_malloced_buffer (argz, argz_len, data, data_len);
+ else
+ free (argz);
+
+ return err;
+}
diff --git a/libdiskfs/fsys-startup.c b/libdiskfs/fsys-startup.c
new file mode 100644
index 00000000..f68438e2
--- /dev/null
+++ b/libdiskfs/fsys-startup.c
@@ -0,0 +1,36 @@
+/*
+ Copyright (C) 1993, 1994, 1995 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include "fsys_S.h"
+
+/* Implement fsys_startup as described in <hurd/fsys.defs>. */
+kern_return_t
+diskfs_S_fsys_startup (mach_port_t port,
+ mach_port_t reply,
+ mach_msg_type_name_t replytype,
+ int flags,
+ mach_port_t ctl,
+ mach_port_t *real,
+ mach_msg_type_name_t *realpoly)
+{
+ return diskfs_execboot_fsys_startup (port, flags, ctl, real, realpoly);
+}
diff --git a/libdiskfs/fsys-syncfs.c b/libdiskfs/fsys-syncfs.c
new file mode 100644
index 00000000..4dceed7f
--- /dev/null
+++ b/libdiskfs/fsys-syncfs.c
@@ -0,0 +1,70 @@
+/*
+ Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "fsys_S.h"
+#include <hurd/fsys.h>
+
+/* Implement fsys_syncfs as described in <hurd/fsys.defs>. */
+kern_return_t
+diskfs_S_fsys_syncfs (struct diskfs_control *pi,
+ mach_port_t reply,
+ mach_msg_type_name_t replytype,
+ int wait,
+ int children)
+{
+ error_t
+ helper (struct node *np)
+ {
+ error_t error;
+ mach_port_t control;
+
+ error = fshelp_fetch_control (&np->transbox, &control);
+ pthread_mutex_unlock (&np->lock);
+ if (!error && (control != MACH_PORT_NULL))
+ {
+ fsys_syncfs (control, wait, 1);
+ mach_port_deallocate (mach_task_self (), control);
+ }
+ pthread_mutex_lock (&np->lock);
+ return 0;
+ }
+
+ if (!pi
+ || pi->pi.class != diskfs_control_class)
+ return EOPNOTSUPP;
+
+ pthread_rwlock_rdlock (&diskfs_fsys_lock);
+
+ if (children)
+ diskfs_node_iterate (helper);
+
+ if (diskfs_synchronous)
+ wait = 1;
+
+ if (! diskfs_readonly)
+ {
+ diskfs_sync_everything (wait);
+ diskfs_set_hypermetadata (wait, 0);
+ }
+
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ return 0;
+}
diff --git a/libdiskfs/get-source.c b/libdiskfs/get-source.c
new file mode 100644
index 00000000..43994464
--- /dev/null
+++ b/libdiskfs/get-source.c
@@ -0,0 +1,33 @@
+/* Default version of diskfs_get_source
+
+ Copyright (C) 2013 Free Software Foundation, Inc.
+
+ Written by Justus Winter <4winter@informatik.uni-hamburg.de>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "priv.h"
+
+error_t
+diskfs_get_source (struct protid *cred, char *source, size_t source_len)
+{
+ if (diskfs_disk_name == NULL)
+ return EOPNOTSUPP;
+
+ strncpy (source, diskfs_disk_name, source_len - 1);
+ source[source_len - 1] = '\0';
+ return 0;
+}
diff --git a/libdiskfs/ifsock.c b/libdiskfs/ifsock.c
new file mode 100644
index 00000000..caf66885
--- /dev/null
+++ b/libdiskfs/ifsock.c
@@ -0,0 +1,131 @@
+/* Implement ifsock inteface
+ Copyright (C) 1994, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "ifsock_S.h"
+#include <hurd/paths.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <hurd/socket.h>
+
+static pthread_spinlock_t pflocalserverlock = PTHREAD_SPINLOCK_INITIALIZER;
+static mach_port_t pflocalserver = MACH_PORT_NULL;
+
+kern_return_t
+diskfs_S_ifsock_getsockaddr (struct protid *cred,
+ mach_port_t *address)
+{
+ error_t err;
+ struct node *np;
+ unsigned restart_tries = 0;
+
+ /* Make sure this is a socket */
+ if (!cred)
+ return EOPNOTSUPP;
+ np = cred->po->np;
+
+ retry:
+ pthread_mutex_lock (&np->lock);
+ if ((np->dn_stat.st_mode & S_IFMT) != S_IFSOCK)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EOPNOTSUPP;
+ }
+ err = fshelp_access (&np->dn_stat, S_IWRITE, cred->user);
+ if (err)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return err;
+ }
+
+ if (np->sockaddr == MACH_PORT_NULL)
+ {
+ mach_port_t server;
+ mach_port_t sockaddr;
+ mach_port_t old;
+
+ pthread_mutex_unlock (&np->lock);
+
+ /* Fetch a port to the PF_LOCAL server, caching it. */
+
+ pthread_spin_lock (&pflocalserverlock);
+ if (pflocalserver == MACH_PORT_NULL)
+ {
+ /* Find out who the PF_LOCAL server is. */
+ char buf[100];
+
+ pthread_spin_unlock (&pflocalserverlock);
+
+ /* Look it up */
+ sprintf (buf, "%s/%d", _SERVERS_SOCKET, PF_LOCAL);
+ server = file_name_lookup (buf, 0, 0);
+ if (server == MACH_PORT_NULL)
+ return EIEIO;
+
+ /* Set it unless someone is already here */
+ pthread_spin_lock (&pflocalserverlock);
+ if (pflocalserver != MACH_PORT_NULL)
+ mach_port_deallocate (mach_task_self (), server);
+ else
+ pflocalserver = server;
+ pthread_spin_unlock (&pflocalserverlock);
+
+ goto retry;
+ }
+ server = pflocalserver;
+ pthread_spin_unlock (&pflocalserverlock);
+
+ /* Create an address for the node */
+ err = socket_fabricate_address (server, AF_LOCAL, &sockaddr);
+ if ((err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED)
+ && restart_tries++ == 0)
+ /* The PF_LOCAL server died; try to restart it. */
+ {
+ pthread_spin_lock (&pflocalserverlock);
+ if (pflocalserver == server)
+ pflocalserver = MACH_PORT_NULL;
+ pthread_spin_unlock (&pflocalserverlock);
+ goto retry;
+ }
+ if (err)
+ return EIEIO;
+
+ pthread_mutex_lock (&np->lock);
+ if (np->sockaddr != MACH_PORT_NULL)
+ /* Someone beat us */
+ mach_port_deallocate (mach_task_self (), sockaddr);
+ else
+ {
+ /* The receive right of the sockaddr holds a reference;
+ when we get a dead name on that right we drop our
+ reference. */
+ mach_port_request_notification (mach_task_self (), sockaddr,
+ MACH_NOTIFY_DEAD_NAME, 1,
+ cred->pi.port_right,
+ MACH_MSG_TYPE_MAKE_SEND_ONCE,
+ &old);
+ if (old != MACH_PORT_NULL)
+ mach_port_deallocate (mach_task_self (), old);
+ np->sockaddr = sockaddr;
+ diskfs_nref_light (np);
+ }
+ }
+
+ *address = np->sockaddr;
+ pthread_mutex_unlock (&np->lock);
+ return 0;
+}
diff --git a/libdiskfs/init-first.c b/libdiskfs/init-first.c
new file mode 100644
index 00000000..6dd5fc4f
--- /dev/null
+++ b/libdiskfs/init-first.c
@@ -0,0 +1,65 @@
+/*
+ Copyright (C) 1994,95,97,2001 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include <stdlib.h>
+#include <hurd/ports.h>
+
+static int thread_timeout = 1000 * 60 * 2; /* two minutes */
+static int server_timeout = 1000 * 60 * 10; /* ten minutes */
+
+
+static void *
+master_thread_function (void *demuxer)
+{
+ error_t err;
+
+ do
+ {
+ ports_manage_port_operations_multithread (diskfs_port_bucket,
+ (ports_demuxer_type) demuxer,
+ thread_timeout,
+ server_timeout,
+ 0);
+ err = diskfs_shutdown (0);
+ }
+ while (err);
+
+ exit (0);
+ /* NOTREACHED */
+ return NULL;
+}
+
+void
+diskfs_spawn_first_thread (ports_demuxer_type demuxer)
+{
+ pthread_t thread;
+ error_t err;
+
+ err = pthread_create (&thread, NULL, master_thread_function, demuxer);
+ if (!err)
+ pthread_detach (thread);
+ else
+ {
+ errno = err;
+ perror ("pthread_create");
+ }
+}
diff --git a/libdiskfs/init-init.c b/libdiskfs/init-init.c
new file mode 100644
index 00000000..7a7f2485
--- /dev/null
+++ b/libdiskfs/init-init.c
@@ -0,0 +1,111 @@
+/*
+ Copyright (C) 1994, 95, 96, 97, 98, 99, 2001 Free Software Foundation, Inc.
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include <device/device.h>
+#include <hurd/fsys.h>
+#include <stdio.h>
+#include <maptime.h>
+
+/* For safe inlining of diskfs_node_disknode and
+ diskfs_disknode_node. */
+size_t const _diskfs_sizeof_struct_node = sizeof (struct node);
+
+mach_port_t diskfs_default_pager;
+mach_port_t diskfs_auth_server_port;
+volatile struct mapped_time_value *diskfs_mtime;
+
+pthread_rwlock_t diskfs_fsys_lock = PTHREAD_RWLOCK_INITIALIZER;
+mach_port_t diskfs_fsys_identity;
+
+int _diskfs_nosuid, _diskfs_noexec;
+int _diskfs_noatime;
+
+struct hurd_port _diskfs_exec_portcell;
+
+pthread_spinlock_t diskfs_node_refcnt_lock = PTHREAD_SPINLOCK_INITIALIZER;
+
+pthread_spinlock_t _diskfs_control_lock = PTHREAD_SPINLOCK_INITIALIZER;
+int _diskfs_ncontrol_ports;
+
+struct port_class *diskfs_protid_class;
+struct port_class *diskfs_control_class;
+struct port_class *diskfs_initboot_class;
+struct port_class *diskfs_execboot_class;
+struct port_class *diskfs_shutdown_notification_class;
+
+struct port_bucket *diskfs_port_bucket;
+
+/* Call this after arguments have been parsed to initialize the
+ library. */
+error_t
+diskfs_init_diskfs (void)
+{
+ error_t err;
+
+ if (diskfs_boot_filesystem ())
+ /* This is a boot filesystem, we have to do some things specially. */
+ {
+ mach_port_t host;
+ err = get_privileged_ports (&host, 0);
+ if (! err)
+ {
+ diskfs_default_pager = MACH_PORT_NULL;
+ err = vm_set_default_memory_manager (host, &diskfs_default_pager);
+ mach_port_deallocate (mach_task_self (), host);
+
+ if (!err)
+ err = maptime_map (1, 0, &diskfs_mtime);
+ }
+ }
+ else
+ err = maptime_map (0, 0, &diskfs_mtime);
+
+ if (err)
+ return err;
+
+ err = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE,
+ &diskfs_fsys_identity);
+ if (err)
+ return err;
+
+ diskfs_auth_server_port = getauth ();
+
+ diskfs_protid_class = ports_create_class (diskfs_protid_rele, 0);
+ diskfs_control_class = ports_create_class (_diskfs_control_clean, 0);
+ diskfs_initboot_class = ports_create_class (0, 0);
+ diskfs_execboot_class = ports_create_class (0, 0);
+ diskfs_shutdown_notification_class = ports_create_class (0, 0);
+
+ diskfs_port_bucket = ports_create_bucket ();
+
+ _hurd_port_init (&_diskfs_exec_portcell, MACH_PORT_NULL);
+
+ return 0;
+}
+
+void
+_diskfs_control_clean (void *arg __attribute__ ((unused)))
+{
+ pthread_spin_lock (&_diskfs_control_lock);
+ _diskfs_ncontrol_ports--;
+ pthread_spin_unlock (&_diskfs_control_lock);
+}
diff --git a/libdiskfs/init-main.c b/libdiskfs/init-main.c
new file mode 100644
index 00000000..16fdafa4
--- /dev/null
+++ b/libdiskfs/init-main.c
@@ -0,0 +1,78 @@
+/* diskfs_init_main -- initialize diskfs world, parse arguments, and open store
+ Copyright (C) 1999, 2001 Free Software Foundation, Inc.
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "diskfs.h"
+#include <argp.h>
+#include <assert.h>
+#include <error.h>
+#include <hurd/store.h>
+
+struct store *
+diskfs_init_main (struct argp *startup_argp,
+ int argc, char **argv,
+ struct store_parsed **store_parsed,
+ mach_port_t *bootstrap)
+{
+ error_t err;
+ struct store_argp_params store_params = { 0 };
+ struct store *store;
+
+ /* We must use ARGP_IN_ORDER for the parsing of --boot-command to work. */
+ err = argp_parse (startup_argp ?: &diskfs_store_startup_argp,
+ argc, argv, ARGP_IN_ORDER, NULL,
+ &store_params);
+ assert_perror (err);
+ *store_parsed = store_params.result;
+
+ err = store_parsed_name (*store_parsed, &diskfs_disk_name);
+ if (err)
+ error (2, err, "store_parsed_name");
+
+ /* This must come after the args have been parsed, as this is where the
+ host priv ports are set for booting. */
+ diskfs_console_stdio ();
+
+ if (diskfs_boot_filesystem ())
+ /* We are the bootstrap filesystem. */
+ *bootstrap = MACH_PORT_NULL;
+ else
+ {
+ task_get_bootstrap_port (mach_task_self (), bootstrap);
+ if (*bootstrap == MACH_PORT_NULL)
+ error (2, 0, "Must be started as a translator");
+ }
+
+ /* Initialize the diskfs library. Must come before any other diskfs call. */
+ err = diskfs_init_diskfs ();
+ if (err)
+ error (4, err, "init");
+
+ err = store_parsed_open (*store_parsed, diskfs_readonly ? STORE_READONLY : 0,
+ &store);
+ if (err)
+ error (3, err, "%s", diskfs_disk_name);
+
+ if (store->flags & STORE_HARD_READONLY)
+ diskfs_readonly = diskfs_hard_readonly = 1;
+
+ /* Start the first request thread, to handle RPCs and page requests. */
+ diskfs_spawn_first_thread (diskfs_demuxer);
+
+ return store;
+}
diff --git a/libdiskfs/init-startup.c b/libdiskfs/init-startup.c
new file mode 100644
index 00000000..d10c9641
--- /dev/null
+++ b/libdiskfs/init-startup.c
@@ -0,0 +1,224 @@
+/* diskfs_startup_diskfs -- advertise our fsys control port to our parent FS.
+ Copyright (C) 1994,95,96,98,99,2000,02 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Roland McGrath. */
+
+#include "priv.h"
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <error.h>
+#include <hurd/fsys.h>
+#include <hurd/startup.h>
+#include <pids.h>
+
+#include "startup_S.h"
+
+char *_diskfs_chroot_directory;
+
+mach_port_t
+diskfs_startup_diskfs (mach_port_t bootstrap, int flags)
+{
+ error_t err;
+ mach_port_t realnode, right;
+ struct port_info *newpi;
+
+ if (_diskfs_chroot_directory != NULL)
+ {
+ /* The boot options requested we change to a subdirectory
+ and treat that as the root of the filesystem. */
+ struct node *np, *old;
+ struct protid *rootpi;
+ struct peropen *rootpo;
+
+ /* Skip leading slashes. */
+ while (*_diskfs_chroot_directory == '/')
+ ++_diskfs_chroot_directory;
+
+ pthread_mutex_lock (&diskfs_root_node->lock);
+
+ /* Create a protid we can use in diskfs_lookup. */
+ err = diskfs_make_peropen (diskfs_root_node, O_READ|O_EXEC,
+ 0, &rootpo);
+ assert_perror (err);
+ err = diskfs_create_protid (rootpo, 0, &rootpi);
+ assert_perror (err);
+
+ /* Look up the directory name. */
+ err = diskfs_lookup (diskfs_root_node, _diskfs_chroot_directory,
+ LOOKUP, &np, NULL, rootpi);
+ pthread_mutex_unlock (&diskfs_root_node->lock);
+ ports_port_deref (rootpi);
+
+ if (err == EAGAIN)
+ error (1, 0, "`--virtual-root=%s' specifies the real root directory",
+ _diskfs_chroot_directory);
+ else if (err)
+ error (1, err, "`%s' not found", _diskfs_chroot_directory);
+
+ if (!S_ISDIR (np->dn_stat.st_mode))
+ {
+ pthread_mutex_unlock (&np->lock);
+ error (1, ENOTDIR, "%s", _diskfs_chroot_directory);
+ }
+
+ /* Install this node as the new root, forgetting about the real root
+ node. The last essential piece that makes the virtual root work
+ is in fsys-getroot.c, which sets the first peropen's shadow_root
+ if _diskfs_chroot_directory is non-null. */
+ old = diskfs_root_node;
+ diskfs_root_node = np;
+ pthread_mutex_unlock (&np->lock);
+ diskfs_nput (old);
+ }
+
+ if (bootstrap != MACH_PORT_NULL)
+ {
+ err = ports_create_port (diskfs_control_class, diskfs_port_bucket,
+ sizeof (struct port_info), &newpi);
+ if (! err)
+ {
+ right = ports_get_send_right (newpi);
+ err = fsys_startup (bootstrap, flags, right,
+ MACH_MSG_TYPE_COPY_SEND, &realnode);
+ mach_port_deallocate (mach_task_self (), right);
+ ports_port_deref (newpi);
+ }
+ if (err)
+ error (1, err, "Translator startup failure: fsys_startup");
+
+ mach_port_deallocate (mach_task_self (), bootstrap);
+ _diskfs_ncontrol_ports++;
+
+ _diskfs_init_completed ();
+ }
+ else
+ {
+ realnode = MACH_PORT_NULL;
+
+ /* We are the bootstrap filesystem; do special boot-time setup. */
+ diskfs_start_bootstrap ();
+ }
+
+ if (diskfs_default_sync_interval)
+ /* Start 'em sync'n */
+ diskfs_set_sync_interval (diskfs_default_sync_interval);
+
+ return realnode;
+}
+
+error_t
+diskfs_S_startup_dosync (mach_port_t handle)
+{
+ error_t err = 0;
+ struct port_info *pi
+ = ports_lookup_port (diskfs_port_bucket, handle,
+ diskfs_shutdown_notification_class);
+
+ if (!pi)
+ return EOPNOTSUPP;
+
+ if (! diskfs_readonly)
+ {
+ /* First start a sync so that if something goes wrong
+ we at least get this much done. */
+ diskfs_sync_everything (0);
+ diskfs_set_hypermetadata (0, 0);
+
+ pthread_rwlock_wrlock (&diskfs_fsys_lock);
+
+ /* Permit all the current RPC's to finish, and then suspend new ones */
+ err = ports_inhibit_class_rpcs (diskfs_protid_class);
+ if (! err)
+ {
+ diskfs_sync_everything (1);
+ diskfs_set_hypermetadata (1, 1);
+ _diskfs_diskdirty = 0;
+
+ /* XXX: if some application writes something after that, we will
+ * crash. That is still better than creating pending writes before
+ * poweroff, and thus fsck on next reboot.
+ */
+ diskfs_readonly = 1;
+ diskfs_readonly_changed (1);
+
+ ports_resume_class_rpcs (diskfs_protid_class);
+ }
+
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ }
+
+ ports_port_deref (pi);
+
+ return err;
+}
+
+/* This is called when we have an ordinary environment, complete
+ with proc and auth ports. */
+void
+_diskfs_init_completed ()
+{
+ startup_t init;
+ process_t proc;
+ error_t err;
+ struct port_info *pi;
+ mach_port_t notify;
+ char *name;
+
+ /* Contact the startup server and register our shutdown request.
+ If we get an error, print an informational message. */
+
+ proc = getproc ();
+ assert (proc);
+
+ err = ports_create_port (diskfs_shutdown_notification_class,
+ diskfs_port_bucket, sizeof (struct port_info),
+ &pi);
+ if (err)
+ goto errout;
+
+ /* Mark us as important. */
+ err = proc_mark_important (proc);
+ /* This might fail due to permissions or because the old proc server
+ is still running, ignore any such errors. */
+ if (err && err != EPERM && err != EMIG_BAD_ID)
+ goto errout;
+
+ err = proc_getmsgport (proc, HURD_PID_STARTUP, &init);
+ mach_port_deallocate (mach_task_self (), proc);
+ if (err)
+ goto errout;
+
+ notify = ports_get_send_right (pi);
+ ports_port_deref (pi);
+ asprintf (&name,
+ "%s %s", program_invocation_short_name, diskfs_disk_name ?: "-");
+ err = startup_request_notification (init, notify,
+ MACH_MSG_TYPE_COPY_SEND, name);
+ mach_port_deallocate (mach_task_self (), notify);
+ free (name);
+ if (err)
+ goto errout;
+
+ mach_port_deallocate (mach_task_self (), init);
+ return;
+
+ errout:
+ error (0, err, "Cannot request shutdown notification");
+}
diff --git a/libdiskfs/io-async-icky.c b/libdiskfs/io-async-icky.c
new file mode 100644
index 00000000..22cb3abb
--- /dev/null
+++ b/libdiskfs/io-async-icky.c
@@ -0,0 +1,33 @@
+/*
+ Copyright (C) 1994, 1995 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_get_icky_async_id as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_get_icky_async_id (struct protid *cred,
+ mach_port_t *idport,
+ mach_msg_type_name_t *idport_type)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ *idport = MACH_PORT_NULL;
+ *idport_type = MACH_MSG_TYPE_COPY_SEND;
+ return 0;
+}
diff --git a/libdiskfs/io-async.c b/libdiskfs/io-async.c
new file mode 100644
index 00000000..b619ddde
--- /dev/null
+++ b/libdiskfs/io-async.c
@@ -0,0 +1,30 @@
+/*
+ Copyright (C) 1994, 1995 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_async as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_async (struct protid *cred __attribute__ ((unused)),
+ mach_port_t notify __attribute__ ((unused)),
+ mach_port_t *idport __attribute__ ((unused)),
+ mach_msg_type_name_t *idport_type
+ __attribute__ ((unused)))
+{
+ return EOPNOTSUPP;
+}
diff --git a/libdiskfs/io-duplicate.c b/libdiskfs/io-duplicate.c
new file mode 100644
index 00000000..45c4df5d
--- /dev/null
+++ b/libdiskfs/io-duplicate.c
@@ -0,0 +1,46 @@
+/*
+ Copyright (C) 1994, 1995, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_duplicate as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_duplicate (struct protid *cred,
+ mach_port_t *port,
+ mach_msg_type_name_t *portpoly)
+{
+ error_t err;
+ struct protid *newpi;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ pthread_mutex_lock (&cred->po->np->lock);
+
+ err = diskfs_create_protid (cred->po, cred->user, &newpi);
+ if (! err)
+ {
+ *port = ports_get_right (newpi);
+ *portpoly = MACH_MSG_TYPE_MAKE_SEND;
+ ports_port_deref (newpi);
+ }
+
+ pthread_mutex_unlock (&cred->po->np->lock);
+
+ return err;
+}
diff --git a/libdiskfs/io-get-conch.c b/libdiskfs/io-get-conch.c
new file mode 100644
index 00000000..fa63a398
--- /dev/null
+++ b/libdiskfs/io-get-conch.c
@@ -0,0 +1,44 @@
+/*
+ Copyright (C) 1994, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_get_conch as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_get_conch (struct protid *cred)
+{
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+
+ if (!cred->mapped)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EINVAL;
+ }
+
+ iohelp_handle_io_get_conch (&np->conch, cred, cred->mapped);
+
+ pthread_mutex_unlock (&np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-identity.c b/libdiskfs/io-identity.c
new file mode 100644
index 00000000..0f3fce0f
--- /dev/null
+++ b/libdiskfs/io-identity.c
@@ -0,0 +1,67 @@
+/* libdiskfs implementation of io_identity RPC
+ Copyright (C) 1996,97,98,2001,02 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_identity as described in <hurd/io.defs>. */
+error_t
+diskfs_S_io_identity (struct protid *cred,
+ mach_port_t *id,
+ mach_msg_type_name_t *idtype,
+ mach_port_t *fsys,
+ mach_msg_type_name_t *fsystype,
+ ino_t *fileno)
+{
+ struct node *np;
+ error_t err;
+ ino_t inum;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ pthread_mutex_lock (&np->lock);
+ inum = np->dn_stat.st_ino;
+ pthread_mutex_unlock (&np->lock);
+
+ err = fshelp_get_identity (diskfs_port_bucket, inum, id);
+ if (! err)
+ {
+ if (cred->po->shadow_root && cred->po->shadow_root != diskfs_root_node)
+ {
+ err = fshelp_get_identity (diskfs_port_bucket,
+ cred->po->shadow_root->dn_stat.st_ino,
+ fsys);
+ if (err)
+ mach_port_deallocate (mach_task_self (), *id);
+ }
+ else
+ *fsys = diskfs_fsys_identity;
+ }
+ if (! err)
+ {
+ *idtype = MACH_MSG_TYPE_MAKE_SEND;
+ *fsystype = MACH_MSG_TYPE_MAKE_SEND;
+ *fileno = inum;
+ }
+
+ return err;
+}
diff --git a/libdiskfs/io-map-cntl.c b/libdiskfs/io-map-cntl.c
new file mode 100644
index 00000000..2e9b9e98
--- /dev/null
+++ b/libdiskfs/io-map-cntl.c
@@ -0,0 +1,53 @@
+/*
+ Copyright (C) 1994,96,2002 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <mach/default_pager.h>
+
+/* Implement io_map_cntl as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_map_cntl (struct protid *cred,
+ memory_object_t *ctlobj,
+ mach_msg_type_name_t *ctlobj_type)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ assert (__vm_page_size >= sizeof (struct shared_io));
+ pthread_mutex_lock (&cred->po->np->lock);
+ if (!cred->mapped)
+ {
+ default_pager_object_create (diskfs_default_pager, &cred->shared_object,
+ __vm_page_size);
+ vm_map (mach_task_self (), (vm_address_t *)&cred->mapped, vm_page_size,
+ 0, 1, cred->shared_object, 0, 0,
+ VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE, 0);
+ cred->mapped->shared_page_magic = SHARED_PAGE_MAGIC;
+ cred->mapped->conch_status = USER_HAS_NOT_CONCH;
+ pthread_spin_init (&cred->mapped->lock, PTHREAD_PROCESS_PRIVATE);
+ *ctlobj = cred->shared_object;
+ *ctlobj_type = MACH_MSG_TYPE_COPY_SEND;
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return 0;
+ }
+ else
+ {
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return EBUSY;
+ }
+}
diff --git a/libdiskfs/io-map.c b/libdiskfs/io-map.c
new file mode 100644
index 00000000..843b6b16
--- /dev/null
+++ b/libdiskfs/io-map.c
@@ -0,0 +1,72 @@
+/*
+ Copyright (C) 1994, 1997 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <fcntl.h>
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_map as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_map (struct protid *cred,
+ memory_object_t *rdobj,
+ mach_msg_type_name_t *rdtype,
+ memory_object_t *wrobj,
+ mach_msg_type_name_t *wrtype)
+{
+ int flags;
+ struct node *node;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ *wrobj = *rdobj = MACH_PORT_NULL;
+
+ node = cred->po->np;
+ flags = cred->po->openstat & (O_READ | O_WRITE);
+
+ pthread_mutex_lock (&node->lock);
+ switch (flags)
+ {
+ case O_READ | O_WRITE:
+ *wrobj = *rdobj = diskfs_get_filemap (node, VM_PROT_READ |VM_PROT_WRITE);
+ if (*wrobj == MACH_PORT_NULL)
+ goto error;
+ mach_port_mod_refs (mach_task_self (), *rdobj, MACH_PORT_RIGHT_SEND, 1);
+ break;
+ case O_READ:
+ *rdobj = diskfs_get_filemap (node, VM_PROT_READ);
+ if (*rdobj == MACH_PORT_NULL)
+ goto error;
+ break;
+ case O_WRITE:
+ *wrobj = diskfs_get_filemap (node, VM_PROT_WRITE);
+ if (*wrobj == MACH_PORT_NULL)
+ goto error;
+ break;
+ }
+ pthread_mutex_unlock (&node->lock);
+
+ *rdtype = MACH_MSG_TYPE_MOVE_SEND;
+ *wrtype = MACH_MSG_TYPE_MOVE_SEND;
+
+ return 0;
+
+error:
+ pthread_mutex_unlock (&node->lock);
+ return errno;
+}
diff --git a/libdiskfs/io-modes-get.c b/libdiskfs/io-modes-get.c
new file mode 100644
index 00000000..9ed3b434
--- /dev/null
+++ b/libdiskfs/io-modes-get.c
@@ -0,0 +1,33 @@
+/*
+ Copyright (C) 1994 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_get_openmodes as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_get_openmodes (struct protid *cred,
+ int *bits)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ pthread_mutex_lock (&cred->po->np->lock);
+ *bits = cred->po->openstat;
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-modes-off.c b/libdiskfs/io-modes-off.c
new file mode 100644
index 00000000..b1e8f978
--- /dev/null
+++ b/libdiskfs/io-modes-off.c
@@ -0,0 +1,35 @@
+/*
+ Copyright (C) 1994, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <fcntl.h>
+
+/* Implement io_clear_some_openmodes as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_clear_some_openmodes (struct protid *cred,
+ int offbits)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ pthread_mutex_lock (&cred->po->np->lock);
+ iohelp_get_conch (&cred->po->np->conch);
+ cred->po->openstat &= ~(offbits & HONORED_STATE_MODES);
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-modes-on.c b/libdiskfs/io-modes-on.c
new file mode 100644
index 00000000..c6ac3b12
--- /dev/null
+++ b/libdiskfs/io-modes-on.c
@@ -0,0 +1,35 @@
+/*
+ Copyright (C) 1994, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <fcntl.h>
+
+/* Implement io_set_some_openmodes as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_set_some_openmodes (struct protid *cred,
+ int newbits)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ pthread_mutex_lock (&cred->po->np->lock);
+ iohelp_get_conch (&cred->po->np->conch);
+ cred->po->openstat |= (newbits & HONORED_STATE_MODES);
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-modes-set.c b/libdiskfs/io-modes-set.c
new file mode 100644
index 00000000..a8e2d7fa
--- /dev/null
+++ b/libdiskfs/io-modes-set.c
@@ -0,0 +1,36 @@
+/*
+ Copyright (C) 1994, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <fcntl.h>
+
+/* Implement io_set_all_openmodes as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_set_all_openmodes (struct protid *cred,
+ int newbits)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ pthread_mutex_lock (&cred->po->np->lock);
+ iohelp_get_conch (&cred->po->np->conch);
+ cred->po->openstat &= ~HONORED_STATE_MODES;
+ cred->po->openstat |= (newbits & HONORED_STATE_MODES);
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-owner-get.c b/libdiskfs/io-owner-get.c
new file mode 100644
index 00000000..2f3e7e33
--- /dev/null
+++ b/libdiskfs/io-owner-get.c
@@ -0,0 +1,36 @@
+/*
+ Copyright (C) 1994 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_get_owner as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_get_owner (struct protid *cred,
+ pid_t *owner)
+{
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+ *owner = np->owner;
+ pthread_mutex_unlock (&np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-owner-mod.c b/libdiskfs/io-owner-mod.c
new file mode 100644
index 00000000..938c3d0d
--- /dev/null
+++ b/libdiskfs/io-owner-mod.c
@@ -0,0 +1,36 @@
+/*
+ Copyright (C) 1994 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_mod_owner as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_mod_owner (struct protid *cred,
+ pid_t owner)
+{
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+ np->owner = owner;
+ pthread_mutex_unlock (&np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-pathconf.c b/libdiskfs/io-pathconf.c
new file mode 100644
index 00000000..38e277c3
--- /dev/null
+++ b/libdiskfs/io-pathconf.c
@@ -0,0 +1,80 @@
+/* libdiskfs implementation of io.defs: io_pathconf
+ Copyright (C) 1992, 1993, 1994, 1995, 1999 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <unistd.h>
+#include "priv.h"
+#include "io_S.h"
+#include <dirent.h>
+#include <limits.h>
+
+/* Implement io_pathconf as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_pathconf (struct protid *cred,
+ int name,
+ int *value)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ switch (name)
+ {
+ case _PC_LINK_MAX:
+ *value = diskfs_link_max;
+ break;
+
+ case _PC_MAX_CANON:
+ case _PC_MAX_INPUT:
+ case _PC_PIPE_BUF:
+ case _PC_VDISABLE:
+ case _PC_SOCK_MAXBUF:
+ case _PC_PATH_MAX:
+ *value = -1;
+ break;
+
+ case _PC_NAME_MAX:
+ /* <hurd/hurd_types.defs> string_t constrains the upper bound.
+ The `struct dirent' format defined by libc further contrains it. */
+#define D_NAMLEN_MAX (UCHAR_MAX * sizeof (((struct dirent *) 0)->d_namlen))
+ if (diskfs_name_max > D_NAMLEN_MAX || diskfs_name_max < 0)
+ diskfs_name_max = D_NAMLEN_MAX;
+ *value = diskfs_name_max;
+ break;
+
+ case _PC_NO_TRUNC: /* enforced in diskfs_lookup */
+ *value = 1; /* diskfs_name_max >= 0; */ /* see above */
+ break;
+
+ case _PC_CHOWN_RESTRICTED:
+ case _PC_SYNC_IO:
+ case _PC_ASYNC_IO:
+ *value = 1;
+ break;
+
+ case _PC_PRIO_IO:
+ *value = 0;
+ break;
+
+ case _PC_FILESIZEBITS:
+ *value = 32;
+ break;
+
+ default:
+ return EINVAL;
+ }
+
+ return 0;
+}
diff --git a/libdiskfs/io-prenotify.c b/libdiskfs/io-prenotify.c
new file mode 100644
index 00000000..4eb1c657
--- /dev/null
+++ b/libdiskfs/io-prenotify.c
@@ -0,0 +1,72 @@
+/*
+ Copyright (C) 1994, 1995, 1996, 2001 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_prenotify as described in <hurd/io.defs>.
+
+ We set the prenotify size to be the allocated size of the file;
+ then users are forced to call this routine before writing past
+ that, and we can do allocation (or return ENOSPC if necessary). */
+kern_return_t
+diskfs_S_io_prenotify (struct protid *cred,
+ vm_offset_t start __attribute__ ((unused)),
+ vm_offset_t end)
+{
+ struct node *np;
+ int err = 0;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+
+ /* Clamp it down */
+ pthread_mutex_lock (&np->lock);
+
+ if (!cred->mapped)
+ {
+ err = EINVAL;
+ goto out;
+ }
+
+ err = iohelp_verify_user_conch (&np->conch, cred);
+ if (err)
+ goto out;
+
+ iohelp_fetch_shared_data (cred);
+
+ if ((off_t) end < np->allocsize)
+ {
+ /* The user didn't need to do this, so we'll make sure they
+ have the right shared page info. */
+ pthread_spin_lock (&cred->mapped->lock);
+ iohelp_put_shared_data (cred);
+ pthread_spin_unlock (&cred->mapped->lock);
+ goto out;
+ }
+
+ err = diskfs_grow (np, end, cred);
+ if (diskfs_synchronous)
+ diskfs_node_update (np, 1);
+ if (!err && np->filemod_reqs)
+ diskfs_notice_filechange (np, FILE_CHANGED_EXTEND, 0, end);
+ out:
+ pthread_mutex_unlock (&np->lock);
+ return err;
+}
diff --git a/libdiskfs/io-read.c b/libdiskfs/io-read.c
new file mode 100644
index 00000000..c849434b
--- /dev/null
+++ b/libdiskfs/io-read.c
@@ -0,0 +1,109 @@
+/*
+ Copyright (C) 1994,95,96,97,99,2001 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <fcntl.h>
+
+/* Implement io_read as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_read (struct protid *cred,
+ char **data,
+ mach_msg_type_number_t *datalen,
+ off_t offset,
+ mach_msg_type_number_t maxread)
+{
+ struct node *np;
+ int err;
+ off_t off = offset;
+ char *buf;
+ int ourbuf = 0;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ if (!(cred->po->openstat & O_READ))
+ return EBADF;
+
+ pthread_mutex_lock (&np->lock);
+
+ iohelp_get_conch (&np->conch);
+
+ if (off == -1)
+ off = cred->po->filepointer;
+ if (off < 0)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EINVAL;
+ }
+
+ if (off > np->dn_stat.st_size)
+ maxread = 0;
+ else if (off + (off_t) maxread > np->dn_stat.st_size)
+ maxread = np->dn_stat.st_size - off;
+
+ if (maxread > *datalen)
+ {
+ ourbuf = 1;
+ buf = mmap (0, maxread, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ *data = buf;
+ }
+ else
+ buf = *data;
+
+ *datalen = maxread;
+
+ if (maxread == 0)
+ err = 0;
+ else if (S_ISLNK (np->dn_stat.st_mode))
+ {
+ /* Read from a symlink. */
+ if (! diskfs_read_symlink_hook)
+ err = EINVAL;
+ else
+ {
+ if (off == 0 && maxread == np->dn_stat.st_size)
+ err = (*diskfs_read_symlink_hook)(np, buf);
+ else
+ {
+ char *whole_link = alloca (np->dn_stat.st_size);
+ err = (*diskfs_read_symlink_hook)(np, whole_link);
+ if (! err)
+ memcpy (buf, whole_link + off, maxread);
+ }
+ }
+ }
+ else
+ err = EINVAL; /* Use read below. */
+
+ if (err == EINVAL)
+ err = _diskfs_rdwr_internal (np, buf, off, datalen, 0,
+ cred->po->openstat & O_NOATIME);
+
+ if (diskfs_synchronous)
+ diskfs_node_update (np, 1); /* atime! */
+
+ if (offset == -1 && !err)
+ cred->po->filepointer += *datalen;
+
+ if (err && ourbuf)
+ munmap (buf, maxread);
+
+ pthread_mutex_unlock (&np->lock);
+ return err;
+}
diff --git a/libdiskfs/io-readable.c b/libdiskfs/io-readable.c
new file mode 100644
index 00000000..c3debac3
--- /dev/null
+++ b/libdiskfs/io-readable.c
@@ -0,0 +1,46 @@
+/*
+ Copyright (C) 1994, 1995, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <fcntl.h>
+
+/* Implement io_readable as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_readable (struct protid *cred,
+ mach_msg_type_number_t *amount)
+{
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ if (!(cred->po->openstat & O_READ))
+ return EINVAL;
+
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+ iohelp_get_conch (&np->conch);
+ if (np->dn_stat.st_size > cred->po->filepointer)
+ *amount = np->dn_stat.st_size - cred->po->filepointer;
+ else
+ *amount = 0;
+
+ pthread_mutex_unlock (&np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-reauthenticate.c b/libdiskfs/io-reauthenticate.c
new file mode 100644
index 00000000..69d78bc5
--- /dev/null
+++ b/libdiskfs/io-reauthenticate.c
@@ -0,0 +1,66 @@
+/*
+ Copyright (C) 1994,95,96,2000,01 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_reathenticate as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_reauthenticate (struct protid *cred,
+ mach_port_t rend_port)
+{
+ struct protid *newcred;
+ error_t err;
+ mach_port_t newright;
+ struct iouser *user;
+
+ if (cred == 0)
+ return EOPNOTSUPP;
+
+ /* This routine must carefully ignore EINTR because we
+ are a simpleroutine, so callers won't know to restart. */
+
+ pthread_mutex_lock (&cred->po->np->lock);
+ do
+ err = diskfs_start_protid (cred->po, &newcred);
+ while (err == EINTR);
+ if (err)
+ {
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return err;
+ }
+
+ newright = ports_get_send_right (newcred);
+ assert (newright != MACH_PORT_NULL);
+
+ err = iohelp_reauth (&user, diskfs_auth_server_port, rend_port,
+ newright, 1);
+ if (! err)
+ {
+ diskfs_finish_protid (newcred, user);
+ iohelp_free_iouser (user);
+ mach_port_deallocate (mach_task_self (), rend_port);
+ }
+
+ mach_port_deallocate (mach_task_self (), newright);
+
+ pthread_mutex_unlock (&cred->po->np->lock);
+
+ ports_port_deref (newcred);
+
+ return err;
+}
diff --git a/libdiskfs/io-rel-conch.c b/libdiskfs/io-rel-conch.c
new file mode 100644
index 00000000..a42d1ba5
--- /dev/null
+++ b/libdiskfs/io-rel-conch.c
@@ -0,0 +1,44 @@
+/*
+ Copyright (C) 1994, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_release_conch as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_release_conch (struct protid *cred)
+{
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ pthread_mutex_lock (&np->lock);
+ if (!cred->mapped)
+ {
+ pthread_mutex_unlock (&np->lock);
+ return EINVAL;
+ }
+
+ np = cred->po->np;
+
+ iohelp_handle_io_release_conch (&np->conch, cred);
+
+ pthread_mutex_unlock (&np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-restrict-auth.c b/libdiskfs/io-restrict-auth.c
new file mode 100644
index 00000000..011aa19e
--- /dev/null
+++ b/libdiskfs/io-restrict-auth.c
@@ -0,0 +1,55 @@
+/*
+ Copyright (C) 1994,95,96,2001, 2002 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+
+/* Implement io_restrict_auth as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_restrict_auth (struct protid *cred,
+ mach_port_t *newport,
+ mach_msg_type_name_t *newportpoly,
+ uid_t *uids,
+ size_t nuids,
+ gid_t *gids,
+ size_t ngids)
+{
+ error_t err;
+ struct iouser *user;
+ struct protid *newpi;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ err = iohelp_restrict_iouser (&user, cred->user, uids, nuids, gids, ngids);
+ if (err)
+ return err;
+
+ pthread_mutex_lock (&cred->po->np->lock);
+ err = diskfs_create_protid (cred->po, user, &newpi);
+ if (! err)
+ {
+ *newport = ports_get_right (newpi);
+ *newportpoly = MACH_MSG_TYPE_MAKE_SEND;
+ ports_port_deref (newpi);
+ }
+ pthread_mutex_unlock (&cred->po->np->lock);
+
+ iohelp_free_iouser (user);
+ return err;
+}
diff --git a/libdiskfs/io-revoke.c b/libdiskfs/io-revoke.c
new file mode 100644
index 00000000..a07777e2
--- /dev/null
+++ b/libdiskfs/io-revoke.c
@@ -0,0 +1,59 @@
+/*
+ Copyright (C) 1999 Free Software Foundation
+ Written by Thomas Bushnell, BSG.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Implement io_revoke as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_revoke (struct protid *cred)
+{
+ error_t err;
+ struct node *np;
+
+ error_t
+ iterator_function (void *port)
+ {
+ struct protid *user = port;
+
+ if ((user != cred)
+ && (user->po->np == np))
+ ports_destroy_right (user);
+ return 0;
+ }
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+
+ err = fshelp_isowner (&np->dn_stat, cred->user);
+
+ pthread_mutex_unlock (&np->lock);
+
+ if (err)
+ return err;
+
+ ports_inhibit_bucket_rpcs (diskfs_port_bucket);
+ ports_class_iterate (diskfs_protid_class, iterator_function);
+ ports_resume_bucket_rpcs (diskfs_port_bucket);
+
+ return 0;
+}
diff --git a/libdiskfs/io-seek.c b/libdiskfs/io-seek.c
new file mode 100644
index 00000000..9e3ff093
--- /dev/null
+++ b/libdiskfs/io-seek.c
@@ -0,0 +1,61 @@
+/*
+ Copyright (C) 1994,1995,1996,2000,2006 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <unistd.h>
+
+/* Implement io_seek as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_seek (struct protid *cred,
+ off_t offset,
+ int whence,
+ off_t *newoffset)
+{
+ error_t err = 0;
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+
+ pthread_mutex_lock (&np->lock);
+
+ iohelp_get_conch (&np->conch);
+ switch (whence)
+ {
+ case SEEK_CUR:
+ offset += cred->po->filepointer;
+ goto check;
+ case SEEK_END:
+ offset += np->dn_stat.st_size;
+ case SEEK_SET:
+ check:
+ if (offset >= 0)
+ {
+ *newoffset = cred->po->filepointer = offset;
+ break;
+ }
+ default:
+ err = EINVAL;
+ break;
+ }
+
+ pthread_mutex_unlock (&np->lock);
+ return err;
+}
diff --git a/libdiskfs/io-select.c b/libdiskfs/io-select.c
new file mode 100644
index 00000000..ddac738f
--- /dev/null
+++ b/libdiskfs/io-select.c
@@ -0,0 +1,40 @@
+/*
+ Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <fcntl.h>
+
+/* Implement io_select as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_select (struct protid *cred,
+ int *type)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ *type &= ~SELECT_URG;
+ return 0;
+}
+
+kern_return_t
+diskfs_S_io_select_timeout (struct protid *cred,
+ struct timespec ts,
+ int *type)
+{
+ return diskfs_S_io_select (cred, type);
+}
diff --git a/libdiskfs/io-sigio.c b/libdiskfs/io-sigio.c
new file mode 100644
index 00000000..1e4c461b
--- /dev/null
+++ b/libdiskfs/io-sigio.c
@@ -0,0 +1,36 @@
+/*
+ Copyright (C) 1994, 1995 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <fcntl.h>
+
+/* Implement io_sigio as described in <hurd/io.defs>. We do this
+ for O_FSYNC right now, but will eventually do it for async I/O
+ too. */
+kern_return_t
+diskfs_S_io_sigio (struct protid *cred)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ pthread_mutex_lock (&cred->po->np->lock);
+ if ((cred->po->openstat & O_FSYNC) || diskfs_synchronous)
+ diskfs_file_update (cred->po->np, 1);
+ pthread_mutex_unlock (&cred->po->np->lock);
+ return 0;
+}
diff --git a/libdiskfs/io-stat.c b/libdiskfs/io-stat.c
new file mode 100644
index 00000000..211b53a8
--- /dev/null
+++ b/libdiskfs/io-stat.c
@@ -0,0 +1,51 @@
+/*
+ Copyright (C) 1994,95,96,97,2001 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <string.h>
+
+/* Implement io_stat as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_stat (struct protid *cred,
+ io_statbuf_t *statbuf)
+{
+ struct node *np;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ pthread_mutex_lock (&np->lock);
+
+ iohelp_get_conch (&np->conch);
+ if (diskfs_synchronous)
+ diskfs_node_update (np, 1);
+ else
+ diskfs_set_node_times (np);
+
+ memcpy (statbuf, &np->dn_stat, sizeof (struct stat));
+ statbuf->st_mode &= ~(S_IATRANS | S_IROOT);
+ if (fshelp_translated (&np->transbox))
+ statbuf->st_mode |= S_IATRANS;
+ if (cred->po->shadow_root == np || np == diskfs_root_node)
+ statbuf->st_mode |= S_IROOT;
+
+ pthread_mutex_unlock (&np->lock);
+
+ return 0;
+}
diff --git a/libdiskfs/io-stubs.c b/libdiskfs/io-stubs.c
new file mode 100644
index 00000000..592b3d8b
--- /dev/null
+++ b/libdiskfs/io-stubs.c
@@ -0,0 +1,62 @@
+/*
+ Copyright (C) 1994, 1995 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+
+/* Unimplemented stubs. */
+
+/* Implement io_readsleep as described in <hurd/io.defs>
+ Semantics of ordinary files say this shouldn't happen, because
+ we never set use_read_size in the shared data. */
+kern_return_t
+diskfs_S_io_readsleep (struct protid *cred)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ return 0;
+}
+
+/* Implement io_eofnotify as described in <hurd/io.defs>.
+ We don't use this feature. */
+kern_return_t
+diskfs_S_io_eofnotify (struct protid *cred)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+
+ return 0;
+}
+
+/* Implement io_postnotify as described in <hurd/io.defs>.
+ We don't use this feature. */
+kern_return_t
+diskfs_S_io_postnotify (struct protid *cred,
+ vm_offset_t start __attribute__ ((unused)),
+ vm_offset_t end __attribute__ ((unused)))
+{
+ return cred ? 0 : EOPNOTSUPP;
+}
+
+/* Implement io_readnotify as described in <hurd/io.defs>.
+ We don't use this feature. */
+kern_return_t
+diskfs_S_io_readnotify (struct protid *cred)
+{
+ return cred ? 0 : EOPNOTSUPP;
+}
diff --git a/libdiskfs/io-version.c b/libdiskfs/io-version.c
new file mode 100644
index 00000000..b19dd0d1
--- /dev/null
+++ b/libdiskfs/io-version.c
@@ -0,0 +1,42 @@
+/*
+ Copyright (C) 1994, 1996 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include <stdio.h>
+
+#include "priv.h"
+#include "io_S.h"
+
+kern_return_t
+diskfs_S_io_server_version (struct protid *cred,
+ char *server_name,
+ int *major,
+ int *minor,
+ int *edit)
+{
+ if (cred)
+ {
+ snprintf (server_name, sizeof (string_t), "%s %s",
+ diskfs_server_name, diskfs_server_version);
+ return 0;
+ }
+ else
+ return EOPNOTSUPP;
+}
diff --git a/libdiskfs/io-write.c b/libdiskfs/io-write.c
new file mode 100644
index 00000000..2967c4c8
--- /dev/null
+++ b/libdiskfs/io-write.c
@@ -0,0 +1,93 @@
+/*
+ Copyright (C) 1994,95,96,97,2001 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include "io_S.h"
+#include <fcntl.h>
+
+/* Implement io_write as described in <hurd/io.defs>. */
+kern_return_t
+diskfs_S_io_write (struct protid *cred,
+ char *data,
+ mach_msg_type_number_t datalen,
+ off_t offset,
+ mach_msg_type_number_t *amt)
+{
+ struct node *np;
+ error_t err;
+ off_t off = offset;
+
+ if (!cred)
+ return EOPNOTSUPP;
+
+ np = cred->po->np;
+ if (!(cred->po->openstat & O_WRITE))
+ return EBADF;
+
+ pthread_mutex_lock (&np->lock);
+
+ assert (!S_ISDIR(np->dn_stat.st_mode));
+
+ iohelp_get_conch (&np->conch);
+
+ if (off == -1)
+ {
+ if (cred->po->openstat & O_APPEND)
+ cred->po->filepointer = np->dn_stat.st_size;
+ off = cred->po->filepointer;
+ }
+ if (off < 0)
+ {
+ err = EINVAL;
+ goto out;
+ }
+
+ while (off + (off_t) datalen > np->allocsize)
+ {
+ err = diskfs_grow (np, off + datalen, cred);
+ if (diskfs_synchronous)
+ diskfs_node_update (np, 1);
+ if (err)
+ goto out;
+ if (np->filemod_reqs)
+ diskfs_notice_filechange (np, FILE_CHANGED_EXTEND, 0, off + datalen);
+ }
+
+ if (off + (off_t) datalen > np->dn_stat.st_size)
+ {
+ np->dn_stat.st_size = off + datalen;
+ np->dn_set_ctime = 1;
+ if (diskfs_synchronous)
+ diskfs_node_update (np, 1);
+ }
+
+ *amt = datalen;
+ err = _diskfs_rdwr_internal (np, data, off, amt, 1, 0);
+
+ if (!err && offset == -1)
+ cred->po->filepointer += *amt;
+
+ if (!err
+ && ((cred->po->openstat & O_FSYNC) || diskfs_synchronous))
+ diskfs_file_update (np, 1);
+
+ if (!err && np->filemod_reqs)
+ diskfs_notice_filechange (np, FILE_CHANGED_WRITE, off, off + *amt);
+ out:
+ pthread_mutex_unlock (&np->lock);
+ return err;
+}
diff --git a/libdiskfs/lithp.h b/libdiskfs/lithp.h
new file mode 100644
index 00000000..be56377d
--- /dev/null
+++ b/libdiskfs/lithp.h
@@ -0,0 +1,31 @@
+/*
+ Copyright (C) 1994, 1996 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+/* Hmm. */
+
+#define dithkfth_TH_file_chauthor diskfs_S_file_chauthor
+#define fthhelp_ithowner fshelp_isowner
+#define dithkfth_validate_author_change diskfs_validate_author_change
+#define dn_thtat dn_stat
+#define tht_author st_author
+#define dn_thet_theetime dn_set_ctime
+#define fth_TH_dot_h "fs_S.h"
+#define uther user
diff --git a/libdiskfs/lookup.c b/libdiskfs/lookup.c
new file mode 100644
index 00000000..bc2ad01e
--- /dev/null
+++ b/libdiskfs/lookup.c
@@ -0,0 +1,234 @@
+/* Wrapper for diskfs_lookup_hard
+ Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+#include <string.h>
+
+static struct
+{
+ int present;
+ int absent;
+ int errors;
+ int dot;
+ int dotdot;
+} cache_misses;
+static pthread_spinlock_t cm_lock = PTHREAD_SPINLOCK_INITIALIZER;
+
+
+/* Lookup in directory DP (which is locked) the name NAME. TYPE will
+ either be LOOKUP, CREATE, RENAME, or REMOVE. CRED identifies the
+ user making the call.
+
+ NAME will have leading and trailing slashes stripped. It is an
+ error if there are internal slashes. NAME will be modified in
+ place if there are slashes in it; it is therefore an error to
+ specify a constant NAME which contains slashes.
+
+ If the name is found, return zero, and (if NP is nonzero) set *NP
+ to point to the node for it, locked. If the name is not found,
+ return ENOENT, and (if NP is nonzero) set *NP to zero. If NP is
+ zero, then the node found must not be locked, even transitorily.
+ Lookups for REMOVE and RENAME (which must often check permissions
+ on the node being found) will always set NP.
+
+ If DS is nonzero then:
+ For LOOKUP: set *DS to be ignored by diskfs_drop_dirstat.
+ For CREATE: on success, set *DS to be ignored by diskfs_drop_dirstat.
+ on failure, set *DS for a future call to diskfs_direnter.
+ For RENAME: on success, set *DS for a future call to diskfs_dirrewrite.
+ on failure, set *DS for a future call to diskfs_direnter.
+ For REMOVE: on success, set *DS for a future call to diskfs_dirremove.
+ on failure, set *DS to be ignored by diskfs_drop_dirstat.
+ The caller of this function guarantees that if DS is nonzero, then
+ either the appropriate call listed above or diskfs_drop_dirstat will
+ be called with DS before the directory DP is unlocked, and guarantees
+ that no lookup calls will be made on this directory between this
+ lookup and the use (or descruction) of *DS.
+
+ If you use the library's versions of diskfs_rename_dir,
+ diskfs_clear_directory, and diskfs_init_dir, then lookups for `..'
+ might have the flag SPEC_DOTDOT or'd in. This has the following special
+ meaning:
+ For LOOKUP: DP should be unlocked and its reference dropped before
+ returning.
+ For RENAME and REMOVE: The node being found (*NP) is already held
+ locked, so don't lock it or add a reference to it.
+ (SPEC_DOTDOT will not be given with CREATE.)
+
+ Return ENOTDIR if DP is not a directory.
+ Return EACCES if CRED isn't allowed to search DP.
+ Return EACCES if completing the operation will require writing
+ the directory and diskfs_checkdirmod won't allow the modification.
+ Return ENOENT if NAME isn't in the directory.
+ Return EAGAIN if NAME refers to the `..' of this filesystem's root.
+ Return EIO if appropriate.
+
+ This function is a wrapper for diskfs_lookup_hard. */
+error_t
+diskfs_lookup (struct node *dp, const char *name, enum lookup_type type,
+ struct node **np, struct dirstat *ds, struct protid *cred)
+{
+ error_t err;
+ struct node *cached;
+
+ if (type == REMOVE || type == RENAME)
+ assert (np);
+
+ if (!S_ISDIR (dp->dn_stat.st_mode))
+ {
+ if (ds)
+ diskfs_null_dirstat (ds);
+ return ENOTDIR;
+ }
+
+ /* Strip leading and trailing slashes. */
+ while (*name == '/')
+ name++;
+
+ if (name[0] == '\0')
+ {
+ if (ds)
+ diskfs_null_dirstat (ds);
+ return EINVAL;
+ }
+ else
+ {
+ char *p = strchr (name, '/');
+ if (p != 0)
+ {
+ *p = '\0';
+ do
+ ++p;
+ while (*p == '/');
+ if (*p != '\0')
+ {
+ if (ds)
+ diskfs_null_dirstat (ds);
+ return EINVAL;
+ }
+ }
+ }
+
+
+ err = fshelp_access (&dp->dn_stat, S_IEXEC, cred->user);
+ if (err)
+ {
+ if (ds)
+ diskfs_null_dirstat (ds);
+ return err;
+ }
+
+ if (dp == cred->po->shadow_root
+ && name[0] == '.' && name[1] == '.' && name[2] == '\0')
+ /* Ran into the root. */
+ {
+ if (ds)
+ diskfs_null_dirstat (ds);
+ return EAGAIN;
+ }
+
+ if (type == LOOKUP)
+ /* Check the cache first */
+ cached = diskfs_check_lookup_cache (dp, name);
+ else
+ cached = 0;
+
+ if (cached == (struct node *)-1)
+ /* Negative lookup cached. */
+ {
+ if (np)
+ *np = 0;
+ return ENOENT;
+ }
+ else if (cached)
+ {
+ if (np)
+ *np = cached; /* Return what we found. */
+ else
+ /* Ick, the user doesn't want the result, we have to drop our
+ reference. */
+ if (cached == dp)
+ diskfs_nrele (cached);
+ else
+ diskfs_nput (cached);
+
+ if (ds)
+ diskfs_null_dirstat (ds);
+ }
+ else
+ {
+ err = diskfs_lookup_hard (dp, name, type, np, ds, cred);
+
+ pthread_spin_lock (&cm_lock);
+ if (type == LOOKUP)
+ {
+ if (err == ENOENT)
+ cache_misses.absent++;
+ else if (err)
+ cache_misses.errors++;
+ else
+ cache_misses.present++;
+ if (name[0] == '.')
+ {
+ if (name[1] == '\0')
+ cache_misses.dot++;
+ else if (name[1] == '.' && name[2] == '\0')
+ cache_misses.dotdot++;
+ }
+ }
+ pthread_spin_unlock (&cm_lock);
+
+ if (err && err != ENOENT)
+ return err;
+
+ if (type == RENAME
+ || (type == CREATE && err == ENOENT)
+ || (type == REMOVE && err != ENOENT))
+ {
+ error_t err2;
+
+ if (diskfs_name_max > 0 && strlen (name) > diskfs_name_max)
+ err2 = ENAMETOOLONG;
+ else
+ err2 = fshelp_checkdirmod (&dp->dn_stat,
+ (err || !np) ? 0 : &(*np)->dn_stat,
+ cred->user);
+ if (err2)
+ {
+ if (np && !err)
+ {
+ if (*np == dp)
+ diskfs_nrele (*np);
+ else
+ diskfs_nput (*np);
+ *np = 0;
+ }
+ return err2;
+ }
+ }
+
+ if ((type == LOOKUP || type == CREATE) && !err && np)
+ diskfs_enter_lookup_cache (dp, *np, name);
+ else if (type == LOOKUP && err == ENOENT)
+ diskfs_enter_lookup_cache (dp, 0, name);
+ }
+
+ return err;
+}
diff --git a/libdiskfs/name-cache.c b/libdiskfs/name-cache.c
new file mode 100644
index 00000000..d8f86b15
--- /dev/null
+++ b/libdiskfs/name-cache.c
@@ -0,0 +1,376 @@
+/* Directory name lookup caching
+
+ Copyright (C) 1996, 1997, 1998, 2014 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG, & Miles Bader.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+#include <assert.h>
+#include <string.h>
+
+/* The name cache is implemented using a hash table.
+
+ We use buckets of a fixed size. We approximate the
+ least-frequently used cache algorithm by counting the number of
+ lookups using saturating arithmetic in the two lowest bits of the
+ pointer to the name. Using this strategy we achieve a constant
+ worst-case lookup and insertion time. */
+
+/* Number of buckets. Must be a power of two. */
+#define CACHE_SIZE 256
+
+/* Entries per bucket. */
+#define BUCKET_SIZE 4
+
+/* A mask for fast binary modulo. */
+#define CACHE_MASK (CACHE_SIZE - 1)
+
+/* Cache bucket with BUCKET_SIZE entries.
+
+ The layout of the bucket is chosen so that it will be straight
+ forward to use vector operations in the future. */
+struct cache_bucket
+{
+ /* Name of the node NODE_CACHE_ID in the directory DIR_CACHE_ID. If
+ NULL, the entry is unused. */
+ unsigned long name[BUCKET_SIZE];
+
+ /* The key. */
+ unsigned long key[BUCKET_SIZE];
+
+ /* Used to indentify nodes to the fs dependent code. */
+ ino64_t dir_cache_id[BUCKET_SIZE];
+
+ /* 0 for NODE_CACHE_ID means a `negative' entry -- recording that
+ there's definitely no node with this name. */
+ ino64_t node_cache_id[BUCKET_SIZE];
+};
+
+/* The cache. */
+static struct cache_bucket name_cache[CACHE_SIZE];
+
+/* Protected by this lock. */
+static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
+
+/* Given VALUE, return the char pointer. */
+static inline char *
+charp (unsigned long value)
+{
+ return (char *) (value & ~3L);
+}
+
+/* Given VALUE, return the approximation of use frequency. */
+static inline unsigned long
+frequ (unsigned long value)
+{
+ return value & 3;
+}
+
+/* Add an entry in the Ith slot of the given bucket. If there is a
+ value there, remove it first. */
+static inline void
+add_entry (struct cache_bucket *b, int i,
+ const char *name, unsigned long key,
+ ino64_t dir_cache_id, ino64_t node_cache_id)
+{
+ if (b->name[i])
+ free (charp (b->name[i]));
+
+ b->name[i] = (unsigned long) strdup (name);
+ assert ((b->name[i] & 3) == 0);
+ if (b->name[i] == 0)
+ return;
+
+ b->key[i] = key;
+ b->dir_cache_id[i] = dir_cache_id;
+ b->node_cache_id[i] = node_cache_id;
+}
+
+/* Remove the entry in the Ith slot of the given bucket. */
+static inline void
+remove_entry (struct cache_bucket *b, int i)
+{
+ if (b->name[i])
+ free (charp (b->name[i]));
+ b->name[i] = 0;
+}
+
+/* Check if the entry in the Ith slot of the given bucket is
+ valid. */
+static inline int
+valid_entry (struct cache_bucket *b, int i)
+{
+ return b->name[i] != 0;
+}
+
+/* This is the Murmur3 hash algorithm. */
+
+#define FORCE_INLINE inline __attribute__((always_inline))
+
+inline uint32_t rotl32 ( uint32_t x, int8_t r )
+{
+ return (x << r) | (x >> (32 - r));
+}
+
+#define ROTL32(x,y) rotl32(x,y)
+
+/* Block read - if your platform needs to do endian-swapping or can
+ only handle aligned reads, do the conversion here. */
+
+FORCE_INLINE uint32_t getblock32 ( const uint32_t * p, int i )
+{
+ return p[i];
+}
+
+/* Finalization mix - force all bits of a hash block to avalanche. */
+
+FORCE_INLINE uint32_t fmix32 ( uint32_t h )
+{
+ h ^= h >> 16;
+ h *= 0x85ebca6b;
+ h ^= h >> 13;
+ h *= 0xc2b2ae35;
+ h ^= h >> 16;
+
+ return h;
+}
+
+/* The Murmur3 hash function. */
+void MurmurHash3_x86_32 ( const void * key, int len,
+ uint32_t seed, void * out )
+{
+ const uint8_t * data = (const uint8_t*)key;
+ const int nblocks = len / 4;
+
+ uint32_t h1 = seed;
+
+ const uint32_t c1 = 0xcc9e2d51;
+ const uint32_t c2 = 0x1b873593;
+
+ /* body */
+
+ const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
+
+ for(int i = -nblocks; i; i++)
+ {
+ uint32_t k1 = getblock32(blocks,i);
+
+ k1 *= c1;
+ k1 = ROTL32(k1,15);
+ k1 *= c2;
+
+ h1 ^= k1;
+ h1 = ROTL32(h1,13);
+ h1 = h1*5+0xe6546b64;
+ }
+
+ /* tail */
+
+ const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
+
+ uint32_t k1 = 0;
+
+ switch(len & 3)
+ {
+ case 3: k1 ^= tail[2] << 16;
+ case 2: k1 ^= tail[1] << 8;
+ case 1: k1 ^= tail[0];
+ k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
+ };
+
+ /* finalization */
+
+ h1 ^= len;
+
+ h1 = fmix32(h1);
+
+ *(uint32_t*)out = h1;
+}
+
+/* If there is no best candidate to replace, pick any. We approximate
+ any by picking the slot depicted by REPLACE, and increment REPLACE
+ then. */
+static int replace;
+
+/* Lookup (DIR_CACHE_ID, NAME, KEY) in the cache. If it is found,
+ return 1 and set BUCKET and INDEX to the item. Otherwise, return 0
+ and set BUCKET and INDEX to the slot where the item should be
+ inserted. */
+static inline int
+lookup (ino64_t dir_cache_id, const char *name, unsigned long key,
+ struct cache_bucket **bucket, int *index)
+{
+ struct cache_bucket *b = *bucket = &name_cache[key & CACHE_MASK];
+ unsigned long best = 3;
+ int i;
+
+ for (i = 0; i < BUCKET_SIZE; i++)
+ {
+ unsigned long f = frequ (b->name[i]);
+
+ if (valid_entry (b, i)
+ && b->key[i] == key
+ && b->dir_cache_id[i] == dir_cache_id
+ && strcmp (charp (b->name[i]), name) == 0)
+ {
+ if (f < 3)
+ b->name[i] += 1;
+
+ *index = i;
+ return 1;
+ }
+
+ /* Keep track of the replacement candidate. */
+ if (f < best)
+ {
+ best = f;
+ *index = i;
+ }
+ }
+
+ /* If there was no entry with a lower use frequency, just replace
+ any entry. */
+ if (best == 3)
+ {
+ *index = replace;
+ replace = (replace + 1) & (BUCKET_SIZE - 1);
+ }
+
+ return 0;
+}
+
+/* Hash the directory cache_id and the name. */
+static inline unsigned long
+hash (ino64_t dir_cache_id, const char *name)
+{
+ unsigned long h;
+ MurmurHash3_x86_32 (&dir_cache_id, sizeof dir_cache_id, 0, &h);
+ MurmurHash3_x86_32 (name, strlen (name), h, &h);
+ return h;
+}
+
+/* Node NP has just been found in DIR with NAME. If NP is null, that
+ means that this name has been confirmed as absent in the directory. */
+void
+diskfs_enter_lookup_cache (struct node *dir, struct node *np, const char *name)
+{
+ unsigned long key = hash (dir->cache_id, name);
+ ino64_t value = np ? np->cache_id : 0;
+ struct cache_bucket *bucket;
+ int i = 0, found;
+
+ pthread_mutex_lock (&cache_lock);
+ found = lookup (dir->cache_id, name, key, &bucket, &i);
+ if (! found)
+ add_entry (bucket, i, name, key, dir->cache_id, value);
+ else
+ if (bucket->node_cache_id[i] != value)
+ bucket->node_cache_id[i] = value;
+
+ pthread_mutex_unlock (&cache_lock);
+}
+
+/* Purge all references in the cache to NP as a node inside
+ directory DP. */
+void
+diskfs_purge_lookup_cache (struct node *dp, struct node *np)
+{
+ int i;
+ struct cache_bucket *b;
+
+ pthread_mutex_lock (&cache_lock);
+
+ for (b = &name_cache[0]; b < &name_cache[CACHE_SIZE]; b++)
+ for (i = 0; i < BUCKET_SIZE; i++)
+ if (valid_entry (b, i)
+ && b->dir_cache_id[i] == dp->cache_id
+ && b->node_cache_id[i] == np->cache_id)
+ remove_entry (b, i);
+
+ pthread_mutex_unlock (&cache_lock);
+}
+
+/* Scan the cache looking for NAME inside DIR. If we don't know
+ anything entry at all, then return 0. If the entry is confirmed to
+ not exist, then return -1. Otherwise, return NP for the entry, with
+ a newly allocated reference. */
+struct node *
+diskfs_check_lookup_cache (struct node *dir, const char *name)
+{
+ unsigned long key = hash (dir->cache_id, name);
+ int lookup_parent = name[0] == '.' && name[1] == '.' && name[2] == '\0';
+ struct cache_bucket *bucket;
+ int i, found;
+
+ if (lookup_parent && dir == diskfs_root_node)
+ /* This is outside our file system, return cache miss. */
+ return NULL;
+
+ pthread_mutex_lock (&cache_lock);
+ found = lookup (dir->cache_id, name, key, &bucket, &i);
+ if (found)
+ {
+ ino64_t id = bucket->node_cache_id[i];
+ pthread_mutex_unlock (&cache_lock);
+
+ if (id == 0)
+ /* A negative cache entry. */
+ return (struct node *) -1;
+ else if (id == dir->cache_id)
+ /* The cached node is the same as DIR. */
+ {
+ diskfs_nref (dir);
+ return dir;
+ }
+ else
+ /* Just a normal entry in DIR; get the actual node. */
+ {
+ struct node *np;
+ error_t err;
+
+ if (lookup_parent)
+ {
+ pthread_mutex_unlock (&dir->lock);
+ err = diskfs_cached_lookup (id, &np);
+ pthread_mutex_lock (&dir->lock);
+
+ /* In the window where DP was unlocked, we might
+ have lost. So check the cache again, and see
+ if it's still there; if so, then we win. */
+ pthread_mutex_lock (&cache_lock);
+ found = lookup (dir->cache_id, name, key, &bucket, &i);
+ if (! found
+ || ! bucket->node_cache_id[i] != id)
+ {
+ pthread_mutex_unlock (&cache_lock);
+
+ /* Lose */
+ diskfs_nput (np);
+ return 0;
+ }
+ pthread_mutex_unlock (&cache_lock);
+ }
+ else
+ err = diskfs_cached_lookup (id, &np);
+ return err ? 0 : np;
+ }
+ }
+
+ pthread_mutex_unlock (&cache_lock);
+ return 0;
+}
diff --git a/libdiskfs/node-create.c b/libdiskfs/node-create.c
new file mode 100644
index 00000000..5b5e4639
--- /dev/null
+++ b/libdiskfs/node-create.c
@@ -0,0 +1,163 @@
+/* Making new files
+ Copyright (C) 1992,93,94,96,98,2001 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+/* This enables SysV style group behaviour. New nodes inherit the GID
+ of the user creating them unless the SGID bit is set of the parent
+ directory. */
+int _diskfs_no_inherit_dir_group;
+
+/* Create a new node. Give it MODE; if that includes IFDIR, also
+ initialize `.' and `..' in the new directory. Return the node in NPP.
+ CRED identifies the user responsible for the call. If NAME is nonzero,
+ then link the new node into DIR with name NAME; DS is the result of a
+ prior diskfs_lookup for creation (and DIR has been held locked since).
+ DIR must always be provided as at least a hint for disk allocation
+ strategies. */
+error_t
+diskfs_create_node (struct node *dir,
+ const char *name,
+ mode_t mode,
+ struct node **newnode,
+ struct protid *cred,
+ struct dirstat *ds)
+{
+ struct node *np;
+ error_t err;
+ uid_t newuid;
+ gid_t newgid;
+
+ if (diskfs_check_readonly ())
+ {
+ *newnode = NULL;
+ return EROFS;
+ }
+
+ /* Make the node */
+ err = diskfs_alloc_node (dir, mode, newnode);
+ if (err)
+ {
+ if (name)
+ diskfs_drop_dirstat (dir, ds);
+ *newnode = NULL;
+ return err;
+ }
+
+ np = *newnode;
+
+ /* Initialize the on-disk fields. */
+ if (cred->user->uids->num)
+ newuid = cred->user->uids->ids[0];
+ else
+ {
+ newuid = dir->dn_stat.st_uid;
+ mode &= ~S_ISUID;
+ }
+ err = diskfs_validate_owner_change (np, newuid);
+ if (err)
+ goto change_err;
+ np->dn_stat.st_uid = newuid;
+ if (np->author_tracks_uid)
+ np->dn_stat.st_author = newuid;
+
+ if (!_diskfs_no_inherit_dir_group)
+ {
+ newgid = dir->dn_stat.st_gid;
+ if (!idvec_contains (cred->user->gids, newgid))
+ mode &= ~S_ISGID;
+ }
+ else
+ {
+ if (dir->dn_stat.st_mode & S_ISGID)
+ {
+ /* If the parent dir has the sgid bit set, inherit its gid.
+ If the new node is a directory, also inherit the sgid bit
+ set. */
+ newgid = dir->dn_stat.st_gid;
+ if (S_ISDIR (mode))
+ mode |= S_ISGID;
+ else
+ {
+ if (!idvec_contains (cred->user->gids, newgid))
+ mode &= ~S_ISGID;
+ }
+ }
+ else
+ {
+ if (cred->user->gids->num)
+ newgid = cred->user->gids->ids[0];
+ else
+ {
+ newgid = dir->dn_stat.st_gid;
+ mode &= ~S_ISGID;
+ }
+ }
+ }
+
+ err = diskfs_validate_group_change (np, newgid);
+ if (err)
+ goto change_err;
+ np->dn_stat.st_gid = newgid;
+
+ np->dn_stat.st_rdev = 0;
+ np->dn_stat.st_nlink = !!name;
+ err = diskfs_validate_mode_change (np, mode);
+ if (err)
+ goto change_err;
+ np->dn_stat.st_mode = mode;
+
+ np->dn_stat.st_blocks = 0;
+ np->dn_stat.st_size = 0;
+ np->dn_stat.st_flags = 0;
+ np->dn_set_atime = 1;
+ np->dn_set_mtime = 1;
+ np->dn_set_ctime = 1;
+
+ if (S_ISDIR (mode))
+ err = diskfs_init_dir (np, dir, cred);
+
+ diskfs_node_update (np, diskfs_synchronous);
+
+ if (err)
+ {
+ change_err:
+ np->dn_stat.st_mode = 0;
+ np->dn_stat.st_nlink = 0;
+ if (name)
+ diskfs_drop_dirstat (dir, ds);
+ *newnode = NULL;
+ return err;
+ }
+
+ if (name)
+ {
+ err = diskfs_direnter (dir, name, np, ds, cred);
+ if (err)
+ {
+ if (S_ISDIR (mode))
+ diskfs_clear_directory (np, dir, cred);
+ np->dn_stat.st_nlink = 0;
+ np->dn_set_ctime = 1;
+ diskfs_nput (np);
+ }
+ }
+ if (err)
+ *newnode = NULL;
+
+ return err;
+}
diff --git a/libdiskfs/node-drop.c b/libdiskfs/node-drop.c
new file mode 100644
index 00000000..83eb5909
--- /dev/null
+++ b/libdiskfs/node-drop.c
@@ -0,0 +1,98 @@
+/*
+ Copyright (C) 1994, 1995, 1996 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+/* Free the list of modification requests MR */
+static void
+free_modreqs (struct modreq *mr)
+{
+ struct modreq *tmp;
+ for (; mr; mr = tmp)
+ {
+ mach_port_deallocate (mach_task_self (), mr->port);
+ tmp = mr->next;
+ free (mr);
+ }
+}
+
+
+/* Node NP now has no more references; clean all state. The
+ diskfs_node_refcnt_lock must be held, and will be released
+ upon return. NP must be locked. */
+void
+diskfs_drop_node (struct node *np)
+{
+ mode_t savemode;
+
+ if (np->dn_stat.st_nlink == 0)
+ {
+ diskfs_check_readonly ();
+ assert (!diskfs_readonly);
+
+ if (np->dn_stat.st_mode & S_IPTRANS)
+ diskfs_set_translator (np, 0, 0, 0);
+
+ if (np->allocsize != 0
+ || (diskfs_create_symlink_hook
+ && S_ISLNK (np->dn_stat.st_mode)
+ && np->dn_stat.st_size))
+ {
+ /* If the node needs to be truncated, then a complication
+ arises, because truncation might require gaining
+ new references to the node. So, we give ourselves
+ a reference back, unlock the refcnt lock. Then
+ we are in the state of a normal user, and do the truncate
+ and an nput. The next time through, this routine
+ will notice that the size is zero, and not have to
+ do anything. */
+ np->references++;
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+ diskfs_truncate (np, 0);
+
+ /* Force allocsize to zero; if truncate consistently fails this
+ will at least prevent an infinite loop in this routine. */
+ np->allocsize = 0;
+
+ diskfs_nput (np);
+ return;
+ }
+
+ assert (np->dn_stat.st_size == 0);
+
+ savemode = np->dn_stat.st_mode;
+ np->dn_stat.st_mode = 0;
+ np->dn_stat.st_rdev = 0;
+ np->dn_set_ctime = np->dn_set_atime = 1;
+ diskfs_node_update (np, diskfs_synchronous);
+ diskfs_free_node (np, savemode);
+ }
+ else
+ diskfs_node_update (np, diskfs_synchronous);
+
+ fshelp_drop_transbox (&np->transbox);
+
+ if (np->dirmod_reqs)
+ free_modreqs (np->dirmod_reqs);
+ if (np->filemod_reqs)
+ free_modreqs (np->filemod_reqs);
+
+ assert (!np->sockaddr);
+
+ diskfs_node_norefs (np);
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+}
diff --git a/libdiskfs/node-make.c b/libdiskfs/node-make.c
new file mode 100644
index 00000000..ff0cc0d4
--- /dev/null
+++ b/libdiskfs/node-make.c
@@ -0,0 +1,75 @@
+/*
+ Copyright (C) 1994,95,96,2002 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include <fcntl.h>
+
+
+static struct node *
+init_node (struct node *np, struct disknode *dn)
+{
+ np->dn = dn;
+ np->dn_set_ctime = 0;
+ np->dn_set_atime = 0;
+ np->dn_set_mtime = 0;
+ np->dn_stat_dirty = 0;
+
+ pthread_mutex_init (&np->lock, NULL);
+ np->references = 1;
+ np->light_references = 0;
+ np->owner = 0;
+ np->sockaddr = MACH_PORT_NULL;
+
+ np->dirmod_reqs = 0;
+ np->dirmod_tick = 0;
+ np->filemod_reqs = 0;
+ np->filemod_tick = 0;
+
+ fshelp_transbox_init (&np->transbox, &np->lock, np);
+ iohelp_initialize_conch (&np->conch, &np->lock);
+ fshelp_lock_init (&np->userlock);
+
+ return np;
+}
+
+/* Create a and return new node structure with DN as its physical disknode.
+ The node will have one hard reference and no light references. */
+struct node *
+diskfs_make_node (struct disknode *dn)
+{
+ struct node *np = malloc (sizeof (struct node));
+
+ if (np == 0)
+ return 0;
+
+ return init_node (np, dn);
+}
+
+/* Create a new node structure. Also allocate SIZE bytes for the
+ disknode. The address of the disknode can be obtained using
+ diskfs_node_disknode. The new node will have one hard reference
+ and no light references. */
+struct node *
+diskfs_make_node_alloc (size_t size)
+{
+ struct node *np = malloc (sizeof (struct node) + size);
+
+ if (np == NULL)
+ return NULL;
+
+ return init_node (np, diskfs_node_disknode (np));
+}
diff --git a/libdiskfs/node-nput.c b/libdiskfs/node-nput.c
new file mode 100644
index 00000000..5043ad1a
--- /dev/null
+++ b/libdiskfs/node-nput.c
@@ -0,0 +1,81 @@
+/*
+ Copyright (C) 1999, 2001 Free Software Foundation, Inc.
+ Written by Thomas Bushnell, BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+/* Unlock node NP and release a hard reference; if this is the last
+ hard reference and there are no links to the file then request
+ weak references to be dropped. */
+void
+diskfs_nput (struct node *np)
+{
+ int tried_drop_softrefs = 0;
+
+ loop:
+ pthread_spin_lock (&diskfs_node_refcnt_lock);
+ assert (np->references);
+ np->references--;
+ if (np->references + np->light_references == 0)
+ diskfs_drop_node (np);
+ else if (np->references == 0 && !tried_drop_softrefs)
+ {
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+
+ /* This is our cue that something akin to "last process closes file"
+ in the POSIX.1 sense happened, so make sure any pending node time
+ updates now happen in a timely fashion. */
+ diskfs_set_node_times (np);
+
+ diskfs_lost_hardrefs (np);
+ if (!np->dn_stat.st_nlink)
+ {
+ /* There are no links. If there are soft references that
+ can be dropped, we can't let them postpone deallocation.
+ So attempt to drop them. But that's a user-supplied
+ routine, which might result in further recursive calls to
+ the ref-counting system. So we have to reacquire our
+ reference around the call to forestall disaster. */
+ pthread_spin_lock (&diskfs_node_refcnt_lock);
+ np->references++;
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+
+ if (np->sockaddr != MACH_PORT_NULL)
+ {
+ mach_port_deallocate (mach_task_self (), np->sockaddr);
+ np->sockaddr = MACH_PORT_NULL;
+ }
+
+ diskfs_try_dropping_softrefs (np);
+
+ /* But there's no value in looping forever in this
+ routine; only try to drop soft refs once. */
+ tried_drop_softrefs = 1;
+
+ /* Now we can drop the reference back... */
+ goto loop;
+ }
+ pthread_mutex_unlock (&np->lock);
+ }
+ else
+ {
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+ pthread_mutex_unlock (&np->lock);
+ }
+}
diff --git a/libdiskfs/node-nputl.c b/libdiskfs/node-nputl.c
new file mode 100644
index 00000000..19596654
--- /dev/null
+++ b/libdiskfs/node-nputl.c
@@ -0,0 +1,38 @@
+/*
+ Copyright (C) 1999 Free Software Foundation, Inc.
+ Written by Thomas Bushnell, BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+
+#include "priv.h"
+
+/* Unlock node NP and release a light reference */
+void
+diskfs_nput_light (struct node *np)
+{
+ pthread_spin_lock (&diskfs_node_refcnt_lock);
+ assert (np->light_references);
+ np->light_references--;
+ if (np->references + np->light_references == 0)
+ diskfs_drop_node (np);
+ else
+ {
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+ pthread_mutex_unlock (&np->lock);
+ }
+}
diff --git a/libdiskfs/node-nref.c b/libdiskfs/node-nref.c
new file mode 100644
index 00000000..13cea056
--- /dev/null
+++ b/libdiskfs/node-nref.c
@@ -0,0 +1,40 @@
+/*
+ Copyright (C) 1999 Free Software Foundation, Inc.
+ Written by Thomas Bushnell, BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+/* Add a hard reference to a node. If there were no hard
+ references previously, then the node cannot be locked
+ (because you must hold a hard reference to hold the lock). */
+void
+diskfs_nref (struct node *np)
+{
+ int new_hardref;
+ pthread_spin_lock (&diskfs_node_refcnt_lock);
+ np->references++;
+ new_hardref = (np->references == 1);
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+ if (new_hardref)
+ {
+ pthread_mutex_lock (&np->lock);
+ diskfs_new_hardrefs (np);
+ pthread_mutex_unlock (&np->lock);
+ }
+}
diff --git a/libdiskfs/node-nrefl.c b/libdiskfs/node-nrefl.c
new file mode 100644
index 00000000..96922471
--- /dev/null
+++ b/libdiskfs/node-nrefl.c
@@ -0,0 +1,30 @@
+/*
+ Copyright (C) 1999 Free Software Foundation, Inc.
+ Written by Thomas Bushnell, BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+/* Add a light reference to a node. */
+void
+diskfs_nref_light (struct node *np)
+{
+ pthread_spin_lock (&diskfs_node_refcnt_lock);
+ np->light_references++;
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+}
diff --git a/libdiskfs/node-nrele.c b/libdiskfs/node-nrele.c
new file mode 100644
index 00000000..cc680893
--- /dev/null
+++ b/libdiskfs/node-nrele.c
@@ -0,0 +1,65 @@
+/*
+ Copyright (C) 1999 Free Software Foundation, Inc.
+ Written by Thomas Bushnell, BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+/* Release a hard reference on NP. If NP is locked by anyone, then
+ this cannot be the last hard reference (because you must hold a
+ hard reference in order to hold the lock). If this is the last
+ hard reference and there are no links, then request soft references
+ to be dropped. */
+void
+diskfs_nrele (struct node *np)
+{
+ int tried_drop_softrefs = 0;
+
+ loop:
+ pthread_spin_lock (&diskfs_node_refcnt_lock);
+ assert (np->references);
+ np->references--;
+ if (np->references + np->light_references == 0)
+ {
+ pthread_mutex_lock (&np->lock);
+ diskfs_drop_node (np);
+ }
+ else if (np->references == 0)
+ {
+ pthread_mutex_lock (&np->lock);
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+ diskfs_lost_hardrefs (np);
+ if (!np->dn_stat.st_nlink && !tried_drop_softrefs)
+ {
+ /* Same issue here as in nput; see that for explanation */
+ pthread_spin_lock (&diskfs_node_refcnt_lock);
+ np->references++;
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+
+ diskfs_try_dropping_softrefs (np);
+ tried_drop_softrefs = 1;
+
+ /* Now we can drop the reference back... */
+ pthread_mutex_unlock (&np->lock);
+ goto loop;
+ }
+ pthread_mutex_unlock (&np->lock);
+ }
+ else
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+}
diff --git a/libdiskfs/node-nrelel.c b/libdiskfs/node-nrelel.c
new file mode 100644
index 00000000..ee53b227
--- /dev/null
+++ b/libdiskfs/node-nrelel.c
@@ -0,0 +1,39 @@
+/*
+ Copyright (C) 1999 Free Software Foundation, Inc.
+ Written by Thomas Bushnell, BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+/* Release a light reference on NP. If NP is locked by anyone, then
+ this cannot be the last reference (because you must hold a
+ hard reference in order to hold the lock). */
+void
+diskfs_nrele_light (struct node *np)
+{
+ pthread_spin_lock (&diskfs_node_refcnt_lock);
+ assert (np->light_references);
+ np->light_references--;
+ if (np->references + np->light_references == 0)
+ {
+ pthread_mutex_lock (&np->lock);
+ diskfs_drop_node (np);
+ }
+ else
+ pthread_spin_unlock (&diskfs_node_refcnt_lock);
+}
diff --git a/libdiskfs/node-rdwr.c b/libdiskfs/node-rdwr.c
new file mode 100644
index 00000000..ed94df44
--- /dev/null
+++ b/libdiskfs/node-rdwr.c
@@ -0,0 +1,77 @@
+/*
+ Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+/* Reading and writing of files. this is called by other filesystem
+ routines and handles extension of files automatically. NP is the
+ node to be read or written, and must be locked. DATA will be
+ written or filled. OFF identifies where in thi fel the I/O is to
+ take place (-1 is not allowed). AMT is the size of DATA and tells
+ how much to copy. DIR is 1 for writing and 0 for reading. CRED is
+ the user doing the access (only used to validate attempted file
+ extension). For reads, *AMTREAD is filled with the amount actually
+ read. */
+error_t
+diskfs_node_rdwr (struct node *np,
+ char *data,
+ off_t off,
+ size_t amt,
+ int dir,
+ struct protid *cred,
+ size_t *amtread)
+{
+ error_t err;
+
+ iohelp_get_conch (&np->conch);
+
+ if (dir)
+ while (off + amt > np->allocsize)
+ {
+ err = diskfs_grow (np, off + amt, cred);
+ if (err)
+ return err;
+ if (np->filemod_reqs)
+ diskfs_notice_filechange (np, FILE_CHANGED_EXTEND, 0, off + amt);
+ }
+
+ if (off + amt > np->dn_stat.st_size)
+ {
+ if (dir)
+ {
+ np->dn_stat.st_size = off + amt;
+ np->dn_set_ctime = 1;
+ }
+ else
+ amt = np->dn_stat.st_size - off;
+ }
+
+ if (amtread)
+ *amtread = amt;
+ else
+ amtread = &amt;
+ err = _diskfs_rdwr_internal (np, data, off, amtread, dir, 0);
+ if (*amtread && diskfs_synchronous)
+ {
+ if (dir)
+ diskfs_file_update (np, 1);
+ else
+ diskfs_node_update (np, 1);
+ }
+
+ return err;
+}
diff --git a/libdiskfs/node-times.c b/libdiskfs/node-times.c
new file mode 100644
index 00000000..67f0142e
--- /dev/null
+++ b/libdiskfs/node-times.c
@@ -0,0 +1,74 @@
+/* Process st_?tim updates marked for a diskfs node.
+
+ Copyright (C) 1994, 1996, 1999, 2000, 2007, 2009 Free Software Foundation,
+ Inc.
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include <maptime.h>
+
+/* If disk is not readonly and the noatime option is not enabled, set
+ NP->dn_set_atime. */
+void
+diskfs_set_node_atime (struct node *np)
+{
+ if (!_diskfs_noatime && !diskfs_check_readonly ())
+ np->dn_set_atime = 1;
+}
+
+/* If NP->dn_set_ctime is set, then modify NP->dn_stat.st_ctim
+ appropriately; do the analogous operation for atime and mtime as well. */
+void
+diskfs_set_node_times (struct node *np)
+{
+ struct timeval t;
+
+ if (!np->dn_set_mtime && !np->dn_set_atime && !np->dn_set_ctime)
+ return;
+
+ maptime_read (diskfs_mtime, &t);
+
+ /* We are careful to test and reset each of these individually, so there
+ is no race condition where a dn_set_?time flag setting gets lost. It
+ is not a problem to have the kind of race where the flag is set after
+ we've tested it and done nothing--as long as the flag remains set so
+ the update will happen at the next call. */
+ if (np->dn_set_mtime)
+ {
+ np->dn_stat.st_mtim.tv_sec = t.tv_sec;
+ np->dn_stat.st_mtim.tv_nsec = t.tv_usec * 1000;
+ np->dn_stat_dirty = 1;
+ np->dn_set_mtime = 0;
+ }
+ if (np->dn_set_atime)
+ {
+ np->dn_stat.st_atim.tv_sec = t.tv_sec;
+ np->dn_stat.st_atim.tv_nsec = t.tv_usec * 1000;
+ np->dn_stat_dirty = 1;
+ np->dn_set_atime = 0;
+ }
+ if (np->dn_set_ctime)
+ {
+ np->dn_stat.st_ctim.tv_sec = t.tv_sec;
+ np->dn_stat.st_ctim.tv_nsec = t.tv_usec * 1000;
+ np->dn_stat_dirty = 1;
+ np->dn_set_ctime = 0;
+ }
+}
diff --git a/libdiskfs/node-update.c b/libdiskfs/node-update.c
new file mode 100644
index 00000000..453dea59
--- /dev/null
+++ b/libdiskfs/node-update.c
@@ -0,0 +1,32 @@
+/*
+ Copyright (C) 1993, 1994 Free Software Foundation
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+
+/* Set on disk fields from NP->dn_stat; update ctime, atime, and mtime
+ if necessary. If WAIT is true, then return only after the physical
+ media has been completely updated. */
+void diskfs_node_update (struct node *np, int wait)
+{
+ diskfs_set_node_times (np);
+ if (np->dn_stat_dirty)
+ diskfs_write_disknode (np, wait);
+}
diff --git a/libdiskfs/opts-append-std.c b/libdiskfs/opts-append-std.c
new file mode 100644
index 00000000..b951bf93
--- /dev/null
+++ b/libdiskfs/opts-append-std.c
@@ -0,0 +1,66 @@
+/* Get standard diskfs run-time options
+
+ Copyright (C) 1995, 96,97,98,99,2002 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.org>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include <argz.h>
+
+#include "priv.h"
+
+error_t
+diskfs_append_std_options (char **argz, size_t *argz_len)
+{
+ error_t err;
+ extern int diskfs_sync_interval;
+
+ if (diskfs_readonly)
+ err = argz_add (argz, argz_len, "--readonly");
+ else
+ err = argz_add (argz, argz_len, "--writable");
+
+ if (!err && _diskfs_nosuid)
+ err = argz_add (argz, argz_len, "--no-suid");
+ if (!err && _diskfs_noexec)
+ err = argz_add (argz, argz_len, "--no-exec");
+ if (!err && _diskfs_noatime)
+ err = argz_add (argz, argz_len, "--no-atime");
+ if (!err && _diskfs_no_inherit_dir_group)
+ err = argz_add (argz, argz_len, "--no-inherit-dir-group");
+
+ if (! err)
+ {
+ if (diskfs_synchronous)
+ err = argz_add (argz, argz_len, "--sync");
+ else if (DEFAULT_SYNC_INTERVAL != diskfs_sync_interval)
+ {
+ if (diskfs_sync_interval == 0)
+ err = argz_add (argz, argz_len, "--no-sync");
+ else
+ {
+ char buf[80];
+ sprintf (buf, "--sync=%d", diskfs_sync_interval);
+ err = argz_add (argz, argz_len, buf);
+ }
+ }
+ }
+
+ return err;
+}
diff --git a/libdiskfs/opts-common.c b/libdiskfs/opts-common.c
new file mode 100644
index 00000000..d37c2868
--- /dev/null
+++ b/libdiskfs/opts-common.c
@@ -0,0 +1,59 @@
+/* Options common to both startup and runtime
+
+ Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <argp.h>
+#include "priv.h"
+
+const struct argp_option diskfs_common_options[] =
+{
+ {"readonly", 'r', 0, 0, "Never write to disk or allow opens for writing"},
+ {"rdonly", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"ro", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"writable", 'w', 0, 0, "Use normal read/write behavior"},
+ {"rdwr", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"rw", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"sync", 's', "INTERVAL", OPTION_ARG_OPTIONAL,
+ "If INTERVAL is supplied, sync all data not actually written to disk"
+ " every INTERVAL seconds, otherwise operate in synchronous mode (the"
+ " default is to sync every " DEFAULT_SYNC_INTERVAL_STRING " seconds)"},
+ {"no-sync", 'n', 0, 0, "Don't automatically sync data to disk"},
+ {"nosync", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"no-suid", 'S', 0, 0, "Don't permit set-uid or set-gid execution"},
+ {"nosuid", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"suid-ok", OPT_SUID_OK, 0, 0, "Enable set-uid execution"},
+ {"no-exec", 'E', 0, 0, "Don't permit any execution of files on this filesystem"},
+ {"noexec", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"exec-ok", OPT_EXEC_OK, 0, 0, "Enable execution of files"},
+ {"no-atime", 'A', 0, 0,
+ "Do not update file access times on disk for reads"},
+ {"noatime", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"atime", OPT_ATIME, 0, 0, "Do update file access times for reads normally"},
+ {"no-inherit-dir-group", OPT_NO_INHERIT_DIR_GROUP, 0, 0,
+ "Create new nodes with gid of the process"},
+ {"nogrpid", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"sysvgroups", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"inherit-dir-group", OPT_INHERIT_DIR_GROUP, 0, 0,
+ "Create new nodes with gid of parent dir (default)"},
+ {"grpid", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {"bsdgroups", 0, 0, OPTION_ALIAS | OPTION_HIDDEN},
+ {0, 0}
+};
diff --git a/libdiskfs/opts-get.c b/libdiskfs/opts-get.c
new file mode 100644
index 00000000..c23e4bec
--- /dev/null
+++ b/libdiskfs/opts-get.c
@@ -0,0 +1,28 @@
+/* Get run-time options
+
+ Copyright (C) 1995,96,2002 Free Software Foundation, Inc.
+ Written by Miles Bader <miles@gnu.org>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+error_t
+diskfs_append_args (char **argz, size_t *argz_len)
+{
+ return diskfs_append_std_options (argz, argz_len);
+}
diff --git a/libdiskfs/opts-runtime.c b/libdiskfs/opts-runtime.c
new file mode 100644
index 00000000..f0d1a382
--- /dev/null
+++ b/libdiskfs/opts-runtime.c
@@ -0,0 +1,24 @@
+/* Default definition for diskfs_runtime_argp
+
+ Copyright (C) 1996, 2004 Free Software Foundation, Inc.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include <argp.h>
+
+struct argp *diskfs_runtime_argp = (struct argp *)&diskfs_std_runtime_argp;
diff --git a/libdiskfs/opts-set.c b/libdiskfs/opts-set.c
new file mode 100644
index 00000000..3cfe3f6b
--- /dev/null
+++ b/libdiskfs/opts-set.c
@@ -0,0 +1,34 @@
+/* Set run-time options
+
+ Copyright (C) 1996, 1998 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <hurd/fshelp.h>
+
+#include "priv.h"
+
+error_t
+diskfs_set_options (const char *argz, size_t argz_len)
+{
+ if (diskfs_runtime_argp)
+ return fshelp_set_options (diskfs_runtime_argp, 0, argz, argz_len, 0);
+ else
+ return EOPNOTSUPP;
+}
diff --git a/libdiskfs/opts-std-runtime.c b/libdiskfs/opts-std-runtime.c
new file mode 100644
index 00000000..177dfaf6
--- /dev/null
+++ b/libdiskfs/opts-std-runtime.c
@@ -0,0 +1,161 @@
+/* Parse standard run-time options
+
+ Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <argp.h>
+
+#include "priv.h"
+
+static const struct argp_option
+std_runtime_options[] =
+{
+ {"update", 'u', 0, 0, "Flush any meta-data cached in core"},
+ {"remount", 0, 0, OPTION_HIDDEN | OPTION_ALIAS}, /* deprecated */
+ {0, 0}
+};
+
+struct parse_hook
+{
+ int readonly, sync, sync_interval, remount, nosuid, noexec, noatime,
+ noinheritdirgroup;
+};
+
+/* Implement the options in H, and free H. */
+static error_t
+set_opts (struct parse_hook *h)
+{
+ error_t err = 0;
+
+ /* Do things in this order: remount, change readonly, change-sync; always
+ do the remount while the disk is readonly, even if only temporarily. */
+
+ if (h->remount)
+ {
+ /* We can only remount while readonly. */
+ err = diskfs_set_readonly (1);
+ if (!err)
+ err = diskfs_remount ();
+ }
+
+ if (h->readonly != diskfs_readonly)
+ {
+ if (err)
+ diskfs_set_readonly (h->readonly); /* keep the old error. */
+ else
+ err = diskfs_set_readonly (h->readonly);
+ }
+
+ /* Change sync mode. */
+ if (h->sync)
+ {
+ diskfs_synchronous = 1;
+ diskfs_set_sync_interval (0); /* Don't waste time syncing. */
+ }
+ else
+ {
+ diskfs_synchronous = 0;
+ if (h->sync_interval >= 0)
+ diskfs_set_sync_interval (h->sync_interval);
+ }
+
+ if (h->nosuid != -1)
+ _diskfs_nosuid = h->nosuid;
+ if (h->noexec != -1)
+ _diskfs_noexec = h->noexec;
+ if (h->noatime != -1)
+ _diskfs_noatime = h->noatime;
+ if (h->noinheritdirgroup != -1)
+ _diskfs_no_inherit_dir_group = h->noinheritdirgroup;
+
+ free (h);
+
+ return err;
+}
+
+/* Parse diskfs standard runtime options. */
+static error_t
+parse_opt (int opt, char *arg, struct argp_state *state)
+{
+ struct parse_hook *h = state->hook;
+ switch (opt)
+ {
+ case 'r': h->readonly = 1; break;
+ case 'w': h->readonly = 0; break;
+ case 'u': h->remount = 1; break;
+ case 'S': h->nosuid = 1; break;
+ case 'E': h->noexec = 1; break;
+ case 'A': h->noatime = 1; break;
+ case OPT_SUID_OK: h->nosuid = 0; break;
+ case OPT_EXEC_OK: h->noexec = 0; break;
+ case OPT_ATIME: h->noatime = 0; break;
+ case OPT_NO_INHERIT_DIR_GROUP: h->noinheritdirgroup = 1; break;
+ case OPT_INHERIT_DIR_GROUP: h->noinheritdirgroup = 0; break;
+ case 'n': h->sync_interval = 0; h->sync = 0; break;
+ case 's':
+ if (arg)
+ {
+ h->sync = 0;
+ h->sync_interval = atoi (arg);
+ }
+ else
+ h->sync = 1;
+ break;
+
+ case ARGP_KEY_INIT:
+ if (state->input)
+ state->hook = state->input; /* Share hook with parent. */
+ else
+ {
+ h = state->hook = malloc (sizeof (struct parse_hook));
+ if (! h)
+ return ENOMEM;
+ h->readonly = diskfs_readonly;
+ h->sync = diskfs_synchronous;
+ h->sync_interval = -1;
+ h->remount = 0;
+ h->nosuid = h->noexec = h->noatime = h->noinheritdirgroup = -1;
+
+ /* We know that we have one child, with which we share our hook. */
+ state->child_inputs[0] = h;
+ }
+ break;
+
+ case ARGP_KEY_ERROR:
+ if (! state->input)
+ free (h);
+ break;
+
+ case ARGP_KEY_SUCCESS:
+ if (! state->input)
+ return set_opts (h);
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+static const struct argp common_argp = { diskfs_common_options, parse_opt };
+
+static const struct argp_child children[] = { {&common_argp}, {0} };
+const struct argp diskfs_std_runtime_argp =
+{
+ std_runtime_options, parse_opt, 0, 0, children
+};
diff --git a/libdiskfs/opts-std-startup.c b/libdiskfs/opts-std-startup.c
new file mode 100644
index 00000000..6fe28758
--- /dev/null
+++ b/libdiskfs/opts-std-startup.c
@@ -0,0 +1,178 @@
+/* Standard startup-time command line parser
+
+ Copyright (C) 1995, 1996, 1997, 1998, 1999, 2001, 2007
+ Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.org>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include <argp.h>
+#include <hurd/store.h>
+#include <hurd/paths.h>
+#include "priv.h"
+
+const char *diskfs_boot_command_line;
+char **_diskfs_boot_command;
+
+int _diskfs_boot_pause;
+
+extern char **diskfs_argv;
+
+mach_port_t diskfs_exec_server_task = MACH_PORT_NULL;
+
+/* ---------------------------------------------------------------- */
+
+#define OPT_HOST_PRIV_PORT (-1)
+#define OPT_DEVICE_MASTER_PORT (-2)
+#define OPT_EXEC_SERVER_TASK (-3)
+#define OPT_BOOT_CMDLINE (-4)
+#define OPT_BOOT_COMMAND (-5)
+#define OPT_BOOT_INIT_PROGRAM (-6)
+#define OPT_BOOT_PAUSE (-7)
+
+static const struct argp_option
+startup_options[] =
+{
+ {"directory", 'C', "DIRECTORY", 0,
+ "Use DIRECTORY as the root of the filesystem"},
+ {"virtual-root", 0, 0, OPTION_ALIAS},
+ {"chroot", 0, 0, OPTION_ALIAS},
+
+ {0,0,0,0, "Boot options:", -2},
+ {"multiboot-command-line", OPT_BOOT_CMDLINE, "ARGS", 0,
+ "Required for bootstrap filesystem, the multiboot kernel command line"},
+ {"bootflags", 0, 0, OPTION_ALIAS|OPTION_HIDDEN},
+ {"boot-init-program", OPT_BOOT_INIT_PROGRAM, "FILE", 0,
+ "For bootstrap filesystem, init program to run (default " _HURD_INIT ")"},
+ {"boot-debug-pause", OPT_BOOT_PAUSE, 0, 0,
+ "Pause for keystroke before starting bootstrap programs"},
+ {"boot-command", OPT_BOOT_COMMAND, 0, 0,
+ "Remaining arguments form command line to run"
+ " at bootstrap instead of init"},
+ {"host-priv-port", OPT_HOST_PRIV_PORT, "PORT"},
+ {"device-master-port", OPT_DEVICE_MASTER_PORT, "PORT"},
+ {"exec-server-task", OPT_EXEC_SERVER_TASK, "PORT"},
+
+ {0}
+};
+
+static error_t
+parse_startup_opt (int opt, char *arg, struct argp_state *state)
+{
+ switch (opt)
+ {
+#define TOGGLE(var, on, off) \
+ case on: var = 1; break; \
+ case off: var = 0; break;
+ TOGGLE (diskfs_readonly, 'r', 'w');
+ TOGGLE (_diskfs_nosuid, 'S', OPT_SUID_OK);
+ TOGGLE (_diskfs_noexec, 'E', OPT_EXEC_OK);
+ TOGGLE (_diskfs_noatime, 'A', OPT_ATIME);
+ TOGGLE (_diskfs_no_inherit_dir_group, OPT_NO_INHERIT_DIR_GROUP,
+ OPT_INHERIT_DIR_GROUP);
+#undef TOGGLE
+
+ case 's':
+ if (arg == NULL)
+ diskfs_synchronous = 1;
+ else
+ diskfs_default_sync_interval = atoi (arg);
+ break;
+ case 'n':
+ diskfs_synchronous = 0;
+ diskfs_default_sync_interval = 0;
+ break;
+
+ /* Boot options */
+ case OPT_DEVICE_MASTER_PORT:
+ _hurd_device_master = atoi (arg); break;
+ case OPT_HOST_PRIV_PORT:
+ _hurd_host_priv = atoi (arg); break;
+ case OPT_EXEC_SERVER_TASK:
+ diskfs_exec_server_task = atoi (arg); break;
+ case OPT_BOOT_CMDLINE:
+ diskfs_boot_command_line = arg; break;
+ case OPT_BOOT_INIT_PROGRAM:
+ diskfs_boot_init_program = arg; break;
+ case OPT_BOOT_PAUSE:
+ _diskfs_boot_pause = 1; break;
+ case 'C':
+ _diskfs_chroot_directory = arg; break;
+
+ case OPT_BOOT_COMMAND:
+ if (state->next == state->argc)
+ argp_error (state, "Command line must follow --boot-command option");
+ _diskfs_boot_command = state->argv + state->next;
+ state->next = state->argc; /* stop parsing */
+ {char **p; for (p = _diskfs_boot_command; *p; ++p) printf("BC %s\n",*p);}
+ break;
+
+ case ARGP_KEY_END:
+ diskfs_argv = state->argv; break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+
+ return 0;
+}
+
+/* Suck in the common arguments. */
+static const struct argp startup_common_argp =
+ { diskfs_common_options, parse_startup_opt };
+static const struct argp_child startup_argp_children[] =
+ { {&startup_common_argp}, {0} };
+
+/* This may be used with argp_parse to parse standard diskfs startup
+ options, possible chained onto the end of a user argp structure. */
+const struct argp
+diskfs_startup_argp =
+{
+ startup_options, parse_startup_opt, 0, 0, startup_argp_children
+};
+
+static error_t
+parse_store_startup_opt (int opt, char *arg, struct argp_state *state)
+{
+ switch (opt)
+ {
+ case ARGP_KEY_INIT:
+ /* Propagate our input to our STORE_ARGP child , which it will use to
+ return what it parses. */
+ state->child_inputs[1] = state->input; break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+
+ return 0;
+}
+
+static const struct argp_child store_argp_children[] =
+ { {&diskfs_startup_argp}, {&store_argp}, {0} };
+
+/* An argp structure for the standard diskfs command line arguments plus a
+ store specification. The address of a location in which to return the
+ resulting struct store_parsed structure should be passed as the input
+ argument to argp_parse; see the declaration for STORE_ARGP in
+ <hurd/store.h> for more information. */
+const struct argp
+diskfs_store_startup_argp =
+{
+ 0, parse_store_startup_opt, 0, 0, store_argp_children
+};
diff --git a/libdiskfs/opts-version.c b/libdiskfs/opts-version.c
new file mode 100644
index 00000000..c26334b2
--- /dev/null
+++ b/libdiskfs/opts-version.c
@@ -0,0 +1,47 @@
+/* Default hook for argp --version handling
+
+ Copyright (C) 1996 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include <argp.h>
+#include <version.h>
+
+#include "priv.h"
+
+static void
+_print_version (FILE *stream, struct argp_state *state)
+{
+ if (argp_program_version)
+ /* If this is non-zero, then the program's probably defined it, so let
+ that take precedence over the default. */
+ fputs (argp_program_version, stream);
+ else if (diskfs_extra_version && *diskfs_extra_version)
+ fprintf (stream, "%s (%s) %s\n",
+ diskfs_server_name, diskfs_extra_version, diskfs_server_version);
+ else
+ fprintf (stream, "%s %s\n", diskfs_server_name, diskfs_server_version);
+
+ /* And because diskfs is big and huge, put our information out too. */
+ fputs (STANDARD_HURD_VERSION (libdiskfs) "\n", stream);
+}
+
+void (*argp_program_version_hook) (FILE *stream, struct argp_state *state)
+ = _print_version;
diff --git a/libdiskfs/peropen-make.c b/libdiskfs/peropen-make.c
new file mode 100644
index 00000000..6d5ca014
--- /dev/null
+++ b/libdiskfs/peropen-make.c
@@ -0,0 +1,72 @@
+/*
+ Copyright (C) 1994,97,99,2001,02 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include <sys/file.h>
+
+/* Create and return a new peropen structure on node NP with open
+ flags FLAGS. */
+error_t
+diskfs_make_peropen (struct node *np, int flags, struct peropen *context,
+ struct peropen **ppo)
+{
+ struct peropen *po = *ppo = malloc (sizeof (struct peropen));
+
+ if (! po)
+ return ENOMEM;
+
+ po->filepointer = 0;
+ po->lock_status = LOCK_UN;
+ refcount_init (&po->refcnt, 0);
+ po->openstat = flags;
+ po->np = np;
+ po->path = NULL;
+
+ if (context)
+ {
+ if (context->path)
+ {
+ po->path = strdup (context->path);
+ if (! po->path)
+ return ENOMEM;
+ }
+
+ po->root_parent = context->root_parent;
+ if (po->root_parent != MACH_PORT_NULL)
+ mach_port_mod_refs (mach_task_self (), po->root_parent,
+ MACH_PORT_RIGHT_SEND, 1);
+
+ po->shadow_root = context->shadow_root;
+ if (po->shadow_root)
+ diskfs_nref (po->shadow_root);
+
+ po->shadow_root_parent = context->shadow_root_parent;
+ if (po->shadow_root_parent != MACH_PORT_NULL)
+ mach_port_mod_refs (mach_task_self (), po->shadow_root_parent,
+ MACH_PORT_RIGHT_SEND, 1);
+ }
+ else
+ {
+ po->root_parent = MACH_PORT_NULL;
+ po->shadow_root_parent = MACH_PORT_NULL;
+ po->shadow_root = _diskfs_chroot_directory ? diskfs_root_node : 0;
+ }
+
+ diskfs_nref (np);
+
+ return 0;
+}
diff --git a/libdiskfs/peropen-rele.c b/libdiskfs/peropen-rele.c
new file mode 100644
index 00000000..877137bb
--- /dev/null
+++ b/libdiskfs/peropen-rele.c
@@ -0,0 +1,49 @@
+/*
+ Copyright (C) 1994, 1996, 1997 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <sys/file.h>
+#include "priv.h"
+
+/* Decrement the reference count on a peropen structure. */
+void
+diskfs_release_peropen (struct peropen *po)
+{
+ if (refcount_deref (&po->refcnt) > 0)
+ return;
+
+ if (po->root_parent)
+ mach_port_deallocate (mach_task_self (), po->root_parent);
+
+ if (po->shadow_root && po->shadow_root != po->np)
+ diskfs_nrele (po->shadow_root);
+
+ if (po->shadow_root_parent)
+ mach_port_deallocate (mach_task_self (), po->shadow_root_parent);
+
+ if (po->lock_status != LOCK_UN)
+ {
+ pthread_mutex_lock (&po->np->lock);
+ fshelp_acquire_lock (&po->np->userlock, &po->lock_status,
+ &po->np->lock, LOCK_UN);
+ diskfs_nput (po->np);
+ }
+ else
+ diskfs_nrele (po->np);
+
+ free (po->path);
+ free (po);
+}
diff --git a/libdiskfs/priv.h b/libdiskfs/priv.h
new file mode 100644
index 00000000..2ac3c9ef
--- /dev/null
+++ b/libdiskfs/priv.h
@@ -0,0 +1,141 @@
+/* Private declarations for fileserver library
+
+ Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2006, 2009 Free
+ Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef DISKFS_PRIV_H
+#define DISKFS_PRIV_H
+
+#include <mach.h>
+#include <hurd.h>
+#include <sys/mman.h>
+#include <hurd/ports.h>
+#include <hurd/fshelp.h>
+#include <hurd/iohelp.h>
+#include <hurd/port.h>
+#include <assert.h>
+#include <argp.h>
+
+#include "diskfs.h"
+
+/* These inhibit setuid or exec. */
+extern int _diskfs_nosuid, _diskfs_noexec;
+
+/* This relaxes the requirement to set `st_atim'. */
+extern int _diskfs_noatime;
+
+/* This enables SysV style group behaviour. New nodes inherit the GID
+ of the user creating them unless the SGID bit is set of the parent
+ directory. */
+extern int _diskfs_no_inherit_dir_group;
+
+/* This is the -C argument value. */
+extern char *_diskfs_chroot_directory;
+
+/* If --boot-command is given, this points to the program and args. */
+extern char **_diskfs_boot_command;
+
+/* Port cell holding a cached port to the exec server. */
+extern struct hurd_port _diskfs_exec_portcell;
+
+volatile struct mapped_time_value *_diskfs_mtime;
+
+extern const struct argp_option diskfs_common_options[];
+/* Option keys for long-only options in diskfs_common_options. */
+#define OPT_SUID_OK 600 /* --suid-ok */
+#define OPT_EXEC_OK 601 /* --exec-ok */
+#define OPT_ATIME 602 /* --atime */
+#define OPT_NO_INHERIT_DIR_GROUP 603 /* --no-inherit-dir-group */
+#define OPT_INHERIT_DIR_GROUP 604 /* --inherit-dir-group */
+
+/* Common value for diskfs_common_options and diskfs_default_sync_interval. */
+#define DEFAULT_SYNC_INTERVAL 30
+#define DEFAULT_SYNC_INTERVAL_STRING STRINGIFY(DEFAULT_SYNC_INTERVAL)
+#define STRINGIFY(x) STRINGIFY_1(x)
+#define STRINGIFY_1(x) #x
+
+/* Diskfs thinks the disk is dirty if this is set. */
+extern int _diskfs_diskdirty;
+
+/* Needed for MiG. */
+typedef struct protid *protid_t;
+typedef struct diskfs_control *control_t;
+typedef struct bootinfo *bootinfo_t;
+
+/* Actually read or write a file. The file size must already permit
+ the requested access. NP is the file to read/write. DATA is a buffer
+ to write from or fill on read. OFFSET is the absolute address (-1
+ not permitted here); AMT is the size of the read/write to perform;
+ DIR is set for writing and clear for reading. The inode must
+ be locked. If NOTIME is set, then don't update the access or
+ modify times on the file. */
+error_t _diskfs_rdwr_internal (struct node *np, char *data, off_t offset,
+ size_t *amt, int dir, int notime);
+
+/* Called when we have a real user environment (complete with proc
+ and auth ports). */
+void _diskfs_init_completed (void);
+
+/* Called in a bootstrap filesystem only, to get the privileged ports. */
+void _diskfs_boot_privports (void);
+
+/* Clean routine for control port. */
+void _diskfs_control_clean (void *);
+
+/* Number of outstanding PT_CTL ports. */
+extern int _diskfs_ncontrol_ports;
+
+/* Lock for _diskfs_ncontrol_ports. */
+extern pthread_spinlock_t _diskfs_control_lock;
+
+/* Callback routines for active translator startup */
+extern fshelp_fetch_root_callback1_t _diskfs_translator_callback1;
+extern fshelp_fetch_root_callback2_t _diskfs_translator_callback2;
+
+/* This macro locks the node associated with PROTID, and then
+ evaluates the expression OPERATION; then it syncs the inode
+ (without waiting) and unlocks everything, and then returns
+ the value `err' (which can be set by OPERATION if desired). */
+#define CHANGE_NODE_FIELD(PROTID, OPERATION) \
+({ \
+ error_t err = 0; \
+ struct node *np; \
+ \
+ if (!(PROTID)) \
+ return EOPNOTSUPP; \
+ \
+ if (diskfs_check_readonly ()) \
+ return EROFS; \
+ \
+ np = (PROTID)->po->np; \
+ \
+ pthread_mutex_lock (&np->lock); \
+ (OPERATION); \
+ if (diskfs_synchronous) \
+ diskfs_node_update (np, 1); \
+ pthread_mutex_unlock (&np->lock); \
+ return err; \
+})
+
+/* Bits the user is permitted to set with io_*_openmodes */
+#define HONORED_STATE_MODES (O_APPEND|O_ASYNC|O_FSYNC|O_NONBLOCK|O_NOATIME)
+
+/* Bits that are turned off after open */
+#define OPENONLY_STATE_MODES \
+ (O_CREAT|O_EXCL|O_NOLINK|O_NOTRANS|O_NONBLOCK|O_EXLOCK|O_SHLOCK)
+
+#endif
diff --git a/libdiskfs/protid-make.c b/libdiskfs/protid-make.c
new file mode 100644
index 00000000..22aaa2e1
--- /dev/null
+++ b/libdiskfs/protid-make.c
@@ -0,0 +1,68 @@
+/*
+ Copyright (C) 1994,95,96,2001 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include <string.h>
+#include <assert.h>
+
+/* Build and return in CRED a protid which has no user identification, for
+ peropen PO. */
+error_t
+diskfs_start_protid (struct peropen *po, struct protid **cred)
+{
+ error_t err =
+ ports_create_port_noinstall (diskfs_protid_class, diskfs_port_bucket,
+ sizeof (struct protid), cred);
+ if (! err)
+ {
+ refcount_ref (&po->refcnt);
+ (*cred)->po = po;
+ (*cred)->shared_object = MACH_PORT_NULL;
+ (*cred)->mapped = 0;
+ }
+ return err;
+}
+
+/* Finish building protid CRED started with diskfs_start_protid;
+ the user to install is USER. */
+void
+diskfs_finish_protid (struct protid *cred, struct iouser *user)
+{
+ error_t err;
+
+ if (!user)
+ err = iohelp_create_simple_iouser (&cred->user, 0, 0);
+ else
+ err = iohelp_dup_iouser (&cred->user, user);
+ assert_perror (err);
+
+ err = mach_port_move_member (mach_task_self (), cred->pi.port_right,
+ diskfs_port_bucket->portset);
+ assert_perror (err);
+}
+
+/* Create and return a protid for an existing peropen PO in CRED for
+ USER. */
+error_t
+diskfs_create_protid (struct peropen *po, struct iouser *user,
+ struct protid **cred)
+{
+ error_t err = diskfs_start_protid (po, cred);
+ if (! err)
+ diskfs_finish_protid (*cred, user);
+ return err;
+}
diff --git a/libdiskfs/protid-rele.c b/libdiskfs/protid-rele.c
new file mode 100644
index 00000000..433b8c9f
--- /dev/null
+++ b/libdiskfs/protid-rele.c
@@ -0,0 +1,35 @@
+/*
+ Copyright (C) 1994, 1999 Free Software Foundation
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+/* Called when a protid CRED has no more references. (Because references
+ to protids are maintained by the port management library, this is
+ installed in the clean routines list.) The ports library will
+ free the structure for us. */
+void
+diskfs_protid_rele (void *arg)
+{
+ struct protid *cred = arg;
+
+ iohelp_free_iouser (cred->user);
+ if (cred->shared_object)
+ mach_port_deallocate (mach_task_self (), cred->shared_object);
+ if (cred->mapped)
+ munmap (cred->mapped, vm_page_size);
+ diskfs_release_peropen (cred->po);
+}
diff --git a/libdiskfs/rdwr-internal.c b/libdiskfs/rdwr-internal.c
new file mode 100644
index 00000000..18a4ae1e
--- /dev/null
+++ b/libdiskfs/rdwr-internal.c
@@ -0,0 +1,74 @@
+/*
+ Copyright (C) 1994,95,96,97,99,2000 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include <string.h>
+#include <fcntl.h>
+#include <hurd/pager.h>
+
+/* Actually read or write a file. The file size must already permit
+ the requested access. NP is the file to read/write. DATA is a buffer
+ to write from or fill on read. OFFSET is the absolute address (-1
+ not permitted here); AMT is the size of the read/write to perform;
+ DIR is set for writing and clear for reading. The inode must
+ be locked. If NOTIME is set, then don't update the mtime or atime. */
+error_t
+_diskfs_rdwr_internal (struct node *np,
+ char *data,
+ off_t offset,
+ size_t *amt,
+ int dir,
+ int notime)
+{
+ memory_object_t memobj;
+ vm_prot_t prot = dir ? (VM_PROT_READ | VM_PROT_WRITE) : VM_PROT_READ;
+ error_t err = 0;
+
+ if (dir)
+ assert (!diskfs_readonly);
+
+ if (*amt == 0)
+ /* Zero-length writes do not update mtime or anything else, by POSIX. */
+ return 0;
+
+ if (!diskfs_check_readonly () && !notime)
+ {
+ if (dir)
+ np->dn_set_mtime = 1;
+ else if (! _diskfs_noatime)
+ np->dn_set_atime = 1;
+ }
+
+ memobj = diskfs_get_filemap (np, prot);
+
+ if (memobj == MACH_PORT_NULL)
+ return errno;
+
+ err = pager_memcpy (diskfs_get_filemap_pager_struct (np), memobj,
+ offset, data, amt, prot);
+
+ if (!diskfs_check_readonly () && !notime)
+ {
+ if (dir)
+ np->dn_set_mtime = 1;
+ else if (!_diskfs_noatime)
+ np->dn_set_atime = 1;
+ }
+
+ mach_port_deallocate (mach_task_self (), memobj);
+ return err;
+}
diff --git a/libdiskfs/readonly-changed.c b/libdiskfs/readonly-changed.c
new file mode 100644
index 00000000..44ee9225
--- /dev/null
+++ b/libdiskfs/readonly-changed.c
@@ -0,0 +1,31 @@
+/* Default hook for change to/from read-only
+
+ Copyright (C) 2001 Free Software Foundation, Inc.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "diskfs.h"
+
+/* The user may define this function. It is called when the disk has been
+ changed from read-only to read-write mode or vice-versa. READONLY is the
+ new state (which is also reflected in DISKFS_READONLY). This function is
+ also called during initial startup if the filesystem is to be writable. */
+void
+diskfs_readonly_changed (int readonly)
+{
+ /* By default do nothing at all. */
+}
diff --git a/libdiskfs/readonly.c b/libdiskfs/readonly.c
new file mode 100644
index 00000000..02f583c8
--- /dev/null
+++ b/libdiskfs/readonly.c
@@ -0,0 +1,111 @@
+/* Change to/from read-only
+
+ Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <fcntl.h>
+#include <error.h>
+
+#include "priv.h"
+
+int _diskfs_diskdirty;
+int diskfs_readonly = 0;
+int diskfs_hard_readonly = 0;
+
+int
+diskfs_check_readonly ()
+{
+ error_t err;
+
+ if (diskfs_readonly)
+ return 1;
+ else
+ {
+ if (!_diskfs_diskdirty)
+ {
+ err = diskfs_set_hypermetadata (1, 0);
+ if (err)
+ {
+ error (0, 0,
+ "%s: MEDIA NOT WRITABLE; switching to READ-ONLY",
+ diskfs_disk_name ?: "-");
+ diskfs_hard_readonly = diskfs_readonly = 1;
+ return 1;
+ }
+ _diskfs_diskdirty = 1;
+ }
+ return 0;
+ }
+}
+
+/* Change an active filesystem between read-only and writable modes, setting
+ the global variable DISKFS_READONLY to reflect the current mode. If an
+ error is returned, nothing will have changed. The user should hold
+ DISKFS_FSYS_LOCK while calling this routine. */
+error_t
+diskfs_set_readonly (int readonly)
+{
+ error_t err = 0;
+
+ if (diskfs_hard_readonly)
+ return readonly ? 0 : EROFS;
+
+ if (readonly != diskfs_readonly)
+ {
+ err = ports_inhibit_class_rpcs (diskfs_protid_class);
+ if (! err)
+ {
+ if (readonly)
+ {
+ error_t peropen_writable (void *pi)
+ {
+ struct protid *const cred = pi;
+ return (cred->po->openstat & O_WRITE) ? EBUSY : 0;
+ }
+
+ /* Any writable open files? */
+ err = ports_class_iterate (diskfs_protid_class,
+ peropen_writable);
+
+ /* Any writable pagers? */
+ if (!err && (diskfs_max_user_pager_prot () & VM_PROT_WRITE))
+ err = EBUSY;
+
+ if (!err)
+ /* Sync */
+ {
+ diskfs_sync_everything (1);
+ diskfs_set_hypermetadata (1, 1);
+ _diskfs_diskdirty = 0;
+ }
+ }
+
+ if (!err)
+ {
+ diskfs_readonly = readonly;
+ diskfs_readonly_changed (readonly);
+ }
+
+ ports_resume_class_rpcs (diskfs_protid_class);
+ }
+ }
+
+ return err;
+}
diff --git a/libdiskfs/remount.c b/libdiskfs/remount.c
new file mode 100644
index 00000000..1ed622f2
--- /dev/null
+++ b/libdiskfs/remount.c
@@ -0,0 +1,47 @@
+/* Remount an active filesystem
+
+ Copyright (C) 1995, 1996 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+/* Re-read all incore data structures from disk. This will only work if
+ DISKFS_READONLY is true. DISKFS_FSYS_LOCK should be held while calling
+ this routine. */
+error_t
+diskfs_remount ()
+{
+ error_t err;
+
+ if (! diskfs_check_readonly ())
+ return EBUSY;
+
+ err = ports_inhibit_class_rpcs (diskfs_protid_class);
+ if (err)
+ return err;
+
+ err = diskfs_reload_global_state ();
+ if (!err)
+ err = diskfs_node_iterate (diskfs_node_reload);
+
+ ports_resume_class_rpcs (diskfs_protid_class);
+
+ return err;
+}
diff --git a/libdiskfs/shutdown.c b/libdiskfs/shutdown.c
new file mode 100644
index 00000000..66deb59a
--- /dev/null
+++ b/libdiskfs/shutdown.c
@@ -0,0 +1,102 @@
+/*
+ Copyright (C) 1993,94,95,96,98,99,2001 Free Software Foundation, Inc.
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+/* Written by Michael I. Bushnell. */
+
+#include "priv.h"
+#include <hurd/fsys.h>
+
+/* Shutdown the filesystem; flags are as for fsys_goaway. */
+error_t
+diskfs_shutdown (int flags)
+{
+ int nports = -1;
+ int err;
+
+ error_t
+ helper (struct node *np)
+ {
+ error_t error;
+ mach_port_t control;
+
+ error = fshelp_fetch_control (&np->transbox, &control);
+ pthread_mutex_unlock (&np->lock);
+ if (!error && (control != MACH_PORT_NULL))
+ {
+ error = fsys_goaway (control, flags);
+ mach_port_deallocate (mach_task_self (), control);
+ }
+ else
+ error = 0;
+ pthread_mutex_lock (&np->lock);
+
+ if ((error == MIG_SERVER_DIED) || (error == MACH_SEND_INVALID_DEST))
+ error = 0;
+
+ return error;
+ }
+
+ if ((flags & FSYS_GOAWAY_UNLINK)
+ && S_ISDIR (diskfs_root_node->dn_stat.st_mode))
+ return EBUSY;
+
+ if (flags & FSYS_GOAWAY_RECURSE)
+ {
+ err = diskfs_node_iterate (helper);
+ if (err)
+ return err;
+ }
+
+ pthread_rwlock_wrlock (&diskfs_fsys_lock);
+
+ /* Permit all the current RPC's to finish, and then
+ suspend new ones. */
+ err = ports_inhibit_class_rpcs (diskfs_protid_class);
+ if (err)
+ {
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ return err;
+ }
+
+ /* Write everything out and set "clean" state. Even if we don't in fact
+ shut down now, this has the nice effect that a disk that has not been
+ written for a long time will not need checking after a crash. */
+ diskfs_sync_everything (1);
+ diskfs_set_hypermetadata (1, 1);
+ _diskfs_diskdirty = 0;
+
+ /* First, see if there are outstanding user ports. */
+ nports = ports_count_class (diskfs_protid_class);
+ if (((flags & FSYS_GOAWAY_FORCE) == 0)
+ && (nports || diskfs_pager_users ()))
+ {
+ ports_enable_class (diskfs_protid_class);
+ ports_resume_class_rpcs (diskfs_protid_class);
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ return EBUSY;
+ }
+
+ if (!diskfs_readonly && (flags & FSYS_GOAWAY_NOSYNC) == 0)
+ {
+ diskfs_shutdown_pager ();
+ diskfs_set_hypermetadata (1, 1);
+ }
+
+ return 0;
+}
diff --git a/libdiskfs/sync-default.c b/libdiskfs/sync-default.c
new file mode 100644
index 00000000..0d1fd93b
--- /dev/null
+++ b/libdiskfs/sync-default.c
@@ -0,0 +1,23 @@
+/* A variable holding the initial sync interval
+
+ Copyright (C) 1995, 1999 Free Software Foundation, Inc.
+
+ Written by Miles Bader <miles@gnu.ai.mit.edu>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+
+int diskfs_default_sync_interval = DEFAULT_SYNC_INTERVAL;
diff --git a/libdiskfs/sync-interval.c b/libdiskfs/sync-interval.c
new file mode 100644
index 00000000..14405f29
--- /dev/null
+++ b/libdiskfs/sync-interval.c
@@ -0,0 +1,136 @@
+/* Support for periodic syncing
+
+ Copyright (C) 1995,96,99,2002 Free Software Foundation, Inc.
+ Written by Miles Bader <miles@gnu.org>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <errno.h>
+#include <pthread.h>
+#include <unistd.h>
+
+#include <hurd/fsys.h>
+
+#include "priv.h"
+
+/* A user-readable variable reflecting the last set sync interval. */
+int diskfs_sync_interval = 0;
+
+/* The thread that's doing the syncing. */
+static pthread_t periodic_sync_thread;
+
+/* This port represents the periodic sync service as if it were
+ an RPC. We can use ports_inhibit_port_rpcs on this port to guarantee
+ that the periodic_sync_thread is quiescent. */
+static struct port_info *pi;
+
+static void * periodic_sync (void *);
+
+/* Establish a thread to sync the filesystem every INTERVAL seconds, or
+ never, if INTERVAL is zero. If an error occurs creating the thread, it is
+ returned, otherwise 0. Subsequent calls will create a new thread and
+ (eventually) get rid of the old one; the old thread won't do any more
+ syncs, regardless. */
+error_t
+diskfs_set_sync_interval (int interval)
+{
+ error_t err = 0;
+
+ if (! pi)
+ {
+ err = ports_create_port (diskfs_control_class, diskfs_port_bucket,
+ sizeof (struct port_info), &pi);
+ if (err)
+ return err;
+ }
+
+ err = ports_inhibit_port_rpcs (pi);
+ if (err)
+ return err;
+
+ /* Here we just set the new thread; any existing thread will notice when it
+ wakes up and go away silently. */
+ if (interval == 0)
+ periodic_sync_thread = 0;
+ else
+ {
+ err = pthread_create (&periodic_sync_thread, NULL, periodic_sync,
+ (void *)(intptr_t) interval);
+ if (!err)
+ pthread_detach (periodic_sync_thread);
+ else
+ {
+ errno = err;
+ perror ("pthread_create");
+ }
+ }
+
+ if (!err)
+ diskfs_sync_interval = interval;
+
+ ports_resume_port_rpcs (pi);
+
+ return err;
+}
+
+/* ---------------------------------------------------------------- */
+
+/* Sync the filesystem (pointed to by the variable CONTROL_PORT above) every
+ INTERVAL seconds, as long as it's in the thread pointed to by the global
+ variable PERIODIC_SYNC_THREAD. */
+static void *
+periodic_sync (void * arg)
+{
+ int interval = (int) arg;
+ for (;;)
+ {
+ error_t err;
+ struct rpc_info link;
+
+ /* This acts as a lock against creation of a new sync thread
+ while we are in the process of syncing. */
+ err = ports_begin_rpc (pi, 0, &link);
+
+ if (periodic_sync_thread != pthread_self ())
+ {
+ /* We've been superseded as the sync thread. Just die silently. */
+ ports_end_rpc (pi, &link);
+ return NULL;
+ }
+
+ if (! err)
+ {
+ if (! diskfs_readonly)
+ {
+ pthread_rwlock_rdlock (&diskfs_fsys_lock);
+ /* Only sync if we need to, to avoid clearing the clean flag
+ when it's just been set. Any other thread doing a sync
+ will have held the lock while it did its work. */
+ if (_diskfs_diskdirty)
+ {
+ diskfs_sync_everything (0);
+ diskfs_set_hypermetadata (0, 0);
+ }
+ pthread_rwlock_unlock (&diskfs_fsys_lock);
+ }
+ ports_end_rpc (pi, &link);
+ }
+
+ /* Wait until next time. */
+ sleep (interval);
+ }
+
+ return NULL;
+}
diff --git a/libdiskfs/trans-callback.c b/libdiskfs/trans-callback.c
new file mode 100644
index 00000000..283b184f
--- /dev/null
+++ b/libdiskfs/trans-callback.c
@@ -0,0 +1,91 @@
+/*
+ Copyright (C) 1995,96,97,98,2001,02 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "priv.h"
+#include <fcntl.h>
+
+/* Callback function needed for calls to fshelp_fetch_root. See
+ <hurd/fshelp.h> for the interface description. */
+static error_t
+_diskfs_translator_callback1_fn (void *cookie1, void *cookie2,
+ uid_t *uid, gid_t *gid,
+ char **argz, size_t *argz_len)
+{
+ error_t err;
+ struct node *np = cookie1;
+
+ if (! (np->dn_stat.st_mode & S_IPTRANS))
+ return ENOENT;
+
+ err = diskfs_get_translator (np, argz, (u_int *) argz_len);
+ if (err)
+ {
+ assert (err != EOPNOTSUPP);
+ return err;
+ }
+
+ *uid = np->dn_stat.st_uid;
+ *gid = np->dn_stat.st_gid;
+
+ return 0;
+}
+
+/* Callback function needed for calls to fshelp_fetch_root. See
+ <hurd/fshelp.h> for the interface description. */
+static error_t
+_diskfs_translator_callback2_fn (void *cookie1, void *cookie2,
+ int flags,
+ mach_port_t *underlying,
+ mach_msg_type_name_t *underlying_type)
+{
+ struct node *np = cookie1;
+ struct protid *cred;
+ struct peropen *po;
+ error_t err;
+ struct iouser *user;
+
+ err = iohelp_create_simple_iouser (&user, np->dn_stat.st_uid,
+ np->dn_stat.st_gid);
+ if (err)
+ return err;
+
+ err = diskfs_make_peropen (np, flags, cookie2, &po);
+ if (! err)
+ {
+ err = diskfs_create_protid (po, user, &cred);
+ if (err)
+ diskfs_release_peropen (po);
+ }
+
+ iohelp_free_iouser (user);
+
+ if (! err)
+ {
+ *underlying = ports_get_right (cred);
+ *underlying_type = MACH_MSG_TYPE_MAKE_SEND;
+ ports_port_deref (cred);
+ }
+ return err;
+}
+
+fshelp_fetch_root_callback1_t _diskfs_translator_callback1 =
+ _diskfs_translator_callback1_fn;
+fshelp_fetch_root_callback2_t _diskfs_translator_callback2 =
+ _diskfs_translator_callback2_fn;
diff --git a/libdiskfs/validate-author.c b/libdiskfs/validate-author.c
new file mode 100644
index 00000000..d7dacabd
--- /dev/null
+++ b/libdiskfs/validate-author.c
@@ -0,0 +1,27 @@
+/* Default version of diskfs_validate_author_change
+ Copyright (C) 1996 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+error_t
+diskfs_validate_author_change (struct node *np, uid_t author)
+{
+ return 0;
+}
diff --git a/libdiskfs/validate-flags.c b/libdiskfs/validate-flags.c
new file mode 100644
index 00000000..bb54dadd
--- /dev/null
+++ b/libdiskfs/validate-flags.c
@@ -0,0 +1,27 @@
+/* Default version of diskfs_validate_flags_change
+ Copyright (C) 1996 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+error_t
+diskfs_validate_flags_change (struct node *np, int flags)
+{
+ return 0;
+}
diff --git a/libdiskfs/validate-group.c b/libdiskfs/validate-group.c
new file mode 100644
index 00000000..556c5aaa
--- /dev/null
+++ b/libdiskfs/validate-group.c
@@ -0,0 +1,27 @@
+/* Default version of diskfs_validate_group_change
+ Copyright (C) 1996 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+error_t
+diskfs_validate_group_change (struct node *np, gid_t group)
+{
+ return 0;
+}
diff --git a/libdiskfs/validate-mode.c b/libdiskfs/validate-mode.c
new file mode 100644
index 00000000..1cf150e6
--- /dev/null
+++ b/libdiskfs/validate-mode.c
@@ -0,0 +1,27 @@
+/* Default version of diskfs_validate_mode_change
+ Copyright (C) 1996 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+error_t
+diskfs_validate_mode_change (struct node *np, mode_t mode)
+{
+ return 0;
+}
diff --git a/libdiskfs/validate-owner.c b/libdiskfs/validate-owner.c
new file mode 100644
index 00000000..0cbe296c
--- /dev/null
+++ b/libdiskfs/validate-owner.c
@@ -0,0 +1,27 @@
+/* Default version of diskfs_validate_owner_change
+ Copyright (C) 1996 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+error_t
+diskfs_validate_owner_change (struct node *np, uid_t uid)
+{
+ return 0;
+}
diff --git a/libdiskfs/validate-rdev.c b/libdiskfs/validate-rdev.c
new file mode 100644
index 00000000..2d766346
--- /dev/null
+++ b/libdiskfs/validate-rdev.c
@@ -0,0 +1,27 @@
+/* Default version of diskfs_validate_rdev_change
+ Copyright (C) 1996 Free Software Foundation, Inc.
+ Written by Michael I. Bushnell, p/BSG.
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+
+#include "priv.h"
+
+error_t
+diskfs_validate_rdev_change (struct node *np, dev_t rdev)
+{
+ return 0;
+}