2 * cpfile.c - NILFS checkpoint file.
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
16 * Written by Koji Sato.
19 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/buffer_head.h>
23 #include <linux/errno.h>
28 static inline unsigned long
29 nilfs_cpfile_checkpoints_per_block(const struct inode
*cpfile
)
31 return NILFS_MDT(cpfile
)->mi_entries_per_block
;
34 /* block number from the beginning of the file */
36 nilfs_cpfile_get_blkoff(const struct inode
*cpfile
, __u64 cno
)
38 __u64 tcno
= cno
+ NILFS_MDT(cpfile
)->mi_first_entry_offset
- 1;
40 do_div(tcno
, nilfs_cpfile_checkpoints_per_block(cpfile
));
41 return (unsigned long)tcno
;
46 nilfs_cpfile_get_offset(const struct inode
*cpfile
, __u64 cno
)
48 __u64 tcno
= cno
+ NILFS_MDT(cpfile
)->mi_first_entry_offset
- 1;
50 return do_div(tcno
, nilfs_cpfile_checkpoints_per_block(cpfile
));
53 static __u64
nilfs_cpfile_first_checkpoint_in_block(const struct inode
*cpfile
,
56 return (__u64
)nilfs_cpfile_checkpoints_per_block(cpfile
) * blkoff
57 + 1 - NILFS_MDT(cpfile
)->mi_first_entry_offset
;
61 nilfs_cpfile_checkpoints_in_block(const struct inode
*cpfile
,
66 nilfs_cpfile_checkpoints_per_block(cpfile
) -
67 nilfs_cpfile_get_offset(cpfile
, curr
),
71 static inline int nilfs_cpfile_is_in_first(const struct inode
*cpfile
,
74 return nilfs_cpfile_get_blkoff(cpfile
, cno
) == 0;
78 nilfs_cpfile_block_add_valid_checkpoints(const struct inode
*cpfile
,
79 struct buffer_head
*bh
,
83 struct nilfs_checkpoint
*cp
= kaddr
+ bh_offset(bh
);
86 count
= le32_to_cpu(cp
->cp_checkpoints_count
) + n
;
87 cp
->cp_checkpoints_count
= cpu_to_le32(count
);
92 nilfs_cpfile_block_sub_valid_checkpoints(const struct inode
*cpfile
,
93 struct buffer_head
*bh
,
97 struct nilfs_checkpoint
*cp
= kaddr
+ bh_offset(bh
);
100 WARN_ON(le32_to_cpu(cp
->cp_checkpoints_count
) < n
);
101 count
= le32_to_cpu(cp
->cp_checkpoints_count
) - n
;
102 cp
->cp_checkpoints_count
= cpu_to_le32(count
);
106 static inline struct nilfs_cpfile_header
*
107 nilfs_cpfile_block_get_header(const struct inode
*cpfile
,
108 struct buffer_head
*bh
,
111 return kaddr
+ bh_offset(bh
);
114 static struct nilfs_checkpoint
*
115 nilfs_cpfile_block_get_checkpoint(const struct inode
*cpfile
, __u64 cno
,
116 struct buffer_head
*bh
,
119 return kaddr
+ bh_offset(bh
) + nilfs_cpfile_get_offset(cpfile
, cno
) *
120 NILFS_MDT(cpfile
)->mi_entry_size
;
123 static void nilfs_cpfile_block_init(struct inode
*cpfile
,
124 struct buffer_head
*bh
,
127 struct nilfs_checkpoint
*cp
= kaddr
+ bh_offset(bh
);
128 size_t cpsz
= NILFS_MDT(cpfile
)->mi_entry_size
;
129 int n
= nilfs_cpfile_checkpoints_per_block(cpfile
);
132 nilfs_checkpoint_set_invalid(cp
);
133 cp
= (void *)cp
+ cpsz
;
137 static inline int nilfs_cpfile_get_header_block(struct inode
*cpfile
,
138 struct buffer_head
**bhp
)
140 return nilfs_mdt_get_block(cpfile
, 0, 0, NULL
, bhp
);
143 static inline int nilfs_cpfile_get_checkpoint_block(struct inode
*cpfile
,
146 struct buffer_head
**bhp
)
148 return nilfs_mdt_get_block(cpfile
,
149 nilfs_cpfile_get_blkoff(cpfile
, cno
),
150 create
, nilfs_cpfile_block_init
, bhp
);
154 * nilfs_cpfile_find_checkpoint_block - find and get a buffer on cpfile
155 * @cpfile: inode of cpfile
156 * @start_cno: start checkpoint number (inclusive)
157 * @end_cno: end checkpoint number (inclusive)
158 * @cnop: place to store the next checkpoint number
159 * @bhp: place to store a pointer to buffer_head struct
161 * Return Value: On success, it returns 0. On error, the following negative
162 * error code is returned.
164 * %-ENOMEM - Insufficient memory available.
168 * %-ENOENT - no block exists in the range.
170 static int nilfs_cpfile_find_checkpoint_block(struct inode
*cpfile
,
171 __u64 start_cno
, __u64 end_cno
,
173 struct buffer_head
**bhp
)
175 unsigned long start
, end
, blkoff
;
178 if (unlikely(start_cno
> end_cno
))
181 start
= nilfs_cpfile_get_blkoff(cpfile
, start_cno
);
182 end
= nilfs_cpfile_get_blkoff(cpfile
, end_cno
);
184 ret
= nilfs_mdt_find_block(cpfile
, start
, end
, &blkoff
, bhp
);
186 *cnop
= (blkoff
== start
) ? start_cno
:
187 nilfs_cpfile_first_checkpoint_in_block(cpfile
, blkoff
);
191 static inline int nilfs_cpfile_delete_checkpoint_block(struct inode
*cpfile
,
194 return nilfs_mdt_delete_block(cpfile
,
195 nilfs_cpfile_get_blkoff(cpfile
, cno
));
199 * nilfs_cpfile_get_checkpoint - get a checkpoint
200 * @cpfile: inode of checkpoint file
201 * @cno: checkpoint number
202 * @create: create flag
203 * @cpp: pointer to a checkpoint
204 * @bhp: pointer to a buffer head
206 * Description: nilfs_cpfile_get_checkpoint() acquires the checkpoint
207 * specified by @cno. A new checkpoint will be created if @cno is the current
208 * checkpoint number and @create is nonzero.
210 * Return Value: On success, 0 is returned, and the checkpoint and the
211 * buffer head of the buffer on which the checkpoint is located are stored in
212 * the place pointed by @cpp and @bhp, respectively. On error, one of the
213 * following negative error codes is returned.
217 * %-ENOMEM - Insufficient amount of memory available.
219 * %-ENOENT - No such checkpoint.
221 * %-EINVAL - invalid checkpoint.
223 int nilfs_cpfile_get_checkpoint(struct inode
*cpfile
,
226 struct nilfs_checkpoint
**cpp
,
227 struct buffer_head
**bhp
)
229 struct buffer_head
*header_bh
, *cp_bh
;
230 struct nilfs_cpfile_header
*header
;
231 struct nilfs_checkpoint
*cp
;
235 if (unlikely(cno
< 1 || cno
> nilfs_mdt_cno(cpfile
) ||
236 (cno
< nilfs_mdt_cno(cpfile
) && create
)))
239 down_write(&NILFS_MDT(cpfile
)->mi_sem
);
241 ret
= nilfs_cpfile_get_header_block(cpfile
, &header_bh
);
244 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, create
, &cp_bh
);
247 kaddr
= kmap(cp_bh
->b_page
);
248 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
249 if (nilfs_checkpoint_invalid(cp
)) {
251 kunmap(cp_bh
->b_page
);
256 /* a newly-created checkpoint */
257 nilfs_checkpoint_clear_invalid(cp
);
258 if (!nilfs_cpfile_is_in_first(cpfile
, cno
))
259 nilfs_cpfile_block_add_valid_checkpoints(cpfile
, cp_bh
,
261 mark_buffer_dirty(cp_bh
);
263 kaddr
= kmap_atomic(header_bh
->b_page
);
264 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
,
266 le64_add_cpu(&header
->ch_ncheckpoints
, 1);
267 kunmap_atomic(kaddr
);
268 mark_buffer_dirty(header_bh
);
269 nilfs_mdt_mark_dirty(cpfile
);
280 up_write(&NILFS_MDT(cpfile
)->mi_sem
);
285 * nilfs_cpfile_put_checkpoint - put a checkpoint
286 * @cpfile: inode of checkpoint file
287 * @cno: checkpoint number
290 * Description: nilfs_cpfile_put_checkpoint() releases the checkpoint
291 * specified by @cno. @bh must be the buffer head which has been returned by
292 * a previous call to nilfs_cpfile_get_checkpoint() with @cno.
294 void nilfs_cpfile_put_checkpoint(struct inode
*cpfile
, __u64 cno
,
295 struct buffer_head
*bh
)
302 * nilfs_cpfile_delete_checkpoints - delete checkpoints
303 * @cpfile: inode of checkpoint file
304 * @start: start checkpoint number
305 * @end: end checkpoint numer
307 * Description: nilfs_cpfile_delete_checkpoints() deletes the checkpoints in
308 * the period from @start to @end, excluding @end itself. The checkpoints
309 * which have been already deleted are ignored.
311 * Return Value: On success, 0 is returned. On error, one of the following
312 * negative error codes is returned.
316 * %-ENOMEM - Insufficient amount of memory available.
318 * %-EINVAL - invalid checkpoints.
320 int nilfs_cpfile_delete_checkpoints(struct inode
*cpfile
,
324 struct buffer_head
*header_bh
, *cp_bh
;
325 struct nilfs_cpfile_header
*header
;
326 struct nilfs_checkpoint
*cp
;
327 size_t cpsz
= NILFS_MDT(cpfile
)->mi_entry_size
;
330 unsigned long tnicps
;
331 int ret
, ncps
, nicps
, nss
, count
, i
;
333 if (unlikely(start
== 0 || start
> end
)) {
334 nilfs_msg(cpfile
->i_sb
, KERN_ERR
,
335 "cannot delete checkpoints: invalid range [%llu, %llu)",
336 (unsigned long long)start
, (unsigned long long)end
);
340 down_write(&NILFS_MDT(cpfile
)->mi_sem
);
342 ret
= nilfs_cpfile_get_header_block(cpfile
, &header_bh
);
348 for (cno
= start
; cno
< end
; cno
+= ncps
) {
349 ncps
= nilfs_cpfile_checkpoints_in_block(cpfile
, cno
, end
);
350 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, 0, &cp_bh
);
359 kaddr
= kmap_atomic(cp_bh
->b_page
);
360 cp
= nilfs_cpfile_block_get_checkpoint(
361 cpfile
, cno
, cp_bh
, kaddr
);
363 for (i
= 0; i
< ncps
; i
++, cp
= (void *)cp
+ cpsz
) {
364 if (nilfs_checkpoint_snapshot(cp
)) {
366 } else if (!nilfs_checkpoint_invalid(cp
)) {
367 nilfs_checkpoint_set_invalid(cp
);
373 mark_buffer_dirty(cp_bh
);
374 nilfs_mdt_mark_dirty(cpfile
);
375 if (!nilfs_cpfile_is_in_first(cpfile
, cno
)) {
377 nilfs_cpfile_block_sub_valid_checkpoints(
378 cpfile
, cp_bh
, kaddr
, nicps
);
381 kunmap_atomic(kaddr
);
384 nilfs_cpfile_delete_checkpoint_block(
388 nilfs_msg(cpfile
->i_sb
, KERN_ERR
,
389 "error %d deleting checkpoint block",
396 kunmap_atomic(kaddr
);
401 kaddr
= kmap_atomic(header_bh
->b_page
);
402 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
,
404 le64_add_cpu(&header
->ch_ncheckpoints
, -(u64
)tnicps
);
405 mark_buffer_dirty(header_bh
);
406 nilfs_mdt_mark_dirty(cpfile
);
407 kunmap_atomic(kaddr
);
415 up_write(&NILFS_MDT(cpfile
)->mi_sem
);
419 static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode
*cpfile
,
420 struct nilfs_checkpoint
*cp
,
421 struct nilfs_cpinfo
*ci
)
423 ci
->ci_flags
= le32_to_cpu(cp
->cp_flags
);
424 ci
->ci_cno
= le64_to_cpu(cp
->cp_cno
);
425 ci
->ci_create
= le64_to_cpu(cp
->cp_create
);
426 ci
->ci_nblk_inc
= le64_to_cpu(cp
->cp_nblk_inc
);
427 ci
->ci_inodes_count
= le64_to_cpu(cp
->cp_inodes_count
);
428 ci
->ci_blocks_count
= le64_to_cpu(cp
->cp_blocks_count
);
429 ci
->ci_next
= le64_to_cpu(cp
->cp_snapshot_list
.ssl_next
);
432 static ssize_t
nilfs_cpfile_do_get_cpinfo(struct inode
*cpfile
, __u64
*cnop
,
433 void *buf
, unsigned int cisz
,
436 struct nilfs_checkpoint
*cp
;
437 struct nilfs_cpinfo
*ci
= buf
;
438 struct buffer_head
*bh
;
439 size_t cpsz
= NILFS_MDT(cpfile
)->mi_entry_size
;
440 __u64 cur_cno
= nilfs_mdt_cno(cpfile
), cno
= *cnop
;
446 return -ENOENT
; /* checkpoint number 0 is invalid */
447 down_read(&NILFS_MDT(cpfile
)->mi_sem
);
449 for (n
= 0; n
< nci
; cno
+= ncps
) {
450 ret
= nilfs_cpfile_find_checkpoint_block(
451 cpfile
, cno
, cur_cno
- 1, &cno
, &bh
);
453 if (likely(ret
== -ENOENT
))
457 ncps
= nilfs_cpfile_checkpoints_in_block(cpfile
, cno
, cur_cno
);
459 kaddr
= kmap_atomic(bh
->b_page
);
460 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, bh
, kaddr
);
461 for (i
= 0; i
< ncps
&& n
< nci
; i
++, cp
= (void *)cp
+ cpsz
) {
462 if (!nilfs_checkpoint_invalid(cp
)) {
463 nilfs_cpfile_checkpoint_to_cpinfo(cpfile
, cp
,
465 ci
= (void *)ci
+ cisz
;
469 kunmap_atomic(kaddr
);
475 ci
= (void *)ci
- cisz
;
476 *cnop
= ci
->ci_cno
+ 1;
480 up_read(&NILFS_MDT(cpfile
)->mi_sem
);
484 static ssize_t
nilfs_cpfile_do_get_ssinfo(struct inode
*cpfile
, __u64
*cnop
,
485 void *buf
, unsigned int cisz
,
488 struct buffer_head
*bh
;
489 struct nilfs_cpfile_header
*header
;
490 struct nilfs_checkpoint
*cp
;
491 struct nilfs_cpinfo
*ci
= buf
;
492 __u64 curr
= *cnop
, next
;
493 unsigned long curr_blkoff
, next_blkoff
;
497 down_read(&NILFS_MDT(cpfile
)->mi_sem
);
500 ret
= nilfs_cpfile_get_header_block(cpfile
, &bh
);
503 kaddr
= kmap_atomic(bh
->b_page
);
504 header
= nilfs_cpfile_block_get_header(cpfile
, bh
, kaddr
);
505 curr
= le64_to_cpu(header
->ch_snapshot_list
.ssl_next
);
506 kunmap_atomic(kaddr
);
512 } else if (unlikely(curr
== ~(__u64
)0)) {
517 curr_blkoff
= nilfs_cpfile_get_blkoff(cpfile
, curr
);
518 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, curr
, 0, &bh
);
519 if (unlikely(ret
< 0)) {
521 ret
= 0; /* No snapshots (started from a hole block) */
524 kaddr
= kmap_atomic(bh
->b_page
);
526 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, curr
, bh
, kaddr
);
527 curr
= ~(__u64
)0; /* Terminator */
528 if (unlikely(nilfs_checkpoint_invalid(cp
) ||
529 !nilfs_checkpoint_snapshot(cp
)))
531 nilfs_cpfile_checkpoint_to_cpinfo(cpfile
, cp
, ci
);
532 ci
= (void *)ci
+ cisz
;
534 next
= le64_to_cpu(cp
->cp_snapshot_list
.ssl_next
);
536 break; /* reach end of the snapshot list */
538 next_blkoff
= nilfs_cpfile_get_blkoff(cpfile
, next
);
539 if (curr_blkoff
!= next_blkoff
) {
540 kunmap_atomic(kaddr
);
542 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, next
,
544 if (unlikely(ret
< 0)) {
545 WARN_ON(ret
== -ENOENT
);
548 kaddr
= kmap_atomic(bh
->b_page
);
551 curr_blkoff
= next_blkoff
;
553 kunmap_atomic(kaddr
);
559 up_read(&NILFS_MDT(cpfile
)->mi_sem
);
564 * nilfs_cpfile_get_cpinfo -
571 ssize_t
nilfs_cpfile_get_cpinfo(struct inode
*cpfile
, __u64
*cnop
, int mode
,
572 void *buf
, unsigned int cisz
, size_t nci
)
575 case NILFS_CHECKPOINT
:
576 return nilfs_cpfile_do_get_cpinfo(cpfile
, cnop
, buf
, cisz
, nci
);
578 return nilfs_cpfile_do_get_ssinfo(cpfile
, cnop
, buf
, cisz
, nci
);
585 * nilfs_cpfile_delete_checkpoint -
589 int nilfs_cpfile_delete_checkpoint(struct inode
*cpfile
, __u64 cno
)
591 struct nilfs_cpinfo ci
;
595 nci
= nilfs_cpfile_do_get_cpinfo(cpfile
, &tcno
, &ci
, sizeof(ci
), 1);
598 else if (nci
== 0 || ci
.ci_cno
!= cno
)
600 else if (nilfs_cpinfo_snapshot(&ci
))
603 return nilfs_cpfile_delete_checkpoints(cpfile
, cno
, cno
+ 1);
606 static struct nilfs_snapshot_list
*
607 nilfs_cpfile_block_get_snapshot_list(const struct inode
*cpfile
,
609 struct buffer_head
*bh
,
612 struct nilfs_cpfile_header
*header
;
613 struct nilfs_checkpoint
*cp
;
614 struct nilfs_snapshot_list
*list
;
617 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, bh
, kaddr
);
618 list
= &cp
->cp_snapshot_list
;
620 header
= nilfs_cpfile_block_get_header(cpfile
, bh
, kaddr
);
621 list
= &header
->ch_snapshot_list
;
626 static int nilfs_cpfile_set_snapshot(struct inode
*cpfile
, __u64 cno
)
628 struct buffer_head
*header_bh
, *curr_bh
, *prev_bh
, *cp_bh
;
629 struct nilfs_cpfile_header
*header
;
630 struct nilfs_checkpoint
*cp
;
631 struct nilfs_snapshot_list
*list
;
633 unsigned long curr_blkoff
, prev_blkoff
;
638 return -ENOENT
; /* checkpoint number 0 is invalid */
639 down_write(&NILFS_MDT(cpfile
)->mi_sem
);
641 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, 0, &cp_bh
);
644 kaddr
= kmap_atomic(cp_bh
->b_page
);
645 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
646 if (nilfs_checkpoint_invalid(cp
)) {
648 kunmap_atomic(kaddr
);
651 if (nilfs_checkpoint_snapshot(cp
)) {
653 kunmap_atomic(kaddr
);
656 kunmap_atomic(kaddr
);
658 ret
= nilfs_cpfile_get_header_block(cpfile
, &header_bh
);
661 kaddr
= kmap_atomic(header_bh
->b_page
);
662 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
, kaddr
);
663 list
= &header
->ch_snapshot_list
;
668 prev
= le64_to_cpu(list
->ssl_prev
);
670 prev_blkoff
= nilfs_cpfile_get_blkoff(cpfile
, prev
);
672 if (curr_blkoff
!= prev_blkoff
) {
673 kunmap_atomic(kaddr
);
675 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, curr
,
679 kaddr
= kmap_atomic(curr_bh
->b_page
);
681 curr_blkoff
= prev_blkoff
;
682 cp
= nilfs_cpfile_block_get_checkpoint(
683 cpfile
, curr
, curr_bh
, kaddr
);
684 list
= &cp
->cp_snapshot_list
;
685 prev
= le64_to_cpu(list
->ssl_prev
);
687 kunmap_atomic(kaddr
);
690 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, prev
, 0,
699 kaddr
= kmap_atomic(curr_bh
->b_page
);
700 list
= nilfs_cpfile_block_get_snapshot_list(
701 cpfile
, curr
, curr_bh
, kaddr
);
702 list
->ssl_prev
= cpu_to_le64(cno
);
703 kunmap_atomic(kaddr
);
705 kaddr
= kmap_atomic(cp_bh
->b_page
);
706 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
707 cp
->cp_snapshot_list
.ssl_next
= cpu_to_le64(curr
);
708 cp
->cp_snapshot_list
.ssl_prev
= cpu_to_le64(prev
);
709 nilfs_checkpoint_set_snapshot(cp
);
710 kunmap_atomic(kaddr
);
712 kaddr
= kmap_atomic(prev_bh
->b_page
);
713 list
= nilfs_cpfile_block_get_snapshot_list(
714 cpfile
, prev
, prev_bh
, kaddr
);
715 list
->ssl_next
= cpu_to_le64(cno
);
716 kunmap_atomic(kaddr
);
718 kaddr
= kmap_atomic(header_bh
->b_page
);
719 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
, kaddr
);
720 le64_add_cpu(&header
->ch_nsnapshots
, 1);
721 kunmap_atomic(kaddr
);
723 mark_buffer_dirty(prev_bh
);
724 mark_buffer_dirty(curr_bh
);
725 mark_buffer_dirty(cp_bh
);
726 mark_buffer_dirty(header_bh
);
727 nilfs_mdt_mark_dirty(cpfile
);
741 up_write(&NILFS_MDT(cpfile
)->mi_sem
);
745 static int nilfs_cpfile_clear_snapshot(struct inode
*cpfile
, __u64 cno
)
747 struct buffer_head
*header_bh
, *next_bh
, *prev_bh
, *cp_bh
;
748 struct nilfs_cpfile_header
*header
;
749 struct nilfs_checkpoint
*cp
;
750 struct nilfs_snapshot_list
*list
;
756 return -ENOENT
; /* checkpoint number 0 is invalid */
757 down_write(&NILFS_MDT(cpfile
)->mi_sem
);
759 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, 0, &cp_bh
);
762 kaddr
= kmap_atomic(cp_bh
->b_page
);
763 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
764 if (nilfs_checkpoint_invalid(cp
)) {
766 kunmap_atomic(kaddr
);
769 if (!nilfs_checkpoint_snapshot(cp
)) {
771 kunmap_atomic(kaddr
);
775 list
= &cp
->cp_snapshot_list
;
776 next
= le64_to_cpu(list
->ssl_next
);
777 prev
= le64_to_cpu(list
->ssl_prev
);
778 kunmap_atomic(kaddr
);
780 ret
= nilfs_cpfile_get_header_block(cpfile
, &header_bh
);
784 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, next
, 0,
793 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, prev
, 0,
802 kaddr
= kmap_atomic(next_bh
->b_page
);
803 list
= nilfs_cpfile_block_get_snapshot_list(
804 cpfile
, next
, next_bh
, kaddr
);
805 list
->ssl_prev
= cpu_to_le64(prev
);
806 kunmap_atomic(kaddr
);
808 kaddr
= kmap_atomic(prev_bh
->b_page
);
809 list
= nilfs_cpfile_block_get_snapshot_list(
810 cpfile
, prev
, prev_bh
, kaddr
);
811 list
->ssl_next
= cpu_to_le64(next
);
812 kunmap_atomic(kaddr
);
814 kaddr
= kmap_atomic(cp_bh
->b_page
);
815 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
816 cp
->cp_snapshot_list
.ssl_next
= cpu_to_le64(0);
817 cp
->cp_snapshot_list
.ssl_prev
= cpu_to_le64(0);
818 nilfs_checkpoint_clear_snapshot(cp
);
819 kunmap_atomic(kaddr
);
821 kaddr
= kmap_atomic(header_bh
->b_page
);
822 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
, kaddr
);
823 le64_add_cpu(&header
->ch_nsnapshots
, -1);
824 kunmap_atomic(kaddr
);
826 mark_buffer_dirty(next_bh
);
827 mark_buffer_dirty(prev_bh
);
828 mark_buffer_dirty(cp_bh
);
829 mark_buffer_dirty(header_bh
);
830 nilfs_mdt_mark_dirty(cpfile
);
844 up_write(&NILFS_MDT(cpfile
)->mi_sem
);
849 * nilfs_cpfile_is_snapshot -
850 * @cpfile: inode of checkpoint file
851 * @cno: checkpoint number
855 * Return Value: On success, 1 is returned if the checkpoint specified by
856 * @cno is a snapshot, or 0 if not. On error, one of the following negative
857 * error codes is returned.
861 * %-ENOMEM - Insufficient amount of memory available.
863 * %-ENOENT - No such checkpoint.
865 int nilfs_cpfile_is_snapshot(struct inode
*cpfile
, __u64 cno
)
867 struct buffer_head
*bh
;
868 struct nilfs_checkpoint
*cp
;
873 * CP number is invalid if it's zero or larger than the
874 * largest existing one.
876 if (cno
== 0 || cno
>= nilfs_mdt_cno(cpfile
))
878 down_read(&NILFS_MDT(cpfile
)->mi_sem
);
880 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, 0, &bh
);
883 kaddr
= kmap_atomic(bh
->b_page
);
884 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, bh
, kaddr
);
885 if (nilfs_checkpoint_invalid(cp
))
888 ret
= nilfs_checkpoint_snapshot(cp
);
889 kunmap_atomic(kaddr
);
893 up_read(&NILFS_MDT(cpfile
)->mi_sem
);
898 * nilfs_cpfile_change_cpmode - change checkpoint mode
899 * @cpfile: inode of checkpoint file
900 * @cno: checkpoint number
901 * @status: mode of checkpoint
903 * Description: nilfs_change_cpmode() changes the mode of the checkpoint
904 * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT.
906 * Return Value: On success, 0 is returned. On error, one of the following
907 * negative error codes is returned.
911 * %-ENOMEM - Insufficient amount of memory available.
913 * %-ENOENT - No such checkpoint.
915 int nilfs_cpfile_change_cpmode(struct inode
*cpfile
, __u64 cno
, int mode
)
920 case NILFS_CHECKPOINT
:
921 if (nilfs_checkpoint_is_mounted(cpfile
->i_sb
, cno
))
923 * Current implementation does not have to protect
924 * plain read-only mounts since they are exclusive
925 * with a read/write mount and are protected from the
930 ret
= nilfs_cpfile_clear_snapshot(cpfile
, cno
);
933 return nilfs_cpfile_set_snapshot(cpfile
, cno
);
940 * nilfs_cpfile_get_stat - get checkpoint statistics
941 * @cpfile: inode of checkpoint file
942 * @stat: pointer to a structure of checkpoint statistics
944 * Description: nilfs_cpfile_get_stat() returns information about checkpoints.
946 * Return Value: On success, 0 is returned, and checkpoints information is
947 * stored in the place pointed by @stat. On error, one of the following
948 * negative error codes is returned.
952 * %-ENOMEM - Insufficient amount of memory available.
954 int nilfs_cpfile_get_stat(struct inode
*cpfile
, struct nilfs_cpstat
*cpstat
)
956 struct buffer_head
*bh
;
957 struct nilfs_cpfile_header
*header
;
961 down_read(&NILFS_MDT(cpfile
)->mi_sem
);
963 ret
= nilfs_cpfile_get_header_block(cpfile
, &bh
);
966 kaddr
= kmap_atomic(bh
->b_page
);
967 header
= nilfs_cpfile_block_get_header(cpfile
, bh
, kaddr
);
968 cpstat
->cs_cno
= nilfs_mdt_cno(cpfile
);
969 cpstat
->cs_ncps
= le64_to_cpu(header
->ch_ncheckpoints
);
970 cpstat
->cs_nsss
= le64_to_cpu(header
->ch_nsnapshots
);
971 kunmap_atomic(kaddr
);
975 up_read(&NILFS_MDT(cpfile
)->mi_sem
);
980 * nilfs_cpfile_read - read or get cpfile inode
981 * @sb: super block instance
982 * @cpsize: size of a checkpoint entry
983 * @raw_inode: on-disk cpfile inode
984 * @inodep: buffer to store the inode
986 int nilfs_cpfile_read(struct super_block
*sb
, size_t cpsize
,
987 struct nilfs_inode
*raw_inode
, struct inode
**inodep
)
989 struct inode
*cpfile
;
992 if (cpsize
> sb
->s_blocksize
) {
993 nilfs_msg(sb
, KERN_ERR
,
994 "too large checkpoint size: %zu bytes", cpsize
);
996 } else if (cpsize
< NILFS_MIN_CHECKPOINT_SIZE
) {
997 nilfs_msg(sb
, KERN_ERR
,
998 "too small checkpoint size: %zu bytes", cpsize
);
1002 cpfile
= nilfs_iget_locked(sb
, NULL
, NILFS_CPFILE_INO
);
1003 if (unlikely(!cpfile
))
1005 if (!(cpfile
->i_state
& I_NEW
))
1008 err
= nilfs_mdt_init(cpfile
, NILFS_MDT_GFP
, 0);
1012 nilfs_mdt_set_entry_size(cpfile
, cpsize
,
1013 sizeof(struct nilfs_cpfile_header
));
1015 err
= nilfs_read_inode_common(cpfile
, raw_inode
);
1019 unlock_new_inode(cpfile
);
1024 iget_failed(cpfile
);