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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1996-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <stdio.h>
#include <stddef.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/fs/cachefs_fs.h>
#include <sys/fs/cachefs_dlog.h>
/* forward references */
static int create_mapfile(char *fname, int size);
int
dlog_ck(char *dir_path, ino64_t *maxlocalfilenop)
{
int err;
int n;
char dlog_path[MAXPATHLEN];
char dmap_path[MAXPATHLEN];
struct stat64 statinfo;
int fd;
int dlog_version;
off_t offset;
struct cfs_dlog_entry buf;
int max_seq_num;
int ent_count = 0;
ino64_t fileno, maxlocalfileno;
if (maxlocalfilenop)
*maxlocalfilenop = 0LL;
n = strlen(dir_path) + strlen(CACHEFS_DLOG_FILE) + 2;
if (n > MAXPATHLEN) {
pr_err(gettext("%s/%s: path too long"),
dir_path, CACHEFS_DLOG_FILE);
return (-1);
}
sprintf(dlog_path, "%s/%s", dir_path, CACHEFS_DLOG_FILE);
n = strlen(dir_path) + strlen(CACHEFS_DMAP_FILE) + 2;
if (n > MAXPATHLEN) {
pr_err(gettext("%s/%s: path too long"),
dir_path, CACHEFS_DMAP_FILE);
return (-1);
}
sprintf(dmap_path, "%s/%s", dir_path, CACHEFS_DMAP_FILE);
err = lstat64(dlog_path, &statinfo);
if (err < 0) {
if (errno == ENOENT)
(void) unlink(dmap_path);
/*
* No disconnect log(dlog) file exists to check
*/
return (0);
}
/* this file will be <2GB */
fd = open(dlog_path, O_RDWR);
if (fd < 0) {
pr_err(gettext("can't open %s"), dlog_path);
return (-2);
}
err = read(fd, &dlog_version, sizeof (dlog_version));
if (err != sizeof (dlog_version)) {
pr_err(gettext("can't read %s"), dlog_path);
(void) close(fd);
return (-3);
}
if (dlog_version != CFS_DLOG_VERSION) {
pr_err(gettext(
"unknown version number in %s"), dlog_path);
(void) close(fd);
return (-4);
}
offset = sizeof (dlog_version);
max_seq_num = 0;
maxlocalfileno = 0LL;
while (offset < (off_t)statinfo.st_size) {
err = (int) lseek(fd, offset, SEEK_SET);
if (err == -1) {
pr_err(gettext("can't lseek %s"), dlog_path);
(void) close(fd);
return (-5);
}
err = read(fd, &buf, sizeof (buf));
if (err < 0) {
pr_err(gettext("can't read %s"), dlog_path);
(void) close(fd);
return (-6);
}
++ent_count;
if (buf.dl_op == CFS_DLOG_TRAILER) {
goto out;
}
if ((buf.dl_len & 3) == 0) {
/*
* Record length must be on a word boundary and
* fit into the correct size range.
*/
if ((buf.dl_len < sizeof (int)) ||
(buf.dl_len > CFS_DLOG_ENTRY_MAXSIZE)) {
goto out;
}
/*
* Make sure length does not point beyond end of
* file
*/
if ((offset + (off_t)buf.dl_len) >
(off_t)statinfo.st_size) {
goto out;
}
} else {
goto out;
}
/* make sure the valid field is reasonable */
switch (buf.dl_valid) {
case CFS_DLOG_VAL_CRASH:
case CFS_DLOG_VAL_COMMITTED:
case CFS_DLOG_VAL_ERROR:
case CFS_DLOG_VAL_PROCESSED:
break;
default:
goto out;
}
/* make sure the operation field is reasonable */
fileno = 0LL;
switch (buf.dl_op) {
case CFS_DLOG_CREATE:
fileno = buf.dl_u.dl_create.dl_new_cid.cid_fileno;
break;
case CFS_DLOG_REMOVE:
break;
case CFS_DLOG_LINK:
break;
case CFS_DLOG_RENAME:
break;
case CFS_DLOG_MKDIR:
fileno = buf.dl_u.dl_mkdir.dl_child_cid.cid_fileno;
break;
case CFS_DLOG_RMDIR:
break;
case CFS_DLOG_SYMLINK:
fileno = buf.dl_u.dl_symlink.dl_child_cid.cid_fileno;
break;
case CFS_DLOG_SETATTR:
break;
case CFS_DLOG_SETSECATTR:
break;
case CFS_DLOG_MODIFIED:
break;
case CFS_DLOG_MAPFID:
break;
default:
goto out;
}
/* track the largest local fileno used */
if (maxlocalfileno < fileno)
maxlocalfileno = fileno;
/* track the largest sequence number used */
if (max_seq_num < buf.dl_seq) {
max_seq_num = buf.dl_seq;
}
offset += buf.dl_len;
}
out:
if ((buf.dl_op != CFS_DLOG_TRAILER) ||
(buf.dl_len != sizeof (struct cfs_dlog_trailer)) ||
(buf.dl_valid != CFS_DLOG_VAL_COMMITTED) ||
((offset + (off_t)buf.dl_len) != (off_t)statinfo.st_size)) {
ftruncate(fd, offset);
buf.dl_len = sizeof (struct cfs_dlog_trailer);
buf.dl_op = CFS_DLOG_TRAILER;
buf.dl_valid = CFS_DLOG_VAL_COMMITTED;
buf.dl_seq = max_seq_num + 1;
if (wrdlog(fd, &buf, buf.dl_len, offset) != 0) {
(void) close(fd);
return (-7);
}
}
if (fsync(fd) == -1) {
pr_err(gettext("Cannot sync %s"), dlog_path);
(void) close(fd);
return (-8);
}
(void) close(fd); /* ignore return since fsync() successful */
/* check to see that mapfile exists; if not, create it. */
if (access(dmap_path, F_OK) != 0) {
/* XXX ent_count is a very high upper bound */
if (create_mapfile(dmap_path,
ent_count * sizeof (struct cfs_dlog_mapping_space)) != 0) {
return (-9);
}
}
if (maxlocalfilenop)
*maxlocalfilenop = maxlocalfileno;
return (0);
}
int
wrdlog(int fd, char * buf, int len, off_t offset)
{
int err;
err = lseek(fd, offset, SEEK_SET);
if (err < 0) {
return (-1);
}
err = write(fd, buf, len);
if (err != len) {
return (-2);
}
return (0);
}
static int
create_mapfile(char *fname, int size)
{
char buffy[BUFSIZ];
int fd, rc, wsize;
/* this file will be <2GB */
fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd < 0)
return (errno);
memset(buffy, '\0', sizeof (buffy));
while (size > 0) {
wsize = (size > sizeof (buffy)) ? sizeof (buffy) : size;
if (write(fd, buffy, wsize) != wsize) {
rc = errno;
(void) close(fd);
(void) unlink(fname);
return (rc);
}
size -= wsize;
}
if (fsync(fd) != 0) {
rc = errno;
(void) close(fd);
(void) unlink(fname);
return (rc);
}
(void) close(fd);
return (0);
}
|