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