2 * Copyright (C) International Business Machines Corp., 2000-2003
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "jfs_incore.h"
21 #include "jfs_superblock.h"
23 #include "jfs_extent.h"
24 #include "jfs_debug.h"
29 static int extBalloc(struct inode *, s64, s64 *, s64 *);
31 static int extBrealloc(struct inode *, s64, s64, s64 *, s64 *);
33 static s64 extRoundDown(s64 nb);
38 extern int jfs_commit_inode(struct inode *, int);
41 #define DPD(a) (printk("(a): %d\n",(a)))
42 #define DPC(a) (printk("(a): %c\n",(a)))
46 printk("(a): %x%08x ",(a)); \
48 printk("(a): %x ",(a) << 32); \
53 printk("(a): %x%08x\n",(a)); \
55 printk("(a): %x\n",(a) << 32); \
58 #define DPD1(a) (printk("(a): %d ",(a)))
59 #define DPX(a) (printk("(a): %08x\n",(a)))
60 #define DPX1(a) (printk("(a): %08x ",(a)))
61 #define DPS(a) (printk("%s\n",(a)))
62 #define DPE(a) (printk("\nENTERING: %s\n",(a)))
63 #define DPE1(a) (printk("\nENTERING: %s",(a)))
64 #define DPS1(a) (printk(" %s ",(a)))
70 * FUNCTION: allocate an extent for a specified page range within a
74 * ip - the inode of the file.
75 * xlen - requested extent length.
76 * pno - the starting page number with the file.
77 * xp - pointer to an xad. on entry, xad describes an
78 * extent that is used as an allocation hint if the
79 * xaddr of the xad is non-zero. on successful exit,
80 * the xad describes the newly allocated extent.
81 * abnr - boolean_t indicating whether the newly allocated extent
82 * should be marked as allocated but not recorded.
87 * -ENOSPC - insufficient disk resources.
90 extAlloc(struct inode *ip, s64 xlen, s64 pno, xad_t * xp, boolean_t abnr)
92 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
93 s64 nxlen, nxaddr, xoff, hint, xaddr = 0;
97 /* This blocks if we are low on resources */
98 txBeginAnon(ip->i_sb);
100 /* Avoid race with jfs_commit_inode() */
101 down(&JFS_IP(ip)->commit_sem);
103 /* validate extent length */
107 /* get the page's starting extent offset */
108 xoff = pno << sbi->l2nbperpage;
110 /* check if an allocation hint was provided */
111 if ((hint = addressXAD(xp))) {
112 /* get the size of the extent described by the hint */
113 nxlen = lengthXAD(xp);
115 /* check if the hint is for the portion of the file
116 * immediately previous to the current allocation
117 * request and if hint extent has the same abnr
118 * value as the current request. if so, we can
119 * extend the hint extent to include the current
120 * extent if we can allocate the blocks immediately
121 * following the hint extent.
123 if (offsetXAD(xp) + nxlen == xoff &&
124 abnr == ((xp->flag & XAD_NOTRECORDED) ? TRUE : FALSE))
125 xaddr = hint + nxlen;
127 /* adjust the hint to the last block of the extent */
131 /* allocate the disk blocks for the extent. initially, extBalloc()
132 * will try to allocate disk blocks for the requested size (xlen).
133 * if this fails (xlen contigious free blocks not avaliable), it'll
134 * try to allocate a smaller number of blocks (producing a smaller
135 * extent), with this smaller number of blocks consisting of the
136 * requested number of blocks rounded down to the next smaller
137 * power of 2 number (i.e. 16 -> 8). it'll continue to round down
138 * and retry the allocation until the number of blocks to allocate
139 * is smaller than the number of blocks per page.
142 if ((rc = extBalloc(ip, hint ? hint : INOHINT(ip), &nxlen, &nxaddr))) {
143 up(&JFS_IP(ip)->commit_sem);
147 /* determine the value of the extent flag */
148 xflag = (abnr == TRUE) ? XAD_NOTRECORDED : 0;
150 /* if we can extend the hint extent to cover the current request,
151 * extend it. otherwise, insert a new extent to
152 * cover the current request.
154 if (xaddr && xaddr == nxaddr)
155 rc = xtExtend(0, ip, xoff, (int) nxlen, 0);
157 rc = xtInsert(0, ip, xflag, xoff, (int) nxlen, &nxaddr, 0);
159 /* if the extend or insert failed,
160 * free the newly allocated blocks and return the error.
163 dbFree(ip, nxaddr, nxlen);
164 up(&JFS_IP(ip)->commit_sem);
168 /* update the number of blocks allocated to the file */
169 ip->i_blocks += LBLK2PBLK(ip->i_sb, nxlen);
171 /* set the results of the extent allocation */
172 XADaddress(xp, nxaddr);
173 XADlength(xp, nxlen);
177 mark_inode_dirty(ip);
178 set_cflag(COMMIT_Syncdata, ip);
180 up(&JFS_IP(ip)->commit_sem);
182 * COMMIT_SyncList flags an anonymous tlock on page that is on
184 * We need to commit the inode to get the page written disk.
186 if (test_and_clear_cflag(COMMIT_Synclist,ip))
187 jfs_commit_inode(ip, 0);
197 * FUNCTION: extend the allocation of a file extent containing a
198 * partial back last page.
201 * ip - the inode of the file.
202 * cp - cbuf for the partial backed last page.
203 * xlen - request size of the resulting extent.
204 * xp - pointer to an xad. on successful exit, the xad
205 * describes the newly allocated extent.
206 * abnr - boolean_t indicating whether the newly allocated extent
207 * should be marked as allocated but not recorded.
212 * -ENOSPC - insufficient disk resources.
214 int extRealloc(struct inode *ip, s64 nxlen, xad_t * xp, boolean_t abnr)
216 struct super_block *sb = ip->i_sb;
217 s64 xaddr, xlen, nxaddr, delta, xoff;
218 s64 ntail, nextend, ninsert;
219 int rc, nbperpage = JFS_SBI(sb)->nbperpage;
222 /* This blocks if we are low on resources */
223 txBeginAnon(ip->i_sb);
225 down(&JFS_IP(ip)->commit_sem);
226 /* validate extent length */
230 /* get the extend (partial) page's disk block address and
233 xaddr = addressXAD(xp);
234 xlen = lengthXAD(xp);
235 xoff = offsetXAD(xp);
237 /* if the extend page is abnr and if the request is for
238 * the extent to be allocated and recorded,
239 * make the page allocated and recorded.
241 if ((xp->flag & XAD_NOTRECORDED) && !abnr) {
243 if ((rc = xtUpdate(0, ip, xp)))
247 /* try to allocated the request number of blocks for the
248 * extent. dbRealloc() first tries to satisfy the request
249 * by extending the allocation in place. otherwise, it will
250 * try to allocate a new set of blocks large enough for the
251 * request. in satisfying a request, dbReAlloc() may allocate
252 * less than what was request but will always allocate enough
253 * space as to satisfy the extend page.
255 if ((rc = extBrealloc(ip, xaddr, xlen, &nxlen, &nxaddr)))
258 delta = nxlen - xlen;
260 /* check if the extend page is not abnr but the request is abnr
261 * and the allocated disk space is for more than one page. if this
262 * is the case, there is a miss match of abnr between the extend page
263 * and the one or more pages following the extend page. as a result,
264 * two extents will have to be manipulated. the first will be that
265 * of the extent of the extend page and will be manipulated thru
266 * an xtExtend() or an xtTailgate(), depending upon whether the
267 * disk allocation occurred as an inplace extension. the second
268 * extent will be manipulated (created) through an xtInsert() and
269 * will be for the pages following the extend page.
271 if (abnr && (!(xp->flag & XAD_NOTRECORDED)) && (nxlen > nbperpage)) {
273 nextend = ntail - xlen;
274 ninsert = nxlen - nbperpage;
276 xflag = XAD_NOTRECORDED;
285 /* if we were able to extend the disk allocation in place,
286 * extend the extent. otherwise, move the extent to a
289 if (xaddr == nxaddr) {
290 /* extend the extent */
291 if ((rc = xtExtend(0, ip, xoff + xlen, (int) nextend, 0))) {
292 dbFree(ip, xaddr + xlen, delta);
297 * move the extent to a new location:
299 * xtTailgate() accounts for relocated tail extent;
301 if ((rc = xtTailgate(0, ip, xoff, (int) ntail, nxaddr, 0))) {
302 dbFree(ip, nxaddr, nxlen);
308 /* check if we need to also insert a new extent */
310 /* perform the insert. if it fails, free the blocks
311 * to be inserted and make it appear that we only did
312 * the xtExtend() or xtTailgate() above.
314 xaddr = nxaddr + ntail;
315 if (xtInsert (0, ip, xflag, xoff + ntail, (int) ninsert,
317 dbFree(ip, xaddr, (s64) ninsert);
324 /* update the inode with the number of blocks allocated */
325 ip->i_blocks += LBLK2PBLK(sb, delta);
327 /* set the return results */
328 XADaddress(xp, nxaddr);
329 XADlength(xp, nxlen);
333 mark_inode_dirty(ip);
335 up(&JFS_IP(ip)->commit_sem);
344 * FUNCTION: produce an extent allocation hint for a file offset.
347 * ip - the inode of the file.
348 * offset - file offset for which the hint is needed.
349 * xp - pointer to the xad that is to be filled in with
356 int extHint(struct inode *ip, s64 offset, xad_t * xp)
358 struct super_block *sb = ip->i_sb;
363 int rc, nbperpage = JFS_SBI(sb)->nbperpage;
365 /* init the hint as "no hint provided" */
368 /* determine the starting extent offset of the page previous
369 * to the page containing the offset.
371 prev = ((offset & ~POFFSET) >> JFS_SBI(sb)->l2bsize) - nbperpage;
373 /* if the offsets in the first page of the file,
379 /* prepare to lookup the previous page's extent info */
383 LXDoffset(&lxd, prev)
384 LXDlength(&lxd, nbperpage);
390 /* perform the lookup */
391 if ((rc = xtLookupList(ip, &lxdl, &xadl, 0)))
394 /* check if not extent exists for the previous page.
395 * this is possible for sparse files.
397 if (xadl.nxad == 0) {
398 // assert(ISSPARSE(ip));
402 /* only preserve the abnr flag within the xad flags
403 * of the returned hint.
405 xp->flag &= XAD_NOTRECORDED;
407 if(xadl.nxad != 1 || lengthXAD(xp) != nbperpage) {
408 jfs_error(ip->i_sb, "extHint: corrupt xtree");
419 * FUNCTION: change a page with a file from not recorded to recorded.
422 * ip - inode of the file.
423 * cp - cbuf of the file page.
428 * -ENOSPC - insufficient disk resources.
430 int extRecord(struct inode *ip, xad_t * xp)
434 txBeginAnon(ip->i_sb);
436 down(&JFS_IP(ip)->commit_sem);
438 /* update the extent */
439 rc = xtUpdate(0, ip, xp);
441 up(&JFS_IP(ip)->commit_sem);
450 * FUNCTION: allocate disk space for a file page that represents
454 * ip - the inode of the file.
455 * cp - cbuf of the file page represent the hole.
460 * -ENOSPC - insufficient disk resources.
462 int extFill(struct inode *ip, xad_t * xp)
464 int rc, nbperpage = JFS_SBI(ip->i_sb)->nbperpage;
465 s64 blkno = offsetXAD(xp) >> ip->i_blksize;
467 // assert(ISSPARSE(ip));
469 /* initialize the extent allocation hint */
472 /* allocate an extent to fill the hole */
473 if ((rc = extAlloc(ip, nbperpage, blkno, xp, FALSE)))
476 assert(lengthPXD(xp) == nbperpage);
486 * FUNCTION: allocate disk blocks to form an extent.
488 * initially, we will try to allocate disk blocks for the
489 * requested size (nblocks). if this fails (nblocks
490 * contigious free blocks not avaliable), we'll try to allocate
491 * a smaller number of blocks (producing a smaller extent), with
492 * this smaller number of blocks consisting of the requested
493 * number of blocks rounded down to the next smaller power of 2
494 * number (i.e. 16 -> 8). we'll continue to round down and
495 * retry the allocation until the number of blocks to allocate
496 * is smaller than the number of blocks per page.
499 * ip - the inode of the file.
500 * hint - disk block number to be used as an allocation hint.
501 * *nblocks - pointer to an s64 value. on entry, this value specifies
502 * the desired number of block to be allocated. on successful
503 * exit, this value is set to the number of blocks actually
505 * blkno - pointer to a block address that is filled in on successful
506 * return with the starting block number of the newly
507 * allocated block range.
512 * -ENOSPC - insufficient disk resources.
515 extBalloc(struct inode *ip, s64 hint, s64 * nblocks, s64 * blkno)
517 struct jfs_inode_info *ji = JFS_IP(ip);
518 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
519 s64 nb, nblks, daddr, max;
520 int rc, nbperpage = sbi->nbperpage;
521 struct bmap *bmp = sbi->bmap;
524 /* get the number of blocks to initially attempt to allocate.
525 * we'll first try the number of blocks requested unless this
526 * number is greater than the maximum number of contigious free
527 * blocks in the map. in that case, we'll start off with the
530 max = (s64) 1 << bmp->db_maxfreebud;
531 if (*nblocks >= max && *nblocks > nbperpage)
532 nb = nblks = (max > nbperpage) ? max : nbperpage;
534 nb = nblks = *nblocks;
536 /* try to allocate blocks */
537 while ((rc = dbAlloc(ip, hint, nb, &daddr))) {
538 /* if something other than an out of space error,
539 * stop and return this error.
544 /* decrease the allocation request size */
545 nb = min(nblks, extRoundDown(nb));
547 /* give up if we cannot cover a page */
555 if (S_ISREG(ip->i_mode) && (ji->fileset == FILESYSTEM_I)) {
556 ag = BLKTOAG(daddr, sbi);
557 spin_lock_irq(&ji->ag_lock);
558 if (ji->active_ag == -1) {
559 atomic_inc(&bmp->db_active[ag]);
561 } else if (ji->active_ag != ag) {
562 atomic_dec(&bmp->db_active[ji->active_ag]);
563 atomic_inc(&bmp->db_active[ag]);
566 spin_unlock_irq(&ji->ag_lock);
575 * NAME: extBrealloc()
577 * FUNCTION: attempt to extend an extent's allocation.
579 * initially, we will try to extend the extent's allocation
580 * in place. if this fails, we'll try to move the extent
581 * to a new set of blocks. if moving the extent, we initially
582 * will try to allocate disk blocks for the requested size
583 * (nnew). if this fails (nnew contigious free blocks not
584 * avaliable), we'll try to allocate a smaller number of
585 * blocks (producing a smaller extent), with this smaller
586 * number of blocks consisting of the requested number of
587 * blocks rounded down to the next smaller power of 2
588 * number (i.e. 16 -> 8). we'll continue to round down and
589 * retry the allocation until the number of blocks to allocate
590 * is smaller than the number of blocks per page.
593 * ip - the inode of the file.
594 * blkno - starting block number of the extents current allocation.
595 * nblks - number of blocks within the extents current allocation.
596 * newnblks - pointer to a s64 value. on entry, this value is the
597 * the new desired extent size (number of blocks). on
598 * successful exit, this value is set to the extent's actual
599 * new size (new number of blocks).
600 * newblkno - the starting block number of the extents new allocation.
605 * -ENOSPC - insufficient disk resources.
608 extBrealloc(struct inode *ip,
609 s64 blkno, s64 nblks, s64 * newnblks, s64 * newblkno)
613 /* try to extend in place */
614 if ((rc = dbExtend(ip, blkno, nblks, *newnblks - nblks)) == 0) {
622 /* in place extension not possible.
623 * try to move the extent to a new set of blocks.
625 return (extBalloc(ip, blkno, newnblks, newblkno));
631 * NAME: extRoundDown()
633 * FUNCTION: round down a specified number of blocks to the next
634 * smallest power of 2 number.
637 * nb - the inode of the file.
640 * next smallest power of 2 number.
642 static s64 extRoundDown(s64 nb)
647 for (i = 0, m = (u64) 1 << 63; i < 64; i++, m >>= 1) {
654 k = ((k - 1) & nb) ? k : k >> 1;