FreeBSD ZFS
The Zettabyte File System
|
00001 /* 00002 * CDDL HEADER START 00003 * 00004 * The contents of this file are subject to the terms of the 00005 * Common Development and Distribution License (the "License"). 00006 * You may not use this file except in compliance with the License. 00007 * 00008 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 00009 * or http://www.opensolaris.org/os/licensing. 00010 * See the License for the specific language governing permissions 00011 * and limitations under the License. 00012 * 00013 * When distributing Covered Code, include this CDDL HEADER in each 00014 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 00015 * If applicable, add the following below this CDDL HEADER, with the 00016 * fields enclosed by brackets "[]" replaced with your own identifying 00017 * information: Portions Copyright [yyyy] [name of copyright owner] 00018 * 00019 * CDDL HEADER END 00020 */ 00021 00022 /* 00023 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 00024 */ 00025 00026 #include <sys/zfs_context.h> 00027 #include <sys/spa.h> 00028 #include <sys/zio.h> 00029 #include <sys/ddt.h> 00030 #include <sys/zap.h> 00031 #include <sys/dmu_tx.h> 00032 00033 int ddt_zap_leaf_blockshift = 12; 00034 int ddt_zap_indirect_blockshift = 12; 00035 00036 static int 00037 ddt_zap_create(objset_t *os, uint64_t *objectp, dmu_tx_t *tx, boolean_t prehash) 00038 { 00039 zap_flags_t flags = ZAP_FLAG_HASH64 | ZAP_FLAG_UINT64_KEY; 00040 00041 if (prehash) 00042 flags |= ZAP_FLAG_PRE_HASHED_KEY; 00043 00044 *objectp = zap_create_flags(os, 0, flags, DMU_OT_DDT_ZAP, 00045 ddt_zap_leaf_blockshift, ddt_zap_indirect_blockshift, 00046 DMU_OT_NONE, 0, tx); 00047 00048 return (*objectp == 0 ? ENOTSUP : 0); 00049 } 00050 00051 static int 00052 ddt_zap_destroy(objset_t *os, uint64_t object, dmu_tx_t *tx) 00053 { 00054 return (zap_destroy(os, object, tx)); 00055 } 00056 00057 static int 00058 ddt_zap_lookup(objset_t *os, uint64_t object, ddt_entry_t *dde) 00059 { 00060 uchar_t cbuf[sizeof (dde->dde_phys) + 1]; 00061 uint64_t one, csize; 00062 int error; 00063 00064 error = zap_length_uint64(os, object, (uint64_t *)&dde->dde_key, 00065 DDT_KEY_WORDS, &one, &csize); 00066 if (error) 00067 return (error); 00068 00069 ASSERT(one == 1); 00070 ASSERT(csize <= sizeof (cbuf)); 00071 00072 error = zap_lookup_uint64(os, object, (uint64_t *)&dde->dde_key, 00073 DDT_KEY_WORDS, 1, csize, cbuf); 00074 if (error) 00075 return (error); 00076 00077 ddt_decompress(cbuf, dde->dde_phys, csize, sizeof (dde->dde_phys)); 00078 00079 return (0); 00080 } 00081 00082 static void 00083 ddt_zap_prefetch(objset_t *os, uint64_t object, ddt_entry_t *dde) 00084 { 00085 (void) zap_prefetch_uint64(os, object, (uint64_t *)&dde->dde_key, 00086 DDT_KEY_WORDS); 00087 } 00088 00089 static int 00090 ddt_zap_update(objset_t *os, uint64_t object, ddt_entry_t *dde, dmu_tx_t *tx) 00091 { 00092 uchar_t cbuf[sizeof (dde->dde_phys) + 1]; 00093 uint64_t csize; 00094 00095 csize = ddt_compress(dde->dde_phys, cbuf, 00096 sizeof (dde->dde_phys), sizeof (cbuf)); 00097 00098 return (zap_update_uint64(os, object, (uint64_t *)&dde->dde_key, 00099 DDT_KEY_WORDS, 1, csize, cbuf, tx)); 00100 } 00101 00102 static int 00103 ddt_zap_remove(objset_t *os, uint64_t object, ddt_entry_t *dde, dmu_tx_t *tx) 00104 { 00105 return (zap_remove_uint64(os, object, (uint64_t *)&dde->dde_key, 00106 DDT_KEY_WORDS, tx)); 00107 } 00108 00109 static int 00110 ddt_zap_walk(objset_t *os, uint64_t object, ddt_entry_t *dde, uint64_t *walk) 00111 { 00112 zap_cursor_t zc; 00113 zap_attribute_t za; 00114 int error; 00115 00116 zap_cursor_init_serialized(&zc, os, object, *walk); 00117 if ((error = zap_cursor_retrieve(&zc, &za)) == 0) { 00118 uchar_t cbuf[sizeof (dde->dde_phys) + 1]; 00119 uint64_t csize = za.za_num_integers; 00120 ASSERT(za.za_integer_length == 1); 00121 error = zap_lookup_uint64(os, object, (uint64_t *)za.za_name, 00122 DDT_KEY_WORDS, 1, csize, cbuf); 00123 ASSERT(error == 0); 00124 if (error == 0) { 00125 ddt_decompress(cbuf, dde->dde_phys, csize, 00126 sizeof (dde->dde_phys)); 00127 dde->dde_key = *(ddt_key_t *)za.za_name; 00128 } 00129 zap_cursor_advance(&zc); 00130 *walk = zap_cursor_serialize(&zc); 00131 } 00132 zap_cursor_fini(&zc); 00133 return (error); 00134 } 00135 00136 static uint64_t 00137 ddt_zap_count(objset_t *os, uint64_t object) 00138 { 00139 uint64_t count = 0; 00140 00141 VERIFY(zap_count(os, object, &count) == 0); 00142 00143 return (count); 00144 } 00145 00146 const ddt_ops_t ddt_zap_ops = { 00147 "zap", 00148 ddt_zap_create, 00149 ddt_zap_destroy, 00150 ddt_zap_lookup, 00151 ddt_zap_prefetch, 00152 ddt_zap_update, 00153 ddt_zap_remove, 00154 ddt_zap_walk, 00155 ddt_zap_count, 00156 };