]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/sa.c
cstyle: Resolve C style issues
[mirror_zfs.git] / module / zfs / sa.c
index f4d264f7136a4910204c48e290015995605e4996..13c09f92c707911fe74122f6a089d329b0a88bcc 100644 (file)
@@ -21,7 +21,7 @@
 
 /*
  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
- * Copyright (c) 2012 by Delphix. All rights reserved.
+ * Copyright (c) 2013 by Delphix. All rights reserved.
  */
 
 #include <sys/zfs_context.h>
  * location.
  *
  * Byteswap implications:
+ *
  * Since the SA attributes are not entirely self describing we can't do
  * the normal byteswap processing.  The special ZAP layout attribute and
  * attribute registration attributes define the byteswap function and the
@@ -136,7 +137,7 @@ static int sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
     sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
     uint16_t buflen, dmu_tx_t *tx);
 
-arc_byteswap_func_t *sa_bswap_table[] = {
+arc_byteswap_func_t sa_bswap_table[] = {
        byteswap_uint64_array,
        byteswap_uint32_array,
        byteswap_uint16_array,
@@ -188,7 +189,6 @@ sa_attr_reg_t sa_legacy_attrs[] = {
 };
 
 /*
- * ZPL legacy layout
  * This is only used for objects of type DMU_OT_ZNODE
  */
 sa_attr_type_t sa_legacy_zpl_layout[] = {
@@ -198,7 +198,6 @@ sa_attr_type_t sa_legacy_zpl_layout[] = {
 /*
  * Special dummy layout used for buffers with no attributes.
  */
-
 sa_attr_type_t sa_dummy_zpl_layout[] = { 0 };
 
 static int sa_legacy_attr_count = 16;
@@ -252,7 +251,7 @@ sa_cache_fini(void)
 void *
 sa_spill_alloc(int flags)
 {
-       return kmem_cache_alloc(spill_cache, flags);
+       return (kmem_cache_alloc(spill_cache, flags));
 }
 
 void
@@ -390,7 +389,7 @@ sa_attr_op(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
                switch (data_op) {
                case SA_LOOKUP:
                        if (bulk[i].sa_addr == NULL)
-                               return (ENOENT);
+                               return (SET_ERROR(ENOENT));
                        if (bulk[i].sa_data) {
                                SA_COPY_DATA(bulk[i].sa_data_func,
                                    bulk[i].sa_addr, bulk[i].sa_data,
@@ -522,7 +521,7 @@ sa_resize_spill(sa_handle_t *hdl, uint32_t size, dmu_tx_t *tx)
                blocksize = SPA_MINBLOCKSIZE;
        } else if (size > SPA_MAXBLOCKSIZE) {
                ASSERT(0);
-               return (EFBIG);
+               return (SET_ERROR(EFBIG));
        } else {
                blocksize = P2ROUNDUP_TYPED(size, SPA_MINBLOCKSIZE, uint32_t);
        }
@@ -572,10 +571,9 @@ sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
 {
        int var_size = 0;
        int i;
-       int j = -1;
        int full_space;
        int hdrsize;
-       boolean_t done = B_FALSE;
+       int extra_hdrsize;
 
        if (buftype == SA_BONUS && sa->sa_force_spill) {
                *total = 0;
@@ -586,10 +584,9 @@ sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
 
        *index = -1;
        *total = 0;
+       *will_spill = B_FALSE;
 
-       if (buftype == SA_BONUS)
-               *will_spill = B_FALSE;
-
+       extra_hdrsize = 0;
        hdrsize = (SA_BONUSTYPE_FROM_DB(db) == DMU_OT_ZNODE) ? 0 :
            sizeof (sa_hdr_phys_t);
 
@@ -601,8 +598,8 @@ sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
 
                *total = P2ROUNDUP(*total, 8);
                *total += attr_desc[i].sa_length;
-               if (done)
-                       goto next;
+               if (*will_spill)
+                       continue;
 
                is_var_sz = (SA_REGISTERED_LEN(sa, attr_desc[i].sa_attr) == 0);
                if (is_var_sz) {
@@ -610,21 +607,28 @@ sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
                }
 
                if (is_var_sz && var_size > 1) {
-                       if (P2ROUNDUP(hdrsize + sizeof (uint16_t), 8) +
+                       /*
+                        * Don't worry that the spill block might overflow.
+                        * It will be resized if needed in sa_build_layouts().
+                        */
+                       if (buftype == SA_SPILL ||
+                           P2ROUNDUP(hdrsize + sizeof (uint16_t), 8) +
                            *total < full_space) {
                                /*
                                 * Account for header space used by array of
                                 * optional sizes of variable-length attributes.
-                                * Record the index in case this increase needs
-                                * to be reversed due to spill-over.
+                                * Record the extra header size in case this
+                                * increase needs to be reversed due to
+                                * spill-over.
                                 */
                                hdrsize += sizeof (uint16_t);
-                               j = i;
+                               if (*index != -1)
+                                       extra_hdrsize += sizeof (uint16_t);
                        } else {
-                               done = B_TRUE;
-                               *index = i;
-                               if (buftype == SA_BONUS)
-                                       *will_spill = B_TRUE;
+                               ASSERT(buftype == SA_BONUS);
+                               if (*index == -1)
+                                       *index = i;
+                               *will_spill = B_TRUE;
                                continue;
                        }
                }
@@ -639,22 +643,15 @@ sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
                    (*total + P2ROUNDUP(hdrsize, 8)) >
                    (full_space - sizeof (blkptr_t))) {
                        *index = i;
-                       done = B_TRUE;
                }
 
-next:
                if ((*total + P2ROUNDUP(hdrsize, 8)) > full_space &&
                    buftype == SA_BONUS)
                        *will_spill = B_TRUE;
        }
 
-       /*
-        * j holds the index of the last variable-sized attribute for
-        * which hdrsize was increased.  Reverse the increase if that
-        * attribute will be relocated to the spill block.
-        */
-       if (*will_spill && j == *index)
-               hdrsize -= sizeof (uint16_t);
+       if (*will_spill)
+               hdrsize -= extra_hdrsize;
 
        hdrsize = P2ROUNDUP(hdrsize, 8);
        return (hdrsize);
@@ -679,7 +676,8 @@ sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
        int buf_space;
        sa_attr_type_t *attrs, *attrs_start;
        int i, lot_count;
-       int hdrsize, spillhdrsize = 0;
+       int hdrsize;
+       int spillhdrsize = 0;
        int used;
        dmu_object_type_t bonustype;
        sa_lot_t *lot;
@@ -695,7 +693,7 @@ sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
            SA_BONUS, &i, &used, &spilling);
 
        if (used > SPA_MAXBLOCKSIZE)
-               return (EFBIG);
+               return (SET_ERROR(EFBIG));
 
        VERIFY(0 == dmu_set_bonus(hdl->sa_bonus, spilling ?
            MIN(DN_MAX_BONUSLEN - sizeof (blkptr_t), used + hdrsize) :
@@ -719,7 +717,7 @@ sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
                    &spill_used, &dummy);
 
                if (spill_used > SPA_MAXBLOCKSIZE)
-                       return (EFBIG);
+                       return (SET_ERROR(EFBIG));
 
                buf_space = hdl->sa_spill->db_size - spillhdrsize;
                if (BUF_SPACE_NEEDED(spill_used, spillhdrsize) >
@@ -876,7 +874,7 @@ sa_attr_table_setup(objset_t *os, sa_attr_reg_t *reg_attrs, int count)
                 */
                if (error || (error == 0 && sa_attr_count == 0)) {
                        if (error == 0)
-                               error = EINVAL;
+                               error = SET_ERROR(EINVAL);
                        goto bail;
                }
                sa_reg_count = sa_attr_count;
@@ -907,7 +905,7 @@ sa_attr_table_setup(objset_t *os, sa_attr_reg_t *reg_attrs, int count)
                        error = zap_lookup(os, sa->sa_reg_attr_obj,
                            reg_attrs[i].sa_name, 8, 1, &attr_value);
                else
-                       error = ENOENT;
+                       error = SET_ERROR(ENOENT);
                switch (error) {
                case ENOENT:
                        sa->sa_user_table[i] = (sa_attr_type_t)sa_attr_count;
@@ -1019,10 +1017,10 @@ sa_setup(objset_t *os, uint64_t sa_obj, sa_attr_reg_t *reg_attrs, int count,
        sa_attr_type_t *tb;
        int error;
 
-       mutex_enter(&os->os_lock);
+       mutex_enter(&os->os_user_ptr_lock);
        if (os->os_sa) {
                mutex_enter(&os->os_sa->sa_lock);
-               mutex_exit(&os->os_lock);
+               mutex_exit(&os->os_user_ptr_lock);
                tb = os->os_sa->sa_user_table;
                mutex_exit(&os->os_sa->sa_lock);
                *user_table = tb;
@@ -1035,7 +1033,7 @@ sa_setup(objset_t *os, uint64_t sa_obj, sa_attr_reg_t *reg_attrs, int count,
 
        os->os_sa = sa;
        mutex_enter(&sa->sa_lock);
-       mutex_exit(&os->os_lock);
+       mutex_exit(&os->os_user_ptr_lock);
        avl_create(&sa->sa_layout_num_tree, layout_num_compare,
            sizeof (sa_lot_t), offsetof(sa_lot_t, lot_num_node));
        avl_create(&sa->sa_layout_hash_tree, layout_hash_compare,
@@ -1066,7 +1064,7 @@ sa_setup(objset_t *os, uint64_t sa_obj, sa_attr_reg_t *reg_attrs, int count,
                 */
                if (error || (error == 0 && layout_count == 0)) {
                        if (error == 0)
-                               error = EINVAL;
+                               error = SET_ERROR(EINVAL);
                        goto fail;
                }
 
@@ -1145,7 +1143,8 @@ sa_tear_down(objset_t *os)
        sa_free_attr_table(sa);
 
        cookie = NULL;
-       while ((layout = avl_destroy_nodes(&sa->sa_layout_hash_tree, &cookie))){
+       while ((layout =
+           avl_destroy_nodes(&sa->sa_layout_hash_tree, &cookie))) {
                sa_idx_tab_t *tab;
                while ((tab = list_head(&layout->lot_idx_tab))) {
                        ASSERT(refcount_count(&tab->sa_refcount));
@@ -1154,7 +1153,7 @@ sa_tear_down(objset_t *os)
        }
 
        cookie = NULL;
-       while ((layout = avl_destroy_nodes(&sa->sa_layout_num_tree, &cookie))){
+       while ((layout = avl_destroy_nodes(&sa->sa_layout_num_tree, &cookie))) {
                kmem_free(layout->lot_attrs,
                    sizeof (sa_attr_type_t) * layout->lot_attr_count);
                kmem_free(layout, sizeof (sa_lot_t));