Docbook XML conversion: projdoc
[ira/wip.git] / docs / docbook / projdoc / locking.xml
1 <chapter id="locking">
2 <chapterinfo>
3         &author.jeremy;
4         &author.jelmer;
5         &author.jht;
6 </chapterinfo>
7 <title>File and Record Locking</title>
8
9 <sect1>
10 <title>Discussion</title>
11
12 <para>
13 One area which sometimes causes trouble is locking.
14 </para>
15
16 <para>
17 There are two types of locking which need to be performed by a SMB server.
18 The first is <emphasis>record locking</emphasis> which allows a client to lock
19 a range of bytes in a open file.  The second is the <emphasis>deny modes</emphasis>
20 that are specified when a file is open.
21 </para>
22
23 <para>
24 Record locking semantics under Unix is very different from record locking under
25 Windows. Versions of Samba before 2.2 have tried to use the native fcntl() unix
26 system call to implement proper record locking between different Samba clients.
27 This can not be fully correct due to several reasons. The simplest is the fact
28 that a Windows client is allowed to lock a byte range up to 2^32 or 2^64,
29 depending on the client OS. The unix locking only supports byte ranges up to 2^31.
30 So it is not possible to correctly satisfy a lock request above 2^31. There are
31 many more differences, too many to be listed here.
32 </para>
33
34 <para>
35 Samba 2.2 and above implements record locking completely independent of the
36 underlying unix system. If a byte range lock that the client requests happens
37 to fall into the range 0-2^31, Samba hands this request down to the Unix system.
38 All other locks can not be seen by unix anyway.
39 </para>
40
41 <para>
42 Strictly a SMB server should check for locks before every read and write call on
43 a file. Unfortunately with the way fcntl() works this can be slow and may overstress
44 the rpc.lockd. It is also almost always unnecessary as clients are supposed to
45 independently make locking calls before reads and writes anyway if locking is
46 important to them. By default Samba only makes locking calls when explicitly asked
47 to by a client, but if you set <emphasis>strict locking = yes</emphasis> then it
48 will make lock checking calls on every read and write.
49 </para>
50
51 <para>
52 You can also disable by range locking completely using <emphasis>locking = no</emphasis>.
53 This is useful for those shares that don't support locking or don't need it
54 (such as cdroms). In this case Samba fakes the return codes of locking calls to
55 tell clients that everything is OK.
56 </para>
57
58 <para>
59 The second class of locking is the <emphasis>deny modes</emphasis>. These 
60 are set by an application when it opens a file to determine what types of
61 access should be allowed simultaneously with its open. A client may ask for
62 DENY_NONE, DENY_READ, DENY_WRITE or DENY_ALL. There are also special compatibility
63 modes called DENY_FCB and  DENY_DOS.
64 </para>
65 </sect1>
66
67 <sect1>
68 <title>Samba Opportunistic Locking Control</title>
69
70 <para>
71 Opportunistic locking essentially means that the client is allowed to download and cache
72 a file on their hard drive while making changes; if a second client wants to access the
73 file, the first client receives a break and must synchronise the file back to the server.
74 This can give significant performance gains in some cases; some programs insist on
75 synchronising the contents of the entire file back to the server for a single change.
76 </para>
77
78 <para>
79 Level1 Oplocks (aka just plain "oplocks") is another term for opportunistic locking.
80 </para>
81
82 <para>
83 Level2 Oplocks provids opportunistic locking for a file that will be treated as
84 <emphasis>read only</emphasis>. Typically this is used on files that are read-only or
85 on files that the client has no initial intention to write to at time of opening the file.
86 </para>
87
88 <para>
89 Kernel Oplocks are essentially a method that allows the Linux kernel to co-exist with
90 Samba's oplocked files, although this has provided better integration of MS Windows network
91 file locking with the under lying OS, SGI IRIX and Linux are the only two OS's that are
92 oplock aware at this time.
93 </para>
94
95 <para>
96 Unless your system supports kernel oplocks, you should disable oplocks if you are
97 accessing the same files from both Unix/Linux and SMB clients. Regardless, oplocks should
98 always be disabled if you are sharing a database file (e.g., Microsoft Access) between
99 multiple clients, as any break the first client receives will affect synchronisation of
100 the entire file (not just the single record), which will result in a noticable performance
101 impairment and, more likely, problems accessing the database in the first place. Notably,
102 Microsoft Outlook's personal folders (*.pst) react very badly to oplocks. If in doubt,
103 disable oplocks and tune your system from that point.
104 </para>
105
106 <para>
107 If client-side caching is desirable and reliable on your network, you will benefit from
108 turning on oplocks. If your network is slow and/or unreliable, or you are sharing your
109 files among other file sharing mechanisms (e.g., NFS) or across a WAN, or multiple people
110 will be accessing the same files frequently, you probably will not benefit from the overhead
111 of your client sending oplock breaks and will instead want to disable oplocks for the share.
112 </para>
113
114 <para>
115 Another factor to consider is the perceived performance of file access. If oplocks provide no
116 measurable speed benefit on your network, it might not be worth the hassle of dealing with them.
117 </para>
118
119 <para>
120 You can disable oplocks on a per-share basis with the following:
121
122 <programlisting>
123         oplocks = False
124         level2 oplocks = False
125 </programlisting>
126
127 Alternately, you could disable oplocks on a per-file basis within the share:
128
129 <programlisting>
130         veto oplock files = /*.mdb/*.MDB/*.dbf/*.DBF/
131 </programlisting>
132 </para>
133
134 <para>
135 If you are experiencing problems with oplocks as apparent from Samba's log entries,
136 you may want to play it safe and disable oplocks and level2 oplocks.
137 </para>
138
139 </sect1>
140
141 <sect1>
142 <title>MS Windows Opportunistic Locking and Caching Controls</title>
143
144 <para>
145 There is a known issue when running applications (like Norton Anti-Virus) on a Windows 2000/ XP
146 workstation computer that can affect any application attempting to access shared database files
147 across a network. This is a result of a default setting configured in the Windows 2000/XP
148 operating system known as <emphasis>Opportunistic Locking</emphasis>. When a workstation
149 attempts to access shared data files located on another Windows 2000/XP computer,
150 the Windows 2000/XP operating system will attempt to increase performance by locking the
151 files and caching information locally. When this occurs, the application is unable to
152 properly function, which results in an <emphasis>Access Denied</emphasis>
153  error message being displayed during network operations.
154 </para>
155
156 <para>
157 All Windows operating systems in the NT family that act as database servers for data files
158 (meaning that data files are stored there and accessed by other Windows PCs) may need to
159 have opportunistic locking disabled in order to minimize the risk of data file corruption.
160 This includes Windows 9x/Me, Windows NT, Windows 200x and Windows XP.
161 </para>
162
163 <para>
164 If you are using a Windows NT family workstation in place of a server, you must also
165 disable opportunistic locking (oplocks) on that workstation. For example, if you use a
166 PC with the Windows NT Workstation operating system instead of Windows NT Server, and you
167 have data files located on it that are accessed from other Windows PCs, you may need to
168 disable oplocks on that system.
169 </para>
170
171 <para>
172 The major difference is the location in the Windows registry where the values for disabling
173 oplocks are entered. Instead of the LanManServer location, the LanManWorkstation location
174 may be used.
175 </para>
176
177 <para>
178 You can verify (or change or add, if necessary) this Registry value using the Windows
179 Registry Editor. When you change this registry value, you will have to reboot the PC
180 to ensure that the new setting goes into effect.
181 </para>
182
183 <para>
184 The location of the client registry entry for opportunistic locking has changed in
185 Windows 2000 from the earlier location in Microsoft Windows NT.
186 </para>
187
188 <note><para>
189 Windows 2000 will still respect the EnableOplocks registry value used to disable oplocks
190 in earlier versions of Windows.
191 </para></note>
192
193 <para>
194 You can also deny the granting of opportunistic locks by changing the following registry entries:
195 </para>
196
197 <para>
198 <programlisting>
199         HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MRXSmb\Parameters\
200
201                 OplocksDisabled REG_DWORD 0 or 1
202                 Default: 0 (not disabled)
203 </programlisting>
204 </para>
205
206 <note><para>
207 The OplocksDisabled registry value configures Windows clients to either request or not
208 request opportunistic locks on a remote file. To disable oplocks, the value of
209  OplocksDisabled must be set to 1.
210 </para></note>
211
212 <para>
213 <programlisting>
214         HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters
215
216                 EnableOplocks REG_DWORD 0 or 1
217                 Default: 1 (Enabled by Default)
218
219                 EnableOpLockForceClose REG_DWORD 0 or 1
220                 Default: 0 (Disabled by Default)
221 </programlisting>
222 </para>
223
224 <note><para>
225 The EnableOplocks value configures Windows-based servers (including Workstations sharing
226 files) to allow or deny opportunistic locks on local files.
227 </para></note>
228
229 <para>
230 To force closure of open oplocks on close or program exit EnableOpLockForceClose must be set to 1.
231 </para>
232
233 <para>
234 An illustration of how level II oplocks work:
235 </para>
236
237 <itemizedlist>
238         <listitem><para>
239         Station 1 opens the file, requesting oplock.
240         </para></listitem>
241         <listitem><para>
242         Since no other station has the file open, the server grants station 1 exclusive oplock.
243         </para></listitem>
244         <listitem><para>
245         Station 2 opens the file, requesting oplock.
246         </para></listitem>
247         <listitem><para>
248         Since station 1 has not yet written to the file, the server asks station 1 to Break
249         to Level II Oplock.
250         </para></listitem>
251         <listitem><para>
252         Station 1 complies by flushing locally buffered lock information to the server.
253         </para></listitem>
254         <listitem><para>
255         Station 1 informs the server that it has Broken to Level II Oplock (alternatively,
256         station 1 could have closed the file).
257         </para></listitem>
258         <listitem><para>
259         The server responds to station 2's open request, granting it level II oplock.
260         Other stations can likewise open the file and obtain level II oplock.
261         </para></listitem>
262         <listitem><para>
263         Station 2 (or any station that has the file open) sends a write request SMB.
264         The server returns the write response.
265         </para></listitem>
266         <listitem><para>
267         The server asks all stations that have the file open to Break to None, meaning no
268         station holds any oplock on the file. Because the workstations can have no cached
269         writes or locks at this point, they need not respond to the break-to-none advisory;
270         all they need do is invalidate locally cashed read-ahead data.
271         </para></listitem>
272 </itemizedlist>
273
274 <sect2>
275 <title>Workstation Service Entries</title>
276
277 <para><programlisting>
278         \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters
279
280         UseOpportunisticLocking   REG_DWORD   0 or 1
281         Default: 1 (true)
282 </programlisting></para>
283
284 <para>
285 Indicates whether the redirector should use opportunistic-locking (oplock) performance
286 enhancement. This parameter should be disabled only to isolate problems.
287 </para>
288
289 </sect2>
290 <sect2>
291 <title>Server Service Entries</title>
292
293 <para><programlisting>
294         \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters
295
296         EnableOplocks   REG_DWORD   0 or 1
297         Default: 1 (true)
298 </programlisting></para>
299
300 <para>
301 Specifies whether the server allows clients to use oplocks on files. Oplocks are a
302 significant performance enhancement, but have the potential to cause lost cached
303 data on some networks, particularly wide-area networks.
304 </para>
305
306 <para><programlisting>
307         MinLinkThroughput   REG_DWORD   0 to infinite bytes per second
308         Default: 0
309 </programlisting></para>
310
311 <para>
312 Specifies the minimum link throughput allowed by the server before it disables
313 raw and opportunistic locks for this connection.
314 </para>
315
316 <para><programlisting>
317         MaxLinkDelay   REG_DWORD   0 to 100,000 seconds
318         Default: 60
319 </programlisting></para>
320
321 <para>
322 Specifies the maximum time allowed for a link delay. If delays exceed this number,
323 the server disables raw I/O and opportunistic locking for this connection.
324 </para>
325
326 <para><programlisting>
327         OplockBreakWait   REG_DWORD   10 to 180 seconds
328         Default: 35
329 </programlisting></para>
330
331 <para>
332 Specifies the time that the server waits for a client to respond to an oplock break
333 request. Smaller values can allow detection of crashed clients more quickly but can
334 potentially cause loss of cached data.
335 </para>
336
337 </sect2>
338 </sect1>
339
340 <sect1>
341 <title>Persistent Data Corruption</title>
342
343 <para>
344 If you have applied all of the settings discussed in this paper but data corruption problems
345 and other symptoms persist, here are some additional things to check out:
346 </para>
347
348 <para>
349 We have credible reports from developers that faulty network hardware, such as a single
350 faulty network card, can cause symptoms similar to read caching and data corruption.
351 If you see persistent data corruption even after repeated reindexing, you may have to
352 rebuild the data files in question. This involves creating a new data file with the
353 same definition as the file to be rebuilt and transferring the data from the old file
354 to the new one. There are several known methods for doing this that can be found in
355 our Knowledge Base.
356 </para>
357
358 </sect1>
359
360 <sect1>
361 <title>Additional Reading</title>
362
363 <para>
364 You may want to check for an updated version of this white paper on our Web site from
365 time to time. Many of our white papers are updated as information changes. For those papers,
366 the Last Edited date is always at the top of the paper.
367 </para>
368
369 <para>
370 Section of the Microsoft MSDN Library on opportunistic locking: 
371 </para>
372
373 <para>
374 Opportunistic Locks, Microsoft Developer Network (MSDN), Windows Development &gt;
375 Windows Base Services &gt; Files and I/O &gt; SDK Documentation &gt; File Storage &gt; File Systems
376 &gt; About File Systems &gt; Opportunistic Locks, Microsoft Corporation.
377 <ulink url="http://msdn.microsoft.com/library/en-us/fileio/storage_5yk3.asp">http://msdn.microsoft.com/library/en-us/fileio/storage_5yk3.asp</ulink>
378 </para>
379
380 <para>
381 Microsoft Knowledge Base Article Q224992 "Maintaining Transactional Integrity with OPLOCKS",
382 Microsoft Corporation, April 1999, <ulink url="=http://support.microsoft.com/default.aspx?scid=kb;en-us;Q224992">http://support.microsoft.com/default.aspx?scid=kb;en-us;Q224992</ulink>.
383 </para>
384
385 <para>
386 Microsoft Knowledge Base Article Q296264 "Configuring Opportunistic Locking in Windows 2000",
387 Microsoft Corporation, April 2001, <ulink url="http://support.microsoft.com/default.aspx?scid=kb;en-us;Q296264">http://support.microsoft.com/default.aspx?scid=kb;en-us;Q296264</ulink>.
388 </para>
389
390 <para>
391 Microsoft Knowledge Base Article Q129202 "PC Ext: Explanation of Opportunistic Locking on Windows NT",
392  Microsoft Corporation, April 1995, <ulink url="http://support.microsoft.com/default.aspx?scid=kb;en-us;Q129202">http://support.microsoft.com/default.aspx?scid=kb;en-us;Q129202</ulink>.
393 </para>
394
395 </sect1>
396 </chapter>