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