Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * bmap.c - NILFS block mapping. |
| 3 | * |
| 4 | * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | * Written by Koji Sato <koji@osrg.net>. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include "nilfs.h" |
| 27 | #include "bmap.h" |
Ryusuke Konishi | 05d0e94 | 2010-07-10 20:52:09 +0900 | [diff] [blame] | 28 | #include "btree.h" |
| 29 | #include "direct.h" |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 30 | #include "btnode.h" |
| 31 | #include "mdt.h" |
| 32 | #include "dat.h" |
| 33 | #include "alloc.h" |
| 34 | |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 35 | struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *bmap) |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 36 | { |
Ryusuke Konishi | 365e215 | 2010-12-27 00:07:30 +0900 | [diff] [blame] | 37 | return NILFS_I_NILFS(bmap->b_inode)->ns_dat; |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 38 | } |
| 39 | |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 40 | static int nilfs_bmap_convert_error(struct nilfs_bmap *bmap, |
| 41 | const char *fname, int err) |
| 42 | { |
| 43 | struct inode *inode = bmap->b_inode; |
| 44 | |
| 45 | if (err == -EINVAL) { |
| 46 | nilfs_error(inode->i_sb, fname, |
| 47 | "broken bmap (inode number=%lu)\n", inode->i_ino); |
| 48 | err = -EIO; |
| 49 | } |
| 50 | return err; |
| 51 | } |
| 52 | |
Ryusuke Konishi | 0f3fe33 | 2009-08-15 02:29:28 +0900 | [diff] [blame] | 53 | /** |
| 54 | * nilfs_bmap_lookup_at_level - find a data block or node block |
| 55 | * @bmap: bmap |
| 56 | * @key: key |
| 57 | * @level: level |
| 58 | * @ptrp: place to store the value associated to @key |
| 59 | * |
| 60 | * Description: nilfs_bmap_lookup_at_level() finds a record whose key |
| 61 | * matches @key in the block at @level of the bmap. |
| 62 | * |
| 63 | * Return Value: On success, 0 is returned and the record associated with @key |
| 64 | * is stored in the place pointed by @ptrp. On error, one of the following |
| 65 | * negative error codes is returned. |
| 66 | * |
| 67 | * %-EIO - I/O error. |
| 68 | * |
| 69 | * %-ENOMEM - Insufficient amount of memory available. |
| 70 | * |
| 71 | * %-ENOENT - A record associated with @key does not exist. |
| 72 | */ |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 73 | int nilfs_bmap_lookup_at_level(struct nilfs_bmap *bmap, __u64 key, int level, |
| 74 | __u64 *ptrp) |
| 75 | { |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 76 | sector_t blocknr; |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 77 | int ret; |
| 78 | |
| 79 | down_read(&bmap->b_sem); |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 80 | ret = bmap->b_ops->bop_lookup(bmap, key, level, ptrp); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 81 | if (ret < 0) { |
| 82 | ret = nilfs_bmap_convert_error(bmap, __func__, ret); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 83 | goto out; |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 84 | } |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 85 | if (NILFS_BMAP_USE_VBN(bmap)) { |
| 86 | ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), *ptrp, |
| 87 | &blocknr); |
| 88 | if (!ret) |
| 89 | *ptrp = blocknr; |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | out: |
| 93 | up_read(&bmap->b_sem); |
| 94 | return ret; |
| 95 | } |
| 96 | |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 97 | int nilfs_bmap_lookup_contig(struct nilfs_bmap *bmap, __u64 key, __u64 *ptrp, |
| 98 | unsigned maxblocks) |
| 99 | { |
| 100 | int ret; |
| 101 | |
| 102 | down_read(&bmap->b_sem); |
| 103 | ret = bmap->b_ops->bop_lookup_contig(bmap, key, ptrp, maxblocks); |
| 104 | up_read(&bmap->b_sem); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 105 | |
| 106 | return nilfs_bmap_convert_error(bmap, __func__, ret); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 107 | } |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 108 | |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 109 | static int nilfs_bmap_do_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr) |
| 110 | { |
| 111 | __u64 keys[NILFS_BMAP_SMALL_HIGH + 1]; |
| 112 | __u64 ptrs[NILFS_BMAP_SMALL_HIGH + 1]; |
| 113 | int ret, n; |
| 114 | |
| 115 | if (bmap->b_ops->bop_check_insert != NULL) { |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 116 | ret = bmap->b_ops->bop_check_insert(bmap, key); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 117 | if (ret > 0) { |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 118 | n = bmap->b_ops->bop_gather_data( |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 119 | bmap, keys, ptrs, NILFS_BMAP_SMALL_HIGH + 1); |
| 120 | if (n < 0) |
| 121 | return n; |
| 122 | ret = nilfs_btree_convert_and_insert( |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 123 | bmap, key, ptr, keys, ptrs, n); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 124 | if (ret == 0) |
| 125 | bmap->b_u.u_flags |= NILFS_BMAP_LARGE; |
| 126 | |
| 127 | return ret; |
| 128 | } else if (ret < 0) |
| 129 | return ret; |
| 130 | } |
| 131 | |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 132 | return bmap->b_ops->bop_insert(bmap, key, ptr); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * nilfs_bmap_insert - insert a new key-record pair into a bmap |
| 137 | * @bmap: bmap |
| 138 | * @key: key |
| 139 | * @rec: record |
| 140 | * |
| 141 | * Description: nilfs_bmap_insert() inserts the new key-record pair specified |
| 142 | * by @key and @rec into @bmap. |
| 143 | * |
| 144 | * Return Value: On success, 0 is returned. On error, one of the following |
| 145 | * negative error codes is returned. |
| 146 | * |
| 147 | * %-EIO - I/O error. |
| 148 | * |
| 149 | * %-ENOMEM - Insufficient amount of memory available. |
| 150 | * |
| 151 | * %-EEXIST - A record associated with @key already exist. |
| 152 | */ |
| 153 | int nilfs_bmap_insert(struct nilfs_bmap *bmap, |
| 154 | unsigned long key, |
| 155 | unsigned long rec) |
| 156 | { |
| 157 | int ret; |
| 158 | |
| 159 | down_write(&bmap->b_sem); |
| 160 | ret = nilfs_bmap_do_insert(bmap, key, rec); |
| 161 | up_write(&bmap->b_sem); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 162 | |
| 163 | return nilfs_bmap_convert_error(bmap, __func__, ret); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key) |
| 167 | { |
| 168 | __u64 keys[NILFS_BMAP_LARGE_LOW + 1]; |
| 169 | __u64 ptrs[NILFS_BMAP_LARGE_LOW + 1]; |
| 170 | int ret, n; |
| 171 | |
| 172 | if (bmap->b_ops->bop_check_delete != NULL) { |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 173 | ret = bmap->b_ops->bop_check_delete(bmap, key); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 174 | if (ret > 0) { |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 175 | n = bmap->b_ops->bop_gather_data( |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 176 | bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1); |
| 177 | if (n < 0) |
| 178 | return n; |
| 179 | ret = nilfs_direct_delete_and_convert( |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 180 | bmap, key, keys, ptrs, n); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 181 | if (ret == 0) |
| 182 | bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE; |
| 183 | |
| 184 | return ret; |
| 185 | } else if (ret < 0) |
| 186 | return ret; |
| 187 | } |
| 188 | |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 189 | return bmap->b_ops->bop_delete(bmap, key); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int nilfs_bmap_last_key(struct nilfs_bmap *bmap, unsigned long *key) |
| 193 | { |
| 194 | __u64 lastkey; |
| 195 | int ret; |
| 196 | |
| 197 | down_read(&bmap->b_sem); |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 198 | ret = bmap->b_ops->bop_last_key(bmap, &lastkey); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 199 | up_read(&bmap->b_sem); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 200 | |
| 201 | if (ret < 0) |
| 202 | ret = nilfs_bmap_convert_error(bmap, __func__, ret); |
| 203 | else |
| 204 | *key = lastkey; |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * nilfs_bmap_delete - delete a key-record pair from a bmap |
| 210 | * @bmap: bmap |
| 211 | * @key: key |
| 212 | * |
| 213 | * Description: nilfs_bmap_delete() deletes the key-record pair specified by |
| 214 | * @key from @bmap. |
| 215 | * |
| 216 | * Return Value: On success, 0 is returned. On error, one of the following |
| 217 | * negative error codes is returned. |
| 218 | * |
| 219 | * %-EIO - I/O error. |
| 220 | * |
| 221 | * %-ENOMEM - Insufficient amount of memory available. |
| 222 | * |
| 223 | * %-ENOENT - A record associated with @key does not exist. |
| 224 | */ |
| 225 | int nilfs_bmap_delete(struct nilfs_bmap *bmap, unsigned long key) |
| 226 | { |
| 227 | int ret; |
| 228 | |
| 229 | down_write(&bmap->b_sem); |
| 230 | ret = nilfs_bmap_do_delete(bmap, key); |
| 231 | up_write(&bmap->b_sem); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 232 | |
| 233 | return nilfs_bmap_convert_error(bmap, __func__, ret); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, unsigned long key) |
| 237 | { |
| 238 | __u64 lastkey; |
| 239 | int ret; |
| 240 | |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 241 | ret = bmap->b_ops->bop_last_key(bmap, &lastkey); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 242 | if (ret < 0) { |
| 243 | if (ret == -ENOENT) |
| 244 | ret = 0; |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | while (key <= lastkey) { |
| 249 | ret = nilfs_bmap_do_delete(bmap, lastkey); |
| 250 | if (ret < 0) |
| 251 | return ret; |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 252 | ret = bmap->b_ops->bop_last_key(bmap, &lastkey); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 253 | if (ret < 0) { |
| 254 | if (ret == -ENOENT) |
| 255 | ret = 0; |
| 256 | return ret; |
| 257 | } |
| 258 | } |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * nilfs_bmap_truncate - truncate a bmap to a specified key |
| 264 | * @bmap: bmap |
| 265 | * @key: key |
| 266 | * |
| 267 | * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are |
| 268 | * greater than or equal to @key from @bmap. |
| 269 | * |
| 270 | * Return Value: On success, 0 is returned. On error, one of the following |
| 271 | * negative error codes is returned. |
| 272 | * |
| 273 | * %-EIO - I/O error. |
| 274 | * |
| 275 | * %-ENOMEM - Insufficient amount of memory available. |
| 276 | */ |
| 277 | int nilfs_bmap_truncate(struct nilfs_bmap *bmap, unsigned long key) |
| 278 | { |
| 279 | int ret; |
| 280 | |
| 281 | down_write(&bmap->b_sem); |
| 282 | ret = nilfs_bmap_do_truncate(bmap, key); |
| 283 | up_write(&bmap->b_sem); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 284 | |
| 285 | return nilfs_bmap_convert_error(bmap, __func__, ret); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * nilfs_bmap_clear - free resources a bmap holds |
| 290 | * @bmap: bmap |
| 291 | * |
| 292 | * Description: nilfs_bmap_clear() frees resources associated with @bmap. |
| 293 | */ |
| 294 | void nilfs_bmap_clear(struct nilfs_bmap *bmap) |
| 295 | { |
| 296 | down_write(&bmap->b_sem); |
| 297 | if (bmap->b_ops->bop_clear != NULL) |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 298 | bmap->b_ops->bop_clear(bmap); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 299 | up_write(&bmap->b_sem); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * nilfs_bmap_propagate - propagate dirty state |
| 304 | * @bmap: bmap |
| 305 | * @bh: buffer head |
| 306 | * |
| 307 | * Description: nilfs_bmap_propagate() marks the buffers that directly or |
| 308 | * indirectly refer to the block specified by @bh dirty. |
| 309 | * |
| 310 | * Return Value: On success, 0 is returned. On error, one of the following |
| 311 | * negative error codes is returned. |
| 312 | * |
| 313 | * %-EIO - I/O error. |
| 314 | * |
| 315 | * %-ENOMEM - Insufficient amount of memory available. |
| 316 | */ |
| 317 | int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh) |
| 318 | { |
| 319 | int ret; |
| 320 | |
| 321 | down_write(&bmap->b_sem); |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 322 | ret = bmap->b_ops->bop_propagate(bmap, bh); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 323 | up_write(&bmap->b_sem); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 324 | |
| 325 | return nilfs_bmap_convert_error(bmap, __func__, ret); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * nilfs_bmap_lookup_dirty_buffers - |
| 330 | * @bmap: bmap |
| 331 | * @listp: pointer to buffer head list |
| 332 | */ |
| 333 | void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *bmap, |
| 334 | struct list_head *listp) |
| 335 | { |
| 336 | if (bmap->b_ops->bop_lookup_dirty_buffers != NULL) |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 337 | bmap->b_ops->bop_lookup_dirty_buffers(bmap, listp); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /** |
| 341 | * nilfs_bmap_assign - assign a new block number to a block |
| 342 | * @bmap: bmap |
| 343 | * @bhp: pointer to buffer head |
| 344 | * @blocknr: block number |
| 345 | * @binfo: block information |
| 346 | * |
| 347 | * Description: nilfs_bmap_assign() assigns the block number @blocknr to the |
| 348 | * buffer specified by @bh. |
| 349 | * |
| 350 | * Return Value: On success, 0 is returned and the buffer head of a newly |
| 351 | * create buffer and the block information associated with the buffer are |
| 352 | * stored in the place pointed by @bh and @binfo, respectively. On error, one |
| 353 | * of the following negative error codes is returned. |
| 354 | * |
| 355 | * %-EIO - I/O error. |
| 356 | * |
| 357 | * %-ENOMEM - Insufficient amount of memory available. |
| 358 | */ |
| 359 | int nilfs_bmap_assign(struct nilfs_bmap *bmap, |
| 360 | struct buffer_head **bh, |
| 361 | unsigned long blocknr, |
| 362 | union nilfs_binfo *binfo) |
| 363 | { |
| 364 | int ret; |
| 365 | |
| 366 | down_write(&bmap->b_sem); |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 367 | ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 368 | up_write(&bmap->b_sem); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 369 | |
| 370 | return nilfs_bmap_convert_error(bmap, __func__, ret); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /** |
| 374 | * nilfs_bmap_mark - mark block dirty |
| 375 | * @bmap: bmap |
| 376 | * @key: key |
| 377 | * @level: level |
| 378 | * |
| 379 | * Description: nilfs_bmap_mark() marks the block specified by @key and @level |
| 380 | * as dirty. |
| 381 | * |
| 382 | * Return Value: On success, 0 is returned. On error, one of the following |
| 383 | * negative error codes is returned. |
| 384 | * |
| 385 | * %-EIO - I/O error. |
| 386 | * |
| 387 | * %-ENOMEM - Insufficient amount of memory available. |
| 388 | */ |
| 389 | int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level) |
| 390 | { |
| 391 | int ret; |
| 392 | |
| 393 | if (bmap->b_ops->bop_mark == NULL) |
| 394 | return 0; |
| 395 | |
| 396 | down_write(&bmap->b_sem); |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 397 | ret = bmap->b_ops->bop_mark(bmap, key, level); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 398 | up_write(&bmap->b_sem); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 399 | |
| 400 | return nilfs_bmap_convert_error(bmap, __func__, ret); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** |
| 404 | * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state |
| 405 | * @bmap: bmap |
| 406 | * |
| 407 | * Description: nilfs_test_and_clear() is the atomic operation to test and |
| 408 | * clear the dirty state of @bmap. |
| 409 | * |
| 410 | * Return Value: 1 is returned if @bmap is dirty, or 0 if clear. |
| 411 | */ |
| 412 | int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *bmap) |
| 413 | { |
| 414 | int ret; |
| 415 | |
| 416 | down_write(&bmap->b_sem); |
| 417 | ret = nilfs_bmap_dirty(bmap); |
| 418 | nilfs_bmap_clear_dirty(bmap); |
| 419 | up_write(&bmap->b_sem); |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | /* |
| 425 | * Internal use only |
| 426 | */ |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 427 | __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *bmap, |
| 428 | const struct buffer_head *bh) |
| 429 | { |
| 430 | struct buffer_head *pbh; |
| 431 | __u64 key; |
| 432 | |
| 433 | key = page_index(bh->b_page) << (PAGE_CACHE_SHIFT - |
| 434 | bmap->b_inode->i_blkbits); |
Jiro SEKIBA | 5ee5814 | 2009-12-06 15:43:56 +0900 | [diff] [blame] | 435 | for (pbh = page_buffers(bh->b_page); pbh != bh; pbh = pbh->b_this_page) |
| 436 | key++; |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 437 | |
| 438 | return key; |
| 439 | } |
| 440 | |
| 441 | __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key) |
| 442 | { |
| 443 | __s64 diff; |
| 444 | |
| 445 | diff = key - bmap->b_last_allocated_key; |
| 446 | if ((nilfs_bmap_keydiff_abs(diff) < NILFS_INODE_BMAP_SIZE) && |
| 447 | (bmap->b_last_allocated_ptr != NILFS_BMAP_INVALID_PTR) && |
| 448 | (bmap->b_last_allocated_ptr + diff > 0)) |
| 449 | return bmap->b_last_allocated_ptr + diff; |
| 450 | else |
| 451 | return NILFS_BMAP_INVALID_PTR; |
| 452 | } |
| 453 | |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 454 | #define NILFS_BMAP_GROUP_DIV 8 |
| 455 | __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap) |
| 456 | { |
| 457 | struct inode *dat = nilfs_bmap_get_dat(bmap); |
| 458 | unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat); |
| 459 | unsigned long group = bmap->b_inode->i_ino / entries_per_group; |
| 460 | |
| 461 | return group * entries_per_group + |
| 462 | (bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) * |
| 463 | (entries_per_group / NILFS_BMAP_GROUP_DIV); |
| 464 | } |
| 465 | |
Ryusuke Konishi | bcb4889 | 2009-03-27 02:51:39 +0900 | [diff] [blame] | 466 | static struct lock_class_key nilfs_bmap_dat_lock_key; |
Ryusuke Konishi | ff54de3 | 2009-06-19 02:53:56 +0900 | [diff] [blame] | 467 | static struct lock_class_key nilfs_bmap_mdt_lock_key; |
Ryusuke Konishi | bcb4889 | 2009-03-27 02:51:39 +0900 | [diff] [blame] | 468 | |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 469 | /** |
| 470 | * nilfs_bmap_read - read a bmap from an inode |
| 471 | * @bmap: bmap |
| 472 | * @raw_inode: on-disk inode |
| 473 | * |
| 474 | * Description: nilfs_bmap_read() initializes the bmap @bmap. |
| 475 | * |
| 476 | * Return Value: On success, 0 is returned. On error, the following negative |
| 477 | * error code is returned. |
| 478 | * |
| 479 | * %-ENOMEM - Insufficient amount of memory available. |
| 480 | */ |
| 481 | int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode) |
| 482 | { |
| 483 | if (raw_inode == NULL) |
| 484 | memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE); |
| 485 | else |
| 486 | memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE); |
| 487 | |
| 488 | init_rwsem(&bmap->b_sem); |
| 489 | bmap->b_state = 0; |
| 490 | bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode; |
| 491 | switch (bmap->b_inode->i_ino) { |
| 492 | case NILFS_DAT_INO: |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 493 | bmap->b_ptr_type = NILFS_BMAP_PTR_P; |
| 494 | bmap->b_last_allocated_key = 0; |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 495 | bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT; |
Ryusuke Konishi | bcb4889 | 2009-03-27 02:51:39 +0900 | [diff] [blame] | 496 | lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 497 | break; |
| 498 | case NILFS_CPFILE_INO: |
| 499 | case NILFS_SUFILE_INO: |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 500 | bmap->b_ptr_type = NILFS_BMAP_PTR_VS; |
| 501 | bmap->b_last_allocated_key = 0; |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 502 | bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR; |
Ryusuke Konishi | ff54de3 | 2009-06-19 02:53:56 +0900 | [diff] [blame] | 503 | lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 504 | break; |
Ryusuke Konishi | ff54de3 | 2009-06-19 02:53:56 +0900 | [diff] [blame] | 505 | case NILFS_IFILE_INO: |
| 506 | lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key); |
| 507 | /* Fall through */ |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 508 | default: |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 509 | bmap->b_ptr_type = NILFS_BMAP_PTR_VM; |
| 510 | bmap->b_last_allocated_key = 0; |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 511 | bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR; |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ? |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 516 | nilfs_btree_init(bmap) : nilfs_direct_init(bmap); |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
| 520 | * nilfs_bmap_write - write back a bmap to an inode |
| 521 | * @bmap: bmap |
| 522 | * @raw_inode: on-disk inode |
| 523 | * |
| 524 | * Description: nilfs_bmap_write() stores @bmap in @raw_inode. |
| 525 | */ |
| 526 | void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode) |
| 527 | { |
| 528 | down_write(&bmap->b_sem); |
| 529 | memcpy(raw_inode->i_bmap, bmap->b_u.u_data, |
| 530 | NILFS_INODE_BMAP_SIZE * sizeof(__le64)); |
| 531 | if (bmap->b_inode->i_ino == NILFS_DAT_INO) |
| 532 | bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT; |
| 533 | |
| 534 | up_write(&bmap->b_sem); |
| 535 | } |
| 536 | |
| 537 | void nilfs_bmap_init_gc(struct nilfs_bmap *bmap) |
| 538 | { |
| 539 | memset(&bmap->b_u, 0, NILFS_BMAP_SIZE); |
| 540 | init_rwsem(&bmap->b_sem); |
| 541 | bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode; |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 542 | bmap->b_ptr_type = NILFS_BMAP_PTR_U; |
Koji Sato | bdb265e | 2009-04-06 19:01:23 -0700 | [diff] [blame] | 543 | bmap->b_last_allocated_key = 0; |
| 544 | bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR; |
| 545 | bmap->b_state = 0; |
| 546 | nilfs_btree_init_gc(bmap); |
| 547 | } |
| 548 | |
Ryusuke Konishi | a8070dd | 2010-08-30 23:42:18 +0900 | [diff] [blame] | 549 | void nilfs_bmap_save(const struct nilfs_bmap *bmap, |
| 550 | struct nilfs_bmap_store *store) |
| 551 | { |
| 552 | memcpy(store->data, bmap->b_u.u_data, sizeof(store->data)); |
| 553 | store->last_allocated_key = bmap->b_last_allocated_key; |
| 554 | store->last_allocated_ptr = bmap->b_last_allocated_ptr; |
| 555 | store->state = bmap->b_state; |
| 556 | } |
| 557 | |
| 558 | void nilfs_bmap_restore(struct nilfs_bmap *bmap, |
| 559 | const struct nilfs_bmap_store *store) |
| 560 | { |
| 561 | memcpy(bmap->b_u.u_data, store->data, sizeof(store->data)); |
| 562 | bmap->b_last_allocated_key = store->last_allocated_key; |
| 563 | bmap->b_last_allocated_ptr = store->last_allocated_ptr; |
| 564 | bmap->b_state = store->state; |
| 565 | } |