Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * direct.c - NILFS direct block pointer. |
| 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/errno.h> |
| 24 | #include "nilfs.h" |
| 25 | #include "page.h" |
| 26 | #include "direct.h" |
| 27 | #include "alloc.h" |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 28 | #include "dat.h" |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 29 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 30 | static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct) |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 31 | { |
| 32 | return (__le64 *) |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 33 | ((struct nilfs_direct_node *)direct->b_u.u_data + 1); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static inline __u64 |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 37 | nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key) |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 38 | { |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 39 | return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key)); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 42 | static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct, |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 43 | __u64 key, __u64 ptr) |
| 44 | { |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 45 | *(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 48 | static int nilfs_direct_lookup(const struct nilfs_bmap *direct, |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 49 | __u64 key, int level, __u64 *ptrp) |
| 50 | { |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 51 | __u64 ptr; |
| 52 | |
Jiro SEKIBA | 5ee5814 | 2009-12-06 15:43:56 +0900 | [diff] [blame] | 53 | if (key > NILFS_DIRECT_KEY_MAX || level != 1) |
| 54 | return -ENOENT; |
| 55 | ptr = nilfs_direct_get_ptr(direct, key); |
| 56 | if (ptr == NILFS_BMAP_INVALID_PTR) |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 57 | return -ENOENT; |
| 58 | |
Ryusuke Konishi | 364ec2d | 2010-07-13 23:33:51 +0900 | [diff] [blame] | 59 | *ptrp = ptr; |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 63 | static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct, |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 64 | __u64 key, __u64 *ptrp, |
| 65 | unsigned maxblocks) |
| 66 | { |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 67 | struct inode *dat = NULL; |
| 68 | __u64 ptr, ptr2; |
| 69 | sector_t blocknr; |
| 70 | int ret, cnt; |
| 71 | |
Jiro SEKIBA | 5ee5814 | 2009-12-06 15:43:56 +0900 | [diff] [blame] | 72 | if (key > NILFS_DIRECT_KEY_MAX) |
| 73 | return -ENOENT; |
| 74 | ptr = nilfs_direct_get_ptr(direct, key); |
| 75 | if (ptr == NILFS_BMAP_INVALID_PTR) |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 76 | return -ENOENT; |
| 77 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 78 | if (NILFS_BMAP_USE_VBN(direct)) { |
| 79 | dat = nilfs_bmap_get_dat(direct); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 80 | ret = nilfs_dat_translate(dat, ptr, &blocknr); |
| 81 | if (ret < 0) |
| 82 | return ret; |
| 83 | ptr = blocknr; |
| 84 | } |
| 85 | |
| 86 | maxblocks = min_t(unsigned, maxblocks, NILFS_DIRECT_KEY_MAX - key + 1); |
| 87 | for (cnt = 1; cnt < maxblocks && |
| 88 | (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) != |
| 89 | NILFS_BMAP_INVALID_PTR; |
| 90 | cnt++) { |
| 91 | if (dat) { |
| 92 | ret = nilfs_dat_translate(dat, ptr2, &blocknr); |
| 93 | if (ret < 0) |
| 94 | return ret; |
| 95 | ptr2 = blocknr; |
| 96 | } |
| 97 | if (ptr2 != ptr + cnt) |
| 98 | break; |
| 99 | } |
| 100 | *ptrp = ptr; |
| 101 | return cnt; |
| 102 | } |
| 103 | |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 104 | static __u64 |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 105 | nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key) |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 106 | { |
| 107 | __u64 ptr; |
| 108 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 109 | ptr = nilfs_bmap_find_target_seq(direct, key); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 110 | if (ptr != NILFS_BMAP_INVALID_PTR) |
| 111 | /* sequential access */ |
| 112 | return ptr; |
| 113 | else |
| 114 | /* block group */ |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 115 | return nilfs_bmap_find_target_in_group(direct); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 118 | static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr) |
| 119 | { |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 120 | union nilfs_bmap_ptr_req req; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 121 | struct inode *dat = NULL; |
| 122 | struct buffer_head *bh; |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 123 | int ret; |
| 124 | |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 125 | if (key > NILFS_DIRECT_KEY_MAX) |
| 126 | return -ENOENT; |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 127 | if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR) |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 128 | return -EEXIST; |
| 129 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 130 | if (NILFS_BMAP_USE_VBN(bmap)) { |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 131 | req.bpr_ptr = nilfs_direct_find_target_v(bmap, key); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 132 | dat = nilfs_bmap_get_dat(bmap); |
| 133 | } |
| 134 | ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat); |
| 135 | if (!ret) { |
| 136 | /* ptr must be a pointer to a buffer head. */ |
| 137 | bh = (struct buffer_head *)((unsigned long)ptr); |
| 138 | set_buffer_nilfs_volatile(bh); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 139 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 140 | nilfs_bmap_commit_alloc_ptr(bmap, &req, dat); |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 141 | nilfs_direct_set_ptr(bmap, key, req.bpr_ptr); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 142 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 143 | if (!nilfs_bmap_dirty(bmap)) |
| 144 | nilfs_bmap_set_dirty(bmap); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 145 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 146 | if (NILFS_BMAP_USE_VBN(bmap)) |
Ryusuke Konishi | dc935be | 2010-07-10 22:21:54 +0900 | [diff] [blame] | 147 | nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 148 | |
Ryusuke Konishi | be66737 | 2011-03-05 00:19:32 +0900 | [diff] [blame] | 149 | nilfs_inode_add_blocks(bmap->b_inode, 1); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 150 | } |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 151 | return ret; |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 154 | static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key) |
| 155 | { |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 156 | union nilfs_bmap_ptr_req req; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 157 | struct inode *dat; |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 158 | int ret; |
| 159 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 160 | if (key > NILFS_DIRECT_KEY_MAX || |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 161 | nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR) |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 162 | return -ENOENT; |
| 163 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 164 | dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL; |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 165 | req.bpr_ptr = nilfs_direct_get_ptr(bmap, key); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 166 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 167 | ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat); |
| 168 | if (!ret) { |
| 169 | nilfs_bmap_commit_end_ptr(bmap, &req, dat); |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 170 | nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR); |
Ryusuke Konishi | be66737 | 2011-03-05 00:19:32 +0900 | [diff] [blame] | 171 | nilfs_inode_sub_blocks(bmap->b_inode, 1); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 172 | } |
| 173 | return ret; |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Ryusuke Konishi | 5b20384 | 2015-04-16 12:46:36 -0700 | [diff] [blame] | 176 | static int nilfs_direct_seek_key(const struct nilfs_bmap *direct, __u64 start, |
| 177 | __u64 *keyp) |
| 178 | { |
| 179 | __u64 key; |
| 180 | |
| 181 | for (key = start; key <= NILFS_DIRECT_KEY_MAX; key++) { |
| 182 | if (nilfs_direct_get_ptr(direct, key) != |
| 183 | NILFS_BMAP_INVALID_PTR) { |
| 184 | *keyp = key; |
| 185 | return 0; |
| 186 | } |
| 187 | } |
| 188 | return -ENOENT; |
| 189 | } |
| 190 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 191 | static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp) |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 192 | { |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 193 | __u64 key, lastkey; |
| 194 | |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 195 | lastkey = NILFS_DIRECT_KEY_MAX + 1; |
| 196 | for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++) |
| 197 | if (nilfs_direct_get_ptr(direct, key) != |
| 198 | NILFS_BMAP_INVALID_PTR) |
| 199 | lastkey = key; |
| 200 | |
| 201 | if (lastkey == NILFS_DIRECT_KEY_MAX + 1) |
| 202 | return -ENOENT; |
| 203 | |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 204 | *keyp = lastkey; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key) |
| 210 | { |
| 211 | return key > NILFS_DIRECT_KEY_MAX; |
| 212 | } |
| 213 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 214 | static int nilfs_direct_gather_data(struct nilfs_bmap *direct, |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 215 | __u64 *keys, __u64 *ptrs, int nitems) |
| 216 | { |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 217 | __u64 key; |
| 218 | __u64 ptr; |
| 219 | int n; |
| 220 | |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 221 | if (nitems > NILFS_DIRECT_NBLOCKS) |
| 222 | nitems = NILFS_DIRECT_NBLOCKS; |
| 223 | n = 0; |
| 224 | for (key = 0; key < nitems; key++) { |
| 225 | ptr = nilfs_direct_get_ptr(direct, key); |
| 226 | if (ptr != NILFS_BMAP_INVALID_PTR) { |
| 227 | keys[n] = key; |
| 228 | ptrs[n] = ptr; |
| 229 | n++; |
| 230 | } |
| 231 | } |
| 232 | return n; |
| 233 | } |
| 234 | |
| 235 | int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap, |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 236 | __u64 key, __u64 *keys, __u64 *ptrs, int n) |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 237 | { |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 238 | __le64 *dptrs; |
| 239 | int ret, i, j; |
| 240 | |
| 241 | /* no need to allocate any resource for conversion */ |
| 242 | |
| 243 | /* delete */ |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 244 | ret = bmap->b_ops->bop_delete(bmap, key); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 245 | if (ret < 0) |
| 246 | return ret; |
| 247 | |
| 248 | /* free resources */ |
| 249 | if (bmap->b_ops->bop_clear != NULL) |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 250 | bmap->b_ops->bop_clear(bmap); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 251 | |
| 252 | /* convert */ |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 253 | dptrs = nilfs_direct_dptrs(bmap); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 254 | for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) { |
| 255 | if ((j < n) && (i == keys[j])) { |
| 256 | dptrs[i] = (i != key) ? |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 257 | cpu_to_le64(ptrs[j]) : |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 258 | NILFS_BMAP_INVALID_PTR; |
| 259 | j++; |
| 260 | } else |
| 261 | dptrs[i] = NILFS_BMAP_INVALID_PTR; |
| 262 | } |
| 263 | |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 264 | nilfs_direct_init(bmap); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Ryusuke Konishi | 583ada4 | 2010-07-10 21:37:47 +0900 | [diff] [blame] | 268 | static int nilfs_direct_propagate(struct nilfs_bmap *bmap, |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 269 | struct buffer_head *bh) |
| 270 | { |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 271 | struct nilfs_palloc_req oldreq, newreq; |
| 272 | struct inode *dat; |
| 273 | __u64 key; |
| 274 | __u64 ptr; |
| 275 | int ret; |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 276 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 277 | if (!NILFS_BMAP_USE_VBN(bmap)) |
| 278 | return 0; |
| 279 | |
| 280 | dat = nilfs_bmap_get_dat(bmap); |
| 281 | key = nilfs_bmap_data_get_key(bmap, bh); |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 282 | ptr = nilfs_direct_get_ptr(bmap, key); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 283 | if (!buffer_nilfs_volatile(bh)) { |
| 284 | oldreq.pr_entry_nr = ptr; |
| 285 | newreq.pr_entry_nr = ptr; |
| 286 | ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq); |
| 287 | if (ret < 0) |
| 288 | return ret; |
| 289 | nilfs_dat_commit_update(dat, &oldreq, &newreq, |
| 290 | bmap->b_ptr_type == NILFS_BMAP_PTR_VS); |
| 291 | set_buffer_nilfs_volatile(bh); |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 292 | nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 293 | } else |
| 294 | ret = nilfs_dat_mark_dirty(dat, ptr); |
| 295 | |
| 296 | return ret; |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 299 | static int nilfs_direct_assign_v(struct nilfs_bmap *direct, |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 300 | __u64 key, __u64 ptr, |
| 301 | struct buffer_head **bh, |
| 302 | sector_t blocknr, |
| 303 | union nilfs_binfo *binfo) |
| 304 | { |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 305 | struct inode *dat = nilfs_bmap_get_dat(direct); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 306 | union nilfs_bmap_ptr_req req; |
| 307 | int ret; |
| 308 | |
| 309 | req.bpr_ptr = ptr; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 310 | ret = nilfs_dat_prepare_start(dat, &req.bpr_req); |
| 311 | if (!ret) { |
| 312 | nilfs_dat_commit_start(dat, &req.bpr_req, blocknr); |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 313 | binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr); |
| 314 | binfo->bi_v.bi_blkoff = cpu_to_le64(key); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 315 | } |
| 316 | return ret; |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 319 | static int nilfs_direct_assign_p(struct nilfs_bmap *direct, |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 320 | __u64 key, __u64 ptr, |
| 321 | struct buffer_head **bh, |
| 322 | sector_t blocknr, |
| 323 | union nilfs_binfo *binfo) |
| 324 | { |
| 325 | nilfs_direct_set_ptr(direct, key, blocknr); |
| 326 | |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 327 | binfo->bi_dat.bi_blkoff = cpu_to_le64(key); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 328 | binfo->bi_dat.bi_level = 0; |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int nilfs_direct_assign(struct nilfs_bmap *bmap, |
| 334 | struct buffer_head **bh, |
| 335 | sector_t blocknr, |
| 336 | union nilfs_binfo *binfo) |
| 337 | { |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 338 | __u64 key; |
| 339 | __u64 ptr; |
| 340 | |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 341 | key = nilfs_bmap_data_get_key(bmap, *bh); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 342 | if (unlikely(key > NILFS_DIRECT_KEY_MAX)) { |
| 343 | printk(KERN_CRIT "%s: invalid key: %llu\n", __func__, |
| 344 | (unsigned long long)key); |
| 345 | return -EINVAL; |
| 346 | } |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 347 | ptr = nilfs_direct_get_ptr(bmap, key); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 348 | if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) { |
| 349 | printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__, |
| 350 | (unsigned long long)ptr); |
| 351 | return -EINVAL; |
| 352 | } |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 353 | |
Ryusuke Konishi | 355c6b6 | 2009-05-24 16:46:37 +0900 | [diff] [blame] | 354 | return NILFS_BMAP_USE_VBN(bmap) ? |
Ryusuke Konishi | 10ff885 | 2010-07-10 18:07:04 +0900 | [diff] [blame] | 355 | nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) : |
| 356 | nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo); |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static const struct nilfs_bmap_operations nilfs_direct_ops = { |
| 360 | .bop_lookup = nilfs_direct_lookup, |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 361 | .bop_lookup_contig = nilfs_direct_lookup_contig, |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 362 | .bop_insert = nilfs_direct_insert, |
| 363 | .bop_delete = nilfs_direct_delete, |
| 364 | .bop_clear = NULL, |
| 365 | |
| 366 | .bop_propagate = nilfs_direct_propagate, |
| 367 | |
| 368 | .bop_lookup_dirty_buffers = NULL, |
| 369 | |
| 370 | .bop_assign = nilfs_direct_assign, |
| 371 | .bop_mark = NULL, |
| 372 | |
Ryusuke Konishi | 5b20384 | 2015-04-16 12:46:36 -0700 | [diff] [blame] | 373 | .bop_seek_key = nilfs_direct_seek_key, |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 374 | .bop_last_key = nilfs_direct_last_key, |
Ryusuke Konishi | 5b20384 | 2015-04-16 12:46:36 -0700 | [diff] [blame] | 375 | |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 376 | .bop_check_insert = nilfs_direct_check_insert, |
| 377 | .bop_check_delete = NULL, |
| 378 | .bop_gather_data = nilfs_direct_gather_data, |
| 379 | }; |
| 380 | |
| 381 | |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 382 | int nilfs_direct_init(struct nilfs_bmap *bmap) |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 383 | { |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 384 | bmap->b_ops = &nilfs_direct_ops; |
Koji Sato | 36a580e | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 385 | return 0; |
| 386 | } |