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