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