Subversion, Mac OS X, and SMB

Posted by Brian on October 5, 2006

I suspect that a number of Macintosh users face the same situation I do – we need to use Subversion on a primarily Windows network and want to do so with FSFS backed repositories stored on the already-provided Windows file servers.

For me, the reason for pursuing this approach over a real Subversion server is that the IT folks where I work are reluctant to implement a Subversion server. Since there are only a few repositories in use by a handful of users, it is understandable that they prefer we utilize the existing file servers instead. When and if the usage of Subversion outgrows the file server approach, they will undoubtedly go ahead with a real Subversion server.

There are several technical problems that need to be overcome in order to use repositories with FSFS data stores on an SMB filesystem.

If you would rather not read a bunch of technical details, feel free to jump to the blog entry that shows how to install Subversion with a patch to support SMB filesystems.

Mac OS X SMB file locking issue

One of the features listed for the FSFS data store is that it is “usable over network filesystems”. Unfortunately, attempts to commit to a repository with this data store does not work when it is on an SMB filesystem mounted by Mac OS X.

svn: Commit failed (details follow):
svn: Can't get exclusive lock on file '/Volumes/127.0.0.1/svn/test/db/write-lock': Operation not supported

This error occurs because FSFS implements file locking using the apr_file_lock function, which for Mac OS X (and most other forms of Unix) uses fcntl for file locking. This file locking mechanism does not support SMB filesystems on Mac OS X.

An alternative is to use flock instead of fcntl. Locks made with this mechanism on an SMB filesystem work as expected with one caveat – shared locks act like exclusive locks, which may cause resources to be temporarily unavailable when a user is working with the repository. This should rarely happen with a small user base and in practice I haven’t seen it happen yet.

However, it is not as simple as patching apr_file_lock to use flock instead of fcntl. Subversion seems to do nested shared file locks. In other words, one function will request a shared lock on a file and then calls another function that also requests a shared lock on the same file. This second request fails because the first lock actually acts like an exclusive lock.

To solve this problem, I developed a patch that uses temporary files to simulate shared file locks when locking files on an SMB filesystem.

Mac OS X 10.4 SMB write bug

There is a problem that prevents successful commits to an FSFS backed repository on an SMB filesystem. I don’t remember seeing it in Mac OS X 10.4.0, but it certainly is present in the most recent releases, including Mac OS X 10.4.8.

svn: Commit failed (details follow):
svn: Can't write to file '/Volumes/127.0.0.1/svn/test/db/transactions/2-1.txn/rev': Input/output error

An error also appears in the system log.

Jul 20 23:18:14 wells kernel[0]: smb_flushrange: ubc_sync_range failure

This appears to happen when Subversion calls apr_file_write_full with a zero byte buffer. This function uses the write system call, which should return with no error if there is nothing to actually write. However, in Mac OS X 10.4 write returns an I/O error if the file is on a mounted SMB filesystem.

I filed a bug report (#4647254) with Apple, which was subsequently closed and marked as a duplicate. So it seems that their engineers are aware of the problem.

The patch mentioned earlier was further enhanced to make sure Subversion does not attempt zero byte write operations on files that are on SMB filesystems.

Patching APR to support SMB hosted repositories

Subversion uses the Apache Portable Runtime (APR) library to handle most system calls instead of making extensive use of OS-specific code. This is thus the ideal place to apply the patch needed to provide support for SMB filesystems on Mac OS X.

The patch to apply fixes for all the technical problems listed above can be found on my Files page. It was developed against the 0.9.x branch of the APR library and has been tested with APR 0.9.12 as distributed alongside Subversion 1.4.0.

Instructions on how to install Subversion with this patch are in this blog entry.

Future Subversion support for SMB filesystems

I have submitted the patch for APR by means of a bug report (#40700). Hopefully the developers of APR will be interested in using it to improve support for SMB filesystems on Mac OS X, thus benefiting the way we are using Subversion.

While using Subversion repositories hosted on SMB filesystems, I have seen a few minor issues, such as the Windows file server maintaining a file lock for too long. These don’t happen very often in my situation, but it would be good for me to document these and the relevant solutions.

Future updates to Mac OS X could introduce additional technical problems that need to be resolved in order to maintain support for SMB filesystems. Future versions of Subversion certainly will. For example, the merge-tracking branch of Subversion uses SQLite, which is not presently compatible with SMB filesystems.

As time allows, I’ll do my best to maintain this patch. Of course, any help or constructive suggestions are always welcome.