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