xref: /netbsd-src/external/gpl2/lvm2/dist/lib/device/dev-swap.c (revision 0fa7e5a89a8a2bd6049fccc8d024ce2e379c576d)
1*0fa7e5a8Shaad /*	$NetBSD: dev-swap.c,v 1.2 2009/12/02 01:53:25 haad Exp $	*/
27c604eeaShaad 
37c604eeaShaad /*
47c604eeaShaad  * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
57c604eeaShaad  *
67c604eeaShaad  * This file is part of LVM2.
77c604eeaShaad  *
87c604eeaShaad  * This copyrighted material is made available to anyone wishing to use,
97c604eeaShaad  * modify, copy, or redistribute it subject to the terms and conditions
107c604eeaShaad  * of the GNU Lesser General Public License v.2.1.
117c604eeaShaad  *
127c604eeaShaad  * You should have received a copy of the GNU Lesser General Public License
137c604eeaShaad  * along with this program; if not, write to the Free Software Foundation,
147c604eeaShaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
157c604eeaShaad  */
167c604eeaShaad 
177c604eeaShaad #include "lib.h"
187c604eeaShaad #include "metadata.h"
197c604eeaShaad #include "xlate.h"
207c604eeaShaad #include "filter.h"
217c604eeaShaad 
227c604eeaShaad #ifdef linux
237c604eeaShaad 
247c604eeaShaad #define MAX_PAGESIZE	(64 * 1024)
257c604eeaShaad #define SIGNATURE_SIZE  10
267c604eeaShaad 
277c604eeaShaad static int
_swap_detect_signature(const char * buf)287c604eeaShaad _swap_detect_signature(const char *buf)
297c604eeaShaad {
307c604eeaShaad 	if (memcmp(buf, "SWAP-SPACE", 10) == 0 ||
317c604eeaShaad             memcmp(buf, "SWAPSPACE2", 10) == 0)
327c604eeaShaad 		return 1;
337c604eeaShaad 
347c604eeaShaad 	if (memcmp(buf, "S1SUSPEND", 9) == 0 ||
357c604eeaShaad 	    memcmp(buf, "S2SUSPEND", 9) == 0 ||
367c604eeaShaad 	    memcmp(buf, "ULSUSPEND", 9) == 0 ||
377c604eeaShaad 	    memcmp(buf, "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8) == 0)
387c604eeaShaad 		return 1;
397c604eeaShaad 
407c604eeaShaad 	return 0;
417c604eeaShaad }
427c604eeaShaad 
dev_is_swap(struct device * dev,uint64_t * signature)437c604eeaShaad int dev_is_swap(struct device *dev, uint64_t *signature)
447c604eeaShaad {
457c604eeaShaad 	char buf[10];
467c604eeaShaad 	uint64_t size;
477c604eeaShaad 	int page;
487c604eeaShaad 
497c604eeaShaad 	if (!dev_get_size(dev, &size)) {
507c604eeaShaad 		stack;
517c604eeaShaad 		return -1;
527c604eeaShaad 	}
537c604eeaShaad 
547c604eeaShaad 	if (!dev_open(dev)) {
557c604eeaShaad 		stack;
567c604eeaShaad 		return -1;
577c604eeaShaad 	}
587c604eeaShaad 
597c604eeaShaad 	*signature = 0;
607c604eeaShaad 
617c604eeaShaad 	for (page = 0x1000; page <= MAX_PAGESIZE; page <<= 1) {
627c604eeaShaad 		/*
637c604eeaShaad 		 * skip 32k pagesize since this does not seem to be supported
647c604eeaShaad 		 */
657c604eeaShaad 		if (page == 0x8000)
667c604eeaShaad 			continue;
677c604eeaShaad 		if (size < page)
687c604eeaShaad 			break;
697c604eeaShaad 		if (!dev_read(dev, page - SIGNATURE_SIZE,
707c604eeaShaad 			      SIGNATURE_SIZE, buf)) {
717c604eeaShaad 			stack;
727c604eeaShaad 			return -1;
737c604eeaShaad 		}
747c604eeaShaad 		if (_swap_detect_signature(buf)) {
757c604eeaShaad 			*signature = page - SIGNATURE_SIZE;
767c604eeaShaad 			break;
777c604eeaShaad 		}
787c604eeaShaad 	}
797c604eeaShaad 
807c604eeaShaad 	if (!dev_close(dev))
817c604eeaShaad 		stack;
827c604eeaShaad 
837c604eeaShaad 	if (*signature)
847c604eeaShaad 		return 1;
857c604eeaShaad 
867c604eeaShaad 	return 0;
877c604eeaShaad }
887c604eeaShaad 
89*0fa7e5a8Shaad #else
90*0fa7e5a8Shaad 
91*0fa7e5a8Shaad #ifdef __NetBSD__
dev_is_swap(struct device * dev,uint64_t * signature)92*0fa7e5a8Shaad int dev_is_swap(struct device *dev, uint64_t *signature)
93*0fa7e5a8Shaad {
94*0fa7e5a8Shaad 	return 0;
95*0fa7e5a8Shaad }
96*0fa7e5a8Shaad #endif
97*0fa7e5a8Shaad 
987c604eeaShaad #endif
99