2 * sufile.c - NILFS segment usage file.
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Koji Sato <koji@osrg.net>.
21 * Revised by Ryusuke Konishi <ryusuke@osrg.net>.
24 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/buffer_head.h>
28 #include <linux/errno.h>
29 #include <linux/nilfs2_fs.h>
34 struct nilfs_sufile_info
{
35 struct nilfs_mdt_info mi
;
36 unsigned long ncleansegs
;
39 static inline struct nilfs_sufile_info
*NILFS_SUI(struct inode
*sufile
)
41 return (struct nilfs_sufile_info
*)NILFS_MDT(sufile
);
44 static inline unsigned long
45 nilfs_sufile_segment_usages_per_block(const struct inode
*sufile
)
47 return NILFS_MDT(sufile
)->mi_entries_per_block
;
51 nilfs_sufile_get_blkoff(const struct inode
*sufile
, __u64 segnum
)
53 __u64 t
= segnum
+ NILFS_MDT(sufile
)->mi_first_entry_offset
;
54 do_div(t
, nilfs_sufile_segment_usages_per_block(sufile
));
55 return (unsigned long)t
;
59 nilfs_sufile_get_offset(const struct inode
*sufile
, __u64 segnum
)
61 __u64 t
= segnum
+ NILFS_MDT(sufile
)->mi_first_entry_offset
;
62 return do_div(t
, nilfs_sufile_segment_usages_per_block(sufile
));
66 nilfs_sufile_segment_usages_in_block(const struct inode
*sufile
, __u64 curr
,
69 return min_t(unsigned long,
70 nilfs_sufile_segment_usages_per_block(sufile
) -
71 nilfs_sufile_get_offset(sufile
, curr
),
75 static struct nilfs_segment_usage
*
76 nilfs_sufile_block_get_segment_usage(const struct inode
*sufile
, __u64 segnum
,
77 struct buffer_head
*bh
, void *kaddr
)
79 return kaddr
+ bh_offset(bh
) +
80 nilfs_sufile_get_offset(sufile
, segnum
) *
81 NILFS_MDT(sufile
)->mi_entry_size
;
84 static inline int nilfs_sufile_get_header_block(struct inode
*sufile
,
85 struct buffer_head
**bhp
)
87 return nilfs_mdt_get_block(sufile
, 0, 0, NULL
, bhp
);
91 nilfs_sufile_get_segment_usage_block(struct inode
*sufile
, __u64 segnum
,
92 int create
, struct buffer_head
**bhp
)
94 return nilfs_mdt_get_block(sufile
,
95 nilfs_sufile_get_blkoff(sufile
, segnum
),
99 static void nilfs_sufile_mod_counter(struct buffer_head
*header_bh
,
100 u64 ncleanadd
, u64 ndirtyadd
)
102 struct nilfs_sufile_header
*header
;
105 kaddr
= kmap_atomic(header_bh
->b_page
, KM_USER0
);
106 header
= kaddr
+ bh_offset(header_bh
);
107 le64_add_cpu(&header
->sh_ncleansegs
, ncleanadd
);
108 le64_add_cpu(&header
->sh_ndirtysegs
, ndirtyadd
);
109 kunmap_atomic(kaddr
, KM_USER0
);
111 nilfs_mdt_mark_buffer_dirty(header_bh
);
115 * nilfs_sufile_get_ncleansegs - return the number of clean segments
116 * @sufile: inode of segment usage file
118 unsigned long nilfs_sufile_get_ncleansegs(struct inode
*sufile
)
120 return NILFS_SUI(sufile
)->ncleansegs
;
124 * nilfs_sufile_updatev - modify multiple segment usages at a time
125 * @sufile: inode of segment usage file
126 * @segnumv: array of segment numbers
127 * @nsegs: size of @segnumv array
128 * @create: creation flag
129 * @ndone: place to store number of modified segments on @segnumv
130 * @dofunc: primitive operation for the update
132 * Description: nilfs_sufile_updatev() repeatedly calls @dofunc
133 * against the given array of segments. The @dofunc is called with
134 * buffers of a header block and the sufile block in which the target
135 * segment usage entry is contained. If @ndone is given, the number
136 * of successfully modified segments from the head is stored in the
137 * place @ndone points to.
139 * Return Value: On success, zero is returned. On error, one of the
140 * following negative error codes is returned.
144 * %-ENOMEM - Insufficient amount of memory available.
146 * %-ENOENT - Given segment usage is in hole block (may be returned if
149 * %-EINVAL - Invalid segment usage number
151 int nilfs_sufile_updatev(struct inode
*sufile
, __u64
*segnumv
, size_t nsegs
,
152 int create
, size_t *ndone
,
153 void (*dofunc
)(struct inode
*, __u64
,
154 struct buffer_head
*,
155 struct buffer_head
*))
157 struct buffer_head
*header_bh
, *bh
;
158 unsigned long blkoff
, prev_blkoff
;
160 size_t nerr
= 0, n
= 0;
163 if (unlikely(nsegs
== 0))
166 down_write(&NILFS_MDT(sufile
)->mi_sem
);
167 for (seg
= segnumv
; seg
< segnumv
+ nsegs
; seg
++) {
168 if (unlikely(*seg
>= nilfs_sufile_get_nsegments(sufile
))) {
170 "%s: invalid segment number: %llu\n", __func__
,
171 (unsigned long long)*seg
);
180 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
185 blkoff
= nilfs_sufile_get_blkoff(sufile
, *seg
);
186 ret
= nilfs_mdt_get_block(sufile
, blkoff
, create
, NULL
, &bh
);
191 dofunc(sufile
, *seg
, header_bh
, bh
);
193 if (++seg
>= segnumv
+ nsegs
)
195 prev_blkoff
= blkoff
;
196 blkoff
= nilfs_sufile_get_blkoff(sufile
, *seg
);
197 if (blkoff
== prev_blkoff
)
200 /* get different block */
202 ret
= nilfs_mdt_get_block(sufile
, blkoff
, create
, NULL
, &bh
);
203 if (unlikely(ret
< 0))
212 up_write(&NILFS_MDT(sufile
)->mi_sem
);
219 int nilfs_sufile_update(struct inode
*sufile
, __u64 segnum
, int create
,
220 void (*dofunc
)(struct inode
*, __u64
,
221 struct buffer_head
*,
222 struct buffer_head
*))
224 struct buffer_head
*header_bh
, *bh
;
227 if (unlikely(segnum
>= nilfs_sufile_get_nsegments(sufile
))) {
228 printk(KERN_WARNING
"%s: invalid segment number: %llu\n",
229 __func__
, (unsigned long long)segnum
);
232 down_write(&NILFS_MDT(sufile
)->mi_sem
);
234 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
238 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, create
, &bh
);
240 dofunc(sufile
, segnum
, header_bh
, bh
);
246 up_write(&NILFS_MDT(sufile
)->mi_sem
);
251 * nilfs_sufile_alloc - allocate a segment
252 * @sufile: inode of segment usage file
253 * @segnump: pointer to segment number
255 * Description: nilfs_sufile_alloc() allocates a clean segment.
257 * Return Value: On success, 0 is returned and the segment number of the
258 * allocated segment is stored in the place pointed by @segnump. On error, one
259 * of the following negative error codes is returned.
263 * %-ENOMEM - Insufficient amount of memory available.
265 * %-ENOSPC - No clean segment left.
267 int nilfs_sufile_alloc(struct inode
*sufile
, __u64
*segnump
)
269 struct buffer_head
*header_bh
, *su_bh
;
270 struct nilfs_sufile_header
*header
;
271 struct nilfs_segment_usage
*su
;
272 size_t susz
= NILFS_MDT(sufile
)->mi_entry_size
;
273 __u64 segnum
, maxsegnum
, last_alloc
;
275 unsigned long nsegments
, ncleansegs
, nsus
;
278 down_write(&NILFS_MDT(sufile
)->mi_sem
);
280 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
283 kaddr
= kmap_atomic(header_bh
->b_page
, KM_USER0
);
284 header
= kaddr
+ bh_offset(header_bh
);
285 ncleansegs
= le64_to_cpu(header
->sh_ncleansegs
);
286 last_alloc
= le64_to_cpu(header
->sh_last_alloc
);
287 kunmap_atomic(kaddr
, KM_USER0
);
289 nsegments
= nilfs_sufile_get_nsegments(sufile
);
290 segnum
= last_alloc
+ 1;
291 maxsegnum
= nsegments
- 1;
292 for (i
= 0; i
< nsegments
; i
+= nsus
) {
293 if (segnum
>= nsegments
) {
296 maxsegnum
= last_alloc
;
298 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 1,
302 kaddr
= kmap_atomic(su_bh
->b_page
, KM_USER0
);
303 su
= nilfs_sufile_block_get_segment_usage(
304 sufile
, segnum
, su_bh
, kaddr
);
306 nsus
= nilfs_sufile_segment_usages_in_block(
307 sufile
, segnum
, maxsegnum
);
308 for (j
= 0; j
< nsus
; j
++, su
= (void *)su
+ susz
, segnum
++) {
309 if (!nilfs_segment_usage_clean(su
))
311 /* found a clean segment */
312 nilfs_segment_usage_set_dirty(su
);
313 kunmap_atomic(kaddr
, KM_USER0
);
315 kaddr
= kmap_atomic(header_bh
->b_page
, KM_USER0
);
316 header
= kaddr
+ bh_offset(header_bh
);
317 le64_add_cpu(&header
->sh_ncleansegs
, -1);
318 le64_add_cpu(&header
->sh_ndirtysegs
, 1);
319 header
->sh_last_alloc
= cpu_to_le64(segnum
);
320 kunmap_atomic(kaddr
, KM_USER0
);
322 NILFS_SUI(sufile
)->ncleansegs
--;
323 nilfs_mdt_mark_buffer_dirty(header_bh
);
324 nilfs_mdt_mark_buffer_dirty(su_bh
);
325 nilfs_mdt_mark_dirty(sufile
);
331 kunmap_atomic(kaddr
, KM_USER0
);
335 /* no segments left */
342 up_write(&NILFS_MDT(sufile
)->mi_sem
);
346 void nilfs_sufile_do_cancel_free(struct inode
*sufile
, __u64 segnum
,
347 struct buffer_head
*header_bh
,
348 struct buffer_head
*su_bh
)
350 struct nilfs_segment_usage
*su
;
353 kaddr
= kmap_atomic(su_bh
->b_page
, KM_USER0
);
354 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, su_bh
, kaddr
);
355 if (unlikely(!nilfs_segment_usage_clean(su
))) {
356 printk(KERN_WARNING
"%s: segment %llu must be clean\n",
357 __func__
, (unsigned long long)segnum
);
358 kunmap_atomic(kaddr
, KM_USER0
);
361 nilfs_segment_usage_set_dirty(su
);
362 kunmap_atomic(kaddr
, KM_USER0
);
364 nilfs_sufile_mod_counter(header_bh
, -1, 1);
365 NILFS_SUI(sufile
)->ncleansegs
--;
367 nilfs_mdt_mark_buffer_dirty(su_bh
);
368 nilfs_mdt_mark_dirty(sufile
);
371 void nilfs_sufile_do_scrap(struct inode
*sufile
, __u64 segnum
,
372 struct buffer_head
*header_bh
,
373 struct buffer_head
*su_bh
)
375 struct nilfs_segment_usage
*su
;
379 kaddr
= kmap_atomic(su_bh
->b_page
, KM_USER0
);
380 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, su_bh
, kaddr
);
381 if (su
->su_flags
== cpu_to_le32(1UL << NILFS_SEGMENT_USAGE_DIRTY
) &&
382 su
->su_nblocks
== cpu_to_le32(0)) {
383 kunmap_atomic(kaddr
, KM_USER0
);
386 clean
= nilfs_segment_usage_clean(su
);
387 dirty
= nilfs_segment_usage_dirty(su
);
389 /* make the segment garbage */
390 su
->su_lastmod
= cpu_to_le64(0);
391 su
->su_nblocks
= cpu_to_le32(0);
392 su
->su_flags
= cpu_to_le32(1UL << NILFS_SEGMENT_USAGE_DIRTY
);
393 kunmap_atomic(kaddr
, KM_USER0
);
395 nilfs_sufile_mod_counter(header_bh
, clean
? (u64
)-1 : 0, dirty
? 0 : 1);
396 NILFS_SUI(sufile
)->ncleansegs
-= clean
;
398 nilfs_mdt_mark_buffer_dirty(su_bh
);
399 nilfs_mdt_mark_dirty(sufile
);
402 void nilfs_sufile_do_free(struct inode
*sufile
, __u64 segnum
,
403 struct buffer_head
*header_bh
,
404 struct buffer_head
*su_bh
)
406 struct nilfs_segment_usage
*su
;
410 kaddr
= kmap_atomic(su_bh
->b_page
, KM_USER0
);
411 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, su_bh
, kaddr
);
412 if (nilfs_segment_usage_clean(su
)) {
413 printk(KERN_WARNING
"%s: segment %llu is already clean\n",
414 __func__
, (unsigned long long)segnum
);
415 kunmap_atomic(kaddr
, KM_USER0
);
418 WARN_ON(nilfs_segment_usage_error(su
));
419 WARN_ON(!nilfs_segment_usage_dirty(su
));
421 sudirty
= nilfs_segment_usage_dirty(su
);
422 nilfs_segment_usage_set_clean(su
);
423 kunmap_atomic(kaddr
, KM_USER0
);
424 nilfs_mdt_mark_buffer_dirty(su_bh
);
426 nilfs_sufile_mod_counter(header_bh
, 1, sudirty
? (u64
)-1 : 0);
427 NILFS_SUI(sufile
)->ncleansegs
++;
429 nilfs_mdt_mark_dirty(sufile
);
433 * nilfs_sufile_mark_dirty - mark the buffer having a segment usage dirty
434 * @sufile: inode of segment usage file
435 * @segnum: segment number
437 int nilfs_sufile_mark_dirty(struct inode
*sufile
, __u64 segnum
)
439 struct buffer_head
*bh
;
442 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 0, &bh
);
444 nilfs_mdt_mark_buffer_dirty(bh
);
445 nilfs_mdt_mark_dirty(sufile
);
452 * nilfs_sufile_set_segment_usage - set usage of a segment
453 * @sufile: inode of segment usage file
454 * @segnum: segment number
455 * @nblocks: number of live blocks in the segment
456 * @modtime: modification time (option)
458 int nilfs_sufile_set_segment_usage(struct inode
*sufile
, __u64 segnum
,
459 unsigned long nblocks
, time_t modtime
)
461 struct buffer_head
*bh
;
462 struct nilfs_segment_usage
*su
;
466 down_write(&NILFS_MDT(sufile
)->mi_sem
);
467 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 0, &bh
);
471 kaddr
= kmap_atomic(bh
->b_page
, KM_USER0
);
472 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, bh
, kaddr
);
473 WARN_ON(nilfs_segment_usage_error(su
));
475 su
->su_lastmod
= cpu_to_le64(modtime
);
476 su
->su_nblocks
= cpu_to_le32(nblocks
);
477 kunmap_atomic(kaddr
, KM_USER0
);
479 nilfs_mdt_mark_buffer_dirty(bh
);
480 nilfs_mdt_mark_dirty(sufile
);
484 up_write(&NILFS_MDT(sufile
)->mi_sem
);
489 * nilfs_sufile_get_stat - get segment usage statistics
490 * @sufile: inode of segment usage file
491 * @stat: pointer to a structure of segment usage statistics
493 * Description: nilfs_sufile_get_stat() returns information about segment
496 * Return Value: On success, 0 is returned, and segment usage information is
497 * stored in the place pointed by @stat. On error, one of the following
498 * negative error codes is returned.
502 * %-ENOMEM - Insufficient amount of memory available.
504 int nilfs_sufile_get_stat(struct inode
*sufile
, struct nilfs_sustat
*sustat
)
506 struct buffer_head
*header_bh
;
507 struct nilfs_sufile_header
*header
;
508 struct the_nilfs
*nilfs
= NILFS_I_NILFS(sufile
);
512 down_read(&NILFS_MDT(sufile
)->mi_sem
);
514 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
518 kaddr
= kmap_atomic(header_bh
->b_page
, KM_USER0
);
519 header
= kaddr
+ bh_offset(header_bh
);
520 sustat
->ss_nsegs
= nilfs_sufile_get_nsegments(sufile
);
521 sustat
->ss_ncleansegs
= le64_to_cpu(header
->sh_ncleansegs
);
522 sustat
->ss_ndirtysegs
= le64_to_cpu(header
->sh_ndirtysegs
);
523 sustat
->ss_ctime
= nilfs
->ns_ctime
;
524 sustat
->ss_nongc_ctime
= nilfs
->ns_nongc_ctime
;
525 spin_lock(&nilfs
->ns_last_segment_lock
);
526 sustat
->ss_prot_seq
= nilfs
->ns_prot_seq
;
527 spin_unlock(&nilfs
->ns_last_segment_lock
);
528 kunmap_atomic(kaddr
, KM_USER0
);
532 up_read(&NILFS_MDT(sufile
)->mi_sem
);
536 void nilfs_sufile_do_set_error(struct inode
*sufile
, __u64 segnum
,
537 struct buffer_head
*header_bh
,
538 struct buffer_head
*su_bh
)
540 struct nilfs_segment_usage
*su
;
544 kaddr
= kmap_atomic(su_bh
->b_page
, KM_USER0
);
545 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, su_bh
, kaddr
);
546 if (nilfs_segment_usage_error(su
)) {
547 kunmap_atomic(kaddr
, KM_USER0
);
550 suclean
= nilfs_segment_usage_clean(su
);
551 nilfs_segment_usage_set_error(su
);
552 kunmap_atomic(kaddr
, KM_USER0
);
555 nilfs_sufile_mod_counter(header_bh
, -1, 0);
556 NILFS_SUI(sufile
)->ncleansegs
--;
558 nilfs_mdt_mark_buffer_dirty(su_bh
);
559 nilfs_mdt_mark_dirty(sufile
);
563 * nilfs_sufile_get_suinfo -
564 * @sufile: inode of segment usage file
565 * @segnum: segment number to start looking
566 * @buf: array of suinfo
567 * @sisz: byte size of suinfo
568 * @nsi: size of suinfo array
572 * Return Value: On success, 0 is returned and .... On error, one of the
573 * following negative error codes is returned.
577 * %-ENOMEM - Insufficient amount of memory available.
579 ssize_t
nilfs_sufile_get_suinfo(struct inode
*sufile
, __u64 segnum
, void *buf
,
580 unsigned sisz
, size_t nsi
)
582 struct buffer_head
*su_bh
;
583 struct nilfs_segment_usage
*su
;
584 struct nilfs_suinfo
*si
= buf
;
585 size_t susz
= NILFS_MDT(sufile
)->mi_entry_size
;
586 struct the_nilfs
*nilfs
= NILFS_I_NILFS(sufile
);
588 unsigned long nsegs
, segusages_per_block
;
592 down_read(&NILFS_MDT(sufile
)->mi_sem
);
594 segusages_per_block
= nilfs_sufile_segment_usages_per_block(sufile
);
595 nsegs
= min_t(unsigned long,
596 nilfs_sufile_get_nsegments(sufile
) - segnum
,
598 for (i
= 0; i
< nsegs
; i
+= n
, segnum
+= n
) {
599 n
= min_t(unsigned long,
600 segusages_per_block
-
601 nilfs_sufile_get_offset(sufile
, segnum
),
603 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 0,
609 memset(si
, 0, sisz
* n
);
610 si
= (void *)si
+ sisz
* n
;
614 kaddr
= kmap_atomic(su_bh
->b_page
, KM_USER0
);
615 su
= nilfs_sufile_block_get_segment_usage(
616 sufile
, segnum
, su_bh
, kaddr
);
618 j
++, su
= (void *)su
+ susz
, si
= (void *)si
+ sisz
) {
619 si
->sui_lastmod
= le64_to_cpu(su
->su_lastmod
);
620 si
->sui_nblocks
= le32_to_cpu(su
->su_nblocks
);
621 si
->sui_flags
= le32_to_cpu(su
->su_flags
) &
622 ~(1UL << NILFS_SEGMENT_USAGE_ACTIVE
);
623 if (nilfs_segment_is_active(nilfs
, segnum
+ j
))
625 (1UL << NILFS_SEGMENT_USAGE_ACTIVE
);
627 kunmap_atomic(kaddr
, KM_USER0
);
633 up_read(&NILFS_MDT(sufile
)->mi_sem
);
638 * nilfs_sufile_read - read or get sufile inode
639 * @sb: super block instance
640 * @susize: size of a segment usage entry
641 * @raw_inode: on-disk sufile inode
642 * @inodep: buffer to store the inode
644 int nilfs_sufile_read(struct super_block
*sb
, size_t susize
,
645 struct nilfs_inode
*raw_inode
, struct inode
**inodep
)
647 struct inode
*sufile
;
648 struct nilfs_sufile_info
*sui
;
649 struct buffer_head
*header_bh
;
650 struct nilfs_sufile_header
*header
;
654 sufile
= nilfs_iget_locked(sb
, NULL
, NILFS_SUFILE_INO
);
655 if (unlikely(!sufile
))
657 if (!(sufile
->i_state
& I_NEW
))
660 err
= nilfs_mdt_init(sufile
, NILFS_MDT_GFP
, sizeof(*sui
));
664 nilfs_mdt_set_entry_size(sufile
, susize
,
665 sizeof(struct nilfs_sufile_header
));
667 err
= nilfs_read_inode_common(sufile
, raw_inode
);
671 err
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
675 sui
= NILFS_SUI(sufile
);
676 kaddr
= kmap_atomic(header_bh
->b_page
, KM_USER0
);
677 header
= kaddr
+ bh_offset(header_bh
);
678 sui
->ncleansegs
= le64_to_cpu(header
->sh_ncleansegs
);
679 kunmap_atomic(kaddr
, KM_USER0
);
682 unlock_new_inode(sufile
);