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