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 *);
19 static s64 extRoundDown(s64 nb);
21 #define DPD(a) (printk("(a): %d\n",(a)))
22 #define DPC(a) (printk("(a): %c\n",(a)))
26 printk("(a): %x%08x ",(a)); \
28 printk("(a): %x ",(a) << 32); \
33 printk("(a): %x%08x\n",(a)); \
35 printk("(a): %x\n",(a) << 32); \
38 #define DPD1(a) (printk("(a): %d ",(a)))
39 #define DPX(a) (printk("(a): %08x\n",(a)))
40 #define DPX1(a) (printk("(a): %08x ",(a)))
41 #define DPS(a) (printk("%s\n",(a)))
42 #define DPE(a) (printk("\nENTERING: %s\n",(a)))
43 #define DPE1(a) (printk("\nENTERING: %s",(a)))
44 #define DPS1(a) (printk(" %s ",(a)))
50 * FUNCTION: allocate an extent for a specified page range within a
54 * ip - the inode of the file.
55 * xlen - requested extent length.
56 * pno - the starting page number with the file.
57 * xp - pointer to an xad. on entry, xad describes an
58 * extent that is used as an allocation hint if the
59 * xaddr of the xad is non-zero. on successful exit,
60 * the xad describes the newly allocated extent.
61 * abnr - bool indicating whether the newly allocated extent
62 * should be marked as allocated but not recorded.
67 * -ENOSPC - insufficient disk resources.
70 extAlloc(struct inode *ip, s64 xlen, s64 pno, xad_t * xp, bool abnr)
72 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
73 s64 nxlen, nxaddr, xoff, hint, xaddr = 0;
77 /* This blocks if we are low on resources */
78 txBeginAnon(ip->i_sb);
80 /* Avoid race with jfs_commit_inode() */
81 mutex_lock(&JFS_IP(ip)->commit_mutex);
83 /* validate extent length */
87 /* get the page's starting extent offset */
88 xoff = pno << sbi->l2nbperpage;
90 /* check if an allocation hint was provided */
91 if ((hint = addressXAD(xp))) {
92 /* get the size of the extent described by the hint */
93 nxlen = lengthXAD(xp);
95 /* check if the hint is for the portion of the file
96 * immediately previous to the current allocation
97 * request and if hint extent has the same abnr
98 * value as the current request. if so, we can
99 * extend the hint extent to include the current
100 * extent if we can allocate the blocks immediately
101 * following the hint extent.
103 if (offsetXAD(xp) + nxlen == xoff &&
104 abnr == ((xp->flag & XAD_NOTRECORDED) ? true : false))
105 xaddr = hint + nxlen;
107 /* adjust the hint to the last block of the extent */
111 /* allocate the disk blocks for the extent. initially, extBalloc()
112 * will try to allocate disk blocks for the requested size (xlen).
113 * if this fails (xlen contiguous free blocks not available), it'll
114 * try to allocate a smaller number of blocks (producing a smaller
115 * extent), with this smaller number of blocks consisting of the
116 * requested number of blocks rounded down to the next smaller
117 * power of 2 number (i.e. 16 -> 8). it'll continue to round down
118 * and retry the allocation until the number of blocks to allocate
119 * is smaller than the number of blocks per page.
122 if ((rc = extBalloc(ip, hint ? hint : INOHINT(ip), &nxlen, &nxaddr))) {
123 mutex_unlock(&JFS_IP(ip)->commit_mutex);
127 /* Allocate blocks to quota. */
128 rc = dquot_alloc_block(ip, nxlen);
130 dbFree(ip, nxaddr, (s64) nxlen);
131 mutex_unlock(&JFS_IP(ip)->commit_mutex);
135 /* determine the value of the extent flag */
136 xflag = abnr ? XAD_NOTRECORDED : 0;
138 /* if we can extend the hint extent to cover the current request,
139 * extend it. otherwise, insert a new extent to
140 * cover the current request.
142 if (xaddr && xaddr == nxaddr)
143 rc = xtExtend(0, ip, xoff, (int) nxlen, 0);
145 rc = xtInsert(0, ip, xflag, xoff, (int) nxlen, &nxaddr, 0);
147 /* if the extend or insert failed,
148 * free the newly allocated blocks and return the error.
151 dbFree(ip, nxaddr, nxlen);
152 dquot_free_block(ip, nxlen);
153 mutex_unlock(&JFS_IP(ip)->commit_mutex);
157 /* set the results of the extent allocation */
158 XADaddress(xp, nxaddr);
159 XADlength(xp, nxlen);
163 mark_inode_dirty(ip);
165 mutex_unlock(&JFS_IP(ip)->commit_mutex);
167 * COMMIT_SyncList flags an anonymous tlock on page that is on
169 * We need to commit the inode to get the page written disk.
171 if (test_and_clear_cflag(COMMIT_Synclist,ip))
172 jfs_commit_inode(ip, 0);
180 * FUNCTION: produce an extent allocation hint for a file offset.
183 * ip - the inode of the file.
184 * offset - file offset for which the hint is needed.
185 * xp - pointer to the xad that is to be filled in with
192 int extHint(struct inode *ip, s64 offset, xad_t * xp)
194 struct super_block *sb = ip->i_sb;
195 int nbperpage = JFS_SBI(sb)->nbperpage;
202 /* init the hint as "no hint provided" */
205 /* determine the starting extent offset of the page previous
206 * to the page containing the offset.
208 prev = ((offset & ~POFFSET) >> JFS_SBI(sb)->l2bsize) - nbperpage;
210 /* if the offset is in the first page of the file, no hint provided.
215 rc = xtLookup(ip, prev, nbperpage, &xflag, &xaddr, &xlen, 0);
217 if ((rc == 0) && xlen) {
218 if (xlen != nbperpage) {
219 jfs_error(ip->i_sb, "corrupt xtree\n");
222 XADaddress(xp, xaddr);
226 * only preserve the abnr flag within the xad flags
227 * of the returned hint.
229 xp->flag = xflag & XAD_NOTRECORDED;
241 * FUNCTION: change a page with a file from not recorded to recorded.
244 * ip - inode of the file.
245 * cp - cbuf of the file page.
250 * -ENOSPC - insufficient disk resources.
252 int extRecord(struct inode *ip, xad_t * xp)
256 txBeginAnon(ip->i_sb);
258 mutex_lock(&JFS_IP(ip)->commit_mutex);
260 /* update the extent */
261 rc = xtUpdate(0, ip, xp);
263 mutex_unlock(&JFS_IP(ip)->commit_mutex);
270 * FUNCTION: allocate disk blocks to form an extent.
272 * initially, we will try to allocate disk blocks for the
273 * requested size (nblocks). if this fails (nblocks
274 * contiguous free blocks not available), we'll try to allocate
275 * a smaller number of blocks (producing a smaller extent), with
276 * this smaller number of blocks consisting of the requested
277 * number of blocks rounded down to the next smaller power of 2
278 * number (i.e. 16 -> 8). we'll continue to round down and
279 * retry the allocation until the number of blocks to allocate
280 * is smaller than the number of blocks per page.
283 * ip - the inode of the file.
284 * hint - disk block number to be used as an allocation hint.
285 * *nblocks - pointer to an s64 value. on entry, this value specifies
286 * the desired number of block to be allocated. on successful
287 * exit, this value is set to the number of blocks actually
289 * blkno - pointer to a block address that is filled in on successful
290 * return with the starting block number of the newly
291 * allocated block range.
296 * -ENOSPC - insufficient disk resources.
299 extBalloc(struct inode *ip, s64 hint, s64 * nblocks, s64 * blkno)
301 struct jfs_inode_info *ji = JFS_IP(ip);
302 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
303 s64 nb, nblks, daddr, max;
304 int rc, nbperpage = sbi->nbperpage;
305 struct bmap *bmp = sbi->bmap;
308 /* get the number of blocks to initially attempt to allocate.
309 * we'll first try the number of blocks requested unless this
310 * number is greater than the maximum number of contiguous free
311 * blocks in the map. in that case, we'll start off with the
314 max = (s64) 1 << bmp->db_maxfreebud;
315 if (*nblocks >= max && *nblocks > nbperpage)
316 nb = nblks = (max > nbperpage) ? max : nbperpage;
318 nb = nblks = *nblocks;
320 /* try to allocate blocks */
321 while ((rc = dbAlloc(ip, hint, nb, &daddr)) != 0) {
322 /* if something other than an out of space error,
323 * stop and return this error.
328 /* decrease the allocation request size */
329 nb = min(nblks, extRoundDown(nb));
331 /* give up if we cannot cover a page */
339 if (S_ISREG(ip->i_mode) && (ji->fileset == FILESYSTEM_I)) {
340 ag = BLKTOAG(daddr, sbi);
341 spin_lock_irq(&ji->ag_lock);
342 if (ji->active_ag == -1) {
343 atomic_inc(&bmp->db_active[ag]);
345 } else if (ji->active_ag != ag) {
346 atomic_dec(&bmp->db_active[ji->active_ag]);
347 atomic_inc(&bmp->db_active[ag]);
350 spin_unlock_irq(&ji->ag_lock);
357 * NAME: extRoundDown()
359 * FUNCTION: round down a specified number of blocks to the next
360 * smallest power of 2 number.
363 * nb - the inode of the file.
366 * next smallest power of 2 number.
368 static s64 extRoundDown(s64 nb)
373 for (i = 0, m = (u64) 1 << 63; i < 64; i++, m >>= 1) {
380 k = ((k - 1) & nb) ? k : k >> 1;