blob: 533e24ea3a88d208e8e39bf1a14a53852accb907 [file] [log] [blame]
Ryusuke Konishiae980432018-09-04 15:46:30 -07001// SPDX-License-Identifier: GPL-2.0+
Koji Sato36a580e2009-04-06 19:01:25 -07002/*
3 * direct.c - NILFS direct block pointer.
4 *
5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
6 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -07007 * Written by Koji Sato.
Koji Sato36a580e2009-04-06 19:01:25 -07008 */
9
10#include <linux/errno.h>
11#include "nilfs.h"
12#include "page.h"
13#include "direct.h"
14#include "alloc.h"
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090015#include "dat.h"
Koji Sato36a580e2009-04-06 19:01:25 -070016
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090017static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct)
Koji Sato36a580e2009-04-06 19:01:25 -070018{
19 return (__le64 *)
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090020 ((struct nilfs_direct_node *)direct->b_u.u_data + 1);
Koji Sato36a580e2009-04-06 19:01:25 -070021}
22
23static inline __u64
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090024nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key)
Koji Sato36a580e2009-04-06 19:01:25 -070025{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +090026 return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key));
Koji Sato36a580e2009-04-06 19:01:25 -070027}
28
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090029static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -070030 __u64 key, __u64 ptr)
31{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +090032 *(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr);
Koji Sato36a580e2009-04-06 19:01:25 -070033}
34
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090035static int nilfs_direct_lookup(const struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -070036 __u64 key, int level, __u64 *ptrp)
37{
Koji Sato36a580e2009-04-06 19:01:25 -070038 __u64 ptr;
39
Jiro SEKIBA5ee58142009-12-06 15:43:56 +090040 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 Sato36a580e2009-04-06 19:01:25 -070044 return -ENOENT;
45
Ryusuke Konishi364ec2d2010-07-13 23:33:51 +090046 *ptrp = ptr;
Koji Sato36a580e2009-04-06 19:01:25 -070047 return 0;
48}
49
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090050static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090051 __u64 key, __u64 *ptrp,
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070052 unsigned int maxblocks)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090053{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090054 struct inode *dat = NULL;
55 __u64 ptr, ptr2;
56 sector_t blocknr;
57 int ret, cnt;
58
Jiro SEKIBA5ee58142009-12-06 15:43:56 +090059 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 Konishic3a7abf2009-05-25 02:47:14 +090063 return -ENOENT;
64
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090065 if (NILFS_BMAP_USE_VBN(direct)) {
66 dat = nilfs_bmap_get_dat(direct);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090067 ret = nilfs_dat_translate(dat, ptr, &blocknr);
68 if (ret < 0)
69 return ret;
70 ptr = blocknr;
71 }
72
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070073 maxblocks = min_t(unsigned int, maxblocks,
74 NILFS_DIRECT_KEY_MAX - key + 1);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090075 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 Sato36a580e2009-04-06 19:01:25 -070092static __u64
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090093nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
Koji Sato36a580e2009-04-06 19:01:25 -070094{
95 __u64 ptr;
96
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090097 ptr = nilfs_bmap_find_target_seq(direct, key);
Koji Sato36a580e2009-04-06 19:01:25 -070098 if (ptr != NILFS_BMAP_INVALID_PTR)
99 /* sequential access */
100 return ptr;
Ryusuke Konishi7f001842016-05-23 16:23:42 -0700101
102 /* block group */
103 return nilfs_bmap_find_target_in_group(direct);
Koji Sato36a580e2009-04-06 19:01:25 -0700104}
105
Koji Sato36a580e2009-04-06 19:01:25 -0700106static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
107{
Koji Sato36a580e2009-04-06 19:01:25 -0700108 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900109 struct inode *dat = NULL;
110 struct buffer_head *bh;
Koji Sato36a580e2009-04-06 19:01:25 -0700111 int ret;
112
Koji Sato36a580e2009-04-06 19:01:25 -0700113 if (key > NILFS_DIRECT_KEY_MAX)
114 return -ENOENT;
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900115 if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -0700116 return -EEXIST;
117
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900118 if (NILFS_BMAP_USE_VBN(bmap)) {
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900119 req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900120 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 Sato36a580e2009-04-06 19:01:25 -0700127
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900128 nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900129 nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
Koji Sato36a580e2009-04-06 19:01:25 -0700130
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900131 if (!nilfs_bmap_dirty(bmap))
132 nilfs_bmap_set_dirty(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700133
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900134 if (NILFS_BMAP_USE_VBN(bmap))
Ryusuke Konishidc935be2010-07-10 22:21:54 +0900135 nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900136
Ryusuke Konishibe667372011-03-05 00:19:32 +0900137 nilfs_inode_add_blocks(bmap->b_inode, 1);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900138 }
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900139 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700140}
141
Koji Sato36a580e2009-04-06 19:01:25 -0700142static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
143{
Koji Sato36a580e2009-04-06 19:01:25 -0700144 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900145 struct inode *dat;
Koji Sato36a580e2009-04-06 19:01:25 -0700146 int ret;
147
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900148 if (key > NILFS_DIRECT_KEY_MAX ||
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900149 nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -0700150 return -ENOENT;
151
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900152 dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900153 req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700154
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900155 ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
156 if (!ret) {
157 nilfs_bmap_commit_end_ptr(bmap, &req, dat);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900158 nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
Ryusuke Konishibe667372011-03-05 00:19:32 +0900159 nilfs_inode_sub_blocks(bmap->b_inode, 1);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900160 }
161 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700162}
163
Ryusuke Konishi5b203842015-04-16 12:46:36 -0700164static 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 Konishi10ff8852010-07-10 18:07:04 +0900179static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
Koji Sato36a580e2009-04-06 19:01:25 -0700180{
Koji Sato36a580e2009-04-06 19:01:25 -0700181 __u64 key, lastkey;
182
Koji Sato36a580e2009-04-06 19:01:25 -0700183 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 Sato36a580e2009-04-06 19:01:25 -0700192 *keyp = lastkey;
193
194 return 0;
195}
196
197static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
198{
199 return key > NILFS_DIRECT_KEY_MAX;
200}
201
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900202static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700203 __u64 *keys, __u64 *ptrs, int nitems)
204{
Koji Sato36a580e2009-04-06 19:01:25 -0700205 __u64 key;
206 __u64 ptr;
207 int n;
208
Koji Sato36a580e2009-04-06 19:01:25 -0700209 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
223int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
Ryusuke Konishi30333422009-05-24 00:09:44 +0900224 __u64 key, __u64 *keys, __u64 *ptrs, int n)
Koji Sato36a580e2009-04-06 19:01:25 -0700225{
Koji Sato36a580e2009-04-06 19:01:25 -0700226 __le64 *dptrs;
227 int ret, i, j;
228
229 /* no need to allocate any resource for conversion */
230
231 /* delete */
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700232 ret = bmap->b_ops->bop_delete(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700233 if (ret < 0)
234 return ret;
235
236 /* free resources */
237 if (bmap->b_ops->bop_clear != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700238 bmap->b_ops->bop_clear(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700239
240 /* convert */
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900241 dptrs = nilfs_direct_dptrs(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700242 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
243 if ((j < n) && (i == keys[j])) {
244 dptrs[i] = (i != key) ?
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900245 cpu_to_le64(ptrs[j]) :
Koji Sato36a580e2009-04-06 19:01:25 -0700246 NILFS_BMAP_INVALID_PTR;
247 j++;
248 } else
249 dptrs[i] = NILFS_BMAP_INVALID_PTR;
250 }
251
Ryusuke Konishi30333422009-05-24 00:09:44 +0900252 nilfs_direct_init(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700253 return 0;
254}
255
Ryusuke Konishi583ada42010-07-10 21:37:47 +0900256static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
Koji Sato36a580e2009-04-06 19:01:25 -0700257 struct buffer_head *bh)
258{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900259 struct nilfs_palloc_req oldreq, newreq;
260 struct inode *dat;
261 __u64 key;
262 __u64 ptr;
263 int ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700264
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900265 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 Konishi10ff8852010-07-10 18:07:04 +0900270 ptr = nilfs_direct_get_ptr(bmap, key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900271 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 Konishi10ff8852010-07-10 18:07:04 +0900280 nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900281 } else
282 ret = nilfs_dat_mark_dirty(dat, ptr);
283
284 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700285}
286
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900287static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700288 __u64 key, __u64 ptr,
289 struct buffer_head **bh,
290 sector_t blocknr,
291 union nilfs_binfo *binfo)
292{
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900293 struct inode *dat = nilfs_bmap_get_dat(direct);
Koji Sato36a580e2009-04-06 19:01:25 -0700294 union nilfs_bmap_ptr_req req;
295 int ret;
296
297 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900298 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
299 if (!ret) {
300 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900301 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
302 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900303 }
304 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700305}
306
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900307static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700308 __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 Konishi25b8d7d2010-07-10 16:50:41 +0900315 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato36a580e2009-04-06 19:01:25 -0700316 binfo->bi_dat.bi_level = 0;
317
318 return 0;
319}
320
321static int nilfs_direct_assign(struct nilfs_bmap *bmap,
322 struct buffer_head **bh,
323 sector_t blocknr,
324 union nilfs_binfo *binfo)
325{
Koji Sato36a580e2009-04-06 19:01:25 -0700326 __u64 key;
327 __u64 ptr;
328
Koji Sato36a580e2009-04-06 19:01:25 -0700329 key = nilfs_bmap_data_get_key(bmap, *bh);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700330 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700331 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 Konishi1f5abe72009-04-06 19:01:55 -0700334 return -EINVAL;
335 }
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900336 ptr = nilfs_direct_get_ptr(bmap, key);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700337 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700338 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 Konishi1f5abe72009-04-06 19:01:55 -0700341 return -EINVAL;
342 }
Koji Sato36a580e2009-04-06 19:01:25 -0700343
Ryusuke Konishi355c6b62009-05-24 16:46:37 +0900344 return NILFS_BMAP_USE_VBN(bmap) ?
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900345 nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
346 nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
Koji Sato36a580e2009-04-06 19:01:25 -0700347}
348
349static const struct nilfs_bmap_operations nilfs_direct_ops = {
350 .bop_lookup = nilfs_direct_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900351 .bop_lookup_contig = nilfs_direct_lookup_contig,
Koji Sato36a580e2009-04-06 19:01:25 -0700352 .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 Konishi5b203842015-04-16 12:46:36 -0700363 .bop_seek_key = nilfs_direct_seek_key,
Koji Sato36a580e2009-04-06 19:01:25 -0700364 .bop_last_key = nilfs_direct_last_key,
Ryusuke Konishi5b203842015-04-16 12:46:36 -0700365
Koji Sato36a580e2009-04-06 19:01:25 -0700366 .bop_check_insert = nilfs_direct_check_insert,
367 .bop_check_delete = NULL,
368 .bop_gather_data = nilfs_direct_gather_data,
369};
370
371
Ryusuke Konishi30333422009-05-24 00:09:44 +0900372int nilfs_direct_init(struct nilfs_bmap *bmap)
Koji Sato36a580e2009-04-06 19:01:25 -0700373{
Koji Sato36a580e2009-04-06 19:01:25 -0700374 bmap->b_ops = &nilfs_direct_ops;
Koji Sato36a580e2009-04-06 19:01:25 -0700375 return 0;
376}