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