/* * Find an unused file structure and return a pointer to it. * Returns NULL, if there are no more free file structures or * we run out of memory. */ struct file * get_empty_filp(void) { int i; int max = max_files; struct file * f; /* * Reserve a few files for the super-user.. */ if (current->euid) max -= 10; /* if the return is taken, we are in deep trouble */ if (!first_file && !grow_files()) return NULL; do { for (f = first_file, i=0; i < nr_files; i++, f = f->f_next) if (!f->f_count) { remove_file_free(f); memset(f,0,sizeof(*f)); put_last_free(f); f->f_count = 1; f->f_version = ++event; return f; } } while (nr_files < max && grow_files()); return NULL; }
static void put_last_free(struct file *file) { remove_file_free(file); file->f_prev = first_file->f_prev; file->f_prev->f_next = file; file->f_next = first_file; file->f_next->f_prev = file; }
struct file * get_empty_filp(void) { int i; struct file * f; if (!first_file) grow_files(); repeat: for (f = first_file, i=0; i < nr_files; i++, f = f->f_next) if (!f->f_count) { remove_file_free(f); memset(f,0,sizeof(*f)); put_last_free(f); f->f_count = 1; return f; } if (nr_files < NR_FILE) { grow_files(); goto repeat; } return NULL; }