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>
24 #include <linux/nilfs2_fs.h>
29 static inline unsigned long
30 nilfs_cpfile_checkpoints_per_block(const struct inode
*cpfile
)
32 return NILFS_MDT(cpfile
)->mi_entries_per_block
;
35 /* block number from the beginning of the file */
37 nilfs_cpfile_get_blkoff(const struct inode
*cpfile
, __u64 cno
)
39 __u64 tcno
= cno
+ NILFS_MDT(cpfile
)->mi_first_entry_offset
- 1;
41 do_div(tcno
, nilfs_cpfile_checkpoints_per_block(cpfile
));
42 return (unsigned long)tcno
;
47 nilfs_cpfile_get_offset(const struct inode
*cpfile
, __u64 cno
)
49 __u64 tcno
= cno
+ NILFS_MDT(cpfile
)->mi_first_entry_offset
- 1;
51 return do_div(tcno
, nilfs_cpfile_checkpoints_per_block(cpfile
));
54 static __u64
nilfs_cpfile_first_checkpoint_in_block(const struct inode
*cpfile
,
57 return (__u64
)nilfs_cpfile_checkpoints_per_block(cpfile
) * blkoff
58 + 1 - NILFS_MDT(cpfile
)->mi_first_entry_offset
;
62 nilfs_cpfile_checkpoints_in_block(const struct inode
*cpfile
,
67 nilfs_cpfile_checkpoints_per_block(cpfile
) -
68 nilfs_cpfile_get_offset(cpfile
, curr
),
72 static inline int nilfs_cpfile_is_in_first(const struct inode
*cpfile
,
75 return nilfs_cpfile_get_blkoff(cpfile
, cno
) == 0;
79 nilfs_cpfile_block_add_valid_checkpoints(const struct inode
*cpfile
,
80 struct buffer_head
*bh
,
84 struct nilfs_checkpoint
*cp
= kaddr
+ bh_offset(bh
);
87 count
= le32_to_cpu(cp
->cp_checkpoints_count
) + n
;
88 cp
->cp_checkpoints_count
= cpu_to_le32(count
);
93 nilfs_cpfile_block_sub_valid_checkpoints(const struct inode
*cpfile
,
94 struct buffer_head
*bh
,
98 struct nilfs_checkpoint
*cp
= kaddr
+ bh_offset(bh
);
101 WARN_ON(le32_to_cpu(cp
->cp_checkpoints_count
) < n
);
102 count
= le32_to_cpu(cp
->cp_checkpoints_count
) - n
;
103 cp
->cp_checkpoints_count
= cpu_to_le32(count
);
107 static inline struct nilfs_cpfile_header
*
108 nilfs_cpfile_block_get_header(const struct inode
*cpfile
,
109 struct buffer_head
*bh
,
112 return kaddr
+ bh_offset(bh
);
115 static struct nilfs_checkpoint
*
116 nilfs_cpfile_block_get_checkpoint(const struct inode
*cpfile
, __u64 cno
,
117 struct buffer_head
*bh
,
120 return kaddr
+ bh_offset(bh
) + nilfs_cpfile_get_offset(cpfile
, cno
) *
121 NILFS_MDT(cpfile
)->mi_entry_size
;
124 static void nilfs_cpfile_block_init(struct inode
*cpfile
,
125 struct buffer_head
*bh
,
128 struct nilfs_checkpoint
*cp
= kaddr
+ bh_offset(bh
);
129 size_t cpsz
= NILFS_MDT(cpfile
)->mi_entry_size
;
130 int n
= nilfs_cpfile_checkpoints_per_block(cpfile
);
133 nilfs_checkpoint_set_invalid(cp
);
134 cp
= (void *)cp
+ cpsz
;
138 static inline int nilfs_cpfile_get_header_block(struct inode
*cpfile
,
139 struct buffer_head
**bhp
)
141 return nilfs_mdt_get_block(cpfile
, 0, 0, NULL
, bhp
);
144 static inline int nilfs_cpfile_get_checkpoint_block(struct inode
*cpfile
,
147 struct buffer_head
**bhp
)
149 return nilfs_mdt_get_block(cpfile
,
150 nilfs_cpfile_get_blkoff(cpfile
, cno
),
151 create
, nilfs_cpfile_block_init
, bhp
);
155 * nilfs_cpfile_find_checkpoint_block - find and get a buffer on cpfile
156 * @cpfile: inode of cpfile
157 * @start_cno: start checkpoint number (inclusive)
158 * @end_cno: end checkpoint number (inclusive)
159 * @cnop: place to store the next checkpoint number
160 * @bhp: place to store a pointer to buffer_head struct
162 * Return Value: On success, it returns 0. On error, the following negative
163 * error code is returned.
165 * %-ENOMEM - Insufficient memory available.
169 * %-ENOENT - no block exists in the range.
171 static int nilfs_cpfile_find_checkpoint_block(struct inode
*cpfile
,
172 __u64 start_cno
, __u64 end_cno
,
174 struct buffer_head
**bhp
)
176 unsigned long start
, end
, blkoff
;
179 if (unlikely(start_cno
> end_cno
))
182 start
= nilfs_cpfile_get_blkoff(cpfile
, start_cno
);
183 end
= nilfs_cpfile_get_blkoff(cpfile
, end_cno
);
185 ret
= nilfs_mdt_find_block(cpfile
, start
, end
, &blkoff
, bhp
);
187 *cnop
= (blkoff
== start
) ? start_cno
:
188 nilfs_cpfile_first_checkpoint_in_block(cpfile
, blkoff
);
192 static inline int nilfs_cpfile_delete_checkpoint_block(struct inode
*cpfile
,
195 return nilfs_mdt_delete_block(cpfile
,
196 nilfs_cpfile_get_blkoff(cpfile
, cno
));
200 * nilfs_cpfile_get_checkpoint - get a checkpoint
201 * @cpfile: inode of checkpoint file
202 * @cno: checkpoint number
203 * @create: create flag
204 * @cpp: pointer to a checkpoint
205 * @bhp: pointer to a buffer head
207 * Description: nilfs_cpfile_get_checkpoint() acquires the checkpoint
208 * specified by @cno. A new checkpoint will be created if @cno is the current
209 * checkpoint number and @create is nonzero.
211 * Return Value: On success, 0 is returned, and the checkpoint and the
212 * buffer head of the buffer on which the checkpoint is located are stored in
213 * the place pointed by @cpp and @bhp, respectively. On error, one of the
214 * following negative error codes is returned.
218 * %-ENOMEM - Insufficient amount of memory available.
220 * %-ENOENT - No such checkpoint.
222 * %-EINVAL - invalid checkpoint.
224 int nilfs_cpfile_get_checkpoint(struct inode
*cpfile
,
227 struct nilfs_checkpoint
**cpp
,
228 struct buffer_head
**bhp
)
230 struct buffer_head
*header_bh
, *cp_bh
;
231 struct nilfs_cpfile_header
*header
;
232 struct nilfs_checkpoint
*cp
;
236 if (unlikely(cno
< 1 || cno
> nilfs_mdt_cno(cpfile
) ||
237 (cno
< nilfs_mdt_cno(cpfile
) && create
)))
240 down_write(&NILFS_MDT(cpfile
)->mi_sem
);
242 ret
= nilfs_cpfile_get_header_block(cpfile
, &header_bh
);
245 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, create
, &cp_bh
);
248 kaddr
= kmap(cp_bh
->b_page
);
249 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
250 if (nilfs_checkpoint_invalid(cp
)) {
252 kunmap(cp_bh
->b_page
);
257 /* a newly-created checkpoint */
258 nilfs_checkpoint_clear_invalid(cp
);
259 if (!nilfs_cpfile_is_in_first(cpfile
, cno
))
260 nilfs_cpfile_block_add_valid_checkpoints(cpfile
, cp_bh
,
262 mark_buffer_dirty(cp_bh
);
264 kaddr
= kmap_atomic(header_bh
->b_page
);
265 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
,
267 le64_add_cpu(&header
->ch_ncheckpoints
, 1);
268 kunmap_atomic(kaddr
);
269 mark_buffer_dirty(header_bh
);
270 nilfs_mdt_mark_dirty(cpfile
);
281 up_write(&NILFS_MDT(cpfile
)->mi_sem
);
286 * nilfs_cpfile_put_checkpoint - put a checkpoint
287 * @cpfile: inode of checkpoint file
288 * @cno: checkpoint number
291 * Description: nilfs_cpfile_put_checkpoint() releases the checkpoint
292 * specified by @cno. @bh must be the buffer head which has been returned by
293 * a previous call to nilfs_cpfile_get_checkpoint() with @cno.
295 void nilfs_cpfile_put_checkpoint(struct inode
*cpfile
, __u64 cno
,
296 struct buffer_head
*bh
)
303 * nilfs_cpfile_delete_checkpoints - delete checkpoints
304 * @cpfile: inode of checkpoint file
305 * @start: start checkpoint number
306 * @end: end checkpoint numer
308 * Description: nilfs_cpfile_delete_checkpoints() deletes the checkpoints in
309 * the period from @start to @end, excluding @end itself. The checkpoints
310 * which have been already deleted are ignored.
312 * Return Value: On success, 0 is returned. On error, one of the following
313 * negative error codes is returned.
317 * %-ENOMEM - Insufficient amount of memory available.
319 * %-EINVAL - invalid checkpoints.
321 int nilfs_cpfile_delete_checkpoints(struct inode
*cpfile
,
325 struct buffer_head
*header_bh
, *cp_bh
;
326 struct nilfs_cpfile_header
*header
;
327 struct nilfs_checkpoint
*cp
;
328 size_t cpsz
= NILFS_MDT(cpfile
)->mi_entry_size
;
331 unsigned long tnicps
;
332 int ret
, ncps
, nicps
, nss
, count
, i
;
334 if (unlikely(start
== 0 || start
> end
)) {
335 printk(KERN_ERR
"%s: invalid range of checkpoint numbers: "
336 "[%llu, %llu)\n", __func__
,
337 (unsigned long long)start
, (unsigned long long)end
);
341 down_write(&NILFS_MDT(cpfile
)->mi_sem
);
343 ret
= nilfs_cpfile_get_header_block(cpfile
, &header_bh
);
349 for (cno
= start
; cno
< end
; cno
+= ncps
) {
350 ncps
= nilfs_cpfile_checkpoints_in_block(cpfile
, cno
, end
);
351 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, 0, &cp_bh
);
360 kaddr
= kmap_atomic(cp_bh
->b_page
);
361 cp
= nilfs_cpfile_block_get_checkpoint(
362 cpfile
, cno
, cp_bh
, kaddr
);
364 for (i
= 0; i
< ncps
; i
++, cp
= (void *)cp
+ cpsz
) {
365 if (nilfs_checkpoint_snapshot(cp
)) {
367 } else if (!nilfs_checkpoint_invalid(cp
)) {
368 nilfs_checkpoint_set_invalid(cp
);
374 mark_buffer_dirty(cp_bh
);
375 nilfs_mdt_mark_dirty(cpfile
);
376 if (!nilfs_cpfile_is_in_first(cpfile
, cno
)) {
378 nilfs_cpfile_block_sub_valid_checkpoints(
379 cpfile
, cp_bh
, kaddr
, nicps
);
382 kunmap_atomic(kaddr
);
385 nilfs_cpfile_delete_checkpoint_block(
390 "%s: cannot delete block\n",
397 kunmap_atomic(kaddr
);
402 kaddr
= kmap_atomic(header_bh
->b_page
);
403 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
,
405 le64_add_cpu(&header
->ch_ncheckpoints
, -(u64
)tnicps
);
406 mark_buffer_dirty(header_bh
);
407 nilfs_mdt_mark_dirty(cpfile
);
408 kunmap_atomic(kaddr
);
416 up_write(&NILFS_MDT(cpfile
)->mi_sem
);
420 static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode
*cpfile
,
421 struct nilfs_checkpoint
*cp
,
422 struct nilfs_cpinfo
*ci
)
424 ci
->ci_flags
= le32_to_cpu(cp
->cp_flags
);
425 ci
->ci_cno
= le64_to_cpu(cp
->cp_cno
);
426 ci
->ci_create
= le64_to_cpu(cp
->cp_create
);
427 ci
->ci_nblk_inc
= le64_to_cpu(cp
->cp_nblk_inc
);
428 ci
->ci_inodes_count
= le64_to_cpu(cp
->cp_inodes_count
);
429 ci
->ci_blocks_count
= le64_to_cpu(cp
->cp_blocks_count
);
430 ci
->ci_next
= le64_to_cpu(cp
->cp_snapshot_list
.ssl_next
);
433 static ssize_t
nilfs_cpfile_do_get_cpinfo(struct inode
*cpfile
, __u64
*cnop
,
434 void *buf
, unsigned int cisz
,
437 struct nilfs_checkpoint
*cp
;
438 struct nilfs_cpinfo
*ci
= buf
;
439 struct buffer_head
*bh
;
440 size_t cpsz
= NILFS_MDT(cpfile
)->mi_entry_size
;
441 __u64 cur_cno
= nilfs_mdt_cno(cpfile
), cno
= *cnop
;
447 return -ENOENT
; /* checkpoint number 0 is invalid */
448 down_read(&NILFS_MDT(cpfile
)->mi_sem
);
450 for (n
= 0; n
< nci
; cno
+= ncps
) {
451 ret
= nilfs_cpfile_find_checkpoint_block(
452 cpfile
, cno
, cur_cno
- 1, &cno
, &bh
);
454 if (likely(ret
== -ENOENT
))
458 ncps
= nilfs_cpfile_checkpoints_in_block(cpfile
, cno
, cur_cno
);
460 kaddr
= kmap_atomic(bh
->b_page
);
461 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, bh
, kaddr
);
462 for (i
= 0; i
< ncps
&& n
< nci
; i
++, cp
= (void *)cp
+ cpsz
) {
463 if (!nilfs_checkpoint_invalid(cp
)) {
464 nilfs_cpfile_checkpoint_to_cpinfo(cpfile
, cp
,
466 ci
= (void *)ci
+ cisz
;
470 kunmap_atomic(kaddr
);
476 ci
= (void *)ci
- cisz
;
477 *cnop
= ci
->ci_cno
+ 1;
481 up_read(&NILFS_MDT(cpfile
)->mi_sem
);
485 static ssize_t
nilfs_cpfile_do_get_ssinfo(struct inode
*cpfile
, __u64
*cnop
,
486 void *buf
, unsigned int cisz
,
489 struct buffer_head
*bh
;
490 struct nilfs_cpfile_header
*header
;
491 struct nilfs_checkpoint
*cp
;
492 struct nilfs_cpinfo
*ci
= buf
;
493 __u64 curr
= *cnop
, next
;
494 unsigned long curr_blkoff
, next_blkoff
;
498 down_read(&NILFS_MDT(cpfile
)->mi_sem
);
501 ret
= nilfs_cpfile_get_header_block(cpfile
, &bh
);
504 kaddr
= kmap_atomic(bh
->b_page
);
505 header
= nilfs_cpfile_block_get_header(cpfile
, bh
, kaddr
);
506 curr
= le64_to_cpu(header
->ch_snapshot_list
.ssl_next
);
507 kunmap_atomic(kaddr
);
513 } else if (unlikely(curr
== ~(__u64
)0)) {
518 curr_blkoff
= nilfs_cpfile_get_blkoff(cpfile
, curr
);
519 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, curr
, 0, &bh
);
520 if (unlikely(ret
< 0)) {
522 ret
= 0; /* No snapshots (started from a hole block) */
525 kaddr
= kmap_atomic(bh
->b_page
);
527 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, curr
, bh
, kaddr
);
528 curr
= ~(__u64
)0; /* Terminator */
529 if (unlikely(nilfs_checkpoint_invalid(cp
) ||
530 !nilfs_checkpoint_snapshot(cp
)))
532 nilfs_cpfile_checkpoint_to_cpinfo(cpfile
, cp
, ci
);
533 ci
= (void *)ci
+ cisz
;
535 next
= le64_to_cpu(cp
->cp_snapshot_list
.ssl_next
);
537 break; /* reach end of the snapshot list */
539 next_blkoff
= nilfs_cpfile_get_blkoff(cpfile
, next
);
540 if (curr_blkoff
!= next_blkoff
) {
541 kunmap_atomic(kaddr
);
543 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, next
,
545 if (unlikely(ret
< 0)) {
546 WARN_ON(ret
== -ENOENT
);
549 kaddr
= kmap_atomic(bh
->b_page
);
552 curr_blkoff
= next_blkoff
;
554 kunmap_atomic(kaddr
);
560 up_read(&NILFS_MDT(cpfile
)->mi_sem
);
565 * nilfs_cpfile_get_cpinfo -
572 ssize_t
nilfs_cpfile_get_cpinfo(struct inode
*cpfile
, __u64
*cnop
, int mode
,
573 void *buf
, unsigned int cisz
, size_t nci
)
576 case NILFS_CHECKPOINT
:
577 return nilfs_cpfile_do_get_cpinfo(cpfile
, cnop
, buf
, cisz
, nci
);
579 return nilfs_cpfile_do_get_ssinfo(cpfile
, cnop
, buf
, cisz
, nci
);
586 * nilfs_cpfile_delete_checkpoint -
590 int nilfs_cpfile_delete_checkpoint(struct inode
*cpfile
, __u64 cno
)
592 struct nilfs_cpinfo ci
;
596 nci
= nilfs_cpfile_do_get_cpinfo(cpfile
, &tcno
, &ci
, sizeof(ci
), 1);
599 else if (nci
== 0 || ci
.ci_cno
!= cno
)
601 else if (nilfs_cpinfo_snapshot(&ci
))
604 return nilfs_cpfile_delete_checkpoints(cpfile
, cno
, cno
+ 1);
607 static struct nilfs_snapshot_list
*
608 nilfs_cpfile_block_get_snapshot_list(const struct inode
*cpfile
,
610 struct buffer_head
*bh
,
613 struct nilfs_cpfile_header
*header
;
614 struct nilfs_checkpoint
*cp
;
615 struct nilfs_snapshot_list
*list
;
618 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, bh
, kaddr
);
619 list
= &cp
->cp_snapshot_list
;
621 header
= nilfs_cpfile_block_get_header(cpfile
, bh
, kaddr
);
622 list
= &header
->ch_snapshot_list
;
627 static int nilfs_cpfile_set_snapshot(struct inode
*cpfile
, __u64 cno
)
629 struct buffer_head
*header_bh
, *curr_bh
, *prev_bh
, *cp_bh
;
630 struct nilfs_cpfile_header
*header
;
631 struct nilfs_checkpoint
*cp
;
632 struct nilfs_snapshot_list
*list
;
634 unsigned long curr_blkoff
, prev_blkoff
;
639 return -ENOENT
; /* checkpoint number 0 is invalid */
640 down_write(&NILFS_MDT(cpfile
)->mi_sem
);
642 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, 0, &cp_bh
);
645 kaddr
= kmap_atomic(cp_bh
->b_page
);
646 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
647 if (nilfs_checkpoint_invalid(cp
)) {
649 kunmap_atomic(kaddr
);
652 if (nilfs_checkpoint_snapshot(cp
)) {
654 kunmap_atomic(kaddr
);
657 kunmap_atomic(kaddr
);
659 ret
= nilfs_cpfile_get_header_block(cpfile
, &header_bh
);
662 kaddr
= kmap_atomic(header_bh
->b_page
);
663 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
, kaddr
);
664 list
= &header
->ch_snapshot_list
;
669 prev
= le64_to_cpu(list
->ssl_prev
);
671 prev_blkoff
= nilfs_cpfile_get_blkoff(cpfile
, prev
);
673 if (curr_blkoff
!= prev_blkoff
) {
674 kunmap_atomic(kaddr
);
676 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, curr
,
680 kaddr
= kmap_atomic(curr_bh
->b_page
);
682 curr_blkoff
= prev_blkoff
;
683 cp
= nilfs_cpfile_block_get_checkpoint(
684 cpfile
, curr
, curr_bh
, kaddr
);
685 list
= &cp
->cp_snapshot_list
;
686 prev
= le64_to_cpu(list
->ssl_prev
);
688 kunmap_atomic(kaddr
);
691 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, prev
, 0,
700 kaddr
= kmap_atomic(curr_bh
->b_page
);
701 list
= nilfs_cpfile_block_get_snapshot_list(
702 cpfile
, curr
, curr_bh
, kaddr
);
703 list
->ssl_prev
= cpu_to_le64(cno
);
704 kunmap_atomic(kaddr
);
706 kaddr
= kmap_atomic(cp_bh
->b_page
);
707 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
708 cp
->cp_snapshot_list
.ssl_next
= cpu_to_le64(curr
);
709 cp
->cp_snapshot_list
.ssl_prev
= cpu_to_le64(prev
);
710 nilfs_checkpoint_set_snapshot(cp
);
711 kunmap_atomic(kaddr
);
713 kaddr
= kmap_atomic(prev_bh
->b_page
);
714 list
= nilfs_cpfile_block_get_snapshot_list(
715 cpfile
, prev
, prev_bh
, kaddr
);
716 list
->ssl_next
= cpu_to_le64(cno
);
717 kunmap_atomic(kaddr
);
719 kaddr
= kmap_atomic(header_bh
->b_page
);
720 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
, kaddr
);
721 le64_add_cpu(&header
->ch_nsnapshots
, 1);
722 kunmap_atomic(kaddr
);
724 mark_buffer_dirty(prev_bh
);
725 mark_buffer_dirty(curr_bh
);
726 mark_buffer_dirty(cp_bh
);
727 mark_buffer_dirty(header_bh
);
728 nilfs_mdt_mark_dirty(cpfile
);
742 up_write(&NILFS_MDT(cpfile
)->mi_sem
);
746 static int nilfs_cpfile_clear_snapshot(struct inode
*cpfile
, __u64 cno
)
748 struct buffer_head
*header_bh
, *next_bh
, *prev_bh
, *cp_bh
;
749 struct nilfs_cpfile_header
*header
;
750 struct nilfs_checkpoint
*cp
;
751 struct nilfs_snapshot_list
*list
;
757 return -ENOENT
; /* checkpoint number 0 is invalid */
758 down_write(&NILFS_MDT(cpfile
)->mi_sem
);
760 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, 0, &cp_bh
);
763 kaddr
= kmap_atomic(cp_bh
->b_page
);
764 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
765 if (nilfs_checkpoint_invalid(cp
)) {
767 kunmap_atomic(kaddr
);
770 if (!nilfs_checkpoint_snapshot(cp
)) {
772 kunmap_atomic(kaddr
);
776 list
= &cp
->cp_snapshot_list
;
777 next
= le64_to_cpu(list
->ssl_next
);
778 prev
= le64_to_cpu(list
->ssl_prev
);
779 kunmap_atomic(kaddr
);
781 ret
= nilfs_cpfile_get_header_block(cpfile
, &header_bh
);
785 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, next
, 0,
794 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, prev
, 0,
803 kaddr
= kmap_atomic(next_bh
->b_page
);
804 list
= nilfs_cpfile_block_get_snapshot_list(
805 cpfile
, next
, next_bh
, kaddr
);
806 list
->ssl_prev
= cpu_to_le64(prev
);
807 kunmap_atomic(kaddr
);
809 kaddr
= kmap_atomic(prev_bh
->b_page
);
810 list
= nilfs_cpfile_block_get_snapshot_list(
811 cpfile
, prev
, prev_bh
, kaddr
);
812 list
->ssl_next
= cpu_to_le64(next
);
813 kunmap_atomic(kaddr
);
815 kaddr
= kmap_atomic(cp_bh
->b_page
);
816 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, cp_bh
, kaddr
);
817 cp
->cp_snapshot_list
.ssl_next
= cpu_to_le64(0);
818 cp
->cp_snapshot_list
.ssl_prev
= cpu_to_le64(0);
819 nilfs_checkpoint_clear_snapshot(cp
);
820 kunmap_atomic(kaddr
);
822 kaddr
= kmap_atomic(header_bh
->b_page
);
823 header
= nilfs_cpfile_block_get_header(cpfile
, header_bh
, kaddr
);
824 le64_add_cpu(&header
->ch_nsnapshots
, -1);
825 kunmap_atomic(kaddr
);
827 mark_buffer_dirty(next_bh
);
828 mark_buffer_dirty(prev_bh
);
829 mark_buffer_dirty(cp_bh
);
830 mark_buffer_dirty(header_bh
);
831 nilfs_mdt_mark_dirty(cpfile
);
845 up_write(&NILFS_MDT(cpfile
)->mi_sem
);
850 * nilfs_cpfile_is_snapshot -
851 * @cpfile: inode of checkpoint file
852 * @cno: checkpoint number
856 * Return Value: On success, 1 is returned if the checkpoint specified by
857 * @cno is a snapshot, or 0 if not. On error, one of the following negative
858 * error codes is returned.
862 * %-ENOMEM - Insufficient amount of memory available.
864 * %-ENOENT - No such checkpoint.
866 int nilfs_cpfile_is_snapshot(struct inode
*cpfile
, __u64 cno
)
868 struct buffer_head
*bh
;
869 struct nilfs_checkpoint
*cp
;
874 * CP number is invalid if it's zero or larger than the
875 * largest existing one.
877 if (cno
== 0 || cno
>= nilfs_mdt_cno(cpfile
))
879 down_read(&NILFS_MDT(cpfile
)->mi_sem
);
881 ret
= nilfs_cpfile_get_checkpoint_block(cpfile
, cno
, 0, &bh
);
884 kaddr
= kmap_atomic(bh
->b_page
);
885 cp
= nilfs_cpfile_block_get_checkpoint(cpfile
, cno
, bh
, kaddr
);
886 if (nilfs_checkpoint_invalid(cp
))
889 ret
= nilfs_checkpoint_snapshot(cp
);
890 kunmap_atomic(kaddr
);
894 up_read(&NILFS_MDT(cpfile
)->mi_sem
);
899 * nilfs_cpfile_change_cpmode - change checkpoint mode
900 * @cpfile: inode of checkpoint file
901 * @cno: checkpoint number
902 * @status: mode of checkpoint
904 * Description: nilfs_change_cpmode() changes the mode of the checkpoint
905 * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT.
907 * Return Value: On success, 0 is returned. On error, one of the following
908 * negative error codes is returned.
912 * %-ENOMEM - Insufficient amount of memory available.
914 * %-ENOENT - No such checkpoint.
916 int nilfs_cpfile_change_cpmode(struct inode
*cpfile
, __u64 cno
, int mode
)
921 case NILFS_CHECKPOINT
:
922 if (nilfs_checkpoint_is_mounted(cpfile
->i_sb
, cno
))
924 * Current implementation does not have to protect
925 * plain read-only mounts since they are exclusive
926 * with a read/write mount and are protected from the
931 ret
= nilfs_cpfile_clear_snapshot(cpfile
, cno
);
934 return nilfs_cpfile_set_snapshot(cpfile
, cno
);
941 * nilfs_cpfile_get_stat - get checkpoint statistics
942 * @cpfile: inode of checkpoint file
943 * @stat: pointer to a structure of checkpoint statistics
945 * Description: nilfs_cpfile_get_stat() returns information about checkpoints.
947 * Return Value: On success, 0 is returned, and checkpoints information is
948 * stored in the place pointed by @stat. On error, one of the following
949 * negative error codes is returned.
953 * %-ENOMEM - Insufficient amount of memory available.
955 int nilfs_cpfile_get_stat(struct inode
*cpfile
, struct nilfs_cpstat
*cpstat
)
957 struct buffer_head
*bh
;
958 struct nilfs_cpfile_header
*header
;
962 down_read(&NILFS_MDT(cpfile
)->mi_sem
);
964 ret
= nilfs_cpfile_get_header_block(cpfile
, &bh
);
967 kaddr
= kmap_atomic(bh
->b_page
);
968 header
= nilfs_cpfile_block_get_header(cpfile
, bh
, kaddr
);
969 cpstat
->cs_cno
= nilfs_mdt_cno(cpfile
);
970 cpstat
->cs_ncps
= le64_to_cpu(header
->ch_ncheckpoints
);
971 cpstat
->cs_nsss
= le64_to_cpu(header
->ch_nsnapshots
);
972 kunmap_atomic(kaddr
);
976 up_read(&NILFS_MDT(cpfile
)->mi_sem
);
981 * nilfs_cpfile_read - read or get cpfile inode
982 * @sb: super block instance
983 * @cpsize: size of a checkpoint entry
984 * @raw_inode: on-disk cpfile inode
985 * @inodep: buffer to store the inode
987 int nilfs_cpfile_read(struct super_block
*sb
, size_t cpsize
,
988 struct nilfs_inode
*raw_inode
, struct inode
**inodep
)
990 struct inode
*cpfile
;
993 if (cpsize
> sb
->s_blocksize
) {
995 "NILFS: too large checkpoint size: %zu bytes.\n",
998 } else if (cpsize
< NILFS_MIN_CHECKPOINT_SIZE
) {
1000 "NILFS: too small checkpoint size: %zu bytes.\n",
1005 cpfile
= nilfs_iget_locked(sb
, NULL
, NILFS_CPFILE_INO
);
1006 if (unlikely(!cpfile
))
1008 if (!(cpfile
->i_state
& I_NEW
))
1011 err
= nilfs_mdt_init(cpfile
, NILFS_MDT_GFP
, 0);
1015 nilfs_mdt_set_entry_size(cpfile
, cpsize
,
1016 sizeof(struct nilfs_cpfile_header
));
1018 err
= nilfs_read_inode_common(cpfile
, raw_inode
);
1022 unlock_new_inode(cpfile
);
1027 iget_failed(cpfile
);