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.
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/sysmacros.h>
29 #include <sys/cmn_err.h>
31 #include <sys/thread.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>
45 #include <sys/zfs_fuid.h>
47 #include <sys/dsl_dataset.h>
50 * These zfs_log_* functions must be called within a dmu tx, in one
51 * of 2 contexts depending on zilog->z_replay:
55 * We need to record the transaction so that if it is committed to
56 * the Intent Log then it can be replayed. An intent log transaction
57 * structure (itx_t) is allocated and all the information necessary to
58 * possibly replay the transaction is saved in it. The itx is then assigned
59 * a sequence number and inserted in the in-memory list anchored in the zilog.
63 * We need to mark the intent log record as replayed in the log header.
64 * This is done in the same transaction as the replay so that they
69 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
71 int isxvattr = (vap->va_mask & AT_XVATTR);
74 if (vsecp == NULL && !isxvattr)
76 if (vsecp && isxvattr)
77 return (TX_CREATE_ACL_ATTR);
79 return (TX_CREATE_ACL);
81 return (TX_CREATE_ATTR);
84 if (vsecp == NULL && !isxvattr)
86 if (vsecp && isxvattr)
87 return (TX_MKDIR_ACL_ATTR);
89 return (TX_MKDIR_ACL);
91 return (TX_MKDIR_ATTR);
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.
109 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
118 xoap = xva_getxoptattr(xvap);
121 lrattr->lr_attr_masksize = xvap->xva_mapsize;
122 bitmap = &lrattr->lr_attr_bitmap;
123 for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
124 *bitmap = xvap->xva_reqattrmap[i];
127 /* Now pack the attributes up in a single uint64_t */
128 attrs = (uint64_t *)bitmap;
130 scanstamp = (caddr_t)(crtime + 2);
132 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
133 *attrs |= (xoap->xoa_readonly == 0) ? 0 :
135 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
136 *attrs |= (xoap->xoa_hidden == 0) ? 0 :
138 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
139 *attrs |= (xoap->xoa_system == 0) ? 0 :
141 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
142 *attrs |= (xoap->xoa_archive == 0) ? 0 :
144 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
145 *attrs |= (xoap->xoa_immutable == 0) ? 0 :
147 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
148 *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
150 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
151 *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
153 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
154 *attrs |= (xoap->xoa_opaque == 0) ? 0 :
156 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
157 *attrs |= (xoap->xoa_nodump == 0) ? 0 :
159 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
160 *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
162 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
163 *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
165 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
166 ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
167 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
168 bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
169 if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
170 *attrs |= (xoap->xoa_reparse == 0) ? 0 :
172 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
173 *attrs |= (xoap->xoa_offline == 0) ? 0 :
175 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
176 *attrs |= (xoap->xoa_sparse == 0) ? 0 :
181 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
184 uint64_t *fuidloc = start;
186 /* First copy in the ACE FUIDs */
187 for (zfuid = list_head(&fuidp->z_fuids); zfuid;
188 zfuid = list_next(&fuidp->z_fuids, zfuid)) {
189 *fuidloc++ = zfuid->z_logfuid;
196 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
198 zfs_fuid_domain_t *zdomain;
200 /* now copy in the domain info, if any */
201 if (fuidp->z_domain_str_sz != 0) {
202 for (zdomain = list_head(&fuidp->z_domains); zdomain;
203 zdomain = list_next(&fuidp->z_domains, zdomain)) {
204 bcopy((void *)zdomain->z_domain, start,
205 strlen(zdomain->z_domain) + 1);
206 start = (caddr_t)start +
207 strlen(zdomain->z_domain) + 1;
214 * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
215 * TX_MKDIR_ATTR and TX_MKXATTR
218 * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
219 * domain information appended prior to the name. In this case the
220 * uid/gid in the log record will be a log centric FUID.
222 * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
223 * may contain attributes, ACL and optional fuid information.
225 * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
226 * and ACL and normal users/groups in the ACEs.
228 * There may be an optional xvattr attribute information similar
229 * to zfs_log_setattr.
231 * Also, after the file name "domain" strings may be appended.
234 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
235 znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
236 zfs_fuid_info_t *fuidp, vattr_t *vap)
240 lr_acl_create_t *lracl;
244 xvattr_t *xvap = (xvattr_t *)vap;
247 size_t namesize = strlen(name) + 1;
250 if (zil_replaying(zilog, tx))
254 * If we have FUIDs present then add in space for
255 * domains and ACE fuid's if any.
258 fuidsz += fuidp->z_domain_str_sz;
259 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
262 if (vap->va_mask & AT_XVATTR)
263 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
265 if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
266 (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
267 (int)txtype == TX_MKXATTR) {
268 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
269 lrsize = sizeof (*lr);
271 aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
273 sizeof (lr_acl_create_t) + namesize + fuidsz +
274 ZIL_ACE_LENGTH(aclsize) + xvatsize;
275 lrsize = sizeof (lr_acl_create_t);
278 itx = zil_itx_create(txtype, txsize);
280 lr = (lr_create_t *)&itx->itx_lr;
281 lr->lr_doid = dzp->z_id;
282 lr->lr_foid = zp->z_id;
283 lr->lr_mode = zp->z_mode;
284 if (!IS_EPHEMERAL(zp->z_uid)) {
285 lr->lr_uid = (uint64_t)zp->z_uid;
287 lr->lr_uid = fuidp->z_fuid_owner;
289 if (!IS_EPHEMERAL(zp->z_gid)) {
290 lr->lr_gid = (uint64_t)zp->z_gid;
292 lr->lr_gid = fuidp->z_fuid_group;
294 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
296 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
297 lr->lr_crtime, sizeof (uint64_t) * 2);
299 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zp->z_zfsvfs), &lr->lr_rdev,
300 sizeof (lr->lr_rdev)) != 0)
304 * Fill in xvattr info if any
306 if (vap->va_mask & AT_XVATTR) {
307 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
308 end = (caddr_t)lr + lrsize + xvatsize;
310 end = (caddr_t)lr + lrsize;
313 /* Now fill in any ACL info */
316 lracl = (lr_acl_create_t *)&itx->itx_lr;
317 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
318 lracl->lr_acl_bytes = aclsize;
319 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
320 lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
321 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
322 lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
324 lracl->lr_acl_flags = 0;
326 bcopy(vsecp->vsa_aclentp, end, aclsize);
327 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
330 /* drop in FUID info */
332 end = zfs_log_fuid_ids(fuidp, end);
333 end = zfs_log_fuid_domains(fuidp, end);
336 * Now place file name in log record
338 bcopy(name, end, namesize);
340 zil_itx_assign(zilog, itx, tx);
344 * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
347 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
348 znode_t *dzp, char *name, uint64_t foid)
352 size_t namesize = strlen(name) + 1;
354 if (zil_replaying(zilog, tx))
357 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
358 lr = (lr_remove_t *)&itx->itx_lr;
359 lr->lr_doid = dzp->z_id;
360 bcopy(name, (char *)(lr + 1), namesize);
364 zil_itx_assign(zilog, itx, tx);
368 * zfs_log_link() handles TX_LINK transactions.
371 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
372 znode_t *dzp, znode_t *zp, char *name)
376 size_t namesize = strlen(name) + 1;
378 if (zil_replaying(zilog, tx))
381 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
382 lr = (lr_link_t *)&itx->itx_lr;
383 lr->lr_doid = dzp->z_id;
384 lr->lr_link_obj = zp->z_id;
385 bcopy(name, (char *)(lr + 1), namesize);
387 zil_itx_assign(zilog, itx, tx);
391 * zfs_log_symlink() handles TX_SYMLINK transactions.
394 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
395 znode_t *dzp, znode_t *zp, char *name, char *link)
399 size_t namesize = strlen(name) + 1;
400 size_t linksize = strlen(link) + 1;
402 if (zil_replaying(zilog, tx))
405 itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
406 lr = (lr_create_t *)&itx->itx_lr;
407 lr->lr_doid = dzp->z_id;
408 lr->lr_foid = zp->z_id;
409 lr->lr_uid = zp->z_uid;
410 lr->lr_gid = zp->z_gid;
411 lr->lr_mode = zp->z_mode;
412 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
414 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
415 lr->lr_crtime, sizeof (uint64_t) * 2);
416 bcopy(name, (char *)(lr + 1), namesize);
417 bcopy(link, (char *)(lr + 1) + namesize, linksize);
419 zil_itx_assign(zilog, itx, tx);
423 * zfs_log_rename() handles TX_RENAME transactions.
426 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
427 znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
431 size_t snamesize = strlen(sname) + 1;
432 size_t dnamesize = strlen(dname) + 1;
434 if (zil_replaying(zilog, tx))
437 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
438 lr = (lr_rename_t *)&itx->itx_lr;
439 lr->lr_sdoid = sdzp->z_id;
440 lr->lr_tdoid = tdzp->z_id;
441 bcopy(sname, (char *)(lr + 1), snamesize);
442 bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
443 itx->itx_oid = szp->z_id;
445 zil_itx_assign(zilog, itx, tx);
449 * zfs_log_write() handles TX_WRITE transactions.
451 ssize_t zfs_immediate_write_sz = 32768;
454 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
455 znode_t *zp, offset_t off, ssize_t resid, int ioflag)
457 itx_wr_state_t write_state;
460 ssize_t immediate_write_sz;
462 if (zil_replaying(zilog, tx) || zp->z_unlinked)
465 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
466 ? 0 : zfs_immediate_write_sz;
468 slogging = spa_has_slogs(zilog->zl_spa) &&
469 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
470 if (resid > immediate_write_sz && !slogging && resid <= zp->z_blksz)
471 write_state = WR_INDIRECT;
472 else if (ioflag & (FSYNC | FDSYNC))
473 write_state = WR_COPIED;
475 write_state = WR_NEED_COPY;
477 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
478 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
487 * If the write would overflow the largest block then split it.
489 if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA)
490 len = SPA_MAXBLOCKSIZE >> 1;
494 itx = zil_itx_create(txtype, sizeof (*lr) +
495 (write_state == WR_COPIED ? len : 0));
496 lr = (lr_write_t *)&itx->itx_lr;
497 if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
498 zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
499 zil_itx_destroy(itx);
500 itx = zil_itx_create(txtype, sizeof (*lr));
501 lr = (lr_write_t *)&itx->itx_lr;
502 write_state = WR_NEED_COPY;
505 itx->itx_wr_state = write_state;
506 if (write_state == WR_NEED_COPY)
508 lr->lr_foid = zp->z_id;
512 BP_ZERO(&lr->lr_blkptr);
514 itx->itx_private = zp->z_zfsvfs;
516 if (!(ioflag & (FSYNC | FDSYNC)) && (zp->z_sync_cnt == 0) &&
518 itx->itx_sync = B_FALSE;
520 zil_itx_assign(zilog, itx, tx);
528 * zfs_log_truncate() handles TX_TRUNCATE transactions.
531 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
532 znode_t *zp, uint64_t off, uint64_t len)
537 if (zil_replaying(zilog, tx) || zp->z_unlinked)
540 itx = zil_itx_create(txtype, sizeof (*lr));
541 lr = (lr_truncate_t *)&itx->itx_lr;
542 lr->lr_foid = zp->z_id;
546 itx->itx_sync = (zp->z_sync_cnt != 0);
547 zil_itx_assign(zilog, itx, tx);
551 * zfs_log_setattr() handles TX_SETATTR transactions.
554 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
555 znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
559 xvattr_t *xvap = (xvattr_t *)vap;
560 size_t recsize = sizeof (lr_setattr_t);
563 if (zil_replaying(zilog, tx) || zp->z_unlinked)
567 * If XVATTR set, then log record size needs to allow
568 * for lr_attr_t + xvattr mask, mapsize and create time
569 * plus actual attribute values
571 if (vap->va_mask & AT_XVATTR)
572 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
575 recsize += fuidp->z_domain_str_sz;
577 itx = zil_itx_create(txtype, recsize);
578 lr = (lr_setattr_t *)&itx->itx_lr;
579 lr->lr_foid = zp->z_id;
580 lr->lr_mask = (uint64_t)mask_applied;
581 lr->lr_mode = (uint64_t)vap->va_mode;
582 if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
583 lr->lr_uid = fuidp->z_fuid_owner;
585 lr->lr_uid = (uint64_t)vap->va_uid;
587 if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
588 lr->lr_gid = fuidp->z_fuid_group;
590 lr->lr_gid = (uint64_t)vap->va_gid;
592 lr->lr_size = (uint64_t)vap->va_size;
593 ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
594 ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
595 start = (lr_setattr_t *)(lr + 1);
596 if (vap->va_mask & AT_XVATTR) {
597 zfs_log_xvattr((lr_attr_t *)start, xvap);
598 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
602 * Now stick on domain information if any on end
606 (void) zfs_log_fuid_domains(fuidp, start);
608 itx->itx_sync = (zp->z_sync_cnt != 0);
609 zil_itx_assign(zilog, itx, tx);
613 * zfs_log_acl() handles TX_ACL transactions.
616 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
617 vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
625 size_t aclbytes = vsecp->vsa_aclentsz;
627 if (zil_replaying(zilog, tx) || zp->z_unlinked)
630 txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
633 if (txtype == TX_ACL)
634 lrsize = sizeof (*lr);
636 lrsize = sizeof (*lrv0);
639 ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
640 (fuidp ? fuidp->z_domain_str_sz : 0) +
641 sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
643 itx = zil_itx_create(txtype, txsize);
645 lr = (lr_acl_t *)&itx->itx_lr;
646 lr->lr_foid = zp->z_id;
647 if (txtype == TX_ACL) {
648 lr->lr_acl_bytes = aclbytes;
649 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
650 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
651 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
652 lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
654 lr->lr_acl_flags = 0;
656 lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
658 if (txtype == TX_ACL_V0) {
659 lrv0 = (lr_acl_v0_t *)lr;
660 bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
662 void *start = (ace_t *)(lr + 1);
664 bcopy(vsecp->vsa_aclentp, start, aclbytes);
666 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
669 start = zfs_log_fuid_ids(fuidp, start);
670 (void) zfs_log_fuid_domains(fuidp, start);
674 itx->itx_sync = (zp->z_sync_cnt != 0);
675 zil_itx_assign(zilog, itx, tx);