Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * cpfile.c - NILFS checkpoint 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 | * |
Ryusuke Konishi | 4b420ab | 2016-05-23 16:23:09 -0700 | [diff] [blame^] | 16 | * Written by Koji Sato. |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/buffer_head.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/nilfs2_fs.h> |
| 25 | #include "mdt.h" |
| 26 | #include "cpfile.h" |
| 27 | |
| 28 | |
| 29 | static inline unsigned long |
| 30 | nilfs_cpfile_checkpoints_per_block(const struct inode *cpfile) |
| 31 | { |
| 32 | return NILFS_MDT(cpfile)->mi_entries_per_block; |
| 33 | } |
| 34 | |
| 35 | /* block number from the beginning of the file */ |
| 36 | static unsigned long |
| 37 | nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno) |
| 38 | { |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 39 | __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 40 | do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile)); |
| 41 | return (unsigned long)tcno; |
| 42 | } |
| 43 | |
| 44 | /* offset in block */ |
| 45 | static unsigned long |
| 46 | nilfs_cpfile_get_offset(const struct inode *cpfile, __u64 cno) |
| 47 | { |
| 48 | __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1; |
| 49 | return do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile)); |
| 50 | } |
| 51 | |
Ryusuke Konishi | 53a2c3b | 2015-04-16 12:46:42 -0700 | [diff] [blame] | 52 | static __u64 nilfs_cpfile_first_checkpoint_in_block(const struct inode *cpfile, |
| 53 | unsigned long blkoff) |
| 54 | { |
| 55 | return (__u64)nilfs_cpfile_checkpoints_per_block(cpfile) * blkoff |
| 56 | + 1 - NILFS_MDT(cpfile)->mi_first_entry_offset; |
| 57 | } |
| 58 | |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 59 | static unsigned long |
| 60 | nilfs_cpfile_checkpoints_in_block(const struct inode *cpfile, |
| 61 | __u64 curr, |
| 62 | __u64 max) |
| 63 | { |
| 64 | return min_t(__u64, |
| 65 | nilfs_cpfile_checkpoints_per_block(cpfile) - |
| 66 | nilfs_cpfile_get_offset(cpfile, curr), |
| 67 | max - curr); |
| 68 | } |
| 69 | |
| 70 | static inline int nilfs_cpfile_is_in_first(const struct inode *cpfile, |
| 71 | __u64 cno) |
| 72 | { |
| 73 | return nilfs_cpfile_get_blkoff(cpfile, cno) == 0; |
| 74 | } |
| 75 | |
| 76 | static unsigned int |
| 77 | nilfs_cpfile_block_add_valid_checkpoints(const struct inode *cpfile, |
| 78 | struct buffer_head *bh, |
| 79 | void *kaddr, |
| 80 | unsigned int n) |
| 81 | { |
| 82 | struct nilfs_checkpoint *cp = kaddr + bh_offset(bh); |
| 83 | unsigned int count; |
| 84 | |
| 85 | count = le32_to_cpu(cp->cp_checkpoints_count) + n; |
| 86 | cp->cp_checkpoints_count = cpu_to_le32(count); |
| 87 | return count; |
| 88 | } |
| 89 | |
| 90 | static unsigned int |
| 91 | nilfs_cpfile_block_sub_valid_checkpoints(const struct inode *cpfile, |
| 92 | struct buffer_head *bh, |
| 93 | void *kaddr, |
| 94 | unsigned int n) |
| 95 | { |
| 96 | struct nilfs_checkpoint *cp = kaddr + bh_offset(bh); |
| 97 | unsigned int count; |
| 98 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 99 | WARN_ON(le32_to_cpu(cp->cp_checkpoints_count) < n); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 100 | count = le32_to_cpu(cp->cp_checkpoints_count) - n; |
| 101 | cp->cp_checkpoints_count = cpu_to_le32(count); |
| 102 | return count; |
| 103 | } |
| 104 | |
| 105 | static inline struct nilfs_cpfile_header * |
| 106 | nilfs_cpfile_block_get_header(const struct inode *cpfile, |
| 107 | struct buffer_head *bh, |
| 108 | void *kaddr) |
| 109 | { |
| 110 | return kaddr + bh_offset(bh); |
| 111 | } |
| 112 | |
| 113 | static struct nilfs_checkpoint * |
| 114 | nilfs_cpfile_block_get_checkpoint(const struct inode *cpfile, __u64 cno, |
| 115 | struct buffer_head *bh, |
| 116 | void *kaddr) |
| 117 | { |
| 118 | return kaddr + bh_offset(bh) + nilfs_cpfile_get_offset(cpfile, cno) * |
| 119 | NILFS_MDT(cpfile)->mi_entry_size; |
| 120 | } |
| 121 | |
| 122 | static void nilfs_cpfile_block_init(struct inode *cpfile, |
| 123 | struct buffer_head *bh, |
| 124 | void *kaddr) |
| 125 | { |
| 126 | struct nilfs_checkpoint *cp = kaddr + bh_offset(bh); |
| 127 | size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size; |
| 128 | int n = nilfs_cpfile_checkpoints_per_block(cpfile); |
| 129 | |
| 130 | while (n-- > 0) { |
| 131 | nilfs_checkpoint_set_invalid(cp); |
| 132 | cp = (void *)cp + cpsz; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static inline int nilfs_cpfile_get_header_block(struct inode *cpfile, |
| 137 | struct buffer_head **bhp) |
| 138 | { |
| 139 | return nilfs_mdt_get_block(cpfile, 0, 0, NULL, bhp); |
| 140 | } |
| 141 | |
| 142 | static inline int nilfs_cpfile_get_checkpoint_block(struct inode *cpfile, |
| 143 | __u64 cno, |
| 144 | int create, |
| 145 | struct buffer_head **bhp) |
| 146 | { |
| 147 | return nilfs_mdt_get_block(cpfile, |
| 148 | nilfs_cpfile_get_blkoff(cpfile, cno), |
| 149 | create, nilfs_cpfile_block_init, bhp); |
| 150 | } |
| 151 | |
Ryusuke Konishi | 53a2c3b | 2015-04-16 12:46:42 -0700 | [diff] [blame] | 152 | /** |
| 153 | * nilfs_cpfile_find_checkpoint_block - find and get a buffer on cpfile |
| 154 | * @cpfile: inode of cpfile |
| 155 | * @start_cno: start checkpoint number (inclusive) |
| 156 | * @end_cno: end checkpoint number (inclusive) |
| 157 | * @cnop: place to store the next checkpoint number |
| 158 | * @bhp: place to store a pointer to buffer_head struct |
| 159 | * |
| 160 | * Return Value: On success, it returns 0. On error, the following negative |
| 161 | * error code is returned. |
| 162 | * |
| 163 | * %-ENOMEM - Insufficient memory available. |
| 164 | * |
| 165 | * %-EIO - I/O error |
| 166 | * |
| 167 | * %-ENOENT - no block exists in the range. |
| 168 | */ |
| 169 | static int nilfs_cpfile_find_checkpoint_block(struct inode *cpfile, |
| 170 | __u64 start_cno, __u64 end_cno, |
| 171 | __u64 *cnop, |
| 172 | struct buffer_head **bhp) |
| 173 | { |
| 174 | unsigned long start, end, blkoff; |
| 175 | int ret; |
| 176 | |
| 177 | if (unlikely(start_cno > end_cno)) |
| 178 | return -ENOENT; |
| 179 | |
| 180 | start = nilfs_cpfile_get_blkoff(cpfile, start_cno); |
| 181 | end = nilfs_cpfile_get_blkoff(cpfile, end_cno); |
| 182 | |
| 183 | ret = nilfs_mdt_find_block(cpfile, start, end, &blkoff, bhp); |
| 184 | if (!ret) |
| 185 | *cnop = (blkoff == start) ? start_cno : |
| 186 | nilfs_cpfile_first_checkpoint_in_block(cpfile, blkoff); |
| 187 | return ret; |
| 188 | } |
| 189 | |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 190 | static inline int nilfs_cpfile_delete_checkpoint_block(struct inode *cpfile, |
| 191 | __u64 cno) |
| 192 | { |
| 193 | return nilfs_mdt_delete_block(cpfile, |
| 194 | nilfs_cpfile_get_blkoff(cpfile, cno)); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * nilfs_cpfile_get_checkpoint - get a checkpoint |
| 199 | * @cpfile: inode of checkpoint file |
| 200 | * @cno: checkpoint number |
| 201 | * @create: create flag |
| 202 | * @cpp: pointer to a checkpoint |
| 203 | * @bhp: pointer to a buffer head |
| 204 | * |
| 205 | * Description: nilfs_cpfile_get_checkpoint() acquires the checkpoint |
| 206 | * specified by @cno. A new checkpoint will be created if @cno is the current |
| 207 | * checkpoint number and @create is nonzero. |
| 208 | * |
| 209 | * Return Value: On success, 0 is returned, and the checkpoint and the |
| 210 | * buffer head of the buffer on which the checkpoint is located are stored in |
| 211 | * the place pointed by @cpp and @bhp, respectively. On error, one of the |
| 212 | * following negative error codes is returned. |
| 213 | * |
| 214 | * %-EIO - I/O error. |
| 215 | * |
| 216 | * %-ENOMEM - Insufficient amount of memory available. |
| 217 | * |
| 218 | * %-ENOENT - No such checkpoint. |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 219 | * |
| 220 | * %-EINVAL - invalid checkpoint. |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 221 | */ |
| 222 | int nilfs_cpfile_get_checkpoint(struct inode *cpfile, |
| 223 | __u64 cno, |
| 224 | int create, |
| 225 | struct nilfs_checkpoint **cpp, |
| 226 | struct buffer_head **bhp) |
| 227 | { |
| 228 | struct buffer_head *header_bh, *cp_bh; |
| 229 | struct nilfs_cpfile_header *header; |
| 230 | struct nilfs_checkpoint *cp; |
| 231 | void *kaddr; |
| 232 | int ret; |
| 233 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 234 | if (unlikely(cno < 1 || cno > nilfs_mdt_cno(cpfile) || |
| 235 | (cno < nilfs_mdt_cno(cpfile) && create))) |
| 236 | return -EINVAL; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 237 | |
| 238 | down_write(&NILFS_MDT(cpfile)->mi_sem); |
| 239 | |
| 240 | ret = nilfs_cpfile_get_header_block(cpfile, &header_bh); |
| 241 | if (ret < 0) |
| 242 | goto out_sem; |
| 243 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, create, &cp_bh); |
| 244 | if (ret < 0) |
| 245 | goto out_header; |
| 246 | kaddr = kmap(cp_bh->b_page); |
| 247 | cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr); |
| 248 | if (nilfs_checkpoint_invalid(cp)) { |
| 249 | if (!create) { |
| 250 | kunmap(cp_bh->b_page); |
| 251 | brelse(cp_bh); |
| 252 | ret = -ENOENT; |
| 253 | goto out_header; |
| 254 | } |
| 255 | /* a newly-created checkpoint */ |
| 256 | nilfs_checkpoint_clear_invalid(cp); |
| 257 | if (!nilfs_cpfile_is_in_first(cpfile, cno)) |
| 258 | nilfs_cpfile_block_add_valid_checkpoints(cpfile, cp_bh, |
| 259 | kaddr, 1); |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 260 | mark_buffer_dirty(cp_bh); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 261 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 262 | kaddr = kmap_atomic(header_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 263 | header = nilfs_cpfile_block_get_header(cpfile, header_bh, |
| 264 | kaddr); |
| 265 | le64_add_cpu(&header->ch_ncheckpoints, 1); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 266 | kunmap_atomic(kaddr); |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 267 | mark_buffer_dirty(header_bh); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 268 | nilfs_mdt_mark_dirty(cpfile); |
| 269 | } |
| 270 | |
| 271 | if (cpp != NULL) |
| 272 | *cpp = cp; |
| 273 | *bhp = cp_bh; |
| 274 | |
| 275 | out_header: |
| 276 | brelse(header_bh); |
| 277 | |
| 278 | out_sem: |
| 279 | up_write(&NILFS_MDT(cpfile)->mi_sem); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * nilfs_cpfile_put_checkpoint - put a checkpoint |
| 285 | * @cpfile: inode of checkpoint file |
| 286 | * @cno: checkpoint number |
| 287 | * @bh: buffer head |
| 288 | * |
| 289 | * Description: nilfs_cpfile_put_checkpoint() releases the checkpoint |
| 290 | * specified by @cno. @bh must be the buffer head which has been returned by |
| 291 | * a previous call to nilfs_cpfile_get_checkpoint() with @cno. |
| 292 | */ |
| 293 | void nilfs_cpfile_put_checkpoint(struct inode *cpfile, __u64 cno, |
| 294 | struct buffer_head *bh) |
| 295 | { |
| 296 | kunmap(bh->b_page); |
| 297 | brelse(bh); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * nilfs_cpfile_delete_checkpoints - delete checkpoints |
| 302 | * @cpfile: inode of checkpoint file |
| 303 | * @start: start checkpoint number |
| 304 | * @end: end checkpoint numer |
| 305 | * |
| 306 | * Description: nilfs_cpfile_delete_checkpoints() deletes the checkpoints in |
| 307 | * the period from @start to @end, excluding @end itself. The checkpoints |
| 308 | * which have been already deleted are ignored. |
| 309 | * |
| 310 | * Return Value: On success, 0 is returned. On error, one of the following |
| 311 | * negative error codes is returned. |
| 312 | * |
| 313 | * %-EIO - I/O error. |
| 314 | * |
| 315 | * %-ENOMEM - Insufficient amount of memory available. |
| 316 | * |
| 317 | * %-EINVAL - invalid checkpoints. |
| 318 | */ |
| 319 | int nilfs_cpfile_delete_checkpoints(struct inode *cpfile, |
| 320 | __u64 start, |
| 321 | __u64 end) |
| 322 | { |
| 323 | struct buffer_head *header_bh, *cp_bh; |
| 324 | struct nilfs_cpfile_header *header; |
| 325 | struct nilfs_checkpoint *cp; |
| 326 | size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size; |
| 327 | __u64 cno; |
| 328 | void *kaddr; |
| 329 | unsigned long tnicps; |
Ryusuke Konishi | fe0627e | 2012-07-30 14:42:05 -0700 | [diff] [blame] | 330 | int ret, ncps, nicps, nss, count, i; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 331 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 332 | if (unlikely(start == 0 || start > end)) { |
| 333 | printk(KERN_ERR "%s: invalid range of checkpoint numbers: " |
| 334 | "[%llu, %llu)\n", __func__, |
| 335 | (unsigned long long)start, (unsigned long long)end); |
| 336 | return -EINVAL; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 339 | down_write(&NILFS_MDT(cpfile)->mi_sem); |
| 340 | |
| 341 | ret = nilfs_cpfile_get_header_block(cpfile, &header_bh); |
| 342 | if (ret < 0) |
| 343 | goto out_sem; |
| 344 | tnicps = 0; |
Ryusuke Konishi | fe0627e | 2012-07-30 14:42:05 -0700 | [diff] [blame] | 345 | nss = 0; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 346 | |
| 347 | for (cno = start; cno < end; cno += ncps) { |
| 348 | ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, end); |
| 349 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh); |
| 350 | if (ret < 0) { |
| 351 | if (ret != -ENOENT) |
Jiro SEKIBA | d9a0a34 | 2009-07-04 23:00:53 +0900 | [diff] [blame] | 352 | break; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 353 | /* skip hole */ |
| 354 | ret = 0; |
| 355 | continue; |
| 356 | } |
| 357 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 358 | kaddr = kmap_atomic(cp_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 359 | cp = nilfs_cpfile_block_get_checkpoint( |
| 360 | cpfile, cno, cp_bh, kaddr); |
| 361 | nicps = 0; |
| 362 | for (i = 0; i < ncps; i++, cp = (void *)cp + cpsz) { |
Ryusuke Konishi | fe0627e | 2012-07-30 14:42:05 -0700 | [diff] [blame] | 363 | if (nilfs_checkpoint_snapshot(cp)) { |
| 364 | nss++; |
| 365 | } else if (!nilfs_checkpoint_invalid(cp)) { |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 366 | nilfs_checkpoint_set_invalid(cp); |
| 367 | nicps++; |
| 368 | } |
| 369 | } |
| 370 | if (nicps > 0) { |
| 371 | tnicps += nicps; |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 372 | mark_buffer_dirty(cp_bh); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 373 | nilfs_mdt_mark_dirty(cpfile); |
Jiro SEKIBA | 5ee5814 | 2009-12-06 15:43:56 +0900 | [diff] [blame] | 374 | if (!nilfs_cpfile_is_in_first(cpfile, cno)) { |
| 375 | count = |
| 376 | nilfs_cpfile_block_sub_valid_checkpoints( |
| 377 | cpfile, cp_bh, kaddr, nicps); |
| 378 | if (count == 0) { |
| 379 | /* make hole */ |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 380 | kunmap_atomic(kaddr); |
Jiro SEKIBA | 5ee5814 | 2009-12-06 15:43:56 +0900 | [diff] [blame] | 381 | brelse(cp_bh); |
| 382 | ret = |
| 383 | nilfs_cpfile_delete_checkpoint_block( |
| 384 | cpfile, cno); |
| 385 | if (ret == 0) |
| 386 | continue; |
| 387 | printk(KERN_ERR |
| 388 | "%s: cannot delete block\n", |
| 389 | __func__); |
| 390 | break; |
| 391 | } |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 395 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 396 | brelse(cp_bh); |
| 397 | } |
| 398 | |
| 399 | if (tnicps > 0) { |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 400 | kaddr = kmap_atomic(header_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 401 | header = nilfs_cpfile_block_get_header(cpfile, header_bh, |
| 402 | kaddr); |
Koji Sato | 6c98cd4 | 2009-04-06 19:01:32 -0700 | [diff] [blame] | 403 | le64_add_cpu(&header->ch_ncheckpoints, -(u64)tnicps); |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 404 | mark_buffer_dirty(header_bh); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 405 | nilfs_mdt_mark_dirty(cpfile); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 406 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 407 | } |
Ryusuke Konishi | 62013ab | 2009-05-30 21:50:58 +0900 | [diff] [blame] | 408 | |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 409 | brelse(header_bh); |
Ryusuke Konishi | fe0627e | 2012-07-30 14:42:05 -0700 | [diff] [blame] | 410 | if (nss > 0) |
| 411 | ret = -EBUSY; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 412 | |
| 413 | out_sem: |
| 414 | up_write(&NILFS_MDT(cpfile)->mi_sem); |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode *cpfile, |
| 419 | struct nilfs_checkpoint *cp, |
| 420 | struct nilfs_cpinfo *ci) |
| 421 | { |
| 422 | ci->ci_flags = le32_to_cpu(cp->cp_flags); |
| 423 | ci->ci_cno = le64_to_cpu(cp->cp_cno); |
| 424 | ci->ci_create = le64_to_cpu(cp->cp_create); |
| 425 | ci->ci_nblk_inc = le64_to_cpu(cp->cp_nblk_inc); |
| 426 | ci->ci_inodes_count = le64_to_cpu(cp->cp_inodes_count); |
| 427 | ci->ci_blocks_count = le64_to_cpu(cp->cp_blocks_count); |
| 428 | ci->ci_next = le64_to_cpu(cp->cp_snapshot_list.ssl_next); |
| 429 | } |
| 430 | |
Ryusuke Konishi | 76068c4 | 2009-04-06 19:01:50 -0700 | [diff] [blame] | 431 | static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop, |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 432 | void *buf, unsigned cisz, size_t nci) |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 433 | { |
| 434 | struct nilfs_checkpoint *cp; |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 435 | struct nilfs_cpinfo *ci = buf; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 436 | struct buffer_head *bh; |
| 437 | size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size; |
Ryusuke Konishi | 76068c4 | 2009-04-06 19:01:50 -0700 | [diff] [blame] | 438 | __u64 cur_cno = nilfs_mdt_cno(cpfile), cno = *cnop; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 439 | void *kaddr; |
| 440 | int n, ret; |
| 441 | int ncps, i; |
| 442 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 443 | if (cno == 0) |
| 444 | return -ENOENT; /* checkpoint number 0 is invalid */ |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 445 | down_read(&NILFS_MDT(cpfile)->mi_sem); |
| 446 | |
Ryusuke Konishi | 53a2c3b | 2015-04-16 12:46:42 -0700 | [diff] [blame] | 447 | for (n = 0; n < nci; cno += ncps) { |
| 448 | ret = nilfs_cpfile_find_checkpoint_block( |
| 449 | cpfile, cno, cur_cno - 1, &cno, &bh); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 450 | if (ret < 0) { |
Ryusuke Konishi | 53a2c3b | 2015-04-16 12:46:42 -0700 | [diff] [blame] | 451 | if (likely(ret == -ENOENT)) |
| 452 | break; |
| 453 | goto out; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 454 | } |
Ryusuke Konishi | 53a2c3b | 2015-04-16 12:46:42 -0700 | [diff] [blame] | 455 | ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, cur_cno); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 456 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 457 | kaddr = kmap_atomic(bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 458 | cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr); |
| 459 | for (i = 0; i < ncps && n < nci; i++, cp = (void *)cp + cpsz) { |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 460 | if (!nilfs_checkpoint_invalid(cp)) { |
| 461 | nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, |
| 462 | ci); |
| 463 | ci = (void *)ci + cisz; |
| 464 | n++; |
| 465 | } |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 466 | } |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 467 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 468 | brelse(bh); |
| 469 | } |
| 470 | |
| 471 | ret = n; |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 472 | if (n > 0) { |
| 473 | ci = (void *)ci - cisz; |
| 474 | *cnop = ci->ci_cno + 1; |
| 475 | } |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 476 | |
| 477 | out: |
| 478 | up_read(&NILFS_MDT(cpfile)->mi_sem); |
| 479 | return ret; |
| 480 | } |
| 481 | |
Ryusuke Konishi | b028fcf | 2009-04-06 19:01:47 -0700 | [diff] [blame] | 482 | static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop, |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 483 | void *buf, unsigned cisz, size_t nci) |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 484 | { |
| 485 | struct buffer_head *bh; |
| 486 | struct nilfs_cpfile_header *header; |
| 487 | struct nilfs_checkpoint *cp; |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 488 | struct nilfs_cpinfo *ci = buf; |
Ryusuke Konishi | b028fcf | 2009-04-06 19:01:47 -0700 | [diff] [blame] | 489 | __u64 curr = *cnop, next; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 490 | unsigned long curr_blkoff, next_blkoff; |
| 491 | void *kaddr; |
Ryusuke Konishi | 7fa10d2 | 2009-04-06 19:01:48 -0700 | [diff] [blame] | 492 | int n = 0, ret; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 493 | |
| 494 | down_read(&NILFS_MDT(cpfile)->mi_sem); |
| 495 | |
Ryusuke Konishi | b028fcf | 2009-04-06 19:01:47 -0700 | [diff] [blame] | 496 | if (curr == 0) { |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 497 | ret = nilfs_cpfile_get_header_block(cpfile, &bh); |
| 498 | if (ret < 0) |
| 499 | goto out; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 500 | kaddr = kmap_atomic(bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 501 | header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr); |
| 502 | curr = le64_to_cpu(header->ch_snapshot_list.ssl_next); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 503 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 504 | brelse(bh); |
| 505 | if (curr == 0) { |
| 506 | ret = 0; |
| 507 | goto out; |
| 508 | } |
Ryusuke Konishi | b028fcf | 2009-04-06 19:01:47 -0700 | [diff] [blame] | 509 | } else if (unlikely(curr == ~(__u64)0)) { |
| 510 | ret = 0; |
| 511 | goto out; |
| 512 | } |
| 513 | |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 514 | curr_blkoff = nilfs_cpfile_get_blkoff(cpfile, curr); |
| 515 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr, 0, &bh); |
Ryusuke Konishi | 7fa10d2 | 2009-04-06 19:01:48 -0700 | [diff] [blame] | 516 | if (unlikely(ret < 0)) { |
| 517 | if (ret == -ENOENT) |
| 518 | ret = 0; /* No snapshots (started from a hole block) */ |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 519 | goto out; |
Ryusuke Konishi | 7fa10d2 | 2009-04-06 19:01:48 -0700 | [diff] [blame] | 520 | } |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 521 | kaddr = kmap_atomic(bh->b_page); |
Ryusuke Konishi | 7fa10d2 | 2009-04-06 19:01:48 -0700 | [diff] [blame] | 522 | while (n < nci) { |
| 523 | cp = nilfs_cpfile_block_get_checkpoint(cpfile, curr, bh, kaddr); |
| 524 | curr = ~(__u64)0; /* Terminator */ |
| 525 | if (unlikely(nilfs_checkpoint_invalid(cp) || |
| 526 | !nilfs_checkpoint_snapshot(cp))) |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 527 | break; |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 528 | nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, ci); |
| 529 | ci = (void *)ci + cisz; |
| 530 | n++; |
Ryusuke Konishi | 7fa10d2 | 2009-04-06 19:01:48 -0700 | [diff] [blame] | 531 | next = le64_to_cpu(cp->cp_snapshot_list.ssl_next); |
| 532 | if (next == 0) |
| 533 | break; /* reach end of the snapshot list */ |
| 534 | |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 535 | next_blkoff = nilfs_cpfile_get_blkoff(cpfile, next); |
| 536 | if (curr_blkoff != next_blkoff) { |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 537 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 538 | brelse(bh); |
| 539 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, next, |
| 540 | 0, &bh); |
Ryusuke Konishi | 7fa10d2 | 2009-04-06 19:01:48 -0700 | [diff] [blame] | 541 | if (unlikely(ret < 0)) { |
| 542 | WARN_ON(ret == -ENOENT); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 543 | goto out; |
Ryusuke Konishi | 7fa10d2 | 2009-04-06 19:01:48 -0700 | [diff] [blame] | 544 | } |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 545 | kaddr = kmap_atomic(bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 546 | } |
| 547 | curr = next; |
| 548 | curr_blkoff = next_blkoff; |
| 549 | } |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 550 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 551 | brelse(bh); |
Ryusuke Konishi | b028fcf | 2009-04-06 19:01:47 -0700 | [diff] [blame] | 552 | *cnop = curr; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 553 | ret = n; |
| 554 | |
| 555 | out: |
| 556 | up_read(&NILFS_MDT(cpfile)->mi_sem); |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * nilfs_cpfile_get_cpinfo - |
| 562 | * @cpfile: |
| 563 | * @cno: |
| 564 | * @ci: |
| 565 | * @nci: |
| 566 | */ |
Ryusuke Konishi | b028fcf | 2009-04-06 19:01:47 -0700 | [diff] [blame] | 567 | |
| 568 | ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode, |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 569 | void *buf, unsigned cisz, size_t nci) |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 570 | { |
| 571 | switch (mode) { |
| 572 | case NILFS_CHECKPOINT: |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 573 | return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, buf, cisz, nci); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 574 | case NILFS_SNAPSHOT: |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 575 | return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, buf, cisz, nci); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 576 | default: |
| 577 | return -EINVAL; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * nilfs_cpfile_delete_checkpoint - |
| 583 | * @cpfile: |
| 584 | * @cno: |
| 585 | */ |
| 586 | int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno) |
| 587 | { |
| 588 | struct nilfs_cpinfo ci; |
Ryusuke Konishi | 76068c4 | 2009-04-06 19:01:50 -0700 | [diff] [blame] | 589 | __u64 tcno = cno; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 590 | ssize_t nci; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 591 | |
Ryusuke Konishi | 003ff18 | 2009-05-12 03:58:47 +0900 | [diff] [blame] | 592 | nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, sizeof(ci), 1); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 593 | if (nci < 0) |
| 594 | return nci; |
| 595 | else if (nci == 0 || ci.ci_cno != cno) |
| 596 | return -ENOENT; |
Ryusuke Konishi | 30c25be | 2009-05-30 19:08:09 +0900 | [diff] [blame] | 597 | else if (nilfs_cpinfo_snapshot(&ci)) |
| 598 | return -EBUSY; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 599 | |
| 600 | return nilfs_cpfile_delete_checkpoints(cpfile, cno, cno + 1); |
| 601 | } |
| 602 | |
| 603 | static struct nilfs_snapshot_list * |
| 604 | nilfs_cpfile_block_get_snapshot_list(const struct inode *cpfile, |
| 605 | __u64 cno, |
| 606 | struct buffer_head *bh, |
| 607 | void *kaddr) |
| 608 | { |
| 609 | struct nilfs_cpfile_header *header; |
| 610 | struct nilfs_checkpoint *cp; |
| 611 | struct nilfs_snapshot_list *list; |
| 612 | |
| 613 | if (cno != 0) { |
| 614 | cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr); |
| 615 | list = &cp->cp_snapshot_list; |
| 616 | } else { |
| 617 | header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr); |
| 618 | list = &header->ch_snapshot_list; |
| 619 | } |
| 620 | return list; |
| 621 | } |
| 622 | |
| 623 | static int nilfs_cpfile_set_snapshot(struct inode *cpfile, __u64 cno) |
| 624 | { |
| 625 | struct buffer_head *header_bh, *curr_bh, *prev_bh, *cp_bh; |
| 626 | struct nilfs_cpfile_header *header; |
| 627 | struct nilfs_checkpoint *cp; |
| 628 | struct nilfs_snapshot_list *list; |
| 629 | __u64 curr, prev; |
| 630 | unsigned long curr_blkoff, prev_blkoff; |
| 631 | void *kaddr; |
| 632 | int ret; |
| 633 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 634 | if (cno == 0) |
| 635 | return -ENOENT; /* checkpoint number 0 is invalid */ |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 636 | down_write(&NILFS_MDT(cpfile)->mi_sem); |
| 637 | |
| 638 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh); |
| 639 | if (ret < 0) |
| 640 | goto out_sem; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 641 | kaddr = kmap_atomic(cp_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 642 | cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr); |
| 643 | if (nilfs_checkpoint_invalid(cp)) { |
| 644 | ret = -ENOENT; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 645 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 646 | goto out_cp; |
| 647 | } |
| 648 | if (nilfs_checkpoint_snapshot(cp)) { |
| 649 | ret = 0; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 650 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 651 | goto out_cp; |
| 652 | } |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 653 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 654 | |
| 655 | ret = nilfs_cpfile_get_header_block(cpfile, &header_bh); |
| 656 | if (ret < 0) |
| 657 | goto out_cp; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 658 | kaddr = kmap_atomic(header_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 659 | header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr); |
| 660 | list = &header->ch_snapshot_list; |
| 661 | curr_bh = header_bh; |
| 662 | get_bh(curr_bh); |
| 663 | curr = 0; |
| 664 | curr_blkoff = 0; |
| 665 | prev = le64_to_cpu(list->ssl_prev); |
| 666 | while (prev > cno) { |
| 667 | prev_blkoff = nilfs_cpfile_get_blkoff(cpfile, prev); |
| 668 | curr = prev; |
| 669 | if (curr_blkoff != prev_blkoff) { |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 670 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 671 | brelse(curr_bh); |
| 672 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr, |
| 673 | 0, &curr_bh); |
| 674 | if (ret < 0) |
| 675 | goto out_header; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 676 | kaddr = kmap_atomic(curr_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 677 | } |
| 678 | curr_blkoff = prev_blkoff; |
| 679 | cp = nilfs_cpfile_block_get_checkpoint( |
| 680 | cpfile, curr, curr_bh, kaddr); |
| 681 | list = &cp->cp_snapshot_list; |
| 682 | prev = le64_to_cpu(list->ssl_prev); |
| 683 | } |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 684 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 685 | |
| 686 | if (prev != 0) { |
| 687 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0, |
| 688 | &prev_bh); |
| 689 | if (ret < 0) |
| 690 | goto out_curr; |
| 691 | } else { |
| 692 | prev_bh = header_bh; |
| 693 | get_bh(prev_bh); |
| 694 | } |
| 695 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 696 | kaddr = kmap_atomic(curr_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 697 | list = nilfs_cpfile_block_get_snapshot_list( |
| 698 | cpfile, curr, curr_bh, kaddr); |
| 699 | list->ssl_prev = cpu_to_le64(cno); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 700 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 701 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 702 | kaddr = kmap_atomic(cp_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 703 | cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr); |
| 704 | cp->cp_snapshot_list.ssl_next = cpu_to_le64(curr); |
| 705 | cp->cp_snapshot_list.ssl_prev = cpu_to_le64(prev); |
| 706 | nilfs_checkpoint_set_snapshot(cp); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 707 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 708 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 709 | kaddr = kmap_atomic(prev_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 710 | list = nilfs_cpfile_block_get_snapshot_list( |
| 711 | cpfile, prev, prev_bh, kaddr); |
| 712 | list->ssl_next = cpu_to_le64(cno); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 713 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 714 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 715 | kaddr = kmap_atomic(header_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 716 | header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr); |
| 717 | le64_add_cpu(&header->ch_nsnapshots, 1); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 718 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 719 | |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 720 | mark_buffer_dirty(prev_bh); |
| 721 | mark_buffer_dirty(curr_bh); |
| 722 | mark_buffer_dirty(cp_bh); |
| 723 | mark_buffer_dirty(header_bh); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 724 | nilfs_mdt_mark_dirty(cpfile); |
| 725 | |
| 726 | brelse(prev_bh); |
| 727 | |
| 728 | out_curr: |
| 729 | brelse(curr_bh); |
| 730 | |
| 731 | out_header: |
| 732 | brelse(header_bh); |
| 733 | |
| 734 | out_cp: |
| 735 | brelse(cp_bh); |
| 736 | |
| 737 | out_sem: |
| 738 | up_write(&NILFS_MDT(cpfile)->mi_sem); |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | static int nilfs_cpfile_clear_snapshot(struct inode *cpfile, __u64 cno) |
| 743 | { |
| 744 | struct buffer_head *header_bh, *next_bh, *prev_bh, *cp_bh; |
| 745 | struct nilfs_cpfile_header *header; |
| 746 | struct nilfs_checkpoint *cp; |
| 747 | struct nilfs_snapshot_list *list; |
| 748 | __u64 next, prev; |
| 749 | void *kaddr; |
| 750 | int ret; |
| 751 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 752 | if (cno == 0) |
| 753 | return -ENOENT; /* checkpoint number 0 is invalid */ |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 754 | down_write(&NILFS_MDT(cpfile)->mi_sem); |
| 755 | |
| 756 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh); |
| 757 | if (ret < 0) |
| 758 | goto out_sem; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 759 | kaddr = kmap_atomic(cp_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 760 | cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr); |
| 761 | if (nilfs_checkpoint_invalid(cp)) { |
| 762 | ret = -ENOENT; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 763 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 764 | goto out_cp; |
| 765 | } |
| 766 | if (!nilfs_checkpoint_snapshot(cp)) { |
| 767 | ret = 0; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 768 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 769 | goto out_cp; |
| 770 | } |
| 771 | |
| 772 | list = &cp->cp_snapshot_list; |
| 773 | next = le64_to_cpu(list->ssl_next); |
| 774 | prev = le64_to_cpu(list->ssl_prev); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 775 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 776 | |
| 777 | ret = nilfs_cpfile_get_header_block(cpfile, &header_bh); |
| 778 | if (ret < 0) |
| 779 | goto out_cp; |
| 780 | if (next != 0) { |
| 781 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, next, 0, |
| 782 | &next_bh); |
| 783 | if (ret < 0) |
| 784 | goto out_header; |
| 785 | } else { |
| 786 | next_bh = header_bh; |
| 787 | get_bh(next_bh); |
| 788 | } |
| 789 | if (prev != 0) { |
| 790 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0, |
| 791 | &prev_bh); |
| 792 | if (ret < 0) |
| 793 | goto out_next; |
| 794 | } else { |
| 795 | prev_bh = header_bh; |
| 796 | get_bh(prev_bh); |
| 797 | } |
| 798 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 799 | kaddr = kmap_atomic(next_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 800 | list = nilfs_cpfile_block_get_snapshot_list( |
| 801 | cpfile, next, next_bh, kaddr); |
| 802 | list->ssl_prev = cpu_to_le64(prev); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 803 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 804 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 805 | kaddr = kmap_atomic(prev_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 806 | list = nilfs_cpfile_block_get_snapshot_list( |
| 807 | cpfile, prev, prev_bh, kaddr); |
| 808 | list->ssl_next = cpu_to_le64(next); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 809 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 810 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 811 | kaddr = kmap_atomic(cp_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 812 | cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr); |
| 813 | cp->cp_snapshot_list.ssl_next = cpu_to_le64(0); |
| 814 | cp->cp_snapshot_list.ssl_prev = cpu_to_le64(0); |
| 815 | nilfs_checkpoint_clear_snapshot(cp); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 816 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 817 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 818 | kaddr = kmap_atomic(header_bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 819 | header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr); |
| 820 | le64_add_cpu(&header->ch_nsnapshots, -1); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 821 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 822 | |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 823 | mark_buffer_dirty(next_bh); |
| 824 | mark_buffer_dirty(prev_bh); |
| 825 | mark_buffer_dirty(cp_bh); |
| 826 | mark_buffer_dirty(header_bh); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 827 | nilfs_mdt_mark_dirty(cpfile); |
| 828 | |
| 829 | brelse(prev_bh); |
| 830 | |
| 831 | out_next: |
| 832 | brelse(next_bh); |
| 833 | |
| 834 | out_header: |
| 835 | brelse(header_bh); |
| 836 | |
| 837 | out_cp: |
| 838 | brelse(cp_bh); |
| 839 | |
| 840 | out_sem: |
| 841 | up_write(&NILFS_MDT(cpfile)->mi_sem); |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * nilfs_cpfile_is_snapshot - |
| 847 | * @cpfile: inode of checkpoint file |
| 848 | * @cno: checkpoint number |
| 849 | * |
| 850 | * Description: |
| 851 | * |
| 852 | * Return Value: On success, 1 is returned if the checkpoint specified by |
| 853 | * @cno is a snapshot, or 0 if not. On error, one of the following negative |
| 854 | * error codes is returned. |
| 855 | * |
| 856 | * %-EIO - I/O error. |
| 857 | * |
| 858 | * %-ENOMEM - Insufficient amount of memory available. |
| 859 | * |
| 860 | * %-ENOENT - No such checkpoint. |
| 861 | */ |
| 862 | int nilfs_cpfile_is_snapshot(struct inode *cpfile, __u64 cno) |
| 863 | { |
| 864 | struct buffer_head *bh; |
| 865 | struct nilfs_checkpoint *cp; |
| 866 | void *kaddr; |
| 867 | int ret; |
| 868 | |
Zhu Yanhai | 43be0ec | 2009-08-12 14:17:59 +0800 | [diff] [blame] | 869 | /* CP number is invalid if it's zero or larger than the |
| 870 | largest exist one.*/ |
| 871 | if (cno == 0 || cno >= nilfs_mdt_cno(cpfile)) |
| 872 | return -ENOENT; |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 873 | down_read(&NILFS_MDT(cpfile)->mi_sem); |
| 874 | |
| 875 | ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh); |
| 876 | if (ret < 0) |
| 877 | goto out; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 878 | kaddr = kmap_atomic(bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 879 | cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr); |
Zhu Yanhai | 43be0ec | 2009-08-12 14:17:59 +0800 | [diff] [blame] | 880 | if (nilfs_checkpoint_invalid(cp)) |
| 881 | ret = -ENOENT; |
| 882 | else |
| 883 | ret = nilfs_checkpoint_snapshot(cp); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 884 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 885 | brelse(bh); |
| 886 | |
| 887 | out: |
| 888 | up_read(&NILFS_MDT(cpfile)->mi_sem); |
| 889 | return ret; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * nilfs_cpfile_change_cpmode - change checkpoint mode |
| 894 | * @cpfile: inode of checkpoint file |
| 895 | * @cno: checkpoint number |
| 896 | * @status: mode of checkpoint |
| 897 | * |
| 898 | * Description: nilfs_change_cpmode() changes the mode of the checkpoint |
| 899 | * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT. |
| 900 | * |
| 901 | * Return Value: On success, 0 is returned. On error, one of the following |
| 902 | * negative error codes is returned. |
| 903 | * |
| 904 | * %-EIO - I/O error. |
| 905 | * |
| 906 | * %-ENOMEM - Insufficient amount of memory available. |
| 907 | * |
| 908 | * %-ENOENT - No such checkpoint. |
| 909 | */ |
| 910 | int nilfs_cpfile_change_cpmode(struct inode *cpfile, __u64 cno, int mode) |
| 911 | { |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 912 | int ret; |
| 913 | |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 914 | switch (mode) { |
| 915 | case NILFS_CHECKPOINT: |
Ryusuke Konishi | 032dbb3 | 2010-09-13 11:16:34 +0900 | [diff] [blame] | 916 | if (nilfs_checkpoint_is_mounted(cpfile->i_sb, cno)) |
| 917 | /* |
| 918 | * Current implementation does not have to protect |
| 919 | * plain read-only mounts since they are exclusive |
| 920 | * with a read/write mount and are protected from the |
| 921 | * cleaner. |
| 922 | */ |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 923 | ret = -EBUSY; |
Ryusuke Konishi | 032dbb3 | 2010-09-13 11:16:34 +0900 | [diff] [blame] | 924 | else |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 925 | ret = nilfs_cpfile_clear_snapshot(cpfile, cno); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 926 | return ret; |
| 927 | case NILFS_SNAPSHOT: |
| 928 | return nilfs_cpfile_set_snapshot(cpfile, cno); |
| 929 | default: |
| 930 | return -EINVAL; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * nilfs_cpfile_get_stat - get checkpoint statistics |
| 936 | * @cpfile: inode of checkpoint file |
| 937 | * @stat: pointer to a structure of checkpoint statistics |
| 938 | * |
| 939 | * Description: nilfs_cpfile_get_stat() returns information about checkpoints. |
| 940 | * |
| 941 | * Return Value: On success, 0 is returned, and checkpoints information is |
| 942 | * stored in the place pointed by @stat. On error, one of the following |
| 943 | * negative error codes is returned. |
| 944 | * |
| 945 | * %-EIO - I/O error. |
| 946 | * |
| 947 | * %-ENOMEM - Insufficient amount of memory available. |
| 948 | */ |
| 949 | int nilfs_cpfile_get_stat(struct inode *cpfile, struct nilfs_cpstat *cpstat) |
| 950 | { |
| 951 | struct buffer_head *bh; |
| 952 | struct nilfs_cpfile_header *header; |
| 953 | void *kaddr; |
| 954 | int ret; |
| 955 | |
| 956 | down_read(&NILFS_MDT(cpfile)->mi_sem); |
| 957 | |
| 958 | ret = nilfs_cpfile_get_header_block(cpfile, &bh); |
| 959 | if (ret < 0) |
| 960 | goto out_sem; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 961 | kaddr = kmap_atomic(bh->b_page); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 962 | header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr); |
| 963 | cpstat->cs_cno = nilfs_mdt_cno(cpfile); |
| 964 | cpstat->cs_ncps = le64_to_cpu(header->ch_ncheckpoints); |
| 965 | cpstat->cs_nsss = le64_to_cpu(header->ch_nsnapshots); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 966 | kunmap_atomic(kaddr); |
Koji Sato | 2961980 | 2009-04-06 19:01:31 -0700 | [diff] [blame] | 967 | brelse(bh); |
| 968 | |
| 969 | out_sem: |
| 970 | up_read(&NILFS_MDT(cpfile)->mi_sem); |
| 971 | return ret; |
| 972 | } |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 973 | |
| 974 | /** |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 975 | * nilfs_cpfile_read - read or get cpfile inode |
| 976 | * @sb: super block instance |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 977 | * @cpsize: size of a checkpoint entry |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 978 | * @raw_inode: on-disk cpfile inode |
| 979 | * @inodep: buffer to store the inode |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 980 | */ |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 981 | int nilfs_cpfile_read(struct super_block *sb, size_t cpsize, |
| 982 | struct nilfs_inode *raw_inode, struct inode **inodep) |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 983 | { |
| 984 | struct inode *cpfile; |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 985 | int err; |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 986 | |
Ryusuke Konishi | 0ec060d | 2014-04-03 14:50:31 -0700 | [diff] [blame] | 987 | if (cpsize > sb->s_blocksize) { |
| 988 | printk(KERN_ERR |
| 989 | "NILFS: too large checkpoint size: %zu bytes.\n", |
| 990 | cpsize); |
| 991 | return -EINVAL; |
| 992 | } else if (cpsize < NILFS_MIN_CHECKPOINT_SIZE) { |
| 993 | printk(KERN_ERR |
| 994 | "NILFS: too small checkpoint size: %zu bytes.\n", |
| 995 | cpsize); |
| 996 | return -EINVAL; |
| 997 | } |
| 998 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 999 | cpfile = nilfs_iget_locked(sb, NULL, NILFS_CPFILE_INO); |
| 1000 | if (unlikely(!cpfile)) |
| 1001 | return -ENOMEM; |
| 1002 | if (!(cpfile->i_state & I_NEW)) |
| 1003 | goto out; |
| 1004 | |
| 1005 | err = nilfs_mdt_init(cpfile, NILFS_MDT_GFP, 0); |
| 1006 | if (err) |
| 1007 | goto failed; |
| 1008 | |
| 1009 | nilfs_mdt_set_entry_size(cpfile, cpsize, |
| 1010 | sizeof(struct nilfs_cpfile_header)); |
| 1011 | |
| 1012 | err = nilfs_read_inode_common(cpfile, raw_inode); |
| 1013 | if (err) |
| 1014 | goto failed; |
| 1015 | |
| 1016 | unlock_new_inode(cpfile); |
| 1017 | out: |
| 1018 | *inodep = cpfile; |
| 1019 | return 0; |
| 1020 | failed: |
| 1021 | iget_failed(cpfile); |
| 1022 | return err; |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 1023 | } |