Blame view

fs/nilfs2/sufile.c 31.6 KB
ae98043f5   Ryusuke Konishi   nilfs2: convert t...
1
  // SPDX-License-Identifier: GPL-2.0+
6c98cd4ec   Koji Sato   nilfs2: segment u...
2
3
4
5
6
  /*
   * sufile.c - NILFS segment usage file.
   *
   * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
   *
4b420ab4e   Ryusuke Konishi   nilfs2: clean up ...
7
8
   * Written by Koji Sato.
   * Revised by Ryusuke Konishi.
6c98cd4ec   Koji Sato   nilfs2: segment u...
9
10
11
12
13
14
15
   */
  
  #include <linux/kernel.h>
  #include <linux/fs.h>
  #include <linux/string.h>
  #include <linux/buffer_head.h>
  #include <linux/errno.h>
6c98cd4ec   Koji Sato   nilfs2: segment u...
16
17
  #include "mdt.h"
  #include "sufile.h"
83eec5e6d   Hitoshi Mitake   nilfs2: add trace...
18
  #include <trace/events/nilfs2.h>
f5974c8f8   Vyacheslav Dubeyko   nilfs2: add omitt...
19
20
21
22
23
24
25
  /**
   * struct nilfs_sufile_info - on-memory private data of sufile
   * @mi: on-memory private data of metadata file
   * @ncleansegs: number of clean segments
   * @allocmin: lower limit of allocatable segment range
   * @allocmax: upper limit of allocatable segment range
   */
aa474a220   Ryusuke Konishi   nilfs2: add local...
26
27
  struct nilfs_sufile_info {
  	struct nilfs_mdt_info mi;
619205da5   Ryusuke Konishi   nilfs2: add ioctl...
28
29
30
  	unsigned long ncleansegs;/* number of clean segments */
  	__u64 allocmin;		/* lower limit of allocatable segment range */
  	__u64 allocmax;		/* upper limit of allocatable segment range */
aa474a220   Ryusuke Konishi   nilfs2: add local...
31
32
33
34
35
36
  };
  
  static inline struct nilfs_sufile_info *NILFS_SUI(struct inode *sufile)
  {
  	return (struct nilfs_sufile_info *)NILFS_MDT(sufile);
  }
6c98cd4ec   Koji Sato   nilfs2: segment u...
37
38
39
40
41
42
43
44
45
46
  static inline unsigned long
  nilfs_sufile_segment_usages_per_block(const struct inode *sufile)
  {
  	return NILFS_MDT(sufile)->mi_entries_per_block;
  }
  
  static unsigned long
  nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum)
  {
  	__u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
4ad364ca1   Ryusuke Konishi   nilfs2: add missi...
47

6c98cd4ec   Koji Sato   nilfs2: segment u...
48
49
50
51
52
53
54
55
  	do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
  	return (unsigned long)t;
  }
  
  static unsigned long
  nilfs_sufile_get_offset(const struct inode *sufile, __u64 segnum)
  {
  	__u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
4ad364ca1   Ryusuke Konishi   nilfs2: add missi...
56

6c98cd4ec   Koji Sato   nilfs2: segment u...
57
58
59
60
61
62
63
64
65
66
67
68
  	return do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
  }
  
  static unsigned long
  nilfs_sufile_segment_usages_in_block(const struct inode *sufile, __u64 curr,
  				     __u64 max)
  {
  	return min_t(unsigned long,
  		     nilfs_sufile_segment_usages_per_block(sufile) -
  		     nilfs_sufile_get_offset(sufile, curr),
  		     max - curr + 1);
  }
6c98cd4ec   Koji Sato   nilfs2: segment u...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  static struct nilfs_segment_usage *
  nilfs_sufile_block_get_segment_usage(const struct inode *sufile, __u64 segnum,
  				     struct buffer_head *bh, void *kaddr)
  {
  	return kaddr + bh_offset(bh) +
  		nilfs_sufile_get_offset(sufile, segnum) *
  		NILFS_MDT(sufile)->mi_entry_size;
  }
  
  static inline int nilfs_sufile_get_header_block(struct inode *sufile,
  						struct buffer_head **bhp)
  {
  	return nilfs_mdt_get_block(sufile, 0, 0, NULL, bhp);
  }
  
  static inline int
  nilfs_sufile_get_segment_usage_block(struct inode *sufile, __u64 segnum,
  				     int create, struct buffer_head **bhp)
  {
  	return nilfs_mdt_get_block(sufile,
  				   nilfs_sufile_get_blkoff(sufile, segnum),
  				   create, NULL, bhp);
  }
78eb64c24   Ryusuke Konishi   nilfs2: add trunc...
92
93
94
95
96
97
  static int nilfs_sufile_delete_segment_usage_block(struct inode *sufile,
  						   __u64 segnum)
  {
  	return nilfs_mdt_delete_block(sufile,
  				      nilfs_sufile_get_blkoff(sufile, segnum));
  }
a703018f7   Ryusuke Konishi   nilfs2: segment u...
98
99
100
101
102
  static void nilfs_sufile_mod_counter(struct buffer_head *header_bh,
  				     u64 ncleanadd, u64 ndirtyadd)
  {
  	struct nilfs_sufile_header *header;
  	void *kaddr;
7b9c0976a   Cong Wang   nilfs2: remove th...
103
  	kaddr = kmap_atomic(header_bh->b_page);
a703018f7   Ryusuke Konishi   nilfs2: segment u...
104
105
106
  	header = kaddr + bh_offset(header_bh);
  	le64_add_cpu(&header->sh_ncleansegs, ncleanadd);
  	le64_add_cpu(&header->sh_ndirtysegs, ndirtyadd);
7b9c0976a   Cong Wang   nilfs2: remove th...
107
  	kunmap_atomic(kaddr);
a703018f7   Ryusuke Konishi   nilfs2: segment u...
108

5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
109
  	mark_buffer_dirty(header_bh);
a703018f7   Ryusuke Konishi   nilfs2: segment u...
110
  }
