blob: 218b344185082fb29ed8741822c2efb37c9616fc [file] [log] [blame]
Koji Sato29619802009-04-06 19:01:31 -07001/*
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 * 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
19 *
20 * Written by Koji Sato <koji@osrg.net>.
21 */
22
23#include <linux/kernel.h>
24#include <linux/fs.h>
25#include <linux/string.h>
26#include <linux/buffer_head.h>
27#include <linux/errno.h>
28#include <linux/nilfs2_fs.h>
29#include "mdt.h"
30#include "cpfile.h"
31
32
33static inline unsigned long
34nilfs_cpfile_checkpoints_per_block(const struct inode *cpfile)
35{
36 return NILFS_MDT(cpfile)->mi_entries_per_block;
37}
38
39/* block number from the beginning of the file */
40static unsigned long
41nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
42{
43 __u64 tcno;
44
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;
49}
50
51/* offset in block */
52static unsigned long
53nilfs_cpfile_get_offset(const struct inode *cpfile, __u64 cno)
54{
55 __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
56 return do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
57}
58
59static unsigned long
60nilfs_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
70static 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
76static unsigned int
77nilfs_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
90static unsigned int
91nilfs_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 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);
102 return count;
103}
104
105static inline struct nilfs_cpfile_header *
106nilfs_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
113static struct nilfs_checkpoint *
114nilfs_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
122static 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
136static 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
142static 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
152static inline int nilfs_cpfile_delete_checkpoint_block(struct inode *cpfile,
153 __u64 cno)
154{
155 return nilfs_mdt_delete_block(cpfile,
156 nilfs_cpfile_get_blkoff(cpfile, cno));
157}
158
159/**
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
166 *
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.
170 *
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.
175 *
176 * %-EIO - I/O error.
177 *
178 * %-ENOMEM - Insufficient amount of memory available.
179 *
180 * %-ENOENT - No such checkpoint.
181 */
182int nilfs_cpfile_get_checkpoint(struct inode *cpfile,
183 __u64 cno,
184 int create,
185 struct nilfs_checkpoint **cpp,
186 struct buffer_head **bhp)
187{
188 struct buffer_head *header_bh, *cp_bh;
189 struct nilfs_cpfile_header *header;
190 struct nilfs_checkpoint *cp;
191 void *kaddr;
192 int ret;
193
194 BUG_ON(cno < 1 || cno > nilfs_mdt_cno(cpfile) ||
195 (cno < nilfs_mdt_cno(cpfile) && create));
196
197 down_write(&NILFS_MDT(cpfile)->mi_sem);
198
199 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
200 if (ret < 0)
201 goto out_sem;
202 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, create, &cp_bh);
203 if (ret < 0)
204 goto out_header;
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)) {
208 if (!create) {
209 kunmap(cp_bh->b_page);
210 brelse(cp_bh);
211 ret = -ENOENT;
212 goto out_header;
213 }
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,
218 kaddr, 1);
219 nilfs_mdt_mark_buffer_dirty(cp_bh);
220
221 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
222 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
223 kaddr);
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);
228 }
229
230 if (cpp != NULL)
231 *cpp = cp;
232 *bhp = cp_bh;
233
234 out_header:
235 brelse(header_bh);
236
237 out_sem:
238 up_write(&NILFS_MDT(cpfile)->mi_sem);
239 return ret;
240}
241
242/**
243 * nilfs_cpfile_put_checkpoint - put a checkpoint
244 * @cpfile: inode of checkpoint file
245 * @cno: checkpoint number
246 * @bh: buffer head
247 *
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.
251 */
252void nilfs_cpfile_put_checkpoint(struct inode *cpfile, __u64 cno,
253 struct buffer_head *bh)
254{
255 kunmap(bh->b_page);
256 brelse(bh);
257}
258
259/**
260 * nilfs_cpfile_delete_checkpoints - delete checkpoints
261 * @cpfile: inode of checkpoint file
262 * @start: start checkpoint number
263 * @end: end checkpoint numer
264 *
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.
268 *
269 * Return Value: On success, 0 is returned. On error, one of the following
270 * negative error codes is returned.
271 *
272 * %-EIO - I/O error.
273 *
274 * %-ENOMEM - Insufficient amount of memory available.
275 *
276 * %-EINVAL - invalid checkpoints.
277 */
278int nilfs_cpfile_delete_checkpoints(struct inode *cpfile,
279 __u64 start,
280 __u64 end)
281{
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;
286 __u64 cno;
287 void *kaddr;
288 unsigned long tnicps;
289 int ret, ncps, nicps, count, i;
290
291 if ((start == 0) || (start > end)) {
292 printk(KERN_CRIT "%s: start = %llu, end = %llu\n",
293 __func__,
294 (unsigned long long)start,
295 (unsigned long long)end);
296 BUG();
297 }
298
299 /* cannot delete the latest checkpoint */
300 if (start == nilfs_mdt_cno(cpfile) - 1)
301 return -EPERM;
302
303 down_write(&NILFS_MDT(cpfile)->mi_sem);
304
305 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
306 if (ret < 0)
307 goto out_sem;
308 tnicps = 0;
309
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);
313 if (ret < 0) {
314 if (ret != -ENOENT)
315 goto out_sem;
316 /* skip hole */
317 ret = 0;
318 continue;
319 }
320
321 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
322 cp = nilfs_cpfile_block_get_checkpoint(
323 cpfile, cno, cp_bh, kaddr);
324 nicps = 0;
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);
329 nicps++;
330 }
331 }
332 if (nicps > 0) {
333 tnicps += nicps;
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) {
339 /* make hole */
340 kunmap_atomic(kaddr, KM_USER0);
341 brelse(cp_bh);
342 ret = nilfs_cpfile_delete_checkpoint_block(
343 cpfile, cno);
344 if (ret == 0)
345 continue;
346 printk(KERN_ERR "%s: cannot delete block\n",
347 __func__);
348 goto out_sem;
349 }
350 }
351
352 kunmap_atomic(kaddr, KM_USER0);
353 brelse(cp_bh);
354 }
355
356 if (tnicps > 0) {
357 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
358 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
359 kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700360 le64_add_cpu(&header->ch_ncheckpoints, -(u64)tnicps);
Koji Sato29619802009-04-06 19:01:31 -0700361 nilfs_mdt_mark_buffer_dirty(header_bh);
362 nilfs_mdt_mark_dirty(cpfile);
363 kunmap_atomic(kaddr, KM_USER0);
364 }
365 brelse(header_bh);
366
367 out_sem:
368 up_write(&NILFS_MDT(cpfile)->mi_sem);
369 return ret;
370}
371
372static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode *cpfile,
373 struct nilfs_checkpoint *cp,
374 struct nilfs_cpinfo *ci)
375{
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);
383}
384
Ryusuke Konishi76068c42009-04-06 19:01:50 -0700385static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop,
Koji Sato29619802009-04-06 19:01:31 -0700386 struct nilfs_cpinfo *ci, size_t nci)
387{
388 struct nilfs_checkpoint *cp;
389 struct buffer_head *bh;
390 size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
Ryusuke Konishi76068c42009-04-06 19:01:50 -0700391 __u64 cur_cno = nilfs_mdt_cno(cpfile), cno = *cnop;
Koji Sato29619802009-04-06 19:01:31 -0700392 void *kaddr;
393 int n, ret;
394 int ncps, i;
395
396 down_read(&NILFS_MDT(cpfile)->mi_sem);
397
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);
401 if (ret < 0) {
402 if (ret != -ENOENT)
403 goto out;
404 continue; /* skip hole */
405 }
406
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++]);
413 }
414 kunmap_atomic(kaddr, KM_USER0);
415 brelse(bh);
416 }
417
418 ret = n;
Ryusuke Konishi76068c42009-04-06 19:01:50 -0700419 if (n > 0)
420 *cnop = ci[n - 1].ci_cno + 1;
Koji Sato29619802009-04-06 19:01:31 -0700421
422 out:
423 up_read(&NILFS_MDT(cpfile)->mi_sem);
424 return ret;
425}
426
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700427static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
Koji Sato29619802009-04-06 19:01:31 -0700428 struct nilfs_cpinfo *ci, size_t nci)
429{
430 struct buffer_head *bh;
431 struct nilfs_cpfile_header *header;
432 struct nilfs_checkpoint *cp;
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700433 __u64 curr = *cnop, next;
Koji Sato29619802009-04-06 19:01:31 -0700434 unsigned long curr_blkoff, next_blkoff;
435 void *kaddr;
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700436 int n = 0, ret;
Koji Sato29619802009-04-06 19:01:31 -0700437
438 down_read(&NILFS_MDT(cpfile)->mi_sem);
439
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700440 if (curr == 0) {
Koji Sato29619802009-04-06 19:01:31 -0700441 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
442 if (ret < 0)
443 goto out;
444 kaddr = kmap_atomic(bh->b_page, KM_USER0);
445 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
446 curr = le64_to_cpu(header->ch_snapshot_list.ssl_next);
447 kunmap_atomic(kaddr, KM_USER0);
448 brelse(bh);
449 if (curr == 0) {
450 ret = 0;
451 goto out;
452 }
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700453 } else if (unlikely(curr == ~(__u64)0)) {
454 ret = 0;
455 goto out;
456 }
457
Koji Sato29619802009-04-06 19:01:31 -0700458 curr_blkoff = nilfs_cpfile_get_blkoff(cpfile, curr);
459 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr, 0, &bh);
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700460 if (unlikely(ret < 0)) {
461 if (ret == -ENOENT)
462 ret = 0; /* No snapshots (started from a hole block) */
Koji Sato29619802009-04-06 19:01:31 -0700463 goto out;
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700464 }
Koji Sato29619802009-04-06 19:01:31 -0700465 kaddr = kmap_atomic(bh->b_page, KM_USER0);
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700466 while (n < nci) {
467 cp = nilfs_cpfile_block_get_checkpoint(cpfile, curr, bh, kaddr);
468 curr = ~(__u64)0; /* Terminator */
469 if (unlikely(nilfs_checkpoint_invalid(cp) ||
470 !nilfs_checkpoint_snapshot(cp)))
Koji Sato29619802009-04-06 19:01:31 -0700471 break;
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700472 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, &ci[n++]);
473 next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
474 if (next == 0)
475 break; /* reach end of the snapshot list */
476
Koji Sato29619802009-04-06 19:01:31 -0700477 next_blkoff = nilfs_cpfile_get_blkoff(cpfile, next);
478 if (curr_blkoff != next_blkoff) {
479 kunmap_atomic(kaddr, KM_USER0);
480 brelse(bh);
481 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next,
482 0, &bh);
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700483 if (unlikely(ret < 0)) {
484 WARN_ON(ret == -ENOENT);
Koji Sato29619802009-04-06 19:01:31 -0700485 goto out;
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700486 }
Koji Sato29619802009-04-06 19:01:31 -0700487 kaddr = kmap_atomic(bh->b_page, KM_USER0);
488 }
489 curr = next;
490 curr_blkoff = next_blkoff;
491 }
492 kunmap_atomic(kaddr, KM_USER0);
493 brelse(bh);
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700494 *cnop = curr;
Koji Sato29619802009-04-06 19:01:31 -0700495 ret = n;
496
497 out:
498 up_read(&NILFS_MDT(cpfile)->mi_sem);
499 return ret;
500}
501
502/**
503 * nilfs_cpfile_get_cpinfo -
504 * @cpfile:
505 * @cno:
506 * @ci:
507 * @nci:
508 */
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700509
510ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode,
Koji Sato29619802009-04-06 19:01:31 -0700511 struct nilfs_cpinfo *ci, size_t nci)
512{
513 switch (mode) {
514 case NILFS_CHECKPOINT:
Ryusuke Konishi76068c42009-04-06 19:01:50 -0700515 return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, ci, nci);
Koji Sato29619802009-04-06 19:01:31 -0700516 case NILFS_SNAPSHOT:
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700517 return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, ci, nci);
Koji Sato29619802009-04-06 19:01:31 -0700518 default:
519 return -EINVAL;
520 }
521}
522
523/**
524 * nilfs_cpfile_delete_checkpoint -
525 * @cpfile:
526 * @cno:
527 */
528int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
529{
530 struct nilfs_cpinfo ci;
Ryusuke Konishi76068c42009-04-06 19:01:50 -0700531 __u64 tcno = cno;
Koji Sato29619802009-04-06 19:01:31 -0700532 ssize_t nci;
533 int ret;
534
535 /* checkpoint number 0 is invalid */
536 if (cno == 0)
537 return -ENOENT;
Ryusuke Konishi76068c42009-04-06 19:01:50 -0700538 nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, 1);
Koji Sato29619802009-04-06 19:01:31 -0700539 if (nci < 0)
540 return nci;
541 else if (nci == 0 || ci.ci_cno != cno)
542 return -ENOENT;
543
544 /* cannot delete the latest checkpoint nor snapshots */
545 ret = nilfs_cpinfo_snapshot(&ci);
546 if (ret < 0)
547 return ret;
548 else if (ret > 0 || cno == nilfs_mdt_cno(cpfile) - 1)
549 return -EPERM;
550
551 return nilfs_cpfile_delete_checkpoints(cpfile, cno, cno + 1);
552}
553
554static struct nilfs_snapshot_list *
555nilfs_cpfile_block_get_snapshot_list(const struct inode *cpfile,
556 __u64 cno,
557 struct buffer_head *bh,
558 void *kaddr)
559{
560 struct nilfs_cpfile_header *header;
561 struct nilfs_checkpoint *cp;
562 struct nilfs_snapshot_list *list;
563
564 if (cno != 0) {
565 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
566 list = &cp->cp_snapshot_list;
567 } else {
568 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
569 list = &header->ch_snapshot_list;
570 }
571 return list;
572}
573
574static int nilfs_cpfile_set_snapshot(struct inode *cpfile, __u64 cno)
575{
576 struct buffer_head *header_bh, *curr_bh, *prev_bh, *cp_bh;
577 struct nilfs_cpfile_header *header;
578 struct nilfs_checkpoint *cp;
579 struct nilfs_snapshot_list *list;
580 __u64 curr, prev;
581 unsigned long curr_blkoff, prev_blkoff;
582 void *kaddr;
583 int ret;
584
585 down_write(&NILFS_MDT(cpfile)->mi_sem);
586
587 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
588 if (ret < 0)
589 goto out_sem;
590 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
591 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
592 if (nilfs_checkpoint_invalid(cp)) {
593 ret = -ENOENT;
594 kunmap_atomic(kaddr, KM_USER0);
595 goto out_cp;
596 }
597 if (nilfs_checkpoint_snapshot(cp)) {
598 ret = 0;
599 kunmap_atomic(kaddr, KM_USER0);
600 goto out_cp;
601 }
602 kunmap_atomic(kaddr, KM_USER0);
603
604 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
605 if (ret < 0)
606 goto out_cp;
607 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
608 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
609 list = &header->ch_snapshot_list;
610 curr_bh = header_bh;
611 get_bh(curr_bh);
612 curr = 0;
613 curr_blkoff = 0;
614 prev = le64_to_cpu(list->ssl_prev);
615 while (prev > cno) {
616 prev_blkoff = nilfs_cpfile_get_blkoff(cpfile, prev);
617 curr = prev;
618 if (curr_blkoff != prev_blkoff) {
619 kunmap_atomic(kaddr, KM_USER0);
620 brelse(curr_bh);
621 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr,
622 0, &curr_bh);
623 if (ret < 0)
624 goto out_header;
625 kaddr = kmap_atomic(curr_bh->b_page, KM_USER0);
626 }
627 curr_blkoff = prev_blkoff;
628 cp = nilfs_cpfile_block_get_checkpoint(
629 cpfile, curr, curr_bh, kaddr);
630 list = &cp->cp_snapshot_list;
631 prev = le64_to_cpu(list->ssl_prev);
632 }
633 kunmap_atomic(kaddr, KM_USER0);
634
635 if (prev != 0) {
636 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
637 &prev_bh);
638 if (ret < 0)
639 goto out_curr;
640 } else {
641 prev_bh = header_bh;
642 get_bh(prev_bh);
643 }
644
645 kaddr = kmap_atomic(curr_bh->b_page, KM_USER0);
646 list = nilfs_cpfile_block_get_snapshot_list(
647 cpfile, curr, curr_bh, kaddr);
648 list->ssl_prev = cpu_to_le64(cno);
649 kunmap_atomic(kaddr, KM_USER0);
650
651 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
652 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
653 cp->cp_snapshot_list.ssl_next = cpu_to_le64(curr);
654 cp->cp_snapshot_list.ssl_prev = cpu_to_le64(prev);
655 nilfs_checkpoint_set_snapshot(cp);
656 kunmap_atomic(kaddr, KM_USER0);
657
658 kaddr = kmap_atomic(prev_bh->b_page, KM_USER0);
659 list = nilfs_cpfile_block_get_snapshot_list(
660 cpfile, prev, prev_bh, kaddr);
661 list->ssl_next = cpu_to_le64(cno);
662 kunmap_atomic(kaddr, KM_USER0);
663
664 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
665 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
666 le64_add_cpu(&header->ch_nsnapshots, 1);
667 kunmap_atomic(kaddr, KM_USER0);
668
669 nilfs_mdt_mark_buffer_dirty(prev_bh);
670 nilfs_mdt_mark_buffer_dirty(curr_bh);
671 nilfs_mdt_mark_buffer_dirty(cp_bh);
672 nilfs_mdt_mark_buffer_dirty(header_bh);
673 nilfs_mdt_mark_dirty(cpfile);
674
675 brelse(prev_bh);
676
677 out_curr:
678 brelse(curr_bh);
679
680 out_header:
681 brelse(header_bh);
682
683 out_cp:
684 brelse(cp_bh);
685
686 out_sem:
687 up_write(&NILFS_MDT(cpfile)->mi_sem);
688 return ret;
689}
690
691static int nilfs_cpfile_clear_snapshot(struct inode *cpfile, __u64 cno)
692{
693 struct buffer_head *header_bh, *next_bh, *prev_bh, *cp_bh;
694 struct nilfs_cpfile_header *header;
695 struct nilfs_checkpoint *cp;
696 struct nilfs_snapshot_list *list;
697 __u64 next, prev;
698 void *kaddr;
699 int ret;
700
701 down_write(&NILFS_MDT(cpfile)->mi_sem);
702
703 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
704 if (ret < 0)
705 goto out_sem;
706 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
707 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
708 if (nilfs_checkpoint_invalid(cp)) {
709 ret = -ENOENT;
710 kunmap_atomic(kaddr, KM_USER0);
711 goto out_cp;
712 }
713 if (!nilfs_checkpoint_snapshot(cp)) {
714 ret = 0;
715 kunmap_atomic(kaddr, KM_USER0);
716 goto out_cp;
717 }
718
719 list = &cp->cp_snapshot_list;
720 next = le64_to_cpu(list->ssl_next);
721 prev = le64_to_cpu(list->ssl_prev);
722 kunmap_atomic(kaddr, KM_USER0);
723
724 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
725 if (ret < 0)
726 goto out_cp;
727 if (next != 0) {
728 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next, 0,
729 &next_bh);
730 if (ret < 0)
731 goto out_header;
732 } else {
733 next_bh = header_bh;
734 get_bh(next_bh);
735 }
736 if (prev != 0) {
737 ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
738 &prev_bh);
739 if (ret < 0)
740 goto out_next;
741 } else {
742 prev_bh = header_bh;
743 get_bh(prev_bh);
744 }
745
746 kaddr = kmap_atomic(next_bh->b_page, KM_USER0);
747 list = nilfs_cpfile_block_get_snapshot_list(
748 cpfile, next, next_bh, kaddr);
749 list->ssl_prev = cpu_to_le64(prev);
750 kunmap_atomic(kaddr, KM_USER0);
751
752 kaddr = kmap_atomic(prev_bh->b_page, KM_USER0);
753 list = nilfs_cpfile_block_get_snapshot_list(
754 cpfile, prev, prev_bh, kaddr);
755 list->ssl_next = cpu_to_le64(next);
756 kunmap_atomic(kaddr, KM_USER0);
757
758 kaddr = kmap_atomic(cp_bh->b_page, KM_USER0);
759 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
760 cp->cp_snapshot_list.ssl_next = cpu_to_le64(0);
761 cp->cp_snapshot_list.ssl_prev = cpu_to_le64(0);
762 nilfs_checkpoint_clear_snapshot(cp);
763 kunmap_atomic(kaddr, KM_USER0);
764
765 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
766 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
767 le64_add_cpu(&header->ch_nsnapshots, -1);
768 kunmap_atomic(kaddr, KM_USER0);
769
770 nilfs_mdt_mark_buffer_dirty(next_bh);
771 nilfs_mdt_mark_buffer_dirty(prev_bh);
772 nilfs_mdt_mark_buffer_dirty(cp_bh);
773 nilfs_mdt_mark_buffer_dirty(header_bh);
774 nilfs_mdt_mark_dirty(cpfile);
775
776 brelse(prev_bh);
777
778 out_next:
779 brelse(next_bh);
780
781 out_header:
782 brelse(header_bh);
783
784 out_cp:
785 brelse(cp_bh);
786
787 out_sem:
788 up_write(&NILFS_MDT(cpfile)->mi_sem);
789 return ret;
790}
791
792/**
793 * nilfs_cpfile_is_snapshot -
794 * @cpfile: inode of checkpoint file
795 * @cno: checkpoint number
796 *
797 * Description:
798 *
799 * Return Value: On success, 1 is returned if the checkpoint specified by
800 * @cno is a snapshot, or 0 if not. On error, one of the following negative
801 * error codes is returned.
802 *
803 * %-EIO - I/O error.
804 *
805 * %-ENOMEM - Insufficient amount of memory available.
806 *
807 * %-ENOENT - No such checkpoint.
808 */
809int nilfs_cpfile_is_snapshot(struct inode *cpfile, __u64 cno)
810{
811 struct buffer_head *bh;
812 struct nilfs_checkpoint *cp;
813 void *kaddr;
814 int ret;
815
816 down_read(&NILFS_MDT(cpfile)->mi_sem);
817
818 ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh);
819 if (ret < 0)
820 goto out;
821 kaddr = kmap_atomic(bh->b_page, KM_USER0);
822 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
823 ret = nilfs_checkpoint_snapshot(cp);
824 kunmap_atomic(kaddr, KM_USER0);
825 brelse(bh);
826
827 out:
828 up_read(&NILFS_MDT(cpfile)->mi_sem);
829 return ret;
830}
831
832/**
833 * nilfs_cpfile_change_cpmode - change checkpoint mode
834 * @cpfile: inode of checkpoint file
835 * @cno: checkpoint number
836 * @status: mode of checkpoint
837 *
838 * Description: nilfs_change_cpmode() changes the mode of the checkpoint
839 * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT.
840 *
841 * Return Value: On success, 0 is returned. On error, one of the following
842 * negative error codes is returned.
843 *
844 * %-EIO - I/O error.
845 *
846 * %-ENOMEM - Insufficient amount of memory available.
847 *
848 * %-ENOENT - No such checkpoint.
849 */
850int nilfs_cpfile_change_cpmode(struct inode *cpfile, __u64 cno, int mode)
851{
852 struct the_nilfs *nilfs;
853 int ret;
854
855 nilfs = NILFS_MDT(cpfile)->mi_nilfs;
856
857 switch (mode) {
858 case NILFS_CHECKPOINT:
859 /*
860 * Check for protecting existing snapshot mounts:
861 * bd_mount_sem is used to make this operation atomic and
862 * exclusive with a new mount job. Though it doesn't cover
863 * umount, it's enough for the purpose.
864 */
865 down(&nilfs->ns_bdev->bd_mount_sem);
866 if (nilfs_checkpoint_is_mounted(nilfs, cno, 1)) {
867 /* Current implementation does not have to protect
868 plain read-only mounts since they are exclusive
869 with a read/write mount and are protected from the
870 cleaner. */
871 ret = -EBUSY;
872 } else
873 ret = nilfs_cpfile_clear_snapshot(cpfile, cno);
874 up(&nilfs->ns_bdev->bd_mount_sem);
875 return ret;
876 case NILFS_SNAPSHOT:
877 return nilfs_cpfile_set_snapshot(cpfile, cno);
878 default:
879 return -EINVAL;
880 }
881}
882
883/**
884 * nilfs_cpfile_get_stat - get checkpoint statistics
885 * @cpfile: inode of checkpoint file
886 * @stat: pointer to a structure of checkpoint statistics
887 *
888 * Description: nilfs_cpfile_get_stat() returns information about checkpoints.
889 *
890 * Return Value: On success, 0 is returned, and checkpoints information is
891 * stored in the place pointed by @stat. On error, one of the following
892 * negative error codes is returned.
893 *
894 * %-EIO - I/O error.
895 *
896 * %-ENOMEM - Insufficient amount of memory available.
897 */
898int nilfs_cpfile_get_stat(struct inode *cpfile, struct nilfs_cpstat *cpstat)
899{
900 struct buffer_head *bh;
901 struct nilfs_cpfile_header *header;
902 void *kaddr;
903 int ret;
904
905 down_read(&NILFS_MDT(cpfile)->mi_sem);
906
907 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
908 if (ret < 0)
909 goto out_sem;
910 kaddr = kmap_atomic(bh->b_page, KM_USER0);
911 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
912 cpstat->cs_cno = nilfs_mdt_cno(cpfile);
913 cpstat->cs_ncps = le64_to_cpu(header->ch_ncheckpoints);
914 cpstat->cs_nsss = le64_to_cpu(header->ch_nsnapshots);
915 kunmap_atomic(kaddr, KM_USER0);
916 brelse(bh);
917
918 out_sem:
919 up_read(&NILFS_MDT(cpfile)->mi_sem);
920 return ret;
921}