]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
3e57ecf6 | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
7b718769 | 3 | * All Rights Reserved. |
1da177e4 | 4 | * |
7b718769 NS |
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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
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. | |
1da177e4 | 13 | * |
7b718769 NS |
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 | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
a844f451 | 21 | #include "xfs_bit.h" |
1da177e4 | 22 | #include "xfs_log.h" |
a844f451 | 23 | #include "xfs_inum.h" |
1da177e4 LT |
24 | #include "xfs_trans.h" |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_ag.h" | |
1da177e4 | 27 | #include "xfs_dir2.h" |
f5ea1100 | 28 | #include "xfs_mount.h" |
a844f451 | 29 | #include "xfs_da_btree.h" |
1da177e4 | 30 | #include "xfs_bmap_btree.h" |
a844f451 | 31 | #include "xfs_alloc_btree.h" |
1da177e4 | 32 | #include "xfs_ialloc_btree.h" |
1da177e4 | 33 | #include "xfs_dinode.h" |
1da177e4 | 34 | #include "xfs_inode.h" |
a844f451 | 35 | #include "xfs_btree.h" |
a844f451 | 36 | #include "xfs_mount.h" |
1da177e4 | 37 | #include "xfs_itable.h" |
a844f451 | 38 | #include "xfs_inode_item.h" |
1da177e4 LT |
39 | #include "xfs_extfree_item.h" |
40 | #include "xfs_alloc.h" | |
41 | #include "xfs_bmap.h" | |
42 | #include "xfs_rtalloc.h" | |
43 | #include "xfs_error.h" | |
d8cc890d | 44 | #include "xfs_attr_leaf.h" |
1da177e4 LT |
45 | #include "xfs_quota.h" |
46 | #include "xfs_trans_space.h" | |
47 | #include "xfs_buf_item.h" | |
2a82b8be | 48 | #include "xfs_filestream.h" |
739bfb2a | 49 | #include "xfs_vnodeops.h" |
0b1b213f | 50 | #include "xfs_trace.h" |
19de7351 | 51 | #include "xfs_symlink.h" |
1da177e4 LT |
52 | |
53 | ||
1da177e4 LT |
54 | kmem_zone_t *xfs_bmap_free_item_zone; |
55 | ||
56 | /* | |
9e5987a7 | 57 | * Miscellaneous helper functions |
1da177e4 LT |
58 | */ |
59 | ||
1da177e4 | 60 | /* |
9e5987a7 DC |
61 | * Compute and fill in the value of the maximum depth of a bmap btree |
62 | * in this filesystem. Done once, during mount. | |
1da177e4 | 63 | */ |
9e5987a7 DC |
64 | void |
65 | xfs_bmap_compute_maxlevels( | |
66 | xfs_mount_t *mp, /* file system mount structure */ | |
67 | int whichfork) /* data or attr fork */ | |
68 | { | |
69 | int level; /* btree level */ | |
70 | uint maxblocks; /* max blocks at this level */ | |
71 | uint maxleafents; /* max leaf entries possible */ | |
72 | int maxrootrecs; /* max records in root block */ | |
73 | int minleafrecs; /* min records in leaf block */ | |
74 | int minnoderecs; /* min records in node block */ | |
75 | int sz; /* root block size */ | |
1da177e4 | 76 | |
9e5987a7 DC |
77 | /* |
78 | * The maximum number of extents in a file, hence the maximum | |
79 | * number of leaf entries, is controlled by the type of di_nextents | |
80 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents | |
81 | * (a signed 16-bit number, xfs_aextnum_t). | |
82 | * | |
83 | * Note that we can no longer assume that if we are in ATTR1 that | |
84 | * the fork offset of all the inodes will be | |
85 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted | |
86 | * with ATTR2 and then mounted back with ATTR1, keeping the | |
87 | * di_forkoff's fixed but probably at various positions. Therefore, | |
88 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario | |
89 | * of a minimum size available. | |
90 | */ | |
91 | if (whichfork == XFS_DATA_FORK) { | |
92 | maxleafents = MAXEXTNUM; | |
93 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); | |
94 | } else { | |
95 | maxleafents = MAXAEXTNUM; | |
96 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); | |
97 | } | |
98 | maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); | |
99 | minleafrecs = mp->m_bmap_dmnr[0]; | |
100 | minnoderecs = mp->m_bmap_dmnr[1]; | |
101 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; | |
102 | for (level = 1; maxblocks > 1; level++) { | |
103 | if (maxblocks <= maxrootrecs) | |
104 | maxblocks = 1; | |
105 | else | |
106 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; | |
107 | } | |
108 | mp->m_bm_maxlevels[whichfork] = level; | |
109 | } | |
91e11088 | 110 | |
1da177e4 | 111 | /* |
9e5987a7 DC |
112 | * Convert the given file system block to a disk block. We have to treat it |
113 | * differently based on whether the file is a real time file or not, because the | |
114 | * bmap code does. | |
1da177e4 | 115 | */ |
9e5987a7 DC |
116 | xfs_daddr_t |
117 | xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb) | |
118 | { | |
119 | return (XFS_IS_REALTIME_INODE(ip) ? \ | |
120 | (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \ | |
121 | XFS_FSB_TO_DADDR((ip)->i_mount, (fsb))); | |
122 | } | |
1da177e4 | 123 | |
fe033cc8 CH |
124 | STATIC int /* error */ |
125 | xfs_bmbt_lookup_eq( | |
126 | struct xfs_btree_cur *cur, | |
127 | xfs_fileoff_t off, | |
128 | xfs_fsblock_t bno, | |
129 | xfs_filblks_t len, | |
130 | int *stat) /* success/failure */ | |
131 | { | |
132 | cur->bc_rec.b.br_startoff = off; | |
133 | cur->bc_rec.b.br_startblock = bno; | |
134 | cur->bc_rec.b.br_blockcount = len; | |
135 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); | |
136 | } | |
137 | ||
138 | STATIC int /* error */ | |
139 | xfs_bmbt_lookup_ge( | |
140 | struct xfs_btree_cur *cur, | |
141 | xfs_fileoff_t off, | |
142 | xfs_fsblock_t bno, | |
143 | xfs_filblks_t len, | |
144 | int *stat) /* success/failure */ | |
145 | { | |
146 | cur->bc_rec.b.br_startoff = off; | |
147 | cur->bc_rec.b.br_startblock = bno; | |
148 | cur->bc_rec.b.br_blockcount = len; | |
149 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); | |
150 | } | |
151 | ||
278d0ca1 | 152 | /* |
8096b1eb CH |
153 | * Check if the inode needs to be converted to btree format. |
154 | */ | |
155 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) | |
156 | { | |
157 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && | |
158 | XFS_IFORK_NEXTENTS(ip, whichfork) > | |
159 | XFS_IFORK_MAXEXT(ip, whichfork); | |
160 | } | |
161 | ||
162 | /* | |
163 | * Check if the inode should be converted to extent format. | |
164 | */ | |
165 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) | |
166 | { | |
167 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && | |
168 | XFS_IFORK_NEXTENTS(ip, whichfork) <= | |
169 | XFS_IFORK_MAXEXT(ip, whichfork); | |
170 | } | |
171 | ||
172 | /* | |
173 | * Update the record referred to by cur to the value given | |
278d0ca1 CH |
174 | * by [off, bno, len, state]. |
175 | * This either works (return 0) or gets an EFSCORRUPTED error. | |
176 | */ | |
177 | STATIC int | |
178 | xfs_bmbt_update( | |
179 | struct xfs_btree_cur *cur, | |
180 | xfs_fileoff_t off, | |
181 | xfs_fsblock_t bno, | |
182 | xfs_filblks_t len, | |
183 | xfs_exntst_t state) | |
184 | { | |
185 | union xfs_btree_rec rec; | |
186 | ||
187 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); | |
188 | return xfs_btree_update(cur, &rec); | |
189 | } | |
fe033cc8 | 190 | |
1da177e4 | 191 | /* |
9e5987a7 DC |
192 | * Compute the worst-case number of indirect blocks that will be used |
193 | * for ip's delayed extent of length "len". | |
1da177e4 | 194 | */ |
9e5987a7 DC |
195 | STATIC xfs_filblks_t |
196 | xfs_bmap_worst_indlen( | |
197 | xfs_inode_t *ip, /* incore inode pointer */ | |
198 | xfs_filblks_t len) /* delayed extent length */ | |
1da177e4 | 199 | { |
9e5987a7 DC |
200 | int level; /* btree level number */ |
201 | int maxrecs; /* maximum record count at this level */ | |
202 | xfs_mount_t *mp; /* mount structure */ | |
203 | xfs_filblks_t rval; /* return value */ | |
1da177e4 LT |
204 | |
205 | mp = ip->i_mount; | |
9e5987a7 DC |
206 | maxrecs = mp->m_bmap_dmxr[0]; |
207 | for (level = 0, rval = 0; | |
208 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); | |
209 | level++) { | |
210 | len += maxrecs - 1; | |
211 | do_div(len, maxrecs); | |
212 | rval += len; | |
213 | if (len == 1) | |
214 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - | |
215 | level - 1; | |
216 | if (level == 0) | |
217 | maxrecs = mp->m_bmap_dmxr[1]; | |
1da177e4 | 218 | } |
9e5987a7 | 219 | return rval; |
1da177e4 LT |
220 | } |
221 | ||
222 | /* | |
9e5987a7 | 223 | * Calculate the default attribute fork offset for newly created inodes. |
1da177e4 | 224 | */ |
9e5987a7 DC |
225 | uint |
226 | xfs_default_attroffset( | |
227 | struct xfs_inode *ip) | |
1da177e4 | 228 | { |
9e5987a7 DC |
229 | struct xfs_mount *mp = ip->i_mount; |
230 | uint offset; | |
1da177e4 | 231 | |
9e5987a7 | 232 | if (mp->m_sb.sb_inodesize == 256) { |
56cea2d0 | 233 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
9e5987a7 DC |
234 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
235 | } else { | |
236 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); | |
1da177e4 | 237 | } |
9e5987a7 | 238 | |
56cea2d0 | 239 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
9e5987a7 | 240 | return offset; |
1da177e4 LT |
241 | } |
242 | ||
243 | /* | |
9e5987a7 DC |
244 | * Helper routine to reset inode di_forkoff field when switching |
245 | * attribute fork from local to extent format - we reset it where | |
246 | * possible to make space available for inline data fork extents. | |
1e82379b DC |
247 | */ |
248 | STATIC void | |
9e5987a7 DC |
249 | xfs_bmap_forkoff_reset( |
250 | xfs_mount_t *mp, | |
251 | xfs_inode_t *ip, | |
252 | int whichfork) | |
1e82379b | 253 | { |
9e5987a7 DC |
254 | if (whichfork == XFS_ATTR_FORK && |
255 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && | |
256 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && | |
257 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { | |
258 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; | |
259 | ||
260 | if (dfl_forkoff > ip->i_d.di_forkoff) | |
261 | ip->i_d.di_forkoff = dfl_forkoff; | |
262 | } | |
1e82379b DC |
263 | } |
264 | ||
9e5987a7 DC |
265 | /* |
266 | * Extent tree block counting routines. | |
267 | */ | |
268 | ||
269 | /* | |
270 | * Count leaf blocks given a range of extent records. | |
271 | */ | |
1e82379b | 272 | STATIC void |
9e5987a7 DC |
273 | xfs_bmap_count_leaves( |
274 | xfs_ifork_t *ifp, | |
275 | xfs_extnum_t idx, | |
276 | int numrecs, | |
277 | int *count) | |
1e82379b | 278 | { |
9e5987a7 DC |
279 | int b; |
280 | ||
281 | for (b = 0; b < numrecs; b++) { | |
282 | xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b); | |
283 | *count += xfs_bmbt_get_blockcount(frp); | |
284 | } | |
1e82379b DC |
285 | } |
286 | ||
287 | /* | |
9e5987a7 DC |
288 | * Count leaf blocks given a range of extent records originally |
289 | * in btree format. | |
1da177e4 | 290 | */ |
9e5987a7 DC |
291 | STATIC void |
292 | xfs_bmap_disk_count_leaves( | |
293 | struct xfs_mount *mp, | |
294 | struct xfs_btree_block *block, | |
295 | int numrecs, | |
296 | int *count) | |
1da177e4 | 297 | { |
9e5987a7 DC |
298 | int b; |
299 | xfs_bmbt_rec_t *frp; | |
1e82379b | 300 | |
9e5987a7 DC |
301 | for (b = 1; b <= numrecs; b++) { |
302 | frp = XFS_BMBT_REC_ADDR(mp, block, b); | |
303 | *count += xfs_bmbt_disk_get_blockcount(frp); | |
1e82379b | 304 | } |
1da177e4 LT |
305 | } |
306 | ||
307 | /* | |
9e5987a7 DC |
308 | * Recursively walks each level of a btree |
309 | * to count total fsblocks is use. | |
1da177e4 | 310 | */ |
9e5987a7 DC |
311 | STATIC int /* error */ |
312 | xfs_bmap_count_tree( | |
313 | xfs_mount_t *mp, /* file system mount point */ | |
314 | xfs_trans_t *tp, /* transaction pointer */ | |
315 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
316 | xfs_fsblock_t blockno, /* file system block number */ | |
317 | int levelin, /* level in btree */ | |
318 | int *count) /* Count of blocks */ | |
1da177e4 | 319 | { |
9e5987a7 DC |
320 | int error; |
321 | xfs_buf_t *bp, *nbp; | |
322 | int level = levelin; | |
323 | __be64 *pp; | |
324 | xfs_fsblock_t bno = blockno; | |
325 | xfs_fsblock_t nextbno; | |
326 | struct xfs_btree_block *block, *nextblock; | |
327 | int numrecs; | |
1da177e4 | 328 | |
9e5987a7 DC |
329 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF, |
330 | &xfs_bmbt_buf_ops); | |
331 | if (error) | |
332 | return error; | |
333 | *count += 1; | |
334 | block = XFS_BUF_TO_BLOCK(bp); | |
a5bd606b | 335 | |
9e5987a7 DC |
336 | if (--level) { |
337 | /* Not at node above leaves, count this level of nodes */ | |
338 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); | |
339 | while (nextbno != NULLFSBLOCK) { | |
340 | error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp, | |
341 | XFS_BMAP_BTREE_REF, | |
342 | &xfs_bmbt_buf_ops); | |
343 | if (error) | |
344 | return error; | |
345 | *count += 1; | |
346 | nextblock = XFS_BUF_TO_BLOCK(nbp); | |
347 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); | |
348 | xfs_trans_brelse(tp, nbp); | |
349 | } | |
a5bd606b | 350 | |
9e5987a7 DC |
351 | /* Dive to the next level */ |
352 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); | |
353 | bno = be64_to_cpu(*pp); | |
354 | if (unlikely((error = | |
355 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { | |
356 | xfs_trans_brelse(tp, bp); | |
357 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", | |
358 | XFS_ERRLEVEL_LOW, mp); | |
359 | return XFS_ERROR(EFSCORRUPTED); | |
360 | } | |
361 | xfs_trans_brelse(tp, bp); | |
362 | } else { | |
363 | /* count all level 1 nodes and their leaves */ | |
364 | for (;;) { | |
365 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); | |
366 | numrecs = be16_to_cpu(block->bb_numrecs); | |
367 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); | |
368 | xfs_trans_brelse(tp, bp); | |
369 | if (nextbno == NULLFSBLOCK) | |
370 | break; | |
371 | bno = nextbno; | |
372 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, | |
373 | XFS_BMAP_BTREE_REF, | |
374 | &xfs_bmbt_buf_ops); | |
375 | if (error) | |
376 | return error; | |
377 | *count += 1; | |
378 | block = XFS_BUF_TO_BLOCK(bp); | |
379 | } | |
380 | } | |
381 | return 0; | |
382 | } | |
a5bd606b | 383 | |
9e5987a7 DC |
384 | /* |
385 | * Count fsblocks of the given fork. | |
386 | */ | |
387 | int /* error */ | |
388 | xfs_bmap_count_blocks( | |
389 | xfs_trans_t *tp, /* transaction pointer */ | |
390 | xfs_inode_t *ip, /* incore inode */ | |
391 | int whichfork, /* data or attr fork */ | |
392 | int *count) /* out: count of blocks */ | |
393 | { | |
394 | struct xfs_btree_block *block; /* current btree block */ | |
395 | xfs_fsblock_t bno; /* block # of "block" */ | |
396 | xfs_ifork_t *ifp; /* fork structure */ | |
397 | int level; /* btree level, for checking */ | |
398 | xfs_mount_t *mp; /* file system mount structure */ | |
399 | __be64 *pp; /* pointer to block address */ | |
400 | ||
401 | bno = NULLFSBLOCK; | |
402 | mp = ip->i_mount; | |
403 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
404 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { | |
405 | xfs_bmap_count_leaves(ifp, 0, | |
406 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), | |
407 | count); | |
408 | return 0; | |
409 | } | |
1da177e4 LT |
410 | |
411 | /* | |
9e5987a7 | 412 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
1da177e4 | 413 | */ |
9e5987a7 DC |
414 | block = ifp->if_broot; |
415 | level = be16_to_cpu(block->bb_level); | |
416 | ASSERT(level > 0); | |
417 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); | |
418 | bno = be64_to_cpu(*pp); | |
419 | ASSERT(bno != NULLDFSBNO); | |
420 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); | |
421 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); | |
7574aa92 | 422 | |
9e5987a7 DC |
423 | if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) { |
424 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, | |
425 | mp); | |
426 | return XFS_ERROR(EFSCORRUPTED); | |
427 | } | |
a5bd606b | 428 | |
9e5987a7 DC |
429 | return 0; |
430 | } | |
7574aa92 | 431 | |
9e5987a7 DC |
432 | /* |
433 | * Debug/sanity checking code | |
434 | */ | |
7574aa92 | 435 | |
9e5987a7 DC |
436 | STATIC int |
437 | xfs_bmap_sanity_check( | |
438 | struct xfs_mount *mp, | |
439 | struct xfs_buf *bp, | |
440 | int level) | |
441 | { | |
442 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); | |
7574aa92 | 443 | |
ee1a47ab CH |
444 | if (block->bb_magic != cpu_to_be32(XFS_BMAP_CRC_MAGIC) && |
445 | block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC)) | |
446 | return 0; | |
447 | ||
448 | if (be16_to_cpu(block->bb_level) != level || | |
9e5987a7 DC |
449 | be16_to_cpu(block->bb_numrecs) == 0 || |
450 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) | |
451 | return 0; | |
ee1a47ab | 452 | |
9e5987a7 DC |
453 | return 1; |
454 | } | |
7574aa92 | 455 | |
9e5987a7 DC |
456 | #ifdef DEBUG |
457 | STATIC struct xfs_buf * | |
458 | xfs_bmap_get_bp( | |
459 | struct xfs_btree_cur *cur, | |
460 | xfs_fsblock_t bno) | |
461 | { | |
462 | struct xfs_log_item_desc *lidp; | |
463 | int i; | |
7574aa92 | 464 | |
9e5987a7 DC |
465 | if (!cur) |
466 | return NULL; | |
467 | ||
468 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { | |
469 | if (!cur->bc_bufs[i]) | |
470 | break; | |
471 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) | |
472 | return cur->bc_bufs[i]; | |
1da177e4 | 473 | } |
7574aa92 | 474 | |
9e5987a7 DC |
475 | /* Chase down all the log items to see if the bp is there */ |
476 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { | |
477 | struct xfs_buf_log_item *bip; | |
478 | bip = (struct xfs_buf_log_item *)lidp->lid_item; | |
479 | if (bip->bli_item.li_type == XFS_LI_BUF && | |
480 | XFS_BUF_ADDR(bip->bli_buf) == bno) | |
481 | return bip->bli_buf; | |
482 | } | |
7574aa92 | 483 | |
9e5987a7 DC |
484 | return NULL; |
485 | } | |
0b1b213f | 486 | |
9e5987a7 DC |
487 | STATIC void |
488 | xfs_check_block( | |
489 | struct xfs_btree_block *block, | |
490 | xfs_mount_t *mp, | |
491 | int root, | |
492 | short sz) | |
493 | { | |
494 | int i, j, dmxr; | |
495 | __be64 *pp, *thispa; /* pointer to block address */ | |
496 | xfs_bmbt_key_t *prevp, *keyp; | |
1da177e4 | 497 | |
9e5987a7 | 498 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
ec90c556 | 499 | |
9e5987a7 DC |
500 | prevp = NULL; |
501 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { | |
502 | dmxr = mp->m_bmap_dmxr[0]; | |
503 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); | |
0b1b213f | 504 | |
9e5987a7 DC |
505 | if (prevp) { |
506 | ASSERT(be64_to_cpu(prevp->br_startoff) < | |
507 | be64_to_cpu(keyp->br_startoff)); | |
1da177e4 | 508 | } |
9e5987a7 | 509 | prevp = keyp; |
1da177e4 | 510 | |
1da177e4 | 511 | /* |
9e5987a7 | 512 | * Compare the block numbers to see if there are dups. |
1da177e4 | 513 | */ |
9e5987a7 DC |
514 | if (root) |
515 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); | |
516 | else | |
517 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); | |
0b1b213f | 518 | |
9e5987a7 DC |
519 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
520 | if (root) | |
521 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); | |
522 | else | |
523 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); | |
524 | if (*thispa == *pp) { | |
525 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", | |
526 | __func__, j, i, | |
527 | (unsigned long long)be64_to_cpu(*thispa)); | |
528 | panic("%s: ptrs are equal in node\n", | |
529 | __func__); | |
530 | } | |
1da177e4 | 531 | } |
9e5987a7 DC |
532 | } |
533 | } | |
1da177e4 | 534 | |
9e5987a7 DC |
535 | /* |
536 | * Check that the extents for the inode ip are in the right order in all | |
537 | * btree leaves. | |
538 | */ | |
0b1b213f | 539 | |
9e5987a7 DC |
540 | STATIC void |
541 | xfs_bmap_check_leaf_extents( | |
542 | xfs_btree_cur_t *cur, /* btree cursor or null */ | |
543 | xfs_inode_t *ip, /* incore inode pointer */ | |
544 | int whichfork) /* data or attr fork */ | |
545 | { | |
546 | struct xfs_btree_block *block; /* current btree block */ | |
547 | xfs_fsblock_t bno; /* block # of "block" */ | |
548 | xfs_buf_t *bp; /* buffer for "block" */ | |
549 | int error; /* error return value */ | |
550 | xfs_extnum_t i=0, j; /* index into the extents list */ | |
551 | xfs_ifork_t *ifp; /* fork structure */ | |
552 | int level; /* btree level, for checking */ | |
553 | xfs_mount_t *mp; /* file system mount structure */ | |
554 | __be64 *pp; /* pointer to block address */ | |
555 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ | |
556 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ | |
557 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ | |
558 | int bp_release = 0; | |
559 | ||
560 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { | |
561 | return; | |
562 | } | |
563 | ||
564 | bno = NULLFSBLOCK; | |
565 | mp = ip->i_mount; | |
566 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
567 | block = ifp->if_broot; | |
568 | /* | |
569 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. | |
570 | */ | |
571 | level = be16_to_cpu(block->bb_level); | |
572 | ASSERT(level > 0); | |
573 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); | |
574 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); | |
575 | bno = be64_to_cpu(*pp); | |
576 | ||
577 | ASSERT(bno != NULLDFSBNO); | |
578 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); | |
579 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); | |
580 | ||
581 | /* | |
582 | * Go down the tree until leaf level is reached, following the first | |
583 | * pointer (leftmost) at each level. | |
584 | */ | |
585 | while (level-- > 0) { | |
586 | /* See if buf is in cur first */ | |
587 | bp_release = 0; | |
588 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); | |
589 | if (!bp) { | |
590 | bp_release = 1; | |
591 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, | |
592 | XFS_BMAP_BTREE_REF, | |
593 | &xfs_bmbt_buf_ops); | |
572a4cf0 | 594 | if (error) |
9e5987a7 | 595 | goto error_norelse; |
1da177e4 | 596 | } |
9e5987a7 DC |
597 | block = XFS_BUF_TO_BLOCK(bp); |
598 | XFS_WANT_CORRUPTED_GOTO( | |
599 | xfs_bmap_sanity_check(mp, bp, level), | |
600 | error0); | |
601 | if (level == 0) | |
602 | break; | |
1da177e4 | 603 | |
1da177e4 | 604 | /* |
9e5987a7 DC |
605 | * Check this block for basic sanity (increasing keys and |
606 | * no duplicate blocks). | |
1da177e4 | 607 | */ |
0b1b213f | 608 | |
9e5987a7 DC |
609 | xfs_check_block(block, mp, 0, 0); |
610 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); | |
611 | bno = be64_to_cpu(*pp); | |
612 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); | |
613 | if (bp_release) { | |
614 | bp_release = 0; | |
615 | xfs_trans_brelse(NULL, bp); | |
1da177e4 | 616 | } |
9e5987a7 | 617 | } |
ec90c556 | 618 | |
9e5987a7 DC |
619 | /* |
620 | * Here with bp and block set to the leftmost leaf node in the tree. | |
621 | */ | |
622 | i = 0; | |
623 | ||
624 | /* | |
625 | * Loop over all leaf nodes checking that all extents are in the right order. | |
626 | */ | |
627 | for (;;) { | |
628 | xfs_fsblock_t nextbno; | |
629 | xfs_extnum_t num_recs; | |
630 | ||
631 | ||
632 | num_recs = xfs_btree_get_numrecs(block); | |
1da177e4 | 633 | |
1da177e4 | 634 | /* |
9e5987a7 | 635 | * Read-ahead the next leaf block, if any. |
1da177e4 | 636 | */ |
8096b1eb | 637 | |
9e5987a7 | 638 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
1da177e4 | 639 | |
1da177e4 | 640 | /* |
9e5987a7 DC |
641 | * Check all the extents to make sure they are OK. |
642 | * If we had a previous block, the last entry should | |
643 | * conform with the first entry in this one. | |
1da177e4 | 644 | */ |
ec90c556 | 645 | |
9e5987a7 DC |
646 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
647 | if (i) { | |
648 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + | |
649 | xfs_bmbt_disk_get_blockcount(&last) <= | |
650 | xfs_bmbt_disk_get_startoff(ep)); | |
651 | } | |
652 | for (j = 1; j < num_recs; j++) { | |
653 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); | |
654 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + | |
655 | xfs_bmbt_disk_get_blockcount(ep) <= | |
656 | xfs_bmbt_disk_get_startoff(nextp)); | |
657 | ep = nextp; | |
658 | } | |
1da177e4 | 659 | |
9e5987a7 DC |
660 | last = *ep; |
661 | i += num_recs; | |
662 | if (bp_release) { | |
663 | bp_release = 0; | |
664 | xfs_trans_brelse(NULL, bp); | |
665 | } | |
666 | bno = nextbno; | |
1da177e4 | 667 | /* |
9e5987a7 | 668 | * If we've reached the end, stop. |
1da177e4 | 669 | */ |
9e5987a7 DC |
670 | if (bno == NULLFSBLOCK) |
671 | break; | |
8096b1eb | 672 | |
9e5987a7 DC |
673 | bp_release = 0; |
674 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); | |
675 | if (!bp) { | |
676 | bp_release = 1; | |
677 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, | |
678 | XFS_BMAP_BTREE_REF, | |
679 | &xfs_bmbt_buf_ops); | |
b9b984d7 | 680 | if (error) |
9e5987a7 | 681 | goto error_norelse; |
1da177e4 | 682 | } |
9e5987a7 | 683 | block = XFS_BUF_TO_BLOCK(bp); |
a5bd606b | 684 | } |
9e5987a7 DC |
685 | if (bp_release) { |
686 | bp_release = 0; | |
687 | xfs_trans_brelse(NULL, bp); | |
a5bd606b | 688 | } |
9e5987a7 | 689 | return; |
a5bd606b | 690 | |
9e5987a7 DC |
691 | error0: |
692 | xfs_warn(mp, "%s: at error0", __func__); | |
693 | if (bp_release) | |
694 | xfs_trans_brelse(NULL, bp); | |
695 | error_norelse: | |
696 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", | |
697 | __func__, i); | |
698 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); | |
699 | return; | |
1da177e4 LT |
700 | } |
701 | ||
702 | /* | |
9e5987a7 | 703 | * Add bmap trace insert entries for all the contents of the extent records. |
1da177e4 | 704 | */ |
9e5987a7 DC |
705 | void |
706 | xfs_bmap_trace_exlist( | |
707 | xfs_inode_t *ip, /* incore inode pointer */ | |
708 | xfs_extnum_t cnt, /* count of entries in the list */ | |
709 | int whichfork, /* data or attr fork */ | |
710 | unsigned long caller_ip) | |
1da177e4 | 711 | { |
9e5987a7 DC |
712 | xfs_extnum_t idx; /* extent record index */ |
713 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
714 | int state = 0; | |
a5bd606b | 715 | |
9e5987a7 DC |
716 | if (whichfork == XFS_ATTR_FORK) |
717 | state |= BMAP_ATTRFORK; | |
a5bd606b | 718 | |
9e5987a7 DC |
719 | ifp = XFS_IFORK_PTR(ip, whichfork); |
720 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); | |
721 | for (idx = 0; idx < cnt; idx++) | |
722 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); | |
723 | } | |
a5bd606b | 724 | |
9e5987a7 DC |
725 | /* |
726 | * Validate that the bmbt_irecs being returned from bmapi are valid | |
727 | * given the callers original parameters. Specifically check the | |
728 | * ranges of the returned irecs to ensure that they only extent beyond | |
729 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. | |
730 | */ | |
731 | STATIC void | |
732 | xfs_bmap_validate_ret( | |
733 | xfs_fileoff_t bno, | |
734 | xfs_filblks_t len, | |
735 | int flags, | |
736 | xfs_bmbt_irec_t *mval, | |
737 | int nmap, | |
738 | int ret_nmap) | |
739 | { | |
740 | int i; /* index to map values */ | |
a5bd606b | 741 | |
9e5987a7 | 742 | ASSERT(ret_nmap <= nmap); |
a5bd606b | 743 | |
9e5987a7 DC |
744 | for (i = 0; i < ret_nmap; i++) { |
745 | ASSERT(mval[i].br_blockcount > 0); | |
746 | if (!(flags & XFS_BMAPI_ENTIRE)) { | |
747 | ASSERT(mval[i].br_startoff >= bno); | |
748 | ASSERT(mval[i].br_blockcount <= len); | |
749 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= | |
750 | bno + len); | |
751 | } else { | |
752 | ASSERT(mval[i].br_startoff < bno + len); | |
753 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > | |
754 | bno); | |
755 | } | |
756 | ASSERT(i == 0 || | |
757 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == | |
758 | mval[i].br_startoff); | |
759 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && | |
760 | mval[i].br_startblock != HOLESTARTBLOCK); | |
761 | ASSERT(mval[i].br_state == XFS_EXT_NORM || | |
762 | mval[i].br_state == XFS_EXT_UNWRITTEN); | |
763 | } | |
764 | } | |
7574aa92 | 765 | |
9e5987a7 DC |
766 | #else |
767 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) | |
768 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) | |
769 | #endif /* DEBUG */ | |
7574aa92 | 770 | |
9e5987a7 DC |
771 | /* |
772 | * bmap free list manipulation functions | |
773 | */ | |
7574aa92 | 774 | |
9e5987a7 DC |
775 | /* |
776 | * Add the extent to the list of extents to be free at transaction end. | |
777 | * The list is maintained sorted (by block number). | |
778 | */ | |
779 | void | |
780 | xfs_bmap_add_free( | |
781 | xfs_fsblock_t bno, /* fs block number of extent */ | |
782 | xfs_filblks_t len, /* length of extent */ | |
783 | xfs_bmap_free_t *flist, /* list of extents */ | |
784 | xfs_mount_t *mp) /* mount point structure */ | |
785 | { | |
786 | xfs_bmap_free_item_t *cur; /* current (next) element */ | |
787 | xfs_bmap_free_item_t *new; /* new element */ | |
788 | xfs_bmap_free_item_t *prev; /* previous element */ | |
789 | #ifdef DEBUG | |
790 | xfs_agnumber_t agno; | |
791 | xfs_agblock_t agbno; | |
792 | ||
793 | ASSERT(bno != NULLFSBLOCK); | |
794 | ASSERT(len > 0); | |
795 | ASSERT(len <= MAXEXTLEN); | |
796 | ASSERT(!isnullstartblock(bno)); | |
797 | agno = XFS_FSB_TO_AGNO(mp, bno); | |
798 | agbno = XFS_FSB_TO_AGBNO(mp, bno); | |
799 | ASSERT(agno < mp->m_sb.sb_agcount); | |
800 | ASSERT(agbno < mp->m_sb.sb_agblocks); | |
801 | ASSERT(len < mp->m_sb.sb_agblocks); | |
802 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); | |
803 | #endif | |
804 | ASSERT(xfs_bmap_free_item_zone != NULL); | |
805 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); | |
806 | new->xbfi_startblock = bno; | |
807 | new->xbfi_blockcount = (xfs_extlen_t)len; | |
808 | for (prev = NULL, cur = flist->xbf_first; | |
809 | cur != NULL; | |
810 | prev = cur, cur = cur->xbfi_next) { | |
811 | if (cur->xbfi_startblock >= bno) | |
812 | break; | |
1da177e4 | 813 | } |
9e5987a7 DC |
814 | if (prev) |
815 | prev->xbfi_next = new; | |
816 | else | |
817 | flist->xbf_first = new; | |
818 | new->xbfi_next = cur; | |
819 | flist->xbf_count++; | |
820 | } | |
7574aa92 | 821 | |
9e5987a7 DC |
822 | /* |
823 | * Remove the entry "free" from the free item list. Prev points to the | |
824 | * previous entry, unless "free" is the head of the list. | |
825 | */ | |
826 | STATIC void | |
827 | xfs_bmap_del_free( | |
828 | xfs_bmap_free_t *flist, /* free item list header */ | |
829 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ | |
830 | xfs_bmap_free_item_t *free) /* list item to be freed */ | |
831 | { | |
832 | if (prev) | |
833 | prev->xbfi_next = free->xbfi_next; | |
834 | else | |
835 | flist->xbf_first = free->xbfi_next; | |
836 | flist->xbf_count--; | |
837 | kmem_zone_free(xfs_bmap_free_item_zone, free); | |
838 | } | |
839 | ||
840 | ||
841 | /* | |
842 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi | |
843 | * caller. Frees all the extents that need freeing, which must be done | |
844 | * last due to locking considerations. We never free any extents in | |
845 | * the first transaction. | |
846 | * | |
847 | * Return 1 if the given transaction was committed and a new one | |
848 | * started, and 0 otherwise in the committed parameter. | |
849 | */ | |
850 | int /* error */ | |
851 | xfs_bmap_finish( | |
852 | xfs_trans_t **tp, /* transaction pointer addr */ | |
853 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ | |
854 | int *committed) /* xact committed or not */ | |
855 | { | |
856 | xfs_efd_log_item_t *efd; /* extent free data */ | |
857 | xfs_efi_log_item_t *efi; /* extent free intention */ | |
858 | int error; /* error return value */ | |
859 | xfs_bmap_free_item_t *free; /* free extent item */ | |
860 | unsigned int logres; /* new log reservation */ | |
861 | unsigned int logcount; /* new log count */ | |
862 | xfs_mount_t *mp; /* filesystem mount structure */ | |
863 | xfs_bmap_free_item_t *next; /* next item on free list */ | |
864 | xfs_trans_t *ntp; /* new transaction pointer */ | |
7574aa92 | 865 | |
9e5987a7 DC |
866 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
867 | if (flist->xbf_count == 0) { | |
868 | *committed = 0; | |
869 | return 0; | |
870 | } | |
871 | ntp = *tp; | |
872 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); | |
873 | for (free = flist->xbf_first; free; free = free->xbfi_next) | |
874 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, | |
875 | free->xbfi_blockcount); | |
876 | logres = ntp->t_log_res; | |
877 | logcount = ntp->t_log_count; | |
878 | ntp = xfs_trans_dup(*tp); | |
879 | error = xfs_trans_commit(*tp, 0); | |
880 | *tp = ntp; | |
881 | *committed = 1; | |
1da177e4 | 882 | /* |
9e5987a7 DC |
883 | * We have a new transaction, so we should return committed=1, |
884 | * even though we're returning an error. | |
1da177e4 | 885 | */ |
9e5987a7 DC |
886 | if (error) |
887 | return error; | |
7574aa92 | 888 | |
1da177e4 | 889 | /* |
9e5987a7 DC |
890 | * transaction commit worked ok so we can drop the extra ticket |
891 | * reference that we gained in xfs_trans_dup() | |
1da177e4 | 892 | */ |
9e5987a7 | 893 | xfs_log_ticket_put(ntp->t_ticket); |
ec90c556 | 894 | |
9e5987a7 DC |
895 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
896 | logcount))) | |
897 | return error; | |
898 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); | |
899 | for (free = flist->xbf_first; free != NULL; free = next) { | |
900 | next = free->xbfi_next; | |
901 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, | |
902 | free->xbfi_blockcount))) { | |
903 | /* | |
904 | * The bmap free list will be cleaned up at a | |
905 | * higher level. The EFI will be canceled when | |
906 | * this transaction is aborted. | |
907 | * Need to force shutdown here to make sure it | |
908 | * happens, since this transaction may not be | |
909 | * dirty yet. | |
910 | */ | |
911 | mp = ntp->t_mountp; | |
912 | if (!XFS_FORCED_SHUTDOWN(mp)) | |
913 | xfs_force_shutdown(mp, | |
914 | (error == EFSCORRUPTED) ? | |
915 | SHUTDOWN_CORRUPT_INCORE : | |
916 | SHUTDOWN_META_IO_ERROR); | |
917 | return error; | |
918 | } | |
919 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, | |
920 | free->xbfi_blockcount); | |
921 | xfs_bmap_del_free(flist, NULL, free); | |
922 | } | |
923 | return 0; | |
924 | } | |
0b1b213f | 925 | |
9e5987a7 DC |
926 | /* |
927 | * Free up any items left in the list. | |
928 | */ | |
929 | void | |
930 | xfs_bmap_cancel( | |
931 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ | |
932 | { | |
933 | xfs_bmap_free_item_t *free; /* free list item */ | |
934 | xfs_bmap_free_item_t *next; | |
ec90c556 | 935 | |
9e5987a7 DC |
936 | if (flist->xbf_count == 0) |
937 | return; | |
938 | ASSERT(flist->xbf_first != NULL); | |
939 | for (free = flist->xbf_first; free; free = next) { | |
940 | next = free->xbfi_next; | |
941 | xfs_bmap_del_free(flist, NULL, free); | |
942 | } | |
943 | ASSERT(flist->xbf_count == 0); | |
944 | } | |
0b1b213f | 945 | |
9e5987a7 DC |
946 | /* |
947 | * Inode fork format manipulation functions | |
948 | */ | |
1da177e4 | 949 | |
9e5987a7 DC |
950 | /* |
951 | * Transform a btree format file with only one leaf node, where the | |
952 | * extents list will fit in the inode, into an extents format file. | |
953 | * Since the file extents are already in-core, all we have to do is | |
954 | * give up the space for the btree root and pitch the leaf block. | |
955 | */ | |
956 | STATIC int /* error */ | |
957 | xfs_bmap_btree_to_extents( | |
958 | xfs_trans_t *tp, /* transaction pointer */ | |
959 | xfs_inode_t *ip, /* incore inode pointer */ | |
960 | xfs_btree_cur_t *cur, /* btree cursor */ | |
961 | int *logflagsp, /* inode logging flags */ | |
962 | int whichfork) /* data or attr fork */ | |
963 | { | |
964 | /* REFERENCED */ | |
965 | struct xfs_btree_block *cblock;/* child btree block */ | |
966 | xfs_fsblock_t cbno; /* child block number */ | |
967 | xfs_buf_t *cbp; /* child block's buffer */ | |
968 | int error; /* error return value */ | |
969 | xfs_ifork_t *ifp; /* inode fork data */ | |
970 | xfs_mount_t *mp; /* mount point structure */ | |
971 | __be64 *pp; /* ptr to block address */ | |
972 | struct xfs_btree_block *rblock;/* root btree block */ | |
1da177e4 | 973 | |
9e5987a7 DC |
974 | mp = ip->i_mount; |
975 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
976 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); | |
977 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); | |
978 | rblock = ifp->if_broot; | |
979 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); | |
980 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); | |
981 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); | |
982 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); | |
983 | cbno = be64_to_cpu(*pp); | |
984 | *logflagsp = 0; | |
985 | #ifdef DEBUG | |
986 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) | |
987 | return error; | |
988 | #endif | |
989 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, | |
990 | &xfs_bmbt_buf_ops); | |
991 | if (error) | |
992 | return error; | |
993 | cblock = XFS_BUF_TO_BLOCK(cbp); | |
994 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) | |
995 | return error; | |
996 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); | |
997 | ip->i_d.di_nblocks--; | |
998 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); | |
999 | xfs_trans_binval(tp, cbp); | |
1000 | if (cur->bc_bufs[0] == cbp) | |
1001 | cur->bc_bufs[0] = NULL; | |
1002 | xfs_iroot_realloc(ip, -1, whichfork); | |
1003 | ASSERT(ifp->if_broot == NULL); | |
1004 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); | |
1005 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); | |
1006 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | |
1007 | return 0; | |
1008 | } | |
0b1b213f | 1009 | |
9e5987a7 DC |
1010 | /* |
1011 | * Convert an extents-format file into a btree-format file. | |
1012 | * The new file will have a root block (in the inode) and a single child block. | |
1013 | */ | |
1014 | STATIC int /* error */ | |
1015 | xfs_bmap_extents_to_btree( | |
1016 | xfs_trans_t *tp, /* transaction pointer */ | |
1017 | xfs_inode_t *ip, /* incore inode pointer */ | |
1018 | xfs_fsblock_t *firstblock, /* first-block-allocated */ | |
1019 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ | |
1020 | xfs_btree_cur_t **curp, /* cursor returned to caller */ | |
1021 | int wasdel, /* converting a delayed alloc */ | |
1022 | int *logflagsp, /* inode logging flags */ | |
1023 | int whichfork) /* data or attr fork */ | |
1024 | { | |
1025 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ | |
1026 | xfs_buf_t *abp; /* buffer for ablock */ | |
1027 | xfs_alloc_arg_t args; /* allocation arguments */ | |
1028 | xfs_bmbt_rec_t *arp; /* child record pointer */ | |
1029 | struct xfs_btree_block *block; /* btree root block */ | |
1030 | xfs_btree_cur_t *cur; /* bmap btree cursor */ | |
1031 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
1032 | int error; /* error return value */ | |
1033 | xfs_extnum_t i, cnt; /* extent record index */ | |
1034 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
1035 | xfs_bmbt_key_t *kp; /* root block key pointer */ | |
1036 | xfs_mount_t *mp; /* mount structure */ | |
1037 | xfs_extnum_t nextents; /* number of file extents */ | |
1038 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ | |
1da177e4 | 1039 | |
ee1a47ab | 1040 | mp = ip->i_mount; |
9e5987a7 DC |
1041 | ifp = XFS_IFORK_PTR(ip, whichfork); |
1042 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); | |
0b1b213f | 1043 | |
9e5987a7 DC |
1044 | /* |
1045 | * Make space in the inode incore. | |
1046 | */ | |
1047 | xfs_iroot_realloc(ip, 1, whichfork); | |
1048 | ifp->if_flags |= XFS_IFBROOT; | |
ec90c556 | 1049 | |
9e5987a7 DC |
1050 | /* |
1051 | * Fill in the root. | |
1052 | */ | |
1053 | block = ifp->if_broot; | |
ee1a47ab CH |
1054 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
1055 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, | |
1056 | XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino, | |
1057 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); | |
1058 | else | |
1059 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, | |
1060 | XFS_BMAP_MAGIC, 1, 1, ip->i_ino, | |
1061 | XFS_BTREE_LONG_PTRS); | |
0b1b213f | 1062 | |
9e5987a7 DC |
1063 | /* |
1064 | * Need a cursor. Can't allocate until bb_level is filled in. | |
1065 | */ | |
9e5987a7 DC |
1066 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
1067 | cur->bc_private.b.firstblock = *firstblock; | |
1068 | cur->bc_private.b.flist = flist; | |
1069 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; | |
1070 | /* | |
1071 | * Convert to a btree with two levels, one record in root. | |
1072 | */ | |
1073 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); | |
1074 | memset(&args, 0, sizeof(args)); | |
1075 | args.tp = tp; | |
1076 | args.mp = mp; | |
1077 | args.firstblock = *firstblock; | |
1078 | if (*firstblock == NULLFSBLOCK) { | |
1079 | args.type = XFS_ALLOCTYPE_START_BNO; | |
1080 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); | |
1081 | } else if (flist->xbf_low) { | |
1082 | args.type = XFS_ALLOCTYPE_START_BNO; | |
1083 | args.fsbno = *firstblock; | |
1084 | } else { | |
1085 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
1086 | args.fsbno = *firstblock; | |
1087 | } | |
1088 | args.minlen = args.maxlen = args.prod = 1; | |
1089 | args.wasdel = wasdel; | |
1090 | *logflagsp = 0; | |
1091 | if ((error = xfs_alloc_vextent(&args))) { | |
1092 | xfs_iroot_realloc(ip, -1, whichfork); | |
1093 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
1094 | return error; | |
1095 | } | |
1096 | /* | |
1097 | * Allocation can't fail, the space was reserved. | |
1098 | */ | |
1099 | ASSERT(args.fsbno != NULLFSBLOCK); | |
1100 | ASSERT(*firstblock == NULLFSBLOCK || | |
1101 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || | |
1102 | (flist->xbf_low && | |
1103 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); | |
1104 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; | |
1105 | cur->bc_private.b.allocated++; | |
1106 | ip->i_d.di_nblocks++; | |
1107 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); | |
1108 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); | |
1109 | /* | |
1110 | * Fill in the child block. | |
1111 | */ | |
1112 | abp->b_ops = &xfs_bmbt_buf_ops; | |
1113 | ablock = XFS_BUF_TO_BLOCK(abp); | |
ee1a47ab CH |
1114 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
1115 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, | |
1116 | XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino, | |
1117 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); | |
1118 | else | |
1119 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, | |
1120 | XFS_BMAP_MAGIC, 0, 0, ip->i_ino, | |
1121 | XFS_BTREE_LONG_PTRS); | |
1122 | ||
9e5987a7 DC |
1123 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
1124 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
1125 | for (cnt = i = 0; i < nextents; i++) { | |
1126 | ep = xfs_iext_get_ext(ifp, i); | |
1127 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { | |
1128 | arp->l0 = cpu_to_be64(ep->l0); | |
1129 | arp->l1 = cpu_to_be64(ep->l1); | |
1130 | arp++; cnt++; | |
1da177e4 | 1131 | } |
9e5987a7 DC |
1132 | } |
1133 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); | |
1134 | xfs_btree_set_numrecs(ablock, cnt); | |
1da177e4 | 1135 | |
9e5987a7 DC |
1136 | /* |
1137 | * Fill in the root key and pointer. | |
1138 | */ | |
1139 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); | |
1140 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); | |
1141 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); | |
1142 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, | |
1143 | be16_to_cpu(block->bb_level))); | |
1144 | *pp = cpu_to_be64(args.fsbno); | |
ec90c556 | 1145 | |
9e5987a7 DC |
1146 | /* |
1147 | * Do all this logging at the end so that | |
1148 | * the root is at the right level. | |
1149 | */ | |
1150 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); | |
1151 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); | |
1152 | ASSERT(*curp == NULL); | |
1153 | *curp = cur; | |
1154 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); | |
1155 | return 0; | |
1156 | } | |
ec90c556 | 1157 | |
9e5987a7 DC |
1158 | /* |
1159 | * Convert a local file to an extents file. | |
1160 | * This code is out of bounds for data forks of regular files, | |
1161 | * since the file data needs to get logged so things will stay consistent. | |
1162 | * (The bmap-level manipulations are ok, though). | |
1163 | */ | |
1164 | STATIC int /* error */ | |
1165 | xfs_bmap_local_to_extents( | |
1166 | xfs_trans_t *tp, /* transaction pointer */ | |
1167 | xfs_inode_t *ip, /* incore inode pointer */ | |
1168 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ | |
1169 | xfs_extlen_t total, /* total blocks needed by transaction */ | |
1170 | int *logflagsp, /* inode logging flags */ | |
1171 | int whichfork, | |
ee1a47ab CH |
1172 | void (*init_fn)(struct xfs_trans *tp, |
1173 | struct xfs_buf *bp, | |
9e5987a7 DC |
1174 | struct xfs_inode *ip, |
1175 | struct xfs_ifork *ifp)) | |
1176 | { | |
1177 | int error; /* error return value */ | |
1178 | int flags; /* logging flags returned */ | |
1179 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
0b1b213f | 1180 | |
9e5987a7 DC |
1181 | /* |
1182 | * We don't want to deal with the case of keeping inode data inline yet. | |
1183 | * So sending the data fork of a regular inode is invalid. | |
1184 | */ | |
1185 | ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK)); | |
1186 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1187 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); | |
1188 | flags = 0; | |
1189 | error = 0; | |
1190 | if (ifp->if_bytes) { | |
1191 | xfs_alloc_arg_t args; /* allocation arguments */ | |
1192 | xfs_buf_t *bp; /* buffer for extent block */ | |
1193 | xfs_bmbt_rec_host_t *ep;/* extent record pointer */ | |
1da177e4 | 1194 | |
9e5987a7 DC |
1195 | ASSERT((ifp->if_flags & |
1196 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); | |
1197 | memset(&args, 0, sizeof(args)); | |
1198 | args.tp = tp; | |
1199 | args.mp = ip->i_mount; | |
1200 | args.firstblock = *firstblock; | |
1da177e4 | 1201 | /* |
9e5987a7 DC |
1202 | * Allocate a block. We know we need only one, since the |
1203 | * file currently fits in an inode. | |
1da177e4 | 1204 | */ |
9e5987a7 DC |
1205 | if (*firstblock == NULLFSBLOCK) { |
1206 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); | |
1207 | args.type = XFS_ALLOCTYPE_START_BNO; | |
1208 | } else { | |
1209 | args.fsbno = *firstblock; | |
1210 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
1211 | } | |
1212 | args.total = total; | |
1213 | args.minlen = args.maxlen = args.prod = 1; | |
1214 | error = xfs_alloc_vextent(&args); | |
1215 | if (error) | |
1216 | goto done; | |
ec90c556 | 1217 | |
9e5987a7 DC |
1218 | /* Can't fail, the space was reserved. */ |
1219 | ASSERT(args.fsbno != NULLFSBLOCK); | |
1220 | ASSERT(args.len == 1); | |
1221 | *firstblock = args.fsbno; | |
1222 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); | |
0b1b213f | 1223 | |
9e5987a7 | 1224 | /* initialise the block and copy the data */ |
ee1a47ab | 1225 | init_fn(tp, bp, ip, ifp); |
0b1b213f | 1226 | |
9e5987a7 DC |
1227 | /* account for the change in fork size and log everything */ |
1228 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); | |
1229 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); | |
1230 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); | |
1231 | xfs_iext_add(ifp, 0, 1); | |
1232 | ep = xfs_iext_get_ext(ifp, 0); | |
1233 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); | |
1234 | trace_xfs_bmap_post_update(ip, 0, | |
1235 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, | |
1236 | _THIS_IP_); | |
1237 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); | |
1238 | ip->i_d.di_nblocks = 1; | |
1239 | xfs_trans_mod_dquot_byino(tp, ip, | |
1240 | XFS_TRANS_DQ_BCOUNT, 1L); | |
1241 | flags |= xfs_ilog_fext(whichfork); | |
1242 | } else { | |
1243 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); | |
1244 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); | |
1245 | } | |
1246 | ifp->if_flags &= ~XFS_IFINLINE; | |
1247 | ifp->if_flags |= XFS_IFEXTENTS; | |
1248 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); | |
1249 | flags |= XFS_ILOG_CORE; | |
1250 | done: | |
1251 | *logflagsp = flags; | |
1252 | return error; | |
1253 | } | |
ec90c556 | 1254 | |
9e5987a7 DC |
1255 | /* |
1256 | * Called from xfs_bmap_add_attrfork to handle btree format files. | |
1257 | */ | |
1258 | STATIC int /* error */ | |
1259 | xfs_bmap_add_attrfork_btree( | |
1260 | xfs_trans_t *tp, /* transaction pointer */ | |
1261 | xfs_inode_t *ip, /* incore inode pointer */ | |
1262 | xfs_fsblock_t *firstblock, /* first block allocated */ | |
1263 | xfs_bmap_free_t *flist, /* blocks to free at commit */ | |
1264 | int *flags) /* inode logging flags */ | |
1265 | { | |
1266 | xfs_btree_cur_t *cur; /* btree cursor */ | |
1267 | int error; /* error return value */ | |
1268 | xfs_mount_t *mp; /* file system mount struct */ | |
1269 | int stat; /* newroot status */ | |
ec90c556 | 1270 | |
9e5987a7 DC |
1271 | mp = ip->i_mount; |
1272 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) | |
1273 | *flags |= XFS_ILOG_DBROOT; | |
1274 | else { | |
1275 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); | |
1276 | cur->bc_private.b.flist = flist; | |
1277 | cur->bc_private.b.firstblock = *firstblock; | |
1278 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) | |
1279 | goto error0; | |
1280 | /* must be at least one entry */ | |
1281 | XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); | |
1282 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) | |
1283 | goto error0; | |
1284 | if (stat == 0) { | |
1285 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
1286 | return XFS_ERROR(ENOSPC); | |
1da177e4 | 1287 | } |
9e5987a7 DC |
1288 | *firstblock = cur->bc_private.b.firstblock; |
1289 | cur->bc_private.b.allocated = 0; | |
1290 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
1da177e4 | 1291 | } |
9e5987a7 DC |
1292 | return 0; |
1293 | error0: | |
1294 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
1295 | return error; | |
1296 | } | |
a5bd606b | 1297 | |
9e5987a7 DC |
1298 | /* |
1299 | * Called from xfs_bmap_add_attrfork to handle extents format files. | |
1300 | */ | |
1301 | STATIC int /* error */ | |
1302 | xfs_bmap_add_attrfork_extents( | |
1303 | xfs_trans_t *tp, /* transaction pointer */ | |
1304 | xfs_inode_t *ip, /* incore inode pointer */ | |
1305 | xfs_fsblock_t *firstblock, /* first block allocated */ | |
1306 | xfs_bmap_free_t *flist, /* blocks to free at commit */ | |
1307 | int *flags) /* inode logging flags */ | |
1308 | { | |
1309 | xfs_btree_cur_t *cur; /* bmap btree cursor */ | |
1310 | int error; /* error return value */ | |
a5bd606b | 1311 | |
9e5987a7 DC |
1312 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
1313 | return 0; | |
1314 | cur = NULL; | |
1315 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, | |
1316 | flags, XFS_DATA_FORK); | |
a5bd606b CH |
1317 | if (cur) { |
1318 | cur->bc_private.b.allocated = 0; | |
9e5987a7 DC |
1319 | xfs_btree_del_cursor(cur, |
1320 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
a5bd606b | 1321 | } |
1da177e4 | 1322 | return error; |
1da177e4 LT |
1323 | } |
1324 | ||
1325 | /* | |
19de7351 DC |
1326 | * Block initialisation function for local to extent format conversion. |
1327 | * | |
1328 | * This shouldn't actually be called by anyone, so make sure debug kernels cause | |
1329 | * a noticable failure. | |
1da177e4 | 1330 | */ |
1fd044d9 | 1331 | STATIC void |
9e5987a7 | 1332 | xfs_bmap_local_to_extents_init_fn( |
ee1a47ab | 1333 | struct xfs_trans *tp, |
9e5987a7 DC |
1334 | struct xfs_buf *bp, |
1335 | struct xfs_inode *ip, | |
1336 | struct xfs_ifork *ifp) | |
1da177e4 | 1337 | { |
19de7351 | 1338 | ASSERT(0); |
9e5987a7 DC |
1339 | bp->b_ops = &xfs_bmbt_buf_ops; |
1340 | memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes); | |
61fe135c | 1341 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_BTREE_BUF); |
9e5987a7 | 1342 | } |
1da177e4 | 1343 | |
9e5987a7 DC |
1344 | /* |
1345 | * Called from xfs_bmap_add_attrfork to handle local format files. Each | |
1346 | * different data fork content type needs a different callout to do the | |
1347 | * conversion. Some are basic and only require special block initialisation | |
1348 | * callouts for the data formating, others (directories) are so specialised they | |
1349 | * handle everything themselves. | |
1350 | * | |
1351 | * XXX (dgc): investigate whether directory conversion can use the generic | |
1352 | * formatting callout. It should be possible - it's just a very complex | |
ee1a47ab | 1353 | * formatter. |
9e5987a7 DC |
1354 | */ |
1355 | STATIC int /* error */ | |
1356 | xfs_bmap_add_attrfork_local( | |
1357 | xfs_trans_t *tp, /* transaction pointer */ | |
1358 | xfs_inode_t *ip, /* incore inode pointer */ | |
1359 | xfs_fsblock_t *firstblock, /* first block allocated */ | |
1360 | xfs_bmap_free_t *flist, /* blocks to free at commit */ | |
1361 | int *flags) /* inode logging flags */ | |
1362 | { | |
1363 | xfs_da_args_t dargs; /* args for dir/attr code */ | |
7574aa92 | 1364 | |
9e5987a7 DC |
1365 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
1366 | return 0; | |
7574aa92 | 1367 | |
9e5987a7 DC |
1368 | if (S_ISDIR(ip->i_d.di_mode)) { |
1369 | memset(&dargs, 0, sizeof(dargs)); | |
1370 | dargs.dp = ip; | |
1371 | dargs.firstblock = firstblock; | |
1372 | dargs.flist = flist; | |
1373 | dargs.total = ip->i_mount->m_dirblkfsbs; | |
1374 | dargs.whichfork = XFS_DATA_FORK; | |
1375 | dargs.trans = tp; | |
1376 | return xfs_dir2_sf_to_block(&dargs); | |
1da177e4 | 1377 | } |
7574aa92 | 1378 | |
9e5987a7 DC |
1379 | if (S_ISLNK(ip->i_d.di_mode)) |
1380 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, | |
1381 | flags, XFS_DATA_FORK, | |
1382 | xfs_symlink_local_to_remote); | |
7574aa92 | 1383 | |
9e5987a7 DC |
1384 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, |
1385 | XFS_DATA_FORK, | |
1386 | xfs_bmap_local_to_extents_init_fn); | |
1387 | } | |
0b1b213f | 1388 | |
9e5987a7 DC |
1389 | /* |
1390 | * Convert inode from non-attributed to attributed. | |
1391 | * Must not be in a transaction, ip must not be locked. | |
1392 | */ | |
1393 | int /* error code */ | |
1394 | xfs_bmap_add_attrfork( | |
1395 | xfs_inode_t *ip, /* incore inode pointer */ | |
1396 | int size, /* space new attribute needs */ | |
1397 | int rsvd) /* xact may use reserved blks */ | |
1398 | { | |
1399 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ | |
1400 | xfs_bmap_free_t flist; /* freed extent records */ | |
1401 | xfs_mount_t *mp; /* mount structure */ | |
1402 | xfs_trans_t *tp; /* transaction pointer */ | |
1403 | int blks; /* space reservation */ | |
1404 | int version = 1; /* superblock attr version */ | |
1405 | int committed; /* xaction was committed */ | |
1406 | int logflags; /* logging flags */ | |
1407 | int error; /* error return value */ | |
0b1b213f | 1408 | |
9e5987a7 | 1409 | ASSERT(XFS_IFORK_Q(ip) == 0); |
1da177e4 | 1410 | |
9e5987a7 DC |
1411 | mp = ip->i_mount; |
1412 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); | |
1413 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); | |
1414 | blks = XFS_ADDAFORK_SPACE_RES(mp); | |
1415 | if (rsvd) | |
1416 | tp->t_flags |= XFS_TRANS_RESERVE; | |
1417 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, | |
1418 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) | |
1419 | goto error0; | |
1420 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
1421 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? | |
1422 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : | |
1423 | XFS_QMOPT_RES_REGBLKS); | |
1424 | if (error) { | |
1425 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1426 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); | |
1427 | return error; | |
1428 | } | |
1429 | if (XFS_IFORK_Q(ip)) | |
1430 | goto error1; | |
1431 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { | |
1da177e4 | 1432 | /* |
9e5987a7 | 1433 | * For inodes coming from pre-6.2 filesystems. |
1da177e4 | 1434 | */ |
9e5987a7 DC |
1435 | ASSERT(ip->i_d.di_aformat == 0); |
1436 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | |
1437 | } | |
1438 | ASSERT(ip->i_d.di_anextents == 0); | |
ec90c556 | 1439 | |
9e5987a7 DC |
1440 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
1441 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1da177e4 | 1442 | |
9e5987a7 DC |
1443 | switch (ip->i_d.di_format) { |
1444 | case XFS_DINODE_FMT_DEV: | |
1445 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; | |
1446 | break; | |
1447 | case XFS_DINODE_FMT_UUID: | |
1448 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; | |
1449 | break; | |
1450 | case XFS_DINODE_FMT_LOCAL: | |
1451 | case XFS_DINODE_FMT_EXTENTS: | |
1452 | case XFS_DINODE_FMT_BTREE: | |
1453 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); | |
1454 | if (!ip->i_d.di_forkoff) | |
1455 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; | |
1456 | else if (mp->m_flags & XFS_MOUNT_ATTR2) | |
1457 | version = 2; | |
1da177e4 | 1458 | break; |
9e5987a7 DC |
1459 | default: |
1460 | ASSERT(0); | |
1461 | error = XFS_ERROR(EINVAL); | |
1462 | goto error1; | |
1463 | } | |
1da177e4 | 1464 | |
9e5987a7 DC |
1465 | ASSERT(ip->i_afp == NULL); |
1466 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); | |
1467 | ip->i_afp->if_flags = XFS_IFEXTENTS; | |
1468 | logflags = 0; | |
1469 | xfs_bmap_init(&flist, &firstblock); | |
1470 | switch (ip->i_d.di_format) { | |
1471 | case XFS_DINODE_FMT_LOCAL: | |
1472 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, | |
1473 | &logflags); | |
1474 | break; | |
1475 | case XFS_DINODE_FMT_EXTENTS: | |
1476 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, | |
1477 | &flist, &logflags); | |
1478 | break; | |
1479 | case XFS_DINODE_FMT_BTREE: | |
1480 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, | |
1481 | &logflags); | |
1482 | break; | |
1483 | default: | |
1484 | error = 0; | |
1da177e4 LT |
1485 | break; |
1486 | } | |
9e5987a7 DC |
1487 | if (logflags) |
1488 | xfs_trans_log_inode(tp, ip, logflags); | |
1489 | if (error) | |
1490 | goto error2; | |
1491 | if (!xfs_sb_version_hasattr(&mp->m_sb) || | |
1492 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { | |
1493 | __int64_t sbfields = 0; | |
1494 | ||
1495 | spin_lock(&mp->m_sb_lock); | |
1496 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { | |
1497 | xfs_sb_version_addattr(&mp->m_sb); | |
1498 | sbfields |= XFS_SB_VERSIONNUM; | |
1499 | } | |
1500 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { | |
1501 | xfs_sb_version_addattr2(&mp->m_sb); | |
1502 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); | |
1503 | } | |
1504 | if (sbfields) { | |
1505 | spin_unlock(&mp->m_sb_lock); | |
1506 | xfs_mod_sb(tp, sbfields); | |
1507 | } else | |
1508 | spin_unlock(&mp->m_sb_lock); | |
1da177e4 | 1509 | } |
9e5987a7 DC |
1510 | |
1511 | error = xfs_bmap_finish(&tp, &flist, &committed); | |
1512 | if (error) | |
1513 | goto error2; | |
1514 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | |
1515 | error2: | |
1516 | xfs_bmap_cancel(&flist); | |
1517 | error1: | |
1518 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1519 | error0: | |
1520 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); | |
1521 | return error; | |
1da177e4 LT |
1522 | } |
1523 | ||
1524 | /* | |
9e5987a7 | 1525 | * Internal and external extent tree search functions. |
1da177e4 | 1526 | */ |
a5bd606b | 1527 | |
9e5987a7 DC |
1528 | /* |
1529 | * Read in the extents to if_extents. | |
1530 | * All inode fields are set up by caller, we just traverse the btree | |
1531 | * and copy the records in. If the file system cannot contain unwritten | |
1532 | * extents, the records are checked for no "state" flags. | |
1533 | */ | |
1534 | int /* error */ | |
1535 | xfs_bmap_read_extents( | |
1536 | xfs_trans_t *tp, /* transaction pointer */ | |
1537 | xfs_inode_t *ip, /* incore inode */ | |
1538 | int whichfork) /* data or attr fork */ | |
1539 | { | |
1540 | struct xfs_btree_block *block; /* current btree block */ | |
1541 | xfs_fsblock_t bno; /* block # of "block" */ | |
1542 | xfs_buf_t *bp; /* buffer for "block" */ | |
1543 | int error; /* error return value */ | |
1544 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ | |
1545 | xfs_extnum_t i, j; /* index into the extents list */ | |
1546 | xfs_ifork_t *ifp; /* fork structure */ | |
1547 | int level; /* btree level, for checking */ | |
1548 | xfs_mount_t *mp; /* file system mount structure */ | |
1549 | __be64 *pp; /* pointer to block address */ | |
1550 | /* REFERENCED */ | |
1551 | xfs_extnum_t room; /* number of entries there's room for */ | |
6ef35544 | 1552 | |
9e5987a7 DC |
1553 | bno = NULLFSBLOCK; |
1554 | mp = ip->i_mount; | |
1555 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1556 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : | |
1557 | XFS_EXTFMT_INODE(ip); | |
1558 | block = ifp->if_broot; | |
1da177e4 | 1559 | /* |
9e5987a7 | 1560 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
1da177e4 | 1561 | */ |
9e5987a7 DC |
1562 | level = be16_to_cpu(block->bb_level); |
1563 | ASSERT(level > 0); | |
1564 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); | |
1565 | bno = be64_to_cpu(*pp); | |
1566 | ASSERT(bno != NULLDFSBNO); | |
1567 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); | |
1568 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); | |
1da177e4 | 1569 | /* |
9e5987a7 DC |
1570 | * Go down the tree until leaf level is reached, following the first |
1571 | * pointer (leftmost) at each level. | |
1da177e4 | 1572 | */ |
9e5987a7 DC |
1573 | while (level-- > 0) { |
1574 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, | |
1575 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); | |
1576 | if (error) | |
1577 | return error; | |
1578 | block = XFS_BUF_TO_BLOCK(bp); | |
1579 | XFS_WANT_CORRUPTED_GOTO( | |
1580 | xfs_bmap_sanity_check(mp, bp, level), | |
1581 | error0); | |
1582 | if (level == 0) | |
1583 | break; | |
1584 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); | |
1585 | bno = be64_to_cpu(*pp); | |
1586 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); | |
1587 | xfs_trans_brelse(tp, bp); | |
1da177e4 LT |
1588 | } |
1589 | /* | |
9e5987a7 | 1590 | * Here with bp and block set to the leftmost leaf node in the tree. |
1da177e4 | 1591 | */ |
9e5987a7 DC |
1592 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
1593 | i = 0; | |
1da177e4 | 1594 | /* |
9e5987a7 | 1595 | * Loop over all leaf nodes. Copy information to the extent records. |
1da177e4 | 1596 | */ |
9e5987a7 DC |
1597 | for (;;) { |
1598 | xfs_bmbt_rec_t *frp; | |
1599 | xfs_fsblock_t nextbno; | |
1600 | xfs_extnum_t num_recs; | |
1601 | xfs_extnum_t start; | |
0b1b213f | 1602 | |
9e5987a7 DC |
1603 | num_recs = xfs_btree_get_numrecs(block); |
1604 | if (unlikely(i + num_recs > room)) { | |
1605 | ASSERT(i + num_recs <= room); | |
1606 | xfs_warn(ip->i_mount, | |
1607 | "corrupt dinode %Lu, (btree extents).", | |
1608 | (unsigned long long) ip->i_ino); | |
1609 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", | |
1610 | XFS_ERRLEVEL_LOW, ip->i_mount, block); | |
1611 | goto error0; | |
1da177e4 | 1612 | } |
9e5987a7 DC |
1613 | XFS_WANT_CORRUPTED_GOTO( |
1614 | xfs_bmap_sanity_check(mp, bp, 0), | |
1615 | error0); | |
1da177e4 | 1616 | /* |
9e5987a7 | 1617 | * Read-ahead the next leaf block, if any. |
1da177e4 | 1618 | */ |
9e5987a7 DC |
1619 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
1620 | if (nextbno != NULLFSBLOCK) | |
1621 | xfs_btree_reada_bufl(mp, nextbno, 1, | |
1622 | &xfs_bmbt_buf_ops); | |
1da177e4 | 1623 | /* |
9e5987a7 | 1624 | * Copy records into the extent records. |
1da177e4 | 1625 | */ |
9e5987a7 DC |
1626 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
1627 | start = i; | |
1628 | for (j = 0; j < num_recs; j++, i++, frp++) { | |
1629 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); | |
1630 | trp->l0 = be64_to_cpu(frp->l0); | |
1631 | trp->l1 = be64_to_cpu(frp->l1); | |
1da177e4 | 1632 | } |
9e5987a7 DC |
1633 | if (exntf == XFS_EXTFMT_NOSTATE) { |
1634 | /* | |
1635 | * Check all attribute bmap btree records and | |
1636 | * any "older" data bmap btree records for a | |
1637 | * set bit in the "extent flag" position. | |
1638 | */ | |
1639 | if (unlikely(xfs_check_nostate_extents(ifp, | |
1640 | start, num_recs))) { | |
1641 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", | |
1642 | XFS_ERRLEVEL_LOW, | |
1643 | ip->i_mount); | |
1644 | goto error0; | |
1645 | } | |
1646 | } | |
1647 | xfs_trans_brelse(tp, bp); | |
1648 | bno = nextbno; | |
1da177e4 | 1649 | /* |
9e5987a7 | 1650 | * If we've reached the end, stop. |
1da177e4 | 1651 | */ |
9e5987a7 DC |
1652 | if (bno == NULLFSBLOCK) |
1653 | break; | |
1654 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, | |
1655 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); | |
1656 | if (error) | |
1657 | return error; | |
1658 | block = XFS_BUF_TO_BLOCK(bp); | |
1da177e4 | 1659 | } |
9e5987a7 DC |
1660 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
1661 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); | |
1662 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); | |
1663 | return 0; | |
1664 | error0: | |
1665 | xfs_trans_brelse(tp, bp); | |
1666 | return XFS_ERROR(EFSCORRUPTED); | |
1667 | } | |
a5bd606b | 1668 | |
a5bd606b | 1669 | |
9e5987a7 DC |
1670 | /* |
1671 | * Search the extent records for the entry containing block bno. | |
1672 | * If bno lies in a hole, point to the next entry. If bno lies | |
1673 | * past eof, *eofp will be set, and *prevp will contain the last | |
1674 | * entry (null if none). Else, *lastxp will be set to the index | |
1675 | * of the found entry; *gotp will contain the entry. | |
1676 | */ | |
1677 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ | |
1678 | xfs_bmap_search_multi_extents( | |
1679 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1680 | xfs_fileoff_t bno, /* block number searched for */ | |
1681 | int *eofp, /* out: end of file found */ | |
1682 | xfs_extnum_t *lastxp, /* out: last extent index */ | |
1683 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ | |
1684 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ | |
1685 | { | |
1686 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
1687 | xfs_extnum_t lastx; /* last extent index */ | |
a5bd606b | 1688 | |
9e5987a7 DC |
1689 | /* |
1690 | * Initialize the extent entry structure to catch access to | |
1691 | * uninitialized br_startblock field. | |
1692 | */ | |
1693 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; | |
1694 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; | |
1695 | gotp->br_state = XFS_EXT_INVALID; | |
1696 | #if XFS_BIG_BLKNOS | |
1697 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; | |
1698 | #else | |
1699 | gotp->br_startblock = 0xffffa5a5; | |
1700 | #endif | |
1701 | prevp->br_startoff = NULLFILEOFF; | |
c6534249 | 1702 | |
9e5987a7 DC |
1703 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
1704 | if (lastx > 0) { | |
1705 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); | |
1706 | } | |
1707 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { | |
1708 | xfs_bmbt_get_all(ep, gotp); | |
1709 | *eofp = 0; | |
1710 | } else { | |
1711 | if (lastx > 0) { | |
1712 | *gotp = *prevp; | |
1713 | } | |
1714 | *eofp = 1; | |
1715 | ep = NULL; | |
1716 | } | |
1717 | *lastxp = lastx; | |
1718 | return ep; | |
1da177e4 LT |
1719 | } |
1720 | ||
dd9f438e | 1721 | /* |
9e5987a7 DC |
1722 | * Search the extents list for the inode, for the extent containing bno. |
1723 | * If bno lies in a hole, point to the next entry. If bno lies past eof, | |
1724 | * *eofp will be set, and *prevp will contain the last entry (null if none). | |
1725 | * Else, *lastxp will be set to the index of the found | |
1726 | * entry; *gotp will contain the entry. | |
dd9f438e | 1727 | */ |
9e5987a7 DC |
1728 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
1729 | xfs_bmap_search_extents( | |
1730 | xfs_inode_t *ip, /* incore inode pointer */ | |
1731 | xfs_fileoff_t bno, /* block number searched for */ | |
1732 | int fork, /* data or attr fork */ | |
1733 | int *eofp, /* out: end of file found */ | |
1734 | xfs_extnum_t *lastxp, /* out: last extent index */ | |
1735 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ | |
1736 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ | |
dd9f438e | 1737 | { |
9e5987a7 DC |
1738 | xfs_ifork_t *ifp; /* inode fork pointer */ |
1739 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
dd9f438e | 1740 | |
9e5987a7 DC |
1741 | XFS_STATS_INC(xs_look_exlist); |
1742 | ifp = XFS_IFORK_PTR(ip, fork); | |
dd9f438e | 1743 | |
9e5987a7 | 1744 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
dd9f438e | 1745 | |
9e5987a7 DC |
1746 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
1747 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { | |
1748 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, | |
1749 | "Access to block zero in inode %llu " | |
1750 | "start_block: %llx start_off: %llx " | |
1751 | "blkcnt: %llx extent-state: %x lastx: %x\n", | |
1752 | (unsigned long long)ip->i_ino, | |
1753 | (unsigned long long)gotp->br_startblock, | |
1754 | (unsigned long long)gotp->br_startoff, | |
1755 | (unsigned long long)gotp->br_blockcount, | |
1756 | gotp->br_state, *lastxp); | |
1757 | *lastxp = NULLEXTNUM; | |
1758 | *eofp = 1; | |
1759 | return NULL; | |
dd9f438e | 1760 | } |
9e5987a7 DC |
1761 | return ep; |
1762 | } | |
dd9f438e | 1763 | |
9e5987a7 DC |
1764 | /* |
1765 | * Returns the file-relative block number of the first unused block(s) | |
1766 | * in the file with at least "len" logically contiguous blocks free. | |
1767 | * This is the lowest-address hole if the file has holes, else the first block | |
1768 | * past the end of file. | |
1769 | * Return 0 if the file is currently local (in-inode). | |
1770 | */ | |
1771 | int /* error */ | |
1772 | xfs_bmap_first_unused( | |
1773 | xfs_trans_t *tp, /* transaction pointer */ | |
1774 | xfs_inode_t *ip, /* incore inode */ | |
1775 | xfs_extlen_t len, /* size of hole to find */ | |
1776 | xfs_fileoff_t *first_unused, /* unused block */ | |
1777 | int whichfork) /* data or attr fork */ | |
1778 | { | |
1779 | int error; /* error return value */ | |
1780 | int idx; /* extent record index */ | |
1781 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
1782 | xfs_fileoff_t lastaddr; /* last block number seen */ | |
1783 | xfs_fileoff_t lowest; /* lowest useful block */ | |
1784 | xfs_fileoff_t max; /* starting useful block */ | |
1785 | xfs_fileoff_t off; /* offset for this block */ | |
1786 | xfs_extnum_t nextents; /* number of extent entries */ | |
1787 | ||
1788 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || | |
1789 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || | |
1790 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); | |
1791 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | |
1792 | *first_unused = 0; | |
1793 | return 0; | |
dd9f438e | 1794 | } |
9e5987a7 DC |
1795 | ifp = XFS_IFORK_PTR(ip, whichfork); |
1796 | if (!(ifp->if_flags & XFS_IFEXTENTS) && | |
1797 | (error = xfs_iread_extents(tp, ip, whichfork))) | |
1798 | return error; | |
1799 | lowest = *first_unused; | |
1800 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
1801 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { | |
1802 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); | |
1803 | off = xfs_bmbt_get_startoff(ep); | |
1804 | /* | |
1805 | * See if the hole before this extent will work. | |
1806 | */ | |
1807 | if (off >= lowest + len && off - max >= len) { | |
1808 | *first_unused = max; | |
1809 | return 0; | |
1810 | } | |
1811 | lastaddr = off + xfs_bmbt_get_blockcount(ep); | |
1812 | max = XFS_FILEOFF_MAX(lastaddr, lowest); | |
dd9f438e | 1813 | } |
9e5987a7 DC |
1814 | *first_unused = max; |
1815 | return 0; | |
1816 | } | |
1817 | ||
1818 | /* | |
1819 | * Returns the file-relative block number of the last block + 1 before | |
1820 | * last_block (input value) in the file. | |
1821 | * This is not based on i_size, it is based on the extent records. | |
1822 | * Returns 0 for local files, as they do not have extent records. | |
1823 | */ | |
1824 | int /* error */ | |
1825 | xfs_bmap_last_before( | |
1826 | xfs_trans_t *tp, /* transaction pointer */ | |
1827 | xfs_inode_t *ip, /* incore inode */ | |
1828 | xfs_fileoff_t *last_block, /* last block */ | |
1829 | int whichfork) /* data or attr fork */ | |
1830 | { | |
1831 | xfs_fileoff_t bno; /* input file offset */ | |
1832 | int eof; /* hit end of file */ | |
1833 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ | |
1834 | int error; /* error return value */ | |
1835 | xfs_bmbt_irec_t got; /* current extent value */ | |
1836 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
1837 | xfs_extnum_t lastx; /* last extent used */ | |
1838 | xfs_bmbt_irec_t prev; /* previous extent value */ | |
1839 | ||
1840 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && | |
1841 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
1842 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) | |
1843 | return XFS_ERROR(EIO); | |
1844 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | |
1845 | *last_block = 0; | |
1846 | return 0; | |
1847 | } | |
1848 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1849 | if (!(ifp->if_flags & XFS_IFEXTENTS) && | |
1850 | (error = xfs_iread_extents(tp, ip, whichfork))) | |
1851 | return error; | |
1852 | bno = *last_block - 1; | |
1853 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, | |
1854 | &prev); | |
1855 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { | |
1856 | if (prev.br_startoff == NULLFILEOFF) | |
1857 | *last_block = 0; | |
dd9f438e | 1858 | else |
9e5987a7 | 1859 | *last_block = prev.br_startoff + prev.br_blockcount; |
dd9f438e | 1860 | } |
dd9f438e | 1861 | /* |
9e5987a7 | 1862 | * Otherwise *last_block is already the right answer. |
dd9f438e | 1863 | */ |
9e5987a7 DC |
1864 | return 0; |
1865 | } | |
1866 | ||
1867 | STATIC int | |
1868 | xfs_bmap_last_extent( | |
1869 | struct xfs_trans *tp, | |
1870 | struct xfs_inode *ip, | |
1871 | int whichfork, | |
1872 | struct xfs_bmbt_irec *rec, | |
1873 | int *is_empty) | |
1874 | { | |
1875 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); | |
1876 | int error; | |
1877 | int nextents; | |
1878 | ||
1879 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { | |
1880 | error = xfs_iread_extents(tp, ip, whichfork); | |
1881 | if (error) | |
1882 | return error; | |
dd9f438e NS |
1883 | } |
1884 | ||
9e5987a7 DC |
1885 | nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
1886 | if (nextents == 0) { | |
1887 | *is_empty = 1; | |
1888 | return 0; | |
1889 | } | |
dd9f438e | 1890 | |
9e5987a7 DC |
1891 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
1892 | *is_empty = 0; | |
dd9f438e NS |
1893 | return 0; |
1894 | } | |
1895 | ||
9e5987a7 DC |
1896 | /* |
1897 | * Check the last inode extent to determine whether this allocation will result | |
1898 | * in blocks being allocated at the end of the file. When we allocate new data | |
1899 | * blocks at the end of the file which do not start at the previous data block, | |
1900 | * we will try to align the new blocks at stripe unit boundaries. | |
1901 | * | |
1902 | * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be | |
1903 | * at, or past the EOF. | |
1904 | */ | |
1905 | STATIC int | |
1906 | xfs_bmap_isaeof( | |
1907 | struct xfs_bmalloca *bma, | |
1908 | int whichfork) | |
1da177e4 | 1909 | { |
9e5987a7 DC |
1910 | struct xfs_bmbt_irec rec; |
1911 | int is_empty; | |
1912 | int error; | |
1da177e4 | 1913 | |
9e5987a7 DC |
1914 | bma->aeof = 0; |
1915 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, | |
1916 | &is_empty); | |
1917 | if (error || is_empty) | |
1918 | return error; | |
1da177e4 | 1919 | |
1da177e4 | 1920 | /* |
9e5987a7 DC |
1921 | * Check if we are allocation or past the last extent, or at least into |
1922 | * the last delayed allocated extent. | |
1da177e4 | 1923 | */ |
9e5987a7 DC |
1924 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
1925 | (bma->offset >= rec.br_startoff && | |
1926 | isnullstartblock(rec.br_startblock)); | |
1927 | return 0; | |
1928 | } | |
1da177e4 | 1929 | |
9e5987a7 DC |
1930 | /* |
1931 | * Check if the endoff is outside the last extent. If so the caller will grow | |
1932 | * the allocation to a stripe unit boundary. All offsets are considered outside | |
1933 | * the end of file for an empty fork, so 1 is returned in *eof in that case. | |
1934 | */ | |
1935 | int | |
1936 | xfs_bmap_eof( | |
1937 | struct xfs_inode *ip, | |
1938 | xfs_fileoff_t endoff, | |
1939 | int whichfork, | |
1940 | int *eof) | |
a365bdd5 | 1941 | { |
9e5987a7 DC |
1942 | struct xfs_bmbt_irec rec; |
1943 | int error; | |
a365bdd5 | 1944 | |
9e5987a7 DC |
1945 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof); |
1946 | if (error || *eof) | |
a365bdd5 | 1947 | return error; |
a365bdd5 | 1948 | |
9e5987a7 DC |
1949 | *eof = endoff >= rec.br_startoff + rec.br_blockcount; |
1950 | return 0; | |
1951 | } | |
04e99455 | 1952 | |
9e5987a7 DC |
1953 | /* |
1954 | * Returns the file-relative block number of the first block past eof in | |
1955 | * the file. This is not based on i_size, it is based on the extent records. | |
1956 | * Returns 0 for local files, as they do not have extent records. | |
1957 | */ | |
1958 | int | |
1959 | xfs_bmap_last_offset( | |
1960 | struct xfs_trans *tp, | |
1961 | struct xfs_inode *ip, | |
1962 | xfs_fileoff_t *last_block, | |
1963 | int whichfork) | |
1964 | { | |
1965 | struct xfs_bmbt_irec rec; | |
1966 | int is_empty; | |
1967 | int error; | |
04e99455 | 1968 | |
9e5987a7 | 1969 | *last_block = 0; |
0892ccd6 | 1970 | |
9e5987a7 DC |
1971 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
1972 | return 0; | |
a365bdd5 | 1973 | |
9e5987a7 DC |
1974 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
1975 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | |
1976 | return XFS_ERROR(EIO); | |
a365bdd5 | 1977 | |
9e5987a7 DC |
1978 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
1979 | if (error || is_empty) | |
a365bdd5 | 1980 | return error; |
9e5987a7 DC |
1981 | |
1982 | *last_block = rec.br_startoff + rec.br_blockcount; | |
a365bdd5 NS |
1983 | return 0; |
1984 | } | |
1985 | ||
9e5987a7 DC |
1986 | /* |
1987 | * Returns whether the selected fork of the inode has exactly one | |
1988 | * block or not. For the data fork we check this matches di_size, | |
1989 | * implying the file's range is 0..bsize-1. | |
1990 | */ | |
1991 | int /* 1=>1 block, 0=>otherwise */ | |
1992 | xfs_bmap_one_block( | |
1993 | xfs_inode_t *ip, /* incore inode */ | |
1994 | int whichfork) /* data or attr fork */ | |
c467c049 | 1995 | { |
9e5987a7 DC |
1996 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
1997 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
1998 | int rval; /* return value */ | |
1999 | xfs_bmbt_irec_t s; /* internal version of extent */ | |
c467c049 | 2000 | |
9e5987a7 DC |
2001 | #ifndef DEBUG |
2002 | if (whichfork == XFS_DATA_FORK) | |
2003 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; | |
2004 | #endif /* !DEBUG */ | |
2005 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) | |
2006 | return 0; | |
2007 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | |
2008 | return 0; | |
2009 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
2010 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); | |
2011 | ep = xfs_iext_get_ext(ifp, 0); | |
2012 | xfs_bmbt_get_all(ep, &s); | |
2013 | rval = s.br_startoff == 0 && s.br_blockcount == 1; | |
2014 | if (rval && whichfork == XFS_DATA_FORK) | |
2015 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); | |
2016 | return rval; | |
2017 | } | |
c467c049 | 2018 | |
9e5987a7 DC |
2019 | /* |
2020 | * Extent tree manipulation functions used during allocation. | |
2021 | */ | |
c467c049 | 2022 | |
9e5987a7 DC |
2023 | /* |
2024 | * Convert a delayed allocation to a real allocation. | |
2025 | */ | |
2026 | STATIC int /* error */ | |
2027 | xfs_bmap_add_extent_delay_real( | |
2028 | struct xfs_bmalloca *bma) | |
2029 | { | |
2030 | struct xfs_bmbt_irec *new = &bma->got; | |
2031 | int diff; /* temp value */ | |
2032 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ | |
2033 | int error; /* error return value */ | |
2034 | int i; /* temp state */ | |
2035 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
2036 | xfs_fileoff_t new_endoff; /* end offset of new entry */ | |
2037 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ | |
2038 | /* left is 0, right is 1, prev is 2 */ | |
2039 | int rval=0; /* return value (logging flags) */ | |
2040 | int state = 0;/* state bits, accessed thru macros */ | |
2041 | xfs_filblks_t da_new; /* new count del alloc blocks used */ | |
2042 | xfs_filblks_t da_old; /* old count del alloc blocks used */ | |
2043 | xfs_filblks_t temp=0; /* value for da_new calculations */ | |
2044 | xfs_filblks_t temp2=0;/* value for da_new calculations */ | |
2045 | int tmp_rval; /* partial logging flags */ | |
c467c049 | 2046 | |
9e5987a7 | 2047 | ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK); |
c467c049 | 2048 | |
9e5987a7 DC |
2049 | ASSERT(bma->idx >= 0); |
2050 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); | |
2051 | ASSERT(!isnullstartblock(new->br_startblock)); | |
2052 | ASSERT(!bma->cur || | |
2053 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); | |
c467c049 | 2054 | |
9e5987a7 | 2055 | XFS_STATS_INC(xs_add_exlist); |
c467c049 | 2056 | |
9e5987a7 DC |
2057 | #define LEFT r[0] |
2058 | #define RIGHT r[1] | |
2059 | #define PREV r[2] | |
c467c049 CH |
2060 | |
2061 | /* | |
9e5987a7 | 2062 | * Set up a bunch of variables to make the tests simpler. |
c467c049 | 2063 | */ |
9e5987a7 DC |
2064 | ep = xfs_iext_get_ext(ifp, bma->idx); |
2065 | xfs_bmbt_get_all(ep, &PREV); | |
2066 | new_endoff = new->br_startoff + new->br_blockcount; | |
2067 | ASSERT(PREV.br_startoff <= new->br_startoff); | |
2068 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); | |
2069 | ||
2070 | da_old = startblockval(PREV.br_startblock); | |
2071 | da_new = 0; | |
2072 | ||
c467c049 | 2073 | /* |
9e5987a7 DC |
2074 | * Set flags determining what part of the previous delayed allocation |
2075 | * extent is being replaced by a real allocation. | |
c467c049 | 2076 | */ |
9e5987a7 DC |
2077 | if (PREV.br_startoff == new->br_startoff) |
2078 | state |= BMAP_LEFT_FILLING; | |
2079 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) | |
2080 | state |= BMAP_RIGHT_FILLING; | |
c467c049 CH |
2081 | |
2082 | /* | |
9e5987a7 DC |
2083 | * Check and set flags if this segment has a left neighbor. |
2084 | * Don't set contiguous if the combined extent would be too large. | |
c467c049 | 2085 | */ |
9e5987a7 DC |
2086 | if (bma->idx > 0) { |
2087 | state |= BMAP_LEFT_VALID; | |
2088 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT); | |
a99ebf43 | 2089 | |
9e5987a7 DC |
2090 | if (isnullstartblock(LEFT.br_startblock)) |
2091 | state |= BMAP_LEFT_DELAY; | |
a365bdd5 | 2092 | } |
a365bdd5 | 2093 | |
9e5987a7 DC |
2094 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
2095 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && | |
2096 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && | |
2097 | LEFT.br_state == new->br_state && | |
2098 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) | |
2099 | state |= BMAP_LEFT_CONTIG; | |
a365bdd5 | 2100 | |
1da177e4 | 2101 | /* |
9e5987a7 DC |
2102 | * Check and set flags if this segment has a right neighbor. |
2103 | * Don't set contiguous if the combined extent would be too large. | |
2104 | * Also check for all-three-contiguous being too large. | |
1da177e4 | 2105 | */ |
9e5987a7 DC |
2106 | if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
2107 | state |= BMAP_RIGHT_VALID; | |
2108 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT); | |
14b064ce | 2109 | |
9e5987a7 DC |
2110 | if (isnullstartblock(RIGHT.br_startblock)) |
2111 | state |= BMAP_RIGHT_DELAY; | |
1da177e4 | 2112 | } |
9e5987a7 DC |
2113 | |
2114 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && | |
2115 | new_endoff == RIGHT.br_startoff && | |
2116 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && | |
2117 | new->br_state == RIGHT.br_state && | |
2118 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && | |
2119 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | |
2120 | BMAP_RIGHT_FILLING)) != | |
2121 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | |
2122 | BMAP_RIGHT_FILLING) || | |
2123 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount | |
2124 | <= MAXEXTLEN)) | |
2125 | state |= BMAP_RIGHT_CONTIG; | |
2126 | ||
2127 | error = 0; | |
1da177e4 | 2128 | /* |
9e5987a7 | 2129 | * Switch out based on the FILLING and CONTIG state bits. |
1da177e4 | 2130 | */ |
9e5987a7 DC |
2131 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
2132 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { | |
2133 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | |
2134 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | |
1da177e4 | 2135 | /* |
9e5987a7 DC |
2136 | * Filling in all of a previously delayed allocation extent. |
2137 | * The left and right neighbors are both contiguous with new. | |
1da177e4 | 2138 | */ |
9e5987a7 DC |
2139 | bma->idx--; |
2140 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2141 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), | |
2142 | LEFT.br_blockcount + PREV.br_blockcount + | |
2143 | RIGHT.br_blockcount); | |
2144 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2145 | ||
2146 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); | |
2147 | bma->ip->i_d.di_nextents--; | |
2148 | if (bma->cur == NULL) | |
2149 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2150 | else { | |
2151 | rval = XFS_ILOG_CORE; | |
2152 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, | |
2153 | RIGHT.br_startblock, | |
2154 | RIGHT.br_blockcount, &i); | |
2155 | if (error) | |
2156 | goto done; | |
2157 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2158 | error = xfs_btree_delete(bma->cur, &i); | |
2159 | if (error) | |
2160 | goto done; | |
2161 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2162 | error = xfs_btree_decrement(bma->cur, 0, &i); | |
2163 | if (error) | |
2164 | goto done; | |
2165 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2166 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, | |
2167 | LEFT.br_startblock, | |
2168 | LEFT.br_blockcount + | |
2169 | PREV.br_blockcount + | |
2170 | RIGHT.br_blockcount, LEFT.br_state); | |
2171 | if (error) | |
2172 | goto done; | |
2173 | } | |
2174 | break; | |
2175 | ||
2176 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | |
a365bdd5 | 2177 | /* |
9e5987a7 DC |
2178 | * Filling in all of a previously delayed allocation extent. |
2179 | * The left neighbor is contiguous, the right is not. | |
a365bdd5 | 2180 | */ |
9e5987a7 DC |
2181 | bma->idx--; |
2182 | ||
2183 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2184 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), | |
2185 | LEFT.br_blockcount + PREV.br_blockcount); | |
2186 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2187 | ||
2188 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); | |
2189 | if (bma->cur == NULL) | |
2190 | rval = XFS_ILOG_DEXT; | |
2191 | else { | |
2192 | rval = 0; | |
2193 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, | |
2194 | LEFT.br_startblock, LEFT.br_blockcount, | |
2195 | &i); | |
2196 | if (error) | |
2197 | goto done; | |
2198 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2199 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, | |
2200 | LEFT.br_startblock, | |
2201 | LEFT.br_blockcount + | |
2202 | PREV.br_blockcount, LEFT.br_state); | |
2203 | if (error) | |
2204 | goto done; | |
2205 | } | |
2206 | break; | |
2207 | ||
2208 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | |
0937e0fd | 2209 | /* |
9e5987a7 DC |
2210 | * Filling in all of a previously delayed allocation extent. |
2211 | * The right neighbor is contiguous, the left is not. | |
0937e0fd | 2212 | */ |
9e5987a7 DC |
2213 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
2214 | xfs_bmbt_set_startblock(ep, new->br_startblock); | |
2215 | xfs_bmbt_set_blockcount(ep, | |
2216 | PREV.br_blockcount + RIGHT.br_blockcount); | |
2217 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
0937e0fd | 2218 | |
9e5987a7 DC |
2219 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
2220 | if (bma->cur == NULL) | |
2221 | rval = XFS_ILOG_DEXT; | |
2222 | else { | |
2223 | rval = 0; | |
2224 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, | |
2225 | RIGHT.br_startblock, | |
2226 | RIGHT.br_blockcount, &i); | |
2227 | if (error) | |
2228 | goto done; | |
2229 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2230 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, | |
2231 | new->br_startblock, | |
2232 | PREV.br_blockcount + | |
2233 | RIGHT.br_blockcount, PREV.br_state); | |
2234 | if (error) | |
2235 | goto done; | |
2236 | } | |
2237 | break; | |
2238 | ||
2239 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: | |
a365bdd5 | 2240 | /* |
9e5987a7 DC |
2241 | * Filling in all of a previously delayed allocation extent. |
2242 | * Neither the left nor right neighbors are contiguous with | |
2243 | * the new one. | |
a365bdd5 | 2244 | */ |
9e5987a7 DC |
2245 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
2246 | xfs_bmbt_set_startblock(ep, new->br_startblock); | |
2247 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
a365bdd5 | 2248 | |
9e5987a7 DC |
2249 | bma->ip->i_d.di_nextents++; |
2250 | if (bma->cur == NULL) | |
2251 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2252 | else { | |
2253 | rval = XFS_ILOG_CORE; | |
2254 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, | |
2255 | new->br_startblock, new->br_blockcount, | |
2256 | &i); | |
2257 | if (error) | |
2258 | goto done; | |
2259 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); | |
2260 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; | |
2261 | error = xfs_btree_insert(bma->cur, &i); | |
2262 | if (error) | |
2263 | goto done; | |
2264 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2265 | } | |
2266 | break; | |
1da177e4 | 2267 | |
9e5987a7 | 2268 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
1da177e4 | 2269 | /* |
9e5987a7 DC |
2270 | * Filling in the first part of a previous delayed allocation. |
2271 | * The left neighbor is contiguous. | |
1da177e4 | 2272 | */ |
9e5987a7 DC |
2273 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
2274 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), | |
2275 | LEFT.br_blockcount + new->br_blockcount); | |
2276 | xfs_bmbt_set_startoff(ep, | |
2277 | PREV.br_startoff + new->br_blockcount); | |
2278 | trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_); | |
1da177e4 | 2279 | |
9e5987a7 DC |
2280 | temp = PREV.br_blockcount - new->br_blockcount; |
2281 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2282 | xfs_bmbt_set_blockcount(ep, temp); | |
2283 | if (bma->cur == NULL) | |
2284 | rval = XFS_ILOG_DEXT; | |
2285 | else { | |
2286 | rval = 0; | |
2287 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, | |
2288 | LEFT.br_startblock, LEFT.br_blockcount, | |
2289 | &i); | |
2290 | if (error) | |
2291 | goto done; | |
2292 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2293 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, | |
2294 | LEFT.br_startblock, | |
2295 | LEFT.br_blockcount + | |
2296 | new->br_blockcount, | |
2297 | LEFT.br_state); | |
f3ca8738 | 2298 | if (error) |
1da177e4 | 2299 | goto done; |
1da177e4 | 2300 | } |
9e5987a7 DC |
2301 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
2302 | startblockval(PREV.br_startblock)); | |
2303 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); | |
2304 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2305 | ||
2306 | bma->idx--; | |
2307 | break; | |
2308 | ||
2309 | case BMAP_LEFT_FILLING: | |
1da177e4 | 2310 | /* |
9e5987a7 DC |
2311 | * Filling in the first part of a previous delayed allocation. |
2312 | * The left neighbor is not contiguous. | |
1da177e4 | 2313 | */ |
9e5987a7 DC |
2314 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
2315 | xfs_bmbt_set_startoff(ep, new_endoff); | |
2316 | temp = PREV.br_blockcount - new->br_blockcount; | |
2317 | xfs_bmbt_set_blockcount(ep, temp); | |
2318 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); | |
2319 | bma->ip->i_d.di_nextents++; | |
2320 | if (bma->cur == NULL) | |
2321 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
1da177e4 | 2322 | else { |
9e5987a7 DC |
2323 | rval = XFS_ILOG_CORE; |
2324 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, | |
2325 | new->br_startblock, new->br_blockcount, | |
2326 | &i); | |
2327 | if (error) | |
2328 | goto done; | |
2329 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); | |
2330 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; | |
2331 | error = xfs_btree_insert(bma->cur, &i); | |
2332 | if (error) | |
1da177e4 | 2333 | goto done; |
6bd8fc8a | 2334 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
1da177e4 | 2335 | } |
233eebb9 | 2336 | |
9e5987a7 DC |
2337 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
2338 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, | |
2339 | bma->firstblock, bma->flist, | |
2340 | &bma->cur, 1, &tmp_rval, XFS_DATA_FORK); | |
2341 | rval |= tmp_rval; | |
2342 | if (error) | |
2343 | goto done; | |
1da177e4 | 2344 | } |
9e5987a7 DC |
2345 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
2346 | startblockval(PREV.br_startblock) - | |
2347 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); | |
2348 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); | |
2349 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); | |
2350 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); | |
1da177e4 LT |
2351 | break; |
2352 | ||
9e5987a7 | 2353 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
1da177e4 | 2354 | /* |
9e5987a7 DC |
2355 | * Filling in the last part of a previous delayed allocation. |
2356 | * The right neighbor is contiguous with the new allocation. | |
1da177e4 | 2357 | */ |
9e5987a7 DC |
2358 | temp = PREV.br_blockcount - new->br_blockcount; |
2359 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_); | |
1da177e4 | 2360 | xfs_bmbt_set_blockcount(ep, temp); |
9e5987a7 DC |
2361 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
2362 | new->br_startoff, new->br_startblock, | |
2363 | new->br_blockcount + RIGHT.br_blockcount, | |
2364 | RIGHT.br_state); | |
2365 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); | |
2366 | if (bma->cur == NULL) | |
2367 | rval = XFS_ILOG_DEXT; | |
2368 | else { | |
2369 | rval = 0; | |
2370 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, | |
2371 | RIGHT.br_startblock, | |
2372 | RIGHT.br_blockcount, &i); | |
2373 | if (error) | |
2374 | goto done; | |
2375 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2376 | error = xfs_bmbt_update(bma->cur, new->br_startoff, | |
2377 | new->br_startblock, | |
2378 | new->br_blockcount + | |
2379 | RIGHT.br_blockcount, | |
2380 | RIGHT.br_state); | |
2381 | if (error) | |
2382 | goto done; | |
1da177e4 | 2383 | } |
9e5987a7 DC |
2384 | |
2385 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), | |
2386 | startblockval(PREV.br_startblock)); | |
2387 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2388 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); | |
2389 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2390 | ||
2391 | bma->idx++; | |
1da177e4 LT |
2392 | break; |
2393 | ||
9e5987a7 | 2394 | case BMAP_RIGHT_FILLING: |
1da177e4 | 2395 | /* |
9e5987a7 DC |
2396 | * Filling in the last part of a previous delayed allocation. |
2397 | * The right neighbor is not contiguous. | |
1da177e4 | 2398 | */ |
9e5987a7 DC |
2399 | temp = PREV.br_blockcount - new->br_blockcount; |
2400 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1da177e4 | 2401 | xfs_bmbt_set_blockcount(ep, temp); |
9e5987a7 DC |
2402 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
2403 | bma->ip->i_d.di_nextents++; | |
2404 | if (bma->cur == NULL) | |
2405 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2406 | else { | |
2407 | rval = XFS_ILOG_CORE; | |
2408 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, | |
2409 | new->br_startblock, new->br_blockcount, | |
2410 | &i); | |
2411 | if (error) | |
2412 | goto done; | |
2413 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); | |
2414 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; | |
2415 | error = xfs_btree_insert(bma->cur, &i); | |
2416 | if (error) | |
2417 | goto done; | |
2418 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
1da177e4 | 2419 | } |
9e5987a7 DC |
2420 | |
2421 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { | |
2422 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, | |
2423 | bma->firstblock, bma->flist, &bma->cur, 1, | |
2424 | &tmp_rval, XFS_DATA_FORK); | |
2425 | rval |= tmp_rval; | |
2426 | if (error) | |
2427 | goto done; | |
1da177e4 | 2428 | } |
9e5987a7 DC |
2429 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
2430 | startblockval(PREV.br_startblock) - | |
2431 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); | |
2432 | ep = xfs_iext_get_ext(ifp, bma->idx); | |
2433 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); | |
2434 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2435 | ||
2436 | bma->idx++; | |
1da177e4 LT |
2437 | break; |
2438 | ||
2439 | case 0: | |
2440 | /* | |
9e5987a7 DC |
2441 | * Filling in the middle part of a previous delayed allocation. |
2442 | * Contiguity is impossible here. | |
2443 | * This case is avoided almost all the time. | |
2444 | * | |
2445 | * We start with a delayed allocation: | |
2446 | * | |
2447 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ | |
2448 | * PREV @ idx | |
2449 | * | |
2450 | * and we are allocating: | |
2451 | * +rrrrrrrrrrrrrrrrr+ | |
2452 | * new | |
2453 | * | |
2454 | * and we set it up for insertion as: | |
2455 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ | |
2456 | * new | |
2457 | * PREV @ idx LEFT RIGHT | |
2458 | * inserted at idx + 1 | |
1da177e4 | 2459 | */ |
9e5987a7 DC |
2460 | temp = new->br_startoff - PREV.br_startoff; |
2461 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; | |
2462 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_); | |
2463 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ | |
2464 | LEFT = *new; | |
2465 | RIGHT.br_state = PREV.br_state; | |
2466 | RIGHT.br_startblock = nullstartblock( | |
2467 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); | |
2468 | RIGHT.br_startoff = new_endoff; | |
2469 | RIGHT.br_blockcount = temp2; | |
2470 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ | |
2471 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); | |
2472 | bma->ip->i_d.di_nextents++; | |
2473 | if (bma->cur == NULL) | |
2474 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2475 | else { | |
2476 | rval = XFS_ILOG_CORE; | |
2477 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, | |
2478 | new->br_startblock, new->br_blockcount, | |
2479 | &i); | |
2480 | if (error) | |
2481 | goto done; | |
2482 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); | |
2483 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; | |
2484 | error = xfs_btree_insert(bma->cur, &i); | |
2485 | if (error) | |
2486 | goto done; | |
2487 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
1da177e4 | 2488 | } |
1da177e4 | 2489 | |
9e5987a7 DC |
2490 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
2491 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, | |
2492 | bma->firstblock, bma->flist, &bma->cur, | |
2493 | 1, &tmp_rval, XFS_DATA_FORK); | |
2494 | rval |= tmp_rval; | |
2495 | if (error) | |
2496 | goto done; | |
2497 | } | |
2498 | temp = xfs_bmap_worst_indlen(bma->ip, temp); | |
2499 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); | |
2500 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - | |
2501 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); | |
2502 | if (diff > 0) { | |
2503 | error = xfs_icsb_modify_counters(bma->ip->i_mount, | |
2504 | XFS_SBS_FDBLOCKS, | |
2505 | -((int64_t)diff), 0); | |
2506 | ASSERT(!error); | |
2507 | if (error) | |
2508 | goto done; | |
2509 | } | |
2510 | ||
2511 | ep = xfs_iext_get_ext(ifp, bma->idx); | |
2512 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
2513 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2514 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); | |
2515 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), | |
2516 | nullstartblock((int)temp2)); | |
2517 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_); | |
2518 | ||
2519 | bma->idx++; | |
2520 | da_new = temp + temp2; | |
2521 | break; | |
2522 | ||
2523 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2524 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2525 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: | |
2526 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | |
2527 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2528 | case BMAP_LEFT_CONTIG: | |
2529 | case BMAP_RIGHT_CONTIG: | |
2530 | /* | |
2531 | * These cases are all impossible. | |
2532 | */ | |
2533 | ASSERT(0); | |
2534 | } | |
2535 | ||
2536 | /* convert to a btree if necessary */ | |
2537 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { | |
2538 | int tmp_logflags; /* partial log flag return val */ | |
2539 | ||
2540 | ASSERT(bma->cur == NULL); | |
2541 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, | |
2542 | bma->firstblock, bma->flist, &bma->cur, | |
2543 | da_old > 0, &tmp_logflags, XFS_DATA_FORK); | |
2544 | bma->logflags |= tmp_logflags; | |
2545 | if (error) | |
2546 | goto done; | |
2547 | } | |
2548 | ||
2549 | /* adjust for changes in reserved delayed indirect blocks */ | |
2550 | if (da_old || da_new) { | |
2551 | temp = da_new; | |
2552 | if (bma->cur) | |
2553 | temp += bma->cur->bc_private.b.allocated; | |
2554 | ASSERT(temp <= da_old); | |
2555 | if (temp < da_old) | |
2556 | xfs_icsb_modify_counters(bma->ip->i_mount, | |
2557 | XFS_SBS_FDBLOCKS, | |
2558 | (int64_t)(da_old - temp), 0); | |
96540c78 | 2559 | } |
9e5987a7 DC |
2560 | |
2561 | /* clear out the allocated field, done with it now in any case. */ | |
2562 | if (bma->cur) | |
2563 | bma->cur->bc_private.b.allocated = 0; | |
2564 | ||
2565 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK); | |
1da177e4 | 2566 | done: |
9e5987a7 | 2567 | bma->logflags |= rval; |
1da177e4 | 2568 | return error; |
9e5987a7 DC |
2569 | #undef LEFT |
2570 | #undef RIGHT | |
2571 | #undef PREV | |
1da177e4 LT |
2572 | } |
2573 | ||
2574 | /* | |
9e5987a7 | 2575 | * Convert an unwritten allocation to a real allocation or vice versa. |
1da177e4 | 2576 | */ |
9e5987a7 DC |
2577 | STATIC int /* error */ |
2578 | xfs_bmap_add_extent_unwritten_real( | |
2579 | struct xfs_trans *tp, | |
2580 | xfs_inode_t *ip, /* incore inode pointer */ | |
2581 | xfs_extnum_t *idx, /* extent number to update/insert */ | |
2582 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ | |
2583 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ | |
2584 | xfs_fsblock_t *first, /* pointer to firstblock variable */ | |
2585 | xfs_bmap_free_t *flist, /* list of extents to be freed */ | |
2586 | int *logflagsp) /* inode logging flags */ | |
1da177e4 | 2587 | { |
9e5987a7 DC |
2588 | xfs_btree_cur_t *cur; /* btree cursor */ |
2589 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ | |
2590 | int error; /* error return value */ | |
2591 | int i; /* temp state */ | |
2592 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
2593 | xfs_fileoff_t new_endoff; /* end offset of new entry */ | |
2594 | xfs_exntst_t newext; /* new extent state */ | |
2595 | xfs_exntst_t oldext; /* old extent state */ | |
2596 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ | |
2597 | /* left is 0, right is 1, prev is 2 */ | |
2598 | int rval=0; /* return value (logging flags) */ | |
2599 | int state = 0;/* state bits, accessed thru macros */ | |
1da177e4 | 2600 | |
9e5987a7 | 2601 | *logflagsp = 0; |
1da177e4 | 2602 | |
9e5987a7 DC |
2603 | cur = *curp; |
2604 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | |
8096b1eb | 2605 | |
9e5987a7 DC |
2606 | ASSERT(*idx >= 0); |
2607 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); | |
2608 | ASSERT(!isnullstartblock(new->br_startblock)); | |
2609 | ||
2610 | XFS_STATS_INC(xs_add_exlist); | |
2611 | ||
2612 | #define LEFT r[0] | |
2613 | #define RIGHT r[1] | |
2614 | #define PREV r[2] | |
7cc95a82 | 2615 | |
1da177e4 | 2616 | /* |
9e5987a7 | 2617 | * Set up a bunch of variables to make the tests simpler. |
1da177e4 | 2618 | */ |
9e5987a7 DC |
2619 | error = 0; |
2620 | ep = xfs_iext_get_ext(ifp, *idx); | |
2621 | xfs_bmbt_get_all(ep, &PREV); | |
2622 | newext = new->br_state; | |
2623 | oldext = (newext == XFS_EXT_UNWRITTEN) ? | |
2624 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; | |
2625 | ASSERT(PREV.br_state == oldext); | |
2626 | new_endoff = new->br_startoff + new->br_blockcount; | |
2627 | ASSERT(PREV.br_startoff <= new->br_startoff); | |
2628 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); | |
7cc95a82 | 2629 | |
1da177e4 | 2630 | /* |
9e5987a7 DC |
2631 | * Set flags determining what part of the previous oldext allocation |
2632 | * extent is being replaced by a newext allocation. | |
1da177e4 | 2633 | */ |
9e5987a7 DC |
2634 | if (PREV.br_startoff == new->br_startoff) |
2635 | state |= BMAP_LEFT_FILLING; | |
2636 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) | |
2637 | state |= BMAP_RIGHT_FILLING; | |
2638 | ||
1da177e4 | 2639 | /* |
9e5987a7 DC |
2640 | * Check and set flags if this segment has a left neighbor. |
2641 | * Don't set contiguous if the combined extent would be too large. | |
1da177e4 | 2642 | */ |
9e5987a7 DC |
2643 | if (*idx > 0) { |
2644 | state |= BMAP_LEFT_VALID; | |
2645 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); | |
2646 | ||
2647 | if (isnullstartblock(LEFT.br_startblock)) | |
2648 | state |= BMAP_LEFT_DELAY; | |
1da177e4 | 2649 | } |
9e5987a7 DC |
2650 | |
2651 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && | |
2652 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && | |
2653 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && | |
2654 | LEFT.br_state == newext && | |
2655 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) | |
2656 | state |= BMAP_LEFT_CONTIG; | |
2657 | ||
1da177e4 | 2658 | /* |
9e5987a7 DC |
2659 | * Check and set flags if this segment has a right neighbor. |
2660 | * Don't set contiguous if the combined extent would be too large. | |
2661 | * Also check for all-three-contiguous being too large. | |
1da177e4 | 2662 | */ |
9e5987a7 DC |
2663 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
2664 | state |= BMAP_RIGHT_VALID; | |
2665 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); | |
2666 | if (isnullstartblock(RIGHT.br_startblock)) | |
2667 | state |= BMAP_RIGHT_DELAY; | |
1da177e4 | 2668 | } |
7cc95a82 | 2669 | |
9e5987a7 DC |
2670 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
2671 | new_endoff == RIGHT.br_startoff && | |
2672 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && | |
2673 | newext == RIGHT.br_state && | |
2674 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && | |
2675 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | |
2676 | BMAP_RIGHT_FILLING)) != | |
2677 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | |
2678 | BMAP_RIGHT_FILLING) || | |
2679 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount | |
2680 | <= MAXEXTLEN)) | |
2681 | state |= BMAP_RIGHT_CONTIG; | |
136341b4 | 2682 | |
1da177e4 | 2683 | /* |
9e5987a7 | 2684 | * Switch out based on the FILLING and CONTIG state bits. |
1da177e4 | 2685 | */ |
9e5987a7 DC |
2686 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
2687 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { | |
2688 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | |
2689 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | |
2690 | /* | |
2691 | * Setting all of a previous oldext extent to newext. | |
2692 | * The left and right neighbors are both contiguous with new. | |
2693 | */ | |
2694 | --*idx; | |
1a5902c5 | 2695 | |
9e5987a7 DC |
2696 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
2697 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), | |
2698 | LEFT.br_blockcount + PREV.br_blockcount + | |
2699 | RIGHT.br_blockcount); | |
2700 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
1a5902c5 | 2701 | |
9e5987a7 DC |
2702 | xfs_iext_remove(ip, *idx + 1, 2, state); |
2703 | ip->i_d.di_nextents -= 2; | |
2704 | if (cur == NULL) | |
2705 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2706 | else { | |
2707 | rval = XFS_ILOG_CORE; | |
2708 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, | |
2709 | RIGHT.br_startblock, | |
2710 | RIGHT.br_blockcount, &i))) | |
2711 | goto done; | |
2712 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2713 | if ((error = xfs_btree_delete(cur, &i))) | |
2714 | goto done; | |
2715 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2716 | if ((error = xfs_btree_decrement(cur, 0, &i))) | |
2717 | goto done; | |
2718 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2719 | if ((error = xfs_btree_delete(cur, &i))) | |
2720 | goto done; | |
2721 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2722 | if ((error = xfs_btree_decrement(cur, 0, &i))) | |
2723 | goto done; | |
2724 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2725 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, | |
2726 | LEFT.br_startblock, | |
2727 | LEFT.br_blockcount + PREV.br_blockcount + | |
2728 | RIGHT.br_blockcount, LEFT.br_state))) | |
2729 | goto done; | |
2730 | } | |
2731 | break; | |
1a5902c5 | 2732 | |
9e5987a7 DC |
2733 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
2734 | /* | |
2735 | * Setting all of a previous oldext extent to newext. | |
2736 | * The left neighbor is contiguous, the right is not. | |
2737 | */ | |
2738 | --*idx; | |
d8cc890d | 2739 | |
9e5987a7 DC |
2740 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
2741 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), | |
2742 | LEFT.br_blockcount + PREV.br_blockcount); | |
2743 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
1da177e4 | 2744 | |
9e5987a7 DC |
2745 | xfs_iext_remove(ip, *idx + 1, 1, state); |
2746 | ip->i_d.di_nextents--; | |
2747 | if (cur == NULL) | |
2748 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2749 | else { | |
2750 | rval = XFS_ILOG_CORE; | |
2751 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2752 | PREV.br_startblock, PREV.br_blockcount, | |
2753 | &i))) | |
2754 | goto done; | |
2755 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2756 | if ((error = xfs_btree_delete(cur, &i))) | |
2757 | goto done; | |
2758 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2759 | if ((error = xfs_btree_decrement(cur, 0, &i))) | |
2760 | goto done; | |
2761 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2762 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, | |
2763 | LEFT.br_startblock, | |
2764 | LEFT.br_blockcount + PREV.br_blockcount, | |
2765 | LEFT.br_state))) | |
2766 | goto done; | |
2767 | } | |
2768 | break; | |
1da177e4 | 2769 | |
9e5987a7 | 2770 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
1da177e4 | 2771 | /* |
9e5987a7 DC |
2772 | * Setting all of a previous oldext extent to newext. |
2773 | * The right neighbor is contiguous, the left is not. | |
1da177e4 | 2774 | */ |
9e5987a7 DC |
2775 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
2776 | xfs_bmbt_set_blockcount(ep, | |
2777 | PREV.br_blockcount + RIGHT.br_blockcount); | |
2778 | xfs_bmbt_set_state(ep, newext); | |
2779 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
2780 | xfs_iext_remove(ip, *idx + 1, 1, state); | |
2781 | ip->i_d.di_nextents--; | |
2782 | if (cur == NULL) | |
2783 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2784 | else { | |
2785 | rval = XFS_ILOG_CORE; | |
2786 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, | |
2787 | RIGHT.br_startblock, | |
2788 | RIGHT.br_blockcount, &i))) | |
2789 | goto done; | |
2790 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2791 | if ((error = xfs_btree_delete(cur, &i))) | |
2792 | goto done; | |
2793 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2794 | if ((error = xfs_btree_decrement(cur, 0, &i))) | |
2795 | goto done; | |
2796 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2797 | if ((error = xfs_bmbt_update(cur, new->br_startoff, | |
2798 | new->br_startblock, | |
2799 | new->br_blockcount + RIGHT.br_blockcount, | |
2800 | newext))) | |
2801 | goto done; | |
1da177e4 | 2802 | } |
9e5987a7 | 2803 | break; |
1e82379b | 2804 | |
9e5987a7 DC |
2805 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
2806 | /* | |
2807 | * Setting all of a previous oldext extent to newext. | |
2808 | * Neither the left nor right neighbors are contiguous with | |
2809 | * the new one. | |
2810 | */ | |
2811 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2812 | xfs_bmbt_set_state(ep, newext); | |
2813 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
1e82379b | 2814 | |
9e5987a7 DC |
2815 | if (cur == NULL) |
2816 | rval = XFS_ILOG_DEXT; | |
2817 | else { | |
2818 | rval = 0; | |
2819 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | |
2820 | new->br_startblock, new->br_blockcount, | |
2821 | &i))) | |
2822 | goto done; | |
2823 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2824 | if ((error = xfs_bmbt_update(cur, new->br_startoff, | |
2825 | new->br_startblock, new->br_blockcount, | |
2826 | newext))) | |
2827 | goto done; | |
2828 | } | |
2829 | break; | |
1e82379b | 2830 | |
9e5987a7 DC |
2831 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
2832 | /* | |
2833 | * Setting the first part of a previous oldext extent to newext. | |
2834 | * The left neighbor is contiguous. | |
2835 | */ | |
2836 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); | |
2837 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), | |
2838 | LEFT.br_blockcount + new->br_blockcount); | |
2839 | xfs_bmbt_set_startoff(ep, | |
2840 | PREV.br_startoff + new->br_blockcount); | |
2841 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); | |
1da177e4 | 2842 | |
9e5987a7 DC |
2843 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
2844 | xfs_bmbt_set_startblock(ep, | |
2845 | new->br_startblock + new->br_blockcount); | |
2846 | xfs_bmbt_set_blockcount(ep, | |
2847 | PREV.br_blockcount - new->br_blockcount); | |
2848 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
0293ce3a | 2849 | |
9e5987a7 | 2850 | --*idx; |
8867bc9b | 2851 | |
9e5987a7 DC |
2852 | if (cur == NULL) |
2853 | rval = XFS_ILOG_DEXT; | |
2854 | else { | |
2855 | rval = 0; | |
2856 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2857 | PREV.br_startblock, PREV.br_blockcount, | |
2858 | &i))) | |
2859 | goto done; | |
2860 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2861 | if ((error = xfs_bmbt_update(cur, | |
2862 | PREV.br_startoff + new->br_blockcount, | |
2863 | PREV.br_startblock + new->br_blockcount, | |
2864 | PREV.br_blockcount - new->br_blockcount, | |
2865 | oldext))) | |
2866 | goto done; | |
2867 | if ((error = xfs_btree_decrement(cur, 0, &i))) | |
2868 | goto done; | |
2869 | error = xfs_bmbt_update(cur, LEFT.br_startoff, | |
2870 | LEFT.br_startblock, | |
2871 | LEFT.br_blockcount + new->br_blockcount, | |
2872 | LEFT.br_state); | |
2873 | if (error) | |
2874 | goto done; | |
8867bc9b | 2875 | } |
9e5987a7 | 2876 | break; |
0293ce3a | 2877 | |
9e5987a7 DC |
2878 | case BMAP_LEFT_FILLING: |
2879 | /* | |
2880 | * Setting the first part of a previous oldext extent to newext. | |
2881 | * The left neighbor is not contiguous. | |
2882 | */ | |
2883 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2884 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); | |
2885 | xfs_bmbt_set_startoff(ep, new_endoff); | |
2886 | xfs_bmbt_set_blockcount(ep, | |
2887 | PREV.br_blockcount - new->br_blockcount); | |
2888 | xfs_bmbt_set_startblock(ep, | |
2889 | new->br_startblock + new->br_blockcount); | |
2890 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
1da177e4 | 2891 | |
9e5987a7 DC |
2892 | xfs_iext_insert(ip, *idx, 1, new, state); |
2893 | ip->i_d.di_nextents++; | |
2894 | if (cur == NULL) | |
2895 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2896 | else { | |
2897 | rval = XFS_ILOG_CORE; | |
2898 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2899 | PREV.br_startblock, PREV.br_blockcount, | |
2900 | &i))) | |
2901 | goto done; | |
2902 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2903 | if ((error = xfs_bmbt_update(cur, | |
2904 | PREV.br_startoff + new->br_blockcount, | |
2905 | PREV.br_startblock + new->br_blockcount, | |
2906 | PREV.br_blockcount - new->br_blockcount, | |
2907 | oldext))) | |
2908 | goto done; | |
2909 | cur->bc_rec.b = *new; | |
2910 | if ((error = xfs_btree_insert(cur, &i))) | |
2911 | goto done; | |
2912 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2913 | } | |
2914 | break; | |
1da177e4 | 2915 | |
9e5987a7 DC |
2916 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
2917 | /* | |
2918 | * Setting the last part of a previous oldext extent to newext. | |
2919 | * The right neighbor is contiguous with the new allocation. | |
2920 | */ | |
2921 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2922 | xfs_bmbt_set_blockcount(ep, | |
2923 | PREV.br_blockcount - new->br_blockcount); | |
2924 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
0293ce3a | 2925 | |
9e5987a7 | 2926 | ++*idx; |
1da177e4 | 2927 | |
9e5987a7 DC |
2928 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
2929 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), | |
2930 | new->br_startoff, new->br_startblock, | |
2931 | new->br_blockcount + RIGHT.br_blockcount, newext); | |
2932 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
1da177e4 | 2933 | |
9e5987a7 DC |
2934 | if (cur == NULL) |
2935 | rval = XFS_ILOG_DEXT; | |
2936 | else { | |
2937 | rval = 0; | |
2938 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2939 | PREV.br_startblock, | |
2940 | PREV.br_blockcount, &i))) | |
2941 | goto done; | |
2942 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2943 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, | |
2944 | PREV.br_startblock, | |
2945 | PREV.br_blockcount - new->br_blockcount, | |
2946 | oldext))) | |
2947 | goto done; | |
2948 | if ((error = xfs_btree_increment(cur, 0, &i))) | |
2949 | goto done; | |
2950 | if ((error = xfs_bmbt_update(cur, new->br_startoff, | |
2951 | new->br_startblock, | |
2952 | new->br_blockcount + RIGHT.br_blockcount, | |
2953 | newext))) | |
2954 | goto done; | |
2955 | } | |
2956 | break; | |
1da177e4 | 2957 | |
9e5987a7 DC |
2958 | case BMAP_RIGHT_FILLING: |
2959 | /* | |
2960 | * Setting the last part of a previous oldext extent to newext. | |
2961 | * The right neighbor is not contiguous. | |
2962 | */ | |
2963 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2964 | xfs_bmbt_set_blockcount(ep, | |
2965 | PREV.br_blockcount - new->br_blockcount); | |
2966 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
1da177e4 | 2967 | |
9e5987a7 DC |
2968 | ++*idx; |
2969 | xfs_iext_insert(ip, *idx, 1, new, state); | |
d8cc890d | 2970 | |
9e5987a7 DC |
2971 | ip->i_d.di_nextents++; |
2972 | if (cur == NULL) | |
2973 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2974 | else { | |
2975 | rval = XFS_ILOG_CORE; | |
2976 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2977 | PREV.br_startblock, PREV.br_blockcount, | |
2978 | &i))) | |
2979 | goto done; | |
2980 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2981 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, | |
2982 | PREV.br_startblock, | |
2983 | PREV.br_blockcount - new->br_blockcount, | |
2984 | oldext))) | |
2985 | goto done; | |
2986 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | |
2987 | new->br_startblock, new->br_blockcount, | |
2988 | &i))) | |
2989 | goto done; | |
2990 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); | |
2991 | cur->bc_rec.b.br_state = XFS_EXT_NORM; | |
2992 | if ((error = xfs_btree_insert(cur, &i))) | |
2993 | goto done; | |
2994 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
2995 | } | |
2996 | break; | |
2997 | ||
2998 | case 0: | |
1da177e4 | 2999 | /* |
9e5987a7 DC |
3000 | * Setting the middle part of a previous oldext extent to |
3001 | * newext. Contiguity is impossible here. | |
3002 | * One extent becomes three extents. | |
1da177e4 | 3003 | */ |
9e5987a7 DC |
3004 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
3005 | xfs_bmbt_set_blockcount(ep, | |
3006 | new->br_startoff - PREV.br_startoff); | |
3007 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
898621d5 | 3008 | |
9e5987a7 DC |
3009 | r[0] = *new; |
3010 | r[1].br_startoff = new_endoff; | |
3011 | r[1].br_blockcount = | |
3012 | PREV.br_startoff + PREV.br_blockcount - new_endoff; | |
3013 | r[1].br_startblock = new->br_startblock + new->br_blockcount; | |
3014 | r[1].br_state = oldext; | |
898621d5 | 3015 | |
9e5987a7 DC |
3016 | ++*idx; |
3017 | xfs_iext_insert(ip, *idx, 2, &r[0], state); | |
3018 | ||
3019 | ip->i_d.di_nextents += 2; | |
3020 | if (cur == NULL) | |
3021 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
3022 | else { | |
3023 | rval = XFS_ILOG_CORE; | |
3024 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
3025 | PREV.br_startblock, PREV.br_blockcount, | |
3026 | &i))) | |
3027 | goto done; | |
3028 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
3029 | /* new right extent - oldext */ | |
3030 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, | |
3031 | r[1].br_startblock, r[1].br_blockcount, | |
3032 | r[1].br_state))) | |
3033 | goto done; | |
3034 | /* new left extent - oldext */ | |
3035 | cur->bc_rec.b = PREV; | |
3036 | cur->bc_rec.b.br_blockcount = | |
3037 | new->br_startoff - PREV.br_startoff; | |
3038 | if ((error = xfs_btree_insert(cur, &i))) | |
3039 | goto done; | |
3040 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
3041 | /* | |
3042 | * Reset the cursor to the position of the new extent | |
3043 | * we are about to insert as we can't trust it after | |
3044 | * the previous insert. | |
3045 | */ | |
3046 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | |
3047 | new->br_startblock, new->br_blockcount, | |
3048 | &i))) | |
3049 | goto done; | |
3050 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); | |
3051 | /* new middle extent - newext */ | |
3052 | cur->bc_rec.b.br_state = new->br_state; | |
3053 | if ((error = xfs_btree_insert(cur, &i))) | |
3054 | goto done; | |
3055 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
3056 | } | |
1da177e4 | 3057 | break; |
9e5987a7 DC |
3058 | |
3059 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
3060 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
3061 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: | |
3062 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | |
3063 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
3064 | case BMAP_LEFT_CONTIG: | |
3065 | case BMAP_RIGHT_CONTIG: | |
3066 | /* | |
3067 | * These cases are all impossible. | |
3068 | */ | |
1da177e4 | 3069 | ASSERT(0); |
1da177e4 | 3070 | } |
8096b1eb | 3071 | |
9e5987a7 DC |
3072 | /* convert to a btree if necessary */ |
3073 | if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) { | |
3074 | int tmp_logflags; /* partial log flag return val */ | |
3075 | ||
3076 | ASSERT(cur == NULL); | |
3077 | error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur, | |
3078 | 0, &tmp_logflags, XFS_DATA_FORK); | |
3079 | *logflagsp |= tmp_logflags; | |
3080 | if (error) | |
3081 | goto done; | |
1da177e4 | 3082 | } |
da087bad | 3083 | |
9e5987a7 DC |
3084 | /* clear out the allocated field, done with it now in any case. */ |
3085 | if (cur) { | |
3086 | cur->bc_private.b.allocated = 0; | |
3087 | *curp = cur; | |
1da177e4 | 3088 | } |
8096b1eb | 3089 | |
9e5987a7 DC |
3090 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); |
3091 | done: | |
3092 | *logflagsp |= rval; | |
1da177e4 | 3093 | return error; |
9e5987a7 DC |
3094 | #undef LEFT |
3095 | #undef RIGHT | |
3096 | #undef PREV | |
1da177e4 LT |
3097 | } |
3098 | ||
3099 | /* | |
9e5987a7 | 3100 | * Convert a hole to a delayed allocation. |
1da177e4 | 3101 | */ |
9e5987a7 DC |
3102 | STATIC void |
3103 | xfs_bmap_add_extent_hole_delay( | |
3104 | xfs_inode_t *ip, /* incore inode pointer */ | |
3105 | xfs_extnum_t *idx, /* extent number to update/insert */ | |
3106 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ | |
1da177e4 | 3107 | { |
9e5987a7 DC |
3108 | xfs_ifork_t *ifp; /* inode fork pointer */ |
3109 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ | |
3110 | xfs_filblks_t newlen=0; /* new indirect size */ | |
3111 | xfs_filblks_t oldlen=0; /* old indirect size */ | |
3112 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ | |
3113 | int state; /* state bits, accessed thru macros */ | |
3114 | xfs_filblks_t temp=0; /* temp for indirect calculations */ | |
1da177e4 | 3115 | |
9e5987a7 DC |
3116 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
3117 | state = 0; | |
3118 | ASSERT(isnullstartblock(new->br_startblock)); | |
1da177e4 LT |
3119 | |
3120 | /* | |
9e5987a7 | 3121 | * Check and set flags if this segment has a left neighbor |
1da177e4 | 3122 | */ |
9e5987a7 DC |
3123 | if (*idx > 0) { |
3124 | state |= BMAP_LEFT_VALID; | |
3125 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); | |
3126 | ||
3127 | if (isnullstartblock(left.br_startblock)) | |
3128 | state |= BMAP_LEFT_DELAY; | |
1da177e4 | 3129 | } |
1da177e4 | 3130 | |
9e5987a7 DC |
3131 | /* |
3132 | * Check and set flags if the current (right) segment exists. | |
3133 | * If it doesn't exist, we're converting the hole at end-of-file. | |
3134 | */ | |
3135 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { | |
3136 | state |= BMAP_RIGHT_VALID; | |
3137 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); | |
1da177e4 | 3138 | |
9e5987a7 DC |
3139 | if (isnullstartblock(right.br_startblock)) |
3140 | state |= BMAP_RIGHT_DELAY; | |
1da177e4 | 3141 | } |
9e5987a7 | 3142 | |
1da177e4 | 3143 | /* |
9e5987a7 DC |
3144 | * Set contiguity flags on the left and right neighbors. |
3145 | * Don't let extents get too large, even if the pieces are contiguous. | |
1da177e4 | 3146 | */ |
9e5987a7 DC |
3147 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
3148 | left.br_startoff + left.br_blockcount == new->br_startoff && | |
3149 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) | |
3150 | state |= BMAP_LEFT_CONTIG; | |
3151 | ||
3152 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && | |
3153 | new->br_startoff + new->br_blockcount == right.br_startoff && | |
3154 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && | |
3155 | (!(state & BMAP_LEFT_CONTIG) || | |
3156 | (left.br_blockcount + new->br_blockcount + | |
3157 | right.br_blockcount <= MAXEXTLEN))) | |
3158 | state |= BMAP_RIGHT_CONTIG; | |
cc09c0dc DC |
3159 | |
3160 | /* | |
9e5987a7 | 3161 | * Switch out based on the contiguity flags. |
cc09c0dc | 3162 | */ |
9e5987a7 DC |
3163 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
3164 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
3165 | /* | |
3166 | * New allocation is contiguous with delayed allocations | |
3167 | * on the left and on the right. | |
3168 | * Merge all three into a single extent record. | |
3169 | */ | |
3170 | --*idx; | |
3171 | temp = left.br_blockcount + new->br_blockcount + | |
3172 | right.br_blockcount; | |
cc09c0dc | 3173 | |
9e5987a7 DC |
3174 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
3175 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); | |
3176 | oldlen = startblockval(left.br_startblock) + | |
3177 | startblockval(new->br_startblock) + | |
3178 | startblockval(right.br_startblock); | |
3179 | newlen = xfs_bmap_worst_indlen(ip, temp); | |
3180 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), | |
3181 | nullstartblock((int)newlen)); | |
3182 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
1da177e4 | 3183 | |
9e5987a7 DC |
3184 | xfs_iext_remove(ip, *idx + 1, 1, state); |
3185 | break; | |
1da177e4 | 3186 | |
9e5987a7 DC |
3187 | case BMAP_LEFT_CONTIG: |
3188 | /* | |
3189 | * New allocation is contiguous with a delayed allocation | |
3190 | * on the left. | |
3191 | * Merge the new allocation with the left neighbor. | |
3192 | */ | |
3193 | --*idx; | |
3194 | temp = left.br_blockcount + new->br_blockcount; | |
1da177e4 | 3195 | |
9e5987a7 DC |
3196 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
3197 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); | |
3198 | oldlen = startblockval(left.br_startblock) + | |
3199 | startblockval(new->br_startblock); | |
3200 | newlen = xfs_bmap_worst_indlen(ip, temp); | |
3201 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), | |
3202 | nullstartblock((int)newlen)); | |
3203 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
3204 | break; | |
1da177e4 | 3205 | |
9e5987a7 DC |
3206 | case BMAP_RIGHT_CONTIG: |
3207 | /* | |
3208 | * New allocation is contiguous with a delayed allocation | |
3209 | * on the right. | |
3210 | * Merge the new allocation with the right neighbor. | |
3211 | */ | |
3212 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
3213 | temp = new->br_blockcount + right.br_blockcount; | |
3214 | oldlen = startblockval(new->br_startblock) + | |
3215 | startblockval(right.br_startblock); | |
3216 | newlen = xfs_bmap_worst_indlen(ip, temp); | |
3217 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), | |
3218 | new->br_startoff, | |
3219 | nullstartblock((int)newlen), temp, right.br_state); | |
3220 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
3221 | break; | |
3222 | ||
3223 | case 0: | |
3224 | /* | |
3225 | * New allocation is not contiguous with another | |
3226 | * delayed allocation. | |
3227 | * Insert a new entry. | |
3228 | */ | |
3229 | oldlen = newlen = 0; | |
3230 | xfs_iext_insert(ip, *idx, 1, new, state); | |
3231 | break; | |
1da177e4 | 3232 | } |
9e5987a7 DC |
3233 | if (oldlen != newlen) { |
3234 | ASSERT(oldlen > newlen); | |
3235 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, | |
3236 | (int64_t)(oldlen - newlen), 0); | |
1da177e4 | 3237 | /* |
9e5987a7 | 3238 | * Nothing to do for disk quota accounting here. |
1da177e4 | 3239 | */ |
1da177e4 | 3240 | } |
1da177e4 LT |
3241 | } |
3242 | ||
3243 | /* | |
9e5987a7 | 3244 | * Convert a hole to a real allocation. |
1da177e4 | 3245 | */ |
9e5987a7 DC |
3246 | STATIC int /* error */ |
3247 | xfs_bmap_add_extent_hole_real( | |
3248 | struct xfs_bmalloca *bma, | |
3249 | int whichfork) | |
27a3f8f2 | 3250 | { |
9e5987a7 DC |
3251 | struct xfs_bmbt_irec *new = &bma->got; |
3252 | int error; /* error return value */ | |
3253 | int i; /* temp state */ | |
3254 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
3255 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ | |
3256 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ | |
3257 | int rval=0; /* return value (logging flags) */ | |
3258 | int state; /* state bits, accessed thru macros */ | |
27a3f8f2 | 3259 | |
9e5987a7 | 3260 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
27a3f8f2 | 3261 | |
9e5987a7 DC |
3262 | ASSERT(bma->idx >= 0); |
3263 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); | |
3264 | ASSERT(!isnullstartblock(new->br_startblock)); | |
3265 | ASSERT(!bma->cur || | |
3266 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); | |
27a3f8f2 | 3267 | |
9e5987a7 | 3268 | XFS_STATS_INC(xs_add_exlist); |
27a3f8f2 | 3269 | |
9e5987a7 DC |
3270 | state = 0; |
3271 | if (whichfork == XFS_ATTR_FORK) | |
3272 | state |= BMAP_ATTRFORK; | |
27a3f8f2 | 3273 | |
9e5987a7 DC |
3274 | /* |
3275 | * Check and set flags if this segment has a left neighbor. | |
3276 | */ | |
3277 | if (bma->idx > 0) { | |
3278 | state |= BMAP_LEFT_VALID; | |
3279 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left); | |
3280 | if (isnullstartblock(left.br_startblock)) | |
3281 | state |= BMAP_LEFT_DELAY; | |
3282 | } | |
27a3f8f2 CH |
3283 | |
3284 | /* | |
9e5987a7 DC |
3285 | * Check and set flags if this segment has a current value. |
3286 | * Not true if we're inserting into the "hole" at eof. | |
27a3f8f2 | 3287 | */ |
9e5987a7 DC |
3288 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
3289 | state |= BMAP_RIGHT_VALID; | |
3290 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); | |
3291 | if (isnullstartblock(right.br_startblock)) | |
3292 | state |= BMAP_RIGHT_DELAY; | |
3293 | } | |
27a3f8f2 | 3294 | |
9e5987a7 DC |
3295 | /* |
3296 | * We're inserting a real allocation between "left" and "right". | |
3297 | * Set the contiguity flags. Don't let extents get too large. | |
3298 | */ | |
3299 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && | |
3300 | left.br_startoff + left.br_blockcount == new->br_startoff && | |
3301 | left.br_startblock + left.br_blockcount == new->br_startblock && | |
3302 | left.br_state == new->br_state && | |
3303 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) | |
3304 | state |= BMAP_LEFT_CONTIG; | |
27a3f8f2 | 3305 | |
9e5987a7 DC |
3306 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
3307 | new->br_startoff + new->br_blockcount == right.br_startoff && | |
3308 | new->br_startblock + new->br_blockcount == right.br_startblock && | |
3309 | new->br_state == right.br_state && | |
3310 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && | |
3311 | (!(state & BMAP_LEFT_CONTIG) || | |
3312 | left.br_blockcount + new->br_blockcount + | |
3313 | right.br_blockcount <= MAXEXTLEN)) | |
3314 | state |= BMAP_RIGHT_CONTIG; | |
27a3f8f2 | 3315 | |
9e5987a7 DC |
3316 | error = 0; |
3317 | /* | |
3318 | * Select which case we're in here, and implement it. | |
3319 | */ | |
3320 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { | |
3321 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
3322 | /* | |
3323 | * New allocation is contiguous with real allocations on the | |
3324 | * left and on the right. | |
3325 | * Merge all three into a single extent record. | |
3326 | */ | |
3327 | --bma->idx; | |
3328 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
3329 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), | |
3330 | left.br_blockcount + new->br_blockcount + | |
3331 | right.br_blockcount); | |
3332 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
27a3f8f2 | 3333 | |
9e5987a7 | 3334 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
27a3f8f2 | 3335 | |
9e5987a7 DC |
3336 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
3337 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); | |
3338 | if (bma->cur == NULL) { | |
3339 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | |
3340 | } else { | |
3341 | rval = XFS_ILOG_CORE; | |
3342 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, | |
3343 | right.br_startblock, right.br_blockcount, | |
3344 | &i); | |
3345 | if (error) | |
3346 | goto done; | |
3347 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
3348 | error = xfs_btree_delete(bma->cur, &i); | |
3349 | if (error) | |
3350 | goto done; | |
3351 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
3352 | error = xfs_btree_decrement(bma->cur, 0, &i); | |
3353 | if (error) | |
3354 | goto done; | |
3355 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
3356 | error = xfs_bmbt_update(bma->cur, left.br_startoff, | |
3357 | left.br_startblock, | |
3358 | left.br_blockcount + | |
3359 | new->br_blockcount + | |
3360 | right.br_blockcount, | |
3361 | left.br_state); | |
3362 | if (error) | |
3363 | goto done; | |
3364 | } | |
3365 | break; | |
27a3f8f2 | 3366 | |
9e5987a7 DC |
3367 | case BMAP_LEFT_CONTIG: |
3368 | /* | |
3369 | * New allocation is contiguous with a real allocation | |
3370 | * on the left. | |
3371 | * Merge the new allocation with the left neighbor. | |
3372 | */ | |
3373 | --bma->idx; | |
3374 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
3375 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), | |
3376 | left.br_blockcount + new->br_blockcount); | |
3377 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1da177e4 | 3378 | |
9e5987a7 DC |
3379 | if (bma->cur == NULL) { |
3380 | rval = xfs_ilog_fext(whichfork); | |
3381 | } else { | |
3382 | rval = 0; | |
3383 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, | |
3384 | left.br_startblock, left.br_blockcount, | |
3385 | &i); | |
3386 | if (error) | |
3387 | goto done; | |
3388 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
3389 | error = xfs_bmbt_update(bma->cur, left.br_startoff, | |
3390 | left.br_startblock, | |
3391 | left.br_blockcount + | |
3392 | new->br_blockcount, | |
3393 | left.br_state); | |
3394 | if (error) | |
3395 | goto done; | |
3396 | } | |
3397 | break; | |
27a3f8f2 | 3398 | |
9e5987a7 DC |
3399 | case BMAP_RIGHT_CONTIG: |
3400 | /* | |
3401 | * New allocation is contiguous with a real allocation | |
3402 | * on the right. | |
3403 | * Merge the new allocation with the right neighbor. | |
3404 | */ | |
3405 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
3406 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), | |
3407 | new->br_startoff, new->br_startblock, | |
3408 | new->br_blockcount + right.br_blockcount, | |
3409 | right.br_state); | |
3410 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
27a3f8f2 | 3411 | |
9e5987a7 DC |
3412 | if (bma->cur == NULL) { |
3413 | rval = xfs_ilog_fext(whichfork); | |
3414 | } else { | |
3415 | rval = 0; | |
3416 | error = xfs_bmbt_lookup_eq(bma->cur, | |
3417 | right.br_startoff, | |
3418 | right.br_startblock, | |
3419 | right.br_blockcount, &i); | |
3420 | if (error) | |
3421 | goto done; | |
3422 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
3423 | error = xfs_bmbt_update(bma->cur, new->br_startoff, | |
3424 | new->br_startblock, | |
3425 | new->br_blockcount + | |
3426 | right.br_blockcount, | |
3427 | right.br_state); | |
3428 | if (error) | |
3429 | goto done; | |
3430 | } | |
3431 | break; | |
3432 | ||
3433 | case 0: | |
3434 | /* | |
3435 | * New allocation is not contiguous with another | |
3436 | * real allocation. | |
3437 | * Insert a new entry. | |
3438 | */ | |
3439 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); | |
3440 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, | |
3441 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); | |
3442 | if (bma->cur == NULL) { | |
3443 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | |
3444 | } else { | |
3445 | rval = XFS_ILOG_CORE; | |
3446 | error = xfs_bmbt_lookup_eq(bma->cur, | |
3447 | new->br_startoff, | |
3448 | new->br_startblock, | |
3449 | new->br_blockcount, &i); | |
3450 | if (error) | |
3451 | goto done; | |
3452 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); | |
3453 | bma->cur->bc_rec.b.br_state = new->br_state; | |
3454 | error = xfs_btree_insert(bma->cur, &i); | |
3455 | if (error) | |
3456 | goto done; | |
3457 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
3458 | } | |
3459 | break; | |
3460 | } | |
3461 | ||
3462 | /* convert to a btree if necessary */ | |
3463 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { | |
3464 | int tmp_logflags; /* partial log flag return val */ | |
3465 | ||
3466 | ASSERT(bma->cur == NULL); | |
3467 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, | |
3468 | bma->firstblock, bma->flist, &bma->cur, | |
3469 | 0, &tmp_logflags, whichfork); | |
3470 | bma->logflags |= tmp_logflags; | |
3471 | if (error) | |
3472 | goto done; | |
3473 | } | |
3474 | ||
3475 | /* clear out the allocated field, done with it now in any case. */ | |
3476 | if (bma->cur) | |
3477 | bma->cur->bc_private.b.allocated = 0; | |
3478 | ||
3479 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); | |
3480 | done: | |
3481 | bma->logflags |= rval; | |
3482 | return error; | |
1da177e4 LT |
3483 | } |
3484 | ||
3485 | /* | |
9e5987a7 | 3486 | * Functions used in the extent read, allocate and remove paths |
1da177e4 | 3487 | */ |
1da177e4 | 3488 | |
9e5987a7 DC |
3489 | /* |
3490 | * Adjust the size of the new extent based on di_extsize and rt extsize. | |
3491 | */ | |
4e8938fe | 3492 | STATIC int |
9e5987a7 DC |
3493 | xfs_bmap_extsize_align( |
3494 | xfs_mount_t *mp, | |
3495 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ | |
3496 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ | |
3497 | xfs_extlen_t extsz, /* align to this extent size */ | |
3498 | int rt, /* is this a realtime inode? */ | |
3499 | int eof, /* is extent at end-of-file? */ | |
3500 | int delay, /* creating delalloc extent? */ | |
3501 | int convert, /* overwriting unwritten extent? */ | |
3502 | xfs_fileoff_t *offp, /* in/out: aligned offset */ | |
3503 | xfs_extlen_t *lenp) /* in/out: aligned length */ | |
4e8938fe | 3504 | { |
9e5987a7 DC |
3505 | xfs_fileoff_t orig_off; /* original offset */ |
3506 | xfs_extlen_t orig_alen; /* original length */ | |
3507 | xfs_fileoff_t orig_end; /* original off+len */ | |
3508 | xfs_fileoff_t nexto; /* next file offset */ | |
3509 | xfs_fileoff_t prevo; /* previous file offset */ | |
3510 | xfs_fileoff_t align_off; /* temp for offset */ | |
3511 | xfs_extlen_t align_alen; /* temp for length */ | |
3512 | xfs_extlen_t temp; /* temp for calculations */ | |
4e8938fe | 3513 | |
9e5987a7 | 3514 | if (convert) |
4e8938fe | 3515 | return 0; |
4e8938fe | 3516 | |
9e5987a7 DC |
3517 | orig_off = align_off = *offp; |
3518 | orig_alen = align_alen = *lenp; | |
3519 | orig_end = orig_off + orig_alen; | |
1da177e4 | 3520 | |
1da177e4 | 3521 | /* |
9e5987a7 DC |
3522 | * If this request overlaps an existing extent, then don't |
3523 | * attempt to perform any additional alignment. | |
1da177e4 | 3524 | */ |
9e5987a7 DC |
3525 | if (!delay && !eof && |
3526 | (orig_off >= gotp->br_startoff) && | |
3527 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { | |
3528 | return 0; | |
3529 | } | |
3530 | ||
1da177e4 | 3531 | /* |
9e5987a7 DC |
3532 | * If the file offset is unaligned vs. the extent size |
3533 | * we need to align it. This will be possible unless | |
3534 | * the file was previously written with a kernel that didn't | |
3535 | * perform this alignment, or if a truncate shot us in the | |
3536 | * foot. | |
1da177e4 | 3537 | */ |
9e5987a7 DC |
3538 | temp = do_mod(orig_off, extsz); |
3539 | if (temp) { | |
3540 | align_alen += temp; | |
3541 | align_off -= temp; | |
1da177e4 LT |
3542 | } |
3543 | /* | |
9e5987a7 | 3544 | * Same adjustment for the end of the requested area. |
1da177e4 | 3545 | */ |
9e5987a7 DC |
3546 | if ((temp = (align_alen % extsz))) { |
3547 | align_alen += extsz - temp; | |
3548 | } | |
1da177e4 | 3549 | /* |
9e5987a7 DC |
3550 | * If the previous block overlaps with this proposed allocation |
3551 | * then move the start forward without adjusting the length. | |
1da177e4 | 3552 | */ |
9e5987a7 DC |
3553 | if (prevp->br_startoff != NULLFILEOFF) { |
3554 | if (prevp->br_startblock == HOLESTARTBLOCK) | |
3555 | prevo = prevp->br_startoff; | |
3556 | else | |
3557 | prevo = prevp->br_startoff + prevp->br_blockcount; | |
3558 | } else | |
3559 | prevo = 0; | |
3560 | if (align_off != orig_off && align_off < prevo) | |
3561 | align_off = prevo; | |
3562 | /* | |
3563 | * If the next block overlaps with this proposed allocation | |
3564 | * then move the start back without adjusting the length, | |
3565 | * but not before offset 0. | |
3566 | * This may of course make the start overlap previous block, | |
3567 | * and if we hit the offset 0 limit then the next block | |
3568 | * can still overlap too. | |
3569 | */ | |
3570 | if (!eof && gotp->br_startoff != NULLFILEOFF) { | |
3571 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || | |
3572 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) | |
3573 | nexto = gotp->br_startoff + gotp->br_blockcount; | |
3574 | else | |
3575 | nexto = gotp->br_startoff; | |
3576 | } else | |
3577 | nexto = NULLFILEOFF; | |
3578 | if (!eof && | |
3579 | align_off + align_alen != orig_end && | |
3580 | align_off + align_alen > nexto) | |
3581 | align_off = nexto > align_alen ? nexto - align_alen : 0; | |
3582 | /* | |
3583 | * If we're now overlapping the next or previous extent that | |
3584 | * means we can't fit an extsz piece in this hole. Just move | |
3585 | * the start forward to the first valid spot and set | |
3586 | * the length so we hit the end. | |
3587 | */ | |
3588 | if (align_off != orig_off && align_off < prevo) | |
3589 | align_off = prevo; | |
3590 | if (align_off + align_alen != orig_end && | |
3591 | align_off + align_alen > nexto && | |
3592 | nexto != NULLFILEOFF) { | |
3593 | ASSERT(nexto > prevo); | |
3594 | align_alen = nexto - align_off; | |
3595 | } | |
1da177e4 | 3596 | |
9e5987a7 DC |
3597 | /* |
3598 | * If realtime, and the result isn't a multiple of the realtime | |
3599 | * extent size we need to remove blocks until it is. | |
3600 | */ | |
3601 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { | |
1da177e4 | 3602 | /* |
9e5987a7 DC |
3603 | * We're not covering the original request, or |
3604 | * we won't be able to once we fix the length. | |
1da177e4 | 3605 | */ |
9e5987a7 DC |
3606 | if (orig_off < align_off || |
3607 | orig_end > align_off + align_alen || | |
3608 | align_alen - temp < orig_alen) | |
3609 | return XFS_ERROR(EINVAL); | |
1da177e4 | 3610 | /* |
9e5987a7 | 3611 | * Try to fix it by moving the start up. |
1da177e4 | 3612 | */ |
9e5987a7 DC |
3613 | if (align_off + temp <= orig_off) { |
3614 | align_alen -= temp; | |
3615 | align_off += temp; | |
1da177e4 | 3616 | } |
9e5987a7 DC |
3617 | /* |
3618 | * Try to fix it by moving the end in. | |
3619 | */ | |
3620 | else if (align_off + align_alen - temp >= orig_end) | |
3621 | align_alen -= temp; | |
3622 | /* | |
3623 | * Set the start to the minimum then trim the length. | |
3624 | */ | |
3625 | else { | |
3626 | align_alen -= orig_off - align_off; | |
3627 | align_off = orig_off; | |
3628 | align_alen -= align_alen % mp->m_sb.sb_rextsize; | |
1da177e4 | 3629 | } |
1da177e4 | 3630 | /* |
9e5987a7 | 3631 | * Result doesn't cover the request, fail it. |
1da177e4 | 3632 | */ |
9e5987a7 DC |
3633 | if (orig_off < align_off || orig_end > align_off + align_alen) |
3634 | return XFS_ERROR(EINVAL); | |
3635 | } else { | |
3636 | ASSERT(orig_off >= align_off); | |
3637 | ASSERT(orig_end <= align_off + align_alen); | |
1da177e4 | 3638 | } |
1da177e4 | 3639 | |
0b1b213f | 3640 | #ifdef DEBUG |
9e5987a7 DC |
3641 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
3642 | ASSERT(align_off + align_alen <= gotp->br_startoff); | |
3643 | if (prevp->br_startoff != NULLFILEOFF) | |
3644 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); | |
3645 | #endif | |
1da177e4 | 3646 | |
9e5987a7 DC |
3647 | *lenp = align_alen; |
3648 | *offp = align_off; | |
3649 | return 0; | |
1da177e4 | 3650 | } |
1da177e4 | 3651 | |
9e5987a7 DC |
3652 | #define XFS_ALLOC_GAP_UNITS 4 |
3653 | ||
1da177e4 | 3654 | STATIC void |
9e5987a7 DC |
3655 | xfs_bmap_adjacent( |
3656 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ | |
1da177e4 | 3657 | { |
9e5987a7 DC |
3658 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
3659 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ | |
3660 | xfs_mount_t *mp; /* mount point structure */ | |
3661 | int nullfb; /* true if ap->firstblock isn't set */ | |
3662 | int rt; /* true if inode is realtime */ | |
1da177e4 | 3663 | |
9e5987a7 DC |
3664 | #define ISVALID(x,y) \ |
3665 | (rt ? \ | |
3666 | (x) < mp->m_sb.sb_rblocks : \ | |
3667 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ | |
3668 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ | |
3669 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) | |
1da177e4 | 3670 | |
9e5987a7 DC |
3671 | mp = ap->ip->i_mount; |
3672 | nullfb = *ap->firstblock == NULLFSBLOCK; | |
3673 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; | |
3674 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); | |
3675 | /* | |
3676 | * If allocating at eof, and there's a previous real block, | |
3677 | * try to use its last block as our starting point. | |
3678 | */ | |
3679 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && | |
3680 | !isnullstartblock(ap->prev.br_startblock) && | |
3681 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, | |
3682 | ap->prev.br_startblock)) { | |
3683 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; | |
3684 | /* | |
3685 | * Adjust for the gap between prevp and us. | |
3686 | */ | |
3687 | adjust = ap->offset - | |
3688 | (ap->prev.br_startoff + ap->prev.br_blockcount); | |
3689 | if (adjust && | |
3690 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) | |
3691 | ap->blkno += adjust; | |
3692 | } | |
3693 | /* | |
3694 | * If not at eof, then compare the two neighbor blocks. | |
3695 | * Figure out whether either one gives us a good starting point, | |
3696 | * and pick the better one. | |
3697 | */ | |
3698 | else if (!ap->eof) { | |
3699 | xfs_fsblock_t gotbno; /* right side block number */ | |
3700 | xfs_fsblock_t gotdiff=0; /* right side difference */ | |
3701 | xfs_fsblock_t prevbno; /* left side block number */ | |
3702 | xfs_fsblock_t prevdiff=0; /* left side difference */ | |
3703 | ||
3704 | /* | |
3705 | * If there's a previous (left) block, select a requested | |
3706 | * start block based on it. | |
3707 | */ | |
3708 | if (ap->prev.br_startoff != NULLFILEOFF && | |
3709 | !isnullstartblock(ap->prev.br_startblock) && | |
3710 | (prevbno = ap->prev.br_startblock + | |
3711 | ap->prev.br_blockcount) && | |
3712 | ISVALID(prevbno, ap->prev.br_startblock)) { | |
3713 | /* | |
3714 | * Calculate gap to end of previous block. | |
3715 | */ | |
3716 | adjust = prevdiff = ap->offset - | |
3717 | (ap->prev.br_startoff + | |
3718 | ap->prev.br_blockcount); | |
3719 | /* | |
3720 | * Figure the startblock based on the previous block's | |
3721 | * end and the gap size. | |
3722 | * Heuristic! | |
3723 | * If the gap is large relative to the piece we're | |
3724 | * allocating, or using it gives us an invalid block | |
3725 | * number, then just use the end of the previous block. | |
3726 | */ | |
3727 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && | |
3728 | ISVALID(prevbno + prevdiff, | |
3729 | ap->prev.br_startblock)) | |
3730 | prevbno += adjust; | |
3731 | else | |
3732 | prevdiff += adjust; | |
3733 | /* | |
3734 | * If the firstblock forbids it, can't use it, | |
3735 | * must use default. | |
3736 | */ | |
3737 | if (!rt && !nullfb && | |
3738 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) | |
3739 | prevbno = NULLFSBLOCK; | |
1da177e4 | 3740 | } |
9e5987a7 DC |
3741 | /* |
3742 | * No previous block or can't follow it, just default. | |
3743 | */ | |
3744 | else | |
3745 | prevbno = NULLFSBLOCK; | |
3746 | /* | |
3747 | * If there's a following (right) block, select a requested | |
3748 | * start block based on it. | |
3749 | */ | |
3750 | if (!isnullstartblock(ap->got.br_startblock)) { | |
3751 | /* | |
3752 | * Calculate gap to start of next block. | |
3753 | */ | |
3754 | adjust = gotdiff = ap->got.br_startoff - ap->offset; | |
3755 | /* | |
3756 | * Figure the startblock based on the next block's | |
3757 | * start and the gap size. | |
3758 | */ | |
3759 | gotbno = ap->got.br_startblock; | |
3760 | /* | |
3761 | * Heuristic! | |
3762 | * If the gap is large relative to the piece we're | |
3763 | * allocating, or using it gives us an invalid block | |
3764 | * number, then just use the start of the next block | |
3765 | * offset by our length. | |
3766 | */ | |
3767 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && | |
3768 | ISVALID(gotbno - gotdiff, gotbno)) | |
3769 | gotbno -= adjust; | |
3770 | else if (ISVALID(gotbno - ap->length, gotbno)) { | |
3771 | gotbno -= ap->length; | |
3772 | gotdiff += adjust - ap->length; | |
3773 | } else | |
3774 | gotdiff += adjust; | |
3775 | /* | |
3776 | * If the firstblock forbids it, can't use it, | |
3777 | * must use default. | |
3778 | */ | |
3779 | if (!rt && !nullfb && | |
3780 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) | |
3781 | gotbno = NULLFSBLOCK; | |
3782 | } | |
3783 | /* | |
3784 | * No next block, just default. | |
3785 | */ | |
3786 | else | |
3787 | gotbno = NULLFSBLOCK; | |
3788 | /* | |
3789 | * If both valid, pick the better one, else the only good | |
3790 | * one, else ap->blkno is already set (to 0 or the inode block). | |
3791 | */ | |
3792 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) | |
3793 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; | |
3794 | else if (prevbno != NULLFSBLOCK) | |
3795 | ap->blkno = prevbno; | |
3796 | else if (gotbno != NULLFSBLOCK) | |
3797 | ap->blkno = gotbno; | |
1da177e4 | 3798 | } |
9e5987a7 | 3799 | #undef ISVALID |
1da177e4 | 3800 | } |
1da177e4 | 3801 | |
9e5987a7 DC |
3802 | STATIC int |
3803 | xfs_bmap_rtalloc( | |
3804 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ | |
aef9a895 | 3805 | { |
9e5987a7 DC |
3806 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
3807 | int error; /* error return value */ | |
3808 | xfs_mount_t *mp; /* mount point structure */ | |
3809 | xfs_extlen_t prod = 0; /* product factor for allocators */ | |
3810 | xfs_extlen_t ralen = 0; /* realtime allocation length */ | |
3811 | xfs_extlen_t align; /* minimum allocation alignment */ | |
3812 | xfs_rtblock_t rtb; | |
3813 | ||
3814 | mp = ap->ip->i_mount; | |
3815 | align = xfs_get_extsz_hint(ap->ip); | |
3816 | prod = align / mp->m_sb.sb_rextsize; | |
3817 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, | |
3818 | align, 1, ap->eof, 0, | |
3819 | ap->conv, &ap->offset, &ap->length); | |
3820 | if (error) | |
3821 | return error; | |
3822 | ASSERT(ap->length); | |
3823 | ASSERT(ap->length % mp->m_sb.sb_rextsize == 0); | |
aef9a895 | 3824 | |
aef9a895 | 3825 | /* |
9e5987a7 DC |
3826 | * If the offset & length are not perfectly aligned |
3827 | * then kill prod, it will just get us in trouble. | |
aef9a895 | 3828 | */ |
9e5987a7 DC |
3829 | if (do_mod(ap->offset, align) || ap->length % align) |
3830 | prod = 1; | |
3831 | /* | |
3832 | * Set ralen to be the actual requested length in rtextents. | |
3833 | */ | |
3834 | ralen = ap->length / mp->m_sb.sb_rextsize; | |
3835 | /* | |
3836 | * If the old value was close enough to MAXEXTLEN that | |
3837 | * we rounded up to it, cut it back so it's valid again. | |
3838 | * Note that if it's a really large request (bigger than | |
3839 | * MAXEXTLEN), we don't hear about that number, and can't | |
3840 | * adjust the starting point to match it. | |
3841 | */ | |
3842 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) | |
3843 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; | |
5c8ed202 | 3844 | |
9e5987a7 DC |
3845 | /* |
3846 | * Lock out other modifications to the RT bitmap inode. | |
3847 | */ | |
3848 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); | |
3849 | xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); | |
5c8ed202 | 3850 | |
9e5987a7 DC |
3851 | /* |
3852 | * If it's an allocation to an empty file at offset 0, | |
3853 | * pick an extent that will space things out in the rt area. | |
3854 | */ | |
3855 | if (ap->eof && ap->offset == 0) { | |
3856 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ | |
5c8ed202 | 3857 | |
9e5987a7 | 3858 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
5c8ed202 DC |
3859 | if (error) |
3860 | return error; | |
9e5987a7 DC |
3861 | ap->blkno = rtx * mp->m_sb.sb_rextsize; |
3862 | } else { | |
3863 | ap->blkno = 0; | |
5c8ed202 DC |
3864 | } |
3865 | ||
9e5987a7 | 3866 | xfs_bmap_adjacent(ap); |
5c8ed202 | 3867 | |
9e5987a7 DC |
3868 | /* |
3869 | * Realtime allocation, done through xfs_rtallocate_extent. | |
3870 | */ | |
3871 | atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; | |
3872 | do_div(ap->blkno, mp->m_sb.sb_rextsize); | |
3873 | rtb = ap->blkno; | |
3874 | ap->length = ralen; | |
3875 | if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length, | |
3876 | &ralen, atype, ap->wasdel, prod, &rtb))) | |
3877 | return error; | |
3878 | if (rtb == NULLFSBLOCK && prod > 1 && | |
3879 | (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, | |
3880 | ap->length, &ralen, atype, | |
3881 | ap->wasdel, 1, &rtb))) | |
3882 | return error; | |
3883 | ap->blkno = rtb; | |
3884 | if (ap->blkno != NULLFSBLOCK) { | |
3885 | ap->blkno *= mp->m_sb.sb_rextsize; | |
3886 | ralen *= mp->m_sb.sb_rextsize; | |
3887 | ap->length = ralen; | |
3888 | ap->ip->i_d.di_nblocks += ralen; | |
3889 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); | |
3890 | if (ap->wasdel) | |
3891 | ap->ip->i_delayed_blks -= ralen; | |
3892 | /* | |
3893 | * Adjust the disk quota also. This was reserved | |
3894 | * earlier. | |
3895 | */ | |
3896 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, | |
3897 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : | |
3898 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); | |
3899 | } else { | |
3900 | ap->length = 0; | |
5c8ed202 | 3901 | } |
5c8ed202 DC |
3902 | return 0; |
3903 | } | |
3904 | ||
b64dfe4e | 3905 | STATIC int |
9e5987a7 DC |
3906 | xfs_bmap_btalloc_nullfb( |
3907 | struct xfs_bmalloca *ap, | |
3908 | struct xfs_alloc_arg *args, | |
3909 | xfs_extlen_t *blen) | |
b64dfe4e | 3910 | { |
9e5987a7 DC |
3911 | struct xfs_mount *mp = ap->ip->i_mount; |
3912 | struct xfs_perag *pag; | |
3913 | xfs_agnumber_t ag, startag; | |
3914 | int notinit = 0; | |
b64dfe4e CH |
3915 | int error; |
3916 | ||
9e5987a7 DC |
3917 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
3918 | args->type = XFS_ALLOCTYPE_NEAR_BNO; | |
3919 | else | |
3920 | args->type = XFS_ALLOCTYPE_START_BNO; | |
3921 | args->total = ap->total; | |
b64dfe4e CH |
3922 | |
3923 | /* | |
9e5987a7 DC |
3924 | * Search for an allocation group with a single extent large enough |
3925 | * for the request. If one isn't found, then adjust the minimum | |
3926 | * allocation size to the largest space found. | |
b64dfe4e | 3927 | */ |
9e5987a7 DC |
3928 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
3929 | if (startag == NULLAGNUMBER) | |
3930 | startag = ag = 0; | |
b64dfe4e | 3931 | |
9e5987a7 DC |
3932 | pag = xfs_perag_get(mp, ag); |
3933 | while (*blen < args->maxlen) { | |
3934 | if (!pag->pagf_init) { | |
3935 | error = xfs_alloc_pagf_init(mp, args->tp, ag, | |
3936 | XFS_ALLOC_FLAG_TRYLOCK); | |
3937 | if (error) { | |
3938 | xfs_perag_put(pag); | |
3939 | return error; | |
3940 | } | |
3941 | } | |
b64dfe4e | 3942 | |
9e5987a7 DC |
3943 | /* |
3944 | * See xfs_alloc_fix_freelist... | |
3945 | */ | |
3946 | if (pag->pagf_init) { | |
3947 | xfs_extlen_t longest; | |
3948 | longest = xfs_alloc_longest_free_extent(mp, pag); | |
3949 | if (*blen < longest) | |
3950 | *blen = longest; | |
3951 | } else | |
3952 | notinit = 1; | |
b64dfe4e | 3953 | |
9e5987a7 DC |
3954 | if (xfs_inode_is_filestream(ap->ip)) { |
3955 | if (*blen >= args->maxlen) | |
3956 | break; | |
b64dfe4e | 3957 | |
9e5987a7 DC |
3958 | if (ap->userdata) { |
3959 | /* | |
3960 | * If startag is an invalid AG, we've | |
3961 | * come here once before and | |
3962 | * xfs_filestream_new_ag picked the | |
3963 | * best currently available. | |
3964 | * | |
3965 | * Don't continue looping, since we | |
3966 | * could loop forever. | |
3967 | */ | |
3968 | if (startag == NULLAGNUMBER) | |
3969 | break; | |
b64dfe4e | 3970 | |
9e5987a7 DC |
3971 | error = xfs_filestream_new_ag(ap, &ag); |
3972 | xfs_perag_put(pag); | |
3973 | if (error) | |
3974 | return error; | |
b64dfe4e | 3975 | |
9e5987a7 DC |
3976 | /* loop again to set 'blen'*/ |
3977 | startag = NULLAGNUMBER; | |
3978 | pag = xfs_perag_get(mp, ag); | |
3979 | continue; | |
3980 | } | |
3981 | } | |
3982 | if (++ag == mp->m_sb.sb_agcount) | |
3983 | ag = 0; | |
3984 | if (ag == startag) | |
3985 | break; | |
3986 | xfs_perag_put(pag); | |
3987 | pag = xfs_perag_get(mp, ag); | |
3988 | } | |
3989 | xfs_perag_put(pag); | |
b64dfe4e | 3990 | |
9e5987a7 DC |
3991 | /* |
3992 | * Since the above loop did a BUF_TRYLOCK, it is | |
3993 | * possible that there is space for this request. | |
3994 | */ | |
3995 | if (notinit || *blen < ap->minlen) | |
3996 | args->minlen = ap->minlen; | |
3997 | /* | |
3998 | * If the best seen length is less than the request | |
3999 | * length, use the best as the minimum. | |
4000 | */ | |
4001 | else if (*blen < args->maxlen) | |
4002 | args->minlen = *blen; | |
4003 | /* | |
4004 | * Otherwise we've seen an extent as big as maxlen, | |
4005 | * use that as the minimum. | |
4006 | */ | |
4007 | else | |
4008 | args->minlen = args->maxlen; | |
b64dfe4e CH |
4009 | |
4010 | /* | |
9e5987a7 DC |
4011 | * set the failure fallback case to look in the selected |
4012 | * AG as the stream may have moved. | |
b64dfe4e | 4013 | */ |
9e5987a7 DC |
4014 | if (xfs_inode_is_filestream(ap->ip)) |
4015 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); | |
b64dfe4e | 4016 | |
b64dfe4e | 4017 | return 0; |
b64dfe4e CH |
4018 | } |
4019 | ||
9e5987a7 DC |
4020 | STATIC int |
4021 | xfs_bmap_btalloc( | |
4022 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ | |
4403280a | 4023 | { |
9e5987a7 DC |
4024 | xfs_mount_t *mp; /* mount point structure */ |
4025 | xfs_alloctype_t atype = 0; /* type for allocation routines */ | |
4026 | xfs_extlen_t align; /* minimum allocation alignment */ | |
4027 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ | |
4028 | xfs_agnumber_t ag; | |
4029 | xfs_alloc_arg_t args; | |
4030 | xfs_extlen_t blen; | |
4031 | xfs_extlen_t nextminlen = 0; | |
4032 | int nullfb; /* true if ap->firstblock isn't set */ | |
4033 | int isaligned; | |
4034 | int tryagain; | |
4035 | int error; | |
4403280a | 4036 | |
9e5987a7 | 4037 | ASSERT(ap->length); |
4403280a | 4038 | |
9e5987a7 DC |
4039 | mp = ap->ip->i_mount; |
4040 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; | |
4041 | if (unlikely(align)) { | |
4042 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, | |
4043 | align, 0, ap->eof, 0, ap->conv, | |
4044 | &ap->offset, &ap->length); | |
4045 | ASSERT(!error); | |
4046 | ASSERT(ap->length); | |
4403280a | 4047 | } |
9e5987a7 DC |
4048 | nullfb = *ap->firstblock == NULLFSBLOCK; |
4049 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); | |
4050 | if (nullfb) { | |
4051 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { | |
4052 | ag = xfs_filestream_lookup_ag(ap->ip); | |
4053 | ag = (ag != NULLAGNUMBER) ? ag : 0; | |
4054 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); | |
4055 | } else { | |
4056 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); | |
4057 | } | |
4058 | } else | |
4059 | ap->blkno = *ap->firstblock; | |
4403280a | 4060 | |
9e5987a7 | 4061 | xfs_bmap_adjacent(ap); |
4403280a | 4062 | |
9e5987a7 DC |
4063 | /* |
4064 | * If allowed, use ap->blkno; otherwise must use firstblock since | |
4065 | * it's in the right allocation group. | |
4066 | */ | |
4067 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) | |
4068 | ; | |
4069 | else | |
4070 | ap->blkno = *ap->firstblock; | |
4071 | /* | |
4072 | * Normal allocation, done through xfs_alloc_vextent. | |
4073 | */ | |
4074 | tryagain = isaligned = 0; | |
4075 | memset(&args, 0, sizeof(args)); | |
4076 | args.tp = ap->tp; | |
4077 | args.mp = mp; | |
4078 | args.fsbno = ap->blkno; | |
4403280a | 4079 | |
9e5987a7 DC |
4080 | /* Trim the allocation back to the maximum an AG can fit. */ |
4081 | args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp)); | |
4082 | args.firstblock = *ap->firstblock; | |
4083 | blen = 0; | |
4084 | if (nullfb) { | |
4085 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); | |
4403280a CH |
4086 | if (error) |
4087 | return error; | |
9e5987a7 DC |
4088 | } else if (ap->flist->xbf_low) { |
4089 | if (xfs_inode_is_filestream(ap->ip)) | |
4090 | args.type = XFS_ALLOCTYPE_FIRST_AG; | |
4091 | else | |
4092 | args.type = XFS_ALLOCTYPE_START_BNO; | |
4093 | args.total = args.minlen = ap->minlen; | |
4094 | } else { | |
4095 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
4096 | args.total = ap->total; | |
4097 | args.minlen = ap->minlen; | |
4403280a | 4098 | } |
9e5987a7 DC |
4099 | /* apply extent size hints if obtained earlier */ |
4100 | if (unlikely(align)) { | |
4101 | args.prod = align; | |
4102 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) | |
4103 | args.mod = (xfs_extlen_t)(args.prod - args.mod); | |
4104 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { | |
4105 | args.prod = 1; | |
4106 | args.mod = 0; | |
4107 | } else { | |
4108 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; | |
4109 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) | |
4110 | args.mod = (xfs_extlen_t)(args.prod - args.mod); | |
4403280a | 4111 | } |
7e47a4ef | 4112 | /* |
9e5987a7 DC |
4113 | * If we are not low on available data blocks, and the |
4114 | * underlying logical volume manager is a stripe, and | |
4115 | * the file offset is zero then try to allocate data | |
4116 | * blocks on stripe unit boundary. | |
4117 | * NOTE: ap->aeof is only set if the allocation length | |
4118 | * is >= the stripe unit and the allocation offset is | |
4119 | * at the end of file. | |
7e47a4ef | 4120 | */ |
9e5987a7 DC |
4121 | if (!ap->flist->xbf_low && ap->aeof) { |
4122 | if (!ap->offset) { | |
4123 | args.alignment = mp->m_dalign; | |
4124 | atype = args.type; | |
4125 | isaligned = 1; | |
4126 | /* | |
4127 | * Adjust for alignment | |
4128 | */ | |
4129 | if (blen > args.alignment && blen <= args.maxlen) | |
4130 | args.minlen = blen - args.alignment; | |
4131 | args.minalignslop = 0; | |
4132 | } else { | |
4133 | /* | |
4134 | * First try an exact bno allocation. | |
4135 | * If it fails then do a near or start bno | |
4136 | * allocation with alignment turned on. | |
4137 | */ | |
4138 | atype = args.type; | |
4139 | tryagain = 1; | |
4140 | args.type = XFS_ALLOCTYPE_THIS_BNO; | |
4141 | args.alignment = 1; | |
4142 | /* | |
4143 | * Compute the minlen+alignment for the | |
4144 | * next case. Set slop so that the value | |
4145 | * of minlen+alignment+slop doesn't go up | |
4146 | * between the calls. | |
4147 | */ | |
4148 | if (blen > mp->m_dalign && blen <= args.maxlen) | |
4149 | nextminlen = blen - mp->m_dalign; | |
4150 | else | |
4151 | nextminlen = args.minlen; | |
4152 | if (nextminlen + mp->m_dalign > args.minlen + 1) | |
4153 | args.minalignslop = | |
4154 | nextminlen + mp->m_dalign - | |
4155 | args.minlen - 1; | |
4156 | else | |
4157 | args.minalignslop = 0; | |
7e47a4ef DC |
4158 | } |
4159 | } else { | |
9e5987a7 DC |
4160 | args.alignment = 1; |
4161 | args.minalignslop = 0; | |
7e47a4ef | 4162 | } |
9e5987a7 DC |
4163 | args.minleft = ap->minleft; |
4164 | args.wasdel = ap->wasdel; | |
4165 | args.isfl = 0; | |
4166 | args.userdata = ap->userdata; | |
4167 | if ((error = xfs_alloc_vextent(&args))) | |
4168 | return error; | |
4169 | if (tryagain && args.fsbno == NULLFSBLOCK) { | |
4170 | /* | |
4171 | * Exact allocation failed. Now try with alignment | |
4172 | * turned on. | |
4173 | */ | |
4174 | args.type = atype; | |
4175 | args.fsbno = ap->blkno; | |
4176 | args.alignment = mp->m_dalign; | |
4177 | args.minlen = nextminlen; | |
4178 | args.minalignslop = 0; | |
4179 | isaligned = 1; | |
4180 | if ((error = xfs_alloc_vextent(&args))) | |
4181 | return error; | |
7e47a4ef | 4182 | } |
9e5987a7 DC |
4183 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
4184 | /* | |
4185 | * allocation failed, so turn off alignment and | |
4186 | * try again. | |
4187 | */ | |
4188 | args.type = atype; | |
4189 | args.fsbno = ap->blkno; | |
4190 | args.alignment = 0; | |
4191 | if ((error = xfs_alloc_vextent(&args))) | |
7e47a4ef DC |
4192 | return error; |
4193 | } | |
9e5987a7 DC |
4194 | if (args.fsbno == NULLFSBLOCK && nullfb && |
4195 | args.minlen > ap->minlen) { | |
4196 | args.minlen = ap->minlen; | |
4197 | args.type = XFS_ALLOCTYPE_START_BNO; | |
4198 | args.fsbno = ap->blkno; | |
4199 | if ((error = xfs_alloc_vextent(&args))) | |
4200 | return error; | |
7e47a4ef | 4201 | } |
9e5987a7 DC |
4202 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
4203 | args.fsbno = 0; | |
4204 | args.type = XFS_ALLOCTYPE_FIRST_AG; | |
4205 | args.total = ap->minlen; | |
4206 | args.minleft = 0; | |
4207 | if ((error = xfs_alloc_vextent(&args))) | |
4208 | return error; | |
4209 | ap->flist->xbf_low = 1; | |
4210 | } | |
4211 | if (args.fsbno != NULLFSBLOCK) { | |
4212 | /* | |
4213 | * check the allocation happened at the same or higher AG than | |
4214 | * the first block that was allocated. | |
4215 | */ | |
4216 | ASSERT(*ap->firstblock == NULLFSBLOCK || | |
4217 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == | |
4218 | XFS_FSB_TO_AGNO(mp, args.fsbno) || | |
4219 | (ap->flist->xbf_low && | |
4220 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < | |
4221 | XFS_FSB_TO_AGNO(mp, args.fsbno))); | |
7e47a4ef | 4222 | |
9e5987a7 DC |
4223 | ap->blkno = args.fsbno; |
4224 | if (*ap->firstblock == NULLFSBLOCK) | |
4225 | *ap->firstblock = args.fsbno; | |
4226 | ASSERT(nullfb || fb_agno == args.agno || | |
4227 | (ap->flist->xbf_low && fb_agno < args.agno)); | |
4228 | ap->length = args.len; | |
4229 | ap->ip->i_d.di_nblocks += args.len; | |
4230 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); | |
4231 | if (ap->wasdel) | |
4232 | ap->ip->i_delayed_blks -= args.len; | |
4233 | /* | |
4234 | * Adjust the disk quota also. This was reserved | |
4235 | * earlier. | |
4236 | */ | |
4237 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, | |
4238 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : | |
4239 | XFS_TRANS_DQ_BCOUNT, | |
4240 | (long) args.len); | |
4241 | } else { | |
4242 | ap->blkno = NULLFSBLOCK; | |
4243 | ap->length = 0; | |
4244 | } | |
4245 | return 0; | |
4246 | } | |
7e47a4ef | 4247 | |
9e5987a7 DC |
4248 | /* |
4249 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. | |
4250 | * It figures out where to ask the underlying allocator to put the new extent. | |
4251 | */ | |
4252 | STATIC int | |
4253 | xfs_bmap_alloc( | |
4254 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ | |
4255 | { | |
4256 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) | |
4257 | return xfs_bmap_rtalloc(ap); | |
4258 | return xfs_bmap_btalloc(ap); | |
4259 | } | |
a5bd606b | 4260 | |
9e5987a7 DC |
4261 | /* |
4262 | * Trim the returned map to the required bounds | |
4263 | */ | |
4264 | STATIC void | |
4265 | xfs_bmapi_trim_map( | |
4266 | struct xfs_bmbt_irec *mval, | |
4267 | struct xfs_bmbt_irec *got, | |
4268 | xfs_fileoff_t *bno, | |
4269 | xfs_filblks_t len, | |
4270 | xfs_fileoff_t obno, | |
4271 | xfs_fileoff_t end, | |
4272 | int n, | |
4273 | int flags) | |
4274 | { | |
4275 | if ((flags & XFS_BMAPI_ENTIRE) || | |
4276 | got->br_startoff + got->br_blockcount <= obno) { | |
4277 | *mval = *got; | |
4278 | if (isnullstartblock(got->br_startblock)) | |
4279 | mval->br_startblock = DELAYSTARTBLOCK; | |
4280 | return; | |
4281 | } | |
7e47a4ef | 4282 | |
9e5987a7 DC |
4283 | if (obno > *bno) |
4284 | *bno = obno; | |
4285 | ASSERT((*bno >= obno) || (n == 0)); | |
4286 | ASSERT(*bno < end); | |
4287 | mval->br_startoff = *bno; | |
4288 | if (isnullstartblock(got->br_startblock)) | |
4289 | mval->br_startblock = DELAYSTARTBLOCK; | |
4290 | else | |
4291 | mval->br_startblock = got->br_startblock + | |
4292 | (*bno - got->br_startoff); | |
7e47a4ef | 4293 | /* |
9e5987a7 DC |
4294 | * Return the minimum of what we got and what we asked for for |
4295 | * the length. We can use the len variable here because it is | |
4296 | * modified below and we could have been there before coming | |
4297 | * here if the first part of the allocation didn't overlap what | |
4298 | * was asked for. | |
7e47a4ef | 4299 | */ |
9e5987a7 DC |
4300 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
4301 | got->br_blockcount - (*bno - got->br_startoff)); | |
4302 | mval->br_state = got->br_state; | |
4303 | ASSERT(mval->br_blockcount <= len); | |
4304 | return; | |
7e47a4ef DC |
4305 | } |
4306 | ||
9e5987a7 DC |
4307 | /* |
4308 | * Update and validate the extent map to return | |
4309 | */ | |
4310 | STATIC void | |
4311 | xfs_bmapi_update_map( | |
4312 | struct xfs_bmbt_irec **map, | |
4313 | xfs_fileoff_t *bno, | |
4314 | xfs_filblks_t *len, | |
4315 | xfs_fileoff_t obno, | |
4316 | xfs_fileoff_t end, | |
4317 | int *n, | |
4318 | int flags) | |
e04426b9 | 4319 | { |
9e5987a7 | 4320 | xfs_bmbt_irec_t *mval = *map; |
e04426b9 | 4321 | |
9e5987a7 DC |
4322 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
4323 | ((mval->br_startoff + mval->br_blockcount) <= end)); | |
4324 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || | |
4325 | (mval->br_startoff < obno)); | |
e04426b9 | 4326 | |
9e5987a7 DC |
4327 | *bno = mval->br_startoff + mval->br_blockcount; |
4328 | *len = end - *bno; | |
4329 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { | |
4330 | /* update previous map with new information */ | |
4331 | ASSERT(mval->br_startblock == mval[-1].br_startblock); | |
4332 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); | |
4333 | ASSERT(mval->br_state == mval[-1].br_state); | |
4334 | mval[-1].br_blockcount = mval->br_blockcount; | |
4335 | mval[-1].br_state = mval->br_state; | |
4336 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && | |
4337 | mval[-1].br_startblock != DELAYSTARTBLOCK && | |
4338 | mval[-1].br_startblock != HOLESTARTBLOCK && | |
4339 | mval->br_startblock == mval[-1].br_startblock + | |
4340 | mval[-1].br_blockcount && | |
4341 | ((flags & XFS_BMAPI_IGSTATE) || | |
4342 | mval[-1].br_state == mval->br_state)) { | |
4343 | ASSERT(mval->br_startoff == | |
4344 | mval[-1].br_startoff + mval[-1].br_blockcount); | |
4345 | mval[-1].br_blockcount += mval->br_blockcount; | |
4346 | } else if (*n > 0 && | |
4347 | mval->br_startblock == DELAYSTARTBLOCK && | |
4348 | mval[-1].br_startblock == DELAYSTARTBLOCK && | |
4349 | mval->br_startoff == | |
4350 | mval[-1].br_startoff + mval[-1].br_blockcount) { | |
4351 | mval[-1].br_blockcount += mval->br_blockcount; | |
4352 | mval[-1].br_state = mval->br_state; | |
4353 | } else if (!((*n == 0) && | |
4354 | ((mval->br_startoff + mval->br_blockcount) <= | |
4355 | obno))) { | |
4356 | mval++; | |
4357 | (*n)++; | |
4358 | } | |
4359 | *map = mval; | |
e04426b9 DC |
4360 | } |
4361 | ||
4362 | /* | |
9e5987a7 | 4363 | * Map file blocks to filesystem blocks without allocation. |
e04426b9 DC |
4364 | */ |
4365 | int | |
9e5987a7 DC |
4366 | xfs_bmapi_read( |
4367 | struct xfs_inode *ip, | |
4368 | xfs_fileoff_t bno, | |
b447fe5a | 4369 | xfs_filblks_t len, |
9e5987a7 DC |
4370 | struct xfs_bmbt_irec *mval, |
4371 | int *nmap, | |
c315c90b | 4372 | int flags) |
b447fe5a | 4373 | { |
9e5987a7 DC |
4374 | struct xfs_mount *mp = ip->i_mount; |
4375 | struct xfs_ifork *ifp; | |
4376 | struct xfs_bmbt_irec got; | |
4377 | struct xfs_bmbt_irec prev; | |
4378 | xfs_fileoff_t obno; | |
4379 | xfs_fileoff_t end; | |
4380 | xfs_extnum_t lastx; | |
4381 | int error; | |
4382 | int eof; | |
4383 | int n = 0; | |
b447fe5a DC |
4384 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
4385 | XFS_ATTR_FORK : XFS_DATA_FORK; | |
b447fe5a | 4386 | |
9e5987a7 DC |
4387 | ASSERT(*nmap >= 1); |
4388 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| | |
4389 | XFS_BMAPI_IGSTATE))); | |
b447fe5a | 4390 | |
9e5987a7 DC |
4391 | if (unlikely(XFS_TEST_ERROR( |
4392 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
4393 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), | |
4394 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { | |
4395 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); | |
4396 | return XFS_ERROR(EFSCORRUPTED); | |
4397 | } | |
b447fe5a | 4398 | |
9e5987a7 DC |
4399 | if (XFS_FORCED_SHUTDOWN(mp)) |
4400 | return XFS_ERROR(EIO); | |
4401 | ||
4402 | XFS_STATS_INC(xs_blk_mapr); | |
4403 | ||
4404 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
4405 | ||
4406 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { | |
4407 | error = xfs_iread_extents(NULL, ip, whichfork); | |
4408 | if (error) | |
4409 | return error; | |
b447fe5a | 4410 | } |
b447fe5a | 4411 | |
9e5987a7 DC |
4412 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); |
4413 | end = bno + len; | |
4414 | obno = bno; | |
b447fe5a | 4415 | |
9e5987a7 DC |
4416 | while (bno < end && n < *nmap) { |
4417 | /* Reading past eof, act as though there's a hole up to end. */ | |
4418 | if (eof) | |
4419 | got.br_startoff = end; | |
4420 | if (got.br_startoff > bno) { | |
4421 | /* Reading in a hole. */ | |
4422 | mval->br_startoff = bno; | |
4423 | mval->br_startblock = HOLESTARTBLOCK; | |
4424 | mval->br_blockcount = | |
4425 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); | |
4426 | mval->br_state = XFS_EXT_NORM; | |
4427 | bno += mval->br_blockcount; | |
4428 | len -= mval->br_blockcount; | |
4429 | mval++; | |
4430 | n++; | |
4431 | continue; | |
4432 | } | |
b447fe5a | 4433 | |
9e5987a7 DC |
4434 | /* set up the extent map to return. */ |
4435 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); | |
4436 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); | |
4437 | ||
4438 | /* If we're done, stop now. */ | |
4439 | if (bno >= end || n >= *nmap) | |
4440 | break; | |
4441 | ||
4442 | /* Else go on to the next record. */ | |
4443 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) | |
4444 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); | |
4445 | else | |
4446 | eof = 1; | |
4447 | } | |
4448 | *nmap = n; | |
b447fe5a DC |
4449 | return 0; |
4450 | } | |
4451 | ||
9e5987a7 DC |
4452 | STATIC int |
4453 | xfs_bmapi_reserve_delalloc( | |
4454 | struct xfs_inode *ip, | |
4455 | xfs_fileoff_t aoff, | |
4456 | xfs_filblks_t len, | |
4457 | struct xfs_bmbt_irec *got, | |
4458 | struct xfs_bmbt_irec *prev, | |
4459 | xfs_extnum_t *lastx, | |
4460 | int eof) | |
1da177e4 | 4461 | { |
c0dc7828 | 4462 | struct xfs_mount *mp = ip->i_mount; |
9e5987a7 DC |
4463 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
4464 | xfs_extlen_t alen; | |
4465 | xfs_extlen_t indlen; | |
4466 | char rt = XFS_IS_REALTIME_INODE(ip); | |
4467 | xfs_extlen_t extsz; | |
4468 | int error; | |
c0dc7828 | 4469 | |
9e5987a7 DC |
4470 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); |
4471 | if (!eof) | |
4472 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); | |
1da177e4 | 4473 | |
9e5987a7 DC |
4474 | /* Figure out the extent size, adjust alen */ |
4475 | extsz = xfs_get_extsz_hint(ip); | |
4476 | if (extsz) { | |
4477 | /* | |
4478 | * Make sure we don't exceed a single extent length when we | |
4479 | * align the extent by reducing length we are going to | |
4480 | * allocate by the maximum amount extent size aligment may | |
4481 | * require. | |
4482 | */ | |
4483 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1)); | |
4484 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, | |
4485 | 1, 0, &aoff, &alen); | |
4486 | ASSERT(!error); | |
4487 | } | |
4488 | ||
4489 | if (rt) | |
4490 | extsz = alen / mp->m_sb.sb_rextsize; | |
4491 | ||
4492 | /* | |
4493 | * Make a transaction-less quota reservation for delayed allocation | |
4494 | * blocks. This number gets adjusted later. We return if we haven't | |
4495 | * allocated blocks already inside this loop. | |
4496 | */ | |
4497 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, | |
4498 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); | |
4499 | if (error) | |
4500 | return error; | |
4501 | ||
4502 | /* | |
4503 | * Split changing sb for alen and indlen since they could be coming | |
4504 | * from different places. | |
4505 | */ | |
4506 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); | |
4507 | ASSERT(indlen > 0); | |
4508 | ||
4509 | if (rt) { | |
4510 | error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, | |
4511 | -((int64_t)extsz), 0); | |
4512 | } else { | |
4513 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, | |
4514 | -((int64_t)alen), 0); | |
4515 | } | |
4516 | ||
4517 | if (error) | |
4518 | goto out_unreserve_quota; | |
4519 | ||
4520 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, | |
4521 | -((int64_t)indlen), 0); | |
4522 | if (error) | |
4523 | goto out_unreserve_blocks; | |
4524 | ||
4525 | ||
4526 | ip->i_delayed_blks += alen; | |
4527 | ||
4528 | got->br_startoff = aoff; | |
4529 | got->br_startblock = nullstartblock(indlen); | |
4530 | got->br_blockcount = alen; | |
4531 | got->br_state = XFS_EXT_NORM; | |
4532 | xfs_bmap_add_extent_hole_delay(ip, lastx, got); | |
4533 | ||
4534 | /* | |
4535 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay | |
4536 | * might have merged it into one of the neighbouring ones. | |
4537 | */ | |
4538 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); | |
4539 | ||
4540 | ASSERT(got->br_startoff <= aoff); | |
4541 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); | |
4542 | ASSERT(isnullstartblock(got->br_startblock)); | |
4543 | ASSERT(got->br_state == XFS_EXT_NORM); | |
4544 | return 0; | |
4545 | ||
4546 | out_unreserve_blocks: | |
4547 | if (rt) | |
4548 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0); | |
4549 | else | |
4550 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0); | |
4551 | out_unreserve_quota: | |
4552 | if (XFS_IS_QUOTA_ON(mp)) | |
4553 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? | |
4554 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); | |
4555 | return error; | |
4556 | } | |
4557 | ||
4558 | /* | |
4559 | * Map file blocks to filesystem blocks, adding delayed allocations as needed. | |
4560 | */ | |
4561 | int | |
4562 | xfs_bmapi_delay( | |
4563 | struct xfs_inode *ip, /* incore inode */ | |
4564 | xfs_fileoff_t bno, /* starting file offs. mapped */ | |
4565 | xfs_filblks_t len, /* length to map in file */ | |
4566 | struct xfs_bmbt_irec *mval, /* output: map values */ | |
4567 | int *nmap, /* i/o: mval size/count */ | |
4568 | int flags) /* XFS_BMAPI_... */ | |
4569 | { | |
4570 | struct xfs_mount *mp = ip->i_mount; | |
4571 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | |
4572 | struct xfs_bmbt_irec got; /* current file extent record */ | |
4573 | struct xfs_bmbt_irec prev; /* previous file extent record */ | |
4574 | xfs_fileoff_t obno; /* old block number (offset) */ | |
4575 | xfs_fileoff_t end; /* end of mapped file region */ | |
4576 | xfs_extnum_t lastx; /* last useful extent number */ | |
4577 | int eof; /* we've hit the end of extents */ | |
4578 | int n = 0; /* current extent index */ | |
4579 | int error = 0; | |
c0dc7828 | 4580 | |
1da177e4 | 4581 | ASSERT(*nmap >= 1); |
c0dc7828 | 4582 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
9e5987a7 | 4583 | ASSERT(!(flags & ~XFS_BMAPI_ENTIRE)); |
c0dc7828 | 4584 | |
1da177e4 | 4585 | if (unlikely(XFS_TEST_ERROR( |
9e5987a7 DC |
4586 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
4587 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), | |
1da177e4 | 4588 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
9e5987a7 | 4589 | XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp); |
1da177e4 LT |
4590 | return XFS_ERROR(EFSCORRUPTED); |
4591 | } | |
c0dc7828 | 4592 | |
1da177e4 LT |
4593 | if (XFS_FORCED_SHUTDOWN(mp)) |
4594 | return XFS_ERROR(EIO); | |
c0dc7828 | 4595 | |
c0dc7828 DC |
4596 | XFS_STATS_INC(xs_blk_mapw); |
4597 | ||
c0dc7828 | 4598 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
9e5987a7 | 4599 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
c0dc7828 | 4600 | if (error) |
9e5987a7 | 4601 | return error; |
c0dc7828 DC |
4602 | } |
4603 | ||
9e5987a7 | 4604 | xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev); |
1da177e4 LT |
4605 | end = bno + len; |
4606 | obno = bno; | |
7e47a4ef | 4607 | |
1da177e4 | 4608 | while (bno < end && n < *nmap) { |
9e5987a7 DC |
4609 | if (eof || got.br_startoff > bno) { |
4610 | error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got, | |
4611 | &prev, &lastx, eof); | |
4612 | if (error) { | |
4613 | if (n == 0) { | |
4614 | *nmap = 0; | |
4615 | return error; | |
4616 | } | |
4617 | break; | |
4618 | } | |
4619 | } | |
c0dc7828 | 4620 | |
9e5987a7 DC |
4621 | /* set up the extent map to return. */ |
4622 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); | |
aef9a895 DC |
4623 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
4624 | ||
9e5987a7 DC |
4625 | /* If we're done, stop now. */ |
4626 | if (bno >= end || n >= *nmap) | |
1da177e4 | 4627 | break; |
c0dc7828 DC |
4628 | |
4629 | /* Else go on to the next record. */ | |
9e5987a7 DC |
4630 | prev = got; |
4631 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) | |
4632 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); | |
4633 | else | |
5690f921 | 4634 | eof = 1; |
1da177e4 | 4635 | } |
9e5987a7 | 4636 | |
1da177e4 | 4637 | *nmap = n; |
9e5987a7 DC |
4638 | return 0; |
4639 | } | |
c0dc7828 | 4640 | |
c315c90b | 4641 | |
9e5987a7 DC |
4642 | STATIC int |
4643 | __xfs_bmapi_allocate( | |
4644 | struct xfs_bmalloca *bma) | |
4645 | { | |
4646 | struct xfs_mount *mp = bma->ip->i_mount; | |
4647 | int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ? | |
4648 | XFS_ATTR_FORK : XFS_DATA_FORK; | |
4649 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); | |
4650 | int tmp_logflags = 0; | |
4651 | int error; | |
4652 | int rt; | |
4653 | ||
4654 | ASSERT(bma->length > 0); | |
4655 | ||
4656 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip); | |
8096b1eb | 4657 | |
1da177e4 | 4658 | /* |
9e5987a7 DC |
4659 | * For the wasdelay case, we could also just allocate the stuff asked |
4660 | * for in this bmap call but that wouldn't be as good. | |
1da177e4 | 4661 | */ |
9e5987a7 DC |
4662 | if (bma->wasdel) { |
4663 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; | |
4664 | bma->offset = bma->got.br_startoff; | |
4665 | if (bma->idx != NULLEXTNUM && bma->idx) { | |
4666 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), | |
4667 | &bma->prev); | |
1da177e4 | 4668 | } |
9e5987a7 DC |
4669 | } else { |
4670 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); | |
4671 | if (!bma->eof) | |
4672 | bma->length = XFS_FILBLKS_MIN(bma->length, | |
4673 | bma->got.br_startoff - bma->offset); | |
1da177e4 | 4674 | } |
1da177e4 | 4675 | |
9e5987a7 DC |
4676 | /* |
4677 | * Indicate if this is the first user data in the file, or just any | |
4678 | * user data. | |
4679 | */ | |
4680 | if (!(bma->flags & XFS_BMAPI_METADATA)) { | |
4681 | bma->userdata = (bma->offset == 0) ? | |
4682 | XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA; | |
4683 | } | |
1da177e4 | 4684 | |
9e5987a7 | 4685 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
0b1b213f | 4686 | |
9e5987a7 DC |
4687 | /* |
4688 | * Only want to do the alignment at the eof if it is userdata and | |
4689 | * allocation length is larger than a stripe unit. | |
4690 | */ | |
4691 | if (mp->m_dalign && bma->length >= mp->m_dalign && | |
4692 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { | |
4693 | error = xfs_bmap_isaeof(bma, whichfork); | |
4694 | if (error) | |
4695 | return error; | |
1da177e4 | 4696 | } |
8096b1eb | 4697 | |
9e5987a7 DC |
4698 | error = xfs_bmap_alloc(bma); |
4699 | if (error) | |
1da177e4 | 4700 | return error; |
9e5987a7 DC |
4701 | |
4702 | if (bma->flist->xbf_low) | |
4703 | bma->minleft = 0; | |
4704 | if (bma->cur) | |
4705 | bma->cur->bc_private.b.firstblock = *bma->firstblock; | |
4706 | if (bma->blkno == NULLFSBLOCK) | |
1da177e4 | 4707 | return 0; |
9e5987a7 DC |
4708 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
4709 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); | |
4710 | bma->cur->bc_private.b.firstblock = *bma->firstblock; | |
4711 | bma->cur->bc_private.b.flist = bma->flist; | |
1da177e4 | 4712 | } |
9e5987a7 DC |
4713 | /* |
4714 | * Bump the number of extents we've allocated | |
4715 | * in this call. | |
4716 | */ | |
4717 | bma->nallocs++; | |
4718 | ||
4719 | if (bma->cur) | |
4720 | bma->cur->bc_private.b.flags = | |
4721 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; | |
4722 | ||
4723 | bma->got.br_startoff = bma->offset; | |
4724 | bma->got.br_startblock = bma->blkno; | |
4725 | bma->got.br_blockcount = bma->length; | |
4726 | bma->got.br_state = XFS_EXT_NORM; | |
b4e9181e | 4727 | |
1da177e4 | 4728 | /* |
9e5987a7 DC |
4729 | * A wasdelay extent has been initialized, so shouldn't be flagged |
4730 | * as unwritten. | |
1da177e4 | 4731 | */ |
9e5987a7 DC |
4732 | if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) && |
4733 | xfs_sb_version_hasextflgbit(&mp->m_sb)) | |
4734 | bma->got.br_state = XFS_EXT_UNWRITTEN; | |
4735 | ||
4736 | if (bma->wasdel) | |
4737 | error = xfs_bmap_add_extent_delay_real(bma); | |
4738 | else | |
4739 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); | |
4740 | ||
4741 | bma->logflags |= tmp_logflags; | |
4742 | if (error) | |
4743 | return error; | |
4744 | ||
4745 | /* | |
4746 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real | |
4747 | * or xfs_bmap_add_extent_hole_real might have merged it into one of | |
4748 | * the neighbouring ones. | |
4749 | */ | |
4750 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); | |
4751 | ||
4752 | ASSERT(bma->got.br_startoff <= bma->offset); | |
4753 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= | |
4754 | bma->offset + bma->length); | |
4755 | ASSERT(bma->got.br_state == XFS_EXT_NORM || | |
4756 | bma->got.br_state == XFS_EXT_UNWRITTEN); | |
4757 | return 0; | |
4758 | } | |
4759 | ||
4760 | static void | |
4761 | xfs_bmapi_allocate_worker( | |
4762 | struct work_struct *work) | |
4763 | { | |
4764 | struct xfs_bmalloca *args = container_of(work, | |
4765 | struct xfs_bmalloca, work); | |
4766 | unsigned long pflags; | |
4767 | ||
4768 | /* we are in a transaction context here */ | |
4769 | current_set_flags_nested(&pflags, PF_FSTRANS); | |
4770 | ||
4771 | args->result = __xfs_bmapi_allocate(args); | |
4772 | complete(args->done); | |
4773 | ||
4774 | current_restore_flags_nested(&pflags, PF_FSTRANS); | |
4775 | } | |
4776 | ||
4777 | /* | |
4778 | * Some allocation requests often come in with little stack to work on. Push | |
4779 | * them off to a worker thread so there is lots of stack to use. Otherwise just | |
4780 | * call directly to avoid the context switch overhead here. | |
4781 | */ | |
4782 | int | |
4783 | xfs_bmapi_allocate( | |
4784 | struct xfs_bmalloca *args) | |
4785 | { | |
4786 | DECLARE_COMPLETION_ONSTACK(done); | |
4787 | ||
4788 | if (!args->stack_switch) | |
4789 | return __xfs_bmapi_allocate(args); | |
4790 | ||
4791 | ||
4792 | args->done = &done; | |
4793 | INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker); | |
4794 | queue_work(xfs_alloc_wq, &args->work); | |
4795 | wait_for_completion(&done); | |
4796 | return args->result; | |
4797 | } | |
4798 | ||
4799 | STATIC int | |
4800 | xfs_bmapi_convert_unwritten( | |
4801 | struct xfs_bmalloca *bma, | |
4802 | struct xfs_bmbt_irec *mval, | |
4803 | xfs_filblks_t len, | |
4804 | int flags) | |
4805 | { | |
4806 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? | |
4807 | XFS_ATTR_FORK : XFS_DATA_FORK; | |
4808 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); | |
4809 | int tmp_logflags = 0; | |
4810 | int error; | |
4811 | ||
4812 | /* check if we need to do unwritten->real conversion */ | |
4813 | if (mval->br_state == XFS_EXT_UNWRITTEN && | |
4814 | (flags & XFS_BMAPI_PREALLOC)) | |
4815 | return 0; | |
4816 | ||
4817 | /* check if we need to do real->unwritten conversion */ | |
4818 | if (mval->br_state == XFS_EXT_NORM && | |
4819 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != | |
4820 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) | |
4821 | return 0; | |
4822 | ||
4823 | /* | |
4824 | * Modify (by adding) the state flag, if writing. | |
4825 | */ | |
4826 | ASSERT(mval->br_blockcount <= len); | |
4827 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { | |
4828 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, | |
4829 | bma->ip, whichfork); | |
4830 | bma->cur->bc_private.b.firstblock = *bma->firstblock; | |
4831 | bma->cur->bc_private.b.flist = bma->flist; | |
1da177e4 | 4832 | } |
9e5987a7 DC |
4833 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
4834 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; | |
5575acc7 | 4835 | |
9e5987a7 DC |
4836 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx, |
4837 | &bma->cur, mval, bma->firstblock, bma->flist, | |
4838 | &tmp_logflags); | |
4839 | bma->logflags |= tmp_logflags; | |
4840 | if (error) | |
4841 | return error; | |
4842 | ||
4843 | /* | |
4844 | * Update our extent pointer, given that | |
4845 | * xfs_bmap_add_extent_unwritten_real might have merged it into one | |
4846 | * of the neighbouring ones. | |
4847 | */ | |
4848 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); | |
4849 | ||
4850 | /* | |
4851 | * We may have combined previously unwritten space with written space, | |
4852 | * so generate another request. | |
4853 | */ | |
4854 | if (mval->br_blockcount < len) | |
4855 | return EAGAIN; | |
4856 | return 0; | |
4857 | } | |
4858 | ||
4859 | /* | |
4860 | * Map file blocks to filesystem blocks, and allocate blocks or convert the | |
4861 | * extent state if necessary. Details behaviour is controlled by the flags | |
4862 | * parameter. Only allocates blocks from a single allocation group, to avoid | |
4863 | * locking problems. | |
4864 | * | |
4865 | * The returned value in "firstblock" from the first call in a transaction | |
4866 | * must be remembered and presented to subsequent calls in "firstblock". | |
4867 | * An upper bound for the number of blocks to be allocated is supplied to | |
4868 | * the first call in "total"; if no allocation group has that many free | |
4869 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). | |
4870 | */ | |
4871 | int | |
4872 | xfs_bmapi_write( | |
4873 | struct xfs_trans *tp, /* transaction pointer */ | |
4874 | struct xfs_inode *ip, /* incore inode */ | |
4875 | xfs_fileoff_t bno, /* starting file offs. mapped */ | |
4876 | xfs_filblks_t len, /* length to map in file */ | |
4877 | int flags, /* XFS_BMAPI_... */ | |
4878 | xfs_fsblock_t *firstblock, /* first allocated block | |
4879 | controls a.g. for allocs */ | |
4880 | xfs_extlen_t total, /* total blocks needed */ | |
4881 | struct xfs_bmbt_irec *mval, /* output: map values */ | |
4882 | int *nmap, /* i/o: mval size/count */ | |
4883 | struct xfs_bmap_free *flist) /* i/o: list extents to free */ | |
4884 | { | |
4885 | struct xfs_mount *mp = ip->i_mount; | |
4886 | struct xfs_ifork *ifp; | |
4887 | struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */ | |
4888 | xfs_fileoff_t end; /* end of mapped file region */ | |
4889 | int eof; /* after the end of extents */ | |
4890 | int error; /* error return */ | |
4891 | int n; /* current extent index */ | |
4892 | xfs_fileoff_t obno; /* old block number (offset) */ | |
4893 | int whichfork; /* data or attr fork */ | |
4894 | char inhole; /* current location is hole in file */ | |
4895 | char wasdelay; /* old extent was delayed */ | |
4896 | ||
4897 | #ifdef DEBUG | |
4898 | xfs_fileoff_t orig_bno; /* original block number value */ | |
4899 | int orig_flags; /* original flags arg value */ | |
4900 | xfs_filblks_t orig_len; /* original value of len arg */ | |
4901 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ | |
4902 | int orig_nmap; /* original value of *nmap */ | |
4903 | ||
4904 | orig_bno = bno; | |
4905 | orig_len = len; | |
4906 | orig_flags = flags; | |
4907 | orig_mval = mval; | |
4908 | orig_nmap = *nmap; | |
4909 | #endif | |
4910 | ||
4911 | ASSERT(*nmap >= 1); | |
4912 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); | |
4913 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); | |
4914 | ASSERT(tp != NULL); | |
4915 | ASSERT(len > 0); | |
4916 | ||
4917 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? | |
4918 | XFS_ATTR_FORK : XFS_DATA_FORK; | |
4919 | ||
4920 | if (unlikely(XFS_TEST_ERROR( | |
4921 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
4922 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && | |
4923 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), | |
4924 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { | |
4925 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); | |
4926 | return XFS_ERROR(EFSCORRUPTED); | |
5575acc7 KD |
4927 | } |
4928 | ||
9e5987a7 DC |
4929 | if (XFS_FORCED_SHUTDOWN(mp)) |
4930 | return XFS_ERROR(EIO); | |
4931 | ||
4932 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
4933 | ||
4934 | XFS_STATS_INC(xs_blk_mapw); | |
4935 | ||
4936 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | |
1da177e4 | 4937 | /* |
9e5987a7 DC |
4938 | * XXX (dgc): This assumes we are only called for inodes that |
4939 | * contain content neutral data in local format. Anything that | |
4940 | * contains caller-specific data in local format that needs | |
4941 | * transformation to move to a block format needs to do the | |
4942 | * conversion to extent format itself. | |
4943 | * | |
4944 | * Directory data forks and attribute forks handle this | |
4945 | * themselves, but with the addition of metadata verifiers every | |
4946 | * data fork in local format now contains caller specific data | |
4947 | * and as such conversion through this function is likely to be | |
4948 | * broken. | |
4949 | * | |
4950 | * The only likely user of this branch is for remote symlinks, | |
4951 | * but we cannot overwrite the data fork contents of the symlink | |
4952 | * (EEXIST occurs higher up the stack) and so it will never go | |
4953 | * from local format to extent format here. Hence I don't think | |
4954 | * this branch is ever executed intentionally and we should | |
4955 | * consider removing it and asserting that xfs_bmapi_write() | |
4956 | * cannot be called directly on local format forks. i.e. callers | |
4957 | * are completely responsible for local to extent format | |
4958 | * conversion, not xfs_bmapi_write(). | |
1da177e4 | 4959 | */ |
9e5987a7 DC |
4960 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, total, |
4961 | &bma.logflags, whichfork, | |
4962 | xfs_bmap_local_to_extents_init_fn); | |
4963 | if (error) | |
4964 | goto error0; | |
4965 | } | |
4966 | ||
4967 | if (*firstblock == NULLFSBLOCK) { | |
4968 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) | |
4969 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; | |
4970 | else | |
4971 | bma.minleft = 1; | |
4972 | } else { | |
4973 | bma.minleft = 0; | |
4974 | } | |
4975 | ||
4976 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { | |
4977 | error = xfs_iread_extents(tp, ip, whichfork); | |
4978 | if (error) | |
4979 | goto error0; | |
4980 | } | |
4981 | ||
4982 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got, | |
4983 | &bma.prev); | |
4984 | n = 0; | |
4985 | end = bno + len; | |
4986 | obno = bno; | |
4987 | ||
4988 | bma.tp = tp; | |
4989 | bma.ip = ip; | |
4990 | bma.total = total; | |
4991 | bma.userdata = 0; | |
4992 | bma.flist = flist; | |
4993 | bma.firstblock = firstblock; | |
4994 | ||
4995 | if (flags & XFS_BMAPI_STACK_SWITCH) | |
4996 | bma.stack_switch = 1; | |
4997 | ||
4998 | while (bno < end && n < *nmap) { | |
4999 | inhole = eof || bma.got.br_startoff > bno; | |
5000 | wasdelay = !inhole && isnullstartblock(bma.got.br_startblock); | |
5001 | ||
1da177e4 | 5002 | /* |
9e5987a7 DC |
5003 | * First, deal with the hole before the allocated space |
5004 | * that we found, if any. | |
1da177e4 | 5005 | */ |
9e5987a7 DC |
5006 | if (inhole || wasdelay) { |
5007 | bma.eof = eof; | |
5008 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); | |
5009 | bma.wasdel = wasdelay; | |
5010 | bma.offset = bno; | |
5011 | bma.flags = flags; | |
5012 | ||
1da177e4 | 5013 | /* |
9e5987a7 DC |
5014 | * There's a 32/64 bit type mismatch between the |
5015 | * allocation length request (which can be 64 bits in | |
5016 | * length) and the bma length request, which is | |
5017 | * xfs_extlen_t and therefore 32 bits. Hence we have to | |
5018 | * check for 32-bit overflows and handle them here. | |
1da177e4 | 5019 | */ |
9e5987a7 DC |
5020 | if (len > (xfs_filblks_t)MAXEXTLEN) |
5021 | bma.length = MAXEXTLEN; | |
5022 | else | |
5023 | bma.length = len; | |
5024 | ||
5025 | ASSERT(len > 0); | |
5026 | ASSERT(bma.length > 0); | |
5027 | error = xfs_bmapi_allocate(&bma); | |
1da177e4 LT |
5028 | if (error) |
5029 | goto error0; | |
9e5987a7 DC |
5030 | if (bma.blkno == NULLFSBLOCK) |
5031 | break; | |
1da177e4 | 5032 | } |
9e5987a7 DC |
5033 | |
5034 | /* Deal with the allocated space we found. */ | |
5035 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, | |
5036 | end, n, flags); | |
5037 | ||
5038 | /* Execute unwritten extent conversion if necessary */ | |
5039 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); | |
5040 | if (error == EAGAIN) | |
5041 | continue; | |
5042 | if (error) | |
5043 | goto error0; | |
5044 | ||
5045 | /* update the extent map to return */ | |
5046 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); | |
5047 | ||
5048 | /* | |
5049 | * If we're done, stop now. Stop when we've allocated | |
5050 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise | |
5051 | * the transaction may get too big. | |
5052 | */ | |
5053 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) | |
5054 | break; | |
5055 | ||
5056 | /* Else go on to the next record. */ | |
5057 | bma.prev = bma.got; | |
5058 | if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { | |
5059 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx), | |
5060 | &bma.got); | |
5061 | } else | |
5062 | eof = 1; | |
5063 | } | |
5064 | *nmap = n; | |
5065 | ||
5066 | /* | |
5067 | * Transform from btree to extents, give it cur. | |
5068 | */ | |
5069 | if (xfs_bmap_wants_extents(ip, whichfork)) { | |
5070 | int tmp_logflags = 0; | |
5071 | ||
5072 | ASSERT(bma.cur); | |
5073 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, | |
5074 | &tmp_logflags, whichfork); | |
5075 | bma.logflags |= tmp_logflags; | |
5076 | if (error) | |
5077 | goto error0; | |
5078 | } | |
5079 | ||
5080 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || | |
5081 | XFS_IFORK_NEXTENTS(ip, whichfork) > | |
5082 | XFS_IFORK_MAXEXT(ip, whichfork)); | |
5083 | error = 0; | |
5084 | error0: | |
5085 | /* | |
5086 | * Log everything. Do this after conversion, there's no point in | |
5087 | * logging the extent records if we've converted to btree format. | |
5088 | */ | |
5089 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && | |
5090 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | |
5091 | bma.logflags &= ~xfs_ilog_fext(whichfork); | |
5092 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && | |
5093 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) | |
5094 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); | |
5095 | /* | |
5096 | * Log whatever the flags say, even if error. Otherwise we might miss | |
5097 | * detecting a case where the data is changed, there's an error, | |
5098 | * and it's not logged so we don't shutdown when we should. | |
5099 | */ | |
5100 | if (bma.logflags) | |
5101 | xfs_trans_log_inode(tp, ip, bma.logflags); | |
5102 | ||
5103 | if (bma.cur) { | |
5104 | if (!error) { | |
5105 | ASSERT(*firstblock == NULLFSBLOCK || | |
5106 | XFS_FSB_TO_AGNO(mp, *firstblock) == | |
5107 | XFS_FSB_TO_AGNO(mp, | |
5108 | bma.cur->bc_private.b.firstblock) || | |
5109 | (flist->xbf_low && | |
5110 | XFS_FSB_TO_AGNO(mp, *firstblock) < | |
5111 | XFS_FSB_TO_AGNO(mp, | |
5112 | bma.cur->bc_private.b.firstblock))); | |
5113 | *firstblock = bma.cur->bc_private.b.firstblock; | |
1da177e4 | 5114 | } |
9e5987a7 DC |
5115 | xfs_btree_del_cursor(bma.cur, |
5116 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
5117 | } | |
5118 | if (!error) | |
5119 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, | |
5120 | orig_nmap, *nmap); | |
5121 | return error; | |
5122 | } | |
06d10dd9 | 5123 | |
9e5987a7 DC |
5124 | /* |
5125 | * Called by xfs_bmapi to update file extent records and the btree | |
5126 | * after removing space (or undoing a delayed allocation). | |
5127 | */ | |
5128 | STATIC int /* error */ | |
5129 | xfs_bmap_del_extent( | |
5130 | xfs_inode_t *ip, /* incore inode pointer */ | |
5131 | xfs_trans_t *tp, /* current transaction pointer */ | |
5132 | xfs_extnum_t *idx, /* extent number to update/delete */ | |
5133 | xfs_bmap_free_t *flist, /* list of extents to be freed */ | |
5134 | xfs_btree_cur_t *cur, /* if null, not a btree */ | |
5135 | xfs_bmbt_irec_t *del, /* data to remove from extents */ | |
5136 | int *logflagsp, /* inode logging flags */ | |
5137 | int whichfork) /* data or attr fork */ | |
5138 | { | |
5139 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ | |
5140 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ | |
5141 | xfs_fsblock_t del_endblock=0; /* first block past del */ | |
5142 | xfs_fileoff_t del_endoff; /* first offset past del */ | |
5143 | int delay; /* current block is delayed allocated */ | |
5144 | int do_fx; /* free extent at end of routine */ | |
5145 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ | |
5146 | int error; /* error return value */ | |
5147 | int flags; /* inode logging flags */ | |
5148 | xfs_bmbt_irec_t got; /* current extent entry */ | |
5149 | xfs_fileoff_t got_endoff; /* first offset past got */ | |
5150 | int i; /* temp state */ | |
5151 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
5152 | xfs_mount_t *mp; /* mount structure */ | |
5153 | xfs_filblks_t nblks; /* quota/sb block count */ | |
5154 | xfs_bmbt_irec_t new; /* new record to be inserted */ | |
5155 | /* REFERENCED */ | |
5156 | uint qfield; /* quota field to update */ | |
5157 | xfs_filblks_t temp; /* for indirect length calculations */ | |
5158 | xfs_filblks_t temp2; /* for indirect length calculations */ | |
5159 | int state = 0; | |
5160 | ||
5161 | XFS_STATS_INC(xs_del_exlist); | |
5162 | ||
5163 | if (whichfork == XFS_ATTR_FORK) | |
5164 | state |= BMAP_ATTRFORK; | |
5165 | ||
5166 | mp = ip->i_mount; | |
5167 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
5168 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / | |
5169 | (uint)sizeof(xfs_bmbt_rec_t))); | |
5170 | ASSERT(del->br_blockcount > 0); | |
5171 | ep = xfs_iext_get_ext(ifp, *idx); | |
5172 | xfs_bmbt_get_all(ep, &got); | |
5173 | ASSERT(got.br_startoff <= del->br_startoff); | |
5174 | del_endoff = del->br_startoff + del->br_blockcount; | |
5175 | got_endoff = got.br_startoff + got.br_blockcount; | |
5176 | ASSERT(got_endoff >= del_endoff); | |
5177 | delay = isnullstartblock(got.br_startblock); | |
5178 | ASSERT(isnullstartblock(del->br_startblock) == delay); | |
5179 | flags = 0; | |
5180 | qfield = 0; | |
5181 | error = 0; | |
5182 | /* | |
5183 | * If deleting a real allocation, must free up the disk space. | |
5184 | */ | |
5185 | if (!delay) { | |
5186 | flags = XFS_ILOG_CORE; | |
5187 | /* | |
5188 | * Realtime allocation. Free it and record di_nblocks update. | |
5189 | */ | |
5190 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { | |
5191 | xfs_fsblock_t bno; | |
5192 | xfs_filblks_t len; | |
5193 | ||
5194 | ASSERT(do_mod(del->br_blockcount, | |
5195 | mp->m_sb.sb_rextsize) == 0); | |
5196 | ASSERT(do_mod(del->br_startblock, | |
5197 | mp->m_sb.sb_rextsize) == 0); | |
5198 | bno = del->br_startblock; | |
5199 | len = del->br_blockcount; | |
5200 | do_div(bno, mp->m_sb.sb_rextsize); | |
5201 | do_div(len, mp->m_sb.sb_rextsize); | |
5202 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); | |
5203 | if (error) | |
5204 | goto done; | |
5205 | do_fx = 0; | |
5206 | nblks = len * mp->m_sb.sb_rextsize; | |
5207 | qfield = XFS_TRANS_DQ_RTBCOUNT; | |
5208 | } | |
1da177e4 | 5209 | /* |
9e5987a7 | 5210 | * Ordinary allocation. |
1da177e4 | 5211 | */ |
9e5987a7 DC |
5212 | else { |
5213 | do_fx = 1; | |
5214 | nblks = del->br_blockcount; | |
5215 | qfield = XFS_TRANS_DQ_BCOUNT; | |
1da177e4 | 5216 | } |
1da177e4 | 5217 | /* |
9e5987a7 | 5218 | * Set up del_endblock and cur for later. |
1da177e4 | 5219 | */ |
9e5987a7 DC |
5220 | del_endblock = del->br_startblock + del->br_blockcount; |
5221 | if (cur) { | |
5222 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, | |
5223 | got.br_startblock, got.br_blockcount, | |
5224 | &i))) | |
5225 | goto done; | |
5226 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
1da177e4 | 5227 | } |
9e5987a7 DC |
5228 | da_old = da_new = 0; |
5229 | } else { | |
5230 | da_old = startblockval(got.br_startblock); | |
5231 | da_new = 0; | |
5232 | nblks = 0; | |
5233 | do_fx = 0; | |
1da177e4 LT |
5234 | } |
5235 | /* | |
9e5987a7 DC |
5236 | * Set flag value to use in switch statement. |
5237 | * Left-contig is 2, right-contig is 1. | |
1da177e4 | 5238 | */ |
9e5987a7 DC |
5239 | switch (((got.br_startoff == del->br_startoff) << 1) | |
5240 | (got_endoff == del_endoff)) { | |
5241 | case 3: | |
5242 | /* | |
5243 | * Matches the whole extent. Delete the entry. | |
5244 | */ | |
5245 | xfs_iext_remove(ip, *idx, 1, | |
5246 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); | |
5247 | --*idx; | |
5248 | if (delay) | |
5249 | break; | |
5250 | ||
5251 | XFS_IFORK_NEXT_SET(ip, whichfork, | |
5252 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); | |
5253 | flags |= XFS_ILOG_CORE; | |
5254 | if (!cur) { | |
5255 | flags |= xfs_ilog_fext(whichfork); | |
5256 | break; | |
5257 | } | |
5258 | if ((error = xfs_btree_delete(cur, &i))) | |
5259 | goto done; | |
5260 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
5261 | break; | |
5262 | ||
5263 | case 2: | |
5264 | /* | |
5265 | * Deleting the first part of the extent. | |
5266 | */ | |
5267 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
5268 | xfs_bmbt_set_startoff(ep, del_endoff); | |
5269 | temp = got.br_blockcount - del->br_blockcount; | |
5270 | xfs_bmbt_set_blockcount(ep, temp); | |
5271 | if (delay) { | |
5272 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | |
5273 | da_old); | |
5274 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
5275 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
5276 | da_new = temp; | |
5277 | break; | |
5278 | } | |
5279 | xfs_bmbt_set_startblock(ep, del_endblock); | |
5280 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
5281 | if (!cur) { | |
5282 | flags |= xfs_ilog_fext(whichfork); | |
5283 | break; | |
5284 | } | |
5285 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, | |
5286 | got.br_blockcount - del->br_blockcount, | |
5287 | got.br_state))) | |
5288 | goto done; | |
5289 | break; | |
5290 | ||
5291 | case 1: | |
5292 | /* | |
5293 | * Deleting the last part of the extent. | |
5294 | */ | |
5295 | temp = got.br_blockcount - del->br_blockcount; | |
5296 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
5297 | xfs_bmbt_set_blockcount(ep, temp); | |
5298 | if (delay) { | |
5299 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | |
5300 | da_old); | |
5301 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
5302 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
5303 | da_new = temp; | |
5304 | break; | |
5305 | } | |
5306 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
5307 | if (!cur) { | |
5308 | flags |= xfs_ilog_fext(whichfork); | |
5309 | break; | |
5310 | } | |
5311 | if ((error = xfs_bmbt_update(cur, got.br_startoff, | |
5312 | got.br_startblock, | |
5313 | got.br_blockcount - del->br_blockcount, | |
5314 | got.br_state))) | |
5315 | goto done; | |
5316 | break; | |
5317 | ||
5318 | case 0: | |
5319 | /* | |
5320 | * Deleting the middle of the extent. | |
5321 | */ | |
5322 | temp = del->br_startoff - got.br_startoff; | |
5323 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
5324 | xfs_bmbt_set_blockcount(ep, temp); | |
5325 | new.br_startoff = del_endoff; | |
5326 | temp2 = got_endoff - del_endoff; | |
5327 | new.br_blockcount = temp2; | |
5328 | new.br_state = got.br_state; | |
5329 | if (!delay) { | |
5330 | new.br_startblock = del_endblock; | |
5331 | flags |= XFS_ILOG_CORE; | |
5332 | if (cur) { | |
5333 | if ((error = xfs_bmbt_update(cur, | |
5334 | got.br_startoff, | |
5335 | got.br_startblock, temp, | |
5336 | got.br_state))) | |
5337 | goto done; | |
5338 | if ((error = xfs_btree_increment(cur, 0, &i))) | |
5339 | goto done; | |
5340 | cur->bc_rec.b = new; | |
5341 | error = xfs_btree_insert(cur, &i); | |
5342 | if (error && error != ENOSPC) | |
5343 | goto done; | |
5344 | /* | |
5345 | * If get no-space back from btree insert, | |
5346 | * it tried a split, and we have a zero | |
5347 | * block reservation. | |
5348 | * Fix up our state and return the error. | |
5349 | */ | |
5350 | if (error == ENOSPC) { | |
5351 | /* | |
5352 | * Reset the cursor, don't trust | |
5353 | * it after any insert operation. | |
5354 | */ | |
5355 | if ((error = xfs_bmbt_lookup_eq(cur, | |
5356 | got.br_startoff, | |
5357 | got.br_startblock, | |
5358 | temp, &i))) | |
5359 | goto done; | |
5360 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
5361 | /* | |
5362 | * Update the btree record back | |
5363 | * to the original value. | |
5364 | */ | |
5365 | if ((error = xfs_bmbt_update(cur, | |
5366 | got.br_startoff, | |
5367 | got.br_startblock, | |
5368 | got.br_blockcount, | |
5369 | got.br_state))) | |
5370 | goto done; | |
5371 | /* | |
5372 | * Reset the extent record back | |
5373 | * to the original value. | |
5374 | */ | |
5375 | xfs_bmbt_set_blockcount(ep, | |
5376 | got.br_blockcount); | |
5377 | flags = 0; | |
5378 | error = XFS_ERROR(ENOSPC); | |
5379 | goto done; | |
5380 | } | |
5381 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); | |
5382 | } else | |
5383 | flags |= xfs_ilog_fext(whichfork); | |
5384 | XFS_IFORK_NEXT_SET(ip, whichfork, | |
5385 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); | |
5386 | } else { | |
5387 | ASSERT(whichfork == XFS_DATA_FORK); | |
5388 | temp = xfs_bmap_worst_indlen(ip, temp); | |
5389 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
5390 | temp2 = xfs_bmap_worst_indlen(ip, temp2); | |
5391 | new.br_startblock = nullstartblock((int)temp2); | |
5392 | da_new = temp + temp2; | |
5393 | while (da_new > da_old) { | |
5394 | if (temp) { | |
5395 | temp--; | |
5396 | da_new--; | |
5397 | xfs_bmbt_set_startblock(ep, | |
5398 | nullstartblock((int)temp)); | |
5399 | } | |
5400 | if (da_new == da_old) | |
5401 | break; | |
5402 | if (temp2) { | |
5403 | temp2--; | |
5404 | da_new--; | |
5405 | new.br_startblock = | |
5406 | nullstartblock((int)temp2); | |
5407 | } | |
5408 | } | |
5409 | } | |
5410 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
5411 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); | |
5412 | ++*idx; | |
5413 | break; | |
1da177e4 LT |
5414 | } |
5415 | /* | |
9e5987a7 | 5416 | * If we need to, add to list of extents to delete. |
1da177e4 | 5417 | */ |
9e5987a7 DC |
5418 | if (do_fx) |
5419 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, | |
5420 | mp); | |
1da177e4 | 5421 | /* |
9e5987a7 | 5422 | * Adjust inode # blocks in the file. |
1da177e4 | 5423 | */ |
9e5987a7 DC |
5424 | if (nblks) |
5425 | ip->i_d.di_nblocks -= nblks; | |
1da177e4 | 5426 | /* |
9e5987a7 | 5427 | * Adjust quota data. |
1da177e4 | 5428 | */ |
9e5987a7 DC |
5429 | if (qfield) |
5430 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); | |
5431 | ||
5432 | /* | |
5433 | * Account for change in delayed indirect blocks. | |
5434 | * Nothing to do for disk quota accounting here. | |
5435 | */ | |
5436 | ASSERT(da_old >= da_new); | |
5437 | if (da_old > da_new) { | |
5438 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, | |
5439 | (int64_t)(da_old - da_new), 0); | |
1da177e4 | 5440 | } |
9e5987a7 DC |
5441 | done: |
5442 | *logflagsp = flags; | |
1da177e4 LT |
5443 | return error; |
5444 | } | |
5445 | ||
3bacbcd8 | 5446 | /* |
9e5987a7 DC |
5447 | * Unmap (remove) blocks from a file. |
5448 | * If nexts is nonzero then the number of extents to remove is limited to | |
5449 | * that value. If not all extents in the block range can be removed then | |
5450 | * *done is set. | |
3bacbcd8 | 5451 | */ |
9e5987a7 DC |
5452 | int /* error */ |
5453 | xfs_bunmapi( | |
5454 | xfs_trans_t *tp, /* transaction pointer */ | |
5455 | struct xfs_inode *ip, /* incore inode */ | |
5456 | xfs_fileoff_t bno, /* starting offset to unmap */ | |
5457 | xfs_filblks_t len, /* length to unmap in file */ | |
5458 | int flags, /* misc flags */ | |
5459 | xfs_extnum_t nexts, /* number of extents max */ | |
5460 | xfs_fsblock_t *firstblock, /* first allocated block | |
5461 | controls a.g. for allocs */ | |
5462 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ | |
5463 | int *done) /* set if not done yet */ | |
3bacbcd8 | 5464 | { |
9e5987a7 DC |
5465 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
5466 | xfs_bmbt_irec_t del; /* extent being deleted */ | |
5467 | int eof; /* is deleting at eof */ | |
5468 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
5469 | int error; /* error return value */ | |
5470 | xfs_extnum_t extno; /* extent number in list */ | |
5471 | xfs_bmbt_irec_t got; /* current extent record */ | |
5af317c9 | 5472 | xfs_ifork_t *ifp; /* inode fork pointer */ |
9e5987a7 DC |
5473 | int isrt; /* freeing in rt area */ |
5474 | xfs_extnum_t lastx; /* last extent index used */ | |
5475 | int logflags; /* transaction logging flags */ | |
5476 | xfs_extlen_t mod; /* rt extent offset */ | |
5477 | xfs_mount_t *mp; /* mount structure */ | |
5478 | xfs_extnum_t nextents; /* number of file extents */ | |
5479 | xfs_bmbt_irec_t prev; /* previous extent record */ | |
5480 | xfs_fileoff_t start; /* first file offset deleted */ | |
5481 | int tmp_logflags; /* partial logging flags */ | |
5482 | int wasdel; /* was a delayed alloc extent */ | |
5483 | int whichfork; /* data or attribute fork */ | |
5484 | xfs_fsblock_t sum; | |
1da177e4 | 5485 | |
9e5987a7 | 5486 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
1da177e4 | 5487 | |
9e5987a7 DC |
5488 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
5489 | XFS_ATTR_FORK : XFS_DATA_FORK; | |
5490 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
5491 | if (unlikely( | |
5492 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
5493 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { | |
5494 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, | |
5495 | ip->i_mount); | |
5496 | return XFS_ERROR(EFSCORRUPTED); | |
1da177e4 | 5497 | } |
9e5987a7 DC |
5498 | mp = ip->i_mount; |
5499 | if (XFS_FORCED_SHUTDOWN(mp)) | |
5500 | return XFS_ERROR(EIO); | |
1da177e4 | 5501 | |
9e5987a7 DC |
5502 | ASSERT(len > 0); |
5503 | ASSERT(nexts >= 0); | |
1da177e4 | 5504 | |
9e5987a7 DC |
5505 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
5506 | (error = xfs_iread_extents(tp, ip, whichfork))) | |
5507 | return error; | |
5508 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
5509 | if (nextents == 0) { | |
5510 | *done = 1; | |
5511 | return 0; | |
5512 | } | |
5513 | XFS_STATS_INC(xs_blk_unmap); | |
5514 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); | |
5515 | start = bno; | |
5516 | bno = start + len - 1; | |
5517 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, | |
5518 | &prev); | |
1da177e4 | 5519 | |
9e5987a7 DC |
5520 | /* |
5521 | * Check to see if the given block number is past the end of the | |
5522 | * file, back up to the last block if so... | |
5523 | */ | |
5524 | if (eof) { | |
5525 | ep = xfs_iext_get_ext(ifp, --lastx); | |
5526 | xfs_bmbt_get_all(ep, &got); | |
5527 | bno = got.br_startoff + got.br_blockcount - 1; | |
5528 | } | |
5529 | logflags = 0; | |
5530 | if (ifp->if_flags & XFS_IFBROOT) { | |
5531 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); | |
5532 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); | |
5533 | cur->bc_private.b.firstblock = *firstblock; | |
5534 | cur->bc_private.b.flist = flist; | |
5535 | cur->bc_private.b.flags = 0; | |
5536 | } else | |
5537 | cur = NULL; | |
5538 | ||
5539 | if (isrt) { | |
5540 | /* | |
5541 | * Synchronize by locking the bitmap inode. | |
5542 | */ | |
5543 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); | |
5544 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); | |
5545 | } | |
58e20770 | 5546 | |
9e5987a7 DC |
5547 | extno = 0; |
5548 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && | |
5549 | (nexts == 0 || extno < nexts)) { | |
5550 | /* | |
5551 | * Is the found extent after a hole in which bno lives? | |
5552 | * Just back up to the previous extent, if so. | |
5553 | */ | |
5554 | if (got.br_startoff > bno) { | |
5555 | if (--lastx < 0) | |
5556 | break; | |
5557 | ep = xfs_iext_get_ext(ifp, lastx); | |
5558 | xfs_bmbt_get_all(ep, &got); | |
5559 | } | |
5560 | /* | |
5561 | * Is the last block of this extent before the range | |
5562 | * we're supposed to delete? If so, we're done. | |
5563 | */ | |
5564 | bno = XFS_FILEOFF_MIN(bno, | |
5565 | got.br_startoff + got.br_blockcount - 1); | |
5566 | if (bno < start) | |
5567 | break; | |
5568 | /* | |
5569 | * Then deal with the (possibly delayed) allocated space | |
5570 | * we found. | |
5571 | */ | |
5572 | ASSERT(ep != NULL); | |
5573 | del = got; | |
5574 | wasdel = isnullstartblock(del.br_startblock); | |
5575 | if (got.br_startoff < start) { | |
5576 | del.br_startoff = start; | |
5577 | del.br_blockcount -= start - got.br_startoff; | |
5578 | if (!wasdel) | |
5579 | del.br_startblock += start - got.br_startoff; | |
5580 | } | |
5581 | if (del.br_startoff + del.br_blockcount > bno + 1) | |
5582 | del.br_blockcount = bno + 1 - del.br_startoff; | |
5583 | sum = del.br_startblock + del.br_blockcount; | |
5584 | if (isrt && | |
5585 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { | |
58e20770 | 5586 | /* |
9e5987a7 DC |
5587 | * Realtime extent not lined up at the end. |
5588 | * The extent could have been split into written | |
5589 | * and unwritten pieces, or we could just be | |
5590 | * unmapping part of it. But we can't really | |
5591 | * get rid of part of a realtime extent. | |
58e20770 | 5592 | */ |
9e5987a7 DC |
5593 | if (del.br_state == XFS_EXT_UNWRITTEN || |
5594 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { | |
5595 | /* | |
5596 | * This piece is unwritten, or we're not | |
5597 | * using unwritten extents. Skip over it. | |
5598 | */ | |
5599 | ASSERT(bno >= mod); | |
5600 | bno -= mod > del.br_blockcount ? | |
5601 | del.br_blockcount : mod; | |
5602 | if (bno < got.br_startoff) { | |
5603 | if (--lastx >= 0) | |
5604 | xfs_bmbt_get_all(xfs_iext_get_ext( | |
5605 | ifp, lastx), &got); | |
5606 | } | |
5607 | continue; | |
1da177e4 | 5608 | } |
9af25465 | 5609 | /* |
9e5987a7 DC |
5610 | * It's written, turn it unwritten. |
5611 | * This is better than zeroing it. | |
9af25465 | 5612 | */ |
9e5987a7 DC |
5613 | ASSERT(del.br_state == XFS_EXT_NORM); |
5614 | ASSERT(xfs_trans_get_block_res(tp) > 0); | |
5615 | /* | |
5616 | * If this spans a realtime extent boundary, | |
5617 | * chop it back to the start of the one we end at. | |
5618 | */ | |
5619 | if (del.br_blockcount > mod) { | |
5620 | del.br_startoff += del.br_blockcount - mod; | |
5621 | del.br_startblock += del.br_blockcount - mod; | |
5622 | del.br_blockcount = mod; | |
5623 | } | |
5624 | del.br_state = XFS_EXT_UNWRITTEN; | |
5625 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, | |
5626 | &lastx, &cur, &del, firstblock, flist, | |
5627 | &logflags); | |
5628 | if (error) | |
5629 | goto error0; | |
5630 | goto nodelete; | |
5631 | } | |
5632 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { | |
5633 | /* | |
5634 | * Realtime extent is lined up at the end but not | |
5635 | * at the front. We'll get rid of full extents if | |
5636 | * we can. | |
5637 | */ | |
5638 | mod = mp->m_sb.sb_rextsize - mod; | |
5639 | if (del.br_blockcount > mod) { | |
5640 | del.br_blockcount -= mod; | |
5641 | del.br_startoff += mod; | |
5642 | del.br_startblock += mod; | |
5643 | } else if ((del.br_startoff == start && | |
5644 | (del.br_state == XFS_EXT_UNWRITTEN || | |
5645 | xfs_trans_get_block_res(tp) == 0)) || | |
5646 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { | |
5647 | /* | |
5648 | * Can't make it unwritten. There isn't | |
5649 | * a full extent here so just skip it. | |
5650 | */ | |
5651 | ASSERT(bno >= del.br_blockcount); | |
5652 | bno -= del.br_blockcount; | |
5653 | if (got.br_startoff > bno) { | |
5654 | if (--lastx >= 0) { | |
5655 | ep = xfs_iext_get_ext(ifp, | |
5656 | lastx); | |
5657 | xfs_bmbt_get_all(ep, &got); | |
5658 | } | |
5659 | } | |
9af25465 | 5660 | continue; |
9e5987a7 DC |
5661 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
5662 | /* | |
5663 | * This one is already unwritten. | |
5664 | * It must have a written left neighbor. | |
5665 | * Unwrite the killed part of that one and | |
5666 | * try again. | |
5667 | */ | |
5668 | ASSERT(lastx > 0); | |
5669 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, | |
5670 | lastx - 1), &prev); | |
5671 | ASSERT(prev.br_state == XFS_EXT_NORM); | |
5672 | ASSERT(!isnullstartblock(prev.br_startblock)); | |
5673 | ASSERT(del.br_startblock == | |
5674 | prev.br_startblock + prev.br_blockcount); | |
5675 | if (prev.br_startoff < start) { | |
5676 | mod = start - prev.br_startoff; | |
5677 | prev.br_blockcount -= mod; | |
5678 | prev.br_startblock += mod; | |
5679 | prev.br_startoff = start; | |
5680 | } | |
5681 | prev.br_state = XFS_EXT_UNWRITTEN; | |
5682 | lastx--; | |
5683 | error = xfs_bmap_add_extent_unwritten_real(tp, | |
5684 | ip, &lastx, &cur, &prev, | |
5685 | firstblock, flist, &logflags); | |
5686 | if (error) | |
5687 | goto error0; | |
5688 | goto nodelete; | |
5689 | } else { | |
5690 | ASSERT(del.br_state == XFS_EXT_NORM); | |
5691 | del.br_state = XFS_EXT_UNWRITTEN; | |
5692 | error = xfs_bmap_add_extent_unwritten_real(tp, | |
5693 | ip, &lastx, &cur, &del, | |
5694 | firstblock, flist, &logflags); | |
5695 | if (error) | |
5696 | goto error0; | |
5697 | goto nodelete; | |
9af25465 | 5698 | } |
1da177e4 | 5699 | } |
9e5987a7 DC |
5700 | if (wasdel) { |
5701 | ASSERT(startblockval(del.br_startblock) > 0); | |
5702 | /* Update realtime/data freespace, unreserve quota */ | |
5703 | if (isrt) { | |
5704 | xfs_filblks_t rtexts; | |
1da177e4 | 5705 | |
9e5987a7 DC |
5706 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
5707 | do_div(rtexts, mp->m_sb.sb_rextsize); | |
5708 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, | |
5709 | (int64_t)rtexts, 0); | |
5710 | (void)xfs_trans_reserve_quota_nblks(NULL, | |
5711 | ip, -((long)del.br_blockcount), 0, | |
5712 | XFS_QMOPT_RES_RTBLKS); | |
5713 | } else { | |
5714 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, | |
5715 | (int64_t)del.br_blockcount, 0); | |
5716 | (void)xfs_trans_reserve_quota_nblks(NULL, | |
5717 | ip, -((long)del.br_blockcount), 0, | |
5718 | XFS_QMOPT_RES_REGBLKS); | |
5719 | } | |
5720 | ip->i_delayed_blks -= del.br_blockcount; | |
5721 | if (cur) | |
5722 | cur->bc_private.b.flags |= | |
5723 | XFS_BTCUR_BPRV_WASDEL; | |
5724 | } else if (cur) | |
5725 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; | |
5726 | /* | |
5727 | * If it's the case where the directory code is running | |
5728 | * with no block reservation, and the deleted block is in | |
5729 | * the middle of its extent, and the resulting insert | |
5730 | * of an extent would cause transformation to btree format, | |
5731 | * then reject it. The calling code will then swap | |
5732 | * blocks around instead. | |
5733 | * We have to do this now, rather than waiting for the | |
5734 | * conversion to btree format, since the transaction | |
5735 | * will be dirty. | |
5736 | */ | |
5737 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && | |
5738 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && | |
5739 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ | |
5740 | XFS_IFORK_MAXEXT(ip, whichfork) && | |
5741 | del.br_startoff > got.br_startoff && | |
5742 | del.br_startoff + del.br_blockcount < | |
5743 | got.br_startoff + got.br_blockcount) { | |
5744 | error = XFS_ERROR(ENOSPC); | |
5745 | goto error0; | |
1da177e4 | 5746 | } |
9e5987a7 DC |
5747 | error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del, |
5748 | &tmp_logflags, whichfork); | |
5749 | logflags |= tmp_logflags; | |
5750 | if (error) | |
5751 | goto error0; | |
5752 | bno = del.br_startoff - 1; | |
5753 | nodelete: | |
1da177e4 | 5754 | /* |
9e5987a7 | 5755 | * If not done go on to the next (previous) record. |
1da177e4 | 5756 | */ |
9e5987a7 DC |
5757 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
5758 | if (lastx >= 0) { | |
5759 | ep = xfs_iext_get_ext(ifp, lastx); | |
5760 | if (xfs_bmbt_get_startoff(ep) > bno) { | |
5761 | if (--lastx >= 0) | |
5762 | ep = xfs_iext_get_ext(ifp, | |
5763 | lastx); | |
5764 | } | |
5765 | xfs_bmbt_get_all(ep, &got); | |
1da177e4 | 5766 | } |
9e5987a7 | 5767 | extno++; |
1da177e4 LT |
5768 | } |
5769 | } | |
9e5987a7 | 5770 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
576039cf | 5771 | |
1da177e4 | 5772 | /* |
9e5987a7 | 5773 | * Convert to a btree if necessary. |
1da177e4 | 5774 | */ |
9e5987a7 DC |
5775 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
5776 | ASSERT(cur == NULL); | |
5777 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, | |
5778 | &cur, 0, &tmp_logflags, whichfork); | |
5779 | logflags |= tmp_logflags; | |
5780 | if (error) | |
5781 | goto error0; | |
1da177e4 | 5782 | } |
1da177e4 | 5783 | /* |
9e5987a7 | 5784 | * transform from btree to extents, give it cur |
1da177e4 | 5785 | */ |
9e5987a7 DC |
5786 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
5787 | ASSERT(cur != NULL); | |
5788 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, | |
5789 | whichfork); | |
5790 | logflags |= tmp_logflags; | |
5791 | if (error) | |
5792 | goto error0; | |
5793 | } | |
1da177e4 | 5794 | /* |
9e5987a7 | 5795 | * transform from extents to local? |
1da177e4 | 5796 | */ |
9e5987a7 DC |
5797 | error = 0; |
5798 | error0: | |
5799 | /* | |
5800 | * Log everything. Do this after conversion, there's no point in | |
5801 | * logging the extent records if we've converted to btree format. | |
5802 | */ | |
5803 | if ((logflags & xfs_ilog_fext(whichfork)) && | |
5804 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | |
5805 | logflags &= ~xfs_ilog_fext(whichfork); | |
5806 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && | |
5807 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) | |
5808 | logflags &= ~xfs_ilog_fbroot(whichfork); | |
5809 | /* | |
5810 | * Log inode even in the error case, if the transaction | |
5811 | * is dirty we'll need to shut down the filesystem. | |
5812 | */ | |
5813 | if (logflags) | |
5814 | xfs_trans_log_inode(tp, ip, logflags); | |
5815 | if (cur) { | |
5816 | if (!error) { | |
5817 | *firstblock = cur->bc_private.b.firstblock; | |
5818 | cur->bc_private.b.allocated = 0; | |
5819 | } | |
5820 | xfs_btree_del_cursor(cur, | |
5821 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
5822 | } | |
5823 | return error; | |
5824 | } | |
1da177e4 | 5825 | |
9e5987a7 DC |
5826 | /* |
5827 | * returns 1 for success, 0 if we failed to map the extent. | |
5828 | */ | |
5829 | STATIC int | |
5830 | xfs_getbmapx_fix_eof_hole( | |
5831 | xfs_inode_t *ip, /* xfs incore inode pointer */ | |
5832 | struct getbmapx *out, /* output structure */ | |
5833 | int prealloced, /* this is a file with | |
5834 | * preallocated data space */ | |
5835 | __int64_t end, /* last block requested */ | |
5836 | xfs_fsblock_t startblock) | |
5837 | { | |
5838 | __int64_t fixlen; | |
5839 | xfs_mount_t *mp; /* file system mount point */ | |
5840 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
5841 | xfs_extnum_t lastx; /* last extent pointer */ | |
5842 | xfs_fileoff_t fileblock; | |
1da177e4 | 5843 | |
9e5987a7 DC |
5844 | if (startblock == HOLESTARTBLOCK) { |
5845 | mp = ip->i_mount; | |
5846 | out->bmv_block = -1; | |
5847 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip))); | |
5848 | fixlen -= out->bmv_offset; | |
5849 | if (prealloced && out->bmv_offset + out->bmv_length == end) { | |
5850 | /* Came to hole at EOF. Trim it. */ | |
5851 | if (fixlen <= 0) | |
5852 | return 0; | |
5853 | out->bmv_length = fixlen; | |
5854 | } | |
5855 | } else { | |
5856 | if (startblock == DELAYSTARTBLOCK) | |
5857 | out->bmv_block = -2; | |
5858 | else | |
5859 | out->bmv_block = xfs_fsb_to_db(ip, startblock); | |
5860 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); | |
5861 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | |
5862 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && | |
5863 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) | |
5864 | out->bmv_oflags |= BMV_OF_LAST; | |
5865 | } | |
1da177e4 | 5866 | |
9e5987a7 DC |
5867 | return 1; |
5868 | } | |
1da177e4 | 5869 | |
9e5987a7 DC |
5870 | /* |
5871 | * Get inode's extents as described in bmv, and format for output. | |
5872 | * Calls formatter to fill the user's buffer until all extents | |
5873 | * are mapped, until the passed-in bmv->bmv_count slots have | |
5874 | * been filled, or until the formatter short-circuits the loop, | |
5875 | * if it is tracking filled-in extents on its own. | |
5876 | */ | |
5877 | int /* error code */ | |
5878 | xfs_getbmap( | |
5879 | xfs_inode_t *ip, | |
5880 | struct getbmapx *bmv, /* user bmap structure */ | |
5881 | xfs_bmap_format_t formatter, /* format to user */ | |
5882 | void *arg) /* formatter arg */ | |
5883 | { | |
5884 | __int64_t bmvend; /* last block requested */ | |
5885 | int error = 0; /* return value */ | |
5886 | __int64_t fixlen; /* length for -1 case */ | |
5887 | int i; /* extent number */ | |
5888 | int lock; /* lock state */ | |
5889 | xfs_bmbt_irec_t *map; /* buffer for user's data */ | |
5890 | xfs_mount_t *mp; /* file system mount point */ | |
5891 | int nex; /* # of user extents can do */ | |
5892 | int nexleft; /* # of user extents left */ | |
5893 | int subnex; /* # of bmapi's can do */ | |
5894 | int nmap; /* number of map entries */ | |
5895 | struct getbmapx *out; /* output structure */ | |
5896 | int whichfork; /* data or attr fork */ | |
5897 | int prealloced; /* this is a file with | |
5898 | * preallocated data space */ | |
5899 | int iflags; /* interface flags */ | |
5900 | int bmapi_flags; /* flags for xfs_bmapi */ | |
5901 | int cur_ext = 0; | |
1da177e4 | 5902 | |
9e5987a7 DC |
5903 | mp = ip->i_mount; |
5904 | iflags = bmv->bmv_iflags; | |
5905 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; | |
1da177e4 | 5906 | |
9e5987a7 DC |
5907 | if (whichfork == XFS_ATTR_FORK) { |
5908 | if (XFS_IFORK_Q(ip)) { | |
5909 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && | |
5910 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && | |
5911 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) | |
5912 | return XFS_ERROR(EINVAL); | |
5913 | } else if (unlikely( | |
5914 | ip->i_d.di_aformat != 0 && | |
5915 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { | |
5916 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, | |
5917 | ip->i_mount); | |
5918 | return XFS_ERROR(EFSCORRUPTED); | |
1da177e4 | 5919 | } |
1da177e4 | 5920 | |
9e5987a7 DC |
5921 | prealloced = 0; |
5922 | fixlen = 1LL << 32; | |
5923 | } else { | |
5924 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && | |
5925 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && | |
5926 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) | |
5927 | return XFS_ERROR(EINVAL); | |
1da177e4 | 5928 | |
9e5987a7 DC |
5929 | if (xfs_get_extsz_hint(ip) || |
5930 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ | |
5931 | prealloced = 1; | |
5932 | fixlen = mp->m_super->s_maxbytes; | |
5933 | } else { | |
5934 | prealloced = 0; | |
5935 | fixlen = XFS_ISIZE(ip); | |
1da177e4 | 5936 | } |
1da177e4 | 5937 | } |
9e5987a7 DC |
5938 | |
5939 | if (bmv->bmv_length == -1) { | |
5940 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); | |
5941 | bmv->bmv_length = | |
5942 | max_t(__int64_t, fixlen - bmv->bmv_offset, 0); | |
5943 | } else if (bmv->bmv_length == 0) { | |
5944 | bmv->bmv_entries = 0; | |
5945 | return 0; | |
5946 | } else if (bmv->bmv_length < 0) { | |
5947 | return XFS_ERROR(EINVAL); | |
1da177e4 | 5948 | } |
1da177e4 | 5949 | |
9e5987a7 DC |
5950 | nex = bmv->bmv_count - 1; |
5951 | if (nex <= 0) | |
5952 | return XFS_ERROR(EINVAL); | |
5953 | bmvend = bmv->bmv_offset + bmv->bmv_length; | |
1da177e4 | 5954 | |
1da177e4 | 5955 | |
9e5987a7 DC |
5956 | if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) |
5957 | return XFS_ERROR(ENOMEM); | |
5958 | out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); | |
5959 | if (!out) { | |
5960 | out = kmem_zalloc_large(bmv->bmv_count * | |
5961 | sizeof(struct getbmapx)); | |
5962 | if (!out) | |
5963 | return XFS_ERROR(ENOMEM); | |
5964 | } | |
5965 | ||
5966 | xfs_ilock(ip, XFS_IOLOCK_SHARED); | |
5967 | if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { | |
5968 | if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) { | |
5969 | error = -filemap_write_and_wait(VFS_I(ip)->i_mapping); | |
5970 | if (error) | |
5971 | goto out_unlock_iolock; | |
5972 | } | |
5973 | /* | |
5974 | * even after flushing the inode, there can still be delalloc | |
5975 | * blocks on the inode beyond EOF due to speculative | |
5976 | * preallocation. These are not removed until the release | |
5977 | * function is called or the inode is inactivated. Hence we | |
5978 | * cannot assert here that ip->i_delayed_blks == 0. | |
5979 | */ | |
1da177e4 LT |
5980 | } |
5981 | ||
9e5987a7 DC |
5982 | lock = xfs_ilock_map_shared(ip); |
5983 | ||
1da177e4 | 5984 | /* |
9e5987a7 DC |
5985 | * Don't let nex be bigger than the number of extents |
5986 | * we can have assuming alternating holes and real extents. | |
1da177e4 | 5987 | */ |
9e5987a7 DC |
5988 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
5989 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; | |
1da177e4 | 5990 | |
9e5987a7 DC |
5991 | bmapi_flags = xfs_bmapi_aflag(whichfork); |
5992 | if (!(iflags & BMV_IF_PREALLOC)) | |
5993 | bmapi_flags |= XFS_BMAPI_IGSTATE; | |
5994 | ||
5995 | /* | |
5996 | * Allocate enough space to handle "subnex" maps at a time. | |
5997 | */ | |
5998 | error = ENOMEM; | |
5999 | subnex = 16; | |
6000 | map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); | |
6001 | if (!map) | |
6002 | goto out_unlock_ilock; | |
6003 | ||
6004 | bmv->bmv_entries = 0; | |
6005 | ||
6006 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && | |
6007 | (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { | |
6008 | error = 0; | |
6009 | goto out_free_map; | |
1da177e4 LT |
6010 | } |
6011 | ||
9e5987a7 | 6012 | nexleft = nex; |
1da177e4 | 6013 | |
9e5987a7 DC |
6014 | do { |
6015 | nmap = (nexleft > subnex) ? subnex : nexleft; | |
6016 | error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), | |
6017 | XFS_BB_TO_FSB(mp, bmv->bmv_length), | |
6018 | map, &nmap, bmapi_flags); | |
6019 | if (error) | |
6020 | goto out_free_map; | |
6021 | ASSERT(nmap <= subnex); | |
1da177e4 | 6022 | |
9e5987a7 DC |
6023 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
6024 | out[cur_ext].bmv_oflags = 0; | |
6025 | if (map[i].br_state == XFS_EXT_UNWRITTEN) | |
6026 | out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; | |
6027 | else if (map[i].br_startblock == DELAYSTARTBLOCK) | |
6028 | out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; | |
6029 | out[cur_ext].bmv_offset = | |
6030 | XFS_FSB_TO_BB(mp, map[i].br_startoff); | |
6031 | out[cur_ext].bmv_length = | |
6032 | XFS_FSB_TO_BB(mp, map[i].br_blockcount); | |
6033 | out[cur_ext].bmv_unused1 = 0; | |
6034 | out[cur_ext].bmv_unused2 = 0; | |
1da177e4 | 6035 | |
9e5987a7 DC |
6036 | /* |
6037 | * delayed allocation extents that start beyond EOF can | |
6038 | * occur due to speculative EOF allocation when the | |
6039 | * delalloc extent is larger than the largest freespace | |
6040 | * extent at conversion time. These extents cannot be | |
6041 | * converted by data writeback, so can exist here even | |
6042 | * if we are not supposed to be finding delalloc | |
6043 | * extents. | |
6044 | */ | |
6045 | if (map[i].br_startblock == DELAYSTARTBLOCK && | |
6046 | map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip))) | |
6047 | ASSERT((iflags & BMV_IF_DELALLOC) != 0); | |
1da177e4 | 6048 | |
9e5987a7 DC |
6049 | if (map[i].br_startblock == HOLESTARTBLOCK && |
6050 | whichfork == XFS_ATTR_FORK) { | |
6051 | /* came to the end of attribute fork */ | |
6052 | out[cur_ext].bmv_oflags |= BMV_OF_LAST; | |
6053 | goto out_free_map; | |
6054 | } | |
1da177e4 | 6055 | |
9e5987a7 DC |
6056 | if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], |
6057 | prealloced, bmvend, | |
6058 | map[i].br_startblock)) | |
6059 | goto out_free_map; | |
1da177e4 | 6060 | |
9e5987a7 DC |
6061 | bmv->bmv_offset = |
6062 | out[cur_ext].bmv_offset + | |
6063 | out[cur_ext].bmv_length; | |
6064 | bmv->bmv_length = | |
6065 | max_t(__int64_t, 0, bmvend - bmv->bmv_offset); | |
91e11088 | 6066 | |
9e5987a7 DC |
6067 | /* |
6068 | * In case we don't want to return the hole, | |
6069 | * don't increase cur_ext so that we can reuse | |
6070 | * it in the next loop. | |
6071 | */ | |
6072 | if ((iflags & BMV_IF_NO_HOLES) && | |
6073 | map[i].br_startblock == HOLESTARTBLOCK) { | |
6074 | memset(&out[cur_ext], 0, sizeof(out[cur_ext])); | |
6075 | continue; | |
6076 | } | |
91e11088 | 6077 | |
9e5987a7 DC |
6078 | nexleft--; |
6079 | bmv->bmv_entries++; | |
6080 | cur_ext++; | |
6081 | } | |
6082 | } while (nmap && nexleft && bmv->bmv_length); | |
6083 | ||
6084 | out_free_map: | |
6085 | kmem_free(map); | |
6086 | out_unlock_ilock: | |
6087 | xfs_iunlock_map_shared(ip, lock); | |
6088 | out_unlock_iolock: | |
6089 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); | |
6090 | ||
6091 | for (i = 0; i < cur_ext; i++) { | |
6092 | int full = 0; /* user array is full */ | |
6093 | ||
6094 | /* format results & advance arg */ | |
6095 | error = formatter(&arg, &out[i], &full); | |
6096 | if (error || full) | |
6097 | break; | |
4eea22f0 | 6098 | } |
9e5987a7 DC |
6099 | |
6100 | if (is_vmalloc_addr(out)) | |
6101 | kmem_free_large(out); | |
6102 | else | |
6103 | kmem_free(out); | |
6104 | return error; | |
1da177e4 | 6105 | } |
c726de44 DC |
6106 | |
6107 | /* | |
6108 | * dead simple method of punching delalyed allocation blocks from a range in | |
6109 | * the inode. Walks a block at a time so will be slow, but is only executed in | |
6110 | * rare error cases so the overhead is not critical. This will alays punch out | |
6111 | * both the start and end blocks, even if the ranges only partially overlap | |
6112 | * them, so it is up to the caller to ensure that partial blocks are not | |
6113 | * passed in. | |
6114 | */ | |
6115 | int | |
6116 | xfs_bmap_punch_delalloc_range( | |
6117 | struct xfs_inode *ip, | |
6118 | xfs_fileoff_t start_fsb, | |
6119 | xfs_fileoff_t length) | |
6120 | { | |
6121 | xfs_fileoff_t remaining = length; | |
6122 | int error = 0; | |
6123 | ||
6124 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); | |
6125 | ||
6126 | do { | |
6127 | int done; | |
6128 | xfs_bmbt_irec_t imap; | |
6129 | int nimaps = 1; | |
6130 | xfs_fsblock_t firstblock; | |
6131 | xfs_bmap_free_t flist; | |
6132 | ||
6133 | /* | |
6134 | * Map the range first and check that it is a delalloc extent | |
6135 | * before trying to unmap the range. Otherwise we will be | |
6136 | * trying to remove a real extent (which requires a | |
6137 | * transaction) or a hole, which is probably a bad idea... | |
6138 | */ | |
5c8ed202 DC |
6139 | error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps, |
6140 | XFS_BMAPI_ENTIRE); | |
c726de44 DC |
6141 | |
6142 | if (error) { | |
6143 | /* something screwed, just bail */ | |
6144 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { | |
53487786 | 6145 | xfs_alert(ip->i_mount, |
c726de44 DC |
6146 | "Failed delalloc mapping lookup ino %lld fsb %lld.", |
6147 | ip->i_ino, start_fsb); | |
6148 | } | |
6149 | break; | |
6150 | } | |
6151 | if (!nimaps) { | |
6152 | /* nothing there */ | |
6153 | goto next_block; | |
6154 | } | |
6155 | if (imap.br_startblock != DELAYSTARTBLOCK) { | |
6156 | /* been converted, ignore */ | |
6157 | goto next_block; | |
6158 | } | |
6159 | WARN_ON(imap.br_blockcount == 0); | |
6160 | ||
6161 | /* | |
6162 | * Note: while we initialise the firstblock/flist pair, they | |
6163 | * should never be used because blocks should never be | |
6164 | * allocated or freed for a delalloc extent and hence we need | |
6165 | * don't cancel or finish them after the xfs_bunmapi() call. | |
6166 | */ | |
6167 | xfs_bmap_init(&flist, &firstblock); | |
6168 | error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock, | |
6169 | &flist, &done); | |
6170 | if (error) | |
6171 | break; | |
6172 | ||
6173 | ASSERT(!flist.xbf_count && !flist.xbf_first); | |
6174 | next_block: | |
6175 | start_fsb++; | |
6176 | remaining--; | |
6177 | } while(remaining > 0); | |
6178 | ||
6179 | return error; | |
6180 | } |