blob: b04a60f664118d201ac9842790da0b939492f594 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_fs.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
24#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_quota.h"
27#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dinode.h"
32#include "xfs_inode.h"
Christoph Hellwig84803fb2012-02-29 09:53:50 +000033#include "xfs_inode_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100036#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_rtalloc.h"
38#include "xfs_error.h"
39#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_attr.h"
41#include "xfs_buf_item.h"
42#include "xfs_trans_space.h"
43#include "xfs_utils.h"
44#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Brian Foster27b52862012-11-06 09:50:38 -050046#include "xfs_icache.h"
Brian Foster76a42022013-03-18 10:51:47 -040047#include "xfs_dquot_item.h"
48#include "xfs_dquot.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
52 << mp->m_writeio_log)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +110056xfs_iomap_eof_align_last_fsb(
57 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100058 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +110059 xfs_extlen_t extsize,
60 xfs_fileoff_t *last_fsb)
61{
62 xfs_fileoff_t new_last_fsb = 0;
Christoph Hellwigbf322d92011-12-18 20:00:05 +000063 xfs_extlen_t align = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +110064 int eof, error;
65
Christoph Hellwigbf322d92011-12-18 20:00:05 +000066 if (!XFS_IS_REALTIME_INODE(ip)) {
67 /*
68 * Round up the allocation request to a stripe unit
69 * (m_dalign) boundary if the file size is >= stripe unit
70 * size, and we are allocating past the allocation eof.
71 *
72 * If mounted with the "-o swalloc" option the alignment is
73 * increased from the strip unit size to the stripe width.
74 */
75 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
76 align = mp->m_swidth;
77 else if (mp->m_dalign)
78 align = mp->m_dalign;
79
Christoph Hellwigce7ae1512011-12-18 20:00:11 +000080 if (align && XFS_ISIZE(ip) >= XFS_FSB_TO_B(mp, align))
Christoph Hellwigbf322d92011-12-18 20:00:05 +000081 new_last_fsb = roundup_64(*last_fsb, align);
82 }
Nathan Scottdd9f4382006-01-11 15:28:28 +110083
84 /*
85 * Always round up the allocation request to an extent boundary
86 * (when file on a real-time subvolume or has di_extsize hint).
87 */
88 if (extsize) {
89 if (new_last_fsb)
90 align = roundup_64(new_last_fsb, extsize);
91 else
92 align = extsize;
93 new_last_fsb = roundup_64(*last_fsb, align);
94 }
95
96 if (new_last_fsb) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100097 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +110098 if (error)
99 return error;
100 if (eof)
101 *last_fsb = new_last_fsb;
102 }
103 return 0;
104}
105
106STATIC int
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100107xfs_alert_fsblock_zero(
Nathan Scott572d95f2006-09-28 11:03:20 +1000108 xfs_inode_t *ip,
109 xfs_bmbt_irec_t *imap)
110{
Dave Chinner6a19d932011-03-07 10:02:35 +1100111 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +1000112 "Access to block zero in inode %llu "
113 "start_block: %llx start_off: %llx "
114 "blkcnt: %llx extent-state: %x\n",
115 (unsigned long long)ip->i_ino,
116 (unsigned long long)imap->br_startblock,
117 (unsigned long long)imap->br_startoff,
118 (unsigned long long)imap->br_blockcount,
119 imap->br_state);
120 return EFSCORRUPTED;
121}
122
Christoph Hellwiga206c812010-12-10 08:42:20 +0000123int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124xfs_iomap_write_direct(
125 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700126 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 size_t count,
Christoph Hellwig30704512010-06-24 11:42:19 +1000128 xfs_bmbt_irec_t *imap,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000129 int nmaps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 xfs_fileoff_t offset_fsb;
133 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100134 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100136 xfs_extlen_t extsz, temp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100137 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000139 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int rt;
141 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100143 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100145 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Dave Chinner507630b2012-03-27 10:34:50 -0400147 error = xfs_qm_dqattach(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (error)
149 return XFS_ERROR(error);
150
Nathan Scottdd9f4382006-01-11 15:28:28 +1100151 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000152 extsz = xfs_get_extsz_hint(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
David Chinner957d0eb2007-06-18 16:50:37 +1000154 offset_fsb = XFS_B_TO_FSBT(mp, offset);
155 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000156 if ((offset + count) > XFS_ISIZE(ip)) {
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100157 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100158 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400159 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100160 } else {
Christoph Hellwig405f8042010-12-10 08:42:19 +0000161 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100162 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
Christoph Hellwig30704512010-06-24 11:42:19 +1000163 imap->br_blockcount +
164 imap->br_startoff);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100165 }
166 count_fsb = last_fsb - offset_fsb;
167 ASSERT(count_fsb > 0);
168
169 resaligned = count_fsb;
170 if (unlikely(extsz)) {
171 if ((temp = do_mod(offset_fsb, extsz)))
172 resaligned += temp;
173 if ((temp = do_mod(resaligned, extsz)))
174 resaligned += extsz - temp;
175 }
176
177 if (unlikely(rt)) {
178 resrtextents = qblocks = resaligned;
179 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000180 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
181 quota_flag = XFS_QMOPT_RES_RTBLKS;
182 } else {
183 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100184 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000185 quota_flag = XFS_QMOPT_RES_REGBLKS;
186 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000189 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000193 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 XFS_TRANS_PERM_LOG_RES,
195 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000197 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Dave Chinner507630b2012-03-27 10:34:50 -0400199 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 xfs_trans_cancel(tp, 0);
Dave Chinner507630b2012-03-27 10:34:50 -0400201 return XFS_ERROR(error);
202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Christoph Hellwig7d095252009-06-08 15:33:32 +0200206 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100207 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400208 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Christoph Hellwigddc34152011-09-19 15:00:54 +0000210 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000212 bmapi_flag = 0;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000213 if (offset < XFS_ISIZE(ip) || extsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 bmapi_flag |= XFS_BMAPI_PREALLOC;
215
216 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000217 * From this point onwards we overwrite the imap pointer that the
218 * caller gave to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600220 xfs_bmap_init(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000221 nimaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000222 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flag,
223 &firstfsb, 0, imap, &nimaps, &free_list);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000224 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400225 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000228 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100230 error = xfs_bmap_finish(&tp, &free_list, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000231 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400232 goto out_bmap_cancel;
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000233 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000234 if (error)
Dave Chinner507630b2012-03-27 10:34:50 -0400235 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Nathan Scott06d10dd2005-06-21 15:48:47 +1000237 /*
238 * Copy any maps to caller's array and return any error.
239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (nimaps == 0) {
Dave Chinner507630b2012-03-27 10:34:50 -0400241 error = XFS_ERROR(ENOSPC);
242 goto out_unlock;
Nathan Scott572d95f2006-09-28 11:03:20 +1000243 }
244
Dave Chinner507630b2012-03-27 10:34:50 -0400245 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100246 error = xfs_alert_fsblock_zero(ip, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dave Chinner507630b2012-03-27 10:34:50 -0400248out_unlock:
249 xfs_iunlock(ip, XFS_ILOCK_EXCL);
250 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Dave Chinner507630b2012-03-27 10:34:50 -0400252out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 xfs_bmap_cancel(&free_list);
Dave Chinnerea562ed2012-05-08 20:48:53 +1000254 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
Dave Chinner507630b2012-03-27 10:34:50 -0400255out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
Dave Chinner507630b2012-03-27 10:34:50 -0400257 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Nathan Scottdd9f4382006-01-11 15:28:28 +1100260/*
Dave Chinner8de2bf92009-04-06 18:49:12 +0200261 * If the caller is doing a write at the end of the file, then extend the
262 * allocation out to the file system's write iosize. We clean up any extra
263 * space left over when the file is closed in xfs_inactive().
Dave Chinner055388a2011-01-04 11:35:03 +1100264 *
265 * If we find we already have delalloc preallocation beyond EOF, don't do more
266 * preallocation as it it not needed.
Nathan Scottdd9f4382006-01-11 15:28:28 +1100267 */
268STATIC int
269xfs_iomap_eof_want_preallocate(
270 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000271 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100272 xfs_off_t offset,
273 size_t count,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100274 xfs_bmbt_irec_t *imap,
275 int nimaps,
276 int *prealloc)
277{
278 xfs_fileoff_t start_fsb;
279 xfs_filblks_t count_fsb;
280 xfs_fsblock_t firstblock;
281 int n, error, imaps;
Dave Chinner055388a2011-01-04 11:35:03 +1100282 int found_delalloc = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100283
284 *prealloc = 0;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000285 if (offset + count <= XFS_ISIZE(ip))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100286 return 0;
287
288 /*
Dave Chinner133eeb12013-06-27 16:04:48 +1000289 * If the file is smaller than the minimum prealloc and we are using
290 * dynamic preallocation, don't do any preallocation at all as it is
291 * likely this is the only write to the file that is going to be done.
292 */
293 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
294 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks))
295 return 0;
296
297 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100298 * If there are any real blocks past eof, then don't
299 * do any speculative allocation.
300 */
301 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
Dave Chinner32972382012-06-08 15:44:54 +1000302 count_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100303 while (count_fsb > 0) {
304 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100305 firstblock = NULLFSBLOCK;
Dave Chinner5c8ed202011-09-18 20:40:45 +0000306 error = xfs_bmapi_read(ip, start_fsb, count_fsb, imap, &imaps,
307 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100308 if (error)
309 return error;
310 for (n = 0; n < imaps; n++) {
311 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
312 (imap[n].br_startblock != DELAYSTARTBLOCK))
313 return 0;
314 start_fsb += imap[n].br_blockcount;
315 count_fsb -= imap[n].br_blockcount;
Dave Chinner055388a2011-01-04 11:35:03 +1100316
317 if (imap[n].br_startblock == DELAYSTARTBLOCK)
318 found_delalloc = 1;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100319 }
320 }
Dave Chinner055388a2011-01-04 11:35:03 +1100321 if (!found_delalloc)
322 *prealloc = 1;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100323 return 0;
324}
325
Dave Chinner055388a2011-01-04 11:35:03 +1100326/*
Dave Chinnera1e16c22013-02-11 16:05:01 +1100327 * Determine the initial size of the preallocation. We are beyond the current
328 * EOF here, but we need to take into account whether this is a sparse write or
329 * an extending write when determining the preallocation size. Hence we need to
330 * look up the extent that ends at the current write offset and use the result
331 * to determine the preallocation size.
332 *
333 * If the extent is a hole, then preallocation is essentially disabled.
334 * Otherwise we take the size of the preceeding data extent as the basis for the
335 * preallocation size. If the size of the extent is greater than half the
336 * maximum extent length, then use the current offset as the basis. This ensures
337 * that for large files the preallocation size always extends to MAXEXTLEN
338 * rather than falling short due to things like stripe unit/width alignment of
339 * real extents.
340 */
Mark Tinguelye8108ce2013-02-24 13:04:37 -0600341STATIC xfs_fsblock_t
Dave Chinnera1e16c22013-02-11 16:05:01 +1100342xfs_iomap_eof_prealloc_initial_size(
343 struct xfs_mount *mp,
344 struct xfs_inode *ip,
345 xfs_off_t offset,
346 xfs_bmbt_irec_t *imap,
347 int nimaps)
348{
349 xfs_fileoff_t start_fsb;
350 int imaps = 1;
351 int error;
352
353 ASSERT(nimaps >= imaps);
354
355 /* if we are using a specific prealloc size, return now */
356 if (mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)
357 return 0;
358
Dave Chinner133eeb12013-06-27 16:04:48 +1000359 /* If the file is small, then use the minimum prealloc */
360 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign))
361 return 0;
362
Dave Chinnera1e16c22013-02-11 16:05:01 +1100363 /*
364 * As we write multiple pages, the offset will always align to the
365 * start of a page and hence point to a hole at EOF. i.e. if the size is
366 * 4096 bytes, we only have one block at FSB 0, but XFS_B_TO_FSB(4096)
367 * will return FSB 1. Hence if there are blocks in the file, we want to
368 * point to the block prior to the EOF block and not the hole that maps
369 * directly at @offset.
370 */
371 start_fsb = XFS_B_TO_FSB(mp, offset);
372 if (start_fsb)
373 start_fsb--;
374 error = xfs_bmapi_read(ip, start_fsb, 1, imap, &imaps, XFS_BMAPI_ENTIRE);
375 if (error)
376 return 0;
377
378 ASSERT(imaps == 1);
379 if (imap[0].br_startblock == HOLESTARTBLOCK)
380 return 0;
381 if (imap[0].br_blockcount <= (MAXEXTLEN >> 1))
Brian Fostere114b5f2013-02-19 10:24:41 -0500382 return imap[0].br_blockcount << 1;
Dave Chinnera1e16c22013-02-11 16:05:01 +1100383 return XFS_B_TO_FSB(mp, offset);
384}
385
Brian Foster76a42022013-03-18 10:51:47 -0400386STATIC bool
387xfs_quota_need_throttle(
388 struct xfs_inode *ip,
389 int type,
390 xfs_fsblock_t alloc_blocks)
391{
392 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
393
394 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
395 return false;
396
397 /* no hi watermark, no throttle */
398 if (!dq->q_prealloc_hi_wmark)
399 return false;
400
401 /* under the lo watermark, no throttle */
402 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
403 return false;
404
405 return true;
406}
407
408STATIC void
409xfs_quota_calc_throttle(
410 struct xfs_inode *ip,
411 int type,
412 xfs_fsblock_t *qblocks,
413 int *qshift)
414{
415 int64_t freesp;
416 int shift = 0;
417 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
418
419 /* over hi wmark, squash the prealloc completely */
420 if (dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
421 *qblocks = 0;
422 return;
423 }
424
425 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
426 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
427 shift = 2;
428 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
429 shift += 2;
430 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
431 shift += 2;
432 }
433
434 /* only overwrite the throttle values if we are more aggressive */
435 if ((freesp >> shift) < (*qblocks >> *qshift)) {
436 *qblocks = freesp;
437 *qshift = shift;
438 }
439}
440
Dave Chinnera1e16c22013-02-11 16:05:01 +1100441/*
Dave Chinner055388a2011-01-04 11:35:03 +1100442 * If we don't have a user specified preallocation size, dynamically increase
443 * the preallocation size as the size of the file grows. Cap the maximum size
444 * at a single extent or less if the filesystem is near full. The closer the
445 * filesystem is to full, the smaller the maximum prealocation.
446 */
447STATIC xfs_fsblock_t
448xfs_iomap_prealloc_size(
449 struct xfs_mount *mp,
Dave Chinnera1e16c22013-02-11 16:05:01 +1100450 struct xfs_inode *ip,
451 xfs_off_t offset,
452 struct xfs_bmbt_irec *imap,
453 int nimaps)
Dave Chinner055388a2011-01-04 11:35:03 +1100454{
455 xfs_fsblock_t alloc_blocks = 0;
Brian Foster3c58b5f2013-03-18 10:51:43 -0400456 int shift = 0;
457 int64_t freesp;
Brian Foster76a42022013-03-18 10:51:47 -0400458 xfs_fsblock_t qblocks;
459 int qshift = 0;
Dave Chinner055388a2011-01-04 11:35:03 +1100460
Dave Chinnera1e16c22013-02-11 16:05:01 +1100461 alloc_blocks = xfs_iomap_eof_prealloc_initial_size(mp, ip, offset,
462 imap, nimaps);
Brian Foster3c58b5f2013-03-18 10:51:43 -0400463 if (!alloc_blocks)
464 goto check_writeio;
Brian Foster76a42022013-03-18 10:51:47 -0400465 qblocks = alloc_blocks;
Dave Chinner055388a2011-01-04 11:35:03 +1100466
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400467 /*
468 * MAXEXTLEN is not a power of two value but we round the prealloc down
469 * to the nearest power of two value after throttling. To prevent the
470 * round down from unconditionally reducing the maximum supported prealloc
471 * size, we round up first, apply appropriate throttling, round down and
472 * cap the value to MAXEXTLEN.
473 */
474 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
475 alloc_blocks);
Dave Chinner055388a2011-01-04 11:35:03 +1100476
Brian Foster3c58b5f2013-03-18 10:51:43 -0400477 xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
478 freesp = mp->m_sb.sb_fdblocks;
479 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
480 shift = 2;
481 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
482 shift++;
483 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
484 shift++;
485 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
486 shift++;
487 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
488 shift++;
Dave Chinner055388a2011-01-04 11:35:03 +1100489 }
Brian Foster76a42022013-03-18 10:51:47 -0400490
491 /*
492 * Check each quota to cap the prealloc size and provide a shift
493 * value to throttle with.
494 */
495 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
496 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift);
497 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
498 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift);
499 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
500 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift);
501
502 /*
503 * The final prealloc size is set to the minimum of free space available
504 * in each of the quotas and the overall filesystem.
505 *
506 * The shift throttle value is set to the maximum value as determined by
507 * the global low free space values and per-quota low free space values.
508 */
509 alloc_blocks = MIN(alloc_blocks, qblocks);
510 shift = MAX(shift, qshift);
511
Brian Foster3c58b5f2013-03-18 10:51:43 -0400512 if (shift)
513 alloc_blocks >>= shift;
Brian Fosterc9bdbdc2013-03-18 10:51:44 -0400514 /*
515 * rounddown_pow_of_two() returns an undefined result if we pass in
516 * alloc_blocks = 0.
517 */
518 if (alloc_blocks)
519 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
520 if (alloc_blocks > MAXEXTLEN)
521 alloc_blocks = MAXEXTLEN;
Dave Chinner055388a2011-01-04 11:35:03 +1100522
Brian Foster3c58b5f2013-03-18 10:51:43 -0400523 /*
524 * If we are still trying to allocate more space than is
525 * available, squash the prealloc hard. This can happen if we
526 * have a large file on a small filesystem and the above
527 * lowspace thresholds are smaller than MAXEXTLEN.
528 */
529 while (alloc_blocks && alloc_blocks >= freesp)
530 alloc_blocks >>= 4;
531
532check_writeio:
Dave Chinner055388a2011-01-04 11:35:03 +1100533 if (alloc_blocks < mp->m_writeio_blocks)
534 alloc_blocks = mp->m_writeio_blocks;
535
Brian Foster19cb7e32013-03-18 10:51:48 -0400536 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
537 mp->m_writeio_blocks);
538
Dave Chinner055388a2011-01-04 11:35:03 +1100539 return alloc_blocks;
540}
541
Christoph Hellwiga206c812010-12-10 08:42:20 +0000542int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543xfs_iomap_write_delay(
544 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700545 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 size_t count,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000547 xfs_bmbt_irec_t *ret_imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 xfs_fileoff_t offset_fsb;
551 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100552 xfs_off_t aligned_offset;
553 xfs_fileoff_t ioalign;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100554 xfs_extlen_t extsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Dave Chinner9aa05002012-10-08 21:56:04 +1100557 int prealloc;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100558 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000560 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /*
563 * Make sure that the dquots are there. This doesn't hold
564 * the ilock across a disk read.
565 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200566 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (error)
568 return XFS_ERROR(error);
569
David Chinner957d0eb2007-06-18 16:50:37 +1000570 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100571 offset_fsb = XFS_B_TO_FSBT(mp, offset);
572
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100573 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000574 imap, XFS_WRITE_IMAPS, &prealloc);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100575 if (error)
576 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Dave Chinner8de2bf92009-04-06 18:49:12 +0200578retry:
Nathan Scottdd9f4382006-01-11 15:28:28 +1100579 if (prealloc) {
Dave Chinnera1e16c22013-02-11 16:05:01 +1100580 xfs_fsblock_t alloc_blocks;
581
582 alloc_blocks = xfs_iomap_prealloc_size(mp, ip, offset, imap,
583 XFS_WRITE_IMAPS);
Dave Chinner055388a2011-01-04 11:35:03 +1100584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
586 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Dave Chinner055388a2011-01-04 11:35:03 +1100587 last_fsb = ioalign + alloc_blocks;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100588 } else {
589 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100591
592 if (prealloc || extsz) {
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100593 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100594 if (error)
595 return error;
596 }
597
Dave Chinner3ed91162012-04-29 22:43:19 +1000598 /*
599 * Make sure preallocation does not create extents beyond the range we
600 * actually support in this filesystem.
601 */
Dave Chinnerd2c28192012-06-08 15:44:53 +1000602 if (last_fsb > XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes))
603 last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Dave Chinner3ed91162012-04-29 22:43:19 +1000604
605 ASSERT(last_fsb > offset_fsb);
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 nimaps = XFS_WRITE_IMAPS;
Christoph Hellwig44032802011-09-18 20:40:48 +0000608 error = xfs_bmapi_delay(ip, offset_fsb, last_fsb - offset_fsb,
609 imap, &nimaps, XFS_BMAPI_ENTIRE);
Dave Chinner055388a2011-01-04 11:35:03 +1100610 switch (error) {
611 case 0:
612 case ENOSPC:
613 case EDQUOT:
614 break;
615 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return XFS_ERROR(error);
Dave Chinner055388a2011-01-04 11:35:03 +1100617 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /*
Dave Chinner9aa05002012-10-08 21:56:04 +1100620 * If bmapi returned us nothing, we got either ENOSPC or EDQUOT. Retry
Dave Chinner055388a2011-01-04 11:35:03 +1100621 * without EOF preallocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623 if (nimaps == 0) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000624 trace_xfs_delalloc_enospc(ip, offset, count);
Dave Chinner9aa05002012-10-08 21:56:04 +1100625 if (prealloc) {
626 prealloc = 0;
627 error = 0;
628 goto retry;
Dave Chinner055388a2011-01-04 11:35:03 +1100629 }
Dave Chinner9aa05002012-10-08 21:56:04 +1100630 return XFS_ERROR(error ? error : ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
David Chinner86c4d622008-04-29 12:53:21 +1000633 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100634 return xfs_alert_fsblock_zero(ip, &imap[0]);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100635
Brian Foster27b52862012-11-06 09:50:38 -0500636 /*
637 * Tag the inode as speculatively preallocated so we can reclaim this
638 * space on demand, if necessary.
639 */
640 if (prealloc)
641 xfs_inode_set_eofblocks_tag(ip);
642
Nathan Scottdd9f4382006-01-11 15:28:28 +1100643 *ret_imap = imap[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 0;
645}
646
647/*
648 * Pass in a delayed allocate extent, convert it to real extents;
649 * return to the caller the extent we create which maps on top of
650 * the originating callers request.
651 *
652 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100653 *
654 * We no longer bother to look at the incoming map - all we have to
655 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
Christoph Hellwiga206c812010-12-10 08:42:20 +0000657int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658xfs_iomap_write_allocate(
659 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700660 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700661 size_t count,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000662 xfs_bmbt_irec_t *imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xfs_fileoff_t offset_fsb, last_block;
666 xfs_fileoff_t end_fsb, map_start_fsb;
667 xfs_fsblock_t first_block;
668 xfs_bmap_free_t free_list;
669 xfs_filblks_t count_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 xfs_trans_t *tp;
David Chinnere4143a12007-11-23 16:29:11 +1100671 int nimaps, committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int error = 0;
673 int nres;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * Make sure that the dquots are there.
677 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200678 error = xfs_qm_dqattach(ip, 0);
679 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return XFS_ERROR(error);
681
Nathan Scott24e17b52005-05-05 13:33:20 -0700682 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig30704512010-06-24 11:42:19 +1000683 count_fsb = imap->br_blockcount;
684 map_start_fsb = imap->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
687
688 while (count_fsb != 0) {
689 /*
690 * Set up a transaction with which to allocate the
691 * backing store for the file. Do allocations in a
692 * loop until we get some space in the range we are
693 * interested in. The other space that might be allocated
694 * is in the delayed allocation extent on which we sit
695 * but before our buffer starts.
696 */
697
698 nimaps = 0;
699 while (nimaps == 0) {
700 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000701 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
703 error = xfs_trans_reserve(tp, nres,
704 XFS_WRITE_LOG_RES(mp),
705 0, XFS_TRANS_PERM_LOG_RES,
706 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (error) {
708 xfs_trans_cancel(tp, 0);
709 return XFS_ERROR(error);
710 }
711 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000712 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Eric Sandeen9d87c312009-01-14 23:22:07 -0600714 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
David Chinnere4143a12007-11-23 16:29:11 +1100717 * it is possible that the extents have changed since
718 * we did the read call as we dropped the ilock for a
719 * while. We have to be careful about truncates or hole
720 * punchs here - we are not allowed to allocate
721 * non-delalloc blocks here.
722 *
723 * The only protection against truncation is the pages
724 * for the range we are being asked to convert are
725 * locked and hence a truncate will block on them
726 * first.
727 *
728 * As a result, if we go beyond the range we really
729 * need and hit an delalloc extent boundary followed by
730 * a hole while we have excess blocks in the map, we
731 * will fill the hole incorrectly and overrun the
732 * transaction reservation.
733 *
734 * Using a single map prevents this as we are forced to
735 * check each map we look for overlap with the desired
736 * range and abort as soon as we find it. Also, given
737 * that we only return a single map, having one beyond
738 * what we can return is probably a bit silly.
739 *
740 * We also need to check that we don't go beyond EOF;
741 * this is a truncate optimisation as a truncate sets
742 * the new file size before block on the pages we
743 * currently have locked under writeback. Because they
744 * are about to be tossed, we don't need to write them
745 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
David Chinnere4143a12007-11-23 16:29:11 +1100747 nimaps = 1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000748 end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
David Chinner7c9ef852008-04-10 12:21:59 +1000749 error = xfs_bmap_last_offset(NULL, ip, &last_block,
750 XFS_DATA_FORK);
751 if (error)
752 goto trans_cancel;
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
755 if ((map_start_fsb + count_fsb) > last_block) {
756 count_fsb = last_block - map_start_fsb;
757 if (count_fsb == 0) {
758 error = EAGAIN;
759 goto trans_cancel;
760 }
761 }
762
Christoph Hellwig30704512010-06-24 11:42:19 +1000763 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000764 * From this point onwards we overwrite the imap
765 * pointer that the caller gave to us.
766 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000767 error = xfs_bmapi_write(tp, ip, map_start_fsb,
Dave Chinner24558812012-10-05 11:06:58 +1000768 count_fsb,
769 XFS_BMAPI_STACK_SWITCH,
770 &first_block, 1,
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000771 imap, &nimaps, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (error)
773 goto trans_cancel;
774
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100775 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (error)
777 goto trans_cancel;
778
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000779 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (error)
781 goto error0;
782
783 xfs_iunlock(ip, XFS_ILOCK_EXCL);
784 }
785
786 /*
787 * See if we were able to allocate an extent that
788 * covers at least part of the callers request
789 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000790 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100791 return xfs_alert_fsblock_zero(ip, imap);
David Chinner86c4d622008-04-29 12:53:21 +1000792
Christoph Hellwig30704512010-06-24 11:42:19 +1000793 if ((offset_fsb >= imap->br_startoff) &&
794 (offset_fsb < (imap->br_startoff +
795 imap->br_blockcount))) {
David Chinnere4143a12007-11-23 16:29:11 +1100796 XFS_STATS_INC(xs_xstrat_quick);
797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
David Chinnere4143a12007-11-23 16:29:11 +1100800 /*
801 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * file, just surrounding data, try again.
803 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000804 count_fsb -= imap->br_blockcount;
805 map_start_fsb = imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808trans_cancel:
809 xfs_bmap_cancel(&free_list);
810 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
811error0:
812 xfs_iunlock(ip, XFS_ILOCK_EXCL);
813 return XFS_ERROR(error);
814}
815
816int
817xfs_iomap_write_unwritten(
818 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700819 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 size_t count)
821{
822 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 xfs_fileoff_t offset_fsb;
824 xfs_filblks_t count_fsb;
825 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100826 xfs_fsblock_t firstfsb;
827 int nimaps;
828 xfs_trans_t *tp;
829 xfs_bmbt_irec_t imap;
830 xfs_bmap_free_t free_list;
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000831 xfs_fsize_t i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100832 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int committed;
834 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000836 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 offset_fsb = XFS_B_TO_FSBT(mp, offset);
839 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
840 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
841
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000842 /*
843 * Reserve enough blocks in this transaction for two complete extent
844 * btree splits. We may be converting the middle part of an unwritten
845 * extent and in this case we will insert two new extents in the btree
846 * each of which could cause a full split.
847 *
848 * This reservation amount will be used in the first call to
849 * xfs_bmbt_split() to select an AG with enough space to satisfy the
850 * rest of the operation.
851 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100852 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Nathan Scottdd9f4382006-01-11 15:28:28 +1100854 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * set up a transaction to convert the range of extents
857 * from unwritten to real. Do allocations in a loop until
858 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000859 *
860 * Note that we open code the transaction allocation here
861 * to pass KM_NOFS--we can't risk to recursing back into
862 * the filesystem here as we might be asked to write out
863 * the same inode that we complete here and might deadlock
864 * on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
Jan Karad9457dc2012-06-12 16:20:39 +0200866 sb_start_intwrite(mp->m_super);
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000867 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
Jan Karad9457dc2012-06-12 16:20:39 +0200868 tp->t_flags |= XFS_TRANS_RESERVE | XFS_TRANS_FREEZE_PROT;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100869 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 XFS_WRITE_LOG_RES(mp), 0,
871 XFS_TRANS_PERM_LOG_RES,
872 XFS_WRITE_LOG_COUNT);
873 if (error) {
874 xfs_trans_cancel(tp, 0);
Nathan Scott572d95f2006-09-28 11:03:20 +1000875 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
878 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000879 xfs_trans_ijoin(tp, ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /*
882 * Modify the unwritten extent state of the buffer.
883 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600884 xfs_bmap_init(&free_list, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 nimaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000886 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
887 XFS_BMAPI_CONVERT, &firstfsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000888 1, &imap, &nimaps, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (error)
890 goto error_on_bmapi_transaction;
891
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000892 /*
893 * Log the updated inode size as we go. We have to be careful
894 * to only log it up to the actual write offset if it is
895 * halfway into a block.
896 */
897 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
898 if (i_size > offset + count)
899 i_size = offset + count;
900
901 i_size = xfs_new_eof(ip, i_size);
902 if (i_size) {
903 ip->i_d.di_size = i_size;
904 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
905 }
906
907 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (error)
909 goto error_on_bmapi_transaction;
910
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000911 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 xfs_iunlock(ip, XFS_ILOCK_EXCL);
913 if (error)
Nathan Scott572d95f2006-09-28 11:03:20 +1000914 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100915
David Chinner86c4d622008-04-29 12:53:21 +1000916 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Dave Chinner6d4a8ec2011-03-07 10:06:35 +1100917 return xfs_alert_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 if ((numblks_fsb = imap.br_blockcount) == 0) {
920 /*
921 * The numblks_fsb value should always get
922 * smaller, otherwise the loop is stuck.
923 */
924 ASSERT(imap.br_blockcount);
925 break;
926 }
927 offset_fsb += numblks_fsb;
928 count_fsb -= numblks_fsb;
929 } while (count_fsb > 0);
930
931 return 0;
932
933error_on_bmapi_transaction:
934 xfs_bmap_cancel(&free_list);
935 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
936 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return XFS_ERROR(error);
938}