minix/include/minix/safecopies.h
Ben Gras 565f13088f make vfs & filesystems use failable copying
Change the kernel to add features to vircopy and safecopies so that
transparent copy fixing won't happen to avoid deadlocks, and such copies
fail with EFAULT.

Transparently making copying work from filesystems (as normally done by
the kernel & VM when copying fails because of missing/readonly memory)
is problematic as it can happen that, for file-mapped ranges, that that
same filesystem that is blocked on the copy request is needed to satisfy
the memory range, leading to deadlock. Dito for VFS itself, if done with
a blocking call.

This change makes the copying done from a filesystem fail in such cases
with EFAULT by VFS adding the CPF_TRY flag to the grants. If a FS call
fails with EFAULT, VFS will then request the range to be made available
to VM after the FS is unblocked, allowing it to be used to satisfy the
range if need be in another VFS thread.

Similarly, for datacopies that VFS itself does, it uses the failable
vircopy variant and callers use a wrapper that talk to VM if necessary
to get the copy to work.

	. kernel: add CPF_TRY flag to safecopies
	. kernel: only request writable ranges to VM for the
	  target buffer when copying fails
	. do copying in VFS TRY-first
	. some fixes in VM to build SANITYCHECK mode
	. add regression test for the cases where
	  - a FS system call needs memory mapped in a process that the
	    FS itself must map.
	  - such a range covers more than one file-mapped region.
	. add 'try' mode to vircopy, physcopy
	. add flags field to copy kernel call messages
	. if CP_FLAG_TRY is set, do not transparently try
	  to fix memory ranges
	. for use by VFS when accessing user buffers to avoid
	  deadlock
	. remove some obsolete backwards compatability assignments
        . VFS: let thread scheduling work for VM requests too
          Allows VFS to make calls to VM while suspending and resuming
          the currently running thread. Does currently not work for the
          main thread.
        . VM: add fix memory range call for use by VFS

Change-Id: I295794269cea51a3163519a9cfe5901301d90b32
2014-07-28 17:05:14 +02:00

94 lines
3.1 KiB
C

#ifndef _MINIX_SAFECOPIES_H
#define _MINIX_SAFECOPIES_H 1
#include <minix/sys_config.h>
#include <sys/types.h>
#include <minix/vm.h>
#include <stdint.h>
typedef struct {
int cp_flags; /* CPF_* below */
union {
struct {
/* CPF_DIRECT */
endpoint_t cp_who_to; /* grantee */
vir_bytes cp_start; /* memory */
size_t cp_len; /* size in bytes */
char cp_reserved[8]; /* future use */
} cp_direct;
struct {
/* CPF_INDIRECT */
endpoint_t cp_who_to; /* grantee */
endpoint_t cp_who_from; /* previous granter */
cp_grant_id_t cp_grant; /* previous grant */
char cp_reserved[8];/* future use */
} cp_indirect;
struct {
/* CPF_MAGIC */
endpoint_t cp_who_from; /* granter */
endpoint_t cp_who_to; /* grantee */
vir_bytes cp_start; /* memory */
size_t cp_len; /* size in bytes */
char cp_reserved[8]; /* future use */
} cp_magic;
} cp_u;
char cp_reserved[8]; /* future use */
} cp_grant_t;
/* Vectored safecopy. */
struct vscp_vec {
/* Exactly one of the following must be SELF. */
endpoint_t v_from; /* source */
endpoint_t v_to; /* destination */
cp_grant_id_t v_gid; /* grant id of other process */
size_t v_offset; /* offset in other grant */
vir_bytes v_addr; /* address in copier's space */
size_t v_bytes; /* no. of bytes */
};
/* Invalid grant number. */
#define GRANT_INVALID ((cp_grant_id_t) -1)
#define GRANT_VALID(g) ((g) > GRANT_INVALID)
/* Operations: any combination is ok. */
#define CPF_READ 0x000001 /* Granted process may read. */
#define CPF_WRITE 0x000002 /* Granted process may write. */
/* Grant flags. */
#define CPF_TRY 0x000010 /* Fail fast on unmapped memory. */
/* Internal flags. */
#define CPF_USED 0x000100 /* Grant slot in use. */
#define CPF_DIRECT 0x000200 /* Grant from this process to another. */
#define CPF_INDIRECT 0x000400 /* Grant from grant to another. */
#define CPF_MAGIC 0x000800 /* Grant from any to any. */
#define CPF_VALID 0x001000 /* Grant slot contains valid grant. */
/* Prototypes for functions in libsys. */
cp_grant_id_t cpf_grant_direct(endpoint_t, vir_bytes, size_t, int);
cp_grant_id_t cpf_grant_indirect(endpoint_t, endpoint_t, cp_grant_id_t);
cp_grant_id_t cpf_grant_magic(endpoint_t, endpoint_t, vir_bytes, size_t,
int);
int cpf_revoke(cp_grant_id_t grant_id);
int cpf_lookup(cp_grant_id_t g, endpoint_t *ep, endpoint_t *ep2);
int cpf_getgrants(cp_grant_id_t *grant_ids, int n);
int cpf_setgrant_direct(cp_grant_id_t g, endpoint_t who, vir_bytes addr,
size_t size, int access);
int cpf_setgrant_indirect(cp_grant_id_t g, endpoint_t who_to, endpoint_t
who_from, cp_grant_id_t his_g);
int cpf_setgrant_magic(cp_grant_id_t g, endpoint_t who_to, endpoint_t
who_from, vir_bytes addr, size_t bytes, int access);
int cpf_setgrant_disable(cp_grant_id_t grant_id);
void cpf_reload(void);
/* Set a process' grant table location and size (in-kernel only). */
#define _K_SET_GRANT_TABLE(rp, ptr, entries) \
priv(rp)->s_grant_table= (ptr); \
priv(rp)->s_grant_entries= (entries);
#endif /* _MINIX_SAFECOPIES_H */