xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_log.c (revision ee5416c9d7e449233197d5d20bc6b81e4ff091b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/cmn_err.h>
33 #include <sys/kmem.h>
34 #include <sys/thread.h>
35 #include <sys/file.h>
36 #include <sys/vfs.h>
37 #include <sys/zfs_znode.h>
38 #include <sys/zfs_dir.h>
39 #include <sys/zil.h>
40 #include <sys/zil_impl.h>
41 #include <sys/byteorder.h>
42 #include <sys/policy.h>
43 #include <sys/stat.h>
44 #include <sys/mode.h>
45 #include <sys/acl.h>
46 #include <sys/dmu.h>
47 #include <sys/spa.h>
48 #include <sys/ddi.h>
49 
50 /*
51  * All the functions in this file are used to construct the log entries
52  * to record transactions. They allocate * a intent log transaction
53  * structure (itx_t) and save within it all the information necessary to
54  * possibly replay the transaction. The itx is then assigned a sequence
55  * number and inserted in the in-memory list anchored in the zilog.
56  */
57 
58 /*
59  * zfs_log_create() is used to handle TX_CREATE, TX_MKDIR and TX_MKXATTR
60  * transactions.
61  */
62 void
63 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, int txtype,
64 	znode_t *dzp, znode_t *zp, char *name)
65 {
66 	itx_t *itx;
67 	uint64_t seq;
68 	lr_create_t *lr;
69 	size_t namesize = strlen(name) + 1;
70 
71 	if (zilog == NULL)
72 		return;
73 
74 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
75 	lr = (lr_create_t *)&itx->itx_lr;
76 	lr->lr_doid = dzp->z_id;
77 	lr->lr_foid = zp->z_id;
78 	lr->lr_mode = zp->z_phys->zp_mode;
79 	lr->lr_uid = zp->z_phys->zp_uid;
80 	lr->lr_gid = zp->z_phys->zp_gid;
81 	lr->lr_gen = zp->z_phys->zp_gen;
82 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
83 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
84 	lr->lr_rdev = zp->z_phys->zp_rdev;
85 	bcopy(name, (char *)(lr + 1), namesize);
86 
87 	seq = zil_itx_assign(zilog, itx, tx);
88 	dzp->z_last_itx = seq;
89 	zp->z_last_itx = seq;
90 }
91 
92 /*
93  * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
94  */
95 void
96 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, int txtype,
97 	znode_t *dzp, char *name)
98 {
99 	itx_t *itx;
100 	uint64_t seq;
101 	lr_remove_t *lr;
102 	size_t namesize = strlen(name) + 1;
103 
104 	if (zilog == NULL)
105 		return;
106 
107 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
108 	lr = (lr_remove_t *)&itx->itx_lr;
109 	lr->lr_doid = dzp->z_id;
110 	bcopy(name, (char *)(lr + 1), namesize);
111 
112 	seq = zil_itx_assign(zilog, itx, tx);
113 	dzp->z_last_itx = seq;
114 }
115 
116 /*
117  * zfs_log_link() handles TX_LINK transactions.
118  */
119 void
120 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, int txtype,
121 	znode_t *dzp, znode_t *zp, char *name)
122 {
123 	itx_t *itx;
124 	uint64_t seq;
125 	lr_link_t *lr;
126 	size_t namesize = strlen(name) + 1;
127 
128 	if (zilog == NULL)
129 		return;
130 
131 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
132 	lr = (lr_link_t *)&itx->itx_lr;
133 	lr->lr_doid = dzp->z_id;
134 	lr->lr_link_obj = zp->z_id;
135 	bcopy(name, (char *)(lr + 1), namesize);
136 
137 	seq = zil_itx_assign(zilog, itx, tx);
138 	dzp->z_last_itx = seq;
139 	zp->z_last_itx = seq;
140 }
141 
142 /*
143  * zfs_log_symlink() handles TX_SYMLINK transactions.
144  */
145 void
146 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, int txtype,
147 	znode_t *dzp, znode_t *zp, char *name, char *link)
148 {
149 	itx_t *itx;
150 	uint64_t seq;
151 	lr_create_t *lr;
152 	size_t namesize = strlen(name) + 1;
153 	size_t linksize = strlen(link) + 1;
154 
155 	if (zilog == NULL)
156 		return;
157 
158 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
159 	lr = (lr_create_t *)&itx->itx_lr;
160 	lr->lr_doid = dzp->z_id;
161 	lr->lr_foid = zp->z_id;
162 	lr->lr_mode = zp->z_phys->zp_mode;
163 	lr->lr_uid = zp->z_phys->zp_uid;
164 	lr->lr_gid = zp->z_phys->zp_gid;
165 	lr->lr_gen = zp->z_phys->zp_gen;
166 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
167 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
168 	bcopy(name, (char *)(lr + 1), namesize);
169 	bcopy(link, (char *)(lr + 1) + namesize, linksize);
170 
171 	seq = zil_itx_assign(zilog, itx, tx);
172 	dzp->z_last_itx = seq;
173 	zp->z_last_itx = seq;
174 }
175 
176 /*
177  * zfs_log_rename() handles TX_RENAME transactions.
178  */
179 void
180 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, int txtype,
181 	znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
182 {
183 	itx_t *itx;
184 	uint64_t seq;
185 	lr_rename_t *lr;
186 	size_t snamesize = strlen(sname) + 1;
187 	size_t dnamesize = strlen(dname) + 1;
188 
189 	if (zilog == NULL)
190 		return;
191 
192 	itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
193 	lr = (lr_rename_t *)&itx->itx_lr;
194 	lr->lr_sdoid = sdzp->z_id;
195 	lr->lr_tdoid = tdzp->z_id;
196 	bcopy(sname, (char *)(lr + 1), snamesize);
197 	bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
198 
199 	seq = zil_itx_assign(zilog, itx, tx);
200 	sdzp->z_last_itx = seq;
201 	tdzp->z_last_itx = seq;
202 	szp->z_last_itx = seq;
203 }
204 
205 /*
206  * zfs_log_write() handles TX_WRITE transactions.
207  */
208 ssize_t zfs_immediate_write_sz = 32768;
209 
210 #define	ZIL_MAX_LOG_DATA (SPA_MAXBLOCKSIZE - sizeof (zil_trailer_t) - \
211     sizeof (lr_write_t))
212 
213 void
214 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
215 	znode_t *zp, offset_t off, ssize_t resid, int ioflag)
216 {
217 	itx_wr_state_t write_state;
218 	boolean_t slogging;
219 	uintptr_t fsync_cnt;
220 
221 	if (zilog == NULL || zp->z_unlinked)
222 		return;
223 
224 	/*
225 	 * Writes are handled in three different ways:
226 	 *
227 	 * WR_INDIRECT:
228 	 *    If the write is greater than zfs_immediate_write_sz and there are
229 	 *    no separate logs in this pool then later *if* we need to log the
230 	 *    write then dmu_sync() is used to immediately write the block and
231 	 *    its block pointer is put in the log record.
232 	 * WR_COPIED:
233 	 *    If we know we'll immediately be committing the
234 	 *    transaction (FDSYNC (O_DSYNC)), the we allocate a larger
235 	 *    log record here for the data and copy the data in.
236 	 * WR_NEED_COPY:
237 	 *    Otherwise we don't allocate a buffer, and *if* we need to
238 	 *    flush the write later then a buffer is allocated and
239 	 *    we retrieve the data using the dmu.
240 	 */
241 	slogging = spa_has_slogs(zilog->zl_spa);
242 	if (resid > zfs_immediate_write_sz && !slogging)
243 		write_state = WR_INDIRECT;
244 	else if (ioflag & FDSYNC)
245 		write_state = WR_COPIED;
246 	else
247 		write_state = WR_NEED_COPY;
248 
249 	if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
250 		(void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
251 	}
252 
253 	while (resid) {
254 		itx_t *itx;
255 		lr_write_t *lr;
256 		ssize_t len;
257 
258 		/*
259 		 * If there are slogs and the write would overflow the largest
260 		 * block, then because we don't want to use the main pool
261 		 * to dmu_sync, we have to split the write.
262 		 */
263 		if (slogging && resid > ZIL_MAX_LOG_DATA)
264 			len = SPA_MAXBLOCKSIZE >> 1;
265 		else
266 			len = resid;
267 
268 		itx = zil_itx_create(txtype, sizeof (*lr) +
269 		    (write_state == WR_COPIED ? len : 0));
270 		lr = (lr_write_t *)&itx->itx_lr;
271 		if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
272 		    zp->z_id, off, len, lr + 1) != 0) {
273 			kmem_free(itx, offsetof(itx_t, itx_lr) +
274 			    itx->itx_lr.lrc_reclen);
275 			itx = zil_itx_create(txtype, sizeof (*lr));
276 			lr = (lr_write_t *)&itx->itx_lr;
277 			write_state = WR_NEED_COPY;
278 		}
279 
280 		itx->itx_wr_state = write_state;
281 		lr->lr_foid = zp->z_id;
282 		lr->lr_offset = off;
283 		lr->lr_length = len;
284 		lr->lr_blkoff = 0;
285 		BP_ZERO(&lr->lr_blkptr);
286 
287 		itx->itx_private = zp->z_zfsvfs;
288 
289 		if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0))
290 			itx->itx_sync = B_TRUE;
291 		else
292 			itx->itx_sync = B_FALSE;
293 
294 		zp->z_last_itx = zil_itx_assign(zilog, itx, tx);
295 
296 		off += len;
297 		resid -= len;
298 	}
299 }
300 
301 /*
302  * zfs_log_truncate() handles TX_TRUNCATE transactions.
303  */
304 void
305 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
306 	znode_t *zp, uint64_t off, uint64_t len)
307 {
308 	itx_t *itx;
309 	uint64_t seq;
310 	lr_truncate_t *lr;
311 
312 	if (zilog == NULL || zp->z_unlinked)
313 		return;
314 
315 	itx = zil_itx_create(txtype, sizeof (*lr));
316 	lr = (lr_truncate_t *)&itx->itx_lr;
317 	lr->lr_foid = zp->z_id;
318 	lr->lr_offset = off;
319 	lr->lr_length = len;
320 
321 	itx->itx_sync = (zp->z_sync_cnt != 0);
322 	seq = zil_itx_assign(zilog, itx, tx);
323 	zp->z_last_itx = seq;
324 }
325 
326 /*
327  * zfs_log_setattr() handles TX_SETATTR transactions.
328  */
329 void
330 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
331 	znode_t *zp, vattr_t *vap, uint_t mask_applied)
332 {
333 	itx_t *itx;
334 	uint64_t seq;
335 	lr_setattr_t *lr;
336 
337 	if (zilog == NULL || zp->z_unlinked)
338 		return;
339 
340 	itx = zil_itx_create(txtype, sizeof (*lr));
341 	lr = (lr_setattr_t *)&itx->itx_lr;
342 	lr->lr_foid = zp->z_id;
343 	lr->lr_mask = (uint64_t)mask_applied;
344 	lr->lr_mode = (uint64_t)vap->va_mode;
345 	lr->lr_uid = (uint64_t)vap->va_uid;
346 	lr->lr_gid = (uint64_t)vap->va_gid;
347 	lr->lr_size = (uint64_t)vap->va_size;
348 	ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
349 	ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
350 
351 	itx->itx_sync = (zp->z_sync_cnt != 0);
352 	seq = zil_itx_assign(zilog, itx, tx);
353 	zp->z_last_itx = seq;
354 }
355 
356 /*
357  * zfs_log_acl() handles TX_ACL transactions.
358  */
359 void
360 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, int txtype,
361 	znode_t *zp, int aclcnt, ace_t *z_ace)
362 {
363 	itx_t *itx;
364 	uint64_t seq;
365 	lr_acl_t *lr;
366 
367 	if (zilog == NULL || zp->z_unlinked)
368 		return;
369 
370 	itx = zil_itx_create(txtype, sizeof (*lr) + aclcnt * sizeof (ace_t));
371 	lr = (lr_acl_t *)&itx->itx_lr;
372 	lr->lr_foid = zp->z_id;
373 	lr->lr_aclcnt = (uint64_t)aclcnt;
374 	bcopy(z_ace, (ace_t *)(lr + 1), aclcnt * sizeof (ace_t));
375 
376 	itx->itx_sync = (zp->z_sync_cnt != 0);
377 	seq = zil_itx_assign(zilog, itx, tx);
378 	zp->z_last_itx = seq;
379 }
380