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