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