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