summaryrefslogtreecommitdiffstats
path: root/sys/fs/nandfs/nandfs_cpfile.c
blob: 8814fc053ce099922c9d5e2a6499621de33a03d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/*-
 * Copyright (c) 2010-2012 Semihalf.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/mutex.h>
#include <sys/namei.h>
#include <sys/sysctl.h>
#include <sys/vnode.h>
#include <sys/buf.h>
#include <sys/bio.h>

#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/vm_kern.h>
#include <vm/vm_page.h>

#include "nandfs_mount.h"
#include "nandfs.h"
#include "nandfs_subr.h"


static int
nandfs_checkpoint_size(struct nandfs_device *fsdev)
{

	return (fsdev->nd_fsdata.f_checkpoint_size);
}

static int
nandfs_checkpoint_blk_offset(struct nandfs_device *fsdev, uint64_t cn,
    uint64_t *blk, uint64_t *offset)
{
	uint64_t off;
	uint16_t cp_size, cp_per_blk;

	KASSERT((cn), ("checkpoing cannot be zero"));

	cp_size = fsdev->nd_fsdata.f_checkpoint_size;
	cp_per_blk = fsdev->nd_blocksize / cp_size;
	off = roundup(sizeof(struct nandfs_cpfile_header), cp_size) / cp_size;
	off += (cn - 1);

	*blk = off / cp_per_blk;
	*offset = (off % cp_per_blk) * cp_size;

	return (0);
}

static int
nandfs_checkpoint_blk_remaining(struct nandfs_device *fsdev, uint64_t cn,
    uint64_t blk, uint64_t offset)
{
	uint16_t cp_size, cp_remaining;

	cp_size = fsdev->nd_fsdata.f_checkpoint_size;
	cp_remaining = (fsdev->nd_blocksize - offset) / cp_size;

	return (cp_remaining);
}

int
nandfs_get_checkpoint(struct nandfs_device *fsdev, struct nandfs_node *cp_node,
    uint64_t cn)
{
	struct buf *bp;
	uint64_t blk, offset;
	int error;

	if (cn != fsdev->nd_last_cno && cn != (fsdev->nd_last_cno + 1)) {
		return (-1);
	}

	error = nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		return (-1);
	}

	error = nandfs_dirty_buf(bp, 0);
	if (error)
		return (-1);


	nandfs_checkpoint_blk_offset(fsdev, cn, &blk, &offset);

	if (blk != 0) {
		if (blk < cp_node->nn_inode.i_blocks)
			error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
		else
			error = nandfs_bcreate(cp_node, blk, NOCRED, 0, &bp);
		if (error) {
			if (bp)
				brelse(bp);
			return (-1);
		}

		nandfs_dirty_buf(bp, 1);
	}

	DPRINTF(CPFILE, ("%s: cn:%#jx entry block:%#jx offset:%#jx\n",
	    __func__, (uintmax_t)cn, (uintmax_t)blk, (uintmax_t)offset));

	return (0);
}

int
nandfs_set_checkpoint(struct nandfs_device *fsdev, struct nandfs_node *cp_node,
    uint64_t cn, struct nandfs_inode *ifile_inode, uint64_t nblocks)
{
	struct nandfs_cpfile_header *cnh;
	struct nandfs_checkpoint *cnp;
	struct buf *bp;
	uint64_t blk, offset;
	int error;

	if (cn != fsdev->nd_last_cno && cn != (fsdev->nd_last_cno + 1)) {
		nandfs_error("%s: trying to set invalid chekpoint %jx - %jx\n",
		    __func__, cn, fsdev->nd_last_cno);
		return (-1);
	}

	error = nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		return error;
	}

	cnh = (struct nandfs_cpfile_header *) bp->b_data;
	cnh->ch_ncheckpoints++;

	nandfs_checkpoint_blk_offset(fsdev, cn, &blk, &offset);

	if(blk != 0) {
		brelse(bp);
		error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
		if (error) {
			brelse(bp);
			return error;
		}
	}

	cnp = (struct nandfs_checkpoint *)((uint8_t *)bp->b_data + offset);
	cnp->cp_flags = 0;
	cnp->cp_checkpoints_count = 1;
	memset(&cnp->cp_snapshot_list, 0, sizeof(struct nandfs_snapshot_list));
	cnp->cp_cno = cn;
	cnp->cp_create = fsdev->nd_ts.tv_sec;
	cnp->cp_nblk_inc = nblocks;
	cnp->cp_blocks_count = 0;
	memcpy (&cnp->cp_ifile_inode, ifile_inode, sizeof(cnp->cp_ifile_inode));

	DPRINTF(CPFILE, ("%s: cn:%#jx ctime:%#jx nblk:%#jx\n",
	    __func__, (uintmax_t)cn, (uintmax_t)cnp->cp_create,
	    (uintmax_t)nblocks));

	brelse(bp);
	return (0);
}

static int
nandfs_cp_mounted(struct nandfs_device *nandfsdev, uint64_t cno)
{
	struct nandfsmount *nmp;
	int mounted = 0;

	mtx_lock(&nandfsdev->nd_mutex);
	/* No double-mounting of the same checkpoint */
	STAILQ_FOREACH(nmp, &nandfsdev->nd_mounts, nm_next_mount) {
		if (nmp->nm_mount_args.cpno == cno) {
			mounted = 1;
			break;
		}
	}
	mtx_unlock(&nandfsdev->nd_mutex);

	return (mounted);
}

