root/fs/file_table.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. insert_file_free
  2. remove_file_free
  3. put_last_free
  4. grow_files
  5. file_table_init
  6. get_empty_filp
  7. add_dquot_ref
  8. reset_dquot_ptrs

   1 /*
   2  *  linux/fs/file_table.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <linux/config.h>
   8 #include <linux/fs.h>
   9 #include <linux/string.h>
  10 #include <linux/mm.h>
  11 
  12 /*
  13  * first_file points to a doubly linked list of all file structures in
  14  *            the system.
  15  * nr_files   holds the length of this list.
  16  */
  17 struct file * first_file = NULL;
  18 int nr_files = 0;
  19 
  20 /*
  21  * Insert a new file structure at the head of the list of available ones.
  22  */
  23 static inline void insert_file_free(struct file *file)
     /* [previous][next][first][last][top][bottom][index][help] */
  24 {
  25         file->f_count = 0;
  26         file->f_next = first_file;
  27         file->f_prev = first_file->f_prev;
  28         file->f_next->f_prev = file;
  29         file->f_prev->f_next = file;
  30         first_file = file;
  31 }
  32 
  33 /*
  34  * Remove a file structure from the list of available ones.
  35  */
  36 static inline void remove_file_free(struct file *file)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38         if (first_file == file)
  39                 first_file = first_file->f_next;
  40         file->f_next->f_prev = file->f_prev;
  41         file->f_prev->f_next = file->f_next;
  42         file->f_next = file->f_prev = NULL;
  43 }
  44 
  45 /*
  46  * Insert a file structure at the end of the list of available ones.
  47  */
  48 static inline void put_last_free(struct file *file)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50         file->f_prev = first_file->f_prev;
  51         file->f_prev->f_next = file;
  52         file->f_next = first_file;
  53         file->f_next->f_prev = file;
  54 }
  55 
  56 /*
  57  * Allocate a new memory page for file structures and
  58  * insert the new structures into the global list.
  59  * Returns 0, if there is no more memory, 1 otherwise.
  60  */
  61 static int grow_files(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63         struct file * file;
  64         int i;
  65 
  66         /*
  67          * We don't have to clear the page because we only look into
  68          * f_count, f_prev and f_next and they get initialized in
  69          * insert_file_free.  The rest of the file structure is cleared
  70          * by get_empty_filp before it is returned.
  71          */
  72         file = (struct file *) __get_free_page(GFP_KERNEL);
  73 
  74         if (!file)
  75                 return 0;
  76 
  77         nr_files += i = PAGE_SIZE/sizeof(struct file);
  78 
  79         if (!first_file)
  80                 file->f_count = 0,
  81                 file->f_next = file->f_prev = first_file = file++,
  82                 i--;
  83 
  84         for (; i ; i--)
  85                 insert_file_free(file++);
  86 
  87         return 1;
  88 }
  89 
  90 unsigned long file_table_init(unsigned long start, unsigned long end)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         return start;
  93 }
  94 
  95 /*
  96  * Find an unused file structure and return a pointer to it.
  97  * Returns NULL, if there are no more free file structures or
  98  * we run out of memory.
  99  */
 100 struct file * get_empty_filp(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 101 {
 102         int i;
 103         struct file * f;
 104 
 105         /* if the return is taken, we are in deep trouble */
 106         if (!first_file && !grow_files())
 107                 return NULL;
 108 
 109         do {
 110                 for (f = first_file, i=0; i < nr_files; i++, f = f->f_next)
 111                         if (!f->f_count) {
 112                                 remove_file_free(f);
 113                                 memset(f,0,sizeof(*f));
 114                                 put_last_free(f);
 115                                 f->f_count = 1;
 116                                 f->f_version = ++event;
 117                                 return f;
 118                         }
 119         } while (nr_files < NR_FILE && grow_files());
 120 
 121         return NULL;
 122 }
 123 
 124 #ifdef CONFIG_QUOTA
 125 
 126 void add_dquot_ref(dev_t dev, short type)
     /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128         struct file *filp;
 129         int cnt;
 130 
 131         for (filp = first_file, cnt = 0; cnt < nr_files; cnt++, filp = filp->f_next) {
 132                 if (!filp->f_count || !filp->f_inode || filp->f_inode->i_dev != dev)
 133                         continue;
 134                 if (filp->f_mode & FMODE_WRITE && filp->f_inode->i_sb->dq_op) {
 135                         filp->f_inode->i_sb->dq_op->initialize(filp->f_inode, type);
 136                         filp->f_inode->i_flags |= S_WRITE;
 137                 }
 138         }
 139 }
 140 
 141 void reset_dquot_ptrs(dev_t dev, short type)
     /* [previous][next][first][last][top][bottom][index][help] */
 142 {
 143         struct file *filp;
 144         int cnt;
 145 
 146         for (filp = first_file, cnt = 0; cnt < nr_files; cnt++, filp = filp->f_next) {
 147                 if (!filp->f_count || !filp->f_inode || filp->f_inode->i_dev != dev)
 148                         continue;
 149                 if (IS_WRITABLE(filp->f_inode)) {
 150                         filp->f_inode->i_dquot[type] = NODQUOT;
 151                         filp->f_inode->i_flags &= ~S_WRITE;
 152                 }
 153         }
 154 }
 155 
 156 #endif

/* [previous][next][first][last][top][bottom][index][help] */