Blame view

fs/nilfs2/bmap.c 14.9 KB
bdb265eae   Koji Sato   nilfs2: integrate...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * bmap.c - NILFS block mapping.
   *
   * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
4b420ab4e   Ryusuke Konishi   nilfs2: clean up ...
16
   * Written by Koji Sato.
bdb265eae   Koji Sato   nilfs2: integrate...
17
18
19
20
21
22
23
   */
  
  #include <linux/fs.h>
  #include <linux/string.h>
  #include <linux/errno.h>
  #include "nilfs.h"
  #include "bmap.h"
05d0e94b6   Ryusuke Konishi   nilfs2: get rid o...
24
25
  #include "btree.h"
  #include "direct.h"
bdb265eae   Koji Sato   nilfs2: integrate...
26
27
28
29
  #include "btnode.h"
  #include "mdt.h"
  #include "dat.h"
  #include "alloc.h"
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
30
  struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *bmap)
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
31
  {
0ef28f9ae   Ryusuke Konishi   nilfs2: get rid o...
32
33
34
  	struct the_nilfs *nilfs = bmap->b_inode->i_sb->s_fs_info;
  
  	return nilfs->ns_dat;
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
35
  }
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
36
37
38
39
40
41
  static int nilfs_bmap_convert_error(struct nilfs_bmap *bmap,
  				     const char *fname, int err)
  {
  	struct inode *inode = bmap->b_inode;
  
  	if (err == -EINVAL) {
cae3d4ca6   Ryusuke Konishi   nilfs2: hide func...
42
43
  		__nilfs_error(inode->i_sb, fname,
  			      "broken bmap (inode number=%lu)", inode->i_ino);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
44
45
46
47
  		err = -EIO;
  	}
  	return err;
  }
0f3fe33b3   Ryusuke Konishi   nilfs2: convert n...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  /**
   * nilfs_bmap_lookup_at_level - find a data block or node block
   * @bmap: bmap
   * @key: key
   * @level: level
   * @ptrp: place to store the value associated to @key
   *
   * Description: nilfs_bmap_lookup_at_level() finds a record whose key
   * matches @key in the block at @level of the bmap.
   *
   * Return Value: On success, 0 is returned and the record associated with @key
   * is stored in the place pointed by @ptrp. On error, one of the following
   * negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   *
   * %-ENOENT - A record associated with @key does not exist.
   */
bdb265eae   Koji Sato   nilfs2: integrate...
68
69
70
  int nilfs_bmap_lookup_at_level(struct nilfs_bmap *bmap, __u64 key, int level,
  			       __u64 *ptrp)
  {
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
71
  	sector_t blocknr;
bdb265eae   Koji Sato   nilfs2: integrate...
72
73
74
  	int ret;
  
  	down_read(&bmap->b_sem);
8acfbf093   Pekka Enberg   nilfs2: clean up ...
75
  	ret = bmap->b_ops->bop_lookup(bmap, key, level, ptrp);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
76
77
  	if (ret < 0) {
  		ret = nilfs_bmap_convert_error(bmap, __func__, ret);
bdb265eae   Koji Sato   nilfs2: integrate...
78
  		goto out;
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
79
  	}
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
80
81
82
83
84
  	if (NILFS_BMAP_USE_VBN(bmap)) {
  		ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), *ptrp,
  					  &blocknr);
  		if (!ret)
  			*ptrp = blocknr;
bdb265eae   Koji Sato   nilfs2: integrate...
85
86
87
88
89
90
  	}
  
   out:
  	up_read(&bmap->b_sem);
  	return ret;
  }
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
91
  int nilfs_bmap_lookup_contig(struct nilfs_bmap *bmap, __u64 key, __u64 *ptrp,
0c6c44cb9   Ryusuke Konishi   nilfs2: avoid bar...
92
  			     unsigned int maxblocks)
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
93
94
95
96
97
98
  {
  	int ret;
  
  	down_read(&bmap->b_sem);
  	ret = bmap->b_ops->bop_lookup_contig(bmap, key, ptrp, maxblocks);
  	up_read(&bmap->b_sem);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
99
100
  
  	return nilfs_bmap_convert_error(bmap, __func__, ret);
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
101
  }
