]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_log.c
ddt: move entry compression into ddt_zap
[mirror_zfs.git] / module / zfs / zfs_log.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
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
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.
b8738257 23 * Copyright (c) 2015, 2018 by Delphix. All rights reserved.
67a1b037 24 * Copyright (c) 2022 by Pawel Jakub Dawidek
34dc7c2f
BB
25 */
26
60101509 27
34dc7c2f
BB
28#include <sys/types.h>
29#include <sys/param.h>
34dc7c2f
BB
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/vfs.h>
36#include <sys/zfs_znode.h>
37#include <sys/zfs_dir.h>
38#include <sys/zil.h>
39#include <sys/zil_impl.h>
40#include <sys/byteorder.h>
41#include <sys/policy.h>
42#include <sys/stat.h>
34dc7c2f
BB
43#include <sys/acl.h>
44#include <sys/dmu.h>
c8bbf7c0 45#include <sys/dbuf.h>
34dc7c2f
BB
46#include <sys/spa.h>
47#include <sys/zfs_fuid.h>
fb5f0bc8
BB
48#include <sys/dsl_dataset.h>
49
34dc7c2f 50/*
fb5f0bc8
BB
51 * These zfs_log_* functions must be called within a dmu tx, in one
52 * of 2 contexts depending on zilog->z_replay:
53 *
54 * Non replay mode
55 * ---------------
56 * We need to record the transaction so that if it is committed to
57 * the Intent Log then it can be replayed. An intent log transaction
58 * structure (itx_t) is allocated and all the information necessary to
59 * possibly replay the transaction is saved in it. The itx is then assigned
60 * a sequence number and inserted in the in-memory list anchored in the zilog.
61 *
62 * Replay mode
63 * -----------
64 * We need to mark the intent log record as replayed in the log header.
65 * This is done in the same transaction as the replay so that they
66 * commit atomically.
34dc7c2f
BB
67 */
68
69int
70zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
71{
5484965a 72 int isxvattr = (vap->va_mask & ATTR_XVATTR);
34dc7c2f
BB
73 switch (type) {
74 case Z_FILE:
75 if (vsecp == NULL && !isxvattr)
76 return (TX_CREATE);
77 if (vsecp && isxvattr)
78 return (TX_CREATE_ACL_ATTR);
79 if (vsecp)
80 return (TX_CREATE_ACL);
81 else
82 return (TX_CREATE_ATTR);
34dc7c2f
BB
83 case Z_DIR:
84 if (vsecp == NULL && !isxvattr)
85 return (TX_MKDIR);
86 if (vsecp && isxvattr)
87 return (TX_MKDIR_ACL_ATTR);
88 if (vsecp)
89 return (TX_MKDIR_ACL);
90 else
91 return (TX_MKDIR_ATTR);
92 case Z_XATTRDIR:
93 return (TX_MKXATTR);
94 }
95 ASSERT(0);
96 return (TX_MAX_TYPE);
97}
98
99/*
100 * build up the log data necessary for logging xvattr_t
101 * First lr_attr_t is initialized. following the lr_attr_t
102 * is the mapsize and attribute bitmap copied from the xvattr_t.
103 * Following the bitmap and bitmapsize two 64 bit words are reserved
104 * for the create time which may be set. Following the create time
105 * records a single 64 bit integer which has the bits to set on
106 * replay for the xvattr.
107 */
108static void
109zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
110{
b0f7dd27 111 xoptattr_t *xoap;
34dc7c2f
BB
112
113 xoap = xva_getxoptattr(xvap);
114 ASSERT(xoap);
115
116 lrattr->lr_attr_masksize = xvap->xva_mapsize;
b0f7dd27
BB
117 uint32_t *bitmap = &lrattr->lr_attr_bitmap;
118 for (int i = 0; i != xvap->xva_mapsize; i++, bitmap++)
34dc7c2f 119 *bitmap = xvap->xva_reqattrmap[i];
34dc7c2f 120
b0f7dd27
BB
121 lr_attr_end_t *end = (lr_attr_end_t *)bitmap;
122 end->lr_attr_attrs = 0;
123 end->lr_attr_crtime[0] = 0;
124 end->lr_attr_crtime[1] = 0;
125 memset(end->lr_attr_scanstamp, 0, AV_SCANSTAMP_SZ);
126
34dc7c2f 127 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
b0f7dd27 128 end->lr_attr_attrs |= (xoap->xoa_readonly == 0) ? 0 :
34dc7c2f
BB
129 XAT0_READONLY;
130 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
b0f7dd27 131 end->lr_attr_attrs |= (xoap->xoa_hidden == 0) ? 0 :
34dc7c2f
BB
132 XAT0_HIDDEN;
133 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
b0f7dd27 134 end->lr_attr_attrs |= (xoap->xoa_system == 0) ? 0 :
34dc7c2f
BB
135 XAT0_SYSTEM;
136 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
b0f7dd27 137 end->lr_attr_attrs |= (xoap->xoa_archive == 0) ? 0 :
34dc7c2f
BB
138 XAT0_ARCHIVE;
139 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
b0f7dd27 140 end->lr_attr_attrs |= (xoap->xoa_immutable == 0) ? 0 :
34dc7c2f
BB
141 XAT0_IMMUTABLE;
142 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
b0f7dd27 143 end->lr_attr_attrs |= (xoap->xoa_nounlink == 0) ? 0 :
34dc7c2f
BB
144 XAT0_NOUNLINK;
145 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
b0f7dd27 146 end->lr_attr_attrs |= (xoap->xoa_appendonly == 0) ? 0 :
34dc7c2f
BB
147 XAT0_APPENDONLY;
148 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
b0f7dd27 149 end->lr_attr_attrs |= (xoap->xoa_opaque == 0) ? 0 :
34dc7c2f
BB
150 XAT0_APPENDONLY;
151 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
b0f7dd27 152 end->lr_attr_attrs |= (xoap->xoa_nodump == 0) ? 0 :
34dc7c2f
BB
153 XAT0_NODUMP;
154 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
b0f7dd27 155 end->lr_attr_attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
34dc7c2f
BB
156 XAT0_AV_QUARANTINED;
157 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
b0f7dd27 158 end->lr_attr_attrs |= (xoap->xoa_av_modified == 0) ? 0 :
34dc7c2f
BB
159 XAT0_AV_MODIFIED;
160 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
b0f7dd27 161 ZFS_TIME_ENCODE(&xoap->xoa_createtime, end->lr_attr_crtime);
9c5167d1
NF
162 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
163 ASSERT(!XVA_ISSET_REQ(xvap, XAT_PROJID));
164
b0f7dd27
BB
165 memcpy(end->lr_attr_scanstamp, xoap->xoa_av_scanstamp,
166 AV_SCANSTAMP_SZ);
9c5167d1
NF
167 } else if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
168 /*
169 * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid
170 * at the same time, so we can share the same space.
171 */
b0f7dd27
BB
172 memcpy(end->lr_attr_scanstamp, &xoap->xoa_projid,
173 sizeof (uint64_t));
9c5167d1 174 }
428870ff 175 if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
b0f7dd27 176 end->lr_attr_attrs |= (xoap->xoa_reparse == 0) ? 0 :
428870ff 177 XAT0_REPARSE;
572e2857 178 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
b0f7dd27 179 end->lr_attr_attrs |= (xoap->xoa_offline == 0) ? 0 :
572e2857
BB
180 XAT0_OFFLINE;
181 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
b0f7dd27 182 end->lr_attr_attrs |= (xoap->xoa_sparse == 0) ? 0 :
572e2857 183 XAT0_SPARSE;
9c5167d1 184 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT))
b0f7dd27 185 end->lr_attr_attrs |= (xoap->xoa_projinherit == 0) ? 0 :
9c5167d1 186 XAT0_PROJINHERIT;
34dc7c2f
BB
187}
188
189static void *
190zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
191{
192 zfs_fuid_t *zfuid;
193 uint64_t *fuidloc = start;
194
195 /* First copy in the ACE FUIDs */
196 for (zfuid = list_head(&fuidp->z_fuids); zfuid;
197 zfuid = list_next(&fuidp->z_fuids, zfuid)) {
198 *fuidloc++ = zfuid->z_logfuid;
199 }
200 return (fuidloc);
201}
202
203
204static void *
205zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
206{
207 zfs_fuid_domain_t *zdomain;
208
209 /* now copy in the domain info, if any */
210 if (fuidp->z_domain_str_sz != 0) {
211 for (zdomain = list_head(&fuidp->z_domains); zdomain;
212 zdomain = list_next(&fuidp->z_domains, zdomain)) {
861166b0 213 memcpy(start, zdomain->z_domain,
34dc7c2f
BB
214 strlen(zdomain->z_domain) + 1);
215 start = (caddr_t)start +
216 strlen(zdomain->z_domain) + 1;
217 }
218 }
219 return (start);
220}
221
98701490
CC
222/*
223 * If zp is an xattr node, check whether the xattr owner is unlinked.
224 * We don't want to log anything if the owner is unlinked.
225 */
226static int
227zfs_xattr_owner_unlinked(znode_t *zp)
228{
229 int unlinked = 0;
230 znode_t *dzp;
cf118ae8
MM
231#ifdef __FreeBSD__
232 znode_t *tzp = zp;
657ce253 233
ba434b18
MM
234 /*
235 * zrele drops the vnode lock which violates the VOP locking contract
236 * on FreeBSD. See comment at the top of zfs_replay.c for more detail.
237 */
cf118ae8
MM
238 /*
239 * if zp is XATTR node, keep walking up via z_xattr_parent until we
240 * get the owner
241 */
242 while (tzp->z_pflags & ZFS_XATTR) {
243 ASSERT3U(zp->z_xattr_parent, !=, 0);
244 if (zfs_zget(ZTOZSB(tzp), tzp->z_xattr_parent, &dzp) != 0) {
245 unlinked = 1;
246 break;
247 }
248
249 if (tzp != zp)
250 zrele(tzp);
251 tzp = dzp;
252 unlinked = tzp->z_unlinked;
253 }
254 if (tzp != zp)
255 zrele(tzp);
256#else
657ce253 257 zhold(zp);
98701490
CC
258 /*
259 * if zp is XATTR node, keep walking up via z_xattr_parent until we
260 * get the owner
261 */
262 while (zp->z_pflags & ZFS_XATTR) {
263 ASSERT3U(zp->z_xattr_parent, !=, 0);
264 if (zfs_zget(ZTOZSB(zp), zp->z_xattr_parent, &dzp) != 0) {
265 unlinked = 1;
266 break;
267 }
cf118ae8 268
657ce253 269 zrele(zp);
98701490
CC
270 zp = dzp;
271 unlinked = zp->z_unlinked;
272 }
657ce253 273 zrele(zp);
ba434b18 274#endif
98701490
CC
275 return (unlinked);
276}
277
34dc7c2f 278/*
d3cc8b15
WA
279 * Handles TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, TX_MKDIR_ATTR and
280 * TK_MKXATTR transactions.
34dc7c2f
BB
281 *
282 * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
283 * domain information appended prior to the name. In this case the
284 * uid/gid in the log record will be a log centric FUID.
285 *
286 * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
287 * may contain attributes, ACL and optional fuid information.
288 *
289 * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
290 * and ACL and normal users/groups in the ACEs.
291 *
292 * There may be an optional xvattr attribute information similar
293 * to zfs_log_setattr.
294 *
295 * Also, after the file name "domain" strings may be appended.
296 */
297void
298zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
4d55ea81 299 znode_t *dzp, znode_t *zp, const char *name, vsecattr_t *vsecp,
34dc7c2f
BB
300 zfs_fuid_info_t *fuidp, vattr_t *vap)
301{
302 itx_t *itx;
34dc7c2f
BB
303 lr_create_t *lr;
304 lr_acl_create_t *lracl;
5484965a 305 size_t aclsize = 0;
34dc7c2f
BB
306 size_t xvatsize = 0;
307 size_t txsize;
a117a6d6 308 xvattr_t *xvap = (xvattr_t *)vap;
34dc7c2f
BB
309 void *end;
310 size_t lrsize;
34dc7c2f
BB
311 size_t namesize = strlen(name) + 1;
312 size_t fuidsz = 0;
313
98701490 314 if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
34dc7c2f
BB
315 return;
316
317 /*
318 * If we have FUIDs present then add in space for
319 * domains and ACE fuid's if any.
320 */
321 if (fuidp) {
322 fuidsz += fuidp->z_domain_str_sz;
323 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
324 }
325
5484965a 326 if (vap->va_mask & ATTR_XVATTR)
34dc7c2f
BB
327 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
328
329 if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
330 (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
331 (int)txtype == TX_MKXATTR) {
332 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
333 lrsize = sizeof (*lr);
334 } else {
34dc7c2f
BB
335 txsize =
336 sizeof (lr_acl_create_t) + namesize + fuidsz +
337 ZIL_ACE_LENGTH(aclsize) + xvatsize;
338 lrsize = sizeof (lr_acl_create_t);
339 }
340
341 itx = zil_itx_create(txtype, txsize);
342
343 lr = (lr_create_t *)&itx->itx_lr;
344 lr->lr_doid = dzp->z_id;
345 lr->lr_foid = zp->z_id;
50c957f7
NB
346 /* Store dnode slot count in 8 bits above object id. */
347 LR_FOID_SET_SLOTS(lr->lr_foid, zp->z_dnodesize >> DNODE_SHIFT);
428870ff 348 lr->lr_mode = zp->z_mode;
6360e277
MM
349 if (!IS_EPHEMERAL(KUID_TO_SUID(ZTOUID(zp)))) {
350 lr->lr_uid = (uint64_t)KUID_TO_SUID(ZTOUID(zp));
34dc7c2f
BB
351 } else {
352 lr->lr_uid = fuidp->z_fuid_owner;
353 }
6360e277
MM
354 if (!IS_EPHEMERAL(KGID_TO_SGID(ZTOGID(zp)))) {
355 lr->lr_gid = (uint64_t)KGID_TO_SGID(ZTOGID(zp));
34dc7c2f
BB
356 } else {
357 lr->lr_gid = fuidp->z_fuid_group;
358 }
633e8030 359 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
428870ff 360 sizeof (uint64_t));
633e8030 361 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
428870ff
BB
362 lr->lr_crtime, sizeof (uint64_t) * 2);
363
633e8030 364 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(ZTOZSB(zp)), &lr->lr_rdev,
428870ff
BB
365 sizeof (lr->lr_rdev)) != 0)
366 lr->lr_rdev = 0;
34dc7c2f
BB
367
368 /*
369 * Fill in xvattr info if any
370 */
5484965a 371 if (vap->va_mask & ATTR_XVATTR) {
34dc7c2f
BB
372 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
373 end = (caddr_t)lr + lrsize + xvatsize;
374 } else {
375 end = (caddr_t)lr + lrsize;
376 }
377
378 /* Now fill in any ACL info */
379
380 if (vsecp) {
381 lracl = (lr_acl_create_t *)&itx->itx_lr;
382 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
383 lracl->lr_acl_bytes = aclsize;
384 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
385 lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
386 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
387 lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
388 else
389 lracl->lr_acl_flags = 0;
390
861166b0 391 memcpy(end, vsecp->vsa_aclentp, aclsize);
34dc7c2f
BB
392 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
393 }
394
395 /* drop in FUID info */
396 if (fuidp) {
397 end = zfs_log_fuid_ids(fuidp, end);
398 end = zfs_log_fuid_domains(fuidp, end);
399 }
400 /*
401 * Now place file name in log record
402 */
861166b0 403 memcpy(end, name, namesize);
34dc7c2f 404
572e2857 405 zil_itx_assign(zilog, itx, tx);
34dc7c2f
BB
406}
407
408/*
d3cc8b15 409 * Handles both TX_REMOVE and TX_RMDIR transactions.
34dc7c2f
BB
410 */
411void
412zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
4d55ea81 413 znode_t *dzp, const char *name, uint64_t foid, boolean_t unlinked)
34dc7c2f
BB
414{
415 itx_t *itx;
34dc7c2f
BB
416 lr_remove_t *lr;
417 size_t namesize = strlen(name) + 1;
418
98701490 419 if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
34dc7c2f
BB
420 return;
421
422 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
423 lr = (lr_remove_t *)&itx->itx_lr;
424 lr->lr_doid = dzp->z_id;
861166b0 425 memcpy(lr + 1, name, namesize);
34dc7c2f 426
572e2857
BB
427 itx->itx_oid = foid;
428
8e556c5e
CC
429 /*
430 * Object ids can be re-instantiated in the next txg so
431 * remove any async transactions to avoid future leaks.
432 * This can happen if a fsync occurs on the re-instantiated
433 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
434 * the new file data and flushes a write record for the old object.
435 */
436 if (unlinked) {
437 ASSERT((txtype & ~TX_CI) == TX_REMOVE);
438 zil_remove_async(zilog, foid);
439 }
572e2857 440 zil_itx_assign(zilog, itx, tx);
34dc7c2f
BB
441}
442
443/*
d3cc8b15 444 * Handles TX_LINK transactions.
34dc7c2f
BB
445 */
446void
447zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
4d55ea81 448 znode_t *dzp, znode_t *zp, const char *name)
34dc7c2f
BB
449{
450 itx_t *itx;
34dc7c2f
BB
451 lr_link_t *lr;
452 size_t namesize = strlen(name) + 1;
453
428870ff 454 if (zil_replaying(zilog, tx))
34dc7c2f
BB
455 return;
456
457 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
458 lr = (lr_link_t *)&itx->itx_lr;
459 lr->lr_doid = dzp->z_id;
460 lr->lr_link_obj = zp->z_id;
861166b0 461 memcpy(lr + 1, name, namesize);
34dc7c2f 462
572e2857 463 zil_itx_assign(zilog, itx, tx);
34dc7c2f
BB
464}
465
466/*
d3cc8b15 467 * Handles TX_SYMLINK transactions.
34dc7c2f
BB
468 */
469void
470zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
4d55ea81 471 znode_t *dzp, znode_t *zp, const char *name, const char *link)
34dc7c2f
BB
472{
473 itx_t *itx;
34dc7c2f
BB
474 lr_create_t *lr;
475 size_t namesize = strlen(name) + 1;
476 size_t linksize = strlen(link) + 1;
477
428870ff 478 if (zil_replaying(zilog, tx))
34dc7c2f
BB
479 return;
480
481 itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
482 lr = (lr_create_t *)&itx->itx_lr;
483 lr->lr_doid = dzp->z_id;
484 lr->lr_foid = zp->z_id;
6360e277
MM
485 lr->lr_uid = KUID_TO_SUID(ZTOUID(zp));
486 lr->lr_gid = KGID_TO_SGID(ZTOGID(zp));
428870ff 487 lr->lr_mode = zp->z_mode;
3558fd73 488 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
428870ff 489 sizeof (uint64_t));
3558fd73 490 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
428870ff 491 lr->lr_crtime, sizeof (uint64_t) * 2);
861166b0
AZ
492 memcpy((char *)(lr + 1), name, namesize);
493 memcpy((char *)(lr + 1) + namesize, link, linksize);
34dc7c2f 494
572e2857 495 zil_itx_assign(zilog, itx, tx);
34dc7c2f
BB
496}
497
dbf6108b
AS
498static void
499do_zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, znode_t *sdzp,
500 const char *sname, znode_t *tdzp, const char *dname, znode_t *szp)
501{
502 itx_t *itx;
503 lr_rename_t *lr;
504 size_t snamesize = strlen(sname) + 1;
505 size_t dnamesize = strlen(dname) + 1;
506
507 if (zil_replaying(zilog, tx))
508 return;
509
510 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
511 lr = (lr_rename_t *)&itx->itx_lr;
512 lr->lr_sdoid = sdzp->z_id;
513 lr->lr_tdoid = tdzp->z_id;
514 memcpy((char *)(lr + 1), sname, snamesize);
515 memcpy((char *)(lr + 1) + snamesize, dname, dnamesize);
516 itx->itx_oid = szp->z_id;
517
518 zil_itx_assign(zilog, itx, tx);
519}
520
34dc7c2f 521/*
d3cc8b15 522 * Handles TX_RENAME transactions.
34dc7c2f
BB
523 */
524void
4d55ea81
RM
525zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, znode_t *sdzp,
526 const char *sname, znode_t *tdzp, const char *dname, znode_t *szp)
dbf6108b
AS
527{
528 txtype |= TX_RENAME;
529 do_zfs_log_rename(zilog, tx, txtype, sdzp, sname, tdzp, dname, szp);
530}
531
532/*
533 * Handles TX_RENAME_EXCHANGE transactions.
534 */
535void
536zfs_log_rename_exchange(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
537 znode_t *sdzp, const char *sname, znode_t *tdzp, const char *dname,
538 znode_t *szp)
539{
540 txtype |= TX_RENAME_EXCHANGE;
541 do_zfs_log_rename(zilog, tx, txtype, sdzp, sname, tdzp, dname, szp);
542}
543
544/*
545 * Handles TX_RENAME_WHITEOUT transactions.
546 *
547 * Unfortunately we cannot reuse do_zfs_log_rename because we we need to call
548 * zfs_mknode() on replay which requires stashing bits as with TX_CREATE.
549 */
550void
551zfs_log_rename_whiteout(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
552 znode_t *sdzp, const char *sname, znode_t *tdzp, const char *dname,
553 znode_t *szp, znode_t *wzp)
34dc7c2f
BB
554{
555 itx_t *itx;
dbf6108b 556 lr_rename_whiteout_t *lr;
34dc7c2f
BB
557 size_t snamesize = strlen(sname) + 1;
558 size_t dnamesize = strlen(dname) + 1;
559
428870ff 560 if (zil_replaying(zilog, tx))
34dc7c2f
BB
561 return;
562
dbf6108b 563 txtype |= TX_RENAME_WHITEOUT;
34dc7c2f 564 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
dbf6108b
AS
565 lr = (lr_rename_whiteout_t *)&itx->itx_lr;
566 lr->lr_rename.lr_sdoid = sdzp->z_id;
567 lr->lr_rename.lr_tdoid = tdzp->z_id;
568
569 /*
570 * RENAME_WHITEOUT will create an entry at the source znode, so we need
571 * to store the same data that the equivalent call to zfs_log_create()
572 * would.
573 */
574 lr->lr_wfoid = wzp->z_id;
575 LR_FOID_SET_SLOTS(lr->lr_wfoid, wzp->z_dnodesize >> DNODE_SHIFT);
576 (void) sa_lookup(wzp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(wzp)), &lr->lr_wgen,
577 sizeof (uint64_t));
578 (void) sa_lookup(wzp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(wzp)),
579 lr->lr_wcrtime, sizeof (uint64_t) * 2);
580 lr->lr_wmode = wzp->z_mode;
581 lr->lr_wuid = (uint64_t)KUID_TO_SUID(ZTOUID(wzp));
582 lr->lr_wgid = (uint64_t)KGID_TO_SGID(ZTOGID(wzp));
583
584 /*
585 * This rdev will always be makdevice(0, 0) but because the ZIL log and
586 * replay code needs to be platform independent (and there is no
587 * platform independent makdev()) we need to copy the one created
588 * during the rename operation.
589 */
590 (void) sa_lookup(wzp->z_sa_hdl, SA_ZPL_RDEV(ZTOZSB(wzp)), &lr->lr_wrdev,
591 sizeof (lr->lr_wrdev));
592
861166b0
AZ
593 memcpy((char *)(lr + 1), sname, snamesize);
594 memcpy((char *)(lr + 1) + snamesize, dname, dnamesize);
572e2857 595 itx->itx_oid = szp->z_id;
34dc7c2f 596
572e2857 597 zil_itx_assign(zilog, itx, tx);
34dc7c2f
BB
598}
599
600/*
119a394a
ED
601 * zfs_log_write() handles TX_WRITE transactions. The specified callback is
602 * called as soon as the write is on stable storage (be it via a DMU sync or a
603 * ZIL commit).
34dc7c2f 604 */
ab8d9c17 605static int64_t zfs_immediate_write_sz = 32768;
34dc7c2f 606
34dc7c2f
BB
607void
608zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
c3773de1 609 znode_t *zp, offset_t off, ssize_t resid, boolean_t commit,
e9aa730c 610 zil_callback_t callback, void *callback_data)
34dc7c2f 611{
c8bbf7c0 612 dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl);
1b7c1e5c 613 uint32_t blocksize = zp->z_blksz;
34dc7c2f 614 itx_wr_state_t write_state;
296a4a36 615 uint64_t gen = 0;
a7bd20e3 616 ssize_t size = resid;
34dc7c2f 617
98701490
CC
618 if (zil_replaying(zilog, tx) || zp->z_unlinked ||
619 zfs_xattr_owner_unlinked(zp)) {
119a394a
ED
620 if (callback != NULL)
621 callback(callback_data);
34dc7c2f 622 return;
119a394a 623 }
34dc7c2f 624
1b7c1e5c
GDN
625 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
626 write_state = WR_INDIRECT;
627 else if (!spa_has_slogs(zilog->zl_spa) &&
628 resid >= zfs_immediate_write_sz)
34dc7c2f 629 write_state = WR_INDIRECT;
c3773de1 630 else if (commit)
34dc7c2f
BB
631 write_state = WR_COPIED;
632 else
633 write_state = WR_NEED_COPY;
634
296a4a36
CC
635 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &gen,
636 sizeof (gen));
637
34dc7c2f
BB
638 while (resid) {
639 itx_t *itx;
640 lr_write_t *lr;
1b7c1e5c
GDN
641 itx_wr_state_t wr_state = write_state;
642 ssize_t len = resid;
34dc7c2f 643
b8738257
MA
644 /*
645 * A WR_COPIED record must fit entirely in one log block.
646 * Large writes can use WR_NEED_COPY, which the ZIL will
647 * split into multiple records across several log blocks
648 * if necessary.
649 */
650 if (wr_state == WR_COPIED &&
651 resid > zil_max_copied_data(zilog))
1b7c1e5c
GDN
652 wr_state = WR_NEED_COPY;
653 else if (wr_state == WR_INDIRECT)
654 len = MIN(blocksize - P2PHASE(off, blocksize), resid);
34dc7c2f
BB
655
656 itx = zil_itx_create(txtype, sizeof (*lr) +
1b7c1e5c 657 (wr_state == WR_COPIED ? len : 0));
34dc7c2f 658 lr = (lr_write_t *)&itx->itx_lr;
c8bbf7c0 659
a5c77dc4
CS
660 /*
661 * For WR_COPIED records, copy the data into the lr_write_t.
662 */
663 if (wr_state == WR_COPIED) {
664 int err;
665 DB_DNODE_ENTER(db);
666 err = dmu_read_by_dnode(DB_DNODE(db), off, len, lr + 1,
667 DMU_READ_NO_PREFETCH);
668 if (err != 0) {
669 zil_itx_destroy(itx);
670 itx = zil_itx_create(txtype, sizeof (*lr));
671 lr = (lr_write_t *)&itx->itx_lr;
672 wr_state = WR_NEED_COPY;
673 }
674 DB_DNODE_EXIT(db);
34dc7c2f
BB
675 }
676
1b7c1e5c 677 itx->itx_wr_state = wr_state;
34dc7c2f
BB
678 lr->lr_foid = zp->z_id;
679 lr->lr_offset = off;
680 lr->lr_length = len;
681 lr->lr_blkoff = 0;
682 BP_ZERO(&lr->lr_blkptr);
683
3558fd73 684 itx->itx_private = ZTOZSB(zp);
c3773de1 685 itx->itx_sync = (zp->z_sync_cnt != 0);
296a4a36 686 itx->itx_gen = gen;
34dc7c2f 687
119a394a
ED
688 itx->itx_callback = callback;
689 itx->itx_callback_data = callback_data;
572e2857 690 zil_itx_assign(zilog, itx, tx);
34dc7c2f
BB
691
692 off += len;
693 resid -= len;
694 }
a7bd20e3
KJ
695
696 if (write_state == WR_COPIED || write_state == WR_NEED_COPY) {
697 dsl_pool_wrlog_count(zilog->zl_dmu_pool, size, tx->tx_txg);
698 }
34dc7c2f
BB
699}
700
701/*
d3cc8b15 702 * Handles TX_TRUNCATE transactions.
34dc7c2f
BB
703 */
704void
705zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
e9aa730c 706 znode_t *zp, uint64_t off, uint64_t len)
34dc7c2f
BB
707{
708 itx_t *itx;
34dc7c2f
BB
709 lr_truncate_t *lr;
710
98701490
CC
711 if (zil_replaying(zilog, tx) || zp->z_unlinked ||
712 zfs_xattr_owner_unlinked(zp))
34dc7c2f
BB
713 return;
714
715 itx = zil_itx_create(txtype, sizeof (*lr));
716 lr = (lr_truncate_t *)&itx->itx_lr;
717 lr->lr_foid = zp->z_id;
718 lr->lr_offset = off;
719 lr->lr_length = len;
720
721 itx->itx_sync = (zp->z_sync_cnt != 0);
572e2857 722 zil_itx_assign(zilog, itx, tx);
34dc7c2f
BB
723}
724
725/*
d3cc8b15 726 * Handles TX_SETATTR transactions.
34dc7c2f
BB
727 */
728void
5484965a
BB
729zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
730 znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
34dc7c2f
BB
731{
732 itx_t *itx;
34dc7c2f
BB
733 lr_setattr_t *lr;
734 xvattr_t *xvap = (xvattr_t *)vap;
735 size_t recsize = sizeof (lr_setattr_t);
736 void *start;
737
428870ff 738 if (zil_replaying(zilog, tx) || zp->z_unlinked)
34dc7c2f
BB
739 return;
740
741 /*
742 * If XVATTR set, then log record size needs to allow
743 * for lr_attr_t + xvattr mask, mapsize and create time
744 * plus actual attribute values
745 */
5484965a 746 if (vap->va_mask & ATTR_XVATTR)
34dc7c2f
BB
747 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
748
749 if (fuidp)
750 recsize += fuidp->z_domain_str_sz;
751
752 itx = zil_itx_create(txtype, recsize);
753 lr = (lr_setattr_t *)&itx->itx_lr;
754 lr->lr_foid = zp->z_id;
755 lr->lr_mask = (uint64_t)mask_applied;
5484965a
BB
756 lr->lr_mode = (uint64_t)vap->va_mode;
757 if ((mask_applied & ATTR_UID) && IS_EPHEMERAL(vap->va_uid))
34dc7c2f
BB
758 lr->lr_uid = fuidp->z_fuid_owner;
759 else
5484965a 760 lr->lr_uid = (uint64_t)vap->va_uid;
34dc7c2f 761
5484965a 762 if ((mask_applied & ATTR_GID) && IS_EPHEMERAL(vap->va_gid))
34dc7c2f
BB
763 lr->lr_gid = fuidp->z_fuid_group;
764 else
5484965a 765 lr->lr_gid = (uint64_t)vap->va_gid;
34dc7c2f 766
5484965a
BB
767 lr->lr_size = (uint64_t)vap->va_size;
768 ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
769 ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
34dc7c2f 770 start = (lr_setattr_t *)(lr + 1);
5484965a 771 if (vap->va_mask & ATTR_XVATTR) {
34dc7c2f
BB
772 zfs_log_xvattr((lr_attr_t *)start, xvap);
773 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
774 }
775
776 /*
777 * Now stick on domain information if any on end
778 */
779
780 if (fuidp)
781 (void) zfs_log_fuid_domains(fuidp, start);
782
783 itx->itx_sync = (zp->z_sync_cnt != 0);
572e2857 784 zil_itx_assign(zilog, itx, tx);
34dc7c2f
BB
785}
786
361a7e82
JP
787/*
788 * Handles TX_SETSAXATTR transactions.
789 */
790void
791zfs_log_setsaxattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
792 znode_t *zp, const char *name, const void *value, size_t size)
793{
794 itx_t *itx;
795 lr_setsaxattr_t *lr;
796 size_t recsize = sizeof (lr_setsaxattr_t);
797 void *xattrstart;
798 int namelen;
799
800 if (zil_replaying(zilog, tx) || zp->z_unlinked)
801 return;
802
803 namelen = strlen(name) + 1;
804 recsize += (namelen + size);
805 itx = zil_itx_create(txtype, recsize);
806 lr = (lr_setsaxattr_t *)&itx->itx_lr;
807 lr->lr_foid = zp->z_id;
808 xattrstart = (char *)(lr + 1);
861166b0 809 memcpy(xattrstart, name, namelen);
361a7e82 810 if (value != NULL) {
861166b0 811 memcpy((char *)xattrstart + namelen, value, size);
361a7e82
JP
812 lr->lr_size = size;
813 } else {
814 lr->lr_size = 0;
815 }
816
817 itx->itx_sync = (zp->z_sync_cnt != 0);
818 zil_itx_assign(zilog, itx, tx);
819}
820
34dc7c2f 821/*
d3cc8b15 822 * Handles TX_ACL transactions.
34dc7c2f
BB
823 */
824void
825zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
826 vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
827{
828 itx_t *itx;
34dc7c2f
BB
829 lr_acl_v0_t *lrv0;
830 lr_acl_t *lr;
831 int txtype;
832 int lrsize;
833 size_t txsize;
834 size_t aclbytes = vsecp->vsa_aclentsz;
835
428870ff 836 if (zil_replaying(zilog, tx) || zp->z_unlinked)
b128c09f
BB
837 return;
838
3558fd73 839 txtype = (ZTOZSB(zp)->z_version < ZPL_VERSION_FUID) ?
34dc7c2f
BB
840 TX_ACL_V0 : TX_ACL;
841
842 if (txtype == TX_ACL)
843 lrsize = sizeof (*lr);
844 else
845 lrsize = sizeof (*lrv0);
846
34dc7c2f
BB
847 txsize = lrsize +
848 ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
849 (fuidp ? fuidp->z_domain_str_sz : 0) +
b128c09f 850 sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
34dc7c2f
BB
851
852 itx = zil_itx_create(txtype, txsize);
853
854 lr = (lr_acl_t *)&itx->itx_lr;
855 lr->lr_foid = zp->z_id;
856 if (txtype == TX_ACL) {
857 lr->lr_acl_bytes = aclbytes;
858 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
859 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
860 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
861 lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
862 else
863 lr->lr_acl_flags = 0;
864 }
865 lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
866
867 if (txtype == TX_ACL_V0) {
868 lrv0 = (lr_acl_v0_t *)lr;
861166b0 869 memcpy(lrv0 + 1, vsecp->vsa_aclentp, aclbytes);
34dc7c2f
BB
870 } else {
871 void *start = (ace_t *)(lr + 1);
872
861166b0 873 memcpy(start, vsecp->vsa_aclentp, aclbytes);
34dc7c2f
BB
874
875 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
876
877 if (fuidp) {
878 start = zfs_log_fuid_ids(fuidp, start);
879 (void) zfs_log_fuid_domains(fuidp, start);
880 }
881 }
882
883 itx->itx_sync = (zp->z_sync_cnt != 0);
572e2857 884 zil_itx_assign(zilog, itx, tx);
34dc7c2f 885}
15fd2749 886
67a1b037
PJD
887/*
888 * Handles TX_CLONE_RANGE transactions.
889 */
890void
891zfs_log_clone_range(zilog_t *zilog, dmu_tx_t *tx, int txtype, znode_t *zp,
892 uint64_t off, uint64_t len, uint64_t blksz, const blkptr_t *bps,
893 size_t nbps)
894{
895 itx_t *itx;
896 lr_clone_range_t *lr;
897 uint64_t partlen, max_log_data;
898 size_t i, partnbps;
899
3b5af201 900 if (zil_replaying(zilog, tx) || zp->z_unlinked)
67a1b037
PJD
901 return;
902
903 max_log_data = zil_max_log_data(zilog, sizeof (lr_clone_range_t));
904
905 while (nbps > 0) {
906 partnbps = MIN(nbps, max_log_data / sizeof (bps[0]));
907 partlen = 0;
908 for (i = 0; i < partnbps; i++) {
909 partlen += BP_GET_LSIZE(&bps[i]);
910 }
911 partlen = MIN(partlen, len);
912
913 itx = zil_itx_create(txtype,
914 sizeof (*lr) + sizeof (bps[0]) * partnbps);
915 lr = (lr_clone_range_t *)&itx->itx_lr;
916 lr->lr_foid = zp->z_id;
917 lr->lr_offset = off;
918 lr->lr_length = partlen;
919 lr->lr_blksz = blksz;
920 lr->lr_nbps = partnbps;
921 memcpy(lr->lr_bps, bps, sizeof (bps[0]) * partnbps);
922
923 itx->itx_sync = (zp->z_sync_cnt != 0);
924
925 zil_itx_assign(zilog, itx, tx);
926
927 bps += partnbps;
928 ASSERT3U(nbps, >=, partnbps);
929 nbps -= partnbps;
930 off += partlen;
931 ASSERT3U(len, >=, partlen);
932 len -= partlen;
933 }
934}
935
ab8d9c17 936ZFS_MODULE_PARAM(zfs, zfs_, immediate_write_sz, S64, ZMOD_RW,
03fdcb9a 937 "Largest data block to write to zil");