xref: /linux/block/bio-integrity.c (revision 785cdec46e9227f9433884ed3b436471e944007c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bio-integrity.c - bio data integrity extensions
4  *
5  * Copyright (C) 2007, 2008, 2009 Oracle Corporation
6  * Written by: Martin K. Petersen <martin.petersen@oracle.com>
7  */
8 
9 #include <linux/blk-integrity.h>
10 #include "blk.h"
11 
12 struct bio_integrity_alloc {
13 	struct bio_integrity_payload	bip;
14 	struct bio_vec			bvecs[];
15 };
16 
17 /**
18  * bio_integrity_free - Free bio integrity payload
19  * @bio:	bio containing bip to be freed
20  *
21  * Description: Free the integrity portion of a bio.
22  */
23 void bio_integrity_free(struct bio *bio)
24 {
25 	kfree(bio_integrity(bio));
26 	bio->bi_integrity = NULL;
27 	bio->bi_opf &= ~REQ_INTEGRITY;
28 }
29 
30 void bio_integrity_init(struct bio *bio, struct bio_integrity_payload *bip,
31 		struct bio_vec *bvecs, unsigned int nr_vecs)
32 {
33 	memset(bip, 0, sizeof(*bip));
34 	bip->bip_max_vcnt = nr_vecs;
35 	if (nr_vecs)
36 		bip->bip_vec = bvecs;
37 
38 	bio->bi_integrity = bip;
39 	bio->bi_opf |= REQ_INTEGRITY;
40 }
41 
42 /**
43  * bio_integrity_alloc - Allocate integrity payload and attach it to bio
44  * @bio:	bio to attach integrity metadata to
45  * @gfp_mask:	Memory allocation mask
46  * @nr_vecs:	Number of integrity metadata scatter-gather elements
47  *
48  * Description: This function prepares a bio for attaching integrity
49  * metadata.  nr_vecs specifies the maximum number of pages containing
50  * integrity metadata that can be attached.
51  */
52 struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
53 						  gfp_t gfp_mask,
54 						  unsigned int nr_vecs)
55 {
56 	struct bio_integrity_alloc *bia;
57 
58 	if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
59 		return ERR_PTR(-EOPNOTSUPP);
60 
61 	bia = kmalloc(struct_size(bia, bvecs, nr_vecs), gfp_mask);
62 	if (unlikely(!bia))
63 		return ERR_PTR(-ENOMEM);
64 	bio_integrity_init(bio, &bia->bip, bia->bvecs, nr_vecs);
65 	return &bia->bip;
66 }
67 EXPORT_SYMBOL(bio_integrity_alloc);
68 
69 static void bio_integrity_unpin_bvec(struct bio_vec *bv, int nr_vecs)
70 {
71 	int i;
72 
73 	for (i = 0; i < nr_vecs; i++)
74 		unpin_user_page(bv[i].bv_page);
75 }
76 
77 static void bio_integrity_uncopy_user(struct bio_integrity_payload *bip)
78 {
79 	unsigned short orig_nr_vecs = bip->bip_max_vcnt - 1;
80 	struct bio_vec *orig_bvecs = &bip->bip_vec[1];
81 	struct bio_vec *bounce_bvec = &bip->bip_vec[0];
82 	size_t bytes = bounce_bvec->bv_len;
83 	struct iov_iter orig_iter;
84 	int ret;
85 
86 	iov_iter_bvec(&orig_iter, ITER_DEST, orig_bvecs, orig_nr_vecs, bytes);
87 	ret = copy_to_iter(bvec_virt(bounce_bvec), bytes, &orig_iter);
88 	WARN_ON_ONCE(ret != bytes);
89 
90 	bio_integrity_unpin_bvec(orig_bvecs, orig_nr_vecs);
91 }
92 
93 /**
94  * bio_integrity_unmap_user - Unmap user integrity payload
95  * @bio:	bio containing bip to be unmapped
96  *
97  * Unmap the user mapped integrity portion of a bio.
98  */
99 void bio_integrity_unmap_user(struct bio *bio)
100 {
101 	struct bio_integrity_payload *bip = bio_integrity(bio);
102 
103 	if (bip->bip_flags & BIP_COPY_USER) {
104 		if (bio_data_dir(bio) == READ)
105 			bio_integrity_uncopy_user(bip);
106 		kfree(bvec_virt(bip->bip_vec));
107 		return;
108 	}
109 
110 	bio_integrity_unpin_bvec(bip->bip_vec, bip->bip_max_vcnt);
111 }
112 
113 /**
114  * bio_integrity_add_page - Attach integrity metadata
115  * @bio:	bio to update
116  * @page:	page containing integrity metadata
117  * @len:	number of bytes of integrity metadata in page
118  * @offset:	start offset within page
119  *
120  * Description: Attach a page containing integrity metadata to bio.
121  */
122 int bio_integrity_add_page(struct bio *bio, struct page *page,
123 			   unsigned int len, unsigned int offset)
124 {
125 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
126 	struct bio_integrity_payload *bip = bio_integrity(bio);
127 
128 	if (bip->bip_vcnt > 0) {
129 		struct bio_vec *bv = &bip->bip_vec[bip->bip_vcnt - 1];
130 
131 		if (bvec_try_merge_hw_page(q, bv, page, len, offset)) {
132 			bip->bip_iter.bi_size += len;
133 			return len;
134 		}
135 
136 		if (bip->bip_vcnt >=
137 		    min(bip->bip_max_vcnt, queue_max_integrity_segments(q)))
138 			return 0;
139 
140 		/*
141 		 * If the queue doesn't support SG gaps and adding this segment
142 		 * would create a gap, disallow it.
143 		 */
144 		if (bvec_gap_to_prev(&q->limits, bv, offset))
145 			return 0;
146 	}
147 
148 	bvec_set_page(&bip->bip_vec[bip->bip_vcnt], page, len, offset);
149 	bip->bip_vcnt++;
150 	bip->bip_iter.bi_size += len;
151 
152 	return len;
153 }
154 EXPORT_SYMBOL(bio_integrity_add_page);
155 
156 static int bio_integrity_copy_user(struct bio *bio, struct bio_vec *bvec,
157 				   int nr_vecs, unsigned int len,
158 				   unsigned int direction)
159 {
160 	bool write = direction == ITER_SOURCE;
161 	struct bio_integrity_payload *bip;
162 	struct iov_iter iter;
163 	void *buf;
164 	int ret;
165 
166 	buf = kmalloc(len, GFP_KERNEL);
167 	if (!buf)
168 		return -ENOMEM;
169 
170 	if (write) {
171 		iov_iter_bvec(&iter, direction, bvec, nr_vecs, len);
172 		if (!copy_from_iter_full(buf, len, &iter)) {
173 			ret = -EFAULT;
174 			goto free_buf;
175 		}
176 
177 		bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
178 	} else {
179 		memset(buf, 0, len);
180 
181 		/*
182 		 * We need to preserve the original bvec and the number of vecs
183 		 * in it for completion handling
184 		 */
185 		bip = bio_integrity_alloc(bio, GFP_KERNEL, nr_vecs + 1);
186 	}
187 
188 	if (IS_ERR(bip)) {
189 		ret = PTR_ERR(bip);
190 		goto free_buf;
191 	}
192 
193 	if (write)
194 		bio_integrity_unpin_bvec(bvec, nr_vecs);
195 	else
196 		memcpy(&bip->bip_vec[1], bvec, nr_vecs * sizeof(*bvec));
197 
198 	ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
199 				     offset_in_page(buf));
200 	if (ret != len) {
201 		ret = -ENOMEM;
202 		goto free_bip;
203 	}
204 
205 	bip->bip_flags |= BIP_COPY_USER;
206 	bip->bip_vcnt = nr_vecs;
207 	return 0;
208 free_bip:
209 	bio_integrity_free(bio);
210 free_buf:
211 	kfree(buf);
212 	return ret;
213 }
214 
215 static int bio_integrity_init_user(struct bio *bio, struct bio_vec *bvec,
216 				   int nr_vecs, unsigned int len)
217 {
218 	struct bio_integrity_payload *bip;
219 
220 	bip = bio_integrity_alloc(bio, GFP_KERNEL, nr_vecs);
221 	if (IS_ERR(bip))
222 		return PTR_ERR(bip);
223 
224 	memcpy(bip->bip_vec, bvec, nr_vecs * sizeof(*bvec));
225 	bip->bip_iter.bi_size = len;
226 	bip->bip_vcnt = nr_vecs;
227 	return 0;
228 }
229 
230 static unsigned int bvec_from_pages(struct bio_vec *bvec, struct page **pages,
231 				    int nr_vecs, ssize_t bytes, ssize_t offset)
232 {
233 	unsigned int nr_bvecs = 0;
234 	int i, j;
235 
236 	for (i = 0; i < nr_vecs; i = j) {
237 		size_t size = min_t(size_t, bytes, PAGE_SIZE - offset);
238 		struct folio *folio = page_folio(pages[i]);
239 
240 		bytes -= size;
241 		for (j = i + 1; j < nr_vecs; j++) {
242 			size_t next = min_t(size_t, PAGE_SIZE, bytes);
243 
244 			if (page_folio(pages[j]) != folio ||
245 			    pages[j] != pages[j - 1] + 1)
246 				break;
247 			unpin_user_page(pages[j]);
248 			size += next;
249 			bytes -= next;
250 		}
251 
252 		bvec_set_page(&bvec[nr_bvecs], pages[i], size, offset);
253 		offset = 0;
254 		nr_bvecs++;
255 	}
256 
257 	return nr_bvecs;
258 }
259 
260 int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
261 {
262 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
263 	unsigned int align = blk_lim_dma_alignment_and_pad(&q->limits);
264 	struct page *stack_pages[UIO_FASTIOV], **pages = stack_pages;
265 	struct bio_vec stack_vec[UIO_FASTIOV], *bvec = stack_vec;
266 	size_t offset, bytes = iter->count;
267 	unsigned int direction, nr_bvecs;
268 	int ret, nr_vecs;
269 	bool copy;
270 
271 	if (bio_integrity(bio))
272 		return -EINVAL;
273 	if (bytes >> SECTOR_SHIFT > queue_max_hw_sectors(q))
274 		return -E2BIG;
275 
276 	if (bio_data_dir(bio) == READ)
277 		direction = ITER_DEST;
278 	else
279 		direction = ITER_SOURCE;
280 
281 	nr_vecs = iov_iter_npages(iter, BIO_MAX_VECS + 1);
282 	if (nr_vecs > BIO_MAX_VECS)
283 		return -E2BIG;
284 	if (nr_vecs > UIO_FASTIOV) {
285 		bvec = kcalloc(nr_vecs, sizeof(*bvec), GFP_KERNEL);
286 		if (!bvec)
287 			return -ENOMEM;
288 		pages = NULL;
289 	}
290 
291 	copy = !iov_iter_is_aligned(iter, align, align);
292 	ret = iov_iter_extract_pages(iter, &pages, bytes, nr_vecs, 0, &offset);
293 	if (unlikely(ret < 0))
294 		goto free_bvec;
295 
296 	nr_bvecs = bvec_from_pages(bvec, pages, nr_vecs, bytes, offset);
297 	if (pages != stack_pages)
298 		kvfree(pages);
299 	if (nr_bvecs > queue_max_integrity_segments(q))
300 		copy = true;
301 
302 	if (copy)
303 		ret = bio_integrity_copy_user(bio, bvec, nr_bvecs, bytes,
304 					      direction);
305 	else
306 		ret = bio_integrity_init_user(bio, bvec, nr_bvecs, bytes);
307 	if (ret)
308 		goto release_pages;
309 	if (bvec != stack_vec)
310 		kfree(bvec);
311 
312 	return 0;
313 
314 release_pages:
315 	bio_integrity_unpin_bvec(bvec, nr_bvecs);
316 free_bvec:
317 	if (bvec != stack_vec)
318 		kfree(bvec);
319 	return ret;
320 }
321 
322 static void bio_uio_meta_to_bip(struct bio *bio, struct uio_meta *meta)
323 {
324 	struct bio_integrity_payload *bip = bio_integrity(bio);
325 
326 	if (meta->flags & IO_INTEGRITY_CHK_GUARD)
327 		bip->bip_flags |= BIP_CHECK_GUARD;
328 	if (meta->flags & IO_INTEGRITY_CHK_APPTAG)
329 		bip->bip_flags |= BIP_CHECK_APPTAG;
330 	if (meta->flags & IO_INTEGRITY_CHK_REFTAG)
331 		bip->bip_flags |= BIP_CHECK_REFTAG;
332 
333 	bip->app_tag = meta->app_tag;
334 }
335 
336 int bio_integrity_map_iter(struct bio *bio, struct uio_meta *meta)
337 {
338 	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
339 	unsigned int integrity_bytes;
340 	int ret;
341 	struct iov_iter it;
342 
343 	if (!bi)
344 		return -EINVAL;
345 	/*
346 	 * original meta iterator can be bigger.
347 	 * process integrity info corresponding to current data buffer only.
348 	 */
349 	it = meta->iter;
350 	integrity_bytes = bio_integrity_bytes(bi, bio_sectors(bio));
351 	if (it.count < integrity_bytes)
352 		return -EINVAL;
353 
354 	/* should fit into two bytes */
355 	BUILD_BUG_ON(IO_INTEGRITY_VALID_FLAGS >= (1 << 16));
356 
357 	if (meta->flags && (meta->flags & ~IO_INTEGRITY_VALID_FLAGS))
358 		return -EINVAL;
359 
360 	it.count = integrity_bytes;
361 	ret = bio_integrity_map_user(bio, &it);
362 	if (!ret) {
363 		bio_uio_meta_to_bip(bio, meta);
364 		bip_set_seed(bio_integrity(bio), meta->seed);
365 		iov_iter_advance(&meta->iter, integrity_bytes);
366 		meta->seed += bio_integrity_intervals(bi, bio_sectors(bio));
367 	}
368 	return ret;
369 }
370 
371 /**
372  * bio_integrity_advance - Advance integrity vector
373  * @bio:	bio whose integrity vector to update
374  * @bytes_done:	number of data bytes that have been completed
375  *
376  * Description: This function calculates how many integrity bytes the
377  * number of completed data bytes correspond to and advances the
378  * integrity vector accordingly.
379  */
380 void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
381 {
382 	struct bio_integrity_payload *bip = bio_integrity(bio);
383 	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
384 	unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
385 
386 	bip->bip_iter.bi_sector += bio_integrity_intervals(bi, bytes_done >> 9);
387 	bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
388 }
389 
390 /**
391  * bio_integrity_trim - Trim integrity vector
392  * @bio:	bio whose integrity vector to update
393  *
394  * Description: Used to trim the integrity vector in a cloned bio.
395  */
396 void bio_integrity_trim(struct bio *bio)
397 {
398 	struct bio_integrity_payload *bip = bio_integrity(bio);
399 	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
400 
401 	bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
402 }
403 EXPORT_SYMBOL(bio_integrity_trim);
404 
405 /**
406  * bio_integrity_clone - Callback for cloning bios with integrity metadata
407  * @bio:	New bio
408  * @bio_src:	Original bio
409  * @gfp_mask:	Memory allocation mask
410  *
411  * Description:	Called to allocate a bip when cloning a bio
412  */
413 int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
414 			gfp_t gfp_mask)
415 {
416 	struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
417 	struct bio_integrity_payload *bip;
418 
419 	BUG_ON(bip_src == NULL);
420 
421 	bip = bio_integrity_alloc(bio, gfp_mask, 0);
422 	if (IS_ERR(bip))
423 		return PTR_ERR(bip);
424 
425 	bip->bip_vec = bip_src->bip_vec;
426 	bip->bip_iter = bip_src->bip_iter;
427 	bip->bip_flags = bip_src->bip_flags & BIP_CLONE_FLAGS;
428 	bip->app_tag = bip_src->app_tag;
429 
430 	return 0;
431 }
432