Skip to content

Commit

Permalink
lib9p illumos port
Browse files Browse the repository at this point in the history
Portions contributed by: Andy Fiddaman <[email protected]>
  • Loading branch information
Jason King authored and citrus-it committed Aug 16, 2021
1 parent 7ef4667 commit 19d3ba7
Show file tree
Hide file tree
Showing 16 changed files with 972 additions and 166 deletions.
271 changes: 224 additions & 47 deletions backend/fs.c

Large diffs are not rendered by default.

90 changes: 88 additions & 2 deletions genacl.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@
#include <sys/acl.h>
#include <sys/stat.h>

#ifdef __illumos__
#include <sys/sysmacros.h>
#endif

#include "lib9p.h"
#include "lib9p_impl.h"
#include "genacl.h"
#include "fid.h"
#include "log.h"

#ifndef __illumos__
typedef int econvertfn(acl_entry_t, struct l9p_ace *);
#endif

#ifndef __APPLE__
#ifdef __FreeBSD__
static struct l9p_acl *l9p_new_acl(uint32_t acetype, uint32_t aceasize);
static struct l9p_acl *l9p_growacl(struct l9p_acl *acl, uint32_t aceasize);
static int l9p_count_aces(acl_t sysacl);
Expand Down Expand Up @@ -453,7 +459,7 @@ l9p_ace_mask_to_rwx(int32_t opmask)
return (rwx);
}

#ifndef __APPLE__
#if defined(__FreeBSD__) || defined(__illumos__)
/*
* Allocate new ACL holder and ACEs.
*/
Expand All @@ -473,7 +479,9 @@ l9p_new_acl(uint32_t acetype, uint32_t aceasize)
}
return (ret);
}
#endif

#ifdef __FreeBSD__
/*
* Expand ACL to accomodate more entries.
*
Expand Down Expand Up @@ -718,3 +726,81 @@ l9p_darwin_nfsv4acl_to_acl(acl_t sysacl)
{
}
#endif

#if defined(HAVE__ILLUMOS_ACLS)

static struct {
uint16_t ace_flag;
uint32_t l9_flag;
} ace_flag_tbl[] = {
{ ACE_FILE_INHERIT_ACE, L9P_ACEF_FILE_INHERIT_ACE },
{ ACE_DIRECTORY_INHERIT_ACE, L9P_ACEF_DIRECTORY_INHERIT_ACE },
{ ACE_NO_PROPAGATE_INHERIT_ACE, L9P_ACEF_NO_PROPAGATE_INHERIT_ACE },
{ ACE_INHERIT_ONLY_ACE, L9P_ACEF_INHERIT_ONLY_ACE },
{ ACE_SUCCESSFUL_ACCESS_ACE_FLAG,
L9P_ACEF_SUCCESSFUL_ACCESS_ACE_FLAG },
{ ACE_IDENTIFIER_GROUP, L9P_ACEF_IDENTIFIER_GROUP },
/* There doesn't appear to be an equivalent for ACE_INHERITED_ACE */
{ ACE_OWNER, L9P_ACEF_OWNER },
{ ACE_GROUP, L9P_ACEF_GROUP },
{ ACE_EVERYONE, L9P_ACEF_EVERYONE }
};

struct l9p_acl *
l9p_illumos_nfsv4acl_to_acl(acl_t *sysacl)
{
struct l9p_acl *l9acl;
struct l9p_ace *l9ace;
ace_t *ent;
int i, j;

/* We only support NFSv4 ACLs.. so don't try this on UFS */
if (sysacl->acl_type != ACE_T)
return (NULL);

l9acl = l9p_new_acl(L9P_ACLTYPE_NFSv4, sysacl->acl_cnt);
if (l9acl == NULL)
return (NULL);

ent = sysacl->acl_aclp;
l9ace = l9acl->acl_aces;
for (i = 0; i < sysacl->acl_cnt; i++, ent++, l9ace++) {
switch (ent->a_type) {
case ACE_ACCESS_ALLOWED_ACE_TYPE:
l9ace->ace_type = L9P_ACET_ACCESS_ALLOWED;
break;
case ACE_ACCESS_DENIED_ACE_TYPE:
l9ace->ace_type = L9P_ACET_ACCESS_DENIED;
break;
case ACE_SYSTEM_AUDIT_ACE_TYPE:
l9ace->ace_type = L9P_ACET_SYSTEM_AUDIT;
break;
case ACE_SYSTEM_ALARM_ACE_TYPE:
l9ace->ace_type = L9P_ACET_SYSTEM_ALARM;
break;
default:
L9P_LOG(L9P_ERROR, "invalid ACL type");
l9p_acl_free(l9acl);
return (NULL);
}

l9ace->ace_flags = 0;
for (j = 0; j < ARRAY_SIZE(ace_flag_tbl); j++) {
if ((ent->a_flags & ace_flag_tbl[j].ace_flag) != 0)
l9ace->ace_flags |= ace_flag_tbl[j].l9_flag;
}

/*
* In a bit of good fortune, the bit values for ace_t masks
* and l9p masks are the same (l9p does have WRITE_RETENTION
* and WRITE_RETENTION_HOLD which aren't used -- we're also
* going ace_t->l9p so they dont matter in this context).
*/
l9ace->ace_mask = ent->a_access_mask;
l9ace->ace_idsize = sizeof (ent->a_who);
memcpy(l9acl->acl_aces, &ent->a_who, sizeof (ent->a_who));
}

return (l9acl);
}
#endif
9 changes: 9 additions & 0 deletions genacl.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
#define HAVE_FREEBSD_ACLS
#endif

