extern int DEBUGLEVEL;
-/* the only restriction is that this must be less than PIPE_HANDLE_OFFSET */
-#define MAX_FNUMS 4096
+static int real_max_open_files;
-#define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < MAX_FNUMS))
+#define VALID_FNUM(fnum) (((fnum) >= 0) && ((fnum) < real_max_open_files))
#define FILE_HANDLE_OFFSET 0x1000
static struct bitmap *file_bmap;
-static struct bitmap *fd_bmap;
static files_struct *Files;
-
+
/* a fsp to use when chaining */
static files_struct *chain_fsp = NULL;
/* a fsp to use to save when breaking an oplock. */
increases the chance that the errant client will get an error rather
than causing corruption */
if (first_file == 0) {
- first_file = (getpid() ^ (int)time(NULL)) % MAX_FNUMS;
+ first_file = (getpid() ^ (int)time(NULL)) % real_max_open_files;
}
i = bitmap_find(file_bmap, first_file);
ZERO_STRUCTP(fsp);
- first_file = (i+1) % MAX_FNUMS;
+ first_file = (i+1) % real_max_open_files;
bitmap_set(file_bmap, i);
files_used++;
DLIST_ADD(Files, fsp);
- DEBUG(5,("allocated file structure %d (%d used)\n",
- i, files_used));
+ DEBUG(5,("allocated file structure %d, fnum = %d (%d used)\n",
+ i, fsp->fnum, files_used));
chain_fsp = fsp;
(sbuf->st_dev == fd_ptr->dev) &&
(sbuf->st_ino == fd_ptr->inode)) {
fd_ptr->ref_count++;
-#ifdef LARGE_SMB_INO_T
+
DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %.0f, ref_count = %d\n",
(unsigned int)fd_ptr->dev, (double)fd_ptr->inode,
fd_ptr->ref_count));
-#else /* LARGE_SMB_INO_T */
- DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %x, ref_count = %d\n",
- (unsigned int)fd_ptr->dev,
- (unsigned int)fd_ptr->inode,
- fd_ptr->ref_count));
-#endif /* LARGE_SMB_INO_T */
+
return fd_ptr;
}
}
file_fd_struct *fd_get_new(void)
{
extern struct current_user current_user;
- int i;
file_fd_struct *fd_ptr;
- i = bitmap_find(fd_bmap, 1);
- if (i == -1) {
- DEBUG(0,("ERROR! Out of file_fd structures\n"));
- return NULL;
- }
-
fd_ptr = (file_fd_struct *)malloc(sizeof(*fd_ptr));
- if (!fd_ptr) return NULL;
+ if (!fd_ptr) {
+ DEBUG(0,("ERROR! malloc fail for file_fd struct.\n"));
+ return NULL;
+ }
ZERO_STRUCTP(fd_ptr);
- fd_ptr->fdnum = i;
fd_ptr->dev = (SMB_DEV_T)-1;
fd_ptr->inode = (SMB_INO_T)-1;
fd_ptr->fd = -1;
fd_add_to_uid_cache(fd_ptr, (uid_t)current_user.uid);
fd_ptr->ref_count++;
- bitmap_set(fd_bmap, i);
fd_ptr_used++;
DLIST_ADD(FileFd, fd_ptr);
- DEBUG(5,("allocated fd_ptr structure %d (%d used)\n",
- i, fd_ptr_used));
+ DEBUG(5,("allocated fd_ptr structure (%d used)\n", fd_ptr_used));
return fd_ptr;
}
for (fsp=Files;fsp;fsp=next) {
next = fsp->next;
if (fsp->conn == conn && fsp->open) {
- if (fsp->is_directory)
- close_directory(fsp);
- else
- close_file(fsp,False);
+ close_file(fsp,False);
}
}
}
/****************************************************************************
initialise file structures
****************************************************************************/
+
+#define MAX_OPEN_FUDGEFACTOR 10
+
void file_init(void)
{
- file_bmap = bitmap_allocate(MAX_FNUMS);
- fd_bmap = bitmap_allocate(MAX_FNUMS);
+ int request_max_open_files = lp_max_open_files();
+ int real_lim;
- if (!file_bmap || !fd_bmap) {
- exit_server("out of memory in file_init");
+ /*
+ * Set the max_open files to be the requested
+ * max plus a fudgefactor to allow for the extra
+ * fd's we need such as log files etc...
+ */
+ real_lim = set_maxfiles(request_max_open_files + MAX_OPEN_FUDGEFACTOR);
+
+ real_max_open_files = real_lim - MAX_OPEN_FUDGEFACTOR;
+
+ if(real_max_open_files != request_max_open_files) {
+ DEBUG(1,("file_init: Information only: requested %d \
+open files, %d are available.\n", request_max_open_files, real_max_open_files));
}
-#if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
- {
- struct rlimit rlp;
- getrlimit(RLIMIT_NOFILE, &rlp);
- /* Set the fd limit to be MAX_FNUMS + 10 to
- * account for the extra fd we need
- * as well as the log files and standard
- * handles etc. */
- rlp.rlim_cur = (MAX_FNUMS+10>rlp.rlim_max)?
- rlp.rlim_max:MAX_FNUMS+10;
- setrlimit(RLIMIT_NOFILE, &rlp);
- getrlimit(RLIMIT_NOFILE, &rlp);
- DEBUG(3,("Maximum number of open files per session is %d\n",
- (int)rlp.rlim_cur));
+ file_bmap = bitmap_allocate(real_max_open_files);
+
+ if (!file_bmap) {
+ exit_server("out of memory in file_init");
}
-#endif
+
+ /*
+ * Ensure that pipe_handle_oppset is set correctly.
+ */
+ set_pipe_handle_offset(real_max_open_files);
}
for (fsp=Files;fsp;fsp=next) {
next=fsp->next;
if ((fsp->vuid == vuid) && fsp->open) {
- if(!fsp->is_directory)
- close_file(fsp,False);
- else
- close_directory(fsp);
+ close_file(fsp,False);
}
}
}
/****************************************************************************
-find a fsp given a device, inode and timevalue
+ Find a fsp given a device, inode and timevalue
+ If this is from a kernel oplock break request then tval may be NULL.
****************************************************************************/
+
files_struct *file_find_dit(SMB_DEV_T dev, SMB_INO_T inode, struct timeval *tval)
{
int count=0;
for (fsp=Files;fsp;fsp=fsp->next,count++) {
if (fsp->open &&
+ fsp->fd_ptr != NULL &&
fsp->fd_ptr->dev == dev &&
fsp->fd_ptr->inode == inode &&
- fsp->open_time.tv_sec == tval->tv_sec &&
- fsp->open_time.tv_usec == tval->tv_usec) {
+ (tval ? (fsp->open_time.tv_sec == tval->tv_sec) : True ) &&
+ (tval ? (fsp->open_time.tv_usec == tval->tv_usec) : True )) {
if (count > 10) {
DLIST_PROMOTE(Files, fsp);
}
return NULL;
}
+/****************************************************************************
+ Find the first fsp given a device and inode.
+****************************************************************************/
+
+files_struct *file_find_di_first(SMB_DEV_T dev, SMB_INO_T inode)
+{
+ files_struct *fsp;
+
+ for (fsp=Files;fsp;fsp=fsp->next) {
+ if (fsp->open &&
+ fsp->fd_ptr != NULL &&
+ fsp->fd_ptr->dev == dev &&
+ fsp->fd_ptr->inode == inode )
+ return fsp;
+ }
+
+ return NULL;
+}
+
+/****************************************************************************
+ Find the next fsp having the same device and inode.
+****************************************************************************/
+
+files_struct *file_find_di_next(files_struct *start_fsp)
+{
+ files_struct *fsp;
+
+ for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
+ if (fsp->open &&
+ fsp->fd_ptr != NULL &&
+ fsp->fd_ptr->dev == start_fsp->fd_ptr->dev &&
+ fsp->fd_ptr->inode == start_fsp->fd_ptr->inode )
+ return fsp;
+ }
+
+ return NULL;
+}
/****************************************************************************
find a fsp that is open for printing
for (fsp=Files;fsp;fsp=fsp->next) {
if (fsp->open && fsp->print_file) return fsp;
}
+
return NULL;
}
for (fsp=Files;fsp;fsp=next) {
next=fsp->next;
- if (fsp->open && conn == fsp->conn) {
+ if (fsp->open && (conn == fsp->conn) && (fsp->fd_ptr != NULL)) {
sync_file(conn,fsp);
}
}
{
DLIST_REMOVE(FileFd, fd_ptr);
- bitmap_clear(fd_bmap, fd_ptr->fdnum);
fd_ptr_used--;
- DEBUG(5,("freed fd_ptr structure %d (%d used)\n",
- fd_ptr->fdnum, fd_ptr_used));
+ DEBUG(5,("freed fd_ptr structure (%d used)\n", fd_ptr_used));
/* paranoia */
ZERO_STRUCTP(fd_ptr);
string_free(&fsp->fsp_name);
- if (fsp->fd_ptr && fsp->fd_ptr->ref_count == 0) {
+ if ((fsp->fd_ptr != NULL) && fsp->fd_ptr->ref_count == 0) {
fd_ptr_free(fsp->fd_ptr);
}
return fsp;
}
}
-
return NULL;
}