xref: /dflybsd-src/contrib/lvm2/dist/lib/device/dev-swap.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*	$NetBSD: dev-swap.c,v 1.2 2009/12/02 01:53:25 haad Exp $	*/
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino  * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
5*86d7f5d3SJohn Marino  *
6*86d7f5d3SJohn Marino  * This file is part of LVM2.
7*86d7f5d3SJohn Marino  *
8*86d7f5d3SJohn Marino  * This copyrighted material is made available to anyone wishing to use,
9*86d7f5d3SJohn Marino  * modify, copy, or redistribute it subject to the terms and conditions
10*86d7f5d3SJohn Marino  * of the GNU Lesser General Public License v.2.1.
11*86d7f5d3SJohn Marino  *
12*86d7f5d3SJohn Marino  * You should have received a copy of the GNU Lesser General Public License
13*86d7f5d3SJohn Marino  * along with this program; if not, write to the Free Software Foundation,
14*86d7f5d3SJohn Marino  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15*86d7f5d3SJohn Marino  */
16*86d7f5d3SJohn Marino 
17*86d7f5d3SJohn Marino #include "lib.h"
18*86d7f5d3SJohn Marino #include "metadata.h"
19*86d7f5d3SJohn Marino #include "xlate.h"
20*86d7f5d3SJohn Marino #include "filter.h"
21*86d7f5d3SJohn Marino 
22*86d7f5d3SJohn Marino #ifdef linux
23*86d7f5d3SJohn Marino 
24*86d7f5d3SJohn Marino #define MAX_PAGESIZE	(64 * 1024)
25*86d7f5d3SJohn Marino #define SIGNATURE_SIZE  10
26*86d7f5d3SJohn Marino 
27*86d7f5d3SJohn Marino static int
_swap_detect_signature(const char * buf)28*86d7f5d3SJohn Marino _swap_detect_signature(const char *buf)
29*86d7f5d3SJohn Marino {
30*86d7f5d3SJohn Marino 	if (memcmp(buf, "SWAP-SPACE", 10) == 0 ||
31*86d7f5d3SJohn Marino             memcmp(buf, "SWAPSPACE2", 10) == 0)
32*86d7f5d3SJohn Marino 		return 1;
33*86d7f5d3SJohn Marino 
34*86d7f5d3SJohn Marino 	if (memcmp(buf, "S1SUSPEND", 9) == 0 ||
35*86d7f5d3SJohn Marino 	    memcmp(buf, "S2SUSPEND", 9) == 0 ||
36*86d7f5d3SJohn Marino 	    memcmp(buf, "ULSUSPEND", 9) == 0 ||
37*86d7f5d3SJohn Marino 	    memcmp(buf, "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8) == 0)
38*86d7f5d3SJohn Marino 		return 1;
39*86d7f5d3SJohn Marino 
40*86d7f5d3SJohn Marino 	return 0;
41*86d7f5d3SJohn Marino }
42*86d7f5d3SJohn Marino 
dev_is_swap(struct device * dev,uint64_t * signature)43*86d7f5d3SJohn Marino int dev_is_swap(struct device *dev, uint64_t *signature)
44*86d7f5d3SJohn Marino {
45*86d7f5d3SJohn Marino 	char buf[10];
46*86d7f5d3SJohn Marino 	uint64_t size;
47*86d7f5d3SJohn Marino 	int page;
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino 	if (!dev_get_size(dev, &size)) {
50*86d7f5d3SJohn Marino 		stack;
51*86d7f5d3SJohn Marino 		return -1;
52*86d7f5d3SJohn Marino 	}
53*86d7f5d3SJohn Marino 
54*86d7f5d3SJohn Marino 	if (!dev_open(dev)) {
55*86d7f5d3SJohn Marino 		stack;
56*86d7f5d3SJohn Marino 		return -1;
57*86d7f5d3SJohn Marino 	}
58*86d7f5d3SJohn Marino 
59*86d7f5d3SJohn Marino 	*signature = 0;
60*86d7f5d3SJohn Marino 
61*86d7f5d3SJohn Marino 	for (page = 0x1000; page <= MAX_PAGESIZE; page <<= 1) {
62*86d7f5d3SJohn Marino 		/*
63*86d7f5d3SJohn Marino 		 * skip 32k pagesize since this does not seem to be supported
64*86d7f5d3SJohn Marino 		 */
65*86d7f5d3SJohn Marino 		if (page == 0x8000)
66*86d7f5d3SJohn Marino 			continue;
67*86d7f5d3SJohn Marino 		if (size < page)
68*86d7f5d3SJohn Marino 			break;
69*86d7f5d3SJohn Marino 		if (!dev_read(dev, page - SIGNATURE_SIZE,
70*86d7f5d3SJohn Marino 			      SIGNATURE_SIZE, buf)) {
71*86d7f5d3SJohn Marino 			stack;
72*86d7f5d3SJohn Marino 			return -1;
73*86d7f5d3SJohn Marino 		}
74*86d7f5d3SJohn Marino 		if (_swap_detect_signature(buf)) {
75*86d7f5d3SJohn Marino 			*signature = page - SIGNATURE_SIZE;
76*86d7f5d3SJohn Marino 			break;
77*86d7f5d3SJohn Marino 		}
78*86d7f5d3SJohn Marino 	}
79*86d7f5d3SJohn Marino 
80*86d7f5d3SJohn Marino 	if (!dev_close(dev))
81*86d7f5d3SJohn Marino 		stack;
82*86d7f5d3SJohn Marino 
83*86d7f5d3SJohn Marino 	if (*signature)
84*86d7f5d3SJohn Marino 		return 1;
85*86d7f5d3SJohn Marino 
86*86d7f5d3SJohn Marino 	return 0;
87*86d7f5d3SJohn Marino }
88*86d7f5d3SJohn Marino 
89*86d7f5d3SJohn Marino #else
90*86d7f5d3SJohn Marino 
91*86d7f5d3SJohn Marino #if defined(__NetBSD__) || defined(__DragonFly__)
dev_is_swap(struct device * dev,uint64_t * signature)92*86d7f5d3SJohn Marino int dev_is_swap(struct device *dev, uint64_t *signature)
93*86d7f5d3SJohn Marino {
94*86d7f5d3SJohn Marino 	return 0;
95*86d7f5d3SJohn Marino }
96*86d7f5d3SJohn Marino #endif
97*86d7f5d3SJohn Marino 
98*86d7f5d3SJohn Marino #endif
99