static int
nandfs_cp_set_snapshot(struct nandfs_node *cp_node, uint64_t cno)
{
	struct nandfs_device *fsdev;
	struct nandfs_cpfile_header *cnh;
	struct nandfs_checkpoint *cnp;
	struct nandfs_snapshot_list *list;
	struct buf *bp;
	uint64_t blk, prev_blk, offset;
	uint64_t curr, prev;
	int error;

	fsdev = cp_node->nn_nandfsdev;

	/* Get snapshot data */
	nandfs_checkpoint_blk_offset(fsdev, cno, &blk, &offset);
	error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		return (error);
	}
	cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
	if (cnp->cp_flags & NANDFS_CHECKPOINT_INVALID) {
		brelse(bp);
		return (ENOENT);
	}
	if ((cnp->cp_flags & NANDFS_CHECKPOINT_SNAPSHOT)) {
		brelse(bp);
		return (EINVAL);
	}

	brelse(bp);
	/* Get list from header */
	error = nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		return (error);
	}

	cnh = (struct nandfs_cpfile_header *) bp->b_data;
	list = &cnh->ch_snapshot_list;
	prev = list->ssl_prev;
	brelse(bp);
	prev_blk = ~(0);
	curr = 0;
	while (prev > cno) {
		curr = prev;
		nandfs_checkpoint_blk_offset(fsdev, prev, &prev_blk, &offset);
		error = nandfs_bread(cp_node, prev_blk, NOCRED, 0, &bp);
		if (error) {
			brelse(bp);
			return (error);
		}
		cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
		list = &cnp->cp_snapshot_list;
		prev = list->ssl_prev;
		brelse(bp);
	}

	if (curr == 0) {
		nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
		cnh = (struct nandfs_cpfile_header *) bp->b_data;
		list = &cnh->ch_snapshot_list;
	} else {
		nandfs_checkpoint_blk_offset(fsdev, curr, &blk, &offset);
		error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
		if (error) {
			brelse(bp);
			return (error);
		}
		cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
		list = &cnp->cp_snapshot_list;
	}

	list->ssl_prev = cno;
	error = nandfs_dirty_buf(bp, 0);
	if (error)
		return (error);


	/* Update snapshot for cno */
	nandfs_checkpoint_blk_offset(fsdev, cno, &blk, &offset);
	error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		return (error);
	}
	cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
	list = &cnp->cp_snapshot_list;
	list->ssl_prev = prev;
	list->ssl_next = curr;
	cnp->cp_flags |= NANDFS_CHECKPOINT_SNAPSHOT;
	nandfs_dirty_buf(bp, 1);

	if (prev == 0) {
		nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
		cnh = (struct nandfs_cpfile_header *) bp->b_data;
		list = &cnh->ch_snapshot_list;
	} else {
		/* Update snapshot list for prev */
		nandfs_checkpoint_blk_offset(fsdev, prev, &blk, &offset);
		error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
		if (error) {
			brelse(bp);
			return (error);
		}
		cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
		list = &cnp->cp_snapshot_list;
	}
	list->ssl_next = cno;
	nandfs_dirty_buf(bp, 1);

	/* Update header */
	error = nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		return (error);
	}
	cnh = (struct nandfs_cpfile_header *) bp->b_data;
	cnh->ch_nsnapshots++;
	nandfs_dirty_buf(bp, 1);

	return (0);
}

