xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_replay.c (revision ebd1706e95186ddae1d4c0d63c47544cf33832ee)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/cmn_err.h>
34 #include <sys/kmem.h>
35 #include <sys/thread.h>
36 #include <sys/file.h>
37 #include <sys/fcntl.h>
38 #include <sys/vfs.h>
39 #include <sys/fs/zfs.h>
40 #include <sys/zfs_znode.h>
41 #include <sys/zfs_dir.h>
42 #include <sys/zfs_acl.h>
43 #include <sys/spa.h>
44 #include <sys/zil.h>
45 #include <sys/byteorder.h>
46 #include <sys/stat.h>
47 #include <sys/mode.h>
48 #include <sys/acl.h>
49 #include <sys/atomic.h>
50 #include <sys/cred.h>
51 
52 /*
53  * Functions to replay ZFS intent log (ZIL) records
54  * The functions are called through a function vector (zfs_replay_vector)
55  * which is indexed by the transaction type.
56  */
57 
58 static void
59 zfs_init_vattr(vattr_t *vap, uint64_t mask, uint64_t mode,
60 	uint64_t uid, uint64_t gid, uint64_t rdev, uint64_t nodeid)
61 {
62 	bzero(vap, sizeof (*vap));
63 	vap->va_mask = (uint_t)mask;
64 	vap->va_type = IFTOVT(mode);
65 	vap->va_mode = mode & MODEMASK;
66 	vap->va_uid = (uid_t)uid;
67 	vap->va_gid = (gid_t)gid;
68 	vap->va_rdev = (dev_t)rdev;
69 	vap->va_nodeid = nodeid;
70 }
71 
72 /* ARGSUSED */
73 static int
74 zfs_replay_error(zfsvfs_t *zfsvfs, lr_t *lr, boolean_t byteswap)
75 {
76 	return (ENOTSUP);
77 }
78 
79 static int
80 zfs_replay_create(zfsvfs_t *zfsvfs, lr_create_t *lr, boolean_t byteswap)
81 {
82 	char *name = (char *)(lr + 1);	/* name follows lr_create_t */
83 	char *link;			/* symlink content follows name */
84 	znode_t *dzp;
85 	vnode_t *vp = NULL;
86 	vattr_t va;
87 	int error;
88 
89 	if (byteswap)
90 		byteswap_uint64_array(lr, sizeof (*lr));
91 
92 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
93 		return (error);
94 
95 	zfs_init_vattr(&va, AT_TYPE | AT_MODE | AT_UID | AT_GID,
96 	    lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
97 
98 	/*
99 	 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
100 	 * eventually end up in zfs_mknode(), which assigns the object's
101 	 * creation time and generation number.  The generic VOP_CREATE()
102 	 * doesn't have either concept, so we smuggle the values inside
103 	 * the vattr's otherwise unused va_ctime and va_nblocks fields.
104 	 */
105 	ZFS_TIME_DECODE(&va.va_ctime, lr->lr_crtime);
106 	va.va_nblocks = lr->lr_gen;
107 
108 	switch ((int)lr->lr_common.lrc_txtype) {
109 	case TX_CREATE:
110 		error = VOP_CREATE(ZTOV(dzp), name, &va, 0, 0, &vp, kcred, 0);
111 		break;
112 	case TX_MKDIR:
113 		error = VOP_MKDIR(ZTOV(dzp), name, &va, &vp, kcred);
114 		break;
115 	case TX_MKXATTR:
116 		error = zfs_make_xattrdir(dzp, &va, &vp, kcred);
117 		break;
118 	case TX_SYMLINK:
119 		link = name + strlen(name) + 1;
120 		error = VOP_SYMLINK(ZTOV(dzp), name, &va, link, kcred);
121 		break;
122 	default:
123 		error = ENOTSUP;
124 	}
125 
126 	if (error == 0 && vp != NULL)
127 		VN_RELE(vp);
128 
129 	VN_RELE(ZTOV(dzp));
130 
131 	return (error);
132 }
133 
134 static int
135 zfs_replay_remove(zfsvfs_t *zfsvfs, lr_remove_t *lr, boolean_t byteswap)
136 {
137 	char *name = (char *)(lr + 1);	/* name follows lr_remove_t */
138 	znode_t *dzp;
139 	int error;
140 
141 	if (byteswap)
142 		byteswap_uint64_array(lr, sizeof (*lr));
143 
144 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
145 		return (error);
146 
147 	switch ((int)lr->lr_common.lrc_txtype) {
148 	case TX_REMOVE:
149 		error = VOP_REMOVE(ZTOV(dzp), name, kcred);
150 		break;
151 	case TX_RMDIR:
152 		error = VOP_RMDIR(ZTOV(dzp), name, NULL, kcred);
153 		break;
154 	default:
155 		error = ENOTSUP;
156 	}
157 
158 	VN_RELE(ZTOV(dzp));
159 
160 	return (error);
161 }
162 
163 static int
164 zfs_replay_link(zfsvfs_t *zfsvfs, lr_link_t *lr, boolean_t byteswap)
165 {
166 	char *name = (char *)(lr + 1);	/* name follows lr_link_t */
167 	znode_t *dzp, *zp;
168 	int error;
169 
170 	if (byteswap)
171 		byteswap_uint64_array(lr, sizeof (*lr));
172 
173 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
174 		return (error);
175 
176 	if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
177 		VN_RELE(ZTOV(dzp));
178 		return (error);
179 	}
180 
181 	error = VOP_LINK(ZTOV(dzp), ZTOV(zp), name, kcred);
182 
183 	VN_RELE(ZTOV(zp));
184 	VN_RELE(ZTOV(dzp));
185 
186 	return (error);
187 }
188 
189 static int
190 zfs_replay_rename(zfsvfs_t *zfsvfs, lr_rename_t *lr, boolean_t byteswap)
191 {
192 	char *sname = (char *)(lr + 1);	/* sname and tname follow lr_rename_t */
193 	char *tname = sname + strlen(sname) + 1;
194 	znode_t *sdzp, *tdzp;
195 	int error;
196 
197 	if (byteswap)
198 		byteswap_uint64_array(lr, sizeof (*lr));
199 
200 	if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
201 		return (error);
202 
203 	if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
204 		VN_RELE(ZTOV(sdzp));
205 		return (error);
206 	}
207 
208 	error = VOP_RENAME(ZTOV(sdzp), sname, ZTOV(tdzp), tname, kcred);
209 
210 	VN_RELE(ZTOV(tdzp));
211 	VN_RELE(ZTOV(sdzp));
212 
213 	return (error);
214 }
215 
216 static int
217 zfs_replay_write(zfsvfs_t *zfsvfs, lr_write_t *lr, boolean_t byteswap)
218 {
219 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
220 	znode_t	*zp;
221 	int error;
222 	ssize_t resid;
223 
224 	if (byteswap)
225 		byteswap_uint64_array(lr, sizeof (*lr));
226 
227 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
228 		return (error);
229 
230 	error = vn_rdwr(UIO_WRITE, ZTOV(zp), data, lr->lr_length,
231 	    lr->lr_offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
232 
233 	VN_RELE(ZTOV(zp));
234 
235 	return (error);
236 }
237 
238 static int
239 zfs_replay_truncate(zfsvfs_t *zfsvfs, lr_truncate_t *lr, boolean_t byteswap)
240 {
241 	znode_t *zp;
242 	flock64_t fl;
243 	int error;
244 
245 	if (byteswap)
246 		byteswap_uint64_array(lr, sizeof (*lr));
247 
248 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
249 		return (error);
250 
251 	bzero(&fl, sizeof (fl));
252 	fl.l_type = F_WRLCK;
253 	fl.l_whence = 0;
254 	fl.l_start = lr->lr_offset;
255 	fl.l_len = lr->lr_length;
256 
257 	error = VOP_SPACE(ZTOV(zp), F_FREESP, &fl, FWRITE | FOFFMAX,
258 	    lr->lr_offset, kcred, NULL);
259 
260 	VN_RELE(ZTOV(zp));
261 
262 	return (error);
263 }
264 
265 static int
266 zfs_replay_setattr(zfsvfs_t *zfsvfs, lr_setattr_t *lr, boolean_t byteswap)
267 {
268 	znode_t *zp;
269 	vattr_t va;
270 	int error;
271 
272 	if (byteswap)
273 		byteswap_uint64_array(lr, sizeof (*lr));
274 
275 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
276 		return (error);
277 
278 	zfs_init_vattr(&va, lr->lr_mask, lr->lr_mode,
279 	    lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
280 
281 	va.va_size = lr->lr_size;
282 	ZFS_TIME_DECODE(&va.va_atime, lr->lr_atime);
283 	ZFS_TIME_DECODE(&va.va_mtime, lr->lr_mtime);
284 
285 	error = VOP_SETATTR(ZTOV(zp), &va, 0, kcred, NULL);
286 
287 	VN_RELE(ZTOV(zp));
288 
289 	return (error);
290 }
291 
292 static int
293 zfs_replay_acl(zfsvfs_t *zfsvfs, lr_acl_t *lr, boolean_t byteswap)
294 {
295 	ace_t *ace = (ace_t *)(lr + 1);	/* ace array follows lr_acl_t */
296 	vsecattr_t vsa;
297 	znode_t *zp;
298 	int error;
299 
300 	if (byteswap) {
301 		byteswap_uint64_array(lr, sizeof (*lr));
302 		zfs_ace_byteswap(ace, lr->lr_aclcnt);
303 	}
304 
305 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
306 		return (error);
307 
308 	bzero(&vsa, sizeof (vsa));
309 	vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
310 	vsa.vsa_aclcnt = lr->lr_aclcnt;
311 	vsa.vsa_aclentp = ace;
312 
313 	error = VOP_SETSECATTR(ZTOV(zp), &vsa, 0, kcred);
314 
315 	VN_RELE(ZTOV(zp));
316 
317 	return (error);
318 }
319 
320 /*
321  * Callback vectors for replaying records
322  */
323 zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
324 	zfs_replay_error,	/* 0 no such transaction type */
325 	zfs_replay_create,	/* TX_CREATE */
326 	zfs_replay_create,	/* TX_MKDIR */
327 	zfs_replay_create,	/* TX_MKXATTR */
328 	zfs_replay_create,	/* TX_SYMLINK */
329 	zfs_replay_remove,	/* TX_REMOVE */
330 	zfs_replay_remove,	/* TX_RMDIR */
331 	zfs_replay_link,	/* TX_LINK */
332 	zfs_replay_rename,	/* TX_RENAME */
333 	zfs_replay_write,	/* TX_WRITE */
334 	zfs_replay_truncate,	/* TX_TRUNCATE */
335 	zfs_replay_setattr,	/* TX_SETATTR */
336 	zfs_replay_acl,		/* TX_ACL */
337 };
338