diff options
Diffstat (limited to 'fs/nilfs2/sufile.c')
-rw-r--r-- | fs/nilfs2/sufile.c | 640 |
1 files changed, 640 insertions, 0 deletions
diff --git a/fs/nilfs2/sufile.c b/fs/nilfs2/sufile.c new file mode 100644 index 000000000000..c774cf397e2f --- /dev/null +++ b/fs/nilfs2/sufile.c | |||
@@ -0,0 +1,640 @@ | |||
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 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | * | ||
20 | * Written by Koji Sato <koji@osrg.net>. | ||
21 | */ | ||
22 | |||
23 | #include <linux/kernel.h> | ||
24 | #include <linux/fs.h> | ||
25 | #include <linux/string.h> | ||
26 | #include <linux/buffer_head.h> | ||
27 | #include <linux/errno.h> | ||
28 | #include <linux/nilfs2_fs.h> | ||
29 | #include "mdt.h" | ||
30 | #include "sufile.h" | ||
31 | |||
32 | |||
33 | static inline unsigned long | ||
34 | nilfs_sufile_segment_usages_per_block(const struct inode *sufile) | ||
35 | { | ||
36 | return NILFS_MDT(sufile)->mi_entries_per_block; | ||
37 | } | ||
38 | |||
39 | static unsigned long | ||
40 | nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum) | ||
41 | { | ||
42 | __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset; | ||
43 | do_div(t, nilfs_sufile_segment_usages_per_block(sufile)); | ||
44 | return (unsigned long)t; | ||
45 | } | ||
46 | |||
47 | static unsigned long | ||
48 | nilfs_sufile_get_offset(const struct inode *sufile, __u64 segnum) | ||
49 | { | ||
50 | __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset; | ||
51 | return do_div(t, nilfs_sufile_segment_usages_per_block(sufile)); | ||
52 | } | ||
53 | |||
54 | static unsigned long | ||
55 | nilfs_sufile_segment_usages_in_block(const struct inode *sufile, __u64 curr, | ||
56 | __u64 max) | ||
57 | { | ||
58 | return min_t(unsigned long, | ||
59 | nilfs_sufile_segment_usages_per_block(sufile) - | ||
60 | nilfs_sufile_get_offset(sufile, curr), | ||
61 | max - curr + 1); | ||
62 | } | ||
63 | |||
64 | static inline struct nilfs_sufile_header * | ||
65 | nilfs_sufile_block_get_header(const struct inode *sufile, | ||
66 | struct buffer_head *bh, | ||
67 | void *kaddr) | ||
68 | { | ||
69 | return kaddr + bh_offset(bh); | ||
70 | } | ||
71 | |||
72 | static struct nilfs_segment_usage * | ||
73 | nilfs_sufile_block_get_segment_usage(const struct inode *sufile, __u64 segnum, | ||
74 | struct buffer_head *bh, void *kaddr) | ||
75 | { | ||
76 | return kaddr + bh_offset(bh) + | ||
77 | nilfs_sufile_get_offset(sufile, segnum) * | ||
78 | NILFS_MDT(sufile)->mi_entry_size; | ||
79 | } | ||
80 | |||
81 | static inline int nilfs_sufile_get_header_block(struct inode *sufile, | ||
82 | struct buffer_head **bhp) | ||
83 | { | ||
84 | return nilfs_mdt_get_block(sufile, 0, 0, NULL, bhp); | ||
85 | } | ||
86 | |||
87 | static inline int | ||
88 | nilfs_sufile_get_segment_usage_block(struct inode *sufile, __u64 segnum, | ||
89 | int create, struct buffer_head **bhp) | ||
90 | { | ||
91 | return nilfs_mdt_get_block(sufile, | ||
92 | nilfs_sufile_get_blkoff(sufile, segnum), | ||
93 | create, NULL, bhp); | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * nilfs_sufile_alloc - allocate a segment | ||
98 | * @sufile: inode of segment usage file | ||
99 | * @segnump: pointer to segment number | ||
100 | * | ||
101 | * Description: nilfs_sufile_alloc() allocates a clean segment. | ||
102 | * | ||
103 | * Return Value: On success, 0 is returned and the segment number of the | ||
104 | * allocated segment is stored in the place pointed by @segnump. On error, one | ||
105 | * of the following negative error codes is returned. | ||
106 | * | ||
107 | * %-EIO - I/O error. | ||
108 | * | ||
109 | * %-ENOMEM - Insufficient amount of memory available. | ||
110 | * | ||
111 | * %-ENOSPC - No clean segment left. | ||
112 | */ | ||
113 | int nilfs_sufile_alloc(struct inode *sufile, __u64 *segnump) | ||
114 | { | ||
115 | struct buffer_head *header_bh, *su_bh; | ||
116 | struct the_nilfs *nilfs; | ||
117 | struct nilfs_sufile_header *header; | ||
118 | struct nilfs_segment_usage *su; | ||
119 | size_t susz = NILFS_MDT(sufile)->mi_entry_size; | ||
120 | __u64 segnum, maxsegnum, last_alloc; | ||
121 | void *kaddr; | ||
122 | unsigned long nsegments, ncleansegs, nsus; | ||
123 | int ret, i, j; | ||
124 | |||
125 | down_write(&NILFS_MDT(sufile)->mi_sem); | ||
126 | |||
127 | nilfs = NILFS_MDT(sufile)->mi_nilfs; | ||
128 | |||
129 | ret = nilfs_sufile_get_header_block(sufile, &header_bh); | ||
130 | if (ret < 0) | ||
131 | goto out_sem; | ||
132 | kaddr = kmap_atomic(header_bh->b_page, KM_USER0); | ||
133 | header = nilfs_sufile_block_get_header(sufile, header_bh, kaddr); | ||
134 | ncleansegs = le64_to_cpu(header->sh_ncleansegs); | ||
135 | last_alloc = le64_to_cpu(header->sh_last_alloc); | ||
136 | kunmap_atomic(kaddr, KM_USER0); | ||
137 | |||
138 | nsegments = nilfs_sufile_get_nsegments(sufile); | ||
139 | segnum = last_alloc + 1; | ||
140 | maxsegnum = nsegments - 1; | ||
141 | for (i = 0; i < nsegments; i += nsus) { | ||
142 | if (segnum >= nsegments) { | ||
143 | /* wrap around */ | ||
144 | segnum = 0; | ||
145 | maxsegnum = last_alloc; | ||
146 | } | ||
147 | ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1, | ||
148 | &su_bh); | ||
149 | if (ret < 0) | ||
150 | goto out_header; | ||
151 | kaddr = kmap_atomic(su_bh->b_page, KM_USER0); | ||
152 | su = nilfs_sufile_block_get_segment_usage( | ||
153 | sufile, segnum, su_bh, kaddr); | ||
154 | |||
155 | nsus = nilfs_sufile_segment_usages_in_block( | ||
156 | sufile, segnum, maxsegnum); | ||
157 | for (j = 0; j < nsus; j++, su = (void *)su + susz, segnum++) { | ||
158 | if (!nilfs_segment_usage_clean(su)) | ||
159 | continue; | ||
160 | /* found a clean segment */ | ||
161 | nilfs_segment_usage_set_dirty(su); | ||
162 | kunmap_atomic(kaddr, KM_USER0); | ||
163 | |||
164 | kaddr = kmap_atomic(header_bh->b_page, KM_USER0); | ||
165 | header = nilfs_sufile_block_get_header( | ||
166 | sufile, header_bh, kaddr); | ||
167 | le64_add_cpu(&header->sh_ncleansegs, -1); | ||
168 | le64_add_cpu(&header->sh_ndirtysegs, 1); | ||
169 | header->sh_last_alloc = cpu_to_le64(segnum); | ||
170 | kunmap_atomic(kaddr, KM_USER0); | ||
171 | |||
172 | nilfs_mdt_mark_buffer_dirty(header_bh); | ||
173 | nilfs_mdt_mark_buffer_dirty(su_bh); | ||
174 | nilfs_mdt_mark_dirty(sufile); | ||
175 | brelse(su_bh); | ||
176 | *segnump = segnum; | ||
177 | goto out_header; | ||
178 | } | ||
179 | |||
180 | kunmap_atomic(kaddr, KM_USER0); | ||
181 | brelse(su_bh); | ||
182 | } | ||
183 | |||
184 | /* no segments left */ | ||
185 | ret = -ENOSPC; | ||
186 | |||
187 | out_header: | ||
188 | brelse(header_bh); | ||
189 | |||
190 | out_sem: | ||
191 | up_write(&NILFS_MDT(sufile)->mi_sem); | ||
192 | return ret; | ||
193 | } | ||
194 | |||
195 | /** | ||
196 | * nilfs_sufile_cancel_free - | ||
197 | * @sufile: inode of segment usage file | ||
198 | * @segnum: segment number | ||
199 | * | ||
200 | * Description: | ||
201 | * | ||
202 | * Return Value: On success, 0 is returned. On error, one of the following | ||
203 | * negative error codes is returned. | ||
204 | * | ||
205 | * %-EIO - I/O error. | ||
206 | * | ||
207 | * %-ENOMEM - Insufficient amount of memory available. | ||
208 | */ | ||
209 | int nilfs_sufile_cancel_free(struct inode *sufile, __u64 segnum) | ||
210 | { | ||
211 | struct buffer_head *header_bh, *su_bh; | ||
212 | struct the_nilfs *nilfs; | ||
213 | struct nilfs_sufile_header *header; | ||
214 | struct nilfs_segment_usage *su; | ||
215 | void *kaddr; | ||
216 | int ret; | ||
217 | |||
218 | down_write(&NILFS_MDT(sufile)->mi_sem); | ||
219 | |||
220 | nilfs = NILFS_MDT(sufile)->mi_nilfs; | ||
221 | |||
222 | ret = nilfs_sufile_get_header_block(sufile, &header_bh); | ||
223 | if (ret < 0) | ||
224 | goto out_sem; | ||
225 | |||
226 | ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &su_bh); | ||
227 | if (ret < 0) | ||
228 | goto out_header; | ||
229 | |||
230 | kaddr = kmap_atomic(su_bh->b_page, KM_USER0); | ||
231 | su = nilfs_sufile_block_get_segment_usage( | ||
232 | sufile, segnum, su_bh, kaddr); | ||
233 | if (unlikely(!nilfs_segment_usage_clean(su))) { | ||
234 | printk(KERN_WARNING "%s: segment %llu must be clean\n", | ||
235 | __func__, (unsigned long long)segnum); | ||
236 | kunmap_atomic(kaddr, KM_USER0); | ||
237 | goto out_su_bh; | ||
238 | } | ||
239 | nilfs_segment_usage_set_dirty(su); | ||
240 | kunmap_atomic(kaddr, KM_USER0); | ||
241 | |||
242 | kaddr = kmap_atomic(header_bh->b_page, KM_USER0); | ||
243 | header = nilfs_sufile_block_get_header(sufile, header_bh, kaddr); | ||
244 | le64_add_cpu(&header->sh_ncleansegs, -1); | ||
245 | le64_add_cpu(&header->sh_ndirtysegs, 1); | ||
246 | kunmap_atomic(kaddr, KM_USER0); | ||
247 | |||
248 | nilfs_mdt_mark_buffer_dirty(header_bh); | ||
249 | nilfs_mdt_mark_buffer_dirty(su_bh); | ||
250 | nilfs_mdt_mark_dirty(sufile); | ||
251 | |||
252 | out_su_bh: | ||
253 | brelse(su_bh); | ||
254 | out_header: | ||
255 | brelse(header_bh); | ||
256 | out_sem: | ||
257 | up_write(&NILFS_MDT(sufile)->mi_sem); | ||
258 | return ret; | ||
259 | } | ||
260 | |||
261 | /** | ||
262 | * nilfs_sufile_freev - free segments | ||
263 | * @sufile: inode of segment usage file | ||
264 | * @segnum: array of segment numbers | ||
265 | * @nsegs: number of segments | ||
266 | * | ||
267 | * Description: nilfs_sufile_freev() frees segments specified by @segnum and | ||
268 | * @nsegs, which must have been returned by a previous call to | ||
269 | * nilfs_sufile_alloc(). | ||
270 | * | ||
271 | * Return Value: On success, 0 is returned. On error, one of the following | ||
272 | * negative error codes is returned. | ||
273 | * | ||
274 | * %-EIO - I/O error. | ||
275 | * | ||
276 | * %-ENOMEM - Insufficient amount of memory available. | ||
277 | */ | ||
278 | #define NILFS_SUFILE_FREEV_PREALLOC 16 | ||
279 | int nilfs_sufile_freev(struct inode *sufile, __u64 *segnum, size_t nsegs) | ||
280 | { | ||
281 | struct buffer_head *header_bh, **su_bh, | ||
282 | *su_bh_prealloc[NILFS_SUFILE_FREEV_PREALLOC]; | ||
283 | struct the_nilfs *nilfs; | ||
284 | struct nilfs_sufile_header *header; | ||
285 | struct nilfs_segment_usage *su; | ||
286 | void *kaddr; | ||
287 | int ret, i; | ||
288 | |||
289 | down_write(&NILFS_MDT(sufile)->mi_sem); | ||
290 | |||
291 | nilfs = NILFS_MDT(sufile)->mi_nilfs; | ||
292 | |||
293 | /* prepare resources */ | ||
294 | if (nsegs <= NILFS_SUFILE_FREEV_PREALLOC) | ||
295 | su_bh = su_bh_prealloc; | ||
296 | else { | ||
297 | su_bh = kmalloc(sizeof(*su_bh) * nsegs, GFP_NOFS); | ||
298 | if (su_bh == NULL) { | ||
299 | ret = -ENOMEM; | ||
300 | goto out_sem; | ||
301 | } | ||
302 | } | ||
303 | |||
304 | ret = nilfs_sufile_get_header_block(sufile, &header_bh); | ||
305 | if (ret < 0) | ||
306 | goto out_su_bh; | ||
307 | for (i = 0; i < nsegs; i++) { | ||
308 | ret = nilfs_sufile_get_segment_usage_block(sufile, segnum[i], | ||
309 | 0, &su_bh[i]); | ||
310 | if (ret < 0) | ||
311 | goto out_bh; | ||
312 | } | ||
313 | |||
314 | /* free segments */ | ||
315 | for (i = 0; i < nsegs; i++) { | ||
316 | kaddr = kmap_atomic(su_bh[i]->b_page, KM_USER0); | ||
317 | su = nilfs_sufile_block_get_segment_usage( | ||
318 | sufile, segnum[i], su_bh[i], kaddr); | ||
319 | WARN_ON(nilfs_segment_usage_error(su)); | ||
320 | nilfs_segment_usage_set_clean(su); | ||
321 | kunmap_atomic(kaddr, KM_USER0); | ||
322 | nilfs_mdt_mark_buffer_dirty(su_bh[i]); | ||
323 | } | ||
324 | kaddr = kmap_atomic(header_bh->b_page, KM_USER0); | ||
325 | header = nilfs_sufile_block_get_header(sufile, header_bh, kaddr); | ||
326 | le64_add_cpu(&header->sh_ncleansegs, nsegs); | ||
327 | le64_add_cpu(&header->sh_ndirtysegs, -(u64)nsegs); | ||
328 | kunmap_atomic(kaddr, KM_USER0); | ||
329 | nilfs_mdt_mark_buffer_dirty(header_bh); | ||
330 | nilfs_mdt_mark_dirty(sufile); | ||
331 | |||
332 | out_bh: | ||
333 | for (i--; i >= 0; i--) | ||
334 | brelse(su_bh[i]); | ||
335 | brelse(header_bh); | ||
336 | |||
337 | out_su_bh: | ||
338 | if (su_bh != su_bh_prealloc) | ||
339 | kfree(su_bh); | ||
340 | |||
341 | out_sem: | ||
342 | up_write(&NILFS_MDT(sufile)->mi_sem); | ||
343 | return ret; | ||
344 | } | ||
345 | |||
346 | /** | ||
347 | * nilfs_sufile_free - | ||
348 | * @sufile: | ||
349 | * @segnum: | ||
350 | */ | ||
351 | int nilfs_sufile_free(struct inode *sufile, __u64 segnum) | ||
352 | { | ||
353 | return nilfs_sufile_freev(sufile, &segnum, 1); | ||
354 | } | ||
355 | |||
356 | /** | ||
357 | * nilfs_sufile_get_segment_usage - get a segment usage | ||
358 | * @sufile: inode of segment usage file | ||
359 | * @segnum: segment number | ||
360 | * @sup: pointer to segment usage | ||
361 | * @bhp: pointer to buffer head | ||
362 | * | ||
363 | * Description: nilfs_sufile_get_segment_usage() acquires the segment usage | ||
364 | * specified by @segnum. | ||
365 | * | ||
366 | * Return Value: On success, 0 is returned, and the segment usage and the | ||
367 | * buffer head of the buffer on which the segment usage is located are stored | ||
368 | * in the place pointed by @sup and @bhp, respectively. On error, one of the | ||
369 | * following negative error codes is returned. | ||
370 | * | ||
371 | * %-EIO - I/O error. | ||
372 | * | ||
373 | * %-ENOMEM - Insufficient amount of memory available. | ||
374 | * | ||
375 | * %-EINVAL - Invalid segment usage number. | ||
376 | */ | ||
377 | int nilfs_sufile_get_segment_usage(struct inode *sufile, __u64 segnum, | ||
378 | struct nilfs_segment_usage **sup, | ||
379 | struct buffer_head **bhp) | ||
380 | { | ||
381 | struct buffer_head *bh; | ||
382 | struct nilfs_segment_usage *su; | ||
383 | void *kaddr; | ||
384 | int ret; | ||
385 | |||
386 | /* segnum is 0 origin */ | ||
387 | if (segnum >= nilfs_sufile_get_nsegments(sufile)) | ||
388 | return -EINVAL; | ||
389 | down_write(&NILFS_MDT(sufile)->mi_sem); | ||
390 | ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1, &bh); | ||
391 | if (ret < 0) | ||
392 | goto out_sem; | ||
393 | kaddr = kmap(bh->b_page); | ||
394 | su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr); | ||
395 | if (nilfs_segment_usage_error(su)) { | ||
396 | kunmap(bh->b_page); | ||
397 | brelse(bh); | ||
398 | ret = -EINVAL; | ||
399 | goto out_sem; | ||
400 | } | ||
401 | |||
402 | if (sup != NULL) | ||
403 | *sup = su; | ||
404 | *bhp = bh; | ||
405 | |||
406 | out_sem: | ||
407 | up_write(&NILFS_MDT(sufile)->mi_sem); | ||
408 | return ret; | ||
409 | } | ||
410 | |||
411 | /** | ||
412 | * nilfs_sufile_put_segment_usage - put a segment usage | ||
413 | * @sufile: inode of segment usage file | ||
414 | * @segnum: segment number | ||
415 | * @bh: buffer head | ||
416 | * | ||
417 | * Description: nilfs_sufile_put_segment_usage() releases the segment usage | ||
418 | * specified by @segnum. @bh must be the buffer head which have been returned | ||
419 | * by a previous call to nilfs_sufile_get_segment_usage() with @segnum. | ||
420 | */ | ||
421 | void nilfs_sufile_put_segment_usage(struct inode *sufile, __u64 segnum, | ||
422 | struct buffer_head *bh) | ||
423 | { | ||
424 | kunmap(bh->b_page); | ||
425 | brelse(bh); | ||
426 | } | ||
427 | |||
428 | /** | ||
429 | * nilfs_sufile_get_stat - get segment usage statistics | ||
430 | * @sufile: inode of segment usage file | ||
431 | * @stat: pointer to a structure of segment usage statistics | ||
432 | * | ||
433 | * Description: nilfs_sufile_get_stat() returns information about segment | ||
434 | * usage. | ||
435 | * | ||
436 | * Return Value: On success, 0 is returned, and segment usage information is | ||
437 | * stored in the place pointed by @stat. On error, one of the following | ||
438 | * negative error codes is returned. | ||
439 | * | ||
440 | * %-EIO - I/O error. | ||
441 | * | ||
442 | * %-ENOMEM - Insufficient amount of memory available. | ||
443 | */ | ||
444 | int nilfs_sufile_get_stat(struct inode *sufile, struct nilfs_sustat *sustat) | ||
445 | { | ||
446 | struct buffer_head *header_bh; | ||
447 | struct nilfs_sufile_header *header; | ||
448 | struct the_nilfs *nilfs = NILFS_MDT(sufile)->mi_nilfs; | ||
449 | void *kaddr; | ||
450 | int ret; | ||
451 | |||
452 | down_read(&NILFS_MDT(sufile)->mi_sem); | ||
453 | |||
454 | ret = nilfs_sufile_get_header_block(sufile, &header_bh); | ||
455 | if (ret < 0) | ||
456 | goto out_sem; | ||
457 | |||
458 | kaddr = kmap_atomic(header_bh->b_page, KM_USER0); | ||
459 | header = nilfs_sufile_block_get_header(sufile, header_bh, kaddr); | ||
460 | sustat->ss_nsegs = nilfs_sufile_get_nsegments(sufile); | ||
461 | sustat->ss_ncleansegs = le64_to_cpu(header->sh_ncleansegs); | ||
462 | sustat->ss_ndirtysegs = le64_to_cpu(header->sh_ndirtysegs); | ||
463 | sustat->ss_ctime = nilfs->ns_ctime; | ||
464 | sustat->ss_nongc_ctime = nilfs->ns_nongc_ctime; | ||
465 | spin_lock(&nilfs->ns_last_segment_lock); | ||
466 | sustat->ss_prot_seq = nilfs->ns_prot_seq; | ||
467 | spin_unlock(&nilfs->ns_last_segment_lock); | ||
468 | kunmap_atomic(kaddr, KM_USER0); | ||
469 | brelse(header_bh); | ||
470 | |||
471 | out_sem: | ||
472 | up_read(&NILFS_MDT(sufile)->mi_sem); | ||
473 | return ret; | ||
474 | } | ||
475 | |||
476 | /** | ||
477 | * nilfs_sufile_get_ncleansegs - get the number of clean segments | ||
478 | * @sufile: inode of segment usage file | ||
479 | * @nsegsp: pointer to the number of clean segments | ||
480 | * | ||
481 | * Description: nilfs_sufile_get_ncleansegs() acquires the number of clean | ||
482 | * segments. | ||
483 | * | ||
484 | * Return Value: On success, 0 is returned and the number of clean segments is | ||
485 | * stored in the place pointed by @nsegsp. On error, one of the following | ||
486 | * negative error codes is returned. | ||
487 | * | ||
488 | * %-EIO - I/O error. | ||
489 | * | ||
490 | * %-ENOMEM - Insufficient amount of memory available. | ||
491 | */ | ||
492 | int nilfs_sufile_get_ncleansegs(struct inode *sufile, unsigned long *nsegsp) | ||
493 | { | ||
494 | struct nilfs_sustat sustat; | ||
495 | int ret; | ||
496 | |||
497 | ret = nilfs_sufile_get_stat(sufile, &sustat); | ||
498 | if (ret == 0) | ||
499 | *nsegsp = sustat.ss_ncleansegs; | ||
500 | return ret; | ||
501 | } | ||
502 | |||
503 | /** | ||
504 | * nilfs_sufile_set_error - mark a segment as erroneous | ||
505 | * @sufile: inode of segment usage file | ||
506 | * @segnum: segment number | ||
507 | * | ||
508 | * Description: nilfs_sufile_set_error() marks the segment specified by | ||
509 | * @segnum as erroneous. The error segment will never be used again. | ||
510 | * | ||
511 | * Return Value: On success, 0 is returned. On error, one of the following | ||
512 | * negative error codes is returned. | ||
513 | * | ||
514 | * %-EIO - I/O error. | ||
515 | * | ||
516 | * %-ENOMEM - Insufficient amount of memory available. | ||
517 | * | ||
518 | * %-EINVAL - Invalid segment usage number. | ||
519 | */ | ||
520 | int nilfs_sufile_set_error(struct inode *sufile, __u64 segnum) | ||
521 | { | ||
522 | struct buffer_head *header_bh, *su_bh; | ||
523 | struct nilfs_segment_usage *su; | ||
524 | struct nilfs_sufile_header *header; | ||
525 | void *kaddr; | ||
526 | int ret; | ||
527 | |||
528 | if (unlikely(segnum >= nilfs_sufile_get_nsegments(sufile))) { | ||
529 | printk(KERN_WARNING "%s: invalid segment number: %llu\n", | ||
530 | __func__, (unsigned long long)segnum); | ||
531 | return -EINVAL; | ||
532 | } | ||
533 | down_write(&NILFS_MDT(sufile)->mi_sem); | ||
534 | |||
535 | ret = nilfs_sufile_get_header_block(sufile, &header_bh); | ||
536 | if (ret < 0) | ||
537 | goto out_sem; | ||
538 | ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &su_bh); | ||
539 | if (ret < 0) | ||
540 | goto out_header; | ||
541 | |||
542 | kaddr = kmap_atomic(su_bh->b_page, KM_USER0); | ||
543 | su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr); | ||
544 | if (nilfs_segment_usage_error(su)) { | ||
545 | kunmap_atomic(kaddr, KM_USER0); | ||
546 | brelse(su_bh); | ||
547 | goto out_header; | ||
548 | } | ||
549 | |||
550 | nilfs_segment_usage_set_error(su); | ||
551 | kunmap_atomic(kaddr, KM_USER0); | ||
552 | brelse(su_bh); | ||
553 | |||
554 | kaddr = kmap_atomic(header_bh->b_page, KM_USER0); | ||
555 | header = nilfs_sufile_block_get_header(sufile, header_bh, kaddr); | ||
556 | le64_add_cpu(&header->sh_ndirtysegs, -1); | ||
557 | kunmap_atomic(kaddr, KM_USER0); | ||
558 | nilfs_mdt_mark_buffer_dirty(header_bh); | ||
559 | nilfs_mdt_mark_buffer_dirty(su_bh); | ||
560 | nilfs_mdt_mark_dirty(sufile); | ||
561 | brelse(su_bh); | ||
562 | |||
563 | out_header: | ||
564 | brelse(header_bh); | ||
565 | |||
566 | out_sem: | ||
567 | up_write(&NILFS_MDT(sufile)->mi_sem); | ||
568 | return ret; | ||
569 | } | ||
570 | |||
571 | /** | ||
572 | * nilfs_sufile_get_suinfo - | ||
573 | * @sufile: inode of segment usage file | ||
574 | * @segnum: segment number to start looking | ||
575 | * @si: array of suinfo | ||
576 | * @nsi: size of suinfo array | ||
577 | * | ||
578 | * Description: | ||
579 | * | ||
580 | * Return Value: On success, 0 is returned and .... On error, one of the | ||
581 | * following negative error codes is returned. | ||
582 | * | ||
583 | * %-EIO - I/O error. | ||
584 | * | ||
585 | * %-ENOMEM - Insufficient amount of memory available. | ||
586 | */ | ||
587 | ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, | ||
588 | struct nilfs_suinfo *si, size_t nsi) | ||
589 | { | ||
590 | struct buffer_head *su_bh; | ||
591 | struct nilfs_segment_usage *su; | ||
592 | size_t susz = NILFS_MDT(sufile)->mi_entry_size; | ||
593 | struct the_nilfs *nilfs = NILFS_MDT(sufile)->mi_nilfs; | ||
594 | void *kaddr; | ||
595 | unsigned long nsegs, segusages_per_block; | ||
596 | ssize_t n; | ||
597 | int ret, i, j; | ||
598 | |||
599 | down_read(&NILFS_MDT(sufile)->mi_sem); | ||
600 | |||
601 | segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile); | ||
602 | nsegs = min_t(unsigned long, | ||
603 | nilfs_sufile_get_nsegments(sufile) - segnum, | ||
604 | nsi); | ||
605 | for (i = 0; i < nsegs; i += n, segnum += n) { | ||
606 | n = min_t(unsigned long, | ||
607 | segusages_per_block - | ||
608 | nilfs_sufile_get_offset(sufile, segnum), | ||
609 | nsegs - i); | ||
610 | ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, | ||
611 | &su_bh); | ||
612 | if (ret < 0) { | ||
613 | if (ret != -ENOENT) | ||
614 | goto out; | ||
615 | /* hole */ | ||
616 | memset(&si[i], 0, sizeof(struct nilfs_suinfo) * n); | ||
617 | continue; | ||
618 | } | ||
619 | |||
620 | kaddr = kmap_atomic(su_bh->b_page, KM_USER0); | ||
621 | su = nilfs_sufile_block_get_segment_usage( | ||
622 | sufile, segnum, su_bh, kaddr); | ||
623 | for (j = 0; j < n; j++, su = (void *)su + susz) { | ||
624 | si[i + j].sui_lastmod = le64_to_cpu(su->su_lastmod); | ||
625 | si[i + j].sui_nblocks = le32_to_cpu(su->su_nblocks); | ||
626 | si[i + j].sui_flags = le32_to_cpu(su->su_flags) & | ||
627 | ~(1UL << NILFS_SEGMENT_USAGE_ACTIVE); | ||
628 | if (nilfs_segment_is_active(nilfs, segnum + i + j)) | ||
629 | si[i + j].sui_flags |= | ||
630 | (1UL << NILFS_SEGMENT_USAGE_ACTIVE); | ||
631 | } | ||
632 | kunmap_atomic(kaddr, KM_USER0); | ||
633 | brelse(su_bh); | ||
634 | } | ||
635 | ret = nsegs; | ||
636 | |||
637 | out: | ||
638 | up_read(&NILFS_MDT(sufile)->mi_sem); | ||
639 | return ret; | ||
640 | } | ||