]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_replay.c
Clear PG_writeback for sync I/O error case
[mirror_zfs.git] / module / zfs / zfs_replay.c
CommitLineData
34dc7c2f
BB
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/*
572e2857 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
34dc7c2f
BB
23 */
24
34dc7c2f
BB
25#include <sys/types.h>
26#include <sys/param.h>
27#include <sys/systm.h>
28#include <sys/sysmacros.h>
29#include <sys/cmn_err.h>
30#include <sys/kmem.h>
31#include <sys/thread.h>
32#include <sys/file.h>
33#include <sys/fcntl.h>
34#include <sys/vfs.h>
35#include <sys/fs/zfs.h>
36#include <sys/zfs_znode.h>
37#include <sys/zfs_dir.h>
38#include <sys/zfs_acl.h>
39#include <sys/zfs_fuid.h>
5484965a 40#include <sys/zfs_vnops.h>
34dc7c2f
BB
41#include <sys/spa.h>
42#include <sys/zil.h>
43#include <sys/byteorder.h>
44#include <sys/stat.h>
45#include <sys/mode.h>
46#include <sys/acl.h>
47#include <sys/atomic.h>
48#include <sys/cred.h>
5484965a 49#include <sys/zpl.h>
34dc7c2f
BB
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
57static void
5484965a
BB
58zfs_init_vattr(vattr_t *vap, uint64_t mask, uint64_t mode,
59 uint64_t uid, uint64_t gid, uint64_t rdev, uint64_t nodeid)
34dc7c2f 60{
5484965a
BB
61 bzero(vap, sizeof (*vap));
62 vap->va_mask = (uint_t)mask;
63 vap->va_type = IFTOVT(mode);
64 vap->va_mode = mode;
65 vap->va_uid = (uid_t)(IS_EPHEMERAL(uid)) ? -1 : uid;
66 vap->va_gid = (gid_t)(IS_EPHEMERAL(gid)) ? -1 : gid;
67 vap->va_rdev = rdev;
68 vap->va_nodeid = nodeid;
34dc7c2f
BB
69}
70
71/* ARGSUSED */
72static int
633e8030 73zfs_replay_error(zfs_sb_t *zsb, lr_t *lr, boolean_t byteswap)
34dc7c2f
BB
74{
75 return (ENOTSUP);
76}
77
78static void
79zfs_replay_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
80{
81 xoptattr_t *xoap = NULL;
82 uint64_t *attrs;
83 uint64_t *crtime;
84 uint32_t *bitmap;
85 void *scanstamp;
86 int i;
87
5484965a 88 xvap->xva_vattr.va_mask |= ATTR_XVATTR;
34dc7c2f 89 if ((xoap = xva_getxoptattr(xvap)) == NULL) {
5484965a 90 xvap->xva_vattr.va_mask &= ~ATTR_XVATTR; /* shouldn't happen */
34dc7c2f
BB
91 return;
92 }
93
94 ASSERT(lrattr->lr_attr_masksize == xvap->xva_mapsize);
95
96 bitmap = &lrattr->lr_attr_bitmap;
97 for (i = 0; i != lrattr->lr_attr_masksize; i++, bitmap++)
98 xvap->xva_reqattrmap[i] = *bitmap;
99
100 attrs = (uint64_t *)(lrattr + lrattr->lr_attr_masksize - 1);
101 crtime = attrs + 1;
102 scanstamp = (caddr_t)(crtime + 2);
103
104 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
105 xoap->xoa_hidden = ((*attrs & XAT0_HIDDEN) != 0);
106 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
107 xoap->xoa_system = ((*attrs & XAT0_SYSTEM) != 0);
108 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
109 xoap->xoa_archive = ((*attrs & XAT0_ARCHIVE) != 0);
110 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
111 xoap->xoa_readonly = ((*attrs & XAT0_READONLY) != 0);
112 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
113 xoap->xoa_immutable = ((*attrs & XAT0_IMMUTABLE) != 0);
114 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
115 xoap->xoa_nounlink = ((*attrs & XAT0_NOUNLINK) != 0);
116 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
117 xoap->xoa_appendonly = ((*attrs & XAT0_APPENDONLY) != 0);
118 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
119 xoap->xoa_nodump = ((*attrs & XAT0_NODUMP) != 0);
120 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
121 xoap->xoa_opaque = ((*attrs & XAT0_OPAQUE) != 0);
122 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
123 xoap->xoa_av_modified = ((*attrs & XAT0_AV_MODIFIED) != 0);
124 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
125 xoap->xoa_av_quarantined =
126 ((*attrs & XAT0_AV_QUARANTINED) != 0);
127 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
128 ZFS_TIME_DECODE(&xoap->xoa_createtime, crtime);
129 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
130 bcopy(scanstamp, xoap->xoa_av_scanstamp, AV_SCANSTAMP_SZ);
428870ff
BB
131 if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
132 xoap->xoa_reparse = ((*attrs & XAT0_REPARSE) != 0);
572e2857
BB
133 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
134 xoap->xoa_offline = ((*attrs & XAT0_OFFLINE) != 0);
135 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
136 xoap->xoa_sparse = ((*attrs & XAT0_SPARSE) != 0);
34dc7c2f
BB
137}
138
139static int
140zfs_replay_domain_cnt(uint64_t uid, uint64_t gid)
141{
142 uint64_t uid_idx;
143 uint64_t gid_idx;
144 int domcnt = 0;
145
146 uid_idx = FUID_INDEX(uid);
147 gid_idx = FUID_INDEX(gid);
148 if (uid_idx)
149 domcnt++;
150 if (gid_idx > 0 && gid_idx != uid_idx)
151 domcnt++;
152
153 return (domcnt);
154}
155
156static void *
157zfs_replay_fuid_domain_common(zfs_fuid_info_t *fuid_infop, void *start,
158 int domcnt)
159{
160 int i;
161
162 for (i = 0; i != domcnt; i++) {
163 fuid_infop->z_domain_table[i] = start;
164 start = (caddr_t)start + strlen(start) + 1;
165 }
166
167 return (start);
168}
169
170/*
171 * Set the uid/gid in the fuid_info structure.
172 */
173static void
174zfs_replay_fuid_ugid(zfs_fuid_info_t *fuid_infop, uint64_t uid, uint64_t gid)
175{
176 /*
177 * If owner or group are log specific FUIDs then slurp up
178 * domain information and build zfs_fuid_info_t
179 */
180 if (IS_EPHEMERAL(uid))
181 fuid_infop->z_fuid_owner = uid;
182
183 if (IS_EPHEMERAL(gid))
184 fuid_infop->z_fuid_group = gid;
185}
186
187/*
188 * Load fuid domains into fuid_info_t
189 */
190static zfs_fuid_info_t *
191zfs_replay_fuid_domain(void *buf, void **end, uint64_t uid, uint64_t gid)
192{
193 int domcnt;
194
195 zfs_fuid_info_t *fuid_infop;
196
197 fuid_infop = zfs_fuid_info_alloc();
198
199 domcnt = zfs_replay_domain_cnt(uid, gid);
200
201 if (domcnt == 0)
202 return (fuid_infop);
203
204 fuid_infop->z_domain_table =
205 kmem_zalloc(domcnt * sizeof (char **), KM_SLEEP);
206
207 zfs_replay_fuid_ugid(fuid_infop, uid, gid);
208
209 fuid_infop->z_domain_cnt = domcnt;
210 *end = zfs_replay_fuid_domain_common(fuid_infop, buf, domcnt);
211 return (fuid_infop);
212}
213
214/*
215 * load zfs_fuid_t's and fuid_domains into fuid_info_t
216 */
217static zfs_fuid_info_t *
218zfs_replay_fuids(void *start, void **end, int idcnt, int domcnt, uint64_t uid,
219 uint64_t gid)
220{
221 uint64_t *log_fuid = (uint64_t *)start;
222 zfs_fuid_info_t *fuid_infop;
223 int i;
224
225 fuid_infop = zfs_fuid_info_alloc();
226 fuid_infop->z_domain_cnt = domcnt;
227
228 fuid_infop->z_domain_table =
229 kmem_zalloc(domcnt * sizeof (char **), KM_SLEEP);
230
231 for (i = 0; i != idcnt; i++) {
232 zfs_fuid_t *zfuid;
233
234 zfuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
235 zfuid->z_logfuid = *log_fuid;
236 zfuid->z_id = -1;
237 zfuid->z_domidx = 0;
238 list_insert_tail(&fuid_infop->z_fuids, zfuid);
239 log_fuid++;
240 }
241
242 zfs_replay_fuid_ugid(fuid_infop, uid, gid);
243
244 *end = zfs_replay_fuid_domain_common(fuid_infop, log_fuid, domcnt);
245 return (fuid_infop);
246}
247
248static void
249zfs_replay_swap_attrs(lr_attr_t *lrattr)
250{
251 /* swap the lr_attr structure */
252 byteswap_uint32_array(lrattr, sizeof (*lrattr));
253 /* swap the bitmap */
254 byteswap_uint32_array(lrattr + 1, (lrattr->lr_attr_masksize - 1) *
255 sizeof (uint32_t));
256 /* swap the attributes, create time + 64 bit word for attributes */
257 byteswap_uint64_array((caddr_t)(lrattr + 1) + (sizeof (uint32_t) *
258 (lrattr->lr_attr_masksize - 1)), 3 * sizeof (uint64_t));
259}
260
261/*
262 * Replay file create with optional ACL, xvattr information as well
263 * as option FUID information.
264 */
265static int
633e8030 266zfs_replay_create_acl(zfs_sb_t *zsb, lr_acl_create_t *lracl, boolean_t byteswap)
34dc7c2f
BB
267{
268 char *name = NULL; /* location determined later */
269 lr_create_t *lr = (lr_create_t *)lracl;
270 znode_t *dzp;
633e8030 271 struct inode *ip = NULL;
34dc7c2f 272 xvattr_t xva;
633e8030 273 int vflg = 0;
5484965a 274 vsecattr_t vsec = { 0 };
34dc7c2f
BB
275 lr_attr_t *lrattr;
276 void *aclstart;
277 void *fuidstart;
278 size_t xvatlen = 0;
279 uint64_t txtype;
280 int error;
281
428870ff 282 txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
34dc7c2f
BB
283 if (byteswap) {
284 byteswap_uint64_array(lracl, sizeof (*lracl));
34dc7c2f
BB
285 if (txtype == TX_CREATE_ACL_ATTR ||
286 txtype == TX_MKDIR_ACL_ATTR) {
287 lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
288 zfs_replay_swap_attrs(lrattr);
289 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
290 }
291
292 aclstart = (caddr_t)(lracl + 1) + xvatlen;
293 zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE);
294 /* swap fuids */
295 if (lracl->lr_fuidcnt) {
296 byteswap_uint64_array((caddr_t)aclstart +
297 ZIL_ACE_LENGTH(lracl->lr_acl_bytes),
298 lracl->lr_fuidcnt * sizeof (uint64_t));
299 }
300 }
301
633e8030 302 if ((error = zfs_zget(zsb, lr->lr_doid, &dzp)) != 0)
34dc7c2f
BB
303 return (error);
304
305 xva_init(&xva);
6742abf9 306 zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
5484965a 307 lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
34dc7c2f
BB
308
309 /*
310 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
311 * eventually end up in zfs_mknode(), which assigns the object's
ab892c5f 312 * creation time and generation number. The generic zfs_create()
34dc7c2f
BB
313 * doesn't have either concept, so we smuggle the values inside
314 * the vattr's otherwise unused va_ctime and va_nblocks fields.
315 */
316 ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
317 xva.xva_vattr.va_nblocks = lr->lr_gen;
318
633e8030 319 error = dmu_object_info(zsb->z_os, lr->lr_foid, NULL);
34dc7c2f
BB
320 if (error != ENOENT)
321 goto bail;
322
323 if (lr->lr_common.lrc_txtype & TX_CI)
324 vflg |= FIGNORECASE;
428870ff 325 switch (txtype) {
34dc7c2f
BB
326 case TX_CREATE_ACL:
327 aclstart = (caddr_t)(lracl + 1);
328 fuidstart = (caddr_t)aclstart +
329 ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
633e8030 330 zsb->z_fuid_replay = zfs_replay_fuids(fuidstart,
34dc7c2f
BB
331 (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
332 lr->lr_uid, lr->lr_gid);
333 /*FALLTHROUGH*/
334 case TX_CREATE_ACL_ATTR:
335 if (name == NULL) {
336 lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
337 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
5484965a 338 xva.xva_vattr.va_mask |= ATTR_XVATTR;
34dc7c2f
BB
339 zfs_replay_xvattr(lrattr, &xva);
340 }
341 vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
342 vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
343 vsec.vsa_aclcnt = lracl->lr_aclcnt;
344 vsec.vsa_aclentsz = lracl->lr_acl_bytes;
345 vsec.vsa_aclflags = lracl->lr_acl_flags;
633e8030 346 if (zsb->z_fuid_replay == NULL) {
34dc7c2f
BB
347 fuidstart = (caddr_t)(lracl + 1) + xvatlen +
348 ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
633e8030 349 zsb->z_fuid_replay =
34dc7c2f
BB
350 zfs_replay_fuids(fuidstart,
351 (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
352 lr->lr_uid, lr->lr_gid);
353 }
354
633e8030 355 error = zfs_create(ZTOI(dzp), name, &xva.xva_vattr,
5484965a 356 0, 0, &ip, kcred, vflg, &vsec);
34dc7c2f
BB
357 break;
358 case TX_MKDIR_ACL:
359 aclstart = (caddr_t)(lracl + 1);
360 fuidstart = (caddr_t)aclstart +
361 ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
633e8030 362 zsb->z_fuid_replay = zfs_replay_fuids(fuidstart,
34dc7c2f
BB
363 (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
364 lr->lr_uid, lr->lr_gid);
365 /*FALLTHROUGH*/
366 case TX_MKDIR_ACL_ATTR:
367 if (name == NULL) {
368 lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
369 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
370 zfs_replay_xvattr(lrattr, &xva);
371 }
372 vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
373 vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
374 vsec.vsa_aclcnt = lracl->lr_aclcnt;
375 vsec.vsa_aclentsz = lracl->lr_acl_bytes;
376 vsec.vsa_aclflags = lracl->lr_acl_flags;
633e8030 377 if (zsb->z_fuid_replay == NULL) {
34dc7c2f
BB
378 fuidstart = (caddr_t)(lracl + 1) + xvatlen +
379 ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
633e8030 380 zsb->z_fuid_replay =
34dc7c2f
BB
381 zfs_replay_fuids(fuidstart,
382 (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
383 lr->lr_uid, lr->lr_gid);
384 }
633e8030 385 error = zfs_mkdir(ZTOI(dzp), name, &xva.xva_vattr,
5484965a 386 &ip, kcred, vflg, &vsec);
34dc7c2f
BB
387 break;
388 default:
389 error = ENOTSUP;
390 }
391
392bail:
633e8030
BB
393 if (error == 0 && ip != NULL)
394 iput(ip);
34dc7c2f 395
633e8030 396 iput(ZTOI(dzp));
34dc7c2f 397
633e8030
BB
398 if (zsb->z_fuid_replay)
399 zfs_fuid_info_free(zsb->z_fuid_replay);
400 zsb->z_fuid_replay = NULL;
34dc7c2f
BB
401
402 return (error);
403}
404
405static int
633e8030 406zfs_replay_create(zfs_sb_t *zsb, lr_create_t *lr, boolean_t byteswap)
34dc7c2f
BB
407{
408 char *name = NULL; /* location determined later */
409 char *link; /* symlink content follows name */
410 znode_t *dzp;
633e8030 411 struct inode *ip = NULL;
34dc7c2f
BB
412 xvattr_t xva;
413 int vflg = 0;
414 size_t lrsize = sizeof (lr_create_t);
415 lr_attr_t *lrattr;
633e8030 416 void *start;
5484965a 417 size_t xvatlen;
34dc7c2f
BB
418 uint64_t txtype;
419 int error;
420
428870ff 421 txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
34dc7c2f
BB
422 if (byteswap) {
423 byteswap_uint64_array(lr, sizeof (*lr));
34dc7c2f
BB
424 if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR)
425 zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
426 }
427
428
633e8030 429 if ((error = zfs_zget(zsb, lr->lr_doid, &dzp)) != 0)
34dc7c2f
BB
430 return (error);
431
432 xva_init(&xva);
6742abf9 433 zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
5484965a 434 lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
34dc7c2f
BB
435
436 /*
437 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
438 * eventually end up in zfs_mknode(), which assigns the object's
ab892c5f 439 * creation time and generation number. The generic zfs_create()
34dc7c2f
BB
440 * doesn't have either concept, so we smuggle the values inside
441 * the vattr's otherwise unused va_ctime and va_nblocks fields.
442 */
443 ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
444 xva.xva_vattr.va_nblocks = lr->lr_gen;
445
633e8030 446 error = dmu_object_info(zsb->z_os, lr->lr_foid, NULL);
34dc7c2f
BB
447 if (error != ENOENT)
448 goto out;
449
450 if (lr->lr_common.lrc_txtype & TX_CI)
451 vflg |= FIGNORECASE;
452
453 /*
454 * Symlinks don't have fuid info, and CIFS never creates
455 * symlinks.
456 *
457 * The _ATTR versions will grab the fuid info in their subcases.
458 */
459 if ((int)lr->lr_common.lrc_txtype != TX_SYMLINK &&
460 (int)lr->lr_common.lrc_txtype != TX_MKDIR_ATTR &&
461 (int)lr->lr_common.lrc_txtype != TX_CREATE_ATTR) {
462 start = (lr + 1);
633e8030 463 zsb->z_fuid_replay =
34dc7c2f
BB
464 zfs_replay_fuid_domain(start, &start,
465 lr->lr_uid, lr->lr_gid);
466 }
467
428870ff 468 switch (txtype) {
34dc7c2f
BB
469 case TX_CREATE_ATTR:
470 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
471 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
472 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
473 start = (caddr_t)(lr + 1) + xvatlen;
633e8030 474 zsb->z_fuid_replay =
34dc7c2f
BB
475 zfs_replay_fuid_domain(start, &start,
476 lr->lr_uid, lr->lr_gid);
477 name = (char *)start;
478
479 /*FALLTHROUGH*/
480 case TX_CREATE:
481 if (name == NULL)
482 name = (char *)start;
483
633e8030
BB
484 error = zfs_create(ZTOI(dzp), name, &xva.xva_vattr,
485 0, 0, &ip, kcred, vflg, NULL);
34dc7c2f
BB
486 break;
487 case TX_MKDIR_ATTR:
488 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
489 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
490 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
491 start = (caddr_t)(lr + 1) + xvatlen;
633e8030 492 zsb->z_fuid_replay =
34dc7c2f
BB
493 zfs_replay_fuid_domain(start, &start,
494 lr->lr_uid, lr->lr_gid);
495 name = (char *)start;
496
497 /*FALLTHROUGH*/
498 case TX_MKDIR:
499 if (name == NULL)
500 name = (char *)(lr + 1);
501
633e8030
BB
502 error = zfs_mkdir(ZTOI(dzp), name, &xva.xva_vattr,
503 &ip, kcred, vflg, NULL);
34dc7c2f
BB
504 break;
505 case TX_MKXATTR:
633e8030 506 error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &ip, kcred);
34dc7c2f
BB
507 break;
508 case TX_SYMLINK:
509 name = (char *)(lr + 1);
510 link = name + strlen(name) + 1;
633e8030
BB
511 error = zfs_symlink(ZTOI(dzp), name, &xva.xva_vattr,
512 link, &ip, kcred, vflg);
34dc7c2f
BB
513 break;
514 default:
515 error = ENOTSUP;
516 }
517
518out:
633e8030
BB
519 if (error == 0 && ip != NULL)
520 iput(ip);
34dc7c2f 521
633e8030 522 iput(ZTOI(dzp));
34dc7c2f 523
633e8030
BB
524 if (zsb->z_fuid_replay)
525 zfs_fuid_info_free(zsb->z_fuid_replay);
526 zsb->z_fuid_replay = NULL;
34dc7c2f
BB
527 return (error);
528}
529
530static int
633e8030 531zfs_replay_remove(zfs_sb_t *zsb, lr_remove_t *lr, boolean_t byteswap)
34dc7c2f
BB
532{
533 char *name = (char *)(lr + 1); /* name follows lr_remove_t */
534 znode_t *dzp;
535 int error;
536 int vflg = 0;
537
538 if (byteswap)
539 byteswap_uint64_array(lr, sizeof (*lr));
540
633e8030 541 if ((error = zfs_zget(zsb, lr->lr_doid, &dzp)) != 0)
34dc7c2f
BB
542 return (error);
543
544 if (lr->lr_common.lrc_txtype & TX_CI)
545 vflg |= FIGNORECASE;
546
547 switch ((int)lr->lr_common.lrc_txtype) {
548 case TX_REMOVE:
633e8030 549 error = zfs_remove(ZTOI(dzp), name, kcred);
34dc7c2f
BB
550 break;
551 case TX_RMDIR:
633e8030 552 error = zfs_rmdir(ZTOI(dzp), name, NULL, kcred, vflg);
34dc7c2f
BB
553 break;
554 default:
555 error = ENOTSUP;
556 }
557
633e8030 558 iput(ZTOI(dzp));
34dc7c2f
BB
559
560 return (error);
561}
562
563static int
633e8030 564zfs_replay_link(zfs_sb_t *zsb, lr_link_t *lr, boolean_t byteswap)
34dc7c2f
BB
565{
566 char *name = (char *)(lr + 1); /* name follows lr_link_t */
567 znode_t *dzp, *zp;
568 int error;
5484965a 569 int vflg = 0;
34dc7c2f
BB
570
571 if (byteswap)
572 byteswap_uint64_array(lr, sizeof (*lr));
573
633e8030 574 if ((error = zfs_zget(zsb, lr->lr_doid, &dzp)) != 0)
34dc7c2f
BB
575 return (error);
576
633e8030
BB
577 if ((error = zfs_zget(zsb, lr->lr_link_obj, &zp)) != 0) {
578 iput(ZTOI(dzp));
34dc7c2f
BB
579 return (error);
580 }
581
582 if (lr->lr_common.lrc_txtype & TX_CI)
583 vflg |= FIGNORECASE;
584
633e8030 585 error = zfs_link(ZTOI(dzp), ZTOI(zp), name, kcred);
34dc7c2f 586
633e8030
BB
587 iput(ZTOI(zp));
588 iput(ZTOI(dzp));
34dc7c2f
BB
589
590 return (error);
591}
592
593static int
633e8030 594zfs_replay_rename(zfs_sb_t *zsb, lr_rename_t *lr, boolean_t byteswap)
34dc7c2f
BB
595{
596 char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */
597 char *tname = sname + strlen(sname) + 1;
598 znode_t *sdzp, *tdzp;
599 int error;
600 int vflg = 0;
601
602 if (byteswap)
603 byteswap_uint64_array(lr, sizeof (*lr));
604
633e8030 605 if ((error = zfs_zget(zsb, lr->lr_sdoid, &sdzp)) != 0)
34dc7c2f
BB
606 return (error);
607
633e8030
BB
608 if ((error = zfs_zget(zsb, lr->lr_tdoid, &tdzp)) != 0) {
609 iput(ZTOI(sdzp));
34dc7c2f
BB
610 return (error);
611 }
612
613 if (lr->lr_common.lrc_txtype & TX_CI)
614 vflg |= FIGNORECASE;
615
633e8030 616 error = zfs_rename(ZTOI(sdzp), sname, ZTOI(tdzp), tname, kcred, vflg);
34dc7c2f 617
633e8030
BB
618 iput(ZTOI(tdzp));
619 iput(ZTOI(sdzp));
34dc7c2f
BB
620
621 return (error);
622}
623
624static int
633e8030 625zfs_replay_write(zfs_sb_t *zsb, lr_write_t *lr, boolean_t byteswap)
34dc7c2f
BB
626{
627 char *data = (char *)(lr + 1); /* data follows lr_write_t */
628 znode_t *zp;
629 int error;
572e2857 630 uint64_t eod, offset, length;
34dc7c2f
BB
631
632 if (byteswap)
633 byteswap_uint64_array(lr, sizeof (*lr));
634
633e8030 635 if ((error = zfs_zget(zsb, lr->lr_foid, &zp)) != 0) {
34dc7c2f
BB
636 /*
637 * As we can log writes out of order, it's possible the
638 * file has been removed. In this case just drop the write
639 * and return success.
640 */
641 if (error == ENOENT)
642 error = 0;
643 return (error);
644 }
645
428870ff
BB
646 offset = lr->lr_offset;
647 length = lr->lr_length;
572e2857 648 eod = offset + length; /* end of data for this write */
428870ff 649
572e2857
BB
650 /*
651 * This may be a write from a dmu_sync() for a whole block,
652 * and may extend beyond the current end of the file.
653 * We can't just replay what was written for this TX_WRITE as
654 * a future TX_WRITE2 may extend the eof and the data for that
655 * write needs to be there. So we write the whole block and
656 * reduce the eof. This needs to be done within the single dmu
657 * transaction created within vn_rdwr -> zfs_write. So a possible
633e8030 658 * new end of file is passed through in zsb->z_replay_eof
572e2857
BB
659 */
660
633e8030 661 zsb->z_replay_eof = 0; /* 0 means don't change end of file */
428870ff
BB
662
663 /* If it's a dmu_sync() block, write the whole block */
664 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
665 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
666 if (length < blocksize) {
667 offset -= offset % blocksize;
668 length = blocksize;
669 }
572e2857 670 if (zp->z_size < eod)
633e8030 671 zsb->z_replay_eof = eod;
428870ff
BB
672 }
673
5484965a
BB
674 error = zpl_write_common(ZTOI(zp), data, length, offset,
675 UIO_SYSSPACE, 0, kcred);
676 if (error) {
677 if (error < 0)
678 error = -error;
679 else
680 error = EIO; /* Short write */
681 }
428870ff 682
633e8030
BB
683 iput(ZTOI(zp));
684 zsb->z_replay_eof = 0; /* safety */
428870ff
BB
685
686 return (error);
687}
688
689/*
690 * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
691 * meaning the pool block is already being synced. So now that we always write
692 * out full blocks, all we have to do is expand the eof if
693 * the file is grown.
694 */
695static int
633e8030 696zfs_replay_write2(zfs_sb_t *zsb, lr_write_t *lr, boolean_t byteswap)
428870ff
BB
697{
698 znode_t *zp;
699 int error;
700 uint64_t end;
701
702 if (byteswap)
703 byteswap_uint64_array(lr, sizeof (*lr));
704
633e8030 705 if ((error = zfs_zget(zsb, lr->lr_foid, &zp)) != 0)
428870ff
BB
706 return (error);
707
572e2857 708top:
428870ff
BB
709 end = lr->lr_offset + lr->lr_length;
710 if (end > zp->z_size) {
633e8030 711 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
572e2857 712
428870ff 713 zp->z_size = end;
572e2857
BB
714 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
715 error = dmu_tx_assign(tx, TXG_WAIT);
716 if (error) {
633e8030 717 iput(ZTOI(zp));
572e2857
BB
718 if (error == ERESTART) {
719 dmu_tx_wait(tx);
720 dmu_tx_abort(tx);
721 goto top;
722 }
723 dmu_tx_abort(tx);
724 return (error);
725 }
633e8030 726 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
572e2857
BB
727 (void *)&zp->z_size, sizeof (uint64_t), tx);
728
729 /* Ensure the replayed seq is updated */
633e8030 730 (void) zil_replaying(zsb->z_log, tx);
572e2857
BB
731
732 dmu_tx_commit(tx);
428870ff 733 }
34dc7c2f 734
633e8030 735 iput(ZTOI(zp));
34dc7c2f
BB
736
737 return (error);
738}
739
740static int
633e8030 741zfs_replay_truncate(zfs_sb_t *zsb, lr_truncate_t *lr, boolean_t byteswap)
34dc7c2f
BB
742{
743 znode_t *zp;
744 flock64_t fl;
745 int error;
746
747 if (byteswap)
748 byteswap_uint64_array(lr, sizeof (*lr));
749
633e8030 750 if ((error = zfs_zget(zsb, lr->lr_foid, &zp)) != 0)
34dc7c2f 751 return (error);
34dc7c2f
BB
752
753 bzero(&fl, sizeof (fl));
754 fl.l_type = F_WRLCK;
755 fl.l_whence = 0;
756 fl.l_start = lr->lr_offset;
757 fl.l_len = lr->lr_length;
758
633e8030
BB
759 error = zfs_space(ZTOI(zp), F_FREESP, &fl, FWRITE | FOFFMAX,
760 lr->lr_offset, kcred);
34dc7c2f 761
633e8030 762 iput(ZTOI(zp));
34dc7c2f
BB
763
764 return (error);
765}
766
767static int
633e8030 768zfs_replay_setattr(zfs_sb_t *zsb, lr_setattr_t *lr, boolean_t byteswap)
34dc7c2f
BB
769{
770 znode_t *zp;
771 xvattr_t xva;
5484965a 772 vattr_t *vap = &xva.xva_vattr;
34dc7c2f
BB
773 int error;
774 void *start;
775
776 xva_init(&xva);
777 if (byteswap) {
778 byteswap_uint64_array(lr, sizeof (*lr));
779
5484965a 780 if ((lr->lr_mask & ATTR_XVATTR) &&
633e8030 781 zsb->z_version >= ZPL_VERSION_INITIAL)
34dc7c2f
BB
782 zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
783 }
784
633e8030 785 if ((error = zfs_zget(zsb, lr->lr_foid, &zp)) != 0)
34dc7c2f 786 return (error);
34dc7c2f 787
5484965a
BB
788 zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode,
789 lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
34dc7c2f 790
5484965a
BB
791 vap->va_size = lr->lr_size;
792 ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime);
793 ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime);
34dc7c2f
BB
794
795 /*
796 * Fill in xvattr_t portions if necessary.
797 */
798
799 start = (lr_setattr_t *)(lr + 1);
5484965a 800 if (vap->va_mask & ATTR_XVATTR) {
34dc7c2f
BB
801 zfs_replay_xvattr((lr_attr_t *)start, &xva);
802 start = (caddr_t)start +
803 ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize);
804 } else
5484965a 805 xva.xva_vattr.va_mask &= ~ATTR_XVATTR;
34dc7c2f 806
633e8030 807 zsb->z_fuid_replay = zfs_replay_fuid_domain(start, &start,
34dc7c2f
BB
808 lr->lr_uid, lr->lr_gid);
809
5484965a 810 error = zfs_setattr(ZTOI(zp), vap, 0, kcred);
34dc7c2f 811
633e8030
BB
812 zfs_fuid_info_free(zsb->z_fuid_replay);
813 zsb->z_fuid_replay = NULL;
814 iput(ZTOI(zp));
34dc7c2f
BB
815
816 return (error);
817}
818
819static int
633e8030 820zfs_replay_acl_v0(zfs_sb_t *zsb, lr_acl_v0_t *lr, boolean_t byteswap)
34dc7c2f
BB
821{
822 ace_t *ace = (ace_t *)(lr + 1); /* ace array follows lr_acl_t */
823 vsecattr_t vsa;
824 znode_t *zp;
825 int error;
826
827 if (byteswap) {
828 byteswap_uint64_array(lr, sizeof (*lr));
829 zfs_oldace_byteswap(ace, lr->lr_aclcnt);
830 }
831
633e8030 832 if ((error = zfs_zget(zsb, lr->lr_foid, &zp)) != 0)
34dc7c2f 833 return (error);
34dc7c2f
BB
834
835 bzero(&vsa, sizeof (vsa));
836 vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
837 vsa.vsa_aclcnt = lr->lr_aclcnt;
b128c09f
BB
838 vsa.vsa_aclentsz = sizeof (ace_t) * vsa.vsa_aclcnt;
839 vsa.vsa_aclflags = 0;
34dc7c2f
BB
840 vsa.vsa_aclentp = ace;
841
633e8030 842 error = zfs_setsecattr(ZTOI(zp), &vsa, 0, kcred);
34dc7c2f 843
633e8030 844 iput(ZTOI(zp));
34dc7c2f
BB
845
846 return (error);
847}
848
849/*
850 * Replaying ACLs is complicated by FUID support.
851 * The log record may contain some optional data
852 * to be used for replaying FUID's. These pieces
853 * are the actual FUIDs that were created initially.
854 * The FUID table index may no longer be valid and
855 * during zfs_create() a new index may be assigned.
856 * Because of this the log will contain the original
857 * doman+rid in order to create a new FUID.
858 *
859 * The individual ACEs may contain an ephemeral uid/gid which is no
860 * longer valid and will need to be replaced with an actual FUID.
861 *
862 */
863static int
633e8030 864zfs_replay_acl(zfs_sb_t *zsb, lr_acl_t *lr, boolean_t byteswap)
34dc7c2f
BB
865{
866 ace_t *ace = (ace_t *)(lr + 1);
867 vsecattr_t vsa;
868 znode_t *zp;
869 int error;
870
871 if (byteswap) {
872 byteswap_uint64_array(lr, sizeof (*lr));
873 zfs_ace_byteswap(ace, lr->lr_acl_bytes, B_FALSE);
874 if (lr->lr_fuidcnt) {
875 byteswap_uint64_array((caddr_t)ace +
876 ZIL_ACE_LENGTH(lr->lr_acl_bytes),
877 lr->lr_fuidcnt * sizeof (uint64_t));
878 }
879 }
880
633e8030 881 if ((error = zfs_zget(zsb, lr->lr_foid, &zp)) != 0)
34dc7c2f 882 return (error);
34dc7c2f
BB
883
884 bzero(&vsa, sizeof (vsa));
885 vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
886 vsa.vsa_aclcnt = lr->lr_aclcnt;
887 vsa.vsa_aclentp = ace;
888 vsa.vsa_aclentsz = lr->lr_acl_bytes;
889 vsa.vsa_aclflags = lr->lr_acl_flags;
890
891 if (lr->lr_fuidcnt) {
892 void *fuidstart = (caddr_t)ace +
893 ZIL_ACE_LENGTH(lr->lr_acl_bytes);
894
633e8030 895 zsb->z_fuid_replay =
34dc7c2f
BB
896 zfs_replay_fuids(fuidstart, &fuidstart,
897 lr->lr_fuidcnt, lr->lr_domcnt, 0, 0);
898 }
899
633e8030 900 error = zfs_setsecattr(ZTOI(zp), &vsa, 0, kcred);
34dc7c2f 901
633e8030
BB
902 if (zsb->z_fuid_replay)
903 zfs_fuid_info_free(zsb->z_fuid_replay);
34dc7c2f 904
633e8030
BB
905 zsb->z_fuid_replay = NULL;
906 iput(ZTOI(zp));
34dc7c2f
BB
907
908 return (error);
909}
910
911/*
912 * Callback vectors for replaying records
913 */
914zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
633e8030
BB
915 (zil_replay_func_t *)zfs_replay_error, /* no such type */
916 (zil_replay_func_t *)zfs_replay_create, /* TX_CREATE */
917 (zil_replay_func_t *)zfs_replay_create, /* TX_MKDIR */
918 (zil_replay_func_t *)zfs_replay_create, /* TX_MKXATTR */
919 (zil_replay_func_t *)zfs_replay_create, /* TX_SYMLINK */
920 (zil_replay_func_t *)zfs_replay_remove, /* TX_REMOVE */
921 (zil_replay_func_t *)zfs_replay_remove, /* TX_RMDIR */
922 (zil_replay_func_t *)zfs_replay_link, /* TX_LINK */
923 (zil_replay_func_t *)zfs_replay_rename, /* TX_RENAME */
924 (zil_replay_func_t *)zfs_replay_write, /* TX_WRITE */
925 (zil_replay_func_t *)zfs_replay_truncate, /* TX_TRUNCATE */
926 (zil_replay_func_t *)zfs_replay_setattr, /* TX_SETATTR */
927 (zil_replay_func_t *)zfs_replay_acl_v0, /* TX_ACL_V0 */
928 (zil_replay_func_t *)zfs_replay_acl, /* TX_ACL */
929 (zil_replay_func_t *)zfs_replay_create_acl, /* TX_CREATE_ACL */
930 (zil_replay_func_t *)zfs_replay_create, /* TX_CREATE_ATTR */
931 (zil_replay_func_t *)zfs_replay_create_acl, /* TX_CREATE_ACL_ATTR */
932 (zil_replay_func_t *)zfs_replay_create_acl, /* TX_MKDIR_ACL */
933 (zil_replay_func_t *)zfs_replay_create, /* TX_MKDIR_ATTR */
934 (zil_replay_func_t *)zfs_replay_create_acl, /* TX_MKDIR_ACL_ATTR */
935 (zil_replay_func_t *)zfs_replay_write2, /* TX_WRITE2 */
34dc7c2f 936};