xref: /titanic_51/usr/src/uts/common/fs/zfs/zfs_replay.c (revision f875b4ebb1dd9fdbeb043557cab38ab3bf7f6e01)
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 2007 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 	error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
108 	if (error != ENOENT)
109 		goto out;
110 
111 	switch ((int)lr->lr_common.lrc_txtype) {
112 	case TX_CREATE:
113 		error = VOP_CREATE(ZTOV(dzp), name, &va, 0, 0, &vp, kcred, 0);
114 		break;
115 	case TX_MKDIR:
116 		error = VOP_MKDIR(ZTOV(dzp), name, &va, &vp, kcred);
117 		break;
118 	case TX_MKXATTR:
119 		error = zfs_make_xattrdir(dzp, &va, &vp, kcred);
120 		break;
121 	case TX_SYMLINK:
122 		link = name + strlen(name) + 1;
123 		error = VOP_SYMLINK(ZTOV(dzp), name, &va, link, kcred);
124 		break;
125 	default:
126 		error = ENOTSUP;
127 	}
128 
129 out:
130 	if (error == 0 && vp != NULL)
131 		VN_RELE(vp);
132 
133 	VN_RELE(ZTOV(dzp));
134 
135 	return (error);
136 }
137 
138 static int
139 zfs_replay_remove(zfsvfs_t *zfsvfs, lr_remove_t *lr, boolean_t byteswap)
140 {
141 	char *name = (char *)(lr + 1);	/* name follows lr_remove_t */
142 	znode_t *dzp;
143 	int error;
144 
145 	if (byteswap)
146 		byteswap_uint64_array(lr, sizeof (*lr));
147 
148 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
149 		return (error);
150 
151 	switch ((int)lr->lr_common.lrc_txtype) {
152 	case TX_REMOVE:
153 		error = VOP_REMOVE(ZTOV(dzp), name, kcred);
154 		break;
155 	case TX_RMDIR:
156 		error = VOP_RMDIR(ZTOV(dzp), name, NULL, kcred);
157 		break;
158 	default:
159 		error = ENOTSUP;
160 	}
161 
162 	VN_RELE(ZTOV(dzp));
163 
164 	return (error);
165 }
166 
167 static int
168 zfs_replay_link(zfsvfs_t *zfsvfs, lr_link_t *lr, boolean_t byteswap)
169 {
170 	char *name = (char *)(lr + 1);	/* name follows lr_link_t */
171 	znode_t *dzp, *zp;
172 	int error;
173 
174 	if (byteswap)
175 		byteswap_uint64_array(lr, sizeof (*lr));
176 
177 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
178 		return (error);
179 
180 	if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
181 		VN_RELE(ZTOV(dzp));
182 		return (error);
183 	}
184 
185 	error = VOP_LINK(ZTOV(dzp), ZTOV(zp), name, kcred);
186 
187 	VN_RELE(ZTOV(zp));
188 	VN_RELE(ZTOV(dzp));
189 
190 	return (error);
191 }
192 
193 static int
194 zfs_replay_rename(zfsvfs_t *zfsvfs, lr_rename_t *lr, boolean_t byteswap)
195 {
196 	char *sname = (char *)(lr + 1);	/* sname and tname follow lr_rename_t */
197 	char *tname = sname + strlen(sname) + 1;
198 	znode_t *sdzp, *tdzp;
199 	int error;
200 
201 	if (byteswap)
202 		byteswap_uint64_array(lr, sizeof (*lr));
203 
204 	if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
205 		return (error);
206 
207 	if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
208 		VN_RELE(ZTOV(sdzp));
209 		return (error);
210 	}
211 
212 	error = VOP_RENAME(ZTOV(sdzp), sname, ZTOV(tdzp), tname, kcred);
213 
214 	VN_RELE(ZTOV(tdzp));
215 	VN_RELE(ZTOV(sdzp));
216 
217 	return (error);
218 }
219 
220 static int
221 zfs_replay_write(zfsvfs_t *zfsvfs, lr_write_t *lr, boolean_t byteswap)
222 {
223 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
224 	znode_t	*zp;
225 	int error;
226 	ssize_t resid;
227 
228 	if (byteswap)
229 		byteswap_uint64_array(lr, sizeof (*lr));
230 
231 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
232 		/*
233 		 * As we can log writes out of order, it's possible the
234 		 * file has been removed. In this case just drop the write
235 		 * and return success.
236 		 */
237 		if (error == ENOENT)
238 			error = 0;
239 		return (error);
240 	}
241 
242 	error = vn_rdwr(UIO_WRITE, ZTOV(zp), data, lr->lr_length,
243 	    lr->lr_offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
244 
245 	VN_RELE(ZTOV(zp));
246 
247 	return (error);
248 }
249 
250 static int
251 zfs_replay_truncate(zfsvfs_t *zfsvfs, lr_truncate_t *lr, boolean_t byteswap)
252 {
253 	znode_t *zp;
254 	flock64_t fl;
255 	int error;
256 
257 	if (byteswap)
258 		byteswap_uint64_array(lr, sizeof (*lr));
259 
260 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
261 		/*
262 		 * As we can log truncates out of order, it's possible the
263 		 * file has been removed. In this case just drop the truncate
264 		 * and return success.
265 		 */
266 		if (error == ENOENT)
267 			error = 0;
268 		return (error);
269 	}
270 
271 	bzero(&fl, sizeof (fl));
272 	fl.l_type = F_WRLCK;
273 	fl.l_whence = 0;
274 	fl.l_start = lr->lr_offset;
275 	fl.l_len = lr->lr_length;
276 
277 	error = VOP_SPACE(ZTOV(zp), F_FREESP, &fl, FWRITE | FOFFMAX,
278 	    lr->lr_offset, kcred, NULL);
279 
280 	VN_RELE(ZTOV(zp));
281 
282 	return (error);
283 }
284 
285 static int
286 zfs_replay_setattr(zfsvfs_t *zfsvfs, lr_setattr_t *lr, boolean_t byteswap)
287 {
288 	znode_t *zp;
289 	vattr_t va;
290 	int error;
291 
292 	if (byteswap)
293 		byteswap_uint64_array(lr, sizeof (*lr));
294 
295 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
296 		/*
297 		 * As we can log setattrs out of order, it's possible the
298 		 * file has been removed. In this case just drop the setattr
299 		 * and return success.
300 		 */
301 		if (error == ENOENT)
302 			error = 0;
303 		return (error);
304 	}
305 
306 	zfs_init_vattr(&va, lr->lr_mask, lr->lr_mode,
307 	    lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
308 
309 	va.va_size = lr->lr_size;
310 	ZFS_TIME_DECODE(&va.va_atime, lr->lr_atime);
311 	ZFS_TIME_DECODE(&va.va_mtime, lr->lr_mtime);
312 
313 	error = VOP_SETATTR(ZTOV(zp), &va, 0, kcred, NULL);
314 
315 	VN_RELE(ZTOV(zp));
316 
317 	return (error);
318 }
319 
320 static int
321 zfs_replay_acl(zfsvfs_t *zfsvfs, lr_acl_t *lr, boolean_t byteswap)
322 {
323 	ace_t *ace = (ace_t *)(lr + 1);	/* ace array follows lr_acl_t */
324 	vsecattr_t vsa;
325 	znode_t *zp;
326 	int error;
327 
328 	if (byteswap) {
329 		byteswap_uint64_array(lr, sizeof (*lr));
330 		zfs_ace_byteswap(ace, lr->lr_aclcnt);
331 	}
332 
333 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
334 		/*
335 		 * As we can log acls out of order, it's possible the
336 		 * file has been removed. In this case just drop the acl
337 		 * and return success.
338 		 */
339 		if (error == ENOENT)
340 			error = 0;
341 		return (error);
342 	}
343 
344 	bzero(&vsa, sizeof (vsa));
345 	vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
346 	vsa.vsa_aclcnt = lr->lr_aclcnt;
347 	vsa.vsa_aclentp = ace;
348 
349 	error = VOP_SETSECATTR(ZTOV(zp), &vsa, 0, kcred);
350 
351 	VN_RELE(ZTOV(zp));
352 
353 	return (error);
354 }
355 
356 /*
357  * Callback vectors for replaying records
358  */
359 zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
360 	zfs_replay_error,	/* 0 no such transaction type */
361 	zfs_replay_create,	/* TX_CREATE */
362 	zfs_replay_create,	/* TX_MKDIR */
363 	zfs_replay_create,	/* TX_MKXATTR */
364 	zfs_replay_create,	/* TX_SYMLINK */
365 	zfs_replay_remove,	/* TX_REMOVE */
366 	zfs_replay_remove,	/* TX_RMDIR */
367 	zfs_replay_link,	/* TX_LINK */
368 	zfs_replay_rename,	/* TX_RENAME */
369 	zfs_replay_write,	/* TX_WRITE */
370 	zfs_replay_truncate,	/* TX_TRUNCATE */
371 	zfs_replay_setattr,	/* TX_SETATTR */
372 	zfs_replay_acl,		/* TX_ACL */
373 };
374