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