root/fs/buffer.c

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

DEFINITIONS

This source file includes following definitions.
  1. __wait_on_buffer
  2. sync_buffers
  3. sync_dev
  4. fsync_dev
  5. sys_sync
  6. file_fsync
  7. sys_fsync
  8. invalidate_buffers
  9. remove_from_hash_queue
  10. remove_from_lru_list
  11. remove_from_free_list
  12. remove_from_queues
  13. put_last_lru
  14. put_last_free
  15. insert_into_queues
  16. find_buffer
  17. get_hash_table
  18. set_blocksize
  19. refill_freelist
  20. getblk
  21. set_writetime
  22. refile_buffer
  23. brelse
  24. bread
  25. breada
  26. put_unused_buffer_head
  27. get_more_buffer_heads
  28. get_unused_buffer_head
  29. create_buffers
  30. read_buffers
  31. try_to_align
  32. check_aligned
  33. try_to_load_aligned
  34. try_to_share_buffers
  35. bread_page
  36. bwrite_page
  37. grow_buffers
  38. try_to_free
  39. maybe_shrink_lav_buffers
  40. shrink_buffers
  41. shrink_specific_buffers
  42. show_buffers
  43. try_to_reassign
  44. reassign_cluster
  45. try_to_generate_cluster
  46. generate_cluster
  47. buffer_init
  48. wakeup_bdflush
  49. sync_old_buffers
  50. sys_bdflush

   1 /*
   2  *  linux/fs/buffer.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  *  'buffer.c' implements the buffer-cache functions. Race-conditions have
   9  * been avoided by NEVER letting an interrupt change a buffer (except for the
  10  * data, of course), but instead letting the caller do it.
  11  */
  12 
  13 /*
  14  * NOTE! There is one discordant note here: checking floppies for
  15  * disk change. This is where it fits best, I think, as it should
  16  * invalidate changed floppy-disk-caches.
  17  */
  18  
  19 #include <linux/sched.h>
  20 #include <linux/kernel.h>
  21 #include <linux/major.h>
  22 #include <linux/string.h>
  23 #include <linux/locks.h>
  24 #include <linux/errno.h>
  25 #include <linux/malloc.h>
  26 
  27 #include <asm/system.h>
  28 #include <asm/segment.h>
  29 #include <asm/io.h>
  30 
  31 #define NR_SIZES 4
  32 static char buffersize_index[9] = {-1,  0,  1, -1,  2, -1, -1, -1, 3};
  33 static short int bufferindex_size[NR_SIZES] = {512, 1024, 2048, 4096};
  34 
  35 #define BUFSIZE_INDEX(X) ((int) buffersize_index[(X)>>9])
  36 #define MAX_BUF_PER_PAGE (PAGE_SIZE / 512)
  37 
  38 static int grow_buffers(int pri, int size);
  39 static int shrink_specific_buffers(unsigned int priority, int size);
  40 static int maybe_shrink_lav_buffers(int);
  41 
  42 static int nr_hash = 0;  /* Size of hash table */
  43 static struct buffer_head ** hash_table;
  44 struct buffer_head ** buffer_pages;
  45 static struct buffer_head * lru_list[NR_LIST] = {NULL, };
  46 static struct buffer_head * free_list[NR_SIZES] = {NULL, };
  47 static struct buffer_head * unused_list = NULL;
  48 static struct wait_queue * buffer_wait = NULL;
  49 
  50 int nr_buffers = 0;
  51 int nr_buffers_type[NR_LIST] = {0,};
  52 int nr_buffers_size[NR_SIZES] = {0,};
  53 int nr_buffers_st[NR_SIZES][NR_LIST] = {{0,},};
  54 int buffer_usage[NR_SIZES] = {0,};  /* Usage counts used to determine load average */
  55 int buffers_lav[NR_SIZES] = {0,};  /* Load average of buffer usage */
  56 int nr_free[NR_SIZES] = {0,};
  57 int buffermem = 0;
  58 int nr_buffer_heads = 0;
  59 extern int *blksize_size[];
  60 
  61 /* Here is the parameter block for the bdflush process. */
  62 static void wakeup_bdflush(int);
  63 
  64 #define N_PARAM 9
  65 #define LAV
  66 
  67 static union bdflush_param{
  68         struct {
  69                 int nfract;  /* Percentage of buffer cache dirty to 
  70                                 activate bdflush */
  71                 int ndirty;  /* Maximum number of dirty blocks to write out per
  72                                 wake-cycle */
  73                 int nrefill; /* Number of clean buffers to try and obtain
  74                                 each time we call refill */
  75                 int nref_dirt; /* Dirty buffer threshold for activating bdflush
  76                                   when trying to refill buffers. */
  77                 int clu_nfract;  /* Percentage of buffer cache to scan to 
  78                                     search for free clusters */
  79                 int age_buffer;  /* Time for normal buffer to age before 
  80                                     we flush it */
  81                 int age_super;  /* Time for superblock to age before we 
  82                                    flush it */
  83                 int lav_const;  /* Constant used for load average (time
  84                                    constant */
  85                 int lav_ratio;  /* Used to determine how low a lav for a
  86                                    particular size can go before we start to
  87                                    trim back the buffers */
  88         } b_un;
  89         unsigned int data[N_PARAM];
  90 } bdf_prm = {{25, 500, 64, 256, 15, 30*HZ, 5*HZ, 1884, 2}};
  91 
  92 /* The lav constant is set for 1 minute, as long as the update process runs
  93    every 5 seconds.  If you change the frequency of update, the time
  94    constant will also change. */
  95 
  96 
  97 /* These are the min and max parameter values that we will allow to be assigned */
  98 static int bdflush_min[N_PARAM] = {  0,  10,    5,   25,  0,   100,   100, 1, 1};
  99 static int bdflush_max[N_PARAM] = {100,5000, 2000, 2000,100, 60000, 60000, 2047, 5};
 100 
 101 /*
 102  * Rewrote the wait-routines to use the "new" wait-queue functionality,
 103  * and getting rid of the cli-sti pairs. The wait-queue routines still
 104  * need cli-sti, but now it's just a couple of 386 instructions or so.
 105  *
 106  * Note that the real wait_on_buffer() is an inline function that checks
 107  * if 'b_wait' is set before calling this, so that the queues aren't set
 108  * up unnecessarily.
 109  */
 110 void __wait_on_buffer(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 111 {
 112         struct wait_queue wait = { current, NULL };
 113 
 114         bh->b_count++;
 115         add_wait_queue(&bh->b_wait, &wait);
 116 repeat:
 117         current->state = TASK_UNINTERRUPTIBLE;
 118         if (bh->b_lock) {
 119                 schedule();
 120                 goto repeat;
 121         }
 122         remove_wait_queue(&bh->b_wait, &wait);
 123         bh->b_count--;
 124         current->state = TASK_RUNNING;
 125 }
 126 
 127 /* Call sync_buffers with wait!=0 to ensure that the call does not
 128    return until all buffer writes have completed.  Sync() may return
 129    before the writes have finished; fsync() may not. */
 130 
 131 
 132 /* Godamity-damn.  Some buffers (bitmaps for filesystems)
 133    spontaneously dirty themselves without ever brelse being called.
 134    We will ultimately want to put these in a separate list, but for
 135    now we search all of the lists for dirty buffers */
 136 
 137 static int sync_buffers(dev_t dev, int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139         int i, retry, pass = 0, err = 0;
 140         int nlist, ncount;
 141         struct buffer_head * bh, *next;
 142 
 143         /* One pass for no-wait, three for wait:
 144            0) write out all dirty, unlocked buffers;
 145            1) write out all dirty buffers, waiting if locked;
 146            2) wait for completion by waiting for all buffers to unlock. */
 147  repeat:
 148         retry = 0;
 149  repeat2:
 150         ncount = 0;
 151         /* We search all lists as a failsafe mechanism, not because we expect
 152            there to be dirty buffers on any of the other lists. */
 153         for(nlist = 0; nlist < NR_LIST; nlist++)
 154          {
 155          repeat1:
 156                  bh = lru_list[nlist];
 157                  if(!bh) continue;
 158                  for (i = nr_buffers_type[nlist]*2 ; i-- > 0 ; bh = next) {
 159                          if(bh->b_list != nlist) goto repeat1;
 160                          next = bh->b_next_free;
 161                          if(!lru_list[nlist]) break;
 162                          if (dev && bh->b_dev != dev)
 163                                   continue;
 164                          if (bh->b_lock)
 165                           {
 166                                   /* Buffer is locked; skip it unless wait is
 167                                      requested AND pass > 0. */
 168                                   if (!wait || !pass) {
 169                                           retry = 1;
 170                                           continue;
 171                                   }
 172                                   wait_on_buffer (bh);
 173                                   goto repeat2;
 174                           }
 175                          /* If an unlocked buffer is not uptodate, there has
 176                              been an IO error. Skip it. */
 177                          if (wait && bh->b_req && !bh->b_lock &&
 178                              !bh->b_dirt && !bh->b_uptodate) {
 179                                   err = 1;
 180                                   printk("Weird - unlocked, clean and not uptodate buffer on list %d %x %lu\n", nlist, bh->b_dev, bh->b_blocknr);
 181                                   continue;
 182                           }
 183                          /* Don't write clean buffers.  Don't write ANY buffers
 184                             on the third pass. */
 185                          if (!bh->b_dirt || pass>=2)
 186                                   continue;
 187                          /* don't bother about locked buffers */
 188                          if (bh->b_lock)
 189                                  continue;
 190                          bh->b_count++;
 191                          bh->b_flushtime = 0;
 192                          ll_rw_block(WRITE, 1, &bh);
 193 
 194                          if(nlist != BUF_DIRTY) { 
 195                                  printk("[%d %x %ld] ", nlist, bh->b_dev, bh->b_blocknr);
 196                                  ncount++;
 197                          };
 198                          bh->b_count--;
 199                          retry = 1;
 200                  }
 201          }
 202         if (ncount) printk("sys_sync: %d dirty buffers not on dirty list\n", ncount);
 203         
 204         /* If we are waiting for the sync to succeed, and if any dirty
 205            blocks were written, then repeat; on the second pass, only
 206            wait for buffers being written (do not pass to write any
 207            more buffers on the second pass). */
 208         if (wait && retry && ++pass<=2)
 209                  goto repeat;
 210         return err;
 211 }
 212 
 213 void sync_dev(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 214 {
 215         sync_buffers(dev, 0);
 216         sync_supers(dev);
 217         sync_inodes(dev);
 218         sync_buffers(dev, 0);
 219 }
 220 
 221 int fsync_dev(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223         sync_buffers(dev, 0);
 224         sync_supers(dev);
 225         sync_inodes(dev);
 226         return sync_buffers(dev, 1);
 227 }
 228 
 229 asmlinkage int sys_sync(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 230 {
 231         fsync_dev(0);
 232         return 0;
 233 }
 234 
 235 int file_fsync (struct inode *inode, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 236 {
 237         return fsync_dev(inode->i_dev);
 238 }
 239 
 240 asmlinkage int sys_fsync(unsigned int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 241 {
 242         struct file * file;
 243         struct inode * inode;
 244 
 245         if (fd>=NR_OPEN || !(file=current->files->fd[fd]) || !(inode=file->f_inode))
 246                 return -EBADF;
 247         if (!file->f_op || !file->f_op->fsync)
 248                 return -EINVAL;
 249         if (file->f_op->fsync(inode,file))
 250                 return -EIO;
 251         return 0;
 252 }
 253 
 254 void invalidate_buffers(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 255 {
 256         int i;
 257         int nlist;
 258         struct buffer_head * bh;
 259 
 260         for(nlist = 0; nlist < NR_LIST; nlist++) {
 261                 bh = lru_list[nlist];
 262                 for (i = nr_buffers_type[nlist]*2 ; --i > 0 ; bh = bh->b_next_free) {
 263                         if (bh->b_dev != dev)
 264                                 continue;
 265                         wait_on_buffer(bh);
 266                         if (bh->b_dev != dev)
 267                                 continue;
 268                         if (bh->b_count)
 269                                 continue;
 270                         bh->b_flushtime = bh->b_uptodate = 
 271                                 bh->b_dirt = bh->b_req = 0;
 272                 }
 273         }
 274 }
 275 
 276 #define _hashfn(dev,block) (((unsigned)(dev^block))%nr_hash)
 277 #define hash(dev,block) hash_table[_hashfn(dev,block)]
 278 
 279 static inline void remove_from_hash_queue(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 280 {
 281         if (bh->b_next)
 282                 bh->b_next->b_prev = bh->b_prev;
 283         if (bh->b_prev)
 284                 bh->b_prev->b_next = bh->b_next;
 285         if (hash(bh->b_dev,bh->b_blocknr) == bh)
 286                 hash(bh->b_dev,bh->b_blocknr) = bh->b_next;
 287         bh->b_next = bh->b_prev = NULL;
 288 }
 289 
 290 static inline void remove_from_lru_list(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 291 {
 292         if (!(bh->b_prev_free) || !(bh->b_next_free))
 293                 panic("VFS: LRU block list corrupted");
 294         if (bh->b_dev == 0xffff) panic("LRU list corrupted");
 295         bh->b_prev_free->b_next_free = bh->b_next_free;
 296         bh->b_next_free->b_prev_free = bh->b_prev_free;
 297 
 298         if (lru_list[bh->b_list] == bh)
 299                  lru_list[bh->b_list] = bh->b_next_free;
 300         if(lru_list[bh->b_list] == bh)
 301                  lru_list[bh->b_list] = NULL;
 302         bh->b_next_free = bh->b_prev_free = NULL;
 303 }
 304 
 305 static inline void remove_from_free_list(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 306 {
 307         int isize = BUFSIZE_INDEX(bh->b_size);
 308         if (!(bh->b_prev_free) || !(bh->b_next_free))
 309                 panic("VFS: Free block list corrupted");
 310         if(bh->b_dev != 0xffff) panic("Free list corrupted");
 311         if(!free_list[isize])
 312                  panic("Free list empty");
 313         nr_free[isize]--;
 314         if(bh->b_next_free == bh)
 315                  free_list[isize] = NULL;
 316         else {
 317                 bh->b_prev_free->b_next_free = bh->b_next_free;
 318                 bh->b_next_free->b_prev_free = bh->b_prev_free;
 319                 if (free_list[isize] == bh)
 320                          free_list[isize] = bh->b_next_free;
 321         };
 322         bh->b_next_free = bh->b_prev_free = NULL;
 323 }
 324 
 325 static inline void remove_from_queues(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 326 {
 327         if(bh->b_dev == 0xffff) {
 328                 remove_from_free_list(bh); /* Free list entries should not be
 329                                               in the hash queue */
 330                 return;
 331         };
 332         nr_buffers_type[bh->b_list]--;
 333         nr_buffers_st[BUFSIZE_INDEX(bh->b_size)][bh->b_list]--;
 334         remove_from_hash_queue(bh);
 335         remove_from_lru_list(bh);
 336 }
 337 
 338 static inline void put_last_lru(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 339 {
 340         if (!bh)
 341                 return;
 342         if (bh == lru_list[bh->b_list]) {
 343                 lru_list[bh->b_list] = bh->b_next_free;
 344                 return;
 345         }
 346         if(bh->b_dev == 0xffff) panic("Wrong block for lru list");
 347         remove_from_lru_list(bh);
 348 /* add to back of free list */
 349 
 350         if(!lru_list[bh->b_list]) {
 351                 lru_list[bh->b_list] = bh;
 352                 lru_list[bh->b_list]->b_prev_free = bh;
 353         };
 354 
 355         bh->b_next_free = lru_list[bh->b_list];
 356         bh->b_prev_free = lru_list[bh->b_list]->b_prev_free;
 357         lru_list[bh->b_list]->b_prev_free->b_next_free = bh;
 358         lru_list[bh->b_list]->b_prev_free = bh;
 359 }
 360 
 361 static inline void put_last_free(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 362 {
 363         int isize;
 364         if (!bh)
 365                 return;
 366 
 367         isize = BUFSIZE_INDEX(bh->b_size);      
 368         bh->b_dev = 0xffff;  /* So it is obvious we are on the free list */
 369 /* add to back of free list */
 370 
 371         if(!free_list[isize]) {
 372                 free_list[isize] = bh;
 373                 bh->b_prev_free = bh;
 374         };
 375 
 376         nr_free[isize]++;
 377         bh->b_next_free = free_list[isize];
 378         bh->b_prev_free = free_list[isize]->b_prev_free;
 379         free_list[isize]->b_prev_free->b_next_free = bh;
 380         free_list[isize]->b_prev_free = bh;
 381 }
 382 
 383 static inline void insert_into_queues(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 384 {
 385 /* put at end of free list */
 386 
 387         if(bh->b_dev == 0xffff) {
 388                 put_last_free(bh);
 389                 return;
 390         };
 391         if(!lru_list[bh->b_list]) {
 392                 lru_list[bh->b_list] = bh;
 393                 bh->b_prev_free = bh;
 394         };
 395         if (bh->b_next_free) panic("VFS: buffer LRU pointers corrupted");
 396         bh->b_next_free = lru_list[bh->b_list];
 397         bh->b_prev_free = lru_list[bh->b_list]->b_prev_free;
 398         lru_list[bh->b_list]->b_prev_free->b_next_free = bh;
 399         lru_list[bh->b_list]->b_prev_free = bh;
 400         nr_buffers_type[bh->b_list]++;
 401         nr_buffers_st[BUFSIZE_INDEX(bh->b_size)][bh->b_list]++;
 402 /* put the buffer in new hash-queue if it has a device */
 403         bh->b_prev = NULL;
 404         bh->b_next = NULL;
 405         if (!bh->b_dev)
 406                 return;
 407         bh->b_next = hash(bh->b_dev,bh->b_blocknr);
 408         hash(bh->b_dev,bh->b_blocknr) = bh;
 409         if (bh->b_next)
 410                 bh->b_next->b_prev = bh;
 411 }
 412 
 413 static struct buffer_head * find_buffer(dev_t dev, int block, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 414 {               
 415         struct buffer_head * tmp;
 416 
 417         for (tmp = hash(dev,block) ; tmp != NULL ; tmp = tmp->b_next)
 418                 if (tmp->b_dev==dev && tmp->b_blocknr==block)
 419                         if (tmp->b_size == size)
 420                                 return tmp;
 421                         else {
 422                                 printk("VFS: Wrong blocksize on device %d/%d\n",
 423                                                         MAJOR(dev), MINOR(dev));
 424                                 return NULL;
 425                         }
 426         return NULL;
 427 }
 428 
 429 /*
 430  * Why like this, I hear you say... The reason is race-conditions.
 431  * As we don't lock buffers (unless we are reading them, that is),
 432  * something might happen to it while we sleep (ie a read-error
 433  * will force it bad). This shouldn't really happen currently, but
 434  * the code is ready.
 435  */
 436 struct buffer_head * get_hash_table(dev_t dev, int block, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 437 {
 438         struct buffer_head * bh;
 439 
 440         for (;;) {
 441                 if (!(bh=find_buffer(dev,block,size)))
 442                         return NULL;
 443                 bh->b_reuse=0;
 444                 bh->b_count++;
 445                 wait_on_buffer(bh);
 446                 if (bh->b_dev == dev && bh->b_blocknr == block && bh->b_size == size)
 447                         return bh;
 448                 bh->b_count--;
 449         }
 450 }
 451 
 452 void set_blocksize(dev_t dev, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 453 {
 454         int i, nlist;
 455         struct buffer_head * bh, *bhnext;
 456 
 457         if (!blksize_size[MAJOR(dev)])
 458                 return;
 459 
 460         switch(size) {
 461                 default: panic("Invalid blocksize passed to set_blocksize");
 462                 case 512: case 1024: case 2048: case 4096:;
 463         }
 464 
 465         if (blksize_size[MAJOR(dev)][MINOR(dev)] == 0 && size == BLOCK_SIZE) {
 466                 blksize_size[MAJOR(dev)][MINOR(dev)] = size;
 467                 return;
 468         }
 469         if (blksize_size[MAJOR(dev)][MINOR(dev)] == size)
 470                 return;
 471         sync_buffers(dev, 2);
 472         blksize_size[MAJOR(dev)][MINOR(dev)] = size;
 473 
 474   /* We need to be quite careful how we do this - we are moving entries
 475      around on the free list, and we can get in a loop if we are not careful.*/
 476 
 477         for(nlist = 0; nlist < NR_LIST; nlist++) {
 478                 bh = lru_list[nlist];
 479                 for (i = nr_buffers_type[nlist]*2 ; --i > 0 ; bh = bhnext) {
 480                         if(!bh) break;
 481                         bhnext = bh->b_next_free; 
 482                         if (bh->b_dev != dev)
 483                                  continue;
 484                         if (bh->b_size == size)
 485                                  continue;
 486                         
 487                         wait_on_buffer(bh);
 488                         if (bh->b_dev == dev && bh->b_size != size) {
 489                                 bh->b_uptodate = bh->b_dirt = bh->b_req =
 490                                          bh->b_flushtime = 0;
 491                         };
 492                         remove_from_hash_queue(bh);
 493                 }
 494         }
 495 }
 496 
 497 #define BADNESS(bh) (((bh)->b_dirt<<1)+(bh)->b_lock)
 498 
 499 void refill_freelist(int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 500 {
 501         struct buffer_head * bh, * tmp;
 502         struct buffer_head * candidate[NR_LIST];
 503         unsigned int best_time, winner;
 504         int isize = BUFSIZE_INDEX(size);
 505         int buffers[NR_LIST];
 506         int i;
 507         int needed;
 508 
 509         /* First see if we even need this.  Sometimes it is advantageous
 510          to request some blocks in a filesystem that we know that we will
 511          be needing ahead of time. */
 512 
 513         if (nr_free[isize] > 100)
 514                 return;
 515 
 516         /* If there are too many dirty buffers, we wake up the update process
 517            now so as to ensure that there are still clean buffers available
 518            for user processes to use (and dirty) */
 519         
 520         /* We are going to try and locate this much memory */
 521         needed =bdf_prm.b_un.nrefill * size;  
 522 
 523         while (nr_free_pages > min_free_pages*2 && needed > 0 &&
 524                grow_buffers(GFP_BUFFER, size)) {
 525                 needed -= PAGE_SIZE;
 526         }
 527 
 528         if(needed <= 0) return;
 529 
 530         /* See if there are too many buffers of a different size.
 531            If so, victimize them */
 532 
 533         while(maybe_shrink_lav_buffers(size))
 534          {
 535                  if(!grow_buffers(GFP_BUFFER, size)) break;
 536                  needed -= PAGE_SIZE;
 537                  if(needed <= 0) return;
 538          };
 539 
 540         /* OK, we cannot grow the buffer cache, now try and get some
 541            from the lru list */
 542 
 543         /* First set the candidate pointers to usable buffers.  This
 544            should be quick nearly all of the time. */
 545 
 546 repeat0:
 547         for(i=0; i<NR_LIST; i++){
 548                 if(i == BUF_DIRTY || i == BUF_SHARED || 
 549                    nr_buffers_type[i] == 0) {
 550                         candidate[i] = NULL;
 551                         buffers[i] = 0;
 552                         continue;
 553                 }
 554                 buffers[i] = nr_buffers_type[i];
 555                 for (bh = lru_list[i]; buffers[i] > 0; bh = tmp, buffers[i]--)
 556                  {
 557                          if(buffers[i] < 0) panic("Here is the problem");
 558                          tmp = bh->b_next_free;
 559                          if (!bh) break;
 560                          
 561                          if (mem_map[MAP_NR((unsigned long) bh->b_data)] != 1 ||
 562                              bh->b_dirt) {
 563                                  refile_buffer(bh);
 564                                  continue;
 565                          };
 566                          
 567                          if (bh->b_count || bh->b_size != size)
 568                                   continue;
 569                          
 570                          /* Buffers are written in the order they are placed 
 571                             on the locked list. If we encounter a locked
 572                             buffer here, this means that the rest of them
 573                             are also locked */
 574                          if(bh->b_lock && (i == BUF_LOCKED || i == BUF_LOCKED1)) {
 575                                  buffers[i] = 0;
 576                                  break;
 577                          }
 578                          
 579                          if (BADNESS(bh)) continue;
 580                          break;
 581                  };
 582                 if(!buffers[i]) candidate[i] = NULL; /* Nothing on this list */
 583                 else candidate[i] = bh;
 584                 if(candidate[i] && candidate[i]->b_count) panic("Here is the problem");
 585         }
 586         
 587  repeat:
 588         if(needed <= 0) return;
 589         
 590         /* Now see which candidate wins the election */
 591         
 592         winner = best_time = UINT_MAX;  
 593         for(i=0; i<NR_LIST; i++){
 594                 if(!candidate[i]) continue;
 595                 if(candidate[i]->b_lru_time < best_time){
 596                         best_time = candidate[i]->b_lru_time;
 597                         winner = i;
 598                 }
 599         }
 600         
 601         /* If we have a winner, use it, and then get a new candidate from that list */
 602         if(winner != UINT_MAX) {
 603                 i = winner;
 604                 bh = candidate[i];
 605                 candidate[i] = bh->b_next_free;
 606                 if(candidate[i] == bh) candidate[i] = NULL;  /* Got last one */
 607                 if (bh->b_count || bh->b_size != size)
 608                          panic("Busy buffer in candidate list\n");
 609                 if (mem_map[MAP_NR((unsigned long) bh->b_data)] != 1)
 610                          panic("Shared buffer in candidate list\n");
 611                 if (BADNESS(bh)) panic("Buffer in candidate list with BADNESS != 0\n");
 612                 
 613                 if(bh->b_dev == 0xffff) panic("Wrong list");
 614                 remove_from_queues(bh);
 615                 bh->b_dev = 0xffff;
 616                 put_last_free(bh);
 617                 needed -= bh->b_size;
 618                 buffers[i]--;
 619                 if(buffers[i] < 0) panic("Here is the problem");
 620                 
 621                 if(buffers[i] == 0) candidate[i] = NULL;
 622                 
 623                 /* Now all we need to do is advance the candidate pointer
 624                    from the winner list to the next usable buffer */
 625                 if(candidate[i] && buffers[i] > 0){
 626                         if(buffers[i] <= 0) panic("Here is another problem");
 627                         for (bh = candidate[i]; buffers[i] > 0; bh = tmp, buffers[i]--) {
 628                                 if(buffers[i] < 0) panic("Here is the problem");
 629                                 tmp = bh->b_next_free;
 630                                 if (!bh) break;
 631                                 
 632                                 if (mem_map[MAP_NR((unsigned long) bh->b_data)] != 1 ||
 633                                     bh->b_dirt) {
 634                                         refile_buffer(bh);
 635                                         continue;
 636                                 };
 637                                 
 638                                 if (bh->b_count || bh->b_size != size)
 639                                          continue;
 640                                 
 641                                 /* Buffers are written in the order they are
 642                                    placed on the locked list.  If we encounter
 643                                    a locked buffer here, this means that the
 644                                    rest of them are also locked */
 645                                 if(bh->b_lock && (i == BUF_LOCKED || i == BUF_LOCKED1)) {
 646                                         buffers[i] = 0;
 647                                         break;
 648                                 }
 649               
 650                                 if (BADNESS(bh)) continue;
 651                                 break;
 652                         };
 653                         if(!buffers[i]) candidate[i] = NULL; /* Nothing here */
 654                         else candidate[i] = bh;
 655                         if(candidate[i] && candidate[i]->b_count) 
 656                                  panic("Here is the problem");
 657                 }
 658                 
 659                 goto repeat;
 660         }
 661         
 662         if(needed <= 0) return;
 663         
 664         /* Too bad, that was not enough. Try a little harder to grow some. */
 665         
 666         if (nr_free_pages > 5) {
 667                 if (grow_buffers(GFP_BUFFER, size)) {
 668                         needed -= PAGE_SIZE;
 669                         goto repeat0;
 670                 };
 671         }
 672         
 673         /* and repeat until we find something good */
 674         if (!grow_buffers(GFP_ATOMIC, size))
 675                 wakeup_bdflush(1);
 676         needed -= PAGE_SIZE;
 677         goto repeat0;
 678 }
 679 
 680 /*
 681  * Ok, this is getblk, and it isn't very clear, again to hinder
 682  * race-conditions. Most of the code is seldom used, (ie repeating),
 683  * so it should be much more efficient than it looks.
 684  *
 685  * The algorithm is changed: hopefully better, and an elusive bug removed.
 686  *
 687  * 14.02.92: changed it to sync dirty buffers a bit: better performance
 688  * when the filesystem starts to get full of dirty blocks (I hope).
 689  */
 690 struct buffer_head * getblk(dev_t dev, int block, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 691 {
 692         struct buffer_head * bh;
 693         int isize = BUFSIZE_INDEX(size);
 694 
 695         /* Update this for the buffer size lav. */
 696         buffer_usage[isize]++;
 697 
 698         /* If there are too many dirty buffers, we wake up the update process
 699            now so as to ensure that there are still clean buffers available
 700            for user processes to use (and dirty) */
 701 repeat:
 702         bh = get_hash_table(dev, block, size);
 703         if (bh) {
 704                 if (bh->b_uptodate && !bh->b_dirt)
 705                          put_last_lru(bh);
 706                 if(!bh->b_dirt) bh->b_flushtime = 0;
 707                 return bh;
 708         }
 709 
 710         while(!free_list[isize]) refill_freelist(size);
 711         
 712         if (find_buffer(dev,block,size))
 713                  goto repeat;
 714 
 715         bh = free_list[isize];
 716         remove_from_free_list(bh);
 717 
 718 /* OK, FINALLY we know that this buffer is the only one of its kind, */
 719 /* and that it's unused (b_count=0), unlocked (b_lock=0), and clean */
 720         bh->b_count=1;
 721         bh->b_dirt=0;
 722         bh->b_lock=0;
 723         bh->b_uptodate=0;
 724         bh->b_flushtime=0;
 725         bh->b_req=0;
 726         bh->b_reuse=0;
 727         bh->b_dev=dev;
 728         bh->b_blocknr=block;
 729         insert_into_queues(bh);
 730         return bh;
 731 }
 732 
 733 void set_writetime(struct buffer_head * buf, int flag)
     /* [previous][next][first][last][top][bottom][index][help] */
 734 {
 735         int newtime;
 736 
 737         if (buf->b_dirt){
 738                 /* Move buffer to dirty list if jiffies is clear */
 739                 newtime = jiffies + (flag ? bdf_prm.b_un.age_super : 
 740                                      bdf_prm.b_un.age_buffer);
 741                 if(!buf->b_flushtime || buf->b_flushtime > newtime)
 742                          buf->b_flushtime = newtime;
 743         } else {
 744                 buf->b_flushtime = 0;
 745         }
 746 }
 747 
 748 
 749 void refile_buffer(struct buffer_head * buf){
     /* [previous][next][first][last][top][bottom][index][help] */
 750         int dispose;
 751         if(buf->b_dev == 0xffff) panic("Attempt to refile free buffer\n");
 752         if (buf->b_dirt)
 753                 dispose = BUF_DIRTY;
 754         else if (mem_map[MAP_NR((unsigned long) buf->b_data)] > 1)
 755                 dispose = BUF_SHARED;
 756         else if (buf->b_lock)
 757                 dispose = BUF_LOCKED;
 758         else if (buf->b_list == BUF_SHARED)
 759                 dispose = BUF_UNSHARED;
 760         else
 761                 dispose = BUF_CLEAN;
 762         if(dispose == BUF_CLEAN) buf->b_lru_time = jiffies;
 763         if(dispose != buf->b_list)  {
 764                 if(dispose == BUF_DIRTY || dispose == BUF_UNSHARED)
 765                          buf->b_lru_time = jiffies;
 766                 if(dispose == BUF_LOCKED && 
 767                    (buf->b_flushtime - buf->b_lru_time) <= bdf_prm.b_un.age_super)
 768                          dispose = BUF_LOCKED1;
 769                 remove_from_queues(buf);
 770                 buf->b_list = dispose;
 771                 insert_into_queues(buf);
 772                 if(dispose == BUF_DIRTY && nr_buffers_type[BUF_DIRTY] > 
 773                    (nr_buffers - nr_buffers_type[BUF_SHARED]) *
 774                    bdf_prm.b_un.nfract/100)
 775                          wakeup_bdflush(0);
 776         }
 777 }
 778 
 779 void brelse(struct buffer_head * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 780 {
 781         if (!buf)
 782                 return;
 783         wait_on_buffer(buf);
 784 
 785         /* If dirty, mark the time this buffer should be written back */
 786         set_writetime(buf, 0);
 787         refile_buffer(buf);
 788 
 789         if (buf->b_count) {
 790                 if (--buf->b_count)
 791                         return;
 792                 wake_up(&buffer_wait);
 793                 if (buf->b_reuse) {
 794                         if (!buf->b_lock && !buf->b_dirt && !buf->b_wait) {
 795                                 buf->b_reuse = 0;
 796                                 if(buf->b_dev == 0xffff) panic("brelse: Wrong list");
 797                                 remove_from_queues(buf);
 798                                 buf->b_dev = 0xffff;
 799                                 put_last_free(buf);
 800                         }
 801                 }
 802                 return;
 803         }
 804         printk("VFS: brelse: Trying to free free buffer\n");
 805 }
 806 
 807 /*
 808  * bread() reads a specified block and returns the buffer that contains
 809  * it. It returns NULL if the block was unreadable.
 810  */
 811 struct buffer_head * bread(dev_t dev, int block, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 812 {
 813         struct buffer_head * bh;
 814 
 815         if (!(bh = getblk(dev, block, size))) {
 816                 printk("VFS: bread: READ error on device %d/%d\n",
 817                                                 MAJOR(dev), MINOR(dev));
 818                 return NULL;
 819         }
 820         if (bh->b_uptodate)
 821                 return bh;
 822         ll_rw_block(READ, 1, &bh);
 823         wait_on_buffer(bh);
 824         if (bh->b_uptodate)
 825                 return bh;
 826         brelse(bh);
 827         return NULL;
 828 }
 829 
 830 /*
 831  * Ok, breada can be used as bread, but additionally to mark other
 832  * blocks for reading as well. End the argument list with a negative
 833  * number.
 834  */
 835 
 836 #define NBUF 16
 837 
 838 struct buffer_head * breada(dev_t dev, int block, int bufsize,
     /* [previous][next][first][last][top][bottom][index][help] */
 839         unsigned int pos, unsigned int filesize)
 840 {
 841         struct buffer_head * bhlist[NBUF];
 842         unsigned int blocks;
 843         struct buffer_head * bh;
 844         int index;
 845         int i, j;
 846 
 847         if (pos >= filesize)
 848                 return NULL;
 849 
 850         if (block < 0 || !(bh = getblk(dev,block,bufsize)))
 851                 return NULL;
 852 
 853         index = BUFSIZE_INDEX(bh->b_size);
 854 
 855         if (bh->b_uptodate)
 856                 return bh;
 857 
 858         blocks = ((filesize & (bufsize - 1)) - (pos & (bufsize - 1))) >> (9+index);
 859 
 860         if (blocks > (read_ahead[MAJOR(dev)] >> index))
 861                 blocks = read_ahead[MAJOR(dev)] >> index;
 862         if (blocks > NBUF)
 863                 blocks = NBUF;
 864         
 865         bhlist[0] = bh;
 866         j = 1;
 867         for(i=1; i<blocks; i++) {
 868                 bh = getblk(dev,block+i,bufsize);
 869                 if (bh->b_uptodate) {
 870                         brelse(bh);
 871                         break;
 872                 }
 873                 bhlist[j++] = bh;
 874         }
 875 
 876         /* Request the read for these buffers, and then release them */
 877         ll_rw_block(READ, j, bhlist);
 878 
 879         for(i=1; i<j; i++)
 880                 brelse(bhlist[i]);
 881 
 882         /* Wait for this buffer, and then continue on */
 883         bh = bhlist[0];
 884         wait_on_buffer(bh);
 885         if (bh->b_uptodate)
 886                 return bh;
 887         brelse(bh);
 888         return NULL;
 889 }
 890 
 891 /*
 892  * See fs/inode.c for the weird use of volatile..
 893  */
 894 static void put_unused_buffer_head(struct buffer_head * bh)
     /* [previous][next][first][last][top][bottom][index][help] */
 895 {
 896         struct wait_queue * wait;
 897 
 898         wait = ((volatile struct buffer_head *) bh)->b_wait;
 899         memset(bh,0,sizeof(*bh));
 900         ((volatile struct buffer_head *) bh)->b_wait = wait;
 901         bh->b_next_free = unused_list;
 902         unused_list = bh;
 903 }
 904 
 905 static void get_more_buffer_heads(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 906 {
 907         int i;
 908         struct buffer_head * bh;
 909 
 910         if (unused_list)
 911                 return;
 912 
 913         if (!(bh = (struct buffer_head*) get_free_page(GFP_BUFFER)))
 914                 return;
 915 
 916         for (nr_buffer_heads+=i=PAGE_SIZE/sizeof*bh ; i>0; i--) {
 917                 bh->b_next_free = unused_list;  /* only make link */
 918                 unused_list = bh++;
 919         }
 920 }
 921 
 922 static struct buffer_head * get_unused_buffer_head(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 923 {
 924         struct buffer_head * bh;
 925 
 926         get_more_buffer_heads();
 927         if (!unused_list)
 928                 return NULL;
 929         bh = unused_list;
 930         unused_list = bh->b_next_free;
 931         bh->b_next_free = NULL;
 932         bh->b_data = NULL;
 933         bh->b_size = 0;
 934         bh->b_req = 0;
 935         return bh;
 936 }
 937 
 938 /*
 939  * Create the appropriate buffers when given a page for data area and
 940  * the size of each buffer.. Use the bh->b_this_page linked list to
 941  * follow the buffers created.  Return NULL if unable to create more
 942  * buffers.
 943  */
 944 static struct buffer_head * create_buffers(unsigned long page, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 945 {
 946         struct buffer_head *bh, *head;
 947         unsigned long offset;
 948 
 949         head = NULL;
 950         offset = PAGE_SIZE;
 951         while ((offset -= size) < PAGE_SIZE) {
 952                 bh = get_unused_buffer_head();
 953                 if (!bh)
 954                         goto no_grow;
 955                 bh->b_this_page = head;
 956                 head = bh;
 957                 bh->b_data = (char *) (page+offset);
 958                 bh->b_size = size;
 959                 bh->b_dev = 0xffff;  /* Flag as unused */
 960         }
 961         return head;
 962 /*
 963  * In case anything failed, we just free everything we got.
 964  */
 965 no_grow:
 966         bh = head;
 967         while (bh) {
 968                 head = bh;
 969                 bh = bh->b_this_page;
 970                 put_unused_buffer_head(head);
 971         }
 972         return NULL;
 973 }
 974 
 975 static void read_buffers(struct buffer_head * bh[], int nrbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 976 {
 977         int i;
 978         int bhnum = 0;
 979         struct buffer_head * bhr[MAX_BUF_PER_PAGE];
 980 
 981         for (i = 0 ; i < nrbuf ; i++) {
 982                 if (bh[i] && !bh[i]->b_uptodate)
 983                         bhr[bhnum++] = bh[i];
 984         }
 985         if (bhnum)
 986                 ll_rw_block(READ, bhnum, bhr);
 987         for (i = nrbuf ; --i >= 0 ; ) {
 988                 if (bh[i]) {
 989                         wait_on_buffer(bh[i]);
 990                 }
 991         }
 992 }
 993 
 994 /*
 995  * This actually gets enough info to try to align the stuff,
 996  * but we don't bother yet.. We'll have to check that nobody
 997  * else uses the buffers etc.
 998  *
 999  * "address" points to the new page we can use to move things
1000  * around..
1001  */
1002 static unsigned long try_to_align(struct buffer_head ** bh, int nrbuf,
     /* [previous][next][first][last][top][bottom][index][help] */
1003         unsigned long address)
1004 {
1005         while (nrbuf-- > 0)
1006                 brelse(bh[nrbuf]);
1007         return 0;
1008 }
1009 
1010 static unsigned long check_aligned(struct buffer_head * first, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
1011         dev_t dev, int *b, int size)
1012 {
1013         struct buffer_head * bh[MAX_BUF_PER_PAGE];
1014         unsigned long page;
1015         unsigned long offset;
1016         int block;
1017         int nrbuf;
1018         int aligned = 1;
1019 
1020         bh[0] = first;
1021         nrbuf = 1;
1022         page = (unsigned long) first->b_data;
1023         if (page & ~PAGE_MASK)
1024                 aligned = 0;
1025         for (offset = size ; offset < PAGE_SIZE ; offset += size) {
1026                 block = *++b;
1027                 if (!block)
1028                         goto no_go;
1029                 first = get_hash_table(dev, block, size);
1030                 if (!first)
1031                         goto no_go;
1032                 bh[nrbuf++] = first;
1033                 if (page+offset != (unsigned long) first->b_data)
1034                         aligned = 0;
1035         }
1036         if (!aligned)
1037                 return try_to_align(bh, nrbuf, address);
1038         mem_map[MAP_NR(page)]++;
1039         read_buffers(bh,nrbuf);         /* make sure they are actually read correctly */
1040         while (nrbuf-- > 0)
1041                 brelse(bh[nrbuf]);
1042         free_page(address);
1043         ++current->mm->min_flt;
1044         return page;
1045 no_go:
1046         while (nrbuf-- > 0)
1047                 brelse(bh[nrbuf]);
1048         return 0;
1049 }
1050 
1051 static unsigned long try_to_load_aligned(unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
1052         dev_t dev, int b[], int size)
1053 {
1054         struct buffer_head * bh, * tmp, * arr[MAX_BUF_PER_PAGE];
1055         unsigned long offset;
1056         int isize = BUFSIZE_INDEX(size);
1057         int * p;
1058         int block;
1059 
1060         bh = create_buffers(address, size);
1061         if (!bh)
1062                 return 0;
1063         /* do any of the buffers already exist? punt if so.. */
1064         p = b;
1065         for (offset = 0 ; offset < PAGE_SIZE ; offset += size) {
1066                 block = *(p++);
1067                 if (!block)
1068                         goto not_aligned;
1069                 if (find_buffer(dev, block, size))
1070                         goto not_aligned;
1071         }
1072         tmp = bh;
1073         p = b;
1074         block = 0;
1075         while (1) {
1076                 arr[block++] = bh;
1077                 bh->b_count = 1;
1078                 bh->b_dirt = 0;
1079                 bh->b_reuse = 0;
1080                 bh->b_flushtime = 0;
1081                 bh->b_uptodate = 0;
1082                 bh->b_req = 0;
1083                 bh->b_dev = dev;
1084                 bh->b_blocknr = *(p++);
1085                 bh->b_list = BUF_CLEAN;
1086                 nr_buffers++;
1087                 nr_buffers_size[isize]++;
1088                 insert_into_queues(bh);
1089                 if (bh->b_this_page)
1090                         bh = bh->b_this_page;
1091                 else
1092                         break;
1093         }
1094         buffermem += PAGE_SIZE;
1095         bh->b_this_page = tmp;
1096         mem_map[MAP_NR(address)]++;
1097         buffer_pages[MAP_NR(address)] = bh;
1098         read_buffers(arr,block);
1099         while (block-- > 0)
1100                 brelse(arr[block]);
1101         ++current->mm->maj_flt;
1102         return address;
1103 not_aligned:
1104         while ((tmp = bh) != NULL) {
1105                 bh = bh->b_this_page;
1106                 put_unused_buffer_head(tmp);
1107         }
1108         return 0;
1109 }
1110 
1111 /*
1112  * Try-to-share-buffers tries to minimize memory use by trying to keep
1113  * both code pages and the buffer area in the same page. This is done by
1114  * (a) checking if the buffers are already aligned correctly in memory and
1115  * (b) if none of the buffer heads are in memory at all, trying to load
1116  * them into memory the way we want them.
1117  *
1118  * This doesn't guarantee that the memory is shared, but should under most
1119  * circumstances work very well indeed (ie >90% sharing of code pages on
1120  * demand-loadable executables).
1121  */
1122 static inline unsigned long try_to_share_buffers(unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
1123         dev_t dev, int *b, int size)
1124 {
1125         struct buffer_head * bh;
1126         int block;
1127 
1128         block = b[0];
1129         if (!block)
1130                 return 0;
1131         bh = get_hash_table(dev, block, size);
1132         if (bh)
1133                 return check_aligned(bh, address, dev, b, size);
1134         return try_to_load_aligned(address, dev, b, size);
1135 }
1136 
1137 /*
1138  * bread_page reads four buffers into memory at the desired address. It's
1139  * a function of its own, as there is some speed to be got by reading them
1140  * all at the same time, not waiting for one to be read, and then another
1141  * etc. This also allows us to optimize memory usage by sharing code pages
1142  * and filesystem buffers..
1143  */
1144 unsigned long bread_page(unsigned long address, dev_t dev, int b[], int size, int no_share)
     /* [previous][next][first][last][top][bottom][index][help] */
1145 {
1146         struct buffer_head * bh[MAX_BUF_PER_PAGE];
1147         unsigned long where;
1148         int i, j;
1149 
1150         if (!no_share) {
1151                 where = try_to_share_buffers(address, dev, b, size);
1152                 if (where)
1153                         return where;
1154         }
1155         ++current->mm->maj_flt;
1156         for (i=0, j=0; j<PAGE_SIZE ; i++, j+= size) {
1157                 bh[i] = NULL;
1158                 if (b[i])
1159                         bh[i] = getblk(dev, b[i], size);
1160         }
1161         read_buffers(bh,i);
1162         where = address;
1163         for (i=0, j=0; j<PAGE_SIZE ; i++, j += size, where += size) {
1164                 if (bh[i]) {
1165                         if (bh[i]->b_uptodate)
1166                                 memcpy((void *) where, bh[i]->b_data, size);
1167                         brelse(bh[i]);
1168                 } else
1169                         memset((void *) where, 0, size);
1170         }
1171         return address;
1172 }
1173 
1174 /*
1175  * bwrite_page writes a page out to the buffer cache and/or the physical device.
1176  * It's used for mmap writes (the same way bread_page() is used for mmap reads).
1177  */
1178 void bwrite_page(unsigned long address, dev_t dev, int b[], int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1179 {
1180         struct buffer_head * bh[MAX_BUF_PER_PAGE];
1181         int i, j;
1182 
1183         for (i=0, j=0; j<PAGE_SIZE ; i++, j+= size) {
1184                 bh[i] = NULL;
1185                 if (b[i])
1186                         bh[i] = getblk(dev, b[i], size);
1187         }
1188         for (i=0, j=0; j<PAGE_SIZE ; i++, j += size, address += size) {
1189                 if (bh[i]) {
1190                         memcpy(bh[i]->b_data, (void *) address, size);
1191                         bh[i]->b_uptodate = 1;
1192                         mark_buffer_dirty(bh[i], 0);
1193                         brelse(bh[i]);
1194                 } else
1195                         memset((void *) address, 0, size);
1196         }       
1197 }
1198 
1199 /*
1200  * Try to increase the number of buffers available: the size argument
1201  * is used to determine what kind of buffers we want.
1202  */
1203 static int grow_buffers(int pri, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1204 {
1205         unsigned long page;
1206         struct buffer_head *bh, *tmp;
1207         struct buffer_head * insert_point;
1208         int isize;
1209 
1210         if ((size & 511) || (size > PAGE_SIZE)) {
1211                 printk("VFS: grow_buffers: size = %d\n",size);
1212                 return 0;
1213         }
1214 
1215         isize = BUFSIZE_INDEX(size);
1216 
1217         if (!(page = __get_free_page(pri)))
1218                 return 0;
1219         bh = create_buffers(page, size);
1220         if (!bh) {
1221                 free_page(page);
1222                 return 0;
1223         }
1224 
1225         insert_point = free_list[isize];
1226 
1227         tmp = bh;
1228         while (1) {
1229                 nr_free[isize]++;
1230                 if (insert_point) {
1231                         tmp->b_next_free = insert_point->b_next_free;
1232                         tmp->b_prev_free = insert_point;
1233                         insert_point->b_next_free->b_prev_free = tmp;
1234                         insert_point->b_next_free = tmp;
1235                 } else {
1236                         tmp->b_prev_free = tmp;
1237                         tmp->b_next_free = tmp;
1238                 }
1239                 insert_point = tmp;
1240                 ++nr_buffers;
1241                 if (tmp->b_this_page)
1242                         tmp = tmp->b_this_page;
1243                 else
1244                         break;
1245         }
1246         free_list[isize] = bh;
1247         buffer_pages[MAP_NR(page)] = bh;
1248         tmp->b_this_page = bh;
1249         wake_up(&buffer_wait);
1250         buffermem += PAGE_SIZE;
1251         return 1;
1252 }
1253 
1254 
1255 /* =========== Reduce the buffer memory ============= */
1256 
1257 /*
1258  * try_to_free() checks if all the buffers on this particular page
1259  * are unused, and free's the page if so.
1260  */
1261 static int try_to_free(struct buffer_head * bh, struct buffer_head ** bhp)
     /* [previous][next][first][last][top][bottom][index][help] */
1262 {
1263         unsigned long page;
1264         struct buffer_head * tmp, * p;
1265         int isize = BUFSIZE_INDEX(bh->b_size);
1266 
1267         *bhp = bh;
1268         page = (unsigned long) bh->b_data;
1269         page &= PAGE_MASK;
1270         tmp = bh;
1271         do {
1272                 if (!tmp)
1273                         return 0;
1274                 if (tmp->b_count || tmp->b_dirt || tmp->b_lock || tmp->b_wait)
1275                         return 0;
1276                 tmp = tmp->b_this_page;
1277         } while (tmp != bh);
1278         tmp = bh;
1279         do {
1280                 p = tmp;
1281                 tmp = tmp->b_this_page;
1282                 nr_buffers--;
1283                 nr_buffers_size[isize]--;
1284                 if (p == *bhp)
1285                   {
1286                     *bhp = p->b_prev_free;
1287                     if (p == *bhp) /* Was this the last in the list? */
1288                       *bhp = NULL;
1289                   }
1290                 remove_from_queues(p);
1291                 put_unused_buffer_head(p);
1292         } while (tmp != bh);
1293         buffermem -= PAGE_SIZE;
1294         buffer_pages[MAP_NR(page)] = NULL;
1295         free_page(page);
1296         return !mem_map[MAP_NR(page)];
1297 }
1298 
1299 
1300 /*
1301  * Consult the load average for buffers and decide whether or not
1302  * we should shrink the buffers of one size or not.  If we decide yes,
1303  * do it and return 1.  Else return 0.  Do not attempt to shrink size
1304  * that is specified.
1305  *
1306  * I would prefer not to use a load average, but the way things are now it
1307  * seems unavoidable.  The way to get rid of it would be to force clustering
1308  * universally, so that when we reclaim buffers we always reclaim an entire
1309  * page.  Doing this would mean that we all need to move towards QMAGIC.
1310  */
1311 
1312 static int maybe_shrink_lav_buffers(int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1313 {          
1314         int nlist;
1315         int isize;
1316         int total_lav, total_n_buffers, n_sizes;
1317         
1318         /* Do not consider the shared buffers since they would not tend
1319            to have getblk called very often, and this would throw off
1320            the lav.  They are not easily reclaimable anyway (let the swapper
1321            make the first move). */
1322   
1323         total_lav = total_n_buffers = n_sizes = 0;
1324         for(nlist = 0; nlist < NR_SIZES; nlist++)
1325          {
1326                  total_lav += buffers_lav[nlist];
1327                  if(nr_buffers_size[nlist]) n_sizes++;
1328                  total_n_buffers += nr_buffers_size[nlist];
1329                  total_n_buffers -= nr_buffers_st[nlist][BUF_SHARED]; 
1330          }
1331         
1332         /* See if we have an excessive number of buffers of a particular
1333            size - if so, victimize that bunch. */
1334   
1335         isize = (size ? BUFSIZE_INDEX(size) : -1);
1336         
1337         if (n_sizes > 1)
1338                  for(nlist = 0; nlist < NR_SIZES; nlist++)
1339                   {
1340                           if(nlist == isize) continue;
1341                           if(nr_buffers_size[nlist] &&
1342                              bdf_prm.b_un.lav_const * buffers_lav[nlist]*total_n_buffers < 
1343                              total_lav * (nr_buffers_size[nlist] - nr_buffers_st[nlist][BUF_SHARED]))
1344                                    if(shrink_specific_buffers(6, bufferindex_size[nlist])) 
1345                                             return 1;
1346                   }
1347         return 0;
1348 }
1349 /*
1350  * Try to free up some pages by shrinking the buffer-cache
1351  *
1352  * Priority tells the routine how hard to try to shrink the
1353  * buffers: 3 means "don't bother too much", while a value
1354  * of 0 means "we'd better get some free pages now".
1355  *
1356  * "limit" is meant to limit the shrink-action only to pages
1357  * that are in the 0 - limit address range, for DMA re-allocations.
1358  * We ignore that right now.
1359  */
1360 int shrink_buffers(unsigned int priority, unsigned long limit)
     /* [previous][next][first][last][top][bottom][index][help] */
1361 {
1362         if (priority < 2) {
1363                 sync_buffers(0,0);
1364         }
1365 
1366         if(priority == 2) wakeup_bdflush(1);
1367 
1368         if(maybe_shrink_lav_buffers(0)) return 1;
1369 
1370         /* No good candidate size - take any size we can find */
1371         return shrink_specific_buffers(priority, 0);
1372 }
1373 
1374 static int shrink_specific_buffers(unsigned int priority, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1375 {
1376         struct buffer_head *bh;
1377         int nlist;
1378         int i, isize, isize1;
1379 
1380 #ifdef DEBUG
1381         if(size) printk("Shrinking buffers of size %d\n", size);
1382 #endif
1383         /* First try the free lists, and see if we can get a complete page
1384            from here */
1385         isize1 = (size ? BUFSIZE_INDEX(size) : -1);
1386 
1387         for(isize = 0; isize<NR_SIZES; isize++){
1388                 if(isize1 != -1 && isize1 != isize) continue;
1389                 bh = free_list[isize];
1390                 if(!bh) continue;
1391                 for (i=0 ; !i || bh != free_list[isize]; bh = bh->b_next_free, i++) {
1392                         if (bh->b_count || !bh->b_this_page)
1393                                  continue;
1394                         if (try_to_free(bh, &bh))
1395                                  return 1;
1396                         if(!bh) break; /* Some interrupt must have used it after we
1397                                           freed the page.  No big deal - keep looking */
1398                 }
1399         }
1400         
1401         /* Not enough in the free lists, now try the lru list */
1402         
1403         for(nlist = 0; nlist < NR_LIST; nlist++) {
1404         repeat1:
1405                 if(priority > 3 && nlist == BUF_SHARED) continue;
1406                 bh = lru_list[nlist];
1407                 if(!bh) continue;
1408                 i = 2*nr_buffers_type[nlist] >> priority;
1409                 for ( ; i-- > 0 ; bh = bh->b_next_free) {
1410                         /* We may have stalled while waiting for I/O to complete. */
1411                         if(bh->b_list != nlist) goto repeat1;
1412                         if (bh->b_count || !bh->b_this_page)
1413                                  continue;
1414                         if(size && bh->b_size != size) continue;
1415                         if (bh->b_lock)
1416                                  if (priority)
1417                                           continue;
1418                                  else
1419                                           wait_on_buffer(bh);
1420                         if (bh->b_dirt) {
1421                                 bh->b_count++;
1422                                 bh->b_flushtime = 0;
1423                                 ll_rw_block(WRITEA, 1, &bh);
1424                                 bh->b_count--;
1425                                 continue;
1426                         }
1427                         if (try_to_free(bh, &bh))
1428                                  return 1;
1429                         if(!bh) break;
1430                 }
1431         }
1432         return 0;
1433 }
1434 
1435 
1436 /* ================== Debugging =================== */
1437 
1438 void show_buffers(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1439 {
1440         struct buffer_head * bh;
1441         int found = 0, locked = 0, dirty = 0, used = 0, lastused = 0;
1442         int shared;
1443         int nlist, isize;
1444 
1445         printk("Buffer memory:   %6dkB\n",buffermem>>10);
1446         printk("Buffer heads:    %6d\n",nr_buffer_heads);
1447         printk("Buffer blocks:   %6d\n",nr_buffers);
1448 
1449         for(nlist = 0; nlist < NR_LIST; nlist++) {
1450           shared = found = locked = dirty = used = lastused = 0;
1451           bh = lru_list[nlist];
1452           if(!bh) continue;
1453           do {
1454                 found++;
1455                 if (bh->b_lock)
1456                         locked++;
1457                 if (bh->b_dirt)
1458                         dirty++;
1459                 if(mem_map[MAP_NR(((unsigned long) bh->b_data))] !=1) shared++;
1460                 if (bh->b_count)
1461                         used++, lastused = found;
1462                 bh = bh->b_next_free;
1463               } while (bh != lru_list[nlist]);
1464         printk("Buffer[%d] mem: %d buffers, %d used (last=%d), %d locked, %d dirty %d shrd\n",
1465                 nlist, found, used, lastused, locked, dirty, shared);
1466         };
1467         printk("Size    [LAV]     Free  Clean  Unshar     Lck    Lck1   Dirty  Shared\n");
1468         for(isize = 0; isize<NR_SIZES; isize++){
1469                 printk("%5d [%5d]: %7d ", bufferindex_size[isize],
1470                        buffers_lav[isize], nr_free[isize]);
1471                 for(nlist = 0; nlist < NR_LIST; nlist++)
1472                          printk("%7d ", nr_buffers_st[isize][nlist]);
1473                 printk("\n");
1474         }
1475 }
1476 
1477 
1478 /* ====================== Cluster patches for ext2 ==================== */
1479 
1480 /*
1481  * try_to_reassign() checks if all the buffers on this particular page
1482  * are unused, and reassign to a new cluster them if this is true.
1483  */
1484 static inline int try_to_reassign(struct buffer_head * bh, struct buffer_head ** bhp,
     /* [previous][next][first][last][top][bottom][index][help] */
1485                            dev_t dev, unsigned int starting_block)
1486 {
1487         unsigned long page;
1488         struct buffer_head * tmp, * p;
1489 
1490         *bhp = bh;
1491         page = (unsigned long) bh->b_data;
1492         page &= PAGE_MASK;
1493         if(mem_map[MAP_NR(page)] != 1) return 0;
1494         tmp = bh;
1495         do {
1496                 if (!tmp)
1497                          return 0;
1498                 
1499                 if (tmp->b_count || tmp->b_dirt || tmp->b_lock)
1500                          return 0;
1501                 tmp = tmp->b_this_page;
1502         } while (tmp != bh);
1503         tmp = bh;
1504         
1505         while((unsigned long) tmp->b_data & (PAGE_SIZE - 1)) 
1506                  tmp = tmp->b_this_page;
1507         
1508         /* This is the buffer at the head of the page */
1509         bh = tmp;
1510         do {
1511                 p = tmp;
1512                 tmp = tmp->b_this_page;
1513                 remove_from_queues(p);
1514                 p->b_dev=dev;
1515                 p->b_uptodate = 0;
1516                 p->b_req = 0;
1517                 p->b_blocknr=starting_block++;
1518                 insert_into_queues(p);
1519         } while (tmp != bh);
1520         return 1;
1521 }
1522 
1523 /*
1524  * Try to find a free cluster by locating a page where
1525  * all of the buffers are unused.  We would like this function
1526  * to be atomic, so we do not call anything that might cause
1527  * the process to sleep.  The priority is somewhat similar to
1528  * the priority used in shrink_buffers.
1529  * 
1530  * My thinking is that the kernel should end up using whole
1531  * pages for the buffer cache as much of the time as possible.
1532  * This way the other buffers on a particular page are likely
1533  * to be very near each other on the free list, and we will not
1534  * be expiring data prematurely.  For now we only cannibalize buffers
1535  * of the same size to keep the code simpler.
1536  */
1537 static int reassign_cluster(dev_t dev, 
     /* [previous][next][first][last][top][bottom][index][help] */
1538                      unsigned int starting_block, int size)
1539 {
1540         struct buffer_head *bh;
1541         int isize = BUFSIZE_INDEX(size);
1542         int i;
1543 
1544         /* We want to give ourselves a really good shot at generating
1545            a cluster, and since we only take buffers from the free
1546            list, we "overfill" it a little. */
1547 
1548         while(nr_free[isize] < 32) refill_freelist(size);
1549 
1550         bh = free_list[isize];
1551         if(bh)
1552                  for (i=0 ; !i || bh != free_list[isize] ; bh = bh->b_next_free, i++) {
1553                          if (!bh->b_this_page)  continue;
1554                          if (try_to_reassign(bh, &bh, dev, starting_block))
1555                                  return 4;
1556                  }
1557         return 0;
1558 }
1559 
1560 /* This function tries to generate a new cluster of buffers
1561  * from a new page in memory.  We should only do this if we have
1562  * not expanded the buffer cache to the maximum size that we allow.
1563  */
1564 static unsigned long try_to_generate_cluster(dev_t dev, int block, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1565 {
1566         struct buffer_head * bh, * tmp, * arr[MAX_BUF_PER_PAGE];
1567         int isize = BUFSIZE_INDEX(size);
1568         unsigned long offset;
1569         unsigned long page;
1570         int nblock;
1571 
1572         page = get_free_page(GFP_NOBUFFER);
1573         if(!page) return 0;
1574 
1575         bh = create_buffers(page, size);
1576         if (!bh) {
1577                 free_page(page);
1578                 return 0;
1579         };
1580         nblock = block;
1581         for (offset = 0 ; offset < PAGE_SIZE ; offset += size) {
1582                 if (find_buffer(dev, nblock++, size))
1583                          goto not_aligned;
1584         }
1585         tmp = bh;
1586         nblock = 0;
1587         while (1) {
1588                 arr[nblock++] = bh;
1589                 bh->b_count = 1;
1590                 bh->b_dirt = 0;
1591                 bh->b_flushtime = 0;
1592                 bh->b_lock = 0;
1593                 bh->b_uptodate = 0;
1594                 bh->b_req = 0;
1595                 bh->b_dev = dev;
1596                 bh->b_list = BUF_CLEAN;
1597                 bh->b_blocknr = block++;
1598                 nr_buffers++;
1599                 nr_buffers_size[isize]++;
1600                 insert_into_queues(bh);
1601                 if (bh->b_this_page)
1602                         bh = bh->b_this_page;
1603                 else
1604                         break;
1605         }
1606         buffermem += PAGE_SIZE;
1607         buffer_pages[MAP_NR(page)] = bh;
1608         bh->b_this_page = tmp;
1609         while (nblock-- > 0)
1610                 brelse(arr[nblock]);
1611         return 4; /* ?? */
1612 not_aligned:
1613         while ((tmp = bh) != NULL) {
1614                 bh = bh->b_this_page;
1615                 put_unused_buffer_head(tmp);
1616         }
1617         free_page(page);
1618         return 0;
1619 }
1620 
1621 unsigned long generate_cluster(dev_t dev, int b[], int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1622 {
1623         int i, offset;
1624         
1625         for (i = 0, offset = 0 ; offset < PAGE_SIZE ; i++, offset += size) {
1626                 if(i && b[i]-1 != b[i-1]) return 0;  /* No need to cluster */
1627                 if(find_buffer(dev, b[i], size)) return 0;
1628         };
1629 
1630         /* OK, we have a candidate for a new cluster */
1631         
1632         /* See if one size of buffer is over-represented in the buffer cache,
1633            if so reduce the numbers of buffers */
1634         if(maybe_shrink_lav_buffers(size))
1635          {
1636                  int retval;
1637                  retval = try_to_generate_cluster(dev, b[0], size);
1638                  if(retval) return retval;
1639          };
1640         
1641         if (nr_free_pages > min_free_pages*2) 
1642                  return try_to_generate_cluster(dev, b[0], size);
1643         else
1644                  return reassign_cluster(dev, b[0], size);
1645 }
1646 
1647 
1648 /* ===================== Init ======================= */
1649 
1650 /*
1651  * This initializes the initial buffer free list.  nr_buffers_type is set
1652  * to one less the actual number of buffers, as a sop to backwards
1653  * compatibility --- the old code did this (I think unintentionally,
1654  * but I'm not sure), and programs in the ps package expect it.
1655  *                                      - TYT 8/30/92
1656  */
1657 void buffer_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1658 {
1659         int i;
1660         int isize = BUFSIZE_INDEX(BLOCK_SIZE);
1661 
1662         if (high_memory >= 4*1024*1024) {
1663                 if(high_memory >= 16*1024*1024)
1664                          nr_hash = 16381;
1665                 else
1666                          nr_hash = 4093;
1667         } else {
1668                 nr_hash = 997;
1669         };
1670         
1671         hash_table = (struct buffer_head **) vmalloc(nr_hash * 
1672                                                      sizeof(struct buffer_head *));
1673 
1674 
1675         buffer_pages = (struct buffer_head **) vmalloc(MAP_NR(high_memory) * 
1676                                                      sizeof(struct buffer_head *));
1677         for (i = 0 ; i < MAP_NR(high_memory) ; i++)
1678                 buffer_pages[i] = NULL;
1679 
1680         for (i = 0 ; i < nr_hash ; i++)
1681                 hash_table[i] = NULL;
1682         lru_list[BUF_CLEAN] = 0;
1683         grow_buffers(GFP_KERNEL, BLOCK_SIZE);
1684         if (!free_list[isize])
1685                 panic("VFS: Unable to initialize buffer free list!");
1686         return;
1687 }
1688 
1689 
1690 /* ====================== bdflush support =================== */
1691 
1692 /* This is a simple kernel daemon, whose job it is to provide a dynamically
1693  * response to dirty buffers.  Once this process is activated, we write back
1694  * a limited number of buffers to the disks and then go back to sleep again.
1695  * In effect this is a process which never leaves kernel mode, and does not have
1696  * any user memory associated with it except for the stack.  There is also
1697  * a kernel stack page, which obviously must be separate from the user stack.
1698  */
1699 struct wait_queue * bdflush_wait = NULL;
1700 struct wait_queue * bdflush_done = NULL;
1701 
1702 static int bdflush_running = 0;
1703 
1704 static void wakeup_bdflush(int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1705 {
1706         if(!bdflush_running){
1707                 printk("Warning - bdflush not running\n");
1708                 sync_buffers(0,0);
1709                 return;
1710         };
1711         wake_up(&bdflush_wait);
1712         if(wait) sleep_on(&bdflush_done);
1713 }
1714 
1715 
1716 
1717 /* 
1718  * Here we attempt to write back old buffers.  We also try and flush inodes 
1719  * and supers as well, since this function is essentially "update", and 
1720  * otherwise there would be no way of ensuring that these quantities ever 
1721  * get written back.  Ideally, we would have a timestamp on the inodes
1722  * and superblocks so that we could write back only the old ones as well
1723  */
1724 
1725 asmlinkage int sync_old_buffers(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1726 {
1727         int i, isize;
1728         int ndirty, nwritten;
1729         int nlist;
1730         int ncount;
1731         struct buffer_head * bh, *next;
1732 
1733         sync_supers(0);
1734         sync_inodes(0);
1735 
1736         ncount = 0;
1737 #ifdef DEBUG
1738         for(nlist = 0; nlist < NR_LIST; nlist++)
1739 #else
1740         for(nlist = BUF_DIRTY; nlist <= BUF_DIRTY; nlist++)
1741 #endif
1742         {
1743                 ndirty = 0;
1744                 nwritten = 0;
1745         repeat:
1746                 bh = lru_list[nlist];
1747                 if(bh) 
1748                          for (i = nr_buffers_type[nlist]; i-- > 0; bh = next) {
1749                                  /* We may have stalled while waiting for I/O to complete. */
1750                                  if(bh->b_list != nlist) goto repeat;
1751                                  next = bh->b_next_free;
1752                                  if(!lru_list[nlist]) {
1753                                          printk("Dirty list empty %d\n", i);
1754                                          break;
1755                                  }
1756                                  
1757                                  /* Clean buffer on dirty list?  Refile it */
1758                                  if (nlist == BUF_DIRTY && !bh->b_dirt && !bh->b_lock)
1759                                   {
1760                                           refile_buffer(bh);
1761                                           continue;
1762                                   }
1763                                  
1764                                  if (bh->b_lock || !bh->b_dirt)
1765                                           continue;
1766                                  ndirty++;
1767                                  if(bh->b_flushtime > jiffies) continue;
1768                                  nwritten++;
1769                                  bh->b_count++;
1770                                  bh->b_flushtime = 0;
1771 #ifdef DEBUG
1772                                  if(nlist != BUF_DIRTY) ncount++;
1773 #endif
1774                                  ll_rw_block(WRITE, 1, &bh);
1775                                  bh->b_count--;
1776                          }
1777         }
1778 #ifdef DEBUG
1779         if (ncount) printk("sync_old_buffers: %d dirty buffers not on dirty list\n", ncount);
1780         printk("Wrote %d/%d buffers\n", nwritten, ndirty);
1781 #endif
1782         
1783         /* We assume that we only come through here on a regular
1784            schedule, like every 5 seconds.  Now update load averages.  
1785            Shift usage counts to prevent overflow. */
1786         for(isize = 0; isize<NR_SIZES; isize++){
1787                 CALC_LOAD(buffers_lav[isize], bdf_prm.b_un.lav_const, buffer_usage[isize]);
1788                 buffer_usage[isize] = 0;
1789         };
1790         return 0;
1791 }
1792 
1793 
1794 /* This is the interface to bdflush.  As we get more sophisticated, we can
1795  * pass tuning parameters to this "process", to adjust how it behaves.  If you
1796  * invoke this again after you have done this once, you would simply modify 
1797  * the tuning parameters.  We would want to verify each parameter, however,
1798  * to make sure that it is reasonable. */
1799 
1800 asmlinkage int sys_bdflush(int func, long data)
     /* [previous][next][first][last][top][bottom][index][help] */
1801 {
1802         int i, error;
1803         int ndirty;
1804         int nlist;
1805         int ncount;
1806         struct buffer_head * bh, *next;
1807 
1808         if (!suser())
1809                 return -EPERM;
1810 
1811         if (func == 1)
1812                  return sync_old_buffers();
1813 
1814         /* Basically func 0 means start, 1 means read param 1, 2 means write param 1, etc */
1815         if (func >= 2) {
1816                 i = (func-2) >> 1;
1817                 if (i < 0 || i >= N_PARAM)
1818                         return -EINVAL;
1819                 if((func & 1) == 0) {
1820                         error = verify_area(VERIFY_WRITE, (void *) data, sizeof(int));
1821                         if (error)
1822                                 return error;
1823                         put_user(bdf_prm.data[i], (int*)data);
1824                         return 0;
1825                 };
1826                 if (data < bdflush_min[i] || data > bdflush_max[i])
1827                         return -EINVAL;
1828                 bdf_prm.data[i] = data;
1829                 return 0;
1830         };
1831         
1832         if (bdflush_running)
1833                 return -EBUSY; /* Only one copy of this running at one time */
1834         bdflush_running++;
1835         
1836         /* OK, from here on is the daemon */
1837         
1838         for (;;) {
1839 #ifdef DEBUG
1840                 printk("bdflush() activated...");
1841 #endif
1842                 
1843                 ncount = 0;
1844 #ifdef DEBUG
1845                 for(nlist = 0; nlist < NR_LIST; nlist++)
1846 #else
1847                 for(nlist = BUF_DIRTY; nlist <= BUF_DIRTY; nlist++)
1848 #endif
1849                  {
1850                          ndirty = 0;
1851                  repeat:
1852                          bh = lru_list[nlist];
1853                          if(bh) 
1854                                   for (i = nr_buffers_type[nlist]; i-- > 0 && ndirty < bdf_prm.b_un.ndirty; 
1855                                        bh = next) {
1856                                           /* We may have stalled while waiting for I/O to complete. */
1857                                           if(bh->b_list != nlist) goto repeat;
1858                                           next = bh->b_next_free;
1859                                           if(!lru_list[nlist]) {
1860                                                   printk("Dirty list empty %d\n", i);
1861                                                   break;
1862                                           }
1863                                           
1864                                           /* Clean buffer on dirty list?  Refile it */
1865                                           if (nlist == BUF_DIRTY && !bh->b_dirt && !bh->b_lock)
1866                                            {
1867                                                    refile_buffer(bh);
1868                                                    continue;
1869                                            }
1870                                           
1871                                           if (bh->b_lock || !bh->b_dirt)
1872                                                    continue;
1873                                           /* Should we write back buffers that are shared or not??
1874                                              currently dirty buffers are not shared, so it does not matter */
1875                                           bh->b_count++;
1876                                           ndirty++;
1877                                           bh->b_flushtime = 0;
1878                                           ll_rw_block(WRITE, 1, &bh);
1879 #ifdef DEBUG
1880                                           if(nlist != BUF_DIRTY) ncount++;
1881 #endif
1882                                           bh->b_count--;
1883                                   }
1884                  }
1885 #ifdef DEBUG
1886                 if (ncount) printk("sys_bdflush: %d dirty buffers not on dirty list\n", ncount);
1887                 printk("sleeping again.\n");
1888 #endif
1889                 wake_up(&bdflush_done);
1890                 
1891                 /* If there are still a lot of dirty buffers around, skip the sleep
1892                    and flush some more */
1893                 
1894                 if(nr_buffers_type[BUF_DIRTY] <= (nr_buffers - nr_buffers_type[BUF_SHARED]) * 
1895                    bdf_prm.b_un.nfract/100) {
1896                         if (current->signal & (1 << (SIGKILL-1))) {
1897                                 bdflush_running--;
1898                                 return 0;
1899                         }
1900                         current->signal = 0;
1901                         interruptible_sleep_on(&bdflush_wait);
1902                 }
1903         }
1904 }
1905 
1906 
1907 /*
1908  * Overrides for Emacs so that we follow Linus's tabbing style.
1909  * Emacs will notice this stuff at the end of the file and automatically
1910  * adjust the settings for this buffer only.  This must remain at the end
1911  * of the file.
1912  * ---------------------------------------------------------------------------
1913  * Local variables:
1914  * c-indent-level: 8
1915  * c-brace-imaginary-offset: 0
1916  * c-brace-offset: -8
1917  * c-argdecl-indent: 8
1918  * c-label-offset: -8
1919  * c-continued-statement-offset: 8
1920  * c-continued-brace-offset: 0
1921  * End:
1922  */

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