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;
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;
49 return do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
52 static __u64 nilfs_cpfile_first_checkpoint_in_block(const struct inode *cpfile,
55 return (__u64)nilfs_cpfile_checkpoints_per_block(cpfile) * blkoff
56 + 1 - NILFS_MDT(cpfile)->mi_first_entry_offset;
60 nilfs_cpfile_checkpoints_in_block(const struct inode *cpfile,
65 nilfs_cpfile_checkpoints_per_block(cpfile) -
66 nilfs_cpfile_get_offset(cpfile, curr),
70 static inline int nilfs_cpfile_is_in_first(const struct inode *cpfile,
73 return nilfs_cpfile_get_blkoff(cpfile, cno) == 0;
77 nilfs_cpfile_block_add_valid_checkpoints(const struct inode *cpfile,
78 struct buffer_head *bh,
82 struct nilfs_checkpoint *cp = kaddr + bh_offset(bh);
85 count = le32_to_cpu(cp->cp_checkpoints_count) + n;
86 cp->cp_checkpoints_count = cpu_to_le32(count);
91 nilfs_cpfile_block_sub_valid_checkpoints(const struct inode *cpfile,
92 struct buffer_head *bh,
96 struct nilfs_checkpoint *cp = kaddr + bh_offset(bh);
99 WARN_ON(le32_to_cpu(cp->cp_checkpoints_count) < n);
100 count = le32_to_cpu(cp->cp_checkpoints_count) - n;
101 cp->cp_checkpoints_count = cpu_to_le32(count);
105 static inline struct nilfs_cpfile_header *
106 nilfs_cpfile_block_get_header(const struct inode *cpfile,
107 struct buffer_head *bh,
110 return kaddr + bh_offset(bh);
113 static struct nilfs_checkpoint *
114 nilfs_cpfile_block_get_checkpoint(const struct inode *cpfile, __u64 cno,
115 struct buffer_head *bh,
118 return kaddr + bh_offset(bh) + nilfs_cpfile_get_offset(cpfile, cno) *
119 NILFS_MDT(cpfile)->mi_entry_size;
122 static void nilfs_cpfile_block_init(struct inode *cpfile,
123 struct buffer_head *bh,
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);
131 nilfs_checkpoint_set_invalid(cp);
132 cp = (void *)cp + cpsz;
136 static inline int nilfs_cpfile_get_header_block(struct inode *cpfile,
137 struct buffer_head **bhp)
139 return nilfs_mdt_get_block(cpfile, 0, 0, NULL, bhp);
142 static inline int nilfs_cpfile_get_checkpoint_block(struct inode *cpfile,
145 struct buffer_head **bhp)
147 return nilfs_mdt_get_block(cpfile,
148 nilfs_cpfile_get_blkoff(cpfile, cno),
149 create, nilfs_cpfile_block_init, bhp);
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
160 * Return Value: On success, it returns 0. On error, the following negative
161 * error code is returned.
163 * %-ENOMEM - Insufficient memory available.
167 * %-ENOENT - no block exists in the range.
169 static int nilfs_cpfile_find_checkpoint_block(struct inode *cpfile,
170 __u64 start_cno, __u64 end_cno,
172 struct buffer_head **bhp)
174 unsigned long start, end, blkoff;
177 if (unlikely(start_cno > end_cno))
180 start = nilfs_cpfile_get_blkoff(cpfile, start_cno);
181 end = nilfs_cpfile_get_blkoff(cpfile, end_cno);
183 ret = nilfs_mdt_find_block(cpfile, start, end, &blkoff, bhp);
185 *cnop = (blkoff == start) ? start_cno :
186 nilfs_cpfile_first_checkpoint_in_block(cpfile, blkoff);
190 static inline int nilfs_cpfile_delete_checkpoint_block(struct inode *cpfile,
193 return nilfs_mdt_delete_block(cpfile,
194 nilfs_cpfile_get_blkoff(cpfile, cno));
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
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.
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.
216 * %-ENOMEM - Insufficient amount of memory available.
218 * %-ENOENT - No such checkpoint.
220 * %-EINVAL - invalid checkpoint.
222 int nilfs_cpfile_get_checkpoint(struct inode *cpfile,
225 struct nilfs_checkpoint **cpp,
226 struct buffer_head **bhp)
228 struct buffer_head *header_bh, *cp_bh;
229 struct nilfs_cpfile_header *header;
230 struct nilfs_checkpoint *cp;
234 if (unlikely(cno < 1 || cno > nilfs_mdt_cno(cpfile) ||
235 (cno < nilfs_mdt_cno(cpfile) && create)))
238 down_write(&NILFS_MDT(cpfile)->mi_sem);
240 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
243 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, create, &cp_bh);
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)) {
250 kunmap(cp_bh->b_page);
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,
260 mark_buffer_dirty(cp_bh);
262 kaddr = kmap_atomic(header_bh->b_page);
263 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
265 le64_add_cpu(&header->ch_ncheckpoints, 1);
266 kunmap_atomic(kaddr);
267 mark_buffer_dirty(header_bh);
268 nilfs_mdt_mark_dirty(cpfile);
279 up_write(&NILFS_MDT(cpfile)->mi_sem);
284 * nilfs_cpfile_put_checkpoint - put a checkpoint
285 * @cpfile: inode of checkpoint file
286 * @cno: checkpoint number
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.
293 void nilfs_cpfile_put_checkpoint(struct inode *cpfile, __u64 cno,
294 struct buffer_head *bh)
301 * nilfs_cpfile_delete_checkpoints - delete checkpoints
302 * @cpfile: inode of checkpoint file
303 * @start: start checkpoint number
304 * @end: end checkpoint numer
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.
310 * Return Value: On success, 0 is returned. On error, one of the following
311 * negative error codes is returned.
315 * %-ENOMEM - Insufficient amount of memory available.
317 * %-EINVAL - invalid checkpoints.
319 int nilfs_cpfile_delete_checkpoints(struct inode *cpfile,
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;
329 unsigned long tnicps;
330 int ret, ncps, nicps, nss, count, i;
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);
339 down_write(&NILFS_MDT(cpfile)->mi_sem);
341 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
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);
358 kaddr = kmap_atomic(cp_bh->b_page);
359 cp = nilfs_cpfile_block_get_checkpoint(
360 cpfile, cno, cp_bh, kaddr);
362 for (i = 0; i < ncps; i++, cp = (void *)cp + cpsz) {
363 if (nilfs_checkpoint_snapshot(cp)) {
365 } else if (!nilfs_checkpoint_invalid(cp)) {
366 nilfs_checkpoint_set_invalid(cp);
372 mark_buffer_dirty(cp_bh);
373 nilfs_mdt_mark_dirty(cpfile);
374 if (!nilfs_cpfile_is_in_first(cpfile, cno)) {
376 nilfs_cpfile_block_sub_valid_checkpoints(
377 cpfile, cp_bh, kaddr, nicps);
380 kunmap_atomic(kaddr);
383 nilfs_cpfile_delete_checkpoint_block(
388 "%s: cannot delete block\n",
395 kunmap_atomic(kaddr);
400 kaddr = kmap_atomic(header_bh->b_page);
401 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
403 le64_add_cpu(&header->ch_ncheckpoints, -(u64)tnicps);
404 mark_buffer_dirty(header_bh);
405 nilfs_mdt_mark_dirty(cpfile);
406 kunmap_atomic(kaddr);
414 up_write(&NILFS_MDT(cpfile)->mi_sem);
418 static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode *cpfile,
419 struct nilfs_checkpoint *cp,
420 struct nilfs_cpinfo *ci)
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);
431 static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop,
432 void *buf, unsigned cisz, size_t nci)
434 struct nilfs_checkpoint *cp;
435 struct nilfs_cpinfo *ci = buf;
436 struct buffer_head *bh;
437 size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
438 __u64 cur_cno = nilfs_mdt_cno(cpfile), cno = *cnop;
444 return -ENOENT; /* checkpoint number 0 is invalid */
445 down_read(&NILFS_MDT(cpfile)->mi_sem);
447 for (n = 0; n < nci; cno += ncps) {
448 ret = nilfs_cpfile_find_checkpoint_block(
449 cpfile, cno, cur_cno - 1, &cno, &bh);
451 if (likely(ret == -ENOENT))
455 ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, cur_cno);
457 kaddr = kmap_atomic(bh->b_page);
458 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
459 for (i = 0; i < ncps && n < nci; i++, cp = (void *)cp + cpsz) {
460 if (!nilfs_checkpoint_invalid(cp)) {
461 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp,
463 ci = (void *)ci + cisz;
467 kunmap_atomic(kaddr);
473 ci = (void *)ci - cisz;
474 *cnop = ci->ci_cno + 1;
478 up_read(&NILFS_MDT(cpfile)->mi_sem);
482 static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
483 void *buf, unsigned cisz, size_t nci)
485 struct buffer_head *bh;
486 struct nilfs_cpfile_header *header;
487 struct nilfs_checkpoint *cp;
488 struct nilfs_cpinfo *ci = buf;
489 __u64 curr = *cnop, next;
490 unsigned long curr_blkoff, next_blkoff;
494 down_read(&NILFS_MDT(cpfile)->mi_sem);
497 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
500 kaddr = kmap_atomic(bh->b_page);
501 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
502 curr = le64_to_cpu(header->ch_snapshot_list.ssl_next);
503 kunmap_atomic(kaddr);
509 } else if (unlikely(curr == ~(__u64)0)) {
514 curr_blkoff = nilfs_cpfile_get_blkoff(cpfile, curr);
515 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr, 0, &bh);
516 if (unlikely(ret < 0)) {
518 ret = 0; /* No snapshots (started from a hole block) */
521 kaddr = kmap_atomic(bh->b_page);
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)))
528 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, ci);
529 ci = (void *)ci + cisz;
531 next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
533 break; /* reach end of the snapshot list */
535 next_blkoff = nilfs_cpfile_get_blkoff(cpfile, next);
536 if (curr_blkoff != next_blkoff) {
537 kunmap_atomic(kaddr);
539 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next,
541 if (unlikely(ret < 0)) {
542 WARN_ON(ret == -ENOENT);
545 kaddr = kmap_atomic(bh->b_page);
548 curr_blkoff = next_blkoff;
550 kunmap_atomic(kaddr);
556 up_read(&NILFS_MDT(cpfile)->mi_sem);
561 * nilfs_cpfile_get_cpinfo -
568 ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode,
569 void *buf, unsigned cisz, size_t nci)
572 case NILFS_CHECKPOINT:
573 return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, buf, cisz, nci);
575 return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, buf, cisz, nci);
582 * nilfs_cpfile_delete_checkpoint -
586 int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
588 struct nilfs_cpinfo ci;
592 nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, sizeof(ci), 1);
595 else if (nci == 0 || ci.ci_cno != cno)
597 else if (nilfs_cpinfo_snapshot(&ci))
600 return nilfs_cpfile_delete_checkpoints(cpfile, cno, cno + 1);
603 static struct nilfs_snapshot_list *
604 nilfs_cpfile_block_get_snapshot_list(const struct inode *cpfile,
606 struct buffer_head *bh,
609 struct nilfs_cpfile_header *header;
610 struct nilfs_checkpoint *cp;
611 struct nilfs_snapshot_list *list;
614 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
615 list = &cp->cp_snapshot_list;
617 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
618 list = &header->ch_snapshot_list;
623 static int nilfs_cpfile_set_snapshot(struct inode *cpfile, __u64 cno)
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;
630 unsigned long curr_blkoff, prev_blkoff;
635 return -ENOENT; /* checkpoint number 0 is invalid */
636 down_write(&NILFS_MDT(cpfile)->mi_sem);
638 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
641 kaddr = kmap_atomic(cp_bh->b_page);
642 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
643 if (nilfs_checkpoint_invalid(cp)) {
645 kunmap_atomic(kaddr);
648 if (nilfs_checkpoint_snapshot(cp)) {
650 kunmap_atomic(kaddr);
653 kunmap_atomic(kaddr);
655 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
658 kaddr = kmap_atomic(header_bh->b_page);
659 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
660 list = &header->ch_snapshot_list;
665 prev = le64_to_cpu(list->ssl_prev);
667 prev_blkoff = nilfs_cpfile_get_blkoff(cpfile, prev);
669 if (curr_blkoff != prev_blkoff) {
670 kunmap_atomic(kaddr);
672 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr,
676 kaddr = kmap_atomic(curr_bh->b_page);
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);
684 kunmap_atomic(kaddr);
687 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
696 kaddr = kmap_atomic(curr_bh->b_page);
697 list = nilfs_cpfile_block_get_snapshot_list(
698 cpfile, curr, curr_bh, kaddr);
699 list->ssl_prev = cpu_to_le64(cno);
700 kunmap_atomic(kaddr);
702 kaddr = kmap_atomic(cp_bh->b_page);
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);
707 kunmap_atomic(kaddr);
709 kaddr = kmap_atomic(prev_bh->b_page);
710 list = nilfs_cpfile_block_get_snapshot_list(
711 cpfile, prev, prev_bh, kaddr);
712 list->ssl_next = cpu_to_le64(cno);
713 kunmap_atomic(kaddr);
715 kaddr = kmap_atomic(header_bh->b_page);
716 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
717 le64_add_cpu(&header->ch_nsnapshots, 1);
718 kunmap_atomic(kaddr);
720 mark_buffer_dirty(prev_bh);
721 mark_buffer_dirty(curr_bh);
722 mark_buffer_dirty(cp_bh);
723 mark_buffer_dirty(header_bh);
724 nilfs_mdt_mark_dirty(cpfile);
738 up_write(&NILFS_MDT(cpfile)->mi_sem);
742 static int nilfs_cpfile_clear_snapshot(struct inode *cpfile, __u64 cno)
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;
753 return -ENOENT; /* checkpoint number 0 is invalid */
754 down_write(&NILFS_MDT(cpfile)->mi_sem);
756 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
759 kaddr = kmap_atomic(cp_bh->b_page);
760 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
761 if (nilfs_checkpoint_invalid(cp)) {
763 kunmap_atomic(kaddr);
766 if (!nilfs_checkpoint_snapshot(cp)) {
768 kunmap_atomic(kaddr);
772 list = &cp->cp_snapshot_list;
773 next = le64_to_cpu(list->ssl_next);
774 prev = le64_to_cpu(list->ssl_prev);
775 kunmap_atomic(kaddr);
777 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
781 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next, 0,
790 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
799 kaddr = kmap_atomic(next_bh->b_page);
800 list = nilfs_cpfile_block_get_snapshot_list(
801 cpfile, next, next_bh, kaddr);
802 list->ssl_prev = cpu_to_le64(prev);
803 kunmap_atomic(kaddr);
805 kaddr = kmap_atomic(prev_bh->b_page);
806 list = nilfs_cpfile_block_get_snapshot_list(
807 cpfile, prev, prev_bh, kaddr);
808 list->ssl_next = cpu_to_le64(next);
809 kunmap_atomic(kaddr);
811 kaddr = kmap_atomic(cp_bh->b_page);
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);
816 kunmap_atomic(kaddr);
818 kaddr = kmap_atomic(header_bh->b_page);
819 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
820 le64_add_cpu(&header->ch_nsnapshots, -1);
821 kunmap_atomic(kaddr);
823 mark_buffer_dirty(next_bh);
824 mark_buffer_dirty(prev_bh);
825 mark_buffer_dirty(cp_bh);
826 mark_buffer_dirty(header_bh);
827 nilfs_mdt_mark_dirty(cpfile);
841 up_write(&NILFS_MDT(cpfile)->mi_sem);
846 * nilfs_cpfile_is_snapshot -
847 * @cpfile: inode of checkpoint file
848 * @cno: checkpoint number
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.
858 * %-ENOMEM - Insufficient amount of memory available.
860 * %-ENOENT - No such checkpoint.
862 int nilfs_cpfile_is_snapshot(struct inode *cpfile, __u64 cno)
864 struct buffer_head *bh;
865 struct nilfs_checkpoint *cp;
869 /* CP number is invalid if it's zero or larger than the
871 if (cno == 0 || cno >= nilfs_mdt_cno(cpfile))
873 down_read(&NILFS_MDT(cpfile)->mi_sem);
875 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh);
878 kaddr = kmap_atomic(bh->b_page);
879 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
880 if (nilfs_checkpoint_invalid(cp))
883 ret = nilfs_checkpoint_snapshot(cp);
884 kunmap_atomic(kaddr);
888 up_read(&NILFS_MDT(cpfile)->mi_sem);
893 * nilfs_cpfile_change_cpmode - change checkpoint mode
894 * @cpfile: inode of checkpoint file
895 * @cno: checkpoint number
896 * @status: mode of checkpoint
898 * Description: nilfs_change_cpmode() changes the mode of the checkpoint
899 * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT.
901 * Return Value: On success, 0 is returned. On error, one of the following
902 * negative error codes is returned.
906 * %-ENOMEM - Insufficient amount of memory available.
908 * %-ENOENT - No such checkpoint.
910 int nilfs_cpfile_change_cpmode(struct inode *cpfile, __u64 cno, int mode)
915 case NILFS_CHECKPOINT:
916 if (nilfs_checkpoint_is_mounted(cpfile->i_sb, cno))
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
925 ret = nilfs_cpfile_clear_snapshot(cpfile, cno);
928 return nilfs_cpfile_set_snapshot(cpfile, cno);
935 * nilfs_cpfile_get_stat - get checkpoint statistics
936 * @cpfile: inode of checkpoint file
937 * @stat: pointer to a structure of checkpoint statistics
939 * Description: nilfs_cpfile_get_stat() returns information about checkpoints.
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.
947 * %-ENOMEM - Insufficient amount of memory available.
949 int nilfs_cpfile_get_stat(struct inode *cpfile, struct nilfs_cpstat *cpstat)
951 struct buffer_head *bh;
952 struct nilfs_cpfile_header *header;
956 down_read(&NILFS_MDT(cpfile)->mi_sem);
958 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
961 kaddr = kmap_atomic(bh->b_page);
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);
966 kunmap_atomic(kaddr);
970 up_read(&NILFS_MDT(cpfile)->mi_sem);
975 * nilfs_cpfile_read - read or get cpfile inode
976 * @sb: super block instance
977 * @cpsize: size of a checkpoint entry
978 * @raw_inode: on-disk cpfile inode
979 * @inodep: buffer to store the inode
981 int nilfs_cpfile_read(struct super_block *sb, size_t cpsize,
982 struct nilfs_inode *raw_inode, struct inode **inodep)
984 struct inode *cpfile;
987 if (cpsize > sb->s_blocksize) {
989 "NILFS: too large checkpoint size: %zu bytes.\n",
992 } else if (cpsize < NILFS_MIN_CHECKPOINT_SIZE) {
994 "NILFS: too small checkpoint size: %zu bytes.\n",
999 cpfile = nilfs_iget_locked(sb, NULL, NILFS_CPFILE_INO);
1000 if (unlikely(!cpfile))
1002 if (!(cpfile->i_state & I_NEW))
1005 err = nilfs_mdt_init(cpfile, NILFS_MDT_GFP, 0);
1009 nilfs_mdt_set_entry_size(cpfile, cpsize,
1010 sizeof(struct nilfs_cpfile_header));
1012 err = nilfs_read_inode_common(cpfile, raw_inode);
1016 unlock_new_inode(cpfile);
1021 iget_failed(cpfile);