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