#if defined (__illumos__)
#define HAVE_POSIX_ACLS
#define HAVE__ILLUMOS_ACLS
#endif

#include <sys/types.h>
#include <sys/acl.h> /* XXX assumes existence of sys/acl.h */

Expand Down Expand Up @@ -302,6 +307,10 @@ struct l9p_acl *l9p_darwin_nfsv4acl_to_acl(acl_t acl);
struct l9p_acl *l9p_freebsd_nfsv4acl_to_acl(acl_t acl);
#endif

#if defined(HAVE__ILLUMOS_ACLS)
struct l9p_acl *l9p_illumos_nfsv4acl_to_acl(acl_t *acl);
#endif

#if defined(HAVE_POSIX_ACLS) && 0 /* not yet */
struct l9p_acl *l9p_posix_acl_to_acl(acl_t acl);
#endif
35 changes: 22 additions & 13 deletions hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ht_init(struct ht *h, ssize_t size)
memset(h, 0, sizeof(struct ht));
h->ht_nentries = size;
h->ht_entries = l9p_calloc((size_t)size, sizeof(struct ht_entry));
pthread_rwlock_init(&h->ht_rwlock, NULL);
(void) pthread_rwlock_init(&h->ht_rwlock, NULL);

for (i = 0; i < size; i++)
TAILQ_INIT(&h->ht_entries[i].hte_items);
Expand All @@ -65,7 +65,7 @@ ht_destroy(struct ht *h)
}
}

pthread_rwlock_destroy(&h->ht_rwlock);
(void) pthread_rwlock_destroy(&h->ht_rwlock);
free(h->ht_entries);
h->ht_entries = NULL;
}
Expand All @@ -75,9 +75,10 @@ ht_find(struct ht *h, uint32_t hash)
{
void *result;

ht_rdlock(h);
if (ht_rdlock(h) != 0)
return (NULL);
result = ht_find_locked(h, hash);
ht_unlock(h);
(void) ht_unlock(h);
return (result);
}

Expand All @@ -102,14 +103,17 @@ ht_add(struct ht *h, uint32_t hash, void *value)
{
struct ht_entry *entry;
struct ht_item *item;
int err;

if ((err = ht_wrlock(h)) != 0)
return (err);

ht_wrlock(h);
entry = &h->ht_entries[hash % h->ht_nentries];

TAILQ_FOREACH(item, &entry->hte_items, hti_link) {
if (item->hti_hash == hash) {
errno = EEXIST;
ht_unlock(h);
(void) ht_unlock(h);
return (-1);
}
}
Expand All @@ -118,7 +122,7 @@ ht_add(struct ht *h, uint32_t hash, void *value)
item->hti_hash = hash;
item->hti_data = value;
TAILQ_INSERT_TAIL(&entry->hte_items, item, hti_link);
ht_unlock(h);
(void) ht_unlock(h);

return (0);
}
Expand All @@ -127,10 +131,12 @@ int
ht_remove(struct ht *h, uint32_t hash)
{
int result;
int err;

ht_wrlock(h);
if ((err = ht_wrlock(h)) != 0)
return (err);
result = ht_remove_locked(h, hash);
ht_unlock(h);
(void) ht_unlock(h);
return (result);
}

Expand Down Expand Up @@ -205,6 +211,7 @@ ht_remove_at_iter(struct ht_iter *iter)
struct ht_item *item;
struct ht *h;
ssize_t slot;
int err;

assert(iter != NULL);

Expand All @@ -215,11 +222,12 @@ ht_remove_at_iter(struct ht_iter *iter)

/* remove the item from the table, saving the NEXT one */
h = iter->htit_parent;
ht_wrlock(h);
if ((err = ht_wrlock(h)) != 0)
return (err);
slot = iter->htit_slot;
iter->htit_next = ht_iter_advance(iter, item);
TAILQ_REMOVE(&h->ht_entries[slot].hte_items, item, hti_link);
ht_unlock(h);
(void) ht_unlock(h);

