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