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 cisz, size_t nci)
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 cisz, size_t nci)
487 struct buffer_head *bh;
488 struct nilfs_cpfile_header *header;
489 struct nilfs_checkpoint *cp;
490 struct nilfs_cpinfo *ci = buf;
491 __u64 curr = *cnop, next;
492 unsigned long curr_blkoff, next_blkoff;
496 down_read(&NILFS_MDT(cpfile)->mi_sem);
499 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
502 kaddr = kmap_atomic(bh->b_page);
503 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
504 curr = le64_to_cpu(header->ch_snapshot_list.ssl_next);
505 kunmap_atomic(kaddr);
511 } else if (unlikely(curr == ~(__u64)0)) {
516 curr_blkoff = nilfs_cpfile_get_blkoff(cpfile, curr);
517 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr, 0, &bh);
518 if (unlikely(ret < 0)) {
520 ret = 0; /* No snapshots (started from a hole block) */
523 kaddr = kmap_atomic(bh->b_page);
525 cp = nilfs_cpfile_block_get_checkpoint(cpfile, curr, bh, kaddr);
526 curr = ~(__u64)0; /* Terminator */
527 if (unlikely(nilfs_checkpoint_invalid(cp) ||
528 !nilfs_checkpoint_snapshot(cp)))
530 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, ci);
531 ci = (void *)ci + cisz;
533 next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
535 break; /* reach end of the snapshot list */
537 next_blkoff = nilfs_cpfile_get_blkoff(cpfile, next);
538 if (curr_blkoff != next_blkoff) {
539 kunmap_atomic(kaddr);
541 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next,
543 if (unlikely(ret < 0)) {
544 WARN_ON(ret == -ENOENT);
547 kaddr = kmap_atomic(bh->b_page);
550 curr_blkoff = next_blkoff;
552 kunmap_atomic(kaddr);
558 up_read(&NILFS_MDT(cpfile)->mi_sem);
563 * nilfs_cpfile_get_cpinfo -
570 ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode,
571 void *buf, unsigned cisz, size_t nci)
574 case NILFS_CHECKPOINT:
575 return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, buf, cisz, nci);
577 return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, buf, cisz, nci);
584 * nilfs_cpfile_delete_checkpoint -
588 int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
590 struct nilfs_cpinfo ci;
594 nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, sizeof(ci), 1);
597 else if (nci == 0 || ci.ci_cno != cno)
599 else if (nilfs_cpinfo_snapshot(&ci))
602 return nilfs_cpfile_delete_checkpoints(cpfile, cno, cno + 1);
605 static struct nilfs_snapshot_list *
606 nilfs_cpfile_block_get_snapshot_list(const struct inode *cpfile,
608 struct buffer_head *bh,
611 struct nilfs_cpfile_header *header;
612 struct nilfs_checkpoint *cp;
613 struct nilfs_snapshot_list *list;
616 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
617 list = &cp->cp_snapshot_list;
619 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
620 list = &header->ch_snapshot_list;
625 static int nilfs_cpfile_set_snapshot(struct inode *cpfile, __u64 cno)
627 struct buffer_head *header_bh, *curr_bh, *prev_bh, *cp_bh;
628 struct nilfs_cpfile_header *header;
629 struct nilfs_checkpoint *cp;
630 struct nilfs_snapshot_list *list;
632 unsigned long curr_blkoff, prev_blkoff;
637 return -ENOENT; /* checkpoint number 0 is invalid */
638 down_write(&NILFS_MDT(cpfile)->mi_sem);
640 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
643 kaddr = kmap_atomic(cp_bh->b_page);
644 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
645 if (nilfs_checkpoint_invalid(cp)) {
647 kunmap_atomic(kaddr);
650 if (nilfs_checkpoint_snapshot(cp)) {
652 kunmap_atomic(kaddr);
655 kunmap_atomic(kaddr);
657 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
660 kaddr = kmap_atomic(header_bh->b_page);
661 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
662 list = &header->ch_snapshot_list;
667 prev = le64_to_cpu(list->ssl_prev);
669 prev_blkoff = nilfs_cpfile_get_blkoff(cpfile, prev);
671 if (curr_blkoff != prev_blkoff) {
672 kunmap_atomic(kaddr);
674 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr,
678 kaddr = kmap_atomic(curr_bh->b_page);
680 curr_blkoff = prev_blkoff;
681 cp = nilfs_cpfile_block_get_checkpoint(
682 cpfile, curr, curr_bh, kaddr);
683 list = &cp->cp_snapshot_list;
684 prev = le64_to_cpu(list->ssl_prev);
686 kunmap_atomic(kaddr);
689 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
698 kaddr = kmap_atomic(curr_bh->b_page);
699 list = nilfs_cpfile_block_get_snapshot_list(
700 cpfile, curr, curr_bh, kaddr);
701 list->ssl_prev = cpu_to_le64(cno);
702 kunmap_atomic(kaddr);
704 kaddr = kmap_atomic(cp_bh->b_page);
705 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
706 cp->cp_snapshot_list.ssl_next = cpu_to_le64(curr);
707 cp->cp_snapshot_list.ssl_prev = cpu_to_le64(prev);
708 nilfs_checkpoint_set_snapshot(cp);
709 kunmap_atomic(kaddr);
711 kaddr = kmap_atomic(prev_bh->b_page);
712 list = nilfs_cpfile_block_get_snapshot_list(
713 cpfile, prev, prev_bh, kaddr);
714 list->ssl_next = cpu_to_le64(cno);
715 kunmap_atomic(kaddr);
717 kaddr = kmap_atomic(header_bh->b_page);
718 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
719 le64_add_cpu(&header->ch_nsnapshots, 1);
720 kunmap_atomic(kaddr);
722 mark_buffer_dirty(prev_bh);
723 mark_buffer_dirty(curr_bh);
724 mark_buffer_dirty(cp_bh);
725 mark_buffer_dirty(header_bh);
726 nilfs_mdt_mark_dirty(cpfile);
740 up_write(&NILFS_MDT(cpfile)->mi_sem);
744 static int nilfs_cpfile_clear_snapshot(struct inode *cpfile, __u64 cno)
746 struct buffer_head *header_bh, *next_bh, *prev_bh, *cp_bh;
747 struct nilfs_cpfile_header *header;
748 struct nilfs_checkpoint *cp;
749 struct nilfs_snapshot_list *list;
755 return -ENOENT; /* checkpoint number 0 is invalid */
756 down_write(&NILFS_MDT(cpfile)->mi_sem);
758 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
761 kaddr = kmap_atomic(cp_bh->b_page);
762 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
763 if (nilfs_checkpoint_invalid(cp)) {
765 kunmap_atomic(kaddr);
768 if (!nilfs_checkpoint_snapshot(cp)) {
770 kunmap_atomic(kaddr);
774 list = &cp->cp_snapshot_list;
775 next = le64_to_cpu(list->ssl_next);
776 prev = le64_to_cpu(list->ssl_prev);
777 kunmap_atomic(kaddr);
779 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
783 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next, 0,
792 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
801 kaddr = kmap_atomic(next_bh->b_page);
802 list = nilfs_cpfile_block_get_snapshot_list(
803 cpfile, next, next_bh, kaddr);
804 list->ssl_prev = cpu_to_le64(prev);
805 kunmap_atomic(kaddr);
807 kaddr = kmap_atomic(prev_bh->b_page);
808 list = nilfs_cpfile_block_get_snapshot_list(
809 cpfile, prev, prev_bh, kaddr);
810 list->ssl_next = cpu_to_le64(next);
811 kunmap_atomic(kaddr);
813 kaddr = kmap_atomic(cp_bh->b_page);
814 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
815 cp->cp_snapshot_list.ssl_next = cpu_to_le64(0);
816 cp->cp_snapshot_list.ssl_prev = cpu_to_le64(0);
817 nilfs_checkpoint_clear_snapshot(cp);
818 kunmap_atomic(kaddr);
820 kaddr = kmap_atomic(header_bh->b_page);
821 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
822 le64_add_cpu(&header->ch_nsnapshots, -1);
823 kunmap_atomic(kaddr);
825 mark_buffer_dirty(next_bh);
826 mark_buffer_dirty(prev_bh);
827 mark_buffer_dirty(cp_bh);
828 mark_buffer_dirty(header_bh);
829 nilfs_mdt_mark_dirty(cpfile);
843 up_write(&NILFS_MDT(cpfile)->mi_sem);
848 * nilfs_cpfile_is_snapshot -
849 * @cpfile: inode of checkpoint file
850 * @cno: checkpoint number
854 * Return Value: On success, 1 is returned if the checkpoint specified by
855 * @cno is a snapshot, or 0 if not. On error, one of the following negative
856 * error codes is returned.
860 * %-ENOMEM - Insufficient amount of memory available.
862 * %-ENOENT - No such checkpoint.
864 int nilfs_cpfile_is_snapshot(struct inode *cpfile, __u64 cno)
866 struct buffer_head *bh;
867 struct nilfs_checkpoint *cp;
871 /* CP number is invalid if it's zero or larger than the
873 if (cno == 0 || cno >= nilfs_mdt_cno(cpfile))
875 down_read(&NILFS_MDT(cpfile)->mi_sem);
877 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh);
880 kaddr = kmap_atomic(bh->b_page);
881 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
882 if (nilfs_checkpoint_invalid(cp))
885 ret = nilfs_checkpoint_snapshot(cp);
886 kunmap_atomic(kaddr);
890 up_read(&NILFS_MDT(cpfile)->mi_sem);
895 * nilfs_cpfile_change_cpmode - change checkpoint mode
896 * @cpfile: inode of checkpoint file
897 * @cno: checkpoint number
898 * @status: mode of checkpoint
900 * Description: nilfs_change_cpmode() changes the mode of the checkpoint
901 * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT.
903 * Return Value: On success, 0 is returned. On error, one of the following
904 * negative error codes is returned.
908 * %-ENOMEM - Insufficient amount of memory available.
910 * %-ENOENT - No such checkpoint.
912 int nilfs_cpfile_change_cpmode(struct inode *cpfile, __u64 cno, int mode)
917 case NILFS_CHECKPOINT:
918 if (nilfs_checkpoint_is_mounted(cpfile->i_sb, cno))
920 * Current implementation does not have to protect
921 * plain read-only mounts since they are exclusive
922 * with a read/write mount and are protected from the
927 ret = nilfs_cpfile_clear_snapshot(cpfile, cno);
930 return nilfs_cpfile_set_snapshot(cpfile, cno);
937 * nilfs_cpfile_get_stat - get checkpoint statistics
938 * @cpfile: inode of checkpoint file
939 * @stat: pointer to a structure of checkpoint statistics
941 * Description: nilfs_cpfile_get_stat() returns information about checkpoints.
943 * Return Value: On success, 0 is returned, and checkpoints information is
944 * stored in the place pointed by @stat. On error, one of the following
945 * negative error codes is returned.
949 * %-ENOMEM - Insufficient amount of memory available.
951 int nilfs_cpfile_get_stat(struct inode *cpfile, struct nilfs_cpstat *cpstat)
953 struct buffer_head *bh;
954 struct nilfs_cpfile_header *header;
958 down_read(&NILFS_MDT(cpfile)->mi_sem);
960 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
963 kaddr = kmap_atomic(bh->b_page);
964 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
965 cpstat->cs_cno = nilfs_mdt_cno(cpfile);
966 cpstat->cs_ncps = le64_to_cpu(header->ch_ncheckpoints);
967 cpstat->cs_nsss = le64_to_cpu(header->ch_nsnapshots);
968 kunmap_atomic(kaddr);
972 up_read(&NILFS_MDT(cpfile)->mi_sem);
977 * nilfs_cpfile_read - read or get cpfile inode
978 * @sb: super block instance
979 * @cpsize: size of a checkpoint entry
980 * @raw_inode: on-disk cpfile inode
981 * @inodep: buffer to store the inode
983 int nilfs_cpfile_read(struct super_block *sb, size_t cpsize,
984 struct nilfs_inode *raw_inode, struct inode **inodep)
986 struct inode *cpfile;
989 if (cpsize > sb->s_blocksize) {
991 "NILFS: too large checkpoint size: %zu bytes.\n",
994 } else if (cpsize < NILFS_MIN_CHECKPOINT_SIZE) {
996 "NILFS: too small checkpoint size: %zu bytes.\n",
1001 cpfile = nilfs_iget_locked(sb, NULL, NILFS_CPFILE_INO);
1002 if (unlikely(!cpfile))
1004 if (!(cpfile->i_state & I_NEW))
1007 err = nilfs_mdt_init(cpfile, NILFS_MDT_GFP, 0);
1011 nilfs_mdt_set_entry_size(cpfile, cpsize,
1012 sizeof(struct nilfs_cpfile_header));
1014 err = nilfs_read_inode_common(cpfile, raw_inode);
1018 unlock_new_inode(cpfile);
1023 iget_failed(cpfile);