dda54f4b8   Ryusuke Konishi   nilfs2: add sufil...
111
  /**
ef7d4757a   Ryusuke Konishi   nilfs2: simplify ...
112
113
114
115
116
117
118
119
120
   * nilfs_sufile_get_ncleansegs - return the number of clean segments
   * @sufile: inode of segment usage file
   */
  unsigned long nilfs_sufile_get_ncleansegs(struct inode *sufile)
  {
  	return NILFS_SUI(sufile)->ncleansegs;
  }
  
  /**
dda54f4b8   Ryusuke Konishi   nilfs2: add sufil...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
   * nilfs_sufile_updatev - modify multiple segment usages at a time
   * @sufile: inode of segment usage file
   * @segnumv: array of segment numbers
   * @nsegs: size of @segnumv array
   * @create: creation flag
   * @ndone: place to store number of modified segments on @segnumv
   * @dofunc: primitive operation for the update
   *
   * Description: nilfs_sufile_updatev() repeatedly calls @dofunc
   * against the given array of segments.  The @dofunc is called with
   * buffers of a header block and the sufile block in which the target
   * segment usage entry is contained.  If @ndone is given, the number
   * of successfully modified segments from the head is stored in the
   * place @ndone points to.
   *
   * Return Value: On success, zero is returned.  On error, one of the
   * following negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   *
   * %-ENOENT - Given segment usage is in hole block (may be returned if
   *            @create is zero)
   *
   * %-EINVAL - Invalid segment usage number
   */
  int 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 *))
  {
  	struct buffer_head *header_bh, *bh;
  	unsigned long blkoff, prev_blkoff;
  	__u64 *seg;
  	size_t nerr = 0, n = 0;
  	int ret = 0;
  
  	if (unlikely(nsegs == 0))
  		goto out;
  
  	down_write(&NILFS_MDT(sufile)->mi_sem);
  	for (seg = segnumv; seg < segnumv + nsegs; seg++) {
  		if (unlikely(*seg >= nilfs_sufile_get_nsegments(sufile))) {
feee880fa   Ryusuke Konishi   nilfs2: reduce ba...
166
167
168
  			nilfs_msg(sufile->i_sb, KERN_WARNING,
  				  "%s: invalid segment number: %llu",
  				  __func__, (unsigned long long)*seg);
dda54f4b8   Ryusuke Konishi   nilfs2: add sufil...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  			nerr++;
  		}
  	}
  	if (nerr > 0) {
  		ret = -EINVAL;
  		goto out_sem;
  	}
  
  	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
  	if (ret < 0)
  		goto out_sem;
  
  	seg = segnumv;
  	blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
  	ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
  	if (ret < 0)
  		goto out_header;
  
  	for (;;) {
  		dofunc(sufile, *seg, header_bh, bh);
  
  		if (++seg >= segnumv + nsegs)
  			break;
  		prev_blkoff = blkoff;
  		blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
  		if (blkoff == prev_blkoff)
  			continue;
  
  		/* get different block */
  		brelse(bh);
  		ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
  		if (unlikely(ret < 0))
  			goto out_header;
  	}
  	brelse(bh);
  
   out_header:
  	n = seg - segnumv;
  	brelse(header_bh);
   out_sem:
  	up_write(&NILFS_MDT(sufile)->mi_sem);
   out:
  	if (ndone)
  		*ndone = n;
  	return ret;
  }
a703018f7   Ryusuke Konishi   nilfs2: segment u...
215
216
217
218
219
220
221
222
223
  int nilfs_sufile_update(struct inode *sufile, __u64 segnum, int create,
  			void (*dofunc)(struct inode *, __u64,
  				       struct buffer_head *,
  				       struct buffer_head *))
  {
  	struct buffer_head *header_bh, *bh;
  	int ret;
  
  	if (unlikely(segnum >= nilfs_sufile_get_nsegments(sufile))) {
feee880fa   Ryusuke Konishi   nilfs2: reduce ba...
224
225
226
  		nilfs_msg(sufile->i_sb, KERN_WARNING,
  			  "%s: invalid segment number: %llu",
  			  __func__, (unsigned long long)segnum);
a703018f7   Ryusuke Konishi   nilfs2: segment u...
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  		return -EINVAL;
  	}
  	down_write(&NILFS_MDT(sufile)->mi_sem);
  
  	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
  	if (ret < 0)
  		goto out_sem;
  
  	ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, create, &bh);
  	if (!ret) {
  		dofunc(sufile, segnum, header_bh, bh);
  		brelse(bh);
  	}
  	brelse(header_bh);
  
   out_sem:
  	up_write(&NILFS_MDT(sufile)->mi_sem);
  	return ret;
  }
6c98cd4ec   Koji Sato   nilfs2: segment u...
246
  /**
619205da5   Ryusuke Konishi   nilfs2: add ioctl...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
   * nilfs_sufile_set_alloc_range - limit range of segment to be allocated
   * @sufile: inode of segment usage file
   * @start: minimum segment number of allocatable region (inclusive)
   * @end: maximum segment number of allocatable region (inclusive)
   *
   * Return Value: On success, 0 is returned.  On error, one of the
   * following negative error codes is returned.
   *
   * %-ERANGE - invalid segment region
   */
  int nilfs_sufile_set_alloc_range(struct inode *sufile, __u64 start, __u64 end)
  {
  	struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
  	__u64 nsegs;
  	int ret = -ERANGE;
  
  	down_write(&NILFS_MDT(sufile)->mi_sem);
  	nsegs = nilfs_sufile_get_nsegments(sufile);
  
  	if (start <= end && end < nsegs) {
  		sui->allocmin = start;
  		sui->allocmax = end;
  		ret = 0;
  	}
  	up_write(&NILFS_MDT(sufile)->mi_sem);
  	return ret;
  }
  
  /**
6c98cd4ec   Koji Sato   nilfs2: segment u...
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
   * nilfs_sufile_alloc - allocate a segment
   * @sufile: inode of segment usage file
   * @segnump: pointer to segment number
   *
   * Description: nilfs_sufile_alloc() allocates a clean segment.
   *
   * Return Value: On success, 0 is returned and the segment number of the
   * allocated segment is stored in the place pointed by @segnump. On error, one
   * of the following negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   *
   * %-ENOSPC - No clean segment left.
   */
  int nilfs_sufile_alloc(struct inode *sufile, __u64 *segnump)
  {
  	struct buffer_head *header_bh, *su_bh;
6c98cd4ec   Koji Sato   nilfs2: segment u...
295
296
  	struct nilfs_sufile_header *header;
  	struct nilfs_segment_usage *su;
619205da5   Ryusuke Konishi   nilfs2: add ioctl...
297
  	struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
6c98cd4ec   Koji Sato   nilfs2: segment u...
298
299
300
  	size_t susz = NILFS_MDT(sufile)->mi_entry_size;
  	__u64 segnum, maxsegnum, last_alloc;
  	void *kaddr;
09ef29e0f   Ryusuke Konishi   nilfs2: fix gcc u...
301
  	unsigned long nsegments, nsus, cnt;
619205da5   Ryusuke Konishi   nilfs2: add ioctl...
302
  	int ret, j;
6c98cd4ec   Koji Sato   nilfs2: segment u...
303
304
  
  	down_write(&NILFS_MDT(sufile)->mi_sem);
6c98cd4ec   Koji Sato   nilfs2: segment u...
305
306
307
  	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
  	if (ret < 0)
  		goto out_sem;
7b9c0976a   Cong Wang   nilfs2: remove th...
308
  	kaddr = kmap_atomic(header_bh->b_page);
7b16c8a21   Ryusuke Konishi   nilfs2: unfold ni...
309
  	header = kaddr + bh_offset(header_bh);
6c98cd4ec   Koji Sato   nilfs2: segment u...
310
  	last_alloc = le64_to_cpu(header->sh_last_alloc);
7b9c0976a   Cong Wang   nilfs2: remove th...
311
  	kunmap_atomic(kaddr);
6c98cd4ec   Koji Sato   nilfs2: segment u...
312
313
  
  	nsegments = nilfs_sufile_get_nsegments(sufile);
619205da5   Ryusuke Konishi   nilfs2: add ioctl...
314
  	maxsegnum = sui->allocmax;
6c98cd4ec   Koji Sato   nilfs2: segment u...
315
  	segnum = last_alloc + 1;
619205da5   Ryusuke Konishi   nilfs2: add ioctl...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  	if (segnum < sui->allocmin || segnum > sui->allocmax)
  		segnum = sui->allocmin;
  
  	for (cnt = 0; cnt < nsegments; cnt += nsus) {
  		if (segnum > maxsegnum) {
  			if (cnt < sui->allocmax - sui->allocmin + 1) {
  				/*
  				 * wrap around in the limited region.
  				 * if allocation started from
  				 * sui->allocmin, this never happens.
  				 */
  				segnum = sui->allocmin;
  				maxsegnum = last_alloc;
  			} else if (segnum > sui->allocmin &&
  				   sui->allocmax + 1 < nsegments) {
  				segnum = sui->allocmax + 1;
  				maxsegnum = nsegments - 1;
  			} else if (sui->allocmin > 0)  {
  				segnum = 0;
  				maxsegnum = sui->allocmin - 1;
  			} else {
  				break; /* never happens */
  			}
6c98cd4ec   Koji Sato   nilfs2: segment u...
339
  		}
83eec5e6d   Hitoshi Mitake   nilfs2: add trace...
340
  		trace_nilfs2_segment_usage_check(sufile, segnum, cnt);
6c98cd4ec   Koji Sato   nilfs2: segment u...
341
342
343
344
  		ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1,
  							   &su_bh);
  		if (ret < 0)
  			goto out_header;
7b9c0976a   Cong Wang   nilfs2: remove th...
345
  		kaddr = kmap_atomic(su_bh->b_page);
6c98cd4ec   Koji Sato   nilfs2: segment u...
346
347
348
349
350
351
352
353
354
  		su = nilfs_sufile_block_get_segment_usage(
  			sufile, segnum, su_bh, kaddr);
  
  		nsus = nilfs_sufile_segment_usages_in_block(
  			sufile, segnum, maxsegnum);
  		for (j = 0; j < nsus; j++, su = (void *)su + susz, segnum++) {
  			if (!nilfs_segment_usage_clean(su))
  				continue;
  			/* found a clean segment */
6c98cd4ec   Koji Sato   nilfs2: segment u...
355
  			nilfs_segment_usage_set_dirty(su);
7b9c0976a   Cong Wang   nilfs2: remove th...
356
  			kunmap_atomic(kaddr);
6c98cd4ec   Koji Sato   nilfs2: segment u...
357

7b9c0976a   Cong Wang   nilfs2: remove th...
358
  			kaddr = kmap_atomic(header_bh->b_page);
7b16c8a21   Ryusuke Konishi   nilfs2: unfold ni...
359
  			header = kaddr + bh_offset(header_bh);
6c98cd4ec   Koji Sato   nilfs2: segment u...
360
361
362
  			le64_add_cpu(&header->sh_ncleansegs, -1);
  			le64_add_cpu(&header->sh_ndirtysegs, 1);
  			header->sh_last_alloc = cpu_to_le64(segnum);
7b9c0976a   Cong Wang   nilfs2: remove th...
363
  			kunmap_atomic(kaddr);
6c98cd4ec   Koji Sato   nilfs2: segment u...
364

619205da5   Ryusuke Konishi   nilfs2: add ioctl...
365
  			sui->ncleansegs--;
5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
366
367
  			mark_buffer_dirty(header_bh);
  			mark_buffer_dirty(su_bh);
6c98cd4ec   Koji Sato   nilfs2: segment u...
368
369
370
  			nilfs_mdt_mark_dirty(sufile);
  			brelse(su_bh);
  			*segnump = segnum;
83eec5e6d   Hitoshi Mitake   nilfs2: add trace...
371
372
  
  			trace_nilfs2_segment_usage_allocated(sufile, segnum);
6c98cd4ec   Koji Sato   nilfs2: segment u...
373
374
  			goto out_header;
  		}
7b9c0976a   Cong Wang   nilfs2: remove th...
375
  		kunmap_atomic(kaddr);
6c98cd4ec   Koji Sato   nilfs2: segment u...
376
377
378
379
380
381
382
383
384
385
386
387
388
  		brelse(su_bh);
  	}
  
  	/* no segments left */
  	ret = -ENOSPC;
  
   out_header:
  	brelse(header_bh);
  
   out_sem:
  	up_write(&NILFS_MDT(sufile)->mi_sem);
  	return ret;
  }
