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 https://opensource.org/licenses/CDDL-1.0.
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) 2015, 2018 by Delphix. All rights reserved.
24 * Copyright (c) 2022 by Pawel Jakub Dawidek
25 */
26
27
28 #include <sys/types.h>
29 #include <sys/param.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/vfs.h>
36 #include <sys/zfs_znode.h>
37 #include <sys/zfs_dir.h>
38 #include <sys/zil.h>
39 #include <sys/zil_impl.h>
40 #include <sys/byteorder.h>
41 #include <sys/policy.h>
42 #include <sys/stat.h>
43 #include <sys/acl.h>
44 #include <sys/dmu.h>
45 #include <sys/dbuf.h>
46 #include <sys/spa.h>
47 #include <sys/zfs_fuid.h>
48 #include <sys/dsl_dataset.h>
49
50 /*
51 * These zfs_log_* functions must be called within a dmu tx, in one
52 * of 2 contexts depending on zilog->z_replay:
53 *
54 * Non replay mode
55 * ---------------
56 * We need to record the transaction so that if it is committed to
57 * the Intent Log then it can be replayed. An intent log transaction
58 * structure (itx_t) is allocated and all the information necessary to
59 * possibly replay the transaction is saved in it. The itx is then assigned
60 * a sequence number and inserted in the in-memory list anchored in the zilog.
61 *
62 * Replay mode
63 * -----------
64 * We need to mark the intent log record as replayed in the log header.
65 * This is done in the same transaction as the replay so that they
66 * commit atomically.
67 */
68
69 int
zfs_log_create_txtype(zil_create_t type,vsecattr_t * vsecp,vattr_t * vap)70 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
71 {
72 int isxvattr = (vap->va_mask & ATTR_XVATTR);
73 switch (type) {
74 case Z_FILE:
75 if (vsecp == NULL && !isxvattr)
76 return (TX_CREATE);
77 if (vsecp && isxvattr)
78 return (TX_CREATE_ACL_ATTR);
79 if (vsecp)
80 return (TX_CREATE_ACL);
81 else
82 return (TX_CREATE_ATTR);
83 case Z_DIR:
84 if (vsecp == NULL && !isxvattr)
85 return (TX_MKDIR);
86 if (vsecp && isxvattr)
87 return (TX_MKDIR_ACL_ATTR);
88 if (vsecp)
89 return (TX_MKDIR_ACL);
90 else
91 return (TX_MKDIR_ATTR);
92 case Z_XATTRDIR:
93 return (TX_MKXATTR);
94 }
95 ASSERT(0);
96 return (TX_MAX_TYPE);
97 }
98
99 /*
100 * build up the log data necessary for logging xvattr_t
101 * First lr_attr_t is initialized. following the lr_attr_t
102 * is the mapsize and attribute bitmap copied from the xvattr_t.
103 * Following the bitmap and bitmapsize two 64 bit words are reserved
104 * for the create time which may be set. Following the create time
105 * records a single 64 bit integer which has the bits to set on
106 * replay for the xvattr.
107 */
108 static void
zfs_log_xvattr(lr_attr_t * lrattr,xvattr_t * xvap)109 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
110 {
111 xoptattr_t *xoap;
112
113 xoap = xva_getxoptattr(xvap);
114 ASSERT(xoap);
115
116 lrattr->lr_attr_masksize = xvap->xva_mapsize;
117 uint32_t *bitmap = &lrattr->lr_attr_bitmap;
118 for (int i = 0; i != xvap->xva_mapsize; i++, bitmap++)
119 *bitmap = xvap->xva_reqattrmap[i];
120
121 lr_attr_end_t *end = (lr_attr_end_t *)bitmap;
122 end->lr_attr_attrs = 0;
123 end->lr_attr_crtime[0] = 0;
124 end->lr_attr_crtime[1] = 0;
125 memset(end->lr_attr_scanstamp, 0, AV_SCANSTAMP_SZ);
126
127 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
128 end->lr_attr_attrs |= (xoap->xoa_readonly == 0) ? 0 :
129 XAT0_READONLY;
130 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
131 end->lr_attr_attrs |= (xoap->xoa_hidden == 0) ? 0 :
132 XAT0_HIDDEN;
133 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
134 end->lr_attr_attrs |= (xoap->xoa_system == 0) ? 0 :
135 XAT0_SYSTEM;
136 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
137 end->lr_attr_attrs |= (xoap->xoa_archive == 0) ? 0 :
138 XAT0_ARCHIVE;
139 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
140 end->lr_attr_attrs |= (xoap->xoa_immutable == 0) ? 0 :
141 XAT0_IMMUTABLE;
142 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
143 end->lr_attr_attrs |= (xoap->xoa_nounlink == 0) ? 0 :
144 XAT0_NOUNLINK;
145 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
146 end->lr_attr_attrs |= (xoap->xoa_appendonly == 0) ? 0 :
147 XAT0_APPENDONLY;
148 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
149 end->lr_attr_attrs |= (xoap->xoa_opaque == 0) ? 0 :
150 XAT0_APPENDONLY;
151 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
152 end->lr_attr_attrs |= (xoap->xoa_nodump == 0) ? 0 :
153 XAT0_NODUMP;
154 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
155 end->lr_attr_attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
156 XAT0_AV_QUARANTINED;
157 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
158 end->lr_attr_attrs |= (xoap->xoa_av_modified == 0) ? 0 :
159 XAT0_AV_MODIFIED;
160 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
161 ZFS_TIME_ENCODE(&xoap->xoa_createtime, end->lr_attr_crtime);
162 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
163 ASSERT(!XVA_ISSET_REQ(xvap, XAT_PROJID));
164
165 memcpy(end->lr_attr_scanstamp, xoap->xoa_av_scanstamp,
166 AV_SCANSTAMP_SZ);
167 } else if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
168 /*
169 * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid
170 * at the same time, so we can share the same space.
171 */
172 memcpy(end->lr_attr_scanstamp, &xoap->xoa_projid,
173 sizeof (uint64_t));
174 }
175 if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
176 end->lr_attr_attrs |= (xoap->xoa_reparse == 0) ? 0 :
177 XAT0_REPARSE;
178 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
179 end->lr_attr_attrs |= (xoap->xoa_offline == 0) ? 0 :
180 XAT0_OFFLINE;
181 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
182 end->lr_attr_attrs |= (xoap->xoa_sparse == 0) ? 0 :
183 XAT0_SPARSE;
184 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT))
185 end->lr_attr_attrs |= (xoap->xoa_projinherit == 0) ? 0 :
186 XAT0_PROJINHERIT;
187 }
188
189 static void *
zfs_log_fuid_ids(zfs_fuid_info_t * fuidp,void * start)190 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
191 {
192 zfs_fuid_t *zfuid;
193 uint64_t *fuidloc = start;
194
195 /* First copy in the ACE FUIDs */
196 for (zfuid = list_head(&fuidp->z_fuids); zfuid;
197 zfuid = list_next(&fuidp->z_fuids, zfuid)) {
198 *fuidloc++ = zfuid->z_logfuid;
199 }
200 return (fuidloc);
201 }
202
203
204 static void *
zfs_log_fuid_domains(zfs_fuid_info_t * fuidp,void * start)205 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
206 {
207 zfs_fuid_domain_t *zdomain;
208
209 /* now copy in the domain info, if any */
210 if (fuidp->z_domain_str_sz != 0) {
211 for (zdomain = list_head(&fuidp->z_domains); zdomain;
212 zdomain = list_next(&fuidp->z_domains, zdomain)) {
213 memcpy(start, zdomain->z_domain,
214 strlen(zdomain->z_domain) + 1);
215 start = (caddr_t)start +
216 strlen(zdomain->z_domain) + 1;
217 }
218 }
219 return (start);
220 }
221
222 /*
223 * If zp is an xattr node, check whether the xattr owner is unlinked.
224 * We don't want to log anything if the owner is unlinked.
225 */
226 static int
zfs_xattr_owner_unlinked(znode_t * zp)227 zfs_xattr_owner_unlinked(znode_t *zp)
228 {
229 int unlinked = 0;
230 znode_t *dzp;
231 #ifdef __FreeBSD__
232 znode_t *tzp = zp;
233
234 /*
235 * zrele drops the vnode lock which violates the VOP locking contract
236 * on FreeBSD. See comment at the top of zfs_replay.c for more detail.
237 */
238 /*
239 * if zp is XATTR node, keep walking up via z_xattr_parent until we
240 * get the owner
241 */
242 while (tzp->z_pflags & ZFS_XATTR) {
243 ASSERT3U(zp->z_xattr_parent, !=, 0);
244 if (zfs_zget(ZTOZSB(tzp), tzp->z_xattr_parent, &dzp) != 0) {
245 unlinked = 1;
246 break;
247 }
248
249 if (tzp != zp)
250 zrele(tzp);
251 tzp = dzp;
252 unlinked = tzp->z_unlinked;
253 }
254 if (tzp != zp)
255 zrele(tzp);
256 #else
257 zhold(zp);
258 /*
259 * if zp is XATTR node, keep walking up via z_xattr_parent until we
260 * get the owner
261 */
262 while (zp->z_pflags & ZFS_XATTR) {
263 ASSERT3U(zp->z_xattr_parent, !=, 0);
264 if (zfs_zget(ZTOZSB(zp), zp->z_xattr_parent, &dzp) != 0) {
265 unlinked = 1;
266 break;
267 }
268
269 zrele(zp);
270 zp = dzp;
271 unlinked = zp->z_unlinked;
272 }
273 zrele(zp);
274 #endif
275 return (unlinked);
276 }
277
278 /*
279 * Handles TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, TX_MKDIR_ATTR and
280 * TK_MKXATTR transactions.
281 *
282 * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
283 * domain information appended prior to the name. In this case the
284 * uid/gid in the log record will be a log centric FUID.
285 *
286 * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
287 * may contain attributes, ACL and optional fuid information.
288 *
289 * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
290 * and ACL and normal users/groups in the ACEs.
291 *
292 * There may be an optional xvattr attribute information similar
293 * to zfs_log_setattr.
294 *
295 * Also, after the file name "domain" strings may be appended.
296 */
297 void
zfs_log_create(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,znode_t * zp,const char * name,vsecattr_t * vsecp,zfs_fuid_info_t * fuidp,vattr_t * vap)298 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
299 znode_t *dzp, znode_t *zp, const char *name, vsecattr_t *vsecp,
300 zfs_fuid_info_t *fuidp, vattr_t *vap)
301 {
302 itx_t *itx;
303 _lr_create_t *lr;
304 lr_acl_create_t *lracl = NULL;
305 uint8_t *lrdata;
306 size_t aclsize = 0;
307 size_t xvatsize = 0;
308 size_t txsize;
309 xvattr_t *xvap = (xvattr_t *)vap;
310 size_t namesize = strlen(name) + 1;
311 size_t fuidsz = 0;
312
313 if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
314 return;
315
316 /*
317 * If we have FUIDs present then add in space for
318 * domains and ACE fuid's if any.
319 */
320 if (fuidp) {
321 fuidsz += fuidp->z_domain_str_sz;
322 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
323 }
324
325 if (vap->va_mask & ATTR_XVATTR)
326 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
327
328 if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
329 (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
330 (int)txtype == TX_MKXATTR) {
331 txsize = sizeof (lr_create_t) + namesize + fuidsz + xvatsize;
332 itx = zil_itx_create(txtype, txsize);
333 lr_create_t *lrc = (lr_create_t *)&itx->itx_lr;
334 lrdata = &lrc->lr_data[0];
335 } else {
336 txsize =
337 sizeof (lr_acl_create_t) + namesize + fuidsz +
338 ZIL_ACE_LENGTH(aclsize) + xvatsize;
339 itx = zil_itx_create(txtype, txsize);
340 lracl = (lr_acl_create_t *)&itx->itx_lr;
341 lrdata = &lracl->lr_data[0];
342 }
343
344
345 lr = (_lr_create_t *)&itx->itx_lr;
346 lr->lr_doid = dzp->z_id;
347 lr->lr_foid = zp->z_id;
348 /* Store dnode slot count in 8 bits above object id. */
349 LR_FOID_SET_SLOTS(lr->lr_foid, zp->z_dnodesize >> DNODE_SHIFT);
350 lr->lr_mode = zp->z_mode;
351 if (!IS_EPHEMERAL(KUID_TO_SUID(ZTOUID(zp)))) {
352 lr->lr_uid = (uint64_t)KUID_TO_SUID(ZTOUID(zp));
353 } else {
354 lr->lr_uid = fuidp->z_fuid_owner;
355 }
356 if (!IS_EPHEMERAL(KGID_TO_SGID(ZTOGID(zp)))) {
357 lr->lr_gid = (uint64_t)KGID_TO_SGID(ZTOGID(zp));
358 } else {
359 lr->lr_gid = fuidp->z_fuid_group;
360 }
361 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
362 sizeof (uint64_t));
363 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
364 lr->lr_crtime, sizeof (uint64_t) * 2);
365
366 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(ZTOZSB(zp)), &lr->lr_rdev,
367 sizeof (lr->lr_rdev)) != 0)
368 lr->lr_rdev = 0;
369
370 /*
371 * Fill in xvattr info if any
372 */
373 if (vap->va_mask & ATTR_XVATTR) {
374 zfs_log_xvattr((lr_attr_t *)lrdata, xvap);
375 lrdata = &lrdata[xvatsize];
376 }
377
378 /* Now fill in any ACL info */
379
380 if (vsecp) {
381 ASSERT3P(lracl, !=, NULL);
382 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
383 lracl->lr_acl_bytes = aclsize;
384 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
385 lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
386 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
387 lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
388 else
389 lracl->lr_acl_flags = 0;
390
391 memcpy(lrdata, vsecp->vsa_aclentp, aclsize);
392 lrdata = &lrdata[ZIL_ACE_LENGTH(aclsize)];
393 }
394
395 /* drop in FUID info */
396 if (fuidp) {
397 lrdata = zfs_log_fuid_ids(fuidp, lrdata);
398 lrdata = zfs_log_fuid_domains(fuidp, lrdata);
399 }
400 /*
401 * Now place file name in log record
402 */
403 memcpy(lrdata, name, namesize);
404
405 zil_itx_assign(zilog, itx, tx);
406 }
407
408 /*
409 * Handles both TX_REMOVE and TX_RMDIR transactions.
410 */
411 void
zfs_log_remove(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,const char * name,uint64_t foid,boolean_t unlinked)412 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
413 znode_t *dzp, const char *name, uint64_t foid, boolean_t unlinked)
414 {
415 itx_t *itx;
416 lr_remove_t *lr;
417 size_t namesize = strlen(name) + 1;
418
419 if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
420 return;
421
422 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
423 lr = (lr_remove_t *)&itx->itx_lr;
424 lr->lr_doid = dzp->z_id;
425 memcpy(&lr->lr_data[0], name, namesize);
426
427 itx->itx_oid = foid;
428
429 /*
430 * Object ids can be re-instantiated in the next txg so
431 * remove any async transactions to avoid future leaks.
432 * This can happen if a fsync occurs on the re-instantiated
433 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
434 * the new file data and flushes a write record for the old object.
435 */
436 if (unlinked) {
437 ASSERT((txtype & ~TX_CI) == TX_REMOVE);
438 zil_remove_async(zilog, foid);
439 }
440 zil_itx_assign(zilog, itx, tx);
441 }
442
443 /*
444 * Handles TX_LINK transactions.
445 */
446 void
zfs_log_link(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,znode_t * zp,const char * name)447 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
448 znode_t *dzp, znode_t *zp, const char *name)
449 {
450 itx_t *itx;
451 lr_link_t *lr;
452 size_t namesize = strlen(name) + 1;
453
454 if (zil_replaying(zilog, tx))
455 return;
456
457 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
458 lr = (lr_link_t *)&itx->itx_lr;
459 lr->lr_doid = dzp->z_id;
460 lr->lr_link_obj = zp->z_id;
461 memcpy(&lr->lr_data[0], name, namesize);
462
463 zil_itx_assign(zilog, itx, tx);
464 }
465
466 /*
467 * Handles TX_SYMLINK transactions.
468 */
469 void
zfs_log_symlink(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,znode_t * zp,const char * name,const char * link)470 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
471 znode_t *dzp, znode_t *zp, const char *name, const char *link)
472 {
473 itx_t *itx;
474 _lr_create_t *lr;
475 lr_create_t *lrc;
476 size_t namesize = strlen(name) + 1;
477 size_t linksize = strlen(link) + 1;
478
479 if (zil_replaying(zilog, tx))
480 return;
481
482 itx = zil_itx_create(txtype, sizeof (*lrc) + namesize + linksize);
483 lrc = (lr_create_t *)&itx->itx_lr;
484 lr = &lrc->lr_create;
485 lr->lr_doid = dzp->z_id;
486 lr->lr_foid = zp->z_id;
487 lr->lr_uid = KUID_TO_SUID(ZTOUID(zp));
488 lr->lr_gid = KGID_TO_SGID(ZTOGID(zp));
489 lr->lr_mode = zp->z_mode;
490 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
491 sizeof (uint64_t));
492 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
493 lr->lr_crtime, sizeof (uint64_t) * 2);
494 memcpy(&lrc->lr_data[0], name, namesize);
495 memcpy(&lrc->lr_data[namesize], link, linksize);
496
497 zil_itx_assign(zilog, itx, tx);
498 }
499
500 static void
do_zfs_log_rename(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * sdzp,const char * sname,znode_t * tdzp,const char * dname,znode_t * szp)501 do_zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, znode_t *sdzp,
502 const char *sname, znode_t *tdzp, const char *dname, znode_t *szp)
503 {
504 itx_t *itx;
505 _lr_rename_t *lr;
506 lr_rename_t *lrr;
507 size_t snamesize = strlen(sname) + 1;
508 size_t dnamesize = strlen(dname) + 1;
509
510 if (zil_replaying(zilog, tx))
511 return;
512
513 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
514 lrr = (lr_rename_t *)&itx->itx_lr;
515 lr = &lrr->lr_rename;
516 lr->lr_sdoid = sdzp->z_id;
517 lr->lr_tdoid = tdzp->z_id;
518 memcpy(&lrr->lr_data[0], sname, snamesize);
519 memcpy(&lrr->lr_data[snamesize], dname, dnamesize);
520 itx->itx_oid = szp->z_id;
521
522 zil_itx_assign(zilog, itx, tx);
523 }
524
525 /*
526 * Handles TX_RENAME transactions.
527 */
528 void
zfs_log_rename(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * sdzp,const char * sname,znode_t * tdzp,const char * dname,znode_t * szp)529 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, znode_t *sdzp,
530 const char *sname, znode_t *tdzp, const char *dname, znode_t *szp)
531 {
532 txtype |= TX_RENAME;
533 do_zfs_log_rename(zilog, tx, txtype, sdzp, sname, tdzp, dname, szp);
534 }
535
536 /*
537 * Handles TX_RENAME_EXCHANGE transactions.
538 */
539 void
zfs_log_rename_exchange(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * sdzp,const char * sname,znode_t * tdzp,const char * dname,znode_t * szp)540 zfs_log_rename_exchange(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
541 znode_t *sdzp, const char *sname, znode_t *tdzp, const char *dname,
542 znode_t *szp)
543 {
544 txtype |= TX_RENAME_EXCHANGE;
545 do_zfs_log_rename(zilog, tx, txtype, sdzp, sname, tdzp, dname, szp);
546 }
547
548 /*
549 * Handles TX_RENAME_WHITEOUT transactions.
550 *
551 * Unfortunately we cannot reuse do_zfs_log_rename because we we need to call
552 * zfs_mknode() on replay which requires stashing bits as with TX_CREATE.
553 */
554 void
zfs_log_rename_whiteout(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * sdzp,const char * sname,znode_t * tdzp,const char * dname,znode_t * szp,znode_t * wzp)555 zfs_log_rename_whiteout(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
556 znode_t *sdzp, const char *sname, znode_t *tdzp, const char *dname,
557 znode_t *szp, znode_t *wzp)
558 {
559 itx_t *itx;
560 lr_rename_whiteout_t *lr;
561 size_t snamesize = strlen(sname) + 1;
562 size_t dnamesize = strlen(dname) + 1;
563
564 if (zil_replaying(zilog, tx))
565 return;
566
567 txtype |= TX_RENAME_WHITEOUT;
568 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
569 lr = (lr_rename_whiteout_t *)&itx->itx_lr;
570 lr->lr_rename.lr_sdoid = sdzp->z_id;
571 lr->lr_rename.lr_tdoid = tdzp->z_id;
572
573 /*
574 * RENAME_WHITEOUT will create an entry at the source znode, so we need
575 * to store the same data that the equivalent call to zfs_log_create()
576 * would.
577 */
578 lr->lr_wfoid = wzp->z_id;
579 LR_FOID_SET_SLOTS(lr->lr_wfoid, wzp->z_dnodesize >> DNODE_SHIFT);
580 (void) sa_lookup(wzp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(wzp)), &lr->lr_wgen,
581 sizeof (uint64_t));
582 (void) sa_lookup(wzp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(wzp)),
583 lr->lr_wcrtime, sizeof (uint64_t) * 2);
584 lr->lr_wmode = wzp->z_mode;
585 lr->lr_wuid = (uint64_t)KUID_TO_SUID(ZTOUID(wzp));
586 lr->lr_wgid = (uint64_t)KGID_TO_SGID(ZTOGID(wzp));
587
588 /*
589 * This rdev will always be makdevice(0, 0) but because the ZIL log and
590 * replay code needs to be platform independent (and there is no
591 * platform independent makdev()) we need to copy the one created
592 * during the rename operation.
593 */
594 (void) sa_lookup(wzp->z_sa_hdl, SA_ZPL_RDEV(ZTOZSB(wzp)), &lr->lr_wrdev,
595 sizeof (lr->lr_wrdev));
596
597 memcpy(&lr->lr_data[0], sname, snamesize);
598 memcpy(&lr->lr_data[snamesize], dname, dnamesize);
599 itx->itx_oid = szp->z_id;
600
601 zil_itx_assign(zilog, itx, tx);
602 }
603
604 /*
605 * zfs_log_write() handles TX_WRITE transactions. The specified callback is
606 * called as soon as the write is on stable storage (be it via a DMU sync or a
607 * ZIL commit).
608 */
609 static int64_t zfs_immediate_write_sz = 32768;
610
611 void
zfs_log_write(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,offset_t off,ssize_t resid,int ioflag,zil_callback_t callback,void * callback_data)612 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
613 znode_t *zp, offset_t off, ssize_t resid, int ioflag,
614 zil_callback_t callback, void *callback_data)
615 {
616 dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl);
617 uint32_t blocksize = zp->z_blksz;
618 itx_wr_state_t write_state;
619 uintptr_t fsync_cnt;
620 uint64_t gen = 0;
621 ssize_t size = resid;
622
623 if (zil_replaying(zilog, tx) || zp->z_unlinked ||
624 zfs_xattr_owner_unlinked(zp)) {
625 if (callback != NULL)
626 callback(callback_data);
627 return;
628 }
629
630 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
631 write_state = WR_INDIRECT;
632 else if (!spa_has_slogs(zilog->zl_spa) &&
633 resid >= zfs_immediate_write_sz)
634 write_state = WR_INDIRECT;
635 else if (ioflag & (O_SYNC | O_DSYNC))
636 write_state = WR_COPIED;
637 else
638 write_state = WR_NEED_COPY;
639
640 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
641 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
642 }
643
644 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &gen,
645 sizeof (gen));
646
647 while (resid) {
648 itx_t *itx;
649 lr_write_t *lr;
650 itx_wr_state_t wr_state = write_state;
651 ssize_t len = resid;
652
653 /*
654 * A WR_COPIED record must fit entirely in one log block.
655 * Large writes can use WR_NEED_COPY, which the ZIL will
656 * split into multiple records across several log blocks
657 * if necessary.
658 */
659 if (wr_state == WR_COPIED &&
660 resid > zil_max_copied_data(zilog))
661 wr_state = WR_NEED_COPY;
662 else if (wr_state == WR_INDIRECT)
663 len = MIN(blocksize - P2PHASE(off, blocksize), resid);
664
665 itx = zil_itx_create(txtype, sizeof (*lr) +
666 (wr_state == WR_COPIED ? len : 0));
667 lr = (lr_write_t *)&itx->itx_lr;
668
669 /*
670 * For WR_COPIED records, copy the data into the lr_write_t.
671 */
672 if (wr_state == WR_COPIED) {
673 int err;
674 DB_DNODE_ENTER(db);
675 err = dmu_read_by_dnode(DB_DNODE(db), off, len,
676 &lr->lr_data[0], DMU_READ_NO_PREFETCH);
677 DB_DNODE_EXIT(db);
678 if (err != 0) {
679 zil_itx_destroy(itx);
680 itx = zil_itx_create(txtype, sizeof (*lr));
681 lr = (lr_write_t *)&itx->itx_lr;
682 wr_state = WR_NEED_COPY;
683 }
684 }
685
686 itx->itx_wr_state = wr_state;
687 lr->lr_foid = zp->z_id;
688 lr->lr_offset = off;
689 lr->lr_length = len;
690 lr->lr_blkoff = 0;
691 BP_ZERO(&lr->lr_blkptr);
692
693 itx->itx_private = ZTOZSB(zp);
694 itx->itx_gen = gen;
695
696 if (!(ioflag & (O_SYNC | O_DSYNC)) && (zp->z_sync_cnt == 0) &&
697 (fsync_cnt == 0))
698 itx->itx_sync = B_FALSE;
699
700 itx->itx_callback = callback;
701 itx->itx_callback_data = callback_data;
702 zil_itx_assign(zilog, itx, tx);
703
704 off += len;
705 resid -= len;
706 }
707
708 if (write_state == WR_COPIED || write_state == WR_NEED_COPY) {
709 dsl_pool_wrlog_count(zilog->zl_dmu_pool, size, tx->tx_txg);
710 }
711 }
712
713 /*
714 * Handles TX_TRUNCATE transactions.
715 */
716 void
zfs_log_truncate(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,uint64_t off,uint64_t len)717 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
718 znode_t *zp, uint64_t off, uint64_t len)
719 {
720 itx_t *itx;
721 lr_truncate_t *lr;
722
723 if (zil_replaying(zilog, tx) || zp->z_unlinked ||
724 zfs_xattr_owner_unlinked(zp))
725 return;
726
727 itx = zil_itx_create(txtype, sizeof (*lr));
728 lr = (lr_truncate_t *)&itx->itx_lr;
729 lr->lr_foid = zp->z_id;
730 lr->lr_offset = off;
731 lr->lr_length = len;
732
733 itx->itx_sync = (zp->z_sync_cnt != 0);
734 zil_itx_assign(zilog, itx, tx);
735 }
736
737 /*
738 * Handles TX_SETATTR transactions.
739 */
740 void
zfs_log_setattr(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,vattr_t * vap,uint_t mask_applied,zfs_fuid_info_t * fuidp)741 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
742 znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
743 {
744 itx_t *itx;
745 lr_setattr_t *lr;
746 xvattr_t *xvap = (xvattr_t *)vap;
747 size_t recsize = sizeof (lr_setattr_t);
748 uint8_t *start;
749
750 if (zil_replaying(zilog, tx) || zp->z_unlinked)
751 return;
752
753 /*
754 * If XVATTR set, then log record size needs to allow
755 * for lr_attr_t + xvattr mask, mapsize and create time
756 * plus actual attribute values
757 */
758 if (vap->va_mask & ATTR_XVATTR)
759 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
760
761 if (fuidp)
762 recsize += fuidp->z_domain_str_sz;
763
764 itx = zil_itx_create(txtype, recsize);
765 lr = (lr_setattr_t *)&itx->itx_lr;
766 lr->lr_foid = zp->z_id;
767 lr->lr_mask = (uint64_t)mask_applied;
768 lr->lr_mode = (uint64_t)vap->va_mode;
769 if ((mask_applied & ATTR_UID) && IS_EPHEMERAL(vap->va_uid))
770 lr->lr_uid = fuidp->z_fuid_owner;
771 else
772 lr->lr_uid = (uint64_t)vap->va_uid;
773
774 if ((mask_applied & ATTR_GID) && IS_EPHEMERAL(vap->va_gid))
775 lr->lr_gid = fuidp->z_fuid_group;
776 else
777 lr->lr_gid = (uint64_t)vap->va_gid;
778
779 lr->lr_size = (uint64_t)vap->va_size;
780 ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
781 ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
782 start = &lr->lr_data[0];
783 if (vap->va_mask & ATTR_XVATTR) {
784 zfs_log_xvattr((lr_attr_t *)start, xvap);
785 start = &lr->lr_data[ZIL_XVAT_SIZE(xvap->xva_mapsize)];
786 }
787
788 /*
789 * Now stick on domain information if any on end
790 */
791
792 if (fuidp)
793 (void) zfs_log_fuid_domains(fuidp, start);
794
795 itx->itx_sync = (zp->z_sync_cnt != 0);
796 zil_itx_assign(zilog, itx, tx);
797 }
798
799 /*
800 * Handles TX_SETSAXATTR transactions.
801 */
802 void
zfs_log_setsaxattr(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,const char * name,const void * value,size_t size)803 zfs_log_setsaxattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
804 znode_t *zp, const char *name, const void *value, size_t size)
805 {
806 itx_t *itx;
807 lr_setsaxattr_t *lr;
808 size_t recsize = sizeof (lr_setsaxattr_t);
809 int namelen;
810
811 if (zil_replaying(zilog, tx) || zp->z_unlinked)
812 return;
813
814 namelen = strlen(name) + 1;
815 recsize += (namelen + size);
816 itx = zil_itx_create(txtype, recsize);
817 lr = (lr_setsaxattr_t *)&itx->itx_lr;
818 lr->lr_foid = zp->z_id;
819 memcpy(&lr->lr_data[0], name, namelen);
820 if (value != NULL) {
821 memcpy(&lr->lr_data[namelen], value, size);
822 lr->lr_size = size;
823 } else {
824 lr->lr_size = 0;
825 }
826
827 itx->itx_sync = (zp->z_sync_cnt != 0);
828 zil_itx_assign(zilog, itx, tx);
829 }
830
831 /*
832 * Handles TX_ACL transactions.
833 */
834 void
zfs_log_acl(zilog_t * zilog,dmu_tx_t * tx,znode_t * zp,vsecattr_t * vsecp,zfs_fuid_info_t * fuidp)835 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
836 vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
837 {
838 itx_t *itx;
839 lr_acl_v0_t *lrv0;
840 lr_acl_t *lr;
841 int txtype;
842 int lrsize;
843 size_t txsize;
844 size_t aclbytes = vsecp->vsa_aclentsz;
845
846 if (zil_replaying(zilog, tx) || zp->z_unlinked)
847 return;
848
849 txtype = (ZTOZSB(zp)->z_version < ZPL_VERSION_FUID) ?
850 TX_ACL_V0 : TX_ACL;
851
852 if (txtype == TX_ACL)
853 lrsize = sizeof (*lr);
854 else
855 lrsize = sizeof (*lrv0);
856
857 txsize = lrsize +
858 ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
859 (fuidp ? fuidp->z_domain_str_sz : 0) +
860 sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
861
862 itx = zil_itx_create(txtype, txsize);
863
864 lr = (lr_acl_t *)&itx->itx_lr;
865 lr->lr_foid = zp->z_id;
866 if (txtype == TX_ACL) {
867 lr->lr_acl_bytes = aclbytes;
868 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
869 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
870 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
871 lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
872 else
873 lr->lr_acl_flags = 0;
874 }
875 lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
876
877 if (txtype == TX_ACL_V0) {
878 lrv0 = (lr_acl_v0_t *)lr;
879 memcpy(&lrv0->lr_data[0], vsecp->vsa_aclentp, aclbytes);
880 } else {
881 uint8_t *start = &lr->lr_data[0];
882
883 memcpy(start, vsecp->vsa_aclentp, aclbytes);
884
885 start = &lr->lr_data[ZIL_ACE_LENGTH(aclbytes)];
886
887 if (fuidp) {
888 start = zfs_log_fuid_ids(fuidp, start);
889 (void) zfs_log_fuid_domains(fuidp, start);
890 }
891 }
892
893 itx->itx_sync = (zp->z_sync_cnt != 0);
894 zil_itx_assign(zilog, itx, tx);
895 }
896
897 /*
898 * Handles TX_CLONE_RANGE transactions.
899 */
900 void
zfs_log_clone_range(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,uint64_t off,uint64_t len,uint64_t blksz,const blkptr_t * bps,size_t nbps)901 zfs_log_clone_range(zilog_t *zilog, dmu_tx_t *tx, int txtype, znode_t *zp,
902 uint64_t off, uint64_t len, uint64_t blksz, const blkptr_t *bps,
903 size_t nbps)
904 {
905 itx_t *itx;
906 lr_clone_range_t *lr;
907 uint64_t partlen, max_log_data;
908 size_t partnbps;
909
910 if (zil_replaying(zilog, tx) || zp->z_unlinked)
911 return;
912
913 max_log_data = zil_max_log_data(zilog, sizeof (lr_clone_range_t));
914
915 while (nbps > 0) {
916 partnbps = MIN(nbps, max_log_data / sizeof (bps[0]));
917 partlen = partnbps * blksz;
918 ASSERT3U(partlen, <, len + blksz);
919 partlen = MIN(partlen, len);
920
921 itx = zil_itx_create(txtype,
922 sizeof (*lr) + sizeof (bps[0]) * partnbps);
923 lr = (lr_clone_range_t *)&itx->itx_lr;
924 lr->lr_foid = zp->z_id;
925 lr->lr_offset = off;
926 lr->lr_length = partlen;
927 lr->lr_blksz = blksz;
928 lr->lr_nbps = partnbps;
929 memcpy(lr->lr_bps, bps, sizeof (bps[0]) * partnbps);
930
931 itx->itx_sync = (zp->z_sync_cnt != 0);
932
933 zil_itx_assign(zilog, itx, tx);
934
935 bps += partnbps;
936 ASSERT3U(nbps, >=, partnbps);
937 nbps -= partnbps;
938 off += partlen;
939 ASSERT3U(len, >=, partlen);
940 len -= partlen;
941 }
942 }
943
944 ZFS_MODULE_PARAM(zfs, zfs_, immediate_write_sz, S64, ZMOD_RW,
945 "Largest data block to write to zil");
946