891260fea11e3bc442f2de17349f16d532fad6a9
[pandora-kernel.git] / fs / xfs / xfs_rtalloc.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_mount.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_alloc.h"
33 #include "xfs_bmap.h"
34 #include "xfs_rtalloc.h"
35 #include "xfs_fsops.h"
36 #include "xfs_error.h"
37 #include "xfs_rw.h"
38 #include "xfs_inode_item.h"
39 #include "xfs_trans_space.h"
40 #include "xfs_utils.h"
41 #include "xfs_trace.h"
42
43
44 /*
45  * Prototypes for internal functions.
46  */
47
48
49 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
50                 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
51 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
52                 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
53 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
54                 xfs_extlen_t, int, xfs_rtblock_t *, int *);
55 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
56                 xfs_rtblock_t, xfs_rtblock_t *);
57 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
58                 xfs_rtblock_t, xfs_rtblock_t *);
59 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
60                 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
61 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
62                 xfs_extlen_t, int);
63 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
64                 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
65
66 /*
67  * Internal functions.
68  */
69
70 /*
71  * Allocate space to the bitmap or summary file, and zero it, for growfs.
72  */
73 STATIC int                              /* error */
74 xfs_growfs_rt_alloc(
75         xfs_mount_t     *mp,            /* file system mount point */
76         xfs_extlen_t    oblocks,        /* old count of blocks */
77         xfs_extlen_t    nblocks,        /* new count of blocks */
78         xfs_ino_t       ino)            /* inode number (bitmap/summary) */
79 {
80         xfs_fileoff_t   bno;            /* block number in file */
81         xfs_buf_t       *bp;            /* temporary buffer for zeroing */
82         int             committed;      /* transaction committed flag */
83         xfs_daddr_t     d;              /* disk block address */
84         int             error;          /* error return value */
85         xfs_fsblock_t   firstblock;     /* first block allocated in xaction */
86         xfs_bmap_free_t flist;          /* list of freed blocks */
87         xfs_fsblock_t   fsbno;          /* filesystem block for bno */
88         xfs_inode_t     *ip;            /* pointer to incore inode */
89         xfs_bmbt_irec_t map;            /* block map output */
90         int             nmap;           /* number of block maps */
91         int             resblks;        /* space reservation */
92
93         /*
94          * Allocate space to the file, as necessary.
95          */
96         while (oblocks < nblocks) {
97                 int             cancelflags = 0;
98                 xfs_trans_t     *tp;
99
100                 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
101                 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
102                 /*
103                  * Reserve space & log for one extent added to the file.
104                  */
105                 if ((error = xfs_trans_reserve(tp, resblks,
106                                 XFS_GROWRTALLOC_LOG_RES(mp), 0,
107                                 XFS_TRANS_PERM_LOG_RES,
108                                 XFS_DEFAULT_PERM_LOG_COUNT)))
109                         goto error_cancel;
110                 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
111                 /*
112                  * Lock the inode.
113                  */
114                 if ((error = xfs_trans_iget(mp, tp, ino, 0,
115                                                 XFS_ILOCK_EXCL, &ip)))
116                         goto error_cancel;
117                 xfs_bmap_init(&flist, &firstblock);
118                 /*
119                  * Allocate blocks to the bitmap file.
120                  */
121                 nmap = 1;
122                 cancelflags |= XFS_TRANS_ABORT;
123                 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
124                         XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
125                         resblks, &map, &nmap, &flist);
126                 if (!error && nmap < 1)
127                         error = XFS_ERROR(ENOSPC);
128                 if (error)
129                         goto error_cancel;
130                 /*
131                  * Free any blocks freed up in the transaction, then commit.
132                  */
133                 error = xfs_bmap_finish(&tp, &flist, &committed);
134                 if (error)
135                         goto error_cancel;
136                 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
137                 if (error)
138                         goto error;
139                 /*
140                  * Now we need to clear the allocated blocks.
141                  * Do this one block per transaction, to keep it simple.
142                  */
143                 cancelflags = 0;
144                 for (bno = map.br_startoff, fsbno = map.br_startblock;
145                      bno < map.br_startoff + map.br_blockcount;
146                      bno++, fsbno++) {
147                         tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
148                         /*
149                          * Reserve log for one block zeroing.
150                          */
151                         if ((error = xfs_trans_reserve(tp, 0,
152                                         XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
153                                 goto error_cancel;
154                         /*
155                          * Lock the bitmap inode.
156                          */
157                         if ((error = xfs_trans_iget(mp, tp, ino, 0,
158                                                         XFS_ILOCK_EXCL, &ip)))
159                                 goto error_cancel;
160                         /*
161                          * Get a buffer for the block.
162                          */
163                         d = XFS_FSB_TO_DADDR(mp, fsbno);
164                         bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
165                                 mp->m_bsize, 0);
166                         if (bp == NULL) {
167                                 error = XFS_ERROR(EIO);
168 error_cancel:
169                                 xfs_trans_cancel(tp, cancelflags);
170                                 goto error;
171                         }
172                         memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
173                         xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
174                         /*
175                          * Commit the transaction.
176                          */
177                         error = xfs_trans_commit(tp, 0);
178                         if (error)
179                                 goto error;
180                 }
181                 /*
182                  * Go on to the next extent, if any.
183                  */
184                 oblocks = map.br_startoff + map.br_blockcount;
185         }
186         return 0;
187 error:
188         return error;
189 }
190
191 /*
192  * Attempt to allocate an extent minlen<=len<=maxlen starting from
193  * bitmap block bbno.  If we don't get maxlen then use prod to trim
194  * the length, if given.  Returns error; returns starting block in *rtblock.
195  * The lengths are all in rtextents.
196  */
197 STATIC int                              /* error */
198 xfs_rtallocate_extent_block(
199         xfs_mount_t     *mp,            /* file system mount point */
200         xfs_trans_t     *tp,            /* transaction pointer */
201         xfs_rtblock_t   bbno,           /* bitmap block number */
202         xfs_extlen_t    minlen,         /* minimum length to allocate */
203         xfs_extlen_t    maxlen,         /* maximum length to allocate */
204         xfs_extlen_t    *len,           /* out: actual length allocated */
205         xfs_rtblock_t   *nextp,         /* out: next block to try */
206         xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
207         xfs_fsblock_t   *rsb,           /* in/out: summary block number */
208         xfs_extlen_t    prod,           /* extent product factor */
209         xfs_rtblock_t   *rtblock)       /* out: start block allocated */
210 {
211         xfs_rtblock_t   besti;          /* best rtblock found so far */
212         xfs_rtblock_t   bestlen;        /* best length found so far */
213         xfs_rtblock_t   end;            /* last rtblock in chunk */
214         int             error;          /* error value */
215         xfs_rtblock_t   i;              /* current rtblock trying */
216         xfs_rtblock_t   next;           /* next rtblock to try */
217         int             stat;           /* status from internal calls */
218
219         /*
220          * Loop over all the extents starting in this bitmap block,
221          * looking for one that's long enough.
222          */
223         for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
224                 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
225              i <= end;
226              i++) {
227                 /*
228                  * See if there's a free extent of maxlen starting at i.
229                  * If it's not so then next will contain the first non-free.
230                  */
231                 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
232                 if (error) {
233                         return error;
234                 }
235                 if (stat) {
236                         /*
237                          * i for maxlen is all free, allocate and return that.
238                          */
239                         error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
240                                 rsb);
241                         if (error) {
242                                 return error;
243                         }
244                         *len = maxlen;
245                         *rtblock = i;
246                         return 0;
247                 }
248                 /*
249                  * In the case where we have a variable-sized allocation
250                  * request, figure out how big this free piece is,
251                  * and if it's big enough for the minimum, and the best
252                  * so far, remember it.
253                  */
254                 if (minlen < maxlen) {
255                         xfs_rtblock_t   thislen;        /* this extent size */
256
257                         thislen = next - i;
258                         if (thislen >= minlen && thislen > bestlen) {
259                                 besti = i;
260                                 bestlen = thislen;
261                         }
262                 }
263                 /*
264                  * If not done yet, find the start of the next free space.
265                  */
266                 if (next < end) {
267                         error = xfs_rtfind_forw(mp, tp, next, end, &i);
268                         if (error) {
269                                 return error;
270                         }
271                 } else
272                         break;
273         }
274         /*
275          * Searched the whole thing & didn't find a maxlen free extent.
276          */
277         if (minlen < maxlen && besti != -1) {
278                 xfs_extlen_t    p;      /* amount to trim length by */
279
280                 /*
281                  * If size should be a multiple of prod, make that so.
282                  */
283                 if (prod > 1 && (p = do_mod(bestlen, prod)))
284                         bestlen -= p;
285                 /*
286                  * Allocate besti for bestlen & return that.
287                  */
288                 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
289                 if (error) {
290                         return error;
291                 }
292                 *len = bestlen;
293                 *rtblock = besti;
294                 return 0;
295         }
296         /*
297          * Allocation failed.  Set *nextp to the next block to try.
298          */
299         *nextp = next;
300         *rtblock = NULLRTBLOCK;
301         return 0;
302 }
303
304 /*
305  * Allocate an extent of length minlen<=len<=maxlen, starting at block
306  * bno.  If we don't get maxlen then use prod to trim the length, if given.
307  * Returns error; returns starting block in *rtblock.
308  * The lengths are all in rtextents.
309  */
310 STATIC int                              /* error */
311 xfs_rtallocate_extent_exact(
312         xfs_mount_t     *mp,            /* file system mount point */
313         xfs_trans_t     *tp,            /* transaction pointer */
314         xfs_rtblock_t   bno,            /* starting block number to allocate */
315         xfs_extlen_t    minlen,         /* minimum length to allocate */
316         xfs_extlen_t    maxlen,         /* maximum length to allocate */
317         xfs_extlen_t    *len,           /* out: actual length allocated */
318         xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
319         xfs_fsblock_t   *rsb,           /* in/out: summary block number */
320         xfs_extlen_t    prod,           /* extent product factor */
321         xfs_rtblock_t   *rtblock)       /* out: start block allocated */
322 {
323         int             error;          /* error value */
324         xfs_extlen_t    i;              /* extent length trimmed due to prod */
325         int             isfree;         /* extent is free */
326         xfs_rtblock_t   next;           /* next block to try (dummy) */
327
328         ASSERT(minlen % prod == 0 && maxlen % prod == 0);
329         /*
330          * Check if the range in question (for maxlen) is free.
331          */
332         error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
333         if (error) {
334                 return error;
335         }
336         if (isfree) {
337                 /*
338                  * If it is, allocate it and return success.
339                  */
340                 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
341                 if (error) {
342                         return error;
343                 }
344                 *len = maxlen;
345                 *rtblock = bno;
346                 return 0;
347         }
348         /*
349          * If not, allocate what there is, if it's at least minlen.
350          */
351         maxlen = next - bno;
352         if (maxlen < minlen) {
353                 /*
354                  * Failed, return failure status.
355                  */
356                 *rtblock = NULLRTBLOCK;
357                 return 0;
358         }
359         /*
360          * Trim off tail of extent, if prod is specified.
361          */
362         if (prod > 1 && (i = maxlen % prod)) {
363                 maxlen -= i;
364                 if (maxlen < minlen) {
365                         /*
366                          * Now we can't do it, return failure status.
367                          */
368                         *rtblock = NULLRTBLOCK;
369                         return 0;
370                 }
371         }
372         /*
373          * Allocate what we can and return it.
374          */
375         error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
376         if (error) {
377                 return error;
378         }
379         *len = maxlen;
380         *rtblock = bno;
381         return 0;
382 }
383
384 /*
385  * Allocate an extent of length minlen<=len<=maxlen, starting as near
386  * to bno as possible.  If we don't get maxlen then use prod to trim
387  * the length, if given.  The lengths are all in rtextents.
388  */
389 STATIC int                              /* error */
390 xfs_rtallocate_extent_near(
391         xfs_mount_t     *mp,            /* file system mount point */
392         xfs_trans_t     *tp,            /* transaction pointer */
393         xfs_rtblock_t   bno,            /* starting block number to allocate */
394         xfs_extlen_t    minlen,         /* minimum length to allocate */
395         xfs_extlen_t    maxlen,         /* maximum length to allocate */
396         xfs_extlen_t    *len,           /* out: actual length allocated */
397         xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
398         xfs_fsblock_t   *rsb,           /* in/out: summary block number */
399         xfs_extlen_t    prod,           /* extent product factor */
400         xfs_rtblock_t   *rtblock)       /* out: start block allocated */
401 {
402         int             any;            /* any useful extents from summary */
403         xfs_rtblock_t   bbno;           /* bitmap block number */
404         int             error;          /* error value */
405         int             i;              /* bitmap block offset (loop control) */
406         int             j;              /* secondary loop control */
407         int             log2len;        /* log2 of minlen */
408         xfs_rtblock_t   n;              /* next block to try */
409         xfs_rtblock_t   r;              /* result block */
410
411         ASSERT(minlen % prod == 0 && maxlen % prod == 0);
412         /*
413          * If the block number given is off the end, silently set it to
414          * the last block.
415          */
416         if (bno >= mp->m_sb.sb_rextents)
417                 bno = mp->m_sb.sb_rextents - 1;
418         /*
419          * Try the exact allocation first.
420          */
421         error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
422                 rbpp, rsb, prod, &r);
423         if (error) {
424                 return error;
425         }
426         /*
427          * If the exact allocation worked, return that.
428          */
429         if (r != NULLRTBLOCK) {
430                 *rtblock = r;
431                 return 0;
432         }
433         bbno = XFS_BITTOBLOCK(mp, bno);
434         i = 0;
435         ASSERT(minlen != 0);
436         log2len = xfs_highbit32(minlen);
437         /*
438          * Loop over all bitmap blocks (bbno + i is current block).
439          */
440         for (;;) {
441                 /*
442                  * Get summary information of extents of all useful levels
443                  * starting in this bitmap block.
444                  */
445                 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
446                         bbno + i, rbpp, rsb, &any);
447                 if (error) {
448                         return error;
449                 }
450                 /*
451                  * If there are any useful extents starting here, try
452                  * allocating one.
453                  */
454                 if (any) {
455                         /*
456                          * On the positive side of the starting location.
457                          */
458                         if (i >= 0) {
459                                 /*
460                                  * Try to allocate an extent starting in
461                                  * this block.
462                                  */
463                                 error = xfs_rtallocate_extent_block(mp, tp,
464                                         bbno + i, minlen, maxlen, len, &n, rbpp,
465                                         rsb, prod, &r);
466                                 if (error) {
467                                         return error;
468                                 }
469                                 /*
470                                  * If it worked, return it.
471                                  */
472                                 if (r != NULLRTBLOCK) {
473                                         *rtblock = r;
474                                         return 0;
475                                 }
476                         }
477                         /*
478                          * On the negative side of the starting location.
479                          */
480                         else {          /* i < 0 */
481                                 /*
482                                  * Loop backwards through the bitmap blocks from
483                                  * the starting point-1 up to where we are now.
484                                  * There should be an extent which ends in this
485                                  * bitmap block and is long enough.
486                                  */
487                                 for (j = -1; j > i; j--) {
488                                         /*
489                                          * Grab the summary information for
490                                          * this bitmap block.
491                                          */
492                                         error = xfs_rtany_summary(mp, tp,
493                                                 log2len, mp->m_rsumlevels - 1,
494                                                 bbno + j, rbpp, rsb, &any);
495                                         if (error) {
496                                                 return error;
497                                         }
498                                         /*
499                                          * If there's no extent given in the
500                                          * summary that means the extent we
501                                          * found must carry over from an
502                                          * earlier block.  If there is an
503                                          * extent given, we've already tried
504                                          * that allocation, don't do it again.
505                                          */
506                                         if (any)
507                                                 continue;
508                                         error = xfs_rtallocate_extent_block(mp,
509                                                 tp, bbno + j, minlen, maxlen,
510                                                 len, &n, rbpp, rsb, prod, &r);
511                                         if (error) {
512                                                 return error;
513                                         }
514                                         /*
515                                          * If it works, return the extent.
516                                          */
517                                         if (r != NULLRTBLOCK) {
518                                                 *rtblock = r;
519                                                 return 0;
520                                         }
521                                 }
522                                 /*
523                                  * There weren't intervening bitmap blocks
524                                  * with a long enough extent, or the
525                                  * allocation didn't work for some reason
526                                  * (i.e. it's a little * too short).
527                                  * Try to allocate from the summary block
528                                  * that we found.
529                                  */
530                                 error = xfs_rtallocate_extent_block(mp, tp,
531                                         bbno + i, minlen, maxlen, len, &n, rbpp,
532                                         rsb, prod, &r);
533                                 if (error) {
534                                         return error;
535                                 }
536                                 /*
537                                  * If it works, return the extent.
538                                  */
539                                 if (r != NULLRTBLOCK) {
540                                         *rtblock = r;
541                                         return 0;
542                                 }
543                         }
544                 }
545                 /*
546                  * Loop control.  If we were on the positive side, and there's
547                  * still more blocks on the negative side, go there.
548                  */
549                 if (i > 0 && (int)bbno - i >= 0)
550                         i = -i;
551                 /*
552                  * If positive, and no more negative, but there are more
553                  * positive, go there.
554                  */
555                 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
556                         i++;
557                 /*
558                  * If negative or 0 (just started), and there are positive
559                  * blocks to go, go there.  The 0 case moves to block 1.
560                  */
561                 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
562                         i = 1 - i;
563                 /*
564                  * If negative or 0 and there are more negative blocks,
565                  * go there.
566                  */
567                 else if (i <= 0 && (int)bbno + i > 0)
568                         i--;
569                 /*
570                  * Must be done.  Return failure.
571                  */
572                 else
573                         break;
574         }
575         *rtblock = NULLRTBLOCK;
576         return 0;
577 }
578
579 /*
580  * Allocate an extent of length minlen<=len<=maxlen, with no position
581  * specified.  If we don't get maxlen then use prod to trim
582  * the length, if given.  The lengths are all in rtextents.
583  */
584 STATIC int                              /* error */
585 xfs_rtallocate_extent_size(
586         xfs_mount_t     *mp,            /* file system mount point */
587         xfs_trans_t     *tp,            /* transaction pointer */
588         xfs_extlen_t    minlen,         /* minimum length to allocate */
589         xfs_extlen_t    maxlen,         /* maximum length to allocate */
590         xfs_extlen_t    *len,           /* out: actual length allocated */
591         xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
592         xfs_fsblock_t   *rsb,           /* in/out: summary block number */
593         xfs_extlen_t    prod,           /* extent product factor */
594         xfs_rtblock_t   *rtblock)       /* out: start block allocated */
595 {
596         int             error;          /* error value */
597         int             i;              /* bitmap block number */
598         int             l;              /* level number (loop control) */
599         xfs_rtblock_t   n;              /* next block to be tried */
600         xfs_rtblock_t   r;              /* result block number */
601         xfs_suminfo_t   sum;            /* summary information for extents */
602
603         ASSERT(minlen % prod == 0 && maxlen % prod == 0);
604         ASSERT(maxlen != 0);
605
606         /*
607          * Loop over all the levels starting with maxlen.
608          * At each level, look at all the bitmap blocks, to see if there
609          * are extents starting there that are long enough (>= maxlen).
610          * Note, only on the initial level can the allocation fail if
611          * the summary says there's an extent.
612          */
613         for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
614                 /*
615                  * Loop over all the bitmap blocks.
616                  */
617                 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
618                         /*
619                          * Get the summary for this level/block.
620                          */
621                         error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
622                                 &sum);
623                         if (error) {
624                                 return error;
625                         }
626                         /*
627                          * Nothing there, on to the next block.
628                          */
629                         if (!sum)
630                                 continue;
631                         /*
632                          * Try allocating the extent.
633                          */
634                         error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
635                                 maxlen, len, &n, rbpp, rsb, prod, &r);
636                         if (error) {
637                                 return error;
638                         }
639                         /*
640                          * If it worked, return that.
641                          */
642                         if (r != NULLRTBLOCK) {
643                                 *rtblock = r;
644                                 return 0;
645                         }
646                         /*
647                          * If the "next block to try" returned from the
648                          * allocator is beyond the next bitmap block,
649                          * skip to that bitmap block.
650                          */
651                         if (XFS_BITTOBLOCK(mp, n) > i + 1)
652                                 i = XFS_BITTOBLOCK(mp, n) - 1;
653                 }
654         }
655         /*
656          * Didn't find any maxlen blocks.  Try smaller ones, unless
657          * we're asking for a fixed size extent.
658          */
659         if (minlen > --maxlen) {
660                 *rtblock = NULLRTBLOCK;
661                 return 0;
662         }
663         ASSERT(minlen != 0);
664         ASSERT(maxlen != 0);
665
666         /*
667          * Loop over sizes, from maxlen down to minlen.
668          * This time, when we do the allocations, allow smaller ones
669          * to succeed.
670          */
671         for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
672                 /*
673                  * Loop over all the bitmap blocks, try an allocation
674                  * starting in that block.
675                  */
676                 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
677                         /*
678                          * Get the summary information for this level/block.
679                          */
680                         error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
681                                                   &sum);
682                         if (error) {
683                                 return error;
684                         }
685                         /*
686                          * If nothing there, go on to next.
687                          */
688                         if (!sum)
689                                 continue;
690                         /*
691                          * Try the allocation.  Make sure the specified
692                          * minlen/maxlen are in the possible range for
693                          * this summary level.
694                          */
695                         error = xfs_rtallocate_extent_block(mp, tp, i,
696                                         XFS_RTMAX(minlen, 1 << l),
697                                         XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
698                                         len, &n, rbpp, rsb, prod, &r);
699                         if (error) {
700                                 return error;
701                         }
702                         /*
703                          * If it worked, return that extent.
704                          */
705                         if (r != NULLRTBLOCK) {
706                                 *rtblock = r;
707                                 return 0;
708                         }
709                         /*
710                          * If the "next block to try" returned from the
711                          * allocator is beyond the next bitmap block,
712                          * skip to that bitmap block.
713                          */
714                         if (XFS_BITTOBLOCK(mp, n) > i + 1)
715                                 i = XFS_BITTOBLOCK(mp, n) - 1;
716                 }
717         }
718         /*
719          * Got nothing, return failure.
720          */
721         *rtblock = NULLRTBLOCK;
722         return 0;
723 }
724
725 /*
726  * Mark an extent specified by start and len allocated.
727  * Updates all the summary information as well as the bitmap.
728  */
729 STATIC int                              /* error */
730 xfs_rtallocate_range(
731         xfs_mount_t     *mp,            /* file system mount point */
732         xfs_trans_t     *tp,            /* transaction pointer */
733         xfs_rtblock_t   start,          /* start block to allocate */
734         xfs_extlen_t    len,            /* length to allocate */
735         xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
736         xfs_fsblock_t   *rsb)           /* in/out: summary block number */
737 {
738         xfs_rtblock_t   end;            /* end of the allocated extent */
739         int             error;          /* error value */
740         xfs_rtblock_t   postblock;      /* first block allocated > end */
741         xfs_rtblock_t   preblock;       /* first block allocated < start */
742
743         end = start + len - 1;
744         /*
745          * Assume we're allocating out of the middle of a free extent.
746          * We need to find the beginning and end of the extent so we can
747          * properly update the summary.
748          */
749         error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
750         if (error) {
751                 return error;
752         }
753         /*
754          * Find the next allocated block (end of free extent).
755          */
756         error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
757                 &postblock);
758         if (error) {
759                 return error;
760         }
761         /*
762          * Decrement the summary information corresponding to the entire
763          * (old) free extent.
764          */
765         error = xfs_rtmodify_summary(mp, tp,
766                 XFS_RTBLOCKLOG(postblock + 1 - preblock),
767                 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
768         if (error) {
769                 return error;
770         }
771         /*
772          * If there are blocks not being allocated at the front of the
773          * old extent, add summary data for them to be free.
774          */
775         if (preblock < start) {
776                 error = xfs_rtmodify_summary(mp, tp,
777                         XFS_RTBLOCKLOG(start - preblock),
778                         XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
779                 if (error) {
780                         return error;
781                 }
782         }
783         /*
784          * If there are blocks not being allocated at the end of the
785          * old extent, add summary data for them to be free.
786          */
787         if (postblock > end) {
788                 error = xfs_rtmodify_summary(mp, tp,
789                         XFS_RTBLOCKLOG(postblock - end),
790                         XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
791                 if (error) {
792                         return error;
793                 }
794         }
795         /*
796          * Modify the bitmap to mark this extent allocated.
797          */
798         error = xfs_rtmodify_range(mp, tp, start, len, 0);
799         return error;
800 }
801
802 /*
803  * Return whether there are any free extents in the size range given
804  * by low and high, for the bitmap block bbno.
805  */
806 STATIC int                              /* error */
807 xfs_rtany_summary(
808         xfs_mount_t     *mp,            /* file system mount structure */
809         xfs_trans_t     *tp,            /* transaction pointer */
810         int             low,            /* low log2 extent size */
811         int             high,           /* high log2 extent size */
812         xfs_rtblock_t   bbno,           /* bitmap block number */
813         xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
814         xfs_fsblock_t   *rsb,           /* in/out: summary block number */
815         int             *stat)          /* out: any good extents here? */
816 {
817         int             error;          /* error value */
818         int             log;            /* loop counter, log2 of ext. size */
819         xfs_suminfo_t   sum;            /* summary data */
820
821         /*
822          * Loop over logs of extent sizes.  Order is irrelevant.
823          */
824         for (log = low; log <= high; log++) {
825                 /*
826                  * Get one summary datum.
827                  */
828                 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
829                 if (error) {
830                         return error;
831                 }
832                 /*
833                  * If there are any, return success.
834                  */
835                 if (sum) {
836                         *stat = 1;
837                         return 0;
838                 }
839         }
840         /*
841          * Found nothing, return failure.
842          */
843         *stat = 0;
844         return 0;
845 }
846
847 /*
848  * Get a buffer for the bitmap or summary file block specified.
849  * The buffer is returned read and locked.
850  */
851 STATIC int                              /* error */
852 xfs_rtbuf_get(
853         xfs_mount_t     *mp,            /* file system mount structure */
854         xfs_trans_t     *tp,            /* transaction pointer */
855         xfs_rtblock_t   block,          /* block number in bitmap or summary */
856         int             issum,          /* is summary not bitmap */
857         xfs_buf_t       **bpp)          /* output: buffer for the block */
858 {
859         xfs_buf_t       *bp;            /* block buffer, result */
860         xfs_daddr_t     d;              /* disk addr of block */
861         int             error;          /* error value */
862         xfs_fsblock_t   fsb;            /* fs block number for block */
863         xfs_inode_t     *ip;            /* bitmap or summary inode */
864
865         ip = issum ? mp->m_rsumip : mp->m_rbmip;
866         /*
867          * Map from the file offset (block) and inode number to the
868          * file system block.
869          */
870         error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
871         if (error) {
872                 return error;
873         }
874         ASSERT(fsb != NULLFSBLOCK);
875         /*
876          * Convert to disk address for buffer cache.
877          */
878         d = XFS_FSB_TO_DADDR(mp, fsb);
879         /*
880          * Read the buffer.
881          */
882         error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
883                                    mp->m_bsize, 0, &bp);
884         if (error) {
885                 return error;
886         }
887         ASSERT(bp && !XFS_BUF_GETERROR(bp));
888         *bpp = bp;
889         return 0;
890 }
891
892 #ifdef DEBUG
893 /*
894  * Check that the given extent (block range) is allocated already.
895  */
896 STATIC int                              /* error */
897 xfs_rtcheck_alloc_range(
898         xfs_mount_t     *mp,            /* file system mount point */
899         xfs_trans_t     *tp,            /* transaction pointer */
900         xfs_rtblock_t   bno,            /* starting block number of extent */
901         xfs_extlen_t    len,            /* length of extent */
902         int             *stat)          /* out: 1 for allocated, 0 for not */
903 {
904         xfs_rtblock_t   new;            /* dummy for xfs_rtcheck_range */
905
906         return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
907 }
908 #endif
909
910 /*
911  * Check that the given range is either all allocated (val = 0) or
912  * all free (val = 1).
913  */
914 STATIC int                              /* error */
915 xfs_rtcheck_range(
916         xfs_mount_t     *mp,            /* file system mount point */
917         xfs_trans_t     *tp,            /* transaction pointer */
918         xfs_rtblock_t   start,          /* starting block number of extent */
919         xfs_extlen_t    len,            /* length of extent */
920         int             val,            /* 1 for free, 0 for allocated */
921         xfs_rtblock_t   *new,           /* out: first block not matching */
922         int             *stat)          /* out: 1 for matches, 0 for not */
923 {
924         xfs_rtword_t    *b;             /* current word in buffer */
925         int             bit;            /* bit number in the word */
926         xfs_rtblock_t   block;          /* bitmap block number */
927         xfs_buf_t       *bp;            /* buf for the block */
928         xfs_rtword_t    *bufp;          /* starting word in buffer */
929         int             error;          /* error value */
930         xfs_rtblock_t   i;              /* current bit number rel. to start */
931         xfs_rtblock_t   lastbit;        /* last useful bit in word */
932         xfs_rtword_t    mask;           /* mask of relevant bits for value */
933         xfs_rtword_t    wdiff;          /* difference from wanted value */
934         int             word;           /* word number in the buffer */
935
936         /*
937          * Compute starting bitmap block number
938          */
939         block = XFS_BITTOBLOCK(mp, start);
940         /*
941          * Read the bitmap block.
942          */
943         error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
944         if (error) {
945                 return error;
946         }
947         bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
948         /*
949          * Compute the starting word's address, and starting bit.
950          */
951         word = XFS_BITTOWORD(mp, start);
952         b = &bufp[word];
953         bit = (int)(start & (XFS_NBWORD - 1));
954         /*
955          * 0 (allocated) => all zero's; 1 (free) => all one's.
956          */
957         val = -val;
958         /*
959          * If not starting on a word boundary, deal with the first
960          * (partial) word.
961          */
962         if (bit) {
963                 /*
964                  * Compute first bit not examined.
965                  */
966                 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
967                 /*
968                  * Mask of relevant bits.
969                  */
970                 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
971                 /*
972                  * Compute difference between actual and desired value.
973                  */
974                 if ((wdiff = (*b ^ val) & mask)) {
975                         /*
976                          * Different, compute first wrong bit and return.
977                          */
978                         xfs_trans_brelse(tp, bp);
979                         i = XFS_RTLOBIT(wdiff) - bit;
980                         *new = start + i;
981                         *stat = 0;
982                         return 0;
983                 }
984                 i = lastbit - bit;
985                 /*
986                  * Go on to next block if that's where the next word is
987                  * and we need the next word.
988                  */
989                 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
990                         /*
991                          * If done with this block, get the next one.
992                          */
993                         xfs_trans_brelse(tp, bp);
994                         error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
995                         if (error) {
996                                 return error;
997                         }
998                         b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
999                         word = 0;
1000                 } else {
1001                         /*
1002                          * Go on to the next word in the buffer.
1003                          */
1004                         b++;
1005                 }
1006         } else {
1007                 /*
1008                  * Starting on a word boundary, no partial word.
1009                  */
1010                 i = 0;
1011         }
1012         /*
1013          * Loop over whole words in buffers.  When we use up one buffer
1014          * we move on to the next one.
1015          */
1016         while (len - i >= XFS_NBWORD) {
1017                 /*
1018                  * Compute difference between actual and desired value.
1019                  */
1020                 if ((wdiff = *b ^ val)) {
1021                         /*
1022                          * Different, compute first wrong bit and return.
1023                          */
1024                         xfs_trans_brelse(tp, bp);
1025                         i += XFS_RTLOBIT(wdiff);
1026                         *new = start + i;
1027                         *stat = 0;
1028                         return 0;
1029                 }
1030                 i += XFS_NBWORD;
1031                 /*
1032                  * Go on to next block if that's where the next word is
1033                  * and we need the next word.
1034                  */
1035                 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1036                         /*
1037                          * If done with this block, get the next one.
1038                          */
1039                         xfs_trans_brelse(tp, bp);
1040                         error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1041                         if (error) {
1042                                 return error;
1043                         }
1044                         b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1045                         word = 0;
1046                 } else {
1047                         /*
1048                          * Go on to the next word in the buffer.
1049                          */
1050                         b++;
1051                 }
1052         }
1053         /*
1054          * If not ending on a word boundary, deal with the last
1055          * (partial) word.
1056          */
1057         if ((lastbit = len - i)) {
1058                 /*
1059                  * Mask of relevant bits.
1060                  */
1061                 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1062                 /*
1063                  * Compute difference between actual and desired value.
1064                  */
1065                 if ((wdiff = (*b ^ val) & mask)) {
1066                         /*
1067                          * Different, compute first wrong bit and return.
1068                          */
1069                         xfs_trans_brelse(tp, bp);
1070                         i += XFS_RTLOBIT(wdiff);
1071                         *new = start + i;
1072                         *stat = 0;
1073                         return 0;
1074                 } else
1075                         i = len;
1076         }
1077         /*
1078          * Successful, return.
1079          */
1080         xfs_trans_brelse(tp, bp);
1081         *new = start + i;
1082         *stat = 1;
1083         return 0;
1084 }
1085
1086 /*
1087  * Copy and transform the summary file, given the old and new
1088  * parameters in the mount structures.
1089  */
1090 STATIC int                              /* error */
1091 xfs_rtcopy_summary(
1092         xfs_mount_t     *omp,           /* old file system mount point */
1093         xfs_mount_t     *nmp,           /* new file system mount point */
1094         xfs_trans_t     *tp)            /* transaction pointer */
1095 {
1096         xfs_rtblock_t   bbno;           /* bitmap block number */
1097         xfs_buf_t       *bp;            /* summary buffer */
1098         int             error;          /* error return value */
1099         int             log;            /* summary level number (log length) */
1100         xfs_suminfo_t   sum;            /* summary data */
1101         xfs_fsblock_t   sumbno;         /* summary block number */
1102
1103         bp = NULL;
1104         for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1105                 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1106                      (xfs_srtblock_t)bbno >= 0;
1107                      bbno--) {
1108                         error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1109                                 &sumbno, &sum);
1110                         if (error)
1111                                 return error;
1112                         if (sum == 0)
1113                                 continue;
1114                         error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1115                                 &bp, &sumbno);
1116                         if (error)
1117                                 return error;
1118                         error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1119                                 &bp, &sumbno);
1120                         if (error)
1121                                 return error;
1122                         ASSERT(sum > 0);
1123                 }
1124         }
1125         return 0;
1126 }
1127
1128 /*
1129  * Searching backward from start to limit, find the first block whose
1130  * allocated/free state is different from start's.
1131  */
1132 STATIC int                              /* error */
1133 xfs_rtfind_back(
1134         xfs_mount_t     *mp,            /* file system mount point */
1135         xfs_trans_t     *tp,            /* transaction pointer */
1136         xfs_rtblock_t   start,          /* starting block to look at */
1137         xfs_rtblock_t   limit,          /* last block to look at */
1138         xfs_rtblock_t   *rtblock)       /* out: start block found */
1139 {
1140         xfs_rtword_t    *b;             /* current word in buffer */
1141         int             bit;            /* bit number in the word */
1142         xfs_rtblock_t   block;          /* bitmap block number */
1143         xfs_buf_t       *bp;            /* buf for the block */
1144         xfs_rtword_t    *bufp;          /* starting word in buffer */
1145         int             error;          /* error value */
1146         xfs_rtblock_t   firstbit;       /* first useful bit in the word */
1147         xfs_rtblock_t   i;              /* current bit number rel. to start */
1148         xfs_rtblock_t   len;            /* length of inspected area */
1149         xfs_rtword_t    mask;           /* mask of relevant bits for value */
1150         xfs_rtword_t    want;           /* mask for "good" values */
1151         xfs_rtword_t    wdiff;          /* difference from wanted value */
1152         int             word;           /* word number in the buffer */
1153
1154         /*
1155          * Compute and read in starting bitmap block for starting block.
1156          */
1157         block = XFS_BITTOBLOCK(mp, start);
1158         error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1159         if (error) {
1160                 return error;
1161         }
1162         bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1163         /*
1164          * Get the first word's index & point to it.
1165          */
1166         word = XFS_BITTOWORD(mp, start);
1167         b = &bufp[word];
1168         bit = (int)(start & (XFS_NBWORD - 1));
1169         len = start - limit + 1;
1170         /*
1171          * Compute match value, based on the bit at start: if 1 (free)
1172          * then all-ones, else all-zeroes.
1173          */
1174         want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1175         /*
1176          * If the starting position is not word-aligned, deal with the
1177          * partial word.
1178          */
1179         if (bit < XFS_NBWORD - 1) {
1180                 /*
1181                  * Calculate first (leftmost) bit number to look at,
1182                  * and mask for all the relevant bits in this word.
1183                  */
1184                 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1185                 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1186                         firstbit;
1187                 /*
1188                  * Calculate the difference between the value there
1189                  * and what we're looking for.
1190                  */
1191                 if ((wdiff = (*b ^ want) & mask)) {
1192                         /*
1193                          * Different.  Mark where we are and return.
1194                          */
1195                         xfs_trans_brelse(tp, bp);
1196                         i = bit - XFS_RTHIBIT(wdiff);
1197                         *rtblock = start - i + 1;
1198                         return 0;
1199                 }
1200                 i = bit - firstbit + 1;
1201                 /*
1202                  * Go on to previous block if that's where the previous word is
1203                  * and we need the previous word.
1204                  */
1205                 if (--word == -1 && i < len) {
1206                         /*
1207                          * If done with this block, get the previous one.
1208                          */
1209                         xfs_trans_brelse(tp, bp);
1210                         error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1211                         if (error) {
1212                                 return error;
1213                         }
1214                         bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1215                         word = XFS_BLOCKWMASK(mp);
1216                         b = &bufp[word];
1217                 } else {
1218                         /*
1219                          * Go on to the previous word in the buffer.
1220                          */
1221                         b--;
1222                 }
1223         } else {
1224                 /*
1225                  * Starting on a word boundary, no partial word.
1226                  */
1227                 i = 0;
1228         }
1229         /*
1230          * Loop over whole words in buffers.  When we use up one buffer
1231          * we move on to the previous one.
1232          */
1233         while (len - i >= XFS_NBWORD) {
1234                 /*
1235                  * Compute difference between actual and desired value.
1236                  */
1237                 if ((wdiff = *b ^ want)) {
1238                         /*
1239                          * Different, mark where we are and return.
1240                          */
1241                         xfs_trans_brelse(tp, bp);
1242                         i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1243                         *rtblock = start - i + 1;
1244                         return 0;
1245                 }
1246                 i += XFS_NBWORD;
1247                 /*
1248                  * Go on to previous block if that's where the previous word is
1249                  * and we need the previous word.
1250                  */
1251                 if (--word == -1 && i < len) {
1252                         /*
1253                          * If done with this block, get the previous one.
1254                          */
1255                         xfs_trans_brelse(tp, bp);
1256                         error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1257                         if (error) {
1258                                 return error;
1259                         }
1260                         bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1261                         word = XFS_BLOCKWMASK(mp);
1262                         b = &bufp[word];
1263                 } else {
1264                         /*
1265                          * Go on to the previous word in the buffer.
1266                          */
1267                         b--;
1268                 }
1269         }
1270         /*
1271          * If not ending on a word boundary, deal with the last
1272          * (partial) word.
1273          */
1274         if (len - i) {
1275                 /*
1276                  * Calculate first (leftmost) bit number to look at,
1277                  * and mask for all the relevant bits in this word.
1278                  */
1279                 firstbit = XFS_NBWORD - (len - i);
1280                 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1281                 /*
1282                  * Compute difference between actual and desired value.
1283                  */
1284                 if ((wdiff = (*b ^ want) & mask)) {
1285                         /*
1286                          * Different, mark where we are and return.
1287                          */
1288                         xfs_trans_brelse(tp, bp);
1289                         i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1290                         *rtblock = start - i + 1;
1291                         return 0;
1292                 } else
1293                         i = len;
1294         }
1295         /*
1296          * No match, return that we scanned the whole area.
1297          */
1298         xfs_trans_brelse(tp, bp);
1299         *rtblock = start - i + 1;
1300         return 0;
1301 }
1302
1303 /*
1304  * Searching forward from start to limit, find the first block whose
1305  * allocated/free state is different from start's.
1306  */
1307 STATIC int                              /* error */
1308 xfs_rtfind_forw(
1309         xfs_mount_t     *mp,            /* file system mount point */
1310         xfs_trans_t     *tp,            /* transaction pointer */
1311         xfs_rtblock_t   start,          /* starting block to look at */
1312         xfs_rtblock_t   limit,          /* last block to look at */
1313         xfs_rtblock_t   *rtblock)       /* out: start block found */
1314 {
1315         xfs_rtword_t    *b;             /* current word in buffer */
1316         int             bit;            /* bit number in the word */
1317         xfs_rtblock_t   block;          /* bitmap block number */
1318         xfs_buf_t       *bp;            /* buf for the block */
1319         xfs_rtword_t    *bufp;          /* starting word in buffer */
1320         int             error;          /* error value */
1321         xfs_rtblock_t   i;              /* current bit number rel. to start */
1322         xfs_rtblock_t   lastbit;        /* last useful bit in the word */
1323         xfs_rtblock_t   len;            /* length of inspected area */
1324         xfs_rtword_t    mask;           /* mask of relevant bits for value */
1325         xfs_rtword_t    want;           /* mask for "good" values */
1326         xfs_rtword_t    wdiff;          /* difference from wanted value */
1327         int             word;           /* word number in the buffer */
1328
1329         /*
1330          * Compute and read in starting bitmap block for starting block.
1331          */
1332         block = XFS_BITTOBLOCK(mp, start);
1333         error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1334         if (error) {
1335                 return error;
1336         }
1337         bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1338         /*
1339          * Get the first word's index & point to it.
1340          */
1341         word = XFS_BITTOWORD(mp, start);
1342         b = &bufp[word];
1343         bit = (int)(start & (XFS_NBWORD - 1));
1344         len = limit - start + 1;
1345         /*
1346          * Compute match value, based on the bit at start: if 1 (free)
1347          * then all-ones, else all-zeroes.
1348          */
1349         want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1350         /*
1351          * If the starting position is not word-aligned, deal with the
1352          * partial word.
1353          */
1354         if (bit) {
1355                 /*
1356                  * Calculate last (rightmost) bit number to look at,
1357                  * and mask for all the relevant bits in this word.
1358                  */
1359                 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1360                 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1361                 /*
1362                  * Calculate the difference between the value there
1363                  * and what we're looking for.
1364                  */
1365                 if ((wdiff = (*b ^ want) & mask)) {
1366                         /*
1367                          * Different.  Mark where we are and return.
1368                          */
1369                         xfs_trans_brelse(tp, bp);
1370                         i = XFS_RTLOBIT(wdiff) - bit;
1371                         *rtblock = start + i - 1;
1372                         return 0;
1373                 }
1374                 i = lastbit - bit;
1375                 /*
1376                  * Go on to next block if that's where the next word is
1377                  * and we need the next word.
1378                  */
1379                 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1380                         /*
1381                          * If done with this block, get the previous one.
1382                          */
1383                         xfs_trans_brelse(tp, bp);
1384                         error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1385                         if (error) {
1386                                 return error;
1387                         }
1388                         b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1389                         word = 0;
1390                 } else {
1391                         /*
1392                          * Go on to the previous word in the buffer.
1393                          */
1394                         b++;
1395                 }
1396         } else {
1397                 /*
1398                  * Starting on a word boundary, no partial word.
1399                  */
1400                 i = 0;
1401         }
1402         /*
1403          * Loop over whole words in buffers.  When we use up one buffer
1404          * we move on to the next one.
1405          */
1406         while (len - i >= XFS_NBWORD) {
1407                 /*
1408                  * Compute difference between actual and desired value.
1409                  */
1410                 if ((wdiff = *b ^ want)) {
1411                         /*
1412                          * Different, mark where we are and return.
1413                          */
1414                         xfs_trans_brelse(tp, bp);
1415                         i += XFS_RTLOBIT(wdiff);
1416                         *rtblock = start + i - 1;
1417                         return 0;
1418                 }
1419                 i += XFS_NBWORD;
1420                 /*
1421                  * Go on to next block if that's where the next word is
1422                  * and we need the next word.
1423                  */
1424                 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1425                         /*
1426                          * If done with this block, get the next one.
1427                          */
1428                         xfs_trans_brelse(tp, bp);
1429                         error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1430                         if (error) {
1431                                 return error;
1432                         }
1433                         b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1434                         word = 0;
1435                 } else {
1436                         /*
1437                          * Go on to the next word in the buffer.
1438                          */
1439                         b++;
1440                 }
1441         }
1442         /*
1443          * If not ending on a word boundary, deal with the last
1444          * (partial) word.
1445          */
1446         if ((lastbit = len - i)) {
1447                 /*
1448                  * Calculate mask for all the relevant bits in this word.
1449                  */
1450                 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1451                 /*
1452                  * Compute difference between actual and desired value.
1453                  */
1454                 if ((wdiff = (*b ^ want) & mask)) {
1455                         /*
1456                          * Different, mark where we are and return.
1457                          */
1458                         xfs_trans_brelse(tp, bp);
1459                         i += XFS_RTLOBIT(wdiff);
1460                         *rtblock = start + i - 1;
1461                         return 0;
1462                 } else
1463                         i = len;
1464         }
1465         /*
1466          * No match, return that we scanned the whole area.
1467          */
1468         xfs_trans_brelse(tp, bp);
1469         *rtblock = start + i - 1;
1470         return 0;
1471 }
1472
1473 /*
1474  * Mark an extent specified by start and len freed.
1475  * Updates all the summary information as well as the bitmap.
1476  */
1477 STATIC int                              /* error */
1478 xfs_rtfree_range(
1479         xfs_mount_t     *mp,            /* file system mount point */
1480         xfs_trans_t     *tp,            /* transaction pointer */
1481         xfs_rtblock_t   start,          /* starting block to free */
1482         xfs_extlen_t    len,            /* length to free */
1483         xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
1484         xfs_fsblock_t   *rsb)           /* in/out: summary block number */
1485 {
1486         xfs_rtblock_t   end;            /* end of the freed extent */
1487         int             error;          /* error value */
1488         xfs_rtblock_t   postblock;      /* first block freed > end */
1489         xfs_rtblock_t   preblock;       /* first block freed < start */
1490
1491         end = start + len - 1;
1492         /*
1493          * Modify the bitmap to mark this extent freed.
1494          */
1495         error = xfs_rtmodify_range(mp, tp, start, len, 1);
1496         if (error) {
1497                 return error;
1498         }
1499         /*
1500          * Assume we're freeing out of the middle of an allocated extent.
1501          * We need to find the beginning and end of the extent so we can
1502          * properly update the summary.
1503          */
1504         error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1505         if (error) {
1506                 return error;
1507         }
1508         /*
1509          * Find the next allocated block (end of allocated extent).
1510          */
1511         error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1512                 &postblock);
1513         if (error)
1514                 return error;
1515         /*
1516          * If there are blocks not being freed at the front of the
1517          * old extent, add summary data for them to be allocated.
1518          */
1519         if (preblock < start) {
1520                 error = xfs_rtmodify_summary(mp, tp,
1521                         XFS_RTBLOCKLOG(start - preblock),
1522                         XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1523                 if (error) {
1524                         return error;
1525                 }
1526         }
1527         /*
1528          * If there are blocks not being freed at the end of the
1529          * old extent, add summary data for them to be allocated.
1530          */
1531         if (postblock > end) {
1532                 error = xfs_rtmodify_summary(mp, tp,
1533                         XFS_RTBLOCKLOG(postblock - end),
1534                         XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1535                 if (error) {
1536                         return error;
1537                 }
1538         }
1539         /*
1540          * Increment the summary information corresponding to the entire
1541          * (new) free extent.
1542          */
1543         error = xfs_rtmodify_summary(mp, tp,
1544                 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1545                 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1546         return error;
1547 }
1548
1549 /*
1550  * Read and return the summary information for a given extent size,
1551  * bitmap block combination.
1552  * Keeps track of a current summary block, so we don't keep reading
1553  * it from the buffer cache.
1554  */
1555 STATIC int                              /* error */
1556 xfs_rtget_summary(
1557         xfs_mount_t     *mp,            /* file system mount structure */
1558         xfs_trans_t     *tp,            /* transaction pointer */
1559         int             log,            /* log2 of extent size */
1560         xfs_rtblock_t   bbno,           /* bitmap block number */
1561         xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
1562         xfs_fsblock_t   *rsb,           /* in/out: summary block number */
1563         xfs_suminfo_t   *sum)           /* out: summary info for this block */
1564 {
1565         xfs_buf_t       *bp;            /* buffer for summary block */
1566         int             error;          /* error value */
1567         xfs_fsblock_t   sb;             /* summary fsblock */
1568         int             so;             /* index into the summary file */
1569         xfs_suminfo_t   *sp;            /* pointer to returned data */
1570
1571         /*
1572          * Compute entry number in the summary file.
1573          */
1574         so = XFS_SUMOFFS(mp, log, bbno);
1575         /*
1576          * Compute the block number in the summary file.
1577          */
1578         sb = XFS_SUMOFFSTOBLOCK(mp, so);
1579         /*
1580          * If we have an old buffer, and the block number matches, use that.
1581          */
1582         if (rbpp && *rbpp && *rsb == sb)
1583                 bp = *rbpp;
1584         /*
1585          * Otherwise we have to get the buffer.
1586          */
1587         else {
1588                 /*
1589                  * If there was an old one, get rid of it first.
1590                  */
1591                 if (rbpp && *rbpp)
1592                         xfs_trans_brelse(tp, *rbpp);
1593                 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1594                 if (error) {
1595                         return error;
1596                 }
1597                 /*
1598                  * Remember this buffer and block for the next call.
1599                  */
1600                 if (rbpp) {
1601                         *rbpp = bp;
1602                         *rsb = sb;
1603                 }
1604         }
1605         /*
1606          * Point to the summary information & copy it out.
1607          */
1608         sp = XFS_SUMPTR(mp, bp, so);
1609         *sum = *sp;
1610         /*
1611          * Drop the buffer if we're not asked to remember it.
1612          */
1613         if (!rbpp)
1614                 xfs_trans_brelse(tp, bp);
1615         return 0;
1616 }
1617
1618 /*
1619  * Set the given range of bitmap bits to the given value.
1620  * Do whatever I/O and logging is required.
1621  */
1622 STATIC int                              /* error */
1623 xfs_rtmodify_range(
1624         xfs_mount_t     *mp,            /* file system mount point */
1625         xfs_trans_t     *tp,            /* transaction pointer */
1626         xfs_rtblock_t   start,          /* starting block to modify */
1627         xfs_extlen_t    len,            /* length of extent to modify */
1628         int             val)            /* 1 for free, 0 for allocated */
1629 {
1630         xfs_rtword_t    *b;             /* current word in buffer */
1631         int             bit;            /* bit number in the word */
1632         xfs_rtblock_t   block;          /* bitmap block number */
1633         xfs_buf_t       *bp;            /* buf for the block */
1634         xfs_rtword_t    *bufp;          /* starting word in buffer */
1635         int             error;          /* error value */
1636         xfs_rtword_t    *first;         /* first used word in the buffer */
1637         int             i;              /* current bit number rel. to start */
1638         int             lastbit;        /* last useful bit in word */
1639         xfs_rtword_t    mask;           /* mask o frelevant bits for value */
1640         int             word;           /* word number in the buffer */
1641
1642         /*
1643          * Compute starting bitmap block number.
1644          */
1645         block = XFS_BITTOBLOCK(mp, start);
1646         /*
1647          * Read the bitmap block, and point to its data.
1648          */
1649         error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1650         if (error) {
1651                 return error;
1652         }
1653         bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1654         /*
1655          * Compute the starting word's address, and starting bit.
1656          */
1657         word = XFS_BITTOWORD(mp, start);
1658         first = b = &bufp[word];
1659         bit = (int)(start & (XFS_NBWORD - 1));
1660         /*
1661          * 0 (allocated) => all zeroes; 1 (free) => all ones.
1662          */
1663         val = -val;
1664         /*
1665          * If not starting on a word boundary, deal with the first
1666          * (partial) word.
1667          */
1668         if (bit) {
1669                 /*
1670                  * Compute first bit not changed and mask of relevant bits.
1671                  */
1672                 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1673                 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1674                 /*
1675                  * Set/clear the active bits.
1676                  */
1677                 if (val)
1678                         *b |= mask;
1679                 else
1680                         *b &= ~mask;
1681                 i = lastbit - bit;
1682                 /*
1683                  * Go on to the next block if that's where the next word is
1684                  * and we need the next word.
1685                  */
1686                 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1687                         /*
1688                          * Log the changed part of this block.
1689                          * Get the next one.
1690                          */
1691                         xfs_trans_log_buf(tp, bp,
1692                                 (uint)((char *)first - (char *)bufp),
1693                                 (uint)((char *)b - (char *)bufp));
1694                         error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1695                         if (error) {
1696                                 return error;
1697                         }
1698                         first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1699                         word = 0;
1700                 } else {
1701                         /*
1702                          * Go on to the next word in the buffer
1703                          */
1704                         b++;
1705                 }
1706         } else {
1707                 /*
1708                  * Starting on a word boundary, no partial word.
1709                  */
1710                 i = 0;
1711         }
1712         /*
1713          * Loop over whole words in buffers.  When we use up one buffer
1714          * we move on to the next one.
1715          */
1716         while (len - i >= XFS_NBWORD) {
1717                 /*
1718                  * Set the word value correctly.
1719                  */
1720                 *b = val;
1721                 i += XFS_NBWORD;
1722                 /*
1723                  * Go on to the next block if that's where the next word is
1724                  * and we need the next word.
1725                  */
1726                 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1727                         /*
1728                          * Log the changed part of this block.
1729                          * Get the next one.
1730                          */
1731                         xfs_trans_log_buf(tp, bp,
1732                                 (uint)((char *)first - (char *)bufp),
1733                                 (uint)((char *)b - (char *)bufp));
1734                         error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1735                         if (error) {
1736                                 return error;
1737                         }
1738                         first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1739                         word = 0;
1740                 } else {
1741                         /*
1742                          * Go on to the next word in the buffer
1743                          */
1744                         b++;
1745                 }
1746         }
1747         /*
1748          * If not ending on a word boundary, deal with the last
1749          * (partial) word.
1750          */
1751         if ((lastbit = len - i)) {
1752                 /*
1753                  * Compute a mask of relevant bits.
1754                  */
1755                 bit = 0;
1756                 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1757                 /*
1758                  * Set/clear the active bits.
1759                  */
1760                 if (val)
1761                         *b |= mask;
1762                 else
1763                         *b &= ~mask;
1764                 b++;
1765         }
1766         /*
1767          * Log any remaining changed bytes.
1768          */
1769         if (b > first)
1770                 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1771                         (uint)((char *)b - (char *)bufp - 1));
1772         return 0;
1773 }
1774
1775 /*
1776  * Read and modify the summary information for a given extent size,
1777  * bitmap block combination.
1778  * Keeps track of a current summary block, so we don't keep reading
1779  * it from the buffer cache.
1780  */
1781 STATIC int                              /* error */
1782 xfs_rtmodify_summary(
1783         xfs_mount_t     *mp,            /* file system mount point */
1784         xfs_trans_t     *tp,            /* transaction pointer */
1785         int             log,            /* log2 of extent size */
1786         xfs_rtblock_t   bbno,           /* bitmap block number */
1787         int             delta,          /* change to make to summary info */
1788         xfs_buf_t       **rbpp,         /* in/out: summary block buffer */
1789         xfs_fsblock_t   *rsb)           /* in/out: summary block number */
1790 {
1791         xfs_buf_t       *bp;            /* buffer for the summary block */
1792         int             error;          /* error value */
1793         xfs_fsblock_t   sb;             /* summary fsblock */
1794         int             so;             /* index into the summary file */
1795         xfs_suminfo_t   *sp;            /* pointer to returned data */
1796
1797         /*
1798          * Compute entry number in the summary file.
1799          */
1800         so = XFS_SUMOFFS(mp, log, bbno);
1801         /*
1802          * Compute the block number in the summary file.
1803          */
1804         sb = XFS_SUMOFFSTOBLOCK(mp, so);
1805         /*
1806          * If we have an old buffer, and the block number matches, use that.
1807          */
1808         if (rbpp && *rbpp && *rsb == sb)
1809                 bp = *rbpp;
1810         /*
1811          * Otherwise we have to get the buffer.
1812          */
1813         else {
1814                 /*
1815                  * If there was an old one, get rid of it first.
1816                  */
1817                 if (rbpp && *rbpp)
1818                         xfs_trans_brelse(tp, *rbpp);
1819                 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1820                 if (error) {
1821                         return error;
1822                 }
1823                 /*
1824                  * Remember this buffer and block for the next call.
1825                  */
1826                 if (rbpp) {
1827                         *rbpp = bp;
1828                         *rsb = sb;
1829                 }
1830         }
1831         /*
1832          * Point to the summary information, modify and log it.
1833          */
1834         sp = XFS_SUMPTR(mp, bp, so);
1835         *sp += delta;
1836         xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1837                 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1838         return 0;
1839 }
1840
1841 /*
1842  * Visible (exported) functions.
1843  */
1844
1845 /*
1846  * Grow the realtime area of the filesystem.
1847  */
1848 int
1849 xfs_growfs_rt(
1850         xfs_mount_t     *mp,            /* mount point for filesystem */
1851         xfs_growfs_rt_t *in)            /* growfs rt input struct */
1852 {
1853         xfs_rtblock_t   bmbno;          /* bitmap block number */
1854         xfs_buf_t       *bp;            /* temporary buffer */
1855         int             error;          /* error return value */
1856         xfs_inode_t     *ip;            /* bitmap inode, used as lock */
1857         xfs_mount_t     *nmp;           /* new (fake) mount structure */
1858         xfs_drfsbno_t   nrblocks;       /* new number of realtime blocks */
1859         xfs_extlen_t    nrbmblocks;     /* new number of rt bitmap blocks */
1860         xfs_drtbno_t    nrextents;      /* new number of realtime extents */
1861         uint8_t         nrextslog;      /* new log2 of sb_rextents */
1862         xfs_extlen_t    nrsumblocks;    /* new number of summary blocks */
1863         uint            nrsumlevels;    /* new rt summary levels */
1864         uint            nrsumsize;      /* new size of rt summary, bytes */
1865         xfs_sb_t        *nsbp;          /* new superblock */
1866         xfs_extlen_t    rbmblocks;      /* current number of rt bitmap blocks */
1867         xfs_extlen_t    rsumblocks;     /* current number of rt summary blks */
1868         xfs_sb_t        *sbp;           /* old superblock */
1869         xfs_fsblock_t   sumbno;         /* summary block number */
1870
1871         sbp = &mp->m_sb;
1872         /*
1873          * Initial error checking.
1874          */
1875         if (!capable(CAP_SYS_ADMIN))
1876                 return XFS_ERROR(EPERM);
1877         if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1878             (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1879             (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1880                 return XFS_ERROR(EINVAL);
1881         if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
1882                 return error;
1883         /*
1884          * Read in the last block of the device, make sure it exists.
1885          */
1886         error = xfs_read_buf(mp, mp->m_rtdev_targp,
1887                         XFS_FSB_TO_BB(mp, nrblocks - 1),
1888                         XFS_FSB_TO_BB(mp, 1), 0, &bp);
1889         if (error)
1890                 return error;
1891         ASSERT(bp);
1892         xfs_buf_relse(bp);
1893         /*
1894          * Calculate new parameters.  These are the final values to be reached.
1895          */
1896         nrextents = nrblocks;
1897         do_div(nrextents, in->extsize);
1898         nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1899         nrextslog = xfs_highbit32(nrextents);
1900         nrsumlevels = nrextslog + 1;
1901         nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1902         nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1903         nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1904         /*
1905          * New summary size can't be more than half the size of
1906          * the log.  This prevents us from getting a log overflow,
1907          * since we'll log basically the whole summary file at once.
1908          */
1909         if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1910                 return XFS_ERROR(EINVAL);
1911         /*
1912          * Get the old block counts for bitmap and summary inodes.
1913          * These can't change since other growfs callers are locked out.
1914          */
1915         rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1916         rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1917         /*
1918          * Allocate space to the bitmap and summary files, as necessary.
1919          */
1920         if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1921                         mp->m_sb.sb_rbmino)))
1922                 return error;
1923         if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1924                         mp->m_sb.sb_rsumino)))
1925                 return error;
1926         /*
1927          * Allocate a new (fake) mount/sb.
1928          */
1929         nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1930         /*
1931          * Loop over the bitmap blocks.
1932          * We will do everything one bitmap block at a time.
1933          * Skip the current block if it is exactly full.
1934          * This also deals with the case where there were no rtextents before.
1935          */
1936         for (bmbno = sbp->sb_rbmblocks -
1937                      ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1938              bmbno < nrbmblocks;
1939              bmbno++) {
1940                 xfs_trans_t     *tp;
1941                 int             cancelflags = 0;
1942
1943                 *nmp = *mp;
1944                 nsbp = &nmp->m_sb;
1945                 /*
1946                  * Calculate new sb and mount fields for this round.
1947                  */
1948                 nsbp->sb_rextsize = in->extsize;
1949                 nsbp->sb_rbmblocks = bmbno + 1;
1950                 nsbp->sb_rblocks =
1951                         XFS_RTMIN(nrblocks,
1952                                   nsbp->sb_rbmblocks * NBBY *
1953                                   nsbp->sb_blocksize * nsbp->sb_rextsize);
1954                 nsbp->sb_rextents = nsbp->sb_rblocks;
1955                 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1956                 ASSERT(nsbp->sb_rextents != 0);
1957                 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1958                 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1959                 nrsumsize =
1960                         (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1961                         nsbp->sb_rbmblocks;
1962                 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1963                 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1964                 /*
1965                  * Start a transaction, get the log reservation.
1966                  */
1967                 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
1968                 if ((error = xfs_trans_reserve(tp, 0,
1969                                 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
1970                         goto error_cancel;
1971                 /*
1972                  * Lock out other callers by grabbing the bitmap inode lock.
1973                  */
1974                 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
1975                                                 XFS_ILOCK_EXCL, &ip)))
1976                         goto error_cancel;
1977                 ASSERT(ip == mp->m_rbmip);
1978                 /*
1979                  * Update the bitmap inode's size.
1980                  */
1981                 mp->m_rbmip->i_d.di_size =
1982                         nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1983                 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1984                 cancelflags |= XFS_TRANS_ABORT;
1985                 /*
1986                  * Get the summary inode into the transaction.
1987                  */
1988                 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
1989                                                 XFS_ILOCK_EXCL, &ip)))
1990                         goto error_cancel;
1991                 ASSERT(ip == mp->m_rsumip);
1992                 /*
1993                  * Update the summary inode's size.
1994                  */
1995                 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
1996                 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1997                 /*
1998                  * Copy summary data from old to new sizes.
1999                  * Do this when the real size (not block-aligned) changes.
2000                  */
2001                 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2002                     mp->m_rsumlevels != nmp->m_rsumlevels) {
2003                         error = xfs_rtcopy_summary(mp, nmp, tp);
2004                         if (error)
2005                                 goto error_cancel;
2006                 }
2007                 /*
2008                  * Update superblock fields.
2009                  */
2010                 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2011                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2012                                 nsbp->sb_rextsize - sbp->sb_rextsize);
2013                 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2014                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2015                                 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2016                 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2017                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2018                                 nsbp->sb_rblocks - sbp->sb_rblocks);
2019                 if (nsbp->sb_rextents != sbp->sb_rextents)
2020                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2021                                 nsbp->sb_rextents - sbp->sb_rextents);
2022                 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2023                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2024                                 nsbp->sb_rextslog - sbp->sb_rextslog);
2025                 /*
2026                  * Free new extent.
2027                  */
2028                 bp = NULL;
2029                 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2030                         nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2031                 if (error) {
2032 error_cancel:
2033                         xfs_trans_cancel(tp, cancelflags);
2034                         break;
2035                 }
2036                 /*
2037                  * Mark more blocks free in the superblock.
2038                  */
2039                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2040                         nsbp->sb_rextents - sbp->sb_rextents);
2041                 /*
2042                  * Update mp values into the real mp structure.
2043                  */
2044                 mp->m_rsumlevels = nrsumlevels;
2045                 mp->m_rsumsize = nrsumsize;
2046
2047                 error = xfs_trans_commit(tp, 0);
2048                 if (error)
2049                         break;
2050         }
2051
2052         /*
2053          * Free the fake mp structure.
2054          */
2055         kmem_free(nmp);
2056
2057         return error;
2058 }
2059
2060 /*
2061  * Allocate an extent in the realtime subvolume, with the usual allocation
2062  * parameters.  The length units are all in realtime extents, as is the
2063  * result block number.
2064  */
2065 int                                     /* error */
2066 xfs_rtallocate_extent(
2067         xfs_trans_t     *tp,            /* transaction pointer */
2068         xfs_rtblock_t   bno,            /* starting block number to allocate */
2069         xfs_extlen_t    minlen,         /* minimum length to allocate */
2070         xfs_extlen_t    maxlen,         /* maximum length to allocate */
2071         xfs_extlen_t    *len,           /* out: actual length allocated */
2072         xfs_alloctype_t type,           /* allocation type XFS_ALLOCTYPE... */
2073         int             wasdel,         /* was a delayed allocation extent */
2074         xfs_extlen_t    prod,           /* extent product factor */
2075         xfs_rtblock_t   *rtblock)       /* out: start block allocated */
2076 {
2077         int             error;          /* error value */
2078         xfs_inode_t     *ip;            /* inode for bitmap file */
2079         xfs_mount_t     *mp;            /* file system mount structure */
2080         xfs_rtblock_t   r;              /* result allocated block */
2081         xfs_fsblock_t   sb;             /* summary file block number */
2082         xfs_buf_t       *sumbp;         /* summary file block buffer */
2083
2084         ASSERT(minlen > 0 && minlen <= maxlen);
2085         mp = tp->t_mountp;
2086         /*
2087          * If prod is set then figure out what to do to minlen and maxlen.
2088          */
2089         if (prod > 1) {
2090                 xfs_extlen_t    i;
2091
2092                 if ((i = maxlen % prod))
2093                         maxlen -= i;
2094                 if ((i = minlen % prod))
2095                         minlen += prod - i;
2096                 if (maxlen < minlen) {
2097                         *rtblock = NULLRTBLOCK;
2098                         return 0;
2099                 }
2100         }
2101         /*
2102          * Lock out other callers by grabbing the bitmap inode lock.
2103          */
2104         if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2105                                         XFS_ILOCK_EXCL, &ip)))
2106                 return error;
2107         sumbp = NULL;
2108         /*
2109          * Allocate by size, or near another block, or exactly at some block.
2110          */
2111         switch (type) {
2112         case XFS_ALLOCTYPE_ANY_AG:
2113                 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2114                                 &sumbp, &sb, prod, &r);
2115                 break;
2116         case XFS_ALLOCTYPE_NEAR_BNO:
2117                 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2118                                 len, &sumbp, &sb, prod, &r);
2119                 break;
2120         case XFS_ALLOCTYPE_THIS_BNO:
2121                 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2122                                 len, &sumbp, &sb, prod, &r);
2123                 break;
2124         default:
2125                 ASSERT(0);
2126         }
2127         if (error) {
2128                 return error;
2129         }
2130         /*
2131          * If it worked, update the superblock.
2132          */
2133         if (r != NULLRTBLOCK) {
2134                 long    slen = (long)*len;
2135
2136                 ASSERT(*len >= minlen && *len <= maxlen);
2137                 if (wasdel)
2138                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2139                 else
2140                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2141         }
2142         *rtblock = r;
2143         return 0;
2144 }
2145
2146 /*
2147  * Free an extent in the realtime subvolume.  Length is expressed in
2148  * realtime extents, as is the block number.
2149  */
2150 int                                     /* error */
2151 xfs_rtfree_extent(
2152         xfs_trans_t     *tp,            /* transaction pointer */
2153         xfs_rtblock_t   bno,            /* starting block number to free */
2154         xfs_extlen_t    len)            /* length of extent freed */
2155 {
2156         int             error;          /* error value */
2157         xfs_inode_t     *ip;            /* bitmap file inode */
2158         xfs_mount_t     *mp;            /* file system mount structure */
2159         xfs_fsblock_t   sb;             /* summary file block number */
2160         xfs_buf_t       *sumbp;         /* summary file block buffer */
2161
2162         mp = tp->t_mountp;
2163         /*
2164          * Synchronize by locking the bitmap inode.
2165          */
2166         if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2167                                         XFS_ILOCK_EXCL, &ip)))
2168                 return error;
2169 #if defined(__KERNEL__) && defined(DEBUG)
2170         /*
2171          * Check to see that this whole range is currently allocated.
2172          */
2173         {
2174                 int     stat;           /* result from checking range */
2175
2176                 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2177                 if (error) {
2178                         return error;
2179                 }
2180                 ASSERT(stat);
2181         }
2182 #endif
2183         sumbp = NULL;
2184         /*
2185          * Free the range of realtime blocks.
2186          */
2187         error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2188         if (error) {
2189                 return error;
2190         }
2191         /*
2192          * Mark more blocks free in the superblock.
2193          */
2194         xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2195         /*
2196          * If we've now freed all the blocks, reset the file sequence
2197          * number to 0.
2198          */
2199         if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2200             mp->m_sb.sb_rextents) {
2201                 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2202                         ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2203                 *(__uint64_t *)&ip->i_d.di_atime = 0;
2204                 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2205         }
2206         return 0;
2207 }
2208
2209 /*
2210  * Initialize realtime fields in the mount structure.
2211  */
2212 int                             /* error */
2213 xfs_rtmount_init(
2214         xfs_mount_t     *mp)    /* file system mount structure */
2215 {
2216         xfs_buf_t       *bp;    /* buffer for last block of subvolume */
2217         xfs_daddr_t     d;      /* address of last block of subvolume */
2218         int             error;  /* error return value */
2219         xfs_sb_t        *sbp;   /* filesystem superblock copy in mount */
2220
2221         sbp = &mp->m_sb;
2222         if (sbp->sb_rblocks == 0)
2223                 return 0;
2224         if (mp->m_rtdev_targp == NULL) {
2225                 cmn_err(CE_WARN,
2226         "XFS: This filesystem has a realtime volume, use rtdev=device option");
2227                 return XFS_ERROR(ENODEV);
2228         }
2229         mp->m_rsumlevels = sbp->sb_rextslog + 1;
2230         mp->m_rsumsize =
2231                 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2232                 sbp->sb_rbmblocks;
2233         mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2234         mp->m_rbmip = mp->m_rsumip = NULL;
2235         /*
2236          * Check that the realtime section is an ok size.
2237          */
2238         d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2239         if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2240                 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2241                         (unsigned long long) XFS_BB_TO_FSB(mp, d),
2242                         (unsigned long long) mp->m_sb.sb_rblocks);
2243                 return XFS_ERROR(EFBIG);
2244         }
2245         error = xfs_read_buf(mp, mp->m_rtdev_targp,
2246                                 d - XFS_FSB_TO_BB(mp, 1),
2247                                 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2248         if (error) {
2249                 cmn_err(CE_WARN,
2250         "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2251                 if (error == ENOSPC)
2252                         return XFS_ERROR(EFBIG);
2253                 return error;
2254         }
2255         xfs_buf_relse(bp);
2256         return 0;
2257 }
2258
2259 /*
2260  * Get the bitmap and summary inodes into the mount structure
2261  * at mount time.
2262  */
2263 int                                     /* error */
2264 xfs_rtmount_inodes(
2265         xfs_mount_t     *mp)            /* file system mount structure */
2266 {
2267         int             error;          /* error return value */
2268         xfs_sb_t        *sbp;
2269
2270         sbp = &mp->m_sb;
2271         if (sbp->sb_rbmino == NULLFSINO)
2272                 return 0;
2273         error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
2274         if (error)
2275                 return error;
2276         ASSERT(mp->m_rbmip != NULL);
2277         ASSERT(sbp->sb_rsumino != NULLFSINO);
2278         error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
2279         if (error) {
2280                 IRELE(mp->m_rbmip);
2281                 return error;
2282         }
2283         ASSERT(mp->m_rsumip != NULL);
2284         return 0;
2285 }
2286
2287 void
2288 xfs_rtunmount_inodes(
2289         struct xfs_mount        *mp)
2290 {
2291         if (mp->m_rbmip)
2292                 IRELE(mp->m_rbmip);
2293         if (mp->m_rsumip)
2294                 IRELE(mp->m_rsumip);
2295 }
2296
2297 /*
2298  * Pick an extent for allocation at the start of a new realtime file.
2299  * Use the sequence number stored in the atime field of the bitmap inode.
2300  * Translate this to a fraction of the rtextents, and return the product
2301  * of rtextents and the fraction.
2302  * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2303  */
2304 int                                     /* error */
2305 xfs_rtpick_extent(
2306         xfs_mount_t     *mp,            /* file system mount point */
2307         xfs_trans_t     *tp,            /* transaction pointer */
2308         xfs_extlen_t    len,            /* allocation length (rtextents) */
2309         xfs_rtblock_t   *pick)          /* result rt extent */
2310 {
2311         xfs_rtblock_t   b;              /* result block */
2312         int             error;          /* error return value */
2313         xfs_inode_t     *ip;            /* bitmap incore inode */
2314         int             log2;           /* log of sequence number */
2315         __uint64_t      resid;          /* residual after log removed */
2316         __uint64_t      seq;            /* sequence number of file creation */
2317         __uint64_t      *seqp;          /* pointer to seqno in inode */
2318
2319         if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2320                                         XFS_ILOCK_EXCL, &ip)))
2321                 return error;
2322         ASSERT(ip == mp->m_rbmip);
2323         seqp = (__uint64_t *)&ip->i_d.di_atime;
2324         if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2325                 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2326                 *seqp = 0;
2327         }
2328         seq = *seqp;
2329         if ((log2 = xfs_highbit64(seq)) == -1)
2330                 b = 0;
2331         else {
2332                 resid = seq - (1ULL << log2);
2333                 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2334                     (log2 + 1);
2335                 if (b >= mp->m_sb.sb_rextents)
2336                         b = do_mod(b, mp->m_sb.sb_rextents);
2337                 if (b + len > mp->m_sb.sb_rextents)
2338                         b = mp->m_sb.sb_rextents - len;
2339         }
2340         *seqp = seq + 1;
2341         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2342         *pick = b;
2343         return 0;
2344 }