]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/bvec.h
net: rtnetlink: validate IFLA_MTU attribute in rtnl_create_link()
[mirror_ubuntu-bionic-kernel.git] / include / linux / bvec.h
CommitLineData
8fc55455
ML
1/*
2 * bvec iterator
3 *
4 * Copyright (C) 2001 Ming Lei <ming.lei@canonical.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public Licens
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
19 */
20#ifndef __LINUX_BVEC_ITER_H
21#define __LINUX_BVEC_ITER_H
22
0781e79e
ML
23#include <linux/kernel.h>
24#include <linux/bug.h>
b1fb2c52 25#include <linux/errno.h>
0781e79e
ML
26
27/*
28 * was unsigned short, but we might as well be ready for > 64kB I/O pages
29 */
30struct bio_vec {
31 struct page *bv_page;
32 unsigned int bv_len;
33 unsigned int bv_offset;
34};
35
36struct bvec_iter {
37 sector_t bi_sector; /* device address in 512 byte
38 sectors */
39 unsigned int bi_size; /* residual I/O count */
40
41 unsigned int bi_idx; /* current index into bvl_vec */
42
f9df1cd9
DM
43 unsigned int bi_done; /* number of bytes completed */
44
0781e79e
ML
45 unsigned int bi_bvec_done; /* number of bytes completed in
46 current bvec */
47};
8fc55455
ML
48
49/*
50 * various member access, note that bio_data should of course not be used
51 * on highmem page vectors
52 */
53#define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
54
55#define bvec_iter_page(bvec, iter) \
56 (__bvec_iter_bvec((bvec), (iter))->bv_page)
57
58#define bvec_iter_len(bvec, iter) \
59 min((iter).bi_size, \
60 __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
61
62#define bvec_iter_offset(bvec, iter) \
63 (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
64
65#define bvec_iter_bvec(bvec, iter) \
66((struct bio_vec) { \
67 .bv_page = bvec_iter_page((bvec), (iter)), \
68 .bv_len = bvec_iter_len((bvec), (iter)), \
69 .bv_offset = bvec_iter_offset((bvec), (iter)), \
70})
71
b1fb2c52
DM
72static inline bool bvec_iter_advance(const struct bio_vec *bv,
73 struct bvec_iter *iter, unsigned bytes)
8fc55455 74{
b1fb2c52
DM
75 if (WARN_ONCE(bytes > iter->bi_size,
76 "Attempted to advance past end of bvec iter\n")) {
77 iter->bi_size = 0;
78 return false;
79 }
8fc55455
ML
80
81 while (bytes) {
1ea049b2
JB
82 unsigned iter_len = bvec_iter_len(bv, *iter);
83 unsigned len = min(bytes, iter_len);
8fc55455
ML
84
85 bytes -= len;
86 iter->bi_size -= len;
87 iter->bi_bvec_done += len;
f9df1cd9 88 iter->bi_done += len;
8fc55455
ML
89
90 if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) {
91 iter->bi_bvec_done = 0;
92 iter->bi_idx++;
93 }
94 }
b1fb2c52 95 return true;
8fc55455
ML
96}
97
f9df1cd9
DM
98static inline bool bvec_iter_rewind(const struct bio_vec *bv,
99 struct bvec_iter *iter,
100 unsigned int bytes)
101{
102 while (bytes) {
103 unsigned len = min(bytes, iter->bi_bvec_done);
104
105 if (iter->bi_bvec_done == 0) {
106 if (WARN_ONCE(iter->bi_idx == 0,
107 "Attempted to rewind iter beyond "
108 "bvec's boundaries\n")) {
109 return false;
110 }
111 iter->bi_idx--;
112 iter->bi_bvec_done = __bvec_iter_bvec(bv, *iter)->bv_len;
113 continue;
114 }
115 bytes -= len;
116 iter->bi_size += len;
117 iter->bi_bvec_done -= len;
118 }
119 return true;
120}
121
8fc55455
ML
122#define for_each_bvec(bvl, bio_vec, iter, start) \
123 for (iter = (start); \
124 (iter).bi_size && \
125 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
126 bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
127
128#endif /* __LINUX_BVEC_ITER_H */