]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
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" | |
26 | #include "xfs_dir.h" | |
27 | #include "xfs_dir2.h" | |
28 | #include "xfs_alloc.h" | |
29 | #include "xfs_dmapi.h" | |
30 | #include "xfs_quota.h" | |
31 | #include "xfs_mount.h" | |
1da177e4 | 32 | #include "xfs_bmap_btree.h" |
a844f451 | 33 | #include "xfs_alloc_btree.h" |
1da177e4 | 34 | #include "xfs_ialloc_btree.h" |
1da177e4 LT |
35 | #include "xfs_dir_sf.h" |
36 | #include "xfs_dir2_sf.h" | |
a844f451 | 37 | #include "xfs_attr_sf.h" |
1da177e4 LT |
38 | #include "xfs_dinode.h" |
39 | #include "xfs_inode.h" | |
a844f451 NS |
40 | #include "xfs_ialloc.h" |
41 | #include "xfs_btree.h" | |
1da177e4 LT |
42 | #include "xfs_bmap.h" |
43 | #include "xfs_bit.h" | |
44 | #include "xfs_rtalloc.h" | |
45 | #include "xfs_error.h" | |
46 | #include "xfs_itable.h" | |
47 | #include "xfs_rw.h" | |
48 | #include "xfs_acl.h" | |
49 | #include "xfs_cap.h" | |
50 | #include "xfs_mac.h" | |
51 | #include "xfs_attr.h" | |
52 | #include "xfs_buf_item.h" | |
53 | #include "xfs_trans_space.h" | |
54 | #include "xfs_utils.h" | |
55 | #include "xfs_iomap.h" | |
56 | ||
57 | #if defined(XFS_RW_TRACE) | |
58 | void | |
59 | xfs_iomap_enter_trace( | |
60 | int tag, | |
61 | xfs_iocore_t *io, | |
62 | xfs_off_t offset, | |
63 | ssize_t count) | |
64 | { | |
65 | xfs_inode_t *ip = XFS_IO_INODE(io); | |
66 | ||
67 | if (!ip->i_rwtrace) | |
68 | return; | |
69 | ||
70 | ktrace_enter(ip->i_rwtrace, | |
71 | (void *)((unsigned long)tag), | |
72 | (void *)ip, | |
73 | (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)), | |
74 | (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)), | |
75 | (void *)((unsigned long)((offset >> 32) & 0xffffffff)), | |
76 | (void *)((unsigned long)(offset & 0xffffffff)), | |
77 | (void *)((unsigned long)count), | |
78 | (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)), | |
79 | (void *)((unsigned long)(io->io_new_size & 0xffffffff)), | |
80 | (void *)NULL, | |
81 | (void *)NULL, | |
82 | (void *)NULL, | |
83 | (void *)NULL, | |
84 | (void *)NULL, | |
85 | (void *)NULL, | |
86 | (void *)NULL); | |
87 | } | |
88 | ||
89 | void | |
90 | xfs_iomap_map_trace( | |
91 | int tag, | |
92 | xfs_iocore_t *io, | |
93 | xfs_off_t offset, | |
94 | ssize_t count, | |
95 | xfs_iomap_t *iomapp, | |
96 | xfs_bmbt_irec_t *imapp, | |
97 | int flags) | |
98 | { | |
99 | xfs_inode_t *ip = XFS_IO_INODE(io); | |
100 | ||
101 | if (!ip->i_rwtrace) | |
102 | return; | |
103 | ||
104 | ktrace_enter(ip->i_rwtrace, | |
105 | (void *)((unsigned long)tag), | |
106 | (void *)ip, | |
107 | (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)), | |
108 | (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)), | |
109 | (void *)((unsigned long)((offset >> 32) & 0xffffffff)), | |
110 | (void *)((unsigned long)(offset & 0xffffffff)), | |
111 | (void *)((unsigned long)count), | |
112 | (void *)((unsigned long)flags), | |
113 | (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)), | |
114 | (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)), | |
115 | (void *)((unsigned long)(iomapp->iomap_delta)), | |
116 | (void *)((unsigned long)(iomapp->iomap_bsize)), | |
117 | (void *)((unsigned long)(iomapp->iomap_bn)), | |
118 | (void *)(__psint_t)(imapp->br_startoff), | |
119 | (void *)((unsigned long)(imapp->br_blockcount)), | |
120 | (void *)(__psint_t)(imapp->br_startblock)); | |
121 | } | |
122 | #else | |
123 | #define xfs_iomap_enter_trace(tag, io, offset, count) | |
124 | #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags) | |
125 | #endif | |
126 | ||
127 | #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \ | |
128 | << mp->m_writeio_log) | |
129 | #define XFS_STRAT_WRITE_IMAPS 2 | |
130 | #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP | |
131 | ||
132 | STATIC int | |
133 | xfs_imap_to_bmap( | |
134 | xfs_iocore_t *io, | |
135 | xfs_off_t offset, | |
136 | xfs_bmbt_irec_t *imap, | |
137 | xfs_iomap_t *iomapp, | |
138 | int imaps, /* Number of imap entries */ | |
139 | int iomaps, /* Number of iomap entries */ | |
140 | int flags) | |
141 | { | |
142 | xfs_mount_t *mp; | |
143 | xfs_fsize_t nisize; | |
144 | int pbm; | |
145 | xfs_fsblock_t start_block; | |
146 | ||
147 | mp = io->io_mount; | |
148 | nisize = XFS_SIZE(mp, io); | |
149 | if (io->io_new_size > nisize) | |
150 | nisize = io->io_new_size; | |
151 | ||
152 | for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) { | |
153 | iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff); | |
154 | iomapp->iomap_delta = offset - iomapp->iomap_offset; | |
155 | iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount); | |
156 | iomapp->iomap_flags = flags; | |
157 | ||
158 | if (io->io_flags & XFS_IOCORE_RT) { | |
159 | iomapp->iomap_flags |= IOMAP_REALTIME; | |
160 | iomapp->iomap_target = mp->m_rtdev_targp; | |
161 | } else { | |
162 | iomapp->iomap_target = mp->m_ddev_targp; | |
163 | } | |
164 | start_block = imap->br_startblock; | |
165 | if (start_block == HOLESTARTBLOCK) { | |
166 | iomapp->iomap_bn = IOMAP_DADDR_NULL; | |
167 | iomapp->iomap_flags |= IOMAP_HOLE; | |
168 | } else if (start_block == DELAYSTARTBLOCK) { | |
169 | iomapp->iomap_bn = IOMAP_DADDR_NULL; | |
170 | iomapp->iomap_flags |= IOMAP_DELAY; | |
171 | } else { | |
172 | iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block); | |
173 | if (ISUNWRITTEN(imap)) | |
174 | iomapp->iomap_flags |= IOMAP_UNWRITTEN; | |
175 | } | |
176 | ||
177 | if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) { | |
178 | iomapp->iomap_flags |= IOMAP_EOF; | |
179 | } | |
180 | ||
181 | offset += iomapp->iomap_bsize - iomapp->iomap_delta; | |
182 | } | |
183 | return pbm; /* Return the number filled */ | |
184 | } | |
185 | ||
186 | int | |
187 | xfs_iomap( | |
188 | xfs_iocore_t *io, | |
189 | xfs_off_t offset, | |
190 | ssize_t count, | |
191 | int flags, | |
192 | xfs_iomap_t *iomapp, | |
193 | int *niomaps) | |
194 | { | |
195 | xfs_mount_t *mp = io->io_mount; | |
196 | xfs_fileoff_t offset_fsb, end_fsb; | |
197 | int error = 0; | |
198 | int lockmode = 0; | |
199 | xfs_bmbt_irec_t imap; | |
200 | int nimaps = 1; | |
201 | int bmapi_flags = 0; | |
202 | int iomap_flags = 0; | |
203 | ||
204 | if (XFS_FORCED_SHUTDOWN(mp)) | |
205 | return XFS_ERROR(EIO); | |
206 | ||
207 | switch (flags & | |
208 | (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE | | |
209 | BMAPI_UNWRITTEN | BMAPI_DEVICE)) { | |
210 | case BMAPI_READ: | |
211 | xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count); | |
212 | lockmode = XFS_LCK_MAP_SHARED(mp, io); | |
213 | bmapi_flags = XFS_BMAPI_ENTIRE; | |
1da177e4 LT |
214 | break; |
215 | case BMAPI_WRITE: | |
216 | xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count); | |
217 | lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR; | |
c31e8878 NS |
218 | if (flags & BMAPI_IGNSTATE) |
219 | bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE; | |
1da177e4 LT |
220 | XFS_ILOCK(mp, io, lockmode); |
221 | break; | |
222 | case BMAPI_ALLOCATE: | |
223 | xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count); | |
224 | lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD; | |
225 | bmapi_flags = XFS_BMAPI_ENTIRE; | |
226 | /* Attempt non-blocking lock */ | |
227 | if (flags & BMAPI_TRYLOCK) { | |
228 | if (!XFS_ILOCK_NOWAIT(mp, io, lockmode)) | |
229 | return XFS_ERROR(EAGAIN); | |
230 | } else { | |
231 | XFS_ILOCK(mp, io, lockmode); | |
232 | } | |
233 | break; | |
234 | case BMAPI_UNWRITTEN: | |
235 | goto phase2; | |
236 | case BMAPI_DEVICE: | |
237 | lockmode = XFS_LCK_MAP_SHARED(mp, io); | |
238 | iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ? | |
239 | mp->m_rtdev_targp : mp->m_ddev_targp; | |
240 | error = 0; | |
241 | *niomaps = 1; | |
242 | goto out; | |
243 | default: | |
244 | BUG(); | |
245 | } | |
246 | ||
247 | ASSERT(offset <= mp->m_maxioffset); | |
248 | if ((xfs_fsize_t)offset + count > mp->m_maxioffset) | |
249 | count = mp->m_maxioffset - offset; | |
250 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); | |
251 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
252 | ||
253 | error = XFS_BMAPI(mp, NULL, io, offset_fsb, | |
254 | (xfs_filblks_t)(end_fsb - offset_fsb), | |
255 | bmapi_flags, NULL, 0, &imap, | |
256 | &nimaps, NULL); | |
257 | ||
258 | if (error) | |
259 | goto out; | |
260 | ||
261 | phase2: | |
262 | switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) { | |
263 | case BMAPI_WRITE: | |
264 | /* If we found an extent, return it */ | |
68d1498c RC |
265 | if (nimaps && |
266 | (imap.br_startblock != HOLESTARTBLOCK) && | |
267 | (imap.br_startblock != DELAYSTARTBLOCK)) { | |
1da177e4 LT |
268 | xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io, |
269 | offset, count, iomapp, &imap, flags); | |
270 | break; | |
271 | } | |
272 | ||
273 | if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) { | |
274 | error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset, | |
275 | count, flags, &imap, &nimaps, nimaps); | |
276 | } else { | |
277 | error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count, | |
278 | flags, &imap, &nimaps); | |
279 | } | |
280 | if (!error) { | |
281 | xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io, | |
282 | offset, count, iomapp, &imap, flags); | |
283 | } | |
284 | iomap_flags = IOMAP_NEW; | |
285 | break; | |
286 | case BMAPI_ALLOCATE: | |
287 | /* If we found an extent, return it */ | |
288 | XFS_IUNLOCK(mp, io, lockmode); | |
289 | lockmode = 0; | |
290 | ||
291 | if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) { | |
292 | xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io, | |
293 | offset, count, iomapp, &imap, flags); | |
294 | break; | |
295 | } | |
296 | ||
24e17b5f NS |
297 | error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count, |
298 | &imap, &nimaps); | |
1da177e4 LT |
299 | break; |
300 | case BMAPI_UNWRITTEN: | |
301 | lockmode = 0; | |
302 | error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count); | |
303 | nimaps = 0; | |
304 | break; | |
305 | } | |
306 | ||
307 | if (nimaps) { | |
308 | *niomaps = xfs_imap_to_bmap(io, offset, &imap, | |
309 | iomapp, nimaps, *niomaps, iomap_flags); | |
310 | } else if (niomaps) { | |
311 | *niomaps = 0; | |
312 | } | |
313 | ||
314 | out: | |
315 | if (lockmode) | |
316 | XFS_IUNLOCK(mp, io, lockmode); | |
317 | return XFS_ERROR(error); | |
318 | } | |
319 | ||
320 | STATIC int | |
321 | xfs_flush_space( | |
322 | xfs_inode_t *ip, | |
323 | int *fsynced, | |
324 | int *ioflags) | |
325 | { | |
326 | switch (*fsynced) { | |
327 | case 0: | |
328 | if (ip->i_delayed_blks) { | |
329 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
330 | xfs_flush_inode(ip); | |
331 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
332 | *fsynced = 1; | |
333 | } else { | |
334 | *ioflags |= BMAPI_SYNC; | |
335 | *fsynced = 2; | |
336 | } | |
337 | return 0; | |
338 | case 1: | |
339 | *fsynced = 2; | |
340 | *ioflags |= BMAPI_SYNC; | |
341 | return 0; | |
342 | case 2: | |
343 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
344 | xfs_flush_device(ip); | |
345 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
346 | *fsynced = 3; | |
347 | return 0; | |
348 | } | |
349 | return 1; | |
350 | } | |
351 | ||
352 | int | |
353 | xfs_iomap_write_direct( | |
354 | xfs_inode_t *ip, | |
f403b7f4 | 355 | xfs_off_t offset, |
1da177e4 LT |
356 | size_t count, |
357 | int flags, | |
358 | xfs_bmbt_irec_t *ret_imap, | |
359 | int *nmaps, | |
360 | int found) | |
361 | { | |
362 | xfs_mount_t *mp = ip->i_mount; | |
363 | xfs_iocore_t *io = &ip->i_iocore; | |
364 | xfs_fileoff_t offset_fsb; | |
365 | xfs_fileoff_t last_fsb; | |
366 | xfs_filblks_t count_fsb; | |
367 | xfs_fsize_t isize; | |
368 | xfs_fsblock_t firstfsb; | |
369 | int nimaps, maps; | |
370 | int error; | |
371 | int bmapi_flag; | |
06d10dd9 | 372 | int quota_flag; |
1da177e4 LT |
373 | int rt; |
374 | xfs_trans_t *tp; | |
375 | xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS], *imapp; | |
376 | xfs_bmap_free_t free_list; | |
377 | int aeof; | |
d52b44d0 | 378 | xfs_filblks_t qblocks, resblks; |
1da177e4 | 379 | int committed; |
d52b44d0 | 380 | int resrtextents; |
1da177e4 LT |
381 | |
382 | /* | |
383 | * Make sure that the dquots are there. This doesn't hold | |
384 | * the ilock across a disk read. | |
385 | */ | |
386 | error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED); | |
387 | if (error) | |
388 | return XFS_ERROR(error); | |
389 | ||
390 | maps = min(XFS_WRITE_IMAPS, *nmaps); | |
391 | nimaps = maps; | |
392 | ||
393 | isize = ip->i_d.di_size; | |
394 | aeof = (offset + count) > isize; | |
395 | ||
396 | if (io->io_new_size > isize) | |
397 | isize = io->io_new_size; | |
398 | ||
399 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
400 | last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count))); | |
401 | count_fsb = last_fsb - offset_fsb; | |
402 | if (found && (ret_imap->br_startblock == HOLESTARTBLOCK)) { | |
403 | xfs_fileoff_t map_last_fsb; | |
404 | ||
405 | map_last_fsb = ret_imap->br_blockcount + ret_imap->br_startoff; | |
1da177e4 LT |
406 | if (map_last_fsb < last_fsb) { |
407 | last_fsb = map_last_fsb; | |
408 | count_fsb = last_fsb - offset_fsb; | |
409 | } | |
410 | ASSERT(count_fsb > 0); | |
411 | } | |
412 | ||
413 | /* | |
06d10dd9 | 414 | * Determine if reserving space on the data or realtime partition. |
1da177e4 LT |
415 | */ |
416 | if ((rt = XFS_IS_REALTIME_INODE(ip))) { | |
06d10dd9 | 417 | xfs_extlen_t extsz; |
1da177e4 | 418 | |
06d10dd9 NS |
419 | if (!(extsz = ip->i_d.di_extsize)) |
420 | extsz = mp->m_sb.sb_rextsize; | |
d52b44d0 NS |
421 | resrtextents = qblocks = (count_fsb + extsz - 1); |
422 | do_div(resrtextents, mp->m_sb.sb_rextsize); | |
423 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); | |
06d10dd9 | 424 | quota_flag = XFS_QMOPT_RES_RTBLKS; |
1da177e4 | 425 | } else { |
d52b44d0 NS |
426 | resrtextents = 0; |
427 | resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, count_fsb); | |
06d10dd9 | 428 | quota_flag = XFS_QMOPT_RES_REGBLKS; |
1da177e4 LT |
429 | } |
430 | ||
431 | /* | |
06d10dd9 | 432 | * Allocate and setup the transaction |
1da177e4 LT |
433 | */ |
434 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
435 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); | |
1da177e4 | 436 | error = xfs_trans_reserve(tp, resblks, |
d52b44d0 | 437 | XFS_WRITE_LOG_RES(mp), resrtextents, |
1da177e4 LT |
438 | XFS_TRANS_PERM_LOG_RES, |
439 | XFS_WRITE_LOG_COUNT); | |
440 | ||
441 | /* | |
06d10dd9 | 442 | * Check for running out of space, note: need lock to return |
1da177e4 LT |
443 | */ |
444 | if (error) | |
1da177e4 | 445 | xfs_trans_cancel(tp, 0); |
1da177e4 | 446 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
1da177e4 | 447 | if (error) |
06d10dd9 | 448 | goto error_out; |
1da177e4 | 449 | |
06d10dd9 | 450 | if (XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag)) { |
1da177e4 LT |
451 | error = (EDQUOT); |
452 | goto error1; | |
453 | } | |
1da177e4 LT |
454 | |
455 | bmapi_flag = XFS_BMAPI_WRITE; | |
456 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
457 | xfs_trans_ihold(tp, ip); | |
458 | ||
459 | if (!(flags & BMAPI_MMAP) && (offset < ip->i_d.di_size || rt)) | |
460 | bmapi_flag |= XFS_BMAPI_PREALLOC; | |
461 | ||
462 | /* | |
06d10dd9 | 463 | * Issue the bmapi() call to allocate the blocks |
1da177e4 LT |
464 | */ |
465 | XFS_BMAP_INIT(&free_list, &firstfsb); | |
06d10dd9 | 466 | nimaps = 1; |
1da177e4 LT |
467 | imapp = &imap[0]; |
468 | error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, | |
469 | bmapi_flag, &firstfsb, 0, imapp, &nimaps, &free_list); | |
06d10dd9 | 470 | if (error) |
1da177e4 | 471 | goto error0; |
1da177e4 LT |
472 | |
473 | /* | |
06d10dd9 | 474 | * Complete the transaction |
1da177e4 | 475 | */ |
1da177e4 | 476 | error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed); |
06d10dd9 | 477 | if (error) |
1da177e4 | 478 | goto error0; |
1da177e4 | 479 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL); |
06d10dd9 | 480 | if (error) |
1da177e4 | 481 | goto error_out; |
1da177e4 | 482 | |
06d10dd9 NS |
483 | /* |
484 | * Copy any maps to caller's array and return any error. | |
485 | */ | |
1da177e4 LT |
486 | if (nimaps == 0) { |
487 | error = (ENOSPC); | |
488 | goto error_out; | |
489 | } | |
490 | ||
491 | *ret_imap = imap[0]; | |
492 | *nmaps = 1; | |
493 | if ( !(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) { | |
494 | cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld " | |
495 | "start_block : %llx start_off : %llx blkcnt : %llx " | |
496 | "extent-state : %x \n", | |
497 | (ip->i_mount)->m_fsname, | |
498 | (long long)ip->i_ino, | |
499 | ret_imap->br_startblock, ret_imap->br_startoff, | |
500 | ret_imap->br_blockcount,ret_imap->br_state); | |
501 | } | |
502 | return 0; | |
503 | ||
06d10dd9 | 504 | error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */ |
1da177e4 | 505 | xfs_bmap_cancel(&free_list); |
06d10dd9 | 506 | XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag); |
1da177e4 | 507 | |
06d10dd9 | 508 | error1: /* Just cancel transaction */ |
1da177e4 LT |
509 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
510 | *nmaps = 0; /* nothing set-up here */ | |
511 | ||
512 | error_out: | |
513 | return XFS_ERROR(error); | |
514 | } | |
515 | ||
516 | int | |
517 | xfs_iomap_write_delay( | |
518 | xfs_inode_t *ip, | |
f403b7f4 | 519 | xfs_off_t offset, |
1da177e4 LT |
520 | size_t count, |
521 | int ioflag, | |
522 | xfs_bmbt_irec_t *ret_imap, | |
523 | int *nmaps) | |
524 | { | |
525 | xfs_mount_t *mp = ip->i_mount; | |
526 | xfs_iocore_t *io = &ip->i_iocore; | |
527 | xfs_fileoff_t offset_fsb; | |
528 | xfs_fileoff_t last_fsb; | |
529 | xfs_fsize_t isize; | |
530 | xfs_fsblock_t firstblock; | |
531 | int nimaps; | |
532 | int error; | |
533 | xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS]; | |
534 | int aeof; | |
535 | int fsynced = 0; | |
536 | ||
537 | ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0); | |
538 | ||
539 | /* | |
540 | * Make sure that the dquots are there. This doesn't hold | |
541 | * the ilock across a disk read. | |
542 | */ | |
543 | ||
544 | error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED); | |
545 | if (error) | |
546 | return XFS_ERROR(error); | |
547 | ||
548 | retry: | |
549 | isize = ip->i_d.di_size; | |
550 | if (io->io_new_size > isize) { | |
551 | isize = io->io_new_size; | |
552 | } | |
553 | ||
554 | aeof = 0; | |
555 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
556 | last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count))); | |
557 | /* | |
558 | * If the caller is doing a write at the end of the file, | |
559 | * then extend the allocation (and the buffer used for the write) | |
560 | * out to the file system's write iosize. We clean up any extra | |
561 | * space left over when the file is closed in xfs_inactive(). | |
562 | * | |
563 | * For sync writes, we are flushing delayed allocate space to | |
564 | * try to make additional space available for allocation near | |
565 | * the filesystem full boundary - preallocation hurts in that | |
566 | * situation, of course. | |
567 | */ | |
568 | if (!(ioflag & BMAPI_SYNC) && ((offset + count) > ip->i_d.di_size)) { | |
569 | xfs_off_t aligned_offset; | |
570 | xfs_filblks_t count_fsb; | |
571 | unsigned int iosize; | |
572 | xfs_fileoff_t ioalign; | |
573 | int n; | |
574 | xfs_fileoff_t start_fsb; | |
575 | ||
576 | /* | |
577 | * If there are any real blocks past eof, then don't | |
578 | * do any speculative allocation. | |
579 | */ | |
580 | start_fsb = XFS_B_TO_FSBT(mp, | |
581 | ((xfs_ufsize_t)(offset + count - 1))); | |
582 | count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); | |
583 | while (count_fsb > 0) { | |
584 | nimaps = XFS_WRITE_IMAPS; | |
585 | error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb, | |
586 | 0, &firstblock, 0, imap, &nimaps, NULL); | |
587 | if (error) { | |
588 | return error; | |
589 | } | |
590 | for (n = 0; n < nimaps; n++) { | |
591 | if ( !(io->io_flags & XFS_IOCORE_RT) && | |
592 | !imap[n].br_startblock) { | |
593 | cmn_err(CE_PANIC,"Access to block " | |
594 | "zero: fs <%s> inode: %lld " | |
595 | "start_block : %llx start_off " | |
596 | ": %llx blkcnt : %llx " | |
597 | "extent-state : %x \n", | |
598 | (ip->i_mount)->m_fsname, | |
599 | (long long)ip->i_ino, | |
600 | imap[n].br_startblock, | |
601 | imap[n].br_startoff, | |
602 | imap[n].br_blockcount, | |
603 | imap[n].br_state); | |
604 | } | |
605 | if ((imap[n].br_startblock != HOLESTARTBLOCK) && | |
606 | (imap[n].br_startblock != DELAYSTARTBLOCK)) { | |
607 | goto write_map; | |
608 | } | |
609 | start_fsb += imap[n].br_blockcount; | |
610 | count_fsb -= imap[n].br_blockcount; | |
611 | } | |
612 | } | |
613 | iosize = mp->m_writeio_blocks; | |
614 | aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1)); | |
615 | ioalign = XFS_B_TO_FSBT(mp, aligned_offset); | |
616 | last_fsb = ioalign + iosize; | |
617 | aeof = 1; | |
618 | } | |
619 | write_map: | |
620 | nimaps = XFS_WRITE_IMAPS; | |
621 | firstblock = NULLFSBLOCK; | |
622 | ||
623 | /* | |
624 | * If mounted with the "-o swalloc" option, roundup the allocation | |
625 | * request to a stripe width boundary if the file size is >= | |
626 | * stripe width and we are allocating past the allocation eof. | |
627 | */ | |
628 | if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_swidth | |
629 | && (mp->m_flags & XFS_MOUNT_SWALLOC) | |
630 | && (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)) && aeof) { | |
631 | int eof; | |
632 | xfs_fileoff_t new_last_fsb; | |
633 | ||
634 | new_last_fsb = roundup_64(last_fsb, mp->m_swidth); | |
635 | error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof); | |
636 | if (error) { | |
637 | return error; | |
638 | } | |
639 | if (eof) { | |
640 | last_fsb = new_last_fsb; | |
641 | } | |
642 | /* | |
643 | * Roundup the allocation request to a stripe unit (m_dalign) boundary | |
644 | * if the file size is >= stripe unit size, and we are allocating past | |
645 | * the allocation eof. | |
646 | */ | |
647 | } else if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_dalign && | |
648 | (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)) && aeof) { | |
649 | int eof; | |
650 | xfs_fileoff_t new_last_fsb; | |
651 | new_last_fsb = roundup_64(last_fsb, mp->m_dalign); | |
652 | error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof); | |
653 | if (error) { | |
654 | return error; | |
655 | } | |
656 | if (eof) { | |
657 | last_fsb = new_last_fsb; | |
658 | } | |
659 | /* | |
660 | * Round up the allocation request to a real-time extent boundary | |
661 | * if the file is on the real-time subvolume. | |
662 | */ | |
663 | } else if (io->io_flags & XFS_IOCORE_RT && aeof) { | |
664 | int eof; | |
665 | xfs_fileoff_t new_last_fsb; | |
666 | ||
667 | new_last_fsb = roundup_64(last_fsb, mp->m_sb.sb_rextsize); | |
668 | error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof); | |
669 | if (error) { | |
670 | return error; | |
671 | } | |
672 | if (eof) | |
673 | last_fsb = new_last_fsb; | |
674 | } | |
675 | error = xfs_bmapi(NULL, ip, offset_fsb, | |
676 | (xfs_filblks_t)(last_fsb - offset_fsb), | |
677 | XFS_BMAPI_DELAY | XFS_BMAPI_WRITE | | |
678 | XFS_BMAPI_ENTIRE, &firstblock, 1, imap, | |
679 | &nimaps, NULL); | |
680 | /* | |
681 | * This can be EDQUOT, if nimaps == 0 | |
682 | */ | |
683 | if (error && (error != ENOSPC)) { | |
684 | return XFS_ERROR(error); | |
685 | } | |
686 | /* | |
687 | * If bmapi returned us nothing, and if we didn't get back EDQUOT, | |
688 | * then we must have run out of space. | |
689 | */ | |
690 | if (nimaps == 0) { | |
691 | xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE, | |
692 | io, offset, count); | |
693 | if (xfs_flush_space(ip, &fsynced, &ioflag)) | |
694 | return XFS_ERROR(ENOSPC); | |
695 | ||
696 | error = 0; | |
697 | goto retry; | |
698 | } | |
699 | ||
700 | *ret_imap = imap[0]; | |
701 | *nmaps = 1; | |
702 | if ( !(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) { | |
703 | cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld " | |
704 | "start_block : %llx start_off : %llx blkcnt : %llx " | |
705 | "extent-state : %x \n", | |
706 | (ip->i_mount)->m_fsname, | |
707 | (long long)ip->i_ino, | |
708 | ret_imap->br_startblock, ret_imap->br_startoff, | |
709 | ret_imap->br_blockcount,ret_imap->br_state); | |
710 | } | |
711 | return 0; | |
712 | } | |
713 | ||
714 | /* | |
715 | * Pass in a delayed allocate extent, convert it to real extents; | |
716 | * return to the caller the extent we create which maps on top of | |
717 | * the originating callers request. | |
718 | * | |
719 | * Called without a lock on the inode. | |
720 | */ | |
721 | int | |
722 | xfs_iomap_write_allocate( | |
723 | xfs_inode_t *ip, | |
f403b7f4 | 724 | xfs_off_t offset, |
24e17b5f | 725 | size_t count, |
1da177e4 LT |
726 | xfs_bmbt_irec_t *map, |
727 | int *retmap) | |
728 | { | |
729 | xfs_mount_t *mp = ip->i_mount; | |
730 | xfs_iocore_t *io = &ip->i_iocore; | |
731 | xfs_fileoff_t offset_fsb, last_block; | |
732 | xfs_fileoff_t end_fsb, map_start_fsb; | |
733 | xfs_fsblock_t first_block; | |
734 | xfs_bmap_free_t free_list; | |
735 | xfs_filblks_t count_fsb; | |
736 | xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS]; | |
737 | xfs_trans_t *tp; | |
738 | int i, nimaps, committed; | |
739 | int error = 0; | |
740 | int nres; | |
741 | ||
742 | *retmap = 0; | |
743 | ||
744 | /* | |
745 | * Make sure that the dquots are there. | |
746 | */ | |
747 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) | |
748 | return XFS_ERROR(error); | |
749 | ||
24e17b5f | 750 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
1da177e4 | 751 | count_fsb = map->br_blockcount; |
24e17b5f | 752 | map_start_fsb = map->br_startoff; |
1da177e4 LT |
753 | |
754 | XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb)); | |
755 | ||
756 | while (count_fsb != 0) { | |
757 | /* | |
758 | * Set up a transaction with which to allocate the | |
759 | * backing store for the file. Do allocations in a | |
760 | * loop until we get some space in the range we are | |
761 | * interested in. The other space that might be allocated | |
762 | * is in the delayed allocation extent on which we sit | |
763 | * but before our buffer starts. | |
764 | */ | |
765 | ||
766 | nimaps = 0; | |
767 | while (nimaps == 0) { | |
768 | tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE); | |
769 | nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK); | |
770 | error = xfs_trans_reserve(tp, nres, | |
771 | XFS_WRITE_LOG_RES(mp), | |
772 | 0, XFS_TRANS_PERM_LOG_RES, | |
773 | XFS_WRITE_LOG_COUNT); | |
774 | if (error == ENOSPC) { | |
775 | error = xfs_trans_reserve(tp, 0, | |
776 | XFS_WRITE_LOG_RES(mp), | |
777 | 0, | |
778 | XFS_TRANS_PERM_LOG_RES, | |
779 | XFS_WRITE_LOG_COUNT); | |
780 | } | |
781 | if (error) { | |
782 | xfs_trans_cancel(tp, 0); | |
783 | return XFS_ERROR(error); | |
784 | } | |
785 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
786 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
787 | xfs_trans_ihold(tp, ip); | |
788 | ||
789 | XFS_BMAP_INIT(&free_list, &first_block); | |
790 | ||
791 | nimaps = XFS_STRAT_WRITE_IMAPS; | |
792 | /* | |
793 | * Ensure we don't go beyond eof - it is possible | |
794 | * the extents changed since we did the read call, | |
795 | * we dropped the ilock in the interim. | |
796 | */ | |
797 | ||
798 | end_fsb = XFS_B_TO_FSB(mp, ip->i_d.di_size); | |
799 | xfs_bmap_last_offset(NULL, ip, &last_block, | |
800 | XFS_DATA_FORK); | |
801 | last_block = XFS_FILEOFF_MAX(last_block, end_fsb); | |
802 | if ((map_start_fsb + count_fsb) > last_block) { | |
803 | count_fsb = last_block - map_start_fsb; | |
804 | if (count_fsb == 0) { | |
805 | error = EAGAIN; | |
806 | goto trans_cancel; | |
807 | } | |
808 | } | |
809 | ||
810 | /* Go get the actual blocks */ | |
811 | error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb, | |
812 | XFS_BMAPI_WRITE, &first_block, 1, | |
813 | imap, &nimaps, &free_list); | |
814 | if (error) | |
815 | goto trans_cancel; | |
816 | ||
817 | error = xfs_bmap_finish(&tp, &free_list, | |
818 | first_block, &committed); | |
819 | if (error) | |
820 | goto trans_cancel; | |
821 | ||
822 | error = xfs_trans_commit(tp, | |
823 | XFS_TRANS_RELEASE_LOG_RES, NULL); | |
824 | if (error) | |
825 | goto error0; | |
826 | ||
827 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
828 | } | |
829 | ||
830 | /* | |
831 | * See if we were able to allocate an extent that | |
832 | * covers at least part of the callers request | |
833 | */ | |
834 | ||
835 | for (i = 0; i < nimaps; i++) { | |
836 | if ( !(io->io_flags & XFS_IOCORE_RT) && | |
837 | !imap[i].br_startblock) { | |
838 | cmn_err(CE_PANIC,"Access to block zero: " | |
839 | "fs <%s> inode: %lld " | |
840 | "start_block : %llx start_off : %llx " | |
841 | "blkcnt : %llx extent-state : %x \n", | |
842 | (ip->i_mount)->m_fsname, | |
843 | (long long)ip->i_ino, | |
844 | imap[i].br_startblock, | |
845 | imap[i].br_startoff, | |
846 | imap[i].br_blockcount,imap[i].br_state); | |
847 | } | |
24e17b5f NS |
848 | if ((offset_fsb >= imap[i].br_startoff) && |
849 | (offset_fsb < (imap[i].br_startoff + | |
850 | imap[i].br_blockcount))) { | |
1da177e4 LT |
851 | *map = imap[i]; |
852 | *retmap = 1; | |
853 | XFS_STATS_INC(xs_xstrat_quick); | |
854 | return 0; | |
855 | } | |
856 | count_fsb -= imap[i].br_blockcount; | |
857 | } | |
858 | ||
859 | /* So far we have not mapped the requested part of the | |
860 | * file, just surrounding data, try again. | |
861 | */ | |
862 | nimaps--; | |
24e17b5f NS |
863 | map_start_fsb = imap[nimaps].br_startoff + |
864 | imap[nimaps].br_blockcount; | |
1da177e4 LT |
865 | } |
866 | ||
867 | trans_cancel: | |
868 | xfs_bmap_cancel(&free_list); | |
869 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); | |
870 | error0: | |
871 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
872 | return XFS_ERROR(error); | |
873 | } | |
874 | ||
875 | int | |
876 | xfs_iomap_write_unwritten( | |
877 | xfs_inode_t *ip, | |
f403b7f4 | 878 | xfs_off_t offset, |
1da177e4 LT |
879 | size_t count) |
880 | { | |
881 | xfs_mount_t *mp = ip->i_mount; | |
882 | xfs_iocore_t *io = &ip->i_iocore; | |
883 | xfs_trans_t *tp; | |
884 | xfs_fileoff_t offset_fsb; | |
885 | xfs_filblks_t count_fsb; | |
886 | xfs_filblks_t numblks_fsb; | |
887 | xfs_bmbt_irec_t imap; | |
888 | int committed; | |
889 | int error; | |
890 | int nres; | |
891 | int nimaps; | |
892 | xfs_fsblock_t firstfsb; | |
893 | xfs_bmap_free_t free_list; | |
894 | ||
895 | xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN, | |
896 | &ip->i_iocore, offset, count); | |
897 | ||
898 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
899 | count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); | |
900 | count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb); | |
901 | ||
902 | do { | |
903 | nres = XFS_DIOSTRAT_SPACE_RES(mp, 0); | |
904 | ||
905 | /* | |
906 | * set up a transaction to convert the range of extents | |
907 | * from unwritten to real. Do allocations in a loop until | |
908 | * we have covered the range passed in. | |
909 | */ | |
910 | ||
911 | tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE); | |
912 | error = xfs_trans_reserve(tp, nres, | |
913 | XFS_WRITE_LOG_RES(mp), 0, | |
914 | XFS_TRANS_PERM_LOG_RES, | |
915 | XFS_WRITE_LOG_COUNT); | |
916 | if (error) { | |
917 | xfs_trans_cancel(tp, 0); | |
918 | goto error0; | |
919 | } | |
920 | ||
921 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
922 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
923 | xfs_trans_ihold(tp, ip); | |
924 | ||
925 | /* | |
926 | * Modify the unwritten extent state of the buffer. | |
927 | */ | |
928 | XFS_BMAP_INIT(&free_list, &firstfsb); | |
929 | nimaps = 1; | |
930 | error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, | |
931 | XFS_BMAPI_WRITE, &firstfsb, | |
932 | 1, &imap, &nimaps, &free_list); | |
933 | if (error) | |
934 | goto error_on_bmapi_transaction; | |
935 | ||
936 | error = xfs_bmap_finish(&(tp), &(free_list), | |
937 | firstfsb, &committed); | |
938 | if (error) | |
939 | goto error_on_bmapi_transaction; | |
940 | ||
941 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL); | |
942 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
943 | if (error) | |
944 | goto error0; | |
945 | ||
946 | if ( !(io->io_flags & XFS_IOCORE_RT) && !imap.br_startblock) { | |
947 | cmn_err(CE_PANIC,"Access to block zero: fs <%s> " | |
948 | "inode: %lld start_block : %llx start_off : " | |
949 | "%llx blkcnt : %llx extent-state : %x \n", | |
950 | (ip->i_mount)->m_fsname, | |
951 | (long long)ip->i_ino, | |
952 | imap.br_startblock,imap.br_startoff, | |
953 | imap.br_blockcount,imap.br_state); | |
954 | } | |
955 | ||
956 | if ((numblks_fsb = imap.br_blockcount) == 0) { | |
957 | /* | |
958 | * The numblks_fsb value should always get | |
959 | * smaller, otherwise the loop is stuck. | |
960 | */ | |
961 | ASSERT(imap.br_blockcount); | |
962 | break; | |
963 | } | |
964 | offset_fsb += numblks_fsb; | |
965 | count_fsb -= numblks_fsb; | |
966 | } while (count_fsb > 0); | |
967 | ||
968 | return 0; | |
969 | ||
970 | error_on_bmapi_transaction: | |
971 | xfs_bmap_cancel(&free_list); | |
972 | xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT)); | |
973 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
974 | error0: | |
975 | return XFS_ERROR(error); | |
976 | } |