Wei-keng Liao
2008-May-13 16:37 UTC
[Lustre-discuss] supporting fcntl, byte-range file locking
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 -------------- next part -------------- #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; }
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
Oleg Drokin
2008-May-13 23:01 UTC
[Lustre-discuss] supporting fcntl, byte-range file locking
Hello! On May 13, 2008, at 12:37 PM, 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.Certainly many versions, going far back to 1.2 and prior. This functionality is disabled by default though. To enable cluster-consistent fcntl locking you need to mount your lustre fs on a client with -o flock option. If you do not need cluster-consistency, you can mount with -o localflock, it would only be consistent on local node of course, but much faster (only useful for some stupid applications that just test the presence without actually using the functionality, of course, or when no cross-node flock interactions are expected otherwise). This "disabled by default" case got into effect in 1.4.8, prior to that if you do not use -o flock option, then only locally-consistent locking is provided. Bye, Oleg