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 /* 229401SNeil.Perrin@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/types.h> 27789Sahrens #include <sys/param.h> 28789Sahrens #include <sys/systm.h> 29789Sahrens #include <sys/sysmacros.h> 30789Sahrens #include <sys/cmn_err.h> 31789Sahrens #include <sys/kmem.h> 32789Sahrens #include <sys/thread.h> 33789Sahrens #include <sys/file.h> 34789Sahrens #include <sys/vfs.h> 35789Sahrens #include <sys/zfs_znode.h> 36789Sahrens #include <sys/zfs_dir.h> 37789Sahrens #include <sys/zil.h> 384620Sperrin #include <sys/zil_impl.h> 39789Sahrens #include <sys/byteorder.h> 40789Sahrens #include <sys/policy.h> 41789Sahrens #include <sys/stat.h> 42789Sahrens #include <sys/mode.h> 43789Sahrens #include <sys/acl.h> 44789Sahrens #include <sys/dmu.h> 45789Sahrens #include <sys/spa.h> 465331Samw #include <sys/zfs_fuid.h> 47789Sahrens #include <sys/ddi.h> 488227SNeil.Perrin@Sun.COM #include <sys/dsl_dataset.h> 498227SNeil.Perrin@Sun.COM 508227SNeil.Perrin@Sun.COM #define ZFS_HANDLE_REPLAY(zilog, tx) \ 518227SNeil.Perrin@Sun.COM if (zilog->zl_replay) { \ 528227SNeil.Perrin@Sun.COM dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); \ 538227SNeil.Perrin@Sun.COM zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = \ 548227SNeil.Perrin@Sun.COM zilog->zl_replaying_seq; \ 558227SNeil.Perrin@Sun.COM return; \ 568227SNeil.Perrin@Sun.COM } 57789Sahrens 58789Sahrens /* 598227SNeil.Perrin@Sun.COM * These zfs_log_* functions must be called within a dmu tx, in one 608227SNeil.Perrin@Sun.COM * of 2 contexts depending on zilog->z_replay: 618227SNeil.Perrin@Sun.COM * 628227SNeil.Perrin@Sun.COM * Non replay mode 638227SNeil.Perrin@Sun.COM * --------------- 648227SNeil.Perrin@Sun.COM * We need to record the transaction so that if it is committed to 658227SNeil.Perrin@Sun.COM * the Intent Log then it can be replayed. An intent log transaction 668227SNeil.Perrin@Sun.COM * structure (itx_t) is allocated and all the information necessary to 678227SNeil.Perrin@Sun.COM * possibly replay the transaction is saved in it. The itx is then assigned 688227SNeil.Perrin@Sun.COM * a sequence number and inserted in the in-memory list anchored in the zilog. 698227SNeil.Perrin@Sun.COM * 708227SNeil.Perrin@Sun.COM * Replay mode 718227SNeil.Perrin@Sun.COM * ----------- 728227SNeil.Perrin@Sun.COM * We need to mark the intent log record as replayed in the log header. 738227SNeil.Perrin@Sun.COM * This is done in the same transaction as the replay so that they 748227SNeil.Perrin@Sun.COM * commit atomically. 75789Sahrens */ 76789Sahrens 775331Samw int 785331Samw zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap) 795331Samw { 805331Samw int isxvattr = (vap->va_mask & AT_XVATTR); 815331Samw switch (type) { 825331Samw case Z_FILE: 835331Samw if (vsecp == NULL && !isxvattr) 845331Samw return (TX_CREATE); 855331Samw if (vsecp && isxvattr) 865331Samw return (TX_CREATE_ACL_ATTR); 875331Samw if (vsecp) 885331Samw return (TX_CREATE_ACL); 895331Samw else 905331Samw return (TX_CREATE_ATTR); 915331Samw /*NOTREACHED*/ 925331Samw case Z_DIR: 935331Samw if (vsecp == NULL && !isxvattr) 945331Samw return (TX_MKDIR); 955331Samw if (vsecp && isxvattr) 965331Samw return (TX_MKDIR_ACL_ATTR); 975331Samw if (vsecp) 985331Samw return (TX_MKDIR_ACL); 995331Samw else 1005331Samw return (TX_MKDIR_ATTR); 1015331Samw case Z_XATTRDIR: 1025331Samw return (TX_MKXATTR); 1035331Samw } 1045331Samw ASSERT(0); 1055331Samw return (TX_MAX_TYPE); 1065331Samw } 1075331Samw 108789Sahrens /* 1095331Samw * build up the log data necessary for logging xvattr_t 1105331Samw * First lr_attr_t is initialized. following the lr_attr_t 1115331Samw * is the mapsize and attribute bitmap copied from the xvattr_t. 1125331Samw * Following the bitmap and bitmapsize two 64 bit words are reserved 1135331Samw * for the create time which may be set. Following the create time 1145331Samw * records a single 64 bit integer which has the bits to set on 1155331Samw * replay for the xvattr. 1165331Samw */ 1175331Samw static void 1185331Samw zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap) 1195331Samw { 1205331Samw uint32_t *bitmap; 1215331Samw uint64_t *attrs; 1225331Samw uint64_t *crtime; 1235331Samw xoptattr_t *xoap; 1245331Samw void *scanstamp; 1255331Samw int i; 1265331Samw 1275331Samw xoap = xva_getxoptattr(xvap); 1285331Samw ASSERT(xoap); 1295331Samw 1305331Samw lrattr->lr_attr_masksize = xvap->xva_mapsize; 1315331Samw bitmap = &lrattr->lr_attr_bitmap; 1325331Samw for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) { 1335331Samw *bitmap = xvap->xva_reqattrmap[i]; 1345331Samw } 1355331Samw 1365331Samw /* Now pack the attributes up in a single uint64_t */ 1375331Samw attrs = (uint64_t *)bitmap; 1385331Samw crtime = attrs + 1; 1395331Samw scanstamp = (caddr_t)(crtime + 2); 1405331Samw *attrs = 0; 1415331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) 1425331Samw *attrs |= (xoap->xoa_readonly == 0) ? 0 : 1435331Samw XAT0_READONLY; 1445331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) 1455331Samw *attrs |= (xoap->xoa_hidden == 0) ? 0 : 1465331Samw XAT0_HIDDEN; 1475331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) 1485331Samw *attrs |= (xoap->xoa_system == 0) ? 0 : 1495331Samw XAT0_SYSTEM; 1505331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) 1515331Samw *attrs |= (xoap->xoa_archive == 0) ? 0 : 1525331Samw XAT0_ARCHIVE; 1535331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) 1545331Samw *attrs |= (xoap->xoa_immutable == 0) ? 0 : 1555331Samw XAT0_IMMUTABLE; 1565331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) 1575331Samw *attrs |= (xoap->xoa_nounlink == 0) ? 0 : 1585331Samw XAT0_NOUNLINK; 1595331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) 1605331Samw *attrs |= (xoap->xoa_appendonly == 0) ? 0 : 1615331Samw XAT0_APPENDONLY; 1625331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) 1635331Samw *attrs |= (xoap->xoa_opaque == 0) ? 0 : 1645331Samw XAT0_APPENDONLY; 1655331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) 1665331Samw *attrs |= (xoap->xoa_nodump == 0) ? 0 : 1675331Samw XAT0_NODUMP; 1685331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) 1695331Samw *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 : 1705331Samw XAT0_AV_QUARANTINED; 1715331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) 1725331Samw *attrs |= (xoap->xoa_av_modified == 0) ? 0 : 1735331Samw XAT0_AV_MODIFIED; 1745331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) 1755331Samw ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime); 1765331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 1775331Samw bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ); 178*10793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) 179*10793Sdai.ngo@sun.com *attrs |= (xoap->xoa_reparse == 0) ? 0 : 180*10793Sdai.ngo@sun.com XAT0_REPARSE; 1815331Samw } 1825331Samw 1835331Samw static void * 1845331Samw zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start) 1855331Samw { 1865331Samw zfs_fuid_t *zfuid; 1875331Samw uint64_t *fuidloc = start; 1885331Samw 1895331Samw /* First copy in the ACE FUIDs */ 1905331Samw for (zfuid = list_head(&fuidp->z_fuids); zfuid; 1915331Samw zfuid = list_next(&fuidp->z_fuids, zfuid)) { 1925331Samw *fuidloc++ = zfuid->z_logfuid; 1935331Samw } 1945331Samw return (fuidloc); 1955331Samw } 1965331Samw 1975331Samw 1985331Samw static void * 1995331Samw zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start) 2005331Samw { 2015331Samw zfs_fuid_domain_t *zdomain; 2025331Samw 2035331Samw /* now copy in the domain info, if any */ 2045331Samw if (fuidp->z_domain_str_sz != 0) { 2055331Samw for (zdomain = list_head(&fuidp->z_domains); zdomain; 2065331Samw zdomain = list_next(&fuidp->z_domains, zdomain)) { 2075331Samw bcopy((void *)zdomain->z_domain, start, 2085331Samw strlen(zdomain->z_domain) + 1); 2095331Samw start = (caddr_t)start + 2105331Samw strlen(zdomain->z_domain) + 1; 2115331Samw } 2125331Samw } 2135331Samw return (start); 2145331Samw } 2155331Samw 2165331Samw /* 2175331Samw * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, 2185331Samw * TX_MKDIR_ATTR and TX_MKXATTR 219789Sahrens * transactions. 2205331Samw * 2215331Samw * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID 2225331Samw * domain information appended prior to the name. In this case the 2235331Samw * uid/gid in the log record will be a log centric FUID. 2245331Samw * 2255331Samw * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that 2265331Samw * may contain attributes, ACL and optional fuid information. 2275331Samw * 2285331Samw * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify 2295331Samw * and ACL and normal users/groups in the ACEs. 2305331Samw * 2315331Samw * There may be an optional xvattr attribute information similar 2325331Samw * to zfs_log_setattr. 2335331Samw * 2345331Samw * Also, after the file name "domain" strings may be appended. 235789Sahrens */ 2362638Sperrin void 2375331Samw zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 2385331Samw znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp, 2395331Samw zfs_fuid_info_t *fuidp, vattr_t *vap) 240789Sahrens { 241789Sahrens itx_t *itx; 242789Sahrens uint64_t seq; 243789Sahrens lr_create_t *lr; 2445331Samw lr_acl_create_t *lracl; 2455331Samw size_t aclsize; 2465331Samw size_t xvatsize = 0; 2475331Samw size_t txsize; 2485331Samw xvattr_t *xvap = (xvattr_t *)vap; 2495331Samw void *end; 2505331Samw size_t lrsize; 251789Sahrens size_t namesize = strlen(name) + 1; 2525331Samw size_t fuidsz = 0; 253789Sahrens 254789Sahrens if (zilog == NULL) 2552638Sperrin return; 256789Sahrens 2578227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 2588227SNeil.Perrin@Sun.COM 2595331Samw /* 2605331Samw * If we have FUIDs present then add in space for 2615331Samw * domains and ACE fuid's if any. 2625331Samw */ 2635331Samw if (fuidp) { 2645331Samw fuidsz += fuidp->z_domain_str_sz; 2655331Samw fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t); 2665331Samw } 2675331Samw 2685331Samw if (vap->va_mask & AT_XVATTR) 2695331Samw xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize); 2705331Samw 2715331Samw if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR || 2725331Samw (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR || 2735331Samw (int)txtype == TX_MKXATTR) { 2745331Samw txsize = sizeof (*lr) + namesize + fuidsz + xvatsize; 2755331Samw lrsize = sizeof (*lr); 2765331Samw } else { 2775331Samw aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0; 2785331Samw txsize = 2795331Samw sizeof (lr_acl_create_t) + namesize + fuidsz + 2805435Smarks ZIL_ACE_LENGTH(aclsize) + xvatsize; 2815331Samw lrsize = sizeof (lr_acl_create_t); 2825331Samw } 2835331Samw 2845331Samw itx = zil_itx_create(txtype, txsize); 2855331Samw 286789Sahrens lr = (lr_create_t *)&itx->itx_lr; 287789Sahrens lr->lr_doid = dzp->z_id; 288789Sahrens lr->lr_foid = zp->z_id; 289789Sahrens lr->lr_mode = zp->z_phys->zp_mode; 2905331Samw if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) { 2915331Samw lr->lr_uid = (uint64_t)zp->z_phys->zp_uid; 2925331Samw } else { 2935331Samw lr->lr_uid = fuidp->z_fuid_owner; 2945331Samw } 2955331Samw if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) { 2965331Samw lr->lr_gid = (uint64_t)zp->z_phys->zp_gid; 2975331Samw } else { 2985331Samw lr->lr_gid = fuidp->z_fuid_group; 2995331Samw } 300789Sahrens lr->lr_gen = zp->z_phys->zp_gen; 301789Sahrens lr->lr_crtime[0] = zp->z_phys->zp_crtime[0]; 302789Sahrens lr->lr_crtime[1] = zp->z_phys->zp_crtime[1]; 303789Sahrens lr->lr_rdev = zp->z_phys->zp_rdev; 3045331Samw 3055331Samw /* 3065331Samw * Fill in xvattr info if any 3075331Samw */ 3085331Samw if (vap->va_mask & AT_XVATTR) { 3095331Samw zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap); 3105331Samw end = (caddr_t)lr + lrsize + xvatsize; 3115331Samw } else { 3125331Samw end = (caddr_t)lr + lrsize; 3135331Samw } 3145331Samw 3155331Samw /* Now fill in any ACL info */ 3165331Samw 3175331Samw if (vsecp) { 3185331Samw lracl = (lr_acl_create_t *)&itx->itx_lr; 3195331Samw lracl->lr_aclcnt = vsecp->vsa_aclcnt; 3205331Samw lracl->lr_acl_bytes = aclsize; 3215331Samw lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0; 3225331Samw lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0; 3235331Samw if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS) 3245331Samw lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags; 3255331Samw else 3265331Samw lracl->lr_acl_flags = 0; 3275331Samw 3285331Samw bcopy(vsecp->vsa_aclentp, end, aclsize); 3295435Smarks end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize); 3305331Samw } 3315331Samw 3325331Samw /* drop in FUID info */ 3335331Samw if (fuidp) { 3345331Samw end = zfs_log_fuid_ids(fuidp, end); 3355331Samw end = zfs_log_fuid_domains(fuidp, end); 3365331Samw } 3375331Samw /* 3385331Samw * Now place file name in log record 3395331Samw */ 3405331Samw bcopy(name, end, namesize); 341789Sahrens 342789Sahrens seq = zil_itx_assign(zilog, itx, tx); 343789Sahrens dzp->z_last_itx = seq; 344789Sahrens zp->z_last_itx = seq; 345789Sahrens } 346789Sahrens 347789Sahrens /* 348789Sahrens * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions. 349789Sahrens */ 3502638Sperrin void 3515331Samw zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 352789Sahrens znode_t *dzp, char *name) 353789Sahrens { 354789Sahrens itx_t *itx; 355789Sahrens uint64_t seq; 356789Sahrens lr_remove_t *lr; 357789Sahrens size_t namesize = strlen(name) + 1; 358789Sahrens 359789Sahrens if (zilog == NULL) 3602638Sperrin return; 361789Sahrens 3628227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 3638227SNeil.Perrin@Sun.COM 364789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize); 365789Sahrens lr = (lr_remove_t *)&itx->itx_lr; 366789Sahrens lr->lr_doid = dzp->z_id; 367789Sahrens bcopy(name, (char *)(lr + 1), namesize); 368789Sahrens 369789Sahrens seq = zil_itx_assign(zilog, itx, tx); 370789Sahrens dzp->z_last_itx = seq; 371789Sahrens } 372789Sahrens 373789Sahrens /* 374789Sahrens * zfs_log_link() handles TX_LINK transactions. 375789Sahrens */ 3762638Sperrin void 3775331Samw zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 378789Sahrens znode_t *dzp, znode_t *zp, char *name) 379789Sahrens { 380789Sahrens itx_t *itx; 381789Sahrens uint64_t seq; 382789Sahrens lr_link_t *lr; 383789Sahrens size_t namesize = strlen(name) + 1; 384789Sahrens 385789Sahrens if (zilog == NULL) 3862638Sperrin return; 387789Sahrens 3888227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 3898227SNeil.Perrin@Sun.COM 390789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize); 391789Sahrens lr = (lr_link_t *)&itx->itx_lr; 392789Sahrens lr->lr_doid = dzp->z_id; 393789Sahrens lr->lr_link_obj = zp->z_id; 394789Sahrens bcopy(name, (char *)(lr + 1), namesize); 395789Sahrens 396789Sahrens seq = zil_itx_assign(zilog, itx, tx); 397789Sahrens dzp->z_last_itx = seq; 398789Sahrens zp->z_last_itx = seq; 399789Sahrens } 400789Sahrens 401789Sahrens /* 402789Sahrens * zfs_log_symlink() handles TX_SYMLINK transactions. 403789Sahrens */ 4042638Sperrin void 4055331Samw zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 4065331Samw znode_t *dzp, znode_t *zp, char *name, char *link) 407789Sahrens { 408789Sahrens itx_t *itx; 409789Sahrens uint64_t seq; 410789Sahrens lr_create_t *lr; 411789Sahrens size_t namesize = strlen(name) + 1; 412789Sahrens size_t linksize = strlen(link) + 1; 413789Sahrens 414789Sahrens if (zilog == NULL) 4152638Sperrin return; 416789Sahrens 4178227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 4188227SNeil.Perrin@Sun.COM 419789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize); 420789Sahrens lr = (lr_create_t *)&itx->itx_lr; 421789Sahrens lr->lr_doid = dzp->z_id; 422789Sahrens lr->lr_foid = zp->z_id; 423789Sahrens lr->lr_mode = zp->z_phys->zp_mode; 424789Sahrens lr->lr_uid = zp->z_phys->zp_uid; 425789Sahrens lr->lr_gid = zp->z_phys->zp_gid; 426789Sahrens lr->lr_gen = zp->z_phys->zp_gen; 427789Sahrens lr->lr_crtime[0] = zp->z_phys->zp_crtime[0]; 428789Sahrens lr->lr_crtime[1] = zp->z_phys->zp_crtime[1]; 429789Sahrens bcopy(name, (char *)(lr + 1), namesize); 430789Sahrens bcopy(link, (char *)(lr + 1) + namesize, linksize); 431789Sahrens 432789Sahrens seq = zil_itx_assign(zilog, itx, tx); 433789Sahrens dzp->z_last_itx = seq; 434789Sahrens zp->z_last_itx = seq; 435789Sahrens } 436789Sahrens 437789Sahrens /* 438789Sahrens * zfs_log_rename() handles TX_RENAME transactions. 439789Sahrens */ 4402638Sperrin void 4415331Samw zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 442789Sahrens znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp) 443789Sahrens { 444789Sahrens itx_t *itx; 445789Sahrens uint64_t seq; 446789Sahrens lr_rename_t *lr; 447789Sahrens size_t snamesize = strlen(sname) + 1; 448789Sahrens size_t dnamesize = strlen(dname) + 1; 449789Sahrens 450789Sahrens if (zilog == NULL) 4512638Sperrin return; 452789Sahrens 4538227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 4548227SNeil.Perrin@Sun.COM 455789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize); 456789Sahrens lr = (lr_rename_t *)&itx->itx_lr; 457789Sahrens lr->lr_sdoid = sdzp->z_id; 458789Sahrens lr->lr_tdoid = tdzp->z_id; 459789Sahrens bcopy(sname, (char *)(lr + 1), snamesize); 460789Sahrens bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize); 461789Sahrens 462789Sahrens seq = zil_itx_assign(zilog, itx, tx); 463789Sahrens sdzp->z_last_itx = seq; 464789Sahrens tdzp->z_last_itx = seq; 465789Sahrens szp->z_last_itx = seq; 466789Sahrens } 467789Sahrens 468789Sahrens /* 469789Sahrens * zfs_log_write() handles TX_WRITE transactions. 470789Sahrens */ 4712237Smaybee ssize_t zfs_immediate_write_sz = 32768; 472789Sahrens 4732638Sperrin void 474789Sahrens zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype, 4754620Sperrin znode_t *zp, offset_t off, ssize_t resid, int ioflag) 476789Sahrens { 4771669Sperrin itx_wr_state_t write_state; 4784620Sperrin boolean_t slogging; 4794720Sfr157268 uintptr_t fsync_cnt; 48010310SNeil.Perrin@Sun.COM ssize_t immediate_write_sz; 481789Sahrens 4823461Sahrens if (zilog == NULL || zp->z_unlinked) 4832638Sperrin return; 484789Sahrens 4858227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 4868227SNeil.Perrin@Sun.COM 48710310SNeil.Perrin@Sun.COM immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 48810310SNeil.Perrin@Sun.COM ? 0 : zfs_immediate_write_sz; 48910310SNeil.Perrin@Sun.COM 49010310SNeil.Perrin@Sun.COM slogging = spa_has_slogs(zilog->zl_spa) && 49110310SNeil.Perrin@Sun.COM (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 49210310SNeil.Perrin@Sun.COM if (resid > immediate_write_sz && !slogging && resid <= zp->z_blksz) 4931669Sperrin write_state = WR_INDIRECT; 4946396Sperrin else if (ioflag & (FSYNC | FDSYNC)) 4951669Sperrin write_state = WR_COPIED; 4963638Sbillm else 4971669Sperrin write_state = WR_NEED_COPY; 4983638Sbillm 4994720Sfr157268 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) { 5004720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1)); 5014720Sfr157268 } 5024720Sfr157268 5034620Sperrin while (resid) { 5044620Sperrin itx_t *itx; 5054620Sperrin lr_write_t *lr; 5064620Sperrin ssize_t len; 5074620Sperrin 5084620Sperrin /* 5097360SNeil.Perrin@Sun.COM * If the write would overflow the largest block then split it. 5104620Sperrin */ 5117360SNeil.Perrin@Sun.COM if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA) 5124620Sperrin len = SPA_MAXBLOCKSIZE >> 1; 5134620Sperrin else 5144620Sperrin len = resid; 5154620Sperrin 5164620Sperrin itx = zil_itx_create(txtype, sizeof (*lr) + 5174620Sperrin (write_state == WR_COPIED ? len : 0)); 5184620Sperrin lr = (lr_write_t *)&itx->itx_lr; 5194620Sperrin if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os, 5209512SNeil.Perrin@Sun.COM zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 5213638Sbillm kmem_free(itx, offsetof(itx_t, itx_lr) + 5223638Sbillm itx->itx_lr.lrc_reclen); 5231669Sperrin itx = zil_itx_create(txtype, sizeof (*lr)); 5243638Sbillm lr = (lr_write_t *)&itx->itx_lr; 5251669Sperrin write_state = WR_NEED_COPY; 5261669Sperrin } 5274620Sperrin 5284620Sperrin itx->itx_wr_state = write_state; 5296101Sperrin if (write_state == WR_NEED_COPY) 5306101Sperrin itx->itx_sod += len; 5314620Sperrin lr->lr_foid = zp->z_id; 5324620Sperrin lr->lr_offset = off; 5334620Sperrin lr->lr_length = len; 5344620Sperrin lr->lr_blkoff = 0; 5354620Sperrin BP_ZERO(&lr->lr_blkptr); 5363638Sbillm 5374620Sperrin itx->itx_private = zp->z_zfsvfs; 538789Sahrens 5396396Sperrin if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0) || 5406396Sperrin (ioflag & (FSYNC | FDSYNC))) 5414720Sfr157268 itx->itx_sync = B_TRUE; 5424720Sfr157268 else 5434720Sfr157268 itx->itx_sync = B_FALSE; 5444720Sfr157268 5454620Sperrin zp->z_last_itx = zil_itx_assign(zilog, itx, tx); 546789Sahrens 5474620Sperrin off += len; 5484620Sperrin resid -= len; 5494620Sperrin } 550789Sahrens } 551789Sahrens 552789Sahrens /* 553789Sahrens * zfs_log_truncate() handles TX_TRUNCATE transactions. 554789Sahrens */ 5552638Sperrin void 556789Sahrens zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype, 557789Sahrens znode_t *zp, uint64_t off, uint64_t len) 558789Sahrens { 559789Sahrens itx_t *itx; 560789Sahrens uint64_t seq; 561789Sahrens lr_truncate_t *lr; 562789Sahrens 5633461Sahrens if (zilog == NULL || zp->z_unlinked) 5642638Sperrin return; 565789Sahrens 5668227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 5678227SNeil.Perrin@Sun.COM 568789Sahrens itx = zil_itx_create(txtype, sizeof (*lr)); 569789Sahrens lr = (lr_truncate_t *)&itx->itx_lr; 570789Sahrens lr->lr_foid = zp->z_id; 571789Sahrens lr->lr_offset = off; 572789Sahrens lr->lr_length = len; 573789Sahrens 5743063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 575789Sahrens seq = zil_itx_assign(zilog, itx, tx); 576789Sahrens zp->z_last_itx = seq; 577789Sahrens } 578789Sahrens 579789Sahrens /* 580789Sahrens * zfs_log_setattr() handles TX_SETATTR transactions. 581789Sahrens */ 5822638Sperrin void 583789Sahrens zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype, 5845331Samw znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp) 585789Sahrens { 5865331Samw itx_t *itx; 5875331Samw uint64_t seq; 5885331Samw lr_setattr_t *lr; 5895331Samw xvattr_t *xvap = (xvattr_t *)vap; 5905331Samw size_t recsize = sizeof (lr_setattr_t); 5915331Samw void *start; 5925331Samw 593789Sahrens 5943461Sahrens if (zilog == NULL || zp->z_unlinked) 5952638Sperrin return; 596789Sahrens 5978227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 5988227SNeil.Perrin@Sun.COM 5995331Samw /* 6005331Samw * If XVATTR set, then log record size needs to allow 6015331Samw * for lr_attr_t + xvattr mask, mapsize and create time 6025331Samw * plus actual attribute values 6035331Samw */ 6045331Samw if (vap->va_mask & AT_XVATTR) 6055331Samw recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize); 6065331Samw 6075331Samw if (fuidp) 6085331Samw recsize += fuidp->z_domain_str_sz; 6095331Samw 6105331Samw itx = zil_itx_create(txtype, recsize); 611789Sahrens lr = (lr_setattr_t *)&itx->itx_lr; 612789Sahrens lr->lr_foid = zp->z_id; 613789Sahrens lr->lr_mask = (uint64_t)mask_applied; 614789Sahrens lr->lr_mode = (uint64_t)vap->va_mode; 6155331Samw if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid)) 6165331Samw lr->lr_uid = fuidp->z_fuid_owner; 6175331Samw else 6185331Samw lr->lr_uid = (uint64_t)vap->va_uid; 6195331Samw 6205331Samw if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid)) 6215331Samw lr->lr_gid = fuidp->z_fuid_group; 6225331Samw else 6235331Samw lr->lr_gid = (uint64_t)vap->va_gid; 6245331Samw 625789Sahrens lr->lr_size = (uint64_t)vap->va_size; 626789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime); 627789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime); 6285331Samw start = (lr_setattr_t *)(lr + 1); 6295331Samw if (vap->va_mask & AT_XVATTR) { 6305331Samw zfs_log_xvattr((lr_attr_t *)start, xvap); 6315331Samw start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize); 6325331Samw } 6335331Samw 6345331Samw /* 6355331Samw * Now stick on domain information if any on end 6365331Samw */ 6375331Samw 6385331Samw if (fuidp) 6395331Samw (void) zfs_log_fuid_domains(fuidp, start); 640789Sahrens 6413063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 642789Sahrens seq = zil_itx_assign(zilog, itx, tx); 643789Sahrens zp->z_last_itx = seq; 644789Sahrens } 645789Sahrens 646789Sahrens /* 647789Sahrens * zfs_log_acl() handles TX_ACL transactions. 648789Sahrens */ 6492638Sperrin void 6505331Samw zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp, 6515331Samw vsecattr_t *vsecp, zfs_fuid_info_t *fuidp) 652789Sahrens { 653789Sahrens itx_t *itx; 654789Sahrens uint64_t seq; 6555331Samw lr_acl_v0_t *lrv0; 656789Sahrens lr_acl_t *lr; 6575331Samw int txtype; 6585331Samw int lrsize; 6595331Samw size_t txsize; 6605331Samw size_t aclbytes = vsecp->vsa_aclentsz; 6615331Samw 6626514Smarks if (zilog == NULL || zp->z_unlinked) 6636514Smarks return; 6646514Smarks 6658227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 6668227SNeil.Perrin@Sun.COM 6676514Smarks txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ? 6685331Samw TX_ACL_V0 : TX_ACL; 6695331Samw 6705331Samw if (txtype == TX_ACL) 6715331Samw lrsize = sizeof (*lr); 6725331Samw else 6735331Samw lrsize = sizeof (*lrv0); 674789Sahrens 6755435Smarks txsize = lrsize + 6765435Smarks ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) + 6775435Smarks (fuidp ? fuidp->z_domain_str_sz : 0) + 6786514Smarks sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0); 6795331Samw 6805331Samw itx = zil_itx_create(txtype, txsize); 6815331Samw 682789Sahrens lr = (lr_acl_t *)&itx->itx_lr; 683789Sahrens lr->lr_foid = zp->z_id; 6845331Samw if (txtype == TX_ACL) { 6855331Samw lr->lr_acl_bytes = aclbytes; 6865331Samw lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0; 6875331Samw lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0; 6885331Samw if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) 6895331Samw lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags; 6905331Samw else 6915331Samw lr->lr_acl_flags = 0; 6925331Samw } 6935331Samw lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt; 6945331Samw 6955331Samw if (txtype == TX_ACL_V0) { 6965331Samw lrv0 = (lr_acl_v0_t *)lr; 6975331Samw bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes); 6985331Samw } else { 6995331Samw void *start = (ace_t *)(lr + 1); 7005331Samw 7015331Samw bcopy(vsecp->vsa_aclentp, start, aclbytes); 7025331Samw 7035435Smarks start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes); 7045331Samw 7055331Samw if (fuidp) { 7065331Samw start = zfs_log_fuid_ids(fuidp, start); 7075331Samw (void) zfs_log_fuid_domains(fuidp, start); 7085331Samw } 7095331Samw } 710789Sahrens 7113063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 712789Sahrens seq = zil_itx_assign(zilog, itx, tx); 713789Sahrens zp->z_last_itx = seq; 714789Sahrens } 715