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