blob: 96e3ed0d9652b67de00717f0e9a9042b3106620d [file] [log] [blame]
Koji Sato36a580e2009-04-06 19:01:25 -07001/*
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 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -070016 * Written by Koji Sato.
Koji Sato36a580e2009-04-06 19:01:25 -070017 */
18
19#include <linux/errno.h>
20#include "nilfs.h"
21#include "page.h"
22#include "direct.h"
23#include "alloc.h"
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090024#include "dat.h"
Koji Sato36a580e2009-04-06 19:01:25 -070025
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090026static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct)
Koji Sato36a580e2009-04-06 19:01:25 -070027{
28 return (__le64 *)
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090029 ((struct nilfs_direct_node *)direct->b_u.u_data + 1);
Koji Sato36a580e2009-04-06 19:01:25 -070030}
31
32static inline __u64
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090033nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key)
Koji Sato36a580e2009-04-06 19:01:25 -070034{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +090035 return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key));
Koji Sato36a580e2009-04-06 19:01:25 -070036}
37
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090038static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -070039 __u64 key, __u64 ptr)
40{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +090041 *(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr);
Koji Sato36a580e2009-04-06 19:01:25 -070042}
43
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090044static int nilfs_direct_lookup(const struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -070045 __u64 key, int level, __u64 *ptrp)
46{
Koji Sato36a580e2009-04-06 19:01:25 -070047 __u64 ptr;
48
Jiro SEKIBA5ee58142009-12-06 15:43:56 +090049 if (key > NILFS_DIRECT_KEY_MAX || level != 1)
50 return -ENOENT;
51 ptr = nilfs_direct_get_ptr(direct, key);
52 if (ptr == NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -070053 return -ENOENT;
54
Ryusuke Konishi364ec2d2010-07-13 23:33:51 +090055 *ptrp = ptr;
Koji Sato36a580e2009-04-06 19:01:25 -070056 return 0;
57}
58
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090059static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090060 __u64 key, __u64 *ptrp,
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070061 unsigned int maxblocks)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090062{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090063 struct inode *dat = NULL;
64 __u64 ptr, ptr2;
65 sector_t blocknr;
66 int ret, cnt;
67
Jiro SEKIBA5ee58142009-12-06 15:43:56 +090068 if (key > NILFS_DIRECT_KEY_MAX)
69 return -ENOENT;
70 ptr = nilfs_direct_get_ptr(direct, key);
71 if (ptr == NILFS_BMAP_INVALID_PTR)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090072 return -ENOENT;
73
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090074 if (NILFS_BMAP_USE_VBN(direct)) {
75 dat = nilfs_bmap_get_dat(direct);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090076 ret = nilfs_dat_translate(dat, ptr, &blocknr);
77 if (ret < 0)
78 return ret;
79 ptr = blocknr;
80 }
81
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070082 maxblocks = min_t(unsigned int, maxblocks,
83 NILFS_DIRECT_KEY_MAX - key + 1);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090084 for (cnt = 1; cnt < maxblocks &&
85 (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) !=
86 NILFS_BMAP_INVALID_PTR;
87 cnt++) {
88 if (dat) {
89 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
90 if (ret < 0)
91 return ret;
92 ptr2 = blocknr;
93 }
94 if (ptr2 != ptr + cnt)
95 break;
96 }
97 *ptrp = ptr;
98 return cnt;
99}
100
Koji Sato36a580e2009-04-06 19:01:25 -0700101static __u64
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900102nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
Koji Sato36a580e2009-04-06 19:01:25 -0700103{
104 __u64 ptr;
105
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900106 ptr = nilfs_bmap_find_target_seq(direct, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700107 if (ptr != NILFS_BMAP_INVALID_PTR)
108 /* sequential access */
109 return ptr;
Ryusuke Konishi7f001842016-05-23 16:23:42 -0700110
111 /* block group */
112 return nilfs_bmap_find_target_in_group(direct);
Koji Sato36a580e2009-04-06 19:01:25 -0700113}
114
Koji Sato36a580e2009-04-06 19:01:25 -0700115static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
116{
Koji Sato36a580e2009-04-06 19:01:25 -0700117 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900118 struct inode *dat = NULL;
119 struct buffer_head *bh;
Koji Sato36a580e2009-04-06 19:01:25 -0700120 int ret;
121
Koji Sato36a580e2009-04-06 19:01:25 -0700122 if (key > NILFS_DIRECT_KEY_MAX)
123 return -ENOENT;
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900124 if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -0700125 return -EEXIST;
126
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900127 if (NILFS_BMAP_USE_VBN(bmap)) {
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900128 req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900129 dat = nilfs_bmap_get_dat(bmap);
130 }
131 ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
132 if (!ret) {
133 /* ptr must be a pointer to a buffer head. */
134 bh = (struct buffer_head *)((unsigned long)ptr);
135 set_buffer_nilfs_volatile(bh);
Koji Sato36a580e2009-04-06 19:01:25 -0700136
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900137 nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900138 nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
Koji Sato36a580e2009-04-06 19:01:25 -0700139
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900140 if (!nilfs_bmap_dirty(bmap))
141 nilfs_bmap_set_dirty(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700142
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900143 if (NILFS_BMAP_USE_VBN(bmap))
Ryusuke Konishidc935be2010-07-10 22:21:54 +0900144 nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900145
Ryusuke Konishibe667372011-03-05 00:19:32 +0900146 nilfs_inode_add_blocks(bmap->b_inode, 1);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900147 }
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900148 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700149}
150
Koji Sato36a580e2009-04-06 19:01:25 -0700151static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
152{
Koji Sato36a580e2009-04-06 19:01:25 -0700153 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900154 struct inode *dat;
Koji Sato36a580e2009-04-06 19:01:25 -0700155 int ret;
156
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900157 if (key > NILFS_DIRECT_KEY_MAX ||
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900158 nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -0700159 return -ENOENT;
160
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900161 dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900162 req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700163
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900164 ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
165 if (!ret) {
166 nilfs_bmap_commit_end_ptr(bmap, &req, dat);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900167 nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
Ryusuke Konishibe667372011-03-05 00:19:32 +0900168 nilfs_inode_sub_blocks(bmap->b_inode, 1);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900169 }
170 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700171}
172
Ryusuke Konishi5b203842015-04-16 12:46:36 -0700173static int nilfs_direct_seek_key(const struct nilfs_bmap *direct, __u64 start,
174 __u64 *keyp)
175{
176 __u64 key;
177
178 for (key = start; key <= NILFS_DIRECT_KEY_MAX; key++) {
179 if (nilfs_direct_get_ptr(direct, key) !=
180 NILFS_BMAP_INVALID_PTR) {
181 *keyp = key;
182 return 0;
183 }
184 }
185 return -ENOENT;
186}
187
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900188static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
Koji Sato36a580e2009-04-06 19:01:25 -0700189{
Koji Sato36a580e2009-04-06 19:01:25 -0700190 __u64 key, lastkey;
191
Koji Sato36a580e2009-04-06 19:01:25 -0700192 lastkey = NILFS_DIRECT_KEY_MAX + 1;
193 for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
194 if (nilfs_direct_get_ptr(direct, key) !=
195 NILFS_BMAP_INVALID_PTR)
196 lastkey = key;
197
198 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
199 return -ENOENT;
200
Koji Sato36a580e2009-04-06 19:01:25 -0700201 *keyp = lastkey;
202
203 return 0;
204}
205
206static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
207{
208 return key > NILFS_DIRECT_KEY_MAX;
209}
210
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900211static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700212 __u64 *keys, __u64 *ptrs, int nitems)
213{
Koji Sato36a580e2009-04-06 19:01:25 -0700214 __u64 key;
215 __u64 ptr;
216 int n;
217
Koji Sato36a580e2009-04-06 19:01:25 -0700218 if (nitems > NILFS_DIRECT_NBLOCKS)
219 nitems = NILFS_DIRECT_NBLOCKS;
220 n = 0;
221 for (key = 0; key < nitems; key++) {
222 ptr = nilfs_direct_get_ptr(direct, key);
223 if (ptr != NILFS_BMAP_INVALID_PTR) {
224 keys[n] = key;
225 ptrs[n] = ptr;
226 n++;
227 }
228 }
229 return n;
230}
231
232int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
Ryusuke Konishi30333422009-05-24 00:09:44 +0900233 __u64 key, __u64 *keys, __u64 *ptrs, int n)
Koji Sato36a580e2009-04-06 19:01:25 -0700234{
Koji Sato36a580e2009-04-06 19:01:25 -0700235 __le64 *dptrs;
236 int ret, i, j;
237
238 /* no need to allocate any resource for conversion */
239
240 /* delete */
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700241 ret = bmap->b_ops->bop_delete(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700242 if (ret < 0)
243 return ret;
244
245 /* free resources */
246 if (bmap->b_ops->bop_clear != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700247 bmap->b_ops->bop_clear(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700248
249 /* convert */
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900250 dptrs = nilfs_direct_dptrs(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700251 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
252 if ((j < n) && (i == keys[j])) {
253 dptrs[i] = (i != key) ?
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900254 cpu_to_le64(ptrs[j]) :
Koji Sato36a580e2009-04-06 19:01:25 -0700255 NILFS_BMAP_INVALID_PTR;
256 j++;
257 } else
258 dptrs[i] = NILFS_BMAP_INVALID_PTR;
259 }
260
Ryusuke Konishi30333422009-05-24 00:09:44 +0900261 nilfs_direct_init(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700262 return 0;
263}
264
Ryusuke Konishi583ada42010-07-10 21:37:47 +0900265static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
Koji Sato36a580e2009-04-06 19:01:25 -0700266 struct buffer_head *bh)
267{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900268 struct nilfs_palloc_req oldreq, newreq;
269 struct inode *dat;
270 __u64 key;
271 __u64 ptr;
272 int ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700273
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900274 if (!NILFS_BMAP_USE_VBN(bmap))
275 return 0;
276
277 dat = nilfs_bmap_get_dat(bmap);
278 key = nilfs_bmap_data_get_key(bmap, bh);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900279 ptr = nilfs_direct_get_ptr(bmap, key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900280 if (!buffer_nilfs_volatile(bh)) {
281 oldreq.pr_entry_nr = ptr;
282 newreq.pr_entry_nr = ptr;
283 ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
284 if (ret < 0)
285 return ret;
286 nilfs_dat_commit_update(dat, &oldreq, &newreq,
287 bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
288 set_buffer_nilfs_volatile(bh);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900289 nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900290 } else
291 ret = nilfs_dat_mark_dirty(dat, ptr);
292
293 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700294}
295
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900296static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700297 __u64 key, __u64 ptr,
298 struct buffer_head **bh,
299 sector_t blocknr,
300 union nilfs_binfo *binfo)
301{
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900302 struct inode *dat = nilfs_bmap_get_dat(direct);
Koji Sato36a580e2009-04-06 19:01:25 -0700303 union nilfs_bmap_ptr_req req;
304 int ret;
305
306 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900307 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
308 if (!ret) {
309 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900310 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
311 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900312 }
313 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700314}
315
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900316static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700317 __u64 key, __u64 ptr,
318 struct buffer_head **bh,
319 sector_t blocknr,
320 union nilfs_binfo *binfo)
321{
322 nilfs_direct_set_ptr(direct, key, blocknr);
323
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900324 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato36a580e2009-04-06 19:01:25 -0700325 binfo->bi_dat.bi_level = 0;
326
327 return 0;
328}
329
330static int nilfs_direct_assign(struct nilfs_bmap *bmap,
331 struct buffer_head **bh,
332 sector_t blocknr,
333 union nilfs_binfo *binfo)
334{
Koji Sato36a580e2009-04-06 19:01:25 -0700335 __u64 key;
336 __u64 ptr;
337
Koji Sato36a580e2009-04-06 19:01:25 -0700338 key = nilfs_bmap_data_get_key(bmap, *bh);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700339 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700340 nilfs_msg(bmap->b_inode->i_sb, KERN_CRIT,
341 "%s (ino=%lu): invalid key: %llu", __func__,
342 bmap->b_inode->i_ino, (unsigned long long)key);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700343 return -EINVAL;
344 }
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900345 ptr = nilfs_direct_get_ptr(bmap, key);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700346 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700347 nilfs_msg(bmap->b_inode->i_sb, KERN_CRIT,
348 "%s (ino=%lu): invalid pointer: %llu", __func__,
349 bmap->b_inode->i_ino, (unsigned long long)ptr);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700350 return -EINVAL;
351 }
Koji Sato36a580e2009-04-06 19:01:25 -0700352
Ryusuke Konishi355c6b62009-05-24 16:46:37 +0900353 return NILFS_BMAP_USE_VBN(bmap) ?
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900354 nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
355 nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
Koji Sato36a580e2009-04-06 19:01:25 -0700356}
357
358static const struct nilfs_bmap_operations nilfs_direct_ops = {
359 .bop_lookup = nilfs_direct_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900360 .bop_lookup_contig = nilfs_direct_lookup_contig,
Koji Sato36a580e2009-04-06 19:01:25 -0700361 .bop_insert = nilfs_direct_insert,
362 .bop_delete = nilfs_direct_delete,
363 .bop_clear = NULL,
364
365 .bop_propagate = nilfs_direct_propagate,
366
367 .bop_lookup_dirty_buffers = NULL,
368
369 .bop_assign = nilfs_direct_assign,
370 .bop_mark = NULL,
371
Ryusuke Konishi5b203842015-04-16 12:46:36 -0700372 .bop_seek_key = nilfs_direct_seek_key,
Koji Sato36a580e2009-04-06 19:01:25 -0700373 .bop_last_key = nilfs_direct_last_key,
Ryusuke Konishi5b203842015-04-16 12:46:36 -0700374
Koji Sato36a580e2009-04-06 19:01:25 -0700375 .bop_check_insert = nilfs_direct_check_insert,
376 .bop_check_delete = NULL,
377 .bop_gather_data = nilfs_direct_gather_data,
378};
379
380
Ryusuke Konishi30333422009-05-24 00:09:44 +0900381int nilfs_direct_init(struct nilfs_bmap *bmap)
Koji Sato36a580e2009-04-06 19:01:25 -0700382{
Koji Sato36a580e2009-04-06 19:01:25 -0700383 bmap->b_ops = &nilfs_direct_ops;
Koji Sato36a580e2009-04-06 19:01:25 -0700384 return 0;
385}