Skip to content

Instantly share code, notes, and snippets.

@tuxillo
Created March 2, 2015 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxillo/4d2b60a6d132f01652a4 to your computer and use it in GitHub Desktop.
Save tuxillo/4d2b60a6d132f01652a4 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2015 The DragonFly Project. All rights reserved.
*
* This code is derived from software contributed to The DragonFly Project
* by Matthew Dillon <dillon@backplane.com>
* by Antonio Huete Jimenez <tuxillo@quantumachine.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of The DragonFly Project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific, prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "hammer.h"
static int
locate_objid(hammer_transaction_t transp, hammer_inode_t ip,
hammer_cursor_t cursorp)
{
struct hammer_btree_leaf_elm leaf;
int error;
bzero(&leaf, sizeof(leaf));
/* XXX Check if ip->cache is null */
error = hammer_init_cursor(transp, cursorp, &ip->cache[0], ip);
if (error) {
hammer_done_cursor(cursorp);
return error;
}
/*
* Starting point for the search
*/
cursorp->key_beg.localization = ip->obj_localization +
HAMMER_LOCALIZE_MISC;
cursorp->key_beg.obj_id = ip->ino_leaf.base.obj_id;
cursorp->key_beg.create_tid = 0;
cursorp->key_beg.delete_tid = 0;
cursorp->key_beg.obj_type = HAMMER_OBJTYPE_UNKNOWN;
cursorp->key_beg.rec_type = HAMMER_RECTYPE_EXT;
cursorp->key_beg.key = 0; /* First ACL entry */
cursorp->asof = ip->obj_asof;
cursorp->flags |= HAMMER_CURSOR_ASOF;
/*
* Ending point for the search
*/
return 0;
}
static void __unused
dumpdata(acl_entry_t ae)
{
int i;
char *p;
unsigned char c;
p = (char *)ae;
for (i = 0; i < sizeof(*ae); i++) {
c = *(p+i);
kprintf("%02x ", c);
}
kprintf("\n");
}
int
hammer_acl_lookup_get(hammer_transaction_t transp, hammer_inode_t ip,
hammer_acl_t hap)
{
struct hammer_cursor cursor;
struct acl_entry *ae;
int error = 0;
int idx = 0;
locate_objid(transp, ip, &cursor);
/*
* ACLs entries are stored in the btree as special records
* HAMMER_RECTYPE_EXT and indexed by their obj_id. Each entry
* has a key corresponding to its position in the acl_entry array
* in struct acl.
*/
while (error == 0 && idx < ACL_MAX_ENTRIES) {
error = hammer_ip_lookup(&cursor);
if (error)
goto out;
kprintf("%s: objid=%jd key=%jd loc=0x%x\n", __func__,
cursor.leaf->base.obj_id, cursor.leaf->base.key,
cursor.leaf->base.localization);
error = hammer_ip_resolve_data(&cursor);
if (error)
goto out;
ae = &hap->acl.acl_entry[idx++];
bcopy((char *)&cursor.data, (char *)ae, cursor.leaf->data_len);
// dumpdata(ae);
cursor.key_beg.key = (int64_t)idx;
}
out:
hap->acl.acl_cnt = idx;
hammer_done_cursor(&cursor);
return error;
}
int
hammer_acl_set(hammer_transaction_t transp, hammer_inode_t ip,
acl_type_t typep, acl_t aclp)
{
struct hammer_cursor cursor;
struct acl_entry *ae;
hammer_record_t record;
hammer_mount_t hmp;
int error;
int i;
error = 0;
hmp = ip->hmp;
error = locate_objid(transp, ip, &cursor);
if (error)
goto out;
// error = hammer_ip_first(&cursor);
// if (error && error != ENOENT)
// goto out;
for (i = 0; i < aclp->acl_cnt && i < ACL_MAX_ENTRIES; i++) {
if (cursor.leaf) {
kprintf("%s: objid=%jd key=%jd loc=0x%x\n", __func__,
cursor.leaf->base.obj_id, cursor.leaf->base.key,
cursor.leaf->base.localization);
}
record = hammer_alloc_mem_record(ip, sizeof(struct acl_entry));
record->type = HAMMER_MEM_RECORD_GENERAL;
record->leaf.base.localization = ip->obj_localization +
HAMMER_LOCALIZE_MISC;
record->leaf.base.rec_type = HAMMER_RECTYPE_EXT;
record->leaf.base.obj_id = ip->ino_leaf.base.obj_id;
record->leaf.base.obj_type = HAMMER_OBJTYPE_UNKNOWN;
record->leaf.base.key = i;
record->leaf.data_len = sizeof(*ae);
ae = &aclp->acl_entry[i];
bcopy((char *)ae, (char *)record->data, sizeof(*ae));
error = hammer_mem_add(record);
kprintf("id=%d perm=%d\n", ae->ae_id, ae->ae_perm);
}
out:
hammer_done_cursor(&cursor);
return error;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment