]>
Commit | Line | Data |
---|---|---|
5c4d97d0 DC |
1 | /* |
2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. | |
3 | * All Rights Reserved. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it would be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | #include <linux/log2.h> | |
19 | ||
20 | #include "xfs.h" | |
21 | #include "xfs_fs.h" | |
22 | #include "xfs_format.h" | |
239880ef DC |
23 | #include "xfs_log_format.h" |
24 | #include "xfs_trans_resv.h" | |
5c4d97d0 | 25 | #include "xfs_mount.h" |
5c4d97d0 | 26 | #include "xfs_inode.h" |
239880ef | 27 | #include "xfs_trans.h" |
5c4d97d0 | 28 | #include "xfs_inode_item.h" |
bcd84e94 | 29 | #include "xfs_btree.h" |
a4fbe6ab | 30 | #include "xfs_bmap_btree.h" |
5c4d97d0 DC |
31 | #include "xfs_bmap.h" |
32 | #include "xfs_error.h" | |
5c4d97d0 | 33 | #include "xfs_trace.h" |
a4fbe6ab | 34 | #include "xfs_attr_sf.h" |
244efeaf | 35 | #include "xfs_da_format.h" |
5c4d97d0 DC |
36 | |
37 | kmem_zone_t *xfs_ifork_zone; | |
38 | ||
39 | STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int); | |
40 | STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int); | |
41 | STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int); | |
42 | ||
43 | #ifdef DEBUG | |
44 | /* | |
45 | * Make sure that the extents in the given memory buffer | |
46 | * are valid. | |
47 | */ | |
48 | void | |
49 | xfs_validate_extents( | |
50 | xfs_ifork_t *ifp, | |
51 | int nrecs, | |
52 | xfs_exntfmt_t fmt) | |
53 | { | |
54 | xfs_bmbt_irec_t irec; | |
55 | xfs_bmbt_rec_host_t rec; | |
56 | int i; | |
57 | ||
58 | for (i = 0; i < nrecs; i++) { | |
59 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); | |
60 | rec.l0 = get_unaligned(&ep->l0); | |
61 | rec.l1 = get_unaligned(&ep->l1); | |
62 | xfs_bmbt_get_all(&rec, &irec); | |
63 | if (fmt == XFS_EXTFMT_NOSTATE) | |
64 | ASSERT(irec.br_state == XFS_EXT_NORM); | |
65 | } | |
66 | } | |
67 | #else /* DEBUG */ | |
68 | #define xfs_validate_extents(ifp, nrecs, fmt) | |
69 | #endif /* DEBUG */ | |
70 | ||
71 | ||
72 | /* | |
73 | * Move inode type and inode format specific information from the | |
74 | * on-disk inode to the in-core inode. For fifos, devs, and sockets | |
75 | * this means set if_rdev to the proper value. For files, directories, | |
76 | * and symlinks this means to bring in the in-line data or extent | |
77 | * pointers. For a file in B-tree format, only the root is immediately | |
78 | * brought in-core. The rest will be in-lined in if_extents when it | |
79 | * is first referenced (see xfs_iread_extents()). | |
80 | */ | |
81 | int | |
82 | xfs_iformat_fork( | |
83 | xfs_inode_t *ip, | |
84 | xfs_dinode_t *dip) | |
85 | { | |
86 | xfs_attr_shortform_t *atp; | |
87 | int size; | |
88 | int error = 0; | |
89 | xfs_fsize_t di_size; | |
90 | ||
91 | if (unlikely(be32_to_cpu(dip->di_nextents) + | |
92 | be16_to_cpu(dip->di_anextents) > | |
93 | be64_to_cpu(dip->di_nblocks))) { | |
94 | xfs_warn(ip->i_mount, | |
95 | "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.", | |
96 | (unsigned long long)ip->i_ino, | |
97 | (int)(be32_to_cpu(dip->di_nextents) + | |
98 | be16_to_cpu(dip->di_anextents)), | |
99 | (unsigned long long) | |
100 | be64_to_cpu(dip->di_nblocks)); | |
101 | XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW, | |
102 | ip->i_mount, dip); | |
2451337d | 103 | return -EFSCORRUPTED; |
5c4d97d0 DC |
104 | } |
105 | ||
106 | if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) { | |
107 | xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.", | |
108 | (unsigned long long)ip->i_ino, | |
109 | dip->di_forkoff); | |
110 | XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW, | |
111 | ip->i_mount, dip); | |
2451337d | 112 | return -EFSCORRUPTED; |
5c4d97d0 DC |
113 | } |
114 | ||
115 | if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && | |
116 | !ip->i_mount->m_rtdev_targp)) { | |
117 | xfs_warn(ip->i_mount, | |
118 | "corrupt dinode %Lu, has realtime flag set.", | |
119 | ip->i_ino); | |
120 | XFS_CORRUPTION_ERROR("xfs_iformat(realtime)", | |
121 | XFS_ERRLEVEL_LOW, ip->i_mount, dip); | |
2451337d | 122 | return -EFSCORRUPTED; |
5c4d97d0 DC |
123 | } |
124 | ||
11715a21 DW |
125 | if (unlikely(xfs_is_reflink_inode(ip) && |
126 | (VFS_I(ip)->i_mode & S_IFMT) != S_IFREG)) { | |
127 | xfs_warn(ip->i_mount, | |
128 | "corrupt dinode %llu, wrong file type for reflink.", | |
129 | ip->i_ino); | |
130 | XFS_CORRUPTION_ERROR("xfs_iformat(reflink)", | |
131 | XFS_ERRLEVEL_LOW, ip->i_mount, dip); | |
132 | return -EFSCORRUPTED; | |
133 | } | |
134 | ||
135 | if (unlikely(xfs_is_reflink_inode(ip) && | |
136 | (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) { | |
137 | xfs_warn(ip->i_mount, | |
138 | "corrupt dinode %llu, has reflink+realtime flag set.", | |
139 | ip->i_ino); | |
140 | XFS_CORRUPTION_ERROR("xfs_iformat(reflink)", | |
141 | XFS_ERRLEVEL_LOW, ip->i_mount, dip); | |
142 | return -EFSCORRUPTED; | |
143 | } | |
144 | ||
c19b3b05 | 145 | switch (VFS_I(ip)->i_mode & S_IFMT) { |
5c4d97d0 DC |
146 | case S_IFIFO: |
147 | case S_IFCHR: | |
148 | case S_IFBLK: | |
149 | case S_IFSOCK: | |
150 | if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) { | |
151 | XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW, | |
152 | ip->i_mount, dip); | |
2451337d | 153 | return -EFSCORRUPTED; |
5c4d97d0 DC |
154 | } |
155 | ip->i_d.di_size = 0; | |
156 | ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip); | |
157 | break; | |
158 | ||
159 | case S_IFREG: | |
160 | case S_IFLNK: | |
161 | case S_IFDIR: | |
162 | switch (dip->di_format) { | |
163 | case XFS_DINODE_FMT_LOCAL: | |
164 | /* | |
165 | * no local regular files yet | |
166 | */ | |
167 | if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) { | |
168 | xfs_warn(ip->i_mount, | |
169 | "corrupt inode %Lu (local format for regular file).", | |
170 | (unsigned long long) ip->i_ino); | |
171 | XFS_CORRUPTION_ERROR("xfs_iformat(4)", | |
172 | XFS_ERRLEVEL_LOW, | |
173 | ip->i_mount, dip); | |
2451337d | 174 | return -EFSCORRUPTED; |
5c4d97d0 DC |
175 | } |
176 | ||
177 | di_size = be64_to_cpu(dip->di_size); | |
0d0ab120 DC |
178 | if (unlikely(di_size < 0 || |
179 | di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) { | |
5c4d97d0 DC |
180 | xfs_warn(ip->i_mount, |
181 | "corrupt inode %Lu (bad size %Ld for local inode).", | |
182 | (unsigned long long) ip->i_ino, | |
183 | (long long) di_size); | |
184 | XFS_CORRUPTION_ERROR("xfs_iformat(5)", | |
185 | XFS_ERRLEVEL_LOW, | |
186 | ip->i_mount, dip); | |
2451337d | 187 | return -EFSCORRUPTED; |
5c4d97d0 DC |
188 | } |
189 | ||
190 | size = (int)di_size; | |
191 | error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size); | |
192 | break; | |
193 | case XFS_DINODE_FMT_EXTENTS: | |
194 | error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK); | |
195 | break; | |
196 | case XFS_DINODE_FMT_BTREE: | |
197 | error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK); | |
198 | break; | |
199 | default: | |
200 | XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW, | |
201 | ip->i_mount); | |
2451337d | 202 | return -EFSCORRUPTED; |
5c4d97d0 DC |
203 | } |
204 | break; | |
205 | ||
206 | default: | |
207 | XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount); | |
2451337d | 208 | return -EFSCORRUPTED; |
5c4d97d0 | 209 | } |
3993baeb | 210 | if (error) |
5c4d97d0 | 211 | return error; |
3993baeb DW |
212 | |
213 | if (xfs_is_reflink_inode(ip)) { | |
214 | ASSERT(ip->i_cowfp == NULL); | |
215 | xfs_ifork_init_cow(ip); | |
5c4d97d0 | 216 | } |
3993baeb | 217 | |
5c4d97d0 DC |
218 | if (!XFS_DFORK_Q(dip)) |
219 | return 0; | |
220 | ||
221 | ASSERT(ip->i_afp == NULL); | |
222 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS); | |
223 | ||
224 | switch (dip->di_aformat) { | |
225 | case XFS_DINODE_FMT_LOCAL: | |
226 | atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip); | |
227 | size = be16_to_cpu(atp->hdr.totsize); | |
228 | ||
229 | if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) { | |
230 | xfs_warn(ip->i_mount, | |
231 | "corrupt inode %Lu (bad attr fork size %Ld).", | |
232 | (unsigned long long) ip->i_ino, | |
233 | (long long) size); | |
234 | XFS_CORRUPTION_ERROR("xfs_iformat(8)", | |
235 | XFS_ERRLEVEL_LOW, | |
236 | ip->i_mount, dip); | |
11715a21 DW |
237 | error = -EFSCORRUPTED; |
238 | break; | |
5c4d97d0 DC |
239 | } |
240 | ||
241 | error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size); | |
242 | break; | |
243 | case XFS_DINODE_FMT_EXTENTS: | |
244 | error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK); | |
245 | break; | |
246 | case XFS_DINODE_FMT_BTREE: | |
247 | error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK); | |
248 | break; | |
249 | default: | |
2451337d | 250 | error = -EFSCORRUPTED; |
5c4d97d0 DC |
251 | break; |
252 | } | |
253 | if (error) { | |
254 | kmem_zone_free(xfs_ifork_zone, ip->i_afp); | |
255 | ip->i_afp = NULL; | |
3993baeb DW |
256 | if (ip->i_cowfp) |
257 | kmem_zone_free(xfs_ifork_zone, ip->i_cowfp); | |
258 | ip->i_cowfp = NULL; | |
5c4d97d0 DC |
259 | xfs_idestroy_fork(ip, XFS_DATA_FORK); |
260 | } | |
261 | return error; | |
262 | } | |
263 | ||
143f4aed CH |
264 | void |
265 | xfs_init_local_fork( | |
266 | struct xfs_inode *ip, | |
267 | int whichfork, | |
268 | const void *data, | |
269 | int size) | |
270 | { | |
271 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); | |
30ee052e CH |
272 | int mem_size = size, real_size = 0; |
273 | bool zero_terminate; | |
274 | ||
275 | /* | |
276 | * If we are using the local fork to store a symlink body we need to | |
277 | * zero-terminate it so that we can pass it back to the VFS directly. | |
278 | * Overallocate the in-memory fork by one for that and add a zero | |
279 | * to terminate it below. | |
280 | */ | |
281 | zero_terminate = S_ISLNK(VFS_I(ip)->i_mode); | |
282 | if (zero_terminate) | |
283 | mem_size++; | |
143f4aed CH |
284 | |
285 | if (size == 0) | |
286 | ifp->if_u1.if_data = NULL; | |
30ee052e | 287 | else if (mem_size <= sizeof(ifp->if_u2.if_inline_data)) |
143f4aed CH |
288 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; |
289 | else { | |
30ee052e | 290 | real_size = roundup(mem_size, 4); |
143f4aed CH |
291 | ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS); |
292 | } | |
293 | ||
30ee052e | 294 | if (size) { |
143f4aed | 295 | memcpy(ifp->if_u1.if_data, data, size); |
30ee052e CH |
296 | if (zero_terminate) |
297 | ifp->if_u1.if_data[size] = '\0'; | |
298 | } | |
143f4aed CH |
299 | |
300 | ifp->if_bytes = size; | |
301 | ifp->if_real_bytes = real_size; | |
302 | ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT); | |
303 | ifp->if_flags |= XFS_IFINLINE; | |
304 | } | |
305 | ||
5c4d97d0 DC |
306 | /* |
307 | * The file is in-lined in the on-disk inode. | |
308 | * If it fits into if_inline_data, then copy | |
309 | * it there, otherwise allocate a buffer for it | |
310 | * and copy the data there. Either way, set | |
311 | * if_data to point at the data. | |
312 | * If we allocate a buffer for the data, make | |
313 | * sure that its size is a multiple of 4 and | |
314 | * record the real size in i_real_bytes. | |
315 | */ | |
316 | STATIC int | |
317 | xfs_iformat_local( | |
318 | xfs_inode_t *ip, | |
319 | xfs_dinode_t *dip, | |
320 | int whichfork, | |
321 | int size) | |
322 | { | |
5c4d97d0 DC |
323 | |
324 | /* | |
325 | * If the size is unreasonable, then something | |
326 | * is wrong and we just bail out rather than crash in | |
327 | * kmem_alloc() or memcpy() below. | |
328 | */ | |
329 | if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { | |
330 | xfs_warn(ip->i_mount, | |
331 | "corrupt inode %Lu (bad size %d for local fork, size = %d).", | |
332 | (unsigned long long) ip->i_ino, size, | |
333 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)); | |
334 | XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW, | |
335 | ip->i_mount, dip); | |
2451337d | 336 | return -EFSCORRUPTED; |
5c4d97d0 | 337 | } |
143f4aed CH |
338 | |
339 | xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size); | |
5c4d97d0 DC |
340 | return 0; |
341 | } | |
342 | ||
343 | /* | |
344 | * The file consists of a set of extents all | |
345 | * of which fit into the on-disk inode. | |
346 | * If there are few enough extents to fit into | |
347 | * the if_inline_ext, then copy them there. | |
348 | * Otherwise allocate a buffer for them and copy | |
349 | * them into it. Either way, set if_extents | |
350 | * to point at the extents. | |
351 | */ | |
352 | STATIC int | |
353 | xfs_iformat_extents( | |
354 | xfs_inode_t *ip, | |
355 | xfs_dinode_t *dip, | |
356 | int whichfork) | |
357 | { | |
358 | xfs_bmbt_rec_t *dp; | |
359 | xfs_ifork_t *ifp; | |
360 | int nex; | |
361 | int size; | |
362 | int i; | |
363 | ||
364 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
365 | nex = XFS_DFORK_NEXTENTS(dip, whichfork); | |
366 | size = nex * (uint)sizeof(xfs_bmbt_rec_t); | |
367 | ||
368 | /* | |
369 | * If the number of extents is unreasonable, then something | |
370 | * is wrong and we just bail out rather than crash in | |
371 | * kmem_alloc() or memcpy() below. | |
372 | */ | |
373 | if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { | |
374 | xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).", | |
375 | (unsigned long long) ip->i_ino, nex); | |
376 | XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW, | |
377 | ip->i_mount, dip); | |
2451337d | 378 | return -EFSCORRUPTED; |
5c4d97d0 DC |
379 | } |
380 | ||
381 | ifp->if_real_bytes = 0; | |
382 | if (nex == 0) | |
383 | ifp->if_u1.if_extents = NULL; | |
384 | else if (nex <= XFS_INLINE_EXTS) | |
385 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; | |
386 | else | |
387 | xfs_iext_add(ifp, 0, nex); | |
388 | ||
389 | ifp->if_bytes = size; | |
390 | if (size) { | |
391 | dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork); | |
392 | xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip)); | |
393 | for (i = 0; i < nex; i++, dp++) { | |
394 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); | |
395 | ep->l0 = get_unaligned_be64(&dp->l0); | |
396 | ep->l1 = get_unaligned_be64(&dp->l1); | |
397 | } | |
398 | XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork); | |
399 | if (whichfork != XFS_DATA_FORK || | |
400 | XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE) | |
401 | if (unlikely(xfs_check_nostate_extents( | |
402 | ifp, 0, nex))) { | |
403 | XFS_ERROR_REPORT("xfs_iformat_extents(2)", | |
404 | XFS_ERRLEVEL_LOW, | |
405 | ip->i_mount); | |
2451337d | 406 | return -EFSCORRUPTED; |
5c4d97d0 DC |
407 | } |
408 | } | |
409 | ifp->if_flags |= XFS_IFEXTENTS; | |
410 | return 0; | |
411 | } | |
412 | ||
413 | /* | |
414 | * The file has too many extents to fit into | |
415 | * the inode, so they are in B-tree format. | |
416 | * Allocate a buffer for the root of the B-tree | |
417 | * and copy the root into it. The i_extents | |
418 | * field will remain NULL until all of the | |
419 | * extents are read in (when they are needed). | |
420 | */ | |
421 | STATIC int | |
422 | xfs_iformat_btree( | |
423 | xfs_inode_t *ip, | |
424 | xfs_dinode_t *dip, | |
425 | int whichfork) | |
426 | { | |
427 | struct xfs_mount *mp = ip->i_mount; | |
428 | xfs_bmdr_block_t *dfp; | |
429 | xfs_ifork_t *ifp; | |
430 | /* REFERENCED */ | |
431 | int nrecs; | |
432 | int size; | |
bcd84e94 | 433 | int level; |
5c4d97d0 DC |
434 | |
435 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
436 | dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork); | |
437 | size = XFS_BMAP_BROOT_SPACE(mp, dfp); | |
438 | nrecs = be16_to_cpu(dfp->bb_numrecs); | |
bcd84e94 | 439 | level = be16_to_cpu(dfp->bb_level); |
5c4d97d0 DC |
440 | |
441 | /* | |
442 | * blow out if -- fork has less extents than can fit in | |
443 | * fork (fork shouldn't be a btree format), root btree | |
444 | * block has more records than can fit into the fork, | |
445 | * or the number of extents is greater than the number of | |
446 | * blocks. | |
447 | */ | |
448 | if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= | |
449 | XFS_IFORK_MAXEXT(ip, whichfork) || | |
450 | XFS_BMDR_SPACE_CALC(nrecs) > | |
451 | XFS_DFORK_SIZE(dip, mp, whichfork) || | |
bcd84e94 DW |
452 | XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) || |
453 | level == 0 || level > XFS_BTREE_MAXLEVELS) { | |
5c4d97d0 DC |
454 | xfs_warn(mp, "corrupt inode %Lu (btree).", |
455 | (unsigned long long) ip->i_ino); | |
456 | XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW, | |
457 | mp, dip); | |
2451337d | 458 | return -EFSCORRUPTED; |
5c4d97d0 DC |
459 | } |
460 | ||
461 | ifp->if_broot_bytes = size; | |
462 | ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS); | |
463 | ASSERT(ifp->if_broot != NULL); | |
464 | /* | |
465 | * Copy and convert from the on-disk structure | |
466 | * to the in-memory structure. | |
467 | */ | |
468 | xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork), | |
469 | ifp->if_broot, size); | |
470 | ifp->if_flags &= ~XFS_IFEXTENTS; | |
471 | ifp->if_flags |= XFS_IFBROOT; | |
472 | ||
473 | return 0; | |
474 | } | |
475 | ||
476 | /* | |
477 | * Read in extents from a btree-format inode. | |
478 | * Allocate and fill in if_extents. Real work is done in xfs_bmap.c. | |
479 | */ | |
480 | int | |
481 | xfs_iread_extents( | |
482 | xfs_trans_t *tp, | |
483 | xfs_inode_t *ip, | |
484 | int whichfork) | |
485 | { | |
486 | int error; | |
487 | xfs_ifork_t *ifp; | |
488 | xfs_extnum_t nextents; | |
489 | ||
eef334e5 CH |
490 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
491 | ||
5c4d97d0 DC |
492 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
493 | XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW, | |
494 | ip->i_mount); | |
2451337d | 495 | return -EFSCORRUPTED; |
5c4d97d0 DC |
496 | } |
497 | nextents = XFS_IFORK_NEXTENTS(ip, whichfork); | |
498 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
499 | ||
500 | /* | |
501 | * We know that the size is valid (it's checked in iformat_btree) | |
502 | */ | |
503 | ifp->if_bytes = ifp->if_real_bytes = 0; | |
5c4d97d0 DC |
504 | xfs_iext_add(ifp, 0, nextents); |
505 | error = xfs_bmap_read_extents(tp, ip, whichfork); | |
506 | if (error) { | |
507 | xfs_iext_destroy(ifp); | |
5c4d97d0 DC |
508 | return error; |
509 | } | |
510 | xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip)); | |
3b3b7e3e | 511 | ifp->if_flags |= XFS_IFEXTENTS; |
5c4d97d0 DC |
512 | return 0; |
513 | } | |
514 | /* | |
515 | * Reallocate the space for if_broot based on the number of records | |
516 | * being added or deleted as indicated in rec_diff. Move the records | |
517 | * and pointers in if_broot to fit the new size. When shrinking this | |
518 | * will eliminate holes between the records and pointers created by | |
519 | * the caller. When growing this will create holes to be filled in | |
520 | * by the caller. | |
521 | * | |
522 | * The caller must not request to add more records than would fit in | |
523 | * the on-disk inode root. If the if_broot is currently NULL, then | |
f6c27349 | 524 | * if we are adding records, one will be allocated. The caller must also |
5c4d97d0 DC |
525 | * not request that the number of records go below zero, although |
526 | * it can go to zero. | |
527 | * | |
528 | * ip -- the inode whose if_broot area is changing | |
529 | * ext_diff -- the change in the number of records, positive or negative, | |
530 | * requested for the if_broot array. | |
531 | */ | |
532 | void | |
533 | xfs_iroot_realloc( | |
534 | xfs_inode_t *ip, | |
535 | int rec_diff, | |
536 | int whichfork) | |
537 | { | |
538 | struct xfs_mount *mp = ip->i_mount; | |
539 | int cur_max; | |
540 | xfs_ifork_t *ifp; | |
541 | struct xfs_btree_block *new_broot; | |
542 | int new_max; | |
543 | size_t new_size; | |
544 | char *np; | |
545 | char *op; | |
546 | ||
547 | /* | |
548 | * Handle the degenerate case quietly. | |
549 | */ | |
550 | if (rec_diff == 0) { | |
551 | return; | |
552 | } | |
553 | ||
554 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
555 | if (rec_diff > 0) { | |
556 | /* | |
557 | * If there wasn't any memory allocated before, just | |
558 | * allocate it now and get out. | |
559 | */ | |
560 | if (ifp->if_broot_bytes == 0) { | |
561 | new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff); | |
562 | ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS); | |
563 | ifp->if_broot_bytes = (int)new_size; | |
564 | return; | |
565 | } | |
566 | ||
567 | /* | |
568 | * If there is already an existing if_broot, then we need | |
569 | * to realloc() it and shift the pointers to their new | |
570 | * location. The records don't change location because | |
571 | * they are kept butted up against the btree block header. | |
572 | */ | |
573 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); | |
574 | new_max = cur_max + rec_diff; | |
575 | new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); | |
576 | ifp->if_broot = kmem_realloc(ifp->if_broot, new_size, | |
5c4d97d0 DC |
577 | KM_SLEEP | KM_NOFS); |
578 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, | |
579 | ifp->if_broot_bytes); | |
580 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, | |
581 | (int)new_size); | |
582 | ifp->if_broot_bytes = (int)new_size; | |
583 | ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= | |
584 | XFS_IFORK_SIZE(ip, whichfork)); | |
d5cf09ba | 585 | memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t)); |
5c4d97d0 DC |
586 | return; |
587 | } | |
588 | ||
589 | /* | |
590 | * rec_diff is less than 0. In this case, we are shrinking the | |
591 | * if_broot buffer. It must already exist. If we go to zero | |
592 | * records, just get rid of the root and clear the status bit. | |
593 | */ | |
594 | ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0)); | |
595 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); | |
596 | new_max = cur_max + rec_diff; | |
597 | ASSERT(new_max >= 0); | |
598 | if (new_max > 0) | |
599 | new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); | |
600 | else | |
601 | new_size = 0; | |
602 | if (new_size > 0) { | |
603 | new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS); | |
604 | /* | |
605 | * First copy over the btree block header. | |
606 | */ | |
607 | memcpy(new_broot, ifp->if_broot, | |
608 | XFS_BMBT_BLOCK_LEN(ip->i_mount)); | |
609 | } else { | |
610 | new_broot = NULL; | |
611 | ifp->if_flags &= ~XFS_IFBROOT; | |
612 | } | |
613 | ||
614 | /* | |
615 | * Only copy the records and pointers if there are any. | |
616 | */ | |
617 | if (new_max > 0) { | |
618 | /* | |
619 | * First copy the records. | |
620 | */ | |
621 | op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1); | |
622 | np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1); | |
623 | memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t)); | |
624 | ||
625 | /* | |
626 | * Then copy the pointers. | |
627 | */ | |
628 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, | |
629 | ifp->if_broot_bytes); | |
630 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1, | |
631 | (int)new_size); | |
d5cf09ba | 632 | memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t)); |
5c4d97d0 DC |
633 | } |
634 | kmem_free(ifp->if_broot); | |
635 | ifp->if_broot = new_broot; | |
636 | ifp->if_broot_bytes = (int)new_size; | |
637 | if (ifp->if_broot) | |
638 | ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= | |
639 | XFS_IFORK_SIZE(ip, whichfork)); | |
640 | return; | |
641 | } | |
642 | ||
643 | ||
644 | /* | |
645 | * This is called when the amount of space needed for if_data | |
646 | * is increased or decreased. The change in size is indicated by | |
647 | * the number of bytes that need to be added or deleted in the | |
648 | * byte_diff parameter. | |
649 | * | |
650 | * If the amount of space needed has decreased below the size of the | |
651 | * inline buffer, then switch to using the inline buffer. Otherwise, | |
652 | * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer | |
653 | * to what is needed. | |
654 | * | |
655 | * ip -- the inode whose if_data area is changing | |
656 | * byte_diff -- the change in the number of bytes, positive or negative, | |
657 | * requested for the if_data array. | |
658 | */ | |
659 | void | |
660 | xfs_idata_realloc( | |
661 | xfs_inode_t *ip, | |
662 | int byte_diff, | |
663 | int whichfork) | |
664 | { | |
665 | xfs_ifork_t *ifp; | |
666 | int new_size; | |
667 | int real_size; | |
668 | ||
669 | if (byte_diff == 0) { | |
670 | return; | |
671 | } | |
672 | ||
673 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
674 | new_size = (int)ifp->if_bytes + byte_diff; | |
675 | ASSERT(new_size >= 0); | |
676 | ||
677 | if (new_size == 0) { | |
678 | if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { | |
679 | kmem_free(ifp->if_u1.if_data); | |
680 | } | |
681 | ifp->if_u1.if_data = NULL; | |
682 | real_size = 0; | |
683 | } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) { | |
684 | /* | |
685 | * If the valid extents/data can fit in if_inline_ext/data, | |
686 | * copy them from the malloc'd vector and free it. | |
687 | */ | |
688 | if (ifp->if_u1.if_data == NULL) { | |
689 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; | |
690 | } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { | |
691 | ASSERT(ifp->if_real_bytes != 0); | |
692 | memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data, | |
693 | new_size); | |
694 | kmem_free(ifp->if_u1.if_data); | |
695 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; | |
696 | } | |
697 | real_size = 0; | |
698 | } else { | |
699 | /* | |
700 | * Stuck with malloc/realloc. | |
701 | * For inline data, the underlying buffer must be | |
702 | * a multiple of 4 bytes in size so that it can be | |
703 | * logged and stay on word boundaries. We enforce | |
704 | * that here. | |
705 | */ | |
706 | real_size = roundup(new_size, 4); | |
707 | if (ifp->if_u1.if_data == NULL) { | |
708 | ASSERT(ifp->if_real_bytes == 0); | |
709 | ifp->if_u1.if_data = kmem_alloc(real_size, | |
710 | KM_SLEEP | KM_NOFS); | |
711 | } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { | |
712 | /* | |
713 | * Only do the realloc if the underlying size | |
714 | * is really changing. | |
715 | */ | |
716 | if (ifp->if_real_bytes != real_size) { | |
717 | ifp->if_u1.if_data = | |
718 | kmem_realloc(ifp->if_u1.if_data, | |
719 | real_size, | |
5c4d97d0 DC |
720 | KM_SLEEP | KM_NOFS); |
721 | } | |
722 | } else { | |
723 | ASSERT(ifp->if_real_bytes == 0); | |
724 | ifp->if_u1.if_data = kmem_alloc(real_size, | |
725 | KM_SLEEP | KM_NOFS); | |
726 | memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data, | |
727 | ifp->if_bytes); | |
728 | } | |
729 | } | |
730 | ifp->if_real_bytes = real_size; | |
731 | ifp->if_bytes = new_size; | |
732 | ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); | |
733 | } | |
734 | ||
735 | void | |
736 | xfs_idestroy_fork( | |
737 | xfs_inode_t *ip, | |
738 | int whichfork) | |
739 | { | |
740 | xfs_ifork_t *ifp; | |
741 | ||
742 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
743 | if (ifp->if_broot != NULL) { | |
744 | kmem_free(ifp->if_broot); | |
745 | ifp->if_broot = NULL; | |
746 | } | |
747 | ||
748 | /* | |
749 | * If the format is local, then we can't have an extents | |
750 | * array so just look for an inline data array. If we're | |
751 | * not local then we may or may not have an extents list, | |
752 | * so check and free it up if we do. | |
753 | */ | |
754 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | |
755 | if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) && | |
756 | (ifp->if_u1.if_data != NULL)) { | |
757 | ASSERT(ifp->if_real_bytes != 0); | |
758 | kmem_free(ifp->if_u1.if_data); | |
759 | ifp->if_u1.if_data = NULL; | |
760 | ifp->if_real_bytes = 0; | |
761 | } | |
762 | } else if ((ifp->if_flags & XFS_IFEXTENTS) && | |
763 | ((ifp->if_flags & XFS_IFEXTIREC) || | |
764 | ((ifp->if_u1.if_extents != NULL) && | |
765 | (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) { | |
766 | ASSERT(ifp->if_real_bytes != 0); | |
767 | xfs_iext_destroy(ifp); | |
768 | } | |
769 | ASSERT(ifp->if_u1.if_extents == NULL || | |
770 | ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext); | |
771 | ASSERT(ifp->if_real_bytes == 0); | |
772 | if (whichfork == XFS_ATTR_FORK) { | |
773 | kmem_zone_free(xfs_ifork_zone, ip->i_afp); | |
774 | ip->i_afp = NULL; | |
3993baeb DW |
775 | } else if (whichfork == XFS_COW_FORK) { |
776 | kmem_zone_free(xfs_ifork_zone, ip->i_cowfp); | |
777 | ip->i_cowfp = NULL; | |
5c4d97d0 DC |
778 | } |
779 | } | |
780 | ||
5d829300 ES |
781 | /* Count number of incore extents based on if_bytes */ |
782 | xfs_extnum_t | |
783 | xfs_iext_count(struct xfs_ifork *ifp) | |
784 | { | |
785 | return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
786 | } | |
787 | ||
5c4d97d0 | 788 | /* |
da776503 | 789 | * Convert in-core extents to on-disk form |
5c4d97d0 | 790 | * |
da776503 CH |
791 | * For either the data or attr fork in extent format, we need to endian convert |
792 | * the in-core extent as we place them into the on-disk inode. | |
5c4d97d0 | 793 | * |
da776503 CH |
794 | * In the case of the data fork, the in-core and on-disk fork sizes can be |
795 | * different due to delayed allocation extents. We only copy on-disk extents | |
796 | * here, so callers must always use the physical fork size to determine the | |
797 | * size of the buffer passed to this routine. We will return the size actually | |
798 | * used. | |
5c4d97d0 DC |
799 | */ |
800 | int | |
801 | xfs_iextents_copy( | |
802 | xfs_inode_t *ip, | |
803 | xfs_bmbt_rec_t *dp, | |
804 | int whichfork) | |
805 | { | |
806 | int copied; | |
807 | int i; | |
808 | xfs_ifork_t *ifp; | |
809 | int nrecs; | |
810 | xfs_fsblock_t start_block; | |
811 | ||
812 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
813 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); | |
814 | ASSERT(ifp->if_bytes > 0); | |
815 | ||
5d829300 | 816 | nrecs = xfs_iext_count(ifp); |
5c4d97d0 DC |
817 | XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork); |
818 | ASSERT(nrecs > 0); | |
819 | ||
820 | /* | |
821 | * There are some delayed allocation extents in the | |
822 | * inode, so copy the extents one at a time and skip | |
823 | * the delayed ones. There must be at least one | |
824 | * non-delayed extent. | |
825 | */ | |
826 | copied = 0; | |
827 | for (i = 0; i < nrecs; i++) { | |
828 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); | |
829 | start_block = xfs_bmbt_get_startblock(ep); | |
830 | if (isnullstartblock(start_block)) { | |
831 | /* | |
832 | * It's a delayed allocation extent, so skip it. | |
833 | */ | |
834 | continue; | |
835 | } | |
836 | ||
837 | /* Translate to on disk format */ | |
c5c249b4 DC |
838 | put_unaligned_be64(ep->l0, &dp->l0); |
839 | put_unaligned_be64(ep->l1, &dp->l1); | |
5c4d97d0 DC |
840 | dp++; |
841 | copied++; | |
842 | } | |
843 | ASSERT(copied != 0); | |
844 | xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip)); | |
845 | ||
846 | return (copied * (uint)sizeof(xfs_bmbt_rec_t)); | |
847 | } | |
848 | ||
849 | /* | |
850 | * Each of the following cases stores data into the same region | |
851 | * of the on-disk inode, so only one of them can be valid at | |
852 | * any given time. While it is possible to have conflicting formats | |
853 | * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is | |
854 | * in EXTENTS format, this can only happen when the fork has | |
855 | * changed formats after being modified but before being flushed. | |
856 | * In these cases, the format always takes precedence, because the | |
857 | * format indicates the current state of the fork. | |
858 | */ | |
859 | void | |
860 | xfs_iflush_fork( | |
861 | xfs_inode_t *ip, | |
862 | xfs_dinode_t *dip, | |
863 | xfs_inode_log_item_t *iip, | |
fd9fdba6 | 864 | int whichfork) |
5c4d97d0 DC |
865 | { |
866 | char *cp; | |
867 | xfs_ifork_t *ifp; | |
868 | xfs_mount_t *mp; | |
869 | static const short brootflag[2] = | |
870 | { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT }; | |
871 | static const short dataflag[2] = | |
872 | { XFS_ILOG_DDATA, XFS_ILOG_ADATA }; | |
873 | static const short extflag[2] = | |
874 | { XFS_ILOG_DEXT, XFS_ILOG_AEXT }; | |
875 | ||
876 | if (!iip) | |
877 | return; | |
878 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
879 | /* | |
880 | * This can happen if we gave up in iformat in an error path, | |
881 | * for the attribute fork. | |
882 | */ | |
883 | if (!ifp) { | |
884 | ASSERT(whichfork == XFS_ATTR_FORK); | |
885 | return; | |
886 | } | |
887 | cp = XFS_DFORK_PTR(dip, whichfork); | |
888 | mp = ip->i_mount; | |
889 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { | |
890 | case XFS_DINODE_FMT_LOCAL: | |
891 | if ((iip->ili_fields & dataflag[whichfork]) && | |
892 | (ifp->if_bytes > 0)) { | |
893 | ASSERT(ifp->if_u1.if_data != NULL); | |
894 | ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); | |
895 | memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes); | |
896 | } | |
897 | break; | |
898 | ||
899 | case XFS_DINODE_FMT_EXTENTS: | |
900 | ASSERT((ifp->if_flags & XFS_IFEXTENTS) || | |
901 | !(iip->ili_fields & extflag[whichfork])); | |
902 | if ((iip->ili_fields & extflag[whichfork]) && | |
903 | (ifp->if_bytes > 0)) { | |
904 | ASSERT(xfs_iext_get_ext(ifp, 0)); | |
905 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0); | |
906 | (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp, | |
907 | whichfork); | |
908 | } | |
909 | break; | |
910 | ||
911 | case XFS_DINODE_FMT_BTREE: | |
912 | if ((iip->ili_fields & brootflag[whichfork]) && | |
913 | (ifp->if_broot_bytes > 0)) { | |
914 | ASSERT(ifp->if_broot != NULL); | |
915 | ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= | |
916 | XFS_IFORK_SIZE(ip, whichfork)); | |
917 | xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes, | |
918 | (xfs_bmdr_block_t *)cp, | |
919 | XFS_DFORK_SIZE(dip, mp, whichfork)); | |
920 | } | |
921 | break; | |
922 | ||
923 | case XFS_DINODE_FMT_DEV: | |
924 | if (iip->ili_fields & XFS_ILOG_DEV) { | |
925 | ASSERT(whichfork == XFS_DATA_FORK); | |
926 | xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev); | |
927 | } | |
928 | break; | |
929 | ||
930 | case XFS_DINODE_FMT_UUID: | |
931 | if (iip->ili_fields & XFS_ILOG_UUID) { | |
932 | ASSERT(whichfork == XFS_DATA_FORK); | |
933 | memcpy(XFS_DFORK_DPTR(dip), | |
934 | &ip->i_df.if_u2.if_uuid, | |
935 | sizeof(uuid_t)); | |
936 | } | |
937 | break; | |
938 | ||
939 | default: | |
940 | ASSERT(0); | |
941 | break; | |
942 | } | |
943 | } | |
944 | ||
945 | /* | |
946 | * Return a pointer to the extent record at file index idx. | |
947 | */ | |
948 | xfs_bmbt_rec_host_t * | |
949 | xfs_iext_get_ext( | |
950 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
951 | xfs_extnum_t idx) /* index of target extent */ | |
952 | { | |
953 | ASSERT(idx >= 0); | |
5d829300 | 954 | ASSERT(idx < xfs_iext_count(ifp)); |
5c4d97d0 DC |
955 | |
956 | if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) { | |
957 | return ifp->if_u1.if_ext_irec->er_extbuf; | |
958 | } else if (ifp->if_flags & XFS_IFEXTIREC) { | |
959 | xfs_ext_irec_t *erp; /* irec pointer */ | |
960 | int erp_idx = 0; /* irec index */ | |
961 | xfs_extnum_t page_idx = idx; /* ext index in target list */ | |
962 | ||
963 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); | |
964 | return &erp->er_extbuf[page_idx]; | |
965 | } else if (ifp->if_bytes) { | |
966 | return &ifp->if_u1.if_extents[idx]; | |
967 | } else { | |
968 | return NULL; | |
969 | } | |
970 | } | |
971 | ||
3993baeb DW |
972 | /* Convert bmap state flags to an inode fork. */ |
973 | struct xfs_ifork * | |
974 | xfs_iext_state_to_fork( | |
975 | struct xfs_inode *ip, | |
976 | int state) | |
977 | { | |
978 | if (state & BMAP_COWFORK) | |
979 | return ip->i_cowfp; | |
980 | else if (state & BMAP_ATTRFORK) | |
981 | return ip->i_afp; | |
982 | return &ip->i_df; | |
983 | } | |
984 | ||
5c4d97d0 DC |
985 | /* |
986 | * Insert new item(s) into the extent records for incore inode | |
987 | * fork 'ifp'. 'count' new items are inserted at index 'idx'. | |
988 | */ | |
989 | void | |
990 | xfs_iext_insert( | |
991 | xfs_inode_t *ip, /* incore inode pointer */ | |
992 | xfs_extnum_t idx, /* starting index of new items */ | |
993 | xfs_extnum_t count, /* number of inserted items */ | |
994 | xfs_bmbt_irec_t *new, /* items to insert */ | |
995 | int state) /* type of extent conversion */ | |
996 | { | |
3993baeb | 997 | xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state); |
5c4d97d0 DC |
998 | xfs_extnum_t i; /* extent record index */ |
999 | ||
1000 | trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_); | |
1001 | ||
1002 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); | |
1003 | xfs_iext_add(ifp, idx, count); | |
1004 | for (i = idx; i < idx + count; i++, new++) | |
1005 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new); | |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * This is called when the amount of space required for incore file | |
1010 | * extents needs to be increased. The ext_diff parameter stores the | |
1011 | * number of new extents being added and the idx parameter contains | |
1012 | * the extent index where the new extents will be added. If the new | |
1013 | * extents are being appended, then we just need to (re)allocate and | |
1014 | * initialize the space. Otherwise, if the new extents are being | |
1015 | * inserted into the middle of the existing entries, a bit more work | |
1016 | * is required to make room for the new extents to be inserted. The | |
1017 | * caller is responsible for filling in the new extent entries upon | |
1018 | * return. | |
1019 | */ | |
1020 | void | |
1021 | xfs_iext_add( | |
1022 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1023 | xfs_extnum_t idx, /* index to begin adding exts */ | |
1024 | int ext_diff) /* number of extents to add */ | |
1025 | { | |
1026 | int byte_diff; /* new bytes being added */ | |
1027 | int new_size; /* size of extents after adding */ | |
1028 | xfs_extnum_t nextents; /* number of extents in file */ | |
1029 | ||
5d829300 | 1030 | nextents = xfs_iext_count(ifp); |
5c4d97d0 DC |
1031 | ASSERT((idx >= 0) && (idx <= nextents)); |
1032 | byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t); | |
1033 | new_size = ifp->if_bytes + byte_diff; | |
1034 | /* | |
1035 | * If the new number of extents (nextents + ext_diff) | |
1036 | * fits inside the inode, then continue to use the inline | |
1037 | * extent buffer. | |
1038 | */ | |
1039 | if (nextents + ext_diff <= XFS_INLINE_EXTS) { | |
1040 | if (idx < nextents) { | |
1041 | memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff], | |
1042 | &ifp->if_u2.if_inline_ext[idx], | |
1043 | (nextents - idx) * sizeof(xfs_bmbt_rec_t)); | |
1044 | memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff); | |
1045 | } | |
1046 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; | |
1047 | ifp->if_real_bytes = 0; | |
1048 | } | |
1049 | /* | |
1050 | * Otherwise use a linear (direct) extent list. | |
1051 | * If the extents are currently inside the inode, | |
1052 | * xfs_iext_realloc_direct will switch us from | |
1053 | * inline to direct extent allocation mode. | |
1054 | */ | |
1055 | else if (nextents + ext_diff <= XFS_LINEAR_EXTS) { | |
1056 | xfs_iext_realloc_direct(ifp, new_size); | |
1057 | if (idx < nextents) { | |
1058 | memmove(&ifp->if_u1.if_extents[idx + ext_diff], | |
1059 | &ifp->if_u1.if_extents[idx], | |
1060 | (nextents - idx) * sizeof(xfs_bmbt_rec_t)); | |
1061 | memset(&ifp->if_u1.if_extents[idx], 0, byte_diff); | |
1062 | } | |
1063 | } | |
1064 | /* Indirection array */ | |
1065 | else { | |
1066 | xfs_ext_irec_t *erp; | |
1067 | int erp_idx = 0; | |
1068 | int page_idx = idx; | |
1069 | ||
1070 | ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS); | |
1071 | if (ifp->if_flags & XFS_IFEXTIREC) { | |
1072 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1); | |
1073 | } else { | |
1074 | xfs_iext_irec_init(ifp); | |
1075 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1076 | erp = ifp->if_u1.if_ext_irec; | |
1077 | } | |
1078 | /* Extents fit in target extent page */ | |
1079 | if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) { | |
1080 | if (page_idx < erp->er_extcount) { | |
1081 | memmove(&erp->er_extbuf[page_idx + ext_diff], | |
1082 | &erp->er_extbuf[page_idx], | |
1083 | (erp->er_extcount - page_idx) * | |
1084 | sizeof(xfs_bmbt_rec_t)); | |
1085 | memset(&erp->er_extbuf[page_idx], 0, byte_diff); | |
1086 | } | |
1087 | erp->er_extcount += ext_diff; | |
1088 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); | |
1089 | } | |
1090 | /* Insert a new extent page */ | |
1091 | else if (erp) { | |
1092 | xfs_iext_add_indirect_multi(ifp, | |
1093 | erp_idx, page_idx, ext_diff); | |
1094 | } | |
1095 | /* | |
1096 | * If extent(s) are being appended to the last page in | |
1097 | * the indirection array and the new extent(s) don't fit | |
1098 | * in the page, then erp is NULL and erp_idx is set to | |
1099 | * the next index needed in the indirection array. | |
1100 | */ | |
1101 | else { | |
bb86d21c | 1102 | uint count = ext_diff; |
5c4d97d0 DC |
1103 | |
1104 | while (count) { | |
1105 | erp = xfs_iext_irec_new(ifp, erp_idx); | |
bb86d21c JL |
1106 | erp->er_extcount = min(count, XFS_LINEAR_EXTS); |
1107 | count -= erp->er_extcount; | |
1108 | if (count) | |
5c4d97d0 | 1109 | erp_idx++; |
5c4d97d0 DC |
1110 | } |
1111 | } | |
1112 | } | |
1113 | ifp->if_bytes = new_size; | |
1114 | } | |
1115 | ||
1116 | /* | |
1117 | * This is called when incore extents are being added to the indirection | |
1118 | * array and the new extents do not fit in the target extent list. The | |
1119 | * erp_idx parameter contains the irec index for the target extent list | |
1120 | * in the indirection array, and the idx parameter contains the extent | |
1121 | * index within the list. The number of extents being added is stored | |
1122 | * in the count parameter. | |
1123 | * | |
1124 | * |-------| |-------| | |
1125 | * | | | | idx - number of extents before idx | |
1126 | * | idx | | count | | |
1127 | * | | | | count - number of extents being inserted at idx | |
1128 | * |-------| |-------| | |
1129 | * | count | | nex2 | nex2 - number of extents after idx + count | |
1130 | * |-------| |-------| | |
1131 | */ | |
1132 | void | |
1133 | xfs_iext_add_indirect_multi( | |
1134 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1135 | int erp_idx, /* target extent irec index */ | |
1136 | xfs_extnum_t idx, /* index within target list */ | |
1137 | int count) /* new extents being added */ | |
1138 | { | |
1139 | int byte_diff; /* new bytes being added */ | |
1140 | xfs_ext_irec_t *erp; /* pointer to irec entry */ | |
1141 | xfs_extnum_t ext_diff; /* number of extents to add */ | |
1142 | xfs_extnum_t ext_cnt; /* new extents still needed */ | |
1143 | xfs_extnum_t nex2; /* extents after idx + count */ | |
1144 | xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */ | |
1145 | int nlists; /* number of irec's (lists) */ | |
1146 | ||
1147 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1148 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
1149 | nex2 = erp->er_extcount - idx; | |
1150 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1151 | ||
1152 | /* | |
1153 | * Save second part of target extent list | |
1154 | * (all extents past */ | |
1155 | if (nex2) { | |
1156 | byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); | |
1157 | nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS); | |
1158 | memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff); | |
1159 | erp->er_extcount -= nex2; | |
1160 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2); | |
1161 | memset(&erp->er_extbuf[idx], 0, byte_diff); | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * Add the new extents to the end of the target | |
1166 | * list, then allocate new irec record(s) and | |
1167 | * extent buffer(s) as needed to store the rest | |
1168 | * of the new extents. | |
1169 | */ | |
1170 | ext_cnt = count; | |
1171 | ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount); | |
1172 | if (ext_diff) { | |
1173 | erp->er_extcount += ext_diff; | |
1174 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); | |
1175 | ext_cnt -= ext_diff; | |
1176 | } | |
1177 | while (ext_cnt) { | |
1178 | erp_idx++; | |
1179 | erp = xfs_iext_irec_new(ifp, erp_idx); | |
1180 | ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS); | |
1181 | erp->er_extcount = ext_diff; | |
1182 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); | |
1183 | ext_cnt -= ext_diff; | |
1184 | } | |
1185 | ||
1186 | /* Add nex2 extents back to indirection array */ | |
1187 | if (nex2) { | |
1188 | xfs_extnum_t ext_avail; | |
1189 | int i; | |
1190 | ||
1191 | byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); | |
1192 | ext_avail = XFS_LINEAR_EXTS - erp->er_extcount; | |
1193 | i = 0; | |
1194 | /* | |
1195 | * If nex2 extents fit in the current page, append | |
1196 | * nex2_ep after the new extents. | |
1197 | */ | |
1198 | if (nex2 <= ext_avail) { | |
1199 | i = erp->er_extcount; | |
1200 | } | |
1201 | /* | |
1202 | * Otherwise, check if space is available in the | |
1203 | * next page. | |
1204 | */ | |
1205 | else if ((erp_idx < nlists - 1) && | |
1206 | (nex2 <= (ext_avail = XFS_LINEAR_EXTS - | |
1207 | ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) { | |
1208 | erp_idx++; | |
1209 | erp++; | |
1210 | /* Create a hole for nex2 extents */ | |
1211 | memmove(&erp->er_extbuf[nex2], erp->er_extbuf, | |
1212 | erp->er_extcount * sizeof(xfs_bmbt_rec_t)); | |
1213 | } | |
1214 | /* | |
1215 | * Final choice, create a new extent page for | |
1216 | * nex2 extents. | |
1217 | */ | |
1218 | else { | |
1219 | erp_idx++; | |
1220 | erp = xfs_iext_irec_new(ifp, erp_idx); | |
1221 | } | |
1222 | memmove(&erp->er_extbuf[i], nex2_ep, byte_diff); | |
1223 | kmem_free(nex2_ep); | |
1224 | erp->er_extcount += nex2; | |
1225 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2); | |
1226 | } | |
1227 | } | |
1228 | ||
1229 | /* | |
1230 | * This is called when the amount of space required for incore file | |
1231 | * extents needs to be decreased. The ext_diff parameter stores the | |
1232 | * number of extents to be removed and the idx parameter contains | |
1233 | * the extent index where the extents will be removed from. | |
1234 | * | |
1235 | * If the amount of space needed has decreased below the linear | |
1236 | * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous | |
1237 | * extent array. Otherwise, use kmem_realloc() to adjust the | |
1238 | * size to what is needed. | |
1239 | */ | |
1240 | void | |
1241 | xfs_iext_remove( | |
1242 | xfs_inode_t *ip, /* incore inode pointer */ | |
1243 | xfs_extnum_t idx, /* index to begin removing exts */ | |
1244 | int ext_diff, /* number of extents to remove */ | |
1245 | int state) /* type of extent conversion */ | |
1246 | { | |
3993baeb | 1247 | xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state); |
5c4d97d0 DC |
1248 | xfs_extnum_t nextents; /* number of extents in file */ |
1249 | int new_size; /* size of extents after removal */ | |
1250 | ||
1251 | trace_xfs_iext_remove(ip, idx, state, _RET_IP_); | |
1252 | ||
1253 | ASSERT(ext_diff > 0); | |
5d829300 | 1254 | nextents = xfs_iext_count(ifp); |
5c4d97d0 DC |
1255 | new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t); |
1256 | ||
1257 | if (new_size == 0) { | |
1258 | xfs_iext_destroy(ifp); | |
1259 | } else if (ifp->if_flags & XFS_IFEXTIREC) { | |
1260 | xfs_iext_remove_indirect(ifp, idx, ext_diff); | |
1261 | } else if (ifp->if_real_bytes) { | |
1262 | xfs_iext_remove_direct(ifp, idx, ext_diff); | |
1263 | } else { | |
1264 | xfs_iext_remove_inline(ifp, idx, ext_diff); | |
1265 | } | |
1266 | ifp->if_bytes = new_size; | |
1267 | } | |
1268 | ||
1269 | /* | |
1270 | * This removes ext_diff extents from the inline buffer, beginning | |
1271 | * at extent index idx. | |
1272 | */ | |
1273 | void | |
1274 | xfs_iext_remove_inline( | |
1275 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1276 | xfs_extnum_t idx, /* index to begin removing exts */ | |
1277 | int ext_diff) /* number of extents to remove */ | |
1278 | { | |
1279 | int nextents; /* number of extents in file */ | |
1280 | ||
1281 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); | |
1282 | ASSERT(idx < XFS_INLINE_EXTS); | |
5d829300 | 1283 | nextents = xfs_iext_count(ifp); |
5c4d97d0 DC |
1284 | ASSERT(((nextents - ext_diff) > 0) && |
1285 | (nextents - ext_diff) < XFS_INLINE_EXTS); | |
1286 | ||
1287 | if (idx + ext_diff < nextents) { | |
1288 | memmove(&ifp->if_u2.if_inline_ext[idx], | |
1289 | &ifp->if_u2.if_inline_ext[idx + ext_diff], | |
1290 | (nextents - (idx + ext_diff)) * | |
1291 | sizeof(xfs_bmbt_rec_t)); | |
1292 | memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff], | |
1293 | 0, ext_diff * sizeof(xfs_bmbt_rec_t)); | |
1294 | } else { | |
1295 | memset(&ifp->if_u2.if_inline_ext[idx], 0, | |
1296 | ext_diff * sizeof(xfs_bmbt_rec_t)); | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | /* | |
1301 | * This removes ext_diff extents from a linear (direct) extent list, | |
1302 | * beginning at extent index idx. If the extents are being removed | |
1303 | * from the end of the list (ie. truncate) then we just need to re- | |
1304 | * allocate the list to remove the extra space. Otherwise, if the | |
1305 | * extents are being removed from the middle of the existing extent | |
1306 | * entries, then we first need to move the extent records beginning | |
1307 | * at idx + ext_diff up in the list to overwrite the records being | |
1308 | * removed, then remove the extra space via kmem_realloc. | |
1309 | */ | |
1310 | void | |
1311 | xfs_iext_remove_direct( | |
1312 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1313 | xfs_extnum_t idx, /* index to begin removing exts */ | |
1314 | int ext_diff) /* number of extents to remove */ | |
1315 | { | |
1316 | xfs_extnum_t nextents; /* number of extents in file */ | |
1317 | int new_size; /* size of extents after removal */ | |
1318 | ||
1319 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); | |
1320 | new_size = ifp->if_bytes - | |
1321 | (ext_diff * sizeof(xfs_bmbt_rec_t)); | |
5d829300 | 1322 | nextents = xfs_iext_count(ifp); |
5c4d97d0 DC |
1323 | |
1324 | if (new_size == 0) { | |
1325 | xfs_iext_destroy(ifp); | |
1326 | return; | |
1327 | } | |
1328 | /* Move extents up in the list (if needed) */ | |
1329 | if (idx + ext_diff < nextents) { | |
1330 | memmove(&ifp->if_u1.if_extents[idx], | |
1331 | &ifp->if_u1.if_extents[idx + ext_diff], | |
1332 | (nextents - (idx + ext_diff)) * | |
1333 | sizeof(xfs_bmbt_rec_t)); | |
1334 | } | |
1335 | memset(&ifp->if_u1.if_extents[nextents - ext_diff], | |
1336 | 0, ext_diff * sizeof(xfs_bmbt_rec_t)); | |
1337 | /* | |
1338 | * Reallocate the direct extent list. If the extents | |
1339 | * will fit inside the inode then xfs_iext_realloc_direct | |
1340 | * will switch from direct to inline extent allocation | |
1341 | * mode for us. | |
1342 | */ | |
1343 | xfs_iext_realloc_direct(ifp, new_size); | |
1344 | ifp->if_bytes = new_size; | |
1345 | } | |
1346 | ||
1347 | /* | |
1348 | * This is called when incore extents are being removed from the | |
1349 | * indirection array and the extents being removed span multiple extent | |
1350 | * buffers. The idx parameter contains the file extent index where we | |
1351 | * want to begin removing extents, and the count parameter contains | |
1352 | * how many extents need to be removed. | |
1353 | * | |
1354 | * |-------| |-------| | |
1355 | * | nex1 | | | nex1 - number of extents before idx | |
1356 | * |-------| | count | | |
1357 | * | | | | count - number of extents being removed at idx | |
1358 | * | count | |-------| | |
1359 | * | | | nex2 | nex2 - number of extents after idx + count | |
1360 | * |-------| |-------| | |
1361 | */ | |
1362 | void | |
1363 | xfs_iext_remove_indirect( | |
1364 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1365 | xfs_extnum_t idx, /* index to begin removing extents */ | |
1366 | int count) /* number of extents to remove */ | |
1367 | { | |
1368 | xfs_ext_irec_t *erp; /* indirection array pointer */ | |
1369 | int erp_idx = 0; /* indirection array index */ | |
1370 | xfs_extnum_t ext_cnt; /* extents left to remove */ | |
1371 | xfs_extnum_t ext_diff; /* extents to remove in current list */ | |
1372 | xfs_extnum_t nex1; /* number of extents before idx */ | |
1373 | xfs_extnum_t nex2; /* extents after idx + count */ | |
1374 | int page_idx = idx; /* index in target extent list */ | |
1375 | ||
1376 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1377 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); | |
1378 | ASSERT(erp != NULL); | |
1379 | nex1 = page_idx; | |
1380 | ext_cnt = count; | |
1381 | while (ext_cnt) { | |
1382 | nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0); | |
1383 | ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1)); | |
1384 | /* | |
1385 | * Check for deletion of entire list; | |
1386 | * xfs_iext_irec_remove() updates extent offsets. | |
1387 | */ | |
1388 | if (ext_diff == erp->er_extcount) { | |
1389 | xfs_iext_irec_remove(ifp, erp_idx); | |
1390 | ext_cnt -= ext_diff; | |
1391 | nex1 = 0; | |
1392 | if (ext_cnt) { | |
1393 | ASSERT(erp_idx < ifp->if_real_bytes / | |
1394 | XFS_IEXT_BUFSZ); | |
1395 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
1396 | nex1 = 0; | |
1397 | continue; | |
1398 | } else { | |
1399 | break; | |
1400 | } | |
1401 | } | |
1402 | /* Move extents up (if needed) */ | |
1403 | if (nex2) { | |
1404 | memmove(&erp->er_extbuf[nex1], | |
1405 | &erp->er_extbuf[nex1 + ext_diff], | |
1406 | nex2 * sizeof(xfs_bmbt_rec_t)); | |
1407 | } | |
1408 | /* Zero out rest of page */ | |
1409 | memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ - | |
1410 | ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t)))); | |
1411 | /* Update remaining counters */ | |
1412 | erp->er_extcount -= ext_diff; | |
1413 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff); | |
1414 | ext_cnt -= ext_diff; | |
1415 | nex1 = 0; | |
1416 | erp_idx++; | |
1417 | erp++; | |
1418 | } | |
1419 | ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t); | |
1420 | xfs_iext_irec_compact(ifp); | |
1421 | } | |
1422 | ||
1423 | /* | |
1424 | * Create, destroy, or resize a linear (direct) block of extents. | |
1425 | */ | |
1426 | void | |
1427 | xfs_iext_realloc_direct( | |
1428 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
17ec81c1 | 1429 | int new_size) /* new size of extents after adding */ |
5c4d97d0 DC |
1430 | { |
1431 | int rnew_size; /* real new size of extents */ | |
1432 | ||
1433 | rnew_size = new_size; | |
1434 | ||
1435 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) || | |
1436 | ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) && | |
1437 | (new_size != ifp->if_real_bytes))); | |
1438 | ||
1439 | /* Free extent records */ | |
1440 | if (new_size == 0) { | |
1441 | xfs_iext_destroy(ifp); | |
1442 | } | |
1443 | /* Resize direct extent list and zero any new bytes */ | |
1444 | else if (ifp->if_real_bytes) { | |
1445 | /* Check if extents will fit inside the inode */ | |
1446 | if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) { | |
1447 | xfs_iext_direct_to_inline(ifp, new_size / | |
1448 | (uint)sizeof(xfs_bmbt_rec_t)); | |
1449 | ifp->if_bytes = new_size; | |
1450 | return; | |
1451 | } | |
1452 | if (!is_power_of_2(new_size)){ | |
1453 | rnew_size = roundup_pow_of_two(new_size); | |
1454 | } | |
1455 | if (rnew_size != ifp->if_real_bytes) { | |
1456 | ifp->if_u1.if_extents = | |
1457 | kmem_realloc(ifp->if_u1.if_extents, | |
664b60f6 | 1458 | rnew_size, KM_NOFS); |
5c4d97d0 DC |
1459 | } |
1460 | if (rnew_size > ifp->if_real_bytes) { | |
1461 | memset(&ifp->if_u1.if_extents[ifp->if_bytes / | |
1462 | (uint)sizeof(xfs_bmbt_rec_t)], 0, | |
1463 | rnew_size - ifp->if_real_bytes); | |
1464 | } | |
1465 | } | |
17ec81c1 | 1466 | /* Switch from the inline extent buffer to a direct extent list */ |
5c4d97d0 | 1467 | else { |
5c4d97d0 DC |
1468 | if (!is_power_of_2(new_size)) { |
1469 | rnew_size = roundup_pow_of_two(new_size); | |
1470 | } | |
1471 | xfs_iext_inline_to_direct(ifp, rnew_size); | |
1472 | } | |
1473 | ifp->if_real_bytes = rnew_size; | |
1474 | ifp->if_bytes = new_size; | |
1475 | } | |
1476 | ||
1477 | /* | |
1478 | * Switch from linear (direct) extent records to inline buffer. | |
1479 | */ | |
1480 | void | |
1481 | xfs_iext_direct_to_inline( | |
1482 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1483 | xfs_extnum_t nextents) /* number of extents in file */ | |
1484 | { | |
1485 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); | |
1486 | ASSERT(nextents <= XFS_INLINE_EXTS); | |
1487 | /* | |
1488 | * The inline buffer was zeroed when we switched | |
1489 | * from inline to direct extent allocation mode, | |
1490 | * so we don't need to clear it here. | |
1491 | */ | |
1492 | memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents, | |
1493 | nextents * sizeof(xfs_bmbt_rec_t)); | |
1494 | kmem_free(ifp->if_u1.if_extents); | |
1495 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; | |
1496 | ifp->if_real_bytes = 0; | |
1497 | } | |
1498 | ||
1499 | /* | |
1500 | * Switch from inline buffer to linear (direct) extent records. | |
1501 | * new_size should already be rounded up to the next power of 2 | |
1502 | * by the caller (when appropriate), so use new_size as it is. | |
1503 | * However, since new_size may be rounded up, we can't update | |
1504 | * if_bytes here. It is the caller's responsibility to update | |
1505 | * if_bytes upon return. | |
1506 | */ | |
1507 | void | |
1508 | xfs_iext_inline_to_direct( | |
1509 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1510 | int new_size) /* number of extents in file */ | |
1511 | { | |
1512 | ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS); | |
1513 | memset(ifp->if_u1.if_extents, 0, new_size); | |
1514 | if (ifp->if_bytes) { | |
1515 | memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext, | |
1516 | ifp->if_bytes); | |
1517 | memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * | |
1518 | sizeof(xfs_bmbt_rec_t)); | |
1519 | } | |
1520 | ifp->if_real_bytes = new_size; | |
1521 | } | |
1522 | ||
1523 | /* | |
1524 | * Resize an extent indirection array to new_size bytes. | |
1525 | */ | |
1526 | STATIC void | |
1527 | xfs_iext_realloc_indirect( | |
1528 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1529 | int new_size) /* new indirection array size */ | |
1530 | { | |
1531 | int nlists; /* number of irec's (ex lists) */ | |
1532 | int size; /* current indirection array size */ | |
1533 | ||
1534 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1535 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1536 | size = nlists * sizeof(xfs_ext_irec_t); | |
1537 | ASSERT(ifp->if_real_bytes); | |
1538 | ASSERT((new_size >= 0) && (new_size != size)); | |
1539 | if (new_size == 0) { | |
1540 | xfs_iext_destroy(ifp); | |
1541 | } else { | |
664b60f6 CH |
1542 | ifp->if_u1.if_ext_irec = |
1543 | kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS); | |
5c4d97d0 DC |
1544 | } |
1545 | } | |
1546 | ||
1547 | /* | |
1548 | * Switch from indirection array to linear (direct) extent allocations. | |
1549 | */ | |
1550 | STATIC void | |
1551 | xfs_iext_indirect_to_direct( | |
1552 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
1553 | { | |
1554 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
1555 | xfs_extnum_t nextents; /* number of extents in file */ | |
1556 | int size; /* size of file extents */ | |
1557 | ||
1558 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
5d829300 | 1559 | nextents = xfs_iext_count(ifp); |
5c4d97d0 DC |
1560 | ASSERT(nextents <= XFS_LINEAR_EXTS); |
1561 | size = nextents * sizeof(xfs_bmbt_rec_t); | |
1562 | ||
1563 | xfs_iext_irec_compact_pages(ifp); | |
1564 | ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ); | |
1565 | ||
1566 | ep = ifp->if_u1.if_ext_irec->er_extbuf; | |
1567 | kmem_free(ifp->if_u1.if_ext_irec); | |
1568 | ifp->if_flags &= ~XFS_IFEXTIREC; | |
1569 | ifp->if_u1.if_extents = ep; | |
1570 | ifp->if_bytes = size; | |
1571 | if (nextents < XFS_LINEAR_EXTS) { | |
1572 | xfs_iext_realloc_direct(ifp, size); | |
1573 | } | |
1574 | } | |
1575 | ||
32b43ab6 AL |
1576 | /* |
1577 | * Remove all records from the indirection array. | |
1578 | */ | |
1579 | STATIC void | |
1580 | xfs_iext_irec_remove_all( | |
1581 | struct xfs_ifork *ifp) | |
1582 | { | |
1583 | int nlists; | |
1584 | int i; | |
1585 | ||
1586 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1587 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1588 | for (i = 0; i < nlists; i++) | |
1589 | kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf); | |
1590 | kmem_free(ifp->if_u1.if_ext_irec); | |
1591 | ifp->if_flags &= ~XFS_IFEXTIREC; | |
1592 | } | |
1593 | ||
5c4d97d0 DC |
1594 | /* |
1595 | * Free incore file extents. | |
1596 | */ | |
1597 | void | |
1598 | xfs_iext_destroy( | |
1599 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
1600 | { | |
1601 | if (ifp->if_flags & XFS_IFEXTIREC) { | |
32b43ab6 | 1602 | xfs_iext_irec_remove_all(ifp); |
5c4d97d0 DC |
1603 | } else if (ifp->if_real_bytes) { |
1604 | kmem_free(ifp->if_u1.if_extents); | |
1605 | } else if (ifp->if_bytes) { | |
1606 | memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * | |
1607 | sizeof(xfs_bmbt_rec_t)); | |
1608 | } | |
1609 | ifp->if_u1.if_extents = NULL; | |
1610 | ifp->if_real_bytes = 0; | |
1611 | ifp->if_bytes = 0; | |
1612 | } | |
1613 | ||
1614 | /* | |
1615 | * Return a pointer to the extent record for file system block bno. | |
1616 | */ | |
1617 | xfs_bmbt_rec_host_t * /* pointer to found extent record */ | |
1618 | xfs_iext_bno_to_ext( | |
1619 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1620 | xfs_fileoff_t bno, /* block number to search for */ | |
1621 | xfs_extnum_t *idxp) /* index of target extent */ | |
1622 | { | |
1623 | xfs_bmbt_rec_host_t *base; /* pointer to first extent */ | |
1624 | xfs_filblks_t blockcount = 0; /* number of blocks in extent */ | |
1625 | xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */ | |
1626 | xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ | |
1627 | int high; /* upper boundary in search */ | |
1628 | xfs_extnum_t idx = 0; /* index of target extent */ | |
1629 | int low; /* lower boundary in search */ | |
1630 | xfs_extnum_t nextents; /* number of file extents */ | |
1631 | xfs_fileoff_t startoff = 0; /* start offset of extent */ | |
1632 | ||
5d829300 | 1633 | nextents = xfs_iext_count(ifp); |
5c4d97d0 DC |
1634 | if (nextents == 0) { |
1635 | *idxp = 0; | |
1636 | return NULL; | |
1637 | } | |
1638 | low = 0; | |
1639 | if (ifp->if_flags & XFS_IFEXTIREC) { | |
1640 | /* Find target extent list */ | |
1641 | int erp_idx = 0; | |
1642 | erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx); | |
1643 | base = erp->er_extbuf; | |
1644 | high = erp->er_extcount - 1; | |
1645 | } else { | |
1646 | base = ifp->if_u1.if_extents; | |
1647 | high = nextents - 1; | |
1648 | } | |
1649 | /* Binary search extent records */ | |
1650 | while (low <= high) { | |
1651 | idx = (low + high) >> 1; | |
1652 | ep = base + idx; | |
1653 | startoff = xfs_bmbt_get_startoff(ep); | |
1654 | blockcount = xfs_bmbt_get_blockcount(ep); | |
1655 | if (bno < startoff) { | |
1656 | high = idx - 1; | |
1657 | } else if (bno >= startoff + blockcount) { | |
1658 | low = idx + 1; | |
1659 | } else { | |
1660 | /* Convert back to file-based extent index */ | |
1661 | if (ifp->if_flags & XFS_IFEXTIREC) { | |
1662 | idx += erp->er_extoff; | |
1663 | } | |
1664 | *idxp = idx; | |
1665 | return ep; | |
1666 | } | |
1667 | } | |
1668 | /* Convert back to file-based extent index */ | |
1669 | if (ifp->if_flags & XFS_IFEXTIREC) { | |
1670 | idx += erp->er_extoff; | |
1671 | } | |
1672 | if (bno >= startoff + blockcount) { | |
1673 | if (++idx == nextents) { | |
1674 | ep = NULL; | |
1675 | } else { | |
1676 | ep = xfs_iext_get_ext(ifp, idx); | |
1677 | } | |
1678 | } | |
1679 | *idxp = idx; | |
1680 | return ep; | |
1681 | } | |
1682 | ||
1683 | /* | |
1684 | * Return a pointer to the indirection array entry containing the | |
1685 | * extent record for filesystem block bno. Store the index of the | |
1686 | * target irec in *erp_idxp. | |
1687 | */ | |
1688 | xfs_ext_irec_t * /* pointer to found extent record */ | |
1689 | xfs_iext_bno_to_irec( | |
1690 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1691 | xfs_fileoff_t bno, /* block number to search for */ | |
1692 | int *erp_idxp) /* irec index of target ext list */ | |
1693 | { | |
1694 | xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ | |
1695 | xfs_ext_irec_t *erp_next; /* next indirection array entry */ | |
1696 | int erp_idx; /* indirection array index */ | |
1697 | int nlists; /* number of extent irec's (lists) */ | |
1698 | int high; /* binary search upper limit */ | |
1699 | int low; /* binary search lower limit */ | |
1700 | ||
1701 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1702 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1703 | erp_idx = 0; | |
1704 | low = 0; | |
1705 | high = nlists - 1; | |
1706 | while (low <= high) { | |
1707 | erp_idx = (low + high) >> 1; | |
1708 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
1709 | erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL; | |
1710 | if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) { | |
1711 | high = erp_idx - 1; | |
1712 | } else if (erp_next && bno >= | |
1713 | xfs_bmbt_get_startoff(erp_next->er_extbuf)) { | |
1714 | low = erp_idx + 1; | |
1715 | } else { | |
1716 | break; | |
1717 | } | |
1718 | } | |
1719 | *erp_idxp = erp_idx; | |
1720 | return erp; | |
1721 | } | |
1722 | ||
1723 | /* | |
1724 | * Return a pointer to the indirection array entry containing the | |
1725 | * extent record at file extent index *idxp. Store the index of the | |
1726 | * target irec in *erp_idxp and store the page index of the target | |
1727 | * extent record in *idxp. | |
1728 | */ | |
1729 | xfs_ext_irec_t * | |
1730 | xfs_iext_idx_to_irec( | |
1731 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1732 | xfs_extnum_t *idxp, /* extent index (file -> page) */ | |
1733 | int *erp_idxp, /* pointer to target irec */ | |
1734 | int realloc) /* new bytes were just added */ | |
1735 | { | |
1736 | xfs_ext_irec_t *prev; /* pointer to previous irec */ | |
1737 | xfs_ext_irec_t *erp = NULL; /* pointer to current irec */ | |
1738 | int erp_idx; /* indirection array index */ | |
1739 | int nlists; /* number of irec's (ex lists) */ | |
1740 | int high; /* binary search upper limit */ | |
1741 | int low; /* binary search lower limit */ | |
1742 | xfs_extnum_t page_idx = *idxp; /* extent index in target list */ | |
1743 | ||
1744 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1745 | ASSERT(page_idx >= 0); | |
5d829300 ES |
1746 | ASSERT(page_idx <= xfs_iext_count(ifp)); |
1747 | ASSERT(page_idx < xfs_iext_count(ifp) || realloc); | |
5c4d97d0 DC |
1748 | |
1749 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1750 | erp_idx = 0; | |
1751 | low = 0; | |
1752 | high = nlists - 1; | |
1753 | ||
1754 | /* Binary search extent irec's */ | |
1755 | while (low <= high) { | |
1756 | erp_idx = (low + high) >> 1; | |
1757 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
1758 | prev = erp_idx > 0 ? erp - 1 : NULL; | |
1759 | if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff && | |
1760 | realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) { | |
1761 | high = erp_idx - 1; | |
1762 | } else if (page_idx > erp->er_extoff + erp->er_extcount || | |
1763 | (page_idx == erp->er_extoff + erp->er_extcount && | |
1764 | !realloc)) { | |
1765 | low = erp_idx + 1; | |
1766 | } else if (page_idx == erp->er_extoff + erp->er_extcount && | |
1767 | erp->er_extcount == XFS_LINEAR_EXTS) { | |
1768 | ASSERT(realloc); | |
1769 | page_idx = 0; | |
1770 | erp_idx++; | |
1771 | erp = erp_idx < nlists ? erp + 1 : NULL; | |
1772 | break; | |
1773 | } else { | |
1774 | page_idx -= erp->er_extoff; | |
1775 | break; | |
1776 | } | |
1777 | } | |
1778 | *idxp = page_idx; | |
1779 | *erp_idxp = erp_idx; | |
d99831ff | 1780 | return erp; |
5c4d97d0 DC |
1781 | } |
1782 | ||
1783 | /* | |
1784 | * Allocate and initialize an indirection array once the space needed | |
1785 | * for incore extents increases above XFS_IEXT_BUFSZ. | |
1786 | */ | |
1787 | void | |
1788 | xfs_iext_irec_init( | |
1789 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
1790 | { | |
1791 | xfs_ext_irec_t *erp; /* indirection array pointer */ | |
1792 | xfs_extnum_t nextents; /* number of extents in file */ | |
1793 | ||
1794 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); | |
5d829300 | 1795 | nextents = xfs_iext_count(ifp); |
5c4d97d0 DC |
1796 | ASSERT(nextents <= XFS_LINEAR_EXTS); |
1797 | ||
1798 | erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS); | |
1799 | ||
1800 | if (nextents == 0) { | |
1801 | ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); | |
1802 | } else if (!ifp->if_real_bytes) { | |
1803 | xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ); | |
1804 | } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) { | |
1805 | xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ); | |
1806 | } | |
1807 | erp->er_extbuf = ifp->if_u1.if_extents; | |
1808 | erp->er_extcount = nextents; | |
1809 | erp->er_extoff = 0; | |
1810 | ||
1811 | ifp->if_flags |= XFS_IFEXTIREC; | |
1812 | ifp->if_real_bytes = XFS_IEXT_BUFSZ; | |
1813 | ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t); | |
1814 | ifp->if_u1.if_ext_irec = erp; | |
1815 | ||
1816 | return; | |
1817 | } | |
1818 | ||
1819 | /* | |
1820 | * Allocate and initialize a new entry in the indirection array. | |
1821 | */ | |
1822 | xfs_ext_irec_t * | |
1823 | xfs_iext_irec_new( | |
1824 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1825 | int erp_idx) /* index for new irec */ | |
1826 | { | |
1827 | xfs_ext_irec_t *erp; /* indirection array pointer */ | |
1828 | int i; /* loop counter */ | |
1829 | int nlists; /* number of irec's (ex lists) */ | |
1830 | ||
1831 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1832 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1833 | ||
1834 | /* Resize indirection array */ | |
1835 | xfs_iext_realloc_indirect(ifp, ++nlists * | |
1836 | sizeof(xfs_ext_irec_t)); | |
1837 | /* | |
1838 | * Move records down in the array so the | |
1839 | * new page can use erp_idx. | |
1840 | */ | |
1841 | erp = ifp->if_u1.if_ext_irec; | |
1842 | for (i = nlists - 1; i > erp_idx; i--) { | |
1843 | memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t)); | |
1844 | } | |
1845 | ASSERT(i == erp_idx); | |
1846 | ||
1847 | /* Initialize new extent record */ | |
1848 | erp = ifp->if_u1.if_ext_irec; | |
1849 | erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); | |
1850 | ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; | |
1851 | memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ); | |
1852 | erp[erp_idx].er_extcount = 0; | |
1853 | erp[erp_idx].er_extoff = erp_idx > 0 ? | |
1854 | erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0; | |
1855 | return (&erp[erp_idx]); | |
1856 | } | |
1857 | ||
1858 | /* | |
1859 | * Remove a record from the indirection array. | |
1860 | */ | |
1861 | void | |
1862 | xfs_iext_irec_remove( | |
1863 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1864 | int erp_idx) /* irec index to remove */ | |
1865 | { | |
1866 | xfs_ext_irec_t *erp; /* indirection array pointer */ | |
1867 | int i; /* loop counter */ | |
1868 | int nlists; /* number of irec's (ex lists) */ | |
1869 | ||
1870 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1871 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1872 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
1873 | if (erp->er_extbuf) { | |
1874 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, | |
1875 | -erp->er_extcount); | |
1876 | kmem_free(erp->er_extbuf); | |
1877 | } | |
1878 | /* Compact extent records */ | |
1879 | erp = ifp->if_u1.if_ext_irec; | |
1880 | for (i = erp_idx; i < nlists - 1; i++) { | |
1881 | memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t)); | |
1882 | } | |
1883 | /* | |
1884 | * Manually free the last extent record from the indirection | |
1885 | * array. A call to xfs_iext_realloc_indirect() with a size | |
1886 | * of zero would result in a call to xfs_iext_destroy() which | |
1887 | * would in turn call this function again, creating a nasty | |
1888 | * infinite loop. | |
1889 | */ | |
1890 | if (--nlists) { | |
1891 | xfs_iext_realloc_indirect(ifp, | |
1892 | nlists * sizeof(xfs_ext_irec_t)); | |
1893 | } else { | |
1894 | kmem_free(ifp->if_u1.if_ext_irec); | |
1895 | } | |
1896 | ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; | |
1897 | } | |
1898 | ||
1899 | /* | |
1900 | * This is called to clean up large amounts of unused memory allocated | |
1901 | * by the indirection array. Before compacting anything though, verify | |
1902 | * that the indirection array is still needed and switch back to the | |
1903 | * linear extent list (or even the inline buffer) if possible. The | |
1904 | * compaction policy is as follows: | |
1905 | * | |
1906 | * Full Compaction: Extents fit into a single page (or inline buffer) | |
1907 | * Partial Compaction: Extents occupy less than 50% of allocated space | |
1908 | * No Compaction: Extents occupy at least 50% of allocated space | |
1909 | */ | |
1910 | void | |
1911 | xfs_iext_irec_compact( | |
1912 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
1913 | { | |
1914 | xfs_extnum_t nextents; /* number of extents in file */ | |
1915 | int nlists; /* number of irec's (ex lists) */ | |
1916 | ||
1917 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1918 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
5d829300 | 1919 | nextents = xfs_iext_count(ifp); |
5c4d97d0 DC |
1920 | |
1921 | if (nextents == 0) { | |
1922 | xfs_iext_destroy(ifp); | |
1923 | } else if (nextents <= XFS_INLINE_EXTS) { | |
1924 | xfs_iext_indirect_to_direct(ifp); | |
1925 | xfs_iext_direct_to_inline(ifp, nextents); | |
1926 | } else if (nextents <= XFS_LINEAR_EXTS) { | |
1927 | xfs_iext_indirect_to_direct(ifp); | |
1928 | } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) { | |
1929 | xfs_iext_irec_compact_pages(ifp); | |
1930 | } | |
1931 | } | |
1932 | ||
1933 | /* | |
1934 | * Combine extents from neighboring extent pages. | |
1935 | */ | |
1936 | void | |
1937 | xfs_iext_irec_compact_pages( | |
1938 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
1939 | { | |
1940 | xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */ | |
1941 | int erp_idx = 0; /* indirection array index */ | |
1942 | int nlists; /* number of irec's (ex lists) */ | |
1943 | ||
1944 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1945 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1946 | while (erp_idx < nlists - 1) { | |
1947 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
1948 | erp_next = erp + 1; | |
1949 | if (erp_next->er_extcount <= | |
1950 | (XFS_LINEAR_EXTS - erp->er_extcount)) { | |
1951 | memcpy(&erp->er_extbuf[erp->er_extcount], | |
1952 | erp_next->er_extbuf, erp_next->er_extcount * | |
1953 | sizeof(xfs_bmbt_rec_t)); | |
1954 | erp->er_extcount += erp_next->er_extcount; | |
1955 | /* | |
1956 | * Free page before removing extent record | |
1957 | * so er_extoffs don't get modified in | |
1958 | * xfs_iext_irec_remove. | |
1959 | */ | |
1960 | kmem_free(erp_next->er_extbuf); | |
1961 | erp_next->er_extbuf = NULL; | |
1962 | xfs_iext_irec_remove(ifp, erp_idx + 1); | |
1963 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1964 | } else { | |
1965 | erp_idx++; | |
1966 | } | |
1967 | } | |
1968 | } | |
1969 | ||
1970 | /* | |
1971 | * This is called to update the er_extoff field in the indirection | |
1972 | * array when extents have been added or removed from one of the | |
1973 | * extent lists. erp_idx contains the irec index to begin updating | |
1974 | * at and ext_diff contains the number of extents that were added | |
1975 | * or removed. | |
1976 | */ | |
1977 | void | |
1978 | xfs_iext_irec_update_extoffs( | |
1979 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1980 | int erp_idx, /* irec index to update */ | |
1981 | int ext_diff) /* number of new extents */ | |
1982 | { | |
1983 | int i; /* loop counter */ | |
1984 | int nlists; /* number of irec's (ex lists */ | |
1985 | ||
1986 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
1987 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
1988 | for (i = erp_idx; i < nlists; i++) { | |
1989 | ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff; | |
1990 | } | |
1991 | } | |
3993baeb DW |
1992 | |
1993 | /* | |
1994 | * Initialize an inode's copy-on-write fork. | |
1995 | */ | |
1996 | void | |
1997 | xfs_ifork_init_cow( | |
1998 | struct xfs_inode *ip) | |
1999 | { | |
2000 | if (ip->i_cowfp) | |
2001 | return; | |
2002 | ||
2003 | ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone, | |
2004 | KM_SLEEP | KM_NOFS); | |
2005 | ip->i_cowfp->if_flags = XFS_IFEXTENTS; | |
2006 | ip->i_cformat = XFS_DINODE_FMT_EXTENTS; | |
2007 | ip->i_cnextents = 0; | |
2008 | } | |
93533c78 CH |
2009 | |
2010 | /* | |
2011 | * Lookup the extent covering bno. | |
2012 | * | |
2013 | * If there is an extent covering bno return the extent index, and store the | |
2014 | * expanded extent structure in *gotp, and the extent index in *idx. | |
2015 | * If there is no extent covering bno, but there is an extent after it (e.g. | |
2016 | * it lies in a hole) return that extent in *gotp and its index in *idx | |
2017 | * instead. | |
2018 | * If bno is beyond the last extent return false, and return the index after | |
2019 | * the last valid index in *idxp. | |
2020 | */ | |
2021 | bool | |
2022 | xfs_iext_lookup_extent( | |
2023 | struct xfs_inode *ip, | |
2024 | struct xfs_ifork *ifp, | |
2025 | xfs_fileoff_t bno, | |
2026 | xfs_extnum_t *idxp, | |
2027 | struct xfs_bmbt_irec *gotp) | |
2028 | { | |
2029 | struct xfs_bmbt_rec_host *ep; | |
2030 | ||
2031 | XFS_STATS_INC(ip->i_mount, xs_look_exlist); | |
2032 | ||
2033 | ep = xfs_iext_bno_to_ext(ifp, bno, idxp); | |
2034 | if (!ep) | |
2035 | return false; | |
2036 | xfs_bmbt_get_all(ep, gotp); | |
2037 | return true; | |
2038 | } | |
2039 | ||
2040 | /* | |
2041 | * Return true if there is an extent at index idx, and return the expanded | |
2042 | * extent structure at idx in that case. Else return false. | |
2043 | */ | |
2044 | bool | |
2045 | xfs_iext_get_extent( | |
2046 | struct xfs_ifork *ifp, | |
2047 | xfs_extnum_t idx, | |
2048 | struct xfs_bmbt_irec *gotp) | |
2049 | { | |
2050 | if (idx < 0 || idx >= xfs_iext_count(ifp)) | |
2051 | return false; | |
2052 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp); | |
2053 | return true; | |
2054 | } |