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