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