<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>delx &#187; hfs</title>
	<atom:link href="http://delx.net.au/blog/tag/hfs/feed/" rel="self" type="application/rss+xml" />
	<link>http://delx.net.au/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 07 Jan 2012 06:37:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>HFS+ auto defragmentation of files under Mac OS X</title>
		<link>http://delx.net.au/blog/2008/11/hfs-auto-defragmentation-of-files-under-mac-os-x/</link>
		<comments>http://delx.net.au/blog/2008/11/hfs-auto-defragmentation-of-files-under-mac-os-x/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 02:14:17 +0000</pubDate>
		<dc:creator>delx</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[defrag]]></category>
		<category><![CDATA[hfs]]></category>
		<category><![CDATA[osx]]></category>

		<guid isPermaLink="false">http://delx.net.au/blog/?p=13</guid>
		<description><![CDATA[HFS+ automatically defragments files as they are used. When a fragmented file is opened, if the system has been up for 3 minutes, the file is not busy and is under 20MiB in size then it will be relocated to be in a contiguous section of the disk So I was having a discussion with [...]]]></description>
			<content:encoded><![CDATA[<p>HFS+ automatically defragments files as they are used. When a fragmented file is opened, if the system has been up for 3 minutes, the file is not busy and is under 20MiB in size then it will be relocated to be in a contiguous section of the disk</p>
<p><span id="more-13"></span></p>
<p>So I was having a discussion with Greg yesterday about file systems and I claimed the above. I have actually made this claim, that OS X automatically defragments files, many times in the past. However this time Greg decided to call me on it. He told me: <i>Citation needed</i>.</p>
<p>I knew I&#8217;d read this somewhere, but couldn&#8217;t track my original source. So I did a little bit of research and found there wasn&#8217;t an easily accessible authoritative source anywhere.</p>
<p><a href="http://support.apple.com/kb/HT1375">Apple Technote HT1375</a> was one of the first pages I found. However it only talks about the Hot-File-Adaptive-Clustering, which is a completely different technique.</p>
<p>A little more searching and I found this Ars Technica article, a <a href="http://arstechnica.com/reviews/os/macosx-10-3.ars/5">review of Mac OS X 10.3</a>. This actually contains some solid details on the auto defragmentation feature. The reference for this information is only a <a href="http://article.gmane.org/gmane.comp.macosx.general/22906">newsgroup post</a> on comp.macosx.general, not exactly definitive.</p>
<p>So I decided to go to the source. Apple publishes the source code for large components of Mac OS X in the <a href="http://www.opensource.apple.com/darwinsource/Current/">Darwin Source Code</a> repository. In this case we needed to look at the HFS+ driver, which is part of XNU &#8211; the OS X kernel.<br />
Incidentally, thanks go to Apple for making this code available to everybody for random research like this.</p>
<p>Before long I&#8217;d narrowed it down to this file: <a href="http://www.opensource.apple.com/darwinsource/10.5.5/xnu-1228.7.58/bsd/hfs/hfs_vnops.c">xnu-1228.7.58/bsd/hfs/hfs_vnops.c</a>. The function that we&#8217;re interested in is <tt>hfs_vnop_open()</tt>, which is called whenever the system needs to open a file or directory on disk for reading or writing. The last two comments in that function explain the conditions under which defragmentation occurs. Nicely written and well commented code.</p>
<p>Since you need a free Apple developer account to view the above link, here&#8217;s the relevant section of the source code:</p>
<pre>
/*
 * Open a file/directory.
 */
static int
hfs_vnop_open(struct vnop_open_args *ap)
{
  struct vnode *vp = ap-&gt;a_vp;
  struct filefork *fp;
  struct timeval tv;
  int error;

  /*
   * Files marked append-only must be opened for appending.
   */
  if ((VTOC(vp)-&gt;c_flags &amp; APPEND) &amp;&amp; !vnode_isdir(vp) &amp;&amp;
      (ap-&gt;a_mode &amp; (FWRITE | O_APPEND)) == FWRITE)
    return (EPERM);

  if (vnode_isreg(vp) &amp;&amp; !UBCINFOEXISTS(vp))
    return (EBUSY);  /* file is in use by the kernel */

  /* Don't allow journal file to be opened externally. */
  if (VTOC(vp)-&gt;c_fileid == VTOHFS(vp)-&gt;hfs_jnlfileid)
    return (EPERM);
  /*
   * On the first (non-busy) open of a fragmented
   * file attempt to de-frag it (if its less than 20MB).
   */
  if ((VTOHFS(vp)-&gt;hfs_flags &amp; HFS_READ_ONLY) ||
      (VTOHFS(vp)-&gt;jnl == NULL) ||
#if NAMEDSTREAMS
      !vnode_isreg(vp) || vnode_isinuse(vp, 0) ||
      vnode_isnamedstream(vp)) {
#else
      !vnode_isreg(vp) || vnode_isinuse(vp, 0)) {
#endif
    return (0);
  }

  if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
    return (error);
  fp = VTOF(vp);
  if (fp-&gt;ff_blocks &amp;&amp;
      fp-&gt;ff_extents[7].blockCount != 0 &amp;&amp;
      fp-&gt;ff_size &lt;= (20 * 1024 * 1024)) {
    struct timeval now;
    struct cnode *cp = VTOC(vp);
    /*
     * Wait until system bootup is done (3 min).
     * And don't relocate a file that's been modified
     * within the past minute -- this can lead to
     * system thrashing.
     */
    microuptime(&amp;tv);
    microtime(&amp;now);
    if (tv.tv_sec &gt; (60 * 3) &amp;&amp;
       ((now.tv_sec - cp-&gt;c_mtime) &gt; 60)) {
      (void) hfs_relocate(vp, VTOVCB(vp)-&gt;nextAllocation + 4096,
                          vfs_context_ucred(ap-&gt;a_context),
                          vfs_context_proc(ap-&gt;a_context));
    }
  }
  hfs_unlock(VTOC(vp));

  return (0);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://delx.net.au/blog/2008/11/hfs-auto-defragmentation-of-files-under-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

