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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
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>
32 #include <sys/thread.h>
34 #include <sys/fcntl.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>
43 #include <sys/byteorder.h>
47 #include <sys/atomic.h>
51 * Functions to replay ZFS intent log (ZIL) records
52 * The functions are called through a function vector (zfs_replay_vector)
53 * which is indexed by the transaction type.
57 zfs_init_vattr(vattr_t
*vap
, uint64_t mask
, uint64_t mode
,
58 uint64_t uid
, uint64_t gid
, uint64_t rdev
, uint64_t nodeid
)
60 bzero(vap
, sizeof (*vap
));
61 vap
->va_mask
= (uint_t
)mask
;
62 vap
->va_type
= IFTOVT(mode
);
63 vap
->va_mode
= mode
& MODEMASK
;
64 vap
->va_uid
= (uid_t
)(IS_EPHEMERAL(uid
)) ? -1 : uid
;
65 vap
->va_gid
= (gid_t
)(IS_EPHEMERAL(gid
)) ? -1 : gid
;
66 vap
->va_rdev
= zfs_cmpldev(rdev
);
67 vap
->va_nodeid
= nodeid
;
72 zfs_replay_error(zfsvfs_t
*zfsvfs
, lr_t
*lr
, boolean_t byteswap
)
74 return (SET_ERROR(ENOTSUP
));
78 zfs_replay_xvattr(lr_attr_t
*lrattr
, xvattr_t
*xvap
)
80 xoptattr_t
*xoap
= NULL
;
87 xvap
->xva_vattr
.va_mask
|= AT_XVATTR
;
88 if ((xoap
= xva_getxoptattr(xvap
)) == NULL
) {
89 xvap
->xva_vattr
.va_mask
&= ~AT_XVATTR
; /* shouldn't happen */
93 ASSERT(lrattr
->lr_attr_masksize
== xvap
->xva_mapsize
);
95 bitmap
= &lrattr
->lr_attr_bitmap
;
96 for (i
= 0; i
!= lrattr
->lr_attr_masksize
; i
++, bitmap
++)
97 xvap
->xva_reqattrmap
[i
] = *bitmap
;
99 attrs
= (uint64_t *)(lrattr
+ lrattr
->lr_attr_masksize
- 1);
101 scanstamp
= (caddr_t
)(crtime
+ 2);
103 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
))
104 xoap
->xoa_hidden
= ((*attrs
& XAT0_HIDDEN
) != 0);
105 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
))
106 xoap
->xoa_system
= ((*attrs
& XAT0_SYSTEM
) != 0);
107 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
))
108 xoap
->xoa_archive
= ((*attrs
& XAT0_ARCHIVE
) != 0);
109 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
))
110 xoap
->xoa_readonly
= ((*attrs
& XAT0_READONLY
) != 0);
111 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
))
112 xoap
->xoa_immutable
= ((*attrs
& XAT0_IMMUTABLE
) != 0);
113 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
))
114 xoap
->xoa_nounlink
= ((*attrs
& XAT0_NOUNLINK
) != 0);
115 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
))
116 xoap
->xoa_appendonly
= ((*attrs
& XAT0_APPENDONLY
) != 0);
117 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
))
118 xoap
->xoa_nodump
= ((*attrs
& XAT0_NODUMP
) != 0);
119 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))
120 xoap
->xoa_opaque
= ((*attrs
& XAT0_OPAQUE
) != 0);
121 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
))
122 xoap
->xoa_av_modified
= ((*attrs
& XAT0_AV_MODIFIED
) != 0);
123 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
))
124 xoap
->xoa_av_quarantined
=
125 ((*attrs
& XAT0_AV_QUARANTINED
) != 0);
126 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
))
127 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, crtime
);
128 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
129 bcopy(scanstamp
, xoap
->xoa_av_scanstamp
, AV_SCANSTAMP_SZ
);
130 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
))
131 xoap
->xoa_reparse
= ((*attrs
& XAT0_REPARSE
) != 0);
132 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
))
133 xoap
->xoa_offline
= ((*attrs
& XAT0_OFFLINE
) != 0);
134 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
))
135 xoap
->xoa_sparse
= ((*attrs
& XAT0_SPARSE
) != 0);
139 zfs_replay_domain_cnt(uint64_t uid
, uint64_t gid
)
145 uid_idx
= FUID_INDEX(uid
);
146 gid_idx
= FUID_INDEX(gid
);
149 if (gid_idx
> 0 && gid_idx
!= uid_idx
)
156 zfs_replay_fuid_domain_common(zfs_fuid_info_t
*fuid_infop
, void *start
,
161 for (i
= 0; i
!= domcnt
; i
++) {
162 fuid_infop
->z_domain_table
[i
] = start
;
163 start
= (caddr_t
)start
+ strlen(start
) + 1;
170 * Set the uid/gid in the fuid_info structure.
173 zfs_replay_fuid_ugid(zfs_fuid_info_t
*fuid_infop
, uint64_t uid
, uint64_t gid
)
176 * If owner or group are log specific FUIDs then slurp up
177 * domain information and build zfs_fuid_info_t
179 if (IS_EPHEMERAL(uid
))
180 fuid_infop
->z_fuid_owner
= uid
;
182 if (IS_EPHEMERAL(gid
))
183 fuid_infop
->z_fuid_group
= gid
;
187 * Load fuid domains into fuid_info_t
189 static zfs_fuid_info_t
*
190 zfs_replay_fuid_domain(void *buf
, void **end
, uint64_t uid
, uint64_t gid
)
194 zfs_fuid_info_t
*fuid_infop
;
196 fuid_infop
= zfs_fuid_info_alloc();
198 domcnt
= zfs_replay_domain_cnt(uid
, gid
);
203 fuid_infop
->z_domain_table
=
204 kmem_zalloc(domcnt
* sizeof (char **), KM_SLEEP
);
206 zfs_replay_fuid_ugid(fuid_infop
, uid
, gid
);
208 fuid_infop
->z_domain_cnt
= domcnt
;
209 *end
= zfs_replay_fuid_domain_common(fuid_infop
, buf
, domcnt
);
214 * load zfs_fuid_t's and fuid_domains into fuid_info_t
216 static zfs_fuid_info_t
*
217 zfs_replay_fuids(void *start
, void **end
, int idcnt
, int domcnt
, uint64_t uid
,
220 uint64_t *log_fuid
= (uint64_t *)start
;
221 zfs_fuid_info_t
*fuid_infop
;
224 fuid_infop
= zfs_fuid_info_alloc();
225 fuid_infop
->z_domain_cnt
= domcnt
;
227 fuid_infop
->z_domain_table
=
228 kmem_zalloc(domcnt
* sizeof (char **), KM_SLEEP
);
230 for (i
= 0; i
!= idcnt
; i
++) {
233 zfuid
= kmem_alloc(sizeof (zfs_fuid_t
), KM_SLEEP
);
234 zfuid
->z_logfuid
= *log_fuid
;
237 list_insert_tail(&fuid_infop
->z_fuids
, zfuid
);
241 zfs_replay_fuid_ugid(fuid_infop
, uid
, gid
);
243 *end
= zfs_replay_fuid_domain_common(fuid_infop
, log_fuid
, domcnt
);
248 zfs_replay_swap_attrs(lr_attr_t
*lrattr
)
250 /* swap the lr_attr structure */
251 byteswap_uint32_array(lrattr
, sizeof (*lrattr
));
252 /* swap the bitmap */
253 byteswap_uint32_array(lrattr
+ 1, (lrattr
->lr_attr_masksize
- 1) *
255 /* swap the attributes, create time + 64 bit word for attributes */
256 byteswap_uint64_array((caddr_t
)(lrattr
+ 1) + (sizeof (uint32_t) *
257 (lrattr
->lr_attr_masksize
- 1)), 3 * sizeof (uint64_t));
261 * Replay file create with optional ACL, xvattr information as well
262 * as option FUID information.
265 zfs_replay_create_acl(zfsvfs_t
*zfsvfs
,
266 lr_acl_create_t
*lracl
, boolean_t byteswap
)
268 char *name
= NULL
; /* location determined later */
269 lr_create_t
*lr
= (lr_create_t
*)lracl
;
274 vsecattr_t vsec
= { 0 };
282 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
284 byteswap_uint64_array(lracl
, sizeof (*lracl
));
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
);
292 aclstart
= (caddr_t
)(lracl
+ 1) + xvatlen
;
293 zfs_ace_byteswap(aclstart
, lracl
->lr_acl_bytes
, B_FALSE
);
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));
302 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
306 zfs_init_vattr(&xva
.xva_vattr
, AT_TYPE
| AT_MODE
| AT_UID
| AT_GID
,
307 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, lr
->lr_foid
);
310 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
311 * eventually end up in zfs_mknode(), which assigns the object's
312 * creation time and generation number. The generic VOP_CREATE()
313 * doesn't have either concept, so we smuggle the values inside
314 * the vattr's otherwise unused va_ctime and va_nblocks fields.
316 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
317 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
319 error
= dmu_object_info(zfsvfs
->z_os
, lr
->lr_foid
, NULL
);
323 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
327 aclstart
= (caddr_t
)(lracl
+ 1);
328 fuidstart
= (caddr_t
)aclstart
+
329 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
330 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
331 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
332 lr
->lr_uid
, lr
->lr_gid
);
334 case TX_CREATE_ACL_ATTR
:
336 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
337 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
338 xva
.xva_vattr
.va_mask
|= AT_XVATTR
;
339 zfs_replay_xvattr(lrattr
, &xva
);
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
;
346 if (zfsvfs
->z_fuid_replay
== NULL
) {
347 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
348 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
349 zfsvfs
->z_fuid_replay
=
350 zfs_replay_fuids(fuidstart
,
351 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
352 lr
->lr_uid
, lr
->lr_gid
);
355 error
= VOP_CREATE(ZTOV(dzp
), name
, &xva
.xva_vattr
,
356 0, 0, &vp
, kcred
, vflg
, NULL
, &vsec
);
359 aclstart
= (caddr_t
)(lracl
+ 1);
360 fuidstart
= (caddr_t
)aclstart
+
361 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
362 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
363 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
364 lr
->lr_uid
, lr
->lr_gid
);
366 case TX_MKDIR_ACL_ATTR
:
368 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
369 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
370 zfs_replay_xvattr(lrattr
, &xva
);
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
;
377 if (zfsvfs
->z_fuid_replay
== NULL
) {
378 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
379 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
380 zfsvfs
->z_fuid_replay
=
381 zfs_replay_fuids(fuidstart
,
382 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
383 lr
->lr_uid
, lr
->lr_gid
);
385 error
= VOP_MKDIR(ZTOV(dzp
), name
, &xva
.xva_vattr
,
386 &vp
, kcred
, NULL
, vflg
, &vsec
);
389 error
= SET_ERROR(ENOTSUP
);
393 if (error
== 0 && vp
!= NULL
)
398 if (zfsvfs
->z_fuid_replay
)
399 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
400 zfsvfs
->z_fuid_replay
= NULL
;
406 zfs_replay_create(zfsvfs_t
*zfsvfs
, lr_create_t
*lr
, boolean_t byteswap
)
408 char *name
= NULL
; /* location determined later */
409 char *link
; /* symlink content follows name */
414 size_t lrsize
= sizeof (lr_create_t
);
421 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
423 byteswap_uint64_array(lr
, sizeof (*lr
));
424 if (txtype
== TX_CREATE_ATTR
|| txtype
== TX_MKDIR_ATTR
)
425 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
429 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
433 zfs_init_vattr(&xva
.xva_vattr
, AT_TYPE
| AT_MODE
| AT_UID
| AT_GID
,
434 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, lr
->lr_foid
);
437 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
438 * eventually end up in zfs_mknode(), which assigns the object's
439 * creation time and generation number. The generic VOP_CREATE()
440 * doesn't have either concept, so we smuggle the values inside
441 * the vattr's otherwise unused va_ctime and va_nblocks fields.
443 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
444 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
446 error
= dmu_object_info(zfsvfs
->z_os
, lr
->lr_foid
, NULL
);
450 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
454 * Symlinks don't have fuid info, and CIFS never creates
457 * The _ATTR versions will grab the fuid info in their subcases.
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
) {
463 zfsvfs
->z_fuid_replay
=
464 zfs_replay_fuid_domain(start
, &start
,
465 lr
->lr_uid
, lr
->lr_gid
);
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
;
474 zfsvfs
->z_fuid_replay
=
475 zfs_replay_fuid_domain(start
, &start
,
476 lr
->lr_uid
, lr
->lr_gid
);
477 name
= (char *)start
;
482 name
= (char *)start
;
484 error
= VOP_CREATE(ZTOV(dzp
), name
, &xva
.xva_vattr
,
485 0, 0, &vp
, kcred
, vflg
, NULL
, NULL
);
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
;
492 zfsvfs
->z_fuid_replay
=
493 zfs_replay_fuid_domain(start
, &start
,
494 lr
->lr_uid
, lr
->lr_gid
);
495 name
= (char *)start
;
500 name
= (char *)(lr
+ 1);
502 error
= VOP_MKDIR(ZTOV(dzp
), name
, &xva
.xva_vattr
,
503 &vp
, kcred
, NULL
, vflg
, NULL
);
506 error
= zfs_make_xattrdir(dzp
, &xva
.xva_vattr
, &vp
, kcred
);
509 name
= (char *)(lr
+ 1);
510 link
= name
+ strlen(name
) + 1;
511 error
= VOP_SYMLINK(ZTOV(dzp
), name
, &xva
.xva_vattr
,
512 link
, kcred
, NULL
, vflg
);
515 error
= SET_ERROR(ENOTSUP
);
519 if (error
== 0 && vp
!= NULL
)
524 if (zfsvfs
->z_fuid_replay
)
525 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
526 zfsvfs
->z_fuid_replay
= NULL
;
531 zfs_replay_remove(zfsvfs_t
*zfsvfs
, lr_remove_t
*lr
, boolean_t byteswap
)
533 char *name
= (char *)(lr
+ 1); /* name follows lr_remove_t */
539 byteswap_uint64_array(lr
, sizeof (*lr
));
541 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
544 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
547 switch ((int)lr
->lr_common
.lrc_txtype
) {
549 error
= VOP_REMOVE(ZTOV(dzp
), name
, kcred
, NULL
, vflg
);
552 error
= VOP_RMDIR(ZTOV(dzp
), name
, NULL
, kcred
, NULL
, vflg
);
555 error
= SET_ERROR(ENOTSUP
);
564 zfs_replay_link(zfsvfs_t
*zfsvfs
, lr_link_t
*lr
, boolean_t byteswap
)
566 char *name
= (char *)(lr
+ 1); /* name follows lr_link_t */
572 byteswap_uint64_array(lr
, sizeof (*lr
));
574 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
577 if ((error
= zfs_zget(zfsvfs
, lr
->lr_link_obj
, &zp
)) != 0) {
582 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
585 error
= VOP_LINK(ZTOV(dzp
), ZTOV(zp
), name
, kcred
, NULL
, vflg
);
594 zfs_replay_rename(zfsvfs_t
*zfsvfs
, lr_rename_t
*lr
, boolean_t byteswap
)
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
;
603 byteswap_uint64_array(lr
, sizeof (*lr
));
605 if ((error
= zfs_zget(zfsvfs
, lr
->lr_sdoid
, &sdzp
)) != 0)
608 if ((error
= zfs_zget(zfsvfs
, lr
->lr_tdoid
, &tdzp
)) != 0) {
613 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
616 error
= VOP_RENAME(ZTOV(sdzp
), sname
, ZTOV(tdzp
), tname
, kcred
,
626 zfs_replay_write(zfsvfs_t
*zfsvfs
, lr_write_t
*lr
, boolean_t byteswap
)
628 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
632 uint64_t eod
, offset
, length
;
635 byteswap_uint64_array(lr
, sizeof (*lr
));
637 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0) {
639 * As we can log writes out of order, it's possible the
640 * file has been removed. In this case just drop the write
641 * and return success.
648 offset
= lr
->lr_offset
;
649 length
= lr
->lr_length
;
650 eod
= offset
+ length
; /* end of data for this write */
653 * This may be a write from a dmu_sync() for a whole block,
654 * and may extend beyond the current end of the file.
655 * We can't just replay what was written for this TX_WRITE as
656 * a future TX_WRITE2 may extend the eof and the data for that
657 * write needs to be there. So we write the whole block and
658 * reduce the eof. This needs to be done within the single dmu
659 * transaction created within vn_rdwr -> zfs_write. So a possible
660 * new end of file is passed through in zfsvfs->z_replay_eof
663 zfsvfs
->z_replay_eof
= 0; /* 0 means don't change end of file */
665 /* If it's a dmu_sync() block, write the whole block */
666 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
667 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
668 if (length
< blocksize
) {
669 offset
-= offset
% blocksize
;
672 if (zp
->z_size
< eod
)
673 zfsvfs
->z_replay_eof
= eod
;
676 error
= vn_rdwr(UIO_WRITE
, ZTOV(zp
), data
, length
, offset
,
677 UIO_SYSSPACE
, 0, RLIM64_INFINITY
, kcred
, &resid
);
680 zfsvfs
->z_replay_eof
= 0; /* safety */
686 * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
687 * meaning the pool block is already being synced. So now that we always write
688 * out full blocks, all we have to do is expand the eof if
692 zfs_replay_write2(zfsvfs_t
*zfsvfs
, lr_write_t
*lr
, boolean_t byteswap
)
699 byteswap_uint64_array(lr
, sizeof (*lr
));
701 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
705 end
= lr
->lr_offset
+ lr
->lr_length
;
706 if (end
> zp
->z_size
) {
707 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
710 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
711 error
= dmu_tx_assign(tx
, TXG_WAIT
);
714 if (error
== ERESTART
) {
722 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
723 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
725 /* Ensure the replayed seq is updated */
726 (void) zil_replaying(zfsvfs
->z_log
, tx
);
737 zfs_replay_truncate(zfsvfs_t
*zfsvfs
, lr_truncate_t
*lr
, boolean_t byteswap
)
744 byteswap_uint64_array(lr
, sizeof (*lr
));
746 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
749 bzero(&fl
, sizeof (fl
));
752 fl
.l_start
= lr
->lr_offset
;
753 fl
.l_len
= lr
->lr_length
;
755 error
= VOP_SPACE(ZTOV(zp
), F_FREESP
, &fl
, FWRITE
| FOFFMAX
,
756 lr
->lr_offset
, kcred
, NULL
);
764 zfs_replay_setattr(zfsvfs_t
*zfsvfs
, lr_setattr_t
*lr
, boolean_t byteswap
)
768 vattr_t
*vap
= &xva
.xva_vattr
;
774 byteswap_uint64_array(lr
, sizeof (*lr
));
776 if ((lr
->lr_mask
& AT_XVATTR
) &&
777 zfsvfs
->z_version
>= ZPL_VERSION_INITIAL
)
778 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
781 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
784 zfs_init_vattr(vap
, lr
->lr_mask
, lr
->lr_mode
,
785 lr
->lr_uid
, lr
->lr_gid
, 0, lr
->lr_foid
);
787 vap
->va_size
= lr
->lr_size
;
788 ZFS_TIME_DECODE(&vap
->va_atime
, lr
->lr_atime
);
789 ZFS_TIME_DECODE(&vap
->va_mtime
, lr
->lr_mtime
);
792 * Fill in xvattr_t portions if necessary.
795 start
= (lr_setattr_t
*)(lr
+ 1);
796 if (vap
->va_mask
& AT_XVATTR
) {
797 zfs_replay_xvattr((lr_attr_t
*)start
, &xva
);
798 start
= (caddr_t
)start
+
799 ZIL_XVAT_SIZE(((lr_attr_t
*)start
)->lr_attr_masksize
);
801 xva
.xva_vattr
.va_mask
&= ~AT_XVATTR
;
803 zfsvfs
->z_fuid_replay
= zfs_replay_fuid_domain(start
, &start
,
804 lr
->lr_uid
, lr
->lr_gid
);
806 error
= VOP_SETATTR(ZTOV(zp
), vap
, 0, kcred
, NULL
);
808 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
809 zfsvfs
->z_fuid_replay
= NULL
;
816 zfs_replay_acl_v0(zfsvfs_t
*zfsvfs
, lr_acl_v0_t
*lr
, boolean_t byteswap
)
818 ace_t
*ace
= (ace_t
*)(lr
+ 1); /* ace array follows lr_acl_t */
824 byteswap_uint64_array(lr
, sizeof (*lr
));
825 zfs_oldace_byteswap(ace
, lr
->lr_aclcnt
);
828 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
831 bzero(&vsa
, sizeof (vsa
));
832 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
;
833 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
834 vsa
.vsa_aclentsz
= sizeof (ace_t
) * vsa
.vsa_aclcnt
;
835 vsa
.vsa_aclflags
= 0;
836 vsa
.vsa_aclentp
= ace
;
838 error
= VOP_SETSECATTR(ZTOV(zp
), &vsa
, 0, kcred
, NULL
);
846 * Replaying ACLs is complicated by FUID support.
847 * The log record may contain some optional data
848 * to be used for replaying FUID's. These pieces
849 * are the actual FUIDs that were created initially.
850 * The FUID table index may no longer be valid and
851 * during zfs_create() a new index may be assigned.
852 * Because of this the log will contain the original
853 * doman+rid in order to create a new FUID.
855 * The individual ACEs may contain an ephemeral uid/gid which is no
856 * longer valid and will need to be replaced with an actual FUID.
860 zfs_replay_acl(zfsvfs_t
*zfsvfs
, lr_acl_t
*lr
, boolean_t byteswap
)
862 ace_t
*ace
= (ace_t
*)(lr
+ 1);
868 byteswap_uint64_array(lr
, sizeof (*lr
));
869 zfs_ace_byteswap(ace
, lr
->lr_acl_bytes
, B_FALSE
);
870 if (lr
->lr_fuidcnt
) {
871 byteswap_uint64_array((caddr_t
)ace
+
872 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
),
873 lr
->lr_fuidcnt
* sizeof (uint64_t));
877 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
880 bzero(&vsa
, sizeof (vsa
));
881 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
| VSA_ACE_ACLFLAGS
;
882 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
883 vsa
.vsa_aclentp
= ace
;
884 vsa
.vsa_aclentsz
= lr
->lr_acl_bytes
;
885 vsa
.vsa_aclflags
= lr
->lr_acl_flags
;
887 if (lr
->lr_fuidcnt
) {
888 void *fuidstart
= (caddr_t
)ace
+
889 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
);
891 zfsvfs
->z_fuid_replay
=
892 zfs_replay_fuids(fuidstart
, &fuidstart
,
893 lr
->lr_fuidcnt
, lr
->lr_domcnt
, 0, 0);
896 error
= VOP_SETSECATTR(ZTOV(zp
), &vsa
, 0, kcred
, NULL
);
898 if (zfsvfs
->z_fuid_replay
)
899 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
901 zfsvfs
->z_fuid_replay
= NULL
;
908 * Callback vectors for replaying records
910 zil_replay_func_t
*zfs_replay_vector
[TX_MAX_TYPE
] = {
911 zfs_replay_error
, /* 0 no such transaction type */
912 zfs_replay_create
, /* TX_CREATE */
913 zfs_replay_create
, /* TX_MKDIR */
914 zfs_replay_create
, /* TX_MKXATTR */
915 zfs_replay_create
, /* TX_SYMLINK */
916 zfs_replay_remove
, /* TX_REMOVE */
917 zfs_replay_remove
, /* TX_RMDIR */
918 zfs_replay_link
, /* TX_LINK */
919 zfs_replay_rename
, /* TX_RENAME */
920 zfs_replay_write
, /* TX_WRITE */
921 zfs_replay_truncate
, /* TX_TRUNCATE */
922 zfs_replay_setattr
, /* TX_SETATTR */
923 zfs_replay_acl_v0
, /* TX_ACL_V0 */
924 zfs_replay_acl
, /* TX_ACL */
925 zfs_replay_create_acl
, /* TX_CREATE_ACL */
926 zfs_replay_create
, /* TX_CREATE_ATTR */
927 zfs_replay_create_acl
, /* TX_CREATE_ACL_ATTR */
928 zfs_replay_create_acl
, /* TX_MKDIR_ACL */
929 zfs_replay_create
, /* TX_MKDIR_ATTR */
930 zfs_replay_create_acl
, /* TX_MKDIR_ACL_ATTR */
931 zfs_replay_write2
, /* TX_WRITE2 */