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