]> granicus.if.org Git - zfs/blob - module/zfs/zfs_replay.c
OpenZFS 8026 - retire zfs_throttle_delay and zfs_throttle_resolution
[zfs] / module / zfs / zfs_replay.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012 Cyril Plisko. All rights reserved.
24  * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
25  */
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
31 #include <sys/cmn_err.h>
32 #include <sys/kmem.h>
33 #include <sys/thread.h>
34 #include <sys/file.h>
35 #include <sys/fcntl.h>
36 #include <sys/vfs.h>
37 #include <sys/fs/zfs.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/zfs_dir.h>
40 #include <sys/zfs_acl.h>
41 #include <sys/zfs_fuid.h>
42 #include <sys/zfs_vnops.h>
43 #include <sys/spa.h>
44 #include <sys/zil.h>
45 #include <sys/byteorder.h>
46 #include <sys/stat.h>
47 #include <sys/mode.h>
48 #include <sys/acl.h>
49 #include <sys/atomic.h>
50 #include <sys/cred.h>
51 #include <sys/zpl.h>
52
53 /*
54  * Functions to replay ZFS intent log (ZIL) records
55  * The functions are called through a function vector (zfs_replay_vector)
56  * which is indexed by the transaction type.
57  */
58
59 static void
60 zfs_init_vattr(vattr_t *vap, uint64_t mask, uint64_t mode,
61     uint64_t uid, uint64_t gid, uint64_t rdev, uint64_t nodeid)
62 {
63         bzero(vap, sizeof (*vap));
64         vap->va_mask = (uint_t)mask;
65         vap->va_type = IFTOVT(mode);
66         vap->va_mode = mode;
67         vap->va_uid = (uid_t)(IS_EPHEMERAL(uid)) ? -1 : uid;
68         vap->va_gid = (gid_t)(IS_EPHEMERAL(gid)) ? -1 : gid;
69         vap->va_rdev = rdev;
70         vap->va_nodeid = nodeid;
71 }
72
73 /* ARGSUSED */
74 static int
75 zfs_replay_error(zfsvfs_t *zfsvfs, lr_t *lr, 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 |= ATTR_XVATTR;
91         if ((xoap = xva_getxoptattr(xvap)) == NULL) {
92                 xvap->xva_vattr.va_mask &= ~ATTR_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(zfsvfs_t *zfsvfs,
269     lr_acl_create_t *lracl, boolean_t byteswap)
270 {
271         char *name = NULL;              /* location determined later */
272         lr_create_t *lr = (lr_create_t *)lracl;
273         znode_t *dzp;
274         struct inode *ip = NULL;
275         xvattr_t xva;
276         int vflg = 0;
277         vsecattr_t vsec = { 0 };
278         lr_attr_t *lrattr;
279         void *aclstart;
280         void *fuidstart;
281         size_t xvatlen = 0;
282         uint64_t txtype;
283         uint64_t objid;
284         uint64_t dnodesize;
285         int error;
286
287         txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
288         if (byteswap) {
289                 byteswap_uint64_array(lracl, sizeof (*lracl));
290                 if (txtype == TX_CREATE_ACL_ATTR ||
291                     txtype == TX_MKDIR_ACL_ATTR) {
292                         lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
293                         zfs_replay_swap_attrs(lrattr);
294                         xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
295                 }
296
297                 aclstart = (caddr_t)(lracl + 1) + xvatlen;
298                 zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE);
299                 /* swap fuids */
300                 if (lracl->lr_fuidcnt) {
301                         byteswap_uint64_array((caddr_t)aclstart +
302                             ZIL_ACE_LENGTH(lracl->lr_acl_bytes),
303                             lracl->lr_fuidcnt * sizeof (uint64_t));
304                 }
305         }
306
307         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
308                 return (error);
309
310         objid = LR_FOID_GET_OBJ(lr->lr_foid);
311         dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT;
312
313         xva_init(&xva);
314         zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
315             lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid);
316
317         /*
318          * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
319          * eventually end up in zfs_mknode(), which assigns the object's
320          * creation time, generation number, and dnode size. The generic
321          * zfs_create() has no concept of these attributes, so we smuggle
322          * the values inside the vattr's otherwise unused va_ctime,
323          * va_nblocks, and va_fsid fields.
324          */
325         ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
326         xva.xva_vattr.va_nblocks = lr->lr_gen;
327         xva.xva_vattr.va_fsid = dnodesize;
328
329         error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
330         if (error != ENOENT)
331                 goto bail;
332
333         if (lr->lr_common.lrc_txtype & TX_CI)
334                 vflg |= FIGNORECASE;
335         switch (txtype) {
336         case TX_CREATE_ACL:
337                 aclstart = (caddr_t)(lracl + 1);
338                 fuidstart = (caddr_t)aclstart +
339                     ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
340                 zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
341                     (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
342                     lr->lr_uid, lr->lr_gid);
343                 /*FALLTHROUGH*/
344         case TX_CREATE_ACL_ATTR:
345                 if (name == NULL) {
346                         lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
347                         xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
348                         xva.xva_vattr.va_mask |= ATTR_XVATTR;
349                         zfs_replay_xvattr(lrattr, &xva);
350                 }
351                 vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
352                 vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
353                 vsec.vsa_aclcnt = lracl->lr_aclcnt;
354                 vsec.vsa_aclentsz = lracl->lr_acl_bytes;
355                 vsec.vsa_aclflags = lracl->lr_acl_flags;
356                 if (zfsvfs->z_fuid_replay == NULL) {
357                         fuidstart = (caddr_t)(lracl + 1) + xvatlen +
358                             ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
359                         zfsvfs->z_fuid_replay =
360                             zfs_replay_fuids(fuidstart,
361                             (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
362                             lr->lr_uid, lr->lr_gid);
363                 }
364
365                 error = zfs_create(ZTOI(dzp), name, &xva.xva_vattr,
366                     0, 0, &ip, kcred, vflg, &vsec);
367                 break;
368         case TX_MKDIR_ACL:
369                 aclstart = (caddr_t)(lracl + 1);
370                 fuidstart = (caddr_t)aclstart +
371                     ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
372                 zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
373                     (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
374                     lr->lr_uid, lr->lr_gid);
375                 /*FALLTHROUGH*/
376         case TX_MKDIR_ACL_ATTR:
377                 if (name == NULL) {
378                         lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
379                         xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
380                         zfs_replay_xvattr(lrattr, &xva);
381                 }
382                 vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
383                 vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
384                 vsec.vsa_aclcnt = lracl->lr_aclcnt;
385                 vsec.vsa_aclentsz = lracl->lr_acl_bytes;
386                 vsec.vsa_aclflags = lracl->lr_acl_flags;
387                 if (zfsvfs->z_fuid_replay == NULL) {
388                         fuidstart = (caddr_t)(lracl + 1) + xvatlen +
389                             ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
390                         zfsvfs->z_fuid_replay =
391                             zfs_replay_fuids(fuidstart,
392                             (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
393                             lr->lr_uid, lr->lr_gid);
394                 }
395                 error = zfs_mkdir(ZTOI(dzp), name, &xva.xva_vattr,
396                     &ip, kcred, vflg, &vsec);
397                 break;
398         default:
399                 error = SET_ERROR(ENOTSUP);
400         }
401
402 bail:
403         if (error == 0 && ip != NULL)
404                 iput(ip);
405
406         iput(ZTOI(dzp));
407
408         if (zfsvfs->z_fuid_replay)
409                 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
410         zfsvfs->z_fuid_replay = NULL;
411
412         return (error);
413 }
414
415 static int
416 zfs_replay_create(zfsvfs_t *zfsvfs, lr_create_t *lr, boolean_t byteswap)
417 {
418         char *name = NULL;              /* location determined later */
419         char *link;                     /* symlink content follows name */
420         znode_t *dzp;
421         struct inode *ip = NULL;
422         xvattr_t xva;
423         int vflg = 0;
424         size_t lrsize = sizeof (lr_create_t);
425         lr_attr_t *lrattr;
426         void *start;
427         size_t xvatlen;
428         uint64_t txtype;
429         uint64_t objid;
430         uint64_t dnodesize;
431         int error;
432
433         txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
434         if (byteswap) {
435                 byteswap_uint64_array(lr, sizeof (*lr));
436                 if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR)
437                         zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
438         }
439
440
441         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
442                 return (error);
443
444         objid = LR_FOID_GET_OBJ(lr->lr_foid);
445         dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT;
446
447         xva_init(&xva);
448         zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
449             lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid);
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, generation number, and dnode slot count. The
455          * generic zfs_create() has no concept of these attributes, so
456          * we smuggle the values inside * the vattr's otherwise unused
457          * va_ctime, va_nblocks, and va_nlink fields.
458          */
459         ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
460         xva.xva_vattr.va_nblocks = lr->lr_gen;
461         xva.xva_vattr.va_fsid = dnodesize;
462
463         error = dmu_object_info(zfsvfs->z_os, objid, NULL);
464         if (error != ENOENT)
465                 goto out;
466
467         if (lr->lr_common.lrc_txtype & TX_CI)
468                 vflg |= FIGNORECASE;
469
470         /*
471          * Symlinks don't have fuid info, and CIFS never creates
472          * symlinks.
473          *
474          * The _ATTR versions will grab the fuid info in their subcases.
475          */
476         if ((int)lr->lr_common.lrc_txtype != TX_SYMLINK &&
477             (int)lr->lr_common.lrc_txtype != TX_MKDIR_ATTR &&
478             (int)lr->lr_common.lrc_txtype != TX_CREATE_ATTR) {
479                 start = (lr + 1);
480                 zfsvfs->z_fuid_replay =
481                     zfs_replay_fuid_domain(start, &start,
482                     lr->lr_uid, lr->lr_gid);
483         }
484
485         switch (txtype) {
486         case TX_CREATE_ATTR:
487                 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
488                 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
489                 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
490                 start = (caddr_t)(lr + 1) + xvatlen;
491                 zfsvfs->z_fuid_replay =
492                     zfs_replay_fuid_domain(start, &start,
493                     lr->lr_uid, lr->lr_gid);
494                 name = (char *)start;
495
496                 /*FALLTHROUGH*/
497         case TX_CREATE:
498                 if (name == NULL)
499                         name = (char *)start;
500
501                 error = zfs_create(ZTOI(dzp), name, &xva.xva_vattr,
502                     0, 0, &ip, kcred, vflg, NULL);
503                 break;
504         case TX_MKDIR_ATTR:
505                 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
506                 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
507                 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
508                 start = (caddr_t)(lr + 1) + xvatlen;
509                 zfsvfs->z_fuid_replay =
510                     zfs_replay_fuid_domain(start, &start,
511                     lr->lr_uid, lr->lr_gid);
512                 name = (char *)start;
513
514                 /*FALLTHROUGH*/
515         case TX_MKDIR:
516                 if (name == NULL)
517                         name = (char *)(lr + 1);
518
519                 error = zfs_mkdir(ZTOI(dzp), name, &xva.xva_vattr,
520                     &ip, kcred, vflg, NULL);
521                 break;
522         case TX_MKXATTR:
523                 error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &ip, kcred);
524                 break;
525         case TX_SYMLINK:
526                 name = (char *)(lr + 1);
527                 link = name + strlen(name) + 1;
528                 error = zfs_symlink(ZTOI(dzp), name, &xva.xva_vattr,
529                     link, &ip, kcred, vflg);
530                 break;
531         default:
532                 error = SET_ERROR(ENOTSUP);
533         }
534
535 out:
536         if (error == 0 && ip != NULL)
537                 iput(ip);
538
539         iput(ZTOI(dzp));
540
541         if (zfsvfs->z_fuid_replay)
542                 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
543         zfsvfs->z_fuid_replay = NULL;
544         return (error);
545 }
546
547 static int
548 zfs_replay_remove(zfsvfs_t *zfsvfs, lr_remove_t *lr, boolean_t byteswap)
549 {
550         char *name = (char *)(lr + 1);  /* name follows lr_remove_t */
551         znode_t *dzp;
552         int error;
553         int vflg = 0;
554
555         if (byteswap)
556                 byteswap_uint64_array(lr, sizeof (*lr));
557
558         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
559                 return (error);
560
561         if (lr->lr_common.lrc_txtype & TX_CI)
562                 vflg |= FIGNORECASE;
563
564         switch ((int)lr->lr_common.lrc_txtype) {
565         case TX_REMOVE:
566                 error = zfs_remove(ZTOI(dzp), name, kcred, vflg);
567                 break;
568         case TX_RMDIR:
569                 error = zfs_rmdir(ZTOI(dzp), name, NULL, kcred, vflg);
570                 break;
571         default:
572                 error = SET_ERROR(ENOTSUP);
573         }
574
575         iput(ZTOI(dzp));
576
577         return (error);
578 }
579
580 static int
581 zfs_replay_link(zfsvfs_t *zfsvfs, lr_link_t *lr, boolean_t byteswap)
582 {
583         char *name = (char *)(lr + 1);  /* name follows lr_link_t */
584         znode_t *dzp, *zp;
585         int error;
586         int vflg = 0;
587
588         if (byteswap)
589                 byteswap_uint64_array(lr, sizeof (*lr));
590
591         if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
592                 return (error);
593
594         if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
595                 iput(ZTOI(dzp));
596                 return (error);
597         }
598
599         if (lr->lr_common.lrc_txtype & TX_CI)
600                 vflg |= FIGNORECASE;
601
602         error = zfs_link(ZTOI(dzp), ZTOI(zp), name, kcred, vflg);
603
604         iput(ZTOI(zp));
605         iput(ZTOI(dzp));
606
607         return (error);
608 }
609
610 static int
611 zfs_replay_rename(zfsvfs_t *zfsvfs, lr_rename_t *lr, boolean_t byteswap)
612 {
613         char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */
614         char *tname = sname + strlen(sname) + 1;
615         znode_t *sdzp, *tdzp;
616         int error;
617         int vflg = 0;
618
619         if (byteswap)
620                 byteswap_uint64_array(lr, sizeof (*lr));
621
622         if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
623                 return (error);
624
625         if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
626                 iput(ZTOI(sdzp));
627                 return (error);
628         }
629
630         if (lr->lr_common.lrc_txtype & TX_CI)
631                 vflg |= FIGNORECASE;
632
633         error = zfs_rename(ZTOI(sdzp), sname, ZTOI(tdzp), tname, kcred, vflg);
634
635         iput(ZTOI(tdzp));
636         iput(ZTOI(sdzp));
637
638         return (error);
639 }
640
641 static int
642 zfs_replay_write(zfsvfs_t *zfsvfs, lr_write_t *lr, boolean_t byteswap)
643 {
644         char *data = (char *)(lr + 1);  /* data follows lr_write_t */
645         znode_t *zp;
646         int error, written;
647         uint64_t eod, offset, length;
648
649         if (byteswap)
650                 byteswap_uint64_array(lr, sizeof (*lr));
651
652         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
653                 /*
654                  * As we can log writes out of order, it's possible the
655                  * file has been removed. In this case just drop the write
656                  * and return success.
657                  */
658                 if (error == ENOENT)
659                         error = 0;
660                 return (error);
661         }
662
663         offset = lr->lr_offset;
664         length = lr->lr_length;
665         eod = offset + length;  /* end of data for this write */
666
667         /*
668          * This may be a write from a dmu_sync() for a whole block,
669          * and may extend beyond the current end of the file.
670          * We can't just replay what was written for this TX_WRITE as
671          * a future TX_WRITE2 may extend the eof and the data for that
672          * write needs to be there. So we write the whole block and
673          * reduce the eof. This needs to be done within the single dmu
674          * transaction created within vn_rdwr -> zfs_write. So a possible
675          * new end of file is passed through in zfsvfs->z_replay_eof
676          */
677
678         zfsvfs->z_replay_eof = 0; /* 0 means don't change end of file */
679
680         /* If it's a dmu_sync() block, write the whole block */
681         if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
682                 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
683                 if (length < blocksize) {
684                         offset -= offset % blocksize;
685                         length = blocksize;
686                 }
687                 if (zp->z_size < eod)
688                         zfsvfs->z_replay_eof = eod;
689         }
690
691         written = zpl_write_common(ZTOI(zp), data, length, &offset,
692             UIO_SYSSPACE, 0, kcred);
693         if (written < 0)
694                 error = -written;
695         else if (written < length)
696                 error = SET_ERROR(EIO); /* short write */
697
698         iput(ZTOI(zp));
699         zfsvfs->z_replay_eof = 0;       /* safety */
700
701         return (error);
702 }
703
704 /*
705  * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
706  * meaning the pool block is already being synced. So now that we always write
707  * out full blocks, all we have to do is expand the eof if
708  * the file is grown.
709  */
710 static int
711 zfs_replay_write2(zfsvfs_t *zfsvfs, lr_write_t *lr, boolean_t byteswap)
712 {
713         znode_t *zp;
714         int error;
715         uint64_t end;
716
717         if (byteswap)
718                 byteswap_uint64_array(lr, sizeof (*lr));
719
720         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
721                 return (error);
722
723 top:
724         end = lr->lr_offset + lr->lr_length;
725         if (end > zp->z_size) {
726                 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
727
728                 zp->z_size = end;
729                 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
730                 error = dmu_tx_assign(tx, TXG_WAIT);
731                 if (error) {
732                         iput(ZTOI(zp));
733                         if (error == ERESTART) {
734                                 dmu_tx_wait(tx);
735                                 dmu_tx_abort(tx);
736                                 goto top;
737                         }
738                         dmu_tx_abort(tx);
739                         return (error);
740                 }
741                 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
742                     (void *)&zp->z_size, sizeof (uint64_t), tx);
743
744                 /* Ensure the replayed seq is updated */
745                 (void) zil_replaying(zfsvfs->z_log, tx);
746
747                 dmu_tx_commit(tx);
748         }
749
750         iput(ZTOI(zp));
751
752         return (error);
753 }
754
755 static int
756 zfs_replay_truncate(zfsvfs_t *zfsvfs, lr_truncate_t *lr, boolean_t byteswap)
757 {
758         znode_t *zp;
759         flock64_t fl;
760         int error;
761
762         if (byteswap)
763                 byteswap_uint64_array(lr, sizeof (*lr));
764
765         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
766                 return (error);
767
768         bzero(&fl, sizeof (fl));
769         fl.l_type = F_WRLCK;
770         fl.l_whence = 0;
771         fl.l_start = lr->lr_offset;
772         fl.l_len = lr->lr_length;
773
774         error = zfs_space(ZTOI(zp), F_FREESP, &fl, FWRITE | FOFFMAX,
775             lr->lr_offset, kcred);
776
777         iput(ZTOI(zp));
778
779         return (error);
780 }
781
782 static int
783 zfs_replay_setattr(zfsvfs_t *zfsvfs, lr_setattr_t *lr, boolean_t byteswap)
784 {
785         znode_t *zp;
786         xvattr_t xva;
787         vattr_t *vap = &xva.xva_vattr;
788         int error;
789         void *start;
790
791         xva_init(&xva);
792         if (byteswap) {
793                 byteswap_uint64_array(lr, sizeof (*lr));
794
795                 if ((lr->lr_mask & ATTR_XVATTR) &&
796                     zfsvfs->z_version >= ZPL_VERSION_INITIAL)
797                         zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
798         }
799
800         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
801                 return (error);
802
803         zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode,
804             lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
805
806         vap->va_size = lr->lr_size;
807         ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime);
808         ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime);
809         gethrestime(&vap->va_ctime);
810         vap->va_mask |= ATTR_CTIME;
811
812         /*
813          * Fill in xvattr_t portions if necessary.
814          */
815
816         start = (lr_setattr_t *)(lr + 1);
817         if (vap->va_mask & ATTR_XVATTR) {
818                 zfs_replay_xvattr((lr_attr_t *)start, &xva);
819                 start = (caddr_t)start +
820                     ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize);
821         } else
822                 xva.xva_vattr.va_mask &= ~ATTR_XVATTR;
823
824         zfsvfs->z_fuid_replay = zfs_replay_fuid_domain(start, &start,
825             lr->lr_uid, lr->lr_gid);
826
827         error = zfs_setattr(ZTOI(zp), vap, 0, kcred);
828
829         zfs_fuid_info_free(zfsvfs->z_fuid_replay);
830         zfsvfs->z_fuid_replay = NULL;
831         iput(ZTOI(zp));
832
833         return (error);
834 }
835
836 static int
837 zfs_replay_acl_v0(zfsvfs_t *zfsvfs, lr_acl_v0_t *lr, boolean_t byteswap)
838 {
839         ace_t *ace = (ace_t *)(lr + 1); /* ace array follows lr_acl_t */
840         vsecattr_t vsa;
841         znode_t *zp;
842         int error;
843
844         if (byteswap) {
845                 byteswap_uint64_array(lr, sizeof (*lr));
846                 zfs_oldace_byteswap(ace, lr->lr_aclcnt);
847         }
848
849         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
850                 return (error);
851
852         bzero(&vsa, sizeof (vsa));
853         vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
854         vsa.vsa_aclcnt = lr->lr_aclcnt;
855         vsa.vsa_aclentsz = sizeof (ace_t) * vsa.vsa_aclcnt;
856         vsa.vsa_aclflags = 0;
857         vsa.vsa_aclentp = ace;
858
859         error = zfs_setsecattr(ZTOI(zp), &vsa, 0, kcred);
860
861         iput(ZTOI(zp));
862
863         return (error);
864 }
865
866 /*
867  * Replaying ACLs is complicated by FUID support.
868  * The log record may contain some optional data
869  * to be used for replaying FUID's.  These pieces
870  * are the actual FUIDs that were created initially.
871  * The FUID table index may no longer be valid and
872  * during zfs_create() a new index may be assigned.
873  * Because of this the log will contain the original
874  * domain+rid in order to create a new FUID.
875  *
876  * The individual ACEs may contain an ephemeral uid/gid which is no
877  * longer valid and will need to be replaced with an actual FUID.
878  *
879  */
880 static int
881 zfs_replay_acl(zfsvfs_t *zfsvfs, lr_acl_t *lr, boolean_t byteswap)
882 {
883         ace_t *ace = (ace_t *)(lr + 1);
884         vsecattr_t vsa;
885         znode_t *zp;
886         int error;
887
888         if (byteswap) {
889                 byteswap_uint64_array(lr, sizeof (*lr));
890                 zfs_ace_byteswap(ace, lr->lr_acl_bytes, B_FALSE);
891                 if (lr->lr_fuidcnt) {
892                         byteswap_uint64_array((caddr_t)ace +
893                             ZIL_ACE_LENGTH(lr->lr_acl_bytes),
894                             lr->lr_fuidcnt * sizeof (uint64_t));
895                 }
896         }
897
898         if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
899                 return (error);
900
901         bzero(&vsa, sizeof (vsa));
902         vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
903         vsa.vsa_aclcnt = lr->lr_aclcnt;
904         vsa.vsa_aclentp = ace;
905         vsa.vsa_aclentsz = lr->lr_acl_bytes;
906         vsa.vsa_aclflags = lr->lr_acl_flags;
907
908         if (lr->lr_fuidcnt) {
909                 void *fuidstart = (caddr_t)ace +
910                     ZIL_ACE_LENGTH(lr->lr_acl_bytes);
911
912                 zfsvfs->z_fuid_replay =
913                     zfs_replay_fuids(fuidstart, &fuidstart,
914                     lr->lr_fuidcnt, lr->lr_domcnt, 0, 0);
915         }
916
917         error = zfs_setsecattr(ZTOI(zp), &vsa, 0, kcred);
918
919         if (zfsvfs->z_fuid_replay)
920                 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
921
922         zfsvfs->z_fuid_replay = NULL;
923         iput(ZTOI(zp));
924
925         return (error);
926 }
927
928 /*
929  * Callback vectors for replaying records
930  */
931 zil_replay_func_t zfs_replay_vector[TX_MAX_TYPE] = {
932         (zil_replay_func_t)zfs_replay_error,            /* no such type */
933         (zil_replay_func_t)zfs_replay_create,           /* TX_CREATE */
934         (zil_replay_func_t)zfs_replay_create,           /* TX_MKDIR */
935         (zil_replay_func_t)zfs_replay_create,           /* TX_MKXATTR */
936         (zil_replay_func_t)zfs_replay_create,           /* TX_SYMLINK */
937         (zil_replay_func_t)zfs_replay_remove,           /* TX_REMOVE */
938         (zil_replay_func_t)zfs_replay_remove,           /* TX_RMDIR */
939         (zil_replay_func_t)zfs_replay_link,             /* TX_LINK */
940         (zil_replay_func_t)zfs_replay_rename,           /* TX_RENAME */
941         (zil_replay_func_t)zfs_replay_write,            /* TX_WRITE */
942         (zil_replay_func_t)zfs_replay_truncate,         /* TX_TRUNCATE */
943         (zil_replay_func_t)zfs_replay_setattr,          /* TX_SETATTR */
944         (zil_replay_func_t)zfs_replay_acl_v0,           /* TX_ACL_V0 */
945         (zil_replay_func_t)zfs_replay_acl,              /* TX_ACL */
946         (zil_replay_func_t)zfs_replay_create_acl,       /* TX_CREATE_ACL */
947         (zil_replay_func_t)zfs_replay_create,           /* TX_CREATE_ATTR */
948         (zil_replay_func_t)zfs_replay_create_acl,       /* TX_CREATE_ACL_ATTR */
949         (zil_replay_func_t)zfs_replay_create_acl,       /* TX_MKDIR_ACL */
950         (zil_replay_func_t)zfs_replay_create,           /* TX_MKDIR_ATTR */
951         (zil_replay_func_t)zfs_replay_create_acl,       /* TX_MKDIR_ACL_ATTR */
952         (zil_replay_func_t)zfs_replay_write2,           /* TX_WRITE2 */
953 };