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;
63 vap->va_type = IFTOVT(mode);
65 vap->va_mode = mode & MODEMASK;
67 vap->va_uid = (uid_t)uid;
69 vap->va_gid = (gid_t)gid;
70 vap->va_rdev = zfs_cmpldev(rdev);
71 vap->va_nodeid = nodeid;
76 zfs_replay_error(zfsvfs_t *zfsvfs, lr_t *lr, boolean_t byteswap)
82 zfs_replay_create(zfsvfs_t *zfsvfs, lr_create_t *lr, boolean_t byteswap)
84 char *name = (char *)(lr + 1); /* name follows lr_create_t */
85 char *link; /* symlink content follows name */
89 struct componentname cn;
93 byteswap_uint64_array(lr, sizeof (*lr));
95 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
98 zfs_init_vattr(&va, AT_TYPE | AT_MODE | AT_UID | AT_GID,
99 lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
102 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
103 * eventually end up in zfs_mknode(), which assigns the object's
104 * creation time and generation number. The generic VOP_CREATE()
105 * doesn't have either concept, so we smuggle the values inside
106 * the vattr's otherwise unused va_ctime and va_nblocks fields.
108 ZFS_TIME_DECODE(&va.va_ctime, lr->lr_crtime);
109 va.va_nblocks = lr->lr_gen;
111 cn.cn_nameptr = name;
113 cn.cn_thread = curthread;
114 cn.cn_flags = SAVENAME;
116 vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY, curthread);
117 switch ((int)lr->lr_common.lrc_txtype) {
119 error = VOP_CREATE(ZTOV(dzp), &vp, &cn, &va);
122 error = VOP_MKDIR(ZTOV(dzp), &vp, &cn, &va);
125 error = zfs_make_xattrdir(dzp, &va, &vp, kcred);
128 link = name + strlen(name) + 1;
129 error = VOP_SYMLINK(ZTOV(dzp), &vp, &cn, &va, link);
134 VOP_UNLOCK(ZTOV(dzp), 0, curthread);
136 if (error == 0 && vp != NULL) {
137 VOP_UNLOCK(vp, 0, curthread);
147 zfs_replay_remove(zfsvfs_t *zfsvfs, lr_remove_t *lr, boolean_t byteswap)
149 char *name = (char *)(lr + 1); /* name follows lr_remove_t */
151 struct componentname cn;
156 byteswap_uint64_array(lr, sizeof (*lr));
158 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
161 bzero(&cn, sizeof(cn));
162 cn.cn_nameptr = name;
163 cn.cn_namelen = strlen(name);
164 cn.cn_nameiop = DELETE;
165 cn.cn_flags = ISLASTCN | SAVENAME;
166 cn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
168 cn.cn_thread = curthread;
169 vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY, curthread);
170 error = VOP_LOOKUP(ZTOV(dzp), &vp, &cn);
172 VOP_UNLOCK(ZTOV(dzp), 0, curthread);
176 switch ((int)lr->lr_common.lrc_txtype) {
178 error = VOP_REMOVE(ZTOV(dzp), vp, &cn);
181 error = VOP_RMDIR(ZTOV(dzp), vp, &cn);
187 VOP_UNLOCK(ZTOV(dzp), 0, curthread);
195 zfs_replay_link(zfsvfs_t *zfsvfs, lr_link_t *lr, boolean_t byteswap)
197 char *name = (char *)(lr + 1); /* name follows lr_link_t */
199 struct componentname cn;
203 byteswap_uint64_array(lr, sizeof (*lr));
205 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
208 if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
213 cn.cn_nameptr = name;
215 cn.cn_thread = curthread;
216 cn.cn_flags = SAVENAME;
218 vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY, curthread);
219 vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY, curthread);
220 error = VOP_LINK(ZTOV(dzp), ZTOV(zp), &cn);
221 VOP_UNLOCK(ZTOV(zp), 0, curthread);
222 VOP_UNLOCK(ZTOV(dzp), 0, curthread);
231 zfs_replay_rename(zfsvfs_t *zfsvfs, lr_rename_t *lr, boolean_t byteswap)
233 char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */
234 char *tname = sname + strlen(sname) + 1;
235 znode_t *sdzp, *tdzp;
236 struct componentname scn, tcn;
238 kthread_t *td = curthread;
242 byteswap_uint64_array(lr, sizeof (*lr));
244 if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
247 if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
254 bzero(&scn, sizeof(scn));
255 scn.cn_nameptr = sname;
256 scn.cn_namelen = strlen(sname);
257 scn.cn_nameiop = DELETE;
258 scn.cn_flags = ISLASTCN | SAVENAME;
259 scn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
262 vn_lock(ZTOV(sdzp), LK_EXCLUSIVE | LK_RETRY, td);
263 error = VOP_LOOKUP(ZTOV(sdzp), &svp, &scn);
264 VOP_UNLOCK(ZTOV(sdzp), 0, td);
267 VOP_UNLOCK(svp, 0, td);
269 bzero(&tcn, sizeof(tcn));
270 tcn.cn_nameptr = tname;
271 tcn.cn_namelen = strlen(tname);
272 tcn.cn_nameiop = RENAME;
273 tcn.cn_flags = ISLASTCN | SAVENAME;
274 tcn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
277 vn_lock(ZTOV(tdzp), LK_EXCLUSIVE | LK_RETRY, td);
278 error = VOP_LOOKUP(ZTOV(tdzp), &tvp, &tcn);
279 if (error == EJUSTRETURN)
281 else if (error != 0) {
282 VOP_UNLOCK(ZTOV(tdzp), 0, td);
286 error = VOP_RENAME(ZTOV(sdzp), svp, &scn, ZTOV(tdzp), tvp, &tcn);
300 zfs_replay_write(zfsvfs_t *zfsvfs, lr_write_t *lr, boolean_t byteswap)
302 char *data = (char *)(lr + 1); /* data follows lr_write_t */
308 byteswap_uint64_array(lr, sizeof (*lr));
310 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
312 * As we can log writes out of order, it's possible the
313 * file has been removed. In this case just drop the write
314 * and return success.
321 error = vn_rdwr(UIO_WRITE, ZTOV(zp), data, lr->lr_length,
322 lr->lr_offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
330 zfs_replay_truncate(zfsvfs_t *zfsvfs, lr_truncate_t *lr, boolean_t byteswap)
333 ZFS_LOG(0, "Unexpected code path, report to pjd@FreeBSD.org");
338 zfs_replay_setattr(zfsvfs_t *zfsvfs, lr_setattr_t *lr, boolean_t byteswap)
346 byteswap_uint64_array(lr, sizeof (*lr));
348 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
350 * As we can log setattrs out of order, it's possible the
351 * file has been removed. In this case just drop the setattr
352 * and return success.
359 zfs_init_vattr(&va, lr->lr_mask, lr->lr_mode,
360 lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
362 va.va_size = lr->lr_size;
363 ZFS_TIME_DECODE(&va.va_atime, lr->lr_atime);
364 ZFS_TIME_DECODE(&va.va_mtime, lr->lr_mtime);
367 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
368 error = VOP_SETATTR(vp, &va, kcred, curthread);
369 VOP_UNLOCK(vp, 0, curthread);
376 zfs_replay_acl(zfsvfs_t *zfsvfs, lr_acl_t *lr, boolean_t byteswap)
378 ace_t *ace = (ace_t *)(lr + 1); /* ace array follows lr_acl_t */
386 byteswap_uint64_array(lr, sizeof (*lr));
387 zfs_ace_byteswap(ace, lr->lr_aclcnt);
390 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
392 * As we can log acls out of order, it's possible the
393 * file has been removed. In this case just drop the acl
394 * and return success.
402 bzero(&vsa, sizeof (vsa));
403 vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
404 vsa.vsa_aclcnt = lr->lr_aclcnt;
405 vsa.vsa_aclentp = ace;
407 error = VOP_SETSECATTR(ZTOV(zp), &vsa, 0, kcred);
418 * Callback vectors for replaying records
420 zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
421 zfs_replay_error, /* 0 no such transaction type */
422 zfs_replay_create, /* TX_CREATE */
423 zfs_replay_create, /* TX_MKDIR */
424 zfs_replay_create, /* TX_MKXATTR */
425 zfs_replay_create, /* TX_SYMLINK */
426 zfs_replay_remove, /* TX_REMOVE */
427 zfs_replay_remove, /* TX_RMDIR */
428 zfs_replay_link, /* TX_LINK */
429 zfs_replay_rename, /* TX_RENAME */
430 zfs_replay_write, /* TX_WRITE */
431 zfs_replay_truncate, /* TX_TRUNCATE */
432 zfs_replay_setattr, /* TX_SETATTR */
433 zfs_replay_acl, /* TX_ACL */