/* mark us as no longer on an item, then free it */
iter->htit_curr = NULL;
Expand Down Expand Up @@ -257,9 +265,10 @@ ht_next(struct ht_iter *iter)
if ((item = iter->htit_next) == NULL) {
/* no pre-loaded next; find next from current */
h = iter->htit_parent;
ht_rdlock(h);
if (ht_rdlock(h) != 0)
return (NULL);
item = ht_iter_advance(iter, iter->htit_curr);
ht_unlock(h);
(void) ht_unlock(h);
} else
iter->htit_next = NULL;
iter->htit_curr = item;
Expand Down
26 changes: 26 additions & 0 deletions illumos_endian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef __ILLUMOS_ENDIAN_H
#define __ILLUMOS_ENDIAN_H

/*
* Shims to make illumos' endian headers and macros compatible
* with FreeBSD's <sys/endian.h>
*/

# include <endian.h>

# define _COMPAT_LITTLE_ENDIAN 0x12345678
# define _COMPAT_BIG_ENDIAN 0x87654321

# ifdef _LITTLE_ENDIAN
# define _BYTE_ORDER _COMPAT_LITTLE_ENDIAN
# endif
# ifdef _BIG_ENDIAN
# define _BYTE_ORDER _COMPAT_BIG_ENDIAN
# endif

# undef _LITTLE_ENDIAN
# undef _BIG_ENDIAN
# define _LITTLE_ENDIAN _COMPAT_LITTLE_ENDIAN
# define _BIG_ENDIAN _COMPAT_BIG_ENDIAN

#endif /* __ILLUMOS_ENDIAN_H */
2 changes: 1 addition & 1 deletion lib9p.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void l9p_respond(struct l9p_request *req, bool drop, bool rmtag);

void l9p_init_msg(struct l9p_message *msg, struct l9p_request *req,
enum l9p_pack_mode mode);
void l9p_seek_iov(struct iovec *iov1, size_t niov1, struct iovec *iov2,
void l9p_seek_iov(const struct iovec *iov1, size_t niov1, struct iovec *iov2,
size_t *niov2, size_t seek);
size_t l9p_truncate_iov(struct iovec *iov, size_t niov, size_t length);
void l9p_describe_fcall(union l9p_fcall *fcall, enum l9p_version version,
Expand Down
3 changes: 3 additions & 0 deletions pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#include <sys/param.h>
#ifdef __APPLE__
# include "apple_endian.h"
#elif __illumos__
# include "illumos_endian.h"
# include <sys/sysmacros.h>
#else
# include <sys/endian.h>
#endif
Expand Down
6 changes: 6 additions & 0 deletions request.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <errno.h>
#include <sys/param.h>
#include <sys/uio.h>
#ifdef __illumos__
#include <sys/sysmacros.h>
#endif
#if defined(__FreeBSD__)
#include <sys/sbuf.h>
#else
Expand Down Expand Up @@ -293,7 +296,9 @@ e2linux(int errnum)
[EHOSTDOWN] = LINUX_EHOSTDOWN,
[EHOSTUNREACH] = LINUX_EHOSTUNREACH,
[ENOTEMPTY] = LINUX_ENOTEMPTY,
#ifndef __illumos__
[EPROCLIM] = LINUX_EAGAIN,
#endif
[EUSERS] = LINUX_EUSERS,
[EDQUOT] = LINUX_EDQUOT,
[ESTALE] = LINUX_ESTALE,
Expand Down Expand Up @@ -391,6 +396,7 @@ l9p_respond(struct l9p_request *req, bool drop, bool rmtag)

switch (req->lr_flushstate) {
case L9P_FLUSH_NONE:
default:
ftype = "";
break;
case L9P_FLUSH_REQUESTED_PRE_START:
Expand Down
2 changes: 1 addition & 1 deletion rfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ r_pgexpand(struct r_pgdata *pg)

nsize = pg->r_pgbufsize << 1;
if (nsize >= (1 << 20) ||
(pg->r_pgbuf = realloc(pg->r_pgbuf, nsize)) == NULL)
(pg->r_pgbuf = reallocf(pg->r_pgbuf, nsize)) == NULL)
return (ENOMEM);
return (0);
}
Expand Down
4 changes: 4 additions & 0 deletions rfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#ifndef LIB9P_RFUNCS_H
#define LIB9P_RFUNCS_H

#if defined(__illumos__) && !defined(_POSIX_PTHREAD_SEMANTICS)
#define _POSIX_PTHREAD_SEMANTICS 1
#endif

#include <grp.h>
#include <pwd.h>
#include <string.h>
Expand Down
Loading

0 comments on commit 19d3ba7

Please sign in to comment.