ntfs_glue.c 10.7 KB
Newer Older
Mike Hibler's avatar
Mike Hibler committed
1
2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2005 University of Utah and the Flux Group.
Mike Hibler's avatar
Mike Hibler committed
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
 * All rights reserved.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#ifndef linux
#include <linux/fs.h>
#endif
#include "volume.h"
#include "inode.h"
#include "support.h" 
#include "attrib.h"
#include "runlist.h"
#include "dir.h"

#include "sliceinfo.h"
#include "global.h"

/********Code to deal with NTFS file system*************/
/* Written by: Russ Christensen <rchriste@cs.utah.edu> */

/**@bug If the Windows partition is only created in a part of a
 * primary partition created with FreeBSD's fdisk then the sectors
 * after the end of the NTFS partition and before the end of the raw
 * primary partition will not be marked as free space. */


struct ntfs_cluster;
struct ntfs_cluster {
	unsigned long start;
	unsigned long length;
	struct ntfs_cluster *next;
};

static __inline__ int
ntfs_isAllocated(char *map, __s64 pos)
{
	int result;
	char unmasked;
	char byte;
	short shift;
	byte = *(map+(pos/8));
	shift = pos % 8;
	unmasked = byte >> shift;
	result = unmasked & 1;
	assert((result == 0 || result == 1) &&
	       "Programming error in statement above");
	return result;
}

static void
ntfs_addskips(ntfs_volume *vol,struct ntfs_cluster *free,u_int32_t offset)
{
	u_int8_t sectors_per_cluster;
	struct ntfs_cluster *cur;
	int count = 0;
	sectors_per_cluster = vol->cluster_size / vol->sector_size;
	if(debug) {
		fprintf(stderr,"sectors per cluster: %d\n",
			sectors_per_cluster);
		fprintf(stderr,"offset: %d\n", offset);
	}
	for(count = 0, cur = free; cur != NULL; cur = cur->next, count++) {
		if(debug > 1) {
			fprintf(stderr, "\tGroup:%-10dCluster%8li, size%8li\n",
				count, cur->start,cur->length);
		}
		addskip(cur->start*sectors_per_cluster + offset,
			cur->length*sectors_per_cluster);
	}
}

static int
ntfs_freeclusters(struct ntfs_cluster *free)
{
	int total;
	struct ntfs_cluster *cur;
	for(total = 0, cur = free; cur != NULL; cur = cur->next)
		total += cur->length;
	return total;
}

/* The calling function owns the pointer returned.*/
static void *
ntfs_read_data_attr(ntfs_attr *na)
{
	void  *result;
	int64_t pos;
	int64_t tmp;
	int64_t amount_needed;
	int   count;

	/**ntfs_attr_pread might actually read in more data than we
	 * ask for.  It will round up to the nearest sector boundry
	 * so make sure we allocate enough memory.*/
	amount_needed = na->data_size - (na->data_size % secsize) + secsize;
	assert(amount_needed > na->data_size && amount_needed % secsize == 0
	       && "amount_needed is rounded up to sector size multiple");
	if(!(result = malloc(amount_needed))) {
		perror("Out of memory!\n");
		exit(1);
	}
	pos = 0;
	count = 0;
	while(pos < na->data_size) {
		tmp = ntfs_attr_pread(na,pos,na->data_size - pos,result+pos);
		if(tmp < 0) {
			perror("ntfs_attr_pread failed");
			exit(1);
		}
		assert(tmp != 0 && "Not supposed to happen error!  "
		       "Either na->data_size is wrong or there is another "
		       "problem");
120
#ifndef linux
Mike Hibler's avatar
Mike Hibler committed
121
		assert(tmp % secsize == 0 && "Not supposed to happen");
122
#endif
Mike Hibler's avatar
Mike Hibler committed
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
		pos += tmp;
	}
#if 0 /*Turn on if you want to look at the free list directly*/
	{
		int fd;

		fprintf(stderr, "Writing ntfs_free_bitmap.bin\n");
		if((fd = open("ntfs_free_bitmap.bin",
			      O_WRONLY | O_CREAT | O_TRUNC)) < 0) {
			perror("open ntfs_free_bitmap.bin failed\n");
			exit(1);
		}
		if(write(fd, result, na->data_size) != na->data_size) {
			perror("writing free space bitmap.bin failed\n");
			exit(1);
		}
		close(fd);
		fprintf(stderr, "Done\n");
	}
#endif
	return result;
}

