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