a703018f7   Ryusuke Konishi   nilfs2: segment u...
389
390
391
  void nilfs_sufile_do_cancel_free(struct inode *sufile, __u64 segnum,
  				 struct buffer_head *header_bh,
  				 struct buffer_head *su_bh)
6c98cd4ec   Koji Sato   nilfs2: segment u...
392
  {
6c98cd4ec   Koji Sato   nilfs2: segment u...
393
394
  	struct nilfs_segment_usage *su;
  	void *kaddr;
6c98cd4ec   Koji Sato   nilfs2: segment u...
395

7b9c0976a   Cong Wang   nilfs2: remove th...
396
  	kaddr = kmap_atomic(su_bh->b_page);
a703018f7   Ryusuke Konishi   nilfs2: segment u...
397
  	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
1f5abe7e7   Ryusuke Konishi   nilfs2: replace B...
398
  	if (unlikely(!nilfs_segment_usage_clean(su))) {
feee880fa   Ryusuke Konishi   nilfs2: reduce ba...
399
400
401
  		nilfs_msg(sufile->i_sb, KERN_WARNING,
  			  "%s: segment %llu must be clean", __func__,
  			  (unsigned long long)segnum);
7b9c0976a   Cong Wang   nilfs2: remove th...
402
  		kunmap_atomic(kaddr);
a703018f7   Ryusuke Konishi   nilfs2: segment u...
403
  		return;
6c98cd4ec   Koji Sato   nilfs2: segment u...
404
405
  	}
  	nilfs_segment_usage_set_dirty(su);
7b9c0976a   Cong Wang   nilfs2: remove th...
406
  	kunmap_atomic(kaddr);
6c98cd4ec   Koji Sato   nilfs2: segment u...
407

a703018f7   Ryusuke Konishi   nilfs2: segment u...
408
  	nilfs_sufile_mod_counter(header_bh, -1, 1);
aa474a220   Ryusuke Konishi   nilfs2: add local...
409
  	NILFS_SUI(sufile)->ncleansegs--;
5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
410
  	mark_buffer_dirty(su_bh);
6c98cd4ec   Koji Sato   nilfs2: segment u...
411
  	nilfs_mdt_mark_dirty(sufile);
6c98cd4ec   Koji Sato   nilfs2: segment u...
412
  }
c85399c2d   Ryusuke Konishi   nilfs2: fix possi...
413
414
415
416
417
418
419
  void nilfs_sufile_do_scrap(struct inode *sufile, __u64 segnum,
  			   struct buffer_head *header_bh,
  			   struct buffer_head *su_bh)
  {
  	struct nilfs_segment_usage *su;
  	void *kaddr;
  	int clean, dirty;
7b9c0976a   Cong Wang   nilfs2: remove th...
420
  	kaddr = kmap_atomic(su_bh->b_page);
c85399c2d   Ryusuke Konishi   nilfs2: fix possi...
421
  	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
4ce5c3426   Ryusuke Konishi   nilfs2: use BIT()...
422
  	if (su->su_flags == cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY)) &&
c85399c2d   Ryusuke Konishi   nilfs2: fix possi...
423
  	    su->su_nblocks == cpu_to_le32(0)) {
7b9c0976a   Cong Wang   nilfs2: remove th...
424
  		kunmap_atomic(kaddr);
c85399c2d   Ryusuke Konishi   nilfs2: fix possi...
425
426
427
428
429
430
431
432
  		return;
  	}
  	clean = nilfs_segment_usage_clean(su);
  	dirty = nilfs_segment_usage_dirty(su);
  
  	/* make the segment garbage */
  	su->su_lastmod = cpu_to_le64(0);
  	su->su_nblocks = cpu_to_le32(0);
