Kernel based rename undo
Posted Apr 10, 2009 7:35 UTC (Fri) by
bojan (subscriber, #14302)
In reply to:
Kernel based rename undo by butlerm
Parent article:
Linux Storage and Filesystem workshop, day 1
I know. What I'm talking about is synchronisation between processes in terms of contents of data (i.e. one process may write a change, which gets lost when another process does the same - your stock race). So, you cannot just open(), write(), close(), rename() with multiple processes. You have to lock, otherwise your processes will stomp all over each other's data.
An example of doing the same with multiple processes when kernel doesn't guarantee data before metadata on rename is below. Bugs included, of course ;-).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <aio.h>
#define BUF_SIZE 50
static int *count=NULL;
/* XXX this is just a demo, no error checking */
static void whack(int signum,siginfo_t *info,void *context){
int sd=*(int*)info->si_value.sival_ptr;
/* critical section */
lockf(sd,F_LOCK,0);
if(!--(*count))
unlink("foo~");
/* end critical section */
lockf(sd,F_ULOCK,0);
}
int main(int argc,char **argv){
int sd,fd;
ssize_t len;
char buf[BUF_SIZE];
struct aiocb cb;
const struct aiocb *cbl[]={&cb};
struct sigaction act;
/* AIO control block setup */
memset(&cb,0,sizeof(cb));
cb.aio_sigevent.sigev_notify=SIGEV_SIGNAL;
cb.aio_sigevent.sigev_signo=SIGRTMIN;
cb.aio_sigevent.sigev_value.sival_ptr=&sd;
/* signal handler setup */
memset(&act,0,sizeof(act));
act.sa_flags=SA_SIGINFO;
act.sa_sigaction=whack;
sigaction(SIGRTMIN,&act,NULL);
/* setup shared counter, restore */
if((sd=shm_open("foo",O_RDWR|O_CREAT|O_EXCL,S_IRUSR|S_IWUSR))==-1){
int tries=20;
struct stat s;
/* not the first to arrive, open and wait for counter to be written */
sd=shm_open("foo",O_RDWR,S_IRUSR|S_IWUSR);
fstat(sd,&s);
while(tries-- && s.st_size<sizeof(*count)){
sleep(1);
fstat(sd,&s);
}
/* something's really screwed */
if(!tries)
return 1;
} else{ /* first to arrive, restore */
int count=0; /* filler */
/* don't care if we fail */
if(!rename("foo~","foo"))
fprintf(stderr,"Restored.\n");
write(sd,&count,sizeof(count));
}
/* shared counter */
count=mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED,sd,0);
/* critical section */
lockf(sd,F_LOCK,0);
/* don't care if it fails - already there */
link("foo","foo~");
/* read existing file */
fd=open("foo",O_RDONLY);
len=read(fd,buf,BUF_SIZE);
close(fd);
/* write to new file and initiate sync */
fd=open("foo.new",O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
write(fd,buf,len);
cb.aio_fildes=fd;
(*count)++;
aio_fsync(O_SYNC,&cb);
close(fd);
/* put the new file in place */
rename("foo.new","foo");
/* end critical section */
lockf(sd,F_ULOCK,0);
/* do something really useful here */
/* wait for AIO completion */
aio_suspend(cbl,1,NULL);
/* clean up shared memory */
munmap(count,sizeof(int));
close(sd);
return 0;
}
(
Log in to post comments)