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