4ce5c3426   Ryusuke Konishi   nilfs2: use BIT()...
433
  	su->su_flags = cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY));
7b9c0976a   Cong Wang   nilfs2: remove th...
434
  	kunmap_atomic(kaddr);
c85399c2d   Ryusuke Konishi   nilfs2: fix possi...
435
436
  
  	nilfs_sufile_mod_counter(header_bh, clean ? (u64)-1 : 0, dirty ? 0 : 1);
aa474a220   Ryusuke Konishi   nilfs2: add local...
437
  	NILFS_SUI(sufile)->ncleansegs -= clean;
5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
438
  	mark_buffer_dirty(su_bh);
c85399c2d   Ryusuke Konishi   nilfs2: fix possi...
439
440
  	nilfs_mdt_mark_dirty(sufile);
  }
a703018f7   Ryusuke Konishi   nilfs2: segment u...
441
442
443
  void nilfs_sufile_do_free(struct inode *sufile, __u64 segnum,
  			  struct buffer_head *header_bh,
  			  struct buffer_head *su_bh)
6c98cd4ec   Koji Sato   nilfs2: segment u...
444
  {
6c98cd4ec   Koji Sato   nilfs2: segment u...
445
446
  	struct nilfs_segment_usage *su;
  	void *kaddr;
a703018f7   Ryusuke Konishi   nilfs2: segment u...
447
  	int sudirty;
6c98cd4ec   Koji Sato   nilfs2: segment u...
448

7b9c0976a   Cong Wang   nilfs2: remove th...
449
  	kaddr = kmap_atomic(su_bh->b_page);
a703018f7   Ryusuke Konishi   nilfs2: segment u...
450
451
  	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
  	if (nilfs_segment_usage_clean(su)) {
feee880fa   Ryusuke Konishi   nilfs2: reduce ba...
452
453
454
  		nilfs_msg(sufile->i_sb, KERN_WARNING,
  			  "%s: segment %llu is already clean",
  			  __func__, (unsigned long long)segnum);
7b9c0976a   Cong Wang   nilfs2: remove th...
455
  		kunmap_atomic(kaddr);
a703018f7   Ryusuke Konishi   nilfs2: segment u...
456
  		return;
6c98cd4ec   Koji Sato   nilfs2: segment u...
457
  	}
a703018f7   Ryusuke Konishi   nilfs2: segment u...
458
459
  	WARN_ON(nilfs_segment_usage_error(su));
  	WARN_ON(!nilfs_segment_usage_dirty(su));
6c98cd4ec   Koji Sato   nilfs2: segment u...
460

a703018f7   Ryusuke Konishi   nilfs2: segment u...
461
462
  	sudirty = nilfs_segment_usage_dirty(su);
  	nilfs_segment_usage_set_clean(su);
7b9c0976a   Cong Wang   nilfs2: remove th...
463
  	kunmap_atomic(kaddr);
5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
464
  	mark_buffer_dirty(su_bh);
6c98cd4ec   Koji Sato   nilfs2: segment u...
465

a703018f7   Ryusuke Konishi   nilfs2: segment u...
466
  	nilfs_sufile_mod_counter(header_bh, 1, sudirty ? (u64)-1 : 0);
aa474a220   Ryusuke Konishi   nilfs2: add local...
467
  	NILFS_SUI(sufile)->ncleansegs++;
a703018f7   Ryusuke Konishi   nilfs2: segment u...
468
  	nilfs_mdt_mark_dirty(sufile);
83eec5e6d   Hitoshi Mitake   nilfs2: add trace...
469
470
  
  	trace_nilfs2_segment_usage_freed(sufile, segnum);
6c98cd4ec   Koji Sato   nilfs2: segment u...
471
472
473
  }
  
  /**
61a189e9c   Ryusuke Konishi   nilfs2: move rout...
474
475
476
477
478
479
480
481
482
483
484
   * nilfs_sufile_mark_dirty - mark the buffer having a segment usage dirty
   * @sufile: inode of segment usage file
   * @segnum: segment number
   */
  int nilfs_sufile_mark_dirty(struct inode *sufile, __u64 segnum)
  {
  	struct buffer_head *bh;
  	int ret;
  
  	ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
  	if (!ret) {
5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
485
  		mark_buffer_dirty(bh);
61a189e9c   Ryusuke Konishi   nilfs2: move rout...
486
487
488
489
490
491
492
  		nilfs_mdt_mark_dirty(sufile);
  		brelse(bh);
  	}
  	return ret;
  }
  
  /**
071ec54dd   Ryusuke Konishi   nilfs2: move rout...
493
494
495
496
497
498
499
   * nilfs_sufile_set_segment_usage - set usage of a segment
   * @sufile: inode of segment usage file
   * @segnum: segment number
   * @nblocks: number of live blocks in the segment
   * @modtime: modification time (option)
   */
  int nilfs_sufile_set_segment_usage(struct inode *sufile, __u64 segnum,
fb04b91bc   Arnd Bergmann   nilfs2: use time6...
500
  				   unsigned long nblocks, time64_t modtime)
071ec54dd   Ryusuke Konishi   nilfs2: move rout...
501
502
503
504
505
506
507
508
509
510
  {
  	struct buffer_head *bh;
  	struct nilfs_segment_usage *su;
  	void *kaddr;
  	int ret;
  
  	down_write(&NILFS_MDT(sufile)->mi_sem);
  	ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
  	if (ret < 0)
  		goto out_sem;
7b9c0976a   Cong Wang   nilfs2: remove th...
511
  	kaddr = kmap_atomic(bh->b_page);
071ec54dd   Ryusuke Konishi   nilfs2: move rout...
512
513
514
515
516
  	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
  	WARN_ON(nilfs_segment_usage_error(su));
  	if (modtime)
  		su->su_lastmod = cpu_to_le64(modtime);
  	su->su_nblocks = cpu_to_le32(nblocks);
7b9c0976a   Cong Wang   nilfs2: remove th...
517
  	kunmap_atomic(kaddr);
071ec54dd   Ryusuke Konishi   nilfs2: move rout...
518

5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
519
  	mark_buffer_dirty(bh);
071ec54dd   Ryusuke Konishi   nilfs2: move rout...
520
521
522
523
524
525
526
527
528
  	nilfs_mdt_mark_dirty(sufile);
  	brelse(bh);
  
   out_sem:
  	up_write(&NILFS_MDT(sufile)->mi_sem);
  	return ret;
  }
  
  /**
6c98cd4ec   Koji Sato   nilfs2: segment u...
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
   * nilfs_sufile_get_stat - get segment usage statistics
   * @sufile: inode of segment usage file
   * @stat: pointer to a structure of segment usage statistics
   *
   * Description: nilfs_sufile_get_stat() returns information about segment
   * usage.
   *
   * Return Value: On success, 0 is returned, and segment usage information is
   * stored in the place pointed by @stat. On error, one of the following
   * negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   */
  int nilfs_sufile_get_stat(struct inode *sufile, struct nilfs_sustat *sustat)
  {
  	struct buffer_head *header_bh;
  	struct nilfs_sufile_header *header;
0ef28f9ae   Ryusuke Konishi   nilfs2: get rid o...
548
  	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
6c98cd4ec   Koji Sato   nilfs2: segment u...
549
550
551
552
553
554
555
556
  	void *kaddr;
  	int ret;
  
  	down_read(&NILFS_MDT(sufile)->mi_sem);
  
  	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
  	if (ret < 0)
  		goto out_sem;
7b9c0976a   Cong Wang   nilfs2: remove th...
557
  	kaddr = kmap_atomic(header_bh->b_page);
7b16c8a21   Ryusuke Konishi   nilfs2: unfold ni...
558
  	header = kaddr + bh_offset(header_bh);
6c98cd4ec   Koji Sato   nilfs2: segment u...
559
560
561
  	sustat->ss_nsegs = nilfs_sufile_get_nsegments(sufile);
  	sustat->ss_ncleansegs = le64_to_cpu(header->sh_ncleansegs);
  	sustat->ss_ndirtysegs = le64_to_cpu(header->sh_ndirtysegs);
2c2e52fc4   Ryusuke Konishi   nilfs2: extend ni...
562
563
564
565
566
  	sustat->ss_ctime = nilfs->ns_ctime;
  	sustat->ss_nongc_ctime = nilfs->ns_nongc_ctime;
  	spin_lock(&nilfs->ns_last_segment_lock);
  	sustat->ss_prot_seq = nilfs->ns_prot_seq;
  	spin_unlock(&nilfs->ns_last_segment_lock);
7b9c0976a   Cong Wang   nilfs2: remove th...
567
  	kunmap_atomic(kaddr);
6c98cd4ec   Koji Sato   nilfs2: segment u...
568
569
570
571
572
573
  	brelse(header_bh);
  
   out_sem:
  	up_read(&NILFS_MDT(sufile)->mi_sem);
  	return ret;
  }
