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