"Fossies" - the Fresh Open Source Software Archive

Member "quotactl-1.00/quotause/dquothash.c" (9 Oct 2005, 4530 Bytes) of package /linux/privat/old/quotactl-1.00.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dquothash.c" see the Fossies "Dox" file reference documentation.

    1 #include <stdbool.h>
    2 #include <giraffe/mallocvar.h>
    3 
    4 #include "quotaio.h"
    5 #include "misc.h"
    6 #include "dquothash.h"
    7 
    8 struct dlinks {
    9     ino_t i_num;
   10     struct dlinks *next;
   11 };
   12 
   13 
   14 #define DQUOTHASHSIZE 32768 /* Size of hashtable for dquots from file */
   15 #define LINKSHASHSIZE 16384 /* Size of hashtable for hardlinked inodes */
   16 
   17 static struct dquot *dquot_hash[MAXQUOTAS][DQUOTHASHSIZE];
   18 static struct dlinks *links_hash[MAXQUOTAS][DQUOTHASHSIZE];
   19 
   20 
   21 /* Hash given id */
   22 static inline uint
   23 hash_dquot(uint const id) {
   24     return ((id ^ (id << 16)) * 997) & (DQUOTHASHSIZE - 1);
   25 }
   26 
   27 
   28 
   29 /* Compute hashvalue for given inode number */
   30 static inline uint 
   31 hash_ino(uint const i_num) {
   32     return ((i_num ^ (i_num << 16)) * 997) & (LINKSHASHSIZE - 1);
   33 }
   34 
   35 
   36 
   37 void
   38 remember_inode(int   const type,
   39                ino_t const i_num) {
   40 /*----------------------------------------------------------------------------
   41    Record the fact that the filesystem object with inode number 'i_num'
   42    has been processed.
   43 
   44    Assume the fact is not already recorded.
   45 -----------------------------------------------------------------------------*/
   46     uint const hash = hash_ino(i_num);
   47 
   48     struct dlinks * lptr;
   49 
   50     debug("Remembering inode number %llu", (unsigned long long)i_num);
   51 
   52     MALLOCVAR_NOFAIL(lptr);
   53 
   54     lptr->i_num = i_num;
   55     lptr->next = links_hash[type][hash];
   56     links_hash[type][hash] = lptr;
   57 }
   58 
   59 
   60 
   61 bool
   62 inode_has_been_processed(int   const type,
   63                          ino_t const i_num) {
   64 
   65     uint const hash = hash_ino(i_num);
   66 
   67     struct dlinks * lptr;
   68 
   69     for (lptr = links_hash[type][hash]; lptr; lptr = lptr->next)
   70         if (lptr->i_num == i_num)
   71             return 1;
   72 
   73     return 0;
   74 }
   75 
   76 
   77 
   78 /*
   79  * Do a lookup of a type of quota for a specific id. Use short cut with
   80  * most recently used dquot struct pointer.
   81  */
   82 struct dquot *
   83 lookup_dquot(qid_t const id,
   84              int   const type) {
   85 
   86     uint const hash = hash_dquot(id);
   87 
   88     struct dquot * lptr;
   89 
   90     for (lptr = dquot_hash[type][hash]; lptr; lptr = lptr->dq_next)
   91         if (lptr->dq_id == id)
   92             return lptr;
   93     return NULL;
   94 }
   95 
   96 
   97 
   98 struct dquot *
   99 create_dquot(qid_t const id) {
  100 
  101     struct dquot * dquotP;
  102 
  103     MALLOCVAR_NOFAIL(dquotP);
  104 
  105     dquotP->dq_id = id;
  106 
  107     dquotP->dq_dqb.dqb_ihardlimit = 0;
  108     dquotP->dq_dqb.dqb_isoftlimit = 0;
  109     dquotP->dq_dqb.dqb_curinodes  = 0;
  110     dquotP->dq_dqb.dqb_itime      = (time_t) 0;
  111     dquotP->dq_dqb.dqb_bhardlimit = 0;
  112     dquotP->dq_dqb.dqb_bsoftlimit = 0;
  113     dquotP->dq_dqb.dqb_curspace   = 0;
  114     dquotP->dq_dqb.dqb_btime      = (time_t) 0;
  115     
  116     return dquotP;
  117 }
  118 
  119 
  120 
  121 void
  122 add_dquot(struct dquot * const dquotP,
  123           int            const type) {
  124 /*----------------------------------------------------------------------------
  125    Add a dquot to the quota cache.
  126 
  127    Assume there is not already a dquot for the same id in there.
  128 -----------------------------------------------------------------------------*/
  129     uint hash = hash_dquot(dquotP->dq_id);
  130 
  131     dquotP->dq_next = dquot_hash[type][hash];
  132     dquot_hash[type][hash] = dquotP;
  133 
  134     debug("Adding dquot structure %p type '%s' for id %d.  Hash=%u",
  135           dquotP, type2name(type), (int)dquotP->dq_id, hash);
  136 }
  137 
  138 
  139 
  140 /*
  141  * Clean up all list from a previous run.
  142  */
  143 void
  144 remove_list(void) {
  145 
  146     int cnt;
  147     uint i;
  148     struct dquot *dquot, *dquot_free;
  149     struct dlinks *dlink, *dlink_free;
  150 
  151     for (cnt = 0; cnt < MAXQUOTAS; ++cnt) {
  152         for (i = 0; i < DQUOTHASHSIZE; ++i) {
  153             dquot = dquot_hash[cnt][i];
  154             while (dquot) {
  155                 dquot_free = dquot;
  156                 dquot = dquot->dq_next;
  157 #ifdef DEBUG_MALLOC
  158                 free_mem += sizeof(struct dquot);
  159 #endif
  160                 free(dquot_free);
  161             }
  162             dquot_hash[cnt][i] = NULL;
  163         }
  164         for (i = 0; i < LINKSHASHSIZE; ++i) {
  165             dlink = links_hash[cnt][i];
  166             while (dlink) {
  167                 dlink_free = dlink;
  168                 dlink = dlink->next;
  169 #ifdef DEBUG_MALLOC
  170                 free_mem += sizeof(struct dlinks);
  171 #endif
  172                 free(dlink_free);
  173             }
  174             links_hash[cnt][i] = NULL;
  175         }
  176     }
  177 }
  178 
  179 
  180 
  181 void
  182 do_for_each_quota(int    const type,
  183                   void   fn(struct dquot *, void *),
  184                   void * const argP) {
  185 
  186     unsigned int i;
  187 
  188     for (i = 0; i < DQUOTHASHSIZE; ++i) {
  189         struct dquot * dquotP;
  190         
  191         for (dquotP = dquot_hash[type][i]; dquotP; dquotP = dquotP->dq_next) {
  192             fn(dquotP, argP);
  193         }
  194     }
  195 }