a703018f7   Ryusuke Konishi   nilfs2: segment u...
574
575
576
  void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
  			       struct buffer_head *header_bh,
  			       struct buffer_head *su_bh)
6c98cd4ec   Koji Sato   nilfs2: segment u...
577
  {
6c98cd4ec   Koji Sato   nilfs2: segment u...
578
  	struct nilfs_segment_usage *su;
6c98cd4ec   Koji Sato   nilfs2: segment u...
579
  	void *kaddr;
a703018f7   Ryusuke Konishi   nilfs2: segment u...
580
  	int suclean;
6c98cd4ec   Koji Sato   nilfs2: segment u...
581

7b9c0976a   Cong Wang   nilfs2: remove th...
582
  	kaddr = kmap_atomic(su_bh->b_page);
6c98cd4ec   Koji Sato   nilfs2: segment u...
583
584
  	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
  	if (nilfs_segment_usage_error(su)) {
7b9c0976a   Cong Wang   nilfs2: remove th...
585
  		kunmap_atomic(kaddr);
a703018f7   Ryusuke Konishi   nilfs2: segment u...
586
  		return;
6c98cd4ec   Koji Sato   nilfs2: segment u...
587
  	}
88072faf9   Ryusuke Konishi   nilfs2: fix wrong...
588
  	suclean = nilfs_segment_usage_clean(su);
6c98cd4ec   Koji Sato   nilfs2: segment u...
589
  	nilfs_segment_usage_set_error(su);
7b9c0976a   Cong Wang   nilfs2: remove th...
590
  	kunmap_atomic(kaddr);
6c98cd4ec   Koji Sato   nilfs2: segment u...
591

aa474a220   Ryusuke Konishi   nilfs2: add local...
592
  	if (suclean) {
a703018f7   Ryusuke Konishi   nilfs2: segment u...
593
  		nilfs_sufile_mod_counter(header_bh, -1, 0);
aa474a220   Ryusuke Konishi   nilfs2: add local...
594
595
  		NILFS_SUI(sufile)->ncleansegs--;
  	}
5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
596
  	mark_buffer_dirty(su_bh);
6c98cd4ec   Koji Sato   nilfs2: segment u...
597
  	nilfs_mdt_mark_dirty(sufile);
6c98cd4ec   Koji Sato   nilfs2: segment u...
598
599
600
  }
  
  /**
4d685f930   Ryusuke Konishi   nilfs2: align blo...
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
   * nilfs_sufile_truncate_range - truncate range of segment array
   * @sufile: inode of segment usage file
   * @start: start segment number (inclusive)
   * @end: end segment number (inclusive)
   *
   * Return Value: On success, 0 is returned.  On error, one of the
   * following negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   *
   * %-EINVAL - Invalid number of segments specified
   *
   * %-EBUSY - Dirty or active segments are present in the range
   */