static int
nandfs_cp_clr_snapshot(struct nandfs_node *cp_node, uint64_t cno)
{
	struct nandfs_device *fsdev;
	struct nandfs_cpfile_header *cnh;
	struct nandfs_checkpoint *cnp;
	struct nandfs_snapshot_list *list;
	struct buf *bp;
	uint64_t blk, offset, snapshot_cnt;
	uint64_t next, prev;
	int error;

	fsdev = cp_node->nn_nandfsdev;

	/* Get snapshot data */
	nandfs_checkpoint_blk_offset(fsdev, cno, &blk, &offset);
	error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		return (error);
	}
	cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
	if (cnp->cp_flags & NANDFS_CHECKPOINT_INVALID) {
		brelse(bp);
		return (ENOENT);
	}
	if (!(cnp->cp_flags & NANDFS_CHECKPOINT_SNAPSHOT)) {
		brelse(bp);
		return (EINVAL);
	}

	list = &cnp->cp_snapshot_list;
	next = list->ssl_next;
	prev = list->ssl_prev;
	brelse(bp);

	/* Get previous snapshot */
	if (prev != 0) {
		nandfs_checkpoint_blk_offset(fsdev, prev, &blk, &offset);
		error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
		if (error) {
			brelse(bp);
			return (error);
		}
		cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
		list = &cnp->cp_snapshot_list;
	} else {
		nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
		cnh = (struct nandfs_cpfile_header *) bp->b_data;
		list = &cnh->ch_snapshot_list;
	}

	list->ssl_next = next;
	error = nandfs_dirty_buf(bp, 0);
	if (error)
		return (error);

	/* Get next snapshot */
	if (next != 0) {
		nandfs_checkpoint_blk_offset(fsdev, next, &blk, &offset);
		error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
		if (error) {
			brelse(bp);
			return (error);
		}
		cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
		list = &cnp->cp_snapshot_list;
	} else {
		nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
		cnh = (struct nandfs_cpfile_header *) bp->b_data;
		list = &cnh->ch_snapshot_list;
	}
	list->ssl_prev = prev;
	nandfs_dirty_buf(bp, 1);

	/* Update snapshot list for cno */
	nandfs_checkpoint_blk_offset(fsdev, cno, &blk, &offset);
	error = nandfs_bread(cp_node, blk, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		return (error);
	}
	cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
	list = &cnp->cp_snapshot_list;
	list->ssl_prev = 0;
	list->ssl_next = 0;
	cnp->cp_flags &= !NANDFS_CHECKPOINT_SNAPSHOT;
	nandfs_dirty_buf(bp, 1);

	/* Update header */
	error = nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		return (error);
	}
	cnh = (struct nandfs_cpfile_header *) bp->b_data;
	snapshot_cnt = cnh->ch_nsnapshots;
	snapshot_cnt--;
	cnh->ch_nsnapshots = snapshot_cnt;
	nandfs_dirty_buf(bp, 1);

	return (0);
}