bdb265eae   Koji Sato   nilfs2: integrate...
102

bdb265eae   Koji Sato   nilfs2: integrate...
103
104
105
106
107
108
109
  static int nilfs_bmap_do_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
  {
  	__u64 keys[NILFS_BMAP_SMALL_HIGH + 1];
  	__u64 ptrs[NILFS_BMAP_SMALL_HIGH + 1];
  	int ret, n;
  
  	if (bmap->b_ops->bop_check_insert != NULL) {
8acfbf093   Pekka Enberg   nilfs2: clean up ...
110
  		ret = bmap->b_ops->bop_check_insert(bmap, key);
bdb265eae   Koji Sato   nilfs2: integrate...
111
  		if (ret > 0) {
8acfbf093   Pekka Enberg   nilfs2: clean up ...
112
  			n = bmap->b_ops->bop_gather_data(
bdb265eae   Koji Sato   nilfs2: integrate...
113
114
115
116
  				bmap, keys, ptrs, NILFS_BMAP_SMALL_HIGH + 1);
  			if (n < 0)
  				return n;
  			ret = nilfs_btree_convert_and_insert(
3033342a0   Ryusuke Konishi   nilfs2: remove us...
117
  				bmap, key, ptr, keys, ptrs, n);
bdb265eae   Koji Sato   nilfs2: integrate...
118
119
120
121
122
123
124
  			if (ret == 0)
  				bmap->b_u.u_flags |= NILFS_BMAP_LARGE;
  
  			return ret;
  		} else if (ret < 0)
  			return ret;
  	}
8acfbf093   Pekka Enberg   nilfs2: clean up ...
125
  	return bmap->b_ops->bop_insert(bmap, key, ptr);
bdb265eae   Koji Sato   nilfs2: integrate...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  }
  
  /**
   * nilfs_bmap_insert - insert a new key-record pair into a bmap
   * @bmap: bmap
   * @key: key
   * @rec: record
   *
   * Description: nilfs_bmap_insert() inserts the new key-record pair specified
   * by @key and @rec into @bmap.
   *
   * Return Value: On success, 0 is returned. On error, one of the following
   * negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   *
   * %-EEXIST - A record associated with @key already exist.
   */
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
146
  int nilfs_bmap_insert(struct nilfs_bmap *bmap, __u64 key, unsigned long rec)
bdb265eae   Koji Sato   nilfs2: integrate...
147
148
149
150
151
152
  {
  	int ret;
  
  	down_write(&bmap->b_sem);
  	ret = nilfs_bmap_do_insert(bmap, key, rec);
  	up_write(&bmap->b_sem);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
153
154
  
  	return nilfs_bmap_convert_error(bmap, __func__, ret);
bdb265eae   Koji Sato   nilfs2: integrate...
155
156
157
158
159
160
161
162
163
  }
  
  static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key)
  {
  	__u64 keys[NILFS_BMAP_LARGE_LOW + 1];
  	__u64 ptrs[NILFS_BMAP_LARGE_LOW + 1];
  	int ret, n;
  
  	if (bmap->b_ops->bop_check_delete != NULL) {
8acfbf093   Pekka Enberg   nilfs2: clean up ...
164
  		ret = bmap->b_ops->bop_check_delete(bmap, key);
bdb265eae   Koji Sato   nilfs2: integrate...
165
  		if (ret > 0) {
8acfbf093   Pekka Enberg   nilfs2: clean up ...
166
  			n = bmap->b_ops->bop_gather_data(
bdb265eae   Koji Sato   nilfs2: integrate...
167
168
169
170
  				bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1);
  			if (n < 0)
  				return n;
  			ret = nilfs_direct_delete_and_convert(
3033342a0   Ryusuke Konishi   nilfs2: remove us...
171
  				bmap, key, keys, ptrs, n);
bdb265eae   Koji Sato   nilfs2: integrate...
172
173
174
175
176
177
178
  			if (ret == 0)
  				bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE;
  
  			return ret;
  		} else if (ret < 0)
  			return ret;
  	}
8acfbf093   Pekka Enberg   nilfs2: clean up ...
179
  	return bmap->b_ops->bop_delete(bmap, key);
bdb265eae   Koji Sato   nilfs2: integrate...
180
  }
