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