File: inv_core.c

package info (click to toggle)
xfsdump 3.1.9%2B0
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 3,932 kB
  • sloc: ansic: 45,863; sh: 3,227; makefile: 545
file content (232 lines) | stat: -rw-r--r-- 5,694 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <xfs/xfs.h>
#include <xfs/jdm.h>

#include <unistd.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/dir.h>
#include <assert.h>
#include <string.h>

#include "config.h"

#include "types.h"
#include "inv_priv.h"



/*----------------------------------------------------------------------*/
/*  get_counters, get_headers, get_invtrecord, put_invtrecord, ...      */
/*                                                                      */
/*  These implement low level routines that take care of disk I/Os.     */
/*  In most cases, the caller has the option of locking before calling  */
/*  the routine.                                                        */
/*                                                                      */
/*----------------------------------------------------------------------*/

int
get_counters(int fd, void **cntpp, size_t cntsz)
{
	/* object must be locked at least SHARED by caller */
	uint num;
	assert(cntsz >= sizeof(invt_counter_t));

	*cntpp =  calloc(1, cntsz);

	/* find the number of sessions and the max possible */
	if (GET_REC_NOLOCK(fd, (void *) *cntpp, cntsz, (off64_t) 0) < 0) {
		free(*cntpp);
		*cntpp = NULL;
		return -1;
	}

	num = ((invt_counter_t *)(*cntpp))->ic_curnum;

	if (((invt_counter_t *)(*cntpp))->ic_vernum != INV_VERSION) {
		mlog(MLOG_NORMAL | MLOG_INV, _(
		      "INV : Unknown version %d - Expected version %d\n"),
		      (int) ((invt_counter_t *)(*cntpp))->ic_vernum,
		      (int) INV_VERSION);
		assert (((invt_counter_t *)(*cntpp))->ic_vernum ==
			INV_VERSION);
	}

	return (int) num;
}





/*----------------------------------------------------------------------*/
/* get_headers                                                          */
/*----------------------------------------------------------------------*/

int
get_headers(int fd, void **hdrs, size_t bufsz, size_t off)
{

	*hdrs = malloc(bufsz);
	if (*hdrs == NULL) {
		INV_PERROR(_("get_headers() - malloc(seshdrs)\n"));
		return -1;
	}
	/* file must be locked at least SHARED by caller */

	/* get the array of hdrs */
	if (GET_REC_NOLOCK(fd, (void *) *hdrs, bufsz, (off64_t)off) < 0) {
		free (*hdrs);
		*hdrs = NULL;
		return -1;
	}

	return 1;
}



/*----------------------------------------------------------------------*/
/* get_invtrecord                                                       */
/*----------------------------------------------------------------------*/

int
get_invtrecord(int fd, void *buf, size_t bufsz, off64_t off,
		bool_t dolock)
{
	int  nread;

	assert (fd >= 0);

	if (dolock)
		INVLOCK(fd, LOCK_SH);

	nread = pread(fd, buf, bufsz, (off_t)off);
	if (nread != (int) bufsz) {
		INV_PERROR(_("Error in reading inventory record :"));
		if (dolock)
			INVLOCK(fd, LOCK_UN);
		return -1;
	}

	if (dolock)
		INVLOCK(fd, LOCK_UN);

	return nread;
}






/*----------------------------------------------------------------------*/
/* put_invtrecord                                                       */
/*----------------------------------------------------------------------*/

int
put_invtrecord(int fd, void *buf, size_t bufsz, off64_t off, bool_t dolock)
{
	int nwritten;

	if (dolock)
		INVLOCK(fd, LOCK_EX);

	nwritten = pwrite(fd, buf, bufsz, (off_t)off);
	if (nwritten != (int) bufsz) {
		INV_PERROR(_("Error in writing inventory record :"));
		if (dolock)
			INVLOCK(fd, LOCK_UN);
		return -1;
	}

	if (dolock)
		INVLOCK(fd, LOCK_UN);
	return nwritten;
}







/*----------------------------------------------------------------------*/
/* get_headerinfo                                                       */
/*----------------------------------------------------------------------*/


int
get_headerinfo(int fd, void **hdrs, void **cnt,
	        size_t hdrsz, size_t cntsz, bool_t dolock)
{
	int num;

	/* get a lock on the table for reading */
	if (dolock) INVLOCK(fd, LOCK_SH);

	num = get_counters(fd, cnt, cntsz);

	/* If there are no sessions recorded yet, we're done too */
	if (num > 0) {
		if (get_headers(fd, hdrs, hdrsz * (size_t)num, cntsz) < 0) {
			free (*cnt);
			num = -1;
		}
	}

	if (dolock) INVLOCK(fd, LOCK_UN);
	return num;
}



/*----------------------------------------------------------------------*/
/* get_lastheader                                                       */
/*----------------------------------------------------------------------*/

int
get_lastheader(int fd, void **ent, size_t hdrsz, size_t cntsz)
{
	int	     	 nindices;
	void	 	 *arr = NULL;
	invt_counter_t	 *cnt = NULL;
	char 		 *pos;
	/* get the entries in the inv_index */
	if ((nindices = GET_ALLHDRS_N_CNTS(fd, &arr, (void **)&cnt,
					 hdrsz, cntsz)) <= 0) {
		return -1;
	}

	/* if there's space anywhere at all, then it must be in the last
	   entry */
	*ent = malloc(hdrsz);
	pos = (char *) arr + ((uint)nindices - 1) * hdrsz;
	memcpy(*ent, pos, hdrsz);
	free (arr);
	free (cnt);

	return nindices;
}