[Lustre-discuss] supporting fcntl, byte-range file locking

Tom.Wang Tom.Wang at Sun.COM
Tue May 13 11:59:40 PDT 2008


Wei-keng Liao wrote:
> What versions of Lustre support fcntl for byte-range file locking? 
> Attached is a test program extracted from ROMIO to test fcntl() for 
> locking. It ran fine on some Lustre versions.
>
> Wei-keng
>   
>   
 Lustre has supported flock since 1.4.3.

 But you need mount with -o flock or localflock(simulate local flock on 
client, since 1.4.10) to enable the flock on client.
 


Thanks
WangDi
> ------------------------------------------------------------------------
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #include <unistd.h>
> #include <fcntl.h>
> #include <errno.h>
>
> #include <mpi.h>
>
> static int rank;
>
> void ADIOI_Set_lock(int fd, int cmd, int type, long long offset, int whence, long long len)
> {
>     int err;
>     struct flock lock;
>     char *cmd_str, *type_str;
>
>     if (len == 0) return;
>
>     lock.l_type   = type;
>     lock.l_whence = whence;
>     lock.l_start  = offset;
>     lock.l_len    = len;
>
>     cmd_str = (cmd == F_GETLK )? "F_GETLK" :
>              ((cmd == F_SETLK )? "F_SETLK" :
>              ((cmd == F_SETLKW)? "F_SETLKW" : "UNEXPECTED"));
>     type_str = (type == F_RDLCK)? "F_RDLCK" :
>               ((type == F_WRLCK)? "F_WRLCK" :
>               ((type == F_UNLCK)? "F_UNLOCK" : "UNEXPECTED"));
>     errno = 0;
>     do {
>         err = fcntl(fd, cmd, &lock);
>     } while (err && (errno == EINTR));
>
>     if (err && (errno != EBADF))
>         printf("%d: File locking failed in (fd %X,cmd %s/%X,type %s/%X,whence %X) with return value %X and errno %X.\n",
>                rank, fd, cmd_str, cmd, type_str, type, whence, err, errno);
>     else
>         printf("%d: File locking succeed in (fd %X,cmd %s/%X,type %s/%X,whence %X)\n",
>                rank, fd, cmd_str, cmd, type_str, type, whence);
>
>     return;
> }
>
> int main(int argc, char **argv)
> {
>     int fd, buf[100];
>
>     MPI_Init(&argc, &argv);
>     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
>
>     if (argc != 2) {
>         printf("Usage: %s <filename>\n", argv[0]);
>         return 1;
>     }
>
>     if ((fd = open(argv[1], O_CREAT|O_RDWR, 0600)) == -1) {
>         printf("Error: opening file %s\n", argv[1]);
>         return 1;
>     }
>
>     MPI_Barrier(MPI_COMM_WORLD);
>     ADIOI_Set_lock(fd, F_SETLKW, F_WRLCK, 0, SEEK_SET, 10);
>     write(fd, buf, 100);
>     ADIOI_Set_lock(fd, F_SETLK,  F_UNLCK, 0, SEEK_SET, 10);
>
>     close(fd);
>
>     MPI_Finalize();
>     return 0;
> }
>
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> Lustre-discuss mailing list
> Lustre-discuss at lists.lustre.org
> http://lists.lustre.org/mailman/listinfo/lustre-discuss
>   


-- 
Regards,
Tom Wangdi    
--
Sun Lustre Group
System Software Engineer 
http://www.sun.com




More information about the lustre-discuss mailing list