Debian Codegraph @

xfield.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2019 Ernesto A. Fernández <[email protected]>
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include "apfs.h"
9
10 /**
11 * apfs_find_xfield - Find an extended field value in an inode or dentry record
12 * @xfields: pointer to the on-disk xfield collection for the record
13 * @len: length of the collection
14 * @xtype: type of the xfield to retrieve
15 * @xval: on return, a pointer to the wanted on-disk xfield value
16 *
17 * Returns the length of @xval on success, or 0 if no matching xfield was found;
18 * the caller must check that the expected structures fit before casting @xval.
19 */
20 int apfs_find_xfield(u8 *xfields, int len, u8 xtype, char **xval)
21 {
22 struct apfs_xf_blob *blob;
23 struct apfs_x_field *xfield;
24 int count;
25 int rest = len;
26 int i;
27
28 if (!len)
29 return 0; /* No xfield data */
30
31 rest -= sizeof(*blob);
32 if (rest < 0)
33 return 0; /* Corruption */
34 blob = (struct apfs_xf_blob *)xfields;
35
36 count = le16_to_cpu(blob->xf_num_exts);
37 rest -= count * sizeof(*xfield);
38 if (rest < 0)
39 return 0; /* Corruption */
40 xfield = (struct apfs_x_field *)blob->xf_data;
41
42 for (i = 0; i < count; ++i) {
43 int xlen;
44
45 /* Attribute length is padded to a multiple of 8 */
46 xlen = round_up(le16_to_cpu(xfield[i].x_size), 8);
47 if (xlen > rest)
48 return 0; /* Corruption */
49
50 if (xfield[i].x_type == xtype) {
51 *xval = (char *)xfields + len - rest;
52 return xlen;
53 }
54 rest -= xlen;
55 }
56 return 0;
57 }
58
59 /**
60 * apfs_init_xfields - Set an empty collection of xfields in a buffer
61 * @buffer: buffer to hold the xfields
62 * @buflen: length of the buffer; should be enough to fit an xfield blob
63 *
64 * Returns 0 on success, or -1 if the buffer isn't long enough.
65 */
66 int apfs_init_xfields(u8 *buffer, int buflen)
67 {
68 struct apfs_xf_blob *blob;
69
70 if (buflen < sizeof(*blob))
71 return -1;
72 blob = (struct apfs_xf_blob *)buffer;
73
74 blob->xf_num_exts = 0;
75 blob->xf_used_data = 0;
76 return 0;
77 }
78
79 /**
80 * apfs_insert_xfield - Add a new xfield to an in-memory collection
81 * @buffer: buffer holding the collection of xfields
82 * @buflen: length of the buffer; should be enough to fit the new xfield
83 * @xkey: metadata for the new xfield
84 * @xval: value for the new xfield
85 *
86 * Returns the new length of the collection, or 0 if it the allocation would
87 * overflow @buffer.
88 */
89 int apfs_insert_xfield(u8 *buffer, int buflen, const struct apfs_x_field *xkey,
90 const void *xval)
91 {
92 struct apfs_xf_blob *blob;
93 struct apfs_x_field *curr_xkey;
94 void *curr_xval;
95 int count;
96 int rest = buflen;
97 u16 used_data;
98 int xlen, padded_xlen;
99 int meta_len, total_len;
100 int i;
101
102 xlen = le16_to_cpu(xkey->x_size);
103 padded_xlen = round_up(xlen, 8);
104
105 if (!buflen)
106 return 0;
107
108 rest -= sizeof(*blob);
109 if (rest < 0)
110 return 0;
111 blob = (struct apfs_xf_blob *)buffer;
112 used_data = le16_to_cpu(blob->xf_used_data);
113
114 count = le16_to_cpu(blob->xf_num_exts);
115 rest -= count * sizeof(*curr_xkey);
116 if (rest < 0)
117 return 0;
118 meta_len = buflen - rest;
119 curr_xkey = (struct apfs_x_field *)blob->xf_data;
120
121 for (i = 0; i < count; ++i, ++curr_xkey) {
122 int curr_xlen;
123
124 /* Attribute length is padded to a multiple of 8 */
125 curr_xlen = round_up(le16_to_cpu(curr_xkey->x_size), 8);
126 if (curr_xlen > rest)
127 return 0;
128 if (curr_xkey->x_type != xkey->x_type) {
129 rest -= curr_xlen;
130 continue;
131 }
132
133 /* The xfield already exists, so just resize it and set it */
134 memcpy(curr_xkey, xkey, sizeof(*curr_xkey));
135 if (padded_xlen > rest)
136 return 0;
137 curr_xval = buffer + buflen - rest;
138 rest -= max(padded_xlen, curr_xlen);
139 memmove(curr_xval + padded_xlen, curr_xval + curr_xlen, rest);
140 memcpy(curr_xval, xval, xlen);
141 memset(curr_xval + xlen, 0, padded_xlen - xlen);
142 used_data += padded_xlen - curr_xlen;
143
144 goto done;
145 }
146
147 /* Create a metadata entry for the new xfield */
148 rest -= sizeof(*curr_xkey);
149 if (rest < 0)
150 return 0;
151 meta_len += sizeof(*curr_xkey);
152 memmove(curr_xkey + 1, curr_xkey, buflen - meta_len);
153 memcpy(curr_xkey, xkey, sizeof(*curr_xkey));
154 ++count;
155
156 /* Now set the xfield value */
157 if (padded_xlen > rest)
158 return 0;
159 curr_xval = buffer + buflen - rest;
160 memcpy(curr_xval, xval, xlen);
161 memset(curr_xval + xlen, 0, padded_xlen - xlen);
162 used_data += padded_xlen;
163
164 done:
165 total_len = used_data + meta_len;
166 if (total_len > buflen)
167 return 0;
168 blob->xf_num_exts = cpu_to_le16(count);
169 blob->xf_used_data = cpu_to_le16(used_data);
170 return total_len;
171 }