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