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) 2015 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>
34 #include <sys/zfs_znode.h>
35 #include <sys/zfs_dir.h>
37 #include <sys/zil_impl.h>
38 #include <sys/byteorder.h>
39 #include <sys/policy.h>
44 #include <sys/zfs_fuid.h>
45 #include <sys/dsl_dataset.h>
48 * These zfs_log_* functions must be called within a dmu tx, in one
49 * of 2 contexts depending on zilog->z_replay:
53 * We need to record the transaction so that if it is committed to
54 * the Intent Log then it can be replayed. An intent log transaction
55 * structure (itx_t) is allocated and all the information necessary to
56 * possibly replay the transaction is saved in it. The itx is then assigned
57 * a sequence number and inserted in the in-memory list anchored in the zilog.
61 * We need to mark the intent log record as replayed in the log header.
62 * This is done in the same transaction as the replay so that they
67 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
69 int isxvattr = (vap->va_mask & AT_XVATTR);
72 if (vsecp == NULL && !isxvattr)
74 if (vsecp && isxvattr)
76 return (TX_CREATE_ACL_ATTR);
78 panic("%s:%u: unsupported condition", __func__, __LINE__);
81 return (TX_CREATE_ACL);
83 return (TX_CREATE_ATTR);
86 if (vsecp == NULL && !isxvattr)
88 if (vsecp && isxvattr)
90 return (TX_MKDIR_ACL_ATTR);
92 panic("%s:%u: unsupported condition", __func__, __LINE__);
95 return (TX_MKDIR_ACL);
97 return (TX_MKDIR_ATTR);
102 return (TX_MAX_TYPE);
106 * build up the log data necessary for logging xvattr_t
107 * First lr_attr_t is initialized. following the lr_attr_t
108 * is the mapsize and attribute bitmap copied from the xvattr_t.
109 * Following the bitmap and bitmapsize two 64 bit words are reserved
110 * for the create time which may be set. Following the create time
111 * records a single 64 bit integer which has the bits to set on
112 * replay for the xvattr.
115 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
124 xoap = xva_getxoptattr(xvap);
127 lrattr->lr_attr_masksize = xvap->xva_mapsize;
128 bitmap = &lrattr->lr_attr_bitmap;
129 for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
130 *bitmap = xvap->xva_reqattrmap[i];
133 /* Now pack the attributes up in a single uint64_t */
134 attrs = (uint64_t *)bitmap;
136 scanstamp = (caddr_t)(crtime + 2);
138 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
139 *attrs |= (xoap->xoa_readonly == 0) ? 0 :
141 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
142 *attrs |= (xoap->xoa_hidden == 0) ? 0 :
144 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
145 *attrs |= (xoap->xoa_system == 0) ? 0 :
147 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
148 *attrs |= (xoap->xoa_archive == 0) ? 0 :
150 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
151 *attrs |= (xoap->xoa_immutable == 0) ? 0 :
153 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
154 *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
156 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
157 *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
159 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
160 *attrs |= (xoap->xoa_opaque == 0) ? 0 :
162 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
163 *attrs |= (xoap->xoa_nodump == 0) ? 0 :
165 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
166 *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
168 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
169 *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
171 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
172 ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
173 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
174 bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
175 if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
176 *attrs |= (xoap->xoa_reparse == 0) ? 0 :
178 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
179 *attrs |= (xoap->xoa_offline == 0) ? 0 :
181 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
182 *attrs |= (xoap->xoa_sparse == 0) ? 0 :
187 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
190 uint64_t *fuidloc = start;
192 /* First copy in the ACE FUIDs */
193 for (zfuid = list_head(&fuidp->z_fuids); zfuid;
194 zfuid = list_next(&fuidp->z_fuids, zfuid)) {
195 *fuidloc++ = zfuid->z_logfuid;
202 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
204 zfs_fuid_domain_t *zdomain;
206 /* now copy in the domain info, if any */
207 if (fuidp->z_domain_str_sz != 0) {
208 for (zdomain = list_head(&fuidp->z_domains); zdomain;
209 zdomain = list_next(&fuidp->z_domains, zdomain)) {
210 bcopy((void *)zdomain->z_domain, start,
211 strlen(zdomain->z_domain) + 1);
212 start = (caddr_t)start +
213 strlen(zdomain->z_domain) + 1;
220 * Handles TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, TX_MKDIR_ATTR and
221 * TK_MKXATTR transactions.
223 * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
224 * domain information appended prior to the name. In this case the
225 * uid/gid in the log record will be a log centric FUID.
227 * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
228 * may contain attributes, ACL and optional fuid information.
230 * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
231 * and ACL and normal users/groups in the ACEs.
233 * There may be an optional xvattr attribute information similar
234 * to zfs_log_setattr.
236 * Also, after the file name "domain" strings may be appended.
239 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
240 znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
241 zfs_fuid_info_t *fuidp, vattr_t *vap)
245 lr_acl_create_t *lracl;
246 size_t aclsize = (vsecp != NULL) ? vsecp->vsa_aclentsz : 0;
249 xvattr_t *xvap = (xvattr_t *)vap;
252 size_t namesize = strlen(name) + 1;
255 if (zil_replaying(zilog, tx))
259 * If we have FUIDs present then add in space for
260 * domains and ACE fuid's if any.
263 fuidsz += fuidp->z_domain_str_sz;
264 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
267 if (vap->va_mask & AT_XVATTR)
268 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
270 if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
271 (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
272 (int)txtype == TX_MKXATTR) {
273 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
274 lrsize = sizeof (*lr);
277 sizeof (lr_acl_create_t) + namesize + fuidsz +
278 ZIL_ACE_LENGTH(aclsize) + xvatsize;
279 lrsize = sizeof (lr_acl_create_t);
282 itx = zil_itx_create(txtype, txsize);
284 lr = (lr_create_t *)&itx->itx_lr;
285 lr->lr_doid = dzp->z_id;
286 lr->lr_foid = zp->z_id;
287 lr->lr_mode = zp->z_mode;
288 if (!IS_EPHEMERAL(zp->z_uid)) {
289 lr->lr_uid = (uint64_t)zp->z_uid;
291 lr->lr_uid = fuidp->z_fuid_owner;
293 if (!IS_EPHEMERAL(zp->z_gid)) {
294 lr->lr_gid = (uint64_t)zp->z_gid;
296 lr->lr_gid = fuidp->z_fuid_group;
298 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
300 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
301 lr->lr_crtime, sizeof (uint64_t) * 2);
303 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zp->z_zfsvfs), &lr->lr_rdev,
304 sizeof (lr->lr_rdev)) != 0)
308 * Fill in xvattr info if any
310 if (vap->va_mask & AT_XVATTR) {
311 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
312 end = (caddr_t)lr + lrsize + xvatsize;
314 end = (caddr_t)lr + lrsize;
317 /* Now fill in any ACL info */
320 lracl = (lr_acl_create_t *)&itx->itx_lr;
321 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
322 lracl->lr_acl_bytes = aclsize;
323 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
324 lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
325 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
326 lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
328 lracl->lr_acl_flags = 0;
330 bcopy(vsecp->vsa_aclentp, end, aclsize);
331 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
334 /* drop in FUID info */
336 end = zfs_log_fuid_ids(fuidp, end);
337 end = zfs_log_fuid_domains(fuidp, end);
340 * Now place file name in log record
342 bcopy(name, end, namesize);
344 zil_itx_assign(zilog, itx, tx);
348 * Handles both TX_REMOVE and TX_RMDIR transactions.
351 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
352 znode_t *dzp, char *name, uint64_t foid)
356 size_t namesize = strlen(name) + 1;
358 if (zil_replaying(zilog, tx))
361 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
362 lr = (lr_remove_t *)&itx->itx_lr;
363 lr->lr_doid = dzp->z_id;
364 bcopy(name, (char *)(lr + 1), namesize);
368 zil_itx_assign(zilog, itx, tx);
372 * Handles TX_LINK transactions.
375 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
376 znode_t *dzp, znode_t *zp, char *name)
380 size_t namesize = strlen(name) + 1;
382 if (zil_replaying(zilog, tx))
385 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
386 lr = (lr_link_t *)&itx->itx_lr;
387 lr->lr_doid = dzp->z_id;
388 lr->lr_link_obj = zp->z_id;
389 bcopy(name, (char *)(lr + 1), namesize);
391 zil_itx_assign(zilog, itx, tx);
395 * Handles TX_SYMLINK transactions.
398 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
399 znode_t *dzp, znode_t *zp, char *name, char *link)
403 size_t namesize = strlen(name) + 1;
404 size_t linksize = strlen(link) + 1;
406 if (zil_replaying(zilog, tx))
409 itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
410 lr = (lr_create_t *)&itx->itx_lr;
411 lr->lr_doid = dzp->z_id;
412 lr->lr_foid = zp->z_id;
413 lr->lr_uid = zp->z_uid;
414 lr->lr_gid = zp->z_gid;
415 lr->lr_mode = zp->z_mode;
416 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
418 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
419 lr->lr_crtime, sizeof (uint64_t) * 2);
420 bcopy(name, (char *)(lr + 1), namesize);
421 bcopy(link, (char *)(lr + 1) + namesize, linksize);
423 zil_itx_assign(zilog, itx, tx);
427 * Handles TX_RENAME transactions.
430 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
431 znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
435 size_t snamesize = strlen(sname) + 1;
436 size_t dnamesize = strlen(dname) + 1;
438 if (zil_replaying(zilog, tx))
441 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
442 lr = (lr_rename_t *)&itx->itx_lr;
443 lr->lr_sdoid = sdzp->z_id;
444 lr->lr_tdoid = tdzp->z_id;
445 bcopy(sname, (char *)(lr + 1), snamesize);
446 bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
447 itx->itx_oid = szp->z_id;
449 zil_itx_assign(zilog, itx, tx);
453 * Handles TX_WRITE transactions.
455 ssize_t zfs_immediate_write_sz = 32768;
458 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
459 znode_t *zp, offset_t off, ssize_t resid, int ioflag)
461 itx_wr_state_t write_state;
464 ssize_t immediate_write_sz;
466 if (zil_replaying(zilog, tx) || zp->z_unlinked)
469 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
470 ? 0 : zfs_immediate_write_sz;
472 slogging = spa_has_slogs(zilog->zl_spa) &&
473 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
474 if (resid > immediate_write_sz && !slogging && resid <= zp->z_blksz)
475 write_state = WR_INDIRECT;
476 else if (ioflag & (FSYNC | FDSYNC))
477 write_state = WR_COPIED;
479 write_state = WR_NEED_COPY;
481 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
482 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
491 * If the write would overflow the largest block then split it.
493 if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA)
494 len = SPA_OLD_MAXBLOCKSIZE >> 1;
498 itx = zil_itx_create(txtype, sizeof (*lr) +
499 (write_state == WR_COPIED ? len : 0));
500 lr = (lr_write_t *)&itx->itx_lr;
501 if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
502 zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
503 zil_itx_destroy(itx);
504 itx = zil_itx_create(txtype, sizeof (*lr));
505 lr = (lr_write_t *)&itx->itx_lr;
506 write_state = WR_NEED_COPY;
509 itx->itx_wr_state = write_state;
510 if (write_state == WR_NEED_COPY)
512 lr->lr_foid = zp->z_id;
516 BP_ZERO(&lr->lr_blkptr);
518 itx->itx_private = zp->z_zfsvfs;
520 if (!(ioflag & (FSYNC | FDSYNC)) && (zp->z_sync_cnt == 0) &&
522 itx->itx_sync = B_FALSE;
524 zil_itx_assign(zilog, itx, tx);
532 * Handles TX_TRUNCATE transactions.
535 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
536 znode_t *zp, uint64_t off, uint64_t len)
541 if (zil_replaying(zilog, tx) || zp->z_unlinked)
544 itx = zil_itx_create(txtype, sizeof (*lr));
545 lr = (lr_truncate_t *)&itx->itx_lr;
546 lr->lr_foid = zp->z_id;
550 itx->itx_sync = (zp->z_sync_cnt != 0);
551 zil_itx_assign(zilog, itx, tx);
555 * Handles TX_SETATTR transactions.
558 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
559 znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
563 xvattr_t *xvap = (xvattr_t *)vap;
564 size_t recsize = sizeof (lr_setattr_t);
567 if (zil_replaying(zilog, tx) || zp->z_unlinked)
571 * If XVATTR set, then log record size needs to allow
572 * for lr_attr_t + xvattr mask, mapsize and create time
573 * plus actual attribute values
575 if (vap->va_mask & AT_XVATTR)
576 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
579 recsize += fuidp->z_domain_str_sz;
581 itx = zil_itx_create(txtype, recsize);
582 lr = (lr_setattr_t *)&itx->itx_lr;
583 lr->lr_foid = zp->z_id;
584 lr->lr_mask = (uint64_t)mask_applied;
585 lr->lr_mode = (uint64_t)vap->va_mode;
586 if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
587 lr->lr_uid = fuidp->z_fuid_owner;
589 lr->lr_uid = (uint64_t)vap->va_uid;
591 if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
592 lr->lr_gid = fuidp->z_fuid_group;
594 lr->lr_gid = (uint64_t)vap->va_gid;
596 lr->lr_size = (uint64_t)vap->va_size;
597 ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
598 ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
599 start = (lr_setattr_t *)(lr + 1);
600 if (vap->va_mask & AT_XVATTR) {
601 zfs_log_xvattr((lr_attr_t *)start, xvap);
602 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
606 * Now stick on domain information if any on end
610 (void) zfs_log_fuid_domains(fuidp, start);
612 itx->itx_sync = (zp->z_sync_cnt != 0);
613 zil_itx_assign(zilog, itx, tx);
617 * Handles TX_ACL transactions.
620 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
621 vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
629 size_t aclbytes = vsecp->vsa_aclentsz;
631 if (zil_replaying(zilog, tx) || zp->z_unlinked)
634 txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
637 if (txtype == TX_ACL)
638 lrsize = sizeof (*lr);
640 lrsize = sizeof (*lrv0);
643 ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
644 (fuidp ? fuidp->z_domain_str_sz : 0) +
645 sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
647 itx = zil_itx_create(txtype, txsize);
649 lr = (lr_acl_t *)&itx->itx_lr;
650 lr->lr_foid = zp->z_id;
651 if (txtype == TX_ACL) {
652 lr->lr_acl_bytes = aclbytes;
653 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
654 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
655 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
656 lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
658 lr->lr_acl_flags = 0;
660 lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
662 if (txtype == TX_ACL_V0) {
663 lrv0 = (lr_acl_v0_t *)lr;
664 bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
666 void *start = (ace_t *)(lr + 1);
668 bcopy(vsecp->vsa_aclentp, start, aclbytes);
670 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
673 start = zfs_log_fuid_ids(fuidp, start);
674 (void) zfs_log_fuid_domains(fuidp, start);
678 itx->itx_sync = (zp->z_sync_cnt != 0);
679 zil_itx_assign(zilog, itx, tx);