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