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) 2012 Cyril Plisko. All rights reserved.
24 * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
27 #include <sys/types.h>
28 #include <sys/param.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>
41 #include <sys/zfs_vnops.h>
44 #include <sys/byteorder.h>
47 #include <sys/atomic.h>
52 * NB: FreeBSD expects to be able to do vnode locking in lookup and
53 * hold the locks across all subsequent VOPs until vput is called.
54 * This means that its zfs vnops routines can't do any internal locking.
55 * In order to have the same contract as the Linux vnops there would
56 * needed to be duplicate locked vnops. If the vnops were used more widely
57 * in common code this would likely be preferable. However, currently
58 * this is the only file where this is the case.
62 * Functions to replay ZFS intent log (ZIL) records
63 * The functions are called through a function vector (zfs_replay_vector)
64 * which is indexed by the transaction type.
68 zfs_init_vattr(vattr_t
*vap
, uint64_t mask
, uint64_t mode
,
69 uint64_t uid
, uint64_t gid
, uint64_t rdev
, uint64_t nodeid
)
71 bzero(vap
, sizeof (*vap
));
72 vap
->va_mask
= (uint_t
)mask
;
74 #if defined(__FreeBSD__) || defined(__APPLE__)
75 vap
->va_type
= IFTOVT(mode
);
77 vap
->va_uid
= (uid_t
)(IS_EPHEMERAL(uid
)) ? -1 : uid
;
78 vap
->va_gid
= (gid_t
)(IS_EPHEMERAL(gid
)) ? -1 : gid
;
79 vap
->va_rdev
= zfs_cmpldev(rdev
);
80 vap
->va_nodeid
= nodeid
;
85 zfs_replay_error(void *arg1
, void *arg2
, boolean_t byteswap
)
87 return (SET_ERROR(ENOTSUP
));
91 zfs_replay_xvattr(lr_attr_t
*lrattr
, xvattr_t
*xvap
)
93 xoptattr_t
*xoap
= NULL
;
100 xvap
->xva_vattr
.va_mask
|= ATTR_XVATTR
;
101 if ((xoap
= xva_getxoptattr(xvap
)) == NULL
) {
102 xvap
->xva_vattr
.va_mask
&= ~ATTR_XVATTR
; /* shouldn't happen */
106 ASSERT(lrattr
->lr_attr_masksize
== xvap
->xva_mapsize
);
108 bitmap
= &lrattr
->lr_attr_bitmap
;
109 for (i
= 0; i
!= lrattr
->lr_attr_masksize
; i
++, bitmap
++)
110 xvap
->xva_reqattrmap
[i
] = *bitmap
;
112 attrs
= (uint64_t *)(lrattr
+ lrattr
->lr_attr_masksize
- 1);
114 scanstamp
= (caddr_t
)(crtime
+ 2);
116 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
))
117 xoap
->xoa_hidden
= ((*attrs
& XAT0_HIDDEN
) != 0);
118 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
))
119 xoap
->xoa_system
= ((*attrs
& XAT0_SYSTEM
) != 0);
120 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
))
121 xoap
->xoa_archive
= ((*attrs
& XAT0_ARCHIVE
) != 0);
122 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
))
123 xoap
->xoa_readonly
= ((*attrs
& XAT0_READONLY
) != 0);
124 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
))
125 xoap
->xoa_immutable
= ((*attrs
& XAT0_IMMUTABLE
) != 0);
126 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
))
127 xoap
->xoa_nounlink
= ((*attrs
& XAT0_NOUNLINK
) != 0);
128 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
))
129 xoap
->xoa_appendonly
= ((*attrs
& XAT0_APPENDONLY
) != 0);
130 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
))
131 xoap
->xoa_nodump
= ((*attrs
& XAT0_NODUMP
) != 0);
132 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))
133 xoap
->xoa_opaque
= ((*attrs
& XAT0_OPAQUE
) != 0);
134 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
))
135 xoap
->xoa_av_modified
= ((*attrs
& XAT0_AV_MODIFIED
) != 0);
136 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
))
137 xoap
->xoa_av_quarantined
=
138 ((*attrs
& XAT0_AV_QUARANTINED
) != 0);
139 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
))
140 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, crtime
);
141 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
142 ASSERT(!XVA_ISSET_REQ(xvap
, XAT_PROJID
));
144 bcopy(scanstamp
, xoap
->xoa_av_scanstamp
, AV_SCANSTAMP_SZ
);
145 } else if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
147 * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid
148 * at the same time, so we can share the same space.
150 bcopy(scanstamp
, &xoap
->xoa_projid
, sizeof (uint64_t));
152 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
))
153 xoap
->xoa_reparse
= ((*attrs
& XAT0_REPARSE
) != 0);
154 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
))
155 xoap
->xoa_offline
= ((*attrs
& XAT0_OFFLINE
) != 0);
156 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
))
157 xoap
->xoa_sparse
= ((*attrs
& XAT0_SPARSE
) != 0);
158 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
))
159 xoap
->xoa_projinherit
= ((*attrs
& XAT0_PROJINHERIT
) != 0);
163 zfs_replay_domain_cnt(uint64_t uid
, uint64_t gid
)
169 uid_idx
= FUID_INDEX(uid
);
170 gid_idx
= FUID_INDEX(gid
);
173 if (gid_idx
> 0 && gid_idx
!= uid_idx
)
180 zfs_replay_fuid_domain_common(zfs_fuid_info_t
*fuid_infop
, void *start
,
185 for (i
= 0; i
!= domcnt
; i
++) {
186 fuid_infop
->z_domain_table
[i
] = start
;
187 start
= (caddr_t
)start
+ strlen(start
) + 1;
194 * Set the uid/gid in the fuid_info structure.
197 zfs_replay_fuid_ugid(zfs_fuid_info_t
*fuid_infop
, uint64_t uid
, uint64_t gid
)
200 * If owner or group are log specific FUIDs then slurp up
201 * domain information and build zfs_fuid_info_t
203 if (IS_EPHEMERAL(uid
))
204 fuid_infop
->z_fuid_owner
= uid
;
206 if (IS_EPHEMERAL(gid
))
207 fuid_infop
->z_fuid_group
= gid
;
211 * Load fuid domains into fuid_info_t
213 static zfs_fuid_info_t
*
214 zfs_replay_fuid_domain(void *buf
, void **end
, uint64_t uid
, uint64_t gid
)
218 zfs_fuid_info_t
*fuid_infop
;
220 fuid_infop
= zfs_fuid_info_alloc();
222 domcnt
= zfs_replay_domain_cnt(uid
, gid
);
227 fuid_infop
->z_domain_table
=
228 kmem_zalloc(domcnt
* sizeof (char *), KM_SLEEP
);
230 zfs_replay_fuid_ugid(fuid_infop
, uid
, gid
);
232 fuid_infop
->z_domain_cnt
= domcnt
;
233 *end
= zfs_replay_fuid_domain_common(fuid_infop
, buf
, domcnt
);
238 * load zfs_fuid_t's and fuid_domains into fuid_info_t
240 static zfs_fuid_info_t
*
241 zfs_replay_fuids(void *start
, void **end
, int idcnt
, int domcnt
, uint64_t uid
,
244 uint64_t *log_fuid
= (uint64_t *)start
;
245 zfs_fuid_info_t
*fuid_infop
;
248 fuid_infop
= zfs_fuid_info_alloc();
249 fuid_infop
->z_domain_cnt
= domcnt
;
251 fuid_infop
->z_domain_table
=
252 kmem_zalloc(domcnt
* sizeof (char *), KM_SLEEP
);
254 for (i
= 0; i
!= idcnt
; i
++) {
257 zfuid
= kmem_alloc(sizeof (zfs_fuid_t
), KM_SLEEP
);
258 zfuid
->z_logfuid
= *log_fuid
;
261 list_insert_tail(&fuid_infop
->z_fuids
, zfuid
);
265 zfs_replay_fuid_ugid(fuid_infop
, uid
, gid
);
267 *end
= zfs_replay_fuid_domain_common(fuid_infop
, log_fuid
, domcnt
);
272 zfs_replay_swap_attrs(lr_attr_t
*lrattr
)
274 /* swap the lr_attr structure */
275 byteswap_uint32_array(lrattr
, sizeof (*lrattr
));
276 /* swap the bitmap */
277 byteswap_uint32_array(lrattr
+ 1, (lrattr
->lr_attr_masksize
- 1) *
279 /* swap the attributes, create time + 64 bit word for attributes */
280 byteswap_uint64_array((caddr_t
)(lrattr
+ 1) + (sizeof (uint32_t) *
281 (lrattr
->lr_attr_masksize
- 1)), 3 * sizeof (uint64_t));
285 * Replay file create with optional ACL, xvattr information as well
286 * as option FUID information.
289 zfs_replay_create_acl(void *arg1
, void *arg2
, boolean_t byteswap
)
291 zfsvfs_t
*zfsvfs
= arg1
;
292 lr_acl_create_t
*lracl
= arg2
;
293 char *name
= NULL
; /* location determined later */
294 lr_create_t
*lr
= (lr_create_t
*)lracl
;
299 vsecattr_t vsec
= { 0 };
309 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
311 byteswap_uint64_array(lracl
, sizeof (*lracl
));
312 if (txtype
== TX_CREATE_ACL_ATTR
||
313 txtype
== TX_MKDIR_ACL_ATTR
) {
314 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
315 zfs_replay_swap_attrs(lrattr
);
316 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
319 aclstart
= (caddr_t
)(lracl
+ 1) + xvatlen
;
320 zfs_ace_byteswap(aclstart
, lracl
->lr_acl_bytes
, B_FALSE
);
322 if (lracl
->lr_fuidcnt
) {
323 byteswap_uint64_array((caddr_t
)aclstart
+
324 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
),
325 lracl
->lr_fuidcnt
* sizeof (uint64_t));
329 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
332 objid
= LR_FOID_GET_OBJ(lr
->lr_foid
);
333 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_foid
) << DNODE_SHIFT
;
336 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
337 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, objid
);
340 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
341 * eventually end up in zfs_mknode(), which assigns the object's
342 * creation time, generation number, and dnode size. The generic
343 * zfs_create() has no concept of these attributes, so we smuggle
344 * the values inside the vattr's otherwise unused va_ctime,
345 * va_nblocks, and va_fsid fields.
347 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
348 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
349 xva
.xva_vattr
.va_fsid
= dnodesize
;
351 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
355 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
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_CREATE_ACL_ATTR
:
368 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
369 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
370 xva
.xva_vattr
.va_mask
|= ATTR_XVATTR
;
371 zfs_replay_xvattr(lrattr
, &xva
);
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
;
378 if (zfsvfs
->z_fuid_replay
== NULL
) {
379 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
380 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
381 zfsvfs
->z_fuid_replay
=
382 zfs_replay_fuids(fuidstart
,
383 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
384 lr
->lr_uid
, lr
->lr_gid
);
387 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
388 0, 0, &zp
, kcred
, vflg
, &vsec
);
391 aclstart
= (caddr_t
)(lracl
+ 1);
392 fuidstart
= (caddr_t
)aclstart
+
393 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
394 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
395 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
396 lr
->lr_uid
, lr
->lr_gid
);
398 case TX_MKDIR_ACL_ATTR
:
400 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
401 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
402 zfs_replay_xvattr(lrattr
, &xva
);
404 vsec
.vsa_mask
= VSA_ACE
| VSA_ACE_ACLFLAGS
;
405 vsec
.vsa_aclentp
= (caddr_t
)(lracl
+ 1) + xvatlen
;
406 vsec
.vsa_aclcnt
= lracl
->lr_aclcnt
;
407 vsec
.vsa_aclentsz
= lracl
->lr_acl_bytes
;
408 vsec
.vsa_aclflags
= lracl
->lr_acl_flags
;
409 if (zfsvfs
->z_fuid_replay
== NULL
) {
410 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
411 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
412 zfsvfs
->z_fuid_replay
=
413 zfs_replay_fuids(fuidstart
,
414 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
415 lr
->lr_uid
, lr
->lr_gid
);
417 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
418 &zp
, kcred
, vflg
, &vsec
);
421 error
= SET_ERROR(ENOTSUP
);
425 if (error
== 0 && zp
!= NULL
) {
427 VOP_UNLOCK1(ZTOV(zp
));
433 if (zfsvfs
->z_fuid_replay
)
434 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
435 zfsvfs
->z_fuid_replay
= NULL
;
441 zfs_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
443 zfsvfs_t
*zfsvfs
= arg1
;
444 lr_create_t
*lr
= arg2
;
445 char *name
= NULL
; /* location determined later */
446 char *link
; /* symlink content follows name */
451 size_t lrsize
= sizeof (lr_create_t
);
460 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
462 byteswap_uint64_array(lr
, sizeof (*lr
));
463 if (txtype
== TX_CREATE_ATTR
|| txtype
== TX_MKDIR_ATTR
)
464 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
468 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
471 objid
= LR_FOID_GET_OBJ(lr
->lr_foid
);
472 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_foid
) << DNODE_SHIFT
;
475 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
476 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, objid
);
479 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
480 * eventually end up in zfs_mknode(), which assigns the object's
481 * creation time, generation number, and dnode slot count. The
482 * generic zfs_create() has no concept of these attributes, so
483 * we smuggle the values inside the vattr's otherwise unused
484 * va_ctime, va_nblocks, and va_fsid fields.
486 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
487 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
488 xva
.xva_vattr
.va_fsid
= dnodesize
;
490 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
494 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
498 * Symlinks don't have fuid info, and CIFS never creates
501 * The _ATTR versions will grab the fuid info in their subcases.
503 if ((int)lr
->lr_common
.lrc_txtype
!= TX_SYMLINK
&&
504 (int)lr
->lr_common
.lrc_txtype
!= TX_MKDIR_ATTR
&&
505 (int)lr
->lr_common
.lrc_txtype
!= TX_CREATE_ATTR
) {
507 zfsvfs
->z_fuid_replay
=
508 zfs_replay_fuid_domain(start
, &start
,
509 lr
->lr_uid
, lr
->lr_gid
);
514 lrattr
= (lr_attr_t
*)(caddr_t
)(lr
+ 1);
515 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
516 zfs_replay_xvattr((lr_attr_t
*)((caddr_t
)lr
+ lrsize
), &xva
);
517 start
= (caddr_t
)(lr
+ 1) + xvatlen
;
518 zfsvfs
->z_fuid_replay
=
519 zfs_replay_fuid_domain(start
, &start
,
520 lr
->lr_uid
, lr
->lr_gid
);
521 name
= (char *)start
;
526 name
= (char *)start
;
528 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
529 0, 0, &zp
, kcred
, vflg
, NULL
);
532 lrattr
= (lr_attr_t
*)(caddr_t
)(lr
+ 1);
533 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
534 zfs_replay_xvattr((lr_attr_t
*)((caddr_t
)lr
+ lrsize
), &xva
);
535 start
= (caddr_t
)(lr
+ 1) + xvatlen
;
536 zfsvfs
->z_fuid_replay
=
537 zfs_replay_fuid_domain(start
, &start
,
538 lr
->lr_uid
, lr
->lr_gid
);
539 name
= (char *)start
;
544 name
= (char *)(lr
+ 1);
546 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
547 &zp
, kcred
, vflg
, NULL
);
550 error
= zfs_make_xattrdir(dzp
, &xva
.xva_vattr
, &zp
, kcred
);
553 name
= (char *)(lr
+ 1);
554 link
= name
+ strlen(name
) + 1;
555 error
= zfs_symlink(dzp
, name
, &xva
.xva_vattr
,
556 link
, &zp
, kcred
, vflg
);
559 error
= SET_ERROR(ENOTSUP
);
563 if (error
== 0 && zp
!= NULL
) {
565 VOP_UNLOCK1(ZTOV(zp
));
571 if (zfsvfs
->z_fuid_replay
)
572 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
573 zfsvfs
->z_fuid_replay
= NULL
;
578 zfs_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
580 zfsvfs_t
*zfsvfs
= arg1
;
581 lr_remove_t
*lr
= arg2
;
582 char *name
= (char *)(lr
+ 1); /* name follows lr_remove_t */
588 byteswap_uint64_array(lr
, sizeof (*lr
));
590 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
593 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
596 switch ((int)lr
->lr_common
.lrc_txtype
) {
598 error
= zfs_remove(dzp
, name
, kcred
, vflg
);
601 error
= zfs_rmdir(dzp
, name
, NULL
, kcred
, vflg
);
604 error
= SET_ERROR(ENOTSUP
);
613 zfs_replay_link(void *arg1
, void *arg2
, boolean_t byteswap
)
615 zfsvfs_t
*zfsvfs
= arg1
;
616 lr_link_t
*lr
= arg2
;
617 char *name
= (char *)(lr
+ 1); /* name follows lr_link_t */
623 byteswap_uint64_array(lr
, sizeof (*lr
));
625 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
628 if ((error
= zfs_zget(zfsvfs
, lr
->lr_link_obj
, &zp
)) != 0) {
633 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
636 error
= zfs_link(dzp
, zp
, name
, kcred
, vflg
);
644 zfs_replay_rename(void *arg1
, void *arg2
, boolean_t byteswap
)
646 zfsvfs_t
*zfsvfs
= arg1
;
647 lr_rename_t
*lr
= arg2
;
648 char *sname
= (char *)(lr
+ 1); /* sname and tname follow lr_rename_t */
649 char *tname
= sname
+ strlen(sname
) + 1;
650 znode_t
*sdzp
, *tdzp
;
655 byteswap_uint64_array(lr
, sizeof (*lr
));
657 if ((error
= zfs_zget(zfsvfs
, lr
->lr_sdoid
, &sdzp
)) != 0)
660 if ((error
= zfs_zget(zfsvfs
, lr
->lr_tdoid
, &tdzp
)) != 0) {
665 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
668 error
= zfs_rename(sdzp
, sname
, tdzp
, tname
, kcred
, vflg
);
676 zfs_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
678 zfsvfs_t
*zfsvfs
= arg1
;
679 lr_write_t
*lr
= arg2
;
680 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
683 uint64_t eod
, offset
, length
;
686 byteswap_uint64_array(lr
, sizeof (*lr
));
688 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0) {
690 * As we can log writes out of order, it's possible the
691 * file has been removed. In this case just drop the write
692 * and return success.
699 offset
= lr
->lr_offset
;
700 length
= lr
->lr_length
;
701 eod
= offset
+ length
; /* end of data for this write */
704 * This may be a write from a dmu_sync() for a whole block,
705 * and may extend beyond the current end of the file.
706 * We can't just replay what was written for this TX_WRITE as
707 * a future TX_WRITE2 may extend the eof and the data for that
708 * write needs to be there. So we write the whole block and
709 * reduce the eof. This needs to be done within the single dmu
710 * transaction created within vn_rdwr -> zfs_write. So a possible
711 * new end of file is passed through in zfsvfs->z_replay_eof
714 zfsvfs
->z_replay_eof
= 0; /* 0 means don't change end of file */
716 /* If it's a dmu_sync() block, write the whole block */
717 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
718 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
719 if (length
< blocksize
) {
720 offset
-= offset
% blocksize
;
723 if (zp
->z_size
< eod
)
724 zfsvfs
->z_replay_eof
= eod
;
726 error
= zfs_write_simple(zp
, data
, length
, offset
, NULL
);
728 zfsvfs
->z_replay_eof
= 0; /* safety */
734 * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
735 * meaning the pool block is already being synced. So now that we always write
736 * out full blocks, all we have to do is expand the eof if
740 zfs_replay_write2(void *arg1
, void *arg2
, boolean_t byteswap
)
742 zfsvfs_t
*zfsvfs
= arg1
;
743 lr_write_t
*lr
= arg2
;
749 byteswap_uint64_array(lr
, sizeof (*lr
));
751 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
755 end
= lr
->lr_offset
+ lr
->lr_length
;
756 if (end
> zp
->z_size
) {
757 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
760 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
761 error
= dmu_tx_assign(tx
, TXG_WAIT
);
764 if (error
== ERESTART
) {
772 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
773 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
775 /* Ensure the replayed seq is updated */
776 (void) zil_replaying(zfsvfs
->z_log
, tx
);
787 zfs_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
789 zfsvfs_t
*zfsvfs
= arg1
;
790 lr_truncate_t
*lr
= arg2
;
796 byteswap_uint64_array(lr
, sizeof (*lr
));
798 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
801 bzero(&fl
, sizeof (fl
));
803 fl
.l_whence
= SEEK_SET
;
804 fl
.l_start
= lr
->lr_offset
;
805 fl
.l_len
= lr
->lr_length
;
807 error
= zfs_space(zp
, F_FREESP
, &fl
, O_RDWR
| O_LARGEFILE
,
808 lr
->lr_offset
, kcred
);
816 zfs_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
818 zfsvfs_t
*zfsvfs
= arg1
;
819 lr_setattr_t
*lr
= arg2
;
822 vattr_t
*vap
= &xva
.xva_vattr
;
828 byteswap_uint64_array(lr
, sizeof (*lr
));
830 if ((lr
->lr_mask
& ATTR_XVATTR
) &&
831 zfsvfs
->z_version
>= ZPL_VERSION_INITIAL
)
832 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
835 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
838 zfs_init_vattr(vap
, lr
->lr_mask
, lr
->lr_mode
,
839 lr
->lr_uid
, lr
->lr_gid
, 0, lr
->lr_foid
);
841 vap
->va_size
= lr
->lr_size
;
842 ZFS_TIME_DECODE(&vap
->va_atime
, lr
->lr_atime
);
843 ZFS_TIME_DECODE(&vap
->va_mtime
, lr
->lr_mtime
);
844 gethrestime(&vap
->va_ctime
);
845 vap
->va_mask
|= ATTR_CTIME
;
848 * Fill in xvattr_t portions if necessary.
851 start
= (lr_setattr_t
*)(lr
+ 1);
852 if (vap
->va_mask
& ATTR_XVATTR
) {
853 zfs_replay_xvattr((lr_attr_t
*)start
, &xva
);
854 start
= (caddr_t
)start
+
855 ZIL_XVAT_SIZE(((lr_attr_t
*)start
)->lr_attr_masksize
);
857 xva
.xva_vattr
.va_mask
&= ~ATTR_XVATTR
;
859 zfsvfs
->z_fuid_replay
= zfs_replay_fuid_domain(start
, &start
,
860 lr
->lr_uid
, lr
->lr_gid
);
862 error
= zfs_setattr(zp
, vap
, 0, kcred
);
864 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
865 zfsvfs
->z_fuid_replay
= NULL
;
872 zfs_replay_acl_v0(void *arg1
, void *arg2
, boolean_t byteswap
)
874 zfsvfs_t
*zfsvfs
= arg1
;
875 lr_acl_v0_t
*lr
= arg2
;
876 ace_t
*ace
= (ace_t
*)(lr
+ 1); /* ace array follows lr_acl_t */
882 byteswap_uint64_array(lr
, sizeof (*lr
));
883 zfs_oldace_byteswap(ace
, lr
->lr_aclcnt
);
886 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
889 bzero(&vsa
, sizeof (vsa
));
890 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
;
891 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
892 vsa
.vsa_aclentsz
= sizeof (ace_t
) * vsa
.vsa_aclcnt
;
893 vsa
.vsa_aclflags
= 0;
894 vsa
.vsa_aclentp
= ace
;
896 error
= zfs_setsecattr(zp
, &vsa
, 0, kcred
);
904 * Replaying ACLs is complicated by FUID support.
905 * The log record may contain some optional data
906 * to be used for replaying FUID's. These pieces
907 * are the actual FUIDs that were created initially.
908 * The FUID table index may no longer be valid and
909 * during zfs_create() a new index may be assigned.
910 * Because of this the log will contain the original
911 * domain+rid in order to create a new FUID.
913 * The individual ACEs may contain an ephemeral uid/gid which is no
914 * longer valid and will need to be replaced with an actual FUID.
918 zfs_replay_acl(void *arg1
, void *arg2
, boolean_t byteswap
)
920 zfsvfs_t
*zfsvfs
= arg1
;
922 ace_t
*ace
= (ace_t
*)(lr
+ 1);
928 byteswap_uint64_array(lr
, sizeof (*lr
));
929 zfs_ace_byteswap(ace
, lr
->lr_acl_bytes
, B_FALSE
);
930 if (lr
->lr_fuidcnt
) {
931 byteswap_uint64_array((caddr_t
)ace
+
932 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
),
933 lr
->lr_fuidcnt
* sizeof (uint64_t));
937 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
940 bzero(&vsa
, sizeof (vsa
));
941 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
| VSA_ACE_ACLFLAGS
;
942 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
943 vsa
.vsa_aclentp
= ace
;
944 vsa
.vsa_aclentsz
= lr
->lr_acl_bytes
;
945 vsa
.vsa_aclflags
= lr
->lr_acl_flags
;
947 if (lr
->lr_fuidcnt
) {
948 void *fuidstart
= (caddr_t
)ace
+
949 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
);
951 zfsvfs
->z_fuid_replay
=
952 zfs_replay_fuids(fuidstart
, &fuidstart
,
953 lr
->lr_fuidcnt
, lr
->lr_domcnt
, 0, 0);
956 error
= zfs_setsecattr(zp
, &vsa
, 0, kcred
);
958 if (zfsvfs
->z_fuid_replay
)
959 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
961 zfsvfs
->z_fuid_replay
= NULL
;
968 * Callback vectors for replaying records
970 zil_replay_func_t
*zfs_replay_vector
[TX_MAX_TYPE
] = {
971 zfs_replay_error
, /* no such type */
972 zfs_replay_create
, /* TX_CREATE */
973 zfs_replay_create
, /* TX_MKDIR */
974 zfs_replay_create
, /* TX_MKXATTR */
975 zfs_replay_create
, /* TX_SYMLINK */
976 zfs_replay_remove
, /* TX_REMOVE */
977 zfs_replay_remove
, /* TX_RMDIR */
978 zfs_replay_link
, /* TX_LINK */
979 zfs_replay_rename
, /* TX_RENAME */
980 zfs_replay_write
, /* TX_WRITE */
981 zfs_replay_truncate
, /* TX_TRUNCATE */
982 zfs_replay_setattr
, /* TX_SETATTR */
983 zfs_replay_acl_v0
, /* TX_ACL_V0 */
984 zfs_replay_acl
, /* TX_ACL */
985 zfs_replay_create_acl
, /* TX_CREATE_ACL */
986 zfs_replay_create
, /* TX_CREATE_ATTR */
987 zfs_replay_create_acl
, /* TX_CREATE_ACL_ATTR */
988 zfs_replay_create_acl
, /* TX_MKDIR_ACL */
989 zfs_replay_create
, /* TX_MKDIR_ATTR */
990 zfs_replay_create_acl
, /* TX_MKDIR_ACL_ATTR */
991 zfs_replay_write2
, /* TX_WRITE2 */