]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_replay.c
Fix BIND named(8) cache poisoning with DNSSEC validation.
[FreeBSD/releng/8.0.git] / sys / cddl / contrib / opensolaris / uts / common / fs / 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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #pragma ident   "%Z%%M% %I%     %E% SMI"
27
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/cmn_err.h>
33 #include <sys/kmem.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/acl.h>
47 #include <sys/atomic.h>
48 #include <sys/cred.h>
49 #include <sys/namei.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
57 static void
58 zfs_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         VATTR_NULL(vap);
62         vap->va_mask = (uint_t)mask;
63         if (mask & AT_TYPE)
64                 vap->va_type = IFTOVT(mode);
65         if (mask & AT_MODE)
66                 vap->va_mode = mode & MODEMASK;
67         if (mask & AT_UID)
68                 vap->va_uid = (uid_t)(IS_EPHEMERAL(uid)) ? -1 : uid;
69         if (mask & AT_GID)
70                 vap->va_gid = (gid_t)(IS_EPHEMERAL(gid)) ? -1 : gid;
71         vap->va_rdev = zfs_cmpldev(rdev);
72         vap->va_nodeid = nodeid;
73 }
74
75 /* ARGSUSED */
76 static int
77 zfs_replay_error(zfsvfs_t *zfsvfs, lr_t *lr, boolean_t byteswap)
78 {
79         return (ENOTSUP);
80 }
81
82 static void
83 zfs_replay_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
84 {
85         xoptattr_t *xoap = NULL;
86         uint64_t *attrs;
87         uint64_t *crtime;
88         uint32_t *bitmap;
89         void *scanstamp;
90         int i;
91
92         xvap->xva_vattr.va_mask |= AT_XVATTR;
93         if ((xoap = xva_getxoptattr(xvap)) == NULL) {
94                 xvap->xva_vattr.va_mask &= ~AT_XVATTR; /* shouldn't happen */
95                 return;
96         }
97
98         ASSERT(lrattr->lr_attr_masksize == xvap->xva_mapsize);
99
100         bitmap = &lrattr->lr_attr_bitmap;
101         for (i = 0; i != lrattr->lr_attr_masksize; i++, bitmap++)
102                 xvap->xva_reqattrmap[i] = *bitmap;
103
104         attrs = (uint64_t *)(lrattr + lrattr->lr_attr_masksize - 1);
105         crtime = attrs + 1;
106         scanstamp = (caddr_t)(crtime + 2);
107
108         if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
109                 xoap->xoa_hidden = ((*attrs & XAT0_HIDDEN) != 0);
110         if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
111                 xoap->xoa_system = ((*attrs & XAT0_SYSTEM) != 0);
112         if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
113                 xoap->xoa_archive = ((*attrs & XAT0_ARCHIVE) != 0);
114         if (XVA_ISSET_REQ(xvap, XAT_READONLY))
115                 xoap->xoa_readonly = ((*attrs & XAT0_READONLY) != 0);
116         if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
117                 xoap->xoa_immutable = ((*attrs & XAT0_IMMUTABLE) != 0);
118         if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
119                 xoap->xoa_nounlink = ((*attrs & XAT0_NOUNLINK) != 0);
120         if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
121                 xoap->xoa_appendonly = ((*attrs & XAT0_APPENDONLY) != 0);
122         if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
123                 xoap->xoa_nodump = ((*attrs & XAT0_NODUMP) != 0);
124         if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
125                 xoap->xoa_opaque = ((*attrs & XAT0_OPAQUE) != 0);
126         if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
127                 xoap->xoa_av_modified = ((*attrs & XAT0_AV_MODIFIED) != 0);
128         if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
129                 xoap->xoa_av_quarantined =
130                     ((*attrs & XAT0_AV_QUARANTINED) != 0);
131         if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
132                 ZFS_TIME_DECODE(&xoap->xoa_createtime, crtime);
133         if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
134                 bcopy(scanstamp, xoap->xoa_av_scanstamp, AV_SCANSTAMP_SZ);
135 }
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(zfsvfs_t *zfsvfs,
265     lr_acl_create_t *lracl, boolean_t byteswap)
266 {
267         char *name = NULL;              /* location determined later */
268         lr_create_t *lr = (lr_create_t *)lracl;
269         znode_t *dzp;
270         vnode_t *vp = NULL;
271         xvattr_t xva;
272         int vflg = 0;
273         vsecattr_t vsec = { 0 };
274         lr_attr_t *lrattr;
275         void *aclstart;
276         void *fuidstart;
277         size_t xvatlen = 0;
278         uint64_t txtype;
279         int error;
280
281         if (byteswap) {
282                 byteswap_uint64_array(lracl, sizeof (*lracl));
283                 txtype = (int)lr->lr_common.lrc_txtype;
284                 if (txtype == TX_CREATE_ACL_ATTR ||
285                     txtype == TX_MKDIR_ACL_ATTR) {
286                         lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
287                         zfs_replay_swap_attrs(lrattr);
288                         xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
289                 }
290
291                 aclstart = (caddr_t)(lracl + 1) + xvatlen;
292                 zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE);
293                 /* swap fuids */
294                 if (lracl->lr_fuidcnt) {
295                         byteswap_uint64_array((caddr_t)aclstart +
296                             ZIL_ACE_LENGTH(lracl->lr_acl_bytes),
297                             lracl->lr_fuidcnt * sizeof (uint64_t));
298                 }
299         }
300
301         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
302                 return (error);
303
304         xva_init(&xva);
305         zfs_init_vattr(&xva.xva_vattr, AT_TYPE | AT_MODE | AT_UID | AT_GID,
306             lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
307
308         /*
309          * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
310          * eventually end up in zfs_mknode(), which assigns the object's
311          * creation time and generation number.  The generic VOP_CREATE()
312          * doesn't have either concept, so we smuggle the values inside
313          * the vattr's otherwise unused va_ctime and va_nblocks fields.
314          */
315         ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
316         xva.xva_vattr.va_nblocks = lr->lr_gen;
317
318         error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
319         if (error != ENOENT)
320                 goto bail;
321
322         if (lr->lr_common.lrc_txtype & TX_CI)
323                 vflg |= FIGNORECASE;
324         switch ((int)lr->lr_common.lrc_txtype) {
325         case TX_CREATE_ACL:
326                 aclstart = (caddr_t)(lracl + 1);
327                 fuidstart = (caddr_t)aclstart +
328                     ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
329                 zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
330                     (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
331                     lr->lr_uid, lr->lr_gid);
332                 /*FALLTHROUGH*/
333         case TX_CREATE_ACL_ATTR:
334                 if (name == NULL) {
335                         lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
336                         xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
337                         xva.xva_vattr.va_mask |= AT_XVATTR;
338                         zfs_replay_xvattr(lrattr, &xva);
339                 }
340                 vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
341                 vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
342                 vsec.vsa_aclcnt = lracl->lr_aclcnt;
343                 vsec.vsa_aclentsz = lracl->lr_acl_bytes;
344                 vsec.vsa_aclflags = lracl->lr_acl_flags;
345                 if (zfsvfs->z_fuid_replay == NULL) {
346                         fuidstart = (caddr_t)(lracl + 1) + xvatlen +
347                             ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
348                         zfsvfs->z_fuid_replay =
349                             zfs_replay_fuids(fuidstart,
350                             (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
351                             lr->lr_uid, lr->lr_gid);
352                 }
353
354 #ifdef TODO
355                 error = VOP_CREATE(ZTOV(dzp), name, &xva.xva_vattr,
356                     0, 0, &vp, kcred, vflg, NULL, &vsec);
357 #else
358                 panic("%s:%u: unsupported condition", __func__, __LINE__);
359 #endif
360                 break;
361         case TX_MKDIR_ACL:
362                 aclstart = (caddr_t)(lracl + 1);
363                 fuidstart = (caddr_t)aclstart +
364                     ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
365                 zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
366                     (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
367                     lr->lr_uid, lr->lr_gid);
368                 /*FALLTHROUGH*/
369         case TX_MKDIR_ACL_ATTR:
370                 if (name == NULL) {
371                         lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
372                         xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
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 #ifdef TODO
389                 error = VOP_MKDIR(ZTOV(dzp), name, &xva.xva_vattr,
390                     &vp, kcred, NULL, vflg, &vsec);
391 #else
392                 panic("%s:%u: unsupported condition", __func__, __LINE__);
393 #endif
394                 break;
395         default:
396                 error = ENOTSUP;
397         }
398
399 bail:
400         if (error == 0 && vp != NULL)
401                 VN_RELE(vp);
402
403         VN_RELE(ZTOV(dzp));
404
405         zfs_fuid_info_free(zfsvfs->z_fuid_replay);
406         zfsvfs->z_fuid_replay = NULL;
407
408         return (error);
409 }
410
411 static int
412 zfs_replay_create(zfsvfs_t *zfsvfs, 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         vnode_t *vp = NULL;
418         xvattr_t xva;
419         int vflg = 0;
420         size_t lrsize = sizeof (lr_create_t);
421         lr_attr_t *lrattr;
422         void *start;
423         size_t xvatlen;
424         uint64_t txtype;
425         struct componentname cn;
426         int error;
427
428         if (byteswap) {
429                 byteswap_uint64_array(lr, sizeof (*lr));
430                 txtype = (int)lr->lr_common.lrc_txtype;
431                 if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR)
432                         zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
433         }
434
435
436         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
437                 return (error);
438
439         xva_init(&xva);
440         zfs_init_vattr(&xva.xva_vattr, AT_TYPE | AT_MODE | AT_UID | AT_GID,
441             lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
442
443         /*
444          * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
445          * eventually end up in zfs_mknode(), which assigns the object's
446          * creation time and generation number.  The generic VOP_CREATE()
447          * doesn't have either concept, so we smuggle the values inside
448          * the vattr's otherwise unused va_ctime and va_nblocks fields.
449          */
450         ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
451         xva.xva_vattr.va_nblocks = lr->lr_gen;
452
453         error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
454         if (error != ENOENT)
455                 goto out;
456
457         if (lr->lr_common.lrc_txtype & TX_CI)
458                 vflg |= FIGNORECASE;
459
460         /*
461          * Symlinks don't have fuid info, and CIFS never creates
462          * symlinks.
463          *
464          * The _ATTR versions will grab the fuid info in their subcases.
465          */
466         if ((int)lr->lr_common.lrc_txtype != TX_SYMLINK &&
467             (int)lr->lr_common.lrc_txtype != TX_MKDIR_ATTR &&
468             (int)lr->lr_common.lrc_txtype != TX_CREATE_ATTR) {
469                 start = (lr + 1);
470                 zfsvfs->z_fuid_replay =
471                     zfs_replay_fuid_domain(start, &start,
472                     lr->lr_uid, lr->lr_gid);
473         }
474
475         cn.cn_cred = kcred;
476         cn.cn_thread = curthread;
477         cn.cn_flags = SAVENAME;
478
479         vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
480         switch ((int)lr->lr_common.lrc_txtype) {
481         case TX_CREATE_ATTR:
482                 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
483                 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
484                 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
485                 start = (caddr_t)(lr + 1) + xvatlen;
486                 zfsvfs->z_fuid_replay =
487                     zfs_replay_fuid_domain(start, &start,
488                     lr->lr_uid, lr->lr_gid);
489                 name = (char *)start;
490
491                 /*FALLTHROUGH*/
492         case TX_CREATE:
493                 if (name == NULL)
494                         name = (char *)start;
495
496                 cn.cn_nameptr = name;
497                 error = VOP_CREATE(ZTOV(dzp), &vp, &cn, &xva.xva_vattr /*,vflg*/);
498                 break;
499         case TX_MKDIR_ATTR:
500                 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
501                 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
502                 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
503                 start = (caddr_t)(lr + 1) + xvatlen;
504                 zfsvfs->z_fuid_replay =
505                     zfs_replay_fuid_domain(start, &start,
506                     lr->lr_uid, lr->lr_gid);
507                 name = (char *)start;
508
509                 /*FALLTHROUGH*/
510         case TX_MKDIR:
511                 if (name == NULL)
512                         name = (char *)(lr + 1);
513
514                 cn.cn_nameptr = name;
515                 error = VOP_MKDIR(ZTOV(dzp), &vp, &cn, &xva.xva_vattr /*,vflg*/);
516                 break;
517         case TX_MKXATTR:
518                 name = (char *)(lr + 1);
519                 error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &vp, kcred);
520                 break;
521         case TX_SYMLINK:
522                 name = (char *)(lr + 1);
523                 link = name + strlen(name) + 1;
524                 cn.cn_nameptr = name;
525                 error = VOP_SYMLINK(ZTOV(dzp), &vp, &cn, &xva.xva_vattr, link /*,vflg*/);
526                 break;
527         default:
528                 error = ENOTSUP;
529         }
530         VOP_UNLOCK(ZTOV(dzp), 0);
531
532 out:
533         if (error == 0 && vp != NULL) {
534                 VOP_UNLOCK(vp, 0);
535                 VN_RELE(vp);
536         }
537
538         VN_RELE(ZTOV(dzp));
539
540         if (zfsvfs->z_fuid_replay)
541                 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
542         zfsvfs->z_fuid_replay = NULL;
543         return (error);
544 }
545
546 static int
547 zfs_replay_remove(zfsvfs_t *zfsvfs, lr_remove_t *lr, boolean_t byteswap)
548 {
549         char *name = (char *)(lr + 1);  /* name follows lr_remove_t */
550         znode_t *dzp;
551         struct componentname cn;
552         vnode_t *vp;
553         int error;
554         int vflg = 0;
555
556         if (byteswap)
557                 byteswap_uint64_array(lr, sizeof (*lr));
558
559         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
560                 return (error);
561
562         if (lr->lr_common.lrc_txtype & TX_CI)
563                 vflg |= FIGNORECASE;
564         cn.cn_nameptr = name;
565         cn.cn_namelen = strlen(name);
566         cn.cn_nameiop = DELETE;
567         cn.cn_flags = ISLASTCN | SAVENAME;
568         cn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
569         cn.cn_cred = kcred;
570         cn.cn_thread = curthread;
571         vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
572         error = VOP_LOOKUP(ZTOV(dzp), &vp, &cn);
573         if (error != 0) {
574                 VOP_UNLOCK(ZTOV(dzp), 0);
575                 goto fail;
576         }
577
578         switch ((int)lr->lr_common.lrc_txtype) {
579         case TX_REMOVE:
580                 error = VOP_REMOVE(ZTOV(dzp), vp, &cn /*,vflg*/);
581                 break;
582         case TX_RMDIR:
583                 error = VOP_RMDIR(ZTOV(dzp), vp, &cn /*,vflg*/);
584                 break;
585         default:
586                 error = ENOTSUP;
587         }
588         vput(vp);
589         VOP_UNLOCK(ZTOV(dzp), 0);
590 fail:
591         VN_RELE(ZTOV(dzp));
592
593         return (error);
594 }
595
596 static int
597 zfs_replay_link(zfsvfs_t *zfsvfs, lr_link_t *lr, boolean_t byteswap)
598 {
599         char *name = (char *)(lr + 1);  /* name follows lr_link_t */
600         znode_t *dzp, *zp;
601         struct componentname cn;
602         int error;
603         int vflg = 0;
604
605         if (byteswap)
606                 byteswap_uint64_array(lr, sizeof (*lr));
607
608         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
609                 return (error);
610
611         if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
612                 VN_RELE(ZTOV(dzp));
613                 return (error);
614         }
615
616         if (lr->lr_common.lrc_txtype & TX_CI)
617                 vflg |= FIGNORECASE;
618         cn.cn_nameptr = name;
619         cn.cn_cred = kcred;
620         cn.cn_thread = curthread;
621         cn.cn_flags = SAVENAME;
622
623         vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
624         vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
625         error = VOP_LINK(ZTOV(dzp), ZTOV(zp), &cn /*,vflg*/);
626         VOP_UNLOCK(ZTOV(zp), 0);
627         VOP_UNLOCK(ZTOV(dzp), 0);
628
629         VN_RELE(ZTOV(zp));
630         VN_RELE(ZTOV(dzp));
631
632         return (error);
633 }
634
635 static int
636 zfs_replay_rename(zfsvfs_t *zfsvfs, lr_rename_t *lr, boolean_t byteswap)
637 {
638         char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */
639         char *tname = sname + strlen(sname) + 1;
640         znode_t *sdzp, *tdzp;
641         struct componentname scn, tcn;
642         vnode_t *svp, *tvp;
643         kthread_t *td = curthread;
644         int error;
645         int vflg = 0;
646
647         if (byteswap)
648                 byteswap_uint64_array(lr, sizeof (*lr));
649
650         if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
651                 return (error);
652
653         if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
654                 VN_RELE(ZTOV(sdzp));
655                 return (error);
656         }
657
658         if (lr->lr_common.lrc_txtype & TX_CI)
659                 vflg |= FIGNORECASE;
660         svp = tvp = NULL;
661
662         scn.cn_nameptr = sname;
663         scn.cn_namelen = strlen(sname);
664         scn.cn_nameiop = DELETE;
665         scn.cn_flags = ISLASTCN | SAVENAME;
666         scn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
667         scn.cn_cred = kcred;
668         scn.cn_thread = td;
669         vn_lock(ZTOV(sdzp), LK_EXCLUSIVE | LK_RETRY);
670         error = VOP_LOOKUP(ZTOV(sdzp), &svp, &scn);
671         VOP_UNLOCK(ZTOV(sdzp), 0);
672         if (error != 0)
673                 goto fail;
674         VOP_UNLOCK(svp, 0);
675
676         tcn.cn_nameptr = tname;
677         tcn.cn_namelen = strlen(tname);
678         tcn.cn_nameiop = RENAME;
679         tcn.cn_flags = ISLASTCN | SAVENAME;
680         tcn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
681         tcn.cn_cred = kcred;
682         tcn.cn_thread = td;
683         vn_lock(ZTOV(tdzp), LK_EXCLUSIVE | LK_RETRY);
684         error = VOP_LOOKUP(ZTOV(tdzp), &tvp, &tcn);
685         if (error == EJUSTRETURN)
686                 tvp = NULL;
687         else if (error != 0) {
688                 VOP_UNLOCK(ZTOV(tdzp), 0);
689                 goto fail;
690         }
691
692         error = VOP_RENAME(ZTOV(sdzp), svp, &scn, ZTOV(tdzp), tvp, &tcn /*,vflg*/);
693         return (error);
694 fail:
695         if (svp != NULL)
696                 vrele(svp);
697         if (tvp != NULL)
698                 vrele(tvp);
699         VN_RELE(ZTOV(tdzp));
700         VN_RELE(ZTOV(sdzp));
701
702         return (error);
703 }
704
705 static int
706 zfs_replay_write(zfsvfs_t *zfsvfs, lr_write_t *lr, boolean_t byteswap)
707 {
708         char *data = (char *)(lr + 1);  /* data follows lr_write_t */
709         znode_t *zp;
710         int error;
711         ssize_t resid;
712
713         if (byteswap)
714                 byteswap_uint64_array(lr, sizeof (*lr));
715
716         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
717                 /*
718                  * As we can log writes out of order, it's possible the
719                  * file has been removed. In this case just drop the write
720                  * and return success.
721                  */
722                 if (error == ENOENT)
723                         error = 0;
724                 return (error);
725         }
726
727         error = vn_rdwr(UIO_WRITE, ZTOV(zp), data, lr->lr_length,
728             lr->lr_offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
729
730         VN_RELE(ZTOV(zp));
731
732         return (error);
733 }
734
735 static int
736 zfs_replay_truncate(zfsvfs_t *zfsvfs, lr_truncate_t *lr, boolean_t byteswap)
737 {
738
739         ZFS_LOG(0, "Unexpected code path, report to pjd@FreeBSD.org");
740         return (EOPNOTSUPP);
741 }
742
743 static int
744 zfs_replay_setattr(zfsvfs_t *zfsvfs, lr_setattr_t *lr, boolean_t byteswap)
745 {
746         znode_t *zp;
747         xvattr_t xva;
748         vattr_t *vap = &xva.xva_vattr;
749         vnode_t *vp;
750         int error;
751         void *start;
752
753         xva_init(&xva);
754         if (byteswap) {
755                 byteswap_uint64_array(lr, sizeof (*lr));
756
757                 if ((lr->lr_mask & AT_XVATTR) &&
758                     zfsvfs->z_version >= ZPL_VERSION_INITIAL)
759                         zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
760         }
761
762         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
763                 /*
764                  * As we can log setattrs out of order, it's possible the
765                  * file has been removed. In this case just drop the setattr
766                  * and return success.
767                  */
768                 if (error == ENOENT)
769                         error = 0;
770                 return (error);
771         }
772
773         zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode,
774             lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
775
776         vap->va_size = lr->lr_size;
777         ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime);
778         ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime);
779
780         /*
781          * Fill in xvattr_t portions if necessary.
782          */
783
784         start = (lr_setattr_t *)(lr + 1);
785         if (vap->va_mask & AT_XVATTR) {
786                 zfs_replay_xvattr((lr_attr_t *)start, &xva);
787                 start = (caddr_t)start +
788                     ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize);
789         } else
790                 xva.xva_vattr.va_mask &= ~AT_XVATTR;
791
792         zfsvfs->z_fuid_replay = zfs_replay_fuid_domain(start, &start,
793             lr->lr_uid, lr->lr_gid);
794
795         vp = ZTOV(zp);
796         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
797         error = VOP_SETATTR(vp, vap, kcred);
798         VOP_UNLOCK(vp, 0);
799
800         zfs_fuid_info_free(zfsvfs->z_fuid_replay);
801         zfsvfs->z_fuid_replay = NULL;
802         VN_RELE(vp);
803
804         return (error);
805 }
806
807 static int
808 zfs_replay_acl_v0(zfsvfs_t *zfsvfs, lr_acl_v0_t *lr, boolean_t byteswap)
809 {
810         ace_t *ace = (ace_t *)(lr + 1); /* ace array follows lr_acl_t */
811         vsecattr_t vsa;
812         znode_t *zp;
813         int error;
814
815         if (byteswap) {
816                 byteswap_uint64_array(lr, sizeof (*lr));
817                 zfs_oldace_byteswap(ace, lr->lr_aclcnt);
818         }
819
820         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
821                 /*
822                  * As we can log acls out of order, it's possible the
823                  * file has been removed. In this case just drop the acl
824                  * and return success.
825                  */
826                 if (error == ENOENT)
827                         error = 0;
828                 return (error);
829         }
830
831         bzero(&vsa, sizeof (vsa));
832         vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
833         vsa.vsa_aclcnt = lr->lr_aclcnt;
834         vsa.vsa_aclentsz = sizeof (ace_t) * vsa.vsa_aclcnt;
835         vsa.vsa_aclflags = 0;
836         vsa.vsa_aclentp = ace;
837
838 #ifdef TODO
839         error = VOP_SETSECATTR(ZTOV(zp), &vsa, 0, kcred, NULL);
840 #else
841         panic("%s:%u: unsupported condition", __func__, __LINE__);
842 #endif
843
844         VN_RELE(ZTOV(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(zfsvfs_t *zfsvfs, 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(zfsvfs, lr->lr_foid, &zp)) != 0) {
882                 /*
883                  * As we can log acls out of order, it's possible the
884                  * file has been removed. In this case just drop the acl
885                  * and return success.
886                  */
887                 if (error == ENOENT)
888                         error = 0;
889                 return (error);
890         }
891
892 #ifdef TODO
893         bzero(&vsa, sizeof (vsa));
894         vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
895         vsa.vsa_aclcnt = lr->lr_aclcnt;
896         vsa.vsa_aclentp = ace;
897         vsa.vsa_aclentsz = lr->lr_acl_bytes;
898         vsa.vsa_aclflags = lr->lr_acl_flags;
899
900         if (lr->lr_fuidcnt) {
901                 void *fuidstart = (caddr_t)ace +
902                     ZIL_ACE_LENGTH(lr->lr_acl_bytes);
903
904                 zfsvfs->z_fuid_replay =
905                     zfs_replay_fuids(fuidstart, &fuidstart,
906                     lr->lr_fuidcnt, lr->lr_domcnt, 0, 0);
907         }
908
909         error = VOP_SETSECATTR(ZTOV(zp), &vsa, 0, kcred, NULL);
910
911         if (zfsvfs->z_fuid_replay)
912                 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
913 #else
914         error = EOPNOTSUPP;
915 #endif
916
917         zfsvfs->z_fuid_replay = NULL;
918         VN_RELE(ZTOV(zp));
919
920         return (error);
921 }
922
923 /*
924  * Callback vectors for replaying records
925  */
926 zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
927         zfs_replay_error,       /* 0 no such transaction type */
928         zfs_replay_create,      /* TX_CREATE */
929         zfs_replay_create,      /* TX_MKDIR */
930         zfs_replay_create,      /* TX_MKXATTR */
931         zfs_replay_create,      /* TX_SYMLINK */
932         zfs_replay_remove,      /* TX_REMOVE */
933         zfs_replay_remove,      /* TX_RMDIR */
934         zfs_replay_link,        /* TX_LINK */
935         zfs_replay_rename,      /* TX_RENAME */
936         zfs_replay_write,       /* TX_WRITE */
937         zfs_replay_truncate,    /* TX_TRUNCATE */
938         zfs_replay_setattr,     /* TX_SETATTR */
939         zfs_replay_acl_v0,      /* TX_ACL_V0 */
940         zfs_replay_acl,         /* TX_ACL */
941         zfs_replay_create_acl,  /* TX_CREATE_ACL */
942         zfs_replay_create,      /* TX_CREATE_ATTR */
943         zfs_replay_create_acl,  /* TX_CREATE_ACL_ATTR */
944         zfs_replay_create_acl,  /* TX_MKDIR_ACL */
945         zfs_replay_create,      /* TX_MKDIR_ATTR */
946         zfs_replay_create_acl,  /* TX_MKDIR_ACL_ATTR */
947 };