r9985: Move the all the strict sync logic into file_sync().
[kai/samba.git] / source3 / smbd / files.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Files[] structure handling
4    Copyright (C) Andrew Tridgell 1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 static int real_max_open_files;
24
25 #define VALID_FNUM(fnum)   (((fnum) >= 0) && ((fnum) < real_max_open_files))
26
27 #define FILE_HANDLE_OFFSET 0x1000
28
29 static struct bitmap *file_bmap;
30
31 static files_struct *Files;
32  
33 /* a fsp to use when chaining */
34 static files_struct *chain_fsp = NULL;
35 /* a fsp to use to save when breaking an oplock. */
36 static files_struct *oplock_save_chain_fsp = NULL;
37
38 static int files_used;
39
40 /* A singleton cache to speed up searching by dev/inode. */
41 static struct fsp_singleton_cache {
42         files_struct *fsp;
43         SMB_DEV_T dev;
44         SMB_INO_T inode;
45 } fsp_fi_cache;
46
47 /****************************************************************************
48  Return a unique number identifying this fsp over the life of this pid.
49 ****************************************************************************/
50
51 static unsigned long get_gen_count(void)
52 {
53         static unsigned long file_gen_counter;
54
55         if ((++file_gen_counter) == 0)
56                 return ++file_gen_counter;
57         return file_gen_counter;
58 }
59
60 /****************************************************************************
61  Find first available file slot.
62 ****************************************************************************/
63
64 files_struct *file_new(connection_struct *conn)
65 {
66         int i;
67         static int first_file;
68         files_struct *fsp, *next;
69
70         /* we want to give out file handles differently on each new
71            connection because of a common bug in MS clients where they try to
72            reuse a file descriptor from an earlier smb connection. This code
73            increases the chance that the errant client will get an error rather
74            than causing corruption */
75         if (first_file == 0) {
76                 first_file = (sys_getpid() ^ (int)time(NULL)) % real_max_open_files;
77         }
78
79         i = bitmap_find(file_bmap, first_file);
80         if (i == -1) {
81                 /* 
82                  * Before we give up, go through the open files 
83                  * and see if there are any files opened with a
84                  * batch oplock. If so break the oplock and then
85                  * re-use that entry (if it becomes closed).
86                  * This may help as NT/95 clients tend to keep
87                  * files batch oplocked for quite a long time
88                  * after they have finished with them.
89                  */
90                 for (fsp=Files;fsp;fsp=next) {
91                         next=fsp->next;
92                         if (attempt_close_oplocked_file(fsp)) {
93                                 return file_new(conn);
94                         }
95                 }
96
97                 DEBUG(0,("ERROR! Out of file structures\n"));
98                 set_saved_error_triple(ERRSRV, ERRnofids, NT_STATUS_TOO_MANY_OPENED_FILES);
99                 return NULL;
100         }
101
102         fsp = SMB_MALLOC_P(files_struct);
103         if (!fsp) {
104                 set_saved_error_triple(ERRDOS, ERRnomem, NT_STATUS_NO_MEMORY);
105                 return NULL;
106         }
107
108         ZERO_STRUCTP(fsp);
109
110         fsp->fh = SMB_MALLOC_P(struct fd_handle);
111         if (!fsp->fh) {
112                 SAFE_FREE(fsp);
113                 set_saved_error_triple(ERRDOS, ERRnomem, NT_STATUS_NO_MEMORY);
114                 return NULL;
115         }
116
117         ZERO_STRUCTP(fsp->fh);
118
119         fsp->fh->ref_count = 1;
120         fsp->fh->fd = -1;
121
122         fsp->conn = conn;
123         fsp->file_id = get_gen_count();
124         GetTimeOfDay(&fsp->open_time);
125
126         first_file = (i+1) % real_max_open_files;
127
128         bitmap_set(file_bmap, i);
129         files_used++;
130
131         fsp->fnum = i + FILE_HANDLE_OFFSET;
132         SMB_ASSERT(fsp->fnum < 65536);
133
134         string_set(&fsp->fsp_name,"");
135         
136         DLIST_ADD(Files, fsp);
137
138         DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
139                  i, fsp->fnum, files_used));
140
141         chain_fsp = fsp;
142
143         /* A new fsp invalidates a negative fsp_fi_cache. */
144         if (fsp_fi_cache.fsp == NULL) {
145                 ZERO_STRUCT(fsp_fi_cache);
146         }
147         
148         return fsp;
149 }
150
151 /****************************************************************************
152  Close all open files for a connection.
153 ****************************************************************************/
154
155 void file_close_conn(connection_struct *conn)
156 {
157         files_struct *fsp, *next;
158         
159         for (fsp=Files;fsp;fsp=next) {
160                 next = fsp->next;
161                 if (fsp->conn == conn) {
162                         close_file(fsp,False); 
163                 }
164         }
165 }
166
167 /****************************************************************************
168  Close all open files for a pid.
169 ****************************************************************************/
170
171 void file_close_pid(uint16 smbpid)
172 {
173         files_struct *fsp, *next;
174         
175         for (fsp=Files;fsp;fsp=next) {
176                 next = fsp->next;
177                 if (fsp->file_pid == smbpid) {
178                         close_file(fsp,False); 
179                 }
180         }
181 }
182
183 /****************************************************************************
184  Initialise file structures.
185 ****************************************************************************/
186
187 #define MAX_OPEN_FUDGEFACTOR 20
188
189 void file_init(void)
190 {
191         int request_max_open_files = lp_max_open_files();
192         int real_lim;
193
194         /*
195          * Set the max_open files to be the requested
196          * max plus a fudgefactor to allow for the extra
197          * fd's we need such as log files etc...
198          */
199         real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
200
201         real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
202
203         if (real_max_open_files + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536)
204                 real_max_open_files = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
205
206         if(real_max_open_files != request_max_open_files) {
207                 DEBUG(1,("file_init: Information only: requested %d \
208 open files, %d are available.\n", request_max_open_files, real_max_open_files));
209         }
210
211         SMB_ASSERT(real_max_open_files > 100);
212
213         file_bmap = bitmap_allocate(real_max_open_files);
214         
215         if (!file_bmap) {
216                 exit_server("out of memory in file_init");
217         }
218         
219         /*
220          * Ensure that pipe_handle_oppset is set correctly.
221          */
222         set_pipe_handle_offset(real_max_open_files);
223 }
224
225 /****************************************************************************
226  Close files open by a specified vuid.
227 ****************************************************************************/
228
229 void file_close_user(int vuid)
230 {
231         files_struct *fsp, *next;
232
233         for (fsp=Files;fsp;fsp=next) {
234                 next=fsp->next;
235                 if (fsp->vuid == vuid) {
236                         close_file(fsp,False);
237                 }
238         }
239 }
240
241 /****************************************************************************
242  Debug to enumerate all open files in the smbd.
243 ****************************************************************************/
244
245 void file_dump_open_table(void)
246 {
247         int count=0;
248         files_struct *fsp;
249
250         for (fsp=Files;fsp;fsp=fsp->next,count++) {
251                 DEBUG(10,("Files[%d], fnum = %d, name %s, fd = %d, fileid = %lu, dev = %x, inode = %.0f\n",
252                         count, fsp->fnum, fsp->fsp_name, fsp->fh->fd, (unsigned long)fsp->file_id,
253                         (unsigned int)fsp->dev, (double)fsp->inode ));
254         }
255 }
256
257 /****************************************************************************
258  Find a fsp given a file descriptor.
259 ****************************************************************************/
260
261 files_struct *file_find_fd(int fd)
262 {
263         int count=0;
264         files_struct *fsp;
265
266         for (fsp=Files;fsp;fsp=fsp->next,count++) {
267                 if (fsp->fh->fd == fd) {
268                         if (count > 10) {
269                                 DLIST_PROMOTE(Files, fsp);
270                         }
271                         return fsp;
272                 }
273         }
274
275         return NULL;
276 }
277
278 /****************************************************************************
279  Find a fsp given a device, inode and file_id.
280 ****************************************************************************/
281
282 files_struct *file_find_dif(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
283 {
284         int count=0;
285         files_struct *fsp;
286
287         for (fsp=Files;fsp;fsp=fsp->next,count++) {
288                 /* We can have a fsp->fh->fd == -1 here as it could be a stat open. */
289                 if (fsp->dev == dev && 
290                     fsp->inode == inode &&
291                     fsp->file_id == file_id ) {
292                         if (count > 10) {
293                                 DLIST_PROMOTE(Files, fsp);
294                         }
295                         /* Paranoia check. */
296                         if (fsp->fh->fd == -1 && fsp->oplock_type != NO_OPLOCK) {
297                                 DEBUG(0,("file_find_dif: file %s dev = %x, inode = %.0f, file_id = %u \
298 oplock_type = %u is a stat open with oplock type !\n", fsp->fsp_name, (unsigned int)fsp->dev,
299                                                 (double)fsp->inode, (unsigned int)fsp->file_id,
300                                                 (unsigned int)fsp->oplock_type ));
301                                 smb_panic("file_find_dif\n");
302                         }
303                         return fsp;
304                 }
305         }
306
307         return NULL;
308 }
309
310 /****************************************************************************
311  Check if an fsp still exists.
312 ****************************************************************************/
313
314 files_struct *file_find_fsp(files_struct *orig_fsp)
315 {
316         files_struct *fsp;
317
318         for (fsp=Files;fsp;fsp=fsp->next) {
319                 if (fsp == orig_fsp)
320                         return fsp;
321         }
322
323         return NULL;
324 }
325
326 /****************************************************************************
327  Find the first fsp given a device and inode.
328  We use a singleton cache here to speed up searching from getfilepathinfo
329  calls.
330 ****************************************************************************/
331
332 files_struct *file_find_di_first(SMB_DEV_T dev, SMB_INO_T inode)
333 {
334         files_struct *fsp;
335
336         if (fsp_fi_cache.dev == dev && fsp_fi_cache.inode == inode) {
337                 /* Positive or negative cache hit. */
338                 return fsp_fi_cache.fsp;
339         }
340
341         fsp_fi_cache.dev = dev;
342         fsp_fi_cache.inode = inode;
343
344         for (fsp=Files;fsp;fsp=fsp->next) {
345                 if ( fsp->fh->fd != -1 &&
346                                 fsp->dev == dev &&
347                                 fsp->inode == inode ) {
348                         /* Setup positive cache. */
349                         fsp_fi_cache.fsp = fsp;
350                         return fsp;
351                 }
352         }
353
354         /* Setup negative cache. */
355         fsp_fi_cache.fsp = NULL;
356         return NULL;
357 }
358
359 /****************************************************************************
360  Find the next fsp having the same device and inode.
361 ****************************************************************************/
362
363 files_struct *file_find_di_next(files_struct *start_fsp)
364 {
365         files_struct *fsp;
366
367         for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
368                 if ( fsp->fh->fd != -1 &&
369                                 fsp->dev == start_fsp->dev &&
370                                 fsp->inode == start_fsp->inode )
371                         return fsp;
372         }
373
374         return NULL;
375 }
376
377 /****************************************************************************
378  Find a fsp that is open for printing.
379 ****************************************************************************/
380
381 files_struct *file_find_print(void)
382 {
383         files_struct *fsp;
384
385         for (fsp=Files;fsp;fsp=fsp->next) {
386                 if (fsp->print_file) {
387                         return fsp;
388                 }
389         } 
390
391         return NULL;
392 }
393
394 /****************************************************************************
395  Set a pending modtime across all files with a given dev/ino pair.
396  Record the owner of that modtime.
397 ****************************************************************************/
398
399 void fsp_set_pending_modtime(files_struct *tfsp, time_t pmod)
400 {
401         files_struct *fsp;
402
403         if (null_mtime(pmod)) {
404                 return;
405         }
406
407         for (fsp = Files;fsp;fsp=fsp->next) {
408                 if ( fsp->fh->fd != -1 &&
409                                 fsp->dev == tfsp->dev &&
410                                 fsp->inode == tfsp->inode ) {
411                         fsp->pending_modtime = pmod;
412                         fsp->pending_modtime_owner = False;
413                 }
414         }
415
416         tfsp->pending_modtime_owner = True;
417 }
418
419 /****************************************************************************
420  Sync open files on a connection.
421 ****************************************************************************/
422
423 void file_sync_all(connection_struct *conn)
424 {
425         files_struct *fsp, *next;
426
427         for (fsp=Files;fsp;fsp=next) {
428                 next=fsp->next;
429                 if ((conn == fsp->conn) && (fsp->fh->fd != -1)) {
430                         sync_file(conn, fsp, True /* write through */);
431                 }
432         }
433 }
434
435 /****************************************************************************
436  Free up a fsp.
437 ****************************************************************************/
438
439 void file_free(files_struct *fsp)
440 {
441         DLIST_REMOVE(Files, fsp);
442
443         string_free(&fsp->fsp_name);
444
445         if (fsp->fake_file_handle) {
446                 destroy_fake_file_handle(&fsp->fake_file_handle);
447         }
448
449         if (fsp->fh->ref_count == 1) {
450                 SAFE_FREE(fsp->fh);
451         } else {
452                 fsp->fh->ref_count--;
453         }
454
455         bitmap_clear(file_bmap, fsp->fnum - FILE_HANDLE_OFFSET);
456         files_used--;
457
458         DEBUG(5,("freed files structure %d (%d used)\n",
459                  fsp->fnum, files_used));
460
461         /* this is paranoia, just in case someone tries to reuse the 
462            information */
463         ZERO_STRUCTP(fsp);
464
465         if (fsp == chain_fsp) {
466                 chain_fsp = NULL;
467         }
468
469         /* Closing a file can invalidate the positive cache. */
470         if (fsp == fsp_fi_cache.fsp) {
471                 ZERO_STRUCT(fsp_fi_cache);
472         }
473
474         SAFE_FREE(fsp);
475 }
476
477 /****************************************************************************
478  Get a fsp from a packet given the offset of a 16 bit fnum.
479 ****************************************************************************/
480
481 files_struct *file_fsp(char *buf, int where)
482 {
483         int fnum, count=0;
484         files_struct *fsp;
485
486         if (chain_fsp)
487                 return chain_fsp;
488
489         if (!buf)
490                 return NULL;
491         fnum = SVAL(buf, where);
492
493         for (fsp=Files;fsp;fsp=fsp->next, count++) {
494                 if (fsp->fnum == fnum) {
495                         chain_fsp = fsp;
496                         if (count > 10) {
497                                 DLIST_PROMOTE(Files, fsp);
498                         }
499                         return fsp;
500                 }
501         }
502         return NULL;
503 }
504
505 /****************************************************************************
506  Reset the chained fsp - done at the start of a packet reply.
507 ****************************************************************************/
508
509 void file_chain_reset(void)
510 {
511         chain_fsp = NULL;
512 }
513
514 /****************************************************************************
515  Save the chained fsp - done when about to do an oplock break.
516 ****************************************************************************/
517
518 void file_chain_save(void)
519 {
520         oplock_save_chain_fsp = chain_fsp;
521 }
522
523 /****************************************************************************
524  Restore the chained fsp - done after an oplock break.
525 ****************************************************************************/
526
527 void file_chain_restore(void)
528 {
529         chain_fsp = oplock_save_chain_fsp;
530 }
531
532 /****************************************************************************
533  Duplicate the file handle part for a DOS or FCB open.
534 ****************************************************************************/
535
536 files_struct *dup_file_fsp(files_struct *fsp,
537                                 uint32 access_mask,
538                                 uint32 share_access,
539                                 uint32 create_options)
540 {
541         files_struct *dup_fsp = file_new(fsp->conn);
542
543         if (!dup_fsp) {
544                 return NULL;
545         }
546
547         SAFE_FREE(dup_fsp->fh);
548
549         dup_fsp->fh = fsp->fh;
550         dup_fsp->fh->ref_count++;
551
552         dup_fsp->dev = fsp->dev;
553         dup_fsp->inode = fsp->inode;
554         dup_fsp->initial_allocation_size = fsp->initial_allocation_size;
555         dup_fsp->mode = fsp->mode;
556         dup_fsp->file_pid = fsp->file_pid;
557         dup_fsp->vuid = fsp->vuid;
558         dup_fsp->open_time = fsp->open_time;
559         dup_fsp->access_mask = access_mask;
560         dup_fsp->share_access = share_access;
561         dup_fsp->pending_modtime_owner = fsp->pending_modtime_owner;
562         dup_fsp->pending_modtime = fsp->pending_modtime;
563         dup_fsp->last_write_time = fsp->last_write_time;
564         dup_fsp->oplock_type = fsp->oplock_type;
565         dup_fsp->can_lock = fsp->can_lock;
566         dup_fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
567         if (!CAN_WRITE(fsp->conn)) {
568                 dup_fsp->can_write = False;
569         } else {
570                 dup_fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
571         }
572         dup_fsp->print_file = fsp->print_file;
573         dup_fsp->modified = fsp->modified;
574         dup_fsp->is_directory = fsp->is_directory;
575         dup_fsp->is_stat = fsp->is_stat;
576         dup_fsp->aio_write_behind = fsp->aio_write_behind;
577         string_set(&dup_fsp->fsp_name,fsp->fsp_name);
578
579         return dup_fsp;
580 }