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