5b20384fb   Ryusuke Konishi   nilfs2: add bmap ...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  /**
   * nilfs_bmap_seek_key - seek a valid entry and return its key
   * @bmap: bmap struct
   * @start: start key number
   * @keyp: place to store valid key
   *
   * Description: nilfs_bmap_seek_key() seeks a valid key on @bmap
   * starting from @start, and stores it to @keyp if found.
   *
   * Return Value: On success, 0 is returned. On error, one of the following
   * negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   *
   * %-ENOENT - No valid entry was found
   */
  int nilfs_bmap_seek_key(struct nilfs_bmap *bmap, __u64 start, __u64 *keyp)
  {
  	int ret;
  
  	down_read(&bmap->b_sem);
  	ret = bmap->b_ops->bop_seek_key(bmap, start, keyp);
  	up_read(&bmap->b_sem);
  
  	if (ret < 0)
  		ret = nilfs_bmap_convert_error(bmap, __func__, ret);
  	return ret;
  }
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
211
  int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp)
bdb265eae   Koji Sato   nilfs2: integrate...
212
  {
bdb265eae   Koji Sato   nilfs2: integrate...
213
214
215
  	int ret;
  
  	down_read(&bmap->b_sem);
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
216
  	ret = bmap->b_ops->bop_last_key(bmap, keyp);
bdb265eae   Koji Sato   nilfs2: integrate...
217
  	up_read(&bmap->b_sem);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
218
219
220
  
  	if (ret < 0)
  		ret = nilfs_bmap_convert_error(bmap, __func__, ret);
bdb265eae   Koji Sato   nilfs2: integrate...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  	return ret;
  }
  
  /**
   * nilfs_bmap_delete - delete a key-record pair from a bmap
   * @bmap: bmap
   * @key: key
   *
   * Description: nilfs_bmap_delete() deletes the key-record pair specified by
   * @key from @bmap.
   *
   * Return Value: On success, 0 is returned. On error, one of the following
   * negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   *
   * %-ENOENT - A record associated with @key does not exist.
   */
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
241
  int nilfs_bmap_delete(struct nilfs_bmap *bmap, __u64 key)
bdb265eae   Koji Sato   nilfs2: integrate...
242
243
244
245
246
247
  {
  	int ret;
  
  	down_write(&bmap->b_sem);
  	ret = nilfs_bmap_do_delete(bmap, key);
  	up_write(&bmap->b_sem);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
248
249
  
  	return nilfs_bmap_convert_error(bmap, __func__, ret);
bdb265eae   Koji Sato   nilfs2: integrate...
250
  }
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
251
  static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, __u64 key)
bdb265eae   Koji Sato   nilfs2: integrate...
252
253
254
  {
  	__u64 lastkey;
  	int ret;
8acfbf093   Pekka Enberg   nilfs2: clean up ...
255
  	ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
bdb265eae   Koji Sato   nilfs2: integrate...
256
257
258
259
260
261
262
263
264
265
  	if (ret < 0) {
  		if (ret == -ENOENT)
  			ret = 0;
  		return ret;
  	}
  
  	while (key <= lastkey) {
  		ret = nilfs_bmap_do_delete(bmap, lastkey);
  		if (ret < 0)
  			return ret;
8acfbf093   Pekka Enberg   nilfs2: clean up ...
266
  		ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
bdb265eae   Koji Sato   nilfs2: integrate...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  		if (ret < 0) {
  			if (ret == -ENOENT)
  				ret = 0;
  			return ret;
  		}
  	}
  	return 0;
  }
  
  /**
   * nilfs_bmap_truncate - truncate a bmap to a specified key
   * @bmap: bmap
   * @key: key
   *
   * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are
   * greater than or equal to @key from @bmap.
   *
   * Return Value: On success, 0 is returned. On error, one of the following
   * negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   */
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
291
  int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key)
