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