static struct ntfs_cluster *
ntfs_compute_freeblocks(ntfs_attr *na, void *cluster_map, __s64 num_clusters)
{
	struct ntfs_cluster *result;
	struct ntfs_cluster *curr;
	struct ntfs_cluster *tmp;
	__s64 pos = 1;
	int total_free = 0;
	result = curr = NULL;
	assert(num_clusters <= na->data_size * 8 && "If there are more "
	       "clusters than bits in the free space file then we have a "
	       "problem.  Fewer clusters than bits is okay.");
	if(debug)
		fprintf(stderr,"num_clusters==%qd\n",num_clusters);
	while(pos < num_clusters) {
		if(!ntfs_isAllocated(cluster_map,pos++)) {
			curr->length++;
			total_free++;
		}
		else {
			while(ntfs_isAllocated(cluster_map,pos)
			      && pos < num_clusters) {
				++pos;
			}
			if(pos >= num_clusters) break;
			if(!(tmp = malloc(sizeof(struct ntfs_cluster)))) {
				perror("clusters_free: Out of memory");
				exit(1);
			}
			if(curr) {
				curr->next = tmp;
				curr = curr->next;
			} else
				result = curr = tmp;
			curr->start = pos;
			curr->length = 0;
			curr->next = NULL;
		}
	}
	if(debug)
		fprintf(stderr, "total_free==%d\n",total_free);
	return result;
}

/*Add the blocks used by filename to the free list*/
void
ntfs_skipfile(ntfs_volume *vol, char *filename, u_int32_t offset)
{
	u_int8_t sectors_per_cluster;
	ntfs_inode *ni, *ni_root;
	ntfs_attr *na;
	MFT_REF File;
	runlist_element *rl;
	int ulen;
	uchar_t *ufilename;
	int i;
	int amount_skipped;

	/*Goal: Get MFT_REF for filename before we can call ntfs_inode_open
	        on the file.*/
	if(!(ni_root = ntfs_inode_open(vol, FILE_root))) {
		perror("Opening file $ROOT failed\n");
		ntfs_umount(vol,TRUE);
		exit(1);
	}
	/* Subgoal: get the uchar_t name for filename */
	ufilename = malloc(sizeof(uchar_t)*(strlen(filename)+1));
	if(!ufilename) {
		fprintf(stderr, "Out of memory\n");
		exit(1);
	}
	bzero(ufilename,sizeof(uchar_t)*strlen(filename)+1);
	ulen = ntfs_mbstoucs(filename, &ufilename, strlen(filename)+1);
	if(ulen == -1) {
		perror("ntfs_mbstoucs failed");
		exit(1);
	}
	File = ntfs_inode_lookup_by_name(ni_root, ufilename, ulen);
	if(IS_ERR_MREF(File)) {
		if (debug > 1) {
			fprintf(stderr, "%s does not exist so there is no need "
				"to skip the file.\n", filename);
		}
		return;
	}
  	free(ufilename);
	ufilename = NULL;
	if(debug > 1 ) fprintf(stderr,"vol->nr_mft_records==%lld\n",
			       vol->nr_mft_records);
	/*Goal: Skip the file*/
	if(!(ni = ntfs_inode_open(vol, File))) {
	  perror("calling ntfs_inode_open (0)");
	  ntfs_umount(vol,TRUE);
	  exit(1);
	}
	if(!(na = ntfs_attr_open(ni, AT_DATA, NULL, 0))) {
		perror("Opening attribute $DATA failed\n");
		ntfs_umount(vol,TRUE);
		exit(1);
	}
	assert(NAttrNonResident(na) && "You are trying to skip a file that is "
	       "small enough to be resident inside the Master File Table. "
	       "This is a bit silly.");
	/*Goal: Find out what clusters on the disk are being used by filename*/
	sectors_per_cluster = vol->cluster_size / vol->sector_size;
	if(!(rl = ntfs_attr_find_vcn(na, 0))) {
	    perror("Error calling ntfs_attr_find_vcn");
	    exit(1);
	}
	amount_skipped = 0;
	for(i=0; rl[i].length != 0; i++) {
		if (rl[i].lcn == LCN_HOLE) {
		    if (debug > 1) {
			fprintf(stderr, "LCN_HOLE\n");
		    }
		    continue;
		}
		if (rl[i].lcn == LCN_RL_NOT_MAPPED) {
		    /* Pull in more of the runlist because the NTFS library
		       might not pull in the entire runlist when you ask
		       for it.  When I asked the NTFS library folks why they
		       do this they said it was for performance reasons. */
		    if (debug > 1) {
			fprintf(stderr, "LCN_RL_NOT_MAPPED\n");
		    }
		    if (ntfs_attr_map_runlist(na, rl[i].vcn) == -1) {
			perror("ntfs_attr_map_runlist failed\n");
			exit(1);
		    } else {
			rl = ntfs_attr_find_vcn(na, 0);
			/* There *might* be a memory leak here.  I don't
			   know if rl needs to be freed by us or not. */
			if(!rl) {
			    perror("Error calling ntfs_attr_find_vcn");
			    exit(1);
			}
			/*retry*/
			--i;
			continue;
		    }
		}
		if (debug > 1) {
		    fprintf(stderr, "For file %s skipping:%lld length:%lld\n",
			    filename,
			    (long long int)rl[i].lcn*sectors_per_cluster +
			    offset,
			    (long long int)rl[i].length*sectors_per_cluster);
		}
		assert(rl[i].length > 0 && "Programming error");
		assert(rl[i].lcn > 0 &&
		       "Programming error: Not catching NTFS Lib error value");
		amount_skipped += rl[i].length*sectors_per_cluster;
		addskip(rl[i].lcn*sectors_per_cluster + offset,
			rl[i].length*sectors_per_cluster);
	}
	if (debug) {
	    fprintf(stderr, "For NTFS file %s skipped %d bytes\n", filename,
		    amount_skipped*512);
	}
}

