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