int
nandfs_chng_cpmode(struct nandfs_node *node, struct nandfs_cpmode *ncpm)
{
	struct nandfs_device *fsdev;
	uint64_t cno = ncpm->ncpm_cno;
	int mode = ncpm->ncpm_mode;
	int ret;

	fsdev = node->nn_nandfsdev;
	VOP_LOCK(NTOV(node), LK_EXCLUSIVE);
	switch (mode) {
	case NANDFS_CHECKPOINT:
		if (nandfs_cp_mounted(fsdev, cno)) {
			ret = EBUSY;
		} else
			ret = nandfs_cp_clr_snapshot(node, cno);
		break;
	case NANDFS_SNAPSHOT:
		ret = nandfs_cp_set_snapshot(node, cno);
		break;
	default:
		ret = EINVAL;
		break;
	}
	VOP_UNLOCK(NTOV(node), 0);

	return (ret);
}

static void
nandfs_cpinfo_fill(struct nandfs_checkpoint *cnp, struct nandfs_cpinfo *nci)
{

	nci->nci_flags = cnp->cp_flags;
	nci->nci_pad = 0;
	nci->nci_cno = cnp->cp_cno;
	nci->nci_create = cnp->cp_create;
	nci->nci_nblk_inc = cnp->cp_nblk_inc;
	nci->nci_blocks_count = cnp->cp_blocks_count;
	nci->nci_next = cnp->cp_snapshot_list.ssl_next;
	DPRINTF(CPFILE, ("%s: cn:%#jx ctime:%#jx\n",
	    __func__, (uintmax_t)cnp->cp_cno,
	    (uintmax_t)cnp->cp_create));
}

static int
nandfs_get_cpinfo_cp(struct nandfs_node *node, uint64_t cno,
    struct nandfs_cpinfo *nci, uint32_t mnmembs, uint32_t *nmembs)
{
	struct nandfs_device *fsdev;
	struct buf *bp;
	uint64_t blk, offset, last_cno, i;
	uint16_t remaining;
	int error;
#ifdef INVARIANTS
	uint64_t testblk, testoffset;
#endif

	if (cno == 0) {
		return (ENOENT);
	}

	if (mnmembs < 1) {
		return (EINVAL);
	}

	fsdev = node->nn_nandfsdev;
	last_cno = fsdev->nd_last_cno;
	DPRINTF(CPFILE, ("%s: cno:%#jx mnmembs: %#jx last:%#jx\n", __func__,
	    (uintmax_t)cno, (uintmax_t)mnmembs,
	    (uintmax_t)fsdev->nd_last_cno));

	/*
	 * do {
	 * 	get block
	 * 	read checkpoints until we hit last checkpoint, end of block or
	 * 	requested number
	 * } while (last read checkpoint <= last checkpoint on fs &&
	 * 		read checkpoints < request number);
	 */
	*nmembs = i = 0;
	do {
		nandfs_checkpoint_blk_offset(fsdev, cno, &blk, &offset);
		remaining = nandfs_checkpoint_blk_remaining(fsdev, cno,
		    blk, offset);
		error = nandfs_bread(node, blk, NOCRED, 0, &bp);
		if (error) {
			brelse(bp);
			return (error);
		}

		while (cno <= last_cno && i < mnmembs && remaining) {
#ifdef INVARIANTS
			nandfs_checkpoint_blk_offset(fsdev, cno, &testblk,
			    &testoffset);
			KASSERT(testblk == blk, ("testblk != blk"));
			KASSERT(testoffset == offset, ("testoffset != offset"));
#endif
			DPRINTF(CPFILE, ("%s: cno %#jx\n", __func__,
			    (uintmax_t)cno));

			nandfs_cpinfo_fill((struct nandfs_checkpoint *)
			    (bp->b_data + offset), nci);
			offset += nandfs_checkpoint_size(fsdev);
			i++;
			nci++;
			cno++;
			(*nmembs)++;
			remaining--;
		}
		brelse(bp);
	} while (cno <= last_cno && i < mnmembs);

