GNU Linux-libre 4.14.332-gnu1
[releases.git] / fs / nilfs2 / btnode.c
1 /*
2  * btnode.c - NILFS B-tree node cache
3  *
4  * Copyright (C) 2005-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  * Originally written by Seiji Kihara.
17  * Fully revised by Ryusuke Konishi for stabilization and simplification.
18  *
19  */
20
21 #include <linux/types.h>
22 #include <linux/buffer_head.h>
23 #include <linux/mm.h>
24 #include <linux/backing-dev.h>
25 #include <linux/gfp.h>
26 #include "nilfs.h"
27 #include "mdt.h"
28 #include "dat.h"
29 #include "page.h"
30 #include "btnode.h"
31
32
33 /**
34  * nilfs_init_btnc_inode - initialize B-tree node cache inode
35  * @btnc_inode: inode to be initialized
36  *
37  * nilfs_init_btnc_inode() sets up an inode for B-tree node cache.
38  */
39 void nilfs_init_btnc_inode(struct inode *btnc_inode)
40 {
41         struct nilfs_inode_info *ii = NILFS_I(btnc_inode);
42
43         btnc_inode->i_mode = S_IFREG;
44         ii->i_flags = 0;
45         memset(&ii->i_bmap_data, 0, sizeof(struct nilfs_bmap));
46         mapping_set_gfp_mask(btnc_inode->i_mapping, GFP_NOFS);
47 }
48
49 void nilfs_btnode_cache_clear(struct address_space *btnc)
50 {
51         invalidate_mapping_pages(btnc, 0, -1);
52         truncate_inode_pages(btnc, 0);
53 }
54
55 struct buffer_head *
56 nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
57 {
58         struct inode *inode = btnc->host;
59         struct buffer_head *bh;
60
61         bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
62         if (unlikely(!bh))
63                 return NULL;
64
65         if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
66                      buffer_dirty(bh))) {
67                 brelse(bh);
68                 BUG();
69         }
70         memset(bh->b_data, 0, i_blocksize(inode));
71         bh->b_bdev = inode->i_sb->s_bdev;
72         bh->b_blocknr = blocknr;
73         set_buffer_mapped(bh);
74         set_buffer_uptodate(bh);
75
76         unlock_page(bh->b_page);
77         put_page(bh->b_page);
78         return bh;
79 }
80
81 int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
82                               sector_t pblocknr, int mode, int mode_flags,
83                               struct buffer_head **pbh, sector_t *submit_ptr)
84 {
85         struct buffer_head *bh;
86         struct inode *inode = btnc->host;
87         struct page *page;
88         int err;
89
90         bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
91         if (unlikely(!bh))
92                 return -ENOMEM;
93
94         err = -EEXIST; /* internal code */
95         page = bh->b_page;
96
97         if (buffer_uptodate(bh) || buffer_dirty(bh))
98                 goto found;
99
100         if (pblocknr == 0) {
101                 pblocknr = blocknr;
102                 if (inode->i_ino != NILFS_DAT_INO) {
103                         struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
104
105                         /* blocknr is a virtual block number */
106                         err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
107                                                   &pblocknr);
108                         if (unlikely(err)) {
109                                 brelse(bh);
110                                 goto out_locked;
111                         }
112                 }
113         }
114
115         if (mode_flags & REQ_RAHEAD) {
116                 if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
117                         err = -EBUSY; /* internal code */
118                         brelse(bh);
119                         goto out_locked;
120                 }
121         } else { /* mode == READ */
122                 lock_buffer(bh);
123         }
124         if (buffer_uptodate(bh)) {
125                 unlock_buffer(bh);
126                 err = -EEXIST; /* internal code */
127                 goto found;
128         }
129         set_buffer_mapped(bh);
130         bh->b_bdev = inode->i_sb->s_bdev;
131         bh->b_blocknr = pblocknr; /* set block address for read */
132         bh->b_end_io = end_buffer_read_sync;
133         get_bh(bh);
134         submit_bh(mode, mode_flags, bh);
135         bh->b_blocknr = blocknr; /* set back to the given block address */
136         *submit_ptr = pblocknr;
137         err = 0;
138 found:
139         *pbh = bh;
140
141 out_locked:
142         unlock_page(page);
143         put_page(page);
144         return err;
145 }
146
147 /**
148  * nilfs_btnode_delete - delete B-tree node buffer
149  * @bh: buffer to be deleted
150  *
151  * nilfs_btnode_delete() invalidates the specified buffer and delete the page
152  * including the buffer if the page gets unbusy.
153  */
154 void nilfs_btnode_delete(struct buffer_head *bh)
155 {
156         struct address_space *mapping;
157         struct page *page = bh->b_page;
158         pgoff_t index = page_index(page);
159         int still_dirty;
160
161         get_page(page);
162         lock_page(page);
163         wait_on_page_writeback(page);
164
165         nilfs_forget_buffer(bh);
166         still_dirty = PageDirty(page);
167         mapping = page->mapping;
168         unlock_page(page);
169         put_page(page);
170
171         if (!still_dirty && mapping)
172                 invalidate_inode_pages2_range(mapping, index, index);
173 }
174
175 /**
176  * nilfs_btnode_prepare_change_key
177  *  prepare to move contents of the block for old key to one of new key.
178  *  the old buffer will not be removed, but might be reused for new buffer.
179  *  it might return -ENOMEM because of memory allocation errors,
180  *  and might return -EIO because of disk read errors.
181  */
182 int nilfs_btnode_prepare_change_key(struct address_space *btnc,
183                                     struct nilfs_btnode_chkey_ctxt *ctxt)
184 {
185         struct buffer_head *obh, *nbh;
186         struct inode *inode = btnc->host;
187         __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
188         int err;
189
190         if (oldkey == newkey)
191                 return 0;
192
193         obh = ctxt->bh;
194         ctxt->newbh = NULL;
195
196         if (inode->i_blkbits == PAGE_SHIFT) {
197                 lock_page(obh->b_page);
198                 /*
199                  * We cannot call radix_tree_preload for the kernels older
200                  * than 2.6.23, because it is not exported for modules.
201                  */
202 retry:
203                 err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
204                 if (err)
205                         goto failed_unlock;
206                 /* BUG_ON(oldkey != obh->b_page->index); */
207                 if (unlikely(oldkey != obh->b_page->index))
208                         NILFS_PAGE_BUG(obh->b_page,
209                                        "invalid oldkey %lld (newkey=%lld)",
210                                        (unsigned long long)oldkey,
211                                        (unsigned long long)newkey);
212
213                 spin_lock_irq(&btnc->tree_lock);
214                 err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page);
215                 spin_unlock_irq(&btnc->tree_lock);
216                 /*
217                  * Note: page->index will not change to newkey until
218                  * nilfs_btnode_commit_change_key() will be called.
219                  * To protect the page in intermediate state, the page lock
220                  * is held.
221                  */
222                 radix_tree_preload_end();
223                 if (!err)
224                         return 0;
225                 else if (err != -EEXIST)
226                         goto failed_unlock;
227
228                 err = invalidate_inode_pages2_range(btnc, newkey, newkey);
229                 if (!err)
230                         goto retry;
231                 /* fallback to copy mode */
232                 unlock_page(obh->b_page);
233         }
234
235         nbh = nilfs_btnode_create_block(btnc, newkey);
236         if (!nbh)
237                 return -ENOMEM;
238
239         BUG_ON(nbh == obh);
240         ctxt->newbh = nbh;
241         return 0;
242
243  failed_unlock:
244         unlock_page(obh->b_page);
245         return err;
246 }
247
248 /**
249  * nilfs_btnode_commit_change_key
250  *  commit the change_key operation prepared by prepare_change_key().
251  */
252 void nilfs_btnode_commit_change_key(struct address_space *btnc,
253                                     struct nilfs_btnode_chkey_ctxt *ctxt)
254 {
255         struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
256         __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
257         struct page *opage;
258
259         if (oldkey == newkey)
260                 return;
261
262         if (nbh == NULL) {      /* blocksize == pagesize */
263                 opage = obh->b_page;
264                 if (unlikely(oldkey != opage->index))
265                         NILFS_PAGE_BUG(opage,
266                                        "invalid oldkey %lld (newkey=%lld)",
267                                        (unsigned long long)oldkey,
268                                        (unsigned long long)newkey);
269                 mark_buffer_dirty(obh);
270
271                 spin_lock_irq(&btnc->tree_lock);
272                 radix_tree_delete(&btnc->page_tree, oldkey);
273                 radix_tree_tag_set(&btnc->page_tree, newkey,
274                                    PAGECACHE_TAG_DIRTY);
275                 spin_unlock_irq(&btnc->tree_lock);
276
277                 opage->index = obh->b_blocknr = newkey;
278                 unlock_page(opage);
279         } else {
280                 nilfs_copy_buffer(nbh, obh);
281                 mark_buffer_dirty(nbh);
282
283                 nbh->b_blocknr = newkey;
284                 ctxt->bh = nbh;
285                 nilfs_btnode_delete(obh); /* will decrement bh->b_count */
286         }
287 }
288
289 /**
290  * nilfs_btnode_abort_change_key
291  *  abort the change_key operation prepared by prepare_change_key().
292  */
293 void nilfs_btnode_abort_change_key(struct address_space *btnc,
294                                    struct nilfs_btnode_chkey_ctxt *ctxt)
295 {
296         struct buffer_head *nbh = ctxt->newbh;
297         __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
298
299         if (oldkey == newkey)
300                 return;
301
302         if (nbh == NULL) {      /* blocksize == pagesize */
303                 spin_lock_irq(&btnc->tree_lock);
304                 radix_tree_delete(&btnc->page_tree, newkey);
305                 spin_unlock_irq(&btnc->tree_lock);
306                 unlock_page(ctxt->bh->b_page);
307         } else {
308                 /*
309                  * When canceling a buffer that a prepare operation has
310                  * allocated to copy a node block to another location, use
311                  * nilfs_btnode_delete() to initialize and release the buffer
312                  * so that the buffer flags will not be in an inconsistent
313                  * state when it is reallocated.
314                  */
315                 nilfs_btnode_delete(nbh);
316         }
317 }