78eb64c24   Ryusuke Konishi   nilfs2: add trunc...
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  static int nilfs_sufile_truncate_range(struct inode *sufile,
  				       __u64 start, __u64 end)
  {
  	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
  	struct buffer_head *header_bh;
  	struct buffer_head *su_bh;
  	struct nilfs_segment_usage *su, *su2;
  	size_t susz = NILFS_MDT(sufile)->mi_entry_size;
  	unsigned long segusages_per_block;
  	unsigned long nsegs, ncleaned;
  	__u64 segnum;
  	void *kaddr;
  	ssize_t n, nc;
  	int ret;
  	int j;
  
  	nsegs = nilfs_sufile_get_nsegments(sufile);
  
  	ret = -EINVAL;
  	if (start > end || start >= nsegs)
  		goto out;
  
  	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
  	if (ret < 0)
  		goto out;
  
  	segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
  	ncleaned = 0;
  
  	for (segnum = start; segnum <= end; segnum += n) {
  		n = min_t(unsigned long,
  			  segusages_per_block -
  				  nilfs_sufile_get_offset(sufile, segnum),
  			  end - segnum + 1);
  		ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
  							   &su_bh);
  		if (ret < 0) {
  			if (ret != -ENOENT)
  				goto out_header;
  			/* hole */
  			continue;
  		}
7b9c0976a   Cong Wang   nilfs2: remove th...
659
  		kaddr = kmap_atomic(su_bh->b_page);
78eb64c24   Ryusuke Konishi   nilfs2: add trunc...
660
661
662
663
664
  		su = nilfs_sufile_block_get_segment_usage(
  			sufile, segnum, su_bh, kaddr);
  		su2 = su;
  		for (j = 0; j < n; j++, su = (void *)su + susz) {
  			if ((le32_to_cpu(su->su_flags) &
4ce5c3426   Ryusuke Konishi   nilfs2: use BIT()...
665
  			     ~BIT(NILFS_SEGMENT_USAGE_ERROR)) ||
78eb64c24   Ryusuke Konishi   nilfs2: add trunc...
666
667
  			    nilfs_segment_is_active(nilfs, segnum + j)) {
  				ret = -EBUSY;
7b9c0976a   Cong Wang   nilfs2: remove th...
668
  				kunmap_atomic(kaddr);
78eb64c24   Ryusuke Konishi   nilfs2: add trunc...
669
670
671
672
673
674
675
676
677
678
679
  				brelse(su_bh);
  				goto out_header;
  			}
  		}
  		nc = 0;
  		for (su = su2, j = 0; j < n; j++, su = (void *)su + susz) {
  			if (nilfs_segment_usage_error(su)) {
  				nilfs_segment_usage_set_clean(su);
  				nc++;
  			}
  		}
7b9c0976a   Cong Wang   nilfs2: remove th...
680
  		kunmap_atomic(kaddr);
78eb64c24   Ryusuke Konishi   nilfs2: add trunc...
681
  		if (nc > 0) {
5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
682
  			mark_buffer_dirty(su_bh);
78eb64c24   Ryusuke Konishi   nilfs2: add trunc...
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
  			ncleaned += nc;
  		}
  		brelse(su_bh);
  
  		if (n == segusages_per_block) {
  			/* make hole */
  			nilfs_sufile_delete_segment_usage_block(sufile, segnum);
  		}
  	}
  	ret = 0;
  
  out_header:
  	if (ncleaned > 0) {
  		NILFS_SUI(sufile)->ncleansegs += ncleaned;
  		nilfs_sufile_mod_counter(header_bh, ncleaned, 0);
  		nilfs_mdt_mark_dirty(sufile);
  	}
  	brelse(header_bh);
  out:
  	return ret;
  }
  
  /**
4e33f9eab   Ryusuke Konishi   nilfs2: implement...
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
   * nilfs_sufile_resize - resize segment array
   * @sufile: inode of segment usage file
   * @newnsegs: new number of segments
   *
   * Return Value: On success, 0 is returned.  On error, one of the
   * following negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   *
   * %-ENOSPC - Enough free space is not left for shrinking
   *
   * %-EBUSY - Dirty or active segments exist in the region to be truncated
   */
  int nilfs_sufile_resize(struct inode *sufile, __u64 newnsegs)
  {
  	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
  	struct buffer_head *header_bh;
  	struct nilfs_sufile_header *header;
  	struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
  	void *kaddr;
  	unsigned long nsegs, nrsvsegs;
  	int ret = 0;
  
  	down_write(&NILFS_MDT(sufile)->mi_sem);
  
  	nsegs = nilfs_sufile_get_nsegments(sufile);
  	if (nsegs == newnsegs)
  		goto out;
  
  	ret = -ENOSPC;
  	nrsvsegs = nilfs_nrsvsegs(nilfs, newnsegs);
  	if (newnsegs < nsegs && nsegs - newnsegs + nrsvsegs > sui->ncleansegs)
  		goto out;
  
  	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
  	if (ret < 0)
  		goto out;
  
  	if (newnsegs > nsegs) {
  		sui->ncleansegs += newnsegs - nsegs;
  	} else /* newnsegs < nsegs */ {
  		ret = nilfs_sufile_truncate_range(sufile, newnsegs, nsegs - 1);
  		if (ret < 0)
  			goto out_header;
  
  		sui->ncleansegs -= nsegs - newnsegs;
  	}
7b9c0976a   Cong Wang   nilfs2: remove th...
755
  	kaddr = kmap_atomic(header_bh->b_page);
4e33f9eab   Ryusuke Konishi   nilfs2: implement...
756
757
  	header = kaddr + bh_offset(header_bh);
  	header->sh_ncleansegs = cpu_to_le64(sui->ncleansegs);
7b9c0976a   Cong Wang   nilfs2: remove th...
758
  	kunmap_atomic(kaddr);
4e33f9eab   Ryusuke Konishi   nilfs2: implement...
759

5fc7b1417   Ryusuke Konishi   nilfs2: use mark_...
760
  	mark_buffer_dirty(header_bh);
4e33f9eab   Ryusuke Konishi   nilfs2: implement...
761
762
763
764
765
766
767
768
769
770
771
  	nilfs_mdt_mark_dirty(sufile);
  	nilfs_set_nsegments(nilfs, newnsegs);
  
  out_header:
  	brelse(header_bh);
  out:
  	up_write(&NILFS_MDT(sufile)->mi_sem);
  	return ret;
  }
  
  /**
6c98cd4ec   Koji Sato   nilfs2: segment u...
772
773
774
   * nilfs_sufile_get_suinfo -
   * @sufile: inode of segment usage file
   * @segnum: segment number to start looking
003ff182f   Ryusuke Konishi   nilfs2: allow fut...
775
776
   * @buf: array of suinfo
   * @sisz: byte size of suinfo
6c98cd4ec   Koji Sato   nilfs2: segment u...
777
778
779
780
781
782
783
784
785
786
787
   * @nsi: size of suinfo array
   *
   * Description:
   *
   * Return Value: On success, 0 is returned and .... On error, one of the
   * following negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   */
003ff182f   Ryusuke Konishi   nilfs2: allow fut...
788
  ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, void *buf,
f19e78dee   Ryusuke Konishi   nilfs2: remove sp...
789
  				unsigned int sisz, size_t nsi)
6c98cd4ec   Koji Sato   nilfs2: segment u...
790
791
792
  {
  	struct buffer_head *su_bh;
  	struct nilfs_segment_usage *su;
003ff182f   Ryusuke Konishi   nilfs2: allow fut...
793
  	struct nilfs_suinfo *si = buf;
6c98cd4ec   Koji Sato   nilfs2: segment u...
794
  	size_t susz = NILFS_MDT(sufile)->mi_entry_size;
0ef28f9ae   Ryusuke Konishi   nilfs2: get rid o...
795
  	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
6c98cd4ec   Koji Sato   nilfs2: segment u...
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
  	void *kaddr;
  	unsigned long nsegs, segusages_per_block;
  	ssize_t n;
  	int ret, i, j;
  
  	down_read(&NILFS_MDT(sufile)->mi_sem);
  
  	segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
  	nsegs = min_t(unsigned long,
  		      nilfs_sufile_get_nsegments(sufile) - segnum,
  		      nsi);
  	for (i = 0; i < nsegs; i += n, segnum += n) {
  		n = min_t(unsigned long,
  			  segusages_per_block -
  				  nilfs_sufile_get_offset(sufile, segnum),
  			  nsegs - i);
  		ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
  							   &su_bh);
  		if (ret < 0) {
  			if (ret != -ENOENT)
  				goto out;
  			/* hole */
003ff182f   Ryusuke Konishi   nilfs2: allow fut...
818
819
  			memset(si, 0, sisz * n);
  			si = (void *)si + sisz * n;
6c98cd4ec   Koji Sato   nilfs2: segment u...
820
821
  			continue;
  		}
7b9c0976a   Cong Wang   nilfs2: remove th...
822
  		kaddr = kmap_atomic(su_bh->b_page);
6c98cd4ec   Koji Sato   nilfs2: segment u...
823
824
  		su = nilfs_sufile_block_get_segment_usage(
  			sufile, segnum, su_bh, kaddr);
003ff182f   Ryusuke Konishi   nilfs2: allow fut...
825
826
827
828
829
  		for (j = 0; j < n;
  		     j++, su = (void *)su + susz, si = (void *)si + sisz) {
  			si->sui_lastmod = le64_to_cpu(su->su_lastmod);
  			si->sui_nblocks = le32_to_cpu(su->su_nblocks);
  			si->sui_flags = le32_to_cpu(su->su_flags) &
4ce5c3426   Ryusuke Konishi   nilfs2: use BIT()...
830
  				~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
3efb55b49   Ryusuke Konishi   nilfs2: simplify ...
831
  			if (nilfs_segment_is_active(nilfs, segnum + j))
003ff182f   Ryusuke Konishi   nilfs2: allow fut...
832
  				si->sui_flags |=
4ce5c3426   Ryusuke Konishi   nilfs2: use BIT()...
833
  					BIT(NILFS_SEGMENT_USAGE_ACTIVE);
6c98cd4ec   Koji Sato   nilfs2: segment u...
834
  		}
7b9c0976a   Cong Wang   nilfs2: remove th...
835
  		kunmap_atomic(kaddr);
6c98cd4ec   Koji Sato   nilfs2: segment u...
836
837
838
839
840
841
842
843
  		brelse(su_bh);
  	}
  	ret = nsegs;
  
   out:
  	up_read(&NILFS_MDT(sufile)->mi_sem);
  	return ret;
  }
