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 "jfs_incore.h"
22 #include "jfs_inode.h"
23 #include "jfs_superblock.h"
25 #include "jfs_extent.h"
26 #include "jfs_debug.h"
31 static int extBalloc(struct inode
*, s64
, s64
*, s64
*);
33 static int extBrealloc(struct inode
*, s64
, s64
, s64
*, s64
*);
35 static s64
extRoundDown(s64 nb
);
37 #define DPD(a) (printk("(a): %d\n",(a)))
38 #define DPC(a) (printk("(a): %c\n",(a)))
42 printk("(a): %x%08x ",(a)); \
44 printk("(a): %x ",(a) << 32); \
49 printk("(a): %x%08x\n",(a)); \
51 printk("(a): %x\n",(a) << 32); \
54 #define DPD1(a) (printk("(a): %d ",(a)))
55 #define DPX(a) (printk("(a): %08x\n",(a)))
56 #define DPX1(a) (printk("(a): %08x ",(a)))
57 #define DPS(a) (printk("%s\n",(a)))
58 #define DPE(a) (printk("\nENTERING: %s\n",(a)))
59 #define DPE1(a) (printk("\nENTERING: %s",(a)))
60 #define DPS1(a) (printk(" %s ",(a)))
66 * FUNCTION: allocate an extent for a specified page range within a
70 * ip - the inode of the file.
71 * xlen - requested extent length.
72 * pno - the starting page number with the file.
73 * xp - pointer to an xad. on entry, xad describes an
74 * extent that is used as an allocation hint if the
75 * xaddr of the xad is non-zero. on successful exit,
76 * the xad describes the newly allocated extent.
77 * abnr - bool indicating whether the newly allocated extent
78 * should be marked as allocated but not recorded.
83 * -ENOSPC - insufficient disk resources.
86 extAlloc(struct inode
*ip
, s64 xlen
, s64 pno
, xad_t
* xp
, bool abnr
)
88 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
89 s64 nxlen
, nxaddr
, xoff
, hint
, xaddr
= 0;
93 /* This blocks if we are low on resources */
94 txBeginAnon(ip
->i_sb
);
96 /* Avoid race with jfs_commit_inode() */
97 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
99 /* validate extent length */
103 /* get the page's starting extent offset */
104 xoff
= pno
<< sbi
->l2nbperpage
;
106 /* check if an allocation hint was provided */
107 if ((hint
= addressXAD(xp
))) {
108 /* get the size of the extent described by the hint */
109 nxlen
= lengthXAD(xp
);
111 /* check if the hint is for the portion of the file
112 * immediately previous to the current allocation
113 * request and if hint extent has the same abnr
114 * value as the current request. if so, we can
115 * extend the hint extent to include the current
116 * extent if we can allocate the blocks immediately
117 * following the hint extent.
119 if (offsetXAD(xp
) + nxlen
== xoff
&&
120 abnr
== ((xp
->flag
& XAD_NOTRECORDED
) ? true : false))
121 xaddr
= hint
+ nxlen
;
123 /* adjust the hint to the last block of the extent */
127 /* allocate the disk blocks for the extent. initially, extBalloc()
128 * will try to allocate disk blocks for the requested size (xlen).
129 * if this fails (xlen contiguous free blocks not avaliable), it'll
130 * try to allocate a smaller number of blocks (producing a smaller
131 * extent), with this smaller number of blocks consisting of the
132 * requested number of blocks rounded down to the next smaller
133 * power of 2 number (i.e. 16 -> 8). it'll continue to round down
134 * and retry the allocation until the number of blocks to allocate
135 * is smaller than the number of blocks per page.
138 if ((rc
= extBalloc(ip
, hint
? hint
: INOHINT(ip
), &nxlen
, &nxaddr
))) {
139 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
143 /* Allocate blocks to quota. */
144 if (vfs_dq_alloc_block(ip
, nxlen
)) {
145 dbFree(ip
, nxaddr
, (s64
) nxlen
);
146 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
150 /* determine the value of the extent flag */
151 xflag
= abnr
? XAD_NOTRECORDED
: 0;
153 /* if we can extend the hint extent to cover the current request,
154 * extend it. otherwise, insert a new extent to
155 * cover the current request.
157 if (xaddr
&& xaddr
== nxaddr
)
158 rc
= xtExtend(0, ip
, xoff
, (int) nxlen
, 0);
160 rc
= xtInsert(0, ip
, xflag
, xoff
, (int) nxlen
, &nxaddr
, 0);
162 /* if the extend or insert failed,
163 * free the newly allocated blocks and return the error.
166 dbFree(ip
, nxaddr
, nxlen
);
167 vfs_dq_free_block(ip
, nxlen
);
168 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
172 /* set the results of the extent allocation */
173 XADaddress(xp
, nxaddr
);
174 XADlength(xp
, nxlen
);
178 mark_inode_dirty(ip
);
180 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
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 - bool 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
, bool 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 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
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 /* Allocat blocks to quota. */
259 if (vfs_dq_alloc_block(ip
, nxlen
)) {
260 dbFree(ip
, nxaddr
, (s64
) nxlen
);
261 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
265 delta
= nxlen
- xlen
;
267 /* check if the extend page is not abnr but the request is abnr
268 * and the allocated disk space is for more than one page. if this
269 * is the case, there is a miss match of abnr between the extend page
270 * and the one or more pages following the extend page. as a result,
271 * two extents will have to be manipulated. the first will be that
272 * of the extent of the extend page and will be manipulated thru
273 * an xtExtend() or an xtTailgate(), depending upon whether the
274 * disk allocation occurred as an inplace extension. the second
275 * extent will be manipulated (created) through an xtInsert() and
276 * will be for the pages following the extend page.
278 if (abnr
&& (!(xp
->flag
& XAD_NOTRECORDED
)) && (nxlen
> nbperpage
)) {
280 nextend
= ntail
- xlen
;
281 ninsert
= nxlen
- nbperpage
;
283 xflag
= XAD_NOTRECORDED
;
292 /* if we were able to extend the disk allocation in place,
293 * extend the extent. otherwise, move the extent to a
296 if (xaddr
== nxaddr
) {
297 /* extend the extent */
298 if ((rc
= xtExtend(0, ip
, xoff
+ xlen
, (int) nextend
, 0))) {
299 dbFree(ip
, xaddr
+ xlen
, delta
);
300 vfs_dq_free_block(ip
, nxlen
);
305 * move the extent to a new location:
307 * xtTailgate() accounts for relocated tail extent;
309 if ((rc
= xtTailgate(0, ip
, xoff
, (int) ntail
, nxaddr
, 0))) {
310 dbFree(ip
, nxaddr
, nxlen
);
311 vfs_dq_free_block(ip
, nxlen
);
317 /* check if we need to also insert a new extent */
319 /* perform the insert. if it fails, free the blocks
320 * to be inserted and make it appear that we only did
321 * the xtExtend() or xtTailgate() above.
323 xaddr
= nxaddr
+ ntail
;
324 if (xtInsert (0, ip
, xflag
, xoff
+ ntail
, (int) ninsert
,
326 dbFree(ip
, xaddr
, (s64
) ninsert
);
333 /* set the return results */
334 XADaddress(xp
, nxaddr
);
335 XADlength(xp
, nxlen
);
339 mark_inode_dirty(ip
);
341 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
350 * FUNCTION: produce an extent allocation hint for a file offset.
353 * ip - the inode of the file.
354 * offset - file offset for which the hint is needed.
355 * xp - pointer to the xad that is to be filled in with
362 int extHint(struct inode
*ip
, s64 offset
, xad_t
* xp
)
364 struct super_block
*sb
= ip
->i_sb
;
365 int nbperpage
= JFS_SBI(sb
)->nbperpage
;
372 /* init the hint as "no hint provided" */
375 /* determine the starting extent offset of the page previous
376 * to the page containing the offset.
378 prev
= ((offset
& ~POFFSET
) >> JFS_SBI(sb
)->l2bsize
) - nbperpage
;
380 /* if the offset is in the first page of the file, no hint provided.
385 rc
= xtLookup(ip
, prev
, nbperpage
, &xflag
, &xaddr
, &xlen
, 0);
387 if ((rc
== 0) && xlen
) {
388 if (xlen
!= nbperpage
) {
389 jfs_error(ip
->i_sb
, "extHint: corrupt xtree");
392 XADaddress(xp
, xaddr
);
396 * only preserve the abnr flag within the xad flags
397 * of the returned hint.
399 xp
->flag
= xflag
& XAD_NOTRECORDED
;
411 * FUNCTION: change a page with a file from not recorded to recorded.
414 * ip - inode of the file.
415 * cp - cbuf of the file page.
420 * -ENOSPC - insufficient disk resources.
422 int extRecord(struct inode
*ip
, xad_t
* xp
)
426 txBeginAnon(ip
->i_sb
);
428 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
430 /* update the extent */
431 rc
= xtUpdate(0, ip
, xp
);
433 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
442 * FUNCTION: allocate disk space for a file page that represents
446 * ip - the inode of the file.
447 * cp - cbuf of the file page represent the hole.
452 * -ENOSPC - insufficient disk resources.
454 int extFill(struct inode
*ip
, xad_t
* xp
)
456 int rc
, nbperpage
= JFS_SBI(ip
->i_sb
)->nbperpage
;
457 s64 blkno
= offsetXAD(xp
) >> ip
->i_blkbits
;
459 // assert(ISSPARSE(ip));
461 /* initialize the extent allocation hint */
464 /* allocate an extent to fill the hole */
465 if ((rc
= extAlloc(ip
, nbperpage
, blkno
, xp
, false)))
468 assert(lengthPXD(xp
) == nbperpage
);
478 * FUNCTION: allocate disk blocks to form an extent.
480 * initially, we will try to allocate disk blocks for the
481 * requested size (nblocks). if this fails (nblocks
482 * contiguous free blocks not avaliable), we'll try to allocate
483 * a smaller number of blocks (producing a smaller extent), with
484 * this smaller number of blocks consisting of the requested
485 * number of blocks rounded down to the next smaller power of 2
486 * number (i.e. 16 -> 8). we'll continue to round down and
487 * retry the allocation until the number of blocks to allocate
488 * is smaller than the number of blocks per page.
491 * ip - the inode of the file.
492 * hint - disk block number to be used as an allocation hint.
493 * *nblocks - pointer to an s64 value. on entry, this value specifies
494 * the desired number of block to be allocated. on successful
495 * exit, this value is set to the number of blocks actually
497 * blkno - pointer to a block address that is filled in on successful
498 * return with the starting block number of the newly
499 * allocated block range.
504 * -ENOSPC - insufficient disk resources.
507 extBalloc(struct inode
*ip
, s64 hint
, s64
* nblocks
, s64
* blkno
)
509 struct jfs_inode_info
*ji
= JFS_IP(ip
);
510 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
511 s64 nb
, nblks
, daddr
, max
;
512 int rc
, nbperpage
= sbi
->nbperpage
;
513 struct bmap
*bmp
= sbi
->bmap
;
516 /* get the number of blocks to initially attempt to allocate.
517 * we'll first try the number of blocks requested unless this
518 * number is greater than the maximum number of contiguous free
519 * blocks in the map. in that case, we'll start off with the
522 max
= (s64
) 1 << bmp
->db_maxfreebud
;
523 if (*nblocks
>= max
&& *nblocks
> nbperpage
)
524 nb
= nblks
= (max
> nbperpage
) ? max
: nbperpage
;
526 nb
= nblks
= *nblocks
;
528 /* try to allocate blocks */
529 while ((rc
= dbAlloc(ip
, hint
, nb
, &daddr
)) != 0) {
530 /* if something other than an out of space error,
531 * stop and return this error.
536 /* decrease the allocation request size */
537 nb
= min(nblks
, extRoundDown(nb
));
539 /* give up if we cannot cover a page */
547 if (S_ISREG(ip
->i_mode
) && (ji
->fileset
== FILESYSTEM_I
)) {
548 ag
= BLKTOAG(daddr
, sbi
);
549 spin_lock_irq(&ji
->ag_lock
);
550 if (ji
->active_ag
== -1) {
551 atomic_inc(&bmp
->db_active
[ag
]);
553 } else if (ji
->active_ag
!= ag
) {
554 atomic_dec(&bmp
->db_active
[ji
->active_ag
]);
555 atomic_inc(&bmp
->db_active
[ag
]);
558 spin_unlock_irq(&ji
->ag_lock
);
567 * NAME: extBrealloc()
569 * FUNCTION: attempt to extend an extent's allocation.
571 * Initially, we will try to extend the extent's allocation
572 * in place. If this fails, we'll try to move the extent
573 * to a new set of blocks. If moving the extent, we initially
574 * will try to allocate disk blocks for the requested size
575 * (newnblks). if this fails (new contiguous free blocks not
576 * avaliable), we'll try to allocate a smaller number of
577 * blocks (producing a smaller extent), with this smaller
578 * number of blocks consisting of the requested number of
579 * blocks rounded down to the next smaller power of 2
580 * number (i.e. 16 -> 8). We'll continue to round down and
581 * retry the allocation until the number of blocks to allocate
582 * is smaller than the number of blocks per page.
585 * ip - the inode of the file.
586 * blkno - starting block number of the extents current allocation.
587 * nblks - number of blocks within the extents current allocation.
588 * newnblks - pointer to a s64 value. on entry, this value is the
589 * the new desired extent size (number of blocks). on
590 * successful exit, this value is set to the extent's actual
591 * new size (new number of blocks).
592 * newblkno - the starting block number of the extents new allocation.
597 * -ENOSPC - insufficient disk resources.
600 extBrealloc(struct inode
*ip
,
601 s64 blkno
, s64 nblks
, s64
* newnblks
, s64
* newblkno
)
605 /* try to extend in place */
606 if ((rc
= dbExtend(ip
, blkno
, nblks
, *newnblks
- nblks
)) == 0) {
614 /* in place extension not possible.
615 * try to move the extent to a new set of blocks.
617 return (extBalloc(ip
, blkno
, newnblks
, newblkno
));
623 * NAME: extRoundDown()
625 * FUNCTION: round down a specified number of blocks to the next
626 * smallest power of 2 number.
629 * nb - the inode of the file.
632 * next smallest power of 2 number.
634 static s64
extRoundDown(s64 nb
)
639 for (i
= 0, m
= (u64
) 1 << 63; i
< 64; i
++, m
>>= 1) {
646 k
= ((k
- 1) & nb
) ? k
: k
>> 1;