xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_log.c (revision fdd1ecae0dfe07e6aa8ee90687e2e91c876dc189)
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 
220 	if (zilog == NULL || zp->z_unlinked)
221 		return;
222 
223 	/*
224 	 * Writes are handled in three different ways:
225 	 *
226 	 * WR_INDIRECT:
227 	 *    If the write is greater than zfs_immediate_write_sz and there are
228 	 *    no separate logs in this pool then later *if* we need to log the
229 	 *    write then dmu_sync() is used to immediately write the block and
230 	 *    its block pointer is put in the log record.
231 	 * WR_COPIED:
232 	 *    If we know we'll immediately be committing the
233 	 *    transaction (FDSYNC (O_DSYNC)), the we allocate a larger
234 	 *    log record here for the data and copy the data in.
235 	 * WR_NEED_COPY:
236 	 *    Otherwise we don't allocate a buffer, and *if* we need to
237 	 *    flush the write later then a buffer is allocated and
238 	 *    we retrieve the data using the dmu.
239 	 */
240 	slogging = spa_has_slogs(zilog->zl_spa);
241 	if (resid > zfs_immediate_write_sz && !slogging)
242 		write_state = WR_INDIRECT;
243 	else if (ioflag & FDSYNC)
244 		write_state = WR_COPIED;
245 	else
246 		write_state = WR_NEED_COPY;
247 
248 	while (resid) {
249 		itx_t *itx;
250 		lr_write_t *lr;
251 		ssize_t len;
252 
253 		/*
254 		 * If there are slogs and the write would overflow the largest
255 		 * block, then because we don't want to use the main pool
256 		 * to dmu_sync, we have to split the write.
257 		 */
258 		if (slogging && resid > ZIL_MAX_LOG_DATA)
259 			len = SPA_MAXBLOCKSIZE >> 1;
260 		else
261 			len = resid;
262 
263 		itx = zil_itx_create(txtype, sizeof (*lr) +
264 		    (write_state == WR_COPIED ? len : 0));
265 		lr = (lr_write_t *)&itx->itx_lr;
266 		if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
267 		    zp->z_id, off, len, lr + 1) != 0) {
268 			kmem_free(itx, offsetof(itx_t, itx_lr) +
269 			    itx->itx_lr.lrc_reclen);
270 			itx = zil_itx_create(txtype, sizeof (*lr));
271 			lr = (lr_write_t *)&itx->itx_lr;
272 			write_state = WR_NEED_COPY;
273 		}
274 
275 		itx->itx_wr_state = write_state;
276 		lr->lr_foid = zp->z_id;
277 		lr->lr_offset = off;
278 		lr->lr_length = len;
279 		lr->lr_blkoff = 0;
280 		BP_ZERO(&lr->lr_blkptr);
281 
282 		itx->itx_private = zp->z_zfsvfs;
283 
284 		itx->itx_sync = (zp->z_sync_cnt != 0);
285 		zp->z_last_itx = zil_itx_assign(zilog, itx, tx);
286 
287 		off += len;
288 		resid -= len;
289 	}
290 }
291 
292 /*
293  * zfs_log_truncate() handles TX_TRUNCATE transactions.
294  */
295 void
296 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
297 	znode_t *zp, uint64_t off, uint64_t len)
298 {
299 	itx_t *itx;
300 	uint64_t seq;
301 	lr_truncate_t *lr;
302 
303 	if (zilog == NULL || zp->z_unlinked)
304 		return;
305 
306 	itx = zil_itx_create(txtype, sizeof (*lr));
307 	lr = (lr_truncate_t *)&itx->itx_lr;
308 	lr->lr_foid = zp->z_id;
309 	lr->lr_offset = off;
310 	lr->lr_length = len;
311 
312 	itx->itx_sync = (zp->z_sync_cnt != 0);
313 	seq = zil_itx_assign(zilog, itx, tx);
314 	zp->z_last_itx = seq;
315 }
316 
317 /*
318  * zfs_log_setattr() handles TX_SETATTR transactions.
319  */
320 void
321 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
322 	znode_t *zp, vattr_t *vap, uint_t mask_applied)
323 {
324 	itx_t *itx;
325 	uint64_t seq;
326 	lr_setattr_t *lr;
327 
328 	if (zilog == NULL || zp->z_unlinked)
329 		return;
330 
331 	itx = zil_itx_create(txtype, sizeof (*lr));
332 	lr = (lr_setattr_t *)&itx->itx_lr;
333 	lr->lr_foid = zp->z_id;
334 	lr->lr_mask = (uint64_t)mask_applied;
335 	lr->lr_mode = (uint64_t)vap->va_mode;
336 	lr->lr_uid = (uint64_t)vap->va_uid;
337 	lr->lr_gid = (uint64_t)vap->va_gid;
338 	lr->lr_size = (uint64_t)vap->va_size;
339 	ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
340 	ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
341 
342 	itx->itx_sync = (zp->z_sync_cnt != 0);
343 	seq = zil_itx_assign(zilog, itx, tx);
344 	zp->z_last_itx = seq;
345 }
346 
347 /*
348  * zfs_log_acl() handles TX_ACL transactions.
349  */
350 void
351 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, int txtype,
352 	znode_t *zp, int aclcnt, ace_t *z_ace)
353 {
354 	itx_t *itx;
355 	uint64_t seq;
356 	lr_acl_t *lr;
357 
358 	if (zilog == NULL || zp->z_unlinked)
359 		return;
360 
361 	itx = zil_itx_create(txtype, sizeof (*lr) + aclcnt * sizeof (ace_t));
362 	lr = (lr_acl_t *)&itx->itx_lr;
363 	lr->lr_foid = zp->z_id;
364 	lr->lr_aclcnt = (uint64_t)aclcnt;
365 	bcopy(z_ace, (ace_t *)(lr + 1), aclcnt * sizeof (ace_t));
366 
367 	itx->itx_sync = (zp->z_sync_cnt != 0);
368 	seq = zil_itx_assign(zilog, itx, tx);
369 	zp->z_last_itx = seq;
370 }
371