Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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 */ |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 233 | if (unlikely(!nilfs_segment_usage_clean(su))) { |
| 234 | printk(KERN_WARNING "%s: segment %llu must be clean\n", |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 235 | __func__, (unsigned long long)segnum); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 236 | kunmap_atomic(kaddr, KM_USER0); |
| 237 | goto out_su_bh; |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 252 | out_su_bh: |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 253 | brelse(su_bh); |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 254 | out_header: |
| 255 | brelse(header_bh); |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 319 | WARN_ON(nilfs_segment_usage_error(su)); |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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 */ |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 387 | if (segnum >= nilfs_sufile_get_nsegments(sufile)) |
| 388 | return -EINVAL; |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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; |
Ryusuke Konishi | 2c2e52f | 2009-04-06 19:01:54 -0700 | [diff] [blame] | 448 | struct the_nilfs *nilfs = NILFS_MDT(sufile)->mi_nilfs; |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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); |
Ryusuke Konishi | 2c2e52f | 2009-04-06 19:01:54 -0700 | [diff] [blame] | 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); |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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. |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 517 | * |
| 518 | * %-EINVAL - Invalid segment usage number. |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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; |
Ryusuke Konishi | 88072fa | 2009-04-05 15:03:16 +0900 | [diff] [blame^] | 526 | int suclean, ret; |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 527 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 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 | } |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 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 | } |
Ryusuke Konishi | 88072fa | 2009-04-05 15:03:16 +0900 | [diff] [blame^] | 549 | suclean = nilfs_segment_usage_clean(su); |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 550 | |
| 551 | nilfs_segment_usage_set_error(su); |
| 552 | kunmap_atomic(kaddr, KM_USER0); |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 553 | |
Ryusuke Konishi | 88072fa | 2009-04-05 15:03:16 +0900 | [diff] [blame^] | 554 | if (suclean) { |
| 555 | kaddr = kmap_atomic(header_bh->b_page, KM_USER0); |
| 556 | header = nilfs_sufile_block_get_header(sufile, header_bh, |
| 557 | kaddr); |
| 558 | le64_add_cpu(&header->sh_ncleansegs, -1); |
| 559 | kunmap_atomic(kaddr, KM_USER0); |
| 560 | nilfs_mdt_mark_buffer_dirty(header_bh); |
| 561 | } |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 562 | nilfs_mdt_mark_buffer_dirty(su_bh); |
| 563 | nilfs_mdt_mark_dirty(sufile); |
| 564 | brelse(su_bh); |
| 565 | |
| 566 | out_header: |
| 567 | brelse(header_bh); |
| 568 | |
| 569 | out_sem: |
| 570 | up_write(&NILFS_MDT(sufile)->mi_sem); |
| 571 | return ret; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * nilfs_sufile_get_suinfo - |
| 576 | * @sufile: inode of segment usage file |
| 577 | * @segnum: segment number to start looking |
| 578 | * @si: array of suinfo |
| 579 | * @nsi: size of suinfo array |
| 580 | * |
| 581 | * Description: |
| 582 | * |
| 583 | * Return Value: On success, 0 is returned and .... On error, one of the |
| 584 | * following negative error codes is returned. |
| 585 | * |
| 586 | * %-EIO - I/O error. |
| 587 | * |
| 588 | * %-ENOMEM - Insufficient amount of memory available. |
| 589 | */ |
| 590 | ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, |
| 591 | struct nilfs_suinfo *si, size_t nsi) |
| 592 | { |
| 593 | struct buffer_head *su_bh; |
| 594 | struct nilfs_segment_usage *su; |
| 595 | size_t susz = NILFS_MDT(sufile)->mi_entry_size; |
Ryusuke Konishi | cece552 | 2009-04-06 19:01:58 -0700 | [diff] [blame] | 596 | struct the_nilfs *nilfs = NILFS_MDT(sufile)->mi_nilfs; |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 597 | void *kaddr; |
| 598 | unsigned long nsegs, segusages_per_block; |
| 599 | ssize_t n; |
| 600 | int ret, i, j; |
| 601 | |
| 602 | down_read(&NILFS_MDT(sufile)->mi_sem); |
| 603 | |
| 604 | segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile); |
| 605 | nsegs = min_t(unsigned long, |
| 606 | nilfs_sufile_get_nsegments(sufile) - segnum, |
| 607 | nsi); |
| 608 | for (i = 0; i < nsegs; i += n, segnum += n) { |
| 609 | n = min_t(unsigned long, |
| 610 | segusages_per_block - |
| 611 | nilfs_sufile_get_offset(sufile, segnum), |
| 612 | nsegs - i); |
| 613 | ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, |
| 614 | &su_bh); |
| 615 | if (ret < 0) { |
| 616 | if (ret != -ENOENT) |
| 617 | goto out; |
| 618 | /* hole */ |
| 619 | memset(&si[i], 0, sizeof(struct nilfs_suinfo) * n); |
| 620 | continue; |
| 621 | } |
| 622 | |
| 623 | kaddr = kmap_atomic(su_bh->b_page, KM_USER0); |
| 624 | su = nilfs_sufile_block_get_segment_usage( |
| 625 | sufile, segnum, su_bh, kaddr); |
| 626 | for (j = 0; j < n; j++, su = (void *)su + susz) { |
| 627 | si[i + j].sui_lastmod = le64_to_cpu(su->su_lastmod); |
| 628 | si[i + j].sui_nblocks = le32_to_cpu(su->su_nblocks); |
Ryusuke Konishi | cece552 | 2009-04-06 19:01:58 -0700 | [diff] [blame] | 629 | si[i + j].sui_flags = le32_to_cpu(su->su_flags) & |
| 630 | ~(1UL << NILFS_SEGMENT_USAGE_ACTIVE); |
Ryusuke Konishi | 3efb55b | 2009-03-30 00:50:19 +0900 | [diff] [blame] | 631 | if (nilfs_segment_is_active(nilfs, segnum + j)) |
Ryusuke Konishi | cece552 | 2009-04-06 19:01:58 -0700 | [diff] [blame] | 632 | si[i + j].sui_flags |= |
| 633 | (1UL << NILFS_SEGMENT_USAGE_ACTIVE); |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 634 | } |
| 635 | kunmap_atomic(kaddr, KM_USER0); |
| 636 | brelse(su_bh); |
| 637 | } |
| 638 | ret = nsegs; |
| 639 | |
| 640 | out: |
| 641 | up_read(&NILFS_MDT(sufile)->mi_sem); |
| 642 | return ret; |
| 643 | } |