bdb265eae   Koji Sato   nilfs2: integrate...
292
293
294
295
296
297
  {
  	int ret;
  
  	down_write(&bmap->b_sem);
  	ret = nilfs_bmap_do_truncate(bmap, key);
  	up_write(&bmap->b_sem);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
298
299
  
  	return nilfs_bmap_convert_error(bmap, __func__, ret);
bdb265eae   Koji Sato   nilfs2: integrate...
300
301
302
303
304
305
306
307
308
309
310
311
  }
  
  /**
   * nilfs_bmap_clear - free resources a bmap holds
   * @bmap: bmap
   *
   * Description: nilfs_bmap_clear() frees resources associated with @bmap.
   */
  void nilfs_bmap_clear(struct nilfs_bmap *bmap)
  {
  	down_write(&bmap->b_sem);
  	if (bmap->b_ops->bop_clear != NULL)
8acfbf093   Pekka Enberg   nilfs2: clean up ...
312
  		bmap->b_ops->bop_clear(bmap);
bdb265eae   Koji Sato   nilfs2: integrate...
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  	up_write(&bmap->b_sem);
  }
  
  /**
   * nilfs_bmap_propagate - propagate dirty state
   * @bmap: bmap
   * @bh: buffer head
   *
   * Description: nilfs_bmap_propagate() marks the buffers that directly or
   * indirectly refer to the block specified by @bh dirty.
   *
   * Return Value: On success, 0 is returned. On error, one of the following
   * negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   */
  int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh)
  {
  	int ret;
  
  	down_write(&bmap->b_sem);
8acfbf093   Pekka Enberg   nilfs2: clean up ...
336
  	ret = bmap->b_ops->bop_propagate(bmap, bh);
bdb265eae   Koji Sato   nilfs2: integrate...
337
  	up_write(&bmap->b_sem);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
338
339
  
  	return nilfs_bmap_convert_error(bmap, __func__, ret);
bdb265eae   Koji Sato   nilfs2: integrate...
340
341
342
343
344
345
346
347
348
349
350
  }
  
  /**
   * nilfs_bmap_lookup_dirty_buffers -
   * @bmap: bmap
   * @listp: pointer to buffer head list
   */
  void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *bmap,
  				     struct list_head *listp)
  {
  	if (bmap->b_ops->bop_lookup_dirty_buffers != NULL)
8acfbf093   Pekka Enberg   nilfs2: clean up ...
351
  		bmap->b_ops->bop_lookup_dirty_buffers(bmap, listp);
bdb265eae   Koji Sato   nilfs2: integrate...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  }
  
  /**
   * nilfs_bmap_assign - assign a new block number to a block
   * @bmap: bmap
   * @bhp: pointer to buffer head
   * @blocknr: block number
   * @binfo: block information
   *
   * Description: nilfs_bmap_assign() assigns the block number @blocknr to the
   * buffer specified by @bh.
   *
   * Return Value: On success, 0 is returned and the buffer head of a newly
   * create buffer and the block information associated with the buffer are
   * stored in the place pointed by @bh and @binfo, respectively. On error, one
   * of the following negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   */
  int nilfs_bmap_assign(struct nilfs_bmap *bmap,
  		      struct buffer_head **bh,
  		      unsigned long blocknr,
  		      union nilfs_binfo *binfo)
  {
  	int ret;
  
  	down_write(&bmap->b_sem);
8acfbf093   Pekka Enberg   nilfs2: clean up ...
381
  	ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo);
bdb265eae   Koji Sato   nilfs2: integrate...
382
  	up_write(&bmap->b_sem);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
383
384
  
  	return nilfs_bmap_convert_error(bmap, __func__, ret);
