xref: /freebsd/sys/contrib/openzfs/module/zfs/dmu_direct.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 
14 #include <sys/dmu.h>
15 #include <sys/dmu_impl.h>
16 #include <sys/dbuf.h>
17 #include <sys/dnode.h>
18 #include <sys/zfs_context.h>
19 #include <sys/zfs_racct.h>
20 #include <sys/dsl_dataset.h>
21 #include <sys/dmu_objset.h>
22 
23 static abd_t *
make_abd_for_dbuf(dmu_buf_impl_t * db,abd_t * data,uint64_t offset,uint64_t size)24 make_abd_for_dbuf(dmu_buf_impl_t *db, abd_t *data, uint64_t offset,
25     uint64_t size)
26 {
27 	size_t buf_size = db->db.db_size;
28 	abd_t *pre_buf = NULL, *post_buf = NULL, *mbuf = NULL;
29 	size_t buf_off = 0;
30 
31 	ASSERT(MUTEX_HELD(&db->db_mtx));
32 
33 	if (offset > db->db.db_offset) {
34 		size_t pre_size = offset - db->db.db_offset;
35 		pre_buf = abd_alloc_for_io(pre_size, B_TRUE);
36 		buf_size -= pre_size;
37 		buf_off = 0;
38 	} else {
39 		buf_off = db->db.db_offset - offset;
40 		size -= buf_off;
41 	}
42 
43 	if (size < buf_size) {
44 		size_t post_size = buf_size - size;
45 		post_buf = abd_alloc_for_io(post_size, B_TRUE);
46 		buf_size -= post_size;
47 	}
48 
49 	ASSERT3U(buf_size, >, 0);
50 	abd_t *buf = abd_get_offset_size(data, buf_off, buf_size);
51 
52 	if (pre_buf || post_buf) {
53 		mbuf = abd_alloc_gang();
54 		if (pre_buf)
55 			abd_gang_add(mbuf, pre_buf, B_TRUE);
56 		abd_gang_add(mbuf, buf, B_TRUE);
57 		if (post_buf)
58 			abd_gang_add(mbuf, post_buf, B_TRUE);
59 	} else {
60 		mbuf = buf;
61 	}
62 
63 	return (mbuf);
64 }
65 
66 static void
dmu_read_abd_done(zio_t * zio)67 dmu_read_abd_done(zio_t *zio)
68 {
69 	abd_free(zio->io_abd);
70 }
71 
72 static void
dmu_write_direct_ready(zio_t * zio)73 dmu_write_direct_ready(zio_t *zio)
74 {
75 	dmu_sync_ready(zio, NULL, zio->io_private);
76 }
77 
78 static void
dmu_write_direct_done(zio_t * zio)79 dmu_write_direct_done(zio_t *zio)
80 {
81 	dmu_sync_arg_t *dsa = zio->io_private;
82 	dbuf_dirty_record_t *dr = dsa->dsa_dr;
83 	dmu_buf_impl_t *db = dr->dr_dbuf;
84 	dmu_tx_t *tx = dsa->dsa_tx;
85 
86 	abd_free(zio->io_abd);
87 
88 	mutex_enter(&db->db_mtx);
89 	ASSERT0P(db->db_buf);
90 	ASSERT0P(dr->dt.dl.dr_data);
91 	ASSERT0P(db->db.db_data);
92 	db->db_state = DB_UNCACHED;
93 	mutex_exit(&db->db_mtx);
94 
95 	/*
96 	 * dmu_sync_done() owns dsa and frees it after publishing the final
97 	 * override state.  The direct-I/O error path still needs the original
98 	 * open-context tx to roll the dirty record back with dbuf_undirty().
99 	 */
100 	dmu_sync_done(zio, NULL, zio->io_private);
101 
102 	if (zio->io_error != 0) {
103 		if (zio->io_post & ZIO_POST_DIO_CHKSUM_ERR)
104 			ASSERT3U(zio->io_error, ==, EIO);
105 
106 		/*
107 		 * In the event of an I/O error this block has been freed in
108 		 * zio_done() through zio_dva_unallocate(). Calling
109 		 * dmu_sync_done() above set dr_override_state to
110 		 * DR_NOT_OVERRIDDEN. In this case when dbuf_undirty() calls
111 		 * dbuf_unoverride(), it will skip doing zio_free() to free
112 		 * this block as that was already taken care of.
113 		 *
114 		 * Since we are undirtying the record in open-context, we must
115 		 * have a hold on the db, so it should never be evicted after
116 		 * calling dbuf_undirty().
117 		 */
118 		mutex_enter(&db->db_mtx);
119 		VERIFY3B(dbuf_undirty(db, tx), ==, B_FALSE);
120 		mutex_exit(&db->db_mtx);
121 	}
122 
123 	kmem_free(zio->io_bp, sizeof (blkptr_t));
124 	zio->io_bp = NULL;
125 }
126 
127 int
dmu_write_direct(zio_t * pio,dmu_buf_impl_t * db,abd_t * data,dmu_tx_t * tx)128 dmu_write_direct(zio_t *pio, dmu_buf_impl_t *db, abd_t *data, dmu_tx_t *tx)
129 {
130 	objset_t *os = db->db_objset;
131 	dsl_dataset_t *ds = dmu_objset_ds(os);
132 	zbookmark_phys_t zb;
133 	dbuf_dirty_record_t *dr_head;
134 
135 	SET_BOOKMARK(&zb, ds->ds_object,
136 	    db->db.db_object, db->db_level, db->db_blkid);
137 
138 	DB_DNODE_ENTER(db);
139 	zio_prop_t zp;
140 	dmu_write_policy(os, DB_DNODE(db), db->db_level,
141 	    WP_DMU_SYNC | WP_DIRECT_WR, &zp);
142 	DB_DNODE_EXIT(db);
143 
144 	/*
145 	 * Dirty this dbuf with DB_NOFILL since we will not have any data
146 	 * associated with the dbuf.
147 	 */
148 	dmu_buf_will_clone_or_dio(&db->db, tx);
149 
150 	mutex_enter(&db->db_mtx);
151 
152 	uint64_t txg = dmu_tx_get_txg(tx);
153 	ASSERT3U(txg, >, spa_last_synced_txg(os->os_spa));
154 	ASSERT3U(txg, >, spa_syncing_txg(os->os_spa));
155 
156 	dr_head = list_head(&db->db_dirty_records);
157 	ASSERT3U(dr_head->dr_txg, ==, txg);
158 	dr_head->dt.dl.dr_diowrite = B_TRUE;
159 	dr_head->dr_accounted = db->db.db_size;
160 
161 	blkptr_t *bp = kmem_alloc(sizeof (blkptr_t), KM_SLEEP);
162 	if (db->db_blkptr != NULL) {
163 		/*
164 		 * Fill in bp with the current block pointer so that
165 		 * the nopwrite code can check if we're writing the same
166 		 * data that's already on disk.
167 		 */
168 		*bp = *db->db_blkptr;
169 	} else {
170 		memset(bp, 0, sizeof (blkptr_t));
171 	}
172 
173 	/*
174 	 * Disable nopwrite if the current block pointer could change
175 	 * before this TXG syncs.
176 	 */
177 	if (list_next(&db->db_dirty_records, dr_head) != NULL)
178 		zp.zp_nopwrite = B_FALSE;
179 
180 	ASSERT0(dr_head->dt.dl.dr_has_raw_params);
181 	ASSERT3S(dr_head->dt.dl.dr_override_state, ==, DR_NOT_OVERRIDDEN);
182 	dr_head->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
183 
184 	mutex_exit(&db->db_mtx);
185 
186 	dmu_objset_willuse_space(os, dr_head->dr_accounted, tx);
187 
188 	dmu_sync_arg_t *dsa = kmem_zalloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
189 	dsa->dsa_dr = dr_head;
190 	dsa->dsa_tx = tx;
191 
192 	zio_t *zio = zio_write(pio, os->os_spa, txg, bp, data,
193 	    db->db.db_size, db->db.db_size, &zp,
194 	    dmu_write_direct_ready, NULL, dmu_write_direct_done, dsa,
195 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, &zb);
196 
197 	if (pio == NULL)
198 		return (zio_wait(zio));
199 
200 	zio_nowait(zio);
201 
202 	return (0);
203 }
204 
205 int
dmu_write_abd(dnode_t * dn,uint64_t offset,uint64_t size,abd_t * data,dmu_flags_t flags,dmu_tx_t * tx)206 dmu_write_abd(dnode_t *dn, uint64_t offset, uint64_t size,
207     abd_t *data, dmu_flags_t flags, dmu_tx_t *tx)
208 {
209 	dmu_buf_t **dbp;
210 	spa_t *spa = dn->dn_objset->os_spa;
211 	int numbufs, err;
212 
213 	ASSERT(flags & DMU_DIRECTIO);
214 
215 	err = dmu_buf_hold_array_by_dnode(dn, offset,
216 	    size, B_FALSE, FTAG, &numbufs, &dbp, flags);
217 	if (err)
218 		return (err);
219 
220 	zio_t *pio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
221 
222 	for (int i = 0; i < numbufs && err == 0; i++) {
223 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
224 
225 		abd_t *abd = abd_get_offset_size(data,
226 		    db->db.db_offset - offset, dn->dn_datablksz);
227 
228 		zfs_racct_write(spa, db->db.db_size, 1, flags);
229 		err = dmu_write_direct(pio, db, abd, tx);
230 		ASSERT0(err);
231 	}
232 
233 	err = zio_wait(pio);
234 
235 	/*
236 	 * The dbuf must be held until the Direct I/O write has completed in
237 	 * the event there was any errors and dbuf_undirty() was called.
238 	 */
239 	dmu_buf_rele_array(dbp, numbufs, FTAG);
240 
241 	return (err);
242 }
243 
244 int
dmu_read_abd(dnode_t * dn,uint64_t offset,uint64_t size,abd_t * data,dmu_flags_t flags)245 dmu_read_abd(dnode_t *dn, uint64_t offset, uint64_t size,
246     abd_t *data, dmu_flags_t flags)
247 {
248 	objset_t *os = dn->dn_objset;
249 	spa_t *spa = os->os_spa;
250 	dmu_buf_t **dbp;
251 	int numbufs, err;
252 
253 	ASSERT(flags & DMU_DIRECTIO);
254 
255 	err = dmu_buf_hold_array_by_dnode(dn, offset,
256 	    size, B_FALSE, FTAG, &numbufs, &dbp, flags);
257 	if (err)
258 		return (err);
259 
260 	zio_t *rio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
261 
262 	for (int i = 0; i < numbufs; i++) {
263 		dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
264 		abd_t *mbuf;
265 		zbookmark_phys_t zb;
266 		blkptr_t *bp;
267 
268 		mutex_enter(&db->db_mtx);
269 
270 		SET_BOOKMARK(&zb, dmu_objset_ds(os)->ds_object,
271 		    db->db.db_object, db->db_level, db->db_blkid);
272 
273 		/*
274 		 * If there is another read for this dbuf, we will wait for
275 		 * that to complete first before checking the db_state below.
276 		 */
277 		while (db->db_state == DB_READ)
278 			cv_wait(&db->db_changed, &db->db_mtx);
279 
280 		err = dmu_buf_get_bp_from_dbuf(db, &bp);
281 		if (err) {
282 			mutex_exit(&db->db_mtx);
283 			goto error;
284 		}
285 
286 		/*
287 		 * There is no need to read if this is a hole or the data is
288 		 * cached. This will not be considered a direct read for IO
289 		 * accounting in the same way that an ARC hit is not counted.
290 		 */
291 		if (bp == NULL || BP_IS_HOLE(bp) || db->db_state == DB_CACHED) {
292 			size_t aoff = offset < db->db.db_offset ?
293 			    db->db.db_offset - offset : 0;
294 			size_t boff = offset > db->db.db_offset ?
295 			    offset - db->db.db_offset : 0;
296 			size_t len = MIN(size - aoff, db->db.db_size - boff);
297 
298 			if (db->db_state == DB_CACHED) {
299 				/*
300 				 * We need to untransformed the ARC buf data
301 				 * before we copy it over.
302 				 */
303 				err = dmu_buf_untransform_direct(db, spa);
304 				ASSERT0(err);
305 				abd_copy_from_buf_off(data,
306 				    (char *)db->db.db_data + boff, aoff, len);
307 			} else {
308 				abd_zero_off(data, aoff, len);
309 			}
310 
311 			mutex_exit(&db->db_mtx);
312 			continue;
313 		}
314 
315 		mbuf = make_abd_for_dbuf(db, data, offset, size);
316 		ASSERT3P(mbuf, !=, NULL);
317 
318 		/*
319 		 * The dbuf mutex (db_mtx) must be held when creating the ZIO
320 		 * for the read. The BP returned from
321 		 * dmu_buf_get_bp_from_dbuf() could be from a pending block
322 		 * clone or a yet to be synced Direct I/O write that is in the
323 		 * dbuf's dirty record. When zio_read() is called, zio_create()
324 		 * will make a copy of the BP. However, if zio_read() is called
325 		 * without the mutex being held then the dirty record from the
326 		 * dbuf could be freed in dbuf_write_done() resulting in garbage
327 		 * being set for the zio BP.
328 		 */
329 		zio_t *cio = zio_read(rio, spa, bp, mbuf, db->db.db_size,
330 		    dmu_read_abd_done, NULL, ZIO_PRIORITY_SYNC_READ,
331 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DIO_READ, &zb);
332 		mutex_exit(&db->db_mtx);
333 
334 		zfs_racct_read(spa, db->db.db_size, 1, flags);
335 		zio_nowait(cio);
336 	}
337 
338 	dmu_buf_rele_array(dbp, numbufs, FTAG);
339 
340 	return (zio_wait(rio));
341 
342 error:
343 	dmu_buf_rele_array(dbp, numbufs, FTAG);
344 	(void) zio_wait(rio);
345 	return (err);
346 }
347 
348 #ifdef _KERNEL
349 int
dmu_read_uio_direct(dnode_t * dn,zfs_uio_t * uio,uint64_t size,dmu_flags_t flags)350 dmu_read_uio_direct(dnode_t *dn, zfs_uio_t *uio, uint64_t size,
351     dmu_flags_t flags)
352 {
353 	offset_t offset = zfs_uio_offset(uio);
354 	offset_t page_index = (offset - zfs_uio_soffset(uio)) >> PAGESHIFT;
355 	int err;
356 
357 	ASSERT(uio->uio_extflg & UIO_DIRECT);
358 	ASSERT3U(page_index, <, uio->uio_dio.npages);
359 
360 	abd_t *data = abd_alloc_from_pages(&uio->uio_dio.pages[page_index],
361 	    offset & (PAGESIZE - 1), size);
362 	err = dmu_read_abd(dn, offset, size, data, flags);
363 	abd_free(data);
364 
365 	if (err == 0)
366 		zfs_uioskip(uio, size);
367 
368 	return (err);
369 }
370 
371 int
dmu_write_uio_direct(dnode_t * dn,zfs_uio_t * uio,uint64_t size,dmu_flags_t flags,dmu_tx_t * tx)372 dmu_write_uio_direct(dnode_t *dn, zfs_uio_t *uio, uint64_t size,
373     dmu_flags_t flags, dmu_tx_t *tx)
374 {
375 	offset_t offset = zfs_uio_offset(uio);
376 	offset_t page_index = (offset - zfs_uio_soffset(uio)) >> PAGESHIFT;
377 	int err;
378 
379 	ASSERT(uio->uio_extflg & UIO_DIRECT);
380 	ASSERT3U(page_index, <, uio->uio_dio.npages);
381 
382 	abd_t *data = abd_alloc_from_pages(&uio->uio_dio.pages[page_index],
383 	    offset & (PAGESIZE - 1), size);
384 	err = dmu_write_abd(dn, offset, size, data, flags, tx);
385 	abd_free(data);
386 
387 	if (err == 0)
388 		zfs_uioskip(uio, size);
389 
390 	return (err);
391 }
392 #endif /* _KERNEL */
393 
394 EXPORT_SYMBOL(dmu_read_abd);
395 EXPORT_SYMBOL(dmu_write_abd);
396