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