blob: 0600b1057c038bb2277e35b4133311ed75a73fae [file] [log] [blame]
whrb973f2b2000-05-05 19:34:50 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
whrb973f2b2000-05-05 19:34:50 +00004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
vapier45a8ba02009-07-20 10:59:32 +00007 *
whrb973f2b2000-05-05 19:34:50 +00008 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
vapier45a8ba02009-07-20 10:59:32 +000011 *
whrb973f2b2000-05-05 19:34:50 +000012 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
vapier45a8ba02009-07-20 10:59:32 +000018 *
whrb973f2b2000-05-05 19:34:50 +000019 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
whrb973f2b2000-05-05 19:34:50 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
whrb973f2b2000-05-05 19:34:50 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32/*
33 * This module contains code for logging writes to files, and for
34 * perusing the resultant logfile. The main intent of all this is
35 * to provide a 'write history' of a file which can be examined to
36 * judge the state of a file (ie. whether it is corrupted or not) based
37 * on the write activity.
38 *
39 * The main abstractions available to the user are the wlog_file, and
40 * the wlog_rec. A wlog_file is a handle encapsulating a write logfile.
41 * It is initialized with the wlog_open() function. This handle is
42 * then passed to the various wlog_xxx() functions to provide transparent
43 * access to the write logfile.
44 *
45 * The wlog_rec datatype is a structure which contains all the information
46 * about a file write. Examples include the file name, offset, length,
47 * pattern, etc. In addition there is a bit which is cleared/set based
vapier45a8ba02009-07-20 10:59:32 +000048 * on whether or not the write has been confirmed as complete. This
whrb973f2b2000-05-05 19:34:50 +000049 * allows the write logfile to contain information on writes which have
50 * been initiated, but not yet completed (as in async io).
51 *
52 * There is also a function to scan a write logfile in reverse order.
53 *
54 * NOTE: For target file analysis based on a write logfile, the
55 * assumption is made that the file being written to is
56 * locked from simultaneous access, so that the order of
57 * write completion is predictable. This is an issue when
58 * more than 1 process is trying to write data to the same
59 * target file simultaneously.
60 *
61 * The history file created is a collection of variable length records
62 * described by scruct wlog_rec_disk in write_log.h. See that module for
63 * the layout of the data on disk.
64 */
65
66#include <stdio.h>
67#include <unistd.h>
68#include <fcntl.h>
69#include <errno.h>
70#include <string.h>
71#include <sys/param.h>
72#include <sys/stat.h>
73#include <sys/types.h>
74#include "write_log.h"
75
76#ifndef BSIZE
mridgee6508f82005-01-04 21:00:17 +000077#ifdef DEV_BSIZE
whrb973f2b2000-05-05 19:34:50 +000078#define BSIZE DEV_BSIZE
79#else
80#define BSIZE BBSIZE
81#endif
82#endif
83
84#ifndef PATH_MAX
85#define PATH_MAX 255
86/*#define PATH_MAX pathconf("/", _PC_PATH_MAX)*/
87#endif
88
whrb973f2b2000-05-05 19:34:50 +000089char Wlog_Error_String[256];
90
91#if __STDC__
92static int wlog_rec_pack(struct wlog_rec *wrec, char *buf, int flag);
93static int wlog_rec_unpack(struct wlog_rec *wrec, char *buf);
94#else
95static int wlog_rec_pack();
96static int wlog_rec_unpack();
97#endif
98
99/*
100 * Initialize a write logfile. wfile is a wlog_file structure that has
101 * the w_file field filled in. The rest of the information in the
102 * structure is initialized by the routine.
103 *
104 * The trunc flag is used to indicate whether or not the logfile should
105 * be truncated if it currently exists. If it is non-zero, the file will
106 * be truncated, otherwise it will be appended to.
107 *
108 * The mode argument is the [absolute] mode which the file will be
109 * given if it does not exist. This mode is not affected by your process
110 * umask.
111 */
112
113int
114wlog_open(wfile, trunc, mode)
115struct wlog_file *wfile;
116int trunc;
117int mode;
118{
119 int omask, oflags;
120
121 if (trunc)
122 trunc = O_TRUNC;
123
124 omask = umask(0);
125
126 /*
127 * Open 1 file descriptor as O_APPEND
128 */
129
130 oflags = O_WRONLY | O_APPEND | O_CREAT | trunc;
131 wfile->w_afd =
132 open(wfile->w_file, oflags, mode);
133 umask(omask);
134
135 if (wfile->w_afd == -1) {
136 sprintf(Wlog_Error_String,
137 "Could not open write_log - open(%s, %#o, %#o) failed: %s\n",
nstraz94181082000-08-30 18:43:38 +0000138 wfile->w_file, oflags, mode, strerror(errno));
whrb973f2b2000-05-05 19:34:50 +0000139 return -1;
140 }
141
142 /*
143 * Open the next fd as a random access descriptor
144 */
145
146 oflags = O_RDWR;
subrata_modake753fd72008-07-11 10:56:34 +0000147 if ((wfile->w_rfd = open(wfile->w_file, oflags)) == -1) {
whrb973f2b2000-05-05 19:34:50 +0000148 sprintf(Wlog_Error_String,
149 "Could not open write log - open(%s, %#o) failed: %s\n",
nstraz94181082000-08-30 18:43:38 +0000150 wfile->w_file, oflags, strerror(errno));
whrb973f2b2000-05-05 19:34:50 +0000151 close(wfile->w_afd);
152 wfile->w_afd = -1;
153 return -1;
154 }
vapier45a8ba02009-07-20 10:59:32 +0000155
whrb973f2b2000-05-05 19:34:50 +0000156 return 0;
157}
158
159/*
160 * Release all resources associated with a wlog_file structure allocated
161 * with the wlog_open() call.
162 */
163
164int
165wlog_close(wfile)
166struct wlog_file *wfile;
167{
168 close(wfile->w_afd);
169 close(wfile->w_rfd);
170 return 0;
171}
172
173/*
174 * Write a wlog_rec structure to a write logfile. Offset is used to
175 * control where the record will be written. If offset is < 0, the
176 * record will be appended to the end of the logfile. Otherwise, the
177 * record which exists at the indicated offset will be overlayed. This
178 * is so that we can record writes which are outstanding (with the w_done
179 * bit in wrec cleared), but not completed, and then later update the
180 * logfile when the write request completes (as with async io). When
vapier45a8ba02009-07-20 10:59:32 +0000181 * offset is >= 0, only the fixed length portion of the record is
whrb973f2b2000-05-05 19:34:50 +0000182 * rewritten. See text in write_log.h for details on the format of an
183 * on-disk record.
vapier45a8ba02009-07-20 10:59:32 +0000184 *
whrb973f2b2000-05-05 19:34:50 +0000185 * The return value of the function is the byte offset in the logfile
186 * where the record begins.
187 *
188 * Note: It is the callers responsibility to make sure that the offset
189 * parameter 'points' to a valid record location when a record is to be
190 * overlayed. This is guarenteed by saving the return value of a previous
191 * call to wlog_record_write() which wrote the record to be overlayed.
192 *
193 * Note2: The on-disk version of the wlog_rec is MUCH different than
194 * the user version. Don't expect to od the logfile and see data formatted
195 * as it is in the wlog_rec structure. Considerable data packing takes
196 * place before the record is written.
197 */
198
199int
200wlog_record_write(wfile, wrec, offset)
201struct wlog_file *wfile;
202struct wlog_rec *wrec;
203long offset;
204{
205 int reclen;
206 char wbuf[WLOG_REC_MAX_SIZE + 2];
207
208 /*
209 * If offset is -1, we append the record at the end of file
210 *
211 * Otherwise, we overlay wrec at the file offset indicated and assume
212 * that the caller passed us the correct offset. We do not record the
213 * fname in this case.
214 */
215
216 reclen = wlog_rec_pack(wrec, wbuf, (offset < 0));
217
218 if (offset < 0) {
219 /*
220 * Since we're writing a complete new record, we must also tack
221 * its length onto the end so that wlog_scan_backward() will work.
222 * Length is asumed to fit into 2 bytes.
223 */
vapier45a8ba02009-07-20 10:59:32 +0000224
whrb973f2b2000-05-05 19:34:50 +0000225 wbuf[reclen] = reclen / 256;
226 wbuf[reclen+1] = reclen % 256;
227 reclen += 2;
228
vapier45a8ba02009-07-20 10:59:32 +0000229 if ( write(wfile->w_afd, wbuf, reclen) == -1 ) {
subrata_modakf3fefe92007-03-28 04:02:46 +0000230 sprintf(Wlog_Error_String,
231 "Could not write log - write(%s, %s, %d) failed: %s\n",
232 wfile->w_file, wbuf, reclen, strerror(errno));
233 return -1;
234 } else {
235 offset = lseek(wfile->w_afd, 0, SEEK_CUR) - reclen;
236 if ( offset == -1 ) {
237 sprintf(Wlog_Error_String,
238 "Could not reposition file pointer - lseek(%s, 0, SEEK_CUR) failed: %s\n",
239 wfile->w_file, strerror(errno));
240 return -1;
241 }
242 }
whrb973f2b2000-05-05 19:34:50 +0000243 } else {
subrata_modakf3fefe92007-03-28 04:02:46 +0000244 if ( (lseek(wfile->w_rfd, offset, SEEK_SET)) == -1 ) {
245 sprintf(Wlog_Error_String,
subrata_modak48f20312007-04-19 06:24:06 +0000246 "Could not reposition file pointer - lseek(%s, %ld, SEEK_SET) failed: %s\n",
vapier45a8ba02009-07-20 10:59:32 +0000247 wfile->w_file, offset, strerror(errno));
subrata_modakf3fefe92007-03-28 04:02:46 +0000248 return -1;
249 } else {
250 if ( (write(wfile->w_rfd, wbuf, reclen)) == -1 ) {
251 sprintf(Wlog_Error_String,
252 "Could not write log - write(%s, %s, %d) failed: %s\n",
253 wfile->w_file, wbuf, reclen, strerror(errno));
254 return -1;
255 }
256 }
whrb973f2b2000-05-05 19:34:50 +0000257 }
vapier45a8ba02009-07-20 10:59:32 +0000258
whrb973f2b2000-05-05 19:34:50 +0000259 return offset;
260}
261
262/*
263 * Function to scan a logfile in reverse order. Wfile is a valid
264 * wlog_file structure initialized by wlog_open(). nrecs is the number
265 * of records to scan (all records are scanned if nrecs is 0). func is
266 * a user-supplied function to call for each record found. The function
267 * will be passed a single parameter - a wlog_rec structure .
268 */
269
270int
271wlog_scan_backward(wfile, nrecs, func, data)
272struct wlog_file *wfile;
273int nrecs;
274int (*func)();
275long data;
276{
subrata_modakf3fefe92007-03-28 04:02:46 +0000277 int fd, leftover, nbytes, offset, recnum, reclen, rval;
278 char buf[BSIZE*32], *bufend, *cp, *bufstart;
nstraz94181082000-08-30 18:43:38 +0000279 char albuf[WLOG_REC_MAX_SIZE];
whrb973f2b2000-05-05 19:34:50 +0000280 struct wlog_rec wrec;
281
282 fd = wfile->w_rfd;
283
284 /*
285 * Move to EOF. offset will always hold the current file offset
286 */
287
subrata_modakf3fefe92007-03-28 04:02:46 +0000288 if ( (lseek(fd, 0, SEEK_END)) == -1 ) {
289 sprintf(Wlog_Error_String,
290 "Could not reposition file pointer - lseek(%s, 0, SEEK_END) failed: %s\n",
291 wfile->w_file, strerror(errno));
292 return -1;
293 }
whrb973f2b2000-05-05 19:34:50 +0000294 offset = lseek(fd, 0, SEEK_CUR);
subrata_modakf3fefe92007-03-28 04:02:46 +0000295 if ( (offset == -1) ) {
296 sprintf(Wlog_Error_String,
297 "Could not reposition file pointer - lseek(%s, 0, SEEK_CUR) failed: %s\n",
298 wfile->w_file, strerror(errno));
299 return -1;
300 }
whrb973f2b2000-05-05 19:34:50 +0000301
302 bufend = buf + sizeof(buf);
303 bufstart = buf;
304
305 recnum = 0;
306 leftover = 0;
307 while ((!nrecs || recnum < nrecs) && offset > 0) {
308 /*
309 * Check for beginning of file - if there aren't enough bytes
310 * remaining to fill buf, adjust bufstart.
311 */
312
robbiew0ccc8272003-06-18 17:44:03 +0000313 if ((unsigned int)offset + leftover < sizeof(buf)) {
whrb973f2b2000-05-05 19:34:50 +0000314 bufstart = bufend - (offset + leftover);
315 offset = 0;
316 } else {
317 offset -= sizeof(buf) - leftover;
318 }
319
vapier45a8ba02009-07-20 10:59:32 +0000320 /*
whrb973f2b2000-05-05 19:34:50 +0000321 * Move to the proper file offset, and read into buf
322 */
subrata_modakf3fefe92007-03-28 04:02:46 +0000323 if ( (lseek(fd, offset, SEEK_SET)) ==-1 ) {
324 sprintf(Wlog_Error_String,
325 "Could not reposition file pointer - lseek(%s, %d, SEEK_SET) failed: %s\n",
326 wfile->w_file, offset, strerror(errno));
327 return -1;
328 }
whrb973f2b2000-05-05 19:34:50 +0000329
whrb973f2b2000-05-05 19:34:50 +0000330 nbytes = read(fd, bufstart, bufend - bufstart - leftover);
331
332 if (nbytes == -1) {
333 sprintf(Wlog_Error_String,
robbiewf3a83d52002-05-28 16:26:16 +0000334 "Could not read history file at offset %d - read(%d, %p, %d) failed: %s\n",
robbiew15226cd2002-04-10 16:10:40 +0000335 offset, fd, bufstart,
336 (int)(bufend - bufstart - leftover), strerror(errno));
whrb973f2b2000-05-05 19:34:50 +0000337 return -1;
338 }
339
340 cp = bufend;
341 leftover = 0;
342
343 while (cp >= bufstart) {
344
345 /*
346 * If cp-bufstart is not large enough to hold a piece
347 * of record length information, copy remainder to end
348 * of buf and continue reading the file.
349 */
350
351 if (cp - bufstart < 2) {
352 leftover = cp - bufstart;
353 memcpy(bufend - leftover, bufstart, leftover);
354 break;
355 }
356
357 /*
358 * Extract the record length. We must do it this way
359 * instead of casting cp to an int because cp might
360 * not be word aligned.
361 */
362
363 reclen = (*(cp-2) * 256) + *(cp -1);
364
365 /*
366 * If cp-bufstart isn't large enough to hold a
367 * complete record, plus the length information, copy
368 * the leftover bytes to the end of buf and continue
369 * reading.
370 */
371
372 if (cp - bufstart < reclen + 2) {
373 leftover = cp - bufstart;
374 memcpy(bufend - leftover, bufstart, leftover);
375 break;
376 }
377
378 /*
379 * Adjust cp to point at the start of the record.
380 * Copy the record into wbuf so that it is word
381 * aligned and pass the record to the user supplied
382 * function.
383 */
384
385 cp -= reclen + 2;
386 memcpy(albuf, cp, reclen);
387
388 wlog_rec_unpack(&wrec, albuf);
389
390 /*
391 * Call the user supplied function -
392 * stop if instructed to.
393 */
394
395 if ((rval = (*func)(&wrec, data)) == WLOG_STOP_SCAN) {
396 break;
397 }
398
399 recnum++;
400
401 if (nrecs && recnum >= nrecs)
402 break;
403 }
404 }
405
406 return 0;
407}
408
409/*
410 * The following 2 routines are used to pack and unpack the user
411 * visible wlog_rec structure to/from a character buffer which is
412 * stored or read from the write logfile. Any changes to either of
413 * these routines must be reflected in the other.
414 */
415
416static int
417wlog_rec_pack(wrec, buf, flag)
418struct wlog_rec *wrec;
419char *buf;
420int flag;
421{
422 char *file, *host, *pattern;
423 struct wlog_rec_disk *wrecd;
424
425 wrecd = (struct wlog_rec_disk *)buf;
426
427 wrecd->w_pid = (uint)wrec->w_pid;
428 wrecd->w_offset = (uint)wrec->w_offset;
429 wrecd->w_nbytes = (uint)wrec->w_nbytes;
430 wrecd->w_oflags = (uint)wrec->w_oflags;
431 wrecd->w_done = (uint)wrec->w_done;
432 wrecd->w_async = (uint)wrec->w_async;
433
434 wrecd->w_pathlen = (wrec->w_pathlen > 0) ? (uint)wrec->w_pathlen : 0;
435 wrecd->w_hostlen = (wrec->w_hostlen > 0) ? (uint)wrec->w_hostlen : 0;
436 wrecd->w_patternlen = (wrec->w_patternlen > 0) ? (uint)wrec->w_patternlen : 0;
437
438 /*
439 * If flag is true, we should also pack the variable length parts
440 * of the wlog_rec. By default, we only pack the fixed length
441 * parts.
442 */
443
444 if (flag) {
445 file = buf + sizeof(struct wlog_rec_disk);
446 host = file + wrecd->w_pathlen;
447 pattern = host + wrecd->w_hostlen;
vapier45a8ba02009-07-20 10:59:32 +0000448
whrb973f2b2000-05-05 19:34:50 +0000449 if (wrecd->w_pathlen > 0)
450 memcpy(file, wrec->w_path, wrecd->w_pathlen);
vapier45a8ba02009-07-20 10:59:32 +0000451
whrb973f2b2000-05-05 19:34:50 +0000452 if (wrecd->w_hostlen > 0)
453 memcpy(host, wrec->w_host, wrecd->w_hostlen);
vapier45a8ba02009-07-20 10:59:32 +0000454
whrb973f2b2000-05-05 19:34:50 +0000455 if (wrecd->w_patternlen > 0)
456 memcpy(pattern, wrec->w_pattern, wrecd->w_patternlen);
457
458 return (sizeof(struct wlog_rec_disk) +
459 wrecd->w_pathlen + wrecd->w_hostlen + wrecd->w_patternlen);
460 } else {
461 return sizeof(struct wlog_rec_disk);
462 }
463}
464
465static int
466wlog_rec_unpack(wrec, buf)
467struct wlog_rec *wrec;
468char *buf;
469{
470 char *file, *host, *pattern;
471 struct wlog_rec_disk *wrecd;
472
vapier32cc0ac2006-06-22 04:28:32 +0000473 memset((char *)wrec, 0x00, sizeof(struct wlog_rec));
whrb973f2b2000-05-05 19:34:50 +0000474 wrecd = (struct wlog_rec_disk *)buf;
475
476 wrec->w_pid = wrecd->w_pid;
477 wrec->w_offset = wrecd->w_offset;
478 wrec->w_nbytes = wrecd->w_nbytes;
479 wrec->w_oflags = wrecd->w_oflags;
480 wrec->w_hostlen = wrecd->w_hostlen;
481 wrec->w_pathlen = wrecd->w_pathlen;
482 wrec->w_patternlen = wrecd->w_patternlen;
483 wrec->w_done = wrecd->w_done;
484 wrec->w_async = wrecd->w_async;
485
486 if (wrec->w_pathlen > 0) {
487 file = buf + sizeof(struct wlog_rec_disk);
488 memcpy(wrec->w_path, file, wrec->w_pathlen);
489 }
490
491 if (wrec->w_hostlen > 0) {
492 host = buf + sizeof(struct wlog_rec_disk) + wrec->w_pathlen;
493 memcpy(wrec->w_host, host, wrec->w_hostlen);
494 }
495
496 if (wrec->w_patternlen > 0) {
497 pattern = buf + sizeof(struct wlog_rec_disk) +
498 wrec->w_pathlen + wrec->w_hostlen;
499 memcpy(wrec->w_pattern, pattern, wrec->w_patternlen);
500 }
501
502 return 0;
503}