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