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 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Koji Sato <koji@osrg.net>.
23 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/buffer_head.h>
27 #include <linux/errno.h>
28 #include <linux/nilfs2_fs.h>
33 static inline unsigned long
34 nilfs_cpfile_checkpoints_per_block(const struct inode *cpfile)
36 return NILFS_MDT(cpfile)->mi_entries_per_block;
39 /* block number from the beginning of the file */
41 nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
45 BUG_ON(cno == 0); /* checkpoint number 0 is invalid */
46 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
47 do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
48 return (unsigned long)tcno;
53 nilfs_cpfile_get_offset(const struct inode *cpfile, __u64 cno)
55 __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
56 return do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
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 BUG_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);
152 static inline int nilfs_cpfile_delete_checkpoint_block(struct inode *cpfile,
155 return nilfs_mdt_delete_block(cpfile,
156 nilfs_cpfile_get_blkoff(cpfile, cno));
160 * nilfs_cpfile_get_checkpoint - get a checkpoint
161 * @cpfile: inode of checkpoint file
162 * @cno: checkpoint number
163 * @create: create flag
164 * @cpp: pointer to a checkpoint
165 * @bhp: pointer to a buffer head
167 * Description: nilfs_cpfile_get_checkpoint() acquires the checkpoint
168 * specified by @cno. A new checkpoint will be created if @cno is the current
169 * checkpoint number and @create is nonzero.
171 * Return Value: On success, 0 is returned, and the checkpoint and the
172 * buffer head of the buffer on which the checkpoint is located are stored in
173 * the place pointed by @cpp and @bhp, respectively. On error, one of the
174 * following negative error codes is returned.
178 * %-ENOMEM - Insufficient amount of memory available.
180 * %-ENOENT - No such checkpoint.
182 int nilfs_cpfile_get_checkpoint(struct inode *cpfile,
185 struct nilfs_checkpoint **cpp,
186 struct buffer_head **bhp)
188 struct buffer_head *header_bh, *cp_bh;
189 struct nilfs_cpfile_header *header;
190 struct nilfs_checkpoint *cp;
194 BUG_ON(cno < 1 || cno > nilfs_mdt_cno(cpfile) ||
195 (cno < nilfs_mdt_cno(cpfile) && create));
197 down_write(&NILFS_MDT(cpfile)->mi_sem);
199 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
202 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, create, &cp_bh);
205 kaddr = kmap(cp_bh->b_page);
206 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
207 if (nilfs_checkpoint_invalid(cp)) {
209 kunmap(cp_bh->b_page);
214 /* a newly-created checkpoint */
215 nilfs_checkpoint_clear_invalid(cp);
216 if (!nilfs_cpfile_is_in_first(cpfile, cno))
217 nilfs_cpfile_block_add_valid_checkpoints(cpfile, cp_bh,
219 nilfs_mdt_mark_buffer_dirty(cp_bh);
221 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
222 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
224 le64_add_cpu(&header->ch_ncheckpoints, 1);
225 kunmap_atomic(kaddr, KM_USER0);
226 nilfs_mdt_mark_buffer_dirty(header_bh);
227 nilfs_mdt_mark_dirty(cpfile);
238 up_write(&NILFS_MDT(cpfile)->mi_sem);
243 * nilfs_cpfile_put_checkpoint - put a checkpoint
244 * @cpfile: inode of checkpoint file
245 * @cno: checkpoint number
248 * Description: nilfs_cpfile_put_checkpoint() releases the checkpoint
249 * specified by @cno. @bh must be the buffer head which has been returned by
250 * a previous call to nilfs_cpfile_get_checkpoint() with @cno.
252 void nilfs_cpfile_put_checkpoint(struct inode *cpfile, __u64 cno,
253 struct buffer_head *bh)
260 * nilfs_cpfile_delete_checkpoints - delete checkpoints
261 * @cpfile: inode of checkpoint file
262 * @start: start checkpoint number
263 * @end: end checkpoint numer
265 * Description: nilfs_cpfile_delete_checkpoints() deletes the checkpoints in
266 * the period from @start to @end, excluding @end itself. The checkpoints
267 * which have been already deleted are ignored.
269 * Return Value: On success, 0 is returned. On error, one of the following
270 * negative error codes is returned.
274 * %-ENOMEM - Insufficient amount of memory available.
276 * %-EINVAL - invalid checkpoints.
278 int nilfs_cpfile_delete_checkpoints(struct inode *cpfile,
282 struct buffer_head *header_bh, *cp_bh;
283 struct nilfs_cpfile_header *header;
284 struct nilfs_checkpoint *cp;
285 size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
288 unsigned long tnicps;
289 int ret, ncps, nicps, count, i;
291 if ((start == 0) || (start > end)) {
292 printk(KERN_CRIT "%s: start = %llu, end = %llu\n",
294 (unsigned long long)start,
295 (unsigned long long)end);
299 /* cannot delete the latest checkpoint */
300 if (start == nilfs_mdt_cno(cpfile) - 1)
303 down_write(&NILFS_MDT(cpfile)->mi_sem);
305 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
310 for (cno = start; cno < end; cno += ncps) {
311 ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, end);
312 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
321 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
322 cp = nilfs_cpfile_block_get_checkpoint(
323 cpfile, cno, cp_bh, kaddr);
325 for (i = 0; i < ncps; i++, cp = (void *)cp + cpsz) {
326 BUG_ON(nilfs_checkpoint_snapshot(cp));
327 if (!nilfs_checkpoint_invalid(cp)) {
328 nilfs_checkpoint_set_invalid(cp);
334 nilfs_mdt_mark_buffer_dirty(cp_bh);
335 nilfs_mdt_mark_dirty(cpfile);
336 if (!nilfs_cpfile_is_in_first(cpfile, cno) &&
337 (count = nilfs_cpfile_block_sub_valid_checkpoints(
338 cpfile, cp_bh, kaddr, nicps)) == 0) {
340 kunmap_atomic(kaddr, KM_USER0);
342 ret = nilfs_cpfile_delete_checkpoint_block(
346 printk(KERN_ERR "%s: cannot delete block\n",
352 kunmap_atomic(kaddr, KM_USER0);
357 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
358 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
360 le64_add_cpu(&header->ch_ncheckpoints, -(u64)tnicps);
361 nilfs_mdt_mark_buffer_dirty(header_bh);
362 nilfs_mdt_mark_dirty(cpfile);
363 kunmap_atomic(kaddr, KM_USER0);
368 up_write(&NILFS_MDT(cpfile)->mi_sem);
372 static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode *cpfile,
373 struct nilfs_checkpoint *cp,
374 struct nilfs_cpinfo *ci)
376 ci->ci_flags = le32_to_cpu(cp->cp_flags);
377 ci->ci_cno = le64_to_cpu(cp->cp_cno);
378 ci->ci_create = le64_to_cpu(cp->cp_create);
379 ci->ci_nblk_inc = le64_to_cpu(cp->cp_nblk_inc);
380 ci->ci_inodes_count = le64_to_cpu(cp->cp_inodes_count);
381 ci->ci_blocks_count = le64_to_cpu(cp->cp_blocks_count);
382 ci->ci_next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
385 static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 cno,
386 struct nilfs_cpinfo *ci, size_t nci)
388 struct nilfs_checkpoint *cp;
389 struct buffer_head *bh;
390 size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
391 __u64 cur_cno = nilfs_mdt_cno(cpfile);
396 down_read(&NILFS_MDT(cpfile)->mi_sem);
398 for (n = 0; cno < cur_cno && n < nci; cno += ncps) {
399 ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, cur_cno);
400 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh);
404 continue; /* skip hole */
407 kaddr = kmap_atomic(bh->b_page, KM_USER0);
408 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
409 for (i = 0; i < ncps && n < nci; i++, cp = (void *)cp + cpsz) {
410 if (!nilfs_checkpoint_invalid(cp))
411 nilfs_cpfile_checkpoint_to_cpinfo(
412 cpfile, cp, &ci[n++]);
414 kunmap_atomic(kaddr, KM_USER0);
421 up_read(&NILFS_MDT(cpfile)->mi_sem);
425 static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
426 struct nilfs_cpinfo *ci, size_t nci)
428 struct buffer_head *bh;
429 struct nilfs_cpfile_header *header;
430 struct nilfs_checkpoint *cp;
431 __u64 curr = *cnop, next;
432 unsigned long curr_blkoff, next_blkoff;
436 down_read(&NILFS_MDT(cpfile)->mi_sem);
439 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
442 kaddr = kmap_atomic(bh->b_page, KM_USER0);
443 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
444 curr = le64_to_cpu(header->ch_snapshot_list.ssl_next);
445 kunmap_atomic(kaddr, KM_USER0);
451 } else if (unlikely(curr == ~(__u64)0)) {
456 curr_blkoff = nilfs_cpfile_get_blkoff(cpfile, curr);
457 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr, 0, &bh);
458 if (unlikely(ret < 0)) {
460 ret = 0; /* No snapshots (started from a hole block) */
463 kaddr = kmap_atomic(bh->b_page, KM_USER0);
465 cp = nilfs_cpfile_block_get_checkpoint(cpfile, curr, bh, kaddr);
466 curr = ~(__u64)0; /* Terminator */
467 if (unlikely(nilfs_checkpoint_invalid(cp) ||
468 !nilfs_checkpoint_snapshot(cp)))
470 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, &ci[n++]);
471 next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
473 break; /* reach end of the snapshot list */
475 next_blkoff = nilfs_cpfile_get_blkoff(cpfile, next);
476 if (curr_blkoff != next_blkoff) {
477 kunmap_atomic(kaddr, KM_USER0);
479 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next,
481 if (unlikely(ret < 0)) {
482 WARN_ON(ret == -ENOENT);
485 kaddr = kmap_atomic(bh->b_page, KM_USER0);
488 curr_blkoff = next_blkoff;
490 kunmap_atomic(kaddr, KM_USER0);
496 up_read(&NILFS_MDT(cpfile)->mi_sem);
501 * nilfs_cpfile_get_cpinfo -
508 ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode,
509 struct nilfs_cpinfo *ci, size_t nci)
512 case NILFS_CHECKPOINT:
513 return nilfs_cpfile_do_get_cpinfo(cpfile, *cnop, ci, nci);
515 return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, ci, nci);
522 * nilfs_cpfile_delete_checkpoint -
526 int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
528 struct nilfs_cpinfo ci;
532 /* checkpoint number 0 is invalid */
535 nci = nilfs_cpfile_do_get_cpinfo(cpfile, cno, &ci, 1);
538 else if (nci == 0 || ci.ci_cno != cno)
541 /* cannot delete the latest checkpoint nor snapshots */
542 ret = nilfs_cpinfo_snapshot(&ci);
545 else if (ret > 0 || cno == nilfs_mdt_cno(cpfile) - 1)
548 return nilfs_cpfile_delete_checkpoints(cpfile, cno, cno + 1);
551 static struct nilfs_snapshot_list *
552 nilfs_cpfile_block_get_snapshot_list(const struct inode *cpfile,
554 struct buffer_head *bh,
557 struct nilfs_cpfile_header *header;
558 struct nilfs_checkpoint *cp;
559 struct nilfs_snapshot_list *list;
562 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
563 list = &cp->cp_snapshot_list;
565 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
566 list = &header->ch_snapshot_list;
571 static int nilfs_cpfile_set_snapshot(struct inode *cpfile, __u64 cno)
573 struct buffer_head *header_bh, *curr_bh, *prev_bh, *cp_bh;
574 struct nilfs_cpfile_header *header;
575 struct nilfs_checkpoint *cp;
576 struct nilfs_snapshot_list *list;
578 unsigned long curr_blkoff, prev_blkoff;
582 down_write(&NILFS_MDT(cpfile)->mi_sem);
584 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
587 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
588 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
589 if (nilfs_checkpoint_invalid(cp)) {
591 kunmap_atomic(kaddr, KM_USER0);
594 if (nilfs_checkpoint_snapshot(cp)) {
596 kunmap_atomic(kaddr, KM_USER0);
599 kunmap_atomic(kaddr, KM_USER0);
601 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
604 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
605 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
606 list = &header->ch_snapshot_list;
611 prev = le64_to_cpu(list->ssl_prev);
613 prev_blkoff = nilfs_cpfile_get_blkoff(cpfile, prev);
615 if (curr_blkoff != prev_blkoff) {
616 kunmap_atomic(kaddr, KM_USER0);
618 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr,
622 kaddr = kmap_atomic(curr_bh->b_page, KM_USER0);
624 curr_blkoff = prev_blkoff;
625 cp = nilfs_cpfile_block_get_checkpoint(
626 cpfile, curr, curr_bh, kaddr);
627 list = &cp->cp_snapshot_list;
628 prev = le64_to_cpu(list->ssl_prev);
630 kunmap_atomic(kaddr, KM_USER0);
633 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
642 kaddr = kmap_atomic(curr_bh->b_page, KM_USER0);
643 list = nilfs_cpfile_block_get_snapshot_list(
644 cpfile, curr, curr_bh, kaddr);
645 list->ssl_prev = cpu_to_le64(cno);
646 kunmap_atomic(kaddr, KM_USER0);
648 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
649 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
650 cp->cp_snapshot_list.ssl_next = cpu_to_le64(curr);
651 cp->cp_snapshot_list.ssl_prev = cpu_to_le64(prev);
652 nilfs_checkpoint_set_snapshot(cp);
653 kunmap_atomic(kaddr, KM_USER0);
655 kaddr = kmap_atomic(prev_bh->b_page, KM_USER0);
656 list = nilfs_cpfile_block_get_snapshot_list(
657 cpfile, prev, prev_bh, kaddr);
658 list->ssl_next = cpu_to_le64(cno);
659 kunmap_atomic(kaddr, KM_USER0);
661 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
662 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
663 le64_add_cpu(&header->ch_nsnapshots, 1);
664 kunmap_atomic(kaddr, KM_USER0);
666 nilfs_mdt_mark_buffer_dirty(prev_bh);
667 nilfs_mdt_mark_buffer_dirty(curr_bh);
668 nilfs_mdt_mark_buffer_dirty(cp_bh);
669 nilfs_mdt_mark_buffer_dirty(header_bh);
670 nilfs_mdt_mark_dirty(cpfile);
684 up_write(&NILFS_MDT(cpfile)->mi_sem);
688 static int nilfs_cpfile_clear_snapshot(struct inode *cpfile, __u64 cno)
690 struct buffer_head *header_bh, *next_bh, *prev_bh, *cp_bh;
691 struct nilfs_cpfile_header *header;
692 struct nilfs_checkpoint *cp;
693 struct nilfs_snapshot_list *list;
698 down_write(&NILFS_MDT(cpfile)->mi_sem);
700 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
703 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
704 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
705 if (nilfs_checkpoint_invalid(cp)) {
707 kunmap_atomic(kaddr, KM_USER0);
710 if (!nilfs_checkpoint_snapshot(cp)) {
712 kunmap_atomic(kaddr, KM_USER0);
716 list = &cp->cp_snapshot_list;
717 next = le64_to_cpu(list->ssl_next);
718 prev = le64_to_cpu(list->ssl_prev);
719 kunmap_atomic(kaddr, KM_USER0);
721 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
725 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next, 0,
734 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
743 kaddr = kmap_atomic(next_bh->b_page, KM_USER0);
744 list = nilfs_cpfile_block_get_snapshot_list(
745 cpfile, next, next_bh, kaddr);
746 list->ssl_prev = cpu_to_le64(prev);
747 kunmap_atomic(kaddr, KM_USER0);
749 kaddr = kmap_atomic(prev_bh->b_page, KM_USER0);
750 list = nilfs_cpfile_block_get_snapshot_list(
751 cpfile, prev, prev_bh, kaddr);
752 list->ssl_next = cpu_to_le64(next);
753 kunmap_atomic(kaddr, KM_USER0);
755 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
756 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
757 cp->cp_snapshot_list.ssl_next = cpu_to_le64(0);
758 cp->cp_snapshot_list.ssl_prev = cpu_to_le64(0);
759 nilfs_checkpoint_clear_snapshot(cp);
760 kunmap_atomic(kaddr, KM_USER0);
762 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
763 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
764 le64_add_cpu(&header->ch_nsnapshots, -1);
765 kunmap_atomic(kaddr, KM_USER0);
767 nilfs_mdt_mark_buffer_dirty(next_bh);
768 nilfs_mdt_mark_buffer_dirty(prev_bh);
769 nilfs_mdt_mark_buffer_dirty(cp_bh);
770 nilfs_mdt_mark_buffer_dirty(header_bh);
771 nilfs_mdt_mark_dirty(cpfile);
785 up_write(&NILFS_MDT(cpfile)->mi_sem);
790 * nilfs_cpfile_is_snapshot -
791 * @cpfile: inode of checkpoint file
792 * @cno: checkpoint number
796 * Return Value: On success, 1 is returned if the checkpoint specified by
797 * @cno is a snapshot, or 0 if not. On error, one of the following negative
798 * error codes is returned.
802 * %-ENOMEM - Insufficient amount of memory available.
804 * %-ENOENT - No such checkpoint.
806 int nilfs_cpfile_is_snapshot(struct inode *cpfile, __u64 cno)
808 struct buffer_head *bh;
809 struct nilfs_checkpoint *cp;
813 down_read(&NILFS_MDT(cpfile)->mi_sem);
815 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh);
818 kaddr = kmap_atomic(bh->b_page, KM_USER0);
819 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
820 ret = nilfs_checkpoint_snapshot(cp);
821 kunmap_atomic(kaddr, KM_USER0);
825 up_read(&NILFS_MDT(cpfile)->mi_sem);
830 * nilfs_cpfile_change_cpmode - change checkpoint mode
831 * @cpfile: inode of checkpoint file
832 * @cno: checkpoint number
833 * @status: mode of checkpoint
835 * Description: nilfs_change_cpmode() changes the mode of the checkpoint
836 * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT.
838 * Return Value: On success, 0 is returned. On error, one of the following
839 * negative error codes is returned.
843 * %-ENOMEM - Insufficient amount of memory available.
845 * %-ENOENT - No such checkpoint.
847 int nilfs_cpfile_change_cpmode(struct inode *cpfile, __u64 cno, int mode)
849 struct the_nilfs *nilfs;
852 nilfs = NILFS_MDT(cpfile)->mi_nilfs;
855 case NILFS_CHECKPOINT:
857 * Check for protecting existing snapshot mounts:
858 * bd_mount_sem is used to make this operation atomic and
859 * exclusive with a new mount job. Though it doesn't cover
860 * umount, it's enough for the purpose.
862 down(&nilfs->ns_bdev->bd_mount_sem);
863 if (nilfs_checkpoint_is_mounted(nilfs, cno, 1)) {
864 /* Current implementation does not have to protect
865 plain read-only mounts since they are exclusive
866 with a read/write mount and are protected from the
870 ret = nilfs_cpfile_clear_snapshot(cpfile, cno);
871 up(&nilfs->ns_bdev->bd_mount_sem);
874 return nilfs_cpfile_set_snapshot(cpfile, cno);
881 * nilfs_cpfile_get_stat - get checkpoint statistics
882 * @cpfile: inode of checkpoint file
883 * @stat: pointer to a structure of checkpoint statistics
885 * Description: nilfs_cpfile_get_stat() returns information about checkpoints.
887 * Return Value: On success, 0 is returned, and checkpoints information is
888 * stored in the place pointed by @stat. On error, one of the following
889 * negative error codes is returned.
893 * %-ENOMEM - Insufficient amount of memory available.
895 int nilfs_cpfile_get_stat(struct inode *cpfile, struct nilfs_cpstat *cpstat)
897 struct buffer_head *bh;
898 struct nilfs_cpfile_header *header;
902 down_read(&NILFS_MDT(cpfile)->mi_sem);
904 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
907 kaddr = kmap_atomic(bh->b_page, KM_USER0);
908 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
909 cpstat->cs_cno = nilfs_mdt_cno(cpfile);
910 cpstat->cs_ncps = le64_to_cpu(header->ch_ncheckpoints);
911 cpstat->cs_nsss = le64_to_cpu(header->ch_nsnapshots);
912 kunmap_atomic(kaddr, KM_USER0);
916 up_read(&NILFS_MDT(cpfile)->mi_sem);