xref: /titanic_41/usr/src/uts/common/fs/zfs/zfs_replay.c (revision 6185db853e024a486ff8837e6784dd290d866112)
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 		/*
228 		 * As we can log writes out of order, it's possible the
229 		 * file has been removed. In this case just drop the write
230 		 * and return success.
231 		 */
232 		if (error == ENOENT)
233 			error = 0;
234 		return (error);
235 	}
236 
237 	error = vn_rdwr(UIO_WRITE, ZTOV(zp), data, lr->lr_length,
238 	    lr->lr_offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
239 
240 	VN_RELE(ZTOV(zp));
241 
242 	return (error);
243 }
244 
245 static int
246 zfs_replay_truncate(zfsvfs_t *zfsvfs, lr_truncate_t *lr, boolean_t byteswap)
247 {
248 	znode_t *zp;
249 	flock64_t fl;
250 	int error;
251 
252 	if (byteswap)
253 		byteswap_uint64_array(lr, sizeof (*lr));
254 
255 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
256 		/*
257 		 * As we can log truncates out of order, it's possible the
258 		 * file has been removed. In this case just drop the truncate
259 		 * and return success.
260 		 */
261 		if (error == ENOENT)
262 			error = 0;
263 		return (error);
264 	}
265 
266 	bzero(&fl, sizeof (fl));
267 	fl.l_type = F_WRLCK;
268 	fl.l_whence = 0;
269 	fl.l_start = lr->lr_offset;
270 	fl.l_len = lr->lr_length;
271 
272 	error = VOP_SPACE(ZTOV(zp), F_FREESP, &fl, FWRITE | FOFFMAX,
273 	    lr->lr_offset, kcred, NULL);
274 
275 	VN_RELE(ZTOV(zp));
276 
277 	return (error);
278 }
279 
280 static int
281 zfs_replay_setattr(zfsvfs_t *zfsvfs, lr_setattr_t *lr, boolean_t byteswap)
282 {
283 	znode_t *zp;
284 	vattr_t va;
285 	int error;
286 
287 	if (byteswap)
288 		byteswap_uint64_array(lr, sizeof (*lr));
289 
290 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
291 		/*
292 		 * As we can log setattrs out of order, it's possible the
293 		 * file has been removed. In this case just drop the setattr
294 		 * and return success.
295 		 */
296 		if (error == ENOENT)
297 			error = 0;
298 		return (error);
299 	}
300 
301 	zfs_init_vattr(&va, lr->lr_mask, lr->lr_mode,
302 	    lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
303 
304 	va.va_size = lr->lr_size;
305 	ZFS_TIME_DECODE(&va.va_atime, lr->lr_atime);
306 	ZFS_TIME_DECODE(&va.va_mtime, lr->lr_mtime);
307 
308 	error = VOP_SETATTR(ZTOV(zp), &va, 0, kcred, NULL);
309 
310 	VN_RELE(ZTOV(zp));
311 
312 	return (error);
313 }
314 
315 static int
316 zfs_replay_acl(zfsvfs_t *zfsvfs, lr_acl_t *lr, boolean_t byteswap)
317 {
318 	ace_t *ace = (ace_t *)(lr + 1);	/* ace array follows lr_acl_t */
319 	vsecattr_t vsa;
320 	znode_t *zp;
321 	int error;
322 
323 	if (byteswap) {
324 		byteswap_uint64_array(lr, sizeof (*lr));
325 		zfs_ace_byteswap(ace, lr->lr_aclcnt);
326 	}
327 
328 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
329 		/*
330 		 * As we can log acls out of order, it's possible the
331 		 * file has been removed. In this case just drop the acl
332 		 * and return success.
333 		 */
334 		if (error == ENOENT)
335 			error = 0;
336 		return (error);
337 	}
338 
339 	bzero(&vsa, sizeof (vsa));
340 	vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
341 	vsa.vsa_aclcnt = lr->lr_aclcnt;
342 	vsa.vsa_aclentp = ace;
343 
344 	error = VOP_SETSECATTR(ZTOV(zp), &vsa, 0, kcred);
345 
346 	VN_RELE(ZTOV(zp));
347 
348 	return (error);
349 }
350 
351 /*
352  * Callback vectors for replaying records
353  */
354 zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
355 	zfs_replay_error,	/* 0 no such transaction type */
356 	zfs_replay_create,	/* TX_CREATE */
357 	zfs_replay_create,	/* TX_MKDIR */
358 	zfs_replay_create,	/* TX_MKXATTR */
359 	zfs_replay_create,	/* TX_SYMLINK */
360 	zfs_replay_remove,	/* TX_REMOVE */
361 	zfs_replay_remove,	/* TX_RMDIR */
362 	zfs_replay_link,	/* TX_LINK */
363 	zfs_replay_rename,	/* TX_RENAME */
364 	zfs_replay_write,	/* TX_WRITE */
365 	zfs_replay_truncate,	/* TX_TRUNCATE */
366 	zfs_replay_setattr,	/* TX_SETATTR */
367 	zfs_replay_acl,		/* TX_ACL */
368 };
369