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