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