bdb265eae   Koji Sato   nilfs2: integrate...
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
  }
  
  /**
   * nilfs_bmap_mark - mark block dirty
   * @bmap: bmap
   * @key: key
   * @level: level
   *
   * Description: nilfs_bmap_mark() marks the block specified by @key and @level
   * as dirty.
   *
   * Return Value: On success, 0 is returned. On error, one of the following
   * negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   */
  int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level)
  {
  	int ret;
  
  	if (bmap->b_ops->bop_mark == NULL)
  		return 0;
  
  	down_write(&bmap->b_sem);
8acfbf093   Pekka Enberg   nilfs2: clean up ...
411
  	ret = bmap->b_ops->bop_mark(bmap, key, level);
bdb265eae   Koji Sato   nilfs2: integrate...
412
  	up_write(&bmap->b_sem);
e828949e5   Ryusuke Konishi   nilfs2: call nilf...
413
414
  
  	return nilfs_bmap_convert_error(bmap, __func__, ret);
bdb265eae   Koji Sato   nilfs2: integrate...
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
  }
  
  /**
   * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
   * @bmap: bmap
   *
   * Description: nilfs_test_and_clear() is the atomic operation to test and
   * clear the dirty state of @bmap.
   *
   * Return Value: 1 is returned if @bmap is dirty, or 0 if clear.
   */
  int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *bmap)
  {
  	int ret;
  
  	down_write(&bmap->b_sem);
  	ret = nilfs_bmap_dirty(bmap);
  	nilfs_bmap_clear_dirty(bmap);
  	up_write(&bmap->b_sem);
  	return ret;
  }
  
  
  /*
   * Internal use only
   */
bdb265eae   Koji Sato   nilfs2: integrate...
441
442
443
444
445
  __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *bmap,
  			      const struct buffer_head *bh)
  {
  	struct buffer_head *pbh;
  	__u64 key;
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
446
  	key = page_index(bh->b_page) << (PAGE_SHIFT -
bdb265eae   Koji Sato   nilfs2: integrate...
447
  					 bmap->b_inode->i_blkbits);
5ee581483   Jiro SEKIBA   nilfs2: trivial c...
448
449
  	for (pbh = page_buffers(bh->b_page); pbh != bh; pbh = pbh->b_this_page)
  		key++;
bdb265eae   Koji Sato   nilfs2: integrate...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  
  	return key;
  }
  
  __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key)
  {
  	__s64 diff;
  
  	diff = key - bmap->b_last_allocated_key;
  	if ((nilfs_bmap_keydiff_abs(diff) < NILFS_INODE_BMAP_SIZE) &&
  	    (bmap->b_last_allocated_ptr != NILFS_BMAP_INVALID_PTR) &&
  	    (bmap->b_last_allocated_ptr + diff > 0))
  		return bmap->b_last_allocated_ptr + diff;
  	else
  		return NILFS_BMAP_INVALID_PTR;
  }
bdb265eae   Koji Sato   nilfs2: integrate...
466
467
468
469
470
471
472
473
474
475
476
  #define NILFS_BMAP_GROUP_DIV	8
  __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap)
  {
  	struct inode *dat = nilfs_bmap_get_dat(bmap);
  	unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat);
  	unsigned long group = bmap->b_inode->i_ino / entries_per_group;
  
  	return group * entries_per_group +
  		(bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) *
  		(entries_per_group / NILFS_BMAP_GROUP_DIV);
  }
bcb48891b   Ryusuke Konishi   nilfs2: fix lockd...
477
  static struct lock_class_key nilfs_bmap_dat_lock_key;
ff54de363   Ryusuke Konishi   nilfs2: fix lockd...
478
  static struct lock_class_key nilfs_bmap_mdt_lock_key;
bcb48891b   Ryusuke Konishi   nilfs2: fix lockd...
479