/*
 * Primary function to call to operate on an NTFS slice.
 */
int
read_ntfsslice(int slice, int stype, u_int32_t start, u_int32_t size,
	       char *openname, int infd)
{
	ntfs_inode     *ni_bitmap;
	ntfs_attr      *na_bitmap;
	void           *buf;
	struct ntfs_cluster *cfree;
  	struct ntfs_cluster *tmp;
	ntfs_volume    *vol;
320
	s64	       offset;
Mike Hibler's avatar
Mike Hibler committed
321
322
323
324
325
326
327
328
329
330
331

	/* Check to make sure the types the NTFS lib defines are what they
	   claim*/
	assert(sizeof(s64) == 8);
	assert(sizeof(s32) == 4);
	assert(sizeof(u64) == 8);
	assert(sizeof(u32) == 4);
	/*
	 * The NTFS library needs the /dev name of the partition to examine.
	 */
	if (slice < 0)
332
		offset = 0LL;
Mike Hibler's avatar
Mike Hibler committed
333
	else
334
		offset = sectobytes(start);
Mike Hibler's avatar
Mike Hibler committed
335
	if (debug)
336
337
		fprintf(stderr, "Using %s at offset %qu for NTFS slice %d\n",
			openname, offset, slice+1);
Mike Hibler's avatar
Mike Hibler committed
338
	/*The volume must be mounted to find out what clusters are free*/
339
340
	if(!(vol = ntfs_mount_with_offset(openname, MS_RDONLY, offset))) {
		perror(openname);
Mike Hibler's avatar
Mike Hibler committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
		fprintf(stderr, "Failed to read superblock information.  "
			"Not a valid NTFS partition\n");
		return 1;
	}
	/*A bitmap of free clusters is in the $DATA attribute of the
	 *  $BITMAP file*/
	if(!(ni_bitmap = ntfs_inode_open(vol, FILE_Bitmap))) {
		perror("Opening file $BITMAP failed\n");
		ntfs_umount(vol, TRUE);
		return 1;
	}
	if(!(na_bitmap = ntfs_attr_open(ni_bitmap, AT_DATA, NULL, 0))) {
		perror("Opening attribute $DATA failed\n");
		return 1;
	}
	buf = ntfs_read_data_attr(na_bitmap);
	cfree = ntfs_compute_freeblocks(na_bitmap,buf,vol->nr_clusters);
	ntfs_addskips(vol,cfree,start);
	if(debug > 1) {
		fprintf(stderr, "  P%d (NTFS v%u.%u)\n",
			slice + 1 /* DOS Numbering */,
			vol->major_ver,vol->minor_ver);
363
		fprintf(stderr, "        %s",openname);
Mike Hibler's avatar
Mike Hibler committed
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
		fprintf(stderr, "      start %10d, size %10d\n", start, size);
		fprintf(stderr, "        Sector size: %u, Cluster size: %u\n",
			vol->sector_size, vol->cluster_size);
		fprintf(stderr, "        Volume size in clusters: %qd\n",
			vol->nr_clusters);
		fprintf(stderr, "        Free clusters:\t\t %u\n",
			ntfs_freeclusters(cfree));
	}

      	ntfs_skipfile(vol, "pagefile.sys", start);
      	ntfs_skipfile(vol, "hiberfil.sys", start);

	/*We have the information we need so unmount everything*/
	ntfs_attr_close(na_bitmap);
	if(ntfs_inode_close(ni_bitmap)) {
		perror("ntfs_close_inode ni_bitmap failed");
		return 1;
	}
	if(ntfs_umount(vol,FALSE)) {
		perror("ntfs_umount failed");
		return 1;
	}
	/*Free NTFS malloc'd memory*/
	assert(buf && "Programming Error, buf should be freed here");
	free(buf);
	assert(cfree && "Programming Error, "
	       "'struct cfree' should be freed here");
	while(cfree) {
		tmp = cfree->next;
		free(cfree);
		cfree = tmp;
	}
	return 0; /*Success*/
}