]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_replay.c
MFV r333789: libpcap 1.9.0 (pre-release)
[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         int error;
285
286         txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
287         if (byteswap) {
288                 byteswap_uint64_array(lracl, sizeof (*lracl));
289                 if (txtype == TX_CREATE_ACL_ATTR ||
290                     txtype == TX_MKDIR_ACL_ATTR) {
291                         lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
292                         zfs_replay_swap_attrs(lrattr);
293                         xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
294                 }
295
296                 aclstart = (caddr_t)(lracl + 1) + xvatlen;
297                 zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE);
298                 /* swap fuids */
299                 if (lracl->lr_fuidcnt) {
300                         byteswap_uint64_array((caddr_t)aclstart +
301                             ZIL_ACE_LENGTH(lracl->lr_acl_bytes),
302                             lracl->lr_fuidcnt * sizeof (uint64_t));
303                 }
304         }
305
306         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
307                 return (error);
308
309         xva_init(&xva);
310         zfs_init_vattr(&xva.xva_vattr, AT_TYPE | AT_MODE | AT_UID | AT_GID,
311             lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
312
313         /*
314          * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
315          * eventually end up in zfs_mknode(), which assigns the object's
316          * creation time and generation number.  The generic VOP_CREATE()
317          * doesn't have either concept, so we smuggle the values inside
318          * the vattr's otherwise unused va_ctime and va_nblocks fields.
319          */
320         ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
321         xva.xva_vattr.va_nblocks = lr->lr_gen;
322
323         error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
324         if (error != ENOENT)
325                 goto bail;
326
327         if (lr->lr_common.lrc_txtype & TX_CI)
328                 vflg |= FIGNORECASE;
329         switch (txtype) {
330         case TX_CREATE_ACL:
331                 aclstart = (caddr_t)(lracl + 1);
332                 fuidstart = (caddr_t)aclstart +
333                     ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
334                 zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
335                     (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
336                     lr->lr_uid, lr->lr_gid);
337                 /*FALLTHROUGH*/
338         case TX_CREATE_ACL_ATTR:
339                 if (name == NULL) {
340                         lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
341                         xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
342                         xva.xva_vattr.va_mask |= AT_XVATTR;
343                         zfs_replay_xvattr(lrattr, &xva);
344                 }
345                 vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
346                 vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
347                 vsec.vsa_aclcnt = lracl->lr_aclcnt;
348                 vsec.vsa_aclentsz = lracl->lr_acl_bytes;
349                 vsec.vsa_aclflags = lracl->lr_acl_flags;
350                 if (zfsvfs->z_fuid_replay == NULL) {
351                         fuidstart = (caddr_t)(lracl + 1) + xvatlen +
352                             ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
353                         zfsvfs->z_fuid_replay =
354                             zfs_replay_fuids(fuidstart,
355                             (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
356                             lr->lr_uid, lr->lr_gid);
357                 }
358
359 #ifdef TODO
360                 error = VOP_CREATE(ZTOV(dzp), name, &xva.xva_vattr,
361                     0, 0, &vp, kcred, vflg, NULL, &vsec);
362 #else
363                 panic("%s:%u: unsupported condition", __func__, __LINE__);
364 #endif
365                 break;
366         case TX_MKDIR_ACL:
367                 aclstart = (caddr_t)(lracl + 1);
368                 fuidstart = (caddr_t)aclstart +
369                     ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
370                 zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
371                     (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
372                     lr->lr_uid, lr->lr_gid);
373                 /*FALLTHROUGH*/
374         case TX_MKDIR_ACL_ATTR:
375                 if (name == NULL) {
376                         lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
377                         xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
378                         zfs_replay_xvattr(lrattr, &xva);
379                 }
380                 vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
381                 vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
382                 vsec.vsa_aclcnt = lracl->lr_aclcnt;
383                 vsec.vsa_aclentsz = lracl->lr_acl_bytes;
384                 vsec.vsa_aclflags = lracl->lr_acl_flags;
385                 if (zfsvfs->z_fuid_replay == NULL) {
386                         fuidstart = (caddr_t)(lracl + 1) + xvatlen +
387                             ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
388                         zfsvfs->z_fuid_replay =
389                             zfs_replay_fuids(fuidstart,
390                             (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
391                             lr->lr_uid, lr->lr_gid);
392                 }
393 #ifdef TODO
394                 error = VOP_MKDIR(ZTOV(dzp), name, &xva.xva_vattr,
395                     &vp, kcred, NULL, vflg, &vsec);
396 #else
397                 panic("%s:%u: unsupported condition", __func__, __LINE__);
398 #endif
399                 break;
400         default:
401                 error = SET_ERROR(ENOTSUP);
402         }
403
404 bail:
405         if (error == 0 && vp != NULL)
406                 VN_RELE(vp);
407
408         VN_RELE(ZTOV(dzp));
409
410         if (zfsvfs->z_fuid_replay)
411                 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
412         zfsvfs->z_fuid_replay = NULL;
413
414         return (error);
415 }
416
417 static int
418 zfs_replay_create(void *arg1, void *arg2, boolean_t byteswap)
419 {
420         zfsvfs_t *zfsvfs = arg1;
421         lr_create_t *lr = arg2;
422         char *name = NULL;              /* location determined later */
423         char *link;                     /* symlink content follows name */
424         znode_t *dzp;
425         vnode_t *vp = NULL;
426         xvattr_t xva;
427         int vflg = 0;
428         size_t lrsize = sizeof (lr_create_t);
429         lr_attr_t *lrattr;
430         void *start;
431         size_t xvatlen;
432         uint64_t txtype;
433         struct componentname cn;
434         int error;
435
436         txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
437         if (byteswap) {
438                 byteswap_uint64_array(lr, sizeof (*lr));
439                 if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR)
440                         zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
441         }
442
443
444         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
445                 return (error);
446
447         xva_init(&xva);
448         zfs_init_vattr(&xva.xva_vattr, AT_TYPE | AT_MODE | AT_UID | AT_GID,
449             lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
450
451         /*
452          * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
453          * eventually end up in zfs_mknode(), which assigns the object's
454          * creation time and generation number.  The generic VOP_CREATE()
455          * doesn't have either concept, so we smuggle the values inside
456          * the vattr's otherwise unused va_ctime and va_nblocks fields.
457          */
458         ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
459         xva.xva_vattr.va_nblocks = lr->lr_gen;
460
461         error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
462         if (error != ENOENT)
463                 goto out;
464
465         if (lr->lr_common.lrc_txtype & TX_CI)
466                 vflg |= FIGNORECASE;
467
468         /*
469          * Symlinks don't have fuid info, and CIFS never creates
470          * symlinks.
471          *
472          * The _ATTR versions will grab the fuid info in their subcases.
473          */
474         if ((int)lr->lr_common.lrc_txtype != TX_SYMLINK &&
475             (int)lr->lr_common.lrc_txtype != TX_MKDIR_ATTR &&
476             (int)lr->lr_common.lrc_txtype != TX_CREATE_ATTR) {
477                 start = (lr + 1);
478                 zfsvfs->z_fuid_replay =
479                     zfs_replay_fuid_domain(start, &start,
480                     lr->lr_uid, lr->lr_gid);
481         }
482
483         cn.cn_cred = kcred;
484         cn.cn_thread = curthread;
485         cn.cn_flags = SAVENAME;
486
487         vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
488         switch (txtype) {
489         case TX_CREATE_ATTR:
490                 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
491                 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
492                 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
493                 start = (caddr_t)(lr + 1) + xvatlen;
494                 zfsvfs->z_fuid_replay =
495                     zfs_replay_fuid_domain(start, &start,
496                     lr->lr_uid, lr->lr_gid);
497                 name = (char *)start;
498
499                 /*FALLTHROUGH*/
500         case TX_CREATE:
501                 if (name == NULL)
502                         name = (char *)start;
503
504                 cn.cn_nameptr = name;
505                 error = VOP_CREATE(ZTOV(dzp), &vp, &cn, &xva.xva_vattr /*,vflg*/);
506                 break;
507         case TX_MKDIR_ATTR:
508                 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
509                 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
510                 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
511                 start = (caddr_t)(lr + 1) + xvatlen;
512                 zfsvfs->z_fuid_replay =
513                     zfs_replay_fuid_domain(start, &start,
514                     lr->lr_uid, lr->lr_gid);
515                 name = (char *)start;
516
517                 /*FALLTHROUGH*/
518         case TX_MKDIR:
519                 if (name == NULL)
520                         name = (char *)(lr + 1);
521
522                 cn.cn_nameptr = name;
523                 error = VOP_MKDIR(ZTOV(dzp), &vp, &cn, &xva.xva_vattr /*,vflg*/);
524                 break;
525         case TX_MKXATTR:
526                 error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &vp, kcred);
527                 break;
528         case TX_SYMLINK:
529                 name = (char *)(lr + 1);
530                 link = name + strlen(name) + 1;
531                 cn.cn_nameptr = name;
532                 error = VOP_SYMLINK(ZTOV(dzp), &vp, &cn, &xva.xva_vattr, link /*,vflg*/);
533                 break;
534         default:
535                 error = SET_ERROR(ENOTSUP);
536         }
537         VOP_UNLOCK(ZTOV(dzp), 0);
538
539 out:
540         if (error == 0 && vp != NULL)
541                 VN_URELE(vp);
542
543         VN_RELE(ZTOV(dzp));
544
545         if (zfsvfs->z_fuid_replay)
546                 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
547         zfsvfs->z_fuid_replay = NULL;
548         return (error);
549 }
550
551 static int
552 zfs_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
553 {
554         zfsvfs_t *zfsvfs = arg1;
555         lr_remove_t *lr = arg2;
556         char *name = (char *)(lr + 1);  /* name follows lr_remove_t */
557         znode_t *dzp;
558         struct componentname cn;
559         vnode_t *vp;
560         int error;
561         int vflg = 0;
562
563         if (byteswap)
564                 byteswap_uint64_array(lr, sizeof (*lr));
565
566         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
567                 return (error);
568
569         if (lr->lr_common.lrc_txtype & TX_CI)
570                 vflg |= FIGNORECASE;
571         cn.cn_nameptr = name;
572         cn.cn_namelen = strlen(name);
573         cn.cn_nameiop = DELETE;
574         cn.cn_flags = ISLASTCN | SAVENAME;
575         cn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
576         cn.cn_cred = kcred;
577         cn.cn_thread = curthread;
578         vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
579         error = VOP_LOOKUP(ZTOV(dzp), &vp, &cn);
580         if (error != 0) {
581                 VOP_UNLOCK(ZTOV(dzp), 0);
582                 goto fail;
583         }
584
585         switch ((int)lr->lr_common.lrc_txtype) {
586         case TX_REMOVE:
587                 error = VOP_REMOVE(ZTOV(dzp), vp, &cn /*,vflg*/);
588                 break;
589         case TX_RMDIR:
590                 error = VOP_RMDIR(ZTOV(dzp), vp, &cn /*,vflg*/);
591                 break;
592         default:
593                 error = SET_ERROR(ENOTSUP);
594         }
595         vput(vp);
596         VOP_UNLOCK(ZTOV(dzp), 0);
597
598 fail:
599         VN_RELE(ZTOV(dzp));
600
601         return (error);
602 }
603
604 static int
605 zfs_replay_link(void *arg1, void *arg2, boolean_t byteswap)
606 {
607         zfsvfs_t *zfsvfs = arg1;
608         lr_link_t *lr = arg2;
609         char *name = (char *)(lr + 1);  /* name follows lr_link_t */
610         znode_t *dzp, *zp;
611         struct componentname cn;
612         int error;
613         int vflg = 0;
614
615         if (byteswap)
616                 byteswap_uint64_array(lr, sizeof (*lr));
617
618         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
619                 return (error);
620
621         if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
622                 VN_RELE(ZTOV(dzp));
623                 return (error);
624         }
625
626         if (lr->lr_common.lrc_txtype & TX_CI)
627                 vflg |= FIGNORECASE;
628
629         cn.cn_nameptr = name;
630         cn.cn_cred = kcred;
631         cn.cn_thread = curthread;
632         cn.cn_flags = SAVENAME;
633
634         vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
635         vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
636         error = VOP_LINK(ZTOV(dzp), ZTOV(zp), &cn /*,vflg*/);
637         VOP_UNLOCK(ZTOV(zp), 0);
638         VOP_UNLOCK(ZTOV(dzp), 0);
639
640         VN_RELE(ZTOV(zp));
641         VN_RELE(ZTOV(dzp));
642
643         return (error);
644 }
645
646 static int
647 zfs_replay_rename(void *arg1, void *arg2, boolean_t byteswap)
648 {
649         zfsvfs_t *zfsvfs = arg1;
650         lr_rename_t *lr = arg2;
651         char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */
652         char *tname = sname + strlen(sname) + 1;
653         znode_t *sdzp, *tdzp;
654         struct componentname scn, tcn;
655         vnode_t *svp, *tvp;
656         kthread_t *td = curthread;
657         int error;
658         int vflg = 0;
659
660         if (byteswap)
661                 byteswap_uint64_array(lr, sizeof (*lr));
662
663         if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
664                 return (error);
665
666         if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
667                 VN_RELE(ZTOV(sdzp));
668                 return (error);
669         }
670
671         if (lr->lr_common.lrc_txtype & TX_CI)
672                 vflg |= FIGNORECASE;
673         svp = tvp = NULL;
674
675         scn.cn_nameptr = sname;
676         scn.cn_namelen = strlen(sname);
677         scn.cn_nameiop = DELETE;
678         scn.cn_flags = ISLASTCN | SAVENAME;
679         scn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
680         scn.cn_cred = kcred;
681         scn.cn_thread = td;
682         vn_lock(ZTOV(sdzp), LK_EXCLUSIVE | LK_RETRY);
683         error = VOP_LOOKUP(ZTOV(sdzp), &svp, &scn);
684         VOP_UNLOCK(ZTOV(sdzp), 0);
685         if (error != 0)
686                 goto fail;
687         VOP_UNLOCK(svp, 0);
688
689         tcn.cn_nameptr = tname;
690         tcn.cn_namelen = strlen(tname);
691         tcn.cn_nameiop = RENAME;
692         tcn.cn_flags = ISLASTCN | SAVENAME;
693         tcn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
694         tcn.cn_cred = kcred;
695         tcn.cn_thread = td;
696         vn_lock(ZTOV(tdzp), LK_EXCLUSIVE | LK_RETRY);
697         error = VOP_LOOKUP(ZTOV(tdzp), &tvp, &tcn);
698         if (error == EJUSTRETURN)
699                 tvp = NULL;
700         else if (error != 0) {
701                 VOP_UNLOCK(ZTOV(tdzp), 0);
702                 goto fail;
703         }
704
705         error = VOP_RENAME(ZTOV(sdzp), svp, &scn, ZTOV(tdzp), tvp, &tcn /*,vflg*/);
706         return (error);
707 fail:
708         if (svp != NULL)
709                 vrele(svp);
710         if (tvp != NULL)
711                 vrele(tvp);
712         VN_RELE(ZTOV(tdzp));
713         VN_RELE(ZTOV(sdzp));
714
715         return (error);
716 }
717
718 static int
719 zfs_replay_write(void *arg1, void *arg2, boolean_t byteswap)
720 {
721         zfsvfs_t *zfsvfs = arg1;
722         lr_write_t *lr = arg2;
723         char *data = (char *)(lr + 1);  /* data follows lr_write_t */
724         znode_t *zp;
725         int error;
726         ssize_t resid;
727         uint64_t eod, offset, length;
728
729         if (byteswap)
730                 byteswap_uint64_array(lr, sizeof (*lr));
731
732         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
733                 /*
734                  * As we can log writes out of order, it's possible the
735                  * file has been removed. In this case just drop the write
736                  * and return success.
737                  */
738                 if (error == ENOENT)
739                         error = 0;
740                 return (error);
741         }
742
743         offset = lr->lr_offset;
744         length = lr->lr_length;
745         eod = offset + length;  /* end of data for this write */
746
747         /*
748          * This may be a write from a dmu_sync() for a whole block,
749          * and may extend beyond the current end of the file.
750          * We can't just replay what was written for this TX_WRITE as
751          * a future TX_WRITE2 may extend the eof and the data for that
752          * write needs to be there. So we write the whole block and
753          * reduce the eof. This needs to be done within the single dmu
754          * transaction created within vn_rdwr -> zfs_write. So a possible
755          * new end of file is passed through in zfsvfs->z_replay_eof
756          */
757
758         zfsvfs->z_replay_eof = 0; /* 0 means don't change end of file */
759
760         /* If it's a dmu_sync() block, write the whole block */
761         if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
762                 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
763                 if (length < blocksize) {
764                         offset -= offset % blocksize;
765                         length = blocksize;
766                 }
767                 if (zp->z_size < eod)
768                         zfsvfs->z_replay_eof = eod;
769         }
770
771         error = vn_rdwr(UIO_WRITE, ZTOV(zp), data, length, offset,
772             UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
773
774         VN_RELE(ZTOV(zp));
775         zfsvfs->z_replay_eof = 0;       /* safety */
776
777         return (error);
778 }
779
780 /*
781  * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
782  * meaning the pool block is already being synced. So now that we always write
783  * out full blocks, all we have to do is expand the eof if
784  * the file is grown.
785  */
786 static int
787 zfs_replay_write2(void *arg1, void *arg2, boolean_t byteswap)
788 {
789         zfsvfs_t *zfsvfs = arg1;
790         lr_write_t *lr = arg2;
791         znode_t *zp;
792         int error;
793         uint64_t end;
794
795         if (byteswap)
796                 byteswap_uint64_array(lr, sizeof (*lr));
797
798         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
799                 return (error);
800
801 top:
802         end = lr->lr_offset + lr->lr_length;
803         if (end > zp->z_size) {
804                 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
805
806                 zp->z_size = end;
807                 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
808                 error = dmu_tx_assign(tx, TXG_WAIT);
809                 if (error) {
810                         VN_RELE(ZTOV(zp));
811                         if (error == ERESTART) {
812                                 dmu_tx_wait(tx);
813                                 dmu_tx_abort(tx);
814                                 goto top;
815                         }
816                         dmu_tx_abort(tx);
817                         return (error);
818                 }
819                 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
820                     (void *)&zp->z_size, sizeof (uint64_t), tx);
821
822                 /* Ensure the replayed seq is updated */
823                 (void) zil_replaying(zfsvfs->z_log, tx);
824
825                 dmu_tx_commit(tx);
826         }
827
828         VN_RELE(ZTOV(zp));
829
830         return (error);
831 }
832
833 static int
834 zfs_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
835 {
836 #ifdef illumos
837         zfsvfs_t *zfsvfs = arg1;
838         lr_truncate_t *lr = arg2;
839         znode_t *zp;
840         flock64_t fl;
841         int error;
842
843         if (byteswap)
844                 byteswap_uint64_array(lr, sizeof (*lr));
845
846         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
847                 return (error);
848
849         bzero(&fl, sizeof (fl));
850         fl.l_type = F_WRLCK;
851         fl.l_whence = 0;
852         fl.l_start = lr->lr_offset;
853         fl.l_len = lr->lr_length;
854
855         error = VOP_SPACE(ZTOV(zp), F_FREESP, &fl, FWRITE | FOFFMAX,
856             lr->lr_offset, kcred, NULL);
857
858         VN_RELE(ZTOV(zp));
859
860         return (error);
861 #else
862         ZFS_LOG(0, "Unexpected code path, report to pjd@FreeBSD.org");
863         return (EOPNOTSUPP);
864 #endif
865 }
866
867 static int
868 zfs_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
869 {
870         zfsvfs_t *zfsvfs = arg1;
871         lr_setattr_t *lr = arg2;
872         znode_t *zp;
873         xvattr_t xva;
874         vattr_t *vap = &xva.xva_vattr;
875         vnode_t *vp;
876         int error;
877         void *start;
878
879         xva_init(&xva);
880         if (byteswap) {
881                 byteswap_uint64_array(lr, sizeof (*lr));
882
883                 if ((lr->lr_mask & AT_XVATTR) &&
884                     zfsvfs->z_version >= ZPL_VERSION_INITIAL)
885                         zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
886         }
887
888         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
889                 return (error);
890
891         zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode,
892             lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
893
894         vap->va_size = lr->lr_size;
895         ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime);
896         ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime);
897
898         /*
899          * Fill in xvattr_t portions if necessary.
900          */
901
902         start = (lr_setattr_t *)(lr + 1);
903         if (vap->va_mask & AT_XVATTR) {
904                 zfs_replay_xvattr((lr_attr_t *)start, &xva);
905                 start = (caddr_t)start +
906                     ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize);
907         } else
908                 xva.xva_vattr.va_mask &= ~AT_XVATTR;
909
910         zfsvfs->z_fuid_replay = zfs_replay_fuid_domain(start, &start,
911             lr->lr_uid, lr->lr_gid);
912
913         vp = ZTOV(zp);
914         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
915         error = VOP_SETATTR(vp, vap, kcred);
916         VOP_UNLOCK(vp, 0);
917
918         zfs_fuid_info_free(zfsvfs->z_fuid_replay);
919         zfsvfs->z_fuid_replay = NULL;
920         VN_RELE(vp);
921
922         return (error);
923 }
924
925 extern int zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
926     caller_context_t *ct);
927
928 static int
929 zfs_replay_acl_v0(void *arg1, void *arg2, boolean_t byteswap)
930 {
931         zfsvfs_t *zfsvfs = arg1;
932         lr_acl_v0_t *lr = arg2;
933         ace_t *ace = (ace_t *)(lr + 1); /* ace array follows lr_acl_t */
934         vsecattr_t vsa;
935         vnode_t *vp;
936         znode_t *zp;
937         int error;
938
939         if (byteswap) {
940                 byteswap_uint64_array(lr, sizeof (*lr));
941                 zfs_oldace_byteswap(ace, lr->lr_aclcnt);
942         }
943
944         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
945                 return (error);
946
947         bzero(&vsa, sizeof (vsa));
948         vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
949         vsa.vsa_aclcnt = lr->lr_aclcnt;
950         vsa.vsa_aclentsz = sizeof (ace_t) * vsa.vsa_aclcnt;
951         vsa.vsa_aclflags = 0;
952         vsa.vsa_aclentp = ace;
953
954         vp = ZTOV(zp);
955         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
956         error = zfs_setsecattr(vp, &vsa, 0, kcred, NULL);
957         VOP_UNLOCK(vp, 0);
958
959         VN_RELE(vp);
960
961         return (error);
962 }
963
964 /*
965  * Replaying ACLs is complicated by FUID support.
966  * The log record may contain some optional data
967  * to be used for replaying FUID's.  These pieces
968  * are the actual FUIDs that were created initially.
969  * The FUID table index may no longer be valid and
970  * during zfs_create() a new index may be assigned.
971  * Because of this the log will contain the original
972  * doman+rid in order to create a new FUID.
973  *
974  * The individual ACEs may contain an ephemeral uid/gid which is no
975  * longer valid and will need to be replaced with an actual FUID.
976  *
977  */
978 static int
979 zfs_replay_acl(void *arg1, void *arg2, boolean_t byteswap)
980 {
981         zfsvfs_t *zfsvfs = arg1;
982         lr_acl_t *lr = arg2;
983         ace_t *ace = (ace_t *)(lr + 1);
984         vsecattr_t vsa;
985         znode_t *zp;
986         vnode_t *vp;
987         int error;
988
989         if (byteswap) {
990                 byteswap_uint64_array(lr, sizeof (*lr));
991                 zfs_ace_byteswap(ace, lr->lr_acl_bytes, B_FALSE);
992                 if (lr->lr_fuidcnt) {
993                         byteswap_uint64_array((caddr_t)ace +
994                             ZIL_ACE_LENGTH(lr->lr_acl_bytes),
995                             lr->lr_fuidcnt * sizeof (uint64_t));
996                 }
997         }
998
999         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1000                 return (error);
1001
1002         bzero(&vsa, sizeof (vsa));
1003         vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1004         vsa.vsa_aclcnt = lr->lr_aclcnt;
1005         vsa.vsa_aclentp = ace;
1006         vsa.vsa_aclentsz = lr->lr_acl_bytes;
1007         vsa.vsa_aclflags = lr->lr_acl_flags;
1008
1009         if (lr->lr_fuidcnt) {
1010                 void *fuidstart = (caddr_t)ace +
1011                     ZIL_ACE_LENGTH(lr->lr_acl_bytes);
1012
1013                 zfsvfs->z_fuid_replay =
1014                     zfs_replay_fuids(fuidstart, &fuidstart,
1015                     lr->lr_fuidcnt, lr->lr_domcnt, 0, 0);
1016         }
1017
1018         vp = ZTOV(zp);
1019         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1020         error = zfs_setsecattr(vp, &vsa, 0, kcred, NULL);
1021         VOP_UNLOCK(vp, 0);
1022
1023         if (zfsvfs->z_fuid_replay)
1024                 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
1025
1026         zfsvfs->z_fuid_replay = NULL;
1027         VN_RELE(vp);
1028
1029         return (error);
1030 }
1031
1032 /*
1033  * Callback vectors for replaying records
1034  */
1035 zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
1036         zfs_replay_error,       /* 0 no such transaction type */
1037         zfs_replay_create,      /* TX_CREATE */
1038         zfs_replay_create,      /* TX_MKDIR */
1039         zfs_replay_create,      /* TX_MKXATTR */
1040         zfs_replay_create,      /* TX_SYMLINK */
1041         zfs_replay_remove,      /* TX_REMOVE */
1042         zfs_replay_remove,      /* TX_RMDIR */
1043         zfs_replay_link,        /* TX_LINK */
1044         zfs_replay_rename,      /* TX_RENAME */
1045         zfs_replay_write,       /* TX_WRITE */
1046         zfs_replay_truncate,    /* TX_TRUNCATE */
1047         zfs_replay_setattr,     /* TX_SETATTR */
1048         zfs_replay_acl_v0,      /* TX_ACL_V0 */
1049         zfs_replay_acl,         /* TX_ACL */
1050         zfs_replay_create_acl,  /* TX_CREATE_ACL */
1051         zfs_replay_create,      /* TX_CREATE_ATTR */
1052         zfs_replay_create_acl,  /* TX_CREATE_ACL_ATTR */
1053         zfs_replay_create_acl,  /* TX_MKDIR_ACL */
1054         zfs_replay_create,      /* TX_MKDIR_ATTR */
1055         zfs_replay_create_acl,  /* TX_MKDIR_ACL_ATTR */
1056         zfs_replay_write2,      /* TX_WRITE2 */
1057 };