-
Notifications
You must be signed in to change notification settings - Fork 5
/
linux.c
300 lines (282 loc) · 9.33 KB
/
linux.c
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
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
/***************************************************************************/
/* Copyright (C) 2006-2011 Brice Arnould. */
/* */
/* This file is part of ShaKe. */
/* */
/* ShaKe 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; either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>. */
/***************************************************************************/
#include "linux.h"
#include <stdlib.h>
#include <stdio.h> // snprintf
#include <limits.h> // CHAR_BIT
#include <time.h> // time, time_t
#include <assert.h> // assert
#include <errno.h> // errno
#include <error.h> // error()
#include <fcntl.h> // fcntl()
#include <signal.h> // sigaction()
#include <unistd.h> // fcntl()
#include <sys/ioctl.h> // ioctl()
#include <sys/xattr.h> // fgetxattr(), setxattr()
#include <linux/fs.h> // FIBMAP, FIGETBSZ
#include <arpa/inet.h> // htonl, ntohl
/* The following try to hide Linux-specific leases behind an interface
* similar to Posix locks.
* It does a lot of thread-unsafe black magic.
*/
#define SIGLOCKEXPIRED OS_RESERVED_SIGNAL
#define MAX_LOCKED_FDS 2 // Never greater than 1
/* Describe locks
*/
struct lock_desc
{
const char *filename;
int fd;
bool write;
};
/* All the currently managed locks (thread-unsafe, would require a mutex)
*/
struct lock_desc LOCKS[MAX_LOCKED_FDS];
/* The current temporary file
*/
const char *TEMPFILE;
/* Return the position of the given fd in LOCKS or a position for the
* invalid fd ( -1 ) if there is no such position
* We are sure it exists because there can only be one locked file
* and LOCKS has a size of 2.
*/
static int
locate_lock (int searchedfd)
{
int invalidfd_pos = -1;
for (int i = 0; i < MAX_LOCKED_FDS; i++)
if (LOCKS[i].fd == searchedfd)
return i;
else if (LOCKS[i].fd == -1)
invalidfd_pos = i;
assert (invalidfd_pos >= 0);
return invalidfd_pos;
}
/* Called when a lease is being cancelled
*/
static void
handle_broken_locks (int sig, siginfo_t * info, void *ignored)
{
assert (SIGLOCKEXPIRED == sig), assert (ignored);
int fd = info->si_fd;
int pos = locate_lock (fd);
assert (LOCKS[pos].fd != -1);
if (LOCKS[pos].write)
error (0, 0,
"%s: Another program is trying to access the file; "
"if shaking takes more than lease-break-time seconds "
"shake will be killed; if this happens a backup will be "
"available in '%s'", LOCKS[pos].filename, TEMPFILE);
else
{
// Cancel this lock
LOCKS[pos].fd = -1;
error (0, 0, "%s: concurent accesses", LOCKS[pos].filename);
}
}
int
os_specific_setup (const char *tempfile)
{
/* Initialize globals */
TEMPFILE = tempfile;
for (int i = 0; i < MAX_LOCKED_FDS; i++)
LOCKS[i].fd = -1;
/* Setup SIGLOCKEXPIRED handler */
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handle_broken_locks;
return sigaction (SIGLOCKEXPIRED, &sa, NULL);
}
int
readlock_file (int fd, const char *filename)
{
int pos = locate_lock (fd);
assert (LOCKS[pos].fd == -1);
// Technically all our locks are write leases
if (fcntl (fd, F_SETLEASE, F_WRLCK) != 0)
return -1;
if (fcntl (fd, F_SETSIG, SIGLOCKEXPIRED) != 0)
return -1;
/* Register the lock in LOCKS */
{
LOCKS[pos].filename = filename;
LOCKS[pos].fd = fd;
LOCKS[pos].write = false;
}
return 0;
}
int
readlock_to_writelock (int fd)
{
int pos = locate_lock (fd);
if (0 > LOCKS[pos].fd)
return -1; // The lock has been canceled
LOCKS[pos].write = true;
return 0;
}
int
unlock_file (int fd)
{
int pos = locate_lock (fd);
if (0 > LOCKS[pos].fd)
return -1;
LOCKS[pos].fd = -1;
// TODO(unbrice): This line is so as to help debugging unlock_file()
// remove it in a few months.
errno = 0;
return fcntl (fd, F_SETLEASE, F_UNLCK);
}
bool
is_locked (int fd)
{
return LOCKS[locate_lock (fd)].fd >= 0;
}
/* could make an estimation of the required size, but should'nt because attr_setf
* set fixed size attributes, so it would cause problems when moving the disk
*/
#define DATE_SIZE sizeof(uint32_t) // TODO: change this value before 2107
int
set_ptime (int fd)
{
assert (fd > -1);
uint32_t date = htonl ((uint32_t) time (NULL));
return fsetxattr (fd, "shake.ptime", (void *) &date, DATE_SIZE, 0);
}
time_t
get_ptime (int fd)
{
assert (fd > -1);
uint32_t date;
int size = DATE_SIZE;
if (-1 ==
fgetxattr (fd, "shake.ptime", (void *) &date, size))
return (time_t) - 1;
date = ntohl (date);
if (date > time (NULL))
return (time_t) - 1;
return (time_t) date;
}
int
get_testimony (struct accused *a, struct law *l)
{
const size_t BUFFSTEP = 32;
/* General stats */
uint physbsize;
int crumbsize;
/* Framents logs */
llint *sizelog = NULL, *poslog = NULL; // Framgents sizes and positions
unsigned int logs_pos = 0; // Position in logs
/* Convert sizes in number of physical blocks */
{
if (-1 == ioctl (a->fd, FIGETBSZ, &physbsize))
{
error (0, errno, "%s: FIGETBSZ() failed", a->name);
return -1;
}
a->blocks = (a->size + physbsize - 1) / physbsize;
crumbsize = (int) ((double) a->size * l->crumbratio);
}
/* Create the log of fragment, terminated by <-1,-1> */
if (l->verbosity >= 3)
{
sizelog = malloc (BUFFSTEP * sizeof (*sizelog));
poslog = malloc (BUFFSTEP * sizeof (*poslog));
if (!sizelog || !poslog)
error (1, errno, "%s: malloc() failed", a->name);
sizelog[logs_pos] = -1;
poslog[logs_pos] = -1;
}
/* FIBMAP return physical block's position. We use it to detect start and end
* of fragments, by checking if the physical position of a block is not
* adjacent to the previous one.
* Please refer to comp.os.linux.development.system for information about
* FIBMAP (or ask me but I don't know anything which is not in this file).
*/
{
llint physpos = 0, prevphyspos = 0;
uint fragsize = 0;
for (int i = 0; i < a->blocks; i++)
{
if (INT_MAX == i)
break; // The file is too large for FIBMAP
/* Query the physical pos of the i-nth block */
prevphyspos = physpos;
physpos = i;
if (-1 == ioctl (a->fd, FIBMAP, &physpos))
{
error (0, errno, "%s: FIBMAP failed", a->name);
return -1;
}
physpos = physpos * physbsize;
/* workaround reiser4 bug fixed 2006-08-27, TODO : remove */
if (physpos < 0)
{
error (0, 0, "ReiserFS4 bug : UPDATE to at least 2006-08-27");
physpos = 0;
}
/* physpos == 0 if sparse file */
if (physpos)
{
if (!a->start)
a->start = physpos;
a->end = physpos;
/* Check if we have a new fragment, */
if (llabs (physpos - prevphyspos) > MAGICLEAP)
{
/* log it */
if (l->verbosity >= 3)
{
/* Periodically enlarge the log */
if (0 == (logs_pos + 2) % BUFFSTEP)
{
size_t nsize =
(logs_pos + 2 + BUFFSTEP) * sizeof (*sizelog);
sizelog = realloc (sizelog, nsize);
poslog = realloc (poslog, nsize);
if (!sizelog || !poslog)
error (1, errno, "%s: malloc() failed", a->name);
}
/* Record the pos of the new frag */
poslog[logs_pos] = physpos;
/* Record the size of the old frag */
if (logs_pos)
sizelog[logs_pos - 1] = fragsize;
logs_pos++;
}
if (fragsize && fragsize < crumbsize)
a->crumbc++;
a->fragc++;
fragsize = 0;
}
}
fragsize += physbsize;
}
/* Record the last size, and close the log */
if (l->verbosity >= 3 && fragsize)
{
if (logs_pos)
sizelog[logs_pos - 1] = fragsize;
poslog[logs_pos] = -1;
sizelog[logs_pos] = -1;
a->poslog = poslog;
a->sizelog = sizelog;
}
}
return 0;
}