bdb265eae   Koji Sato   nilfs2: integrate...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
  /**
   * nilfs_bmap_read - read a bmap from an inode
   * @bmap: bmap
   * @raw_inode: on-disk inode
   *
   * Description: nilfs_bmap_read() initializes the bmap @bmap.
   *
   * Return Value: On success, 0 is returned. On error, the following negative
   * error code is returned.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   */
  int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  {
  	if (raw_inode == NULL)
  		memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
  	else
  		memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
  
  	init_rwsem(&bmap->b_sem);
  	bmap->b_state = 0;
  	bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  	switch (bmap->b_inode->i_ino) {
  	case NILFS_DAT_INO:
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
504
505
  		bmap->b_ptr_type = NILFS_BMAP_PTR_P;
  		bmap->b_last_allocated_key = 0;
bdb265eae   Koji Sato   nilfs2: integrate...
506
  		bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
bcb48891b   Ryusuke Konishi   nilfs2: fix lockd...
507
  		lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
bdb265eae   Koji Sato   nilfs2: integrate...
508
509
510
  		break;
  	case NILFS_CPFILE_INO:
  	case NILFS_SUFILE_INO:
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
511
512
  		bmap->b_ptr_type = NILFS_BMAP_PTR_VS;
  		bmap->b_last_allocated_key = 0;
bdb265eae   Koji Sato   nilfs2: integrate...
513
  		bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
ff54de363   Ryusuke Konishi   nilfs2: fix lockd...
514
  		lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
bdb265eae   Koji Sato   nilfs2: integrate...
515
  		break;
ff54de363   Ryusuke Konishi   nilfs2: fix lockd...
516
517
518
  	case NILFS_IFILE_INO:
  		lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
  		/* Fall through */
bdb265eae   Koji Sato   nilfs2: integrate...
519
  	default:
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
520
521
  		bmap->b_ptr_type = NILFS_BMAP_PTR_VM;
  		bmap->b_last_allocated_key = 0;
bdb265eae   Koji Sato   nilfs2: integrate...
522
523
524
525
526
  		bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  		break;
  	}
  
  	return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
3033342a0   Ryusuke Konishi   nilfs2: remove us...
527
  		nilfs_btree_init(bmap) : nilfs_direct_init(bmap);
bdb265eae   Koji Sato   nilfs2: integrate...
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
  }
  
  /**
   * nilfs_bmap_write - write back a bmap to an inode
   * @bmap: bmap
   * @raw_inode: on-disk inode
   *
   * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
   */
  void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  {
  	down_write(&bmap->b_sem);
  	memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
  	       NILFS_INODE_BMAP_SIZE * sizeof(__le64));
  	if (bmap->b_inode->i_ino == NILFS_DAT_INO)
  		bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  
  	up_write(&bmap->b_sem);
  }
  
  void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
  {
  	memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
  	init_rwsem(&bmap->b_sem);
  	bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
553
  	bmap->b_ptr_type = NILFS_BMAP_PTR_U;
bdb265eae   Koji Sato   nilfs2: integrate...
554
555
556
557
558
  	bmap->b_last_allocated_key = 0;
  	bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  	bmap->b_state = 0;
  	nilfs_btree_init_gc(bmap);
  }
a8070dd36   Ryusuke Konishi   nilfs2: add routi...
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  void nilfs_bmap_save(const struct nilfs_bmap *bmap,
  		     struct nilfs_bmap_store *store)
  {
  	memcpy(store->data, bmap->b_u.u_data, sizeof(store->data));
  	store->last_allocated_key = bmap->b_last_allocated_key;
  	store->last_allocated_ptr = bmap->b_last_allocated_ptr;
  	store->state = bmap->b_state;
  }
  
  void nilfs_bmap_restore(struct nilfs_bmap *bmap,
  			const struct nilfs_bmap_store *store)
  {
  	memcpy(bmap->b_u.u_data, store->data, sizeof(store->data));
  	bmap->b_last_allocated_key = store->last_allocated_key;
  	bmap->b_last_allocated_ptr = store->last_allocated_ptr;
  	bmap->b_state = store->state;
  }