Add some more common errors
[sfrench/samba-autobuild/.git] / docs / docbook / projdoc / locking.xml
1 <chapter id="locking">
2 <chapterinfo>
3         &author.jeremy;
4         &author.jelmer;
5         &author.jht;
6         &author.eroseme;
7 </chapterinfo>
8 <title>File and Record Locking</title>
9
10 <para>
11 One area which causes trouble for many network administrators is locking.
12 The extent of the problem is readily evident from searches over the internet.
13 </para>
14
15 <sect1>
16 <title>Features and Benefits</title>
17
18 <para>
19 Samba provides all the same locking semantics that MS Windows clients expect
20 and that MS Windows NT4 / 200x servers provide also.
21 </para>
22
23 <para>
24 The term <emphasis>locking</emphasis> has exceptionally broad meaning and covers
25 a range of functions that are all categorized under this one term.
26 </para>
27
28 <para>
29 Opportunistic locking is a desirable feature when it can enhance the
30 perceived performance of applications on a networked client.  However, the
31 opportunistic locking protocol is not robust, and therefore can
32 encounter problems when invoked beyond a simplistic configuration, or
33 on extended, slow, or faulty networks.  In these cases, operating
34 system management of opportunistic locking and/or recovering from
35 repetitive errors can offset the perceived performance advantage that
36 it is intended to provide.
37 </para>
38
39 <para>
40 The MS Windows network administrator needs to be aware that file and record
41 locking semantics (behaviour) can be controlled either in Samba or by way of registry
42 settings on the MS Windows client.
43 </para>
44
45 <note>
46 <para>
47 Sometimes it is necessary to disable locking control settings BOTH on the Samba
48 server as well as on each MS Windows client!
49 </para>
50 </note>
51
52 </sect1>
53
54 <sect1>
55 <title>Discussion</title>
56
57 <para>
58 There are two types of locking which need to be performed by a SMB server.
59 The first is <emphasis>record locking</emphasis> which allows a client to lock
60 a range of bytes in a open file.  The second is the <emphasis>deny modes</emphasis>
61 that are specified when a file is open.
62 </para>
63
64 <para>
65 Record locking semantics under Unix is very different from record locking under
66 Windows. Versions of Samba before 2.2 have tried to use the native fcntl() unix
67 system call to implement proper record locking between different Samba clients.
68 This can not be fully correct due to several reasons. The simplest is the fact
69 that a Windows client is allowed to lock a byte range up to 2^32 or 2^64,
70 depending on the client OS. The unix locking only supports byte ranges up to 2^31.
71 So it is not possible to correctly satisfy a lock request above 2^31. There are
72 many more differences, too many to be listed here.
73 </para>
74
75 <para>
76 Samba 2.2 and above implements record locking completely independent of the
77 underlying unix system. If a byte range lock that the client requests happens
78 to fall into the range 0-2^31, Samba hands this request down to the Unix system.
79 All other locks can not be seen by unix anyway.
80 </para>
81
82 <para>
83 Strictly a SMB server should check for locks before every read and write call on
84 a file. Unfortunately with the way fcntl() works this can be slow and may overstress
85 the <command>rpc.lockd</command>. It is also almost always unnecessary as clients are supposed to
86 independently make locking calls before reads and writes anyway if locking is
87 important to them. By default Samba only makes locking calls when explicitly asked
88 to by a client, but if you set <parameter>strict locking = yes</parameter> then it
89 will make lock checking calls on every read and write.
90 </para>
91
92 <para>
93 You can also disable by range locking completely using <parameter>locking = no</parameter>.
94 This is useful for those shares that don't support locking or don't need it
95 (such as cdroms). In this case Samba fakes the return codes of locking calls to
96 tell clients that everything is OK.
97 </para>
98
99 <para>
100 The second class of locking is the <parameter>deny modes</parameter>. These 
101 are set by an application when it opens a file to determine what types of
102 access should be allowed simultaneously with its open. A client may ask for
103 <constant>DENY_NONE</constant>, <constant>DENY_READ</constant>, 
104 <constant>DENY_WRITE</constant> or <constant>DENY_ALL</constant>. There are also special compatibility
105 modes called <constant>DENY_FCB</constant> and <constant>DENY_DOS</constant>.
106 </para>
107
108 <sect2>
109 <title>Opportunistic Locking Overview</title>
110
111 <para>
112 Opportunistic locking (Oplocks) is invoked by the Windows file system
113 (as opposed to an API) via registry entries (on the server AND client)
114 for the purpose of enhancing network performance when accessing a file
115 residing on a server. Performance is enhanced by caching the file
116 locally on the client which allows:
117 </para>
118
119 <variablelist>
120         <varlistentry><term>Read-ahead:</term>
121                 <listitem><para>
122                 The client reads the local copy of the file, eliminating network latency
123                 </para></listitem>
124         </varlistentry>
125
126         <varlistentry><term>Write caching:</term>
127                 <listitem><para>
128                 The client writes to the local copy of the file, eliminating network latency
129                 </para></listitem>
130         </varlistentry>
131
132         <varlistentry><term>Lock caching:</term>
133         <listitem><para>
134                 The client caches application locks locally, eliminating network latency
135                 </para></listitem>
136         </varlistentry>
137 </variablelist>
138
139 <para>
140 The performance enhancement of oplocks is due to the opportunity of
141 exclusive access to the file - even if it is opened with deny-none -
142 because Windows monitors the file's status for concurrent access from
143 other processes.
144 </para>
145
146 <variablelist>
147 <title>Windows defines 4 kinds of Oplocks:</title>
148         <varlistentry><term>Level1 Oplock:</term>
149                 <listitem><para>
150                 The redirector sees that the file was opened with deny
151                 none (allowing concurrent access), verifies that no
152                 other process is accessing the file, checks that
153                 oplocks are enabled, then grants deny-all/read-write/ex-
154                 clusive access to the file.  The client now performs
155                 operations on the cached local file.
156                 </para>
157
158                 <para>
159                 If a second process attempts to open the file, the open
160                 is deferred while the redirector "breaks" the original
161                 oplock.  The oplock break signals the caching client to
162                 write the local file back to the server, flush the
163                 local locks, and discard read-ahead data.  The break is
164                 then complete, the deferred open is granted, and the
165                 multiple processes can enjoy concurrent file access as
166                 dictated by mandatory or byte-range locking options.
167                 However, if the original opening process opened the
168                 file with a share mode other than deny-none, then the
169                 second process is granted limited or no access, despite
170                 the oplock break.
171                 </para></listitem>
172         </varlistentry>
173
174         <varlistentry><term>Level2 Oplock:</term>
175                 <listitem><para>
176                 Performs like a level1 oplock, except caching is only
177                 operative for reads. All other operations are performed
178                 on the server disk copy of the file.
179                 </para></listitem>
180         </varlistentry>
181
182         <varlistentry><term>Filter Oplock:</term>
183                 <listitem><para>
184                 Does not allow write or delete file access
185                 </para></listitem>
186         </varlistentry>
187
188         <varlistentry><term>Batch Oplock:</term>
189                 <listitem><para>
190                 Manipulates file openings and closings - allows caching
191                 of file attributes
192                 </para></listitem>
193         </varlistentry>
194 </variablelist>
195
196 <para>
197 An important detail is that oplocks are invoked by the file system, not
198 an application API.  Therefore, an application can close an oplocked
199 file, but the file system does not relinquish the oplock.  When the
200 oplock break is issued, the file system then simply closes the file in
201 preparation for the subsequent open by the second process.
202 </para>
203
204 <para>
205 <emphasis>Opportunistic Locking</emphasis> is actually an improper name for this feature.
206 The true benefit of this feature is client-side data caching, and
207 oplocks is merely a notification mechanism for writing data back to the
208 networked storage disk.  The limitation of opportunistic locking is the
209 reliability of the mechanism to process an oplock break (notification)
210 between the server and the caching client.  If this exchange is faulty
211 (usually due to timing out for any number of reasons) then the
212 client-side caching benefit is negated.
213 </para>
214
215 <para>
216 The actual decision that a user or administrator should consider is
217 whether it is sensible to share amongst multiple users data that will
218 be cached locally on a client.  In many cases the answer is no.
219 Deciding when to cache or not cache data is the real question, and thus
220 "opportunistic locking" should be treated as a toggle for client-side
221 caching. Turn it "ON" when client-side caching is desirable and
222 reliable.  Turn it "OFF" when client-side caching is redundant,
223 unreliable, or counter-productive.
224 </para>
225
226 <para>
227 Opportunistic locking is by default set to "on" by Samba on all
228 configured shares, so careful attention should be given to each case to
229 determine if the potential benefit is worth the potential for delays.
230 The following recommendations will help to characterize the environment
231 where opportunistic locking may be effectively configured.
232 </para>
233
234 <para>
235 Windows Opportunistic Locking is a lightweight performance-enhancing
236 feature.  It is not a robust and reliable protocol.  Every
237 implementation of Opportunistic Locking should be evaluated as a
238 tradeoff between perceived performance and reliability.  Reliability
239 decreases as each successive rule above is not enforced.  Consider a
240 share with oplocks enabled, over a wide area network, to a client on a
241 South Pacific atoll, on a high-availability server, serving a
242 mission-critical multi-user corporate database, during a tropical
243 storm.  This configuration will likely encounter problems with oplocks.
244 </para>
245
246 <para>
247 Oplocks can be beneficial to perceived client performance when treated
248 as a configuration toggle for client-side data caching.  If the data
249 caching is likely to be interrupted, then oplock usage should be
250 reviewed.  Samba enables opportunistic locking by default on all
251 shares.  Careful attention should be given to the client usage of
252 shared data on the server, the server network reliability, and the
253 opportunistic locking configuration of each share.
254 n mission critical high availability environments, data integrity is
255 often a priority.  Complex and expensive configurations are implemented
256 to ensure that if a client loses connectivity with a file server, a
257 failover replacement will be available immediately to provide
258 continuous data availability.
259 </para>
260
261 <para>
262 Windows client failover behavior is more at risk of application
263 interruption than other platforms because it is dependant upon an
264 established TCP transport connection.  If the connection is interrupted
265 - as in a file server failover - a new session must be established.
266 It is rare for Windows client applications to be coded to recover
267 correctly from a transport connection loss, therefore most applications
268 will experience some sort of interruption - at worst, abort and
269 require restarting.
270 </para>
271
272 <para>
273 If a client session has been caching writes and reads locally due to
274 opportunistic locking, it is likely that the data will be lost when the
275 application restarts, or recovers from the TCP interrupt. When the TCP
276 connection drops, the client state is lost.  When the file server
277 recovers, an oplock break is not sent to the client.  In this case, the
278 work from the prior session is lost.  Observing this scenario with
279 oplocks disabled, and the client was writing data to the file server
280 real-time, then the failover will provide the data on disk as it
281 existed at the time of the disconnect.
282 </para>
283
284 <para>
285 In mission critical high availability environments, careful attention
286 should be given to opportunistic locking.  Ideally, comprehensive
287 testing should be done with all affected applications with oplocks
288 enabled and disabled.
289 </para>
290
291 <sect3>
292 <title>Exclusively Accessed Shares</title>
293
294 <para>
295 Opportunistic locking is most effective when it is confined to shares
296 that are exclusively accessed by a single user, or by only one user at
297 a time.  Because the true value of opportunistic locking is the local
298 client caching of data, any operation that interrupts the caching
299 mechanism will cause a delay.
300 </para>
301
302 <para>
303 Home directories are the most obvious examples of where the performance
304 benefit of opportunistic locking can be safely realized.
305 </para>
306
307 </sect3>
308
309 <sect3>
310 <title>Multiple-Accessed Shares or Files</title>
311
312 <para>
313 As each additional user accesses a file in a share with opportunistic
314 locking enabled, the potential for delays and resulting perceived poor
315 performance increases.  When multiple users are accessing a file on a
316 share that has oplocks enabled, the management impact of sending and
317 receiving oplock breaks, and the resulting latency while other clients
318 wait for the caching client to flush data, offset the performance gains
319 of the caching user.
320 </para>
321
322 <para>
323 As each additional client attempts to access a file with oplocks set,
324 the potential performance improvement is negated and eventually results
325 in a performance bottleneck.
326 </para>
327
328 </sect3>
329
330 <sect3>
331 <title>Unix or NFS Client Accessed Files</title>
332
333 <para>
334 Local Unix and NFS clients access files without a mandatory
335 file locking mechanism.  Thus, these client platforms are incapable of
336 initiating an oplock break request from the server to a Windows client
337 that has a file cached. Local Unix or NFS file access can therefore
338 write to a file that has been cached by a Windows client, which
339 exposes the file to likely data corruption.
340 </para>
341
342 <para>
343 If files are shared between Windows clients, and either loca Unix 
344 or NFS users, then turn opportunistic locking off.
345 </para>
346
347 </sect3>
348
349 <sect3>
350 <title>Slow and/or Unreliable Networks</title>
351
352 <para>
353 The biggest potential performance improvement for opportunistic locking
354 occurs when the client-side caching of reads and writes delivers the
355 most differential over sending those reads and writes over the wire.
356 This is most likely to occur when the network is extremely slow,
357 congested, or distributed (as in a WAN).  However, network latency also
358 has a very high impact on the reliability of the oplock break
359 mechanism, and thus increases the likelihood of encountering oplock
360 problems that more than offset the potential perceived performance
361 gain. Of course, if an oplock break never has to be sent, then this is
362 the most advantageous scenario to utilize opportunistic locking.
363 </para>
364
365 <para>
366 If the network is slow, unreliable, or a WAN, then do not configure
367 opportunistic locking if there is any chance of multiple users
368 regularly opening the same file.
369 </para>
370
371 </sect3>
372
373 <sect3>
374 <title>Multi-User Databases</title>
375
376 <para>
377 Multi-user databases clearly pose a risk due to their very nature -
378 they are typically heavily accessed by numerous users at random
379 intervals.  Placing a multi-user database on a share with opportunistic
380 locking enabled will likely result in a locking management bottleneck
381 on the Samba server.  Whether the database application is developed
382 in-house or a commercially available product, ensure that the share
383 has opportunistic locking disabled.
384 </para>
385
386 </sect3>
387
388 <sect3>
389 <title>PDM Data Shares</title>
390
391 <para>
392 Process Data Management (PDM) applications such as IMAN, Enovia, and
393 Clearcase, are increasing in usage with Windows client platforms, and
394 therefore SMB data stores.  PDM applications manage multi-user
395 environments for critical data security and access.  The typical PDM
396 environment is usually associated with sophisticated client design
397 applications that will load data locally as demanded.  In addition, the
398 PDM application will usually monitor the data-state of each client.
399 In this case, client-side data caching is best left to the local
400 application and PDM server to negotiate and maintain.  It is
401 appropriate to eliminate the client OS from any caching tasks, and the
402 server from any oplock management, by disabling opportunistic locking on
403 the share.
404 </para>
405
406 </sect3>
407
408 <sect3>
409 <title>Beware of Force User</title>
410
411 <para>
412 Samba includes an &smb.conf; parameter called <parameter>force user</parameter> that changes
413 the user accessing a share from the incoming user to whatever user is
414 defined by the smb.conf variable.  If opportunistic locking is enabled
415 on a share, the change in user access causes an oplock break to be sent
416 to the client, even if the user has not explicitly loaded a file.  In
417 cases where the network is slow or unreliable, an oplock break can
418 become lost without the user even accessing a file.  This can cause
419 apparent performance degradation as the client continually reconnects
420 to overcome the lost oplock break.
421 </para>
422
423 <para>
424 Avoid the combination of the following: 
425 </para>
426
427 <itemizedlist>
428         <listitem><para>
429         <parameter>force user</parameter> in the &smb.conf; share configuration.
430         </para></listitem>
431
432         <listitem><para>
433         Slow or unreliable networks
434         </para></listitem>
435
436         <listitem><para>
437         Opportunistic Locking Enabled
438         </para></listitem>
439 </itemizedlist>
440
441 </sect3>
442
443 <sect3>
444 <title>Advanced Samba Opportunistic Locking Parameters</title>
445
446 <para>
447 Samba provides opportunistic locking parameters that allow the
448 administrator to adjust various properties of the oplock mechanism to
449 account for timing and usage levels.  These parameters provide good
450 versatility for implementing oplocks in environments where they would
451 likely cause problems.  The parameters are: 
452 <parameter>oplock break wait time</parameter>,
453 <parameter>oplock contention limit</parameter>.
454 </para>
455
456 <para>
457 For most users, administrators, and environments, if these parameters
458 are required, then the better option is to simply turn oplocks off.
459 The samba SWAT help text for both parameters reads "DO NOT CHANGE THIS
460 PARAMETER UNLESS YOU HAVE READ AND UNDERSTOOD THE SAMBA OPLOCK CODE."
461 This is good advice.
462 </para>
463
464 </sect3>
465
466 <sect3>
467 <title>Mission Critical High Availability</title>
468
469 <para>
470 In mission critical high availability environments, data integrity is
471 often a priority.  Complex and expensive configurations are implemented
472 to ensure that if a client loses connectivity with a file server, a
473 failover replacement will be available immediately to provide
474 continuous data availability.
475 </para>
476
477 <para>
478 Windows client failover behavior is more at risk of application
479 interruption than other platforms because it is dependant upon an
480 established TCP transport connection.  If the connection is interrupted
481 - as in a file server failover - a new session must be established.
482 It is rare for Windows client applications to be coded to recover
483 correctly from a transport connection loss, therefore most applications
484 will experience some sort of interruption - at worst, abort and
485 require restarting.
486 </para>
487
488 <para>
489 If a client session has been caching writes and reads locally due to
490 opportunistic locking, it is likely that the data will be lost when the
491 application restarts, or recovers from the TCP interrupt. When the TCP
492 connection drops, the client state is lost.  When the file server
493 recovers, an oplock break is not sent to the client.  In this case, the
494 work from the prior session is lost.  Observing this scenario with
495 oplocks disabled, and the client was writing data to the file server
496 real-time, then the failover will provide the data on disk as it
497 existed at the time of the disconnect.
498 </para>
499
500 <para>
501 In mission critical high availability environments, careful attention
502 should be given to opportunistic locking.  Ideally, comprehensive
503 testing should be done with all affected applications with oplocks
504 enabled and disabled.
505 </para>
506
507 </sect3>
508 </sect2>
509 </sect1>
510
511 <sect1>
512 <title>Samba Opportunistic Locking Control</title>
513
514 <para>
515 Opportunistic Locking is a unique Windows file locking feature.  It is
516 not really file locking, but is included in most discussions of Windows
517 file locking, so is considered a defacto locking feature.
518 Opportunistic Locking is actually part of the Windows client file
519 caching mechanism.  It is not a particularly robust or reliable feature
520 when implemented on the variety of customized networks that exist in
521 enterprise computing.
522 </para>
523
524 <para>
525 Like Windows, Samba implements Opportunistic Locking as a server-side
526 component of the client caching mechanism.  Because of the lightweight
527 nature of the Windows feature design, effective configuration of
528 Opportunistic Locking requires a good understanding of its limitations,
529 and then applying that understanding when configuring data access for
530 each particular customized network and client usage state.
531 </para>
532
533 <para>
534 Opportunistic locking essentially means that the client is allowed to download and cache
535 a file on their hard drive while making changes; if a second client wants to access the
536 file, the first client receives a break and must synchronise the file back to the server.
537 This can give significant performance gains in some cases; some programs insist on
538 synchronising the contents of the entire file back to the server for a single change.
539 </para>
540
541 <para>
542 Level1 Oplocks (aka just plain "oplocks") is another term for opportunistic locking.
543 </para>
544
545 <para>
546 Level2 Oplocks provids opportunistic locking for a file that will be treated as
547 <emphasis>read only</emphasis>. Typically this is used on files that are read-only or
548 on files that the client has no initial intention to write to at time of opening the file.
549 </para>
550
551 <para>
552 Kernel Oplocks are essentially a method that allows the Linux kernel to co-exist with
553 Samba's oplocked files, although this has provided better integration of MS Windows network
554 file locking with the under lying OS, SGI IRIX and Linux are the only two OS's that are
555 oplock aware at this time.
556 </para>
557
558 <para>
559 Unless your system supports kernel oplocks, you should disable oplocks if you are
560 accessing the same files from both Unix/Linux and SMB clients. Regardless, oplocks should
561 always be disabled if you are sharing a database file (e.g., Microsoft Access) between
562 multiple clients, as any break the first client receives will affect synchronisation of
563 the entire file (not just the single record), which will result in a noticable performance
564 impairment and, more likely, problems accessing the database in the first place. Notably,
565 Microsoft Outlook's personal folders (*.pst) react very badly to oplocks. If in doubt,
566 disable oplocks and tune your system from that point.
567 </para>
568
569 <para>
570 If client-side caching is desirable and reliable on your network, you will benefit from
571 turning on oplocks. If your network is slow and/or unreliable, or you are sharing your
572 files among other file sharing mechanisms (e.g., NFS) or across a WAN, or multiple people
573 will be accessing the same files frequently, you probably will not benefit from the overhead
574 of your client sending oplock breaks and will instead want to disable oplocks for the share.
575 </para>
576
577 <para>
578 Another factor to consider is the perceived performance of file access. If oplocks provide no
579 measurable speed benefit on your network, it might not be worth the hassle of dealing with them.
580 </para>
581
582 <sect2>
583 <title>Example Configuration</title>
584
585 <para>
586 In the following we examine two destinct aspects of samba locking controls.
587 </para>
588
589 <sect3>
590 <title>Disabling Oplocks</title>
591
592 <para>
593 You can disable oplocks on a per-share basis with the following:
594 </para>
595
596 <para>
597 <programlisting>
598 [acctdata]
599         oplocks = False
600         level2 oplocks = False
601 </programlisting>
602 </para>
603
604 <para>
605 The default oplock type is Level1. Level2 Oplocks are enabled on a per-share basis
606 in the &smb.conf; file.
607 </para>
608
609 <para>
610 Alternately, you could disable oplocks on a per-file basis within the share:
611 </para>
612
613 <para>
614 <programlisting>
615         veto oplock files = /*.mdb/*.MDB/*.dbf/*.DBF/
616 </programlisting>
617 </para>
618
619 <para>
620 If you are experiencing problems with oplocks as apparent from Samba's log entries,
621 you may want to play it safe and disable oplocks and level2 oplocks.
622 </para>
623
624 </sect3>
625
626 <sect3>
627 <title>Disabling Kernel OpLocks</title>
628
629 <para>
630 Kernel OpLocks is an &smb.conf; parameter that notifies Samba (if
631 the UNIX kernel has the capability to send a Windows client an oplock
632 break) when a UNIX process is attempting to open the file that is
633 cached.  This parameter addresses sharing files between UNIX and
634 Windows with Oplocks enabled on the Samba server: the UNIX process
635 can open the file that is Oplocked (cached) by the Windows client and
636 the smbd process will not send an oplock break, which exposes the file
637 to the risk of data corruption.  If the UNIX kernel has the ability to
638 send an oplock break, then the kernel oplocks parameter enables Samba
639 to send the oplock break.  Kernel oplocks are enabled on a per-server
640 basis in the &smb.conf; file.
641 </para>
642
643 <para>
644 <programlisting>
645 [global]
646 kernel oplocks = yes
647 </programlisting>
648 The default is "no".
649 </para>
650
651 <para>
652 Veto OpLocks is an &smb.conf; parameter that identifies specific files for
653 which Oplocks are disabled.  When a Windows client opens a file that
654 has been configured for veto oplocks, the client will not be granted
655 the oplock, and all operations will be executed on the original file on
656 disk instead of a client-cached file copy.  By explicitly identifying
657 files that are shared with UNIX processes, and disabling oplocks for
658 those files, the server-wide Oplock configuration can be enabled to
659 allow Windows clients to utilize the performance benefit of file
660 caching without the risk of data corruption.  Veto Oplocks can be
661 enabled on a per-share basis, or globally for the entire server, in the
662 &smb.conf; file:
663 </para>
664
665 <para>
666 <programlisting><title>Example Veto OpLock Settings</title>
667 [global]
668         veto oplock files = /filename.htm/*.txt/
669
670 [share_name]
671         veto oplock files = /*.exe/filename.ext/
672 </programlisting>
673 </para>
674
675 <para>
676 <emphasis>Oplock break wait time</emphasis> is an &smb.conf; parameter that adjusts the time
677 interval for Samba to reply to an oplock break request.  Samba
678 recommends "DO NOT CHANGE THIS PARAMETER UNLESS YOU HAVE READ AND
679 UNDERSTOOD THE SAMBA OPLOCK CODE."  Oplock Break Wait Time can only be
680 configured globally in the &smb.conf; file:
681 </para>
682
683 <para>
684 <programlisting>
685 [global]
686           oplock break wait time =  0 (default)
687 </programlisting>
688 </para>
689
690 <para>
691 <emphasis>Oplock break contention limit</emphasis> is an &smb.conf; parameter that limits the
692 response of the Samba server to grant an oplock if the configured
693 number of contending clients reaches the limit specified by the
694 parameter.  Samba recommends "DO NOT CHANGE THIS PARAMETER UNLESS YOU
695 HAVE READ AND UNDERSTOOD THE SAMBA OPLOCK CODE."  Oplock Break
696 Contention Limit can be enable on a per-share basis, or globally for
697 the entire server, in the &smb.conf; file:
698 </para>
699
700 <para>
701 <programlisting>
702 [global]
703           oplock break contention limit =  2 (default)
704
705 [share_name]
706          oplock break contention limit =  2 (default)
707 </programlisting>
708 </para>
709
710 </sect3>
711 </sect2>
712
713 </sect1>
714
715 <sect1>
716 <title>MS Windows Opportunistic Locking and Caching Controls</title>
717
718 <para>
719 There is a known issue when running applications (like Norton Anti-Virus) on a Windows 2000/ XP
720 workstation computer that can affect any application attempting to access shared database files
721 across a network. This is a result of a default setting configured in the Windows 2000/XP
722 operating system known as <emphasis>Opportunistic Locking</emphasis>. When a workstation
723 attempts to access shared data files located on another Windows 2000/XP computer,
724 the Windows 2000/XP operating system will attempt to increase performance by locking the
725 files and caching information locally. When this occurs, the application is unable to
726 properly function, which results in an <errorname>Access Denied</errorname>
727  error message being displayed during network operations.
728 </para>
729
730 <para>
731 All Windows operating systems in the NT family that act as database servers for data files
732 (meaning that data files are stored there and accessed by other Windows PCs) may need to
733 have opportunistic locking disabled in order to minimize the risk of data file corruption.
734 This includes Windows 9x/Me, Windows NT, Windows 200x and Windows XP.
735 </para>
736
737 <para>
738 If you are using a Windows NT family workstation in place of a server, you must also
739 disable opportunistic locking (oplocks) on that workstation. For example, if you use a
740 PC with the Windows NT Workstation operating system instead of Windows NT Server, and you
741 have data files located on it that are accessed from other Windows PCs, you may need to
742 disable oplocks on that system.
743 </para>
744
745 <para>
746 The major difference is the location in the Windows registry where the values for disabling
747 oplocks are entered. Instead of the LanManServer location, the LanManWorkstation location
748 may be used.
749 </para>
750
751 <para>
752 You can verify (or change or add, if necessary) this Registry value using the Windows
753 Registry Editor. When you change this registry value, you will have to reboot the PC
754 to ensure that the new setting goes into effect.
755 </para>
756
757 <para>
758 The location of the client registry entry for opportunistic locking has changed in
759 Windows 2000 from the earlier location in Microsoft Windows NT.
760 </para>
761
762 <note><para>
763 Windows 2000 will still respect the EnableOplocks registry value used to disable oplocks
764 in earlier versions of Windows.
765 </para></note>
766
767 <para>
768 You can also deny the granting of opportunistic locks by changing the following registry entries:
769 </para>
770
771 <para>
772 <programlisting>
773         HKEY_LOCAL_MACHINE\System\
774                 CurrentControlSet\Services\MRXSmb\Parameters\
775
776                 OplocksDisabled REG_DWORD 0 or 1
777                 Default: 0 (not disabled)
778 </programlisting>
779 </para>
780
781 <note><para>
782 The OplocksDisabled registry value configures Windows clients to either request or not
783 request opportunistic locks on a remote file. To disable oplocks, the value of
784  OplocksDisabled must be set to 1.
785 </para></note>
786
787 <para>
788 <programlisting>
789         HKEY_LOCAL_MACHINE\System\
790                 CurrentControlSet\Services\LanmanServer\Parameters
791
792                 EnableOplocks REG_DWORD 0 or 1
793                 Default: 1 (Enabled by Default)
794
795                 EnableOpLockForceClose REG_DWORD 0 or 1
796                 Default: 0 (Disabled by Default)
797 </programlisting>
798 </para>
799
800 <note><para>
801 The EnableOplocks value configures Windows-based servers (including Workstations sharing
802 files) to allow or deny opportunistic locks on local files.
803 </para></note>
804
805 <para>
806 To force closure of open oplocks on close or program exit EnableOpLockForceClose must be set to 1.
807 </para>
808
809 <para>
810 An illustration of how level II oplocks work:
811 </para>
812
813 <itemizedlist>
814         <listitem><para>
815         Station 1 opens the file, requesting oplock.
816         </para></listitem>
817         <listitem><para>
818         Since no other station has the file open, the server grants station 1 exclusive oplock.
819         </para></listitem>
820         <listitem><para>
821         Station 2 opens the file, requesting oplock.
822         </para></listitem>
823         <listitem><para>
824         Since station 1 has not yet written to the file, the server asks station 1 to Break
825         to Level II Oplock.
826         </para></listitem>
827         <listitem><para>
828         Station 1 complies by flushing locally buffered lock information to the server.
829         </para></listitem>
830         <listitem><para>
831         Station 1 informs the server that it has Broken to Level II Oplock (alternatively,
832         station 1 could have closed the file).
833         </para></listitem>
834         <listitem><para>
835         The server responds to station 2's open request, granting it level II oplock.
836         Other stations can likewise open the file and obtain level II oplock.
837         </para></listitem>
838         <listitem><para>
839         Station 2 (or any station that has the file open) sends a write request SMB.
840         The server returns the write response.
841         </para></listitem>
842         <listitem><para>
843         The server asks all stations that have the file open to Break to None, meaning no
844         station holds any oplock on the file. Because the workstations can have no cached
845         writes or locks at this point, they need not respond to the break-to-none advisory;
846         all they need do is invalidate locally cashed read-ahead data.
847         </para></listitem>
848 </itemizedlist>
849
850 <sect2>
851 <title>Workstation Service Entries</title>
852
853 <para><programlisting>
854         \HKEY_LOCAL_MACHINE\System\
855                 CurrentControlSet\Services\LanmanWorkstation\Parameters
856
857         UseOpportunisticLocking   REG_DWORD   0 or 1
858         Default: 1 (true)
859 </programlisting></para>
860
861 <para>
862 Indicates whether the redirector should use opportunistic-locking (oplock) performance
863 enhancement. This parameter should be disabled only to isolate problems.
864 </para>
865
866 </sect2>
867 <sect2>
868 <title>Server Service Entries</title>
869
870 <para><programlisting>
871         \HKEY_LOCAL_MACHINE\System\
872                 CurrentControlSet\Services\LanmanServer\Parameters
873
874         EnableOplocks   REG_DWORD   0 or 1
875         Default: 1 (true)
876 </programlisting></para>
877
878 <para>
879 Specifies whether the server allows clients to use oplocks on files. Oplocks are a
880 significant performance enhancement, but have the potential to cause lost cached
881 data on some networks, particularly wide-area networks.
882 </para>
883
884 <para><programlisting>
885         MinLinkThroughput   REG_DWORD   0 to infinite bytes per second
886         Default: 0
887 </programlisting></para>
888
889 <para>
890 Specifies the minimum link throughput allowed by the server before it disables
891 raw and opportunistic locks for this connection.
892 </para>
893
894 <para><programlisting>
895         MaxLinkDelay   REG_DWORD   0 to 100,000 seconds
896         Default: 60
897 </programlisting></para>
898
899 <para>
900 Specifies the maximum time allowed for a link delay. If delays exceed this number,
901 the server disables raw I/O and opportunistic locking for this connection.
902 </para>
903
904 <para><programlisting>
905         OplockBreakWait   REG_DWORD   10 to 180 seconds
906         Default: 35
907 </programlisting></para>
908
909 <para>
910 Specifies the time that the server waits for a client to respond to an oplock break
911 request. Smaller values can allow detection of crashed clients more quickly but can
912 potentially cause loss of cached data.
913 </para>
914
915 </sect2>
916 </sect1>
917
918 <sect1>
919 <title>Persistent Data Corruption</title>
920
921 <para>
922 If you have applied all of the settings discussed in this paper but data corruption problems
923 and other symptoms persist, here are some additional things to check out:
924 </para>
925
926 <para>
927 We have credible reports from developers that faulty network hardware, such as a single
928 faulty network card, can cause symptoms similar to read caching and data corruption.
929 If you see persistent data corruption even after repeated reindexing, you may have to
930 rebuild the data files in question. This involves creating a new data file with the
931 same definition as the file to be rebuilt and transferring the data from the old file
932 to the new one. There are several known methods for doing this that can be found in
933 our Knowledge Base.
934 </para>
935
936 </sect1>
937
938 <sect1>
939 <title>Common Errors</title>
940
941 <para>
942 In some sites locking problems surface as soon as a server is installed, in other sites
943 locking problems may not surface for a long time. Almost without exeception, when a locking
944 problem does surface it will cause embarassment and potential data corruption.
945 </para>
946
947 <para>
948 Over the past few years there have been a number of complaints on the samba mailing lists
949 that have claimed that samba caused data corruption. Three causes have been identified
950 so far:
951 </para>
952
953 <itemizedlist>
954         <listitem><para>
955         Incorrect configuration of opportunistic locking (incompatible with the application
956         being used. This is a VERY common problem even where MS Windows NT4 or MS Windows 200x
957         based servers were in use. It is imperative that the software application vendors'
958         instructions for configuration of file locking should be followed. If in doubt,
959         disable oplocks on both the server and the client. Disabling of all forms of file
960         caching on the MS Windows client may be necessary also.
961         </para></listitem>
962
963         <listitem><para>
964         Defective network cards, cables, or HUBs / Switched. This is generally a more
965         prevalent factor with low cost networking hardware, though occasionally there
966         have been problems with incompatibilities in more up market hardware also.
967         </para></listitem>
968
969         <listitem><para>
970         There have been some random reports of samba log files being written over data
971         files. This has been reported by very few sites (about 5 in the past 3 years)
972         and all attempts to reproduce the problem have failed. The Samba-Team has been
973         unable to catch this happening and thus has NOT been able to isolate any particular
974         cause. Considering the millions of systems that use samba, for the sites that have
975         been affected by this as well as for the Samba-Team this is a frustrating and
976         a vexing challenge. If you see this type of thing happening please create a bug
977         report on https://bugzilla.samba.org without delay. Make sure that you give as much
978         information as you possibly can to help isolate the cause and to allow reproduction
979         of the problem (an essential step in problem isolation and correction).
980         </para></listitem>
981 </itemizedlist>
982
983         <sect2>
984         <title>locking.tdb error messages</title>
985
986         <para>
987         <screen>
988         > We are seeing lots of errors in the samba logs like:
989         >
990         >    tdb(/usr/local/samba_2.2.7/var/locks/locking.tdb): rec_read bad magic
991         > 0x4d6f4b61 at offset=36116
992         >
993         > What do these mean?
994         </screen>
995         </para>
996
997         <para>
998         Corrupted tdb.  Stop all instancesd of smbd, delete locking.tdb, restart smbd.
999         </para>
1000
1001         </sect2>
1002
1003 </sect1>
1004
1005 <sect1>
1006 <title>Additional Reading</title>
1007
1008 <para>
1009 You may want to check for an updated version of this white paper on our Web site from
1010 time to time. Many of our white papers are updated as information changes. For those papers,
1011 the Last Edited date is always at the top of the paper.
1012 </para>
1013
1014 <para>
1015 Section of the Microsoft MSDN Library on opportunistic locking: 
1016 </para>
1017
1018 <para>
1019 Opportunistic Locks, Microsoft Developer Network (MSDN), Windows Development &gt;
1020 Windows Base Services &gt; Files and I/O &gt; SDK Documentation &gt; File Storage &gt; File Systems
1021 &gt; About File Systems &gt; Opportunistic Locks, Microsoft Corporation.
1022 <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>
1023 </para>
1024
1025 <para>
1026 Microsoft Knowledge Base Article Q224992 "Maintaining Transactional Integrity with OPLOCKS",
1027 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>.
1028 </para>
1029
1030 <para>
1031 Microsoft Knowledge Base Article Q296264 "Configuring Opportunistic Locking in Windows 2000",
1032 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>.
1033 </para>
1034
1035 <para>
1036 Microsoft Knowledge Base Article Q129202 "PC Ext: Explanation of Opportunistic Locking on Windows NT",
1037  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>.
1038 </para>
1039
1040 </sect1>
1041 </chapter>