NFS File Locking

Rick Stevens rstevens at internap.com
Thu Nov 29 18:57:50 UTC 2007


On Wed, 2007-11-28 at 20:05 -0700, Karl Pearson wrote:
> On Wed, November 28, 2007 7:26 pm, Rick Stevens wrote:
> > On Wed, 2007-11-28 at 18:19 -0700, Karl Pearson wrote:
> >> On Wed, November 28, 2007 12:08 pm, Rick Stevens wrote:
> >> > On Wed, 2007-11-28 at 09:27 -0800, Waldher, Travis R wrote:
> >> >> Is there a command I can use to manually lock a file?
> >> >>
> >> >>
> >> >>
> >> >> My goal is to test NFS file locking with groups greater than 16.
> >> >
> >> > I don't know of one, but it wouldn't be hard to write.  You'd need to
> >> > open the file, lock it via fcntl() (flock() does not work over NFS) and
> >> > hold the lock until not needed.
> >> >
> >> > Remember that if the program exits, the lock would be released.
> >>
> >> Can't you just emacs or vi the file and accomplish the same thing? Just a
> >> thought to save something or other.
> >
> > I'm not 100% sure of that.  Typically an editor doesn't take a write
> > lock on a file.  Rather, the editor makes a work copy of the file which
> > you edit (which is why editing a large file in vi takes so long to
> > start...it's making a copy of the file).  Some editors actually just
> > keep a record of what you did.  If you "quit and discard changes", the
> > work file or the log of what you did are simply discarded and the
> > original file is left alone.
> >
> > When you "exit and save", the old file gets overwritten by the work file
> > or the edits get played back to modify the file.  So, the only time a
> > write lock is made on the file is when you commit the edits.
> 
> I believe you. So, what of something like XV or GIMP then? They make an
> exclusive lock against the file, don't they?

I don't know.  I've never straced those apps.  You could do that and see
what they do.  Watch for "fcntl(fd, F_SETLK, pointer-to-struct-flock)"
calls.

If you need an app written to do it, I can probably crank some
comamnd-line thing out in an hour or two, e.g.

	$setalock [-w] /path/to/file

If you specify "-w", it'd set a write lock on the file.  Default would
be a read lock.  It'd hold it until you CTRL-C the program.

----------------------------------------------------------------------
- Rick Stevens, Principal Engineer             rstevens at internap.com -
- CDN Systems, Internap, Inc.                http://www.internap.com -
-                                                                    -
-          Consciousness: that annoying time between naps.           -
----------------------------------------------------------------------




More information about the Redhat-install-list mailing list