1 // SPDX-License-Identifier: GPL-2.0+
3 * 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_warn(sufile->i_sb,
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_warn(sufile->i_sb, "%s: invalid segment number: %llu",
234 __func__, (unsigned long long)segnum);
237 down_write(&NILFS_MDT(sufile)->mi_sem);
239 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
243 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, create, &bh);
245 dofunc(sufile, segnum, header_bh, bh);
251 up_write(&NILFS_MDT(sufile)->mi_sem);
256 * nilfs_sufile_set_alloc_range - limit range of segment to be allocated
257 * @sufile: inode of segment usage file
258 * @start: minimum segment number of allocatable region (inclusive)
259 * @end: maximum segment number of allocatable region (inclusive)
261 * Return Value: On success, 0 is returned. On error, one of the
262 * following negative error codes is returned.
264 * %-ERANGE - invalid segment region
266 int nilfs_sufile_set_alloc_range(struct inode *sufile, __u64 start, __u64 end)
268 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
272 down_write(&NILFS_MDT(sufile)->mi_sem);
273 nsegs = nilfs_sufile_get_nsegments(sufile);
275 if (start <= end && end < nsegs) {
276 sui->allocmin = start;
280 up_write(&NILFS_MDT(sufile)->mi_sem);
285 * nilfs_sufile_alloc - allocate a segment
286 * @sufile: inode of segment usage file
287 * @segnump: pointer to segment number
289 * Description: nilfs_sufile_alloc() allocates a clean segment.
291 * Return Value: On success, 0 is returned and the segment number of the
292 * allocated segment is stored in the place pointed by @segnump. On error, one
293 * of the following negative error codes is returned.
297 * %-ENOMEM - Insufficient amount of memory available.
299 * %-ENOSPC - No clean segment left.
301 int nilfs_sufile_alloc(struct inode *sufile, __u64 *segnump)
303 struct buffer_head *header_bh, *su_bh;
304 struct nilfs_sufile_header *header;
305 struct nilfs_segment_usage *su;
306 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
307 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
308 __u64 segnum, maxsegnum, last_alloc;
310 unsigned long nsegments, nsus, cnt;
313 down_write(&NILFS_MDT(sufile)->mi_sem);
315 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
318 kaddr = kmap_atomic(header_bh->b_page);
319 header = kaddr + bh_offset(header_bh);
320 last_alloc = le64_to_cpu(header->sh_last_alloc);
321 kunmap_atomic(kaddr);
323 nsegments = nilfs_sufile_get_nsegments(sufile);
324 maxsegnum = sui->allocmax;
325 segnum = last_alloc + 1;
326 if (segnum < sui->allocmin || segnum > sui->allocmax)
327 segnum = sui->allocmin;
329 for (cnt = 0; cnt < nsegments; cnt += nsus) {
330 if (segnum > maxsegnum) {
331 if (cnt < sui->allocmax - sui->allocmin + 1) {
333 * wrap around in the limited region.
334 * if allocation started from
335 * sui->allocmin, this never happens.
337 segnum = sui->allocmin;
338 maxsegnum = last_alloc;
339 } else if (segnum > sui->allocmin &&
340 sui->allocmax + 1 < nsegments) {
341 segnum = sui->allocmax + 1;
342 maxsegnum = nsegments - 1;
343 } else if (sui->allocmin > 0) {
345 maxsegnum = sui->allocmin - 1;
347 break; /* never happens */
350 trace_nilfs2_segment_usage_check(sufile, segnum, cnt);
351 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1,
355 kaddr = kmap_atomic(su_bh->b_page);
356 su = nilfs_sufile_block_get_segment_usage(
357 sufile, segnum, su_bh, kaddr);
359 nsus = nilfs_sufile_segment_usages_in_block(
360 sufile, segnum, maxsegnum);
361 for (j = 0; j < nsus; j++, su = (void *)su + susz, segnum++) {
362 if (!nilfs_segment_usage_clean(su))
364 /* found a clean segment */
365 nilfs_segment_usage_set_dirty(su);
366 kunmap_atomic(kaddr);
368 kaddr = kmap_atomic(header_bh->b_page);
369 header = kaddr + bh_offset(header_bh);
370 le64_add_cpu(&header->sh_ncleansegs, -1);
371 le64_add_cpu(&header->sh_ndirtysegs, 1);
372 header->sh_last_alloc = cpu_to_le64(segnum);
373 kunmap_atomic(kaddr);
376 mark_buffer_dirty(header_bh);
377 mark_buffer_dirty(su_bh);
378 nilfs_mdt_mark_dirty(sufile);
382 trace_nilfs2_segment_usage_allocated(sufile, segnum);
387 kunmap_atomic(kaddr);
391 /* no segments left */
398 up_write(&NILFS_MDT(sufile)->mi_sem);
402 void nilfs_sufile_do_cancel_free(struct inode *sufile, __u64 segnum,
403 struct buffer_head *header_bh,
404 struct buffer_head *su_bh)
406 struct nilfs_segment_usage *su;
409 kaddr = kmap_atomic(su_bh->b_page);
410 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
411 if (unlikely(!nilfs_segment_usage_clean(su))) {
412 nilfs_warn(sufile->i_sb, "%s: segment %llu must be clean",
413 __func__, (unsigned long long)segnum);
414 kunmap_atomic(kaddr);
417 nilfs_segment_usage_set_dirty(su);
418 kunmap_atomic(kaddr);
420 nilfs_sufile_mod_counter(header_bh, -1, 1);
421 NILFS_SUI(sufile)->ncleansegs--;
423 mark_buffer_dirty(su_bh);
424 nilfs_mdt_mark_dirty(sufile);
427 void nilfs_sufile_do_scrap(struct inode *sufile, __u64 segnum,
428 struct buffer_head *header_bh,
429 struct buffer_head *su_bh)
431 struct nilfs_segment_usage *su;
435 kaddr = kmap_atomic(su_bh->b_page);
436 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
437 if (su->su_flags == cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY)) &&
438 su->su_nblocks == cpu_to_le32(0)) {
439 kunmap_atomic(kaddr);
442 clean = nilfs_segment_usage_clean(su);
443 dirty = nilfs_segment_usage_dirty(su);
445 /* make the segment garbage */
446 su->su_lastmod = cpu_to_le64(0);
447 su->su_nblocks = cpu_to_le32(0);
448 su->su_flags = cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY));
449 kunmap_atomic(kaddr);
451 nilfs_sufile_mod_counter(header_bh, clean ? (u64)-1 : 0, dirty ? 0 : 1);
452 NILFS_SUI(sufile)->ncleansegs -= clean;
454 mark_buffer_dirty(su_bh);
455 nilfs_mdt_mark_dirty(sufile);
458 void nilfs_sufile_do_free(struct inode *sufile, __u64 segnum,
459 struct buffer_head *header_bh,
460 struct buffer_head *su_bh)
462 struct nilfs_segment_usage *su;
466 kaddr = kmap_atomic(su_bh->b_page);
467 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
468 if (nilfs_segment_usage_clean(su)) {
469 nilfs_warn(sufile->i_sb, "%s: segment %llu is already clean",
470 __func__, (unsigned long long)segnum);
471 kunmap_atomic(kaddr);
474 WARN_ON(nilfs_segment_usage_error(su));
475 WARN_ON(!nilfs_segment_usage_dirty(su));
477 sudirty = nilfs_segment_usage_dirty(su);
478 nilfs_segment_usage_set_clean(su);
479 kunmap_atomic(kaddr);
480 mark_buffer_dirty(su_bh);
482 nilfs_sufile_mod_counter(header_bh, 1, sudirty ? (u64)-1 : 0);
483 NILFS_SUI(sufile)->ncleansegs++;
485 nilfs_mdt_mark_dirty(sufile);
487 trace_nilfs2_segment_usage_freed(sufile, segnum);
491 * nilfs_sufile_mark_dirty - mark the buffer having a segment usage dirty
492 * @sufile: inode of segment usage file
493 * @segnum: segment number
495 int nilfs_sufile_mark_dirty(struct inode *sufile, __u64 segnum)
497 struct buffer_head *bh;
499 struct nilfs_segment_usage *su;
502 down_write(&NILFS_MDT(sufile)->mi_sem);
503 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
505 mark_buffer_dirty(bh);
506 nilfs_mdt_mark_dirty(sufile);
507 kaddr = kmap_atomic(bh->b_page);
508 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
509 nilfs_segment_usage_set_dirty(su);
510 kunmap_atomic(kaddr);
513 up_write(&NILFS_MDT(sufile)->mi_sem);
518 * nilfs_sufile_set_segment_usage - set usage of a segment
519 * @sufile: inode of segment usage file
520 * @segnum: segment number
521 * @nblocks: number of live blocks in the segment
522 * @modtime: modification time (option)
524 int nilfs_sufile_set_segment_usage(struct inode *sufile, __u64 segnum,
525 unsigned long nblocks, time64_t modtime)
527 struct buffer_head *bh;
528 struct nilfs_segment_usage *su;
532 down_write(&NILFS_MDT(sufile)->mi_sem);
533 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
537 kaddr = kmap_atomic(bh->b_page);
538 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
539 WARN_ON(nilfs_segment_usage_error(su));
541 su->su_lastmod = cpu_to_le64(modtime);
542 su->su_nblocks = cpu_to_le32(nblocks);
543 kunmap_atomic(kaddr);
545 mark_buffer_dirty(bh);
546 nilfs_mdt_mark_dirty(sufile);
550 up_write(&NILFS_MDT(sufile)->mi_sem);
555 * nilfs_sufile_get_stat - get segment usage statistics
556 * @sufile: inode of segment usage file
557 * @sustat: pointer to a structure of segment usage statistics
559 * Description: nilfs_sufile_get_stat() returns information about segment
562 * Return Value: On success, 0 is returned, and segment usage information is
563 * stored in the place pointed by @sustat. On error, one of the following
564 * negative error codes is returned.
568 * %-ENOMEM - Insufficient amount of memory available.
570 int nilfs_sufile_get_stat(struct inode *sufile, struct nilfs_sustat *sustat)
572 struct buffer_head *header_bh;
573 struct nilfs_sufile_header *header;
574 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
578 down_read(&NILFS_MDT(sufile)->mi_sem);
580 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
584 kaddr = kmap_atomic(header_bh->b_page);
585 header = kaddr + bh_offset(header_bh);
586 sustat->ss_nsegs = nilfs_sufile_get_nsegments(sufile);
587 sustat->ss_ncleansegs = le64_to_cpu(header->sh_ncleansegs);
588 sustat->ss_ndirtysegs = le64_to_cpu(header->sh_ndirtysegs);
589 sustat->ss_ctime = nilfs->ns_ctime;
590 sustat->ss_nongc_ctime = nilfs->ns_nongc_ctime;
591 spin_lock(&nilfs->ns_last_segment_lock);
592 sustat->ss_prot_seq = nilfs->ns_prot_seq;
593 spin_unlock(&nilfs->ns_last_segment_lock);
594 kunmap_atomic(kaddr);
598 up_read(&NILFS_MDT(sufile)->mi_sem);
602 void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
603 struct buffer_head *header_bh,
604 struct buffer_head *su_bh)
606 struct nilfs_segment_usage *su;
610 kaddr = kmap_atomic(su_bh->b_page);
611 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
612 if (nilfs_segment_usage_error(su)) {
613 kunmap_atomic(kaddr);
616 suclean = nilfs_segment_usage_clean(su);
617 nilfs_segment_usage_set_error(su);
618 kunmap_atomic(kaddr);
621 nilfs_sufile_mod_counter(header_bh, -1, 0);
622 NILFS_SUI(sufile)->ncleansegs--;
624 mark_buffer_dirty(su_bh);
625 nilfs_mdt_mark_dirty(sufile);
629 * nilfs_sufile_truncate_range - truncate range of segment array
630 * @sufile: inode of segment usage file
631 * @start: start segment number (inclusive)
632 * @end: end segment number (inclusive)
634 * Return Value: On success, 0 is returned. On error, one of the
635 * following negative error codes is returned.
639 * %-ENOMEM - Insufficient amount of memory available.
641 * %-EINVAL - Invalid number of segments specified
643 * %-EBUSY - Dirty or active segments are present in the range
645 static int nilfs_sufile_truncate_range(struct inode *sufile,
646 __u64 start, __u64 end)
648 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
649 struct buffer_head *header_bh;
650 struct buffer_head *su_bh;
651 struct nilfs_segment_usage *su, *su2;
652 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
653 unsigned long segusages_per_block;
654 unsigned long nsegs, ncleaned;
661 nsegs = nilfs_sufile_get_nsegments(sufile);
664 if (start > end || start >= nsegs)
667 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
671 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
674 for (segnum = start; segnum <= end; segnum += n) {
675 n = min_t(unsigned long,
676 segusages_per_block -
677 nilfs_sufile_get_offset(sufile, segnum),
679 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
687 kaddr = kmap_atomic(su_bh->b_page);
688 su = nilfs_sufile_block_get_segment_usage(
689 sufile, segnum, su_bh, kaddr);
691 for (j = 0; j < n; j++, su = (void *)su + susz) {
692 if ((le32_to_cpu(su->su_flags) &
693 ~BIT(NILFS_SEGMENT_USAGE_ERROR)) ||
694 nilfs_segment_is_active(nilfs, segnum + j)) {
696 kunmap_atomic(kaddr);
702 for (su = su2, j = 0; j < n; j++, su = (void *)su + susz) {
703 if (nilfs_segment_usage_error(su)) {
704 nilfs_segment_usage_set_clean(su);
708 kunmap_atomic(kaddr);
710 mark_buffer_dirty(su_bh);
715 if (n == segusages_per_block) {
717 nilfs_sufile_delete_segment_usage_block(sufile, segnum);
724 NILFS_SUI(sufile)->ncleansegs += ncleaned;
725 nilfs_sufile_mod_counter(header_bh, ncleaned, 0);
726 nilfs_mdt_mark_dirty(sufile);
734 * nilfs_sufile_resize - resize segment array
735 * @sufile: inode of segment usage file
736 * @newnsegs: new number of segments
738 * Return Value: On success, 0 is returned. On error, one of the
739 * following negative error codes is returned.
743 * %-ENOMEM - Insufficient amount of memory available.
745 * %-ENOSPC - Enough free space is not left for shrinking
747 * %-EBUSY - Dirty or active segments exist in the region to be truncated
749 int nilfs_sufile_resize(struct inode *sufile, __u64 newnsegs)
751 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
752 struct buffer_head *header_bh;
753 struct nilfs_sufile_header *header;
754 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
756 unsigned long nsegs, nrsvsegs;
759 down_write(&NILFS_MDT(sufile)->mi_sem);
761 nsegs = nilfs_sufile_get_nsegments(sufile);
762 if (nsegs == newnsegs)
766 nrsvsegs = nilfs_nrsvsegs(nilfs, newnsegs);
767 if (newnsegs < nsegs && nsegs - newnsegs + nrsvsegs > sui->ncleansegs)
770 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
774 if (newnsegs > nsegs) {
775 sui->ncleansegs += newnsegs - nsegs;
776 } else /* newnsegs < nsegs */ {
777 ret = nilfs_sufile_truncate_range(sufile, newnsegs, nsegs - 1);
781 sui->ncleansegs -= nsegs - newnsegs;
784 * If the sufile is successfully truncated, immediately adjust
785 * the segment allocation space while locking the semaphore
786 * "mi_sem" so that nilfs_sufile_alloc() never allocates
787 * segments in the truncated space.
789 sui->allocmax = newnsegs - 1;
793 kaddr = kmap_atomic(header_bh->b_page);
794 header = kaddr + bh_offset(header_bh);
795 header->sh_ncleansegs = cpu_to_le64(sui->ncleansegs);
796 kunmap_atomic(kaddr);
798 mark_buffer_dirty(header_bh);
799 nilfs_mdt_mark_dirty(sufile);
800 nilfs_set_nsegments(nilfs, newnsegs);
805 up_write(&NILFS_MDT(sufile)->mi_sem);
810 * nilfs_sufile_get_suinfo -
811 * @sufile: inode of segment usage file
812 * @segnum: segment number to start looking
813 * @buf: array of suinfo
814 * @sisz: byte size of suinfo
815 * @nsi: size of suinfo array
819 * Return Value: On success, 0 is returned and .... On error, one of the
820 * following negative error codes is returned.
824 * %-ENOMEM - Insufficient amount of memory available.
826 ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, void *buf,
827 unsigned int sisz, size_t nsi)
829 struct buffer_head *su_bh;
830 struct nilfs_segment_usage *su;
831 struct nilfs_suinfo *si = buf;
832 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
833 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
835 unsigned long nsegs, segusages_per_block;
839 down_read(&NILFS_MDT(sufile)->mi_sem);
841 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
842 nsegs = min_t(unsigned long,
843 nilfs_sufile_get_nsegments(sufile) - segnum,
845 for (i = 0; i < nsegs; i += n, segnum += n) {
846 n = min_t(unsigned long,
847 segusages_per_block -
848 nilfs_sufile_get_offset(sufile, segnum),
850 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
856 memset(si, 0, sisz * n);
857 si = (void *)si + sisz * n;
861 kaddr = kmap_atomic(su_bh->b_page);
862 su = nilfs_sufile_block_get_segment_usage(
863 sufile, segnum, su_bh, kaddr);
865 j++, su = (void *)su + susz, si = (void *)si + sisz) {
866 si->sui_lastmod = le64_to_cpu(su->su_lastmod);
867 si->sui_nblocks = le32_to_cpu(su->su_nblocks);
868 si->sui_flags = le32_to_cpu(su->su_flags) &
869 ~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
870 if (nilfs_segment_is_active(nilfs, segnum + j))
872 BIT(NILFS_SEGMENT_USAGE_ACTIVE);
874 kunmap_atomic(kaddr);
880 up_read(&NILFS_MDT(sufile)->mi_sem);
885 * nilfs_sufile_set_suinfo - sets segment usage info
886 * @sufile: inode of segment usage file
887 * @buf: array of suinfo_update
888 * @supsz: byte size of suinfo_update
889 * @nsup: size of suinfo_update array
891 * Description: Takes an array of nilfs_suinfo_update structs and updates
892 * segment usage accordingly. Only the fields indicated by the sup_flags
895 * Return Value: On success, 0 is returned. On error, one of the
896 * following negative error codes is returned.
900 * %-ENOMEM - Insufficient amount of memory available.
902 * %-EINVAL - Invalid values in input (segment number, flags or nblocks)
904 ssize_t nilfs_sufile_set_suinfo(struct inode *sufile, void *buf,
905 unsigned int supsz, size_t nsup)
907 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
908 struct buffer_head *header_bh, *bh;
909 struct nilfs_suinfo_update *sup, *supend = buf + supsz * nsup;
910 struct nilfs_segment_usage *su;
912 unsigned long blkoff, prev_blkoff;
913 int cleansi, cleansu, dirtysi, dirtysu;
914 long ncleaned = 0, ndirtied = 0;
917 if (unlikely(nsup == 0))
920 for (sup = buf; sup < supend; sup = (void *)sup + supsz) {
921 if (sup->sup_segnum >= nilfs->ns_nsegments
923 (~0UL << __NR_NILFS_SUINFO_UPDATE_FIELDS))
924 || (nilfs_suinfo_update_nblocks(sup) &&
925 sup->sup_sui.sui_nblocks >
926 nilfs->ns_blocks_per_segment))
930 down_write(&NILFS_MDT(sufile)->mi_sem);
932 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
937 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
938 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
943 kaddr = kmap_atomic(bh->b_page);
944 su = nilfs_sufile_block_get_segment_usage(
945 sufile, sup->sup_segnum, bh, kaddr);
947 if (nilfs_suinfo_update_lastmod(sup))
948 su->su_lastmod = cpu_to_le64(sup->sup_sui.sui_lastmod);
950 if (nilfs_suinfo_update_nblocks(sup))
951 su->su_nblocks = cpu_to_le32(sup->sup_sui.sui_nblocks);
953 if (nilfs_suinfo_update_flags(sup)) {
955 * Active flag is a virtual flag projected by running
956 * nilfs kernel code - drop it not to write it to
959 sup->sup_sui.sui_flags &=
960 ~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
962 cleansi = nilfs_suinfo_clean(&sup->sup_sui);
963 cleansu = nilfs_segment_usage_clean(su);
964 dirtysi = nilfs_suinfo_dirty(&sup->sup_sui);
965 dirtysu = nilfs_segment_usage_dirty(su);
967 if (cleansi && !cleansu)
969 else if (!cleansi && cleansu)
972 if (dirtysi && !dirtysu)
974 else if (!dirtysi && dirtysu)
977 su->su_flags = cpu_to_le32(sup->sup_sui.sui_flags);
980 kunmap_atomic(kaddr);
982 sup = (void *)sup + supsz;
986 prev_blkoff = blkoff;
987 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
988 if (blkoff == prev_blkoff)
991 /* get different block */
992 mark_buffer_dirty(bh);
994 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
995 if (unlikely(ret < 0))
998 mark_buffer_dirty(bh);
1002 if (ncleaned || ndirtied) {
1003 nilfs_sufile_mod_counter(header_bh, (u64)ncleaned,
1005 NILFS_SUI(sufile)->ncleansegs += ncleaned;
1007 nilfs_mdt_mark_dirty(sufile);
1011 up_write(&NILFS_MDT(sufile)->mi_sem);
1016 * nilfs_sufile_trim_fs() - trim ioctl handle function
1017 * @sufile: inode of segment usage file
1018 * @range: fstrim_range structure
1020 * start: First Byte to trim
1021 * len: number of Bytes to trim from start
1022 * minlen: minimum extent length in Bytes
1024 * Decription: nilfs_sufile_trim_fs goes through all segments containing bytes
1025 * from start to start+len. start is rounded up to the next block boundary
1026 * and start+len is rounded down. For each clean segment blkdev_issue_discard
1027 * function is invoked.
1029 * Return Value: On success, 0 is returned or negative error code, otherwise.
1031 int nilfs_sufile_trim_fs(struct inode *sufile, struct fstrim_range *range)
1033 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
1034 struct buffer_head *su_bh;
1035 struct nilfs_segment_usage *su;
1037 size_t n, i, susz = NILFS_MDT(sufile)->mi_entry_size;
1038 sector_t seg_start, seg_end, start_block, end_block;
1039 sector_t start = 0, nblocks = 0;
1040 u64 segnum, segnum_end, minlen, len, max_blocks, ndiscarded = 0;
1042 unsigned int sects_per_block;
1044 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
1045 bdev_logical_block_size(nilfs->ns_bdev);
1046 len = range->len >> nilfs->ns_blocksize_bits;
1047 minlen = range->minlen >> nilfs->ns_blocksize_bits;
1048 max_blocks = ((u64)nilfs->ns_nsegments * nilfs->ns_blocks_per_segment);
1050 if (!len || range->start >= max_blocks << nilfs->ns_blocksize_bits)
1053 start_block = (range->start + nilfs->ns_blocksize - 1) >>
1054 nilfs->ns_blocksize_bits;
1057 * range->len can be very large (actually, it is set to
1058 * ULLONG_MAX by default) - truncate upper end of the range
1059 * carefully so as not to overflow.
1061 if (max_blocks - start_block < len)
1062 end_block = max_blocks - 1;
1064 end_block = start_block + len - 1;
1066 segnum = nilfs_get_segnum_of_block(nilfs, start_block);
1067 segnum_end = nilfs_get_segnum_of_block(nilfs, end_block);
1069 down_read(&NILFS_MDT(sufile)->mi_sem);
1071 while (segnum <= segnum_end) {
1072 n = nilfs_sufile_segment_usages_in_block(sufile, segnum,
1075 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
1085 kaddr = kmap_atomic(su_bh->b_page);
1086 su = nilfs_sufile_block_get_segment_usage(sufile, segnum,
1088 for (i = 0; i < n; ++i, ++segnum, su = (void *)su + susz) {
1089 if (!nilfs_segment_usage_clean(su))
1092 nilfs_get_segment_range(nilfs, segnum, &seg_start,
1096 /* start new extent */
1098 nblocks = seg_end - seg_start + 1;
1102 if (start + nblocks == seg_start) {
1103 /* add to previous extent */
1104 nblocks += seg_end - seg_start + 1;
1108 /* discard previous extent */
1109 if (start < start_block) {
1110 nblocks -= start_block - start;
1111 start = start_block;
1114 if (nblocks >= minlen) {
1115 kunmap_atomic(kaddr);
1117 ret = blkdev_issue_discard(nilfs->ns_bdev,
1118 start * sects_per_block,
1119 nblocks * sects_per_block,
1126 ndiscarded += nblocks;
1127 kaddr = kmap_atomic(su_bh->b_page);
1128 su = nilfs_sufile_block_get_segment_usage(
1129 sufile, segnum, su_bh, kaddr);
1132 /* start new extent */
1134 nblocks = seg_end - seg_start + 1;
1136 kunmap_atomic(kaddr);
1142 /* discard last extent */
1143 if (start < start_block) {
1144 nblocks -= start_block - start;
1145 start = start_block;
1147 if (start + nblocks > end_block + 1)
1148 nblocks = end_block - start + 1;
1150 if (nblocks >= minlen) {
1151 ret = blkdev_issue_discard(nilfs->ns_bdev,
1152 start * sects_per_block,
1153 nblocks * sects_per_block,
1156 ndiscarded += nblocks;
1161 up_read(&NILFS_MDT(sufile)->mi_sem);
1163 range->len = ndiscarded << nilfs->ns_blocksize_bits;
1168 * nilfs_sufile_read - read or get sufile inode
1169 * @sb: super block instance
1170 * @susize: size of a segment usage entry
1171 * @raw_inode: on-disk sufile inode
1172 * @inodep: buffer to store the inode
1174 int nilfs_sufile_read(struct super_block *sb, size_t susize,
1175 struct nilfs_inode *raw_inode, struct inode **inodep)
1177 struct inode *sufile;
1178 struct nilfs_sufile_info *sui;
1179 struct buffer_head *header_bh;
1180 struct nilfs_sufile_header *header;
1184 if (susize > sb->s_blocksize) {
1185 nilfs_err(sb, "too large segment usage size: %zu bytes",
1188 } else if (susize < NILFS_MIN_SEGMENT_USAGE_SIZE) {
1189 nilfs_err(sb, "too small segment usage size: %zu bytes",
1194 sufile = nilfs_iget_locked(sb, NULL, NILFS_SUFILE_INO);
1195 if (unlikely(!sufile))
1197 if (!(sufile->i_state & I_NEW))
1200 err = nilfs_mdt_init(sufile, NILFS_MDT_GFP, sizeof(*sui));
1204 nilfs_mdt_set_entry_size(sufile, susize,
1205 sizeof(struct nilfs_sufile_header));
1207 err = nilfs_read_inode_common(sufile, raw_inode);
1211 err = nilfs_sufile_get_header_block(sufile, &header_bh);
1215 sui = NILFS_SUI(sufile);
1216 kaddr = kmap_atomic(header_bh->b_page);
1217 header = kaddr + bh_offset(header_bh);
1218 sui->ncleansegs = le64_to_cpu(header->sh_ncleansegs);
1219 kunmap_atomic(kaddr);
1222 sui->allocmax = nilfs_sufile_get_nsegments(sufile) - 1;
1225 unlock_new_inode(sufile);
1230 iget_failed(sufile);