JFS: use list_move instead of list_del/list_add
[deliverable/linux.git] / fs / jfs / jfs_dmap.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) International Business Machines Corp., 2000-2004
b40c2e66 3 * Portions Copyright (C) Tino Reichardt, 2012
1da177e4
LT
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
63f83c9f 7 * the Free Software Foundation; either version 2 of the License, or
1da177e4 8 * (at your option) any later version.
63f83c9f 9 *
1da177e4
LT
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
63f83c9f 16 * along with this program; if not, write to the Free Software
1da177e4
LT
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/fs.h>
5a0e3ad6 21#include <linux/slab.h>
1da177e4
LT
22#include "jfs_incore.h"
23#include "jfs_superblock.h"
24#include "jfs_dmap.h"
25#include "jfs_imap.h"
26#include "jfs_lock.h"
27#include "jfs_metapage.h"
28#include "jfs_debug.h"
b40c2e66 29#include "jfs_discard.h"
1da177e4 30
1da177e4
LT
31/*
32 * SERIALIZATION of the Block Allocation Map.
33 *
34 * the working state of the block allocation map is accessed in
35 * two directions:
63f83c9f 36 *
1da177e4
LT
37 * 1) allocation and free requests that start at the dmap
38 * level and move up through the dmap control pages (i.e.
39 * the vast majority of requests).
63f83c9f
DK
40 *
41 * 2) allocation requests that start at dmap control page
1da177e4 42 * level and work down towards the dmaps.
1da177e4 43 *
63f83c9f
DK
44 * the serialization scheme used here is as follows.
45 *
46 * requests which start at the bottom are serialized against each
47 * other through buffers and each requests holds onto its buffers
48 * as it works it way up from a single dmap to the required level
1da177e4
LT
49 * of dmap control page.
50 * requests that start at the top are serialized against each other
51 * and request that start from the bottom by the multiple read/single
52 * write inode lock of the bmap inode. requests starting at the top
53 * take this lock in write mode while request starting at the bottom
54 * take the lock in read mode. a single top-down request may proceed
63f83c9f
DK
55 * exclusively while multiple bottoms-up requests may proceed
56 * simultaneously (under the protection of busy buffers).
57 *
1da177e4
LT
58 * in addition to information found in dmaps and dmap control pages,
59 * the working state of the block allocation map also includes read/
60 * write information maintained in the bmap descriptor (i.e. total
61 * free block count, allocation group level free block counts).
62 * a single exclusive lock (BMAP_LOCK) is used to guard this information
63 * in the face of multiple-bottoms up requests.
64 * (lock ordering: IREAD_LOCK, BMAP_LOCK);
63f83c9f 65 *
1da177e4
LT
66 * accesses to the persistent state of the block allocation map (limited
67 * to the persistent bitmaps in dmaps) is guarded by (busy) buffers.
68 */
69
1de87444
IM
70#define BMAP_LOCK_INIT(bmp) mutex_init(&bmp->db_bmaplock)
71#define BMAP_LOCK(bmp) mutex_lock(&bmp->db_bmaplock)
72#define BMAP_UNLOCK(bmp) mutex_unlock(&bmp->db_bmaplock)
1da177e4
LT
73
74/*
75 * forward references
76 */
77static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
78 int nblocks);
79static void dbSplit(dmtree_t * tp, int leafno, int splitsz, int newval);
b6a47fd8 80static int dbBackSplit(dmtree_t * tp, int leafno);
56d12549 81static int dbJoin(dmtree_t * tp, int leafno, int newval);
1da177e4
LT
82static void dbAdjTree(dmtree_t * tp, int leafno, int newval);
83static int dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc,
84 int level);
85static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results);
86static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
87 int nblocks);
88static int dbAllocNear(struct bmap * bmp, struct dmap * dp, s64 blkno,
89 int nblocks,
90 int l2nb, s64 * results);
91static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
92 int nblocks);
93static int dbAllocDmapLev(struct bmap * bmp, struct dmap * dp, int nblocks,
94 int l2nb,
95 s64 * results);
96static int dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb,
97 s64 * results);
98static int dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno,
99 s64 * results);
100static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks);
101static int dbFindBits(u32 word, int l2nb);
102static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno);
103static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx);
56d12549
DK
104static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
105 int nblocks);
1da177e4
LT
106static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
107 int nblocks);
108static int dbMaxBud(u8 * cp);
1da177e4
LT
109static int blkstol2(s64 nb);
110
111static int cntlz(u32 value);
112static int cnttz(u32 word);
113
114static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
115 int nblocks);
116static int dbInitDmap(struct dmap * dp, s64 blkno, int nblocks);
117static int dbInitDmapTree(struct dmap * dp);
118static int dbInitTree(struct dmaptree * dtp);
119static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i);
120static int dbGetL2AGSize(s64 nblocks);
121
122/*
123 * buddy table
124 *
63f83c9f 125 * table used for determining buddy sizes within characters of
1da177e4
LT
126 * dmap bitmap words. the characters themselves serve as indexes
127 * into the table, with the table elements yielding the maximum
128 * binary buddy of free bits within the character.
129 */
4d5dbd09 130static const s8 budtab[256] = {
1da177e4
LT
131 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
132 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
133 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
134 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
135 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
136 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
137 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
138 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
139 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
140 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
141 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
142 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
143 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
144 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
145 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
146 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1
147};
148
1da177e4 149/*
63f83c9f 150 * NAME: dbMount()
1da177e4
LT
151 *
152 * FUNCTION: initializate the block allocation map.
153 *
154 * memory is allocated for the in-core bmap descriptor and
155 * the in-core descriptor is initialized from disk.
156 *
157 * PARAMETERS:
f720e3ba 158 * ipbmap - pointer to in-core inode for the block map.
1da177e4
LT
159 *
160 * RETURN VALUES:
f720e3ba
DK
161 * 0 - success
162 * -ENOMEM - insufficient memory
163 * -EIO - i/o error
1da177e4
LT
164 */
165int dbMount(struct inode *ipbmap)
166{
167 struct bmap *bmp;
168 struct dbmap_disk *dbmp_le;
169 struct metapage *mp;
170 int i;
171
172 /*
173 * allocate/initialize the in-memory bmap descriptor
174 */
175 /* allocate memory for the in-memory bmap descriptor */
176 bmp = kmalloc(sizeof(struct bmap), GFP_KERNEL);
177 if (bmp == NULL)
178 return -ENOMEM;
179
180 /* read the on-disk bmap descriptor. */
181 mp = read_metapage(ipbmap,
182 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
183 PSIZE, 0);
184 if (mp == NULL) {
185 kfree(bmp);
186 return -EIO;
187 }
188
189 /* copy the on-disk bmap descriptor to its in-memory version. */
190 dbmp_le = (struct dbmap_disk *) mp->data;
191 bmp->db_mapsize = le64_to_cpu(dbmp_le->dn_mapsize);
192 bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree);
193 bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
194 bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
195 bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel);
196 bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag);
197 bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref);
198 bmp->db_aglevel = le32_to_cpu(dbmp_le->dn_aglevel);
d7eecb48 199 bmp->db_agheight = le32_to_cpu(dbmp_le->dn_agheight);
1da177e4
LT
200 bmp->db_agwidth = le32_to_cpu(dbmp_le->dn_agwidth);
201 bmp->db_agstart = le32_to_cpu(dbmp_le->dn_agstart);
202 bmp->db_agl2size = le32_to_cpu(dbmp_le->dn_agl2size);
203 for (i = 0; i < MAXAG; i++)
204 bmp->db_agfree[i] = le64_to_cpu(dbmp_le->dn_agfree[i]);
205 bmp->db_agsize = le64_to_cpu(dbmp_le->dn_agsize);
206 bmp->db_maxfreebud = dbmp_le->dn_maxfreebud;
207
208 /* release the buffer. */
209 release_metapage(mp);
210
211 /* bind the bmap inode and the bmap descriptor to each other. */
212 bmp->db_ipbmap = ipbmap;
213 JFS_SBI(ipbmap->i_sb)->bmap = bmp;
214
215 memset(bmp->db_active, 0, sizeof(bmp->db_active));
1da177e4
LT
216
217 /*
218 * allocate/initialize the bmap lock
219 */
220 BMAP_LOCK_INIT(bmp);
221
222 return (0);
223}
224
225
226/*
63f83c9f 227 * NAME: dbUnmount()
1da177e4
LT
228 *
229 * FUNCTION: terminate the block allocation map in preparation for
230 * file system unmount.
231 *
63f83c9f 232 * the in-core bmap descriptor is written to disk and
1da177e4
LT
233 * the memory for this descriptor is freed.
234 *
235 * PARAMETERS:
f720e3ba 236 * ipbmap - pointer to in-core inode for the block map.
1da177e4
LT
237 *
238 * RETURN VALUES:
f720e3ba
DK
239 * 0 - success
240 * -EIO - i/o error
1da177e4
LT
241 */
242int dbUnmount(struct inode *ipbmap, int mounterror)
243{
244 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
1da177e4
LT
245
246 if (!(mounterror || isReadOnly(ipbmap)))
247 dbSync(ipbmap);
248
249 /*
250 * Invalidate the page cache buffers
251 */
252 truncate_inode_pages(ipbmap->i_mapping, 0);
253
1da177e4
LT
254 /* free the memory for the in-memory bmap. */
255 kfree(bmp);
256
257 return (0);
258}
259
260/*
261 * dbSync()
262 */
263int dbSync(struct inode *ipbmap)
264{
265 struct dbmap_disk *dbmp_le;
266 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
267 struct metapage *mp;
268 int i;
269
270 /*
271 * write bmap global control page
272 */
273 /* get the buffer for the on-disk bmap descriptor. */
274 mp = read_metapage(ipbmap,
275 BMAPBLKNO << JFS_SBI(ipbmap->i_sb)->l2nbperpage,
276 PSIZE, 0);
277 if (mp == NULL) {
278 jfs_err("dbSync: read_metapage failed!");
279 return -EIO;
280 }
281 /* copy the in-memory version of the bmap to the on-disk version */
282 dbmp_le = (struct dbmap_disk *) mp->data;
283 dbmp_le->dn_mapsize = cpu_to_le64(bmp->db_mapsize);
284 dbmp_le->dn_nfree = cpu_to_le64(bmp->db_nfree);
285 dbmp_le->dn_l2nbperpage = cpu_to_le32(bmp->db_l2nbperpage);
286 dbmp_le->dn_numag = cpu_to_le32(bmp->db_numag);
287 dbmp_le->dn_maxlevel = cpu_to_le32(bmp->db_maxlevel);
288 dbmp_le->dn_maxag = cpu_to_le32(bmp->db_maxag);
289 dbmp_le->dn_agpref = cpu_to_le32(bmp->db_agpref);
290 dbmp_le->dn_aglevel = cpu_to_le32(bmp->db_aglevel);
d7eecb48 291 dbmp_le->dn_agheight = cpu_to_le32(bmp->db_agheight);
1da177e4
LT
292 dbmp_le->dn_agwidth = cpu_to_le32(bmp->db_agwidth);
293 dbmp_le->dn_agstart = cpu_to_le32(bmp->db_agstart);
294 dbmp_le->dn_agl2size = cpu_to_le32(bmp->db_agl2size);
295 for (i = 0; i < MAXAG; i++)
296 dbmp_le->dn_agfree[i] = cpu_to_le64(bmp->db_agfree[i]);
297 dbmp_le->dn_agsize = cpu_to_le64(bmp->db_agsize);
298 dbmp_le->dn_maxfreebud = bmp->db_maxfreebud;
299
300 /* write the buffer */
301 write_metapage(mp);
302
303 /*
304 * write out dirty pages of bmap
305 */
28fd1298 306 filemap_write_and_wait(ipbmap->i_mapping);
1da177e4 307
1da177e4
LT
308 diWriteSpecial(ipbmap, 0);
309
310 return (0);
311}
312
1da177e4 313/*
63f83c9f 314 * NAME: dbFree()
1da177e4
LT
315 *
316 * FUNCTION: free the specified block range from the working block
317 * allocation map.
318 *
319 * the blocks will be free from the working map one dmap
320 * at a time.
321 *
322 * PARAMETERS:
f720e3ba
DK
323 * ip - pointer to in-core inode;
324 * blkno - starting block number to be freed.
325 * nblocks - number of blocks to be freed.
1da177e4
LT
326 *
327 * RETURN VALUES:
f720e3ba
DK
328 * 0 - success
329 * -EIO - i/o error
1da177e4
LT
330 */
331int dbFree(struct inode *ip, s64 blkno, s64 nblocks)
332{
333 struct metapage *mp;
334 struct dmap *dp;
335 int nb, rc;
336 s64 lblkno, rem;
337 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
338 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
b40c2e66 339 struct super_block *sb = ipbmap->i_sb;
1da177e4 340
82d5b9a7 341 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
1da177e4
LT
342
343 /* block to be freed better be within the mapsize. */
344 if (unlikely((blkno == 0) || (blkno + nblocks > bmp->db_mapsize))) {
345 IREAD_UNLOCK(ipbmap);
346 printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
347 (unsigned long long) blkno,
348 (unsigned long long) nblocks);
349 jfs_error(ip->i_sb,
350 "dbFree: block to be freed is outside the map");
351 return -EIO;
352 }
353
b40c2e66
TR
354 /**
355 * TRIM the blocks, when mounted with discard option
356 */
357 if (JFS_SBI(sb)->flag & JFS_DISCARD)
358 if (JFS_SBI(sb)->minblks_trim <= nblocks)
359 jfs_issue_discard(ipbmap, blkno, nblocks);
360
1da177e4
LT
361 /*
362 * free the blocks a dmap at a time.
363 */
364 mp = NULL;
365 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
366 /* release previous dmap if any */
367 if (mp) {
368 write_metapage(mp);
369 }
370
371 /* get the buffer for the current dmap. */
372 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
373 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
374 if (mp == NULL) {
375 IREAD_UNLOCK(ipbmap);
376 return -EIO;
377 }
378 dp = (struct dmap *) mp->data;
379
380 /* determine the number of blocks to be freed from
381 * this dmap.
382 */
383 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
384
1da177e4
LT
385 /* free the blocks. */
386 if ((rc = dbFreeDmap(bmp, dp, blkno, nb))) {
56d12549 387 jfs_error(ip->i_sb, "dbFree: error in block map\n");
1da177e4
LT
388 release_metapage(mp);
389 IREAD_UNLOCK(ipbmap);
390 return (rc);
391 }
1da177e4
LT
392 }
393
394 /* write the last buffer. */
395 write_metapage(mp);
396
397 IREAD_UNLOCK(ipbmap);
398
399 return (0);
400}
401
402
403/*
404 * NAME: dbUpdatePMap()
405 *
f720e3ba 406 * FUNCTION: update the allocation state (free or allocate) of the
1da177e4 407 * specified block range in the persistent block allocation map.
63f83c9f 408 *
1da177e4
LT
409 * the blocks will be updated in the persistent map one
410 * dmap at a time.
411 *
412 * PARAMETERS:
f720e3ba
DK
413 * ipbmap - pointer to in-core inode for the block map.
414 * free - 'true' if block range is to be freed from the persistent
415 * map; 'false' if it is to be allocated.
416 * blkno - starting block number of the range.
417 * nblocks - number of contiguous blocks in the range.
418 * tblk - transaction block;
1da177e4
LT
419 *
420 * RETURN VALUES:
f720e3ba
DK
421 * 0 - success
422 * -EIO - i/o error
1da177e4
LT
423 */
424int
425dbUpdatePMap(struct inode *ipbmap,
426 int free, s64 blkno, s64 nblocks, struct tblock * tblk)
427{
428 int nblks, dbitno, wbitno, rbits;
429 int word, nbits, nwords;
430 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
431 s64 lblkno, rem, lastlblkno;
432 u32 mask;
433 struct dmap *dp;
434 struct metapage *mp;
435 struct jfs_log *log;
436 int lsn, difft, diffp;
7fab479b 437 unsigned long flags;
1da177e4
LT
438
439 /* the blocks better be within the mapsize. */
440 if (blkno + nblocks > bmp->db_mapsize) {
441 printk(KERN_ERR "blkno = %Lx, nblocks = %Lx\n",
442 (unsigned long long) blkno,
443 (unsigned long long) nblocks);
444 jfs_error(ipbmap->i_sb,
445 "dbUpdatePMap: blocks are outside the map");
446 return -EIO;
447 }
448
449 /* compute delta of transaction lsn from log syncpt */
450 lsn = tblk->lsn;
451 log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
452 logdiff(difft, lsn, log);
453
454 /*
455 * update the block state a dmap at a time.
456 */
457 mp = NULL;
458 lastlblkno = 0;
459 for (rem = nblocks; rem > 0; rem -= nblks, blkno += nblks) {
460 /* get the buffer for the current dmap. */
461 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
462 if (lblkno != lastlblkno) {
463 if (mp) {
464 write_metapage(mp);
465 }
466
467 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE,
468 0);
469 if (mp == NULL)
470 return -EIO;
7fab479b 471 metapage_wait_for_io(mp);
1da177e4
LT
472 }
473 dp = (struct dmap *) mp->data;
474
475 /* determine the bit number and word within the dmap of
476 * the starting block. also determine how many blocks
477 * are to be updated within this dmap.
478 */
479 dbitno = blkno & (BPERDMAP - 1);
480 word = dbitno >> L2DBWORD;
481 nblks = min(rem, (s64)BPERDMAP - dbitno);
482
483 /* update the bits of the dmap words. the first and last
484 * words may only have a subset of their bits updated. if
485 * this is the case, we'll work against that word (i.e.
63f83c9f 486 * partial first and/or last) only in a single pass. a
1da177e4
LT
487 * single pass will also be used to update all words that
488 * are to have all their bits updated.
489 */
490 for (rbits = nblks; rbits > 0;
491 rbits -= nbits, dbitno += nbits) {
492 /* determine the bit number within the word and
493 * the number of bits within the word.
494 */
495 wbitno = dbitno & (DBWORD - 1);
496 nbits = min(rbits, DBWORD - wbitno);
497
498 /* check if only part of the word is to be updated. */
499 if (nbits < DBWORD) {
500 /* update (free or allocate) the bits
501 * in this word.
502 */
503 mask =
504 (ONES << (DBWORD - nbits) >> wbitno);
505 if (free)
506 dp->pmap[word] &=
507 cpu_to_le32(~mask);
508 else
509 dp->pmap[word] |=
510 cpu_to_le32(mask);
511
512 word += 1;
513 } else {
514 /* one or more words are to have all
515 * their bits updated. determine how
516 * many words and how many bits.
517 */
518 nwords = rbits >> L2DBWORD;
519 nbits = nwords << L2DBWORD;
520
521 /* update (free or allocate) the bits
522 * in these words.
523 */
524 if (free)
525 memset(&dp->pmap[word], 0,
526 nwords * 4);
527 else
528 memset(&dp->pmap[word], (int) ONES,
529 nwords * 4);
530
531 word += nwords;
532 }
533 }
534
535 /*
536 * update dmap lsn
537 */
538 if (lblkno == lastlblkno)
539 continue;
540
541 lastlblkno = lblkno;
542
be0bf7da 543 LOGSYNC_LOCK(log, flags);
1da177e4
LT
544 if (mp->lsn != 0) {
545 /* inherit older/smaller lsn */
546 logdiff(diffp, mp->lsn, log);
547 if (difft < diffp) {
548 mp->lsn = lsn;
549
550 /* move bp after tblock in logsync list */
1da177e4 551 list_move(&mp->synclist, &tblk->synclist);
1da177e4
LT
552 }
553
554 /* inherit younger/larger clsn */
1da177e4
LT
555 logdiff(difft, tblk->clsn, log);
556 logdiff(diffp, mp->clsn, log);
557 if (difft > diffp)
558 mp->clsn = tblk->clsn;
1da177e4
LT
559 } else {
560 mp->log = log;
561 mp->lsn = lsn;
562
563 /* insert bp after tblock in logsync list */
1da177e4
LT
564 log->count++;
565 list_add(&mp->synclist, &tblk->synclist);
566
567 mp->clsn = tblk->clsn;
1da177e4 568 }
be0bf7da 569 LOGSYNC_UNLOCK(log, flags);
1da177e4
LT
570 }
571
572 /* write the last buffer. */
573 if (mp) {
574 write_metapage(mp);
575 }
576
577 return (0);
578}
579
580
581/*
582 * NAME: dbNextAG()
583 *
f720e3ba 584 * FUNCTION: find the preferred allocation group for new allocations.
1da177e4
LT
585 *
586 * Within the allocation groups, we maintain a preferred
587 * allocation group which consists of a group with at least
588 * average free space. It is the preferred group that we target
589 * new inode allocation towards. The tie-in between inode
590 * allocation and block allocation occurs as we allocate the
591 * first (data) block of an inode and specify the inode (block)
592 * as the allocation hint for this block.
593 *
594 * We try to avoid having more than one open file growing in
595 * an allocation group, as this will lead to fragmentation.
596 * This differs from the old OS/2 method of trying to keep
597 * empty ags around for large allocations.
598 *
599 * PARAMETERS:
f720e3ba 600 * ipbmap - pointer to in-core inode for the block map.
1da177e4
LT
601 *
602 * RETURN VALUES:
f720e3ba 603 * the preferred allocation group number.
1da177e4
LT
604 */
605int dbNextAG(struct inode *ipbmap)
606{
607 s64 avgfree;
608 int agpref;
609 s64 hwm = 0;
610 int i;
611 int next_best = -1;
612 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
613
614 BMAP_LOCK(bmp);
615
616 /* determine the average number of free blocks within the ags. */
617 avgfree = (u32)bmp->db_nfree / bmp->db_numag;
618
619 /*
620 * if the current preferred ag does not have an active allocator
621 * and has at least average freespace, return it
622 */
623 agpref = bmp->db_agpref;
624 if ((atomic_read(&bmp->db_active[agpref]) == 0) &&
625 (bmp->db_agfree[agpref] >= avgfree))
626 goto unlock;
627
628 /* From the last preferred ag, find the next one with at least
629 * average free space.
630 */
631 for (i = 0 ; i < bmp->db_numag; i++, agpref++) {
632 if (agpref == bmp->db_numag)
633 agpref = 0;
634
635 if (atomic_read(&bmp->db_active[agpref]))
636 /* open file is currently growing in this ag */
637 continue;
638 if (bmp->db_agfree[agpref] >= avgfree) {
639 /* Return this one */
640 bmp->db_agpref = agpref;
641 goto unlock;
642 } else if (bmp->db_agfree[agpref] > hwm) {
643 /* Less than avg. freespace, but best so far */
644 hwm = bmp->db_agfree[agpref];
645 next_best = agpref;
646 }
647 }
648
649 /*
650 * If no inactive ag was found with average freespace, use the
651 * next best
652 */
653 if (next_best != -1)
654 bmp->db_agpref = next_best;
655 /* else leave db_agpref unchanged */
656unlock:
657 BMAP_UNLOCK(bmp);
658
659 /* return the preferred group.
660 */
661 return (bmp->db_agpref);
662}
663
664/*
665 * NAME: dbAlloc()
666 *
f720e3ba 667 * FUNCTION: attempt to allocate a specified number of contiguous free
1da177e4
LT
668 * blocks from the working allocation block map.
669 *
670 * the block allocation policy uses hints and a multi-step
671 * approach.
672 *
63f83c9f 673 * for allocation requests smaller than the number of blocks
1da177e4
LT
674 * per dmap, we first try to allocate the new blocks
675 * immediately following the hint. if these blocks are not
676 * available, we try to allocate blocks near the hint. if
63f83c9f 677 * no blocks near the hint are available, we next try to
1da177e4
LT
678 * allocate within the same dmap as contains the hint.
679 *
680 * if no blocks are available in the dmap or the allocation
681 * request is larger than the dmap size, we try to allocate
682 * within the same allocation group as contains the hint. if
683 * this does not succeed, we finally try to allocate anywhere
684 * within the aggregate.
685 *
686 * we also try to allocate anywhere within the aggregate for
687 * for allocation requests larger than the allocation group
688 * size or requests that specify no hint value.
689 *
690 * PARAMETERS:
f720e3ba
DK
691 * ip - pointer to in-core inode;
692 * hint - allocation hint.
693 * nblocks - number of contiguous blocks in the range.
694 * results - on successful return, set to the starting block number
1da177e4
LT
695 * of the newly allocated contiguous range.
696 *
697 * RETURN VALUES:
f720e3ba
DK
698 * 0 - success
699 * -ENOSPC - insufficient disk resources
700 * -EIO - i/o error
1da177e4
LT
701 */
702int dbAlloc(struct inode *ip, s64 hint, s64 nblocks, s64 * results)
703{
704 int rc, agno;
705 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
706 struct bmap *bmp;
707 struct metapage *mp;
708 s64 lblkno, blkno;
709 struct dmap *dp;
710 int l2nb;
711 s64 mapSize;
712 int writers;
713
714 /* assert that nblocks is valid */
715 assert(nblocks > 0);
716
1da177e4 717 /* get the log2 number of blocks to be allocated.
63f83c9f 718 * if the number of blocks is not a log2 multiple,
1da177e4
LT
719 * it will be rounded up to the next log2 multiple.
720 */
721 l2nb = BLKSTOL2(nblocks);
722
723 bmp = JFS_SBI(ip->i_sb)->bmap;
724
1da177e4
LT
725 mapSize = bmp->db_mapsize;
726
727 /* the hint should be within the map */
728 if (hint >= mapSize) {
729 jfs_error(ip->i_sb, "dbAlloc: the hint is outside the map");
730 return -EIO;
731 }
732
733 /* if the number of blocks to be allocated is greater than the
734 * allocation group size, try to allocate anywhere.
735 */
736 if (l2nb > bmp->db_agl2size) {
82d5b9a7 737 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
1da177e4
LT
738
739 rc = dbAllocAny(bmp, nblocks, l2nb, results);
1da177e4
LT
740
741 goto write_unlock;
742 }
743
744 /*
745 * If no hint, let dbNextAG recommend an allocation group
746 */
747 if (hint == 0)
748 goto pref_ag;
749
750 /* we would like to allocate close to the hint. adjust the
751 * hint to the block following the hint since the allocators
752 * will start looking for free space starting at this point.
753 */
754 blkno = hint + 1;
755
756 if (blkno >= bmp->db_mapsize)
757 goto pref_ag;
758
759 agno = blkno >> bmp->db_agl2size;
760
761 /* check if blkno crosses over into a new allocation group.
762 * if so, check if we should allow allocations within this
763 * allocation group.
764 */
765 if ((blkno & (bmp->db_agsize - 1)) == 0)
af901ca1 766 /* check if the AG is currently being written to.
1da177e4
LT
767 * if so, call dbNextAG() to find a non-busy
768 * AG with sufficient free space.
769 */
770 if (atomic_read(&bmp->db_active[agno]))
771 goto pref_ag;
772
773 /* check if the allocation request size can be satisfied from a
774 * single dmap. if so, try to allocate from the dmap containing
775 * the hint using a tiered strategy.
776 */
777 if (nblocks <= BPERDMAP) {
82d5b9a7 778 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
1da177e4
LT
779
780 /* get the buffer for the dmap containing the hint.
781 */
782 rc = -EIO;
783 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
784 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
785 if (mp == NULL)
786 goto read_unlock;
787
788 dp = (struct dmap *) mp->data;
789
790 /* first, try to satisfy the allocation request with the
791 * blocks beginning at the hint.
792 */
793 if ((rc = dbAllocNext(bmp, dp, blkno, (int) nblocks))
794 != -ENOSPC) {
795 if (rc == 0) {
796 *results = blkno;
1da177e4
LT
797 mark_metapage_dirty(mp);
798 }
799
800 release_metapage(mp);
801 goto read_unlock;
802 }
803
804 writers = atomic_read(&bmp->db_active[agno]);
805 if ((writers > 1) ||
806 ((writers == 1) && (JFS_IP(ip)->active_ag != agno))) {
807 /*
808 * Someone else is writing in this allocation
809 * group. To avoid fragmenting, try another ag
810 */
811 release_metapage(mp);
812 IREAD_UNLOCK(ipbmap);
813 goto pref_ag;
814 }
815
816 /* next, try to satisfy the allocation request with blocks
817 * near the hint.
818 */
819 if ((rc =
820 dbAllocNear(bmp, dp, blkno, (int) nblocks, l2nb, results))
821 != -ENOSPC) {
b38a3ab3 822 if (rc == 0)
1da177e4 823 mark_metapage_dirty(mp);
1da177e4
LT
824
825 release_metapage(mp);
826 goto read_unlock;
827 }
828
829 /* try to satisfy the allocation request with blocks within
830 * the same dmap as the hint.
831 */
832 if ((rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results))
833 != -ENOSPC) {
b38a3ab3 834 if (rc == 0)
1da177e4 835 mark_metapage_dirty(mp);
1da177e4
LT
836
837 release_metapage(mp);
838 goto read_unlock;
839 }
840
841 release_metapage(mp);
842 IREAD_UNLOCK(ipbmap);
843 }
844
845 /* try to satisfy the allocation request with blocks within
846 * the same allocation group as the hint.
847 */
82d5b9a7 848 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
b38a3ab3 849 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) != -ENOSPC)
1da177e4 850 goto write_unlock;
b38a3ab3 851
1da177e4
LT
852 IWRITE_UNLOCK(ipbmap);
853
854
855 pref_ag:
856 /*
857 * Let dbNextAG recommend a preferred allocation group
858 */
859 agno = dbNextAG(ipbmap);
82d5b9a7 860 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
1da177e4
LT
861
862 /* Try to allocate within this allocation group. if that fails, try to
863 * allocate anywhere in the map.
864 */
865 if ((rc = dbAllocAG(bmp, agno, nblocks, l2nb, results)) == -ENOSPC)
866 rc = dbAllocAny(bmp, nblocks, l2nb, results);
1da177e4
LT
867
868 write_unlock:
869 IWRITE_UNLOCK(ipbmap);
870
871 return (rc);
872
873 read_unlock:
874 IREAD_UNLOCK(ipbmap);
875
876 return (rc);
877}
878
879#ifdef _NOTYET
880/*
881 * NAME: dbAllocExact()
882 *
f720e3ba 883 * FUNCTION: try to allocate the requested extent;
1da177e4
LT
884 *
885 * PARAMETERS:
f720e3ba
DK
886 * ip - pointer to in-core inode;
887 * blkno - extent address;
888 * nblocks - extent length;
1da177e4
LT
889 *
890 * RETURN VALUES:
f720e3ba
DK
891 * 0 - success
892 * -ENOSPC - insufficient disk resources
893 * -EIO - i/o error
1da177e4
LT
894 */
895int dbAllocExact(struct inode *ip, s64 blkno, int nblocks)
896{
897 int rc;
898 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
899 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
900 struct dmap *dp;
901 s64 lblkno;
902 struct metapage *mp;
903
82d5b9a7 904 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
1da177e4
LT
905
906 /*
907 * validate extent request:
908 *
909 * note: defragfs policy:
63f83c9f 910 * max 64 blocks will be moved.
1da177e4
LT
911 * allocation request size must be satisfied from a single dmap.
912 */
913 if (nblocks <= 0 || nblocks > BPERDMAP || blkno >= bmp->db_mapsize) {
914 IREAD_UNLOCK(ipbmap);
915 return -EINVAL;
916 }
917
918 if (nblocks > ((s64) 1 << bmp->db_maxfreebud)) {
919 /* the free space is no longer available */
920 IREAD_UNLOCK(ipbmap);
921 return -ENOSPC;
922 }
923
924 /* read in the dmap covering the extent */
925 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
926 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
927 if (mp == NULL) {
928 IREAD_UNLOCK(ipbmap);
929 return -EIO;
930 }
931 dp = (struct dmap *) mp->data;
932
933 /* try to allocate the requested extent */
934 rc = dbAllocNext(bmp, dp, blkno, nblocks);
935
936 IREAD_UNLOCK(ipbmap);
937
b38a3ab3 938 if (rc == 0)
1da177e4 939 mark_metapage_dirty(mp);
b38a3ab3 940
1da177e4
LT
941 release_metapage(mp);
942
943 return (rc);
944}
945#endif /* _NOTYET */
946
947/*
948 * NAME: dbReAlloc()
949 *
f720e3ba 950 * FUNCTION: attempt to extend a current allocation by a specified
1da177e4
LT
951 * number of blocks.
952 *
953 * this routine attempts to satisfy the allocation request
954 * by first trying to extend the existing allocation in
955 * place by allocating the additional blocks as the blocks
956 * immediately following the current allocation. if these
957 * blocks are not available, this routine will attempt to
958 * allocate a new set of contiguous blocks large enough
959 * to cover the existing allocation plus the additional
960 * number of blocks required.
961 *
962 * PARAMETERS:
f720e3ba
DK
963 * ip - pointer to in-core inode requiring allocation.
964 * blkno - starting block of the current allocation.
965 * nblocks - number of contiguous blocks within the current
1da177e4 966 * allocation.
f720e3ba
DK
967 * addnblocks - number of blocks to add to the allocation.
968 * results - on successful return, set to the starting block number
1da177e4
LT
969 * of the existing allocation if the existing allocation
970 * was extended in place or to a newly allocated contiguous
971 * range if the existing allocation could not be extended
972 * in place.
973 *
974 * RETURN VALUES:
f720e3ba
DK
975 * 0 - success
976 * -ENOSPC - insufficient disk resources
977 * -EIO - i/o error
1da177e4
LT
978 */
979int
980dbReAlloc(struct inode *ip,
981 s64 blkno, s64 nblocks, s64 addnblocks, s64 * results)
982{
983 int rc;
984
985 /* try to extend the allocation in place.
986 */
987 if ((rc = dbExtend(ip, blkno, nblocks, addnblocks)) == 0) {
988 *results = blkno;
989 return (0);
990 } else {
991 if (rc != -ENOSPC)
992 return (rc);
993 }
994
995 /* could not extend the allocation in place, so allocate a
996 * new set of blocks for the entire request (i.e. try to get
997 * a range of contiguous blocks large enough to cover the
998 * existing allocation plus the additional blocks.)
999 */
1000 return (dbAlloc
1001 (ip, blkno + nblocks - 1, addnblocks + nblocks, results));
1002}
1003
1004
1005/*
1006 * NAME: dbExtend()
1007 *
f720e3ba 1008 * FUNCTION: attempt to extend a current allocation by a specified
1da177e4
LT
1009 * number of blocks.
1010 *
1011 * this routine attempts to satisfy the allocation request
1012 * by first trying to extend the existing allocation in
1013 * place by allocating the additional blocks as the blocks
1014 * immediately following the current allocation.
1015 *
1016 * PARAMETERS:
f720e3ba
DK
1017 * ip - pointer to in-core inode requiring allocation.
1018 * blkno - starting block of the current allocation.
1019 * nblocks - number of contiguous blocks within the current
1da177e4 1020 * allocation.
f720e3ba 1021 * addnblocks - number of blocks to add to the allocation.
1da177e4
LT
1022 *
1023 * RETURN VALUES:
f720e3ba
DK
1024 * 0 - success
1025 * -ENOSPC - insufficient disk resources
1026 * -EIO - i/o error
1da177e4
LT
1027 */
1028static int dbExtend(struct inode *ip, s64 blkno, s64 nblocks, s64 addnblocks)
1029{
1030 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
1031 s64 lblkno, lastblkno, extblkno;
1032 uint rel_block;
1033 struct metapage *mp;
1034 struct dmap *dp;
1035 int rc;
1036 struct inode *ipbmap = sbi->ipbmap;
1037 struct bmap *bmp;
1038
1039 /*
1040 * We don't want a non-aligned extent to cross a page boundary
1041 */
1042 if (((rel_block = blkno & (sbi->nbperpage - 1))) &&
1043 (rel_block + nblocks + addnblocks > sbi->nbperpage))
1044 return -ENOSPC;
1045
1046 /* get the last block of the current allocation */
1047 lastblkno = blkno + nblocks - 1;
1048
1049 /* determine the block number of the block following
1050 * the existing allocation.
1051 */
1052 extblkno = lastblkno + 1;
1053
82d5b9a7 1054 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
1da177e4
LT
1055
1056 /* better be within the file system */
1057 bmp = sbi->bmap;
1058 if (lastblkno < 0 || lastblkno >= bmp->db_mapsize) {
1059 IREAD_UNLOCK(ipbmap);
1060 jfs_error(ip->i_sb,
1061 "dbExtend: the block is outside the filesystem");
1062 return -EIO;
1063 }
1064
1065 /* we'll attempt to extend the current allocation in place by
1066 * allocating the additional blocks as the blocks immediately
1067 * following the current allocation. we only try to extend the
1068 * current allocation in place if the number of additional blocks
1069 * can fit into a dmap, the last block of the current allocation
1070 * is not the last block of the file system, and the start of the
1071 * inplace extension is not on an allocation group boundary.
1072 */
1073 if (addnblocks > BPERDMAP || extblkno >= bmp->db_mapsize ||
1074 (extblkno & (bmp->db_agsize - 1)) == 0) {
1075 IREAD_UNLOCK(ipbmap);
1076 return -ENOSPC;
1077 }
1078
1079 /* get the buffer for the dmap containing the first block
1080 * of the extension.
1081 */
1082 lblkno = BLKTODMAP(extblkno, bmp->db_l2nbperpage);
1083 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
1084 if (mp == NULL) {
1085 IREAD_UNLOCK(ipbmap);
1086 return -EIO;
1087 }
1088
1da177e4
LT
1089 dp = (struct dmap *) mp->data;
1090
1091 /* try to allocate the blocks immediately following the
1092 * current allocation.
1093 */
1094 rc = dbAllocNext(bmp, dp, extblkno, (int) addnblocks);
1095
1096 IREAD_UNLOCK(ipbmap);
1097
1098 /* were we successful ? */
b38a3ab3 1099 if (rc == 0)
1da177e4 1100 write_metapage(mp);
b38a3ab3 1101 else
1da177e4
LT
1102 /* we were not successful */
1103 release_metapage(mp);
1104
1da177e4
LT
1105 return (rc);
1106}
1107
1108
1109/*
1110 * NAME: dbAllocNext()
1111 *
f720e3ba 1112 * FUNCTION: attempt to allocate the blocks of the specified block
1da177e4
LT
1113 * range within a dmap.
1114 *
1115 * PARAMETERS:
f720e3ba
DK
1116 * bmp - pointer to bmap descriptor
1117 * dp - pointer to dmap.
1118 * blkno - starting block number of the range.
1119 * nblocks - number of contiguous free blocks of the range.
1da177e4
LT
1120 *
1121 * RETURN VALUES:
f720e3ba
DK
1122 * 0 - success
1123 * -ENOSPC - insufficient disk resources
1124 * -EIO - i/o error
1da177e4
LT
1125 *
1126 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1127 */
1128static int dbAllocNext(struct bmap * bmp, struct dmap * dp, s64 blkno,
1129 int nblocks)
1130{
1131 int dbitno, word, rembits, nb, nwords, wbitno, nw;
1132 int l2size;
1133 s8 *leaf;
1134 u32 mask;
1135
1136 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1137 jfs_error(bmp->db_ipbmap->i_sb,
1138 "dbAllocNext: Corrupt dmap page");
1139 return -EIO;
1140 }
1141
1142 /* pick up a pointer to the leaves of the dmap tree.
1143 */
1144 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
1145
1146 /* determine the bit number and word within the dmap of the
1147 * starting block.
1148 */
1149 dbitno = blkno & (BPERDMAP - 1);
1150 word = dbitno >> L2DBWORD;
1151
1152 /* check if the specified block range is contained within
1153 * this dmap.
1154 */
1155 if (dbitno + nblocks > BPERDMAP)
1156 return -ENOSPC;
1157
1158 /* check if the starting leaf indicates that anything
1159 * is free.
1160 */
1161 if (leaf[word] == NOFREE)
1162 return -ENOSPC;
1163
1164 /* check the dmaps words corresponding to block range to see
1165 * if the block range is free. not all bits of the first and
1166 * last words may be contained within the block range. if this
1167 * is the case, we'll work against those words (i.e. partial first
1168 * and/or last) on an individual basis (a single pass) and examine
1169 * the actual bits to determine if they are free. a single pass
1170 * will be used for all dmap words fully contained within the
1171 * specified range. within this pass, the leaves of the dmap
1172 * tree will be examined to determine if the blocks are free. a
1173 * single leaf may describe the free space of multiple dmap
1174 * words, so we may visit only a subset of the actual leaves
1175 * corresponding to the dmap words of the block range.
1176 */
1177 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
1178 /* determine the bit number within the word and
1179 * the number of bits within the word.
1180 */
1181 wbitno = dbitno & (DBWORD - 1);
1182 nb = min(rembits, DBWORD - wbitno);
1183
1184 /* check if only part of the word is to be examined.
1185 */
1186 if (nb < DBWORD) {
1187 /* check if the bits are free.
1188 */
1189 mask = (ONES << (DBWORD - nb) >> wbitno);
1190 if ((mask & ~le32_to_cpu(dp->wmap[word])) != mask)
1191 return -ENOSPC;
1192
1193 word += 1;
1194 } else {
1195 /* one or more dmap words are fully contained
1196 * within the block range. determine how many
1197 * words and how many bits.
1198 */
1199 nwords = rembits >> L2DBWORD;
1200 nb = nwords << L2DBWORD;
1201
1202 /* now examine the appropriate leaves to determine
1203 * if the blocks are free.
1204 */
1205 while (nwords > 0) {
1206 /* does the leaf describe any free space ?
1207 */
1208 if (leaf[word] < BUDMIN)
1209 return -ENOSPC;
1210
1211 /* determine the l2 number of bits provided
1212 * by this leaf.
1213 */
1214 l2size =
1215 min((int)leaf[word], NLSTOL2BSZ(nwords));
1216
1217 /* determine how many words were handled.
1218 */
1219 nw = BUDSIZE(l2size, BUDMIN);
1220
1221 nwords -= nw;
1222 word += nw;
1223 }
1224 }
1225 }
1226
1227 /* allocate the blocks.
1228 */
1229 return (dbAllocDmap(bmp, dp, blkno, nblocks));
1230}
1231
1232
1233/*
1234 * NAME: dbAllocNear()
1235 *
f720e3ba 1236 * FUNCTION: attempt to allocate a number of contiguous free blocks near
1da177e4
LT
1237 * a specified block (hint) within a dmap.
1238 *
1239 * starting with the dmap leaf that covers the hint, we'll
1240 * check the next four contiguous leaves for sufficient free
1241 * space. if sufficient free space is found, we'll allocate
1242 * the desired free space.
1243 *
1244 * PARAMETERS:
f720e3ba
DK
1245 * bmp - pointer to bmap descriptor
1246 * dp - pointer to dmap.
1247 * blkno - block number to allocate near.
1248 * nblocks - actual number of contiguous free blocks desired.
1249 * l2nb - log2 number of contiguous free blocks desired.
1250 * results - on successful return, set to the starting block number
1da177e4
LT
1251 * of the newly allocated range.
1252 *
1253 * RETURN VALUES:
f720e3ba
DK
1254 * 0 - success
1255 * -ENOSPC - insufficient disk resources
1256 * -EIO - i/o error
1da177e4
LT
1257 *
1258 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
1259 */
1260static int
1261dbAllocNear(struct bmap * bmp,
1262 struct dmap * dp, s64 blkno, int nblocks, int l2nb, s64 * results)
1263{
1264 int word, lword, rc;
1265 s8 *leaf;
1266
1267 if (dp->tree.leafidx != cpu_to_le32(LEAFIND)) {
1268 jfs_error(bmp->db_ipbmap->i_sb,
1269 "dbAllocNear: Corrupt dmap page");
1270 return -EIO;
1271 }
1272
1273 leaf = dp->tree.stree + le32_to_cpu(dp->tree.leafidx);
1274
1275 /* determine the word within the dmap that holds the hint
1276 * (i.e. blkno). also, determine the last word in the dmap
1277 * that we'll include in our examination.
1278 */
1279 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
1280 lword = min(word + 4, LPERDMAP);
1281
1282 /* examine the leaves for sufficient free space.
1283 */
1284 for (; word < lword; word++) {
1285 /* does the leaf describe sufficient free space ?
1286 */
1287 if (leaf[word] < l2nb)
1288 continue;
1289
1290 /* determine the block number within the file system
1291 * of the first block described by this dmap word.
1292 */
1293 blkno = le64_to_cpu(dp->start) + (word << L2DBWORD);
1294
1295 /* if not all bits of the dmap word are free, get the
1296 * starting bit number within the dmap word of the required
1297 * string of free bits and adjust the block number with the
1298 * value.
1299 */
1300 if (leaf[word] < BUDMIN)
1301 blkno +=
1302 dbFindBits(le32_to_cpu(dp->wmap[word]), l2nb);
1303
1304 /* allocate the blocks.
1305 */
1306 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
1307 *results = blkno;
1308
1309 return (rc);
1310 }
1311
1312 return -ENOSPC;
1313}
1314
1315
1316/*
1317 * NAME: dbAllocAG()
1318 *
f720e3ba 1319 * FUNCTION: attempt to allocate the specified number of contiguous
1da177e4
LT
1320 * free blocks within the specified allocation group.
1321 *
1322 * unless the allocation group size is equal to the number
1323 * of blocks per dmap, the dmap control pages will be used to
1324 * find the required free space, if available. we start the
1325 * search at the highest dmap control page level which
1326 * distinctly describes the allocation group's free space
1327 * (i.e. the highest level at which the allocation group's
1328 * free space is not mixed in with that of any other group).
1329 * in addition, we start the search within this level at a
1330 * height of the dmapctl dmtree at which the nodes distinctly
1331 * describe the allocation group's free space. at this height,
1332 * the allocation group's free space may be represented by 1
1333 * or two sub-trees, depending on the allocation group size.
1334 * we search the top nodes of these subtrees left to right for
1335 * sufficient free space. if sufficient free space is found,
63f83c9f 1336 * the subtree is searched to find the leftmost leaf that
1da177e4
LT
1337 * has free space. once we have made it to the leaf, we
1338 * move the search to the next lower level dmap control page
1339 * corresponding to this leaf. we continue down the dmap control
1340 * pages until we find the dmap that contains or starts the
1341 * sufficient free space and we allocate at this dmap.
1342 *
1343 * if the allocation group size is equal to the dmap size,
1344 * we'll start at the dmap corresponding to the allocation
1345 * group and attempt the allocation at this level.
1346 *
1347 * the dmap control page search is also not performed if the
1348 * allocation group is completely free and we go to the first
1349 * dmap of the allocation group to do the allocation. this is
1350 * done because the allocation group may be part (not the first
1351 * part) of a larger binary buddy system, causing the dmap
1352 * control pages to indicate no free space (NOFREE) within
1353 * the allocation group.
1354 *
1355 * PARAMETERS:
f720e3ba 1356 * bmp - pointer to bmap descriptor
1da177e4 1357 * agno - allocation group number.
f720e3ba
DK
1358 * nblocks - actual number of contiguous free blocks desired.
1359 * l2nb - log2 number of contiguous free blocks desired.
1360 * results - on successful return, set to the starting block number
1da177e4
LT
1361 * of the newly allocated range.
1362 *
1363 * RETURN VALUES:
f720e3ba
DK
1364 * 0 - success
1365 * -ENOSPC - insufficient disk resources
1366 * -EIO - i/o error
1da177e4
LT
1367 *
1368 * note: IWRITE_LOCK(ipmap) held on entry/exit;
1369 */
1370static int
1371dbAllocAG(struct bmap * bmp, int agno, s64 nblocks, int l2nb, s64 * results)
1372{
1373 struct metapage *mp;
1374 struct dmapctl *dcp;
1375 int rc, ti, i, k, m, n, agperlev;
1376 s64 blkno, lblkno;
1377 int budmin;
1378
1379 /* allocation request should not be for more than the
1380 * allocation group size.
1381 */
1382 if (l2nb > bmp->db_agl2size) {
1383 jfs_error(bmp->db_ipbmap->i_sb,
1384 "dbAllocAG: allocation request is larger than the "
1385 "allocation group size");
1386 return -EIO;
1387 }
1388
1389 /* determine the starting block number of the allocation
1390 * group.
1391 */
1392 blkno = (s64) agno << bmp->db_agl2size;
1393
1394 /* check if the allocation group size is the minimum allocation
1395 * group size or if the allocation group is completely free. if
1396 * the allocation group size is the minimum size of BPERDMAP (i.e.
1397 * 1 dmap), there is no need to search the dmap control page (below)
1398 * that fully describes the allocation group since the allocation
1399 * group is already fully described by a dmap. in this case, we
1400 * just call dbAllocCtl() to search the dmap tree and allocate the
63f83c9f 1401 * required space if available.
1da177e4
LT
1402 *
1403 * if the allocation group is completely free, dbAllocCtl() is
1404 * also called to allocate the required space. this is done for
1405 * two reasons. first, it makes no sense searching the dmap control
1406 * pages for free space when we know that free space exists. second,
1407 * the dmap control pages may indicate that the allocation group
1408 * has no free space if the allocation group is part (not the first
1409 * part) of a larger binary buddy system.
1410 */
1411 if (bmp->db_agsize == BPERDMAP
1412 || bmp->db_agfree[agno] == bmp->db_agsize) {
1413 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1414 if ((rc == -ENOSPC) &&
1415 (bmp->db_agfree[agno] == bmp->db_agsize)) {
1416 printk(KERN_ERR "blkno = %Lx, blocks = %Lx\n",
1417 (unsigned long long) blkno,
1418 (unsigned long long) nblocks);
1419 jfs_error(bmp->db_ipbmap->i_sb,
1420 "dbAllocAG: dbAllocCtl failed in free AG");
1421 }
1422 return (rc);
1423 }
1424
1425 /* the buffer for the dmap control page that fully describes the
1426 * allocation group.
1427 */
1428 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, bmp->db_aglevel);
1429 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1430 if (mp == NULL)
1431 return -EIO;
1432 dcp = (struct dmapctl *) mp->data;
1433 budmin = dcp->budmin;
1434
1435 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1436 jfs_error(bmp->db_ipbmap->i_sb,
1437 "dbAllocAG: Corrupt dmapctl page");
1438 release_metapage(mp);
1439 return -EIO;
1440 }
1441
1442 /* search the subtree(s) of the dmap control page that describes
1443 * the allocation group, looking for sufficient free space. to begin,
1444 * determine how many allocation groups are represented in a dmap
1445 * control page at the control page level (i.e. L0, L1, L2) that
1446 * fully describes an allocation group. next, determine the starting
1447 * tree index of this allocation group within the control page.
1448 */
1449 agperlev =
d7eecb48 1450 (1 << (L2LPERCTL - (bmp->db_agheight << 1))) / bmp->db_agwidth;
1da177e4
LT
1451 ti = bmp->db_agstart + bmp->db_agwidth * (agno & (agperlev - 1));
1452
63f83c9f 1453 /* dmap control page trees fan-out by 4 and a single allocation
1da177e4
LT
1454 * group may be described by 1 or 2 subtrees within the ag level
1455 * dmap control page, depending upon the ag size. examine the ag's
1456 * subtrees for sufficient free space, starting with the leftmost
1457 * subtree.
1458 */
1459 for (i = 0; i < bmp->db_agwidth; i++, ti++) {
1460 /* is there sufficient free space ?
1461 */
1462 if (l2nb > dcp->stree[ti])
1463 continue;
1464
1465 /* sufficient free space found in a subtree. now search down
1466 * the subtree to find the leftmost leaf that describes this
1467 * free space.
1468 */
d7eecb48 1469 for (k = bmp->db_agheight; k > 0; k--) {
1da177e4
LT
1470 for (n = 0, m = (ti << 2) + 1; n < 4; n++) {
1471 if (l2nb <= dcp->stree[m + n]) {
1472 ti = m + n;
1473 break;
1474 }
1475 }
1476 if (n == 4) {
1477 jfs_error(bmp->db_ipbmap->i_sb,
1478 "dbAllocAG: failed descending stree");
1479 release_metapage(mp);
1480 return -EIO;
1481 }
1482 }
1483
1484 /* determine the block number within the file system
1485 * that corresponds to this leaf.
1486 */
1487 if (bmp->db_aglevel == 2)
1488 blkno = 0;
1489 else if (bmp->db_aglevel == 1)
1490 blkno &= ~(MAXL1SIZE - 1);
1491 else /* bmp->db_aglevel == 0 */
1492 blkno &= ~(MAXL0SIZE - 1);
1493
1494 blkno +=
1495 ((s64) (ti - le32_to_cpu(dcp->leafidx))) << budmin;
1496
1497 /* release the buffer in preparation for going down
1498 * the next level of dmap control pages.
1499 */
1500 release_metapage(mp);
1501
1502 /* check if we need to continue to search down the lower
1503 * level dmap control pages. we need to if the number of
1504 * blocks required is less than maximum number of blocks
1505 * described at the next lower level.
1506 */
1507 if (l2nb < budmin) {
1508
1509 /* search the lower level dmap control pages to get
59c51591 1510 * the starting block number of the dmap that
1da177e4
LT
1511 * contains or starts off the free space.
1512 */
1513 if ((rc =
1514 dbFindCtl(bmp, l2nb, bmp->db_aglevel - 1,
1515 &blkno))) {
1516 if (rc == -ENOSPC) {
1517 jfs_error(bmp->db_ipbmap->i_sb,
1518 "dbAllocAG: control page "
1519 "inconsistent");
1520 return -EIO;
1521 }
1522 return (rc);
1523 }
1524 }
1525
1526 /* allocate the blocks.
1527 */
1528 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1529 if (rc == -ENOSPC) {
1530 jfs_error(bmp->db_ipbmap->i_sb,
1531 "dbAllocAG: unable to allocate blocks");
1532 rc = -EIO;
1533 }
1534 return (rc);
1535 }
1536
1537 /* no space in the allocation group. release the buffer and
1538 * return -ENOSPC.
1539 */
1540 release_metapage(mp);
1541
1542 return -ENOSPC;
1543}
1544
1545
1546/*
1547 * NAME: dbAllocAny()
1548 *
f720e3ba 1549 * FUNCTION: attempt to allocate the specified number of contiguous
1da177e4
LT
1550 * free blocks anywhere in the file system.
1551 *
1552 * dbAllocAny() attempts to find the sufficient free space by
1553 * searching down the dmap control pages, starting with the
1554 * highest level (i.e. L0, L1, L2) control page. if free space
1555 * large enough to satisfy the desired free space is found, the
1556 * desired free space is allocated.
1557 *
1558 * PARAMETERS:
f720e3ba
DK
1559 * bmp - pointer to bmap descriptor
1560 * nblocks - actual number of contiguous free blocks desired.
1561 * l2nb - log2 number of contiguous free blocks desired.
1562 * results - on successful return, set to the starting block number
1da177e4
LT
1563 * of the newly allocated range.
1564 *
1565 * RETURN VALUES:
f720e3ba
DK
1566 * 0 - success
1567 * -ENOSPC - insufficient disk resources
1568 * -EIO - i/o error
1da177e4
LT
1569 *
1570 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1571 */
1572static int dbAllocAny(struct bmap * bmp, s64 nblocks, int l2nb, s64 * results)
1573{
1574 int rc;
1575 s64 blkno = 0;
1576
1577 /* starting with the top level dmap control page, search
1578 * down the dmap control levels for sufficient free space.
1579 * if free space is found, dbFindCtl() returns the starting
1580 * block number of the dmap that contains or starts off the
1581 * range of free space.
1582 */
1583 if ((rc = dbFindCtl(bmp, l2nb, bmp->db_maxlevel, &blkno)))
1584 return (rc);
1585
1586 /* allocate the blocks.
1587 */
1588 rc = dbAllocCtl(bmp, nblocks, l2nb, blkno, results);
1589 if (rc == -ENOSPC) {
1590 jfs_error(bmp->db_ipbmap->i_sb,
1591 "dbAllocAny: unable to allocate blocks");
1592 return -EIO;
1593 }
1594 return (rc);
1595}
1596
1597
b40c2e66
TR
1598/*
1599 * NAME: dbDiscardAG()
1600 *
1601 * FUNCTION: attempt to discard (TRIM) all free blocks of specific AG
1602 *
1603 * algorithm:
1604 * 1) allocate blocks, as large as possible and save them
1605 * while holding IWRITE_LOCK on ipbmap
1606 * 2) trim all these saved block/length values
1607 * 3) mark the blocks free again
1608 *
1609 * benefit:
1610 * - we work only on one ag at some time, minimizing how long we
1611 * need to lock ipbmap
1612 * - reading / writing the fs is possible most time, even on
1613 * trimming
1614 *
1615 * downside:
1616 * - we write two times to the dmapctl and dmap pages
1617 * - but for me, this seems the best way, better ideas?
1618 * /TR 2012
1619 *
1620 * PARAMETERS:
1621 * ip - pointer to in-core inode
1622 * agno - ag to trim
1623 * minlen - minimum value of contiguous blocks
1624 *
1625 * RETURN VALUES:
1626 * s64 - actual number of blocks trimmed
1627 */
1628s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen)
1629{
1630 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
1631 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
1632 s64 nblocks, blkno;
1633 u64 trimmed = 0;
1634 int rc, l2nb;
1635 struct super_block *sb = ipbmap->i_sb;
1636
1637 struct range2trim {
1638 u64 blkno;
1639 u64 nblocks;
1640 } *totrim, *tt;
1641
1642 /* max blkno / nblocks pairs to trim */
1643 int count = 0, range_cnt;
1644
1645 /* prevent others from writing new stuff here, while trimming */
1646 IWRITE_LOCK(ipbmap, RDWRLOCK_DMAP);
1647
1648 nblocks = bmp->db_agfree[agno];
1649 range_cnt = nblocks;
1650 do_div(range_cnt, (int)minlen);
1651 range_cnt = min(range_cnt + 1, 32 * 1024);
1652 totrim = kmalloc(sizeof(struct range2trim) * range_cnt, GFP_NOFS);
1653 if (totrim == NULL) {
1654 jfs_error(bmp->db_ipbmap->i_sb,
1655 "dbDiscardAG: no memory for trim array");
1656 IWRITE_UNLOCK(ipbmap);
1657 return 0;
1658 }
1659
1660 tt = totrim;
1661 while (nblocks >= minlen) {
1662 l2nb = BLKSTOL2(nblocks);
1663
1664 /* 0 = okay, -EIO = fatal, -ENOSPC -> try smaller block */
1665 rc = dbAllocAG(bmp, agno, nblocks, l2nb, &blkno);
1666 if (rc == 0) {
1667 tt->blkno = blkno;
1668 tt->nblocks = nblocks;
1669 tt++; count++;
1670
1671 /* the whole ag is free, trim now */
1672 if (bmp->db_agfree[agno] == 0)
1673 break;
1674
1675 /* give a hint for the next while */
1676 nblocks = bmp->db_agfree[agno];
1677 continue;
1678 } else if (rc == -ENOSPC) {
1679 /* search for next smaller log2 block */
1680 l2nb = BLKSTOL2(nblocks) - 1;
1681 nblocks = 1 << l2nb;
1682 } else {
1683 /* Trim any already allocated blocks */
1684 jfs_error(bmp->db_ipbmap->i_sb,
1685 "dbDiscardAG: -EIO");
1686 break;
1687 }
1688
1689 /* check, if our trim array is full */
1690 if (unlikely(count >= range_cnt - 1))
1691 break;
1692 }
1693 IWRITE_UNLOCK(ipbmap);
1694
1695 tt->nblocks = 0; /* mark the current end */
1696 for (tt = totrim; tt->nblocks != 0; tt++) {
1697 /* when mounted with online discard, dbFree() will
1698 * call jfs_issue_discard() itself */
1699 if (!(JFS_SBI(sb)->flag & JFS_DISCARD))
1700 jfs_issue_discard(ip, tt->blkno, tt->nblocks);
1701 dbFree(ip, tt->blkno, tt->nblocks);
1702 trimmed += tt->nblocks;
1703 }
1704 kfree(totrim);
1705
1706 return trimmed;
1707}
1708
1da177e4
LT
1709/*
1710 * NAME: dbFindCtl()
1711 *
f720e3ba 1712 * FUNCTION: starting at a specified dmap control page level and block
1da177e4 1713 * number, search down the dmap control levels for a range of
f720e3ba 1714 * contiguous free blocks large enough to satisfy an allocation
1da177e4
LT
1715 * request for the specified number of free blocks.
1716 *
1717 * if sufficient contiguous free blocks are found, this routine
1718 * returns the starting block number within a dmap page that
1719 * contains or starts a range of contiqious free blocks that
1720 * is sufficient in size.
1721 *
1722 * PARAMETERS:
f720e3ba
DK
1723 * bmp - pointer to bmap descriptor
1724 * level - starting dmap control page level.
1725 * l2nb - log2 number of contiguous free blocks desired.
1726 * *blkno - on entry, starting block number for conducting the search.
1da177e4
LT
1727 * on successful return, the first block within a dmap page
1728 * that contains or starts a range of contiguous free blocks.
1729 *
1730 * RETURN VALUES:
f720e3ba
DK
1731 * 0 - success
1732 * -ENOSPC - insufficient disk resources
1733 * -EIO - i/o error
1da177e4
LT
1734 *
1735 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1736 */
1737static int dbFindCtl(struct bmap * bmp, int l2nb, int level, s64 * blkno)
1738{
1739 int rc, leafidx, lev;
1740 s64 b, lblkno;
1741 struct dmapctl *dcp;
1742 int budmin;
1743 struct metapage *mp;
1744
1745 /* starting at the specified dmap control page level and block
1746 * number, search down the dmap control levels for the starting
63f83c9f 1747 * block number of a dmap page that contains or starts off
1da177e4
LT
1748 * sufficient free blocks.
1749 */
1750 for (lev = level, b = *blkno; lev >= 0; lev--) {
1751 /* get the buffer of the dmap control page for the block
1752 * number and level (i.e. L0, L1, L2).
1753 */
1754 lblkno = BLKTOCTL(b, bmp->db_l2nbperpage, lev);
1755 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1756 if (mp == NULL)
1757 return -EIO;
1758 dcp = (struct dmapctl *) mp->data;
1759 budmin = dcp->budmin;
1760
1761 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
1762 jfs_error(bmp->db_ipbmap->i_sb,
1763 "dbFindCtl: Corrupt dmapctl page");
1764 release_metapage(mp);
1765 return -EIO;
1766 }
1767
1768 /* search the tree within the dmap control page for
25985edc 1769 * sufficient free space. if sufficient free space is found,
1da177e4
LT
1770 * dbFindLeaf() returns the index of the leaf at which
1771 * free space was found.
1772 */
1773 rc = dbFindLeaf((dmtree_t *) dcp, l2nb, &leafidx);
1774
1775 /* release the buffer.
1776 */
1777 release_metapage(mp);
1778
1779 /* space found ?
1780 */
1781 if (rc) {
1782 if (lev != level) {
1783 jfs_error(bmp->db_ipbmap->i_sb,
1784 "dbFindCtl: dmap inconsistent");
1785 return -EIO;
1786 }
1787 return -ENOSPC;
1788 }
1789
1790 /* adjust the block number to reflect the location within
63f83c9f 1791 * the dmap control page (i.e. the leaf) at which free
1da177e4
LT
1792 * space was found.
1793 */
1794 b += (((s64) leafidx) << budmin);
1795
1796 /* we stop the search at this dmap control page level if
1797 * the number of blocks required is greater than or equal
1798 * to the maximum number of blocks described at the next
1799 * (lower) level.
1800 */
1801 if (l2nb >= budmin)
1802 break;
1803 }
1804
1805 *blkno = b;
1806 return (0);
1807}
1808
1809
1810/*
1811 * NAME: dbAllocCtl()
1812 *
f720e3ba 1813 * FUNCTION: attempt to allocate a specified number of contiguous
63f83c9f
DK
1814 * blocks starting within a specific dmap.
1815 *
1da177e4
LT
1816 * this routine is called by higher level routines that search
1817 * the dmap control pages above the actual dmaps for contiguous
1818 * free space. the result of successful searches by these
63f83c9f 1819 * routines are the starting block numbers within dmaps, with
1da177e4
LT
1820 * the dmaps themselves containing the desired contiguous free
1821 * space or starting a contiguous free space of desired size
1822 * that is made up of the blocks of one or more dmaps. these
1823 * calls should not fail due to insufficent resources.
1824 *
1825 * this routine is called in some cases where it is not known
1826 * whether it will fail due to insufficient resources. more
1827 * specifically, this occurs when allocating from an allocation
1828 * group whose size is equal to the number of blocks per dmap.
1829 * in this case, the dmap control pages are not examined prior
1830 * to calling this routine (to save pathlength) and the call
1831 * might fail.
1832 *
1833 * for a request size that fits within a dmap, this routine relies
1834 * upon the dmap's dmtree to find the requested contiguous free
1835 * space. for request sizes that are larger than a dmap, the
1836 * requested free space will start at the first block of the
1837 * first dmap (i.e. blkno).
1838 *
1839 * PARAMETERS:
f720e3ba
DK
1840 * bmp - pointer to bmap descriptor
1841 * nblocks - actual number of contiguous free blocks to allocate.
1842 * l2nb - log2 number of contiguous free blocks to allocate.
1843 * blkno - starting block number of the dmap to start the allocation
1da177e4 1844 * from.
f720e3ba 1845 * results - on successful return, set to the starting block number
1da177e4
LT
1846 * of the newly allocated range.
1847 *
1848 * RETURN VALUES:
f720e3ba
DK
1849 * 0 - success
1850 * -ENOSPC - insufficient disk resources
1851 * -EIO - i/o error
1da177e4
LT
1852 *
1853 * serialization: IWRITE_LOCK(ipbmap) held on entry/exit;
1854 */
1855static int
1856dbAllocCtl(struct bmap * bmp, s64 nblocks, int l2nb, s64 blkno, s64 * results)
1857{
1858 int rc, nb;
1859 s64 b, lblkno, n;
1860 struct metapage *mp;
1861 struct dmap *dp;
1862
1863 /* check if the allocation request is confined to a single dmap.
1864 */
1865 if (l2nb <= L2BPERDMAP) {
1866 /* get the buffer for the dmap.
1867 */
1868 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
1869 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1870 if (mp == NULL)
1871 return -EIO;
1872 dp = (struct dmap *) mp->data;
1873
1874 /* try to allocate the blocks.
1875 */
1876 rc = dbAllocDmapLev(bmp, dp, (int) nblocks, l2nb, results);
1877 if (rc == 0)
1878 mark_metapage_dirty(mp);
1879
1880 release_metapage(mp);
1881
1882 return (rc);
1883 }
1884
1885 /* allocation request involving multiple dmaps. it must start on
1886 * a dmap boundary.
1887 */
1888 assert((blkno & (BPERDMAP - 1)) == 0);
1889
1890 /* allocate the blocks dmap by dmap.
1891 */
1892 for (n = nblocks, b = blkno; n > 0; n -= nb, b += nb) {
1893 /* get the buffer for the dmap.
1894 */
1895 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1896 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1897 if (mp == NULL) {
1898 rc = -EIO;
1899 goto backout;
1900 }
1901 dp = (struct dmap *) mp->data;
1902
1903 /* the dmap better be all free.
1904 */
1905 if (dp->tree.stree[ROOT] != L2BPERDMAP) {
1906 release_metapage(mp);
1907 jfs_error(bmp->db_ipbmap->i_sb,
1908 "dbAllocCtl: the dmap is not all free");
1909 rc = -EIO;
1910 goto backout;
1911 }
1912
1913 /* determine how many blocks to allocate from this dmap.
1914 */
1915 nb = min(n, (s64)BPERDMAP);
1916
1917 /* allocate the blocks from the dmap.
1918 */
1919 if ((rc = dbAllocDmap(bmp, dp, b, nb))) {
1920 release_metapage(mp);
1921 goto backout;
1922 }
1923
1924 /* write the buffer.
1925 */
1926 write_metapage(mp);
1927 }
1928
1929 /* set the results (starting block number) and return.
1930 */
1931 *results = blkno;
1932 return (0);
1933
1934 /* something failed in handling an allocation request involving
1935 * multiple dmaps. we'll try to clean up by backing out any
1936 * allocation that has already happened for this request. if
1937 * we fail in backing out the allocation, we'll mark the file
1938 * system to indicate that blocks have been leaked.
1939 */
1940 backout:
1941
1942 /* try to backout the allocations dmap by dmap.
1943 */
1944 for (n = nblocks - n, b = blkno; n > 0;
1945 n -= BPERDMAP, b += BPERDMAP) {
1946 /* get the buffer for this dmap.
1947 */
1948 lblkno = BLKTODMAP(b, bmp->db_l2nbperpage);
1949 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
1950 if (mp == NULL) {
1951 /* could not back out. mark the file system
1952 * to indicate that we have leaked blocks.
1953 */
1954 jfs_error(bmp->db_ipbmap->i_sb,
1955 "dbAllocCtl: I/O Error: Block Leakage.");
1956 continue;
1957 }
1958 dp = (struct dmap *) mp->data;
1959
1960 /* free the blocks is this dmap.
1961 */
1962 if (dbFreeDmap(bmp, dp, b, BPERDMAP)) {
1963 /* could not back out. mark the file system
1964 * to indicate that we have leaked blocks.
1965 */
1966 release_metapage(mp);
1967 jfs_error(bmp->db_ipbmap->i_sb,
1968 "dbAllocCtl: Block Leakage.");
1969 continue;
1970 }
1971
1972 /* write the buffer.
1973 */
1974 write_metapage(mp);
1975 }
1976
1977 return (rc);
1978}
1979
1980
1981/*
1982 * NAME: dbAllocDmapLev()
1983 *
f720e3ba 1984 * FUNCTION: attempt to allocate a specified number of contiguous blocks
1da177e4 1985 * from a specified dmap.
63f83c9f 1986 *
1da177e4
LT
1987 * this routine checks if the contiguous blocks are available.
1988 * if so, nblocks of blocks are allocated; otherwise, ENOSPC is
1989 * returned.
1990 *
1991 * PARAMETERS:
f720e3ba
DK
1992 * mp - pointer to bmap descriptor
1993 * dp - pointer to dmap to attempt to allocate blocks from.
1994 * l2nb - log2 number of contiguous block desired.
1995 * nblocks - actual number of contiguous block desired.
1996 * results - on successful return, set to the starting block number
1da177e4
LT
1997 * of the newly allocated range.
1998 *
1999 * RETURN VALUES:
f720e3ba
DK
2000 * 0 - success
2001 * -ENOSPC - insufficient disk resources
2002 * -EIO - i/o error
1da177e4 2003 *
63f83c9f 2004 * serialization: IREAD_LOCK(ipbmap), e.g., from dbAlloc(), or
1da177e4
LT
2005 * IWRITE_LOCK(ipbmap), e.g., dbAllocCtl(), held on entry/exit;
2006 */
2007static int
2008dbAllocDmapLev(struct bmap * bmp,
2009 struct dmap * dp, int nblocks, int l2nb, s64 * results)
2010{
2011 s64 blkno;
2012 int leafidx, rc;
2013
2014 /* can't be more than a dmaps worth of blocks */
2015 assert(l2nb <= L2BPERDMAP);
2016
2017 /* search the tree within the dmap page for sufficient
2018 * free space. if sufficient free space is found, dbFindLeaf()
2019 * returns the index of the leaf at which free space was found.
2020 */
2021 if (dbFindLeaf((dmtree_t *) & dp->tree, l2nb, &leafidx))
2022 return -ENOSPC;
2023
2024 /* determine the block number within the file system corresponding
2025 * to the leaf at which free space was found.
2026 */
2027 blkno = le64_to_cpu(dp->start) + (leafidx << L2DBWORD);
2028
2029 /* if not all bits of the dmap word are free, get the starting
2030 * bit number within the dmap word of the required string of free
2031 * bits and adjust the block number with this value.
2032 */
2033 if (dp->tree.stree[leafidx + LEAFIND] < BUDMIN)
2034 blkno += dbFindBits(le32_to_cpu(dp->wmap[leafidx]), l2nb);
2035
2036 /* allocate the blocks */
2037 if ((rc = dbAllocDmap(bmp, dp, blkno, nblocks)) == 0)
2038 *results = blkno;
2039
2040 return (rc);
2041}
2042
2043
2044/*
2045 * NAME: dbAllocDmap()
2046 *
f720e3ba 2047 * FUNCTION: adjust the disk allocation map to reflect the allocation
1da177e4
LT
2048 * of a specified block range within a dmap.
2049 *
2050 * this routine allocates the specified blocks from the dmap
2051 * through a call to dbAllocBits(). if the allocation of the
2052 * block range causes the maximum string of free blocks within
2053 * the dmap to change (i.e. the value of the root of the dmap's
2054 * dmtree), this routine will cause this change to be reflected
2055 * up through the appropriate levels of the dmap control pages
2056 * by a call to dbAdjCtl() for the L0 dmap control page that
2057 * covers this dmap.
2058 *
2059 * PARAMETERS:
f720e3ba
DK
2060 * bmp - pointer to bmap descriptor
2061 * dp - pointer to dmap to allocate the block range from.
2062 * blkno - starting block number of the block to be allocated.
2063 * nblocks - number of blocks to be allocated.
1da177e4
LT
2064 *
2065 * RETURN VALUES:
f720e3ba
DK
2066 * 0 - success
2067 * -EIO - i/o error
1da177e4
LT
2068 *
2069 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2070 */
2071static int dbAllocDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2072 int nblocks)
2073{
2074 s8 oldroot;
2075 int rc;
2076
2077 /* save the current value of the root (i.e. maximum free string)
2078 * of the dmap tree.
2079 */
2080 oldroot = dp->tree.stree[ROOT];
2081
2082 /* allocate the specified (blocks) bits */
2083 dbAllocBits(bmp, dp, blkno, nblocks);
2084
2085 /* if the root has not changed, done. */
2086 if (dp->tree.stree[ROOT] == oldroot)
2087 return (0);
2088
2089 /* root changed. bubble the change up to the dmap control pages.
2090 * if the adjustment of the upper level control pages fails,
2091 * backout the bit allocation (thus making everything consistent).
2092 */
2093 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 1, 0)))
2094 dbFreeBits(bmp, dp, blkno, nblocks);
2095
2096 return (rc);
2097}
2098
2099
2100/*
2101 * NAME: dbFreeDmap()
2102 *
f720e3ba 2103 * FUNCTION: adjust the disk allocation map to reflect the allocation
1da177e4
LT
2104 * of a specified block range within a dmap.
2105 *
2106 * this routine frees the specified blocks from the dmap through
2107 * a call to dbFreeBits(). if the deallocation of the block range
2108 * causes the maximum string of free blocks within the dmap to
2109 * change (i.e. the value of the root of the dmap's dmtree), this
2110 * routine will cause this change to be reflected up through the
f720e3ba 2111 * appropriate levels of the dmap control pages by a call to
1da177e4
LT
2112 * dbAdjCtl() for the L0 dmap control page that covers this dmap.
2113 *
2114 * PARAMETERS:
f720e3ba
DK
2115 * bmp - pointer to bmap descriptor
2116 * dp - pointer to dmap to free the block range from.
2117 * blkno - starting block number of the block to be freed.
2118 * nblocks - number of blocks to be freed.
1da177e4
LT
2119 *
2120 * RETURN VALUES:
f720e3ba
DK
2121 * 0 - success
2122 * -EIO - i/o error
1da177e4
LT
2123 *
2124 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2125 */
2126static int dbFreeDmap(struct bmap * bmp, struct dmap * dp, s64 blkno,
2127 int nblocks)
2128{
2129 s8 oldroot;
56d12549 2130 int rc = 0, word;
1da177e4
LT
2131
2132 /* save the current value of the root (i.e. maximum free string)
2133 * of the dmap tree.
2134 */
2135 oldroot = dp->tree.stree[ROOT];
2136
2137 /* free the specified (blocks) bits */
56d12549 2138 rc = dbFreeBits(bmp, dp, blkno, nblocks);
1da177e4 2139
56d12549
DK
2140 /* if error or the root has not changed, done. */
2141 if (rc || (dp->tree.stree[ROOT] == oldroot))
2142 return (rc);
1da177e4
LT
2143
2144 /* root changed. bubble the change up to the dmap control pages.
2145 * if the adjustment of the upper level control pages fails,
63f83c9f 2146 * backout the deallocation.
1da177e4
LT
2147 */
2148 if ((rc = dbAdjCtl(bmp, blkno, dp->tree.stree[ROOT], 0, 0))) {
2149 word = (blkno & (BPERDMAP - 1)) >> L2DBWORD;
2150
2151 /* as part of backing out the deallocation, we will have
2152 * to back split the dmap tree if the deallocation caused
2153 * the freed blocks to become part of a larger binary buddy
2154 * system.
2155 */
2156 if (dp->tree.stree[word] == NOFREE)
2157 dbBackSplit((dmtree_t *) & dp->tree, word);
2158
2159 dbAllocBits(bmp, dp, blkno, nblocks);
2160 }
2161
2162 return (rc);
2163}
2164
2165
2166/*
2167 * NAME: dbAllocBits()
2168 *
f720e3ba 2169 * FUNCTION: allocate a specified block range from a dmap.
1da177e4
LT
2170 *
2171 * this routine updates the dmap to reflect the working
2172 * state allocation of the specified block range. it directly
2173 * updates the bits of the working map and causes the adjustment
2174 * of the binary buddy system described by the dmap's dmtree
2175 * leaves to reflect the bits allocated. it also causes the
2176 * dmap's dmtree, as a whole, to reflect the allocated range.
2177 *
2178 * PARAMETERS:
f720e3ba
DK
2179 * bmp - pointer to bmap descriptor
2180 * dp - pointer to dmap to allocate bits from.
2181 * blkno - starting block number of the bits to be allocated.
2182 * nblocks - number of bits to be allocated.
1da177e4
LT
2183 *
2184 * RETURN VALUES: none
2185 *
2186 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2187 */
2188static void dbAllocBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
2189 int nblocks)
2190{
2191 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2192 dmtree_t *tp = (dmtree_t *) & dp->tree;
2193 int size;
2194 s8 *leaf;
2195
2196 /* pick up a pointer to the leaves of the dmap tree */
2197 leaf = dp->tree.stree + LEAFIND;
2198
2199 /* determine the bit number and word within the dmap of the
2200 * starting block.
2201 */
2202 dbitno = blkno & (BPERDMAP - 1);
2203 word = dbitno >> L2DBWORD;
2204
2205 /* block range better be within the dmap */
2206 assert(dbitno + nblocks <= BPERDMAP);
2207
2208 /* allocate the bits of the dmap's words corresponding to the block
2209 * range. not all bits of the first and last words may be contained
2210 * within the block range. if this is the case, we'll work against
2211 * those words (i.e. partial first and/or last) on an individual basis
2212 * (a single pass), allocating the bits of interest by hand and
2213 * updating the leaf corresponding to the dmap word. a single pass
2214 * will be used for all dmap words fully contained within the
2215 * specified range. within this pass, the bits of all fully contained
2216 * dmap words will be marked as free in a single shot and the leaves
2217 * will be updated. a single leaf may describe the free space of
2218 * multiple dmap words, so we may update only a subset of the actual
2219 * leaves corresponding to the dmap words of the block range.
2220 */
2221 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2222 /* determine the bit number within the word and
2223 * the number of bits within the word.
2224 */
2225 wbitno = dbitno & (DBWORD - 1);
2226 nb = min(rembits, DBWORD - wbitno);
2227
2228 /* check if only part of a word is to be allocated.
2229 */
2230 if (nb < DBWORD) {
2231 /* allocate (set to 1) the appropriate bits within
2232 * this dmap word.
2233 */
2234 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
2235 >> wbitno);
2236
2237 /* update the leaf for this dmap word. in addition
2238 * to setting the leaf value to the binary buddy max
2239 * of the updated dmap word, dbSplit() will split
2240 * the binary system of the leaves if need be.
2241 */
2242 dbSplit(tp, word, BUDMIN,
2243 dbMaxBud((u8 *) & dp->wmap[word]));
2244
2245 word += 1;
2246 } else {
2247 /* one or more dmap words are fully contained
2248 * within the block range. determine how many
2249 * words and allocate (set to 1) the bits of these
2250 * words.
2251 */
2252 nwords = rembits >> L2DBWORD;
2253 memset(&dp->wmap[word], (int) ONES, nwords * 4);
2254
2255 /* determine how many bits.
2256 */
2257 nb = nwords << L2DBWORD;
2258
2259 /* now update the appropriate leaves to reflect
2260 * the allocated words.
2261 */
2262 for (; nwords > 0; nwords -= nw) {
f720e3ba 2263 if (leaf[word] < BUDMIN) {
1da177e4
LT
2264 jfs_error(bmp->db_ipbmap->i_sb,
2265 "dbAllocBits: leaf page "
2266 "corrupt");
2267 break;
2268 }
2269
2270 /* determine what the leaf value should be
2271 * updated to as the minimum of the l2 number
2272 * of bits being allocated and the l2 number
2273 * of bits currently described by this leaf.
2274 */
2275 size = min((int)leaf[word], NLSTOL2BSZ(nwords));
2276
2277 /* update the leaf to reflect the allocation.
2278 * in addition to setting the leaf value to
2279 * NOFREE, dbSplit() will split the binary
2280 * system of the leaves to reflect the current
2281 * allocation (size).
2282 */
2283 dbSplit(tp, word, size, NOFREE);
2284
2285 /* get the number of dmap words handled */
2286 nw = BUDSIZE(size, BUDMIN);
2287 word += nw;
2288 }
2289 }
2290 }
2291
2292 /* update the free count for this dmap */
89145622 2293 le32_add_cpu(&dp->nfree, -nblocks);
1da177e4
LT
2294
2295 BMAP_LOCK(bmp);
2296
2297 /* if this allocation group is completely free,
2298 * update the maximum allocation group number if this allocation
2299 * group is the new max.
2300 */
2301 agno = blkno >> bmp->db_agl2size;
2302 if (agno > bmp->db_maxag)
2303 bmp->db_maxag = agno;
2304
2305 /* update the free count for the allocation group and map */
2306 bmp->db_agfree[agno] -= nblocks;
2307 bmp->db_nfree -= nblocks;
2308
2309 BMAP_UNLOCK(bmp);
2310}
2311
2312
2313/*
2314 * NAME: dbFreeBits()
2315 *
f720e3ba 2316 * FUNCTION: free a specified block range from a dmap.
1da177e4
LT
2317 *
2318 * this routine updates the dmap to reflect the working
2319 * state allocation of the specified block range. it directly
2320 * updates the bits of the working map and causes the adjustment
2321 * of the binary buddy system described by the dmap's dmtree
2322 * leaves to reflect the bits freed. it also causes the dmap's
2323 * dmtree, as a whole, to reflect the deallocated range.
2324 *
2325 * PARAMETERS:
f720e3ba
DK
2326 * bmp - pointer to bmap descriptor
2327 * dp - pointer to dmap to free bits from.
2328 * blkno - starting block number of the bits to be freed.
2329 * nblocks - number of bits to be freed.
1da177e4 2330 *
56d12549 2331 * RETURN VALUES: 0 for success
1da177e4
LT
2332 *
2333 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2334 */
56d12549 2335static int dbFreeBits(struct bmap * bmp, struct dmap * dp, s64 blkno,
1da177e4
LT
2336 int nblocks)
2337{
2338 int dbitno, word, rembits, nb, nwords, wbitno, nw, agno;
2339 dmtree_t *tp = (dmtree_t *) & dp->tree;
56d12549 2340 int rc = 0;
1da177e4
LT
2341 int size;
2342
2343 /* determine the bit number and word within the dmap of the
2344 * starting block.
2345 */
2346 dbitno = blkno & (BPERDMAP - 1);
2347 word = dbitno >> L2DBWORD;
2348
2349 /* block range better be within the dmap.
2350 */
2351 assert(dbitno + nblocks <= BPERDMAP);
2352
2353 /* free the bits of the dmaps words corresponding to the block range.
2354 * not all bits of the first and last words may be contained within
2355 * the block range. if this is the case, we'll work against those
2356 * words (i.e. partial first and/or last) on an individual basis
2357 * (a single pass), freeing the bits of interest by hand and updating
2358 * the leaf corresponding to the dmap word. a single pass will be used
63f83c9f 2359 * for all dmap words fully contained within the specified range.
1da177e4
LT
2360 * within this pass, the bits of all fully contained dmap words will
2361 * be marked as free in a single shot and the leaves will be updated. a
2362 * single leaf may describe the free space of multiple dmap words,
2363 * so we may update only a subset of the actual leaves corresponding
2364 * to the dmap words of the block range.
2365 *
2366 * dbJoin() is used to update leaf values and will join the binary
2367 * buddy system of the leaves if the new leaf values indicate this
2368 * should be done.
2369 */
2370 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
2371 /* determine the bit number within the word and
2372 * the number of bits within the word.
2373 */
2374 wbitno = dbitno & (DBWORD - 1);
2375 nb = min(rembits, DBWORD - wbitno);
2376
2377 /* check if only part of a word is to be freed.
2378 */
2379 if (nb < DBWORD) {
2380 /* free (zero) the appropriate bits within this
63f83c9f 2381 * dmap word.
1da177e4
LT
2382 */
2383 dp->wmap[word] &=
2384 cpu_to_le32(~(ONES << (DBWORD - nb)
2385 >> wbitno));
2386
2387 /* update the leaf for this dmap word.
2388 */
56d12549
DK
2389 rc = dbJoin(tp, word,
2390 dbMaxBud((u8 *) & dp->wmap[word]));
2391 if (rc)
2392 return rc;
1da177e4
LT
2393
2394 word += 1;
2395 } else {
2396 /* one or more dmap words are fully contained
2397 * within the block range. determine how many
2398 * words and free (zero) the bits of these words.
2399 */
2400 nwords = rembits >> L2DBWORD;
2401 memset(&dp->wmap[word], 0, nwords * 4);
2402
2403 /* determine how many bits.
2404 */
2405 nb = nwords << L2DBWORD;
2406
2407 /* now update the appropriate leaves to reflect
2408 * the freed words.
2409 */
2410 for (; nwords > 0; nwords -= nw) {
2411 /* determine what the leaf value should be
2412 * updated to as the minimum of the l2 number
2413 * of bits being freed and the l2 (max) number
2414 * of bits that can be described by this leaf.
2415 */
2416 size =
2417 min(LITOL2BSZ
2418 (word, L2LPERDMAP, BUDMIN),
2419 NLSTOL2BSZ(nwords));
2420
2421 /* update the leaf.
2422 */
56d12549
DK
2423 rc = dbJoin(tp, word, size);
2424 if (rc)
2425 return rc;
1da177e4
LT
2426
2427 /* get the number of dmap words handled.
2428 */
2429 nw = BUDSIZE(size, BUDMIN);
2430 word += nw;
2431 }
2432 }
2433 }
2434
2435 /* update the free count for this dmap.
2436 */
89145622 2437 le32_add_cpu(&dp->nfree, nblocks);
1da177e4
LT
2438
2439 BMAP_LOCK(bmp);
2440
63f83c9f 2441 /* update the free count for the allocation group and
1da177e4
LT
2442 * map.
2443 */
2444 agno = blkno >> bmp->db_agl2size;
2445 bmp->db_nfree += nblocks;
2446 bmp->db_agfree[agno] += nblocks;
2447
2448 /* check if this allocation group is not completely free and
2449 * if it is currently the maximum (rightmost) allocation group.
2450 * if so, establish the new maximum allocation group number by
2451 * searching left for the first allocation group with allocation.
2452 */
2453 if ((bmp->db_agfree[agno] == bmp->db_agsize && agno == bmp->db_maxag) ||
2454 (agno == bmp->db_numag - 1 &&
2455 bmp->db_agfree[agno] == (bmp-> db_mapsize & (BPERDMAP - 1)))) {
2456 while (bmp->db_maxag > 0) {
2457 bmp->db_maxag -= 1;
2458 if (bmp->db_agfree[bmp->db_maxag] !=
2459 bmp->db_agsize)
2460 break;
2461 }
2462
2463 /* re-establish the allocation group preference if the
2464 * current preference is right of the maximum allocation
2465 * group.
2466 */
2467 if (bmp->db_agpref > bmp->db_maxag)
2468 bmp->db_agpref = bmp->db_maxag;
2469 }
2470
2471 BMAP_UNLOCK(bmp);
56d12549
DK
2472
2473 return 0;
1da177e4
LT
2474}
2475
2476
2477/*
2478 * NAME: dbAdjCtl()
2479 *
2480 * FUNCTION: adjust a dmap control page at a specified level to reflect
2481 * the change in a lower level dmap or dmap control page's
2482 * maximum string of free blocks (i.e. a change in the root
2483 * of the lower level object's dmtree) due to the allocation
2484 * or deallocation of a range of blocks with a single dmap.
2485 *
2486 * on entry, this routine is provided with the new value of
2487 * the lower level dmap or dmap control page root and the
2488 * starting block number of the block range whose allocation
2489 * or deallocation resulted in the root change. this range
2490 * is respresented by a single leaf of the current dmapctl
2491 * and the leaf will be updated with this value, possibly
63f83c9f 2492 * causing a binary buddy system within the leaves to be
1da177e4
LT
2493 * split or joined. the update may also cause the dmapctl's
2494 * dmtree to be updated.
2495 *
2496 * if the adjustment of the dmap control page, itself, causes its
2497 * root to change, this change will be bubbled up to the next dmap
2498 * control level by a recursive call to this routine, specifying
2499 * the new root value and the next dmap control page level to
2500 * be adjusted.
2501 * PARAMETERS:
f720e3ba
DK
2502 * bmp - pointer to bmap descriptor
2503 * blkno - the first block of a block range within a dmap. it is
1da177e4
LT
2504 * the allocation or deallocation of this block range that
2505 * requires the dmap control page to be adjusted.
f720e3ba 2506 * newval - the new value of the lower level dmap or dmap control
1da177e4 2507 * page root.
f720e3ba
DK
2508 * alloc - 'true' if adjustment is due to an allocation.
2509 * level - current level of dmap control page (i.e. L0, L1, L2) to
1da177e4
LT
2510 * be adjusted.
2511 *
2512 * RETURN VALUES:
f720e3ba
DK
2513 * 0 - success
2514 * -EIO - i/o error
1da177e4
LT
2515 *
2516 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2517 */
2518static int
2519dbAdjCtl(struct bmap * bmp, s64 blkno, int newval, int alloc, int level)
2520{
2521 struct metapage *mp;
2522 s8 oldroot;
2523 int oldval;
2524 s64 lblkno;
2525 struct dmapctl *dcp;
2526 int rc, leafno, ti;
2527
2528 /* get the buffer for the dmap control page for the specified
2529 * block number and control page level.
2530 */
2531 lblkno = BLKTOCTL(blkno, bmp->db_l2nbperpage, level);
2532 mp = read_metapage(bmp->db_ipbmap, lblkno, PSIZE, 0);
2533 if (mp == NULL)
2534 return -EIO;
2535 dcp = (struct dmapctl *) mp->data;
2536
2537 if (dcp->leafidx != cpu_to_le32(CTLLEAFIND)) {
2538 jfs_error(bmp->db_ipbmap->i_sb,
2539 "dbAdjCtl: Corrupt dmapctl page");
2540 release_metapage(mp);
2541 return -EIO;
2542 }
2543
2544 /* determine the leaf number corresponding to the block and
2545 * the index within the dmap control tree.
2546 */
2547 leafno = BLKTOCTLLEAF(blkno, dcp->budmin);
2548 ti = leafno + le32_to_cpu(dcp->leafidx);
2549
2550 /* save the current leaf value and the current root level (i.e.
2551 * maximum l2 free string described by this dmapctl).
2552 */
2553 oldval = dcp->stree[ti];
2554 oldroot = dcp->stree[ROOT];
2555
2556 /* check if this is a control page update for an allocation.
2557 * if so, update the leaf to reflect the new leaf value using
88393161 2558 * dbSplit(); otherwise (deallocation), use dbJoin() to update
1da177e4
LT
2559 * the leaf with the new value. in addition to updating the
2560 * leaf, dbSplit() will also split the binary buddy system of
2561 * the leaves, if required, and bubble new values within the
2562 * dmapctl tree, if required. similarly, dbJoin() will join
2563 * the binary buddy system of leaves and bubble new values up
2564 * the dmapctl tree as required by the new leaf value.
2565 */
2566 if (alloc) {
2567 /* check if we are in the middle of a binary buddy
2568 * system. this happens when we are performing the
2569 * first allocation out of an allocation group that
2570 * is part (not the first part) of a larger binary
2571 * buddy system. if we are in the middle, back split
2572 * the system prior to calling dbSplit() which assumes
2573 * that it is at the front of a binary buddy system.
2574 */
2575 if (oldval == NOFREE) {
b6a47fd8
DK
2576 rc = dbBackSplit((dmtree_t *) dcp, leafno);
2577 if (rc)
2578 return rc;
1da177e4
LT
2579 oldval = dcp->stree[ti];
2580 }
2581 dbSplit((dmtree_t *) dcp, leafno, dcp->budmin, newval);
2582 } else {
56d12549
DK
2583 rc = dbJoin((dmtree_t *) dcp, leafno, newval);
2584 if (rc)
2585 return rc;
1da177e4
LT
2586 }
2587
2588 /* check if the root of the current dmap control page changed due
2589 * to the update and if the current dmap control page is not at
2590 * the current top level (i.e. L0, L1, L2) of the map. if so (i.e.
2591 * root changed and this is not the top level), call this routine
2592 * again (recursion) for the next higher level of the mapping to
2593 * reflect the change in root for the current dmap control page.
2594 */
2595 if (dcp->stree[ROOT] != oldroot) {
2596 /* are we below the top level of the map. if so,
2597 * bubble the root up to the next higher level.
2598 */
2599 if (level < bmp->db_maxlevel) {
2600 /* bubble up the new root of this dmap control page to
2601 * the next level.
2602 */
2603 if ((rc =
2604 dbAdjCtl(bmp, blkno, dcp->stree[ROOT], alloc,
2605 level + 1))) {
2606 /* something went wrong in bubbling up the new
2607 * root value, so backout the changes to the
2608 * current dmap control page.
2609 */
2610 if (alloc) {
2611 dbJoin((dmtree_t *) dcp, leafno,
2612 oldval);
2613 } else {
2614 /* the dbJoin() above might have
2615 * caused a larger binary buddy system
2616 * to form and we may now be in the
2617 * middle of it. if this is the case,
2618 * back split the buddies.
2619 */
2620 if (dcp->stree[ti] == NOFREE)
2621 dbBackSplit((dmtree_t *)
2622 dcp, leafno);
2623 dbSplit((dmtree_t *) dcp, leafno,
2624 dcp->budmin, oldval);
2625 }
2626
2627 /* release the buffer and return the error.
2628 */
2629 release_metapage(mp);
2630 return (rc);
2631 }
2632 } else {
2633 /* we're at the top level of the map. update
2634 * the bmap control page to reflect the size
2635 * of the maximum free buddy system.
2636 */
2637 assert(level == bmp->db_maxlevel);
2638 if (bmp->db_maxfreebud != oldroot) {
2639 jfs_error(bmp->db_ipbmap->i_sb,
2640 "dbAdjCtl: the maximum free buddy is "
2641 "not the old root");
2642 }
2643 bmp->db_maxfreebud = dcp->stree[ROOT];
2644 }
2645 }
2646
2647 /* write the buffer.
2648 */
2649 write_metapage(mp);
2650
2651 return (0);
2652}
2653
2654
2655/*
2656 * NAME: dbSplit()
2657 *
f720e3ba 2658 * FUNCTION: update the leaf of a dmtree with a new value, splitting
1da177e4
LT
2659 * the leaf from the binary buddy system of the dmtree's
2660 * leaves, as required.
2661 *
2662 * PARAMETERS:
f720e3ba
DK
2663 * tp - pointer to the tree containing the leaf.
2664 * leafno - the number of the leaf to be updated.
2665 * splitsz - the size the binary buddy system starting at the leaf
1da177e4 2666 * must be split to, specified as the log2 number of blocks.
f720e3ba 2667 * newval - the new value for the leaf.
1da177e4
LT
2668 *
2669 * RETURN VALUES: none
2670 *
2671 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2672 */
2673static void dbSplit(dmtree_t * tp, int leafno, int splitsz, int newval)
2674{
2675 int budsz;
2676 int cursz;
2677 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2678
2679 /* check if the leaf needs to be split.
2680 */
2681 if (leaf[leafno] > tp->dmt_budmin) {
2682 /* the split occurs by cutting the buddy system in half
2683 * at the specified leaf until we reach the specified
2684 * size. pick up the starting split size (current size
2685 * - 1 in l2) and the corresponding buddy size.
2686 */
2687 cursz = leaf[leafno] - 1;
2688 budsz = BUDSIZE(cursz, tp->dmt_budmin);
2689
2690 /* split until we reach the specified size.
2691 */
2692 while (cursz >= splitsz) {
2693 /* update the buddy's leaf with its new value.
2694 */
2695 dbAdjTree(tp, leafno ^ budsz, cursz);
2696
2697 /* on to the next size and buddy.
2698 */
2699 cursz -= 1;
2700 budsz >>= 1;
2701 }
2702 }
2703
63f83c9f 2704 /* adjust the dmap tree to reflect the specified leaf's new
1da177e4
LT
2705 * value.
2706 */
2707 dbAdjTree(tp, leafno, newval);
2708}
2709
2710
2711/*
2712 * NAME: dbBackSplit()
2713 *
f720e3ba 2714 * FUNCTION: back split the binary buddy system of dmtree leaves
1da177e4
LT
2715 * that hold a specified leaf until the specified leaf
2716 * starts its own binary buddy system.
2717 *
2718 * the allocators typically perform allocations at the start
2719 * of binary buddy systems and dbSplit() is used to accomplish
2720 * any required splits. in some cases, however, allocation
2721 * may occur in the middle of a binary system and requires a
2722 * back split, with the split proceeding out from the middle of
2723 * the system (less efficient) rather than the start of the
2724 * system (more efficient). the cases in which a back split
2725 * is required are rare and are limited to the first allocation
2726 * within an allocation group which is a part (not first part)
2727 * of a larger binary buddy system and a few exception cases
2728 * in which a previous join operation must be backed out.
2729 *
2730 * PARAMETERS:
f720e3ba
DK
2731 * tp - pointer to the tree containing the leaf.
2732 * leafno - the number of the leaf to be updated.
1da177e4
LT
2733 *
2734 * RETURN VALUES: none
2735 *
2736 * serialization: IREAD_LOCK(ipbmap) or IWRITE_LOCK(ipbmap) held on entry/exit;
2737 */
b6a47fd8 2738static int dbBackSplit(dmtree_t * tp, int leafno)
1da177e4
LT
2739{
2740 int budsz, bud, w, bsz, size;
2741 int cursz;
2742 s8 *leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2743
2744 /* leaf should be part (not first part) of a binary
2745 * buddy system.
2746 */
2747 assert(leaf[leafno] == NOFREE);
2748
2749 /* the back split is accomplished by iteratively finding the leaf
2750 * that starts the buddy system that contains the specified leaf and
2751 * splitting that system in two. this iteration continues until
63f83c9f 2752 * the specified leaf becomes the start of a buddy system.
1da177e4
LT
2753 *
2754 * determine maximum possible l2 size for the specified leaf.
2755 */
2756 size =
2757 LITOL2BSZ(leafno, le32_to_cpu(tp->dmt_l2nleafs),
2758 tp->dmt_budmin);
2759
2760 /* determine the number of leaves covered by this size. this
2761 * is the buddy size that we will start with as we search for
2762 * the buddy system that contains the specified leaf.
2763 */
2764 budsz = BUDSIZE(size, tp->dmt_budmin);
2765
2766 /* back split.
2767 */
2768 while (leaf[leafno] == NOFREE) {
2769 /* find the leftmost buddy leaf.
2770 */
2771 for (w = leafno, bsz = budsz;; bsz <<= 1,
2772 w = (w < bud) ? w : bud) {
b6a47fd8
DK
2773 if (bsz >= le32_to_cpu(tp->dmt_nleafs)) {
2774 jfs_err("JFS: block map error in dbBackSplit");
2775 return -EIO;
2776 }
1da177e4
LT
2777
2778 /* determine the buddy.
2779 */
2780 bud = w ^ bsz;
2781
2782 /* check if this buddy is the start of the system.
2783 */
2784 if (leaf[bud] != NOFREE) {
2785 /* split the leaf at the start of the
2786 * system in two.
2787 */
2788 cursz = leaf[bud] - 1;
2789 dbSplit(tp, bud, cursz, cursz);
2790 break;
2791 }
2792 }
2793 }
2794
b6a47fd8
DK
2795 if (leaf[leafno] != size) {
2796 jfs_err("JFS: wrong leaf value in dbBackSplit");
2797 return -EIO;
2798 }
2799 return 0;
1da177e4
LT
2800}
2801
2802
2803/*
2804 * NAME: dbJoin()
2805 *
f720e3ba 2806 * FUNCTION: update the leaf of a dmtree with a new value, joining
1da177e4
LT
2807 * the leaf with other leaves of the dmtree into a multi-leaf
2808 * binary buddy system, as required.
2809 *
2810 * PARAMETERS:
f720e3ba
DK
2811 * tp - pointer to the tree containing the leaf.
2812 * leafno - the number of the leaf to be updated.
2813 * newval - the new value for the leaf.
1da177e4
LT
2814 *
2815 * RETURN VALUES: none
2816 */
56d12549 2817static int dbJoin(dmtree_t * tp, int leafno, int newval)
1da177e4
LT
2818{
2819 int budsz, buddy;
2820 s8 *leaf;
2821
2822 /* can the new leaf value require a join with other leaves ?
2823 */
2824 if (newval >= tp->dmt_budmin) {
2825 /* pickup a pointer to the leaves of the tree.
2826 */
2827 leaf = tp->dmt_stree + le32_to_cpu(tp->dmt_leafidx);
2828
2829 /* try to join the specified leaf into a large binary
2830 * buddy system. the join proceeds by attempting to join
2831 * the specified leafno with its buddy (leaf) at new value.
2832 * if the join occurs, we attempt to join the left leaf
2833 * of the joined buddies with its buddy at new value + 1.
2834 * we continue to join until we find a buddy that cannot be
2835 * joined (does not have a value equal to the size of the
2836 * last join) or until all leaves have been joined into a
2837 * single system.
2838 *
2839 * get the buddy size (number of words covered) of
2840 * the new value.
2841 */
2842 budsz = BUDSIZE(newval, tp->dmt_budmin);
2843
2844 /* try to join.
2845 */
2846 while (budsz < le32_to_cpu(tp->dmt_nleafs)) {
2847 /* get the buddy leaf.
2848 */
2849 buddy = leafno ^ budsz;
2850
2851 /* if the leaf's new value is greater than its
2852 * buddy's value, we join no more.
2853 */
2854 if (newval > leaf[buddy])
2855 break;
2856
56d12549
DK
2857 /* It shouldn't be less */
2858 if (newval < leaf[buddy])
2859 return -EIO;
1da177e4
LT
2860
2861 /* check which (leafno or buddy) is the left buddy.
2862 * the left buddy gets to claim the blocks resulting
2863 * from the join while the right gets to claim none.
25985edc 2864 * the left buddy is also eligible to participate in
1da177e4
LT
2865 * a join at the next higher level while the right
2866 * is not.
2867 *
2868 */
2869 if (leafno < buddy) {
2870 /* leafno is the left buddy.
2871 */
2872 dbAdjTree(tp, buddy, NOFREE);
2873 } else {
2874 /* buddy is the left buddy and becomes
2875 * leafno.
2876 */
2877 dbAdjTree(tp, leafno, NOFREE);
2878 leafno = buddy;
2879 }
2880
2881 /* on to try the next join.
2882 */
2883 newval += 1;
2884 budsz <<= 1;
2885 }
2886 }
2887
2888 /* update the leaf value.
2889 */
2890 dbAdjTree(tp, leafno, newval);
56d12549
DK
2891
2892 return 0;
1da177e4
LT
2893}
2894
2895
2896/*
2897 * NAME: dbAdjTree()
2898 *
f720e3ba 2899 * FUNCTION: update a leaf of a dmtree with a new value, adjusting
1da177e4
LT
2900 * the dmtree, as required, to reflect the new leaf value.
2901 * the combination of any buddies must already be done before
2902 * this is called.
2903 *
2904 * PARAMETERS:
f720e3ba
DK
2905 * tp - pointer to the tree to be adjusted.
2906 * leafno - the number of the leaf to be updated.
2907 * newval - the new value for the leaf.
1da177e4
LT
2908 *
2909 * RETURN VALUES: none
2910 */
2911static void dbAdjTree(dmtree_t * tp, int leafno, int newval)
2912{
2913 int lp, pp, k;
2914 int max;
2915
2916 /* pick up the index of the leaf for this leafno.
2917 */
2918 lp = leafno + le32_to_cpu(tp->dmt_leafidx);
2919
2920 /* is the current value the same as the old value ? if so,
2921 * there is nothing to do.
2922 */
2923 if (tp->dmt_stree[lp] == newval)
2924 return;
2925
2926 /* set the new value.
2927 */
2928 tp->dmt_stree[lp] = newval;
2929
2930 /* bubble the new value up the tree as required.
2931 */
2932 for (k = 0; k < le32_to_cpu(tp->dmt_height); k++) {
2933 /* get the index of the first leaf of the 4 leaf
2934 * group containing the specified leaf (leafno).
2935 */
2936 lp = ((lp - 1) & ~0x03) + 1;
2937
2938 /* get the index of the parent of this 4 leaf group.
2939 */
2940 pp = (lp - 1) >> 2;
2941
2942 /* determine the maximum of the 4 leaves.
2943 */
2944 max = TREEMAX(&tp->dmt_stree[lp]);
2945
2946 /* if the maximum of the 4 is the same as the
2947 * parent's value, we're done.
2948 */
2949 if (tp->dmt_stree[pp] == max)
2950 break;
2951
2952 /* parent gets new value.
2953 */
2954 tp->dmt_stree[pp] = max;
2955
2956 /* parent becomes leaf for next go-round.
2957 */
2958 lp = pp;
2959 }
2960}
2961
2962
2963/*
2964 * NAME: dbFindLeaf()
2965 *
f720e3ba 2966 * FUNCTION: search a dmtree_t for sufficient free blocks, returning
63f83c9f 2967 * the index of a leaf describing the free blocks if
1da177e4
LT
2968 * sufficient free blocks are found.
2969 *
2970 * the search starts at the top of the dmtree_t tree and
2971 * proceeds down the tree to the leftmost leaf with sufficient
2972 * free space.
2973 *
2974 * PARAMETERS:
f720e3ba
DK
2975 * tp - pointer to the tree to be searched.
2976 * l2nb - log2 number of free blocks to search for.
1da177e4
LT
2977 * leafidx - return pointer to be set to the index of the leaf
2978 * describing at least l2nb free blocks if sufficient
2979 * free blocks are found.
2980 *
2981 * RETURN VALUES:
f720e3ba
DK
2982 * 0 - success
2983 * -ENOSPC - insufficient free blocks.
1da177e4
LT
2984 */
2985static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx)
2986{
2987 int ti, n = 0, k, x = 0;
2988
2989 /* first check the root of the tree to see if there is
2990 * sufficient free space.
2991 */
2992 if (l2nb > tp->dmt_stree[ROOT])
2993 return -ENOSPC;
2994
2995 /* sufficient free space available. now search down the tree
2996 * starting at the next level for the leftmost leaf that
2997 * describes sufficient free space.
2998 */
2999 for (k = le32_to_cpu(tp->dmt_height), ti = 1;
3000 k > 0; k--, ti = ((ti + n) << 2) + 1) {
3001 /* search the four nodes at this level, starting from
3002 * the left.
3003 */
3004 for (x = ti, n = 0; n < 4; n++) {
3005 /* sufficient free space found. move to the next
3006 * level (or quit if this is the last level).
3007 */
3008 if (l2nb <= tp->dmt_stree[x + n])
3009 break;
3010 }
3011
3012 /* better have found something since the higher
3013 * levels of the tree said it was here.
3014 */
3015 assert(n < 4);
3016 }
3017
3018 /* set the return to the leftmost leaf describing sufficient
3019 * free space.
3020 */
3021 *leafidx = x + n - le32_to_cpu(tp->dmt_leafidx);
3022
3023 return (0);
3024}
3025
3026
3027/*
3028 * NAME: dbFindBits()
3029 *
f720e3ba 3030 * FUNCTION: find a specified number of binary buddy free bits within a
1da177e4
LT
3031 * dmap bitmap word value.
3032 *
3033 * this routine searches the bitmap value for (1 << l2nb) free
3034 * bits at (1 << l2nb) alignments within the value.
3035 *
3036 * PARAMETERS:
f720e3ba
DK
3037 * word - dmap bitmap word value.
3038 * l2nb - number of free bits specified as a log2 number.
1da177e4
LT
3039 *
3040 * RETURN VALUES:
f720e3ba 3041 * starting bit number of free bits.
1da177e4
LT
3042 */
3043static int dbFindBits(u32 word, int l2nb)
3044{
3045 int bitno, nb;
3046 u32 mask;
3047
3048 /* get the number of bits.
3049 */
3050 nb = 1 << l2nb;
3051 assert(nb <= DBWORD);
3052
3053 /* complement the word so we can use a mask (i.e. 0s represent
3054 * free bits) and compute the mask.
3055 */
3056 word = ~word;
3057 mask = ONES << (DBWORD - nb);
3058
3059 /* scan the word for nb free bits at nb alignments.
3060 */
3061 for (bitno = 0; mask != 0; bitno += nb, mask >>= nb) {
3062 if ((mask & word) == mask)
3063 break;
3064 }
3065
3066 ASSERT(bitno < 32);
3067
3068 /* return the bit number.
3069 */
3070 return (bitno);
3071}
3072
3073
3074/*
3075 * NAME: dbMaxBud(u8 *cp)
3076 *
f720e3ba 3077 * FUNCTION: determine the largest binary buddy string of free
1da177e4
LT
3078 * bits within 32-bits of the map.
3079 *
3080 * PARAMETERS:
f720e3ba 3081 * cp - pointer to the 32-bit value.
1da177e4
LT
3082 *
3083 * RETURN VALUES:
f720e3ba 3084 * largest binary buddy of free bits within a dmap word.
1da177e4
LT
3085 */
3086static int dbMaxBud(u8 * cp)
3087{
3088 signed char tmp1, tmp2;
3089
3090 /* check if the wmap word is all free. if so, the
3091 * free buddy size is BUDMIN.
3092 */
3093 if (*((uint *) cp) == 0)
3094 return (BUDMIN);
3095
3096 /* check if the wmap word is half free. if so, the
3097 * free buddy size is BUDMIN-1.
3098 */
3099 if (*((u16 *) cp) == 0 || *((u16 *) cp + 1) == 0)
3100 return (BUDMIN - 1);
3101
3102 /* not all free or half free. determine the free buddy
3103 * size thru table lookup using quarters of the wmap word.
3104 */
3105 tmp1 = max(budtab[cp[2]], budtab[cp[3]]);
3106 tmp2 = max(budtab[cp[0]], budtab[cp[1]]);
3107 return (max(tmp1, tmp2));
3108}
3109
3110
3111/*
3112 * NAME: cnttz(uint word)
3113 *
f720e3ba 3114 * FUNCTION: determine the number of trailing zeros within a 32-bit
1da177e4
LT
3115 * value.
3116 *
3117 * PARAMETERS:
f720e3ba 3118 * value - 32-bit value to be examined.
1da177e4
LT
3119 *
3120 * RETURN VALUES:
f720e3ba 3121 * count of trailing zeros
1da177e4
LT
3122 */
3123static int cnttz(u32 word)
3124{
3125 int n;
3126
3127 for (n = 0; n < 32; n++, word >>= 1) {
3128 if (word & 0x01)
3129 break;
3130 }
3131
3132 return (n);
3133}
3134
3135
3136/*
3137 * NAME: cntlz(u32 value)
3138 *
f720e3ba 3139 * FUNCTION: determine the number of leading zeros within a 32-bit
1da177e4
LT
3140 * value.
3141 *
3142 * PARAMETERS:
f720e3ba 3143 * value - 32-bit value to be examined.
1da177e4
LT
3144 *
3145 * RETURN VALUES:
f720e3ba 3146 * count of leading zeros
1da177e4
LT
3147 */
3148static int cntlz(u32 value)
3149{
3150 int n;
3151
3152 for (n = 0; n < 32; n++, value <<= 1) {
3153 if (value & HIGHORDER)
3154 break;
3155 }
3156 return (n);
3157}
3158
3159
3160/*
3161 * NAME: blkstol2(s64 nb)
3162 *
3163 * FUNCTION: convert a block count to its log2 value. if the block
f720e3ba 3164 * count is not a l2 multiple, it is rounded up to the next
1da177e4
LT
3165 * larger l2 multiple.
3166 *
3167 * PARAMETERS:
f720e3ba 3168 * nb - number of blocks
1da177e4
LT
3169 *
3170 * RETURN VALUES:
f720e3ba 3171 * log2 number of blocks
1da177e4 3172 */
6cb1269b 3173static int blkstol2(s64 nb)
1da177e4
LT
3174{
3175 int l2nb;
3176 s64 mask; /* meant to be signed */
3177
3178 mask = (s64) 1 << (64 - 1);
3179
3180 /* count the leading bits.
3181 */
3182 for (l2nb = 0; l2nb < 64; l2nb++, mask >>= 1) {
3183 /* leading bit found.
3184 */
3185 if (nb & mask) {
3186 /* determine the l2 value.
3187 */
3188 l2nb = (64 - 1) - l2nb;
3189
3190 /* check if we need to round up.
3191 */
3192 if (~mask & nb)
3193 l2nb++;
3194
3195 return (l2nb);
3196 }
3197 }
3198 assert(0);
3199 return 0; /* fix compiler warning */
3200}
3201
3202
3203/*
63f83c9f 3204 * NAME: dbAllocBottomUp()
1da177e4
LT
3205 *
3206 * FUNCTION: alloc the specified block range from the working block
3207 * allocation map.
3208 *
3209 * the blocks will be alloc from the working map one dmap
3210 * at a time.
3211 *
3212 * PARAMETERS:
f720e3ba
DK
3213 * ip - pointer to in-core inode;
3214 * blkno - starting block number to be freed.
3215 * nblocks - number of blocks to be freed.
1da177e4
LT
3216 *
3217 * RETURN VALUES:
f720e3ba
DK
3218 * 0 - success
3219 * -EIO - i/o error
1da177e4
LT
3220 */
3221int dbAllocBottomUp(struct inode *ip, s64 blkno, s64 nblocks)
3222{
3223 struct metapage *mp;
3224 struct dmap *dp;
3225 int nb, rc;
3226 s64 lblkno, rem;
3227 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
3228 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
3229
82d5b9a7 3230 IREAD_LOCK(ipbmap, RDWRLOCK_DMAP);
1da177e4
LT
3231
3232 /* block to be allocated better be within the mapsize. */
3233 ASSERT(nblocks <= bmp->db_mapsize - blkno);
3234
3235 /*
3236 * allocate the blocks a dmap at a time.
3237 */
3238 mp = NULL;
3239 for (rem = nblocks; rem > 0; rem -= nb, blkno += nb) {
3240 /* release previous dmap if any */
3241 if (mp) {
3242 write_metapage(mp);
3243 }
3244
3245 /* get the buffer for the current dmap. */
3246 lblkno = BLKTODMAP(blkno, bmp->db_l2nbperpage);
3247 mp = read_metapage(ipbmap, lblkno, PSIZE, 0);
3248 if (mp == NULL) {
3249 IREAD_UNLOCK(ipbmap);
3250 return -EIO;
3251 }
3252 dp = (struct dmap *) mp->data;
3253
3254 /* determine the number of blocks to be allocated from
3255 * this dmap.
3256 */
3257 nb = min(rem, BPERDMAP - (blkno & (BPERDMAP - 1)));
3258
1da177e4
LT
3259 /* allocate the blocks. */
3260 if ((rc = dbAllocDmapBU(bmp, dp, blkno, nb))) {
3261 release_metapage(mp);
3262 IREAD_UNLOCK(ipbmap);
3263 return (rc);
3264 }
1da177e4
LT
3265 }
3266
3267 /* write the last buffer. */
3268 write_metapage(mp);
3269
3270 IREAD_UNLOCK(ipbmap);
3271
3272 return (0);
3273}
3274
3275
3276static int dbAllocDmapBU(struct bmap * bmp, struct dmap * dp, s64 blkno,
3277 int nblocks)
3278{
3279 int rc;
3280 int dbitno, word, rembits, nb, nwords, wbitno, agno;
3c2c2262 3281 s8 oldroot;
1da177e4
LT
3282 struct dmaptree *tp = (struct dmaptree *) & dp->tree;
3283
3284 /* save the current value of the root (i.e. maximum free string)
3285 * of the dmap tree.
3286 */
3287 oldroot = tp->stree[ROOT];
3288
1da177e4
LT
3289 /* determine the bit number and word within the dmap of the
3290 * starting block.
3291 */
3292 dbitno = blkno & (BPERDMAP - 1);
3293 word = dbitno >> L2DBWORD;
3294
3295 /* block range better be within the dmap */
3296 assert(dbitno + nblocks <= BPERDMAP);
3297
3298 /* allocate the bits of the dmap's words corresponding to the block
3299 * range. not all bits of the first and last words may be contained
3300 * within the block range. if this is the case, we'll work against
3301 * those words (i.e. partial first and/or last) on an individual basis
3302 * (a single pass), allocating the bits of interest by hand and
3303 * updating the leaf corresponding to the dmap word. a single pass
3304 * will be used for all dmap words fully contained within the
3305 * specified range. within this pass, the bits of all fully contained
3306 * dmap words will be marked as free in a single shot and the leaves
3307 * will be updated. a single leaf may describe the free space of
3308 * multiple dmap words, so we may update only a subset of the actual
3309 * leaves corresponding to the dmap words of the block range.
3310 */
3311 for (rembits = nblocks; rembits > 0; rembits -= nb, dbitno += nb) {
3312 /* determine the bit number within the word and
3313 * the number of bits within the word.
3314 */
3315 wbitno = dbitno & (DBWORD - 1);
3316 nb = min(rembits, DBWORD - wbitno);
3317
3318 /* check if only part of a word is to be allocated.
3319 */
3320 if (nb < DBWORD) {
3321 /* allocate (set to 1) the appropriate bits within
3322 * this dmap word.
3323 */
3324 dp->wmap[word] |= cpu_to_le32(ONES << (DBWORD - nb)
3325 >> wbitno);
3326
3327 word++;
3328 } else {
3329 /* one or more dmap words are fully contained
3330 * within the block range. determine how many
3331 * words and allocate (set to 1) the bits of these
3332 * words.
3333 */
3334 nwords = rembits >> L2DBWORD;
3335 memset(&dp->wmap[word], (int) ONES, nwords * 4);
3336
3337 /* determine how many bits */
3338 nb = nwords << L2DBWORD;
3339 word += nwords;
3340 }
3341 }
3342
3343 /* update the free count for this dmap */
89145622 3344 le32_add_cpu(&dp->nfree, -nblocks);
1da177e4
LT
3345
3346 /* reconstruct summary tree */
3347 dbInitDmapTree(dp);
3348
3349 BMAP_LOCK(bmp);
3350
3351 /* if this allocation group is completely free,
63f83c9f 3352 * update the highest active allocation group number
1da177e4
LT
3353 * if this allocation group is the new max.
3354 */
3355 agno = blkno >> bmp->db_agl2size;
3356 if (agno > bmp->db_maxag)
3357 bmp->db_maxag = agno;
3358
3359 /* update the free count for the allocation group and map */
3360 bmp->db_agfree[agno] -= nblocks;
3361 bmp->db_nfree -= nblocks;
3362
3363 BMAP_UNLOCK(bmp);
3364
3365 /* if the root has not changed, done. */
3366 if (tp->stree[ROOT] == oldroot)
3367 return (0);
3368
3369 /* root changed. bubble the change up to the dmap control pages.
3370 * if the adjustment of the upper level control pages fails,
3371 * backout the bit allocation (thus making everything consistent).
3372 */
3373 if ((rc = dbAdjCtl(bmp, blkno, tp->stree[ROOT], 1, 0)))
3374 dbFreeBits(bmp, dp, blkno, nblocks);
3375
3376 return (rc);
3377}
3378
3379
3380/*
3381 * NAME: dbExtendFS()
3382 *
3383 * FUNCTION: extend bmap from blkno for nblocks;
63f83c9f 3384 * dbExtendFS() updates bmap ready for dbAllocBottomUp();
1da177e4
LT
3385 *
3386 * L2
3387 * |
3388 * L1---------------------------------L1
f720e3ba
DK
3389 * | |
3390 * L0---------L0---------L0 L0---------L0---------L0
3391 * | | | | | |
3392 * d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,...,dn d0,.,dm;
1da177e4
LT
3393 * L2L1L0d0,...,dnL0d0,...,dnL0d0,...,dnL1L0d0,...,dnL0d0,...,dnL0d0,..dm
3394 *
63f83c9f 3395 * <---old---><----------------------------extend----------------------->
1da177e4
LT
3396 */
3397int dbExtendFS(struct inode *ipbmap, s64 blkno, s64 nblocks)
3398{
3399 struct jfs_sb_info *sbi = JFS_SBI(ipbmap->i_sb);
3400 int nbperpage = sbi->nbperpage;
4d81715f 3401 int i, i0 = true, j, j0 = true, k, n;
1da177e4
LT
3402 s64 newsize;
3403 s64 p;
3404 struct metapage *mp, *l2mp, *l1mp = NULL, *l0mp = NULL;
3405 struct dmapctl *l2dcp, *l1dcp, *l0dcp;
3406 struct dmap *dp;
3407 s8 *l0leaf, *l1leaf, *l2leaf;
3408 struct bmap *bmp = sbi->bmap;
3409 int agno, l2agsize, oldl2agsize;
3410 s64 ag_rem;
3411
3412 newsize = blkno + nblocks;
3413
3414 jfs_info("dbExtendFS: blkno:%Ld nblocks:%Ld newsize:%Ld",
3415 (long long) blkno, (long long) nblocks, (long long) newsize);
3416
3417 /*
f720e3ba 3418 * initialize bmap control page.
1da177e4
LT
3419 *
3420 * all the data in bmap control page should exclude
3421 * the mkfs hidden dmap page.
3422 */
3423
3424 /* update mapsize */
3425 bmp->db_mapsize = newsize;
3426 bmp->db_maxlevel = BMAPSZTOLEV(bmp->db_mapsize);
3427
3428 /* compute new AG size */
3429 l2agsize = dbGetL2AGSize(newsize);
3430 oldl2agsize = bmp->db_agl2size;
3431
3432 bmp->db_agl2size = l2agsize;
3433 bmp->db_agsize = 1 << l2agsize;
3434
3435 /* compute new number of AG */
3436 agno = bmp->db_numag;
3437 bmp->db_numag = newsize >> l2agsize;
3438 bmp->db_numag += ((u32) newsize % (u32) bmp->db_agsize) ? 1 : 0;
3439
3440 /*
f720e3ba 3441 * reconfigure db_agfree[]
1da177e4
LT
3442 * from old AG configuration to new AG configuration;
3443 *
3444 * coalesce contiguous k (newAGSize/oldAGSize) AGs;
3445 * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
3446 * note: new AG size = old AG size * (2**x).
3447 */
3448 if (l2agsize == oldl2agsize)
3449 goto extend;
3450 k = 1 << (l2agsize - oldl2agsize);
3451 ag_rem = bmp->db_agfree[0]; /* save agfree[0] */
3452 for (i = 0, n = 0; i < agno; n++) {
3453 bmp->db_agfree[n] = 0; /* init collection point */
3454
af901ca1 3455 /* coalesce contiguous k AGs; */
1da177e4
LT
3456 for (j = 0; j < k && i < agno; j++, i++) {
3457 /* merge AGi to AGn */
3458 bmp->db_agfree[n] += bmp->db_agfree[i];
3459 }
3460 }
3461 bmp->db_agfree[0] += ag_rem; /* restore agfree[0] */
3462
3463 for (; n < MAXAG; n++)
3464 bmp->db_agfree[n] = 0;
3465
3466 /*
3467 * update highest active ag number
3468 */
3469
3470 bmp->db_maxag = bmp->db_maxag / k;
3471
3472 /*
f720e3ba 3473 * extend bmap
1da177e4
LT
3474 *
3475 * update bit maps and corresponding level control pages;
3476 * global control page db_nfree, db_agfree[agno], db_maxfreebud;
3477 */
3478 extend:
3479 /* get L2 page */
3480 p = BMAPBLKNO + nbperpage; /* L2 page */
3481 l2mp = read_metapage(ipbmap, p, PSIZE, 0);
3482 if (!l2mp) {
3483 jfs_error(ipbmap->i_sb, "dbExtendFS: L2 page could not be read");
3484 return -EIO;
3485 }
3486 l2dcp = (struct dmapctl *) l2mp->data;
3487
3488 /* compute start L1 */
3489 k = blkno >> L2MAXL1SIZE;
3490 l2leaf = l2dcp->stree + CTLLEAFIND + k;
3491 p = BLKTOL1(blkno, sbi->l2nbperpage); /* L1 page */
3492
3493 /*
3494 * extend each L1 in L2
3495 */
3496 for (; k < LPERCTL; k++, p += nbperpage) {
3497 /* get L1 page */
3498 if (j0) {
3499 /* read in L1 page: (blkno & (MAXL1SIZE - 1)) */
3500 l1mp = read_metapage(ipbmap, p, PSIZE, 0);
3501 if (l1mp == NULL)
3502 goto errout;
3503 l1dcp = (struct dmapctl *) l1mp->data;
3504
3505 /* compute start L0 */
3506 j = (blkno & (MAXL1SIZE - 1)) >> L2MAXL0SIZE;
3507 l1leaf = l1dcp->stree + CTLLEAFIND + j;
3508 p = BLKTOL0(blkno, sbi->l2nbperpage);
4d81715f 3509 j0 = false;
1da177e4
LT
3510 } else {
3511 /* assign/init L1 page */
3512 l1mp = get_metapage(ipbmap, p, PSIZE, 0);
3513 if (l1mp == NULL)
3514 goto errout;
3515
3516 l1dcp = (struct dmapctl *) l1mp->data;
3517
3518 /* compute start L0 */
3519 j = 0;
3520 l1leaf = l1dcp->stree + CTLLEAFIND;
f720e3ba 3521 p += nbperpage; /* 1st L0 of L1.k */
1da177e4
LT
3522 }
3523
3524 /*
3525 * extend each L0 in L1
3526 */
3527 for (; j < LPERCTL; j++) {
3528 /* get L0 page */
3529 if (i0) {
3530 /* read in L0 page: (blkno & (MAXL0SIZE - 1)) */
3531
3532 l0mp = read_metapage(ipbmap, p, PSIZE, 0);
3533 if (l0mp == NULL)
3534 goto errout;
3535 l0dcp = (struct dmapctl *) l0mp->data;
3536
3537 /* compute start dmap */
3538 i = (blkno & (MAXL0SIZE - 1)) >>
3539 L2BPERDMAP;
3540 l0leaf = l0dcp->stree + CTLLEAFIND + i;
3541 p = BLKTODMAP(blkno,
3542 sbi->l2nbperpage);
4d81715f 3543 i0 = false;
1da177e4
LT
3544 } else {
3545 /* assign/init L0 page */
3546 l0mp = get_metapage(ipbmap, p, PSIZE, 0);
3547 if (l0mp == NULL)
3548 goto errout;
3549
3550 l0dcp = (struct dmapctl *) l0mp->data;
3551
3552 /* compute start dmap */
3553 i = 0;
3554 l0leaf = l0dcp->stree + CTLLEAFIND;
3555 p += nbperpage; /* 1st dmap of L0.j */
3556 }
3557
3558 /*
3559 * extend each dmap in L0
3560 */
3561 for (; i < LPERCTL; i++) {
3562 /*
3563 * reconstruct the dmap page, and
3564 * initialize corresponding parent L0 leaf
3565 */
3566 if ((n = blkno & (BPERDMAP - 1))) {
3567 /* read in dmap page: */
3568 mp = read_metapage(ipbmap, p,
3569 PSIZE, 0);
3570 if (mp == NULL)
3571 goto errout;
3572 n = min(nblocks, (s64)BPERDMAP - n);
3573 } else {
3574 /* assign/init dmap page */
3575 mp = read_metapage(ipbmap, p,
3576 PSIZE, 0);
3577 if (mp == NULL)
3578 goto errout;
3579
3580 n = min(nblocks, (s64)BPERDMAP);
3581 }
3582
3583 dp = (struct dmap *) mp->data;
3584 *l0leaf = dbInitDmap(dp, blkno, n);
3585
3586 bmp->db_nfree += n;
3587 agno = le64_to_cpu(dp->start) >> l2agsize;
3588 bmp->db_agfree[agno] += n;
3589
3590 write_metapage(mp);
3591
3592 l0leaf++;
3593 p += nbperpage;
3594
3595 blkno += n;
3596 nblocks -= n;
3597 if (nblocks == 0)
3598 break;
3599 } /* for each dmap in a L0 */
3600
3601 /*
63f83c9f 3602 * build current L0 page from its leaves, and
1da177e4
LT
3603 * initialize corresponding parent L1 leaf
3604 */
3605 *l1leaf = dbInitDmapCtl(l0dcp, 0, ++i);
3606 write_metapage(l0mp);
3607 l0mp = NULL;
3608
3609 if (nblocks)
3610 l1leaf++; /* continue for next L0 */
3611 else {
3612 /* more than 1 L0 ? */
3613 if (j > 0)
3614 break; /* build L1 page */
3615 else {
3616 /* summarize in global bmap page */
3617 bmp->db_maxfreebud = *l1leaf;
3618 release_metapage(l1mp);
3619 release_metapage(l2mp);
3620 goto finalize;
3621 }
3622 }
3623 } /* for each L0 in a L1 */
3624
3625 /*
63f83c9f 3626 * build current L1 page from its leaves, and
1da177e4
LT
3627 * initialize corresponding parent L2 leaf
3628 */
3629 *l2leaf = dbInitDmapCtl(l1dcp, 1, ++j);
3630 write_metapage(l1mp);
3631 l1mp = NULL;
3632
3633 if (nblocks)
3634 l2leaf++; /* continue for next L1 */
3635 else {
3636 /* more than 1 L1 ? */
3637 if (k > 0)
3638 break; /* build L2 page */
3639 else {
3640 /* summarize in global bmap page */
3641 bmp->db_maxfreebud = *l2leaf;
3642 release_metapage(l2mp);
3643 goto finalize;
3644 }
3645 }
3646 } /* for each L1 in a L2 */
3647
3648 jfs_error(ipbmap->i_sb,
3649 "dbExtendFS: function has not returned as expected");
3650errout:
3651 if (l0mp)
3652 release_metapage(l0mp);
3653 if (l1mp)
3654 release_metapage(l1mp);
3655 release_metapage(l2mp);
3656 return -EIO;
3657
3658 /*
f720e3ba 3659 * finalize bmap control page
1da177e4
LT
3660 */
3661finalize:
3662
3663 return 0;
3664}
3665
3666
3667/*
3668 * dbFinalizeBmap()
3669 */
3670void dbFinalizeBmap(struct inode *ipbmap)
3671{
3672 struct bmap *bmp = JFS_SBI(ipbmap->i_sb)->bmap;
3673 int actags, inactags, l2nl;
3674 s64 ag_rem, actfree, inactfree, avgfree;
3675 int i, n;
3676
3677 /*
f720e3ba 3678 * finalize bmap control page
1da177e4
LT
3679 */
3680//finalize:
63f83c9f 3681 /*
1da177e4
LT
3682 * compute db_agpref: preferred ag to allocate from
3683 * (the leftmost ag with average free space in it);
3684 */
3685//agpref:
3686 /* get the number of active ags and inacitve ags */
3687 actags = bmp->db_maxag + 1;
3688 inactags = bmp->db_numag - actags;
3689 ag_rem = bmp->db_mapsize & (bmp->db_agsize - 1); /* ??? */
3690
3691 /* determine how many blocks are in the inactive allocation
3692 * groups. in doing this, we must account for the fact that
3693 * the rightmost group might be a partial group (i.e. file
3694 * system size is not a multiple of the group size).
3695 */
3696 inactfree = (inactags && ag_rem) ?
3697 ((inactags - 1) << bmp->db_agl2size) + ag_rem
3698 : inactags << bmp->db_agl2size;
3699
3700 /* determine how many free blocks are in the active
3701 * allocation groups plus the average number of free blocks
3702 * within the active ags.
3703 */
3704 actfree = bmp->db_nfree - inactfree;
3705 avgfree = (u32) actfree / (u32) actags;
3706
3707 /* if the preferred allocation group has not average free space.
3708 * re-establish the preferred group as the leftmost
3709 * group with average free space.
3710 */
3711 if (bmp->db_agfree[bmp->db_agpref] < avgfree) {
3712 for (bmp->db_agpref = 0; bmp->db_agpref < actags;
3713 bmp->db_agpref++) {
3714 if (bmp->db_agfree[bmp->db_agpref] >= avgfree)
3715 break;
3716 }
3717 if (bmp->db_agpref >= bmp->db_numag) {
3718 jfs_error(ipbmap->i_sb,
3719 "cannot find ag with average freespace");
3720 }
3721 }
3722
3723 /*
d7eecb48 3724 * compute db_aglevel, db_agheight, db_width, db_agstart:
63f83c9f
DK
3725 * an ag is covered in aglevel dmapctl summary tree,
3726 * at agheight level height (from leaf) with agwidth number of nodes
3727 * each, which starts at agstart index node of the smmary tree node
1da177e4
LT
3728 * array;
3729 */
3730 bmp->db_aglevel = BMAPSZTOLEV(bmp->db_agsize);
3731 l2nl =
3732 bmp->db_agl2size - (L2BPERDMAP + bmp->db_aglevel * L2LPERCTL);
d7eecb48
DM
3733 bmp->db_agheight = l2nl >> 1;
3734 bmp->db_agwidth = 1 << (l2nl - (bmp->db_agheight << 1));
3735 for (i = 5 - bmp->db_agheight, bmp->db_agstart = 0, n = 1; i > 0;
1da177e4
LT
3736 i--) {
3737 bmp->db_agstart += n;
3738 n <<= 2;
3739 }
3740
3741}
3742
3743
3744/*
3745 * NAME: dbInitDmap()/ujfs_idmap_page()
63f83c9f 3746 *
1da177e4
LT
3747 * FUNCTION: initialize working/persistent bitmap of the dmap page
3748 * for the specified number of blocks:
63f83c9f 3749 *
1da177e4 3750 * at entry, the bitmaps had been initialized as free (ZEROS);
63f83c9f
DK
3751 * The number of blocks will only account for the actually
3752 * existing blocks. Blocks which don't actually exist in
1da177e4
LT
3753 * the aggregate will be marked as allocated (ONES);
3754 *
3755 * PARAMETERS:
3756 * dp - pointer to page of map
3757 * nblocks - number of blocks this page
3758 *
3759 * RETURNS: NONE
3760 */
3761static int dbInitDmap(struct dmap * dp, s64 Blkno, int nblocks)
3762{
3763 int blkno, w, b, r, nw, nb, i;
3764
3765 /* starting block number within the dmap */
3766 blkno = Blkno & (BPERDMAP - 1);
3767
3768 if (blkno == 0) {
3769 dp->nblocks = dp->nfree = cpu_to_le32(nblocks);
3770 dp->start = cpu_to_le64(Blkno);
3771
3772 if (nblocks == BPERDMAP) {
3773 memset(&dp->wmap[0], 0, LPERDMAP * 4);
3774 memset(&dp->pmap[0], 0, LPERDMAP * 4);
3775 goto initTree;
3776 }
3777 } else {
89145622
MS
3778 le32_add_cpu(&dp->nblocks, nblocks);
3779 le32_add_cpu(&dp->nfree, nblocks);
1da177e4
LT
3780 }
3781
3782 /* word number containing start block number */
3783 w = blkno >> L2DBWORD;
3784
3785 /*
3786 * free the bits corresponding to the block range (ZEROS):
63f83c9f 3787 * note: not all bits of the first and last words may be contained
1da177e4
LT
3788 * within the block range.
3789 */
3790 for (r = nblocks; r > 0; r -= nb, blkno += nb) {
3791 /* number of bits preceding range to be freed in the word */
3792 b = blkno & (DBWORD - 1);
3793 /* number of bits to free in the word */
3794 nb = min(r, DBWORD - b);
3795
3796 /* is partial word to be freed ? */
3797 if (nb < DBWORD) {
3798 /* free (set to 0) from the bitmap word */
3799 dp->wmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3800 >> b));
3801 dp->pmap[w] &= cpu_to_le32(~(ONES << (DBWORD - nb)
3802 >> b));
3803
3804 /* skip the word freed */
3805 w++;
3806 } else {
3807 /* free (set to 0) contiguous bitmap words */
3808 nw = r >> L2DBWORD;
3809 memset(&dp->wmap[w], 0, nw * 4);
3810 memset(&dp->pmap[w], 0, nw * 4);
3811
3812 /* skip the words freed */
3813 nb = nw << L2DBWORD;
3814 w += nw;
3815 }
3816 }
3817
3818 /*
63f83c9f 3819 * mark bits following the range to be freed (non-existing
1da177e4
LT
3820 * blocks) as allocated (ONES)
3821 */
3822
3823 if (blkno == BPERDMAP)
3824 goto initTree;
3825
3826 /* the first word beyond the end of existing blocks */
3827 w = blkno >> L2DBWORD;
3828
3829 /* does nblocks fall on a 32-bit boundary ? */
3830 b = blkno & (DBWORD - 1);
3831 if (b) {
3832 /* mark a partial word allocated */
3833 dp->wmap[w] = dp->pmap[w] = cpu_to_le32(ONES >> b);
3834 w++;
3835 }
3836
3837 /* set the rest of the words in the page to allocated (ONES) */
3838 for (i = w; i < LPERDMAP; i++)
3839 dp->pmap[i] = dp->wmap[i] = cpu_to_le32(ONES);
3840
3841 /*
3842 * init tree
3843 */
3844 initTree:
3845 return (dbInitDmapTree(dp));
3846}
3847
3848
3849/*
3850 * NAME: dbInitDmapTree()/ujfs_complete_dmap()
63f83c9f 3851 *
1da177e4
LT
3852 * FUNCTION: initialize summary tree of the specified dmap:
3853 *
3854 * at entry, bitmap of the dmap has been initialized;
63f83c9f 3855 *
1da177e4
LT
3856 * PARAMETERS:
3857 * dp - dmap to complete
3858 * blkno - starting block number for this dmap
3859 * treemax - will be filled in with max free for this dmap
3860 *
3861 * RETURNS: max free string at the root of the tree
3862 */
3863static int dbInitDmapTree(struct dmap * dp)
3864{
3865 struct dmaptree *tp;
3866 s8 *cp;
3867 int i;
3868
3869 /* init fixed info of tree */
3870 tp = &dp->tree;
3871 tp->nleafs = cpu_to_le32(LPERDMAP);
3872 tp->l2nleafs = cpu_to_le32(L2LPERDMAP);
3873 tp->leafidx = cpu_to_le32(LEAFIND);
3874 tp->height = cpu_to_le32(4);
3875 tp->budmin = BUDMIN;
3876
3877 /* init each leaf from corresponding wmap word:
3878 * note: leaf is set to NOFREE(-1) if all blocks of corresponding
63f83c9f 3879 * bitmap word are allocated.
1da177e4
LT
3880 */
3881 cp = tp->stree + le32_to_cpu(tp->leafidx);
3882 for (i = 0; i < LPERDMAP; i++)
3883 *cp++ = dbMaxBud((u8 *) & dp->wmap[i]);
3884
3885 /* build the dmap's binary buddy summary tree */
3886 return (dbInitTree(tp));
3887}
3888
3889
3890/*
3891 * NAME: dbInitTree()/ujfs_adjtree()
63f83c9f 3892 *
1da177e4
LT
3893 * FUNCTION: initialize binary buddy summary tree of a dmap or dmapctl.
3894 *
63f83c9f 3895 * at entry, the leaves of the tree has been initialized
1da177e4
LT
3896 * from corresponding bitmap word or root of summary tree
3897 * of the child control page;
3898 * configure binary buddy system at the leaf level, then
3899 * bubble up the values of the leaf nodes up the tree.
3900 *
3901 * PARAMETERS:
3902 * cp - Pointer to the root of the tree
3903 * l2leaves- Number of leaf nodes as a power of 2
3904 * l2min - Number of blocks that can be covered by a leaf
3905 * as a power of 2
3906 *
3907 * RETURNS: max free string at the root of the tree
3908 */
3909static int dbInitTree(struct dmaptree * dtp)
3910{
3911 int l2max, l2free, bsize, nextb, i;
3912 int child, parent, nparent;
3913 s8 *tp, *cp, *cp1;
3914
3915 tp = dtp->stree;
3916
3917 /* Determine the maximum free string possible for the leaves */
3918 l2max = le32_to_cpu(dtp->l2nleafs) + dtp->budmin;
3919
3920 /*
3921 * configure the leaf levevl into binary buddy system
3922 *
63f83c9f
DK
3923 * Try to combine buddies starting with a buddy size of 1
3924 * (i.e. two leaves). At a buddy size of 1 two buddy leaves
3925 * can be combined if both buddies have a maximum free of l2min;
3926 * the combination will result in the left-most buddy leaf having
3927 * a maximum free of l2min+1.
3928 * After processing all buddies for a given size, process buddies
3929 * at the next higher buddy size (i.e. current size * 2) and
3930 * the next maximum free (current free + 1).
3931 * This continues until the maximum possible buddy combination
1da177e4
LT
3932 * yields maximum free.
3933 */
3934 for (l2free = dtp->budmin, bsize = 1; l2free < l2max;
3935 l2free++, bsize = nextb) {
3936 /* get next buddy size == current buddy pair size */
3937 nextb = bsize << 1;
3938
3939 /* scan each adjacent buddy pair at current buddy size */
3940 for (i = 0, cp = tp + le32_to_cpu(dtp->leafidx);
3941 i < le32_to_cpu(dtp->nleafs);
3942 i += nextb, cp += nextb) {
3943 /* coalesce if both adjacent buddies are max free */
3944 if (*cp == l2free && *(cp + bsize) == l2free) {
3945 *cp = l2free + 1; /* left take right */
3946 *(cp + bsize) = -1; /* right give left */
3947 }
3948 }
3949 }
3950
3951 /*
3952 * bubble summary information of leaves up the tree.
3953 *
3954 * Starting at the leaf node level, the four nodes described by
63f83c9f
DK
3955 * the higher level parent node are compared for a maximum free and
3956 * this maximum becomes the value of the parent node.
3957 * when all lower level nodes are processed in this fashion then
3958 * move up to the next level (parent becomes a lower level node) and
1da177e4
LT
3959 * continue the process for that level.
3960 */
3961 for (child = le32_to_cpu(dtp->leafidx),
3962 nparent = le32_to_cpu(dtp->nleafs) >> 2;
3963 nparent > 0; nparent >>= 2, child = parent) {
3964 /* get index of 1st node of parent level */
3965 parent = (child - 1) >> 2;
3966
63f83c9f 3967 /* set the value of the parent node as the maximum
1da177e4
LT
3968 * of the four nodes of the current level.
3969 */
3970 for (i = 0, cp = tp + child, cp1 = tp + parent;
3971 i < nparent; i++, cp += 4, cp1++)
3972 *cp1 = TREEMAX(cp);
3973 }
3974
3975 return (*tp);
3976}
3977
3978
3979/*
3980 * dbInitDmapCtl()
3981 *
3982 * function: initialize dmapctl page
3983 */
3984static int dbInitDmapCtl(struct dmapctl * dcp, int level, int i)
3985{ /* start leaf index not covered by range */
3986 s8 *cp;
3987
3988 dcp->nleafs = cpu_to_le32(LPERCTL);
3989 dcp->l2nleafs = cpu_to_le32(L2LPERCTL);
3990 dcp->leafidx = cpu_to_le32(CTLLEAFIND);
3991 dcp->height = cpu_to_le32(5);
3992 dcp->budmin = L2BPERDMAP + L2LPERCTL * level;
3993
3994 /*
63f83c9f
DK
3995 * initialize the leaves of current level that were not covered
3996 * by the specified input block range (i.e. the leaves have no
1da177e4
LT
3997 * low level dmapctl or dmap).
3998 */
3999 cp = &dcp->stree[CTLLEAFIND + i];
4000 for (; i < LPERCTL; i++)
4001 *cp++ = NOFREE;
4002
4003 /* build the dmap's binary buddy summary tree */
4004 return (dbInitTree((struct dmaptree *) dcp));
4005}
4006
4007
4008/*
4009 * NAME: dbGetL2AGSize()/ujfs_getagl2size()
63f83c9f 4010 *
1da177e4 4011 * FUNCTION: Determine log2(allocation group size) from aggregate size
63f83c9f 4012 *
1da177e4
LT
4013 * PARAMETERS:
4014 * nblocks - Number of blocks in aggregate
4015 *
4016 * RETURNS: log2(allocation group size) in aggregate blocks
4017 */
4018static int dbGetL2AGSize(s64 nblocks)
4019{
4020 s64 sz;
4021 s64 m;
4022 int l2sz;
4023
4024 if (nblocks < BPERDMAP * MAXAG)
4025 return (L2BPERDMAP);
4026
4027 /* round up aggregate size to power of 2 */
4028 m = ((u64) 1 << (64 - 1));
4029 for (l2sz = 64; l2sz >= 0; l2sz--, m >>= 1) {
4030 if (m & nblocks)
4031 break;
4032 }
4033
4034 sz = (s64) 1 << l2sz;
4035 if (sz < nblocks)
4036 l2sz += 1;
4037
4038 /* agsize = roundupSize/max_number_of_ag */
4039 return (l2sz - L2MAXAG);
4040}
4041
4042
4043/*
4044 * NAME: dbMapFileSizeToMapSize()
63f83c9f
DK
4045 *
4046 * FUNCTION: compute number of blocks the block allocation map file
1da177e4
LT
4047 * can cover from the map file size;
4048 *
4049 * RETURNS: Number of blocks which can be covered by this block map file;
4050 */
4051
4052/*
4053 * maximum number of map pages at each level including control pages
4054 */
4055#define MAXL0PAGES (1 + LPERCTL)
4056#define MAXL1PAGES (1 + LPERCTL * MAXL0PAGES)
4057#define MAXL2PAGES (1 + LPERCTL * MAXL1PAGES)
4058
4059/*
4060 * convert number of map pages to the zero origin top dmapctl level
4061 */
4062#define BMAPPGTOLEV(npages) \
f720e3ba
DK
4063 (((npages) <= 3 + MAXL0PAGES) ? 0 : \
4064 ((npages) <= 2 + MAXL1PAGES) ? 1 : 2)
1da177e4
LT
4065
4066s64 dbMapFileSizeToMapSize(struct inode * ipbmap)
4067{
4068 struct super_block *sb = ipbmap->i_sb;
4069 s64 nblocks;
4070 s64 npages, ndmaps;
4071 int level, i;
4072 int complete, factor;
4073
4074 nblocks = ipbmap->i_size >> JFS_SBI(sb)->l2bsize;
4075 npages = nblocks >> JFS_SBI(sb)->l2nbperpage;
4076 level = BMAPPGTOLEV(npages);
4077
63f83c9f 4078 /* At each level, accumulate the number of dmap pages covered by
1da177e4
LT
4079 * the number of full child levels below it;
4080 * repeat for the last incomplete child level.
4081 */
4082 ndmaps = 0;
4083 npages--; /* skip the first global control page */
4084 /* skip higher level control pages above top level covered by map */
4085 npages -= (2 - level);
4086 npages--; /* skip top level's control page */
4087 for (i = level; i >= 0; i--) {
4088 factor =
4089 (i == 2) ? MAXL1PAGES : ((i == 1) ? MAXL0PAGES : 1);
4090 complete = (u32) npages / factor;
f720e3ba
DK
4091 ndmaps += complete * ((i == 2) ? LPERCTL * LPERCTL :
4092 ((i == 1) ? LPERCTL : 1));
1da177e4
LT
4093
4094 /* pages in last/incomplete child */
4095 npages = (u32) npages % factor;
4096 /* skip incomplete child's level control page */
4097 npages--;
4098 }
4099
63f83c9f 4100 /* convert the number of dmaps into the number of blocks
1da177e4
LT
4101 * which can be covered by the dmaps;
4102 */
4103 nblocks = ndmaps << L2BPERDMAP;
4104
4105 return (nblocks);
4106}
This page took 2.346852 seconds and 5 git commands to generate.