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.
24 * Copyright (c) 2014 Integros [integros.com]
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
31 #include <sys/cmn_err.h>
35 #include <sys/zfs_znode.h>
36 #include <sys/zfs_dir.h>
38 #include <sys/zil_impl.h>
39 #include <sys/byteorder.h>
40 #include <sys/policy.h>
45 #include <sys/zfs_fuid.h>
46 #include <sys/dsl_dataset.h>
49 * These zfs_log_* functions must be called within a dmu tx, in one
50 * of 2 contexts depending on zilog->z_replay:
54 * We need to record the transaction so that if it is committed to
55 * the Intent Log then it can be replayed. An intent log transaction
56 * structure (itx_t) is allocated and all the information necessary to
57 * possibly replay the transaction is saved in it. The itx is then assigned
58 * a sequence number and inserted in the in-memory list anchored in the zilog.
62 * We need to mark the intent log record as replayed in the log header.
63 * This is done in the same transaction as the replay so that they
68 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
70 int isxvattr = (vap->va_mask & AT_XVATTR);
73 if (vsecp == NULL && !isxvattr)
75 if (vsecp && isxvattr)
77 return (TX_CREATE_ACL_ATTR);
79 panic("%s:%u: unsupported condition", __func__, __LINE__);
82 return (TX_CREATE_ACL);
84 return (TX_CREATE_ATTR);
87 if (vsecp == NULL && !isxvattr)
89 if (vsecp && isxvattr)
91 return (TX_MKDIR_ACL_ATTR);
93 panic("%s:%u: unsupported condition", __func__, __LINE__);
96 return (TX_MKDIR_ACL);
98 return (TX_MKDIR_ATTR);
103 return (TX_MAX_TYPE);
107 * build up the log data necessary for logging xvattr_t
108 * First lr_attr_t is initialized. following the lr_attr_t
109 * is the mapsize and attribute bitmap copied from the xvattr_t.
110 * Following the bitmap and bitmapsize two 64 bit words are reserved
111 * for the create time which may be set. Following the create time
112 * records a single 64 bit integer which has the bits to set on
113 * replay for the xvattr.
116 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
125 xoap = xva_getxoptattr(xvap);
128 lrattr->lr_attr_masksize = xvap->xva_mapsize;
129 bitmap = &lrattr->lr_attr_bitmap;
130 for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
131 *bitmap = xvap->xva_reqattrmap[i];
134 /* Now pack the attributes up in a single uint64_t */
135 attrs = (uint64_t *)bitmap;
137 scanstamp = (caddr_t)(crtime + 2);
139 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
140 *attrs |= (xoap->xoa_readonly == 0) ? 0 :
142 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
143 *attrs |= (xoap->xoa_hidden == 0) ? 0 :
145 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
146 *attrs |= (xoap->xoa_system == 0) ? 0 :
148 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
149 *attrs |= (xoap->xoa_archive == 0) ? 0 :
151 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
152 *attrs |= (xoap->xoa_immutable == 0) ? 0 :
154 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
155 *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
157 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
158 *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
160 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
161 *attrs |= (xoap->xoa_opaque == 0) ? 0 :
163 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
164 *attrs |= (xoap->xoa_nodump == 0) ? 0 :
166 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
167 *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
169 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
170 *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
172 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
173 ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
174 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
175 bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
176 if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
177 *attrs |= (xoap->xoa_reparse == 0) ? 0 :
179 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
180 *attrs |= (xoap->xoa_offline == 0) ? 0 :
182 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
183 *attrs |= (xoap->xoa_sparse == 0) ? 0 :
188 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
191 uint64_t *fuidloc = start;
193 /* First copy in the ACE FUIDs */
194 for (zfuid = list_head(&fuidp->z_fuids); zfuid;
195 zfuid = list_next(&fuidp->z_fuids, zfuid)) {
196 *fuidloc++ = zfuid->z_logfuid;
203 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
205 zfs_fuid_domain_t *zdomain;
207 /* now copy in the domain info, if any */
208 if (fuidp->z_domain_str_sz != 0) {
209 for (zdomain = list_head(&fuidp->z_domains); zdomain;
210 zdomain = list_next(&fuidp->z_domains, zdomain)) {
211 bcopy((void *)zdomain->z_domain, start,
212 strlen(zdomain->z_domain) + 1);
213 start = (caddr_t)start +
214 strlen(zdomain->z_domain) + 1;
221 * Handles TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, TX_MKDIR_ATTR and
222 * TK_MKXATTR transactions.
224 * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
225 * domain information appended prior to the name. In this case the
226 * uid/gid in the log record will be a log centric FUID.
228 * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
229 * may contain attributes, ACL and optional fuid information.
231 * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
232 * and ACL and normal users/groups in the ACEs.
234 * There may be an optional xvattr attribute information similar
235 * to zfs_log_setattr.
237 * Also, after the file name "domain" strings may be appended.
240 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
241 znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
242 zfs_fuid_info_t *fuidp, vattr_t *vap)
246 lr_acl_create_t *lracl;
247 size_t aclsize = (vsecp != NULL) ? vsecp->vsa_aclentsz : 0;
250 xvattr_t *xvap = (xvattr_t *)vap;
253 size_t namesize = strlen(name) + 1;
256 if (zil_replaying(zilog, tx))
260 * If we have FUIDs present then add in space for
261 * domains and ACE fuid's if any.
264 fuidsz += fuidp->z_domain_str_sz;
265 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
268 if (vap->va_mask & AT_XVATTR)
269 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
271 if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
272 (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
273 (int)txtype == TX_MKXATTR) {
274 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
275 lrsize = sizeof (*lr);
278 sizeof (lr_acl_create_t) + namesize + fuidsz +
279 ZIL_ACE_LENGTH(aclsize) + xvatsize;
280 lrsize = sizeof (lr_acl_create_t);
283 itx = zil_itx_create(txtype, txsize);
285 lr = (lr_create_t *)&itx->itx_lr;
286 lr->lr_doid = dzp->z_id;
287 lr->lr_foid = zp->z_id;
288 /* Store dnode slot count in 8 bits above object id. */
289 LR_FOID_SET_SLOTS(lr->lr_foid, zp->z_dnodesize >> DNODE_SHIFT);
290 lr->lr_mode = zp->z_mode;
291 if (!IS_EPHEMERAL(zp->z_uid)) {
292 lr->lr_uid = (uint64_t)zp->z_uid;
294 lr->lr_uid = fuidp->z_fuid_owner;
296 if (!IS_EPHEMERAL(zp->z_gid)) {
297 lr->lr_gid = (uint64_t)zp->z_gid;
299 lr->lr_gid = fuidp->z_fuid_group;
301 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
303 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
304 lr->lr_crtime, sizeof (uint64_t) * 2);
306 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zp->z_zfsvfs), &lr->lr_rdev,
307 sizeof (lr->lr_rdev)) != 0)
311 * Fill in xvattr info if any
313 if (vap->va_mask & AT_XVATTR) {
314 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
315 end = (caddr_t)lr + lrsize + xvatsize;
317 end = (caddr_t)lr + lrsize;
320 /* Now fill in any ACL info */
323 lracl = (lr_acl_create_t *)&itx->itx_lr;
324 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
325 lracl->lr_acl_bytes = aclsize;
326 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
327 lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
328 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
329 lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
331 lracl->lr_acl_flags = 0;
333 bcopy(vsecp->vsa_aclentp, end, aclsize);
334 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
337 /* drop in FUID info */
339 end = zfs_log_fuid_ids(fuidp, end);
340 end = zfs_log_fuid_domains(fuidp, end);
343 * Now place file name in log record
345 bcopy(name, end, namesize);
347 zil_itx_assign(zilog, itx, tx);
351 * Handles both TX_REMOVE and TX_RMDIR transactions.
354 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
355 znode_t *dzp, char *name, uint64_t foid)
359 size_t namesize = strlen(name) + 1;
361 if (zil_replaying(zilog, tx))
364 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
365 lr = (lr_remove_t *)&itx->itx_lr;
366 lr->lr_doid = dzp->z_id;
367 bcopy(name, (char *)(lr + 1), namesize);
371 zil_itx_assign(zilog, itx, tx);
375 * Handles TX_LINK transactions.
378 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
379 znode_t *dzp, znode_t *zp, char *name)
383 size_t namesize = strlen(name) + 1;
385 if (zil_replaying(zilog, tx))
388 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
389 lr = (lr_link_t *)&itx->itx_lr;
390 lr->lr_doid = dzp->z_id;
391 lr->lr_link_obj = zp->z_id;
392 bcopy(name, (char *)(lr + 1), namesize);
394 zil_itx_assign(zilog, itx, tx);
398 * Handles TX_SYMLINK transactions.
401 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
402 znode_t *dzp, znode_t *zp, char *name, char *link)
406 size_t namesize = strlen(name) + 1;
407 size_t linksize = strlen(link) + 1;
409 if (zil_replaying(zilog, tx))
412 itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
413 lr = (lr_create_t *)&itx->itx_lr;
414 lr->lr_doid = dzp->z_id;
415 lr->lr_foid = zp->z_id;
416 lr->lr_uid = zp->z_uid;
417 lr->lr_gid = zp->z_gid;
418 lr->lr_mode = zp->z_mode;
419 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
421 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
422 lr->lr_crtime, sizeof (uint64_t) * 2);
423 bcopy(name, (char *)(lr + 1), namesize);
424 bcopy(link, (char *)(lr + 1) + namesize, linksize);
426 zil_itx_assign(zilog, itx, tx);
430 * Handles TX_RENAME transactions.
433 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
434 znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
438 size_t snamesize = strlen(sname) + 1;
439 size_t dnamesize = strlen(dname) + 1;
441 if (zil_replaying(zilog, tx))
444 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
445 lr = (lr_rename_t *)&itx->itx_lr;
446 lr->lr_sdoid = sdzp->z_id;
447 lr->lr_tdoid = tdzp->z_id;
448 bcopy(sname, (char *)(lr + 1), snamesize);
449 bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
450 itx->itx_oid = szp->z_id;
452 zil_itx_assign(zilog, itx, tx);
456 * Handles TX_WRITE transactions.
458 ssize_t zfs_immediate_write_sz = 32768;
460 SYSCTL_DECL(_vfs_zfs);
461 SYSCTL_LONG(_vfs_zfs, OID_AUTO, immediate_write_sz, CTLFLAG_RWTUN,
462 &zfs_immediate_write_sz, 0, "Minimal size for indirect log write");
466 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
467 znode_t *zp, offset_t off, ssize_t resid, int ioflag)
469 uint32_t blocksize = zp->z_blksz;
470 itx_wr_state_t write_state;
473 if (zil_replaying(zilog, tx) || zp->z_unlinked)
476 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
477 write_state = WR_INDIRECT;
478 else if (!spa_has_slogs(zilog->zl_spa) &&
479 resid >= zfs_immediate_write_sz)
480 write_state = WR_INDIRECT;
481 else if (ioflag & (FSYNC | FDSYNC))
482 write_state = WR_COPIED;
484 write_state = WR_NEED_COPY;
486 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
487 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
493 itx_wr_state_t wr_state = write_state;
496 if (wr_state == WR_COPIED && resid > ZIL_MAX_COPIED_DATA)
497 wr_state = WR_NEED_COPY;
498 else if (wr_state == WR_INDIRECT)
499 len = MIN(blocksize - P2PHASE(off, blocksize), resid);
501 itx = zil_itx_create(txtype, sizeof (*lr) +
502 (wr_state == WR_COPIED ? len : 0));
503 lr = (lr_write_t *)&itx->itx_lr;
504 if (wr_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
505 zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
506 zil_itx_destroy(itx);
507 itx = zil_itx_create(txtype, sizeof (*lr));
508 lr = (lr_write_t *)&itx->itx_lr;
509 wr_state = WR_NEED_COPY;
512 itx->itx_wr_state = wr_state;
513 lr->lr_foid = zp->z_id;
517 BP_ZERO(&lr->lr_blkptr);
519 itx->itx_private = zp->z_zfsvfs;
521 if (!(ioflag & (FSYNC | FDSYNC)) && (zp->z_sync_cnt == 0) &&
523 itx->itx_sync = B_FALSE;
525 zil_itx_assign(zilog, itx, tx);
533 * Handles TX_TRUNCATE transactions.
536 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
537 znode_t *zp, uint64_t off, uint64_t len)
542 if (zil_replaying(zilog, tx) || zp->z_unlinked)
545 itx = zil_itx_create(txtype, sizeof (*lr));
546 lr = (lr_truncate_t *)&itx->itx_lr;
547 lr->lr_foid = zp->z_id;
551 itx->itx_sync = (zp->z_sync_cnt != 0);
552 zil_itx_assign(zilog, itx, tx);
556 * Handles TX_SETATTR transactions.
559 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
560 znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
564 xvattr_t *xvap = (xvattr_t *)vap;
565 size_t recsize = sizeof (lr_setattr_t);
568 if (zil_replaying(zilog, tx) || zp->z_unlinked)
572 * If XVATTR set, then log record size needs to allow
573 * for lr_attr_t + xvattr mask, mapsize and create time
574 * plus actual attribute values
576 if (vap->va_mask & AT_XVATTR)
577 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
580 recsize += fuidp->z_domain_str_sz;
582 itx = zil_itx_create(txtype, recsize);
583 lr = (lr_setattr_t *)&itx->itx_lr;
584 lr->lr_foid = zp->z_id;
585 lr->lr_mask = (uint64_t)mask_applied;
586 lr->lr_mode = (uint64_t)vap->va_mode;
587 if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
588 lr->lr_uid = fuidp->z_fuid_owner;
590 lr->lr_uid = (uint64_t)vap->va_uid;
592 if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
593 lr->lr_gid = fuidp->z_fuid_group;
595 lr->lr_gid = (uint64_t)vap->va_gid;
597 lr->lr_size = (uint64_t)vap->va_size;
598 ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
599 ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
600 start = (lr_setattr_t *)(lr + 1);
601 if (vap->va_mask & AT_XVATTR) {
602 zfs_log_xvattr((lr_attr_t *)start, xvap);
603 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
607 * Now stick on domain information if any on end
611 (void) zfs_log_fuid_domains(fuidp, start);
613 itx->itx_sync = (zp->z_sync_cnt != 0);
614 zil_itx_assign(zilog, itx, tx);
618 * Handles TX_ACL transactions.
621 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
622 vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
630 size_t aclbytes = vsecp->vsa_aclentsz;
632 if (zil_replaying(zilog, tx) || zp->z_unlinked)
635 txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
638 if (txtype == TX_ACL)
639 lrsize = sizeof (*lr);
641 lrsize = sizeof (*lrv0);
644 ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
645 (fuidp ? fuidp->z_domain_str_sz : 0) +
646 sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
648 itx = zil_itx_create(txtype, txsize);
650 lr = (lr_acl_t *)&itx->itx_lr;
651 lr->lr_foid = zp->z_id;
652 if (txtype == TX_ACL) {
653 lr->lr_acl_bytes = aclbytes;
654 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
655 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
656 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
657 lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
659 lr->lr_acl_flags = 0;
661 lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
663 if (txtype == TX_ACL_V0) {
664 lrv0 = (lr_acl_v0_t *)lr;
665 bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
667 void *start = (ace_t *)(lr + 1);
669 bcopy(vsecp->vsa_aclentp, start, aclbytes);
671 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
674 start = zfs_log_fuid_ids(fuidp, start);
675 (void) zfs_log_fuid_domains(fuidp, start);
679 itx->itx_sync = (zp->z_sync_cnt != 0);
680 zil_itx_assign(zilog, itx, tx);