79739565e   Ryusuke Konishi   nilfs2: separate ...
844
845
  
  /**
00e9ffcd2   Andreas Rohner   nilfs2: add nilfs...
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
   * nilfs_sufile_set_suinfo - sets segment usage info
   * @sufile: inode of segment usage file
   * @buf: array of suinfo_update
   * @supsz: byte size of suinfo_update
   * @nsup: size of suinfo_update array
   *
   * Description: Takes an array of nilfs_suinfo_update structs and updates
   * segment usage accordingly. Only the fields indicated by the sup_flags
   * are updated.
   *
   * Return Value: On success, 0 is returned. On error, one of the
   * following negative error codes is returned.
   *
   * %-EIO - I/O error.
   *
   * %-ENOMEM - Insufficient amount of memory available.
   *
   * %-EINVAL - Invalid values in input (segment number, flags or nblocks)
   */
  ssize_t nilfs_sufile_set_suinfo(struct inode *sufile, void *buf,
f19e78dee   Ryusuke Konishi   nilfs2: remove sp...
866
  				unsigned int supsz, size_t nsup)
00e9ffcd2   Andreas Rohner   nilfs2: add nilfs...
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
  {
  	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
  	struct buffer_head *header_bh, *bh;
  	struct nilfs_suinfo_update *sup, *supend = buf + supsz * nsup;
  	struct nilfs_segment_usage *su;
  	void *kaddr;
  	unsigned long blkoff, prev_blkoff;
  	int cleansi, cleansu, dirtysi, dirtysu;
  	long ncleaned = 0, ndirtied = 0;
  	int ret = 0;
  
  	if (unlikely(nsup == 0))
  		return ret;
  
  	for (sup = buf; sup < supend; sup = (void *)sup + supsz) {
  		if (sup->sup_segnum >= nilfs->ns_nsegments
  			|| (sup->sup_flags &
  				(~0UL << __NR_NILFS_SUINFO_UPDATE_FIELDS))
  			|| (nilfs_suinfo_update_nblocks(sup) &&
  				sup->sup_sui.sui_nblocks >
  				nilfs->ns_blocks_per_segment))
  			return -EINVAL;
  	}
  
  	down_write(&NILFS_MDT(sufile)->mi_sem);
  
  	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
  	if (ret < 0)
  		goto out_sem;
  
  	sup = buf;
  	blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
  	ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
  	if (ret < 0)
  		goto out_header;
  
  	for (;;) {
  		kaddr = kmap_atomic(bh->b_page);
  		su = nilfs_sufile_block_get_segment_usage(
  			sufile, sup->sup_segnum, bh, kaddr);
  
  		if (nilfs_suinfo_update_lastmod(sup))
  			su->su_lastmod = cpu_to_le64(sup->sup_sui.sui_lastmod);
  
  		if (nilfs_suinfo_update_nblocks(sup))
  			su->su_nblocks = cpu_to_le32(sup->sup_sui.sui_nblocks);
  
  		if (nilfs_suinfo_update_flags(sup)) {
  			/*
  			 * Active flag is a virtual flag projected by running
  			 * nilfs kernel code - drop it not to write it to
  			 * disk.
  			 */
  			sup->sup_sui.sui_flags &=
4ce5c3426   Ryusuke Konishi   nilfs2: use BIT()...
921
  					~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
00e9ffcd2   Andreas Rohner   nilfs2: add nilfs...
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
  
  			cleansi = nilfs_suinfo_clean(&sup->sup_sui);
  			cleansu = nilfs_segment_usage_clean(su);
  			dirtysi = nilfs_suinfo_dirty(&sup->sup_sui);
  			dirtysu = nilfs_segment_usage_dirty(su);
  
  			if (cleansi && !cleansu)
  				++ncleaned;
  			else if (!cleansi && cleansu)
  				--ncleaned;
  
  			if (dirtysi && !dirtysu)
  				++ndirtied;
  			else if (!dirtysi && dirtysu)
  				--ndirtied;
  
  			su->su_flags = cpu_to_le32(sup->sup_sui.sui_flags);
  		}
  
  		kunmap_atomic(kaddr);
  
  		sup = (void *)sup + supsz;
  		if (sup >= supend)
  			break;
  
  		prev_blkoff = blkoff;
  		blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
  		if (blkoff == prev_blkoff)
  			continue;
  
  		/* get different block */
  		mark_buffer_dirty(bh);
  		put_bh(bh);
  		ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
  		if (unlikely(ret < 0))
  			goto out_mark;
  	}
  	mark_buffer_dirty(bh);
  	put_bh(bh);
  
   out_mark:
  	if (ncleaned || ndirtied) {
  		nilfs_sufile_mod_counter(header_bh, (u64)ncleaned,
  				(u64)ndirtied);
  		NILFS_SUI(sufile)->ncleansegs += ncleaned;
  	}
  	nilfs_mdt_mark_dirty(sufile);
   out_header:
  	put_bh(header_bh);
   out_sem:
  	up_write(&NILFS_MDT(sufile)->mi_sem);
  	return ret;
  }
  
  /**
82e11e857   Andreas Rohner   nilfs2: add nilfs...
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
   * nilfs_sufile_trim_fs() - trim ioctl handle function
   * @sufile: inode of segment usage file
   * @range: fstrim_range structure
   *
   * start:	First Byte to trim
   * len:		number of Bytes to trim from start
   * minlen:	minimum extent length in Bytes
   *
   * Decription: nilfs_sufile_trim_fs goes through all segments containing bytes
   * from start to start+len. start is rounded up to the next block boundary
   * and start+len is rounded down. For each clean segment blkdev_issue_discard
   * function is invoked.
   *
   * Return Value: On success, 0 is returned or negative error code, otherwise.
   */
  int nilfs_sufile_trim_fs(struct inode *sufile, struct fstrim_range *range)
  {
  	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
  	struct buffer_head *su_bh;
  	struct nilfs_segment_usage *su;
  	void *kaddr;
  	size_t n, i, susz = NILFS_MDT(sufile)->mi_entry_size;
  	sector_t seg_start, seg_end, start_block, end_block;
  	sector_t start = 0, nblocks = 0;
  	u64 segnum, segnum_end, minlen, len, max_blocks, ndiscarded = 0;
  	int ret = 0;
  	unsigned int sects_per_block;
  
  	sects_per_block = (1 << nilfs->ns_blocksize_bits) /
  			bdev_logical_block_size(nilfs->ns_bdev);
  	len = range->len >> nilfs->ns_blocksize_bits;
  	minlen = range->minlen >> nilfs->ns_blocksize_bits;
  	max_blocks = ((u64)nilfs->ns_nsegments * nilfs->ns_blocks_per_segment);
  
  	if (!len || range->start >= max_blocks << nilfs->ns_blocksize_bits)
  		return -EINVAL;
  
  	start_block = (range->start + nilfs->ns_blocksize - 1) >>
  			nilfs->ns_blocksize_bits;
  
  	/*
  	 * range->len can be very large (actually, it is set to
  	 * ULLONG_MAX by default) - truncate upper end of the range
  	 * carefully so as not to overflow.
  	 */
  	if (max_blocks - start_block < len)
  		end_block = max_blocks - 1;
  	else
  		end_block = start_block + len - 1;
  
  	segnum = nilfs_get_segnum_of_block(nilfs, start_block);
  	segnum_end = nilfs_get_segnum_of_block(nilfs, end_block);
  
  	down_read(&NILFS_MDT(sufile)->mi_sem);
  
  	while (segnum <= segnum_end) {
  		n = nilfs_sufile_segment_usages_in_block(sufile, segnum,
  				segnum_end);
  
  		ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
  							   &su_bh);
  		if (ret < 0) {
  			if (ret != -ENOENT)
  				goto out_sem;
  			/* hole */
  			segnum += n;
  			continue;
  		}
  
  		kaddr = kmap_atomic(su_bh->b_page);
  		su = nilfs_sufile_block_get_segment_usage(sufile, segnum,
  				su_bh, kaddr);
  		for (i = 0; i < n; ++i, ++segnum, su = (void *)su + susz) {
  			if (!nilfs_segment_usage_clean(su))
  				continue;
  
  			nilfs_get_segment_range(nilfs, segnum, &seg_start,
  						&seg_end);
  
  			if (!nblocks) {
  				/* start new extent */
  				start = seg_start;
  				nblocks = seg_end - seg_start + 1;
  				continue;
  			}
  
  			if (start + nblocks == seg_start) {
  				/* add to previous extent */
  				nblocks += seg_end - seg_start + 1;
  				continue;
  			}
  
  			/* discard previous extent */
  			if (start < start_block) {
  				nblocks -= start_block - start;
  				start = start_block;
  			}
  
  			if (nblocks >= minlen) {
  				kunmap_atomic(kaddr);
  
  				ret = blkdev_issue_discard(nilfs->ns_bdev,
  						start * sects_per_block,
  						nblocks * sects_per_block,
  						GFP_NOFS, 0);
  				if (ret < 0) {
  					put_bh(su_bh);
  					goto out_sem;
  				}
  
  				ndiscarded += nblocks;
  				kaddr = kmap_atomic(su_bh->b_page);
  				su = nilfs_sufile_block_get_segment_usage(
  					sufile, segnum, su_bh, kaddr);
  			}
  
  			/* start new extent */
  			start = seg_start;
  			nblocks = seg_end - seg_start + 1;
  		}
  		kunmap_atomic(kaddr);
  		put_bh(su_bh);
  	}
  
  
  	if (nblocks) {
  		/* discard last extent */
  		if (start < start_block) {
  			nblocks -= start_block - start;
  			start = start_block;
  		}
  		if (start + nblocks > end_block + 1)
  			nblocks = end_block - start + 1;
  
  		if (nblocks >= minlen) {
  			ret = blkdev_issue_discard(nilfs->ns_bdev,
  					start * sects_per_block,
  					nblocks * sects_per_block,
  					GFP_NOFS, 0);
  			if (!ret)
  				ndiscarded += nblocks;
  		}
  	}
  
  out_sem:
  	up_read(&NILFS_MDT(sufile)->mi_sem);
  
  	range->len = ndiscarded << nilfs->ns_blocksize_bits;
  	return ret;
  }
  
  /**
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1129
1130
1131
   * nilfs_sufile_read - read or get sufile inode
   * @sb: super block instance
   * @susize: size of a segment usage entry
8707df384   Ryusuke Konishi   nilfs2: separate ...
1132
   * @raw_inode: on-disk sufile inode
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1133
   * @inodep: buffer to store the inode
8707df384   Ryusuke Konishi   nilfs2: separate ...
1134
   */
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1135
1136
  int nilfs_sufile_read(struct super_block *sb, size_t susize,
  		      struct nilfs_inode *raw_inode, struct inode **inodep)
