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. */
+static files_struct *oplock_save_chain_fsp = NULL;
/*
* Indirection for file fd's. Needed as POSIX locking
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);
fsp = (files_struct *)malloc(sizeof(*fsp));
if (!fsp) return NULL;
- memset(fsp, 0, sizeof(*fsp));
+ ZERO_STRUCTP(fsp);
- first_file = (i+1) % MAX_FNUMS;
+ first_file = (i+1) % real_max_open_files;
bitmap_set(file_bmap, i);
files_used++;
fsp->fnum = i + FILE_HANDLE_OFFSET;
string_init(&fsp->fsp_name,"");
- /* hook into the front of the list */
- if (!Files) {
- Files = fsp;
- } else {
- Files->prev = fsp;
- fsp->next = Files;
- Files = fsp;
- }
+ 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;
fd support routines - attempt to find an already open file by dev
and inode - increments the ref_count of the returned file_fd_struct *.
****************************************************************************/
-file_fd_struct *fd_get_already_open(struct stat *sbuf)
+file_fd_struct *fd_get_already_open(SMB_STRUCT_STAT *sbuf)
{
file_fd_struct *fd_ptr;
for (fd_ptr=FileFd;fd_ptr;fd_ptr=fd_ptr->next) {
if ((fd_ptr->ref_count > 0) &&
- (((uint32)sbuf->st_dev) == fd_ptr->dev) &&
- (((uint32)sbuf->st_ino) == fd_ptr->inode)) {
+ (sbuf->st_dev == fd_ptr->dev) &&
+ (sbuf->st_ino == fd_ptr->inode)) {
fd_ptr->ref_count++;
- DEBUG(3,("Re-used file_fd_struct dev = %x, inode = %x, ref_count = %d\n",
- fd_ptr->dev, fd_ptr->inode,
+
+ 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));
+
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;
+ }
- memset(fd_ptr, 0, sizeof(*fd_ptr));
+ ZERO_STRUCTP(fd_ptr);
- fd_ptr->fdnum = i;
- fd_ptr->dev = (uint32)-1;
- fd_ptr->inode = (uint32)-1;
+ fd_ptr->dev = (SMB_DEV_T)-1;
+ fd_ptr->inode = (SMB_INO_T)-1;
fd_ptr->fd = -1;
fd_ptr->fd_readonly = -1;
fd_ptr->fd_writeonly = -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++;
- /* hook into the front of the list */
- if (!FileFd) {
- FileFd = fd_ptr;
- } else {
- FileFd->prev = fd_ptr;
- fd_ptr->next = FileFd;
- FileFd = fd_ptr;
- }
+ 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(int dev, int inode, struct timeval *tval)
+
+files_struct *file_find_dit(SMB_DEV_T dev, SMB_INO_T inode, struct timeval *tval)
{
+ int count=0;
files_struct *fsp;
- for (fsp=Files;fsp;fsp=fsp->next) {
+ 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 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);
}
}
/****************************************************************************
free up a fd_ptr
****************************************************************************/
-static void fd_ptr_free(file_fd_struct *fd_ptr)
+void fd_ptr_free(file_fd_struct *fd_ptr)
{
- if (fd_ptr == FileFd) {
- FileFd = fd_ptr->next;
- if (FileFd) FileFd->prev = NULL;
- } else {
- fd_ptr->prev->next = fd_ptr->next;
- if (fd_ptr->next) fd_ptr->next->prev = fd_ptr->prev;
- }
+ 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 */
- memset(fd_ptr, 0, sizeof(*fd_ptr));
+ ZERO_STRUCTP(fd_ptr);
free(fd_ptr);
}
****************************************************************************/
void file_free(files_struct *fsp)
{
- if (fsp == Files) {
- Files = fsp->next;
- if (Files) Files->prev = NULL;
- } else {
- fsp->prev->next = fsp->next;
- if (fsp->next) fsp->next->prev = fsp->prev;
- }
+ DLIST_REMOVE(Files, fsp);
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);
}
/* this is paranoia, just in case someone tries to reuse the
information */
- memset(fsp, 0, sizeof(*fsp));
+ ZERO_STRUCTP(fsp);
if (fsp == chain_fsp) chain_fsp = NULL;
****************************************************************************/
files_struct *file_fsp(char *buf, int where)
{
- int fnum;
+ int fnum, count=0;
files_struct *fsp;
if (chain_fsp) return chain_fsp;
fnum = SVAL(buf, where);
- for (fsp=Files;fsp;fsp=fsp->next) {
+ for (fsp=Files;fsp;fsp=fsp->next, count++) {
if (fsp->fnum == fnum) {
chain_fsp = fsp;
+ if (count > 10) {
+ DLIST_PROMOTE(Files, fsp);
+ }
return fsp;
}
}
-
return NULL;
}
-
/****************************************************************************
-reset the chained fsp - done at the start of a packet reply
+ Reset the chained fsp - done at the start of a packet reply
****************************************************************************/
+
void file_chain_reset(void)
{
chain_fsp = NULL;
}
+
+/****************************************************************************
+Save the chained fsp - done when about to do an oplock break.
+****************************************************************************/
+
+void file_chain_save(void)
+{
+ oplock_save_chain_fsp = chain_fsp;
+}
+
+/****************************************************************************
+Restore the chained fsp - done after an oplock break.
+****************************************************************************/
+void file_chain_restore(void)
+{
+ chain_fsp = oplock_save_chain_fsp;
+}