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