1 // SPDX-License-Identifier: GPL-2.0+
3 * sufile.c - NILFS segment usage file.
5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
7 * Written by Koji Sato.
8 * Revised by Ryusuke Konishi.
11 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/buffer_head.h>
15 #include <linux/errno.h>
19 #include <trace/events/nilfs2.h>
22 * struct nilfs_sufile_info - on-memory private data of sufile
23 * @mi: on-memory private data of metadata file
24 * @ncleansegs: number of clean segments
25 * @allocmin: lower limit of allocatable segment range
26 * @allocmax: upper limit of allocatable segment range
28 struct nilfs_sufile_info
{
29 struct nilfs_mdt_info mi
;
30 unsigned long ncleansegs
;/* number of clean segments */
31 __u64 allocmin
; /* lower limit of allocatable segment range */
32 __u64 allocmax
; /* upper limit of allocatable segment range */
35 static inline struct nilfs_sufile_info
*NILFS_SUI(struct inode
*sufile
)
37 return (struct nilfs_sufile_info
*)NILFS_MDT(sufile
);
40 static inline unsigned long
41 nilfs_sufile_segment_usages_per_block(const struct inode
*sufile
)
43 return NILFS_MDT(sufile
)->mi_entries_per_block
;
47 nilfs_sufile_get_blkoff(const struct inode
*sufile
, __u64 segnum
)
49 __u64 t
= segnum
+ NILFS_MDT(sufile
)->mi_first_entry_offset
;
51 do_div(t
, nilfs_sufile_segment_usages_per_block(sufile
));
52 return (unsigned long)t
;
56 nilfs_sufile_get_offset(const struct inode
*sufile
, __u64 segnum
)
58 __u64 t
= segnum
+ NILFS_MDT(sufile
)->mi_first_entry_offset
;
60 return do_div(t
, nilfs_sufile_segment_usages_per_block(sufile
));
64 nilfs_sufile_segment_usages_in_block(const struct inode
*sufile
, __u64 curr
,
67 return min_t(unsigned long,
68 nilfs_sufile_segment_usages_per_block(sufile
) -
69 nilfs_sufile_get_offset(sufile
, curr
),
73 static struct nilfs_segment_usage
*
74 nilfs_sufile_block_get_segment_usage(const struct inode
*sufile
, __u64 segnum
,
75 struct buffer_head
*bh
, void *kaddr
)
77 return kaddr
+ bh_offset(bh
) +
78 nilfs_sufile_get_offset(sufile
, segnum
) *
79 NILFS_MDT(sufile
)->mi_entry_size
;
82 static inline int nilfs_sufile_get_header_block(struct inode
*sufile
,
83 struct buffer_head
**bhp
)
85 return nilfs_mdt_get_block(sufile
, 0, 0, NULL
, bhp
);
89 nilfs_sufile_get_segment_usage_block(struct inode
*sufile
, __u64 segnum
,
90 int create
, struct buffer_head
**bhp
)
92 return nilfs_mdt_get_block(sufile
,
93 nilfs_sufile_get_blkoff(sufile
, segnum
),
97 static int nilfs_sufile_delete_segment_usage_block(struct inode
*sufile
,
100 return nilfs_mdt_delete_block(sufile
,
101 nilfs_sufile_get_blkoff(sufile
, segnum
));
104 static void nilfs_sufile_mod_counter(struct buffer_head
*header_bh
,
105 u64 ncleanadd
, u64 ndirtyadd
)
107 struct nilfs_sufile_header
*header
;
110 kaddr
= kmap_atomic(header_bh
->b_page
);
111 header
= kaddr
+ bh_offset(header_bh
);
112 le64_add_cpu(&header
->sh_ncleansegs
, ncleanadd
);
113 le64_add_cpu(&header
->sh_ndirtysegs
, ndirtyadd
);
114 kunmap_atomic(kaddr
);
116 mark_buffer_dirty(header_bh
);
120 * nilfs_sufile_get_ncleansegs - return the number of clean segments
121 * @sufile: inode of segment usage file
123 unsigned long nilfs_sufile_get_ncleansegs(struct inode
*sufile
)
125 return NILFS_SUI(sufile
)->ncleansegs
;
129 * nilfs_sufile_updatev - modify multiple segment usages at a time
130 * @sufile: inode of segment usage file
131 * @segnumv: array of segment numbers
132 * @nsegs: size of @segnumv array
133 * @create: creation flag
134 * @ndone: place to store number of modified segments on @segnumv
135 * @dofunc: primitive operation for the update
137 * Description: nilfs_sufile_updatev() repeatedly calls @dofunc
138 * against the given array of segments. The @dofunc is called with
139 * buffers of a header block and the sufile block in which the target
140 * segment usage entry is contained. If @ndone is given, the number
141 * of successfully modified segments from the head is stored in the
142 * place @ndone points to.
144 * Return Value: On success, zero is returned. On error, one of the
145 * following negative error codes is returned.
149 * %-ENOMEM - Insufficient amount of memory available.
151 * %-ENOENT - Given segment usage is in hole block (may be returned if
154 * %-EINVAL - Invalid segment usage number
156 int nilfs_sufile_updatev(struct inode
*sufile
, __u64
*segnumv
, size_t nsegs
,
157 int create
, size_t *ndone
,
158 void (*dofunc
)(struct inode
*, __u64
,
159 struct buffer_head
*,
160 struct buffer_head
*))
162 struct buffer_head
*header_bh
, *bh
;
163 unsigned long blkoff
, prev_blkoff
;
165 size_t nerr
= 0, n
= 0;
168 if (unlikely(nsegs
== 0))
171 down_write(&NILFS_MDT(sufile
)->mi_sem
);
172 for (seg
= segnumv
; seg
< segnumv
+ nsegs
; seg
++) {
173 if (unlikely(*seg
>= nilfs_sufile_get_nsegments(sufile
))) {
174 nilfs_msg(sufile
->i_sb
, KERN_WARNING
,
175 "%s: invalid segment number: %llu",
176 __func__
, (unsigned long long)*seg
);
185 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
190 blkoff
= nilfs_sufile_get_blkoff(sufile
, *seg
);
191 ret
= nilfs_mdt_get_block(sufile
, blkoff
, create
, NULL
, &bh
);
196 dofunc(sufile
, *seg
, header_bh
, bh
);
198 if (++seg
>= segnumv
+ nsegs
)
200 prev_blkoff
= blkoff
;
201 blkoff
= nilfs_sufile_get_blkoff(sufile
, *seg
);
202 if (blkoff
== prev_blkoff
)
205 /* get different block */
207 ret
= nilfs_mdt_get_block(sufile
, blkoff
, create
, NULL
, &bh
);
208 if (unlikely(ret
< 0))
217 up_write(&NILFS_MDT(sufile
)->mi_sem
);
224 int nilfs_sufile_update(struct inode
*sufile
, __u64 segnum
, int create
,
225 void (*dofunc
)(struct inode
*, __u64
,
226 struct buffer_head
*,
227 struct buffer_head
*))
229 struct buffer_head
*header_bh
, *bh
;
232 if (unlikely(segnum
>= nilfs_sufile_get_nsegments(sufile
))) {
233 nilfs_msg(sufile
->i_sb
, KERN_WARNING
,
234 "%s: invalid segment number: %llu",
235 __func__
, (unsigned long long)segnum
);
238 down_write(&NILFS_MDT(sufile
)->mi_sem
);
240 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
244 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, create
, &bh
);
246 dofunc(sufile
, segnum
, header_bh
, bh
);
252 up_write(&NILFS_MDT(sufile
)->mi_sem
);
257 * nilfs_sufile_set_alloc_range - limit range of segment to be allocated
258 * @sufile: inode of segment usage file
259 * @start: minimum segment number of allocatable region (inclusive)
260 * @end: maximum segment number of allocatable region (inclusive)
262 * Return Value: On success, 0 is returned. On error, one of the
263 * following negative error codes is returned.
265 * %-ERANGE - invalid segment region
267 int nilfs_sufile_set_alloc_range(struct inode
*sufile
, __u64 start
, __u64 end
)
269 struct nilfs_sufile_info
*sui
= NILFS_SUI(sufile
);
273 down_write(&NILFS_MDT(sufile
)->mi_sem
);
274 nsegs
= nilfs_sufile_get_nsegments(sufile
);
276 if (start
<= end
&& end
< nsegs
) {
277 sui
->allocmin
= start
;
281 up_write(&NILFS_MDT(sufile
)->mi_sem
);
286 * nilfs_sufile_alloc - allocate a segment
287 * @sufile: inode of segment usage file
288 * @segnump: pointer to segment number
290 * Description: nilfs_sufile_alloc() allocates a clean segment.
292 * Return Value: On success, 0 is returned and the segment number of the
293 * allocated segment is stored in the place pointed by @segnump. On error, one
294 * of the following negative error codes is returned.
298 * %-ENOMEM - Insufficient amount of memory available.
300 * %-ENOSPC - No clean segment left.
302 int nilfs_sufile_alloc(struct inode
*sufile
, __u64
*segnump
)
304 struct buffer_head
*header_bh
, *su_bh
;
305 struct nilfs_sufile_header
*header
;
306 struct nilfs_segment_usage
*su
;
307 struct nilfs_sufile_info
*sui
= NILFS_SUI(sufile
);
308 size_t susz
= NILFS_MDT(sufile
)->mi_entry_size
;
309 __u64 segnum
, maxsegnum
, last_alloc
;
311 unsigned long nsegments
, nsus
, cnt
;
314 down_write(&NILFS_MDT(sufile
)->mi_sem
);
316 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
319 kaddr
= kmap_atomic(header_bh
->b_page
);
320 header
= kaddr
+ bh_offset(header_bh
);
321 last_alloc
= le64_to_cpu(header
->sh_last_alloc
);
322 kunmap_atomic(kaddr
);
324 nsegments
= nilfs_sufile_get_nsegments(sufile
);
325 maxsegnum
= sui
->allocmax
;
326 segnum
= last_alloc
+ 1;
327 if (segnum
< sui
->allocmin
|| segnum
> sui
->allocmax
)
328 segnum
= sui
->allocmin
;
330 for (cnt
= 0; cnt
< nsegments
; cnt
+= nsus
) {
331 if (segnum
> maxsegnum
) {
332 if (cnt
< sui
->allocmax
- sui
->allocmin
+ 1) {
334 * wrap around in the limited region.
335 * if allocation started from
336 * sui->allocmin, this never happens.
338 segnum
= sui
->allocmin
;
339 maxsegnum
= last_alloc
;
340 } else if (segnum
> sui
->allocmin
&&
341 sui
->allocmax
+ 1 < nsegments
) {
342 segnum
= sui
->allocmax
+ 1;
343 maxsegnum
= nsegments
- 1;
344 } else if (sui
->allocmin
> 0) {
346 maxsegnum
= sui
->allocmin
- 1;
348 break; /* never happens */
351 trace_nilfs2_segment_usage_check(sufile
, segnum
, cnt
);
352 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 1,
356 kaddr
= kmap_atomic(su_bh
->b_page
);
357 su
= nilfs_sufile_block_get_segment_usage(
358 sufile
, segnum
, su_bh
, kaddr
);
360 nsus
= nilfs_sufile_segment_usages_in_block(
361 sufile
, segnum
, maxsegnum
);
362 for (j
= 0; j
< nsus
; j
++, su
= (void *)su
+ susz
, segnum
++) {
363 if (!nilfs_segment_usage_clean(su
))
365 /* found a clean segment */
366 nilfs_segment_usage_set_dirty(su
);
367 kunmap_atomic(kaddr
);
369 kaddr
= kmap_atomic(header_bh
->b_page
);
370 header
= kaddr
+ bh_offset(header_bh
);
371 le64_add_cpu(&header
->sh_ncleansegs
, -1);
372 le64_add_cpu(&header
->sh_ndirtysegs
, 1);
373 header
->sh_last_alloc
= cpu_to_le64(segnum
);
374 kunmap_atomic(kaddr
);
377 mark_buffer_dirty(header_bh
);
378 mark_buffer_dirty(su_bh
);
379 nilfs_mdt_mark_dirty(sufile
);
383 trace_nilfs2_segment_usage_allocated(sufile
, segnum
);
388 kunmap_atomic(kaddr
);
392 /* no segments left */
399 up_write(&NILFS_MDT(sufile
)->mi_sem
);
403 void nilfs_sufile_do_cancel_free(struct inode
*sufile
, __u64 segnum
,
404 struct buffer_head
*header_bh
,
405 struct buffer_head
*su_bh
)
407 struct nilfs_segment_usage
*su
;
410 kaddr
= kmap_atomic(su_bh
->b_page
);
411 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, su_bh
, kaddr
);
412 if (unlikely(!nilfs_segment_usage_clean(su
))) {
413 nilfs_msg(sufile
->i_sb
, KERN_WARNING
,
414 "%s: segment %llu must be clean", __func__
,
415 (unsigned long long)segnum
);
416 kunmap_atomic(kaddr
);
419 nilfs_segment_usage_set_dirty(su
);
420 kunmap_atomic(kaddr
);
422 nilfs_sufile_mod_counter(header_bh
, -1, 1);
423 NILFS_SUI(sufile
)->ncleansegs
--;
425 mark_buffer_dirty(su_bh
);
426 nilfs_mdt_mark_dirty(sufile
);
429 void nilfs_sufile_do_scrap(struct inode
*sufile
, __u64 segnum
,
430 struct buffer_head
*header_bh
,
431 struct buffer_head
*su_bh
)
433 struct nilfs_segment_usage
*su
;
437 kaddr
= kmap_atomic(su_bh
->b_page
);
438 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, su_bh
, kaddr
);
439 if (su
->su_flags
== cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY
)) &&
440 su
->su_nblocks
== cpu_to_le32(0)) {
441 kunmap_atomic(kaddr
);
444 clean
= nilfs_segment_usage_clean(su
);
445 dirty
= nilfs_segment_usage_dirty(su
);
447 /* make the segment garbage */
448 su
->su_lastmod
= cpu_to_le64(0);
449 su
->su_nblocks
= cpu_to_le32(0);
450 su
->su_flags
= cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY
));
451 kunmap_atomic(kaddr
);
453 nilfs_sufile_mod_counter(header_bh
, clean
? (u64
)-1 : 0, dirty
? 0 : 1);
454 NILFS_SUI(sufile
)->ncleansegs
-= clean
;
456 mark_buffer_dirty(su_bh
);
457 nilfs_mdt_mark_dirty(sufile
);
460 void nilfs_sufile_do_free(struct inode
*sufile
, __u64 segnum
,
461 struct buffer_head
*header_bh
,
462 struct buffer_head
*su_bh
)
464 struct nilfs_segment_usage
*su
;
468 kaddr
= kmap_atomic(su_bh
->b_page
);
469 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, su_bh
, kaddr
);
470 if (nilfs_segment_usage_clean(su
)) {
471 nilfs_msg(sufile
->i_sb
, KERN_WARNING
,
472 "%s: segment %llu is already clean",
473 __func__
, (unsigned long long)segnum
);
474 kunmap_atomic(kaddr
);
477 WARN_ON(nilfs_segment_usage_error(su
));
478 WARN_ON(!nilfs_segment_usage_dirty(su
));
480 sudirty
= nilfs_segment_usage_dirty(su
);
481 nilfs_segment_usage_set_clean(su
);
482 kunmap_atomic(kaddr
);
483 mark_buffer_dirty(su_bh
);
485 nilfs_sufile_mod_counter(header_bh
, 1, sudirty
? (u64
)-1 : 0);
486 NILFS_SUI(sufile
)->ncleansegs
++;
488 nilfs_mdt_mark_dirty(sufile
);
490 trace_nilfs2_segment_usage_freed(sufile
, segnum
);
494 * nilfs_sufile_mark_dirty - mark the buffer having a segment usage dirty
495 * @sufile: inode of segment usage file
496 * @segnum: segment number
498 int nilfs_sufile_mark_dirty(struct inode
*sufile
, __u64 segnum
)
500 struct buffer_head
*bh
;
503 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 0, &bh
);
505 mark_buffer_dirty(bh
);
506 nilfs_mdt_mark_dirty(sufile
);
513 * nilfs_sufile_set_segment_usage - set usage of a segment
514 * @sufile: inode of segment usage file
515 * @segnum: segment number
516 * @nblocks: number of live blocks in the segment
517 * @modtime: modification time (option)
519 int nilfs_sufile_set_segment_usage(struct inode
*sufile
, __u64 segnum
,
520 unsigned long nblocks
, time64_t modtime
)
522 struct buffer_head
*bh
;
523 struct nilfs_segment_usage
*su
;
527 down_write(&NILFS_MDT(sufile
)->mi_sem
);
528 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 0, &bh
);
532 kaddr
= kmap_atomic(bh
->b_page
);
533 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, bh
, kaddr
);
534 WARN_ON(nilfs_segment_usage_error(su
));
536 su
->su_lastmod
= cpu_to_le64(modtime
);
537 su
->su_nblocks
= cpu_to_le32(nblocks
);
538 kunmap_atomic(kaddr
);
540 mark_buffer_dirty(bh
);
541 nilfs_mdt_mark_dirty(sufile
);
545 up_write(&NILFS_MDT(sufile
)->mi_sem
);
550 * nilfs_sufile_get_stat - get segment usage statistics
551 * @sufile: inode of segment usage file
552 * @stat: pointer to a structure of segment usage statistics
554 * Description: nilfs_sufile_get_stat() returns information about segment
557 * Return Value: On success, 0 is returned, and segment usage information is
558 * stored in the place pointed by @stat. On error, one of the following
559 * negative error codes is returned.
563 * %-ENOMEM - Insufficient amount of memory available.
565 int nilfs_sufile_get_stat(struct inode
*sufile
, struct nilfs_sustat
*sustat
)
567 struct buffer_head
*header_bh
;
568 struct nilfs_sufile_header
*header
;
569 struct the_nilfs
*nilfs
= sufile
->i_sb
->s_fs_info
;
573 down_read(&NILFS_MDT(sufile
)->mi_sem
);
575 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
579 kaddr
= kmap_atomic(header_bh
->b_page
);
580 header
= kaddr
+ bh_offset(header_bh
);
581 sustat
->ss_nsegs
= nilfs_sufile_get_nsegments(sufile
);
582 sustat
->ss_ncleansegs
= le64_to_cpu(header
->sh_ncleansegs
);
583 sustat
->ss_ndirtysegs
= le64_to_cpu(header
->sh_ndirtysegs
);
584 sustat
->ss_ctime
= nilfs
->ns_ctime
;
585 sustat
->ss_nongc_ctime
= nilfs
->ns_nongc_ctime
;
586 spin_lock(&nilfs
->ns_last_segment_lock
);
587 sustat
->ss_prot_seq
= nilfs
->ns_prot_seq
;
588 spin_unlock(&nilfs
->ns_last_segment_lock
);
589 kunmap_atomic(kaddr
);
593 up_read(&NILFS_MDT(sufile
)->mi_sem
);
597 void nilfs_sufile_do_set_error(struct inode
*sufile
, __u64 segnum
,
598 struct buffer_head
*header_bh
,
599 struct buffer_head
*su_bh
)
601 struct nilfs_segment_usage
*su
;
605 kaddr
= kmap_atomic(su_bh
->b_page
);
606 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
, su_bh
, kaddr
);
607 if (nilfs_segment_usage_error(su
)) {
608 kunmap_atomic(kaddr
);
611 suclean
= nilfs_segment_usage_clean(su
);
612 nilfs_segment_usage_set_error(su
);
613 kunmap_atomic(kaddr
);
616 nilfs_sufile_mod_counter(header_bh
, -1, 0);
617 NILFS_SUI(sufile
)->ncleansegs
--;
619 mark_buffer_dirty(su_bh
);
620 nilfs_mdt_mark_dirty(sufile
);
624 * nilfs_sufile_truncate_range - truncate range of segment array
625 * @sufile: inode of segment usage file
626 * @start: start segment number (inclusive)
627 * @end: end segment number (inclusive)
629 * Return Value: On success, 0 is returned. On error, one of the
630 * following negative error codes is returned.
634 * %-ENOMEM - Insufficient amount of memory available.
636 * %-EINVAL - Invalid number of segments specified
638 * %-EBUSY - Dirty or active segments are present in the range
640 static int nilfs_sufile_truncate_range(struct inode
*sufile
,
641 __u64 start
, __u64 end
)
643 struct the_nilfs
*nilfs
= sufile
->i_sb
->s_fs_info
;
644 struct buffer_head
*header_bh
;
645 struct buffer_head
*su_bh
;
646 struct nilfs_segment_usage
*su
, *su2
;
647 size_t susz
= NILFS_MDT(sufile
)->mi_entry_size
;
648 unsigned long segusages_per_block
;
649 unsigned long nsegs
, ncleaned
;
656 nsegs
= nilfs_sufile_get_nsegments(sufile
);
659 if (start
> end
|| start
>= nsegs
)
662 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
666 segusages_per_block
= nilfs_sufile_segment_usages_per_block(sufile
);
669 for (segnum
= start
; segnum
<= end
; segnum
+= n
) {
670 n
= min_t(unsigned long,
671 segusages_per_block
-
672 nilfs_sufile_get_offset(sufile
, segnum
),
674 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 0,
682 kaddr
= kmap_atomic(su_bh
->b_page
);
683 su
= nilfs_sufile_block_get_segment_usage(
684 sufile
, segnum
, su_bh
, kaddr
);
686 for (j
= 0; j
< n
; j
++, su
= (void *)su
+ susz
) {
687 if ((le32_to_cpu(su
->su_flags
) &
688 ~BIT(NILFS_SEGMENT_USAGE_ERROR
)) ||
689 nilfs_segment_is_active(nilfs
, segnum
+ j
)) {
691 kunmap_atomic(kaddr
);
697 for (su
= su2
, j
= 0; j
< n
; j
++, su
= (void *)su
+ susz
) {
698 if (nilfs_segment_usage_error(su
)) {
699 nilfs_segment_usage_set_clean(su
);
703 kunmap_atomic(kaddr
);
705 mark_buffer_dirty(su_bh
);
710 if (n
== segusages_per_block
) {
712 nilfs_sufile_delete_segment_usage_block(sufile
, segnum
);
719 NILFS_SUI(sufile
)->ncleansegs
+= ncleaned
;
720 nilfs_sufile_mod_counter(header_bh
, ncleaned
, 0);
721 nilfs_mdt_mark_dirty(sufile
);
729 * nilfs_sufile_resize - resize segment array
730 * @sufile: inode of segment usage file
731 * @newnsegs: new number of segments
733 * Return Value: On success, 0 is returned. On error, one of the
734 * following negative error codes is returned.
738 * %-ENOMEM - Insufficient amount of memory available.
740 * %-ENOSPC - Enough free space is not left for shrinking
742 * %-EBUSY - Dirty or active segments exist in the region to be truncated
744 int nilfs_sufile_resize(struct inode
*sufile
, __u64 newnsegs
)
746 struct the_nilfs
*nilfs
= sufile
->i_sb
->s_fs_info
;
747 struct buffer_head
*header_bh
;
748 struct nilfs_sufile_header
*header
;
749 struct nilfs_sufile_info
*sui
= NILFS_SUI(sufile
);
751 unsigned long nsegs
, nrsvsegs
;
754 down_write(&NILFS_MDT(sufile
)->mi_sem
);
756 nsegs
= nilfs_sufile_get_nsegments(sufile
);
757 if (nsegs
== newnsegs
)
761 nrsvsegs
= nilfs_nrsvsegs(nilfs
, newnsegs
);
762 if (newnsegs
< nsegs
&& nsegs
- newnsegs
+ nrsvsegs
> sui
->ncleansegs
)
765 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
769 if (newnsegs
> nsegs
) {
770 sui
->ncleansegs
+= newnsegs
- nsegs
;
771 } else /* newnsegs < nsegs */ {
772 ret
= nilfs_sufile_truncate_range(sufile
, newnsegs
, nsegs
- 1);
776 sui
->ncleansegs
-= nsegs
- newnsegs
;
779 kaddr
= kmap_atomic(header_bh
->b_page
);
780 header
= kaddr
+ bh_offset(header_bh
);
781 header
->sh_ncleansegs
= cpu_to_le64(sui
->ncleansegs
);
782 kunmap_atomic(kaddr
);
784 mark_buffer_dirty(header_bh
);
785 nilfs_mdt_mark_dirty(sufile
);
786 nilfs_set_nsegments(nilfs
, newnsegs
);
791 up_write(&NILFS_MDT(sufile
)->mi_sem
);
796 * nilfs_sufile_get_suinfo -
797 * @sufile: inode of segment usage file
798 * @segnum: segment number to start looking
799 * @buf: array of suinfo
800 * @sisz: byte size of suinfo
801 * @nsi: size of suinfo array
805 * Return Value: On success, 0 is returned and .... On error, one of the
806 * following negative error codes is returned.
810 * %-ENOMEM - Insufficient amount of memory available.
812 ssize_t
nilfs_sufile_get_suinfo(struct inode
*sufile
, __u64 segnum
, void *buf
,
813 unsigned int sisz
, size_t nsi
)
815 struct buffer_head
*su_bh
;
816 struct nilfs_segment_usage
*su
;
817 struct nilfs_suinfo
*si
= buf
;
818 size_t susz
= NILFS_MDT(sufile
)->mi_entry_size
;
819 struct the_nilfs
*nilfs
= sufile
->i_sb
->s_fs_info
;
821 unsigned long nsegs
, segusages_per_block
;
825 down_read(&NILFS_MDT(sufile
)->mi_sem
);
827 segusages_per_block
= nilfs_sufile_segment_usages_per_block(sufile
);
828 nsegs
= min_t(unsigned long,
829 nilfs_sufile_get_nsegments(sufile
) - segnum
,
831 for (i
= 0; i
< nsegs
; i
+= n
, segnum
+= n
) {
832 n
= min_t(unsigned long,
833 segusages_per_block
-
834 nilfs_sufile_get_offset(sufile
, segnum
),
836 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 0,
842 memset(si
, 0, sisz
* n
);
843 si
= (void *)si
+ sisz
* n
;
847 kaddr
= kmap_atomic(su_bh
->b_page
);
848 su
= nilfs_sufile_block_get_segment_usage(
849 sufile
, segnum
, su_bh
, kaddr
);
851 j
++, su
= (void *)su
+ susz
, si
= (void *)si
+ sisz
) {
852 si
->sui_lastmod
= le64_to_cpu(su
->su_lastmod
);
853 si
->sui_nblocks
= le32_to_cpu(su
->su_nblocks
);
854 si
->sui_flags
= le32_to_cpu(su
->su_flags
) &
855 ~BIT(NILFS_SEGMENT_USAGE_ACTIVE
);
856 if (nilfs_segment_is_active(nilfs
, segnum
+ j
))
858 BIT(NILFS_SEGMENT_USAGE_ACTIVE
);
860 kunmap_atomic(kaddr
);
866 up_read(&NILFS_MDT(sufile
)->mi_sem
);
871 * nilfs_sufile_set_suinfo - sets segment usage info
872 * @sufile: inode of segment usage file
873 * @buf: array of suinfo_update
874 * @supsz: byte size of suinfo_update
875 * @nsup: size of suinfo_update array
877 * Description: Takes an array of nilfs_suinfo_update structs and updates
878 * segment usage accordingly. Only the fields indicated by the sup_flags
881 * Return Value: On success, 0 is returned. On error, one of the
882 * following negative error codes is returned.
886 * %-ENOMEM - Insufficient amount of memory available.
888 * %-EINVAL - Invalid values in input (segment number, flags or nblocks)
890 ssize_t
nilfs_sufile_set_suinfo(struct inode
*sufile
, void *buf
,
891 unsigned int supsz
, size_t nsup
)
893 struct the_nilfs
*nilfs
= sufile
->i_sb
->s_fs_info
;
894 struct buffer_head
*header_bh
, *bh
;
895 struct nilfs_suinfo_update
*sup
, *supend
= buf
+ supsz
* nsup
;
896 struct nilfs_segment_usage
*su
;
898 unsigned long blkoff
, prev_blkoff
;
899 int cleansi
, cleansu
, dirtysi
, dirtysu
;
900 long ncleaned
= 0, ndirtied
= 0;
903 if (unlikely(nsup
== 0))
906 for (sup
= buf
; sup
< supend
; sup
= (void *)sup
+ supsz
) {
907 if (sup
->sup_segnum
>= nilfs
->ns_nsegments
909 (~0UL << __NR_NILFS_SUINFO_UPDATE_FIELDS
))
910 || (nilfs_suinfo_update_nblocks(sup
) &&
911 sup
->sup_sui
.sui_nblocks
>
912 nilfs
->ns_blocks_per_segment
))
916 down_write(&NILFS_MDT(sufile
)->mi_sem
);
918 ret
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
923 blkoff
= nilfs_sufile_get_blkoff(sufile
, sup
->sup_segnum
);
924 ret
= nilfs_mdt_get_block(sufile
, blkoff
, 1, NULL
, &bh
);
929 kaddr
= kmap_atomic(bh
->b_page
);
930 su
= nilfs_sufile_block_get_segment_usage(
931 sufile
, sup
->sup_segnum
, bh
, kaddr
);
933 if (nilfs_suinfo_update_lastmod(sup
))
934 su
->su_lastmod
= cpu_to_le64(sup
->sup_sui
.sui_lastmod
);
936 if (nilfs_suinfo_update_nblocks(sup
))
937 su
->su_nblocks
= cpu_to_le32(sup
->sup_sui
.sui_nblocks
);
939 if (nilfs_suinfo_update_flags(sup
)) {
941 * Active flag is a virtual flag projected by running
942 * nilfs kernel code - drop it not to write it to
945 sup
->sup_sui
.sui_flags
&=
946 ~BIT(NILFS_SEGMENT_USAGE_ACTIVE
);
948 cleansi
= nilfs_suinfo_clean(&sup
->sup_sui
);
949 cleansu
= nilfs_segment_usage_clean(su
);
950 dirtysi
= nilfs_suinfo_dirty(&sup
->sup_sui
);
951 dirtysu
= nilfs_segment_usage_dirty(su
);
953 if (cleansi
&& !cleansu
)
955 else if (!cleansi
&& cleansu
)
958 if (dirtysi
&& !dirtysu
)
960 else if (!dirtysi
&& dirtysu
)
963 su
->su_flags
= cpu_to_le32(sup
->sup_sui
.sui_flags
);
966 kunmap_atomic(kaddr
);
968 sup
= (void *)sup
+ supsz
;
972 prev_blkoff
= blkoff
;
973 blkoff
= nilfs_sufile_get_blkoff(sufile
, sup
->sup_segnum
);
974 if (blkoff
== prev_blkoff
)
977 /* get different block */
978 mark_buffer_dirty(bh
);
980 ret
= nilfs_mdt_get_block(sufile
, blkoff
, 1, NULL
, &bh
);
981 if (unlikely(ret
< 0))
984 mark_buffer_dirty(bh
);
988 if (ncleaned
|| ndirtied
) {
989 nilfs_sufile_mod_counter(header_bh
, (u64
)ncleaned
,
991 NILFS_SUI(sufile
)->ncleansegs
+= ncleaned
;
993 nilfs_mdt_mark_dirty(sufile
);
997 up_write(&NILFS_MDT(sufile
)->mi_sem
);
1002 * nilfs_sufile_trim_fs() - trim ioctl handle function
1003 * @sufile: inode of segment usage file
1004 * @range: fstrim_range structure
1006 * start: First Byte to trim
1007 * len: number of Bytes to trim from start
1008 * minlen: minimum extent length in Bytes
1010 * Decription: nilfs_sufile_trim_fs goes through all segments containing bytes
1011 * from start to start+len. start is rounded up to the next block boundary
1012 * and start+len is rounded down. For each clean segment blkdev_issue_discard
1013 * function is invoked.
1015 * Return Value: On success, 0 is returned or negative error code, otherwise.
1017 int nilfs_sufile_trim_fs(struct inode
*sufile
, struct fstrim_range
*range
)
1019 struct the_nilfs
*nilfs
= sufile
->i_sb
->s_fs_info
;
1020 struct buffer_head
*su_bh
;
1021 struct nilfs_segment_usage
*su
;
1023 size_t n
, i
, susz
= NILFS_MDT(sufile
)->mi_entry_size
;
1024 sector_t seg_start
, seg_end
, start_block
, end_block
;
1025 sector_t start
= 0, nblocks
= 0;
1026 u64 segnum
, segnum_end
, minlen
, len
, max_blocks
, ndiscarded
= 0;
1028 unsigned int sects_per_block
;
1030 sects_per_block
= (1 << nilfs
->ns_blocksize_bits
) /
1031 bdev_logical_block_size(nilfs
->ns_bdev
);
1032 len
= range
->len
>> nilfs
->ns_blocksize_bits
;
1033 minlen
= range
->minlen
>> nilfs
->ns_blocksize_bits
;
1034 max_blocks
= ((u64
)nilfs
->ns_nsegments
* nilfs
->ns_blocks_per_segment
);
1036 if (!len
|| range
->start
>= max_blocks
<< nilfs
->ns_blocksize_bits
)
1039 start_block
= (range
->start
+ nilfs
->ns_blocksize
- 1) >>
1040 nilfs
->ns_blocksize_bits
;
1043 * range->len can be very large (actually, it is set to
1044 * ULLONG_MAX by default) - truncate upper end of the range
1045 * carefully so as not to overflow.
1047 if (max_blocks
- start_block
< len
)
1048 end_block
= max_blocks
- 1;
1050 end_block
= start_block
+ len
- 1;
1052 segnum
= nilfs_get_segnum_of_block(nilfs
, start_block
);
1053 segnum_end
= nilfs_get_segnum_of_block(nilfs
, end_block
);
1055 down_read(&NILFS_MDT(sufile
)->mi_sem
);
1057 while (segnum
<= segnum_end
) {
1058 n
= nilfs_sufile_segment_usages_in_block(sufile
, segnum
,
1061 ret
= nilfs_sufile_get_segment_usage_block(sufile
, segnum
, 0,
1071 kaddr
= kmap_atomic(su_bh
->b_page
);
1072 su
= nilfs_sufile_block_get_segment_usage(sufile
, segnum
,
1074 for (i
= 0; i
< n
; ++i
, ++segnum
, su
= (void *)su
+ susz
) {
1075 if (!nilfs_segment_usage_clean(su
))
1078 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
,
1082 /* start new extent */
1084 nblocks
= seg_end
- seg_start
+ 1;
1088 if (start
+ nblocks
== seg_start
) {
1089 /* add to previous extent */
1090 nblocks
+= seg_end
- seg_start
+ 1;
1094 /* discard previous extent */
1095 if (start
< start_block
) {
1096 nblocks
-= start_block
- start
;
1097 start
= start_block
;
1100 if (nblocks
>= minlen
) {
1101 kunmap_atomic(kaddr
);
1103 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
1104 start
* sects_per_block
,
1105 nblocks
* sects_per_block
,
1112 ndiscarded
+= nblocks
;
1113 kaddr
= kmap_atomic(su_bh
->b_page
);
1114 su
= nilfs_sufile_block_get_segment_usage(
1115 sufile
, segnum
, su_bh
, kaddr
);
1118 /* start new extent */
1120 nblocks
= seg_end
- seg_start
+ 1;
1122 kunmap_atomic(kaddr
);
1128 /* discard last extent */
1129 if (start
< start_block
) {
1130 nblocks
-= start_block
- start
;
1131 start
= start_block
;
1133 if (start
+ nblocks
> end_block
+ 1)
1134 nblocks
= end_block
- start
+ 1;
1136 if (nblocks
>= minlen
) {
1137 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
1138 start
* sects_per_block
,
1139 nblocks
* sects_per_block
,
1142 ndiscarded
+= nblocks
;
1147 up_read(&NILFS_MDT(sufile
)->mi_sem
);
1149 range
->len
= ndiscarded
<< nilfs
->ns_blocksize_bits
;
1154 * nilfs_sufile_read - read or get sufile inode
1155 * @sb: super block instance
1156 * @susize: size of a segment usage entry
1157 * @raw_inode: on-disk sufile inode
1158 * @inodep: buffer to store the inode
1160 int nilfs_sufile_read(struct super_block
*sb
, size_t susize
,
1161 struct nilfs_inode
*raw_inode
, struct inode
**inodep
)
1163 struct inode
*sufile
;
1164 struct nilfs_sufile_info
*sui
;
1165 struct buffer_head
*header_bh
;
1166 struct nilfs_sufile_header
*header
;
1170 if (susize
> sb
->s_blocksize
) {
1171 nilfs_msg(sb
, KERN_ERR
,
1172 "too large segment usage size: %zu bytes", susize
);
1174 } else if (susize
< NILFS_MIN_SEGMENT_USAGE_SIZE
) {
1175 nilfs_msg(sb
, KERN_ERR
,
1176 "too small segment usage size: %zu bytes", susize
);
1180 sufile
= nilfs_iget_locked(sb
, NULL
, NILFS_SUFILE_INO
);
1181 if (unlikely(!sufile
))
1183 if (!(sufile
->i_state
& I_NEW
))
1186 err
= nilfs_mdt_init(sufile
, NILFS_MDT_GFP
, sizeof(*sui
));
1190 nilfs_mdt_set_entry_size(sufile
, susize
,
1191 sizeof(struct nilfs_sufile_header
));
1193 err
= nilfs_read_inode_common(sufile
, raw_inode
);
1197 err
= nilfs_sufile_get_header_block(sufile
, &header_bh
);
1201 sui
= NILFS_SUI(sufile
);
1202 kaddr
= kmap_atomic(header_bh
->b_page
);
1203 header
= kaddr
+ bh_offset(header_bh
);
1204 sui
->ncleansegs
= le64_to_cpu(header
->sh_ncleansegs
);
1205 kunmap_atomic(kaddr
);
1208 sui
->allocmax
= nilfs_sufile_get_nsegments(sufile
) - 1;
1211 unlock_new_inode(sufile
);
1216 iget_failed(sufile
);