1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2004
7 #include <linux/quotaops.h>
8 #include "jfs_incore.h"
10 #include "jfs_superblock.h"
12 #include "jfs_extent.h"
13 #include "jfs_debug.h"
18 static int extBalloc(struct inode
*, s64
, s64
*, s64
*);
20 static int extBrealloc(struct inode
*, s64
, s64
, s64
*, s64
*);
22 static s64
extRoundDown(s64 nb
);
24 #define DPD(a) (printk("(a): %d\n",(a)))
25 #define DPC(a) (printk("(a): %c\n",(a)))
29 printk("(a): %x%08x ",(a)); \
31 printk("(a): %x ",(a) << 32); \
36 printk("(a): %x%08x\n",(a)); \
38 printk("(a): %x\n",(a) << 32); \
41 #define DPD1(a) (printk("(a): %d ",(a)))
42 #define DPX(a) (printk("(a): %08x\n",(a)))
43 #define DPX1(a) (printk("(a): %08x ",(a)))
44 #define DPS(a) (printk("%s\n",(a)))
45 #define DPE(a) (printk("\nENTERING: %s\n",(a)))
46 #define DPE1(a) (printk("\nENTERING: %s",(a)))
47 #define DPS1(a) (printk(" %s ",(a)))
53 * FUNCTION: allocate an extent for a specified page range within a
57 * ip - the inode of the file.
58 * xlen - requested extent length.
59 * pno - the starting page number with the file.
60 * xp - pointer to an xad. on entry, xad describes an
61 * extent that is used as an allocation hint if the
62 * xaddr of the xad is non-zero. on successful exit,
63 * the xad describes the newly allocated extent.
64 * abnr - bool indicating whether the newly allocated extent
65 * should be marked as allocated but not recorded.
70 * -ENOSPC - insufficient disk resources.
73 extAlloc(struct inode
*ip
, s64 xlen
, s64 pno
, xad_t
* xp
, bool abnr
)
75 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
76 s64 nxlen
, nxaddr
, xoff
, hint
, xaddr
= 0;
80 /* This blocks if we are low on resources */
81 txBeginAnon(ip
->i_sb
);
83 /* Avoid race with jfs_commit_inode() */
84 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
86 /* validate extent length */
90 /* get the page's starting extent offset */
91 xoff
= pno
<< sbi
->l2nbperpage
;
93 /* check if an allocation hint was provided */
94 if ((hint
= addressXAD(xp
))) {
95 /* get the size of the extent described by the hint */
96 nxlen
= lengthXAD(xp
);
98 /* check if the hint is for the portion of the file
99 * immediately previous to the current allocation
100 * request and if hint extent has the same abnr
101 * value as the current request. if so, we can
102 * extend the hint extent to include the current
103 * extent if we can allocate the blocks immediately
104 * following the hint extent.
106 if (offsetXAD(xp
) + nxlen
== xoff
&&
107 abnr
== ((xp
->flag
& XAD_NOTRECORDED
) ? true : false))
108 xaddr
= hint
+ nxlen
;
110 /* adjust the hint to the last block of the extent */
114 /* allocate the disk blocks for the extent. initially, extBalloc()
115 * will try to allocate disk blocks for the requested size (xlen).
116 * if this fails (xlen contiguous free blocks not available), it'll
117 * try to allocate a smaller number of blocks (producing a smaller
118 * extent), with this smaller number of blocks consisting of the
119 * requested number of blocks rounded down to the next smaller
120 * power of 2 number (i.e. 16 -> 8). it'll continue to round down
121 * and retry the allocation until the number of blocks to allocate
122 * is smaller than the number of blocks per page.
125 if ((rc
= extBalloc(ip
, hint
? hint
: INOHINT(ip
), &nxlen
, &nxaddr
))) {
126 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
130 /* Allocate blocks to quota. */
131 rc
= dquot_alloc_block(ip
, nxlen
);
133 dbFree(ip
, nxaddr
, (s64
) nxlen
);
134 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
138 /* determine the value of the extent flag */
139 xflag
= abnr
? XAD_NOTRECORDED
: 0;
141 /* if we can extend the hint extent to cover the current request,
142 * extend it. otherwise, insert a new extent to
143 * cover the current request.
145 if (xaddr
&& xaddr
== nxaddr
)
146 rc
= xtExtend(0, ip
, xoff
, (int) nxlen
, 0);
148 rc
= xtInsert(0, ip
, xflag
, xoff
, (int) nxlen
, &nxaddr
, 0);
150 /* if the extend or insert failed,
151 * free the newly allocated blocks and return the error.
154 dbFree(ip
, nxaddr
, nxlen
);
155 dquot_free_block(ip
, nxlen
);
156 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
160 /* set the results of the extent allocation */
161 XADaddress(xp
, nxaddr
);
162 XADlength(xp
, nxlen
);
166 mark_inode_dirty(ip
);
168 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
170 * COMMIT_SyncList flags an anonymous tlock on page that is on
172 * We need to commit the inode to get the page written disk.
174 if (test_and_clear_cflag(COMMIT_Synclist
,ip
))
175 jfs_commit_inode(ip
, 0);
185 * FUNCTION: extend the allocation of a file extent containing a
186 * partial back last page.
189 * ip - the inode of the file.
190 * cp - cbuf for the partial backed last page.
191 * xlen - request size of the resulting extent.
192 * xp - pointer to an xad. on successful exit, the xad
193 * describes the newly allocated extent.
194 * abnr - bool indicating whether the newly allocated extent
195 * should be marked as allocated but not recorded.
200 * -ENOSPC - insufficient disk resources.
202 int extRealloc(struct inode
*ip
, s64 nxlen
, xad_t
* xp
, bool abnr
)
204 struct super_block
*sb
= ip
->i_sb
;
205 s64 xaddr
, xlen
, nxaddr
, delta
, xoff
;
206 s64 ntail
, nextend
, ninsert
;
207 int rc
, nbperpage
= JFS_SBI(sb
)->nbperpage
;
210 /* This blocks if we are low on resources */
211 txBeginAnon(ip
->i_sb
);
213 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
214 /* validate extent length */
218 /* get the extend (partial) page's disk block address and
221 xaddr
= addressXAD(xp
);
222 xlen
= lengthXAD(xp
);
223 xoff
= offsetXAD(xp
);
225 /* if the extend page is abnr and if the request is for
226 * the extent to be allocated and recorded,
227 * make the page allocated and recorded.
229 if ((xp
->flag
& XAD_NOTRECORDED
) && !abnr
) {
231 if ((rc
= xtUpdate(0, ip
, xp
)))
235 /* try to allocated the request number of blocks for the
236 * extent. dbRealloc() first tries to satisfy the request
237 * by extending the allocation in place. otherwise, it will
238 * try to allocate a new set of blocks large enough for the
239 * request. in satisfying a request, dbReAlloc() may allocate
240 * less than what was request but will always allocate enough
241 * space as to satisfy the extend page.
243 if ((rc
= extBrealloc(ip
, xaddr
, xlen
, &nxlen
, &nxaddr
)))
246 /* Allocat blocks to quota. */
247 rc
= dquot_alloc_block(ip
, nxlen
);
249 dbFree(ip
, nxaddr
, (s64
) nxlen
);
250 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
254 delta
= nxlen
- xlen
;
256 /* check if the extend page is not abnr but the request is abnr
257 * and the allocated disk space is for more than one page. if this
258 * is the case, there is a miss match of abnr between the extend page
259 * and the one or more pages following the extend page. as a result,
260 * two extents will have to be manipulated. the first will be that
261 * of the extent of the extend page and will be manipulated thru
262 * an xtExtend() or an xtTailgate(), depending upon whether the
263 * disk allocation occurred as an inplace extension. the second
264 * extent will be manipulated (created) through an xtInsert() and
265 * will be for the pages following the extend page.
267 if (abnr
&& (!(xp
->flag
& XAD_NOTRECORDED
)) && (nxlen
> nbperpage
)) {
269 nextend
= ntail
- xlen
;
270 ninsert
= nxlen
- nbperpage
;
272 xflag
= XAD_NOTRECORDED
;
281 /* if we were able to extend the disk allocation in place,
282 * extend the extent. otherwise, move the extent to a
285 if (xaddr
== nxaddr
) {
286 /* extend the extent */
287 if ((rc
= xtExtend(0, ip
, xoff
+ xlen
, (int) nextend
, 0))) {
288 dbFree(ip
, xaddr
+ xlen
, delta
);
289 dquot_free_block(ip
, nxlen
);
294 * move the extent to a new location:
296 * xtTailgate() accounts for relocated tail extent;
298 if ((rc
= xtTailgate(0, ip
, xoff
, (int) ntail
, nxaddr
, 0))) {
299 dbFree(ip
, nxaddr
, nxlen
);
300 dquot_free_block(ip
, nxlen
);
306 /* check if we need to also insert a new extent */
308 /* perform the insert. if it fails, free the blocks
309 * to be inserted and make it appear that we only did
310 * the xtExtend() or xtTailgate() above.
312 xaddr
= nxaddr
+ ntail
;
313 if (xtInsert (0, ip
, xflag
, xoff
+ ntail
, (int) ninsert
,
315 dbFree(ip
, xaddr
, (s64
) ninsert
);
322 /* set the return results */
323 XADaddress(xp
, nxaddr
);
324 XADlength(xp
, nxlen
);
328 mark_inode_dirty(ip
);
330 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
339 * FUNCTION: produce an extent allocation hint for a file offset.
342 * ip - the inode of the file.
343 * offset - file offset for which the hint is needed.
344 * xp - pointer to the xad that is to be filled in with
351 int extHint(struct inode
*ip
, s64 offset
, xad_t
* xp
)
353 struct super_block
*sb
= ip
->i_sb
;
354 int nbperpage
= JFS_SBI(sb
)->nbperpage
;
361 /* init the hint as "no hint provided" */
364 /* determine the starting extent offset of the page previous
365 * to the page containing the offset.
367 prev
= ((offset
& ~POFFSET
) >> JFS_SBI(sb
)->l2bsize
) - nbperpage
;
369 /* if the offset is in the first page of the file, no hint provided.
374 rc
= xtLookup(ip
, prev
, nbperpage
, &xflag
, &xaddr
, &xlen
, 0);
376 if ((rc
== 0) && xlen
) {
377 if (xlen
!= nbperpage
) {
378 jfs_error(ip
->i_sb
, "corrupt xtree\n");
381 XADaddress(xp
, xaddr
);
385 * only preserve the abnr flag within the xad flags
386 * of the returned hint.
388 xp
->flag
= xflag
& XAD_NOTRECORDED
;
400 * FUNCTION: change a page with a file from not recorded to recorded.
403 * ip - inode of the file.
404 * cp - cbuf of the file page.
409 * -ENOSPC - insufficient disk resources.
411 int extRecord(struct inode
*ip
, xad_t
* xp
)
415 txBeginAnon(ip
->i_sb
);
417 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
419 /* update the extent */
420 rc
= xtUpdate(0, ip
, xp
);
422 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
431 * FUNCTION: allocate disk space for a file page that represents
435 * ip - the inode of the file.
436 * cp - cbuf of the file page represent the hole.
441 * -ENOSPC - insufficient disk resources.
443 int extFill(struct inode
*ip
, xad_t
* xp
)
445 int rc
, nbperpage
= JFS_SBI(ip
->i_sb
)->nbperpage
;
446 s64 blkno
= offsetXAD(xp
) >> ip
->i_blkbits
;
448 // assert(ISSPARSE(ip));
450 /* initialize the extent allocation hint */
453 /* allocate an extent to fill the hole */
454 if ((rc
= extAlloc(ip
, nbperpage
, blkno
, xp
, false)))
457 assert(lengthPXD(xp
) == nbperpage
);
467 * FUNCTION: allocate disk blocks to form an extent.
469 * initially, we will try to allocate disk blocks for the
470 * requested size (nblocks). if this fails (nblocks
471 * contiguous free blocks not available), we'll try to allocate
472 * a smaller number of blocks (producing a smaller extent), with
473 * this smaller number of blocks consisting of the requested
474 * number of blocks rounded down to the next smaller power of 2
475 * number (i.e. 16 -> 8). we'll continue to round down and
476 * retry the allocation until the number of blocks to allocate
477 * is smaller than the number of blocks per page.
480 * ip - the inode of the file.
481 * hint - disk block number to be used as an allocation hint.
482 * *nblocks - pointer to an s64 value. on entry, this value specifies
483 * the desired number of block to be allocated. on successful
484 * exit, this value is set to the number of blocks actually
486 * blkno - pointer to a block address that is filled in on successful
487 * return with the starting block number of the newly
488 * allocated block range.
493 * -ENOSPC - insufficient disk resources.
496 extBalloc(struct inode
*ip
, s64 hint
, s64
* nblocks
, s64
* blkno
)
498 struct jfs_inode_info
*ji
= JFS_IP(ip
);
499 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
500 s64 nb
, nblks
, daddr
, max
;
501 int rc
, nbperpage
= sbi
->nbperpage
;
502 struct bmap
*bmp
= sbi
->bmap
;
505 /* get the number of blocks to initially attempt to allocate.
506 * we'll first try the number of blocks requested unless this
507 * number is greater than the maximum number of contiguous free
508 * blocks in the map. in that case, we'll start off with the
511 max
= (s64
) 1 << bmp
->db_maxfreebud
;
512 if (*nblocks
>= max
&& *nblocks
> nbperpage
)
513 nb
= nblks
= (max
> nbperpage
) ? max
: nbperpage
;
515 nb
= nblks
= *nblocks
;
517 /* try to allocate blocks */
518 while ((rc
= dbAlloc(ip
, hint
, nb
, &daddr
)) != 0) {
519 /* if something other than an out of space error,
520 * stop and return this error.
525 /* decrease the allocation request size */
526 nb
= min(nblks
, extRoundDown(nb
));
528 /* give up if we cannot cover a page */
536 if (S_ISREG(ip
->i_mode
) && (ji
->fileset
== FILESYSTEM_I
)) {
537 ag
= BLKTOAG(daddr
, sbi
);
538 spin_lock_irq(&ji
->ag_lock
);
539 if (ji
->active_ag
== -1) {
540 atomic_inc(&bmp
->db_active
[ag
]);
542 } else if (ji
->active_ag
!= ag
) {
543 atomic_dec(&bmp
->db_active
[ji
->active_ag
]);
544 atomic_inc(&bmp
->db_active
[ag
]);
547 spin_unlock_irq(&ji
->ag_lock
);
556 * NAME: extBrealloc()
558 * FUNCTION: attempt to extend an extent's allocation.
560 * Initially, we will try to extend the extent's allocation
561 * in place. If this fails, we'll try to move the extent
562 * to a new set of blocks. If moving the extent, we initially
563 * will try to allocate disk blocks for the requested size
564 * (newnblks). if this fails (new contiguous free blocks not
565 * available), we'll try to allocate a smaller number of
566 * blocks (producing a smaller extent), with this smaller
567 * number of blocks consisting of the requested number of
568 * blocks rounded down to the next smaller power of 2
569 * number (i.e. 16 -> 8). We'll continue to round down and
570 * retry the allocation until the number of blocks to allocate
571 * is smaller than the number of blocks per page.
574 * ip - the inode of the file.
575 * blkno - starting block number of the extents current allocation.
576 * nblks - number of blocks within the extents current allocation.
577 * newnblks - pointer to a s64 value. on entry, this value is the
578 * the new desired extent size (number of blocks). on
579 * successful exit, this value is set to the extent's actual
580 * new size (new number of blocks).
581 * newblkno - the starting block number of the extents new allocation.
586 * -ENOSPC - insufficient disk resources.
589 extBrealloc(struct inode
*ip
,
590 s64 blkno
, s64 nblks
, s64
* newnblks
, s64
* newblkno
)
594 /* try to extend in place */
595 if ((rc
= dbExtend(ip
, blkno
, nblks
, *newnblks
- nblks
)) == 0) {
603 /* in place extension not possible.
604 * try to move the extent to a new set of blocks.
606 return (extBalloc(ip
, blkno
, newnblks
, newblkno
));
612 * NAME: extRoundDown()
614 * FUNCTION: round down a specified number of blocks to the next
615 * smallest power of 2 number.
618 * nb - the inode of the file.
621 * next smallest power of 2 number.
623 static s64
extRoundDown(s64 nb
)
628 for (i
= 0, m
= (u64
) 1 << 63; i
< 64; i
++, m
>>= 1) {
635 k
= ((k
- 1) & nb
) ? k
: k
>> 1;