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