1 // SPDX-License-Identifier: GPL-2.0+
3 * NILFS checkpoint file.
5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
7 * Written by Koji Sato.
10 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/buffer_head.h>
14 #include <linux/errno.h>
19 static inline unsigned long
20 nilfs_cpfile_checkpoints_per_block(const struct inode *cpfile)
22 return NILFS_MDT(cpfile)->mi_entries_per_block;
25 /* block number from the beginning of the file */
27 nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
29 __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
31 tcno = div64_ul(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
32 return (unsigned long)tcno;
37 nilfs_cpfile_get_offset(const struct inode *cpfile, __u64 cno)
39 __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
41 return do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
44 static __u64 nilfs_cpfile_first_checkpoint_in_block(const struct inode *cpfile,
47 return (__u64)nilfs_cpfile_checkpoints_per_block(cpfile) * blkoff
48 + 1 - NILFS_MDT(cpfile)->mi_first_entry_offset;
52 nilfs_cpfile_checkpoints_in_block(const struct inode *cpfile,
57 nilfs_cpfile_checkpoints_per_block(cpfile) -
58 nilfs_cpfile_get_offset(cpfile, curr),
62 static inline int nilfs_cpfile_is_in_first(const struct inode *cpfile,
65 return nilfs_cpfile_get_blkoff(cpfile, cno) == 0;
69 nilfs_cpfile_block_add_valid_checkpoints(const struct inode *cpfile,
70 struct buffer_head *bh,
73 struct nilfs_checkpoint *cp;
76 cp = kmap_local_folio(bh->b_folio,
77 offset_in_folio(bh->b_folio, bh->b_data));
78 count = le32_to_cpu(cp->cp_checkpoints_count) + n;
79 cp->cp_checkpoints_count = cpu_to_le32(count);
85 nilfs_cpfile_block_sub_valid_checkpoints(const struct inode *cpfile,
86 struct buffer_head *bh,
89 struct nilfs_checkpoint *cp;
92 cp = kmap_local_folio(bh->b_folio,
93 offset_in_folio(bh->b_folio, bh->b_data));
94 WARN_ON(le32_to_cpu(cp->cp_checkpoints_count) < n);
95 count = le32_to_cpu(cp->cp_checkpoints_count) - n;
96 cp->cp_checkpoints_count = cpu_to_le32(count);
101 static void nilfs_cpfile_block_init(struct inode *cpfile,
102 struct buffer_head *bh,
105 struct nilfs_checkpoint *cp = from;
106 size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
107 int n = nilfs_cpfile_checkpoints_per_block(cpfile);
110 nilfs_checkpoint_set_invalid(cp);
111 cp = (void *)cp + cpsz;
116 * nilfs_cpfile_checkpoint_offset - calculate the byte offset of a checkpoint
117 * entry in the folio containing it
118 * @cpfile: checkpoint file inode
119 * @cno: checkpoint number
120 * @bh: buffer head of block containing checkpoint indexed by @cno
122 * Return: Byte offset in the folio of the checkpoint specified by @cno.
124 static size_t nilfs_cpfile_checkpoint_offset(const struct inode *cpfile,
126 struct buffer_head *bh)
128 return offset_in_folio(bh->b_folio, bh->b_data) +
129 nilfs_cpfile_get_offset(cpfile, cno) *
130 NILFS_MDT(cpfile)->mi_entry_size;
134 * nilfs_cpfile_cp_snapshot_list_offset - calculate the byte offset of a
135 * checkpoint snapshot list in the folio
137 * @cpfile: checkpoint file inode
138 * @cno: checkpoint number
139 * @bh: buffer head of block containing checkpoint indexed by @cno
141 * Return: Byte offset in the folio of the checkpoint snapshot list specified
144 static size_t nilfs_cpfile_cp_snapshot_list_offset(const struct inode *cpfile,
146 struct buffer_head *bh)
148 return nilfs_cpfile_checkpoint_offset(cpfile, cno, bh) +
149 offsetof(struct nilfs_checkpoint, cp_snapshot_list);
153 * nilfs_cpfile_ch_snapshot_list_offset - calculate the byte offset of the
154 * snapshot list in the header
156 * Return: Byte offset in the folio of the checkpoint snapshot list
158 static size_t nilfs_cpfile_ch_snapshot_list_offset(void)
160 return offsetof(struct nilfs_cpfile_header, ch_snapshot_list);
163 static int nilfs_cpfile_get_header_block(struct inode *cpfile,
164 struct buffer_head **bhp)
166 int err = nilfs_mdt_get_block(cpfile, 0, 0, NULL, bhp);
168 if (unlikely(err == -ENOENT)) {
169 nilfs_error(cpfile->i_sb,
170 "missing header block in checkpoint metadata");
176 static inline int nilfs_cpfile_get_checkpoint_block(struct inode *cpfile,
179 struct buffer_head **bhp)
181 return nilfs_mdt_get_block(cpfile,
182 nilfs_cpfile_get_blkoff(cpfile, cno),
183 create, nilfs_cpfile_block_init, bhp);
187 * nilfs_cpfile_find_checkpoint_block - find and get a buffer on cpfile
188 * @cpfile: inode of cpfile
189 * @start_cno: start checkpoint number (inclusive)
190 * @end_cno: end checkpoint number (inclusive)
191 * @cnop: place to store the next checkpoint number
192 * @bhp: place to store a pointer to buffer_head struct
194 * Return Value: On success, it returns 0. On error, the following negative
195 * error code is returned.
197 * %-ENOMEM - Insufficient memory available.
201 * %-ENOENT - no block exists in the range.
203 static int nilfs_cpfile_find_checkpoint_block(struct inode *cpfile,
204 __u64 start_cno, __u64 end_cno,
206 struct buffer_head **bhp)
208 unsigned long start, end, blkoff;
211 if (unlikely(start_cno > end_cno))
214 start = nilfs_cpfile_get_blkoff(cpfile, start_cno);
215 end = nilfs_cpfile_get_blkoff(cpfile, end_cno);
217 ret = nilfs_mdt_find_block(cpfile, start, end, &blkoff, bhp);
219 *cnop = (blkoff == start) ? start_cno :
220 nilfs_cpfile_first_checkpoint_in_block(cpfile, blkoff);
224 static inline int nilfs_cpfile_delete_checkpoint_block(struct inode *cpfile,
227 return nilfs_mdt_delete_block(cpfile,
228 nilfs_cpfile_get_blkoff(cpfile, cno));
232 * nilfs_cpfile_read_checkpoint - read a checkpoint entry in cpfile
233 * @cpfile: checkpoint file inode
234 * @cno: number of checkpoint entry to read
235 * @root: nilfs root object
236 * @ifile: ifile's inode to read and attach to @root
238 * This function imports checkpoint information from the checkpoint file and
239 * stores it to the inode file given by @ifile and the nilfs root object
242 * Return: 0 on success, or the following negative error code on failure.
243 * * %-EINVAL - Invalid checkpoint.
244 * * %-ENOMEM - Insufficient memory available.
245 * * %-EIO - I/O error (including metadata corruption).
247 int nilfs_cpfile_read_checkpoint(struct inode *cpfile, __u64 cno,
248 struct nilfs_root *root, struct inode *ifile)
250 struct buffer_head *cp_bh;
251 struct nilfs_checkpoint *cp;
255 if (cno < 1 || cno > nilfs_mdt_cno(cpfile))
258 down_read(&NILFS_MDT(cpfile)->mi_sem);
259 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
260 if (unlikely(ret < 0)) {
266 offset = nilfs_cpfile_checkpoint_offset(cpfile, cno, cp_bh);
267 cp = kmap_local_folio(cp_bh->b_folio, offset);
268 if (nilfs_checkpoint_invalid(cp)) {
273 ret = nilfs_read_inode_common(ifile, &cp->cp_ifile_inode);
276 * Since this inode is on a checkpoint entry, treat errors
277 * as metadata corruption.
279 nilfs_err(cpfile->i_sb,
280 "ifile inode (checkpoint number=%llu) corrupted",
281 (unsigned long long)cno);
286 /* Configure the nilfs root object */
287 atomic64_set(&root->inodes_count, le64_to_cpu(cp->cp_inodes_count));
288 atomic64_set(&root->blocks_count, le64_to_cpu(cp->cp_blocks_count));
295 up_read(&NILFS_MDT(cpfile)->mi_sem);
300 * nilfs_cpfile_create_checkpoint - create a checkpoint entry on cpfile
301 * @cpfile: checkpoint file inode
302 * @cno: number of checkpoint to set up
304 * This function creates a checkpoint with the number specified by @cno on
305 * cpfile. If the specified checkpoint entry already exists due to a past
306 * failure, it will be reused without returning an error.
307 * In either case, the buffer of the block containing the checkpoint entry
308 * and the cpfile inode are made dirty for inclusion in the write log.
310 * Return: 0 on success, or the following negative error code on failure.
311 * * %-ENOMEM - Insufficient memory available.
312 * * %-EIO - I/O error (including metadata corruption).
313 * * %-EROFS - Read only filesystem
315 int nilfs_cpfile_create_checkpoint(struct inode *cpfile, __u64 cno)
317 struct buffer_head *header_bh, *cp_bh;
318 struct nilfs_cpfile_header *header;
319 struct nilfs_checkpoint *cp;
323 if (WARN_ON_ONCE(cno < 1))
326 down_write(&NILFS_MDT(cpfile)->mi_sem);
327 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
328 if (unlikely(ret < 0))
331 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 1, &cp_bh);
332 if (unlikely(ret < 0))
335 offset = nilfs_cpfile_checkpoint_offset(cpfile, cno, cp_bh);
336 cp = kmap_local_folio(cp_bh->b_folio, offset);
337 if (nilfs_checkpoint_invalid(cp)) {
338 /* a newly-created checkpoint */
339 nilfs_checkpoint_clear_invalid(cp);
341 if (!nilfs_cpfile_is_in_first(cpfile, cno))
342 nilfs_cpfile_block_add_valid_checkpoints(cpfile, cp_bh,
345 header = kmap_local_folio(header_bh->b_folio, 0);
346 le64_add_cpu(&header->ch_ncheckpoints, 1);
347 kunmap_local(header);
348 mark_buffer_dirty(header_bh);
353 /* Force the buffer and the inode to become dirty */
354 mark_buffer_dirty(cp_bh);
356 nilfs_mdt_mark_dirty(cpfile);
362 up_write(&NILFS_MDT(cpfile)->mi_sem);
367 * nilfs_cpfile_finalize_checkpoint - fill in a checkpoint entry in cpfile
368 * @cpfile: checkpoint file inode
369 * @cno: checkpoint number
370 * @root: nilfs root object
371 * @blkinc: number of blocks added by this checkpoint
372 * @ctime: checkpoint creation time
373 * @minor: minor checkpoint flag
375 * This function completes the checkpoint entry numbered by @cno in the
376 * cpfile with the data given by the arguments @root, @blkinc, @ctime, and
379 * Return: 0 on success, or the following negative error code on failure.
380 * * %-ENOMEM - Insufficient memory available.
381 * * %-EIO - I/O error (including metadata corruption).
383 int nilfs_cpfile_finalize_checkpoint(struct inode *cpfile, __u64 cno,
384 struct nilfs_root *root, __u64 blkinc,
385 time64_t ctime, bool minor)
387 struct buffer_head *cp_bh;
388 struct nilfs_checkpoint *cp;
392 if (WARN_ON_ONCE(cno < 1))
395 down_write(&NILFS_MDT(cpfile)->mi_sem);
396 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
397 if (unlikely(ret < 0)) {
403 offset = nilfs_cpfile_checkpoint_offset(cpfile, cno, cp_bh);
404 cp = kmap_local_folio(cp_bh->b_folio, offset);
405 if (unlikely(nilfs_checkpoint_invalid(cp))) {
411 cp->cp_snapshot_list.ssl_next = 0;
412 cp->cp_snapshot_list.ssl_prev = 0;
413 cp->cp_inodes_count = cpu_to_le64(atomic64_read(&root->inodes_count));
414 cp->cp_blocks_count = cpu_to_le64(atomic64_read(&root->blocks_count));
415 cp->cp_nblk_inc = cpu_to_le64(blkinc);
416 cp->cp_create = cpu_to_le64(ctime);
417 cp->cp_cno = cpu_to_le64(cno);
420 nilfs_checkpoint_set_minor(cp);
422 nilfs_checkpoint_clear_minor(cp);
424 nilfs_write_inode_common(root->ifile, &cp->cp_ifile_inode);
425 nilfs_bmap_write(NILFS_I(root->ifile)->i_bmap, &cp->cp_ifile_inode);
430 up_write(&NILFS_MDT(cpfile)->mi_sem);
434 nilfs_error(cpfile->i_sb,
435 "checkpoint finalization failed due to metadata corruption.");
441 * nilfs_cpfile_delete_checkpoints - delete checkpoints
442 * @cpfile: inode of checkpoint file
443 * @start: start checkpoint number
444 * @end: end checkpoint number
446 * Description: nilfs_cpfile_delete_checkpoints() deletes the checkpoints in
447 * the period from @start to @end, excluding @end itself. The checkpoints
448 * which have been already deleted are ignored.
450 * Return Value: On success, 0 is returned. On error, one of the following
451 * negative error codes is returned.
455 * %-ENOMEM - Insufficient amount of memory available.
457 * %-EINVAL - invalid checkpoints.
459 int nilfs_cpfile_delete_checkpoints(struct inode *cpfile,
463 struct buffer_head *header_bh, *cp_bh;
464 struct nilfs_cpfile_header *header;
465 struct nilfs_checkpoint *cp;
466 size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
470 unsigned long tnicps;
471 int ret, ncps, nicps, nss, count, i;
473 if (unlikely(start == 0 || start > end)) {
474 nilfs_err(cpfile->i_sb,
475 "cannot delete checkpoints: invalid range [%llu, %llu)",
476 (unsigned long long)start, (unsigned long long)end);
480 down_write(&NILFS_MDT(cpfile)->mi_sem);
482 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
488 for (cno = start; cno < end; cno += ncps) {
489 ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, end);
490 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
499 offset = nilfs_cpfile_checkpoint_offset(cpfile, cno, cp_bh);
500 cp = kaddr = kmap_local_folio(cp_bh->b_folio, offset);
502 for (i = 0; i < ncps; i++, cp = (void *)cp + cpsz) {
503 if (nilfs_checkpoint_snapshot(cp)) {
505 } else if (!nilfs_checkpoint_invalid(cp)) {
506 nilfs_checkpoint_set_invalid(cp);
518 mark_buffer_dirty(cp_bh);
519 nilfs_mdt_mark_dirty(cpfile);
520 if (nilfs_cpfile_is_in_first(cpfile, cno)) {
525 count = nilfs_cpfile_block_sub_valid_checkpoints(cpfile, cp_bh,
531 /* Delete the block if there are no more valid checkpoints */
532 ret = nilfs_cpfile_delete_checkpoint_block(cpfile, cno);
534 nilfs_err(cpfile->i_sb,
535 "error %d deleting checkpoint block", ret);
541 header = kmap_local_folio(header_bh->b_folio, 0);
542 le64_add_cpu(&header->ch_ncheckpoints, -(u64)tnicps);
543 mark_buffer_dirty(header_bh);
544 nilfs_mdt_mark_dirty(cpfile);
545 kunmap_local(header);
553 up_write(&NILFS_MDT(cpfile)->mi_sem);
557 static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode *cpfile,
558 struct nilfs_checkpoint *cp,
559 struct nilfs_cpinfo *ci)
561 ci->ci_flags = le32_to_cpu(cp->cp_flags);
562 ci->ci_cno = le64_to_cpu(cp->cp_cno);
563 ci->ci_create = le64_to_cpu(cp->cp_create);
564 ci->ci_nblk_inc = le64_to_cpu(cp->cp_nblk_inc);
565 ci->ci_inodes_count = le64_to_cpu(cp->cp_inodes_count);
566 ci->ci_blocks_count = le64_to_cpu(cp->cp_blocks_count);
567 ci->ci_next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
570 static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop,
571 void *buf, unsigned int cisz,
574 struct nilfs_checkpoint *cp;
575 struct nilfs_cpinfo *ci = buf;
576 struct buffer_head *bh;
577 size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
578 __u64 cur_cno = nilfs_mdt_cno(cpfile), cno = *cnop;
585 return -ENOENT; /* checkpoint number 0 is invalid */
586 down_read(&NILFS_MDT(cpfile)->mi_sem);
588 for (n = 0; n < nci; cno += ncps) {
589 ret = nilfs_cpfile_find_checkpoint_block(
590 cpfile, cno, cur_cno - 1, &cno, &bh);
592 if (likely(ret == -ENOENT))
596 ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, cur_cno);
598 offset = nilfs_cpfile_checkpoint_offset(cpfile, cno, bh);
599 cp = kaddr = kmap_local_folio(bh->b_folio, offset);
600 for (i = 0; i < ncps && n < nci; i++, cp = (void *)cp + cpsz) {
601 if (!nilfs_checkpoint_invalid(cp)) {
602 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp,
604 ci = (void *)ci + cisz;
614 ci = (void *)ci - cisz;
615 *cnop = ci->ci_cno + 1;
619 up_read(&NILFS_MDT(cpfile)->mi_sem);
623 static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
624 void *buf, unsigned int cisz,
627 struct buffer_head *bh;
628 struct nilfs_cpfile_header *header;
629 struct nilfs_checkpoint *cp;
630 struct nilfs_cpinfo *ci = buf;
631 __u64 curr = *cnop, next;
632 unsigned long curr_blkoff, next_blkoff;
636 down_read(&NILFS_MDT(cpfile)->mi_sem);
639 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
642 header = kmap_local_folio(bh->b_folio, 0);
643 curr = le64_to_cpu(header->ch_snapshot_list.ssl_next);
644 kunmap_local(header);
650 } else if (unlikely(curr == ~(__u64)0)) {
655 curr_blkoff = nilfs_cpfile_get_blkoff(cpfile, curr);
656 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr, 0, &bh);
657 if (unlikely(ret < 0)) {
659 ret = 0; /* No snapshots (started from a hole block) */
662 offset = nilfs_cpfile_checkpoint_offset(cpfile, curr, bh);
663 cp = kmap_local_folio(bh->b_folio, offset);
665 curr = ~(__u64)0; /* Terminator */
666 if (unlikely(nilfs_checkpoint_invalid(cp) ||
667 !nilfs_checkpoint_snapshot(cp)))
669 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, ci);
670 ci = (void *)ci + cisz;
672 next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
674 break; /* reach end of the snapshot list */
677 next_blkoff = nilfs_cpfile_get_blkoff(cpfile, next);
678 if (curr_blkoff != next_blkoff) {
680 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next,
682 if (unlikely(ret < 0)) {
683 WARN_ON(ret == -ENOENT);
687 offset = nilfs_cpfile_checkpoint_offset(cpfile, next, bh);
688 cp = kmap_local_folio(bh->b_folio, offset);
690 curr_blkoff = next_blkoff;
698 up_read(&NILFS_MDT(cpfile)->mi_sem);
703 * nilfs_cpfile_get_cpinfo - get information on checkpoints
704 * @cpfile: checkpoint file inode
705 * @cnop: place to pass a starting checkpoint number and receive a
706 * checkpoint number to continue the search
707 * @mode: mode of checkpoints that the caller wants to retrieve
708 * @buf: buffer for storing checkpoints' information
709 * @cisz: byte size of one checkpoint info item in array
710 * @nci: number of checkpoint info items to retrieve
712 * nilfs_cpfile_get_cpinfo() searches for checkpoints in @mode state
713 * starting from the checkpoint number stored in @cnop, and stores
714 * information about found checkpoints in @buf.
715 * The buffer pointed to by @buf must be large enough to store information
716 * for @nci checkpoints. If at least one checkpoint information is
717 * successfully retrieved, @cnop is updated to point to the checkpoint
718 * number to continue searching.
720 * Return: Count of checkpoint info items stored in the output buffer on
721 * success, or the following negative error code on failure.
722 * * %-EINVAL - Invalid checkpoint mode.
723 * * %-ENOMEM - Insufficient memory available.
724 * * %-EIO - I/O error (including metadata corruption).
725 * * %-ENOENT - Invalid checkpoint number specified.
728 ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode,
729 void *buf, unsigned int cisz, size_t nci)
732 case NILFS_CHECKPOINT:
733 return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, buf, cisz, nci);
735 return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, buf, cisz, nci);
742 * nilfs_cpfile_delete_checkpoint - delete a checkpoint
743 * @cpfile: checkpoint file inode
744 * @cno: checkpoint number to delete
746 * Return: 0 on success, or the following negative error code on failure.
747 * * %-EBUSY - Checkpoint in use (snapshot specified).
748 * * %-EIO - I/O error (including metadata corruption).
749 * * %-ENOENT - No valid checkpoint found.
750 * * %-ENOMEM - Insufficient memory available.
752 int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
754 struct nilfs_cpinfo ci;
758 nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, sizeof(ci), 1);
761 else if (nci == 0 || ci.ci_cno != cno)
763 else if (nilfs_cpinfo_snapshot(&ci))
766 return nilfs_cpfile_delete_checkpoints(cpfile, cno, cno + 1);
769 static int nilfs_cpfile_set_snapshot(struct inode *cpfile, __u64 cno)
771 struct buffer_head *header_bh, *curr_bh, *prev_bh, *cp_bh;
772 struct nilfs_cpfile_header *header;
773 struct nilfs_checkpoint *cp;
774 struct nilfs_snapshot_list *list;
776 unsigned long curr_blkoff, prev_blkoff;
777 size_t offset, curr_list_offset, prev_list_offset;
781 return -ENOENT; /* checkpoint number 0 is invalid */
782 down_write(&NILFS_MDT(cpfile)->mi_sem);
784 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
785 if (unlikely(ret < 0))
788 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
792 offset = nilfs_cpfile_checkpoint_offset(cpfile, cno, cp_bh);
793 cp = kmap_local_folio(cp_bh->b_folio, offset);
794 if (nilfs_checkpoint_invalid(cp)) {
799 if (nilfs_checkpoint_snapshot(cp)) {
807 * Find the last snapshot before the checkpoint being changed to
808 * snapshot mode by going backwards through the snapshot list.
809 * Set "prev" to its checkpoint number, or 0 if not found.
811 header = kmap_local_folio(header_bh->b_folio, 0);
812 list = &header->ch_snapshot_list;
817 curr_list_offset = nilfs_cpfile_ch_snapshot_list_offset();
818 prev = le64_to_cpu(list->ssl_prev);
820 prev_blkoff = nilfs_cpfile_get_blkoff(cpfile, prev);
823 if (curr_blkoff != prev_blkoff) {
825 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr,
827 if (unlikely(ret < 0))
830 curr_list_offset = nilfs_cpfile_cp_snapshot_list_offset(
831 cpfile, curr, curr_bh);
832 list = kmap_local_folio(curr_bh->b_folio, curr_list_offset);
833 curr_blkoff = prev_blkoff;
834 prev = le64_to_cpu(list->ssl_prev);
839 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
844 prev_list_offset = nilfs_cpfile_cp_snapshot_list_offset(
845 cpfile, prev, prev_bh);
849 prev_list_offset = nilfs_cpfile_ch_snapshot_list_offset();
852 /* Update the list entry for the next snapshot */
853 list = kmap_local_folio(curr_bh->b_folio, curr_list_offset);
854 list->ssl_prev = cpu_to_le64(cno);
857 /* Update the checkpoint being changed to a snapshot */
858 offset = nilfs_cpfile_checkpoint_offset(cpfile, cno, cp_bh);
859 cp = kmap_local_folio(cp_bh->b_folio, offset);
860 cp->cp_snapshot_list.ssl_next = cpu_to_le64(curr);
861 cp->cp_snapshot_list.ssl_prev = cpu_to_le64(prev);
862 nilfs_checkpoint_set_snapshot(cp);
865 /* Update the list entry for the previous snapshot */
866 list = kmap_local_folio(prev_bh->b_folio, prev_list_offset);
867 list->ssl_next = cpu_to_le64(cno);
870 /* Update the statistics in the header */
871 header = kmap_local_folio(header_bh->b_folio, 0);
872 le64_add_cpu(&header->ch_nsnapshots, 1);
873 kunmap_local(header);
875 mark_buffer_dirty(prev_bh);
876 mark_buffer_dirty(curr_bh);
877 mark_buffer_dirty(cp_bh);
878 mark_buffer_dirty(header_bh);
879 nilfs_mdt_mark_dirty(cpfile);
893 up_write(&NILFS_MDT(cpfile)->mi_sem);
897 static int nilfs_cpfile_clear_snapshot(struct inode *cpfile, __u64 cno)
899 struct buffer_head *header_bh, *next_bh, *prev_bh, *cp_bh;
900 struct nilfs_cpfile_header *header;
901 struct nilfs_checkpoint *cp;
902 struct nilfs_snapshot_list *list;
904 size_t offset, next_list_offset, prev_list_offset;
908 return -ENOENT; /* checkpoint number 0 is invalid */
909 down_write(&NILFS_MDT(cpfile)->mi_sem);
911 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
912 if (unlikely(ret < 0))
915 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
919 offset = nilfs_cpfile_checkpoint_offset(cpfile, cno, cp_bh);
920 cp = kmap_local_folio(cp_bh->b_folio, offset);
921 if (nilfs_checkpoint_invalid(cp)) {
926 if (!nilfs_checkpoint_snapshot(cp)) {
932 list = &cp->cp_snapshot_list;
933 next = le64_to_cpu(list->ssl_next);
934 prev = le64_to_cpu(list->ssl_prev);
938 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next, 0,
943 next_list_offset = nilfs_cpfile_cp_snapshot_list_offset(
944 cpfile, next, next_bh);
948 next_list_offset = nilfs_cpfile_ch_snapshot_list_offset();
951 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
956 prev_list_offset = nilfs_cpfile_cp_snapshot_list_offset(
957 cpfile, prev, prev_bh);
961 prev_list_offset = nilfs_cpfile_ch_snapshot_list_offset();
964 /* Update the list entry for the next snapshot */
965 list = kmap_local_folio(next_bh->b_folio, next_list_offset);
966 list->ssl_prev = cpu_to_le64(prev);
969 /* Update the list entry for the previous snapshot */
970 list = kmap_local_folio(prev_bh->b_folio, prev_list_offset);
971 list->ssl_next = cpu_to_le64(next);
974 /* Update the snapshot being changed back to a plain checkpoint */
975 cp = kmap_local_folio(cp_bh->b_folio, offset);
976 cp->cp_snapshot_list.ssl_next = cpu_to_le64(0);
977 cp->cp_snapshot_list.ssl_prev = cpu_to_le64(0);
978 nilfs_checkpoint_clear_snapshot(cp);
981 /* Update the statistics in the header */
982 header = kmap_local_folio(header_bh->b_folio, 0);
983 le64_add_cpu(&header->ch_nsnapshots, -1);
984 kunmap_local(header);
986 mark_buffer_dirty(next_bh);
987 mark_buffer_dirty(prev_bh);
988 mark_buffer_dirty(cp_bh);
989 mark_buffer_dirty(header_bh);
990 nilfs_mdt_mark_dirty(cpfile);
1004 up_write(&NILFS_MDT(cpfile)->mi_sem);
1009 * nilfs_cpfile_is_snapshot - determine if checkpoint is a snapshot
1010 * @cpfile: inode of checkpoint file
1011 * @cno: checkpoint number
1013 * Return: 1 if the checkpoint specified by @cno is a snapshot, 0 if not, or
1014 * the following negative error code on failure.
1015 * * %-EIO - I/O error (including metadata corruption).
1016 * * %-ENOENT - No such checkpoint.
1017 * * %-ENOMEM - Insufficient memory available.
1019 int nilfs_cpfile_is_snapshot(struct inode *cpfile, __u64 cno)
1021 struct buffer_head *bh;
1022 struct nilfs_checkpoint *cp;
1027 * CP number is invalid if it's zero or larger than the
1028 * largest existing one.
1030 if (cno == 0 || cno >= nilfs_mdt_cno(cpfile))
1032 down_read(&NILFS_MDT(cpfile)->mi_sem);
1034 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh);
1038 offset = nilfs_cpfile_checkpoint_offset(cpfile, cno, bh);
1039 cp = kmap_local_folio(bh->b_folio, offset);
1040 if (nilfs_checkpoint_invalid(cp))
1043 ret = nilfs_checkpoint_snapshot(cp);
1048 up_read(&NILFS_MDT(cpfile)->mi_sem);
1053 * nilfs_cpfile_change_cpmode - change checkpoint mode
1054 * @cpfile: inode of checkpoint file
1055 * @cno: checkpoint number
1056 * @mode: mode of checkpoint
1058 * Description: nilfs_change_cpmode() changes the mode of the checkpoint
1059 * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT.
1061 * Return Value: On success, 0 is returned. On error, one of the following
1062 * negative error codes is returned.
1064 * %-EIO - I/O error.
1066 * %-ENOMEM - Insufficient amount of memory available.
1068 * %-ENOENT - No such checkpoint.
1070 int nilfs_cpfile_change_cpmode(struct inode *cpfile, __u64 cno, int mode)
1075 case NILFS_CHECKPOINT:
1076 if (nilfs_checkpoint_is_mounted(cpfile->i_sb, cno))
1078 * Current implementation does not have to protect
1079 * plain read-only mounts since they are exclusive
1080 * with a read/write mount and are protected from the
1085 ret = nilfs_cpfile_clear_snapshot(cpfile, cno);
1087 case NILFS_SNAPSHOT:
1088 return nilfs_cpfile_set_snapshot(cpfile, cno);
1095 * nilfs_cpfile_get_stat - get checkpoint statistics
1096 * @cpfile: inode of checkpoint file
1097 * @cpstat: pointer to a structure of checkpoint statistics
1099 * Description: nilfs_cpfile_get_stat() returns information about checkpoints.
1101 * Return Value: On success, 0 is returned, and checkpoints information is
1102 * stored in the place pointed by @cpstat. On error, one of the following
1103 * negative error codes is returned.
1105 * %-EIO - I/O error.
1107 * %-ENOMEM - Insufficient amount of memory available.
1109 int nilfs_cpfile_get_stat(struct inode *cpfile, struct nilfs_cpstat *cpstat)
1111 struct buffer_head *bh;
1112 struct nilfs_cpfile_header *header;
1115 down_read(&NILFS_MDT(cpfile)->mi_sem);
1117 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
1120 header = kmap_local_folio(bh->b_folio, 0);
1121 cpstat->cs_cno = nilfs_mdt_cno(cpfile);
1122 cpstat->cs_ncps = le64_to_cpu(header->ch_ncheckpoints);
1123 cpstat->cs_nsss = le64_to_cpu(header->ch_nsnapshots);
1124 kunmap_local(header);
1128 up_read(&NILFS_MDT(cpfile)->mi_sem);
1133 * nilfs_cpfile_read - read or get cpfile inode
1134 * @sb: super block instance
1135 * @cpsize: size of a checkpoint entry
1136 * @raw_inode: on-disk cpfile inode
1137 * @inodep: buffer to store the inode
1139 int nilfs_cpfile_read(struct super_block *sb, size_t cpsize,
1140 struct nilfs_inode *raw_inode, struct inode **inodep)
1142 struct inode *cpfile;
1145 if (cpsize > sb->s_blocksize) {
1146 nilfs_err(sb, "too large checkpoint size: %zu bytes", cpsize);
1148 } else if (cpsize < NILFS_MIN_CHECKPOINT_SIZE) {
1149 nilfs_err(sb, "too small checkpoint size: %zu bytes", cpsize);
1153 cpfile = nilfs_iget_locked(sb, NULL, NILFS_CPFILE_INO);
1154 if (unlikely(!cpfile))
1156 if (!(cpfile->i_state & I_NEW))
1159 err = nilfs_mdt_init(cpfile, NILFS_MDT_GFP, 0);
1163 nilfs_mdt_set_entry_size(cpfile, cpsize,
1164 sizeof(struct nilfs_cpfile_header));
1166 err = nilfs_read_inode_common(cpfile, raw_inode);
1170 unlock_new_inode(cpfile);
1175 iget_failed(cpfile);