1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Module that interacts with the ZFS DMU and provides an abstraction
6 * to the rest of Lustre.
8 * Copyright (c) 2007 Cluster File Systems, Inc.
9 * Author: Manoj Joseph <manoj.joseph@sun.com>
11 * This file is part of the Lustre file system, http://www.lustre.org
12 * Lustre is a trademark of Cluster File Systems, Inc.
14 * You may have signed or agreed to another license before downloading
15 * this software. If so, you are bound by the terms and conditions
16 * of that agreement, and the following does not apply to you. See the
17 * LICENSE file included with this distribution for more information.
19 * If you did not agree to a different license, then this copy of Lustre
20 * is open source software; you can redistribute it and/or modify it
21 * under the terms of version 2 of the GNU General Public License as
22 * published by the Free Software Foundation.
24 * In either case, Lustre is distributed in the hope that it will be
25 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * license text for more details.
33 #include <sys/debug.h>
35 #include <sys/statvfs.h>
36 #include <sys/errno.h>
39 #include <udmu_util.h>
41 static int udmu_util_object_delete(udmu_objset_t *uos, dmu_buf_t **dbp,
48 id = udmu_object_get_id(*dbp);
49 tx = udmu_tx_create(uos);
51 udmu_tx_hold_free(tx, id, 0, DMU_OBJECT_END);
53 rc = udmu_tx_assign(tx, TXG_WAIT);
56 "udmu_util_object_delete: udmu_tx_assign failed (%d)", rc);
61 rc = udmu_object_delete(uos, dbp, tx, tag);
63 fprintf(stderr, "udmu_object_delete() failed (%d)", rc);
69 int udmu_util_mkdir(udmu_objset_t *uos, dmu_buf_t *parent_db,
70 const char *name, dmu_buf_t **new_dbp, void *tag)
74 uint64_t id, pid, value;
77 /* return EEXIST early to avoid object creation/deletion */
78 rc = udmu_zap_lookup(uos, parent_db, name, &id,
79 sizeof(id), sizeof(uint64_t));
83 pid = udmu_object_get_id(parent_db);
85 tx = udmu_tx_create(uos);
86 udmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL); /* for zap create */
87 udmu_tx_hold_bonus(tx, pid); /* for zap_add */
88 udmu_tx_hold_zap(tx, pid, 1, (char *)name); /* for zap_add */
90 rc = udmu_tx_assign(tx, TXG_WAIT);
93 "udmu_util_mkdir: udmu_tx_assign failed (%d)", rc);
98 udmu_zap_create(uos, &db, tx, tag);
99 id = udmu_object_get_id(db);
100 value = ZFS_DIRENT_MAKE(0, id);
101 rc = udmu_zap_insert(uos, parent_db, tx, name, &value, sizeof(value));
105 fprintf(stderr, "can't insert (%s) in zap (%d)", name, rc);
106 /* error handling, delete just created object */
107 udmu_util_object_delete(uos, &db, tag);
108 } else if (new_dbp) {
111 udmu_object_put_dmu_buf(db, tag);
117 int udmu_util_setattr(udmu_objset_t *uos, dmu_buf_t *db, vnattr_t *va)
122 tx = udmu_tx_create(uos);
123 udmu_tx_hold_bonus(tx, udmu_object_get_id(db));
125 rc = udmu_tx_assign(tx, TXG_WAIT);
129 udmu_object_setattr(db, tx, va);
136 int udmu_util_create(udmu_objset_t *uos, dmu_buf_t *parent_db,
137 const char *name, dmu_buf_t **new_dbp, void *tag)
141 uint64_t id, pid, value;
144 /* return EEXIST early to avoid object creation/deletion */
145 rc = udmu_zap_lookup(uos, parent_db, name, &id,
146 sizeof(id), sizeof(uint64_t));
150 pid = udmu_object_get_id(parent_db);
152 tx = udmu_tx_create(uos);
154 udmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
155 udmu_tx_hold_bonus(tx, pid);
156 udmu_tx_hold_zap(tx, pid, 1, (char *) name);
158 rc = udmu_tx_assign(tx, TXG_WAIT);
161 "udmu_util_create: udmu_tx_assign failed (%d)", rc);
166 udmu_object_create(uos, &db, tx, tag);
167 id = udmu_object_get_id(db);
168 value = ZFS_DIRENT_MAKE(0, id);
169 rc = udmu_zap_insert(uos, parent_db, tx, name,
170 &value, sizeof(value));
174 fprintf(stderr, "can't insert new object in zap (%d)", rc);
175 /* error handling, delete just created object */
176 udmu_util_object_delete(uos, &db, tag);
177 } else if (new_dbp) {
180 udmu_object_put_dmu_buf(db, tag);
186 int udmu_util_lookup(udmu_objset_t *uos, dmu_buf_t *parent_db,
187 const char *name, dmu_buf_t **new_dbp, void *tag)
192 rc = udmu_zap_lookup(uos, parent_db, name, &id,
193 sizeof(id), sizeof(uint64_t));
195 udmu_object_get_dmu_buf(uos, id, new_dbp, tag);
201 int udmu_util_write(udmu_objset_t *uos, dmu_buf_t *db,
202 uint64_t offset, uint64_t len, void *buf)
206 uint64_t end = offset + len;
210 udmu_object_getattr(db, &va);
212 if (va.va_size < end) {
213 /* extending write; set file size */
215 va.va_mask = AT_SIZE;
219 tx = udmu_tx_create(uos);
221 udmu_tx_hold_bonus(tx, udmu_object_get_id(db));
223 udmu_tx_hold_write(tx, udmu_object_get_id(db), offset, len);
225 rc = udmu_tx_assign(tx, TXG_WAIT);
227 fprintf(stderr, "dmu_tx_assign() failed %d", rc);
232 udmu_object_write(uos, db, tx, offset,
235 udmu_object_setattr(db, tx, &va);