1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51669Sperrin * Common Development and Distribution License (the "License"). 61669Sperrin * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 22*6101Sperrin * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/types.h> 29789Sahrens #include <sys/param.h> 30789Sahrens #include <sys/systm.h> 31789Sahrens #include <sys/sysmacros.h> 32789Sahrens #include <sys/cmn_err.h> 33789Sahrens #include <sys/kmem.h> 34789Sahrens #include <sys/thread.h> 35789Sahrens #include <sys/file.h> 36789Sahrens #include <sys/vfs.h> 37789Sahrens #include <sys/zfs_znode.h> 38789Sahrens #include <sys/zfs_dir.h> 39789Sahrens #include <sys/zil.h> 404620Sperrin #include <sys/zil_impl.h> 41789Sahrens #include <sys/byteorder.h> 42789Sahrens #include <sys/policy.h> 43789Sahrens #include <sys/stat.h> 44789Sahrens #include <sys/mode.h> 45789Sahrens #include <sys/acl.h> 46789Sahrens #include <sys/dmu.h> 47789Sahrens #include <sys/spa.h> 485331Samw #include <sys/zfs_fuid.h> 49789Sahrens #include <sys/ddi.h> 50789Sahrens 51789Sahrens /* 52789Sahrens * All the functions in this file are used to construct the log entries 535331Samw * to record transactions. They allocate * an intent log transaction 54789Sahrens * structure (itx_t) and save within it all the information necessary to 55789Sahrens * possibly replay the transaction. The itx is then assigned a sequence 56789Sahrens * number and inserted in the in-memory list anchored in the zilog. 57789Sahrens */ 58789Sahrens 595331Samw int 605331Samw zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap) 615331Samw { 625331Samw int isxvattr = (vap->va_mask & AT_XVATTR); 635331Samw switch (type) { 645331Samw case Z_FILE: 655331Samw if (vsecp == NULL && !isxvattr) 665331Samw return (TX_CREATE); 675331Samw if (vsecp && isxvattr) 685331Samw return (TX_CREATE_ACL_ATTR); 695331Samw if (vsecp) 705331Samw return (TX_CREATE_ACL); 715331Samw else 725331Samw return (TX_CREATE_ATTR); 735331Samw /*NOTREACHED*/ 745331Samw case Z_DIR: 755331Samw if (vsecp == NULL && !isxvattr) 765331Samw return (TX_MKDIR); 775331Samw if (vsecp && isxvattr) 785331Samw return (TX_MKDIR_ACL_ATTR); 795331Samw if (vsecp) 805331Samw return (TX_MKDIR_ACL); 815331Samw else 825331Samw return (TX_MKDIR_ATTR); 835331Samw case Z_XATTRDIR: 845331Samw return (TX_MKXATTR); 855331Samw } 865331Samw ASSERT(0); 875331Samw return (TX_MAX_TYPE); 885331Samw } 895331Samw 90789Sahrens /* 915331Samw * build up the log data necessary for logging xvattr_t 925331Samw * First lr_attr_t is initialized. following the lr_attr_t 935331Samw * is the mapsize and attribute bitmap copied from the xvattr_t. 945331Samw * Following the bitmap and bitmapsize two 64 bit words are reserved 955331Samw * for the create time which may be set. Following the create time 965331Samw * records a single 64 bit integer which has the bits to set on 975331Samw * replay for the xvattr. 985331Samw */ 995331Samw static void 1005331Samw zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap) 1015331Samw { 1025331Samw uint32_t *bitmap; 1035331Samw uint64_t *attrs; 1045331Samw uint64_t *crtime; 1055331Samw xoptattr_t *xoap; 1065331Samw void *scanstamp; 1075331Samw int i; 1085331Samw 1095331Samw xoap = xva_getxoptattr(xvap); 1105331Samw ASSERT(xoap); 1115331Samw 1125331Samw lrattr->lr_attr_masksize = xvap->xva_mapsize; 1135331Samw bitmap = &lrattr->lr_attr_bitmap; 1145331Samw for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) { 1155331Samw *bitmap = xvap->xva_reqattrmap[i]; 1165331Samw } 1175331Samw 1185331Samw /* Now pack the attributes up in a single uint64_t */ 1195331Samw attrs = (uint64_t *)bitmap; 1205331Samw crtime = attrs + 1; 1215331Samw scanstamp = (caddr_t)(crtime + 2); 1225331Samw *attrs = 0; 1235331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) 1245331Samw *attrs |= (xoap->xoa_readonly == 0) ? 0 : 1255331Samw XAT0_READONLY; 1265331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) 1275331Samw *attrs |= (xoap->xoa_hidden == 0) ? 0 : 1285331Samw XAT0_HIDDEN; 1295331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) 1305331Samw *attrs |= (xoap->xoa_system == 0) ? 0 : 1315331Samw XAT0_SYSTEM; 1325331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) 1335331Samw *attrs |= (xoap->xoa_archive == 0) ? 0 : 1345331Samw XAT0_ARCHIVE; 1355331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) 1365331Samw *attrs |= (xoap->xoa_immutable == 0) ? 0 : 1375331Samw XAT0_IMMUTABLE; 1385331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) 1395331Samw *attrs |= (xoap->xoa_nounlink == 0) ? 0 : 1405331Samw XAT0_NOUNLINK; 1415331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) 1425331Samw *attrs |= (xoap->xoa_appendonly == 0) ? 0 : 1435331Samw XAT0_APPENDONLY; 1445331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) 1455331Samw *attrs |= (xoap->xoa_opaque == 0) ? 0 : 1465331Samw XAT0_APPENDONLY; 1475331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) 1485331Samw *attrs |= (xoap->xoa_nodump == 0) ? 0 : 1495331Samw XAT0_NODUMP; 1505331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) 1515331Samw *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 : 1525331Samw XAT0_AV_QUARANTINED; 1535331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) 1545331Samw *attrs |= (xoap->xoa_av_modified == 0) ? 0 : 1555331Samw XAT0_AV_MODIFIED; 1565331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) 1575331Samw ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime); 1585331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 1595331Samw bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ); 1605331Samw } 1615331Samw 1625331Samw static void * 1635331Samw zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start) 1645331Samw { 1655331Samw zfs_fuid_t *zfuid; 1665331Samw uint64_t *fuidloc = start; 1675331Samw 1685331Samw /* First copy in the ACE FUIDs */ 1695331Samw for (zfuid = list_head(&fuidp->z_fuids); zfuid; 1705331Samw zfuid = list_next(&fuidp->z_fuids, zfuid)) { 1715331Samw *fuidloc++ = zfuid->z_logfuid; 1725331Samw } 1735331Samw return (fuidloc); 1745331Samw } 1755331Samw 1765331Samw 1775331Samw static void * 1785331Samw zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start) 1795331Samw { 1805331Samw zfs_fuid_domain_t *zdomain; 1815331Samw 1825331Samw /* now copy in the domain info, if any */ 1835331Samw if (fuidp->z_domain_str_sz != 0) { 1845331Samw for (zdomain = list_head(&fuidp->z_domains); zdomain; 1855331Samw zdomain = list_next(&fuidp->z_domains, zdomain)) { 1865331Samw bcopy((void *)zdomain->z_domain, start, 1875331Samw strlen(zdomain->z_domain) + 1); 1885331Samw start = (caddr_t)start + 1895331Samw strlen(zdomain->z_domain) + 1; 1905331Samw } 1915331Samw } 1925331Samw return (start); 1935331Samw } 1945331Samw 1955331Samw /* 1965331Samw * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, 1975331Samw * TX_MKDIR_ATTR and TX_MKXATTR 198789Sahrens * transactions. 1995331Samw * 2005331Samw * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID 2015331Samw * domain information appended prior to the name. In this case the 2025331Samw * uid/gid in the log record will be a log centric FUID. 2035331Samw * 2045331Samw * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that 2055331Samw * may contain attributes, ACL and optional fuid information. 2065331Samw * 2075331Samw * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify 2085331Samw * and ACL and normal users/groups in the ACEs. 2095331Samw * 2105331Samw * There may be an optional xvattr attribute information similar 2115331Samw * to zfs_log_setattr. 2125331Samw * 2135331Samw * Also, after the file name "domain" strings may be appended. 214789Sahrens */ 2152638Sperrin void 2165331Samw zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 2175331Samw znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp, 2185331Samw zfs_fuid_info_t *fuidp, vattr_t *vap) 219789Sahrens { 220789Sahrens itx_t *itx; 221789Sahrens uint64_t seq; 222789Sahrens lr_create_t *lr; 2235331Samw lr_acl_create_t *lracl; 2245331Samw size_t aclsize; 2255331Samw size_t xvatsize = 0; 2265331Samw size_t txsize; 2275331Samw xvattr_t *xvap = (xvattr_t *)vap; 2285331Samw void *end; 2295331Samw size_t lrsize; 2305331Samw 231789Sahrens size_t namesize = strlen(name) + 1; 2325331Samw size_t fuidsz = 0; 233789Sahrens 234789Sahrens if (zilog == NULL) 2352638Sperrin return; 236789Sahrens 2375331Samw /* 2385331Samw * If we have FUIDs present then add in space for 2395331Samw * domains and ACE fuid's if any. 2405331Samw */ 2415331Samw if (fuidp) { 2425331Samw fuidsz += fuidp->z_domain_str_sz; 2435331Samw fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t); 2445331Samw } 2455331Samw 2465331Samw if (vap->va_mask & AT_XVATTR) 2475331Samw xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize); 2485331Samw 2495331Samw if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR || 2505331Samw (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR || 2515331Samw (int)txtype == TX_MKXATTR) { 2525331Samw txsize = sizeof (*lr) + namesize + fuidsz + xvatsize; 2535331Samw lrsize = sizeof (*lr); 2545331Samw } else { 2555331Samw aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0; 2565331Samw txsize = 2575331Samw sizeof (lr_acl_create_t) + namesize + fuidsz + 2585435Smarks ZIL_ACE_LENGTH(aclsize) + xvatsize; 2595331Samw lrsize = sizeof (lr_acl_create_t); 2605331Samw } 2615331Samw 2625331Samw itx = zil_itx_create(txtype, txsize); 2635331Samw 264789Sahrens lr = (lr_create_t *)&itx->itx_lr; 265789Sahrens lr->lr_doid = dzp->z_id; 266789Sahrens lr->lr_foid = zp->z_id; 267789Sahrens lr->lr_mode = zp->z_phys->zp_mode; 2685331Samw if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) { 2695331Samw lr->lr_uid = (uint64_t)zp->z_phys->zp_uid; 2705331Samw } else { 2715331Samw lr->lr_uid = fuidp->z_fuid_owner; 2725331Samw } 2735331Samw if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) { 2745331Samw lr->lr_gid = (uint64_t)zp->z_phys->zp_gid; 2755331Samw } else { 2765331Samw lr->lr_gid = fuidp->z_fuid_group; 2775331Samw } 278789Sahrens lr->lr_gen = zp->z_phys->zp_gen; 279789Sahrens lr->lr_crtime[0] = zp->z_phys->zp_crtime[0]; 280789Sahrens lr->lr_crtime[1] = zp->z_phys->zp_crtime[1]; 281789Sahrens lr->lr_rdev = zp->z_phys->zp_rdev; 2825331Samw 2835331Samw /* 2845331Samw * Fill in xvattr info if any 2855331Samw */ 2865331Samw if (vap->va_mask & AT_XVATTR) { 2875331Samw zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap); 2885331Samw end = (caddr_t)lr + lrsize + xvatsize; 2895331Samw } else { 2905331Samw end = (caddr_t)lr + lrsize; 2915331Samw } 2925331Samw 2935331Samw /* Now fill in any ACL info */ 2945331Samw 2955331Samw if (vsecp) { 2965331Samw lracl = (lr_acl_create_t *)&itx->itx_lr; 2975331Samw lracl->lr_aclcnt = vsecp->vsa_aclcnt; 2985331Samw lracl->lr_acl_bytes = aclsize; 2995331Samw lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0; 3005331Samw lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0; 3015331Samw if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS) 3025331Samw lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags; 3035331Samw else 3045331Samw lracl->lr_acl_flags = 0; 3055331Samw 3065331Samw bcopy(vsecp->vsa_aclentp, end, aclsize); 3075435Smarks end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize); 3085331Samw } 3095331Samw 3105331Samw /* drop in FUID info */ 3115331Samw if (fuidp) { 3125331Samw end = zfs_log_fuid_ids(fuidp, end); 3135331Samw end = zfs_log_fuid_domains(fuidp, end); 3145331Samw } 3155331Samw /* 3165331Samw * Now place file name in log record 3175331Samw */ 3185331Samw bcopy(name, end, namesize); 319789Sahrens 320789Sahrens seq = zil_itx_assign(zilog, itx, tx); 321789Sahrens dzp->z_last_itx = seq; 322789Sahrens zp->z_last_itx = seq; 323789Sahrens } 324789Sahrens 325789Sahrens /* 326789Sahrens * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions. 327789Sahrens */ 3282638Sperrin void 3295331Samw zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 330789Sahrens znode_t *dzp, char *name) 331789Sahrens { 332789Sahrens itx_t *itx; 333789Sahrens uint64_t seq; 334789Sahrens lr_remove_t *lr; 335789Sahrens size_t namesize = strlen(name) + 1; 336789Sahrens 337789Sahrens if (zilog == NULL) 3382638Sperrin return; 339789Sahrens 340789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize); 341789Sahrens lr = (lr_remove_t *)&itx->itx_lr; 342789Sahrens lr->lr_doid = dzp->z_id; 343789Sahrens bcopy(name, (char *)(lr + 1), namesize); 344789Sahrens 345789Sahrens seq = zil_itx_assign(zilog, itx, tx); 346789Sahrens dzp->z_last_itx = seq; 347789Sahrens } 348789Sahrens 349789Sahrens /* 350789Sahrens * zfs_log_link() handles TX_LINK transactions. 351789Sahrens */ 3522638Sperrin void 3535331Samw zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 354789Sahrens znode_t *dzp, znode_t *zp, char *name) 355789Sahrens { 356789Sahrens itx_t *itx; 357789Sahrens uint64_t seq; 358789Sahrens lr_link_t *lr; 359789Sahrens size_t namesize = strlen(name) + 1; 360789Sahrens 361789Sahrens if (zilog == NULL) 3622638Sperrin return; 363789Sahrens 364789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize); 365789Sahrens lr = (lr_link_t *)&itx->itx_lr; 366789Sahrens lr->lr_doid = dzp->z_id; 367789Sahrens lr->lr_link_obj = zp->z_id; 368789Sahrens bcopy(name, (char *)(lr + 1), namesize); 369789Sahrens 370789Sahrens seq = zil_itx_assign(zilog, itx, tx); 371789Sahrens dzp->z_last_itx = seq; 372789Sahrens zp->z_last_itx = seq; 373789Sahrens } 374789Sahrens 375789Sahrens /* 376789Sahrens * zfs_log_symlink() handles TX_SYMLINK transactions. 377789Sahrens */ 3782638Sperrin void 3795331Samw zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 3805331Samw znode_t *dzp, znode_t *zp, char *name, char *link) 381789Sahrens { 382789Sahrens itx_t *itx; 383789Sahrens uint64_t seq; 384789Sahrens lr_create_t *lr; 385789Sahrens size_t namesize = strlen(name) + 1; 386789Sahrens size_t linksize = strlen(link) + 1; 387789Sahrens 388789Sahrens if (zilog == NULL) 3892638Sperrin return; 390789Sahrens 391789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize); 392789Sahrens lr = (lr_create_t *)&itx->itx_lr; 393789Sahrens lr->lr_doid = dzp->z_id; 394789Sahrens lr->lr_foid = zp->z_id; 395789Sahrens lr->lr_mode = zp->z_phys->zp_mode; 396789Sahrens lr->lr_uid = zp->z_phys->zp_uid; 397789Sahrens lr->lr_gid = zp->z_phys->zp_gid; 398789Sahrens lr->lr_gen = zp->z_phys->zp_gen; 399789Sahrens lr->lr_crtime[0] = zp->z_phys->zp_crtime[0]; 400789Sahrens lr->lr_crtime[1] = zp->z_phys->zp_crtime[1]; 401789Sahrens bcopy(name, (char *)(lr + 1), namesize); 402789Sahrens bcopy(link, (char *)(lr + 1) + namesize, linksize); 403789Sahrens 404789Sahrens seq = zil_itx_assign(zilog, itx, tx); 405789Sahrens dzp->z_last_itx = seq; 406789Sahrens zp->z_last_itx = seq; 407789Sahrens } 408789Sahrens 409789Sahrens /* 410789Sahrens * zfs_log_rename() handles TX_RENAME transactions. 411789Sahrens */ 4122638Sperrin void 4135331Samw zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 414789Sahrens znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp) 415789Sahrens { 416789Sahrens itx_t *itx; 417789Sahrens uint64_t seq; 418789Sahrens lr_rename_t *lr; 419789Sahrens size_t snamesize = strlen(sname) + 1; 420789Sahrens size_t dnamesize = strlen(dname) + 1; 421789Sahrens 422789Sahrens if (zilog == NULL) 4232638Sperrin return; 424789Sahrens 425789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize); 426789Sahrens lr = (lr_rename_t *)&itx->itx_lr; 427789Sahrens lr->lr_sdoid = sdzp->z_id; 428789Sahrens lr->lr_tdoid = tdzp->z_id; 429789Sahrens bcopy(sname, (char *)(lr + 1), snamesize); 430789Sahrens bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize); 431789Sahrens 432789Sahrens seq = zil_itx_assign(zilog, itx, tx); 433789Sahrens sdzp->z_last_itx = seq; 434789Sahrens tdzp->z_last_itx = seq; 435789Sahrens szp->z_last_itx = seq; 436789Sahrens } 437789Sahrens 438789Sahrens /* 439789Sahrens * zfs_log_write() handles TX_WRITE transactions. 440789Sahrens */ 4412237Smaybee ssize_t zfs_immediate_write_sz = 32768; 442789Sahrens 4434620Sperrin #define ZIL_MAX_LOG_DATA (SPA_MAXBLOCKSIZE - sizeof (zil_trailer_t) - \ 4444620Sperrin sizeof (lr_write_t)) 4454620Sperrin 4462638Sperrin void 447789Sahrens zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype, 4484620Sperrin znode_t *zp, offset_t off, ssize_t resid, int ioflag) 449789Sahrens { 4501669Sperrin itx_wr_state_t write_state; 4514620Sperrin boolean_t slogging; 4524720Sfr157268 uintptr_t fsync_cnt; 453789Sahrens 4543461Sahrens if (zilog == NULL || zp->z_unlinked) 4552638Sperrin return; 456789Sahrens 4571669Sperrin /* 4581669Sperrin * Writes are handled in three different ways: 4591669Sperrin * 4601669Sperrin * WR_INDIRECT: 4614620Sperrin * If the write is greater than zfs_immediate_write_sz and there are 4624620Sperrin * no separate logs in this pool then later *if* we need to log the 4634620Sperrin * write then dmu_sync() is used to immediately write the block and 4644620Sperrin * its block pointer is put in the log record. 4651669Sperrin * WR_COPIED: 4661669Sperrin * If we know we'll immediately be committing the 4671669Sperrin * transaction (FDSYNC (O_DSYNC)), the we allocate a larger 4681669Sperrin * log record here for the data and copy the data in. 4691669Sperrin * WR_NEED_COPY: 4701669Sperrin * Otherwise we don't allocate a buffer, and *if* we need to 4711669Sperrin * flush the write later then a buffer is allocated and 4721669Sperrin * we retrieve the data using the dmu. 4731669Sperrin */ 4744620Sperrin slogging = spa_has_slogs(zilog->zl_spa); 4754620Sperrin if (resid > zfs_immediate_write_sz && !slogging) 4761669Sperrin write_state = WR_INDIRECT; 4773638Sbillm else if (ioflag & FDSYNC) 4781669Sperrin write_state = WR_COPIED; 4793638Sbillm else 4801669Sperrin write_state = WR_NEED_COPY; 4813638Sbillm 4824720Sfr157268 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) { 4834720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1)); 4844720Sfr157268 } 4854720Sfr157268 4864620Sperrin while (resid) { 4874620Sperrin itx_t *itx; 4884620Sperrin lr_write_t *lr; 4894620Sperrin ssize_t len; 4904620Sperrin 4914620Sperrin /* 4924620Sperrin * If there are slogs and the write would overflow the largest 4934620Sperrin * block, then because we don't want to use the main pool 4944620Sperrin * to dmu_sync, we have to split the write. 4954620Sperrin */ 4964620Sperrin if (slogging && resid > ZIL_MAX_LOG_DATA) 4974620Sperrin len = SPA_MAXBLOCKSIZE >> 1; 4984620Sperrin else 4994620Sperrin len = resid; 5004620Sperrin 5014620Sperrin itx = zil_itx_create(txtype, sizeof (*lr) + 5024620Sperrin (write_state == WR_COPIED ? len : 0)); 5034620Sperrin lr = (lr_write_t *)&itx->itx_lr; 5044620Sperrin if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os, 5054620Sperrin zp->z_id, off, len, lr + 1) != 0) { 5063638Sbillm kmem_free(itx, offsetof(itx_t, itx_lr) + 5073638Sbillm itx->itx_lr.lrc_reclen); 5081669Sperrin itx = zil_itx_create(txtype, sizeof (*lr)); 5093638Sbillm lr = (lr_write_t *)&itx->itx_lr; 5101669Sperrin write_state = WR_NEED_COPY; 5111669Sperrin } 5124620Sperrin 5134620Sperrin itx->itx_wr_state = write_state; 514*6101Sperrin if (write_state == WR_NEED_COPY) 515*6101Sperrin itx->itx_sod += len; 5164620Sperrin lr->lr_foid = zp->z_id; 5174620Sperrin lr->lr_offset = off; 5184620Sperrin lr->lr_length = len; 5194620Sperrin lr->lr_blkoff = 0; 5204620Sperrin BP_ZERO(&lr->lr_blkptr); 5213638Sbillm 5224620Sperrin itx->itx_private = zp->z_zfsvfs; 523789Sahrens 5244720Sfr157268 if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0)) 5254720Sfr157268 itx->itx_sync = B_TRUE; 5264720Sfr157268 else 5274720Sfr157268 itx->itx_sync = B_FALSE; 5284720Sfr157268 5294620Sperrin zp->z_last_itx = zil_itx_assign(zilog, itx, tx); 530789Sahrens 5314620Sperrin off += len; 5324620Sperrin resid -= len; 5334620Sperrin } 534789Sahrens } 535789Sahrens 536789Sahrens /* 537789Sahrens * zfs_log_truncate() handles TX_TRUNCATE transactions. 538789Sahrens */ 5392638Sperrin void 540789Sahrens zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype, 541789Sahrens znode_t *zp, uint64_t off, uint64_t len) 542789Sahrens { 543789Sahrens itx_t *itx; 544789Sahrens uint64_t seq; 545789Sahrens lr_truncate_t *lr; 546789Sahrens 5473461Sahrens if (zilog == NULL || zp->z_unlinked) 5482638Sperrin return; 549789Sahrens 550789Sahrens itx = zil_itx_create(txtype, sizeof (*lr)); 551789Sahrens lr = (lr_truncate_t *)&itx->itx_lr; 552789Sahrens lr->lr_foid = zp->z_id; 553789Sahrens lr->lr_offset = off; 554789Sahrens lr->lr_length = len; 555789Sahrens 5563063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 557789Sahrens seq = zil_itx_assign(zilog, itx, tx); 558789Sahrens zp->z_last_itx = seq; 559789Sahrens } 560789Sahrens 561789Sahrens /* 562789Sahrens * zfs_log_setattr() handles TX_SETATTR transactions. 563789Sahrens */ 5642638Sperrin void 565789Sahrens zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype, 5665331Samw znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp) 567789Sahrens { 5685331Samw itx_t *itx; 5695331Samw uint64_t seq; 5705331Samw lr_setattr_t *lr; 5715331Samw xvattr_t *xvap = (xvattr_t *)vap; 5725331Samw size_t recsize = sizeof (lr_setattr_t); 5735331Samw void *start; 5745331Samw 575789Sahrens 5763461Sahrens if (zilog == NULL || zp->z_unlinked) 5772638Sperrin return; 578789Sahrens 5795331Samw /* 5805331Samw * If XVATTR set, then log record size needs to allow 5815331Samw * for lr_attr_t + xvattr mask, mapsize and create time 5825331Samw * plus actual attribute values 5835331Samw */ 5845331Samw if (vap->va_mask & AT_XVATTR) 5855331Samw recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize); 5865331Samw 5875331Samw if (fuidp) 5885331Samw recsize += fuidp->z_domain_str_sz; 5895331Samw 5905331Samw itx = zil_itx_create(txtype, recsize); 591789Sahrens lr = (lr_setattr_t *)&itx->itx_lr; 592789Sahrens lr->lr_foid = zp->z_id; 593789Sahrens lr->lr_mask = (uint64_t)mask_applied; 594789Sahrens lr->lr_mode = (uint64_t)vap->va_mode; 5955331Samw if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid)) 5965331Samw lr->lr_uid = fuidp->z_fuid_owner; 5975331Samw else 5985331Samw lr->lr_uid = (uint64_t)vap->va_uid; 5995331Samw 6005331Samw if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid)) 6015331Samw lr->lr_gid = fuidp->z_fuid_group; 6025331Samw else 6035331Samw lr->lr_gid = (uint64_t)vap->va_gid; 6045331Samw 605789Sahrens lr->lr_size = (uint64_t)vap->va_size; 606789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime); 607789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime); 6085331Samw start = (lr_setattr_t *)(lr + 1); 6095331Samw if (vap->va_mask & AT_XVATTR) { 6105331Samw zfs_log_xvattr((lr_attr_t *)start, xvap); 6115331Samw start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize); 6125331Samw } 6135331Samw 6145331Samw /* 6155331Samw * Now stick on domain information if any on end 6165331Samw */ 6175331Samw 6185331Samw if (fuidp) 6195331Samw (void) zfs_log_fuid_domains(fuidp, start); 620789Sahrens 6213063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 622789Sahrens seq = zil_itx_assign(zilog, itx, tx); 623789Sahrens zp->z_last_itx = seq; 624789Sahrens } 625789Sahrens 626789Sahrens /* 627789Sahrens * zfs_log_acl() handles TX_ACL transactions. 628789Sahrens */ 6292638Sperrin void 6305331Samw zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp, 6315331Samw vsecattr_t *vsecp, zfs_fuid_info_t *fuidp) 632789Sahrens { 633789Sahrens itx_t *itx; 634789Sahrens uint64_t seq; 6355331Samw lr_acl_v0_t *lrv0; 636789Sahrens lr_acl_t *lr; 6375331Samw int txtype; 6385331Samw int lrsize; 6395331Samw size_t txsize; 6405331Samw size_t aclbytes = vsecp->vsa_aclentsz; 6415331Samw 6425331Samw txtype = (zp->z_zfsvfs->z_version == ZPL_VERSION_INITIAL) ? 6435331Samw TX_ACL_V0 : TX_ACL; 6445331Samw 6455331Samw if (txtype == TX_ACL) 6465331Samw lrsize = sizeof (*lr); 6475331Samw else 6485331Samw lrsize = sizeof (*lrv0); 649789Sahrens 6503461Sahrens if (zilog == NULL || zp->z_unlinked) 6512638Sperrin return; 652789Sahrens 6535435Smarks txsize = lrsize + 6545435Smarks ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) + 6555435Smarks (fuidp ? fuidp->z_domain_str_sz : 0) + 6565331Samw sizeof (uint64) * (fuidp ? fuidp->z_fuid_cnt : 0); 6575331Samw 6585331Samw itx = zil_itx_create(txtype, txsize); 6595331Samw 660789Sahrens lr = (lr_acl_t *)&itx->itx_lr; 661789Sahrens lr->lr_foid = zp->z_id; 6625331Samw if (txtype == TX_ACL) { 6635331Samw lr->lr_acl_bytes = aclbytes; 6645331Samw lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0; 6655331Samw lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0; 6665331Samw if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) 6675331Samw lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags; 6685331Samw else 6695331Samw lr->lr_acl_flags = 0; 6705331Samw } 6715331Samw lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt; 6725331Samw 6735331Samw if (txtype == TX_ACL_V0) { 6745331Samw lrv0 = (lr_acl_v0_t *)lr; 6755331Samw bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes); 6765331Samw } else { 6775331Samw void *start = (ace_t *)(lr + 1); 6785331Samw 6795331Samw bcopy(vsecp->vsa_aclentp, start, aclbytes); 6805331Samw 6815435Smarks start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes); 6825331Samw 6835331Samw if (fuidp) { 6845331Samw start = zfs_log_fuid_ids(fuidp, start); 6855331Samw (void) zfs_log_fuid_domains(fuidp, start); 6865331Samw } 6875331Samw } 688789Sahrens 6893063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 690789Sahrens seq = zil_itx_assign(zilog, itx, tx); 691789Sahrens zp->z_last_itx = seq; 692789Sahrens } 693