8707df384   Ryusuke Konishi   nilfs2: separate ...
1137
  {
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1138
1139
  	struct inode *sufile;
  	struct nilfs_sufile_info *sui;
aa474a220   Ryusuke Konishi   nilfs2: add local...
1140
1141
1142
  	struct buffer_head *header_bh;
  	struct nilfs_sufile_header *header;
  	void *kaddr;
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1143
  	int err;
aa474a220   Ryusuke Konishi   nilfs2: add local...
1144

0ec060d18   Ryusuke Konishi   nilfs2: verify me...
1145
  	if (susize > sb->s_blocksize) {
feee880fa   Ryusuke Konishi   nilfs2: reduce ba...
1146
1147
  		nilfs_msg(sb, KERN_ERR,
  			  "too large segment usage size: %zu bytes", susize);
0ec060d18   Ryusuke Konishi   nilfs2: verify me...
1148
1149
  		return -EINVAL;
  	} else if (susize < NILFS_MIN_SEGMENT_USAGE_SIZE) {
feee880fa   Ryusuke Konishi   nilfs2: reduce ba...
1150
1151
  		nilfs_msg(sb, KERN_ERR,
  			  "too small segment usage size: %zu bytes", susize);
0ec060d18   Ryusuke Konishi   nilfs2: verify me...
1152
1153
  		return -EINVAL;
  	}
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1154
1155
1156
1157
1158
  	sufile = nilfs_iget_locked(sb, NULL, NILFS_SUFILE_INO);
  	if (unlikely(!sufile))
  		return -ENOMEM;
  	if (!(sufile->i_state & I_NEW))
  		goto out;
aa474a220   Ryusuke Konishi   nilfs2: add local...
1159

f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1160
1161
1162
  	err = nilfs_mdt_init(sufile, NILFS_MDT_GFP, sizeof(*sui));
  	if (err)
  		goto failed;
8707df384   Ryusuke Konishi   nilfs2: separate ...
1163

f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
  	nilfs_mdt_set_entry_size(sufile, susize,
  				 sizeof(struct nilfs_sufile_header));
  
  	err = nilfs_read_inode_common(sufile, raw_inode);
  	if (err)
  		goto failed;
  
  	err = nilfs_sufile_get_header_block(sufile, &header_bh);
  	if (err)
  		goto failed;
79739565e   Ryusuke Konishi   nilfs2: separate ...
1174

f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1175
  	sui = NILFS_SUI(sufile);
7b9c0976a   Cong Wang   nilfs2: remove th...
1176
  	kaddr = kmap_atomic(header_bh->b_page);
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1177
1178
  	header = kaddr + bh_offset(header_bh);
  	sui->ncleansegs = le64_to_cpu(header->sh_ncleansegs);
7b9c0976a   Cong Wang   nilfs2: remove th...
1179
  	kunmap_atomic(kaddr);
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1180
  	brelse(header_bh);
619205da5   Ryusuke Konishi   nilfs2: add ioctl...
1181
1182
  	sui->allocmax = nilfs_sufile_get_nsegments(sufile) - 1;
  	sui->allocmin = 0;
f1e89c86f   Ryusuke Konishi   nilfs2: use iget ...
1183
1184
1185
1186
1187
1188
1189
  	unlock_new_inode(sufile);
   out:
  	*inodep = sufile;
  	return 0;
   failed:
  	iget_failed(sufile);
  	return err;
79739565e   Ryusuke Konishi   nilfs2: separate ...
1190
  }