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