4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/cmn_err.h>
35 #include <sys/fcntl.h>
37 #include <sys/fs/zfs.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/zfs_dir.h>
40 #include <sys/zfs_acl.h>
43 #include <sys/byteorder.h>
46 #include <sys/atomic.h>
48 #include <sys/namei.h>
51 * Functions to replay ZFS intent log (ZIL) records
52 * The functions are called through a function vector (zfs_replay_vector)
53 * which is indexed by the transaction type.
57 zfs_init_vattr(vattr_t *vap, uint64_t mask, uint64_t mode,
58 uint64_t uid, uint64_t gid, uint64_t rdev, uint64_t nodeid)
61 vap->va_mask = (uint_t)mask;
62 vap->va_type = IFTOVT(mode);
63 vap->va_mode = mode & MODEMASK;
64 vap->va_uid = (uid_t)uid;
65 vap->va_gid = (gid_t)gid;
66 vap->va_rdev = zfs_cmpldev(rdev);
67 vap->va_nodeid = nodeid;
72 zfs_replay_error(zfsvfs_t *zfsvfs, lr_t *lr, boolean_t byteswap)
78 zfs_replay_create(zfsvfs_t *zfsvfs, lr_create_t *lr, boolean_t byteswap)
80 char *name = (char *)(lr + 1); /* name follows lr_create_t */
81 char *link; /* symlink content follows name */
85 struct componentname cn;
89 byteswap_uint64_array(lr, sizeof (*lr));
91 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
94 zfs_init_vattr(&va, AT_TYPE | AT_MODE | AT_UID | AT_GID,
95 lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
98 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
99 * eventually end up in zfs_mknode(), which assigns the object's
100 * creation time and generation number. The generic VOP_CREATE()
101 * doesn't have either concept, so we smuggle the values inside
102 * the vattr's otherwise unused va_ctime and va_nblocks fields.
104 ZFS_TIME_DECODE(&va.va_ctime, lr->lr_crtime);
105 va.va_nblocks = lr->lr_gen;
107 cn.cn_nameptr = name;
109 cn.cn_thread = curthread;
110 cn.cn_flags = SAVENAME;
112 vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
113 switch ((int)lr->lr_common.lrc_txtype) {
115 error = VOP_CREATE(ZTOV(dzp), &vp, &cn, &va);
118 error = VOP_MKDIR(ZTOV(dzp), &vp, &cn, &va);
121 error = zfs_make_xattrdir(dzp, &va, &vp, kcred);
124 link = name + strlen(name) + 1;
125 error = VOP_SYMLINK(ZTOV(dzp), &vp, &cn, &va, link);
130 VOP_UNLOCK(ZTOV(dzp), 0);
132 if (error == 0 && vp != NULL) {
143 zfs_replay_remove(zfsvfs_t *zfsvfs, lr_remove_t *lr, boolean_t byteswap)
145 char *name = (char *)(lr + 1); /* name follows lr_remove_t */
147 struct componentname cn;
152 byteswap_uint64_array(lr, sizeof (*lr));
154 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
157 bzero(&cn, sizeof(cn));
158 cn.cn_nameptr = name;
159 cn.cn_namelen = strlen(name);
160 cn.cn_nameiop = DELETE;
161 cn.cn_flags = ISLASTCN | SAVENAME;
162 cn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
164 cn.cn_thread = curthread;
165 vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
166 error = VOP_LOOKUP(ZTOV(dzp), &vp, &cn);
168 VOP_UNLOCK(ZTOV(dzp), 0);
172 switch ((int)lr->lr_common.lrc_txtype) {
174 error = VOP_REMOVE(ZTOV(dzp), vp, &cn);
177 error = VOP_RMDIR(ZTOV(dzp), vp, &cn);
183 VOP_UNLOCK(ZTOV(dzp), 0);
191 zfs_replay_link(zfsvfs_t *zfsvfs, lr_link_t *lr, boolean_t byteswap)
193 char *name = (char *)(lr + 1); /* name follows lr_link_t */
195 struct componentname cn;
199 byteswap_uint64_array(lr, sizeof (*lr));
201 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
204 if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
209 cn.cn_nameptr = name;
211 cn.cn_thread = curthread;
212 cn.cn_flags = SAVENAME;
214 vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
215 vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
216 error = VOP_LINK(ZTOV(dzp), ZTOV(zp), &cn);
217 VOP_UNLOCK(ZTOV(zp), 0);
218 VOP_UNLOCK(ZTOV(dzp), 0);
227 zfs_replay_rename(zfsvfs_t *zfsvfs, lr_rename_t *lr, boolean_t byteswap)
229 char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */
230 char *tname = sname + strlen(sname) + 1;
231 znode_t *sdzp, *tdzp;
232 struct componentname scn, tcn;
234 kthread_t *td = curthread;
238 byteswap_uint64_array(lr, sizeof (*lr));
240 if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
243 if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
250 bzero(&scn, sizeof(scn));
251 scn.cn_nameptr = sname;
252 scn.cn_namelen = strlen(sname);
253 scn.cn_nameiop = DELETE;
254 scn.cn_flags = ISLASTCN | SAVENAME;
255 scn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
258 vn_lock(ZTOV(sdzp), LK_EXCLUSIVE | LK_RETRY);
259 error = VOP_LOOKUP(ZTOV(sdzp), &svp, &scn);
260 VOP_UNLOCK(ZTOV(sdzp), 0);
265 bzero(&tcn, sizeof(tcn));
266 tcn.cn_nameptr = tname;
267 tcn.cn_namelen = strlen(tname);
268 tcn.cn_nameiop = RENAME;
269 tcn.cn_flags = ISLASTCN | SAVENAME;
270 tcn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
273 vn_lock(ZTOV(tdzp), LK_EXCLUSIVE | LK_RETRY);
274 error = VOP_LOOKUP(ZTOV(tdzp), &tvp, &tcn);
275 if (error == EJUSTRETURN)
277 else if (error != 0) {
278 VOP_UNLOCK(ZTOV(tdzp), 0);
282 error = VOP_RENAME(ZTOV(sdzp), svp, &scn, ZTOV(tdzp), tvp, &tcn);
296 zfs_replay_write(zfsvfs_t *zfsvfs, lr_write_t *lr, boolean_t byteswap)
298 char *data = (char *)(lr + 1); /* data follows lr_write_t */
304 byteswap_uint64_array(lr, sizeof (*lr));
306 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
308 * As we can log writes out of order, it's possible the
309 * file has been removed. In this case just drop the write
310 * and return success.
317 error = vn_rdwr(UIO_WRITE, ZTOV(zp), data, lr->lr_length,
318 lr->lr_offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
326 zfs_replay_truncate(zfsvfs_t *zfsvfs, lr_truncate_t *lr, boolean_t byteswap)
329 ZFS_LOG(0, "Unexpected code path, report to pjd@FreeBSD.org");
334 zfs_replay_setattr(zfsvfs_t *zfsvfs, lr_setattr_t *lr, boolean_t byteswap)
342 byteswap_uint64_array(lr, sizeof (*lr));
344 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
346 * As we can log setattrs out of order, it's possible the
347 * file has been removed. In this case just drop the setattr
348 * and return success.
355 zfs_init_vattr(&va, lr->lr_mask, lr->lr_mode,
356 lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
358 va.va_size = lr->lr_size;
359 ZFS_TIME_DECODE(&va.va_atime, lr->lr_atime);
360 ZFS_TIME_DECODE(&va.va_mtime, lr->lr_mtime);
363 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
364 error = VOP_SETATTR(vp, &va, kcred);
372 zfs_replay_acl(zfsvfs_t *zfsvfs, lr_acl_t *lr, boolean_t byteswap)
374 ace_t *ace = (ace_t *)(lr + 1); /* ace array follows lr_acl_t */
382 byteswap_uint64_array(lr, sizeof (*lr));
383 zfs_ace_byteswap(ace, lr->lr_aclcnt);
386 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
388 * As we can log acls out of order, it's possible the
389 * file has been removed. In this case just drop the acl
390 * and return success.
398 bzero(&vsa, sizeof (vsa));
399 vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
400 vsa.vsa_aclcnt = lr->lr_aclcnt;
401 vsa.vsa_aclentp = ace;
403 error = VOP_SETSECATTR(ZTOV(zp), &vsa, 0, kcred);
414 * Callback vectors for replaying records
416 zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
417 zfs_replay_error, /* 0 no such transaction type */
418 zfs_replay_create, /* TX_CREATE */
419 zfs_replay_create, /* TX_MKDIR */
420 zfs_replay_create, /* TX_MKXATTR */
421 zfs_replay_create, /* TX_SYMLINK */
422 zfs_replay_remove, /* TX_REMOVE */
423 zfs_replay_remove, /* TX_RMDIR */
424 zfs_replay_link, /* TX_LINK */
425 zfs_replay_rename, /* TX_RENAME */
426 zfs_replay_write, /* TX_WRITE */
427 zfs_replay_truncate, /* TX_TRUNCATE */
428 zfs_replay_setattr, /* TX_SETATTR */
429 zfs_replay_acl, /* TX_ACL */