	return (0);
}

static int
nandfs_get_cpinfo_sp(struct nandfs_node *node, uint64_t cno,
    struct nandfs_cpinfo *nci, uint32_t mnmembs, uint32_t *nmembs)
{
	struct nandfs_checkpoint *cnp;
	struct nandfs_cpfile_header *cnh;
	struct nandfs_device *fsdev;
	struct buf *bp = NULL;
	uint64_t curr = 0;
	uint64_t blk, offset, curr_cno;
	uint32_t flag;
	int i, error;

	if (cno == 0 || cno == ~(0))
		return (ENOENT);

	fsdev = node->nn_nandfsdev;
	curr_cno = cno;

	if (nmembs)
		*nmembs = 0;
	if (curr_cno == 1) {
		/* Get list from header */
		error = nandfs_bread(node, 0, NOCRED, 0, &bp);
		if (error) {
			brelse(bp);
			return (error);
		}
		cnh = (struct nandfs_cpfile_header *) bp->b_data;
		curr_cno = cnh->ch_snapshot_list.ssl_next;
		brelse(bp);
		bp = NULL;

		/* No snapshots */
		if (curr_cno == 0)
			return (0);
	}

	for (i = 0; i < mnmembs; i++, nci++) {
		nandfs_checkpoint_blk_offset(fsdev, curr_cno, &blk, &offset);
		if (i == 0 || curr != blk) {
			if (bp)
				brelse(bp);
			error = nandfs_bread(node, blk, NOCRED, 0, &bp);
			if (error) {
				brelse(bp);
				return (ENOENT);
			}
			curr = blk;
		}
		cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
		flag = cnp->cp_flags;
		if (!(flag & NANDFS_CHECKPOINT_SNAPSHOT) ||
		    (flag & NANDFS_CHECKPOINT_INVALID))
			break;

		nci->nci_flags = flag;
		nci->nci_pad = 0;
		nci->nci_cno = cnp->cp_cno;
		nci->nci_create = cnp->cp_create;
		nci->nci_nblk_inc = cnp->cp_nblk_inc;
		nci->nci_blocks_count = cnp->cp_blocks_count;
		nci->nci_next = cnp->cp_snapshot_list.ssl_next;
		if (nmembs)
			(*nmembs)++;

		curr_cno = nci->nci_next;
		if (!curr_cno)
			break;
	}

	brelse(bp);

	return (0);
}

int
nandfs_get_cpinfo(struct nandfs_node *node, uint64_t cno, uint16_t flags,
    struct nandfs_cpinfo *nci, uint32_t nmembs, uint32_t *nnmembs)
{
	int error;

	VOP_LOCK(NTOV(node), LK_EXCLUSIVE);
	switch (flags) {
	case NANDFS_CHECKPOINT:
		error = nandfs_get_cpinfo_cp(node, cno, nci, nmembs, nnmembs);
		break;
	case NANDFS_SNAPSHOT:
		error = nandfs_get_cpinfo_sp(node, cno, nci, nmembs, nnmembs);
		break;
	default:
		error = EINVAL;
		break;
	}
	VOP_UNLOCK(NTOV(node), 0);

	return (error);
}

int
nandfs_get_cpinfo_ioctl(struct nandfs_node *node, struct nandfs_argv *nargv)
{
	struct nandfs_cpinfo *nci;
	uint64_t cno = nargv->nv_index;
	void *buf = (void *)((uintptr_t)nargv->nv_base);
	uint16_t flags = nargv->nv_flags;
	uint32_t nmembs = 0;
	int error;

	if (nargv->nv_nmembs > NANDFS_CPINFO_MAX)
		return (EINVAL);

	nci = malloc(sizeof(struct nandfs_cpinfo) * nargv->nv_nmembs,
	    M_NANDFSTEMP, M_WAITOK | M_ZERO);

	error = nandfs_get_cpinfo(node, cno, flags, nci, nargv->nv_nmembs, &nmembs);

	if (error == 0) {
		nargv->nv_nmembs = nmembs;
		error = copyout(nci, buf,
		    sizeof(struct nandfs_cpinfo) * nmembs);
	}

	free(nci, M_NANDFSTEMP);
	return (error);
}

