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_superblock.h"
24 #include "jfs_extent.h"
25 #include "jfs_debug.h"
30 static int extBalloc(struct inode
*, s64
, s64
*, s64
*);
32 static int extBrealloc(struct inode
*, s64
, s64
, s64
*, s64
*);
34 static s64
extRoundDown(s64 nb
);
39 extern int jfs_commit_inode(struct inode
*, int);
42 #define DPD(a) (printk("(a): %d\n",(a)))
43 #define DPC(a) (printk("(a): %c\n",(a)))
47 printk("(a): %x%08x ",(a)); \
49 printk("(a): %x ",(a) << 32); \
54 printk("(a): %x%08x\n",(a)); \
56 printk("(a): %x\n",(a) << 32); \
59 #define DPD1(a) (printk("(a): %d ",(a)))
60 #define DPX(a) (printk("(a): %08x\n",(a)))
61 #define DPX1(a) (printk("(a): %08x ",(a)))
62 #define DPS(a) (printk("%s\n",(a)))
63 #define DPE(a) (printk("\nENTERING: %s\n",(a)))
64 #define DPE1(a) (printk("\nENTERING: %s",(a)))
65 #define DPS1(a) (printk(" %s ",(a)))
71 * FUNCTION: allocate an extent for a specified page range within a
75 * ip - the inode of the file.
76 * xlen - requested extent length.
77 * pno - the starting page number with the file.
78 * xp - pointer to an xad. on entry, xad describes an
79 * extent that is used as an allocation hint if the
80 * xaddr of the xad is non-zero. on successful exit,
81 * the xad describes the newly allocated extent.
82 * abnr - boolean_t indicating whether the newly allocated extent
83 * should be marked as allocated but not recorded.
88 * -ENOSPC - insufficient disk resources.
91 extAlloc(struct inode
*ip
, s64 xlen
, s64 pno
, xad_t
* xp
, boolean_t abnr
)
93 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
94 s64 nxlen
, nxaddr
, xoff
, hint
, xaddr
= 0;
98 /* This blocks if we are low on resources */
99 txBeginAnon(ip
->i_sb
);
101 /* Avoid race with jfs_commit_inode() */
102 down(&JFS_IP(ip
)->commit_sem
);
104 /* validate extent length */
108 /* get the page's starting extent offset */
109 xoff
= pno
<< sbi
->l2nbperpage
;
111 /* check if an allocation hint was provided */
112 if ((hint
= addressXAD(xp
))) {
113 /* get the size of the extent described by the hint */
114 nxlen
= lengthXAD(xp
);
116 /* check if the hint is for the portion of the file
117 * immediately previous to the current allocation
118 * request and if hint extent has the same abnr
119 * value as the current request. if so, we can
120 * extend the hint extent to include the current
121 * extent if we can allocate the blocks immediately
122 * following the hint extent.
124 if (offsetXAD(xp
) + nxlen
== xoff
&&
125 abnr
== ((xp
->flag
& XAD_NOTRECORDED
) ? TRUE
: FALSE
))
126 xaddr
= hint
+ nxlen
;
128 /* adjust the hint to the last block of the extent */
132 /* allocate the disk blocks for the extent. initially, extBalloc()
133 * will try to allocate disk blocks for the requested size (xlen).
134 * if this fails (xlen contigious free blocks not avaliable), it'll
135 * try to allocate a smaller number of blocks (producing a smaller
136 * extent), with this smaller number of blocks consisting of the
137 * requested number of blocks rounded down to the next smaller
138 * power of 2 number (i.e. 16 -> 8). it'll continue to round down
139 * and retry the allocation until the number of blocks to allocate
140 * is smaller than the number of blocks per page.
143 if ((rc
= extBalloc(ip
, hint
? hint
: INOHINT(ip
), &nxlen
, &nxaddr
))) {
144 up(&JFS_IP(ip
)->commit_sem
);
148 /* Allocate blocks to quota. */
149 if (DQUOT_ALLOC_BLOCK(ip
, nxlen
)) {
150 dbFree(ip
, nxaddr
, (s64
) nxlen
);
151 up(&JFS_IP(ip
)->commit_sem
);
155 /* determine the value of the extent flag */
156 xflag
= (abnr
== TRUE
) ? XAD_NOTRECORDED
: 0;
158 /* if we can extend the hint extent to cover the current request,
159 * extend it. otherwise, insert a new extent to
160 * cover the current request.
162 if (xaddr
&& xaddr
== nxaddr
)
163 rc
= xtExtend(0, ip
, xoff
, (int) nxlen
, 0);
165 rc
= xtInsert(0, ip
, xflag
, xoff
, (int) nxlen
, &nxaddr
, 0);
167 /* if the extend or insert failed,
168 * free the newly allocated blocks and return the error.
171 dbFree(ip
, nxaddr
, nxlen
);
172 DQUOT_FREE_BLOCK(ip
, nxlen
);
173 up(&JFS_IP(ip
)->commit_sem
);
177 /* set the results of the extent allocation */
178 XADaddress(xp
, nxaddr
);
179 XADlength(xp
, nxlen
);
183 mark_inode_dirty(ip
);
185 up(&JFS_IP(ip
)->commit_sem
);
187 * COMMIT_SyncList flags an anonymous tlock on page that is on
189 * We need to commit the inode to get the page written disk.
191 if (test_and_clear_cflag(COMMIT_Synclist
,ip
))
192 jfs_commit_inode(ip
, 0);
202 * FUNCTION: extend the allocation of a file extent containing a
203 * partial back last page.
206 * ip - the inode of the file.
207 * cp - cbuf for the partial backed last page.
208 * xlen - request size of the resulting extent.
209 * xp - pointer to an xad. on successful exit, the xad
210 * describes the newly allocated extent.
211 * abnr - boolean_t indicating whether the newly allocated extent
212 * should be marked as allocated but not recorded.
217 * -ENOSPC - insufficient disk resources.
219 int extRealloc(struct inode
*ip
, s64 nxlen
, xad_t
* xp
, boolean_t abnr
)
221 struct super_block
*sb
= ip
->i_sb
;
222 s64 xaddr
, xlen
, nxaddr
, delta
, xoff
;
223 s64 ntail
, nextend
, ninsert
;
224 int rc
, nbperpage
= JFS_SBI(sb
)->nbperpage
;
227 /* This blocks if we are low on resources */
228 txBeginAnon(ip
->i_sb
);
230 down(&JFS_IP(ip
)->commit_sem
);
231 /* validate extent length */
235 /* get the extend (partial) page's disk block address and
238 xaddr
= addressXAD(xp
);
239 xlen
= lengthXAD(xp
);
240 xoff
= offsetXAD(xp
);
242 /* if the extend page is abnr and if the request is for
243 * the extent to be allocated and recorded,
244 * make the page allocated and recorded.
246 if ((xp
->flag
& XAD_NOTRECORDED
) && !abnr
) {
248 if ((rc
= xtUpdate(0, ip
, xp
)))
252 /* try to allocated the request number of blocks for the
253 * extent. dbRealloc() first tries to satisfy the request
254 * by extending the allocation in place. otherwise, it will
255 * try to allocate a new set of blocks large enough for the
256 * request. in satisfying a request, dbReAlloc() may allocate
257 * less than what was request but will always allocate enough
258 * space as to satisfy the extend page.
260 if ((rc
= extBrealloc(ip
, xaddr
, xlen
, &nxlen
, &nxaddr
)))
263 /* Allocat blocks to quota. */
264 if (DQUOT_ALLOC_BLOCK(ip
, nxlen
)) {
265 dbFree(ip
, nxaddr
, (s64
) nxlen
);
266 up(&JFS_IP(ip
)->commit_sem
);
270 delta
= nxlen
- xlen
;
272 /* check if the extend page is not abnr but the request is abnr
273 * and the allocated disk space is for more than one page. if this
274 * is the case, there is a miss match of abnr between the extend page
275 * and the one or more pages following the extend page. as a result,
276 * two extents will have to be manipulated. the first will be that
277 * of the extent of the extend page and will be manipulated thru
278 * an xtExtend() or an xtTailgate(), depending upon whether the
279 * disk allocation occurred as an inplace extension. the second
280 * extent will be manipulated (created) through an xtInsert() and
281 * will be for the pages following the extend page.
283 if (abnr
&& (!(xp
->flag
& XAD_NOTRECORDED
)) && (nxlen
> nbperpage
)) {
285 nextend
= ntail
- xlen
;
286 ninsert
= nxlen
- nbperpage
;
288 xflag
= XAD_NOTRECORDED
;
297 /* if we were able to extend the disk allocation in place,
298 * extend the extent. otherwise, move the extent to a
301 if (xaddr
== nxaddr
) {
302 /* extend the extent */
303 if ((rc
= xtExtend(0, ip
, xoff
+ xlen
, (int) nextend
, 0))) {
304 dbFree(ip
, xaddr
+ xlen
, delta
);
305 DQUOT_FREE_BLOCK(ip
, nxlen
);
310 * move the extent to a new location:
312 * xtTailgate() accounts for relocated tail extent;
314 if ((rc
= xtTailgate(0, ip
, xoff
, (int) ntail
, nxaddr
, 0))) {
315 dbFree(ip
, nxaddr
, nxlen
);
316 DQUOT_FREE_BLOCK(ip
, nxlen
);
322 /* check if we need to also insert a new extent */
324 /* perform the insert. if it fails, free the blocks
325 * to be inserted and make it appear that we only did
326 * the xtExtend() or xtTailgate() above.
328 xaddr
= nxaddr
+ ntail
;
329 if (xtInsert (0, ip
, xflag
, xoff
+ ntail
, (int) ninsert
,
331 dbFree(ip
, xaddr
, (s64
) ninsert
);
338 /* set the return results */
339 XADaddress(xp
, nxaddr
);
340 XADlength(xp
, nxlen
);
344 mark_inode_dirty(ip
);
346 up(&JFS_IP(ip
)->commit_sem
);
355 * FUNCTION: produce an extent allocation hint for a file offset.
358 * ip - the inode of the file.
359 * offset - file offset for which the hint is needed.
360 * xp - pointer to the xad that is to be filled in with
367 int extHint(struct inode
*ip
, s64 offset
, xad_t
* xp
)
369 struct super_block
*sb
= ip
->i_sb
;
374 int rc
, nbperpage
= JFS_SBI(sb
)->nbperpage
;
376 /* init the hint as "no hint provided" */
379 /* determine the starting extent offset of the page previous
380 * to the page containing the offset.
382 prev
= ((offset
& ~POFFSET
) >> JFS_SBI(sb
)->l2bsize
) - nbperpage
;
384 /* if the offsets in the first page of the file,
390 /* prepare to lookup the previous page's extent info */
394 LXDoffset(&lxd
, prev
)
395 LXDlength(&lxd
, nbperpage
);
401 /* perform the lookup */
402 if ((rc
= xtLookupList(ip
, &lxdl
, &xadl
, 0)))
405 /* check if not extent exists for the previous page.
406 * this is possible for sparse files.
408 if (xadl
.nxad
== 0) {
409 // assert(ISSPARSE(ip));
413 /* only preserve the abnr flag within the xad flags
414 * of the returned hint.
416 xp
->flag
&= XAD_NOTRECORDED
;
418 if(xadl
.nxad
!= 1 || lengthXAD(xp
) != nbperpage
) {
419 jfs_error(ip
->i_sb
, "extHint: corrupt xtree");
430 * FUNCTION: change a page with a file from not recorded to recorded.
433 * ip - inode of the file.
434 * cp - cbuf of the file page.
439 * -ENOSPC - insufficient disk resources.
441 int extRecord(struct inode
*ip
, xad_t
* xp
)
445 txBeginAnon(ip
->i_sb
);
447 down(&JFS_IP(ip
)->commit_sem
);
449 /* update the extent */
450 rc
= xtUpdate(0, ip
, xp
);
452 up(&JFS_IP(ip
)->commit_sem
);
461 * FUNCTION: allocate disk space for a file page that represents
465 * ip - the inode of the file.
466 * cp - cbuf of the file page represent the hole.
471 * -ENOSPC - insufficient disk resources.
473 int extFill(struct inode
*ip
, xad_t
* xp
)
475 int rc
, nbperpage
= JFS_SBI(ip
->i_sb
)->nbperpage
;
476 s64 blkno
= offsetXAD(xp
) >> ip
->i_blksize
;
478 // assert(ISSPARSE(ip));
480 /* initialize the extent allocation hint */
483 /* allocate an extent to fill the hole */
484 if ((rc
= extAlloc(ip
, nbperpage
, blkno
, xp
, FALSE
)))
487 assert(lengthPXD(xp
) == nbperpage
);
497 * FUNCTION: allocate disk blocks to form an extent.
499 * initially, we will try to allocate disk blocks for the
500 * requested size (nblocks). if this fails (nblocks
501 * contigious free blocks not avaliable), we'll try to allocate
502 * a smaller number of blocks (producing a smaller extent), with
503 * this smaller number of blocks consisting of the requested
504 * number of blocks rounded down to the next smaller power of 2
505 * number (i.e. 16 -> 8). we'll continue to round down and
506 * retry the allocation until the number of blocks to allocate
507 * is smaller than the number of blocks per page.
510 * ip - the inode of the file.
511 * hint - disk block number to be used as an allocation hint.
512 * *nblocks - pointer to an s64 value. on entry, this value specifies
513 * the desired number of block to be allocated. on successful
514 * exit, this value is set to the number of blocks actually
516 * blkno - pointer to a block address that is filled in on successful
517 * return with the starting block number of the newly
518 * allocated block range.
523 * -ENOSPC - insufficient disk resources.
526 extBalloc(struct inode
*ip
, s64 hint
, s64
* nblocks
, s64
* blkno
)
528 struct jfs_inode_info
*ji
= JFS_IP(ip
);
529 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
530 s64 nb
, nblks
, daddr
, max
;
531 int rc
, nbperpage
= sbi
->nbperpage
;
532 struct bmap
*bmp
= sbi
->bmap
;
535 /* get the number of blocks to initially attempt to allocate.
536 * we'll first try the number of blocks requested unless this
537 * number is greater than the maximum number of contigious free
538 * blocks in the map. in that case, we'll start off with the
541 max
= (s64
) 1 << bmp
->db_maxfreebud
;
542 if (*nblocks
>= max
&& *nblocks
> nbperpage
)
543 nb
= nblks
= (max
> nbperpage
) ? max
: nbperpage
;
545 nb
= nblks
= *nblocks
;
547 /* try to allocate blocks */
548 while ((rc
= dbAlloc(ip
, hint
, nb
, &daddr
)) != 0) {
549 /* if something other than an out of space error,
550 * stop and return this error.
555 /* decrease the allocation request size */
556 nb
= min(nblks
, extRoundDown(nb
));
558 /* give up if we cannot cover a page */
566 if (S_ISREG(ip
->i_mode
) && (ji
->fileset
== FILESYSTEM_I
)) {
567 ag
= BLKTOAG(daddr
, sbi
);
568 spin_lock_irq(&ji
->ag_lock
);
569 if (ji
->active_ag
== -1) {
570 atomic_inc(&bmp
->db_active
[ag
]);
572 } else if (ji
->active_ag
!= ag
) {
573 atomic_dec(&bmp
->db_active
[ji
->active_ag
]);
574 atomic_inc(&bmp
->db_active
[ag
]);
577 spin_unlock_irq(&ji
->ag_lock
);
586 * NAME: extBrealloc()
588 * FUNCTION: attempt to extend an extent's allocation.
590 * initially, we will try to extend the extent's allocation
591 * in place. if this fails, we'll try to move the extent
592 * to a new set of blocks. if moving the extent, we initially
593 * will try to allocate disk blocks for the requested size
594 * (nnew). if this fails (nnew contigious free blocks not
595 * avaliable), we'll try to allocate a smaller number of
596 * blocks (producing a smaller extent), with this smaller
597 * number of blocks consisting of the requested number of
598 * blocks rounded down to the next smaller power of 2
599 * number (i.e. 16 -> 8). we'll continue to round down and
600 * retry the allocation until the number of blocks to allocate
601 * is smaller than the number of blocks per page.
604 * ip - the inode of the file.
605 * blkno - starting block number of the extents current allocation.
606 * nblks - number of blocks within the extents current allocation.
607 * newnblks - pointer to a s64 value. on entry, this value is the
608 * the new desired extent size (number of blocks). on
609 * successful exit, this value is set to the extent's actual
610 * new size (new number of blocks).
611 * newblkno - the starting block number of the extents new allocation.
616 * -ENOSPC - insufficient disk resources.
619 extBrealloc(struct inode
*ip
,
620 s64 blkno
, s64 nblks
, s64
* newnblks
, s64
* newblkno
)
624 /* try to extend in place */
625 if ((rc
= dbExtend(ip
, blkno
, nblks
, *newnblks
- nblks
)) == 0) {
633 /* in place extension not possible.
634 * try to move the extent to a new set of blocks.
636 return (extBalloc(ip
, blkno
, newnblks
, newblkno
));
642 * NAME: extRoundDown()
644 * FUNCTION: round down a specified number of blocks to the next
645 * smallest power of 2 number.
648 * nb - the inode of the file.
651 * next smallest power of 2 number.
653 static s64
extRoundDown(s64 nb
)
658 for (i
= 0, m
= (u64
) 1 << 63; i
< 64; i
++, m
>>= 1) {
665 k
= ((k
- 1) & nb
) ? k
: k
>> 1;