drm/amdgpu: Add missing firmware entry for HAINAN
[linux/fpc-iii.git] / fs / nilfs2 / sufile.c
blobbf3f8f05c89b3dbb10849111e7a0815b2a3786e5
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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.
9 */
11 #include <linux/kernel.h>
12 #include <linux/fs.h>
13 #include <linux/string.h>
14 #include <linux/buffer_head.h>
15 #include <linux/errno.h>
16 #include "mdt.h"
17 #include "sufile.h"
19 #include <trace/events/nilfs2.h>
21 /**
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;
46 static unsigned long
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;
55 static unsigned long
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));
63 static unsigned long
64 nilfs_sufile_segment_usages_in_block(const struct inode *sufile, __u64 curr,
65 __u64 max)
67 return min_t(unsigned long,
68 nilfs_sufile_segment_usages_per_block(sufile) -
69 nilfs_sufile_get_offset(sufile, curr),
70 max - curr + 1);
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);
88 static inline int
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),
94 create, NULL, bhp);
97 static int nilfs_sufile_delete_segment_usage_block(struct inode *sufile,
98 __u64 segnum)
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;
108 void *kaddr;
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.
147 * %-EIO - I/O error.
149 * %-ENOMEM - Insufficient amount of memory available.
151 * %-ENOENT - Given segment usage is in hole block (may be returned if
152 * @create is zero)
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;
164 __u64 *seg;
165 size_t nerr = 0, n = 0;
166 int ret = 0;
168 if (unlikely(nsegs == 0))
169 goto out;
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);
177 nerr++;
180 if (nerr > 0) {
181 ret = -EINVAL;
182 goto out_sem;
185 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
186 if (ret < 0)
187 goto out_sem;
189 seg = segnumv;
190 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
191 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
192 if (ret < 0)
193 goto out_header;
195 for (;;) {
196 dofunc(sufile, *seg, header_bh, bh);
198 if (++seg >= segnumv + nsegs)
199 break;
200 prev_blkoff = blkoff;
201 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
202 if (blkoff == prev_blkoff)
203 continue;
205 /* get different block */
206 brelse(bh);
207 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
208 if (unlikely(ret < 0))
209 goto out_header;
211 brelse(bh);
213 out_header:
214 n = seg - segnumv;
215 brelse(header_bh);
216 out_sem:
217 up_write(&NILFS_MDT(sufile)->mi_sem);
218 out:
219 if (ndone)
220 *ndone = n;
221 return ret;
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;
230 int ret;
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);
236 return -EINVAL;
238 down_write(&NILFS_MDT(sufile)->mi_sem);
240 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
241 if (ret < 0)
242 goto out_sem;
244 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, create, &bh);
245 if (!ret) {
246 dofunc(sufile, segnum, header_bh, bh);
247 brelse(bh);
249 brelse(header_bh);
251 out_sem:
252 up_write(&NILFS_MDT(sufile)->mi_sem);
253 return ret;
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);
270 __u64 nsegs;
271 int ret = -ERANGE;
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;
278 sui->allocmax = end;
279 ret = 0;
281 up_write(&NILFS_MDT(sufile)->mi_sem);
282 return ret;
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.
296 * %-EIO - I/O error.
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;
310 void *kaddr;
311 unsigned long nsegments, nsus, cnt;
312 int ret, j;
314 down_write(&NILFS_MDT(sufile)->mi_sem);
316 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
317 if (ret < 0)
318 goto out_sem;
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) {
345 segnum = 0;
346 maxsegnum = sui->allocmin - 1;
347 } else {
348 break; /* never happens */
351 trace_nilfs2_segment_usage_check(sufile, segnum, cnt);
352 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1,
353 &su_bh);
354 if (ret < 0)
355 goto out_header;
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))
364 continue;
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);
376 sui->ncleansegs--;
377 mark_buffer_dirty(header_bh);
378 mark_buffer_dirty(su_bh);
379 nilfs_mdt_mark_dirty(sufile);
380 brelse(su_bh);
381 *segnump = segnum;
383 trace_nilfs2_segment_usage_allocated(sufile, segnum);
385 goto out_header;
388 kunmap_atomic(kaddr);
389 brelse(su_bh);
392 /* no segments left */
393 ret = -ENOSPC;
395 out_header:
396 brelse(header_bh);
398 out_sem:
399 up_write(&NILFS_MDT(sufile)->mi_sem);
400 return ret;
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;
408 void *kaddr;
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);
417 return;
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;
434 void *kaddr;
435 int clean, dirty;
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);
442 return;
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;
465 void *kaddr;
466 int sudirty;
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);
475 return;
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;
501 int ret;
503 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
504 if (!ret) {
505 mark_buffer_dirty(bh);
506 nilfs_mdt_mark_dirty(sufile);
507 brelse(bh);
509 return ret;
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;
524 void *kaddr;
525 int ret;
527 down_write(&NILFS_MDT(sufile)->mi_sem);
528 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
529 if (ret < 0)
530 goto out_sem;
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));
535 if (modtime)
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);
542 brelse(bh);
544 out_sem:
545 up_write(&NILFS_MDT(sufile)->mi_sem);
546 return ret;
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
555 * usage.
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.
561 * %-EIO - I/O error.
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;
570 void *kaddr;
571 int ret;
573 down_read(&NILFS_MDT(sufile)->mi_sem);
575 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
576 if (ret < 0)
577 goto out_sem;
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);
590 brelse(header_bh);
592 out_sem:
593 up_read(&NILFS_MDT(sufile)->mi_sem);
594 return ret;
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;
602 void *kaddr;
603 int suclean;
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);
609 return;
611 suclean = nilfs_segment_usage_clean(su);
612 nilfs_segment_usage_set_error(su);
613 kunmap_atomic(kaddr);
615 if (suclean) {
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.
632 * %-EIO - I/O error.
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;
650 __u64 segnum;
651 void *kaddr;
652 ssize_t n, nc;
653 int ret;
654 int j;
656 nsegs = nilfs_sufile_get_nsegments(sufile);
658 ret = -EINVAL;
659 if (start > end || start >= nsegs)
660 goto out;
662 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
663 if (ret < 0)
664 goto out;
666 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
667 ncleaned = 0;
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),
673 end - segnum + 1);
674 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
675 &su_bh);
676 if (ret < 0) {
677 if (ret != -ENOENT)
678 goto out_header;
679 /* hole */
680 continue;
682 kaddr = kmap_atomic(su_bh->b_page);
683 su = nilfs_sufile_block_get_segment_usage(
684 sufile, segnum, su_bh, kaddr);
685 su2 = su;
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)) {
690 ret = -EBUSY;
691 kunmap_atomic(kaddr);
692 brelse(su_bh);
693 goto out_header;
696 nc = 0;
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);
700 nc++;
703 kunmap_atomic(kaddr);
704 if (nc > 0) {
705 mark_buffer_dirty(su_bh);
706 ncleaned += nc;
708 brelse(su_bh);
710 if (n == segusages_per_block) {
711 /* make hole */
712 nilfs_sufile_delete_segment_usage_block(sufile, segnum);
715 ret = 0;
717 out_header:
718 if (ncleaned > 0) {
719 NILFS_SUI(sufile)->ncleansegs += ncleaned;
720 nilfs_sufile_mod_counter(header_bh, ncleaned, 0);
721 nilfs_mdt_mark_dirty(sufile);
723 brelse(header_bh);
724 out:
725 return ret;
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.
736 * %-EIO - I/O error.
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);
750 void *kaddr;
751 unsigned long nsegs, nrsvsegs;
752 int ret = 0;
754 down_write(&NILFS_MDT(sufile)->mi_sem);
756 nsegs = nilfs_sufile_get_nsegments(sufile);
757 if (nsegs == newnsegs)
758 goto out;
760 ret = -ENOSPC;
761 nrsvsegs = nilfs_nrsvsegs(nilfs, newnsegs);
762 if (newnsegs < nsegs && nsegs - newnsegs + nrsvsegs > sui->ncleansegs)
763 goto out;
765 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
766 if (ret < 0)
767 goto out;
769 if (newnsegs > nsegs) {
770 sui->ncleansegs += newnsegs - nsegs;
771 } else /* newnsegs < nsegs */ {
772 ret = nilfs_sufile_truncate_range(sufile, newnsegs, nsegs - 1);
773 if (ret < 0)
774 goto out_header;
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);
788 out_header:
789 brelse(header_bh);
790 out:
791 up_write(&NILFS_MDT(sufile)->mi_sem);
792 return ret;
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
803 * Description:
805 * Return Value: On success, 0 is returned and .... On error, one of the
806 * following negative error codes is returned.
808 * %-EIO - I/O error.
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;
820 void *kaddr;
821 unsigned long nsegs, segusages_per_block;
822 ssize_t n;
823 int ret, i, j;
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,
830 nsi);
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),
835 nsegs - i);
836 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
837 &su_bh);
838 if (ret < 0) {
839 if (ret != -ENOENT)
840 goto out;
841 /* hole */
842 memset(si, 0, sisz * n);
843 si = (void *)si + sisz * n;
844 continue;
847 kaddr = kmap_atomic(su_bh->b_page);
848 su = nilfs_sufile_block_get_segment_usage(
849 sufile, segnum, su_bh, kaddr);
850 for (j = 0; j < n;
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))
857 si->sui_flags |=
858 BIT(NILFS_SEGMENT_USAGE_ACTIVE);
860 kunmap_atomic(kaddr);
861 brelse(su_bh);
863 ret = nsegs;
865 out:
866 up_read(&NILFS_MDT(sufile)->mi_sem);
867 return ret;
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
879 * are updated.
881 * Return Value: On success, 0 is returned. On error, one of the
882 * following negative error codes is returned.
884 * %-EIO - I/O error.
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;
897 void *kaddr;
898 unsigned long blkoff, prev_blkoff;
899 int cleansi, cleansu, dirtysi, dirtysu;
900 long ncleaned = 0, ndirtied = 0;
901 int ret = 0;
903 if (unlikely(nsup == 0))
904 return ret;
906 for (sup = buf; sup < supend; sup = (void *)sup + supsz) {
907 if (sup->sup_segnum >= nilfs->ns_nsegments
908 || (sup->sup_flags &
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))
913 return -EINVAL;
916 down_write(&NILFS_MDT(sufile)->mi_sem);
918 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
919 if (ret < 0)
920 goto out_sem;
922 sup = buf;
923 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
924 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
925 if (ret < 0)
926 goto out_header;
928 for (;;) {
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
943 * disk.
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)
954 ++ncleaned;
955 else if (!cleansi && cleansu)
956 --ncleaned;
958 if (dirtysi && !dirtysu)
959 ++ndirtied;
960 else if (!dirtysi && dirtysu)
961 --ndirtied;
963 su->su_flags = cpu_to_le32(sup->sup_sui.sui_flags);
966 kunmap_atomic(kaddr);
968 sup = (void *)sup + supsz;
969 if (sup >= supend)
970 break;
972 prev_blkoff = blkoff;
973 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
974 if (blkoff == prev_blkoff)
975 continue;
977 /* get different block */
978 mark_buffer_dirty(bh);
979 put_bh(bh);
980 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
981 if (unlikely(ret < 0))
982 goto out_mark;
984 mark_buffer_dirty(bh);
985 put_bh(bh);
987 out_mark:
988 if (ncleaned || ndirtied) {
989 nilfs_sufile_mod_counter(header_bh, (u64)ncleaned,
990 (u64)ndirtied);
991 NILFS_SUI(sufile)->ncleansegs += ncleaned;
993 nilfs_mdt_mark_dirty(sufile);
994 out_header:
995 put_bh(header_bh);
996 out_sem:
997 up_write(&NILFS_MDT(sufile)->mi_sem);
998 return ret;
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;
1022 void *kaddr;
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;
1027 int ret = 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)
1037 return -EINVAL;
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;
1049 else
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,
1059 segnum_end);
1061 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
1062 &su_bh);
1063 if (ret < 0) {
1064 if (ret != -ENOENT)
1065 goto out_sem;
1066 /* hole */
1067 segnum += n;
1068 continue;
1071 kaddr = kmap_atomic(su_bh->b_page);
1072 su = nilfs_sufile_block_get_segment_usage(sufile, segnum,
1073 su_bh, kaddr);
1074 for (i = 0; i < n; ++i, ++segnum, su = (void *)su + susz) {
1075 if (!nilfs_segment_usage_clean(su))
1076 continue;
1078 nilfs_get_segment_range(nilfs, segnum, &seg_start,
1079 &seg_end);
1081 if (!nblocks) {
1082 /* start new extent */
1083 start = seg_start;
1084 nblocks = seg_end - seg_start + 1;
1085 continue;
1088 if (start + nblocks == seg_start) {
1089 /* add to previous extent */
1090 nblocks += seg_end - seg_start + 1;
1091 continue;
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,
1106 GFP_NOFS, 0);
1107 if (ret < 0) {
1108 put_bh(su_bh);
1109 goto out_sem;
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 */
1119 start = seg_start;
1120 nblocks = seg_end - seg_start + 1;
1122 kunmap_atomic(kaddr);
1123 put_bh(su_bh);
1127 if (nblocks) {
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,
1140 GFP_NOFS, 0);
1141 if (!ret)
1142 ndiscarded += nblocks;
1146 out_sem:
1147 up_read(&NILFS_MDT(sufile)->mi_sem);
1149 range->len = ndiscarded << nilfs->ns_blocksize_bits;
1150 return ret;
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;
1167 void *kaddr;
1168 int err;
1170 if (susize > sb->s_blocksize) {
1171 nilfs_msg(sb, KERN_ERR,
1172 "too large segment usage size: %zu bytes", susize);
1173 return -EINVAL;
1174 } else if (susize < NILFS_MIN_SEGMENT_USAGE_SIZE) {
1175 nilfs_msg(sb, KERN_ERR,
1176 "too small segment usage size: %zu bytes", susize);
1177 return -EINVAL;
1180 sufile = nilfs_iget_locked(sb, NULL, NILFS_SUFILE_INO);
1181 if (unlikely(!sufile))
1182 return -ENOMEM;
1183 if (!(sufile->i_state & I_NEW))
1184 goto out;
1186 err = nilfs_mdt_init(sufile, NILFS_MDT_GFP, sizeof(*sui));
1187 if (err)
1188 goto failed;
1190 nilfs_mdt_set_entry_size(sufile, susize,
1191 sizeof(struct nilfs_sufile_header));
1193 err = nilfs_read_inode_common(sufile, raw_inode);
1194 if (err)
1195 goto failed;
1197 err = nilfs_sufile_get_header_block(sufile, &header_bh);
1198 if (err)
1199 goto failed;
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);
1206 brelse(header_bh);
1208 sui->allocmax = nilfs_sufile_get_nsegments(sufile) - 1;
1209 sui->allocmin = 0;
1211 unlock_new_inode(sufile);
1212 out:
1213 *inodep = sufile;
1214 return 0;
1215 failed:
1216 iget_failed(sufile);
1217 return err;