]>
Commit | Line | Data |
---|---|---|
37b3b4d6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5d90ab5a DC |
2 | /* |
3 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. | |
4 | * All Rights Reserved. | |
5d90ab5a | 5 | */ |
2cf10e4c | 6 | |
9c799827 | 7 | #include "libxfs_priv.h" |
b626fb59 | 8 | #include "xfs_fs.h" |
27846843 | 9 | #include "xfs_shared.h" |
b626fb59 DC |
10 | #include "xfs_format.h" |
11 | #include "xfs_log_format.h" | |
12 | #include "xfs_trans_resv.h" | |
13 | #include "xfs_mount.h" | |
14 | #include "xfs_inode.h" | |
15 | #include "xfs_trans.h" | |
81bf5e85 | 16 | #include "xfs_btree.h" |
b626fb59 DC |
17 | #include "xfs_bmap_btree.h" |
18 | #include "xfs_bmap.h" | |
19 | #include "xfs_trace.h" | |
9a5fc886 | 20 | #include "xfs_da_format.h" |
fc1d6454 DW |
21 | #include "xfs_da_btree.h" |
22 | #include "xfs_dir2_priv.h" | |
20e882d4 | 23 | #include "xfs_attr_leaf.h" |
5d90ab5a DC |
24 | |
25 | kmem_zone_t *xfs_ifork_zone; | |
26 | ||
219eec76 CH |
27 | void |
28 | xfs_init_local_fork( | |
29 | struct xfs_inode *ip, | |
30 | int whichfork, | |
31 | const void *data, | |
b6d2b93c | 32 | int64_t size) |
219eec76 CH |
33 | { |
34 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); | |
4cab415f CH |
35 | int mem_size = size, real_size = 0; |
36 | bool zero_terminate; | |
37 | ||
38 | /* | |
39 | * If we are using the local fork to store a symlink body we need to | |
40 | * zero-terminate it so that we can pass it back to the VFS directly. | |
41 | * Overallocate the in-memory fork by one for that and add a zero | |
42 | * to terminate it below. | |
43 | */ | |
44 | zero_terminate = S_ISLNK(VFS_I(ip)->i_mode); | |
45 | if (zero_terminate) | |
46 | mem_size++; | |
219eec76 | 47 | |
07fe4665 | 48 | if (size) { |
4cab415f | 49 | real_size = roundup(mem_size, 4); |
6cd1e6db | 50 | ifp->if_u1.if_data = kmem_alloc(real_size, KM_NOFS); |
219eec76 | 51 | memcpy(ifp->if_u1.if_data, data, size); |
4cab415f CH |
52 | if (zero_terminate) |
53 | ifp->if_u1.if_data[size] = '\0'; | |
07fe4665 CH |
54 | } else { |
55 | ifp->if_u1.if_data = NULL; | |
4cab415f | 56 | } |
219eec76 CH |
57 | |
58 | ifp->if_bytes = size; | |
219eec76 CH |
59 | ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT); |
60 | ifp->if_flags |= XFS_IFINLINE; | |
61 | } | |
62 | ||
5d90ab5a DC |
63 | /* |
64 | * The file is in-lined in the on-disk inode. | |
5d90ab5a DC |
65 | */ |
66 | STATIC int | |
67 | xfs_iformat_local( | |
68 | xfs_inode_t *ip, | |
69 | xfs_dinode_t *dip, | |
70 | int whichfork, | |
71 | int size) | |
72 | { | |
5d90ab5a DC |
73 | /* |
74 | * If the size is unreasonable, then something | |
75 | * is wrong and we just bail out rather than crash in | |
76 | * kmem_alloc() or memcpy() below. | |
77 | */ | |
78 | if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { | |
79 | xfs_warn(ip->i_mount, | |
4de63245 | 80 | "corrupt inode %Lu (bad size %d for local fork, size = %zd).", |
5d90ab5a DC |
81 | (unsigned long long) ip->i_ino, size, |
82 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)); | |
b02a2c9e DW |
83 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
84 | "xfs_iformat_local", dip, sizeof(*dip), | |
85 | __this_address); | |
12b53197 | 86 | return -EFSCORRUPTED; |
5d90ab5a | 87 | } |
219eec76 CH |
88 | |
89 | xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size); | |
5d90ab5a DC |
90 | return 0; |
91 | } | |
92 | ||
93 | /* | |
1f2a4478 | 94 | * The file consists of a set of extents all of which fit into the on-disk |
07fe4665 | 95 | * inode. |
5d90ab5a DC |
96 | */ |
97 | STATIC int | |
98 | xfs_iformat_extents( | |
1f2a4478 CH |
99 | struct xfs_inode *ip, |
100 | struct xfs_dinode *dip, | |
101 | int whichfork) | |
5d90ab5a | 102 | { |
1f2a4478 CH |
103 | struct xfs_mount *mp = ip->i_mount; |
104 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); | |
322fd804 | 105 | int state = xfs_bmap_fork_to_state(whichfork); |
1f2a4478 CH |
106 | int nex = XFS_DFORK_NEXTENTS(dip, whichfork); |
107 | int size = nex * sizeof(xfs_bmbt_rec_t); | |
9788e059 | 108 | struct xfs_iext_cursor icur; |
1f2a4478 | 109 | struct xfs_bmbt_rec *dp; |
b37d753d | 110 | struct xfs_bmbt_irec new; |
1f2a4478 | 111 | int i; |
5d90ab5a DC |
112 | |
113 | /* | |
1f2a4478 CH |
114 | * If the number of extents is unreasonable, then something is wrong and |
115 | * we just bail out rather than crash in kmem_alloc() or memcpy() below. | |
5d90ab5a | 116 | */ |
1f2a4478 | 117 | if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) { |
5d90ab5a DC |
118 | xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).", |
119 | (unsigned long long) ip->i_ino, nex); | |
b02a2c9e DW |
120 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
121 | "xfs_iformat_extents(1)", dip, sizeof(*dip), | |
122 | __this_address); | |
12b53197 | 123 | return -EFSCORRUPTED; |
5d90ab5a DC |
124 | } |
125 | ||
b37d753d CH |
126 | ifp->if_bytes = 0; |
127 | ifp->if_u1.if_root = NULL; | |
128 | ifp->if_height = 0; | |
5d90ab5a DC |
129 | if (size) { |
130 | dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork); | |
9788e059 CH |
131 | |
132 | xfs_iext_first(ifp, &icur); | |
5d90ab5a | 133 | for (i = 0; i < nex; i++, dp++) { |
0cf6a3a9 DW |
134 | xfs_failaddr_t fa; |
135 | ||
080f0c71 | 136 | xfs_bmbt_disk_get_all(dp, &new); |
0cf6a3a9 DW |
137 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
138 | if (fa) { | |
139 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, | |
140 | "xfs_iformat_extents(2)", | |
141 | dp, sizeof(*dp), fa); | |
1f2a4478 CH |
142 | return -EFSCORRUPTED; |
143 | } | |
9788e059 | 144 | |
26a75f67 | 145 | xfs_iext_insert(ip, &icur, &new, state); |
9788e059 CH |
146 | trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); |
147 | xfs_iext_next(ifp, &icur); | |
5d90ab5a | 148 | } |
5d90ab5a DC |
149 | } |
150 | ifp->if_flags |= XFS_IFEXTENTS; | |
151 | return 0; | |
152 | } | |
153 | ||
154 | /* | |
155 | * The file has too many extents to fit into | |
156 | * the inode, so they are in B-tree format. | |
157 | * Allocate a buffer for the root of the B-tree | |
158 | * and copy the root into it. The i_extents | |
159 | * field will remain NULL until all of the | |
160 | * extents are read in (when they are needed). | |
161 | */ | |
162 | STATIC int | |
163 | xfs_iformat_btree( | |
164 | xfs_inode_t *ip, | |
165 | xfs_dinode_t *dip, | |
166 | int whichfork) | |
167 | { | |
168 | struct xfs_mount *mp = ip->i_mount; | |
169 | xfs_bmdr_block_t *dfp; | |
e07055b8 | 170 | struct xfs_ifork *ifp; |
5d90ab5a DC |
171 | /* REFERENCED */ |
172 | int nrecs; | |
173 | int size; | |
81bf5e85 | 174 | int level; |
5d90ab5a DC |
175 | |
176 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
177 | dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork); | |
178 | size = XFS_BMAP_BROOT_SPACE(mp, dfp); | |
179 | nrecs = be16_to_cpu(dfp->bb_numrecs); | |
81bf5e85 | 180 | level = be16_to_cpu(dfp->bb_level); |
5d90ab5a DC |
181 | |
182 | /* | |
183 | * blow out if -- fork has less extents than can fit in | |
184 | * fork (fork shouldn't be a btree format), root btree | |
185 | * block has more records than can fit into the fork, | |
186 | * or the number of extents is greater than the number of | |
187 | * blocks. | |
188 | */ | |
87c472b7 | 189 | if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) || |
e4b963e9 | 190 | nrecs == 0 || |
5d90ab5a DC |
191 | XFS_BMDR_SPACE_CALC(nrecs) > |
192 | XFS_DFORK_SIZE(dip, mp, whichfork) || | |
87c472b7 | 193 | ifp->if_nextents > ip->i_d.di_nblocks) || |
81bf5e85 | 194 | level == 0 || level > XFS_BTREE_MAXLEVELS) { |
5d90ab5a DC |
195 | xfs_warn(mp, "corrupt inode %Lu (btree).", |
196 | (unsigned long long) ip->i_ino); | |
b02a2c9e DW |
197 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
198 | "xfs_iformat_btree", dfp, size, | |
199 | __this_address); | |
12b53197 | 200 | return -EFSCORRUPTED; |
5d90ab5a DC |
201 | } |
202 | ||
203 | ifp->if_broot_bytes = size; | |
6cd1e6db | 204 | ifp->if_broot = kmem_alloc(size, KM_NOFS); |
5d90ab5a DC |
205 | ASSERT(ifp->if_broot != NULL); |
206 | /* | |
207 | * Copy and convert from the on-disk structure | |
208 | * to the in-memory structure. | |
209 | */ | |
210 | xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork), | |
211 | ifp->if_broot, size); | |
212 | ifp->if_flags &= ~XFS_IFEXTENTS; | |
213 | ifp->if_flags |= XFS_IFBROOT; | |
214 | ||
b37d753d CH |
215 | ifp->if_bytes = 0; |
216 | ifp->if_u1.if_root = NULL; | |
217 | ifp->if_height = 0; | |
5d90ab5a DC |
218 | return 0; |
219 | } | |
220 | ||
07973a77 CH |
221 | int |
222 | xfs_iformat_data_fork( | |
223 | struct xfs_inode *ip, | |
224 | struct xfs_dinode *dip) | |
225 | { | |
226 | struct inode *inode = VFS_I(ip); | |
0815f7ab | 227 | int error; |
07973a77 | 228 | |
87c472b7 CH |
229 | /* |
230 | * Initialize the extent count early, as the per-format routines may | |
231 | * depend on it. | |
232 | */ | |
d967a68d | 233 | ip->i_df.if_format = dip->di_format; |
87c472b7 CH |
234 | ip->i_df.if_nextents = be32_to_cpu(dip->di_nextents); |
235 | ||
07973a77 CH |
236 | switch (inode->i_mode & S_IFMT) { |
237 | case S_IFIFO: | |
238 | case S_IFCHR: | |
239 | case S_IFBLK: | |
240 | case S_IFSOCK: | |
241 | ip->i_d.di_size = 0; | |
242 | inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip)); | |
243 | return 0; | |
244 | case S_IFREG: | |
245 | case S_IFLNK: | |
246 | case S_IFDIR: | |
d967a68d | 247 | switch (ip->i_df.if_format) { |
07973a77 | 248 | case XFS_DINODE_FMT_LOCAL: |
0815f7ab | 249 | error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, |
07973a77 | 250 | be64_to_cpu(dip->di_size)); |
0815f7ab CH |
251 | if (!error) |
252 | error = xfs_ifork_verify_local_data(ip); | |
253 | return error; | |
07973a77 CH |
254 | case XFS_DINODE_FMT_EXTENTS: |
255 | return xfs_iformat_extents(ip, dip, XFS_DATA_FORK); | |
256 | case XFS_DINODE_FMT_BTREE: | |
257 | return xfs_iformat_btree(ip, dip, XFS_DATA_FORK); | |
258 | default: | |
259 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, | |
260 | dip, sizeof(*dip), __this_address); | |
261 | return -EFSCORRUPTED; | |
262 | } | |
263 | break; | |
264 | default: | |
265 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip, | |
266 | sizeof(*dip), __this_address); | |
267 | return -EFSCORRUPTED; | |
268 | } | |
269 | } | |
270 | ||
271 | static uint16_t | |
272 | xfs_dfork_attr_shortform_size( | |
273 | struct xfs_dinode *dip) | |
274 | { | |
275 | struct xfs_attr_shortform *atp = | |
276 | (struct xfs_attr_shortform *)XFS_DFORK_APTR(dip); | |
277 | ||
278 | return be16_to_cpu(atp->hdr.totsize); | |
279 | } | |
280 | ||
281 | int | |
282 | xfs_iformat_attr_fork( | |
283 | struct xfs_inode *ip, | |
284 | struct xfs_dinode *dip) | |
285 | { | |
286 | int error = 0; | |
287 | ||
87c472b7 CH |
288 | /* |
289 | * Initialize the extent count early, as the per-format routines may | |
290 | * depend on it. | |
291 | */ | |
07973a77 | 292 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_NOFS); |
d967a68d CH |
293 | ip->i_afp->if_format = dip->di_aformat; |
294 | if (unlikely(ip->i_afp->if_format == 0)) /* pre IRIX 6.2 file system */ | |
295 | ip->i_afp->if_format = XFS_DINODE_FMT_EXTENTS; | |
87c472b7 CH |
296 | ip->i_afp->if_nextents = be16_to_cpu(dip->di_anextents); |
297 | ||
d967a68d | 298 | switch (ip->i_afp->if_format) { |
07973a77 CH |
299 | case XFS_DINODE_FMT_LOCAL: |
300 | error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, | |
301 | xfs_dfork_attr_shortform_size(dip)); | |
0815f7ab CH |
302 | if (!error) |
303 | error = xfs_ifork_verify_local_attr(ip); | |
07973a77 CH |
304 | break; |
305 | case XFS_DINODE_FMT_EXTENTS: | |
306 | error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK); | |
307 | break; | |
308 | case XFS_DINODE_FMT_BTREE: | |
309 | error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK); | |
310 | break; | |
311 | default: | |
312 | xfs_inode_verifier_error(ip, error, __func__, dip, | |
313 | sizeof(*dip), __this_address); | |
314 | error = -EFSCORRUPTED; | |
315 | break; | |
316 | } | |
317 | ||
318 | if (error) { | |
319 | kmem_cache_free(xfs_ifork_zone, ip->i_afp); | |
320 | ip->i_afp = NULL; | |
321 | } | |
322 | return error; | |
323 | } | |
324 | ||
5d90ab5a DC |
325 | /* |
326 | * Reallocate the space for if_broot based on the number of records | |
327 | * being added or deleted as indicated in rec_diff. Move the records | |
328 | * and pointers in if_broot to fit the new size. When shrinking this | |
329 | * will eliminate holes between the records and pointers created by | |
330 | * the caller. When growing this will create holes to be filled in | |
331 | * by the caller. | |
332 | * | |
333 | * The caller must not request to add more records than would fit in | |
334 | * the on-disk inode root. If the if_broot is currently NULL, then | |
e6d77a21 | 335 | * if we are adding records, one will be allocated. The caller must also |
5d90ab5a DC |
336 | * not request that the number of records go below zero, although |
337 | * it can go to zero. | |
338 | * | |
339 | * ip -- the inode whose if_broot area is changing | |
340 | * ext_diff -- the change in the number of records, positive or negative, | |
341 | * requested for the if_broot array. | |
342 | */ | |
343 | void | |
344 | xfs_iroot_realloc( | |
345 | xfs_inode_t *ip, | |
346 | int rec_diff, | |
347 | int whichfork) | |
348 | { | |
349 | struct xfs_mount *mp = ip->i_mount; | |
350 | int cur_max; | |
e07055b8 | 351 | struct xfs_ifork *ifp; |
5d90ab5a DC |
352 | struct xfs_btree_block *new_broot; |
353 | int new_max; | |
354 | size_t new_size; | |
355 | char *np; | |
356 | char *op; | |
357 | ||
358 | /* | |
359 | * Handle the degenerate case quietly. | |
360 | */ | |
361 | if (rec_diff == 0) { | |
362 | return; | |
363 | } | |
364 | ||
365 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
366 | if (rec_diff > 0) { | |
367 | /* | |
368 | * If there wasn't any memory allocated before, just | |
369 | * allocate it now and get out. | |
370 | */ | |
371 | if (ifp->if_broot_bytes == 0) { | |
372 | new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff); | |
6cd1e6db | 373 | ifp->if_broot = kmem_alloc(new_size, KM_NOFS); |
5d90ab5a DC |
374 | ifp->if_broot_bytes = (int)new_size; |
375 | return; | |
376 | } | |
377 | ||
378 | /* | |
379 | * If there is already an existing if_broot, then we need | |
380 | * to realloc() it and shift the pointers to their new | |
381 | * location. The records don't change location because | |
382 | * they are kept butted up against the btree block header. | |
383 | */ | |
384 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); | |
385 | new_max = cur_max + rec_diff; | |
386 | new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); | |
387 | ifp->if_broot = kmem_realloc(ifp->if_broot, new_size, | |
6cd1e6db | 388 | KM_NOFS); |
5d90ab5a DC |
389 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, |
390 | ifp->if_broot_bytes); | |
391 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, | |
392 | (int)new_size); | |
393 | ifp->if_broot_bytes = (int)new_size; | |
394 | ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= | |
395 | XFS_IFORK_SIZE(ip, whichfork)); | |
5a35bf2c | 396 | memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t)); |
5d90ab5a DC |
397 | return; |
398 | } | |
399 | ||
400 | /* | |
401 | * rec_diff is less than 0. In this case, we are shrinking the | |
402 | * if_broot buffer. It must already exist. If we go to zero | |
403 | * records, just get rid of the root and clear the status bit. | |
404 | */ | |
405 | ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0)); | |
406 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); | |
407 | new_max = cur_max + rec_diff; | |
408 | ASSERT(new_max >= 0); | |
409 | if (new_max > 0) | |
410 | new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); | |
411 | else | |
412 | new_size = 0; | |
413 | if (new_size > 0) { | |
6cd1e6db | 414 | new_broot = kmem_alloc(new_size, KM_NOFS); |
5d90ab5a DC |
415 | /* |
416 | * First copy over the btree block header. | |
417 | */ | |
418 | memcpy(new_broot, ifp->if_broot, | |
419 | XFS_BMBT_BLOCK_LEN(ip->i_mount)); | |
420 | } else { | |
421 | new_broot = NULL; | |
422 | ifp->if_flags &= ~XFS_IFBROOT; | |
423 | } | |
424 | ||
425 | /* | |
426 | * Only copy the records and pointers if there are any. | |
427 | */ | |
428 | if (new_max > 0) { | |
429 | /* | |
430 | * First copy the records. | |
431 | */ | |
432 | op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1); | |
433 | np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1); | |
434 | memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t)); | |
435 | ||
436 | /* | |
437 | * Then copy the pointers. | |
438 | */ | |
439 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, | |
440 | ifp->if_broot_bytes); | |
441 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1, | |
442 | (int)new_size); | |
5a35bf2c | 443 | memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t)); |
5d90ab5a DC |
444 | } |
445 | kmem_free(ifp->if_broot); | |
446 | ifp->if_broot = new_broot; | |
447 | ifp->if_broot_bytes = (int)new_size; | |
448 | if (ifp->if_broot) | |
449 | ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= | |
450 | XFS_IFORK_SIZE(ip, whichfork)); | |
451 | return; | |
452 | } | |
453 | ||
454 | ||
455 | /* | |
456 | * This is called when the amount of space needed for if_data | |
457 | * is increased or decreased. The change in size is indicated by | |
458 | * the number of bytes that need to be added or deleted in the | |
459 | * byte_diff parameter. | |
460 | * | |
461 | * If the amount of space needed has decreased below the size of the | |
462 | * inline buffer, then switch to using the inline buffer. Otherwise, | |
463 | * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer | |
464 | * to what is needed. | |
465 | * | |
466 | * ip -- the inode whose if_data area is changing | |
467 | * byte_diff -- the change in the number of bytes, positive or negative, | |
468 | * requested for the if_data array. | |
469 | */ | |
470 | void | |
471 | xfs_idata_realloc( | |
2282d1d1 | 472 | struct xfs_inode *ip, |
b6d2b93c | 473 | int64_t byte_diff, |
2282d1d1 | 474 | int whichfork) |
5d90ab5a | 475 | { |
2282d1d1 | 476 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
b6d2b93c | 477 | int64_t new_size = ifp->if_bytes + byte_diff; |
5d90ab5a | 478 | |
5d90ab5a | 479 | ASSERT(new_size >= 0); |
2282d1d1 CH |
480 | ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork)); |
481 | ||
482 | if (byte_diff == 0) | |
483 | return; | |
5d90ab5a DC |
484 | |
485 | if (new_size == 0) { | |
07fe4665 | 486 | kmem_free(ifp->if_u1.if_data); |
5d90ab5a | 487 | ifp->if_u1.if_data = NULL; |
2282d1d1 CH |
488 | ifp->if_bytes = 0; |
489 | return; | |
5d90ab5a | 490 | } |
2282d1d1 CH |
491 | |
492 | /* | |
493 | * For inline data, the underlying buffer must be a multiple of 4 bytes | |
494 | * in size so that it can be logged and stay on word boundaries. | |
495 | * We enforce that here. | |
496 | */ | |
497 | ifp->if_u1.if_data = kmem_realloc(ifp->if_u1.if_data, | |
6cd1e6db | 498 | roundup(new_size, 4), KM_NOFS); |
5d90ab5a | 499 | ifp->if_bytes = new_size; |
5d90ab5a DC |
500 | } |
501 | ||
502 | void | |
503 | xfs_idestroy_fork( | |
a87a40a2 | 504 | struct xfs_ifork *ifp) |
5d90ab5a | 505 | { |
5d90ab5a DC |
506 | if (ifp->if_broot != NULL) { |
507 | kmem_free(ifp->if_broot); | |
508 | ifp->if_broot = NULL; | |
509 | } | |
510 | ||
511 | /* | |
a87a40a2 CH |
512 | * If the format is local, then we can't have an extents array so just |
513 | * look for an inline data array. If we're not local then we may or may | |
514 | * not have an extents list, so check and free it up if we do. | |
5d90ab5a | 515 | */ |
d967a68d | 516 | if (ifp->if_format == XFS_DINODE_FMT_LOCAL) { |
a87a40a2 CH |
517 | kmem_free(ifp->if_u1.if_data); |
518 | ifp->if_u1.if_data = NULL; | |
519 | } else if (ifp->if_flags & XFS_IFEXTENTS) { | |
520 | if (ifp->if_height) | |
521 | xfs_iext_destroy(ifp); | |
5d90ab5a DC |
522 | } |
523 | } | |
524 | ||
525 | /* | |
ff105f75 | 526 | * Convert in-core extents to on-disk form |
5d90ab5a | 527 | * |
ff105f75 DC |
528 | * In the case of the data fork, the in-core and on-disk fork sizes can be |
529 | * different due to delayed allocation extents. We only copy on-disk extents | |
530 | * here, so callers must always use the physical fork size to determine the | |
531 | * size of the buffer passed to this routine. We will return the size actually | |
532 | * used. | |
5d90ab5a DC |
533 | */ |
534 | int | |
535 | xfs_iextents_copy( | |
f11e7501 CH |
536 | struct xfs_inode *ip, |
537 | struct xfs_bmbt_rec *dp, | |
5d90ab5a DC |
538 | int whichfork) |
539 | { | |
322fd804 | 540 | int state = xfs_bmap_fork_to_state(whichfork); |
f11e7501 | 541 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
9788e059 | 542 | struct xfs_iext_cursor icur; |
f11e7501 | 543 | struct xfs_bmbt_irec rec; |
b6d2b93c | 544 | int64_t copied = 0; |
5d90ab5a | 545 | |
f11e7501 | 546 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED)); |
5d90ab5a DC |
547 | ASSERT(ifp->if_bytes > 0); |
548 | ||
9788e059 | 549 | for_each_xfs_iext(ifp, &icur, &rec) { |
f11e7501 | 550 | if (isnullstartblock(rec.br_startblock)) |
5d90ab5a | 551 | continue; |
0cf6a3a9 | 552 | ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL); |
f11e7501 | 553 | xfs_bmbt_disk_set_all(dp, &rec); |
9788e059 | 554 | trace_xfs_write_extent(ip, &icur, state, _RET_IP_); |
f11e7501 | 555 | copied += sizeof(struct xfs_bmbt_rec); |
5d90ab5a | 556 | dp++; |
5d90ab5a | 557 | } |
5d90ab5a | 558 | |
f11e7501 CH |
559 | ASSERT(copied > 0); |
560 | ASSERT(copied <= ifp->if_bytes); | |
561 | return copied; | |
5d90ab5a DC |
562 | } |
563 | ||
564 | /* | |
565 | * Each of the following cases stores data into the same region | |
566 | * of the on-disk inode, so only one of them can be valid at | |
567 | * any given time. While it is possible to have conflicting formats | |
568 | * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is | |
569 | * in EXTENTS format, this can only happen when the fork has | |
570 | * changed formats after being modified but before being flushed. | |
571 | * In these cases, the format always takes precedence, because the | |
572 | * format indicates the current state of the fork. | |
573 | */ | |
d15188a1 | 574 | void |
5d90ab5a DC |
575 | xfs_iflush_fork( |
576 | xfs_inode_t *ip, | |
577 | xfs_dinode_t *dip, | |
ed8d09e1 | 578 | struct xfs_inode_log_item *iip, |
ff105f75 | 579 | int whichfork) |
5d90ab5a DC |
580 | { |
581 | char *cp; | |
e07055b8 | 582 | struct xfs_ifork *ifp; |
5d90ab5a DC |
583 | xfs_mount_t *mp; |
584 | static const short brootflag[2] = | |
585 | { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT }; | |
586 | static const short dataflag[2] = | |
587 | { XFS_ILOG_DDATA, XFS_ILOG_ADATA }; | |
588 | static const short extflag[2] = | |
589 | { XFS_ILOG_DEXT, XFS_ILOG_AEXT }; | |
590 | ||
591 | if (!iip) | |
d15188a1 | 592 | return; |
5d90ab5a DC |
593 | ifp = XFS_IFORK_PTR(ip, whichfork); |
594 | /* | |
595 | * This can happen if we gave up in iformat in an error path, | |
596 | * for the attribute fork. | |
597 | */ | |
598 | if (!ifp) { | |
599 | ASSERT(whichfork == XFS_ATTR_FORK); | |
d15188a1 | 600 | return; |
5d90ab5a DC |
601 | } |
602 | cp = XFS_DFORK_PTR(dip, whichfork); | |
603 | mp = ip->i_mount; | |
d967a68d | 604 | switch (ifp->if_format) { |
5d90ab5a DC |
605 | case XFS_DINODE_FMT_LOCAL: |
606 | if ((iip->ili_fields & dataflag[whichfork]) && | |
607 | (ifp->if_bytes > 0)) { | |
608 | ASSERT(ifp->if_u1.if_data != NULL); | |
609 | ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); | |
610 | memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes); | |
611 | } | |
612 | break; | |
613 | ||
614 | case XFS_DINODE_FMT_EXTENTS: | |
615 | ASSERT((ifp->if_flags & XFS_IFEXTENTS) || | |
616 | !(iip->ili_fields & extflag[whichfork])); | |
617 | if ((iip->ili_fields & extflag[whichfork]) && | |
618 | (ifp->if_bytes > 0)) { | |
87c472b7 | 619 | ASSERT(ifp->if_nextents > 0); |
5d90ab5a DC |
620 | (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp, |
621 | whichfork); | |
622 | } | |
623 | break; | |
624 | ||
625 | case XFS_DINODE_FMT_BTREE: | |
626 | if ((iip->ili_fields & brootflag[whichfork]) && | |
627 | (ifp->if_broot_bytes > 0)) { | |
628 | ASSERT(ifp->if_broot != NULL); | |
629 | ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= | |
630 | XFS_IFORK_SIZE(ip, whichfork)); | |
631 | xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes, | |
632 | (xfs_bmdr_block_t *)cp, | |
633 | XFS_DFORK_SIZE(dip, mp, whichfork)); | |
634 | } | |
635 | break; | |
636 | ||
637 | case XFS_DINODE_FMT_DEV: | |
638 | if (iip->ili_fields & XFS_ILOG_DEV) { | |
639 | ASSERT(whichfork == XFS_DATA_FORK); | |
dc9b1f58 CH |
640 | xfs_dinode_put_rdev(dip, |
641 | linux_to_xfs_dev_t(VFS_I(ip)->i_rdev)); | |
5d90ab5a DC |
642 | } |
643 | break; | |
644 | ||
5d90ab5a DC |
645 | default: |
646 | ASSERT(0); | |
647 | break; | |
648 | } | |
649 | } | |
650 | ||
cb8a004a DW |
651 | /* Convert bmap state flags to an inode fork. */ |
652 | struct xfs_ifork * | |
653 | xfs_iext_state_to_fork( | |
654 | struct xfs_inode *ip, | |
655 | int state) | |
656 | { | |
657 | if (state & BMAP_COWFORK) | |
658 | return ip->i_cowfp; | |
659 | else if (state & BMAP_ATTRFORK) | |
660 | return ip->i_afp; | |
661 | return &ip->i_df; | |
662 | } | |
663 | ||
cb8a004a DW |
664 | /* |
665 | * Initialize an inode's copy-on-write fork. | |
666 | */ | |
667 | void | |
668 | xfs_ifork_init_cow( | |
669 | struct xfs_inode *ip) | |
670 | { | |
671 | if (ip->i_cowfp) | |
672 | return; | |
673 | ||
674 | ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone, | |
6cd1e6db | 675 | KM_NOFS); |
cb8a004a | 676 | ip->i_cowfp->if_flags = XFS_IFEXTENTS; |
d967a68d | 677 | ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS; |
cb8a004a | 678 | } |
20e882d4 | 679 | |
20e882d4 | 680 | /* Verify the inline contents of the data fork of an inode. */ |
318d12fd CH |
681 | int |
682 | xfs_ifork_verify_local_data( | |
1fecabf9 | 683 | struct xfs_inode *ip) |
20e882d4 | 684 | { |
318d12fd | 685 | xfs_failaddr_t fa = NULL; |
20e882d4 | 686 | |
20e882d4 DW |
687 | switch (VFS_I(ip)->i_mode & S_IFMT) { |
688 | case S_IFDIR: | |
318d12fd CH |
689 | fa = xfs_dir2_sf_verify(ip); |
690 | break; | |
20e882d4 | 691 | case S_IFLNK: |
318d12fd CH |
692 | fa = xfs_symlink_shortform_verify(ip); |
693 | break; | |
20e882d4 | 694 | default: |
318d12fd | 695 | break; |
20e882d4 | 696 | } |
318d12fd CH |
697 | |
698 | if (fa) { | |
699 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork", | |
700 | ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa); | |
701 | return -EFSCORRUPTED; | |
702 | } | |
703 | ||
704 | return 0; | |
20e882d4 DW |
705 | } |
706 | ||
707 | /* Verify the inline contents of the attr fork of an inode. */ | |
318d12fd CH |
708 | int |
709 | xfs_ifork_verify_local_attr( | |
1fecabf9 | 710 | struct xfs_inode *ip) |
20e882d4 | 711 | { |
318d12fd CH |
712 | struct xfs_ifork *ifp = ip->i_afp; |
713 | xfs_failaddr_t fa; | |
714 | ||
715 | if (!ifp) | |
716 | fa = __this_address; | |
717 | else | |
718 | fa = xfs_attr_shortform_verify(ip); | |
719 | ||
720 | if (fa) { | |
721 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork", | |
722 | ifp ? ifp->if_u1.if_data : NULL, | |
723 | ifp ? ifp->if_bytes : 0, fa); | |
724 | return -EFSCORRUPTED; | |
725 | } | |
726 | ||
727 | return 0; | |
20e882d4 | 728 | } |