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