blob: d192b48df9fb9a80d7e67bc971bd0822a27c8fc6 [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 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -070016 * Written by Koji Sato.
Koji Sato29619802009-04-06 19:01:31 -070017 */
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
29static inline unsigned long
30nilfs_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 */
36static unsigned long
37nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
38{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -070039 __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
Koji Sato29619802009-04-06 19:01:31 -070040 do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
41 return (unsigned long)tcno;
42}
43
44/* offset in block */
45static unsigned long
46nilfs_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
Ryusuke Konishi53a2c3b2015-04-16 12:46:42 -070052static __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
Koji Sato29619802009-04-06 19:01:31 -070059static 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
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -070099 WARN_ON(le32_to_cpu(cp->cp_checkpoints_count) < n);
Koji Sato29619802009-04-06 19:01:31 -0700100 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
Ryusuke Konishi53a2c3b2015-04-16 12:46:42 -0700152/**
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 */
169static 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
Koji Sato29619802009-04-06 19:01:31 -0700190static 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.
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700219 *
220 * %-EINVAL - invalid checkpoint.
Koji Sato29619802009-04-06 19:01:31 -0700221 */
222int 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
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700234 if (unlikely(cno < 1 || cno > nilfs_mdt_cno(cpfile) ||
235 (cno < nilfs_mdt_cno(cpfile) && create)))
236 return -EINVAL;
Koji Sato29619802009-04-06 19:01:31 -0700237
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);
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900260 mark_buffer_dirty(cp_bh);
Koji Sato29619802009-04-06 19:01:31 -0700261
Cong Wang7b9c0972011-11-25 23:14:33 +0800262 kaddr = kmap_atomic(header_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700263 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
264 kaddr);
265 le64_add_cpu(&header->ch_ncheckpoints, 1);
Cong Wang7b9c0972011-11-25 23:14:33 +0800266 kunmap_atomic(kaddr);
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900267 mark_buffer_dirty(header_bh);
Koji Sato29619802009-04-06 19:01:31 -0700268 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 */
293void 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 */
319int 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;
Ryusuke Konishife0627e2012-07-30 14:42:05 -0700330 int ret, ncps, nicps, nss, count, i;
Koji Sato29619802009-04-06 19:01:31 -0700331
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700332 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;
Koji Sato29619802009-04-06 19:01:31 -0700337 }
338
Koji Sato29619802009-04-06 19:01:31 -0700339 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;
Ryusuke Konishife0627e2012-07-30 14:42:05 -0700345 nss = 0;
Koji Sato29619802009-04-06 19:01:31 -0700346
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)
Jiro SEKIBAd9a0a342009-07-04 23:00:53 +0900352 break;
Koji Sato29619802009-04-06 19:01:31 -0700353 /* skip hole */
354 ret = 0;
355 continue;
356 }
357
Cong Wang7b9c0972011-11-25 23:14:33 +0800358 kaddr = kmap_atomic(cp_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700359 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) {
Ryusuke Konishife0627e2012-07-30 14:42:05 -0700363 if (nilfs_checkpoint_snapshot(cp)) {
364 nss++;
365 } else if (!nilfs_checkpoint_invalid(cp)) {
Koji Sato29619802009-04-06 19:01:31 -0700366 nilfs_checkpoint_set_invalid(cp);
367 nicps++;
368 }
369 }
370 if (nicps > 0) {
371 tnicps += nicps;
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900372 mark_buffer_dirty(cp_bh);
Koji Sato29619802009-04-06 19:01:31 -0700373 nilfs_mdt_mark_dirty(cpfile);
Jiro SEKIBA5ee58142009-12-06 15:43:56 +0900374 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 */
Cong Wang7b9c0972011-11-25 23:14:33 +0800380 kunmap_atomic(kaddr);
Jiro SEKIBA5ee58142009-12-06 15:43:56 +0900381 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 }
Koji Sato29619802009-04-06 19:01:31 -0700392 }
393 }
394
Cong Wang7b9c0972011-11-25 23:14:33 +0800395 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700396 brelse(cp_bh);
397 }
398
399 if (tnicps > 0) {
Cong Wang7b9c0972011-11-25 23:14:33 +0800400 kaddr = kmap_atomic(header_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700401 header = nilfs_cpfile_block_get_header(cpfile, header_bh,
402 kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700403 le64_add_cpu(&header->ch_ncheckpoints, -(u64)tnicps);
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900404 mark_buffer_dirty(header_bh);
Koji Sato29619802009-04-06 19:01:31 -0700405 nilfs_mdt_mark_dirty(cpfile);
Cong Wang7b9c0972011-11-25 23:14:33 +0800406 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700407 }
Ryusuke Konishi62013ab2009-05-30 21:50:58 +0900408
Koji Sato29619802009-04-06 19:01:31 -0700409 brelse(header_bh);
Ryusuke Konishife0627e2012-07-30 14:42:05 -0700410 if (nss > 0)
411 ret = -EBUSY;
Koji Sato29619802009-04-06 19:01:31 -0700412
413 out_sem:
414 up_write(&NILFS_MDT(cpfile)->mi_sem);
415 return ret;
416}
417
418static 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
Ryusuke Konishi76068c42009-04-06 19:01:50 -0700431static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop,
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900432 void *buf, unsigned cisz, size_t nci)
Koji Sato29619802009-04-06 19:01:31 -0700433{
434 struct nilfs_checkpoint *cp;
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900435 struct nilfs_cpinfo *ci = buf;
Koji Sato29619802009-04-06 19:01:31 -0700436 struct buffer_head *bh;
437 size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
Ryusuke Konishi76068c42009-04-06 19:01:50 -0700438 __u64 cur_cno = nilfs_mdt_cno(cpfile), cno = *cnop;
Koji Sato29619802009-04-06 19:01:31 -0700439 void *kaddr;
440 int n, ret;
441 int ncps, i;
442
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700443 if (cno == 0)
444 return -ENOENT; /* checkpoint number 0 is invalid */
Koji Sato29619802009-04-06 19:01:31 -0700445 down_read(&NILFS_MDT(cpfile)->mi_sem);
446
Ryusuke Konishi53a2c3b2015-04-16 12:46:42 -0700447 for (n = 0; n < nci; cno += ncps) {
448 ret = nilfs_cpfile_find_checkpoint_block(
449 cpfile, cno, cur_cno - 1, &cno, &bh);
Koji Sato29619802009-04-06 19:01:31 -0700450 if (ret < 0) {
Ryusuke Konishi53a2c3b2015-04-16 12:46:42 -0700451 if (likely(ret == -ENOENT))
452 break;
453 goto out;
Koji Sato29619802009-04-06 19:01:31 -0700454 }
Ryusuke Konishi53a2c3b2015-04-16 12:46:42 -0700455 ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, cur_cno);
Koji Sato29619802009-04-06 19:01:31 -0700456
Cong Wang7b9c0972011-11-25 23:14:33 +0800457 kaddr = kmap_atomic(bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700458 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
459 for (i = 0; i < ncps && n < nci; i++, cp = (void *)cp + cpsz) {
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900460 if (!nilfs_checkpoint_invalid(cp)) {
461 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp,
462 ci);
463 ci = (void *)ci + cisz;
464 n++;
465 }
Koji Sato29619802009-04-06 19:01:31 -0700466 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800467 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700468 brelse(bh);
469 }
470
471 ret = n;
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900472 if (n > 0) {
473 ci = (void *)ci - cisz;
474 *cnop = ci->ci_cno + 1;
475 }
Koji Sato29619802009-04-06 19:01:31 -0700476
477 out:
478 up_read(&NILFS_MDT(cpfile)->mi_sem);
479 return ret;
480}
481
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700482static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900483 void *buf, unsigned cisz, size_t nci)
Koji Sato29619802009-04-06 19:01:31 -0700484{
485 struct buffer_head *bh;
486 struct nilfs_cpfile_header *header;
487 struct nilfs_checkpoint *cp;
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900488 struct nilfs_cpinfo *ci = buf;
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700489 __u64 curr = *cnop, next;
Koji Sato29619802009-04-06 19:01:31 -0700490 unsigned long curr_blkoff, next_blkoff;
491 void *kaddr;
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700492 int n = 0, ret;
Koji Sato29619802009-04-06 19:01:31 -0700493
494 down_read(&NILFS_MDT(cpfile)->mi_sem);
495
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700496 if (curr == 0) {
Koji Sato29619802009-04-06 19:01:31 -0700497 ret = nilfs_cpfile_get_header_block(cpfile, &bh);
498 if (ret < 0)
499 goto out;
Cong Wang7b9c0972011-11-25 23:14:33 +0800500 kaddr = kmap_atomic(bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700501 header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
502 curr = le64_to_cpu(header->ch_snapshot_list.ssl_next);
Cong Wang7b9c0972011-11-25 23:14:33 +0800503 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700504 brelse(bh);
505 if (curr == 0) {
506 ret = 0;
507 goto out;
508 }
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700509 } else if (unlikely(curr == ~(__u64)0)) {
510 ret = 0;
511 goto out;
512 }
513
Koji Sato29619802009-04-06 19:01:31 -0700514 curr_blkoff = nilfs_cpfile_get_blkoff(cpfile, curr);
515 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr, 0, &bh);
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700516 if (unlikely(ret < 0)) {
517 if (ret == -ENOENT)
518 ret = 0; /* No snapshots (started from a hole block) */
Koji Sato29619802009-04-06 19:01:31 -0700519 goto out;
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700520 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800521 kaddr = kmap_atomic(bh->b_page);
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700522 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)))
Koji Sato29619802009-04-06 19:01:31 -0700527 break;
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900528 nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, ci);
529 ci = (void *)ci + cisz;
530 n++;
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700531 next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
532 if (next == 0)
533 break; /* reach end of the snapshot list */
534
Koji Sato29619802009-04-06 19:01:31 -0700535 next_blkoff = nilfs_cpfile_get_blkoff(cpfile, next);
536 if (curr_blkoff != next_blkoff) {
Cong Wang7b9c0972011-11-25 23:14:33 +0800537 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700538 brelse(bh);
539 ret = nilfs_cpfile_get_checkpoint_block(cpfile, next,
540 0, &bh);
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700541 if (unlikely(ret < 0)) {
542 WARN_ON(ret == -ENOENT);
Koji Sato29619802009-04-06 19:01:31 -0700543 goto out;
Ryusuke Konishi7fa10d22009-04-06 19:01:48 -0700544 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800545 kaddr = kmap_atomic(bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700546 }
547 curr = next;
548 curr_blkoff = next_blkoff;
549 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800550 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700551 brelse(bh);
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700552 *cnop = curr;
Koji Sato29619802009-04-06 19:01:31 -0700553 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 */
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700567
568ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode,
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900569 void *buf, unsigned cisz, size_t nci)
Koji Sato29619802009-04-06 19:01:31 -0700570{
571 switch (mode) {
572 case NILFS_CHECKPOINT:
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900573 return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, buf, cisz, nci);
Koji Sato29619802009-04-06 19:01:31 -0700574 case NILFS_SNAPSHOT:
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900575 return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, buf, cisz, nci);
Koji Sato29619802009-04-06 19:01:31 -0700576 default:
577 return -EINVAL;
578 }
579}
580
581/**
582 * nilfs_cpfile_delete_checkpoint -
583 * @cpfile:
584 * @cno:
585 */
586int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
587{
588 struct nilfs_cpinfo ci;
Ryusuke Konishi76068c42009-04-06 19:01:50 -0700589 __u64 tcno = cno;
Koji Sato29619802009-04-06 19:01:31 -0700590 ssize_t nci;
Koji Sato29619802009-04-06 19:01:31 -0700591
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900592 nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, sizeof(ci), 1);
Koji Sato29619802009-04-06 19:01:31 -0700593 if (nci < 0)
594 return nci;
595 else if (nci == 0 || ci.ci_cno != cno)
596 return -ENOENT;
Ryusuke Konishi30c25be2009-05-30 19:08:09 +0900597 else if (nilfs_cpinfo_snapshot(&ci))
598 return -EBUSY;
Koji Sato29619802009-04-06 19:01:31 -0700599
600 return nilfs_cpfile_delete_checkpoints(cpfile, cno, cno + 1);
601}
602
603static struct nilfs_snapshot_list *
604nilfs_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
623static 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
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700634 if (cno == 0)
635 return -ENOENT; /* checkpoint number 0 is invalid */
Koji Sato29619802009-04-06 19:01:31 -0700636 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;
Cong Wang7b9c0972011-11-25 23:14:33 +0800641 kaddr = kmap_atomic(cp_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700642 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
643 if (nilfs_checkpoint_invalid(cp)) {
644 ret = -ENOENT;
Cong Wang7b9c0972011-11-25 23:14:33 +0800645 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700646 goto out_cp;
647 }
648 if (nilfs_checkpoint_snapshot(cp)) {
649 ret = 0;
Cong Wang7b9c0972011-11-25 23:14:33 +0800650 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700651 goto out_cp;
652 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800653 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700654
655 ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
656 if (ret < 0)
657 goto out_cp;
Cong Wang7b9c0972011-11-25 23:14:33 +0800658 kaddr = kmap_atomic(header_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700659 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) {
Cong Wang7b9c0972011-11-25 23:14:33 +0800670 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700671 brelse(curr_bh);
672 ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr,
673 0, &curr_bh);
674 if (ret < 0)
675 goto out_header;
Cong Wang7b9c0972011-11-25 23:14:33 +0800676 kaddr = kmap_atomic(curr_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700677 }
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 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800684 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700685
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
Cong Wang7b9c0972011-11-25 23:14:33 +0800696 kaddr = kmap_atomic(curr_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700697 list = nilfs_cpfile_block_get_snapshot_list(
698 cpfile, curr, curr_bh, kaddr);
699 list->ssl_prev = cpu_to_le64(cno);
Cong Wang7b9c0972011-11-25 23:14:33 +0800700 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700701
Cong Wang7b9c0972011-11-25 23:14:33 +0800702 kaddr = kmap_atomic(cp_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700703 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);
Cong Wang7b9c0972011-11-25 23:14:33 +0800707 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700708
Cong Wang7b9c0972011-11-25 23:14:33 +0800709 kaddr = kmap_atomic(prev_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700710 list = nilfs_cpfile_block_get_snapshot_list(
711 cpfile, prev, prev_bh, kaddr);
712 list->ssl_next = cpu_to_le64(cno);
Cong Wang7b9c0972011-11-25 23:14:33 +0800713 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700714
Cong Wang7b9c0972011-11-25 23:14:33 +0800715 kaddr = kmap_atomic(header_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700716 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
717 le64_add_cpu(&header->ch_nsnapshots, 1);
Cong Wang7b9c0972011-11-25 23:14:33 +0800718 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700719
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900720 mark_buffer_dirty(prev_bh);
721 mark_buffer_dirty(curr_bh);
722 mark_buffer_dirty(cp_bh);
723 mark_buffer_dirty(header_bh);
Koji Sato29619802009-04-06 19:01:31 -0700724 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
742static 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
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700752 if (cno == 0)
753 return -ENOENT; /* checkpoint number 0 is invalid */
Koji Sato29619802009-04-06 19:01:31 -0700754 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;
Cong Wang7b9c0972011-11-25 23:14:33 +0800759 kaddr = kmap_atomic(cp_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700760 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
761 if (nilfs_checkpoint_invalid(cp)) {
762 ret = -ENOENT;
Cong Wang7b9c0972011-11-25 23:14:33 +0800763 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700764 goto out_cp;
765 }
766 if (!nilfs_checkpoint_snapshot(cp)) {
767 ret = 0;
Cong Wang7b9c0972011-11-25 23:14:33 +0800768 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700769 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);
Cong Wang7b9c0972011-11-25 23:14:33 +0800775 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700776
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
Cong Wang7b9c0972011-11-25 23:14:33 +0800799 kaddr = kmap_atomic(next_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700800 list = nilfs_cpfile_block_get_snapshot_list(
801 cpfile, next, next_bh, kaddr);
802 list->ssl_prev = cpu_to_le64(prev);
Cong Wang7b9c0972011-11-25 23:14:33 +0800803 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700804
Cong Wang7b9c0972011-11-25 23:14:33 +0800805 kaddr = kmap_atomic(prev_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700806 list = nilfs_cpfile_block_get_snapshot_list(
807 cpfile, prev, prev_bh, kaddr);
808 list->ssl_next = cpu_to_le64(next);
Cong Wang7b9c0972011-11-25 23:14:33 +0800809 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700810
Cong Wang7b9c0972011-11-25 23:14:33 +0800811 kaddr = kmap_atomic(cp_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700812 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);
Cong Wang7b9c0972011-11-25 23:14:33 +0800816 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700817
Cong Wang7b9c0972011-11-25 23:14:33 +0800818 kaddr = kmap_atomic(header_bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700819 header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
820 le64_add_cpu(&header->ch_nsnapshots, -1);
Cong Wang7b9c0972011-11-25 23:14:33 +0800821 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700822
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900823 mark_buffer_dirty(next_bh);
824 mark_buffer_dirty(prev_bh);
825 mark_buffer_dirty(cp_bh);
826 mark_buffer_dirty(header_bh);
Koji Sato29619802009-04-06 19:01:31 -0700827 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 */
862int 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
Zhu Yanhai43be0ec2009-08-12 14:17:59 +0800869 /* 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;
Koji Sato29619802009-04-06 19:01:31 -0700873 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;
Cong Wang7b9c0972011-11-25 23:14:33 +0800878 kaddr = kmap_atomic(bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700879 cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
Zhu Yanhai43be0ec2009-08-12 14:17:59 +0800880 if (nilfs_checkpoint_invalid(cp))
881 ret = -ENOENT;
882 else
883 ret = nilfs_checkpoint_snapshot(cp);
Cong Wang7b9c0972011-11-25 23:14:33 +0800884 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700885 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 */
910int nilfs_cpfile_change_cpmode(struct inode *cpfile, __u64 cno, int mode)
911{
Koji Sato29619802009-04-06 19:01:31 -0700912 int ret;
913
Koji Sato29619802009-04-06 19:01:31 -0700914 switch (mode) {
915 case NILFS_CHECKPOINT:
Ryusuke Konishi032dbb32010-09-13 11:16:34 +0900916 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 */
Koji Sato29619802009-04-06 19:01:31 -0700923 ret = -EBUSY;
Ryusuke Konishi032dbb32010-09-13 11:16:34 +0900924 else
Koji Sato29619802009-04-06 19:01:31 -0700925 ret = nilfs_cpfile_clear_snapshot(cpfile, cno);
Koji Sato29619802009-04-06 19:01:31 -0700926 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 */
949int 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;
Cong Wang7b9c0972011-11-25 23:14:33 +0800961 kaddr = kmap_atomic(bh->b_page);
Koji Sato29619802009-04-06 19:01:31 -0700962 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);
Cong Wang7b9c0972011-11-25 23:14:33 +0800966 kunmap_atomic(kaddr);
Koji Sato29619802009-04-06 19:01:31 -0700967 brelse(bh);
968
969 out_sem:
970 up_read(&NILFS_MDT(cpfile)->mi_sem);
971 return ret;
972}
Ryusuke Konishi79739562009-11-12 23:56:43 +0900973
974/**
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900975 * nilfs_cpfile_read - read or get cpfile inode
976 * @sb: super block instance
Ryusuke Konishi79739562009-11-12 23:56:43 +0900977 * @cpsize: size of a checkpoint entry
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900978 * @raw_inode: on-disk cpfile inode
979 * @inodep: buffer to store the inode
Ryusuke Konishi79739562009-11-12 23:56:43 +0900980 */
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900981int nilfs_cpfile_read(struct super_block *sb, size_t cpsize,
982 struct nilfs_inode *raw_inode, struct inode **inodep)
Ryusuke Konishi79739562009-11-12 23:56:43 +0900983{
984 struct inode *cpfile;
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900985 int err;
Ryusuke Konishi79739562009-11-12 23:56:43 +0900986
Ryusuke Konishi0ec060d2014-04-03 14:50:31 -0700987 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
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900999 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;
Ryusuke Konishi79739562009-11-12 23:56:43 +09001023}