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