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