]> git.karo-electronics.de Git - linux-beck.git/blob - fs/nilfs2/cpfile.c
d192b48df9fb9a80d7e67bc971bd0822a27c8fc6
[linux-beck.git] / fs / nilfs2 / cpfile.c
1 /*
2  * cpfile.c - NILFS checkpoint file.
3  *
4  * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
5  *
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.
10  *
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.
15  *
16  * Written by Koji Sato.
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/fs.h>
21 #include <linux/string.h>
22 #include <linux/buffer_head.h>
23 #include <linux/errno.h>
24 #include <linux/nilfs2_fs.h>
25 #include "mdt.h"
26 #include "cpfile.h"
27
28
29 static inline unsigned long
30 nilfs_cpfile_checkpoints_per_block(const struct inode *cpfile)
31 {
32         return NILFS_MDT(cpfile)->mi_entries_per_block;
33 }
34
35 /* block number from the beginning of the file */
36 static unsigned long
37 nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
38 {
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;
42 }
43
44 /* offset in block */
45 static unsigned long
46 nilfs_cpfile_get_offset(const struct inode *cpfile, __u64 cno)
47 {
48         __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
49         return do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
50 }
51
52 static __u64 nilfs_cpfile_first_checkpoint_in_block(const struct inode *cpfile,
53                                                     unsigned long blkoff)
54 {
55         return (__u64)nilfs_cpfile_checkpoints_per_block(cpfile) * blkoff
56                 + 1 - NILFS_MDT(cpfile)->mi_first_entry_offset;
57 }
58
59 static unsigned long
60 nilfs_cpfile_checkpoints_in_block(const struct inode *cpfile,
61                                   __u64 curr,
62                                   __u64 max)
63 {
64         return min_t(__u64,
65                      nilfs_cpfile_checkpoints_per_block(cpfile) -
66                      nilfs_cpfile_get_offset(cpfile, curr),
67                      max - curr);
68 }
69
70 static inline int nilfs_cpfile_is_in_first(const struct inode *cpfile,
71                                            __u64 cno)
72 {
73         return nilfs_cpfile_get_blkoff(cpfile, cno) == 0;
74 }
75
76 static unsigned int
77 nilfs_cpfile_block_add_valid_checkpoints(const struct inode *cpfile,
78                                          struct buffer_head *bh,
79                                          void *kaddr,
80                                          unsigned int n)
81 {
82         struct nilfs_checkpoint *cp = kaddr + bh_offset(bh);
83         unsigned int count;
84
85         count = le32_to_cpu(cp->cp_checkpoints_count) + n;
86         cp->cp_checkpoints_count = cpu_to_le32(count);
87         return count;
88 }
89
90 static unsigned int
91 nilfs_cpfile_block_sub_valid_checkpoints(const struct inode *cpfile,
92                                          struct buffer_head *bh,
93                                          void *kaddr,
94                                          unsigned int n)
95 {
96         struct nilfs_checkpoint *cp = kaddr + bh_offset(bh);
97         unsigned int count;
98
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);
102         return count;
103 }
104
105 static inline struct nilfs_cpfile_header *
106 nilfs_cpfile_block_get_header(const struct inode *cpfile,
107                               struct buffer_head *bh,
108                               void *kaddr)
109 {
110         return kaddr + bh_offset(bh);
111 }
112
113 static struct nilfs_checkpoint *
114 nilfs_cpfile_block_get_checkpoint(const struct inode *cpfile, __u64 cno,
115                                   struct buffer_head *bh,
116                                   void *kaddr)
117 {
118         return kaddr + bh_offset(bh) + nilfs_cpfile_get_offset(cpfile, cno) *
119                 NILFS_MDT(cpfile)->mi_entry_size;
120 }
121
122 static void nilfs_cpfile_block_init(struct inode *cpfile,
123                                     struct buffer_head *bh,
124                                     void *kaddr)
125 {
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);
129
130         while (n-- > 0) {
131                 nilfs_checkpoint_set_invalid(cp);
132                 cp = (void *)cp + cpsz;
133         }
134 }
135
136 static inline int nilfs_cpfile_get_header_block(struct inode *cpfile,
137                                                 struct buffer_head **bhp)
138 {
139         return nilfs_mdt_get_block(cpfile, 0, 0, NULL, bhp);
140 }
141
142 static inline int nilfs_cpfile_get_checkpoint_block(struct inode *cpfile,
143                                                     __u64 cno,
144                                                     int create,
145                                                     struct buffer_head **bhp)
146 {
147         return nilfs_mdt_get_block(cpfile,
148                                    nilfs_cpfile_get_blkoff(cpfile, cno),
149                                    create, nilfs_cpfile_block_init, bhp);
150 }
151
152 /**
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
159  *
160  * Return Value: On success, it returns 0. On error, the following negative
161  * error code is returned.
162  *
163  * %-ENOMEM - Insufficient memory available.
164  *
165  * %-EIO - I/O error
166  *
167  * %-ENOENT - no block exists in the range.
168  */
169 static int nilfs_cpfile_find_checkpoint_block(struct inode *cpfile,
170                                               __u64 start_cno, __u64 end_cno,
171                                               __u64 *cnop,
172                                               struct buffer_head **bhp)
173 {
174         unsigned long start, end, blkoff;
175         int ret;
176
177         if (unlikely(start_cno > end_cno))
178                 return -ENOENT;
179
180         start = nilfs_cpfile_get_blkoff(cpfile, start_cno);
181         end = nilfs_cpfile_get_blkoff(cpfile, end_cno);
182
183         ret = nilfs_mdt_find_block(cpfile, start, end, &blkoff, bhp);
184         if (!ret)
185                 *cnop = (blkoff == start) ? start_cno :
186                         nilfs_cpfile_first_checkpoint_in_block(cpfile, blkoff);
187         return ret;
188 }
189
190 static inline int nilfs_cpfile_delete_checkpoint_block(struct inode *cpfile,
191                                                        __u64 cno)
192 {
193         return nilfs_mdt_delete_block(cpfile,
194                                       nilfs_cpfile_get_blkoff(cpfile, cno));
195 }
196
197 /**
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
204  *
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.
208  *
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.
213  *
214  * %-EIO - I/O error.
215  *
216  * %-ENOMEM - Insufficient amount of memory available.
217  *
218  * %-ENOENT - No such checkpoint.
219  *
220  * %-EINVAL - invalid checkpoint.
221  */
222 int nilfs_cpfile_get_checkpoint(struct inode *cpfile,
223                                 __u64 cno,
224                                 int create,
225                                 struct nilfs_checkpoint **cpp,
226                                 struct buffer_head **bhp)
227 {
228         struct buffer_head *header_bh, *cp_bh;
229         struct nilfs_cpfile_header *header;
230         struct nilfs_checkpoint *cp;
231         void *kaddr;
232         int ret;
233
234         if (unlikely(cno < 1 || cno > nilfs_mdt_cno(cpfile) ||
235                      (cno < nilfs_mdt_cno(cpfile) && create)))
236                 return -EINVAL;
237
238         down_write(&NILFS_MDT(cpfile)->mi_sem);
239
240         ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
241         if (ret < 0)
242                 goto out_sem;
243         ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, create, &cp_bh);
244         if (ret < 0)
245                 goto out_header;
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)) {
249                 if (!create) {
250                         kunmap(cp_bh->b_page);
251                         brelse(cp_bh);
252                         ret = -ENOENT;
253                         goto out_header;
254                 }
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,
259                                                                  kaddr, 1);
260                 mark_buffer_dirty(cp_bh);
261
262                 kaddr = kmap_atomic(header_bh->b_page);
263                 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
264                                                        kaddr);
265                 le64_add_cpu(&header->ch_ncheckpoints, 1);
266                 kunmap_atomic(kaddr);
267                 mark_buffer_dirty(header_bh);
268                 nilfs_mdt_mark_dirty(cpfile);
269         }
270
271         if (cpp != NULL)
272                 *cpp = cp;
273         *bhp = cp_bh;
274
275  out_header:
276         brelse(header_bh);
277
278  out_sem:
279         up_write(&NILFS_MDT(cpfile)->mi_sem);
280         return ret;
281 }
282
283 /**
284  * nilfs_cpfile_put_checkpoint - put a checkpoint
285  * @cpfile: inode of checkpoint file
286  * @cno: checkpoint number
287  * @bh: buffer head
288  *
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.
292  */
293 void nilfs_cpfile_put_checkpoint(struct inode *cpfile, __u64 cno,
294                                  struct buffer_head *bh)
295 {
296         kunmap(bh->b_page);
297         brelse(bh);
298 }
299
300 /**
301  * nilfs_cpfile_delete_checkpoints - delete checkpoints
302  * @cpfile: inode of checkpoint file
303  * @start: start checkpoint number
304  * @end: end checkpoint numer
305  *
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.
309  *
310  * Return Value: On success, 0 is returned. On error, one of the following
311  * negative error codes is returned.
312  *
313  * %-EIO - I/O error.
314  *
315  * %-ENOMEM - Insufficient amount of memory available.
316  *
317  * %-EINVAL - invalid checkpoints.
318  */
319 int nilfs_cpfile_delete_checkpoints(struct inode *cpfile,
320                                     __u64 start,
321                                     __u64 end)
322 {
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;
327         __u64 cno;
328         void *kaddr;
329         unsigned long tnicps;
330         int ret, ncps, nicps, nss, count, i;
331
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);
336                 return -EINVAL;
337         }
338
339         down_write(&NILFS_MDT(cpfile)->mi_sem);
340
341         ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
342         if (ret < 0)
343                 goto out_sem;
344         tnicps = 0;
345         nss = 0;
346
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);
350                 if (ret < 0) {
351                         if (ret != -ENOENT)
352                                 break;
353                         /* skip hole */
354                         ret = 0;
355                         continue;
356                 }
357
358                 kaddr = kmap_atomic(cp_bh->b_page);
359                 cp = nilfs_cpfile_block_get_checkpoint(
360                         cpfile, cno, cp_bh, kaddr);
361                 nicps = 0;
362                 for (i = 0; i < ncps; i++, cp = (void *)cp + cpsz) {
363                         if (nilfs_checkpoint_snapshot(cp)) {
364                                 nss++;
365                         } else if (!nilfs_checkpoint_invalid(cp)) {
366                                 nilfs_checkpoint_set_invalid(cp);
367                                 nicps++;
368                         }
369                 }
370                 if (nicps > 0) {
371                         tnicps += nicps;
372                         mark_buffer_dirty(cp_bh);
373                         nilfs_mdt_mark_dirty(cpfile);
374                         if (!nilfs_cpfile_is_in_first(cpfile, cno)) {
375                                 count =
376                                   nilfs_cpfile_block_sub_valid_checkpoints(
377                                                 cpfile, cp_bh, kaddr, nicps);
378                                 if (count == 0) {
379                                         /* make hole */
380                                         kunmap_atomic(kaddr);
381                                         brelse(cp_bh);
382                                         ret =
383                                           nilfs_cpfile_delete_checkpoint_block(
384                                                                    cpfile, cno);
385                                         if (ret == 0)
386                                                 continue;
387                                         printk(KERN_ERR
388                                                "%s: cannot delete block\n",
389                                                __func__);
390                                         break;
391                                 }
392                         }
393                 }
394
395                 kunmap_atomic(kaddr);
396                 brelse(cp_bh);
397         }
398
399         if (tnicps > 0) {
400                 kaddr = kmap_atomic(header_bh->b_page);
401                 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
402                                                        kaddr);
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);
407         }
408
409         brelse(header_bh);
410         if (nss > 0)
411                 ret = -EBUSY;
412
413  out_sem:
414         up_write(&NILFS_MDT(cpfile)->mi_sem);
415         return ret;
416 }
417
418 static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode *cpfile,
419                                               struct nilfs_checkpoint *cp,
420                                               struct nilfs_cpinfo *ci)
421 {
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);
429 }
430
431 static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop,
432                                           void *buf, unsigned cisz, size_t nci)
433 {
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;
439         void *kaddr;
440         int n, ret;
441         int ncps, i;
442
443         if (cno == 0)
444                 return -ENOENT; /* checkpoint number 0 is invalid */
445         down_read(&NILFS_MDT(cpfile)->mi_sem);
446
447         for (n = 0; n < nci; cno += ncps) {
448                 ret = nilfs_cpfile_find_checkpoint_block(
449                         cpfile, cno, cur_cno - 1, &cno, &bh);
450                 if (ret < 0) {
451                         if (likely(ret == -ENOENT))
452                                 break;
453                         goto out;
454                 }
455                 ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, cur_cno);
456
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,
462                                                                   ci);
463                                 ci = (void *)ci + cisz;
464                                 n++;
465                         }
466                 }
467                 kunmap_atomic(kaddr);
468                 brelse(bh);
469         }
470
471         ret = n;
472         if (n > 0) {
473                 ci = (void *)ci - cisz;
474                 *cnop = ci->ci_cno + 1;
475         }
476
477  out:
478         up_read(&NILFS_MDT(cpfile)->mi_sem);
479         return ret;
480 }
481
482 static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
483                                           void *buf, unsigned cisz, size_t nci)
484 {
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;
491         void *kaddr;
492         int n = 0, ret;
493
494         down_read(&NILFS_MDT(cpfile)->mi_sem);
495
496         if (curr == 0) {
497                 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
498                 if (ret < 0)
499                         goto out;
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);
504                 brelse(bh);
505                 if (curr == 0) {
506                         ret = 0;
507                         goto out;
508                 }
509         } else if (unlikely(curr == ~(__u64)0)) {
510                 ret = 0;
511                 goto out;
512         }
513
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)) {
517                 if (ret == -ENOENT)
518                         ret = 0; /* No snapshots (started from a hole block) */
519                 goto out;
520         }
521         kaddr = kmap_atomic(bh->b_page);
522         while (n < nci) {
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)))
527                         break;
528                 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, ci);
529                 ci = (void *)ci + cisz;
530                 n++;
531                 next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
532                 if (next == 0)
533                         break; /* reach end of the snapshot list */
534
535                 next_blkoff = nilfs_cpfile_get_blkoff(cpfile, next);
536                 if (curr_blkoff != next_blkoff) {
537                         kunmap_atomic(kaddr);
538                         brelse(bh);
539                         ret = nilfs_cpfile_get_checkpoint_block(cpfile, next,
540                                                                 0, &bh);
541                         if (unlikely(ret < 0)) {
542                                 WARN_ON(ret == -ENOENT);
543                                 goto out;
544                         }
545                         kaddr = kmap_atomic(bh->b_page);
546                 }
547                 curr = next;
548                 curr_blkoff = next_blkoff;
549         }
550         kunmap_atomic(kaddr);
551         brelse(bh);
552         *cnop = curr;
553         ret = n;
554
555  out:
556         up_read(&NILFS_MDT(cpfile)->mi_sem);
557         return ret;
558 }
559
560 /**
561  * nilfs_cpfile_get_cpinfo -
562  * @cpfile:
563  * @cno:
564  * @ci:
565  * @nci:
566  */
567
568 ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode,
569                                 void *buf, unsigned cisz, size_t nci)
570 {
571         switch (mode) {
572         case NILFS_CHECKPOINT:
573                 return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, buf, cisz, nci);
574         case NILFS_SNAPSHOT:
575                 return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, buf, cisz, nci);
576         default:
577                 return -EINVAL;
578         }
579 }
580
581 /**
582  * nilfs_cpfile_delete_checkpoint -
583  * @cpfile:
584  * @cno:
585  */
586 int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
587 {
588         struct nilfs_cpinfo ci;
589         __u64 tcno = cno;
590         ssize_t nci;
591
592         nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, sizeof(ci), 1);
593         if (nci < 0)
594                 return nci;
595         else if (nci == 0 || ci.ci_cno != cno)
596                 return -ENOENT;
597         else if (nilfs_cpinfo_snapshot(&ci))
598                 return -EBUSY;
599
600         return nilfs_cpfile_delete_checkpoints(cpfile, cno, cno + 1);
601 }
602
603 static struct nilfs_snapshot_list *
604 nilfs_cpfile_block_get_snapshot_list(const struct inode *cpfile,
605                                      __u64 cno,
606                                      struct buffer_head *bh,
607                                      void *kaddr)
608 {
609         struct nilfs_cpfile_header *header;
610         struct nilfs_checkpoint *cp;
611         struct nilfs_snapshot_list *list;
612
613         if (cno != 0) {
614                 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
615                 list = &cp->cp_snapshot_list;
616         } else {
617                 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
618                 list = &header->ch_snapshot_list;
619         }
620         return list;
621 }
622
623 static int nilfs_cpfile_set_snapshot(struct inode *cpfile, __u64 cno)
624 {
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;
629         __u64 curr, prev;
630         unsigned long curr_blkoff, prev_blkoff;
631         void *kaddr;
632         int ret;
633
634         if (cno == 0)
635                 return -ENOENT; /* checkpoint number 0 is invalid */
636         down_write(&NILFS_MDT(cpfile)->mi_sem);
637
638         ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
639         if (ret < 0)
640                 goto out_sem;
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)) {
644                 ret = -ENOENT;
645                 kunmap_atomic(kaddr);
646                 goto out_cp;
647         }
648         if (nilfs_checkpoint_snapshot(cp)) {
649                 ret = 0;
650                 kunmap_atomic(kaddr);
651                 goto out_cp;
652         }
653         kunmap_atomic(kaddr);
654
655         ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
656         if (ret < 0)
657                 goto out_cp;
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;
661         curr_bh = header_bh;
662         get_bh(curr_bh);
663         curr = 0;
664         curr_blkoff = 0;
665         prev = le64_to_cpu(list->ssl_prev);
666         while (prev > cno) {
667                 prev_blkoff = nilfs_cpfile_get_blkoff(cpfile, prev);
668                 curr = prev;
669                 if (curr_blkoff != prev_blkoff) {
670                         kunmap_atomic(kaddr);
671                         brelse(curr_bh);
672                         ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr,
673                                                                 0, &curr_bh);
674                         if (ret < 0)
675                                 goto out_header;
676                         kaddr = kmap_atomic(curr_bh->b_page);
677                 }
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);
683         }
684         kunmap_atomic(kaddr);
685
686         if (prev != 0) {
687                 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
688                                                         &prev_bh);
689                 if (ret < 0)
690                         goto out_curr;
691         } else {
692                 prev_bh = header_bh;
693                 get_bh(prev_bh);
694         }
695
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);
701
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);
708
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);
714
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);
719
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);
725
726         brelse(prev_bh);
727
728  out_curr:
729         brelse(curr_bh);
730
731  out_header:
732         brelse(header_bh);
733
734  out_cp:
735         brelse(cp_bh);
736
737  out_sem:
738         up_write(&NILFS_MDT(cpfile)->mi_sem);
739         return ret;
740 }
741
742 static int nilfs_cpfile_clear_snapshot(struct inode *cpfile, __u64 cno)
743 {
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;
748         __u64 next, prev;
749         void *kaddr;
750         int ret;
751
752         if (cno == 0)
753                 return -ENOENT; /* checkpoint number 0 is invalid */
754         down_write(&NILFS_MDT(cpfile)->mi_sem);
755
756         ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
757         if (ret < 0)
758                 goto out_sem;
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)) {
762                 ret = -ENOENT;
763                 kunmap_atomic(kaddr);
764                 goto out_cp;
765         }
766         if (!nilfs_checkpoint_snapshot(cp)) {
767                 ret = 0;
768                 kunmap_atomic(kaddr);
769                 goto out_cp;
770         }
771
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);
776
777         ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
778         if (ret < 0)
779                 goto out_cp;
780         if (next != 0) {
781                 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next, 0,
782                                                         &next_bh);
783                 if (ret < 0)
784                         goto out_header;
785         } else {
786                 next_bh = header_bh;
787                 get_bh(next_bh);
788         }
789         if (prev != 0) {
790                 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
791                                                         &prev_bh);
792                 if (ret < 0)
793                         goto out_next;
794         } else {
795                 prev_bh = header_bh;
796                 get_bh(prev_bh);
797         }
798
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);
804
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);
810
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);
817
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);
822
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);
828
829         brelse(prev_bh);
830
831  out_next:
832         brelse(next_bh);
833
834  out_header:
835         brelse(header_bh);
836
837  out_cp:
838         brelse(cp_bh);
839
840  out_sem:
841         up_write(&NILFS_MDT(cpfile)->mi_sem);
842         return ret;
843 }
844
845 /**
846  * nilfs_cpfile_is_snapshot -
847  * @cpfile: inode of checkpoint file
848  * @cno: checkpoint number
849  *
850  * Description:
851  *
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.
855  *
856  * %-EIO - I/O error.
857  *
858  * %-ENOMEM - Insufficient amount of memory available.
859  *
860  * %-ENOENT - No such checkpoint.
861  */
862 int nilfs_cpfile_is_snapshot(struct inode *cpfile, __u64 cno)
863 {
864         struct buffer_head *bh;
865         struct nilfs_checkpoint *cp;
866         void *kaddr;
867         int ret;
868
869         /* CP number is invalid if it's zero or larger than the
870         largest exist one.*/
871         if (cno == 0 || cno >= nilfs_mdt_cno(cpfile))
872                 return -ENOENT;
873         down_read(&NILFS_MDT(cpfile)->mi_sem);
874
875         ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh);
876         if (ret < 0)
877                 goto out;
878         kaddr = kmap_atomic(bh->b_page);
879         cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
880         if (nilfs_checkpoint_invalid(cp))
881                 ret = -ENOENT;
882         else
883                 ret = nilfs_checkpoint_snapshot(cp);
884         kunmap_atomic(kaddr);
885         brelse(bh);
886
887  out:
888         up_read(&NILFS_MDT(cpfile)->mi_sem);
889         return ret;
890 }
891
892 /**
893  * nilfs_cpfile_change_cpmode - change checkpoint mode
894  * @cpfile: inode of checkpoint file
895  * @cno: checkpoint number
896  * @status: mode of checkpoint
897  *
898  * Description: nilfs_change_cpmode() changes the mode of the checkpoint
899  * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT.
900  *
901  * Return Value: On success, 0 is returned. On error, one of the following
902  * negative error codes is returned.
903  *
904  * %-EIO - I/O error.
905  *
906  * %-ENOMEM - Insufficient amount of memory available.
907  *
908  * %-ENOENT - No such checkpoint.
909  */
910 int nilfs_cpfile_change_cpmode(struct inode *cpfile, __u64 cno, int mode)
911 {
912         int ret;
913
914         switch (mode) {
915         case NILFS_CHECKPOINT:
916                 if (nilfs_checkpoint_is_mounted(cpfile->i_sb, cno))
917                         /*
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
921                          * cleaner.
922                          */
923                         ret = -EBUSY;
924                 else
925                         ret = nilfs_cpfile_clear_snapshot(cpfile, cno);
926                 return ret;
927         case NILFS_SNAPSHOT:
928                 return nilfs_cpfile_set_snapshot(cpfile, cno);
929         default:
930                 return -EINVAL;
931         }
932 }
933
934 /**
935  * nilfs_cpfile_get_stat - get checkpoint statistics
936  * @cpfile: inode of checkpoint file
937  * @stat: pointer to a structure of checkpoint statistics
938  *
939  * Description: nilfs_cpfile_get_stat() returns information about checkpoints.
940  *
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.
944  *
945  * %-EIO - I/O error.
946  *
947  * %-ENOMEM - Insufficient amount of memory available.
948  */
949 int nilfs_cpfile_get_stat(struct inode *cpfile, struct nilfs_cpstat *cpstat)
950 {
951         struct buffer_head *bh;
952         struct nilfs_cpfile_header *header;
953         void *kaddr;
954         int ret;
955
956         down_read(&NILFS_MDT(cpfile)->mi_sem);
957
958         ret = nilfs_cpfile_get_header_block(cpfile, &bh);
959         if (ret < 0)
960                 goto out_sem;
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);
967         brelse(bh);
968
969  out_sem:
970         up_read(&NILFS_MDT(cpfile)->mi_sem);
971         return ret;
972 }
973
974 /**
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
980  */
981 int nilfs_cpfile_read(struct super_block *sb, size_t cpsize,
982                       struct nilfs_inode *raw_inode, struct inode **inodep)
983 {
984         struct inode *cpfile;
985         int err;
986
987         if (cpsize > sb->s_blocksize) {
988                 printk(KERN_ERR
989                        "NILFS: too large checkpoint size: %zu bytes.\n",
990                        cpsize);
991                 return -EINVAL;
992         } else if (cpsize < NILFS_MIN_CHECKPOINT_SIZE) {
993                 printk(KERN_ERR
994                        "NILFS: too small checkpoint size: %zu bytes.\n",
995                        cpsize);
996                 return -EINVAL;
997         }
998
999         cpfile = nilfs_iget_locked(sb, NULL, NILFS_CPFILE_INO);
1000         if (unlikely(!cpfile))
1001                 return -ENOMEM;
1002         if (!(cpfile->i_state & I_NEW))
1003                 goto out;
1004
1005         err = nilfs_mdt_init(cpfile, NILFS_MDT_GFP, 0);
1006         if (err)
1007                 goto failed;
1008
1009         nilfs_mdt_set_entry_size(cpfile, cpsize,
1010                                  sizeof(struct nilfs_cpfile_header));
1011
1012         err = nilfs_read_inode_common(cpfile, raw_inode);
1013         if (err)
1014                 goto failed;
1015
1016         unlock_new_inode(cpfile);
1017  out:
1018         *inodep = cpfile;
1019         return 0;
1020  failed:
1021         iget_failed(cpfile);
1022         return err;
1023 }