2 * Copyright (C) International Business Machines Corp., 2000-2004
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 <linux/quotaops.h>
21 #include <linux/vs_dlimit.h>
22 #include "jfs_incore.h"
23 #include "jfs_inode.h"
24 #include "jfs_superblock.h"
26 #include "jfs_extent.h"
27 #include "jfs_debug.h"
32 static int extBalloc(struct inode *, s64, s64 *, s64 *);
34 static int extBrealloc(struct inode *, s64, s64, s64 *, s64 *);
36 static s64 extRoundDown(s64 nb);
38 #define DPD(a) (printk("(a): %d\n",(a)))
39 #define DPC(a) (printk("(a): %c\n",(a)))
43 printk("(a): %x%08x ",(a)); \
45 printk("(a): %x ",(a) << 32); \
50 printk("(a): %x%08x\n",(a)); \
52 printk("(a): %x\n",(a) << 32); \
55 #define DPD1(a) (printk("(a): %d ",(a)))
56 #define DPX(a) (printk("(a): %08x\n",(a)))
57 #define DPX1(a) (printk("(a): %08x ",(a)))
58 #define DPS(a) (printk("%s\n",(a)))
59 #define DPE(a) (printk("\nENTERING: %s\n",(a)))
60 #define DPE1(a) (printk("\nENTERING: %s",(a)))
61 #define DPS1(a) (printk(" %s ",(a)))
67 * FUNCTION: allocate an extent for a specified page range within a
71 * ip - the inode of the file.
72 * xlen - requested extent length.
73 * pno - the starting page number with the file.
74 * xp - pointer to an xad. on entry, xad describes an
75 * extent that is used as an allocation hint if the
76 * xaddr of the xad is non-zero. on successful exit,
77 * the xad describes the newly allocated extent.
78 * abnr - bool indicating whether the newly allocated extent
79 * should be marked as allocated but not recorded.
84 * -ENOSPC - insufficient disk resources.
87 extAlloc(struct inode *ip, s64 xlen, s64 pno, xad_t * xp, bool abnr)
89 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
90 s64 nxlen, nxaddr, xoff, hint, xaddr = 0;
94 /* This blocks if we are low on resources */
95 txBeginAnon(ip->i_sb);
97 /* Avoid race with jfs_commit_inode() */
98 mutex_lock(&JFS_IP(ip)->commit_mutex);
100 /* validate extent length */
104 /* get the page's starting extent offset */
105 xoff = pno << sbi->l2nbperpage;
107 /* check if an allocation hint was provided */
108 if ((hint = addressXAD(xp))) {
109 /* get the size of the extent described by the hint */
110 nxlen = lengthXAD(xp);
112 /* check if the hint is for the portion of the file
113 * immediately previous to the current allocation
114 * request and if hint extent has the same abnr
115 * value as the current request. if so, we can
116 * extend the hint extent to include the current
117 * extent if we can allocate the blocks immediately
118 * following the hint extent.
120 if (offsetXAD(xp) + nxlen == xoff &&
121 abnr == ((xp->flag & XAD_NOTRECORDED) ? true : false))
122 xaddr = hint + nxlen;
124 /* adjust the hint to the last block of the extent */
128 /* allocate the disk blocks for the extent. initially, extBalloc()
129 * will try to allocate disk blocks for the requested size (xlen).
130 * if this fails (xlen contiguous free blocks not avaliable), it'll
131 * try to allocate a smaller number of blocks (producing a smaller
132 * extent), with this smaller number of blocks consisting of the
133 * requested number of blocks rounded down to the next smaller
134 * power of 2 number (i.e. 16 -> 8). it'll continue to round down
135 * and retry the allocation until the number of blocks to allocate
136 * is smaller than the number of blocks per page.
139 if ((rc = extBalloc(ip, hint ? hint : INOHINT(ip), &nxlen, &nxaddr))) {
140 mutex_unlock(&JFS_IP(ip)->commit_mutex);
144 /* Allocate blocks to quota. */
145 if (DQUOT_ALLOC_BLOCK(ip, nxlen)) {
146 dbFree(ip, nxaddr, (s64) nxlen);
147 mutex_unlock(&JFS_IP(ip)->commit_mutex);
151 /* Allocate blocks to dlimit. */
152 if (DLIMIT_ALLOC_BLOCK(ip, nxlen)) {
153 DQUOT_FREE_BLOCK(ip, nxlen);
154 dbFree(ip, nxaddr, (s64) nxlen);
155 mutex_unlock(&JFS_IP(ip)->commit_mutex);
159 /* determine the value of the extent flag */
160 xflag = abnr ? XAD_NOTRECORDED : 0;
162 /* if we can extend the hint extent to cover the current request,
163 * extend it. otherwise, insert a new extent to
164 * cover the current request.
166 if (xaddr && xaddr == nxaddr)
167 rc = xtExtend(0, ip, xoff, (int) nxlen, 0);
169 rc = xtInsert(0, ip, xflag, xoff, (int) nxlen, &nxaddr, 0);
171 /* if the extend or insert failed,
172 * free the newly allocated blocks and return the error.
175 dbFree(ip, nxaddr, nxlen);
176 DLIMIT_FREE_BLOCK(ip, nxlen);
177 DQUOT_FREE_BLOCK(ip, nxlen);
178 mutex_unlock(&JFS_IP(ip)->commit_mutex);
182 /* set the results of the extent allocation */
183 XADaddress(xp, nxaddr);
184 XADlength(xp, nxlen);
188 mark_inode_dirty(ip);
190 mutex_unlock(&JFS_IP(ip)->commit_mutex);
192 * COMMIT_SyncList flags an anonymous tlock on page that is on
194 * We need to commit the inode to get the page written disk.
196 if (test_and_clear_cflag(COMMIT_Synclist,ip))
197 jfs_commit_inode(ip, 0);
207 * FUNCTION: extend the allocation of a file extent containing a
208 * partial back last page.
211 * ip - the inode of the file.
212 * cp - cbuf for the partial backed last page.
213 * xlen - request size of the resulting extent.
214 * xp - pointer to an xad. on successful exit, the xad
215 * describes the newly allocated extent.
216 * abnr - bool indicating whether the newly allocated extent
217 * should be marked as allocated but not recorded.
222 * -ENOSPC - insufficient disk resources.
224 int extRealloc(struct inode *ip, s64 nxlen, xad_t * xp, bool abnr)
226 struct super_block *sb = ip->i_sb;
227 s64 xaddr, xlen, nxaddr, delta, xoff;
228 s64 ntail, nextend, ninsert;
229 int rc, nbperpage = JFS_SBI(sb)->nbperpage;
232 /* This blocks if we are low on resources */
233 txBeginAnon(ip->i_sb);
235 mutex_lock(&JFS_IP(ip)->commit_mutex);
236 /* validate extent length */
240 /* get the extend (partial) page's disk block address and
243 xaddr = addressXAD(xp);
244 xlen = lengthXAD(xp);
245 xoff = offsetXAD(xp);
247 /* if the extend page is abnr and if the request is for
248 * the extent to be allocated and recorded,
249 * make the page allocated and recorded.
251 if ((xp->flag & XAD_NOTRECORDED) && !abnr) {
253 if ((rc = xtUpdate(0, ip, xp)))
257 /* try to allocated the request number of blocks for the
258 * extent. dbRealloc() first tries to satisfy the request
259 * by extending the allocation in place. otherwise, it will
260 * try to allocate a new set of blocks large enough for the
261 * request. in satisfying a request, dbReAlloc() may allocate
262 * less than what was request but will always allocate enough
263 * space as to satisfy the extend page.
265 if ((rc = extBrealloc(ip, xaddr, xlen, &nxlen, &nxaddr)))
268 /* Allocat blocks to quota. */
269 if (DQUOT_ALLOC_BLOCK(ip, nxlen)) {
270 dbFree(ip, nxaddr, (s64) nxlen);
271 mutex_unlock(&JFS_IP(ip)->commit_mutex);
274 /* Allocate blocks to dlimit. */
275 if (DLIMIT_ALLOC_BLOCK(ip, nxlen)) {
276 DQUOT_FREE_BLOCK(ip, nxlen);
277 dbFree(ip, nxaddr, (s64) nxlen);
278 up(&JFS_IP(ip)->commit_sem);
282 delta = nxlen - xlen;
284 /* check if the extend page is not abnr but the request is abnr
285 * and the allocated disk space is for more than one page. if this
286 * is the case, there is a miss match of abnr between the extend page
287 * and the one or more pages following the extend page. as a result,
288 * two extents will have to be manipulated. the first will be that
289 * of the extent of the extend page and will be manipulated thru
290 * an xtExtend() or an xtTailgate(), depending upon whether the
291 * disk allocation occurred as an inplace extension. the second
292 * extent will be manipulated (created) through an xtInsert() and
293 * will be for the pages following the extend page.
295 if (abnr && (!(xp->flag & XAD_NOTRECORDED)) && (nxlen > nbperpage)) {
297 nextend = ntail - xlen;
298 ninsert = nxlen - nbperpage;
300 xflag = XAD_NOTRECORDED;
309 /* if we were able to extend the disk allocation in place,
310 * extend the extent. otherwise, move the extent to a
313 if (xaddr == nxaddr) {
314 /* extend the extent */
315 if ((rc = xtExtend(0, ip, xoff + xlen, (int) nextend, 0))) {
316 dbFree(ip, xaddr + xlen, delta);
317 DLIMIT_FREE_BLOCK(ip, nxlen);
318 DQUOT_FREE_BLOCK(ip, nxlen);
323 * move the extent to a new location:
325 * xtTailgate() accounts for relocated tail extent;
327 if ((rc = xtTailgate(0, ip, xoff, (int) ntail, nxaddr, 0))) {
328 dbFree(ip, nxaddr, nxlen);
329 DLIMIT_FREE_BLOCK(ip, nxlen);
330 DQUOT_FREE_BLOCK(ip, nxlen);
336 /* check if we need to also insert a new extent */
338 /* perform the insert. if it fails, free the blocks
339 * to be inserted and make it appear that we only did
340 * the xtExtend() or xtTailgate() above.
342 xaddr = nxaddr + ntail;
343 if (xtInsert (0, ip, xflag, xoff + ntail, (int) ninsert,
345 dbFree(ip, xaddr, (s64) ninsert);
352 /* set the return results */
353 XADaddress(xp, nxaddr);
354 XADlength(xp, nxlen);
358 mark_inode_dirty(ip);
360 mutex_unlock(&JFS_IP(ip)->commit_mutex);
369 * FUNCTION: produce an extent allocation hint for a file offset.
372 * ip - the inode of the file.
373 * offset - file offset for which the hint is needed.
374 * xp - pointer to the xad that is to be filled in with
381 int extHint(struct inode *ip, s64 offset, xad_t * xp)
383 struct super_block *sb = ip->i_sb;
388 int rc, nbperpage = JFS_SBI(sb)->nbperpage;
390 /* init the hint as "no hint provided" */
393 /* determine the starting extent offset of the page previous
394 * to the page containing the offset.
396 prev = ((offset & ~POFFSET) >> JFS_SBI(sb)->l2bsize) - nbperpage;
398 /* if the offsets in the first page of the file,
404 /* prepare to lookup the previous page's extent info */
408 LXDoffset(&lxd, prev)
409 LXDlength(&lxd, nbperpage);
415 /* perform the lookup */
416 if ((rc = xtLookupList(ip, &lxdl, &xadl, 0)))
419 /* check if not extent exists for the previous page.
420 * this is possible for sparse files.
422 if (xadl.nxad == 0) {
423 // assert(ISSPARSE(ip));
427 /* only preserve the abnr flag within the xad flags
428 * of the returned hint.
430 xp->flag &= XAD_NOTRECORDED;
432 if(xadl.nxad != 1 || lengthXAD(xp) != nbperpage) {
433 jfs_error(ip->i_sb, "extHint: corrupt xtree");
444 * FUNCTION: change a page with a file from not recorded to recorded.
447 * ip - inode of the file.
448 * cp - cbuf of the file page.
453 * -ENOSPC - insufficient disk resources.
455 int extRecord(struct inode *ip, xad_t * xp)
459 txBeginAnon(ip->i_sb);
461 mutex_lock(&JFS_IP(ip)->commit_mutex);
463 /* update the extent */
464 rc = xtUpdate(0, ip, xp);
466 mutex_unlock(&JFS_IP(ip)->commit_mutex);
475 * FUNCTION: allocate disk space for a file page that represents
479 * ip - the inode of the file.
480 * cp - cbuf of the file page represent the hole.
485 * -ENOSPC - insufficient disk resources.
487 int extFill(struct inode *ip, xad_t * xp)
489 int rc, nbperpage = JFS_SBI(ip->i_sb)->nbperpage;
490 s64 blkno = offsetXAD(xp) >> ip->i_blkbits;
492 // assert(ISSPARSE(ip));
494 /* initialize the extent allocation hint */
497 /* allocate an extent to fill the hole */
498 if ((rc = extAlloc(ip, nbperpage, blkno, xp, false)))
501 assert(lengthPXD(xp) == nbperpage);
511 * FUNCTION: allocate disk blocks to form an extent.
513 * initially, we will try to allocate disk blocks for the
514 * requested size (nblocks). if this fails (nblocks
515 * contiguous free blocks not avaliable), we'll try to allocate
516 * a smaller number of blocks (producing a smaller extent), with
517 * this smaller number of blocks consisting of the requested
518 * number of blocks rounded down to the next smaller power of 2
519 * number (i.e. 16 -> 8). we'll continue to round down and
520 * retry the allocation until the number of blocks to allocate
521 * is smaller than the number of blocks per page.
524 * ip - the inode of the file.
525 * hint - disk block number to be used as an allocation hint.
526 * *nblocks - pointer to an s64 value. on entry, this value specifies
527 * the desired number of block to be allocated. on successful
528 * exit, this value is set to the number of blocks actually
530 * blkno - pointer to a block address that is filled in on successful
531 * return with the starting block number of the newly
532 * allocated block range.
537 * -ENOSPC - insufficient disk resources.
540 extBalloc(struct inode *ip, s64 hint, s64 * nblocks, s64 * blkno)
542 struct jfs_inode_info *ji = JFS_IP(ip);
543 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
544 s64 nb, nblks, daddr, max;
545 int rc, nbperpage = sbi->nbperpage;
546 struct bmap *bmp = sbi->bmap;
549 /* get the number of blocks to initially attempt to allocate.
550 * we'll first try the number of blocks requested unless this
551 * number is greater than the maximum number of contiguous free
552 * blocks in the map. in that case, we'll start off with the
555 max = (s64) 1 << bmp->db_maxfreebud;
556 if (*nblocks >= max && *nblocks > nbperpage)
557 nb = nblks = (max > nbperpage) ? max : nbperpage;
559 nb = nblks = *nblocks;
561 /* try to allocate blocks */
562 while ((rc = dbAlloc(ip, hint, nb, &daddr)) != 0) {
563 /* if something other than an out of space error,
564 * stop and return this error.
569 /* decrease the allocation request size */
570 nb = min(nblks, extRoundDown(nb));
572 /* give up if we cannot cover a page */
580 if (S_ISREG(ip->i_mode) && (ji->fileset == FILESYSTEM_I)) {
581 ag = BLKTOAG(daddr, sbi);
582 spin_lock_irq(&ji->ag_lock);
583 if (ji->active_ag == -1) {
584 atomic_inc(&bmp->db_active[ag]);
586 } else if (ji->active_ag != ag) {
587 atomic_dec(&bmp->db_active[ji->active_ag]);
588 atomic_inc(&bmp->db_active[ag]);
591 spin_unlock_irq(&ji->ag_lock);
600 * NAME: extBrealloc()
602 * FUNCTION: attempt to extend an extent's allocation.
604 * Initially, we will try to extend the extent's allocation
605 * in place. If this fails, we'll try to move the extent
606 * to a new set of blocks. If moving the extent, we initially
607 * will try to allocate disk blocks for the requested size
608 * (newnblks). if this fails (new contiguous free blocks not
609 * avaliable), we'll try to allocate a smaller number of
610 * blocks (producing a smaller extent), with this smaller
611 * number of blocks consisting of the requested number of
612 * blocks rounded down to the next smaller power of 2
613 * number (i.e. 16 -> 8). We'll continue to round down and
614 * retry the allocation until the number of blocks to allocate
615 * is smaller than the number of blocks per page.
618 * ip - the inode of the file.
619 * blkno - starting block number of the extents current allocation.
620 * nblks - number of blocks within the extents current allocation.
621 * newnblks - pointer to a s64 value. on entry, this value is the
622 * the new desired extent size (number of blocks). on
623 * successful exit, this value is set to the extent's actual
624 * new size (new number of blocks).
625 * newblkno - the starting block number of the extents new allocation.
630 * -ENOSPC - insufficient disk resources.
633 extBrealloc(struct inode *ip,
634 s64 blkno, s64 nblks, s64 * newnblks, s64 * newblkno)
638 /* try to extend in place */
639 if ((rc = dbExtend(ip, blkno, nblks, *newnblks - nblks)) == 0) {
647 /* in place extension not possible.
648 * try to move the extent to a new set of blocks.
650 return (extBalloc(ip, blkno, newnblks, newblkno));
656 * NAME: extRoundDown()
658 * FUNCTION: round down a specified number of blocks to the next
659 * smallest power of 2 number.
662 * nb - the inode of the file.
665 * next smallest power of 2 number.
667 static s64 extRoundDown(s64 nb)
672 for (i = 0, m = (u64) 1 << 63; i < 64; i++, m >>= 1) {
679 k = ((k - 1) & nb) ? k : k >> 1;