]>
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" |
1da177e4 | 19 | #include "xfs_fs.h" |
a844f451 | 20 | #include "xfs_bit.h" |
1da177e4 | 21 | #include "xfs_log.h" |
a844f451 | 22 | #include "xfs_inum.h" |
1da177e4 LT |
23 | #include "xfs_trans.h" |
24 | #include "xfs_sb.h" | |
25 | #include "xfs_ag.h" | |
1da177e4 LT |
26 | #include "xfs_dir2.h" |
27 | #include "xfs_alloc.h" | |
28 | #include "xfs_dmapi.h" | |
29 | #include "xfs_quota.h" | |
30 | #include "xfs_mount.h" | |
1da177e4 | 31 | #include "xfs_bmap_btree.h" |
a844f451 | 32 | #include "xfs_alloc_btree.h" |
1da177e4 | 33 | #include "xfs_ialloc_btree.h" |
1da177e4 | 34 | #include "xfs_dir2_sf.h" |
a844f451 | 35 | #include "xfs_attr_sf.h" |
1da177e4 LT |
36 | #include "xfs_dinode.h" |
37 | #include "xfs_inode.h" | |
a844f451 NS |
38 | #include "xfs_ialloc.h" |
39 | #include "xfs_btree.h" | |
1da177e4 | 40 | #include "xfs_bmap.h" |
1da177e4 LT |
41 | #include "xfs_rtalloc.h" |
42 | #include "xfs_error.h" | |
43 | #include "xfs_itable.h" | |
44 | #include "xfs_rw.h" | |
1da177e4 LT |
45 | #include "xfs_attr.h" |
46 | #include "xfs_buf_item.h" | |
47 | #include "xfs_trans_space.h" | |
48 | #include "xfs_utils.h" | |
49 | #include "xfs_iomap.h" | |
0b1b213f | 50 | #include "xfs_trace.h" |
1da177e4 | 51 | |
1da177e4 LT |
52 | |
53 | #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \ | |
54 | << mp->m_writeio_log) | |
55 | #define XFS_STRAT_WRITE_IMAPS 2 | |
56 | #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP | |
57 | ||
826bf0ad | 58 | STATIC void |
1da177e4 | 59 | xfs_imap_to_bmap( |
541d7d3c | 60 | xfs_inode_t *ip, |
1da177e4 LT |
61 | xfs_off_t offset, |
62 | xfs_bmbt_irec_t *imap, | |
63 | xfs_iomap_t *iomapp, | |
64 | int imaps, /* Number of imap entries */ | |
1da177e4 LT |
65 | int flags) |
66 | { | |
1da177e4 LT |
67 | xfs_fsblock_t start_block; |
68 | ||
8699bb0a CH |
69 | iomapp->iomap_offset = imap->br_startoff; |
70 | iomapp->iomap_bsize = imap->br_blockcount; | |
826bf0ad | 71 | iomapp->iomap_flags = flags; |
1da177e4 | 72 | |
826bf0ad CH |
73 | start_block = imap->br_startblock; |
74 | if (start_block == HOLESTARTBLOCK) { | |
75 | iomapp->iomap_bn = IOMAP_DADDR_NULL; | |
76 | iomapp->iomap_flags |= IOMAP_HOLE; | |
77 | } else if (start_block == DELAYSTARTBLOCK) { | |
78 | iomapp->iomap_bn = IOMAP_DADDR_NULL; | |
79 | iomapp->iomap_flags |= IOMAP_DELAY; | |
80 | } else { | |
81 | iomapp->iomap_bn = xfs_fsb_to_db(ip, start_block); | |
82 | if (ISUNWRITTEN(imap)) | |
83 | iomapp->iomap_flags |= IOMAP_UNWRITTEN; | |
1da177e4 | 84 | } |
1da177e4 LT |
85 | } |
86 | ||
87 | int | |
88 | xfs_iomap( | |
541d7d3c | 89 | xfs_inode_t *ip, |
1da177e4 LT |
90 | xfs_off_t offset, |
91 | ssize_t count, | |
92 | int flags, | |
93 | xfs_iomap_t *iomapp, | |
94 | int *niomaps) | |
95 | { | |
541d7d3c | 96 | xfs_mount_t *mp = ip->i_mount; |
1da177e4 LT |
97 | xfs_fileoff_t offset_fsb, end_fsb; |
98 | int error = 0; | |
99 | int lockmode = 0; | |
100 | xfs_bmbt_irec_t imap; | |
101 | int nimaps = 1; | |
102 | int bmapi_flags = 0; | |
103 | int iomap_flags = 0; | |
104 | ||
541d7d3c | 105 | ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG); |
826bf0ad | 106 | ASSERT(niomaps && *niomaps == 1); |
541d7d3c | 107 | |
1da177e4 LT |
108 | if (XFS_FORCED_SHUTDOWN(mp)) |
109 | return XFS_ERROR(EIO); | |
110 | ||
0b1b213f CH |
111 | trace_xfs_iomap_enter(ip, offset, count, flags, NULL); |
112 | ||
7642861b | 113 | switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) { |
1da177e4 | 114 | case BMAPI_READ: |
541d7d3c | 115 | lockmode = xfs_ilock_map_shared(ip); |
1da177e4 | 116 | bmapi_flags = XFS_BMAPI_ENTIRE; |
1da177e4 LT |
117 | break; |
118 | case BMAPI_WRITE: | |
579aa9ca | 119 | lockmode = XFS_ILOCK_EXCL; |
c31e8878 NS |
120 | if (flags & BMAPI_IGNSTATE) |
121 | bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE; | |
541d7d3c | 122 | xfs_ilock(ip, lockmode); |
1da177e4 LT |
123 | break; |
124 | case BMAPI_ALLOCATE: | |
579aa9ca | 125 | lockmode = XFS_ILOCK_SHARED; |
1da177e4 | 126 | bmapi_flags = XFS_BMAPI_ENTIRE; |
541d7d3c | 127 | |
1da177e4 LT |
128 | /* Attempt non-blocking lock */ |
129 | if (flags & BMAPI_TRYLOCK) { | |
541d7d3c | 130 | if (!xfs_ilock_nowait(ip, lockmode)) |
1da177e4 LT |
131 | return XFS_ERROR(EAGAIN); |
132 | } else { | |
541d7d3c | 133 | xfs_ilock(ip, lockmode); |
1da177e4 LT |
134 | } |
135 | break; | |
1da177e4 LT |
136 | default: |
137 | BUG(); | |
138 | } | |
139 | ||
140 | ASSERT(offset <= mp->m_maxioffset); | |
141 | if ((xfs_fsize_t)offset + count > mp->m_maxioffset) | |
142 | count = mp->m_maxioffset - offset; | |
143 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); | |
144 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
145 | ||
541d7d3c | 146 | error = xfs_bmapi(NULL, ip, offset_fsb, |
1da177e4 LT |
147 | (xfs_filblks_t)(end_fsb - offset_fsb), |
148 | bmapi_flags, NULL, 0, &imap, | |
3e57ecf6 | 149 | &nimaps, NULL, NULL); |
1da177e4 LT |
150 | |
151 | if (error) | |
152 | goto out; | |
153 | ||
7642861b | 154 | switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) { |
1da177e4 LT |
155 | case BMAPI_WRITE: |
156 | /* If we found an extent, return it */ | |
68d1498c | 157 | if (nimaps && |
dd9f438e | 158 | (imap.br_startblock != HOLESTARTBLOCK) && |
68d1498c | 159 | (imap.br_startblock != DELAYSTARTBLOCK)) { |
0b1b213f | 160 | trace_xfs_iomap_found(ip, offset, count, flags, &imap); |
1da177e4 LT |
161 | break; |
162 | } | |
163 | ||
164 | if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) { | |
541d7d3c LM |
165 | error = xfs_iomap_write_direct(ip, offset, count, flags, |
166 | &imap, &nimaps, nimaps); | |
1da177e4 | 167 | } else { |
541d7d3c LM |
168 | error = xfs_iomap_write_delay(ip, offset, count, flags, |
169 | &imap, &nimaps); | |
1da177e4 LT |
170 | } |
171 | if (!error) { | |
0b1b213f | 172 | trace_xfs_iomap_alloc(ip, offset, count, flags, &imap); |
1da177e4 LT |
173 | } |
174 | iomap_flags = IOMAP_NEW; | |
175 | break; | |
176 | case BMAPI_ALLOCATE: | |
177 | /* If we found an extent, return it */ | |
541d7d3c | 178 | xfs_iunlock(ip, lockmode); |
1da177e4 LT |
179 | lockmode = 0; |
180 | ||
9d87c319 | 181 | if (nimaps && !isnullstartblock(imap.br_startblock)) { |
0b1b213f | 182 | trace_xfs_iomap_found(ip, offset, count, flags, &imap); |
1da177e4 LT |
183 | break; |
184 | } | |
185 | ||
541d7d3c | 186 | error = xfs_iomap_write_allocate(ip, offset, count, |
24e17b5f | 187 | &imap, &nimaps); |
1da177e4 | 188 | break; |
1da177e4 LT |
189 | } |
190 | ||
826bf0ad CH |
191 | ASSERT(nimaps <= 1); |
192 | ||
193 | if (nimaps) | |
194 | xfs_imap_to_bmap(ip, offset, &imap, iomapp, nimaps, iomap_flags); | |
195 | *niomaps = nimaps; | |
1da177e4 LT |
196 | |
197 | out: | |
198 | if (lockmode) | |
541d7d3c | 199 | xfs_iunlock(ip, lockmode); |
1da177e4 LT |
200 | return XFS_ERROR(error); |
201 | } | |
202 | ||
541d7d3c | 203 | |
dd9f438e NS |
204 | STATIC int |
205 | xfs_iomap_eof_align_last_fsb( | |
206 | xfs_mount_t *mp, | |
541d7d3c | 207 | xfs_inode_t *ip, |
dd9f438e NS |
208 | xfs_extlen_t extsize, |
209 | xfs_fileoff_t *last_fsb) | |
210 | { | |
211 | xfs_fileoff_t new_last_fsb = 0; | |
212 | xfs_extlen_t align; | |
213 | int eof, error; | |
214 | ||
71ddabb9 | 215 | if (XFS_IS_REALTIME_INODE(ip)) |
dd9f438e NS |
216 | ; |
217 | /* | |
218 | * If mounted with the "-o swalloc" option, roundup the allocation | |
219 | * request to a stripe width boundary if the file size is >= | |
220 | * stripe width and we are allocating past the allocation eof. | |
221 | */ | |
222 | else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) && | |
9f6c92b9 | 223 | (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth))) |
dd9f438e NS |
224 | new_last_fsb = roundup_64(*last_fsb, mp->m_swidth); |
225 | /* | |
226 | * Roundup the allocation request to a stripe unit (m_dalign) boundary | |
227 | * if the file size is >= stripe unit size, and we are allocating past | |
228 | * the allocation eof. | |
229 | */ | |
9f6c92b9 | 230 | else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign))) |
dd9f438e NS |
231 | new_last_fsb = roundup_64(*last_fsb, mp->m_dalign); |
232 | ||
233 | /* | |
234 | * Always round up the allocation request to an extent boundary | |
235 | * (when file on a real-time subvolume or has di_extsize hint). | |
236 | */ | |
237 | if (extsize) { | |
238 | if (new_last_fsb) | |
239 | align = roundup_64(new_last_fsb, extsize); | |
240 | else | |
241 | align = extsize; | |
242 | new_last_fsb = roundup_64(*last_fsb, align); | |
243 | } | |
244 | ||
245 | if (new_last_fsb) { | |
541d7d3c | 246 | error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof); |
dd9f438e NS |
247 | if (error) |
248 | return error; | |
249 | if (eof) | |
250 | *last_fsb = new_last_fsb; | |
251 | } | |
252 | return 0; | |
253 | } | |
254 | ||
572d95f4 NS |
255 | STATIC int |
256 | xfs_cmn_err_fsblock_zero( | |
257 | xfs_inode_t *ip, | |
258 | xfs_bmbt_irec_t *imap) | |
259 | { | |
260 | xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount, | |
261 | "Access to block zero in inode %llu " | |
262 | "start_block: %llx start_off: %llx " | |
263 | "blkcnt: %llx extent-state: %x\n", | |
264 | (unsigned long long)ip->i_ino, | |
265 | (unsigned long long)imap->br_startblock, | |
266 | (unsigned long long)imap->br_startoff, | |
267 | (unsigned long long)imap->br_blockcount, | |
268 | imap->br_state); | |
269 | return EFSCORRUPTED; | |
270 | } | |
271 | ||
1da177e4 LT |
272 | int |
273 | xfs_iomap_write_direct( | |
274 | xfs_inode_t *ip, | |
f403b7f4 | 275 | xfs_off_t offset, |
1da177e4 LT |
276 | size_t count, |
277 | int flags, | |
278 | xfs_bmbt_irec_t *ret_imap, | |
279 | int *nmaps, | |
280 | int found) | |
281 | { | |
282 | xfs_mount_t *mp = ip->i_mount; | |
1da177e4 LT |
283 | xfs_fileoff_t offset_fsb; |
284 | xfs_fileoff_t last_fsb; | |
dd9f438e | 285 | xfs_filblks_t count_fsb, resaligned; |
1da177e4 | 286 | xfs_fsblock_t firstfsb; |
dd9f438e | 287 | xfs_extlen_t extsz, temp; |
0116d935 | 288 | int nimaps; |
1da177e4 | 289 | int bmapi_flag; |
06d10dd9 | 290 | int quota_flag; |
1da177e4 LT |
291 | int rt; |
292 | xfs_trans_t *tp; | |
0116d935 | 293 | xfs_bmbt_irec_t imap; |
1da177e4 | 294 | xfs_bmap_free_t free_list; |
dd9f438e | 295 | uint qblocks, resblks, resrtextents; |
1da177e4 | 296 | int committed; |
dd9f438e | 297 | int error; |
1da177e4 LT |
298 | |
299 | /* | |
300 | * Make sure that the dquots are there. This doesn't hold | |
301 | * the ilock across a disk read. | |
302 | */ | |
7d095257 | 303 | error = xfs_qm_dqattach_locked(ip, 0); |
1da177e4 LT |
304 | if (error) |
305 | return XFS_ERROR(error); | |
306 | ||
dd9f438e | 307 | rt = XFS_IS_REALTIME_INODE(ip); |
957d0ebe | 308 | extsz = xfs_get_extsz_hint(ip); |
1da177e4 | 309 | |
957d0ebe DC |
310 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
311 | last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count))); | |
9f6c92b9 LM |
312 | if ((offset + count) > ip->i_size) { |
313 | error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb); | |
dd9f438e NS |
314 | if (error) |
315 | goto error_out; | |
1da177e4 | 316 | } else { |
dd9f438e NS |
317 | if (found && (ret_imap->br_startblock == HOLESTARTBLOCK)) |
318 | last_fsb = MIN(last_fsb, (xfs_fileoff_t) | |
319 | ret_imap->br_blockcount + | |
320 | ret_imap->br_startoff); | |
1da177e4 | 321 | } |
dd9f438e NS |
322 | count_fsb = last_fsb - offset_fsb; |
323 | ASSERT(count_fsb > 0); | |
324 | ||
325 | resaligned = count_fsb; | |
326 | if (unlikely(extsz)) { | |
327 | if ((temp = do_mod(offset_fsb, extsz))) | |
328 | resaligned += temp; | |
329 | if ((temp = do_mod(resaligned, extsz))) | |
330 | resaligned += extsz - temp; | |
331 | } | |
332 | ||
333 | if (unlikely(rt)) { | |
334 | resrtextents = qblocks = resaligned; | |
335 | resrtextents /= mp->m_sb.sb_rextsize; | |
84e1e99f DC |
336 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
337 | quota_flag = XFS_QMOPT_RES_RTBLKS; | |
338 | } else { | |
339 | resrtextents = 0; | |
dd9f438e | 340 | resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned); |
84e1e99f DC |
341 | quota_flag = XFS_QMOPT_RES_REGBLKS; |
342 | } | |
1da177e4 LT |
343 | |
344 | /* | |
06d10dd9 | 345 | * Allocate and setup the transaction |
1da177e4 LT |
346 | */ |
347 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
348 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); | |
1da177e4 | 349 | error = xfs_trans_reserve(tp, resblks, |
d52b44d0 | 350 | XFS_WRITE_LOG_RES(mp), resrtextents, |
1da177e4 LT |
351 | XFS_TRANS_PERM_LOG_RES, |
352 | XFS_WRITE_LOG_COUNT); | |
1da177e4 | 353 | /* |
06d10dd9 | 354 | * Check for running out of space, note: need lock to return |
1da177e4 LT |
355 | */ |
356 | if (error) | |
1da177e4 | 357 | xfs_trans_cancel(tp, 0); |
1da177e4 | 358 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
1da177e4 | 359 | if (error) |
06d10dd9 | 360 | goto error_out; |
1da177e4 | 361 | |
7d095257 | 362 | error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag); |
dd9f438e | 363 | if (error) |
1da177e4 | 364 | goto error1; |
1da177e4 | 365 | |
1da177e4 LT |
366 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
367 | xfs_trans_ihold(tp, ip); | |
368 | ||
dd9f438e | 369 | bmapi_flag = XFS_BMAPI_WRITE; |
ba87ea69 | 370 | if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz)) |
1da177e4 LT |
371 | bmapi_flag |= XFS_BMAPI_PREALLOC; |
372 | ||
373 | /* | |
dd9f438e | 374 | * Issue the xfs_bmapi() call to allocate the blocks |
1da177e4 | 375 | */ |
9d87c319 | 376 | xfs_bmap_init(&free_list, &firstfsb); |
06d10dd9 | 377 | nimaps = 1; |
541d7d3c | 378 | error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag, |
3e57ecf6 | 379 | &firstfsb, 0, &imap, &nimaps, &free_list, NULL); |
06d10dd9 | 380 | if (error) |
1da177e4 | 381 | goto error0; |
1da177e4 LT |
382 | |
383 | /* | |
06d10dd9 | 384 | * Complete the transaction |
1da177e4 | 385 | */ |
f7c99b6f | 386 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
06d10dd9 | 387 | if (error) |
1da177e4 | 388 | goto error0; |
1c72bf90 | 389 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
06d10dd9 | 390 | if (error) |
1da177e4 | 391 | goto error_out; |
1da177e4 | 392 | |
06d10dd9 NS |
393 | /* |
394 | * Copy any maps to caller's array and return any error. | |
395 | */ | |
1da177e4 | 396 | if (nimaps == 0) { |
572d95f4 NS |
397 | error = ENOSPC; |
398 | goto error_out; | |
399 | } | |
400 | ||
86c4d623 | 401 | if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) { |
572d95f4 | 402 | error = xfs_cmn_err_fsblock_zero(ip, &imap); |
1da177e4 LT |
403 | goto error_out; |
404 | } | |
405 | ||
0116d935 | 406 | *ret_imap = imap; |
1da177e4 | 407 | *nmaps = 1; |
1da177e4 LT |
408 | return 0; |
409 | ||
06d10dd9 | 410 | error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */ |
1da177e4 | 411 | xfs_bmap_cancel(&free_list); |
7d095257 | 412 | xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag); |
1da177e4 | 413 | |
06d10dd9 | 414 | error1: /* Just cancel transaction */ |
1da177e4 LT |
415 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
416 | *nmaps = 0; /* nothing set-up here */ | |
417 | ||
418 | error_out: | |
419 | return XFS_ERROR(error); | |
420 | } | |
421 | ||
dd9f438e | 422 | /* |
8de2bf93 DC |
423 | * If the caller is doing a write at the end of the file, then extend the |
424 | * allocation out to the file system's write iosize. We clean up any extra | |
425 | * space left over when the file is closed in xfs_inactive(). | |
dd9f438e NS |
426 | */ |
427 | STATIC int | |
428 | xfs_iomap_eof_want_preallocate( | |
429 | xfs_mount_t *mp, | |
541d7d3c | 430 | xfs_inode_t *ip, |
dd9f438e NS |
431 | xfs_off_t offset, |
432 | size_t count, | |
433 | int ioflag, | |
434 | xfs_bmbt_irec_t *imap, | |
435 | int nimaps, | |
436 | int *prealloc) | |
437 | { | |
438 | xfs_fileoff_t start_fsb; | |
439 | xfs_filblks_t count_fsb; | |
440 | xfs_fsblock_t firstblock; | |
441 | int n, error, imaps; | |
442 | ||
443 | *prealloc = 0; | |
8de2bf93 | 444 | if ((offset + count) <= ip->i_size) |
dd9f438e NS |
445 | return 0; |
446 | ||
447 | /* | |
448 | * If there are any real blocks past eof, then don't | |
449 | * do any speculative allocation. | |
450 | */ | |
451 | start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1))); | |
452 | count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); | |
453 | while (count_fsb > 0) { | |
454 | imaps = nimaps; | |
3ddb8fa9 | 455 | firstblock = NULLFSBLOCK; |
541d7d3c | 456 | error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0, |
3e57ecf6 | 457 | &firstblock, 0, imap, &imaps, NULL, NULL); |
dd9f438e NS |
458 | if (error) |
459 | return error; | |
460 | for (n = 0; n < imaps; n++) { | |
461 | if ((imap[n].br_startblock != HOLESTARTBLOCK) && | |
462 | (imap[n].br_startblock != DELAYSTARTBLOCK)) | |
463 | return 0; | |
464 | start_fsb += imap[n].br_blockcount; | |
465 | count_fsb -= imap[n].br_blockcount; | |
466 | } | |
467 | } | |
468 | *prealloc = 1; | |
469 | return 0; | |
470 | } | |
471 | ||
1da177e4 LT |
472 | int |
473 | xfs_iomap_write_delay( | |
474 | xfs_inode_t *ip, | |
f403b7f4 | 475 | xfs_off_t offset, |
1da177e4 LT |
476 | size_t count, |
477 | int ioflag, | |
478 | xfs_bmbt_irec_t *ret_imap, | |
479 | int *nmaps) | |
480 | { | |
481 | xfs_mount_t *mp = ip->i_mount; | |
1da177e4 LT |
482 | xfs_fileoff_t offset_fsb; |
483 | xfs_fileoff_t last_fsb; | |
dd9f438e NS |
484 | xfs_off_t aligned_offset; |
485 | xfs_fileoff_t ioalign; | |
1da177e4 | 486 | xfs_fsblock_t firstblock; |
dd9f438e | 487 | xfs_extlen_t extsz; |
1da177e4 | 488 | int nimaps; |
1da177e4 | 489 | xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS]; |
8de2bf93 | 490 | int prealloc, flushed = 0; |
dd9f438e | 491 | int error; |
1da177e4 | 492 | |
579aa9ca | 493 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 LT |
494 | |
495 | /* | |
496 | * Make sure that the dquots are there. This doesn't hold | |
497 | * the ilock across a disk read. | |
498 | */ | |
7d095257 | 499 | error = xfs_qm_dqattach_locked(ip, 0); |
1da177e4 LT |
500 | if (error) |
501 | return XFS_ERROR(error); | |
502 | ||
957d0ebe | 503 | extsz = xfs_get_extsz_hint(ip); |
dd9f438e NS |
504 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
505 | ||
9f6c92b9 | 506 | error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count, |
dd9f438e NS |
507 | ioflag, imap, XFS_WRITE_IMAPS, &prealloc); |
508 | if (error) | |
509 | return error; | |
1da177e4 | 510 | |
8de2bf93 | 511 | retry: |
dd9f438e | 512 | if (prealloc) { |
1da177e4 LT |
513 | aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1)); |
514 | ioalign = XFS_B_TO_FSBT(mp, aligned_offset); | |
dd9f438e NS |
515 | last_fsb = ioalign + mp->m_writeio_blocks; |
516 | } else { | |
517 | last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count))); | |
1da177e4 | 518 | } |
1da177e4 | 519 | |
dd9f438e | 520 | if (prealloc || extsz) { |
9f6c92b9 | 521 | error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb); |
dd9f438e | 522 | if (error) |
1da177e4 | 523 | return error; |
1da177e4 | 524 | } |
dd9f438e NS |
525 | |
526 | nimaps = XFS_WRITE_IMAPS; | |
527 | firstblock = NULLFSBLOCK; | |
541d7d3c | 528 | error = xfs_bmapi(NULL, ip, offset_fsb, |
1da177e4 LT |
529 | (xfs_filblks_t)(last_fsb - offset_fsb), |
530 | XFS_BMAPI_DELAY | XFS_BMAPI_WRITE | | |
531 | XFS_BMAPI_ENTIRE, &firstblock, 1, imap, | |
3e57ecf6 | 532 | &nimaps, NULL, NULL); |
dd9f438e | 533 | if (error && (error != ENOSPC)) |
1da177e4 | 534 | return XFS_ERROR(error); |
dd9f438e | 535 | |
1da177e4 LT |
536 | /* |
537 | * If bmapi returned us nothing, and if we didn't get back EDQUOT, | |
8de2bf93 DC |
538 | * then we must have run out of space - flush all other inodes with |
539 | * delalloc blocks and retry without EOF preallocation. | |
1da177e4 LT |
540 | */ |
541 | if (nimaps == 0) { | |
0b1b213f | 542 | trace_xfs_delalloc_enospc(ip, offset, count); |
8de2bf93 | 543 | if (flushed) |
1da177e4 LT |
544 | return XFS_ERROR(ENOSPC); |
545 | ||
8de2bf93 DC |
546 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
547 | xfs_flush_inodes(ip); | |
548 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
549 | ||
550 | flushed = 1; | |
1da177e4 | 551 | error = 0; |
8de2bf93 | 552 | prealloc = 0; |
1da177e4 LT |
553 | goto retry; |
554 | } | |
555 | ||
86c4d623 | 556 | if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip))) |
572d95f4 | 557 | return xfs_cmn_err_fsblock_zero(ip, &imap[0]); |
dd9f438e NS |
558 | |
559 | *ret_imap = imap[0]; | |
560 | *nmaps = 1; | |
561 | ||
1da177e4 LT |
562 | return 0; |
563 | } | |
564 | ||
565 | /* | |
566 | * Pass in a delayed allocate extent, convert it to real extents; | |
567 | * return to the caller the extent we create which maps on top of | |
568 | * the originating callers request. | |
569 | * | |
570 | * Called without a lock on the inode. | |
e4143a1c DC |
571 | * |
572 | * We no longer bother to look at the incoming map - all we have to | |
573 | * guarantee is that whatever we allocate fills the required range. | |
1da177e4 LT |
574 | */ |
575 | int | |
576 | xfs_iomap_write_allocate( | |
577 | xfs_inode_t *ip, | |
f403b7f4 | 578 | xfs_off_t offset, |
24e17b5f | 579 | size_t count, |
1da177e4 LT |
580 | xfs_bmbt_irec_t *map, |
581 | int *retmap) | |
582 | { | |
583 | xfs_mount_t *mp = ip->i_mount; | |
1da177e4 LT |
584 | xfs_fileoff_t offset_fsb, last_block; |
585 | xfs_fileoff_t end_fsb, map_start_fsb; | |
586 | xfs_fsblock_t first_block; | |
587 | xfs_bmap_free_t free_list; | |
588 | xfs_filblks_t count_fsb; | |
e4143a1c | 589 | xfs_bmbt_irec_t imap; |
1da177e4 | 590 | xfs_trans_t *tp; |
e4143a1c | 591 | int nimaps, committed; |
1da177e4 LT |
592 | int error = 0; |
593 | int nres; | |
594 | ||
595 | *retmap = 0; | |
596 | ||
597 | /* | |
598 | * Make sure that the dquots are there. | |
599 | */ | |
7d095257 CH |
600 | error = xfs_qm_dqattach(ip, 0); |
601 | if (error) | |
1da177e4 LT |
602 | return XFS_ERROR(error); |
603 | ||
24e17b5f | 604 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
1da177e4 | 605 | count_fsb = map->br_blockcount; |
24e17b5f | 606 | map_start_fsb = map->br_startoff; |
1da177e4 LT |
607 | |
608 | XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb)); | |
609 | ||
610 | while (count_fsb != 0) { | |
611 | /* | |
612 | * Set up a transaction with which to allocate the | |
613 | * backing store for the file. Do allocations in a | |
614 | * loop until we get some space in the range we are | |
615 | * interested in. The other space that might be allocated | |
616 | * is in the delayed allocation extent on which we sit | |
617 | * but before our buffer starts. | |
618 | */ | |
619 | ||
620 | nimaps = 0; | |
621 | while (nimaps == 0) { | |
622 | tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE); | |
84e1e99f | 623 | tp->t_flags |= XFS_TRANS_RESERVE; |
1da177e4 LT |
624 | nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK); |
625 | error = xfs_trans_reserve(tp, nres, | |
626 | XFS_WRITE_LOG_RES(mp), | |
627 | 0, XFS_TRANS_PERM_LOG_RES, | |
628 | XFS_WRITE_LOG_COUNT); | |
1da177e4 LT |
629 | if (error) { |
630 | xfs_trans_cancel(tp, 0); | |
631 | return XFS_ERROR(error); | |
632 | } | |
633 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
634 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
635 | xfs_trans_ihold(tp, ip); | |
636 | ||
9d87c319 | 637 | xfs_bmap_init(&free_list, &first_block); |
1da177e4 | 638 | |
1da177e4 | 639 | /* |
e4143a1c DC |
640 | * it is possible that the extents have changed since |
641 | * we did the read call as we dropped the ilock for a | |
642 | * while. We have to be careful about truncates or hole | |
643 | * punchs here - we are not allowed to allocate | |
644 | * non-delalloc blocks here. | |
645 | * | |
646 | * The only protection against truncation is the pages | |
647 | * for the range we are being asked to convert are | |
648 | * locked and hence a truncate will block on them | |
649 | * first. | |
650 | * | |
651 | * As a result, if we go beyond the range we really | |
652 | * need and hit an delalloc extent boundary followed by | |
653 | * a hole while we have excess blocks in the map, we | |
654 | * will fill the hole incorrectly and overrun the | |
655 | * transaction reservation. | |
656 | * | |
657 | * Using a single map prevents this as we are forced to | |
658 | * check each map we look for overlap with the desired | |
659 | * range and abort as soon as we find it. Also, given | |
660 | * that we only return a single map, having one beyond | |
661 | * what we can return is probably a bit silly. | |
662 | * | |
663 | * We also need to check that we don't go beyond EOF; | |
664 | * this is a truncate optimisation as a truncate sets | |
665 | * the new file size before block on the pages we | |
666 | * currently have locked under writeback. Because they | |
667 | * are about to be tossed, we don't need to write them | |
668 | * back.... | |
1da177e4 | 669 | */ |
e4143a1c | 670 | nimaps = 1; |
ba87ea69 | 671 | end_fsb = XFS_B_TO_FSB(mp, ip->i_size); |
7c9ef85c DC |
672 | error = xfs_bmap_last_offset(NULL, ip, &last_block, |
673 | XFS_DATA_FORK); | |
674 | if (error) | |
675 | goto trans_cancel; | |
676 | ||
1da177e4 LT |
677 | last_block = XFS_FILEOFF_MAX(last_block, end_fsb); |
678 | if ((map_start_fsb + count_fsb) > last_block) { | |
679 | count_fsb = last_block - map_start_fsb; | |
680 | if (count_fsb == 0) { | |
681 | error = EAGAIN; | |
682 | goto trans_cancel; | |
683 | } | |
684 | } | |
685 | ||
686 | /* Go get the actual blocks */ | |
541d7d3c | 687 | error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb, |
1da177e4 | 688 | XFS_BMAPI_WRITE, &first_block, 1, |
e4143a1c | 689 | &imap, &nimaps, &free_list, NULL); |
1da177e4 LT |
690 | if (error) |
691 | goto trans_cancel; | |
692 | ||
f7c99b6f | 693 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
1da177e4 LT |
694 | if (error) |
695 | goto trans_cancel; | |
696 | ||
1c72bf90 | 697 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
1da177e4 LT |
698 | if (error) |
699 | goto error0; | |
700 | ||
701 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
702 | } | |
703 | ||
704 | /* | |
705 | * See if we were able to allocate an extent that | |
706 | * covers at least part of the callers request | |
707 | */ | |
86c4d623 | 708 | if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) |
e4143a1c | 709 | return xfs_cmn_err_fsblock_zero(ip, &imap); |
86c4d623 | 710 | |
e4143a1c DC |
711 | if ((offset_fsb >= imap.br_startoff) && |
712 | (offset_fsb < (imap.br_startoff + | |
713 | imap.br_blockcount))) { | |
714 | *map = imap; | |
715 | *retmap = 1; | |
716 | XFS_STATS_INC(xs_xstrat_quick); | |
717 | return 0; | |
1da177e4 LT |
718 | } |
719 | ||
e4143a1c DC |
720 | /* |
721 | * So far we have not mapped the requested part of the | |
1da177e4 LT |
722 | * file, just surrounding data, try again. |
723 | */ | |
e4143a1c DC |
724 | count_fsb -= imap.br_blockcount; |
725 | map_start_fsb = imap.br_startoff + imap.br_blockcount; | |
1da177e4 LT |
726 | } |
727 | ||
728 | trans_cancel: | |
729 | xfs_bmap_cancel(&free_list); | |
730 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); | |
731 | error0: | |
732 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
733 | return XFS_ERROR(error); | |
734 | } | |
735 | ||
736 | int | |
737 | xfs_iomap_write_unwritten( | |
738 | xfs_inode_t *ip, | |
f403b7f4 | 739 | xfs_off_t offset, |
1da177e4 LT |
740 | size_t count) |
741 | { | |
742 | xfs_mount_t *mp = ip->i_mount; | |
1da177e4 LT |
743 | xfs_fileoff_t offset_fsb; |
744 | xfs_filblks_t count_fsb; | |
745 | xfs_filblks_t numblks_fsb; | |
dd9f438e NS |
746 | xfs_fsblock_t firstfsb; |
747 | int nimaps; | |
748 | xfs_trans_t *tp; | |
749 | xfs_bmbt_irec_t imap; | |
750 | xfs_bmap_free_t free_list; | |
751 | uint resblks; | |
1da177e4 LT |
752 | int committed; |
753 | int error; | |
1da177e4 | 754 | |
0b1b213f | 755 | trace_xfs_unwritten_convert(ip, offset, count); |
1da177e4 LT |
756 | |
757 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
758 | count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); | |
759 | count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb); | |
760 | ||
4ddd8bb1 LM |
761 | /* |
762 | * Reserve enough blocks in this transaction for two complete extent | |
763 | * btree splits. We may be converting the middle part of an unwritten | |
764 | * extent and in this case we will insert two new extents in the btree | |
765 | * each of which could cause a full split. | |
766 | * | |
767 | * This reservation amount will be used in the first call to | |
768 | * xfs_bmbt_split() to select an AG with enough space to satisfy the | |
769 | * rest of the operation. | |
770 | */ | |
dd9f438e | 771 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1; |
1da177e4 | 772 | |
dd9f438e | 773 | do { |
1da177e4 LT |
774 | /* |
775 | * set up a transaction to convert the range of extents | |
776 | * from unwritten to real. Do allocations in a loop until | |
777 | * we have covered the range passed in. | |
80641dc6 CH |
778 | * |
779 | * Note that we open code the transaction allocation here | |
780 | * to pass KM_NOFS--we can't risk to recursing back into | |
781 | * the filesystem here as we might be asked to write out | |
782 | * the same inode that we complete here and might deadlock | |
783 | * on the iolock. | |
1da177e4 | 784 | */ |
80641dc6 CH |
785 | xfs_wait_for_freeze(mp, SB_FREEZE_TRANS); |
786 | tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS); | |
84e1e99f | 787 | tp->t_flags |= XFS_TRANS_RESERVE; |
dd9f438e | 788 | error = xfs_trans_reserve(tp, resblks, |
1da177e4 LT |
789 | XFS_WRITE_LOG_RES(mp), 0, |
790 | XFS_TRANS_PERM_LOG_RES, | |
791 | XFS_WRITE_LOG_COUNT); | |
792 | if (error) { | |
793 | xfs_trans_cancel(tp, 0); | |
572d95f4 | 794 | return XFS_ERROR(error); |
1da177e4 LT |
795 | } |
796 | ||
797 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
798 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
799 | xfs_trans_ihold(tp, ip); | |
800 | ||
801 | /* | |
802 | * Modify the unwritten extent state of the buffer. | |
803 | */ | |
9d87c319 | 804 | xfs_bmap_init(&free_list, &firstfsb); |
1da177e4 | 805 | nimaps = 1; |
541d7d3c | 806 | error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, |
dd9f438e | 807 | XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb, |
3e57ecf6 | 808 | 1, &imap, &nimaps, &free_list, NULL); |
1da177e4 LT |
809 | if (error) |
810 | goto error_on_bmapi_transaction; | |
811 | ||
f7c99b6f | 812 | error = xfs_bmap_finish(&(tp), &(free_list), &committed); |
1da177e4 LT |
813 | if (error) |
814 | goto error_on_bmapi_transaction; | |
815 | ||
1c72bf90 | 816 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
1da177e4 LT |
817 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
818 | if (error) | |
572d95f4 NS |
819 | return XFS_ERROR(error); |
820 | ||
86c4d623 | 821 | if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) |
572d95f4 | 822 | return xfs_cmn_err_fsblock_zero(ip, &imap); |
1da177e4 LT |
823 | |
824 | if ((numblks_fsb = imap.br_blockcount) == 0) { | |
825 | /* | |
826 | * The numblks_fsb value should always get | |
827 | * smaller, otherwise the loop is stuck. | |
828 | */ | |
829 | ASSERT(imap.br_blockcount); | |
830 | break; | |
831 | } | |
832 | offset_fsb += numblks_fsb; | |
833 | count_fsb -= numblks_fsb; | |
834 | } while (count_fsb > 0); | |
835 | ||
836 | return 0; | |
837 | ||
838 | error_on_bmapi_transaction: | |
839 | xfs_bmap_cancel(&free_list); | |
840 | xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT)); | |
841 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1da177e4 LT |
842 | return XFS_ERROR(error); |
843 | } |