1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * NILFS segment usage file.
4 *
5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
6 *
7 * Written by Koji Sato.
8 * Revised by Ryusuke Konishi.
9 */
10
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"
18
19 #include <trace/events/nilfs2.h>
20
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
27 */
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 */
33 };
34
NILFS_SUI(struct inode * sufile)35 static inline struct nilfs_sufile_info *NILFS_SUI(struct inode *sufile)
36 {
37 return (struct nilfs_sufile_info *)NILFS_MDT(sufile);
38 }
39
40 static inline unsigned long
nilfs_sufile_segment_usages_per_block(const struct inode * sufile)41 nilfs_sufile_segment_usages_per_block(const struct inode *sufile)
42 {
43 return NILFS_MDT(sufile)->mi_entries_per_block;
44 }
45
46 static unsigned long
nilfs_sufile_get_blkoff(const struct inode * sufile,__u64 segnum)47 nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum)
48 {
49 __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
50
51 t = div64_ul(t, nilfs_sufile_segment_usages_per_block(sufile));
52 return (unsigned long)t;
53 }
54
55 static unsigned long
nilfs_sufile_get_offset(const struct inode * sufile,__u64 segnum)56 nilfs_sufile_get_offset(const struct inode *sufile, __u64 segnum)
57 {
58 __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
59
60 return do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
61 }
62
63 static unsigned long
nilfs_sufile_segment_usages_in_block(const struct inode * sufile,__u64 curr,__u64 max)64 nilfs_sufile_segment_usages_in_block(const struct inode *sufile, __u64 curr,
65 __u64 max)
66 {
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);
71 }
72
73 /**
74 * nilfs_sufile_segment_usage_offset - calculate the byte offset of a segment
75 * usage entry in the folio containing it
76 * @sufile: segment usage file inode
77 * @segnum: number of segment usage
78 * @bh: buffer head of block containing segment usage indexed by @segnum
79 *
80 * Return: Byte offset in the folio of the segment usage entry.
81 */
nilfs_sufile_segment_usage_offset(const struct inode * sufile,__u64 segnum,struct buffer_head * bh)82 static size_t nilfs_sufile_segment_usage_offset(const struct inode *sufile,
83 __u64 segnum,
84 struct buffer_head *bh)
85 {
86 return offset_in_folio(bh->b_folio, bh->b_data) +
87 nilfs_sufile_get_offset(sufile, segnum) *
88 NILFS_MDT(sufile)->mi_entry_size;
89 }
90
nilfs_sufile_get_header_block(struct inode * sufile,struct buffer_head ** bhp)91 static int nilfs_sufile_get_header_block(struct inode *sufile,
92 struct buffer_head **bhp)
93 {
94 int err = nilfs_mdt_get_block(sufile, 0, 0, NULL, bhp);
95
96 if (unlikely(err == -ENOENT)) {
97 nilfs_error(sufile->i_sb,
98 "missing header block in segment usage metadata");
99 err = -EIO;
100 }
101 return err;
102 }
103
104 static inline int
nilfs_sufile_get_segment_usage_block(struct inode * sufile,__u64 segnum,int create,struct buffer_head ** bhp)105 nilfs_sufile_get_segment_usage_block(struct inode *sufile, __u64 segnum,
106 int create, struct buffer_head **bhp)
107 {
108 return nilfs_mdt_get_block(sufile,
109 nilfs_sufile_get_blkoff(sufile, segnum),
110 create, NULL, bhp);
111 }
112
nilfs_sufile_delete_segment_usage_block(struct inode * sufile,__u64 segnum)113 static int nilfs_sufile_delete_segment_usage_block(struct inode *sufile,
114 __u64 segnum)
115 {
116 return nilfs_mdt_delete_block(sufile,
117 nilfs_sufile_get_blkoff(sufile, segnum));
118 }
119
nilfs_sufile_mod_counter(struct buffer_head * header_bh,u64 ncleanadd,u64 ndirtyadd)120 static void nilfs_sufile_mod_counter(struct buffer_head *header_bh,
121 u64 ncleanadd, u64 ndirtyadd)
122 {
123 struct nilfs_sufile_header *header;
124
125 header = kmap_local_folio(header_bh->b_folio, 0);
126 le64_add_cpu(&header->sh_ncleansegs, ncleanadd);
127 le64_add_cpu(&header->sh_ndirtysegs, ndirtyadd);
128 kunmap_local(header);
129
130 mark_buffer_dirty(header_bh);
131 }
132
133 /**
134 * nilfs_sufile_get_ncleansegs - return the number of clean segments
135 * @sufile: inode of segment usage file
136 *
137 * Return: Number of clean segments.
138 */
nilfs_sufile_get_ncleansegs(struct inode * sufile)139 unsigned long nilfs_sufile_get_ncleansegs(struct inode *sufile)
140 {
141 return NILFS_SUI(sufile)->ncleansegs;
142 }
143
144 /**
145 * nilfs_sufile_updatev - modify multiple segment usages at a time
146 * @sufile: inode of segment usage file
147 * @segnumv: array of segment numbers
148 * @nsegs: size of @segnumv array
149 * @create: creation flag
150 * @ndone: place to store number of modified segments on @segnumv
151 * @dofunc: primitive operation for the update
152 *
153 * Description: nilfs_sufile_updatev() repeatedly calls @dofunc
154 * against the given array of segments. The @dofunc is called with
155 * buffers of a header block and the sufile block in which the target
156 * segment usage entry is contained. If @ndone is given, the number
157 * of successfully modified segments from the head is stored in the
158 * place @ndone points to.
159 *
160 * Return: 0 on success, or one of the following negative error codes on
161 * failure:
162 * * %-EINVAL - Invalid segment usage number
163 * * %-EIO - I/O error (including metadata corruption).
164 * * %-ENOENT - Given segment usage is in hole block (may be returned if
165 * @create is zero)
166 * * %-ENOMEM - Insufficient memory available.
167 */
nilfs_sufile_updatev(struct inode * sufile,__u64 * segnumv,size_t nsegs,int create,size_t * ndone,void (* dofunc)(struct inode *,__u64,struct buffer_head *,struct buffer_head *))168 int nilfs_sufile_updatev(struct inode *sufile, __u64 *segnumv, size_t nsegs,
169 int create, size_t *ndone,
170 void (*dofunc)(struct inode *, __u64,
171 struct buffer_head *,
172 struct buffer_head *))
173 {
174 struct buffer_head *header_bh, *bh;
175 unsigned long blkoff, prev_blkoff;
176 __u64 *seg;
177 size_t nerr = 0, n = 0;
178 int ret = 0;
179
180 if (unlikely(nsegs == 0))
181 goto out;
182
183 down_write(&NILFS_MDT(sufile)->mi_sem);
184 for (seg = segnumv; seg < segnumv + nsegs; seg++) {
185 if (unlikely(*seg >= nilfs_sufile_get_nsegments(sufile))) {
186 nilfs_warn(sufile->i_sb,
187 "%s: invalid segment number: %llu",
188 __func__, (unsigned long long)*seg);
189 nerr++;
190 }
191 }
192 if (nerr > 0) {
193 ret = -EINVAL;
194 goto out_sem;
195 }
196
197 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
198 if (ret < 0)
199 goto out_sem;
200
201 seg = segnumv;
202 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
203 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
204 if (ret < 0)
205 goto out_header;
206
207 for (;;) {
208 dofunc(sufile, *seg, header_bh, bh);
209
210 if (++seg >= segnumv + nsegs)
211 break;
212 prev_blkoff = blkoff;
213 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
214 if (blkoff == prev_blkoff)
215 continue;
216
217 /* get different block */
218 brelse(bh);
219 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
220 if (unlikely(ret < 0))
221 goto out_header;
222 }
223 brelse(bh);
224
225 out_header:
226 n = seg - segnumv;
227 brelse(header_bh);
228 out_sem:
229 up_write(&NILFS_MDT(sufile)->mi_sem);
230 out:
231 if (ndone)
232 *ndone = n;
233 return ret;
234 }
235
nilfs_sufile_update(struct inode * sufile,__u64 segnum,int create,void (* dofunc)(struct inode *,__u64,struct buffer_head *,struct buffer_head *))236 int nilfs_sufile_update(struct inode *sufile, __u64 segnum, int create,
237 void (*dofunc)(struct inode *, __u64,
238 struct buffer_head *,
239 struct buffer_head *))
240 {
241 struct buffer_head *header_bh, *bh;
242 int ret;
243
244 if (unlikely(segnum >= nilfs_sufile_get_nsegments(sufile))) {
245 nilfs_warn(sufile->i_sb, "%s: invalid segment number: %llu",
246 __func__, (unsigned long long)segnum);
247 return -EINVAL;
248 }
249 down_write(&NILFS_MDT(sufile)->mi_sem);
250
251 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
252 if (ret < 0)
253 goto out_sem;
254
255 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, create, &bh);
256 if (!ret) {
257 dofunc(sufile, segnum, header_bh, bh);
258 brelse(bh);
259 }
260 brelse(header_bh);
261
262 out_sem:
263 up_write(&NILFS_MDT(sufile)->mi_sem);
264 return ret;
265 }
266
267 /**
268 * nilfs_sufile_set_alloc_range - limit range of segment to be allocated
269 * @sufile: inode of segment usage file
270 * @start: minimum segment number of allocatable region (inclusive)
271 * @end: maximum segment number of allocatable region (inclusive)
272 *
273 * Return: 0 on success, or %-ERANGE if segment range is invalid.
274 */
nilfs_sufile_set_alloc_range(struct inode * sufile,__u64 start,__u64 end)275 int nilfs_sufile_set_alloc_range(struct inode *sufile, __u64 start, __u64 end)
276 {
277 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
278 __u64 nsegs;
279 int ret = -ERANGE;
280
281 down_write(&NILFS_MDT(sufile)->mi_sem);
282 nsegs = nilfs_sufile_get_nsegments(sufile);
283
284 if (start <= end && end < nsegs) {
285 sui->allocmin = start;
286 sui->allocmax = end;
287 ret = 0;
288 }
289 up_write(&NILFS_MDT(sufile)->mi_sem);
290 return ret;
291 }
292
293 /**
294 * nilfs_sufile_alloc - allocate a segment
295 * @sufile: inode of segment usage file
296 * @segnump: pointer to segment number
297 *
298 * Description: nilfs_sufile_alloc() allocates a clean segment, and stores
299 * its segment number in the place pointed to by @segnump.
300 *
301 * Return: 0 on success, or one of the following negative error codes on
302 * failure:
303 * * %-EIO - I/O error (including metadata corruption).
304 * * %-ENOMEM - Insufficient memory available.
305 * * %-ENOSPC - No clean segment left.
306 */
nilfs_sufile_alloc(struct inode * sufile,__u64 * segnump)307 int nilfs_sufile_alloc(struct inode *sufile, __u64 *segnump)
308 {
309 struct buffer_head *header_bh, *su_bh;
310 struct nilfs_sufile_header *header;
311 struct nilfs_segment_usage *su;
312 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
313 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
314 __u64 segnum, maxsegnum, last_alloc;
315 size_t offset;
316 void *kaddr;
317 unsigned long nsegments, nsus, cnt;
318 int ret, j;
319
320 down_write(&NILFS_MDT(sufile)->mi_sem);
321
322 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
323 if (ret < 0)
324 goto out_sem;
325 header = kmap_local_folio(header_bh->b_folio, 0);
326 last_alloc = le64_to_cpu(header->sh_last_alloc);
327 kunmap_local(header);
328
329 nsegments = nilfs_sufile_get_nsegments(sufile);
330 maxsegnum = sui->allocmax;
331 segnum = last_alloc + 1;
332 if (segnum < sui->allocmin || segnum > sui->allocmax)
333 segnum = sui->allocmin;
334
335 for (cnt = 0; cnt < nsegments; cnt += nsus) {
336 if (segnum > maxsegnum) {
337 if (cnt < sui->allocmax - sui->allocmin + 1) {
338 /*
339 * wrap around in the limited region.
340 * if allocation started from
341 * sui->allocmin, this never happens.
342 */
343 segnum = sui->allocmin;
344 maxsegnum = last_alloc;
345 } else if (segnum > sui->allocmin &&
346 sui->allocmax + 1 < nsegments) {
347 segnum = sui->allocmax + 1;
348 maxsegnum = nsegments - 1;
349 } else if (sui->allocmin > 0) {
350 segnum = 0;
351 maxsegnum = sui->allocmin - 1;
352 } else {
353 break; /* never happens */
354 }
355 }
356 trace_nilfs2_segment_usage_check(sufile, segnum, cnt);
357 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1,
358 &su_bh);
359 if (ret < 0)
360 goto out_header;
361
362 offset = nilfs_sufile_segment_usage_offset(sufile, segnum,
363 su_bh);
364 su = kaddr = kmap_local_folio(su_bh->b_folio, offset);
365
366 nsus = nilfs_sufile_segment_usages_in_block(
367 sufile, segnum, maxsegnum);
368 for (j = 0; j < nsus; j++, su = (void *)su + susz, segnum++) {
369 if (!nilfs_segment_usage_clean(su))
370 continue;
371 /* found a clean segment */
372 nilfs_segment_usage_set_dirty(su);
373 kunmap_local(kaddr);
374
375 header = kmap_local_folio(header_bh->b_folio, 0);
376 le64_add_cpu(&header->sh_ncleansegs, -1);
377 le64_add_cpu(&header->sh_ndirtysegs, 1);
378 header->sh_last_alloc = cpu_to_le64(segnum);
379 kunmap_local(header);
380
381 sui->ncleansegs--;
382 mark_buffer_dirty(header_bh);
383 mark_buffer_dirty(su_bh);
384 nilfs_mdt_mark_dirty(sufile);
385 brelse(su_bh);
386 *segnump = segnum;
387
388 trace_nilfs2_segment_usage_allocated(sufile, segnum);
389
390 goto out_header;
391 }
392
393 kunmap_local(kaddr);
394 brelse(su_bh);
395 }
396
397 /* no segments left */
398 ret = -ENOSPC;
399
400 out_header:
401 brelse(header_bh);
402
403 out_sem:
404 up_write(&NILFS_MDT(sufile)->mi_sem);
405 return ret;
406 }
407
nilfs_sufile_do_cancel_free(struct inode * sufile,__u64 segnum,struct buffer_head * header_bh,struct buffer_head * su_bh)408 void nilfs_sufile_do_cancel_free(struct inode *sufile, __u64 segnum,
409 struct buffer_head *header_bh,
410 struct buffer_head *su_bh)
411 {
412 struct nilfs_segment_usage *su;
413 size_t offset;
414
415 offset = nilfs_sufile_segment_usage_offset(sufile, segnum, su_bh);
416 su = kmap_local_folio(su_bh->b_folio, offset);
417 if (unlikely(!nilfs_segment_usage_clean(su))) {
418 nilfs_warn(sufile->i_sb, "%s: segment %llu must be clean",
419 __func__, (unsigned long long)segnum);
420 kunmap_local(su);
421 return;
422 }
423 nilfs_segment_usage_set_dirty(su);
424 kunmap_local(su);
425
426 nilfs_sufile_mod_counter(header_bh, -1, 1);
427 NILFS_SUI(sufile)->ncleansegs--;
428
429 mark_buffer_dirty(su_bh);
430 nilfs_mdt_mark_dirty(sufile);
431 }
432
nilfs_sufile_do_scrap(struct inode * sufile,__u64 segnum,struct buffer_head * header_bh,struct buffer_head * su_bh)433 void nilfs_sufile_do_scrap(struct inode *sufile, __u64 segnum,
434 struct buffer_head *header_bh,
435 struct buffer_head *su_bh)
436 {
437 struct nilfs_segment_usage *su;
438 size_t offset;
439 int clean, dirty;
440
441 offset = nilfs_sufile_segment_usage_offset(sufile, segnum, su_bh);
442 su = kmap_local_folio(su_bh->b_folio, offset);
443 if (su->su_flags == cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY)) &&
444 su->su_nblocks == cpu_to_le32(0)) {
445 kunmap_local(su);
446 return;
447 }
448 clean = nilfs_segment_usage_clean(su);
449 dirty = nilfs_segment_usage_dirty(su);
450
451 /* make the segment garbage */
452 su->su_lastmod = cpu_to_le64(0);
453 su->su_nblocks = cpu_to_le32(0);
454 su->su_flags = cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY));
455 kunmap_local(su);
456
457 nilfs_sufile_mod_counter(header_bh, clean ? (u64)-1 : 0, dirty ? 0 : 1);
458 NILFS_SUI(sufile)->ncleansegs -= clean;
459
460 mark_buffer_dirty(su_bh);
461 nilfs_mdt_mark_dirty(sufile);
462 }
463
nilfs_sufile_do_free(struct inode * sufile,__u64 segnum,struct buffer_head * header_bh,struct buffer_head * su_bh)464 void nilfs_sufile_do_free(struct inode *sufile, __u64 segnum,
465 struct buffer_head *header_bh,
466 struct buffer_head *su_bh)
467 {
468 struct nilfs_segment_usage *su;
469 size_t offset;
470 int sudirty;
471
472 offset = nilfs_sufile_segment_usage_offset(sufile, segnum, su_bh);
473 su = kmap_local_folio(su_bh->b_folio, offset);
474 if (nilfs_segment_usage_clean(su)) {
475 nilfs_warn(sufile->i_sb, "%s: segment %llu is already clean",
476 __func__, (unsigned long long)segnum);
477 kunmap_local(su);
478 return;
479 }
480 if (unlikely(nilfs_segment_usage_error(su)))
481 nilfs_warn(sufile->i_sb, "free segment %llu marked in error",
482 (unsigned long long)segnum);
483
484 sudirty = nilfs_segment_usage_dirty(su);
485 if (unlikely(!sudirty))
486 nilfs_warn(sufile->i_sb, "free unallocated segment %llu",
487 (unsigned long long)segnum);
488
489 nilfs_segment_usage_set_clean(su);
490 kunmap_local(su);
491 mark_buffer_dirty(su_bh);
492
493 nilfs_sufile_mod_counter(header_bh, 1, sudirty ? (u64)-1 : 0);
494 NILFS_SUI(sufile)->ncleansegs++;
495
496 nilfs_mdt_mark_dirty(sufile);
497
498 trace_nilfs2_segment_usage_freed(sufile, segnum);
499 }
500
501 /**
502 * nilfs_sufile_mark_dirty - mark the buffer having a segment usage dirty
503 * @sufile: inode of segment usage file
504 * @segnum: segment number
505 *
506 * Return: 0 on success, or a negative error code on failure.
507 */
nilfs_sufile_mark_dirty(struct inode * sufile,__u64 segnum)508 int nilfs_sufile_mark_dirty(struct inode *sufile, __u64 segnum)
509 {
510 struct buffer_head *bh;
511 size_t offset;
512 struct nilfs_segment_usage *su;
513 int ret;
514
515 down_write(&NILFS_MDT(sufile)->mi_sem);
516 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
517 if (unlikely(ret)) {
518 if (ret == -ENOENT) {
519 nilfs_error(sufile->i_sb,
520 "segment usage for segment %llu is unreadable due to a hole block",
521 (unsigned long long)segnum);
522 ret = -EIO;
523 }
524 goto out_sem;
525 }
526
527 offset = nilfs_sufile_segment_usage_offset(sufile, segnum, bh);
528 su = kmap_local_folio(bh->b_folio, offset);
529 if (unlikely(nilfs_segment_usage_error(su))) {
530 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
531
532 kunmap_local(su);
533 brelse(bh);
534 if (nilfs_segment_is_active(nilfs, segnum)) {
535 nilfs_error(sufile->i_sb,
536 "active segment %llu is erroneous",
537 (unsigned long long)segnum);
538 } else {
539 /*
540 * Segments marked erroneous are never allocated by
541 * nilfs_sufile_alloc(); only active segments, ie,
542 * the segments indexed by ns_segnum or ns_nextnum,
543 * can be erroneous here.
544 */
545 WARN_ON_ONCE(1);
546 }
547 ret = -EIO;
548 } else {
549 nilfs_segment_usage_set_dirty(su);
550 kunmap_local(su);
551 mark_buffer_dirty(bh);
552 nilfs_mdt_mark_dirty(sufile);
553 brelse(bh);
554 }
555 out_sem:
556 up_write(&NILFS_MDT(sufile)->mi_sem);
557 return ret;
558 }
559
560 /**
561 * nilfs_sufile_set_segment_usage - set usage of a segment
562 * @sufile: inode of segment usage file
563 * @segnum: segment number
564 * @nblocks: number of live blocks in the segment
565 * @modtime: modification time (option)
566 *
567 * Return: 0 on success, or a negative error code on failure.
568 */
nilfs_sufile_set_segment_usage(struct inode * sufile,__u64 segnum,unsigned long nblocks,time64_t modtime)569 int nilfs_sufile_set_segment_usage(struct inode *sufile, __u64 segnum,
570 unsigned long nblocks, time64_t modtime)
571 {
572 struct buffer_head *bh;
573 struct nilfs_segment_usage *su;
574 size_t offset;
575 int ret;
576
577 down_write(&NILFS_MDT(sufile)->mi_sem);
578 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
579 if (ret < 0)
580 goto out_sem;
581
582 offset = nilfs_sufile_segment_usage_offset(sufile, segnum, bh);
583 su = kmap_local_folio(bh->b_folio, offset);
584 if (modtime) {
585 /*
586 * Check segusage error and set su_lastmod only when updating
587 * this entry with a valid timestamp, not for cancellation.
588 */
589 WARN_ON_ONCE(nilfs_segment_usage_error(su));
590 su->su_lastmod = cpu_to_le64(modtime);
591 }
592 su->su_nblocks = cpu_to_le32(nblocks);
593 kunmap_local(su);
594
595 mark_buffer_dirty(bh);
596 nilfs_mdt_mark_dirty(sufile);
597 brelse(bh);
598
599 out_sem:
600 up_write(&NILFS_MDT(sufile)->mi_sem);
601 return ret;
602 }
603
604 /**
605 * nilfs_sufile_get_stat - get segment usage statistics
606 * @sufile: inode of segment usage file
607 * @sustat: pointer to a structure of segment usage statistics
608 *
609 * Description: nilfs_sufile_get_stat() retrieves segment usage statistics
610 * and stores them in the location pointed to by @sustat.
611 *
612 * Return: 0 on success, or one of the following negative error codes on
613 * failure:
614 * * %-EIO - I/O error (including metadata corruption).
615 * * %-ENOMEM - Insufficient memory available.
616 */
nilfs_sufile_get_stat(struct inode * sufile,struct nilfs_sustat * sustat)617 int nilfs_sufile_get_stat(struct inode *sufile, struct nilfs_sustat *sustat)
618 {
619 struct buffer_head *header_bh;
620 struct nilfs_sufile_header *header;
621 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
622 int ret;
623
624 down_read(&NILFS_MDT(sufile)->mi_sem);
625
626 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
627 if (ret < 0)
628 goto out_sem;
629
630 header = kmap_local_folio(header_bh->b_folio, 0);
631 sustat->ss_nsegs = nilfs_sufile_get_nsegments(sufile);
632 sustat->ss_ncleansegs = le64_to_cpu(header->sh_ncleansegs);
633 sustat->ss_ndirtysegs = le64_to_cpu(header->sh_ndirtysegs);
634 sustat->ss_ctime = nilfs->ns_ctime;
635 sustat->ss_nongc_ctime = nilfs->ns_nongc_ctime;
636 spin_lock(&nilfs->ns_last_segment_lock);
637 sustat->ss_prot_seq = nilfs->ns_prot_seq;
638 spin_unlock(&nilfs->ns_last_segment_lock);
639 kunmap_local(header);
640 brelse(header_bh);
641
642 out_sem:
643 up_read(&NILFS_MDT(sufile)->mi_sem);
644 return ret;
645 }
646
nilfs_sufile_do_set_error(struct inode * sufile,__u64 segnum,struct buffer_head * header_bh,struct buffer_head * su_bh)647 void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
648 struct buffer_head *header_bh,
649 struct buffer_head *su_bh)
650 {
651 struct nilfs_segment_usage *su;
652 size_t offset;
653 int suclean;
654
655 offset = nilfs_sufile_segment_usage_offset(sufile, segnum, su_bh);
656 su = kmap_local_folio(su_bh->b_folio, offset);
657 if (nilfs_segment_usage_error(su)) {
658 kunmap_local(su);
659 return;
660 }
661 suclean = nilfs_segment_usage_clean(su);
662 nilfs_segment_usage_set_error(su);
663 kunmap_local(su);
664
665 if (suclean) {
666 nilfs_sufile_mod_counter(header_bh, -1, 0);
667 NILFS_SUI(sufile)->ncleansegs--;
668 }
669 mark_buffer_dirty(su_bh);
670 nilfs_mdt_mark_dirty(sufile);
671 }
672
673 /**
674 * nilfs_sufile_truncate_range - truncate range of segment array
675 * @sufile: inode of segment usage file
676 * @start: start segment number (inclusive)
677 * @end: end segment number (inclusive)
678 *
679 * Return: 0 on success, or one of the following negative error codes on
680 * failure:
681 * * %-EBUSY - Dirty or active segments are present in the range.
682 * * %-EINVAL - Invalid number of segments specified.
683 * * %-EIO - I/O error (including metadata corruption).
684 * * %-ENOMEM - Insufficient memory available.
685 */
nilfs_sufile_truncate_range(struct inode * sufile,__u64 start,__u64 end)686 static int nilfs_sufile_truncate_range(struct inode *sufile,
687 __u64 start, __u64 end)
688 {
689 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
690 struct buffer_head *header_bh;
691 struct buffer_head *su_bh;
692 struct nilfs_segment_usage *su, *su2;
693 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
694 unsigned long segusages_per_block;
695 unsigned long nsegs, ncleaned;
696 __u64 segnum;
697 size_t offset;
698 ssize_t n, nc;
699 int ret;
700 int j;
701
702 nsegs = nilfs_sufile_get_nsegments(sufile);
703
704 ret = -EINVAL;
705 if (start > end || start >= nsegs)
706 goto out;
707
708 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
709 if (ret < 0)
710 goto out;
711
712 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
713 ncleaned = 0;
714
715 for (segnum = start; segnum <= end; segnum += n) {
716 n = min_t(unsigned long,
717 segusages_per_block -
718 nilfs_sufile_get_offset(sufile, segnum),
719 end - segnum + 1);
720 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
721 &su_bh);
722 if (ret < 0) {
723 if (ret != -ENOENT)
724 goto out_header;
725 /* hole */
726 continue;
727 }
728 offset = nilfs_sufile_segment_usage_offset(sufile, segnum,
729 su_bh);
730 su = kmap_local_folio(su_bh->b_folio, offset);
731 su2 = su;
732 for (j = 0; j < n; j++, su = (void *)su + susz) {
733 if ((le32_to_cpu(su->su_flags) &
734 ~BIT(NILFS_SEGMENT_USAGE_ERROR)) ||
735 nilfs_segment_is_active(nilfs, segnum + j)) {
736 ret = -EBUSY;
737 kunmap_local(su2);
738 brelse(su_bh);
739 goto out_header;
740 }
741 }
742 nc = 0;
743 for (su = su2, j = 0; j < n; j++, su = (void *)su + susz) {
744 if (nilfs_segment_usage_error(su)) {
745 nilfs_segment_usage_set_clean(su);
746 nc++;
747 }
748 }
749 kunmap_local(su2);
750 if (nc > 0) {
751 mark_buffer_dirty(su_bh);
752 ncleaned += nc;
753 }
754 brelse(su_bh);
755
756 if (n == segusages_per_block) {
757 /* make hole */
758 nilfs_sufile_delete_segment_usage_block(sufile, segnum);
759 }
760 }
761 ret = 0;
762
763 out_header:
764 if (ncleaned > 0) {
765 NILFS_SUI(sufile)->ncleansegs += ncleaned;
766 nilfs_sufile_mod_counter(header_bh, ncleaned, 0);
767 nilfs_mdt_mark_dirty(sufile);
768 }
769 brelse(header_bh);
770 out:
771 return ret;
772 }
773
774 /**
775 * nilfs_sufile_resize - resize segment array
776 * @sufile: inode of segment usage file
777 * @newnsegs: new number of segments
778 *
779 * Return: 0 on success, or one of the following negative error codes on
780 * failure:
781 * * %-EBUSY - Dirty or active segments exist in the region to be truncated.
782 * * %-EIO - I/O error (including metadata corruption).
783 * * %-ENOMEM - Insufficient memory available.
784 * * %-ENOSPC - Enough free space is not left for shrinking.
785 */
nilfs_sufile_resize(struct inode * sufile,__u64 newnsegs)786 int nilfs_sufile_resize(struct inode *sufile, __u64 newnsegs)
787 {
788 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
789 struct buffer_head *header_bh;
790 struct nilfs_sufile_header *header;
791 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
792 unsigned long nsegs, nrsvsegs;
793 int ret = 0;
794
795 down_write(&NILFS_MDT(sufile)->mi_sem);
796
797 nsegs = nilfs_sufile_get_nsegments(sufile);
798 if (nsegs == newnsegs)
799 goto out;
800
801 ret = -ENOSPC;
802 nrsvsegs = nilfs_nrsvsegs(nilfs, newnsegs);
803 if (newnsegs < nsegs && nsegs - newnsegs + nrsvsegs > sui->ncleansegs)
804 goto out;
805
806 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
807 if (ret < 0)
808 goto out;
809
810 if (newnsegs > nsegs) {
811 sui->ncleansegs += newnsegs - nsegs;
812 } else /* newnsegs < nsegs */ {
813 ret = nilfs_sufile_truncate_range(sufile, newnsegs, nsegs - 1);
814 if (ret < 0)
815 goto out_header;
816
817 sui->ncleansegs -= nsegs - newnsegs;
818
819 /*
820 * If the sufile is successfully truncated, immediately adjust
821 * the segment allocation space while locking the semaphore
822 * "mi_sem" so that nilfs_sufile_alloc() never allocates
823 * segments in the truncated space.
824 */
825 sui->allocmax = newnsegs - 1;
826 sui->allocmin = 0;
827 }
828
829 header = kmap_local_folio(header_bh->b_folio, 0);
830 header->sh_ncleansegs = cpu_to_le64(sui->ncleansegs);
831 kunmap_local(header);
832
833 mark_buffer_dirty(header_bh);
834 nilfs_mdt_mark_dirty(sufile);
835 nilfs_set_nsegments(nilfs, newnsegs);
836
837 out_header:
838 brelse(header_bh);
839 out:
840 up_write(&NILFS_MDT(sufile)->mi_sem);
841 return ret;
842 }
843
844 /**
845 * nilfs_sufile_get_suinfo - get segment usage information
846 * @sufile: inode of segment usage file
847 * @segnum: segment number to start looking
848 * @buf: array of suinfo
849 * @sisz: byte size of suinfo
850 * @nsi: size of suinfo array
851 *
852 * Return: Count of segment usage info items stored in the output buffer on
853 * success, or one of the following negative error codes on failure:
854 * * %-EIO - I/O error (including metadata corruption).
855 * * %-ENOMEM - Insufficient memory available.
856 */
nilfs_sufile_get_suinfo(struct inode * sufile,__u64 segnum,void * buf,unsigned int sisz,size_t nsi)857 ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, void *buf,
858 unsigned int sisz, size_t nsi)
859 {
860 struct buffer_head *su_bh;
861 struct nilfs_segment_usage *su;
862 struct nilfs_suinfo *si = buf;
863 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
864 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
865 size_t offset;
866 void *kaddr;
867 unsigned long nsegs, segusages_per_block;
868 ssize_t n;
869 int ret, i, j;
870
871 down_read(&NILFS_MDT(sufile)->mi_sem);
872
873 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
874 nsegs = min_t(unsigned long,
875 nilfs_sufile_get_nsegments(sufile) - segnum,
876 nsi);
877 for (i = 0; i < nsegs; i += n, segnum += n) {
878 n = min_t(unsigned long,
879 segusages_per_block -
880 nilfs_sufile_get_offset(sufile, segnum),
881 nsegs - i);
882 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
883 &su_bh);
884 if (ret < 0) {
885 if (ret != -ENOENT)
886 goto out;
887 /* hole */
888 memset(si, 0, sisz * n);
889 si = (void *)si + sisz * n;
890 continue;
891 }
892
893 offset = nilfs_sufile_segment_usage_offset(sufile, segnum,
894 su_bh);
895 su = kaddr = kmap_local_folio(su_bh->b_folio, offset);
896 for (j = 0; j < n;
897 j++, su = (void *)su + susz, si = (void *)si + sisz) {
898 si->sui_lastmod = le64_to_cpu(su->su_lastmod);
899 si->sui_nblocks = le32_to_cpu(su->su_nblocks);
900 si->sui_flags = le32_to_cpu(su->su_flags) &
901 ~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
902 if (nilfs_segment_is_active(nilfs, segnum + j))
903 si->sui_flags |=
904 BIT(NILFS_SEGMENT_USAGE_ACTIVE);
905 }
906 kunmap_local(kaddr);
907 brelse(su_bh);
908 }
909 ret = nsegs;
910
911 out:
912 up_read(&NILFS_MDT(sufile)->mi_sem);
913 return ret;
914 }
915
916 /**
917 * nilfs_sufile_set_suinfo - sets segment usage info
918 * @sufile: inode of segment usage file
919 * @buf: array of suinfo_update
920 * @supsz: byte size of suinfo_update
921 * @nsup: size of suinfo_update array
922 *
923 * Description: Takes an array of nilfs_suinfo_update structs and updates
924 * segment usage accordingly. Only the fields indicated by the sup_flags
925 * are updated.
926 *
927 * Return: 0 on success, or one of the following negative error codes on
928 * failure:
929 * * %-EINVAL - Invalid values in input (segment number, flags or nblocks).
930 * * %-EIO - I/O error (including metadata corruption).
931 * * %-ENOMEM - Insufficient memory available.
932 */
nilfs_sufile_set_suinfo(struct inode * sufile,void * buf,unsigned int supsz,size_t nsup)933 ssize_t nilfs_sufile_set_suinfo(struct inode *sufile, void *buf,
934 unsigned int supsz, size_t nsup)
935 {
936 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
937 struct buffer_head *header_bh, *bh;
938 struct nilfs_suinfo_update *sup, *supend = buf + supsz * nsup;
939 struct nilfs_segment_usage *su;
940 size_t offset;
941 unsigned long blkoff, prev_blkoff;
942 int cleansi, cleansu, dirtysi, dirtysu;
943 long ncleaned = 0, ndirtied = 0;
944 int ret = 0;
945
946 if (unlikely(nsup == 0))
947 return ret;
948
949 for (sup = buf; sup < supend; sup = (void *)sup + supsz) {
950 if (sup->sup_segnum >= nilfs->ns_nsegments
951 || (sup->sup_flags &
952 (~0UL << __NR_NILFS_SUINFO_UPDATE_FIELDS))
953 || (nilfs_suinfo_update_nblocks(sup) &&
954 sup->sup_sui.sui_nblocks >
955 nilfs->ns_blocks_per_segment))
956 return -EINVAL;
957 }
958
959 down_write(&NILFS_MDT(sufile)->mi_sem);
960
961 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
962 if (ret < 0)
963 goto out_sem;
964
965 sup = buf;
966 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
967 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
968 if (ret < 0)
969 goto out_header;
970
971 for (;;) {
972 offset = nilfs_sufile_segment_usage_offset(
973 sufile, sup->sup_segnum, bh);
974 su = kmap_local_folio(bh->b_folio, offset);
975
976 if (nilfs_suinfo_update_lastmod(sup))
977 su->su_lastmod = cpu_to_le64(sup->sup_sui.sui_lastmod);
978
979 if (nilfs_suinfo_update_nblocks(sup))
980 su->su_nblocks = cpu_to_le32(sup->sup_sui.sui_nblocks);
981
982 if (nilfs_suinfo_update_flags(sup)) {
983 /*
984 * Active flag is a virtual flag projected by running
985 * nilfs kernel code - drop it not to write it to
986 * disk.
987 */
988 sup->sup_sui.sui_flags &=
989 ~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
990
991 cleansi = nilfs_suinfo_clean(&sup->sup_sui);
992 cleansu = nilfs_segment_usage_clean(su);
993 dirtysi = nilfs_suinfo_dirty(&sup->sup_sui);
994 dirtysu = nilfs_segment_usage_dirty(su);
995
996 if (cleansi && !cleansu)
997 ++ncleaned;
998 else if (!cleansi && cleansu)
999 --ncleaned;
1000
1001 if (dirtysi && !dirtysu)
1002 ++ndirtied;
1003 else if (!dirtysi && dirtysu)
1004 --ndirtied;
1005
1006 su->su_flags = cpu_to_le32(sup->sup_sui.sui_flags);
1007 }
1008
1009 kunmap_local(su);
1010
1011 sup = (void *)sup + supsz;
1012 if (sup >= supend)
1013 break;
1014
1015 prev_blkoff = blkoff;
1016 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
1017 if (blkoff == prev_blkoff)
1018 continue;
1019
1020 /* get different block */
1021 mark_buffer_dirty(bh);
1022 put_bh(bh);
1023 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
1024 if (unlikely(ret < 0))
1025 goto out_mark;
1026 }
1027 mark_buffer_dirty(bh);
1028 put_bh(bh);
1029
1030 out_mark:
1031 if (ncleaned || ndirtied) {
1032 nilfs_sufile_mod_counter(header_bh, (u64)ncleaned,
1033 (u64)ndirtied);
1034 NILFS_SUI(sufile)->ncleansegs += ncleaned;
1035 }
1036 nilfs_mdt_mark_dirty(sufile);
1037 out_header:
1038 put_bh(header_bh);
1039 out_sem:
1040 up_write(&NILFS_MDT(sufile)->mi_sem);
1041 return ret;
1042 }
1043
1044 /**
1045 * nilfs_sufile_trim_fs() - trim ioctl handle function
1046 * @sufile: inode of segment usage file
1047 * @range: fstrim_range structure
1048 *
1049 * start: First Byte to trim
1050 * len: number of Bytes to trim from start
1051 * minlen: minimum extent length in Bytes
1052 *
1053 * Decription: nilfs_sufile_trim_fs goes through all segments containing bytes
1054 * from start to start+len. start is rounded up to the next block boundary
1055 * and start+len is rounded down. For each clean segment blkdev_issue_discard
1056 * function is invoked.
1057 *
1058 * Return: 0 on success, or a negative error code on failure.
1059 */
nilfs_sufile_trim_fs(struct inode * sufile,struct fstrim_range * range)1060 int nilfs_sufile_trim_fs(struct inode *sufile, struct fstrim_range *range)
1061 {
1062 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
1063 struct buffer_head *su_bh;
1064 struct nilfs_segment_usage *su;
1065 size_t offset;
1066 void *kaddr;
1067 size_t n, i, susz = NILFS_MDT(sufile)->mi_entry_size;
1068 sector_t seg_start, seg_end, start_block, end_block;
1069 sector_t start = 0, nblocks = 0;
1070 u64 segnum, segnum_end, minlen, len, max_blocks, ndiscarded = 0;
1071 int ret = 0;
1072 unsigned int sects_per_block;
1073
1074 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
1075 bdev_logical_block_size(nilfs->ns_bdev);
1076 len = range->len >> nilfs->ns_blocksize_bits;
1077 minlen = range->minlen >> nilfs->ns_blocksize_bits;
1078 max_blocks = ((u64)nilfs->ns_nsegments * nilfs->ns_blocks_per_segment);
1079
1080 if (!len || range->start >= max_blocks << nilfs->ns_blocksize_bits)
1081 return -EINVAL;
1082
1083 start_block = (range->start + nilfs->ns_blocksize - 1) >>
1084 nilfs->ns_blocksize_bits;
1085
1086 /*
1087 * range->len can be very large (actually, it is set to
1088 * ULLONG_MAX by default) - truncate upper end of the range
1089 * carefully so as not to overflow.
1090 */
1091 if (max_blocks - start_block < len)
1092 end_block = max_blocks - 1;
1093 else
1094 end_block = start_block + len - 1;
1095
1096 segnum = nilfs_get_segnum_of_block(nilfs, start_block);
1097 segnum_end = nilfs_get_segnum_of_block(nilfs, end_block);
1098
1099 down_read(&NILFS_MDT(sufile)->mi_sem);
1100
1101 while (segnum <= segnum_end) {
1102 n = nilfs_sufile_segment_usages_in_block(sufile, segnum,
1103 segnum_end);
1104
1105 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
1106 &su_bh);
1107 if (ret < 0) {
1108 if (ret != -ENOENT)
1109 goto out_sem;
1110 /* hole */
1111 segnum += n;
1112 continue;
1113 }
1114
1115 offset = nilfs_sufile_segment_usage_offset(sufile, segnum,
1116 su_bh);
1117 su = kaddr = kmap_local_folio(su_bh->b_folio, offset);
1118 for (i = 0; i < n; ++i, ++segnum, su = (void *)su + susz) {
1119 if (!nilfs_segment_usage_clean(su))
1120 continue;
1121
1122 nilfs_get_segment_range(nilfs, segnum, &seg_start,
1123 &seg_end);
1124
1125 if (!nblocks) {
1126 /* start new extent */
1127 start = seg_start;
1128 nblocks = seg_end - seg_start + 1;
1129 continue;
1130 }
1131
1132 if (start + nblocks == seg_start) {
1133 /* add to previous extent */
1134 nblocks += seg_end - seg_start + 1;
1135 continue;
1136 }
1137
1138 /* discard previous extent */
1139 if (start < start_block) {
1140 nblocks -= start_block - start;
1141 start = start_block;
1142 }
1143
1144 if (nblocks >= minlen) {
1145 kunmap_local(kaddr);
1146
1147 ret = blkdev_issue_discard(nilfs->ns_bdev,
1148 start * sects_per_block,
1149 nblocks * sects_per_block,
1150 GFP_NOFS);
1151 if (ret < 0) {
1152 put_bh(su_bh);
1153 goto out_sem;
1154 }
1155
1156 ndiscarded += nblocks;
1157 offset = nilfs_sufile_segment_usage_offset(
1158 sufile, segnum, su_bh);
1159 su = kaddr = kmap_local_folio(su_bh->b_folio,
1160 offset);
1161 }
1162
1163 /* start new extent */
1164 start = seg_start;
1165 nblocks = seg_end - seg_start + 1;
1166 }
1167 kunmap_local(kaddr);
1168 put_bh(su_bh);
1169 }
1170
1171
1172 if (nblocks) {
1173 /* discard last extent */
1174 if (start < start_block) {
1175 nblocks -= start_block - start;
1176 start = start_block;
1177 }
1178 if (start + nblocks > end_block + 1)
1179 nblocks = end_block - start + 1;
1180
1181 if (nblocks >= minlen) {
1182 ret = blkdev_issue_discard(nilfs->ns_bdev,
1183 start * sects_per_block,
1184 nblocks * sects_per_block,
1185 GFP_NOFS);
1186 if (!ret)
1187 ndiscarded += nblocks;
1188 }
1189 }
1190
1191 out_sem:
1192 up_read(&NILFS_MDT(sufile)->mi_sem);
1193
1194 range->len = ndiscarded << nilfs->ns_blocksize_bits;
1195 return ret;
1196 }
1197
1198 /**
1199 * nilfs_sufile_read - read or get sufile inode
1200 * @sb: super block instance
1201 * @susize: size of a segment usage entry
1202 * @raw_inode: on-disk sufile inode
1203 * @inodep: buffer to store the inode
1204 *
1205 * Return: 0 on success, or a negative error code on failure.
1206 */
nilfs_sufile_read(struct super_block * sb,size_t susize,struct nilfs_inode * raw_inode,struct inode ** inodep)1207 int nilfs_sufile_read(struct super_block *sb, size_t susize,
1208 struct nilfs_inode *raw_inode, struct inode **inodep)
1209 {
1210 struct inode *sufile;
1211 struct nilfs_sufile_info *sui;
1212 struct buffer_head *header_bh;
1213 struct nilfs_sufile_header *header;
1214 int err;
1215
1216 if (susize > sb->s_blocksize) {
1217 nilfs_err(sb, "too large segment usage size: %zu bytes",
1218 susize);
1219 return -EINVAL;
1220 } else if (susize < NILFS_MIN_SEGMENT_USAGE_SIZE) {
1221 nilfs_err(sb, "too small segment usage size: %zu bytes",
1222 susize);
1223 return -EINVAL;
1224 }
1225
1226 sufile = nilfs_iget_locked(sb, NULL, NILFS_SUFILE_INO);
1227 if (unlikely(!sufile))
1228 return -ENOMEM;
1229 if (!(sufile->i_state & I_NEW))
1230 goto out;
1231
1232 err = nilfs_mdt_init(sufile, NILFS_MDT_GFP, sizeof(*sui));
1233 if (err)
1234 goto failed;
1235
1236 nilfs_mdt_set_entry_size(sufile, susize,
1237 sizeof(struct nilfs_sufile_header));
1238
1239 err = nilfs_read_inode_common(sufile, raw_inode);
1240 if (err)
1241 goto failed;
1242
1243 err = nilfs_mdt_get_block(sufile, 0, 0, NULL, &header_bh);
1244 if (unlikely(err)) {
1245 if (err == -ENOENT) {
1246 nilfs_err(sb,
1247 "missing header block in segment usage metadata");
1248 err = -EINVAL;
1249 }
1250 goto failed;
1251 }
1252
1253 sui = NILFS_SUI(sufile);
1254 header = kmap_local_folio(header_bh->b_folio, 0);
1255 sui->ncleansegs = le64_to_cpu(header->sh_ncleansegs);
1256 kunmap_local(header);
1257 brelse(header_bh);
1258
1259 sui->allocmax = nilfs_sufile_get_nsegments(sufile) - 1;
1260 sui->allocmin = 0;
1261
1262 unlock_new_inode(sufile);
1263 out:
1264 *inodep = sufile;
1265 return 0;
1266 failed:
1267 iget_failed(sufile);
1268 return err;
1269 }
1270