int
nandfs_delete_cp(struct nandfs_node *node, uint64_t start, uint64_t end)
{
	struct nandfs_checkpoint *cnp;
	struct nandfs_device *fsdev;
	struct buf *bp;
	uint64_t cno = start, blk, offset;
	int error;

	DPRINTF(CPFILE, ("%s: delete cno %jx-%jx\n", __func__, start, end));
	VOP_LOCK(NTOV(node), LK_EXCLUSIVE);
	fsdev = node->nn_nandfsdev;
	for (cno = start; cno <= end; cno++) {
		if (!cno)
			continue;

		nandfs_checkpoint_blk_offset(fsdev, cno, &blk, &offset);
		error = nandfs_bread(node, blk, NOCRED, 0, &bp);
		if (error) {
			VOP_UNLOCK(NTOV(node), 0);
			brelse(bp);
			return (error);
		}

		cnp = (struct nandfs_checkpoint *)(bp->b_data + offset);
		if (cnp->cp_flags & NANDFS_CHECKPOINT_SNAPSHOT) {
			brelse(bp);
			VOP_UNLOCK(NTOV(node), 0);
			return (0);
		}

		cnp->cp_flags |= NANDFS_CHECKPOINT_INVALID;

		error = nandfs_dirty_buf(bp, 0);
		if (error)
			return (error);
	}
	VOP_UNLOCK(NTOV(node), 0);

	return (0);
}

int
nandfs_make_snap(struct nandfs_device *fsdev, uint64_t *cno)
{
	struct nandfs_cpmode cpm;
	int error;

	*cno = cpm.ncpm_cno = fsdev->nd_last_cno;
	cpm.ncpm_mode = NANDFS_SNAPSHOT;
	error = nandfs_chng_cpmode(fsdev->nd_cp_node, &cpm);
	return (error);
}

int
nandfs_delete_snap(struct nandfs_device *fsdev, uint64_t cno)
{
	struct nandfs_cpmode cpm;
	int error;

	cpm.ncpm_cno = cno;
	cpm.ncpm_mode = NANDFS_CHECKPOINT;
	error = nandfs_chng_cpmode(fsdev->nd_cp_node, &cpm);
	return (error);
}

int nandfs_get_cpstat(struct nandfs_node *cp_node, struct nandfs_cpstat *ncp)
{
	struct nandfs_device *fsdev;
	struct nandfs_cpfile_header *cnh;
	struct buf *bp;
	int error;

	VOP_LOCK(NTOV(cp_node), LK_EXCLUSIVE);
	fsdev = cp_node->nn_nandfsdev;

	/* Get header */
	error = nandfs_bread(cp_node, 0, NOCRED, 0, &bp);
	if (error) {
		brelse(bp);
		VOP_UNLOCK(NTOV(cp_node), 0);
		return (error);
	}
	cnh = (struct nandfs_cpfile_header *) bp->b_data;
	ncp->ncp_cno = fsdev->nd_last_cno;
	ncp->ncp_ncps = cnh->ch_ncheckpoints;
	ncp->ncp_nss = cnh->ch_nsnapshots;
	DPRINTF(CPFILE, ("%s: cno:%#jx ncps:%#jx nss:%#jx\n",
	    __func__, ncp->ncp_cno, ncp->ncp_ncps, ncp->ncp_nss));
	brelse(bp);
	VOP_UNLOCK(NTOV(cp_node), 0);

	return (0);
}
OpenPOWER on IntegriCloud