1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy /* 22eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23eda14cbcSMatt Macy * Copyright (c) 2012 Cyril Plisko. All rights reserved. 24eda14cbcSMatt Macy * Copyright (c) 2013, 2017 by Delphix. All rights reserved. 25eda14cbcSMatt Macy */ 26eda14cbcSMatt Macy 27eda14cbcSMatt Macy #include <sys/types.h> 28eda14cbcSMatt Macy #include <sys/param.h> 29eda14cbcSMatt Macy #include <sys/sysmacros.h> 30eda14cbcSMatt Macy #include <sys/cmn_err.h> 31eda14cbcSMatt Macy #include <sys/kmem.h> 32eda14cbcSMatt Macy #include <sys/thread.h> 33eda14cbcSMatt Macy #include <sys/file.h> 34eda14cbcSMatt Macy #include <sys/fcntl.h> 35eda14cbcSMatt Macy #include <sys/vfs.h> 36eda14cbcSMatt Macy #include <sys/fs/zfs.h> 37eda14cbcSMatt Macy #include <sys/zfs_znode.h> 38eda14cbcSMatt Macy #include <sys/zfs_dir.h> 39eda14cbcSMatt Macy #include <sys/zfs_acl.h> 40eda14cbcSMatt Macy #include <sys/zfs_fuid.h> 41eda14cbcSMatt Macy #include <sys/zfs_vnops.h> 42eda14cbcSMatt Macy #include <sys/spa.h> 43eda14cbcSMatt Macy #include <sys/zil.h> 44eda14cbcSMatt Macy #include <sys/byteorder.h> 45eda14cbcSMatt Macy #include <sys/stat.h> 46eda14cbcSMatt Macy #include <sys/acl.h> 47eda14cbcSMatt Macy #include <sys/atomic.h> 48eda14cbcSMatt Macy #include <sys/cred.h> 49eda14cbcSMatt Macy #include <sys/zpl.h> 50eda14cbcSMatt Macy 51eda14cbcSMatt Macy /* 52eda14cbcSMatt Macy * NB: FreeBSD expects to be able to do vnode locking in lookup and 53eda14cbcSMatt Macy * hold the locks across all subsequent VOPs until vput is called. 54eda14cbcSMatt Macy * This means that its zfs vnops routines can't do any internal locking. 55eda14cbcSMatt Macy * In order to have the same contract as the Linux vnops there would 56eda14cbcSMatt Macy * needed to be duplicate locked vnops. If the vnops were used more widely 57eda14cbcSMatt Macy * in common code this would likely be preferable. However, currently 58eda14cbcSMatt Macy * this is the only file where this is the case. 59eda14cbcSMatt Macy */ 60eda14cbcSMatt Macy 61eda14cbcSMatt Macy /* 62eda14cbcSMatt Macy * Functions to replay ZFS intent log (ZIL) records 63eda14cbcSMatt Macy * The functions are called through a function vector (zfs_replay_vector) 64eda14cbcSMatt Macy * which is indexed by the transaction type. 65eda14cbcSMatt Macy */ 66eda14cbcSMatt Macy 67eda14cbcSMatt Macy static void 68eda14cbcSMatt Macy zfs_init_vattr(vattr_t *vap, uint64_t mask, uint64_t mode, 69eda14cbcSMatt Macy uint64_t uid, uint64_t gid, uint64_t rdev, uint64_t nodeid) 70eda14cbcSMatt Macy { 71eda14cbcSMatt Macy bzero(vap, sizeof (*vap)); 72eda14cbcSMatt Macy vap->va_mask = (uint_t)mask; 73eda14cbcSMatt Macy vap->va_mode = mode; 74*1f88aa09SMartin Matuska #if defined(__FreeBSD__) || defined(__APPLE__) 75eda14cbcSMatt Macy vap->va_type = IFTOVT(mode); 76eda14cbcSMatt Macy #endif 77eda14cbcSMatt Macy vap->va_uid = (uid_t)(IS_EPHEMERAL(uid)) ? -1 : uid; 78eda14cbcSMatt Macy vap->va_gid = (gid_t)(IS_EPHEMERAL(gid)) ? -1 : gid; 79eda14cbcSMatt Macy vap->va_rdev = zfs_cmpldev(rdev); 80eda14cbcSMatt Macy vap->va_nodeid = nodeid; 81eda14cbcSMatt Macy } 82eda14cbcSMatt Macy 83eda14cbcSMatt Macy /* ARGSUSED */ 84eda14cbcSMatt Macy static int 85eda14cbcSMatt Macy zfs_replay_error(void *arg1, void *arg2, boolean_t byteswap) 86eda14cbcSMatt Macy { 87eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP)); 88eda14cbcSMatt Macy } 89eda14cbcSMatt Macy 90eda14cbcSMatt Macy static void 91eda14cbcSMatt Macy zfs_replay_xvattr(lr_attr_t *lrattr, xvattr_t *xvap) 92eda14cbcSMatt Macy { 93eda14cbcSMatt Macy xoptattr_t *xoap = NULL; 94eda14cbcSMatt Macy uint64_t *attrs; 95eda14cbcSMatt Macy uint64_t *crtime; 96eda14cbcSMatt Macy uint32_t *bitmap; 97eda14cbcSMatt Macy void *scanstamp; 98eda14cbcSMatt Macy int i; 99eda14cbcSMatt Macy 100eda14cbcSMatt Macy xvap->xva_vattr.va_mask |= ATTR_XVATTR; 101eda14cbcSMatt Macy if ((xoap = xva_getxoptattr(xvap)) == NULL) { 102eda14cbcSMatt Macy xvap->xva_vattr.va_mask &= ~ATTR_XVATTR; /* shouldn't happen */ 103eda14cbcSMatt Macy return; 104eda14cbcSMatt Macy } 105eda14cbcSMatt Macy 106eda14cbcSMatt Macy ASSERT(lrattr->lr_attr_masksize == xvap->xva_mapsize); 107eda14cbcSMatt Macy 108eda14cbcSMatt Macy bitmap = &lrattr->lr_attr_bitmap; 109eda14cbcSMatt Macy for (i = 0; i != lrattr->lr_attr_masksize; i++, bitmap++) 110eda14cbcSMatt Macy xvap->xva_reqattrmap[i] = *bitmap; 111eda14cbcSMatt Macy 112eda14cbcSMatt Macy attrs = (uint64_t *)(lrattr + lrattr->lr_attr_masksize - 1); 113eda14cbcSMatt Macy crtime = attrs + 1; 114eda14cbcSMatt Macy scanstamp = (caddr_t)(crtime + 2); 115eda14cbcSMatt Macy 116eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) 117eda14cbcSMatt Macy xoap->xoa_hidden = ((*attrs & XAT0_HIDDEN) != 0); 118eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) 119eda14cbcSMatt Macy xoap->xoa_system = ((*attrs & XAT0_SYSTEM) != 0); 120eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) 121eda14cbcSMatt Macy xoap->xoa_archive = ((*attrs & XAT0_ARCHIVE) != 0); 122eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_READONLY)) 123eda14cbcSMatt Macy xoap->xoa_readonly = ((*attrs & XAT0_READONLY) != 0); 124eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) 125eda14cbcSMatt Macy xoap->xoa_immutable = ((*attrs & XAT0_IMMUTABLE) != 0); 126eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) 127eda14cbcSMatt Macy xoap->xoa_nounlink = ((*attrs & XAT0_NOUNLINK) != 0); 128eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) 129eda14cbcSMatt Macy xoap->xoa_appendonly = ((*attrs & XAT0_APPENDONLY) != 0); 130eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) 131eda14cbcSMatt Macy xoap->xoa_nodump = ((*attrs & XAT0_NODUMP) != 0); 132eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) 133eda14cbcSMatt Macy xoap->xoa_opaque = ((*attrs & XAT0_OPAQUE) != 0); 134eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) 135eda14cbcSMatt Macy xoap->xoa_av_modified = ((*attrs & XAT0_AV_MODIFIED) != 0); 136eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) 137eda14cbcSMatt Macy xoap->xoa_av_quarantined = 138eda14cbcSMatt Macy ((*attrs & XAT0_AV_QUARANTINED) != 0); 139eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) 140eda14cbcSMatt Macy ZFS_TIME_DECODE(&xoap->xoa_createtime, crtime); 141eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 142eda14cbcSMatt Macy ASSERT(!XVA_ISSET_REQ(xvap, XAT_PROJID)); 143eda14cbcSMatt Macy 144eda14cbcSMatt Macy bcopy(scanstamp, xoap->xoa_av_scanstamp, AV_SCANSTAMP_SZ); 145eda14cbcSMatt Macy } else if (XVA_ISSET_REQ(xvap, XAT_PROJID)) { 146eda14cbcSMatt Macy /* 147eda14cbcSMatt Macy * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid 148eda14cbcSMatt Macy * at the same time, so we can share the same space. 149eda14cbcSMatt Macy */ 150eda14cbcSMatt Macy bcopy(scanstamp, &xoap->xoa_projid, sizeof (uint64_t)); 151eda14cbcSMatt Macy } 152eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) 153eda14cbcSMatt Macy xoap->xoa_reparse = ((*attrs & XAT0_REPARSE) != 0); 154eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) 155eda14cbcSMatt Macy xoap->xoa_offline = ((*attrs & XAT0_OFFLINE) != 0); 156eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) 157eda14cbcSMatt Macy xoap->xoa_sparse = ((*attrs & XAT0_SPARSE) != 0); 158eda14cbcSMatt Macy if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) 159eda14cbcSMatt Macy xoap->xoa_projinherit = ((*attrs & XAT0_PROJINHERIT) != 0); 160eda14cbcSMatt Macy } 161eda14cbcSMatt Macy 162eda14cbcSMatt Macy static int 163eda14cbcSMatt Macy zfs_replay_domain_cnt(uint64_t uid, uint64_t gid) 164eda14cbcSMatt Macy { 165eda14cbcSMatt Macy uint64_t uid_idx; 166eda14cbcSMatt Macy uint64_t gid_idx; 167eda14cbcSMatt Macy int domcnt = 0; 168eda14cbcSMatt Macy 169eda14cbcSMatt Macy uid_idx = FUID_INDEX(uid); 170eda14cbcSMatt Macy gid_idx = FUID_INDEX(gid); 171eda14cbcSMatt Macy if (uid_idx) 172eda14cbcSMatt Macy domcnt++; 173eda14cbcSMatt Macy if (gid_idx > 0 && gid_idx != uid_idx) 174eda14cbcSMatt Macy domcnt++; 175eda14cbcSMatt Macy 176eda14cbcSMatt Macy return (domcnt); 177eda14cbcSMatt Macy } 178eda14cbcSMatt Macy 179eda14cbcSMatt Macy static void * 180eda14cbcSMatt Macy zfs_replay_fuid_domain_common(zfs_fuid_info_t *fuid_infop, void *start, 181eda14cbcSMatt Macy int domcnt) 182eda14cbcSMatt Macy { 183eda14cbcSMatt Macy int i; 184eda14cbcSMatt Macy 185eda14cbcSMatt Macy for (i = 0; i != domcnt; i++) { 186eda14cbcSMatt Macy fuid_infop->z_domain_table[i] = start; 187eda14cbcSMatt Macy start = (caddr_t)start + strlen(start) + 1; 188eda14cbcSMatt Macy } 189eda14cbcSMatt Macy 190eda14cbcSMatt Macy return (start); 191eda14cbcSMatt Macy } 192eda14cbcSMatt Macy 193eda14cbcSMatt Macy /* 194eda14cbcSMatt Macy * Set the uid/gid in the fuid_info structure. 195eda14cbcSMatt Macy */ 196eda14cbcSMatt Macy static void 197eda14cbcSMatt Macy zfs_replay_fuid_ugid(zfs_fuid_info_t *fuid_infop, uint64_t uid, uint64_t gid) 198eda14cbcSMatt Macy { 199eda14cbcSMatt Macy /* 200eda14cbcSMatt Macy * If owner or group are log specific FUIDs then slurp up 201eda14cbcSMatt Macy * domain information and build zfs_fuid_info_t 202eda14cbcSMatt Macy */ 203eda14cbcSMatt Macy if (IS_EPHEMERAL(uid)) 204eda14cbcSMatt Macy fuid_infop->z_fuid_owner = uid; 205eda14cbcSMatt Macy 206eda14cbcSMatt Macy if (IS_EPHEMERAL(gid)) 207eda14cbcSMatt Macy fuid_infop->z_fuid_group = gid; 208eda14cbcSMatt Macy } 209eda14cbcSMatt Macy 210eda14cbcSMatt Macy /* 211eda14cbcSMatt Macy * Load fuid domains into fuid_info_t 212eda14cbcSMatt Macy */ 213eda14cbcSMatt Macy static zfs_fuid_info_t * 214eda14cbcSMatt Macy zfs_replay_fuid_domain(void *buf, void **end, uint64_t uid, uint64_t gid) 215eda14cbcSMatt Macy { 216eda14cbcSMatt Macy int domcnt; 217eda14cbcSMatt Macy 218eda14cbcSMatt Macy zfs_fuid_info_t *fuid_infop; 219eda14cbcSMatt Macy 220eda14cbcSMatt Macy fuid_infop = zfs_fuid_info_alloc(); 221eda14cbcSMatt Macy 222eda14cbcSMatt Macy domcnt = zfs_replay_domain_cnt(uid, gid); 223eda14cbcSMatt Macy 224eda14cbcSMatt Macy if (domcnt == 0) 225eda14cbcSMatt Macy return (fuid_infop); 226eda14cbcSMatt Macy 227eda14cbcSMatt Macy fuid_infop->z_domain_table = 228eda14cbcSMatt Macy kmem_zalloc(domcnt * sizeof (char *), KM_SLEEP); 229eda14cbcSMatt Macy 230eda14cbcSMatt Macy zfs_replay_fuid_ugid(fuid_infop, uid, gid); 231eda14cbcSMatt Macy 232eda14cbcSMatt Macy fuid_infop->z_domain_cnt = domcnt; 233eda14cbcSMatt Macy *end = zfs_replay_fuid_domain_common(fuid_infop, buf, domcnt); 234eda14cbcSMatt Macy return (fuid_infop); 235eda14cbcSMatt Macy } 236eda14cbcSMatt Macy 237eda14cbcSMatt Macy /* 238eda14cbcSMatt Macy * load zfs_fuid_t's and fuid_domains into fuid_info_t 239eda14cbcSMatt Macy */ 240eda14cbcSMatt Macy static zfs_fuid_info_t * 241eda14cbcSMatt Macy zfs_replay_fuids(void *start, void **end, int idcnt, int domcnt, uint64_t uid, 242eda14cbcSMatt Macy uint64_t gid) 243eda14cbcSMatt Macy { 244eda14cbcSMatt Macy uint64_t *log_fuid = (uint64_t *)start; 245eda14cbcSMatt Macy zfs_fuid_info_t *fuid_infop; 246eda14cbcSMatt Macy int i; 247eda14cbcSMatt Macy 248eda14cbcSMatt Macy fuid_infop = zfs_fuid_info_alloc(); 249eda14cbcSMatt Macy fuid_infop->z_domain_cnt = domcnt; 250eda14cbcSMatt Macy 251eda14cbcSMatt Macy fuid_infop->z_domain_table = 252eda14cbcSMatt Macy kmem_zalloc(domcnt * sizeof (char *), KM_SLEEP); 253eda14cbcSMatt Macy 254eda14cbcSMatt Macy for (i = 0; i != idcnt; i++) { 255eda14cbcSMatt Macy zfs_fuid_t *zfuid; 256eda14cbcSMatt Macy 257eda14cbcSMatt Macy zfuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP); 258eda14cbcSMatt Macy zfuid->z_logfuid = *log_fuid; 259eda14cbcSMatt Macy zfuid->z_id = -1; 260eda14cbcSMatt Macy zfuid->z_domidx = 0; 261eda14cbcSMatt Macy list_insert_tail(&fuid_infop->z_fuids, zfuid); 262eda14cbcSMatt Macy log_fuid++; 263eda14cbcSMatt Macy } 264eda14cbcSMatt Macy 265eda14cbcSMatt Macy zfs_replay_fuid_ugid(fuid_infop, uid, gid); 266eda14cbcSMatt Macy 267eda14cbcSMatt Macy *end = zfs_replay_fuid_domain_common(fuid_infop, log_fuid, domcnt); 268eda14cbcSMatt Macy return (fuid_infop); 269eda14cbcSMatt Macy } 270eda14cbcSMatt Macy 271eda14cbcSMatt Macy static void 272eda14cbcSMatt Macy zfs_replay_swap_attrs(lr_attr_t *lrattr) 273eda14cbcSMatt Macy { 274eda14cbcSMatt Macy /* swap the lr_attr structure */ 275eda14cbcSMatt Macy byteswap_uint32_array(lrattr, sizeof (*lrattr)); 276eda14cbcSMatt Macy /* swap the bitmap */ 277eda14cbcSMatt Macy byteswap_uint32_array(lrattr + 1, (lrattr->lr_attr_masksize - 1) * 278eda14cbcSMatt Macy sizeof (uint32_t)); 279eda14cbcSMatt Macy /* swap the attributes, create time + 64 bit word for attributes */ 280eda14cbcSMatt Macy byteswap_uint64_array((caddr_t)(lrattr + 1) + (sizeof (uint32_t) * 281eda14cbcSMatt Macy (lrattr->lr_attr_masksize - 1)), 3 * sizeof (uint64_t)); 282eda14cbcSMatt Macy } 283eda14cbcSMatt Macy 284eda14cbcSMatt Macy /* 285eda14cbcSMatt Macy * Replay file create with optional ACL, xvattr information as well 286eda14cbcSMatt Macy * as option FUID information. 287eda14cbcSMatt Macy */ 288eda14cbcSMatt Macy static int 289eda14cbcSMatt Macy zfs_replay_create_acl(void *arg1, void *arg2, boolean_t byteswap) 290eda14cbcSMatt Macy { 291eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 292eda14cbcSMatt Macy lr_acl_create_t *lracl = arg2; 293eda14cbcSMatt Macy char *name = NULL; /* location determined later */ 294eda14cbcSMatt Macy lr_create_t *lr = (lr_create_t *)lracl; 295eda14cbcSMatt Macy znode_t *dzp; 296eda14cbcSMatt Macy znode_t *zp; 297eda14cbcSMatt Macy xvattr_t xva; 298eda14cbcSMatt Macy int vflg = 0; 299eda14cbcSMatt Macy vsecattr_t vsec = { 0 }; 300eda14cbcSMatt Macy lr_attr_t *lrattr; 301eda14cbcSMatt Macy void *aclstart; 302eda14cbcSMatt Macy void *fuidstart; 303eda14cbcSMatt Macy size_t xvatlen = 0; 304eda14cbcSMatt Macy uint64_t txtype; 305eda14cbcSMatt Macy uint64_t objid; 306eda14cbcSMatt Macy uint64_t dnodesize; 307eda14cbcSMatt Macy int error; 308eda14cbcSMatt Macy 309eda14cbcSMatt Macy txtype = (lr->lr_common.lrc_txtype & ~TX_CI); 310eda14cbcSMatt Macy if (byteswap) { 311eda14cbcSMatt Macy byteswap_uint64_array(lracl, sizeof (*lracl)); 312eda14cbcSMatt Macy if (txtype == TX_CREATE_ACL_ATTR || 313eda14cbcSMatt Macy txtype == TX_MKDIR_ACL_ATTR) { 314eda14cbcSMatt Macy lrattr = (lr_attr_t *)(caddr_t)(lracl + 1); 315eda14cbcSMatt Macy zfs_replay_swap_attrs(lrattr); 316eda14cbcSMatt Macy xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize); 317eda14cbcSMatt Macy } 318eda14cbcSMatt Macy 319eda14cbcSMatt Macy aclstart = (caddr_t)(lracl + 1) + xvatlen; 320eda14cbcSMatt Macy zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE); 321eda14cbcSMatt Macy /* swap fuids */ 322eda14cbcSMatt Macy if (lracl->lr_fuidcnt) { 323eda14cbcSMatt Macy byteswap_uint64_array((caddr_t)aclstart + 324eda14cbcSMatt Macy ZIL_ACE_LENGTH(lracl->lr_acl_bytes), 325eda14cbcSMatt Macy lracl->lr_fuidcnt * sizeof (uint64_t)); 326eda14cbcSMatt Macy } 327eda14cbcSMatt Macy } 328eda14cbcSMatt Macy 329eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0) 330eda14cbcSMatt Macy return (error); 331eda14cbcSMatt Macy 332eda14cbcSMatt Macy objid = LR_FOID_GET_OBJ(lr->lr_foid); 333eda14cbcSMatt Macy dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT; 334eda14cbcSMatt Macy 335eda14cbcSMatt Macy xva_init(&xva); 336eda14cbcSMatt Macy zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID, 337eda14cbcSMatt Macy lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid); 338eda14cbcSMatt Macy 339eda14cbcSMatt Macy /* 340eda14cbcSMatt Macy * All forms of zfs create (create, mkdir, mkxattrdir, symlink) 341eda14cbcSMatt Macy * eventually end up in zfs_mknode(), which assigns the object's 342eda14cbcSMatt Macy * creation time, generation number, and dnode size. The generic 343eda14cbcSMatt Macy * zfs_create() has no concept of these attributes, so we smuggle 344eda14cbcSMatt Macy * the values inside the vattr's otherwise unused va_ctime, 345eda14cbcSMatt Macy * va_nblocks, and va_fsid fields. 346eda14cbcSMatt Macy */ 347eda14cbcSMatt Macy ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime); 348eda14cbcSMatt Macy xva.xva_vattr.va_nblocks = lr->lr_gen; 349eda14cbcSMatt Macy xva.xva_vattr.va_fsid = dnodesize; 350eda14cbcSMatt Macy 351eda14cbcSMatt Macy error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT); 352eda14cbcSMatt Macy if (error) 353eda14cbcSMatt Macy goto bail; 354eda14cbcSMatt Macy 355eda14cbcSMatt Macy if (lr->lr_common.lrc_txtype & TX_CI) 356eda14cbcSMatt Macy vflg |= FIGNORECASE; 357eda14cbcSMatt Macy switch (txtype) { 358eda14cbcSMatt Macy case TX_CREATE_ACL: 359eda14cbcSMatt Macy aclstart = (caddr_t)(lracl + 1); 360eda14cbcSMatt Macy fuidstart = (caddr_t)aclstart + 361eda14cbcSMatt Macy ZIL_ACE_LENGTH(lracl->lr_acl_bytes); 362eda14cbcSMatt Macy zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart, 363eda14cbcSMatt Macy (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt, 364eda14cbcSMatt Macy lr->lr_uid, lr->lr_gid); 365eda14cbcSMatt Macy /* FALLTHROUGH */ 366eda14cbcSMatt Macy case TX_CREATE_ACL_ATTR: 367eda14cbcSMatt Macy if (name == NULL) { 368eda14cbcSMatt Macy lrattr = (lr_attr_t *)(caddr_t)(lracl + 1); 369eda14cbcSMatt Macy xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize); 370eda14cbcSMatt Macy xva.xva_vattr.va_mask |= ATTR_XVATTR; 371eda14cbcSMatt Macy zfs_replay_xvattr(lrattr, &xva); 372eda14cbcSMatt Macy } 373eda14cbcSMatt Macy vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS; 374eda14cbcSMatt Macy vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen; 375eda14cbcSMatt Macy vsec.vsa_aclcnt = lracl->lr_aclcnt; 376eda14cbcSMatt Macy vsec.vsa_aclentsz = lracl->lr_acl_bytes; 377eda14cbcSMatt Macy vsec.vsa_aclflags = lracl->lr_acl_flags; 378eda14cbcSMatt Macy if (zfsvfs->z_fuid_replay == NULL) { 379eda14cbcSMatt Macy fuidstart = (caddr_t)(lracl + 1) + xvatlen + 380eda14cbcSMatt Macy ZIL_ACE_LENGTH(lracl->lr_acl_bytes); 381eda14cbcSMatt Macy zfsvfs->z_fuid_replay = 382eda14cbcSMatt Macy zfs_replay_fuids(fuidstart, 383eda14cbcSMatt Macy (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt, 384eda14cbcSMatt Macy lr->lr_uid, lr->lr_gid); 385eda14cbcSMatt Macy } 386eda14cbcSMatt Macy 387eda14cbcSMatt Macy error = zfs_create(dzp, name, &xva.xva_vattr, 388eda14cbcSMatt Macy 0, 0, &zp, kcred, vflg, &vsec); 389eda14cbcSMatt Macy break; 390eda14cbcSMatt Macy case TX_MKDIR_ACL: 391eda14cbcSMatt Macy aclstart = (caddr_t)(lracl + 1); 392eda14cbcSMatt Macy fuidstart = (caddr_t)aclstart + 393eda14cbcSMatt Macy ZIL_ACE_LENGTH(lracl->lr_acl_bytes); 394eda14cbcSMatt Macy zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart, 395eda14cbcSMatt Macy (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt, 396eda14cbcSMatt Macy lr->lr_uid, lr->lr_gid); 397eda14cbcSMatt Macy /* FALLTHROUGH */ 398eda14cbcSMatt Macy case TX_MKDIR_ACL_ATTR: 399eda14cbcSMatt Macy if (name == NULL) { 400eda14cbcSMatt Macy lrattr = (lr_attr_t *)(caddr_t)(lracl + 1); 401eda14cbcSMatt Macy xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize); 402eda14cbcSMatt Macy zfs_replay_xvattr(lrattr, &xva); 403eda14cbcSMatt Macy } 404eda14cbcSMatt Macy vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS; 405eda14cbcSMatt Macy vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen; 406eda14cbcSMatt Macy vsec.vsa_aclcnt = lracl->lr_aclcnt; 407eda14cbcSMatt Macy vsec.vsa_aclentsz = lracl->lr_acl_bytes; 408eda14cbcSMatt Macy vsec.vsa_aclflags = lracl->lr_acl_flags; 409eda14cbcSMatt Macy if (zfsvfs->z_fuid_replay == NULL) { 410eda14cbcSMatt Macy fuidstart = (caddr_t)(lracl + 1) + xvatlen + 411eda14cbcSMatt Macy ZIL_ACE_LENGTH(lracl->lr_acl_bytes); 412eda14cbcSMatt Macy zfsvfs->z_fuid_replay = 413eda14cbcSMatt Macy zfs_replay_fuids(fuidstart, 414eda14cbcSMatt Macy (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt, 415eda14cbcSMatt Macy lr->lr_uid, lr->lr_gid); 416eda14cbcSMatt Macy } 417eda14cbcSMatt Macy error = zfs_mkdir(dzp, name, &xva.xva_vattr, 418eda14cbcSMatt Macy &zp, kcred, vflg, &vsec); 419eda14cbcSMatt Macy break; 420eda14cbcSMatt Macy default: 421eda14cbcSMatt Macy error = SET_ERROR(ENOTSUP); 422eda14cbcSMatt Macy } 423eda14cbcSMatt Macy 424eda14cbcSMatt Macy bail: 425eda14cbcSMatt Macy if (error == 0 && zp != NULL) { 426eda14cbcSMatt Macy #ifdef __FreeBSD__ 427eda14cbcSMatt Macy VOP_UNLOCK1(ZTOV(zp)); 428eda14cbcSMatt Macy #endif 429eda14cbcSMatt Macy zrele(zp); 430eda14cbcSMatt Macy } 431eda14cbcSMatt Macy zrele(dzp); 432eda14cbcSMatt Macy 433eda14cbcSMatt Macy if (zfsvfs->z_fuid_replay) 434eda14cbcSMatt Macy zfs_fuid_info_free(zfsvfs->z_fuid_replay); 435eda14cbcSMatt Macy zfsvfs->z_fuid_replay = NULL; 436eda14cbcSMatt Macy 437eda14cbcSMatt Macy return (error); 438eda14cbcSMatt Macy } 439eda14cbcSMatt Macy 440eda14cbcSMatt Macy static int 441eda14cbcSMatt Macy zfs_replay_create(void *arg1, void *arg2, boolean_t byteswap) 442eda14cbcSMatt Macy { 443eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 444eda14cbcSMatt Macy lr_create_t *lr = arg2; 445eda14cbcSMatt Macy char *name = NULL; /* location determined later */ 446eda14cbcSMatt Macy char *link; /* symlink content follows name */ 447eda14cbcSMatt Macy znode_t *dzp; 448eda14cbcSMatt Macy znode_t *zp = NULL; 449eda14cbcSMatt Macy xvattr_t xva; 450eda14cbcSMatt Macy int vflg = 0; 451eda14cbcSMatt Macy size_t lrsize = sizeof (lr_create_t); 452eda14cbcSMatt Macy lr_attr_t *lrattr; 453eda14cbcSMatt Macy void *start; 454eda14cbcSMatt Macy size_t xvatlen; 455eda14cbcSMatt Macy uint64_t txtype; 456eda14cbcSMatt Macy uint64_t objid; 457eda14cbcSMatt Macy uint64_t dnodesize; 458eda14cbcSMatt Macy int error; 459eda14cbcSMatt Macy 460eda14cbcSMatt Macy txtype = (lr->lr_common.lrc_txtype & ~TX_CI); 461eda14cbcSMatt Macy if (byteswap) { 462eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 463eda14cbcSMatt Macy if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR) 464eda14cbcSMatt Macy zfs_replay_swap_attrs((lr_attr_t *)(lr + 1)); 465eda14cbcSMatt Macy } 466eda14cbcSMatt Macy 467eda14cbcSMatt Macy 468eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0) 469eda14cbcSMatt Macy return (error); 470eda14cbcSMatt Macy 471eda14cbcSMatt Macy objid = LR_FOID_GET_OBJ(lr->lr_foid); 472eda14cbcSMatt Macy dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT; 473eda14cbcSMatt Macy 474eda14cbcSMatt Macy xva_init(&xva); 475eda14cbcSMatt Macy zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID, 476eda14cbcSMatt Macy lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid); 477eda14cbcSMatt Macy 478eda14cbcSMatt Macy /* 479eda14cbcSMatt Macy * All forms of zfs create (create, mkdir, mkxattrdir, symlink) 480eda14cbcSMatt Macy * eventually end up in zfs_mknode(), which assigns the object's 481eda14cbcSMatt Macy * creation time, generation number, and dnode slot count. The 482eda14cbcSMatt Macy * generic zfs_create() has no concept of these attributes, so 483eda14cbcSMatt Macy * we smuggle the values inside the vattr's otherwise unused 484eda14cbcSMatt Macy * va_ctime, va_nblocks, and va_fsid fields. 485eda14cbcSMatt Macy */ 486eda14cbcSMatt Macy ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime); 487eda14cbcSMatt Macy xva.xva_vattr.va_nblocks = lr->lr_gen; 488eda14cbcSMatt Macy xva.xva_vattr.va_fsid = dnodesize; 489eda14cbcSMatt Macy 490eda14cbcSMatt Macy error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT); 491eda14cbcSMatt Macy if (error) 492eda14cbcSMatt Macy goto out; 493eda14cbcSMatt Macy 494eda14cbcSMatt Macy if (lr->lr_common.lrc_txtype & TX_CI) 495eda14cbcSMatt Macy vflg |= FIGNORECASE; 496eda14cbcSMatt Macy 497eda14cbcSMatt Macy /* 498eda14cbcSMatt Macy * Symlinks don't have fuid info, and CIFS never creates 499eda14cbcSMatt Macy * symlinks. 500eda14cbcSMatt Macy * 501eda14cbcSMatt Macy * The _ATTR versions will grab the fuid info in their subcases. 502eda14cbcSMatt Macy */ 503eda14cbcSMatt Macy if ((int)lr->lr_common.lrc_txtype != TX_SYMLINK && 504eda14cbcSMatt Macy (int)lr->lr_common.lrc_txtype != TX_MKDIR_ATTR && 505eda14cbcSMatt Macy (int)lr->lr_common.lrc_txtype != TX_CREATE_ATTR) { 506eda14cbcSMatt Macy start = (lr + 1); 507eda14cbcSMatt Macy zfsvfs->z_fuid_replay = 508eda14cbcSMatt Macy zfs_replay_fuid_domain(start, &start, 509eda14cbcSMatt Macy lr->lr_uid, lr->lr_gid); 510eda14cbcSMatt Macy } 511eda14cbcSMatt Macy 512eda14cbcSMatt Macy switch (txtype) { 513eda14cbcSMatt Macy case TX_CREATE_ATTR: 514eda14cbcSMatt Macy lrattr = (lr_attr_t *)(caddr_t)(lr + 1); 515eda14cbcSMatt Macy xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize); 516eda14cbcSMatt Macy zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva); 517eda14cbcSMatt Macy start = (caddr_t)(lr + 1) + xvatlen; 518eda14cbcSMatt Macy zfsvfs->z_fuid_replay = 519eda14cbcSMatt Macy zfs_replay_fuid_domain(start, &start, 520eda14cbcSMatt Macy lr->lr_uid, lr->lr_gid); 521eda14cbcSMatt Macy name = (char *)start; 522eda14cbcSMatt Macy /* FALLTHROUGH */ 523*1f88aa09SMartin Matuska 524eda14cbcSMatt Macy case TX_CREATE: 525eda14cbcSMatt Macy if (name == NULL) 526eda14cbcSMatt Macy name = (char *)start; 527eda14cbcSMatt Macy 528eda14cbcSMatt Macy error = zfs_create(dzp, name, &xva.xva_vattr, 529eda14cbcSMatt Macy 0, 0, &zp, kcred, vflg, NULL); 530eda14cbcSMatt Macy break; 531eda14cbcSMatt Macy case TX_MKDIR_ATTR: 532eda14cbcSMatt Macy lrattr = (lr_attr_t *)(caddr_t)(lr + 1); 533eda14cbcSMatt Macy xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize); 534eda14cbcSMatt Macy zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva); 535eda14cbcSMatt Macy start = (caddr_t)(lr + 1) + xvatlen; 536eda14cbcSMatt Macy zfsvfs->z_fuid_replay = 537eda14cbcSMatt Macy zfs_replay_fuid_domain(start, &start, 538eda14cbcSMatt Macy lr->lr_uid, lr->lr_gid); 539eda14cbcSMatt Macy name = (char *)start; 540eda14cbcSMatt Macy /* FALLTHROUGH */ 541*1f88aa09SMartin Matuska 542eda14cbcSMatt Macy case TX_MKDIR: 543eda14cbcSMatt Macy if (name == NULL) 544eda14cbcSMatt Macy name = (char *)(lr + 1); 545eda14cbcSMatt Macy 546eda14cbcSMatt Macy error = zfs_mkdir(dzp, name, &xva.xva_vattr, 547eda14cbcSMatt Macy &zp, kcred, vflg, NULL); 548eda14cbcSMatt Macy break; 549eda14cbcSMatt Macy case TX_MKXATTR: 550eda14cbcSMatt Macy error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &zp, kcred); 551eda14cbcSMatt Macy break; 552eda14cbcSMatt Macy case TX_SYMLINK: 553eda14cbcSMatt Macy name = (char *)(lr + 1); 554eda14cbcSMatt Macy link = name + strlen(name) + 1; 555eda14cbcSMatt Macy error = zfs_symlink(dzp, name, &xva.xva_vattr, 556eda14cbcSMatt Macy link, &zp, kcred, vflg); 557eda14cbcSMatt Macy break; 558eda14cbcSMatt Macy default: 559eda14cbcSMatt Macy error = SET_ERROR(ENOTSUP); 560eda14cbcSMatt Macy } 561eda14cbcSMatt Macy 562eda14cbcSMatt Macy out: 563eda14cbcSMatt Macy if (error == 0 && zp != NULL) { 564eda14cbcSMatt Macy #ifdef __FreeBSD__ 565eda14cbcSMatt Macy VOP_UNLOCK1(ZTOV(zp)); 566eda14cbcSMatt Macy #endif 567eda14cbcSMatt Macy zrele(zp); 568eda14cbcSMatt Macy } 569eda14cbcSMatt Macy zrele(dzp); 570eda14cbcSMatt Macy 571eda14cbcSMatt Macy if (zfsvfs->z_fuid_replay) 572eda14cbcSMatt Macy zfs_fuid_info_free(zfsvfs->z_fuid_replay); 573eda14cbcSMatt Macy zfsvfs->z_fuid_replay = NULL; 574eda14cbcSMatt Macy return (error); 575eda14cbcSMatt Macy } 576eda14cbcSMatt Macy 577eda14cbcSMatt Macy static int 578eda14cbcSMatt Macy zfs_replay_remove(void *arg1, void *arg2, boolean_t byteswap) 579eda14cbcSMatt Macy { 580eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 581eda14cbcSMatt Macy lr_remove_t *lr = arg2; 582eda14cbcSMatt Macy char *name = (char *)(lr + 1); /* name follows lr_remove_t */ 583eda14cbcSMatt Macy znode_t *dzp; 584eda14cbcSMatt Macy int error; 585eda14cbcSMatt Macy int vflg = 0; 586eda14cbcSMatt Macy 587eda14cbcSMatt Macy if (byteswap) 588eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 589eda14cbcSMatt Macy 590eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0) 591eda14cbcSMatt Macy return (error); 592eda14cbcSMatt Macy 593eda14cbcSMatt Macy if (lr->lr_common.lrc_txtype & TX_CI) 594eda14cbcSMatt Macy vflg |= FIGNORECASE; 595eda14cbcSMatt Macy 596eda14cbcSMatt Macy switch ((int)lr->lr_common.lrc_txtype) { 597eda14cbcSMatt Macy case TX_REMOVE: 598eda14cbcSMatt Macy error = zfs_remove(dzp, name, kcred, vflg); 599eda14cbcSMatt Macy break; 600eda14cbcSMatt Macy case TX_RMDIR: 601eda14cbcSMatt Macy error = zfs_rmdir(dzp, name, NULL, kcred, vflg); 602eda14cbcSMatt Macy break; 603eda14cbcSMatt Macy default: 604eda14cbcSMatt Macy error = SET_ERROR(ENOTSUP); 605eda14cbcSMatt Macy } 606eda14cbcSMatt Macy 607eda14cbcSMatt Macy zrele(dzp); 608eda14cbcSMatt Macy 609eda14cbcSMatt Macy return (error); 610eda14cbcSMatt Macy } 611eda14cbcSMatt Macy 612eda14cbcSMatt Macy static int 613eda14cbcSMatt Macy zfs_replay_link(void *arg1, void *arg2, boolean_t byteswap) 614eda14cbcSMatt Macy { 615eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 616eda14cbcSMatt Macy lr_link_t *lr = arg2; 617eda14cbcSMatt Macy char *name = (char *)(lr + 1); /* name follows lr_link_t */ 618eda14cbcSMatt Macy znode_t *dzp, *zp; 619eda14cbcSMatt Macy int error; 620eda14cbcSMatt Macy int vflg = 0; 621eda14cbcSMatt Macy 622eda14cbcSMatt Macy if (byteswap) 623eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 624eda14cbcSMatt Macy 625eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0) 626eda14cbcSMatt Macy return (error); 627eda14cbcSMatt Macy 628eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) { 629eda14cbcSMatt Macy zrele(dzp); 630eda14cbcSMatt Macy return (error); 631eda14cbcSMatt Macy } 632eda14cbcSMatt Macy 633eda14cbcSMatt Macy if (lr->lr_common.lrc_txtype & TX_CI) 634eda14cbcSMatt Macy vflg |= FIGNORECASE; 635eda14cbcSMatt Macy 636eda14cbcSMatt Macy error = zfs_link(dzp, zp, name, kcred, vflg); 637eda14cbcSMatt Macy zrele(zp); 638eda14cbcSMatt Macy zrele(dzp); 639eda14cbcSMatt Macy 640eda14cbcSMatt Macy return (error); 641eda14cbcSMatt Macy } 642eda14cbcSMatt Macy 643eda14cbcSMatt Macy static int 644eda14cbcSMatt Macy zfs_replay_rename(void *arg1, void *arg2, boolean_t byteswap) 645eda14cbcSMatt Macy { 646eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 647eda14cbcSMatt Macy lr_rename_t *lr = arg2; 648eda14cbcSMatt Macy char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */ 649eda14cbcSMatt Macy char *tname = sname + strlen(sname) + 1; 650eda14cbcSMatt Macy znode_t *sdzp, *tdzp; 651eda14cbcSMatt Macy int error; 652eda14cbcSMatt Macy int vflg = 0; 653eda14cbcSMatt Macy 654eda14cbcSMatt Macy if (byteswap) 655eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 656eda14cbcSMatt Macy 657eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0) 658eda14cbcSMatt Macy return (error); 659eda14cbcSMatt Macy 660eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) { 661eda14cbcSMatt Macy zrele(sdzp); 662eda14cbcSMatt Macy return (error); 663eda14cbcSMatt Macy } 664eda14cbcSMatt Macy 665eda14cbcSMatt Macy if (lr->lr_common.lrc_txtype & TX_CI) 666eda14cbcSMatt Macy vflg |= FIGNORECASE; 667eda14cbcSMatt Macy 668eda14cbcSMatt Macy error = zfs_rename(sdzp, sname, tdzp, tname, kcred, vflg); 669eda14cbcSMatt Macy 670eda14cbcSMatt Macy zrele(tdzp); 671eda14cbcSMatt Macy zrele(sdzp); 672eda14cbcSMatt Macy return (error); 673eda14cbcSMatt Macy } 674eda14cbcSMatt Macy 675eda14cbcSMatt Macy static int 676eda14cbcSMatt Macy zfs_replay_write(void *arg1, void *arg2, boolean_t byteswap) 677eda14cbcSMatt Macy { 678eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 679eda14cbcSMatt Macy lr_write_t *lr = arg2; 680eda14cbcSMatt Macy char *data = (char *)(lr + 1); /* data follows lr_write_t */ 681eda14cbcSMatt Macy znode_t *zp; 682eda14cbcSMatt Macy int error; 683eda14cbcSMatt Macy uint64_t eod, offset, length; 684eda14cbcSMatt Macy 685eda14cbcSMatt Macy if (byteswap) 686eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 687eda14cbcSMatt Macy 688eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) { 689eda14cbcSMatt Macy /* 690eda14cbcSMatt Macy * As we can log writes out of order, it's possible the 691eda14cbcSMatt Macy * file has been removed. In this case just drop the write 692eda14cbcSMatt Macy * and return success. 693eda14cbcSMatt Macy */ 694eda14cbcSMatt Macy if (error == ENOENT) 695eda14cbcSMatt Macy error = 0; 696eda14cbcSMatt Macy return (error); 697eda14cbcSMatt Macy } 698eda14cbcSMatt Macy 699eda14cbcSMatt Macy offset = lr->lr_offset; 700eda14cbcSMatt Macy length = lr->lr_length; 701eda14cbcSMatt Macy eod = offset + length; /* end of data for this write */ 702eda14cbcSMatt Macy 703eda14cbcSMatt Macy /* 704eda14cbcSMatt Macy * This may be a write from a dmu_sync() for a whole block, 705eda14cbcSMatt Macy * and may extend beyond the current end of the file. 706eda14cbcSMatt Macy * We can't just replay what was written for this TX_WRITE as 707eda14cbcSMatt Macy * a future TX_WRITE2 may extend the eof and the data for that 708eda14cbcSMatt Macy * write needs to be there. So we write the whole block and 709eda14cbcSMatt Macy * reduce the eof. This needs to be done within the single dmu 710eda14cbcSMatt Macy * transaction created within vn_rdwr -> zfs_write. So a possible 711eda14cbcSMatt Macy * new end of file is passed through in zfsvfs->z_replay_eof 712eda14cbcSMatt Macy */ 713eda14cbcSMatt Macy 714eda14cbcSMatt Macy zfsvfs->z_replay_eof = 0; /* 0 means don't change end of file */ 715eda14cbcSMatt Macy 716eda14cbcSMatt Macy /* If it's a dmu_sync() block, write the whole block */ 717eda14cbcSMatt Macy if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 718eda14cbcSMatt Macy uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 719eda14cbcSMatt Macy if (length < blocksize) { 720eda14cbcSMatt Macy offset -= offset % blocksize; 721eda14cbcSMatt Macy length = blocksize; 722eda14cbcSMatt Macy } 723eda14cbcSMatt Macy if (zp->z_size < eod) 724eda14cbcSMatt Macy zfsvfs->z_replay_eof = eod; 725eda14cbcSMatt Macy } 726eda14cbcSMatt Macy error = zfs_write_simple(zp, data, length, offset, NULL); 727eda14cbcSMatt Macy zrele(zp); 728eda14cbcSMatt Macy zfsvfs->z_replay_eof = 0; /* safety */ 729eda14cbcSMatt Macy 730eda14cbcSMatt Macy return (error); 731eda14cbcSMatt Macy } 732eda14cbcSMatt Macy 733eda14cbcSMatt Macy /* 734eda14cbcSMatt Macy * TX_WRITE2 are only generated when dmu_sync() returns EALREADY 735eda14cbcSMatt Macy * meaning the pool block is already being synced. So now that we always write 736eda14cbcSMatt Macy * out full blocks, all we have to do is expand the eof if 737eda14cbcSMatt Macy * the file is grown. 738eda14cbcSMatt Macy */ 739eda14cbcSMatt Macy static int 740eda14cbcSMatt Macy zfs_replay_write2(void *arg1, void *arg2, boolean_t byteswap) 741eda14cbcSMatt Macy { 742eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 743eda14cbcSMatt Macy lr_write_t *lr = arg2; 744eda14cbcSMatt Macy znode_t *zp; 745eda14cbcSMatt Macy int error; 746eda14cbcSMatt Macy uint64_t end; 747eda14cbcSMatt Macy 748eda14cbcSMatt Macy if (byteswap) 749eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 750eda14cbcSMatt Macy 751eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) 752eda14cbcSMatt Macy return (error); 753eda14cbcSMatt Macy 754eda14cbcSMatt Macy top: 755eda14cbcSMatt Macy end = lr->lr_offset + lr->lr_length; 756eda14cbcSMatt Macy if (end > zp->z_size) { 757eda14cbcSMatt Macy dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 758eda14cbcSMatt Macy 759eda14cbcSMatt Macy zp->z_size = end; 760eda14cbcSMatt Macy dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 761eda14cbcSMatt Macy error = dmu_tx_assign(tx, TXG_WAIT); 762eda14cbcSMatt Macy if (error) { 763eda14cbcSMatt Macy zrele(zp); 764eda14cbcSMatt Macy if (error == ERESTART) { 765eda14cbcSMatt Macy dmu_tx_wait(tx); 766eda14cbcSMatt Macy dmu_tx_abort(tx); 767eda14cbcSMatt Macy goto top; 768eda14cbcSMatt Macy } 769eda14cbcSMatt Macy dmu_tx_abort(tx); 770eda14cbcSMatt Macy return (error); 771eda14cbcSMatt Macy } 772eda14cbcSMatt Macy (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 773eda14cbcSMatt Macy (void *)&zp->z_size, sizeof (uint64_t), tx); 774eda14cbcSMatt Macy 775eda14cbcSMatt Macy /* Ensure the replayed seq is updated */ 776eda14cbcSMatt Macy (void) zil_replaying(zfsvfs->z_log, tx); 777eda14cbcSMatt Macy 778eda14cbcSMatt Macy dmu_tx_commit(tx); 779eda14cbcSMatt Macy } 780eda14cbcSMatt Macy 781eda14cbcSMatt Macy zrele(zp); 782eda14cbcSMatt Macy 783eda14cbcSMatt Macy return (error); 784eda14cbcSMatt Macy } 785eda14cbcSMatt Macy 786eda14cbcSMatt Macy static int 787eda14cbcSMatt Macy zfs_replay_truncate(void *arg1, void *arg2, boolean_t byteswap) 788eda14cbcSMatt Macy { 789eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 790eda14cbcSMatt Macy lr_truncate_t *lr = arg2; 791eda14cbcSMatt Macy znode_t *zp; 792eda14cbcSMatt Macy flock64_t fl; 793eda14cbcSMatt Macy int error; 794eda14cbcSMatt Macy 795eda14cbcSMatt Macy if (byteswap) 796eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 797eda14cbcSMatt Macy 798eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) 799eda14cbcSMatt Macy return (error); 800eda14cbcSMatt Macy 801eda14cbcSMatt Macy bzero(&fl, sizeof (fl)); 802eda14cbcSMatt Macy fl.l_type = F_WRLCK; 803eda14cbcSMatt Macy fl.l_whence = SEEK_SET; 804eda14cbcSMatt Macy fl.l_start = lr->lr_offset; 805eda14cbcSMatt Macy fl.l_len = lr->lr_length; 806eda14cbcSMatt Macy 807eda14cbcSMatt Macy error = zfs_space(zp, F_FREESP, &fl, O_RDWR | O_LARGEFILE, 808eda14cbcSMatt Macy lr->lr_offset, kcred); 809eda14cbcSMatt Macy 810eda14cbcSMatt Macy zrele(zp); 811eda14cbcSMatt Macy 812eda14cbcSMatt Macy return (error); 813eda14cbcSMatt Macy } 814eda14cbcSMatt Macy 815eda14cbcSMatt Macy static int 816eda14cbcSMatt Macy zfs_replay_setattr(void *arg1, void *arg2, boolean_t byteswap) 817eda14cbcSMatt Macy { 818eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 819eda14cbcSMatt Macy lr_setattr_t *lr = arg2; 820eda14cbcSMatt Macy znode_t *zp; 821eda14cbcSMatt Macy xvattr_t xva; 822eda14cbcSMatt Macy vattr_t *vap = &xva.xva_vattr; 823eda14cbcSMatt Macy int error; 824eda14cbcSMatt Macy void *start; 825eda14cbcSMatt Macy 826eda14cbcSMatt Macy xva_init(&xva); 827eda14cbcSMatt Macy if (byteswap) { 828eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 829eda14cbcSMatt Macy 830eda14cbcSMatt Macy if ((lr->lr_mask & ATTR_XVATTR) && 831eda14cbcSMatt Macy zfsvfs->z_version >= ZPL_VERSION_INITIAL) 832eda14cbcSMatt Macy zfs_replay_swap_attrs((lr_attr_t *)(lr + 1)); 833eda14cbcSMatt Macy } 834eda14cbcSMatt Macy 835eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) 836eda14cbcSMatt Macy return (error); 837eda14cbcSMatt Macy 838eda14cbcSMatt Macy zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode, 839eda14cbcSMatt Macy lr->lr_uid, lr->lr_gid, 0, lr->lr_foid); 840eda14cbcSMatt Macy 841eda14cbcSMatt Macy vap->va_size = lr->lr_size; 842eda14cbcSMatt Macy ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime); 843eda14cbcSMatt Macy ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime); 844eda14cbcSMatt Macy gethrestime(&vap->va_ctime); 845eda14cbcSMatt Macy vap->va_mask |= ATTR_CTIME; 846eda14cbcSMatt Macy 847eda14cbcSMatt Macy /* 848eda14cbcSMatt Macy * Fill in xvattr_t portions if necessary. 849eda14cbcSMatt Macy */ 850eda14cbcSMatt Macy 851eda14cbcSMatt Macy start = (lr_setattr_t *)(lr + 1); 852eda14cbcSMatt Macy if (vap->va_mask & ATTR_XVATTR) { 853eda14cbcSMatt Macy zfs_replay_xvattr((lr_attr_t *)start, &xva); 854eda14cbcSMatt Macy start = (caddr_t)start + 855eda14cbcSMatt Macy ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize); 856eda14cbcSMatt Macy } else 857eda14cbcSMatt Macy xva.xva_vattr.va_mask &= ~ATTR_XVATTR; 858eda14cbcSMatt Macy 859eda14cbcSMatt Macy zfsvfs->z_fuid_replay = zfs_replay_fuid_domain(start, &start, 860eda14cbcSMatt Macy lr->lr_uid, lr->lr_gid); 861eda14cbcSMatt Macy 862eda14cbcSMatt Macy error = zfs_setattr(zp, vap, 0, kcred); 863eda14cbcSMatt Macy 864eda14cbcSMatt Macy zfs_fuid_info_free(zfsvfs->z_fuid_replay); 865eda14cbcSMatt Macy zfsvfs->z_fuid_replay = NULL; 866eda14cbcSMatt Macy zrele(zp); 867eda14cbcSMatt Macy 868eda14cbcSMatt Macy return (error); 869eda14cbcSMatt Macy } 870eda14cbcSMatt Macy 871eda14cbcSMatt Macy static int 872eda14cbcSMatt Macy zfs_replay_acl_v0(void *arg1, void *arg2, boolean_t byteswap) 873eda14cbcSMatt Macy { 874eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 875eda14cbcSMatt Macy lr_acl_v0_t *lr = arg2; 876eda14cbcSMatt Macy ace_t *ace = (ace_t *)(lr + 1); /* ace array follows lr_acl_t */ 877eda14cbcSMatt Macy vsecattr_t vsa; 878eda14cbcSMatt Macy znode_t *zp; 879eda14cbcSMatt Macy int error; 880eda14cbcSMatt Macy 881eda14cbcSMatt Macy if (byteswap) { 882eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 883eda14cbcSMatt Macy zfs_oldace_byteswap(ace, lr->lr_aclcnt); 884eda14cbcSMatt Macy } 885eda14cbcSMatt Macy 886eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) 887eda14cbcSMatt Macy return (error); 888eda14cbcSMatt Macy 889eda14cbcSMatt Macy bzero(&vsa, sizeof (vsa)); 890eda14cbcSMatt Macy vsa.vsa_mask = VSA_ACE | VSA_ACECNT; 891eda14cbcSMatt Macy vsa.vsa_aclcnt = lr->lr_aclcnt; 892eda14cbcSMatt Macy vsa.vsa_aclentsz = sizeof (ace_t) * vsa.vsa_aclcnt; 893eda14cbcSMatt Macy vsa.vsa_aclflags = 0; 894eda14cbcSMatt Macy vsa.vsa_aclentp = ace; 895eda14cbcSMatt Macy 896eda14cbcSMatt Macy error = zfs_setsecattr(zp, &vsa, 0, kcred); 897eda14cbcSMatt Macy 898eda14cbcSMatt Macy zrele(zp); 899eda14cbcSMatt Macy 900eda14cbcSMatt Macy return (error); 901eda14cbcSMatt Macy } 902eda14cbcSMatt Macy 903eda14cbcSMatt Macy /* 904eda14cbcSMatt Macy * Replaying ACLs is complicated by FUID support. 905eda14cbcSMatt Macy * The log record may contain some optional data 906eda14cbcSMatt Macy * to be used for replaying FUID's. These pieces 907eda14cbcSMatt Macy * are the actual FUIDs that were created initially. 908eda14cbcSMatt Macy * The FUID table index may no longer be valid and 909eda14cbcSMatt Macy * during zfs_create() a new index may be assigned. 910eda14cbcSMatt Macy * Because of this the log will contain the original 911eda14cbcSMatt Macy * domain+rid in order to create a new FUID. 912eda14cbcSMatt Macy * 913eda14cbcSMatt Macy * The individual ACEs may contain an ephemeral uid/gid which is no 914eda14cbcSMatt Macy * longer valid and will need to be replaced with an actual FUID. 915eda14cbcSMatt Macy * 916eda14cbcSMatt Macy */ 917eda14cbcSMatt Macy static int 918eda14cbcSMatt Macy zfs_replay_acl(void *arg1, void *arg2, boolean_t byteswap) 919eda14cbcSMatt Macy { 920eda14cbcSMatt Macy zfsvfs_t *zfsvfs = arg1; 921eda14cbcSMatt Macy lr_acl_t *lr = arg2; 922eda14cbcSMatt Macy ace_t *ace = (ace_t *)(lr + 1); 923eda14cbcSMatt Macy vsecattr_t vsa; 924eda14cbcSMatt Macy znode_t *zp; 925eda14cbcSMatt Macy int error; 926eda14cbcSMatt Macy 927eda14cbcSMatt Macy if (byteswap) { 928eda14cbcSMatt Macy byteswap_uint64_array(lr, sizeof (*lr)); 929eda14cbcSMatt Macy zfs_ace_byteswap(ace, lr->lr_acl_bytes, B_FALSE); 930eda14cbcSMatt Macy if (lr->lr_fuidcnt) { 931eda14cbcSMatt Macy byteswap_uint64_array((caddr_t)ace + 932eda14cbcSMatt Macy ZIL_ACE_LENGTH(lr->lr_acl_bytes), 933eda14cbcSMatt Macy lr->lr_fuidcnt * sizeof (uint64_t)); 934eda14cbcSMatt Macy } 935eda14cbcSMatt Macy } 936eda14cbcSMatt Macy 937eda14cbcSMatt Macy if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) 938eda14cbcSMatt Macy return (error); 939eda14cbcSMatt Macy 940eda14cbcSMatt Macy bzero(&vsa, sizeof (vsa)); 941eda14cbcSMatt Macy vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS; 942eda14cbcSMatt Macy vsa.vsa_aclcnt = lr->lr_aclcnt; 943eda14cbcSMatt Macy vsa.vsa_aclentp = ace; 944eda14cbcSMatt Macy vsa.vsa_aclentsz = lr->lr_acl_bytes; 945eda14cbcSMatt Macy vsa.vsa_aclflags = lr->lr_acl_flags; 946eda14cbcSMatt Macy 947eda14cbcSMatt Macy if (lr->lr_fuidcnt) { 948eda14cbcSMatt Macy void *fuidstart = (caddr_t)ace + 949eda14cbcSMatt Macy ZIL_ACE_LENGTH(lr->lr_acl_bytes); 950eda14cbcSMatt Macy 951eda14cbcSMatt Macy zfsvfs->z_fuid_replay = 952eda14cbcSMatt Macy zfs_replay_fuids(fuidstart, &fuidstart, 953eda14cbcSMatt Macy lr->lr_fuidcnt, lr->lr_domcnt, 0, 0); 954eda14cbcSMatt Macy } 955eda14cbcSMatt Macy 956eda14cbcSMatt Macy error = zfs_setsecattr(zp, &vsa, 0, kcred); 957eda14cbcSMatt Macy 958eda14cbcSMatt Macy if (zfsvfs->z_fuid_replay) 959eda14cbcSMatt Macy zfs_fuid_info_free(zfsvfs->z_fuid_replay); 960eda14cbcSMatt Macy 961eda14cbcSMatt Macy zfsvfs->z_fuid_replay = NULL; 962eda14cbcSMatt Macy zrele(zp); 963eda14cbcSMatt Macy 964eda14cbcSMatt Macy return (error); 965eda14cbcSMatt Macy } 966eda14cbcSMatt Macy 967eda14cbcSMatt Macy /* 968eda14cbcSMatt Macy * Callback vectors for replaying records 969eda14cbcSMatt Macy */ 970eda14cbcSMatt Macy zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = { 971eda14cbcSMatt Macy zfs_replay_error, /* no such type */ 972eda14cbcSMatt Macy zfs_replay_create, /* TX_CREATE */ 973eda14cbcSMatt Macy zfs_replay_create, /* TX_MKDIR */ 974eda14cbcSMatt Macy zfs_replay_create, /* TX_MKXATTR */ 975eda14cbcSMatt Macy zfs_replay_create, /* TX_SYMLINK */ 976eda14cbcSMatt Macy zfs_replay_remove, /* TX_REMOVE */ 977eda14cbcSMatt Macy zfs_replay_remove, /* TX_RMDIR */ 978eda14cbcSMatt Macy zfs_replay_link, /* TX_LINK */ 979eda14cbcSMatt Macy zfs_replay_rename, /* TX_RENAME */ 980eda14cbcSMatt Macy zfs_replay_write, /* TX_WRITE */ 981eda14cbcSMatt Macy zfs_replay_truncate, /* TX_TRUNCATE */ 982eda14cbcSMatt Macy zfs_replay_setattr, /* TX_SETATTR */ 983eda14cbcSMatt Macy zfs_replay_acl_v0, /* TX_ACL_V0 */ 984eda14cbcSMatt Macy zfs_replay_acl, /* TX_ACL */ 985eda14cbcSMatt Macy zfs_replay_create_acl, /* TX_CREATE_ACL */ 986eda14cbcSMatt Macy zfs_replay_create, /* TX_CREATE_ATTR */ 987eda14cbcSMatt Macy zfs_replay_create_acl, /* TX_CREATE_ACL_ATTR */ 988eda14cbcSMatt Macy zfs_replay_create_acl, /* TX_MKDIR_ACL */ 989eda14cbcSMatt Macy zfs_replay_create, /* TX_MKDIR_ATTR */ 990eda14cbcSMatt Macy zfs_replay_create_acl, /* TX_MKDIR_ACL_ATTR */ 991eda14cbcSMatt Macy zfs_replay_write2, /* TX_WRITE2 */ 992eda14cbcSMatt Macy }; 993