This source file includes following definitions.
- __wait_on_buffer
- sync_buffers
- sync_dev
- fsync_dev
- sys_sync
- file_fsync
- sys_fsync
- sys_fdatasync
- invalidate_buffers
- remove_from_hash_queue
- remove_from_lru_list
- remove_from_free_list
- remove_from_queues
- put_last_lru
- put_last_free
- insert_into_queues
- find_buffer
- get_hash_table
- set_blocksize
- refill_freelist
- getblk
- set_writetime
- refile_buffer
- __brelse
- __bforget
- bread
- breada
- put_unused_buffer_head
- get_more_buffer_heads
- recover_reusable_buffer_heads
- get_unused_buffer_head
- create_buffers
- brw_page
- mark_buffer_uptodate
- unlock_buffer
- generic_readpage
- grow_buffers
- try_to_free_buffer
- age_buffer
- maybe_shrink_lav_buffers
- shrink_specific_buffers
- show_buffers
- try_to_reassign
- reassign_cluster
- try_to_generate_cluster
- generate_cluster
- buffer_init
- wakeup_bdflush
- sync_old_buffers
- sys_bdflush
- bdflush
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/major.h>
24 #include <linux/string.h>
25 #include <linux/locks.h>
26 #include <linux/errno.h>
27 #include <linux/malloc.h>
28 #include <linux/pagemap.h>
29 #include <linux/swap.h>
30 #include <linux/swapctl.h>
31 #include <linux/smp.h>
32 #include <linux/smp_lock.h>
33
34 #include <asm/system.h>
35 #include <asm/segment.h>
36 #include <asm/io.h>
37
38 #define NR_SIZES 4
39 static char buffersize_index[9] = {-1, 0, 1, -1, 2, -1, -1, -1, 3};
40 static short int bufferindex_size[NR_SIZES] = {512, 1024, 2048, 4096};
41
42 #define BUFSIZE_INDEX(X) ((int) buffersize_index[(X)>>9])
43 #define MAX_BUF_PER_PAGE (PAGE_SIZE / 512)
44
45 static int grow_buffers(int pri, int size);
46 static int shrink_specific_buffers(unsigned int priority, int size);
47 static int maybe_shrink_lav_buffers(int);
48
49 static int nr_hash = 0;
50 static struct buffer_head ** hash_table;
51 struct buffer_head ** buffer_pages;
52 static struct buffer_head * lru_list[NR_LIST] = {NULL, };
53
54
55
56 static struct buffer_head * next_to_age[NR_LIST] = {NULL, };
57 static struct buffer_head * free_list[NR_SIZES] = {NULL, };
58 static struct buffer_head * unused_list = NULL;
59 struct buffer_head * reuse_list = NULL;
60 static struct wait_queue * buffer_wait = NULL;
61
62 int nr_buffers = 0;
63 int nr_buffers_type[NR_LIST] = {0,};
64 int nr_buffers_size[NR_SIZES] = {0,};
65 int nr_buffers_st[NR_SIZES][NR_LIST] = {{0,},};
66 int buffer_usage[NR_SIZES] = {0,};
67 int buffers_lav[NR_SIZES] = {0,};
68 int nr_free[NR_SIZES] = {0,};
69 int buffermem = 0;
70 int nr_buffer_heads = 0;
71 extern int *blksize_size[];
72
73
74 static void wakeup_bdflush(int);
75
76 #define N_PARAM 9
77 #define LAV
78
79 static union bdflush_param{
80 struct {
81 int nfract;
82
83 int ndirty;
84
85 int nrefill;
86
87 int nref_dirt;
88
89 int clu_nfract;
90
91 int age_buffer;
92
93 int age_super;
94
95 int lav_const;
96
97 int lav_ratio;
98
99
100 } b_un;
101 unsigned int data[N_PARAM];
102 } bdf_prm = {{25, 500, 64, 256, 15, 30*HZ, 5*HZ, 1884, 2}};
103
104
105
106
107
108
109
110 static int bdflush_min[N_PARAM] = { 0, 10, 5, 25, 0, 100, 100, 1, 1};
111 static int bdflush_max[N_PARAM] = {100,5000, 2000, 2000,100, 60000, 60000, 2047, 5};
112
113
114
115
116
117
118
119
120
121
122 void __wait_on_buffer(struct buffer_head * bh)
123 {
124 struct wait_queue wait = { current, NULL };
125
126 bh->b_count++;
127 add_wait_queue(&bh->b_wait, &wait);
128 repeat:
129 current->state = TASK_UNINTERRUPTIBLE;
130 if (buffer_locked(bh)) {
131 schedule();
132 goto repeat;
133 }
134 remove_wait_queue(&bh->b_wait, &wait);
135 bh->b_count--;
136 current->state = TASK_RUNNING;
137 }
138
139
140
141
142
143
144
145
146
147
148
149 static int sync_buffers(kdev_t dev, int wait)
150 {
151 int i, retry, pass = 0, err = 0;
152 int nlist, ncount;
153 struct buffer_head * bh, *next;
154
155
156
157
158
159 repeat:
160 retry = 0;
161 repeat2:
162 ncount = 0;
163
164
165 for(nlist = 0; nlist < NR_LIST; nlist++)
166 {
167 repeat1:
168 bh = lru_list[nlist];
169 if(!bh) continue;
170 for (i = nr_buffers_type[nlist]*2 ; i-- > 0 ; bh = next) {
171 if(bh->b_list != nlist) goto repeat1;
172 next = bh->b_next_free;
173 if(!lru_list[nlist]) break;
174 if (dev && bh->b_dev != dev)
175 continue;
176 if (buffer_locked(bh))
177 {
178
179
180 if (!wait || !pass) {
181 retry = 1;
182 continue;
183 }
184 wait_on_buffer (bh);
185 goto repeat2;
186 }
187
188
189 if (wait && buffer_req(bh) && !buffer_locked(bh) &&
190 !buffer_dirty(bh) && !buffer_uptodate(bh)) {
191 err = 1;
192 continue;
193 }
194
195
196 if (!buffer_dirty(bh) || pass>=2)
197 continue;
198
199 if (buffer_locked(bh))
200 continue;
201 bh->b_count++;
202 bh->b_flushtime = 0;
203 ll_rw_block(WRITE, 1, &bh);
204
205 if(nlist != BUF_DIRTY) {
206 printk("[%d %s %ld] ", nlist,
207 kdevname(bh->b_dev), bh->b_blocknr);
208 ncount++;
209 };
210 bh->b_count--;
211 retry = 1;
212 }
213 }
214 if (ncount)
215 printk("sys_sync: %d dirty buffers not on dirty list\n", ncount);
216
217
218
219
220
221 if (wait && retry && ++pass<=2)
222 goto repeat;
223 return err;
224 }
225
226 void sync_dev(kdev_t dev)
227 {
228 sync_buffers(dev, 0);
229 sync_supers(dev);
230 sync_inodes(dev);
231 sync_buffers(dev, 0);
232 sync_dquots(dev, -1);
233 }
234
235 int fsync_dev(kdev_t dev)
236 {
237 sync_buffers(dev, 0);
238 sync_supers(dev);
239 sync_inodes(dev);
240 sync_dquots(dev, -1);
241 return sync_buffers(dev, 1);
242 }
243
244 asmlinkage int sys_sync(void)
245 {
246 fsync_dev(0);
247 return 0;
248 }
249
250 int file_fsync (struct inode *inode, struct file *filp)
251 {
252 return fsync_dev(inode->i_dev);
253 }
254
255 asmlinkage int sys_fsync(unsigned int fd)
256 {
257 struct file * file;
258 struct inode * inode;
259
260 if (fd>=NR_OPEN || !(file=current->files->fd[fd]) || !(inode=file->f_inode))
261 return -EBADF;
262 if (!file->f_op || !file->f_op->fsync)
263 return -EINVAL;
264 if (file->f_op->fsync(inode,file))
265 return -EIO;
266 return 0;
267 }
268
269 asmlinkage int sys_fdatasync(unsigned int fd)
270 {
271 struct file * file;
272 struct inode * inode;
273
274 if (fd>=NR_OPEN || !(file=current->files->fd[fd]) || !(inode=file->f_inode))
275 return -EBADF;
276 if (!file->f_op || !file->f_op->fsync)
277 return -EINVAL;
278
279 if (file->f_op->fsync(inode,file))
280 return -EIO;
281 return 0;
282 }
283
284 void invalidate_buffers(kdev_t dev)
285 {
286 int i;
287 int nlist;
288 struct buffer_head * bh;
289
290 for(nlist = 0; nlist < NR_LIST; nlist++) {
291 bh = lru_list[nlist];
292 for (i = nr_buffers_type[nlist]*2 ; --i > 0 ; bh = bh->b_next_free) {
293 if (bh->b_dev != dev)
294 continue;
295 wait_on_buffer(bh);
296 if (bh->b_dev != dev)
297 continue;
298 if (bh->b_count)
299 continue;
300 bh->b_flushtime = 0;
301 clear_bit(BH_Protected, &bh->b_state);
302 clear_bit(BH_Uptodate, &bh->b_state);
303 clear_bit(BH_Dirty, &bh->b_state);
304 clear_bit(BH_Req, &bh->b_state);
305 }
306 }
307 }
308
309 #define _hashfn(dev,block) (((unsigned)(HASHDEV(dev)^block))%nr_hash)
310 #define hash(dev,block) hash_table[_hashfn(dev,block)]
311
312 static inline void remove_from_hash_queue(struct buffer_head * bh)
313 {
314 if (bh->b_next)
315 bh->b_next->b_prev = bh->b_prev;
316 if (bh->b_prev)
317 bh->b_prev->b_next = bh->b_next;
318 if (hash(bh->b_dev,bh->b_blocknr) == bh)
319 hash(bh->b_dev,bh->b_blocknr) = bh->b_next;
320 bh->b_next = bh->b_prev = NULL;
321 }
322
323 static inline void remove_from_lru_list(struct buffer_head * bh)
324 {
325 if (!(bh->b_prev_free) || !(bh->b_next_free))
326 panic("VFS: LRU block list corrupted");
327 if (bh->b_dev == B_FREE)
328 panic("LRU list corrupted");
329 bh->b_prev_free->b_next_free = bh->b_next_free;
330 bh->b_next_free->b_prev_free = bh->b_prev_free;
331
332 if (lru_list[bh->b_list] == bh)
333 lru_list[bh->b_list] = bh->b_next_free;
334 if (lru_list[bh->b_list] == bh)
335 lru_list[bh->b_list] = NULL;
336 if (next_to_age[bh->b_list] == bh)
337 next_to_age[bh->b_list] = bh->b_next_free;
338 if (next_to_age[bh->b_list] == bh)
339 next_to_age[bh->b_list] = NULL;
340
341 bh->b_next_free = bh->b_prev_free = NULL;
342 }
343
344 static inline void remove_from_free_list(struct buffer_head * bh)
345 {
346 int isize = BUFSIZE_INDEX(bh->b_size);
347 if (!(bh->b_prev_free) || !(bh->b_next_free))
348 panic("VFS: Free block list corrupted");
349 if(bh->b_dev != B_FREE)
350 panic("Free list corrupted");
351 if(!free_list[isize])
352 panic("Free list empty");
353 nr_free[isize]--;
354 if(bh->b_next_free == bh)
355 free_list[isize] = NULL;
356 else {
357 bh->b_prev_free->b_next_free = bh->b_next_free;
358 bh->b_next_free->b_prev_free = bh->b_prev_free;
359 if (free_list[isize] == bh)
360 free_list[isize] = bh->b_next_free;
361 };
362 bh->b_next_free = bh->b_prev_free = NULL;
363 }
364
365 static inline void remove_from_queues(struct buffer_head * bh)
366 {
367 if(bh->b_dev == B_FREE) {
368 remove_from_free_list(bh);
369
370 return;
371 };
372 nr_buffers_type[bh->b_list]--;
373 nr_buffers_st[BUFSIZE_INDEX(bh->b_size)][bh->b_list]--;
374 remove_from_hash_queue(bh);
375 remove_from_lru_list(bh);
376 }
377
378 static inline void put_last_lru(struct buffer_head * bh)
379 {
380 if (!bh)
381 return;
382 if (bh == lru_list[bh->b_list]) {
383 lru_list[bh->b_list] = bh->b_next_free;
384 if (next_to_age[bh->b_list] == bh)
385 next_to_age[bh->b_list] = bh->b_next_free;
386 return;
387 }
388 if(bh->b_dev == B_FREE)
389 panic("Wrong block for lru list");
390 remove_from_lru_list(bh);
391
392
393 if(!lru_list[bh->b_list]) {
394 lru_list[bh->b_list] = bh;
395 lru_list[bh->b_list]->b_prev_free = bh;
396 };
397 if (!next_to_age[bh->b_list])
398 next_to_age[bh->b_list] = bh;
399
400 bh->b_next_free = lru_list[bh->b_list];
401 bh->b_prev_free = lru_list[bh->b_list]->b_prev_free;
402 lru_list[bh->b_list]->b_prev_free->b_next_free = bh;
403 lru_list[bh->b_list]->b_prev_free = bh;
404 }
405
406 static inline void put_last_free(struct buffer_head * bh)
407 {
408 int isize;
409 if (!bh)
410 return;
411
412 isize = BUFSIZE_INDEX(bh->b_size);
413 bh->b_dev = B_FREE;
414
415 if(!free_list[isize]) {
416 free_list[isize] = bh;
417 bh->b_prev_free = bh;
418 };
419
420 nr_free[isize]++;
421 bh->b_next_free = free_list[isize];
422 bh->b_prev_free = free_list[isize]->b_prev_free;
423 free_list[isize]->b_prev_free->b_next_free = bh;
424 free_list[isize]->b_prev_free = bh;
425 }
426
427 static inline void insert_into_queues(struct buffer_head * bh)
428 {
429
430 if(bh->b_dev == B_FREE) {
431 put_last_free(bh);
432 return;
433 }
434 if(!lru_list[bh->b_list]) {
435 lru_list[bh->b_list] = bh;
436 bh->b_prev_free = bh;
437 }
438 if (!next_to_age[bh->b_list])
439 next_to_age[bh->b_list] = bh;
440 if (bh->b_next_free) panic("VFS: buffer LRU pointers corrupted");
441 bh->b_next_free = lru_list[bh->b_list];
442 bh->b_prev_free = lru_list[bh->b_list]->b_prev_free;
443 lru_list[bh->b_list]->b_prev_free->b_next_free = bh;
444 lru_list[bh->b_list]->b_prev_free = bh;
445 nr_buffers_type[bh->b_list]++;
446 nr_buffers_st[BUFSIZE_INDEX(bh->b_size)][bh->b_list]++;
447
448 bh->b_prev = NULL;
449 bh->b_next = NULL;
450 if (!(bh->b_dev))
451 return;
452 bh->b_next = hash(bh->b_dev,bh->b_blocknr);
453 hash(bh->b_dev,bh->b_blocknr) = bh;
454 if (bh->b_next)
455 bh->b_next->b_prev = bh;
456 }
457
458 static inline struct buffer_head * find_buffer(kdev_t dev, int block, int size)
459 {
460 struct buffer_head * tmp;
461
462 for (tmp = hash(dev,block) ; tmp != NULL ; tmp = tmp->b_next)
463 if (tmp->b_blocknr == block && tmp->b_dev == dev)
464 if (tmp->b_size == size)
465 return tmp;
466 else {
467 printk("VFS: Wrong blocksize on device %s\n",
468 kdevname(dev));
469 return NULL;
470 }
471 return NULL;
472 }
473
474
475
476
477
478
479
480
481 struct buffer_head * get_hash_table(kdev_t dev, int block, int size)
482 {
483 struct buffer_head * bh;
484
485 for (;;) {
486 if (!(bh=find_buffer(dev,block,size)))
487 return NULL;
488 bh->b_count++;
489 wait_on_buffer(bh);
490 if (bh->b_dev == dev && bh->b_blocknr == block
491 && bh->b_size == size)
492 return bh;
493 bh->b_count--;
494 }
495 }
496
497 void set_blocksize(kdev_t dev, int size)
498 {
499 int i, nlist;
500 struct buffer_head * bh, *bhnext;
501
502 if (!blksize_size[MAJOR(dev)])
503 return;
504
505 switch(size) {
506 default: panic("Invalid blocksize passed to set_blocksize");
507 case 512: case 1024: case 2048: case 4096:;
508 }
509
510 if (blksize_size[MAJOR(dev)][MINOR(dev)] == 0 && size == BLOCK_SIZE) {
511 blksize_size[MAJOR(dev)][MINOR(dev)] = size;
512 return;
513 }
514 if (blksize_size[MAJOR(dev)][MINOR(dev)] == size)
515 return;
516 sync_buffers(dev, 2);
517 blksize_size[MAJOR(dev)][MINOR(dev)] = size;
518
519
520
521
522 for(nlist = 0; nlist < NR_LIST; nlist++) {
523 bh = lru_list[nlist];
524 for (i = nr_buffers_type[nlist]*2 ; --i > 0 ; bh = bhnext) {
525 if(!bh) break;
526 bhnext = bh->b_next_free;
527 if (bh->b_dev != dev)
528 continue;
529 if (bh->b_size == size)
530 continue;
531
532 wait_on_buffer(bh);
533 if (bh->b_dev == dev && bh->b_size != size) {
534 clear_bit(BH_Dirty, &bh->b_state);
535 clear_bit(BH_Uptodate, &bh->b_state);
536 clear_bit(BH_Req, &bh->b_state);
537 bh->b_flushtime = 0;
538 }
539 remove_from_hash_queue(bh);
540 }
541 }
542 }
543
544 #define BADNESS(bh) (buffer_dirty(bh) || buffer_locked(bh))
545
546 void refill_freelist(int size)
547 {
548 struct buffer_head * bh, * tmp;
549 struct buffer_head * candidate[NR_LIST];
550 unsigned int best_time, winner;
551 int isize = BUFSIZE_INDEX(size);
552 int buffers[NR_LIST];
553 int i;
554 int needed;
555
556
557
558
559
560 if (nr_free[isize] > 100)
561 return;
562
563
564
565
566
567
568 needed =bdf_prm.b_un.nrefill * size;
569
570 while (nr_free_pages > min_free_pages*2 && needed > 0 &&
571 grow_buffers(GFP_BUFFER, size)) {
572 needed -= PAGE_SIZE;
573 }
574
575 if(needed <= 0) return;
576
577
578
579
580 while(maybe_shrink_lav_buffers(size))
581 {
582 if(!grow_buffers(GFP_BUFFER, size)) break;
583 needed -= PAGE_SIZE;
584 if(needed <= 0) return;
585 };
586
587
588
589
590
591
592
593 repeat0:
594 for(i=0; i<NR_LIST; i++){
595 if(i == BUF_DIRTY || i == BUF_SHARED ||
596 nr_buffers_type[i] == 0) {
597 candidate[i] = NULL;
598 buffers[i] = 0;
599 continue;
600 }
601 buffers[i] = nr_buffers_type[i];
602 for (bh = lru_list[i]; buffers[i] > 0; bh = tmp, buffers[i]--)
603 {
604 if(buffers[i] < 0) panic("Here is the problem");
605 tmp = bh->b_next_free;
606 if (!bh) break;
607
608 if (mem_map[MAP_NR((unsigned long) bh->b_data)].count != 1 ||
609 buffer_dirty(bh)) {
610 refile_buffer(bh);
611 continue;
612 }
613
614 if (bh->b_count || buffer_protected(bh) || bh->b_size != size)
615 continue;
616
617
618
619
620
621 if (buffer_locked(bh) && (i == BUF_LOCKED || i == BUF_LOCKED1)) {
622 buffers[i] = 0;
623 break;
624 }
625
626 if (BADNESS(bh)) continue;
627 break;
628 };
629 if(!buffers[i]) candidate[i] = NULL;
630 else candidate[i] = bh;
631 if(candidate[i] && candidate[i]->b_count) panic("Here is the problem");
632 }
633
634 repeat:
635 if(needed <= 0) return;
636
637
638
639 winner = best_time = UINT_MAX;
640 for(i=0; i<NR_LIST; i++){
641 if(!candidate[i]) continue;
642 if(candidate[i]->b_lru_time < best_time){
643 best_time = candidate[i]->b_lru_time;
644 winner = i;
645 }
646 }
647
648
649 if(winner != UINT_MAX) {
650 i = winner;
651 bh = candidate[i];
652 candidate[i] = bh->b_next_free;
653 if(candidate[i] == bh) candidate[i] = NULL;
654 if (bh->b_count || bh->b_size != size)
655 panic("Busy buffer in candidate list\n");
656 if (mem_map[MAP_NR((unsigned long) bh->b_data)].count != 1)
657 panic("Shared buffer in candidate list\n");
658 if (buffer_protected(bh))
659 panic("Protected buffer in candidate list\n");
660 if (BADNESS(bh)) panic("Buffer in candidate list with BADNESS != 0\n");
661
662 if(bh->b_dev == B_FREE)
663 panic("Wrong list");
664 remove_from_queues(bh);
665 bh->b_dev = B_FREE;
666 put_last_free(bh);
667 needed -= bh->b_size;
668 buffers[i]--;
669 if(buffers[i] < 0) panic("Here is the problem");
670
671 if(buffers[i] == 0) candidate[i] = NULL;
672
673
674
675 if(candidate[i] && buffers[i] > 0){
676 if(buffers[i] <= 0) panic("Here is another problem");
677 for (bh = candidate[i]; buffers[i] > 0; bh = tmp, buffers[i]--) {
678 if(buffers[i] < 0) panic("Here is the problem");
679 tmp = bh->b_next_free;
680 if (!bh) break;
681
682 if (mem_map[MAP_NR((unsigned long) bh->b_data)].count != 1 ||
683 buffer_dirty(bh)) {
684 refile_buffer(bh);
685 continue;
686 };
687
688 if (bh->b_count || buffer_protected(bh) || bh->b_size != size)
689 continue;
690
691
692
693
694
695 if (buffer_locked(bh) && (i == BUF_LOCKED || i == BUF_LOCKED1)) {
696 buffers[i] = 0;
697 break;
698 }
699
700 if (BADNESS(bh)) continue;
701 break;
702 };
703 if(!buffers[i]) candidate[i] = NULL;
704 else candidate[i] = bh;
705 if(candidate[i] && candidate[i]->b_count)
706 panic("Here is the problem");
707 }
708
709 goto repeat;
710 }
711
712 if(needed <= 0) return;
713
714
715
716 if (nr_free_pages > min_free_pages + 5) {
717 if (grow_buffers(GFP_BUFFER, size)) {
718 needed -= PAGE_SIZE;
719 goto repeat0;
720 };
721 }
722
723
724 if (!grow_buffers(GFP_ATOMIC, size))
725 wakeup_bdflush(1);
726 needed -= PAGE_SIZE;
727 goto repeat0;
728 }
729
730
731
732
733
734
735
736
737
738
739
740 struct buffer_head * getblk(kdev_t dev, int block, int size)
741 {
742 struct buffer_head * bh;
743 int isize = BUFSIZE_INDEX(size);
744
745
746 buffer_usage[isize]++;
747
748
749
750
751 repeat:
752 bh = get_hash_table(dev, block, size);
753 if (bh) {
754 if (!buffer_dirty(bh)) {
755 if (buffer_uptodate(bh))
756 put_last_lru(bh);
757 bh->b_flushtime = 0;
758 }
759 set_bit(BH_Touched, &bh->b_state);
760 return bh;
761 }
762
763 while(!free_list[isize]) refill_freelist(size);
764
765 if (find_buffer(dev,block,size))
766 goto repeat;
767
768 bh = free_list[isize];
769 remove_from_free_list(bh);
770
771
772
773 bh->b_count=1;
774 bh->b_flushtime=0;
775 bh->b_state=(1<<BH_Touched);
776 bh->b_dev=dev;
777 bh->b_blocknr=block;
778 insert_into_queues(bh);
779 return bh;
780 }
781
782 void set_writetime(struct buffer_head * buf, int flag)
783 {
784 int newtime;
785
786 if (buffer_dirty(buf)) {
787
788 newtime = jiffies + (flag ? bdf_prm.b_un.age_super :
789 bdf_prm.b_un.age_buffer);
790 if(!buf->b_flushtime || buf->b_flushtime > newtime)
791 buf->b_flushtime = newtime;
792 } else {
793 buf->b_flushtime = 0;
794 }
795 }
796
797
798 void refile_buffer(struct buffer_head * buf)
799 {
800 int dispose;
801
802 if(buf->b_dev == B_FREE) {
803 printk("Attempt to refile free buffer\n");
804 return;
805 }
806 if (buffer_dirty(buf))
807 dispose = BUF_DIRTY;
808 else if ((mem_map[MAP_NR((unsigned long) buf->b_data)].count > 1) || buffer_protected(buf))
809 dispose = BUF_SHARED;
810 else if (buffer_locked(buf))
811 dispose = BUF_LOCKED;
812 else if (buf->b_list == BUF_SHARED)
813 dispose = BUF_UNSHARED;
814 else
815 dispose = BUF_CLEAN;
816 if(dispose == BUF_CLEAN) buf->b_lru_time = jiffies;
817 if(dispose != buf->b_list) {
818 if(dispose == BUF_DIRTY || dispose == BUF_UNSHARED)
819 buf->b_lru_time = jiffies;
820 if(dispose == BUF_LOCKED &&
821 (buf->b_flushtime - buf->b_lru_time) <= bdf_prm.b_un.age_super)
822 dispose = BUF_LOCKED1;
823 remove_from_queues(buf);
824 buf->b_list = dispose;
825 insert_into_queues(buf);
826 if(dispose == BUF_DIRTY && nr_buffers_type[BUF_DIRTY] >
827 (nr_buffers - nr_buffers_type[BUF_SHARED]) *
828 bdf_prm.b_un.nfract/100)
829 wakeup_bdflush(0);
830 }
831 }
832
833
834
835
836 void __brelse(struct buffer_head * buf)
837 {
838 wait_on_buffer(buf);
839
840
841 set_writetime(buf, 0);
842 refile_buffer(buf);
843
844 if (buf->b_count) {
845 if (!--buf->b_count)
846 wake_up(&buffer_wait);
847 return;
848 }
849 printk("VFS: brelse: Trying to free free buffer\n");
850 }
851
852
853
854
855
856
857 void __bforget(struct buffer_head * buf)
858 {
859 wait_on_buffer(buf);
860 mark_buffer_clean(buf);
861 clear_bit(BH_Protected, &buf->b_state);
862 buf->b_count--;
863 remove_from_hash_queue(buf);
864 buf->b_dev = NODEV;
865 refile_buffer(buf);
866 wake_up(&buffer_wait);
867 }
868
869
870
871
872
873 struct buffer_head * bread(kdev_t dev, int block, int size)
874 {
875 struct buffer_head * bh;
876
877 if (!(bh = getblk(dev, block, size))) {
878 printk("VFS: bread: READ error on device %s\n",
879 kdevname(dev));
880 return NULL;
881 }
882 if (buffer_uptodate(bh))
883 return bh;
884 ll_rw_block(READ, 1, &bh);
885 wait_on_buffer(bh);
886 if (buffer_uptodate(bh))
887 return bh;
888 brelse(bh);
889 return NULL;
890 }
891
892
893
894
895
896
897
898 #define NBUF 16
899
900 struct buffer_head * breada(kdev_t dev, int block, int bufsize,
901 unsigned int pos, unsigned int filesize)
902 {
903 struct buffer_head * bhlist[NBUF];
904 unsigned int blocks;
905 struct buffer_head * bh;
906 int index;
907 int i, j;
908
909 if (pos >= filesize)
910 return NULL;
911
912 if (block < 0 || !(bh = getblk(dev,block,bufsize)))
913 return NULL;
914
915 index = BUFSIZE_INDEX(bh->b_size);
916
917 if (buffer_uptodate(bh))
918 return bh;
919
920 blocks = ((filesize & (bufsize - 1)) - (pos & (bufsize - 1))) >> (9+index);
921
922 if (blocks > (read_ahead[MAJOR(dev)] >> index))
923 blocks = read_ahead[MAJOR(dev)] >> index;
924 if (blocks > NBUF)
925 blocks = NBUF;
926
927 bhlist[0] = bh;
928 j = 1;
929 for(i=1; i<blocks; i++) {
930 bh = getblk(dev,block+i,bufsize);
931 if (buffer_uptodate(bh)) {
932 brelse(bh);
933 break;
934 }
935 bhlist[j++] = bh;
936 }
937
938
939 ll_rw_block(READ, j, bhlist);
940
941 for(i=1; i<j; i++)
942 brelse(bhlist[i]);
943
944
945 bh = bhlist[0];
946 wait_on_buffer(bh);
947 if (buffer_uptodate(bh))
948 return bh;
949 brelse(bh);
950 return NULL;
951 }
952
953
954
955
956 static void put_unused_buffer_head(struct buffer_head * bh)
957 {
958 struct wait_queue * wait;
959
960 wait = ((volatile struct buffer_head *) bh)->b_wait;
961 memset(bh,0,sizeof(*bh));
962 ((volatile struct buffer_head *) bh)->b_wait = wait;
963 bh->b_next_free = unused_list;
964 unused_list = bh;
965 }
966
967 static void get_more_buffer_heads(void)
968 {
969 int i;
970 struct buffer_head * bh;
971
972 if (unused_list)
973 return;
974
975 if (!(bh = (struct buffer_head*) get_free_page(GFP_KERNEL)))
976 return;
977
978 for (nr_buffer_heads+=i=PAGE_SIZE/sizeof*bh ; i>0; i--) {
979 bh->b_next_free = unused_list;
980 unused_list = bh++;
981 }
982 }
983
984
985
986
987
988
989
990
991
992
993
994
995 static inline void recover_reusable_buffer_heads(void)
996 {
997 struct buffer_head *bh;
998 unsigned long flags;
999
1000 save_flags(flags);
1001 while (reuse_list) {
1002 cli();
1003 bh = reuse_list;
1004 reuse_list = bh->b_next_free;
1005 restore_flags(flags);
1006 put_unused_buffer_head(bh);
1007 }
1008 }
1009
1010 static struct buffer_head * get_unused_buffer_head(void)
1011 {
1012 struct buffer_head * bh;
1013
1014 recover_reusable_buffer_heads();
1015 get_more_buffer_heads();
1016 if (!unused_list)
1017 return NULL;
1018 bh = unused_list;
1019 unused_list = bh->b_next_free;
1020 bh->b_next_free = NULL;
1021 bh->b_data = NULL;
1022 bh->b_size = 0;
1023 bh->b_state = 0;
1024 return bh;
1025 }
1026
1027
1028
1029
1030
1031
1032
1033 static struct buffer_head * create_buffers(unsigned long page, unsigned long size)
1034 {
1035 struct buffer_head *bh, *head;
1036 unsigned long offset;
1037
1038 head = NULL;
1039 offset = PAGE_SIZE;
1040 while ((offset -= size) < PAGE_SIZE) {
1041 bh = get_unused_buffer_head();
1042 if (!bh)
1043 goto no_grow;
1044 bh->b_this_page = head;
1045 head = bh;
1046 bh->b_data = (char *) (page+offset);
1047 bh->b_size = size;
1048 bh->b_dev = B_FREE;
1049 }
1050 return head;
1051
1052
1053
1054 no_grow:
1055 bh = head;
1056 while (bh) {
1057 head = bh;
1058 bh = bh->b_this_page;
1059 put_unused_buffer_head(head);
1060 }
1061 return NULL;
1062 }
1063
1064 int brw_page(int rw, unsigned long address, kdev_t dev, int b[], int size, int bmap)
1065 {
1066 struct buffer_head *bh, *prev, *next, *arr[MAX_BUF_PER_PAGE];
1067 int block, nr;
1068 struct page *page;
1069
1070 page = mem_map + MAP_NR(address);
1071 page->uptodate = 0;
1072 bh = create_buffers(address, size);
1073 if (!bh)
1074 return -ENOMEM;
1075 nr = 0;
1076 next = bh;
1077 do {
1078 struct buffer_head * tmp;
1079 block = *(b++);
1080
1081 set_bit(BH_FreeOnIO, &next->b_state);
1082 next->b_list = BUF_CLEAN;
1083 next->b_dev = dev;
1084 next->b_blocknr = block;
1085 next->b_count = 1;
1086 next->b_flushtime = 0;
1087 set_bit(BH_Uptodate, &next->b_state);
1088
1089
1090
1091
1092
1093
1094 if (bmap && !block) {
1095 memset(next->b_data, 0, size);
1096 next->b_count--;
1097 continue;
1098 }
1099 tmp = get_hash_table(dev, block, size);
1100 if (tmp) {
1101 if (!buffer_uptodate(tmp)) {
1102 if (rw == READ)
1103 ll_rw_block(READ, 1, &tmp);
1104 wait_on_buffer(tmp);
1105 }
1106 if (rw == READ)
1107 memcpy(next->b_data, tmp->b_data, size);
1108 else {
1109 memcpy(tmp->b_data, next->b_data, size);
1110 set_bit(BH_Dirty, &tmp->b_state);
1111 }
1112 brelse(tmp);
1113 next->b_count--;
1114 continue;
1115 }
1116 if (rw == READ)
1117 clear_bit(BH_Uptodate, &next->b_state);
1118 else
1119 set_bit(BH_Dirty, &next->b_state);
1120 arr[nr++] = next;
1121 } while (prev = next, (next = next->b_this_page) != NULL);
1122 prev->b_this_page = bh;
1123
1124 if (nr)
1125 ll_rw_block(rw, nr, arr);
1126 else {
1127 page->locked = 0;
1128 page->uptodate = 1;
1129 wake_up(&page->wait);
1130 next = bh;
1131 do {
1132 next->b_next_free = reuse_list;
1133 reuse_list = next;
1134 next = next->b_this_page;
1135 } while (next != bh);
1136 }
1137 ++current->maj_flt;
1138 return 0;
1139 }
1140
1141 void mark_buffer_uptodate(struct buffer_head * bh, int on)
1142 {
1143 if (on) {
1144 struct buffer_head *tmp = bh;
1145 int page_uptodate = 1;
1146 set_bit(BH_Uptodate, &bh->b_state);
1147 do {
1148 if (!test_bit(BH_Uptodate, &tmp->b_state)) {
1149 page_uptodate = 0;
1150 break;
1151 }
1152 tmp=tmp->b_this_page;
1153 } while (tmp && tmp != bh);
1154 if (page_uptodate)
1155 mem_map[MAP_NR(bh->b_data)].uptodate = 1;
1156 } else
1157 clear_bit(BH_Uptodate, &bh->b_state);
1158 }
1159
1160 void unlock_buffer(struct buffer_head * bh)
1161 {
1162 struct buffer_head *tmp;
1163 unsigned long flags;
1164 struct page *page;
1165
1166 clear_bit(BH_Lock, &bh->b_state);
1167 wake_up(&bh->b_wait);
1168
1169 if (!test_bit(BH_FreeOnIO, &bh->b_state))
1170 return;
1171 page = mem_map + MAP_NR(bh->b_data);
1172 if (!page->locked) {
1173 printk ("Whoops: unlock_buffer: "
1174 "async io complete on unlocked page\n");
1175 return;
1176 }
1177 if (bh->b_count != 1) {
1178 printk ("Whoops: unlock_buffer: b_count != 1 on async io.\n");
1179 return;
1180 }
1181
1182
1183
1184
1185 bh->b_count--;
1186 for (tmp = bh; tmp=tmp->b_this_page, tmp!=bh; ) {
1187 if (test_bit(BH_Lock, &tmp->b_state) || tmp->b_count)
1188 return;
1189 }
1190
1191
1192 save_flags(flags);
1193 page->locked = 0;
1194 wake_up(&page->wait);
1195 cli();
1196 tmp = bh;
1197 do {
1198 if (!test_bit(BH_FreeOnIO, &tmp->b_state)) {
1199 printk ("Whoops: unlock_buffer: "
1200 "async IO mismatch on page.\n");
1201 restore_flags(flags);
1202 return;
1203 }
1204 tmp->b_next_free = reuse_list;
1205 reuse_list = tmp;
1206 clear_bit(BH_FreeOnIO, &tmp->b_state);
1207 tmp = tmp->b_this_page;
1208 } while (tmp != bh);
1209 restore_flags(flags);
1210 if (page->free_after) {
1211 extern int nr_async_pages;
1212 nr_async_pages--;
1213 page->free_after = 0;
1214 free_page(page_address(page));
1215 }
1216 }
1217
1218
1219
1220
1221
1222
1223
1224
1225 int generic_readpage(struct inode * inode, struct page * page)
1226 {
1227 unsigned long block, address;
1228 int *p, nr[PAGE_SIZE/512];
1229 int i;
1230
1231 address = page_address(page);
1232 page->count++;
1233 wait_on_page(page);
1234 if (page->uptodate) {
1235 free_page(address);
1236 return 0;
1237 }
1238 page->locked = 1;
1239
1240 i = PAGE_SIZE >> inode->i_sb->s_blocksize_bits;
1241 block = page->offset >> inode->i_sb->s_blocksize_bits;
1242 p = nr;
1243 do {
1244 *p = inode->i_op->bmap(inode, block);
1245 i--;
1246 block++;
1247 p++;
1248 } while (i > 0);
1249
1250
1251 brw_page(READ, address, inode->i_dev, nr, inode->i_sb->s_blocksize, 1);
1252 free_page(address);
1253 return 0;
1254 }
1255
1256
1257
1258
1259
1260 static int grow_buffers(int pri, int size)
1261 {
1262 unsigned long page;
1263 struct buffer_head *bh, *tmp;
1264 struct buffer_head * insert_point;
1265 int isize;
1266
1267 if ((size & 511) || (size > PAGE_SIZE)) {
1268 printk("VFS: grow_buffers: size = %d\n",size);
1269 return 0;
1270 }
1271
1272 isize = BUFSIZE_INDEX(size);
1273
1274 if (!(page = __get_free_page(pri)))
1275 return 0;
1276 bh = create_buffers(page, size);
1277 if (!bh) {
1278 free_page(page);
1279 return 0;
1280 }
1281
1282 insert_point = free_list[isize];
1283
1284 tmp = bh;
1285 while (1) {
1286 nr_free[isize]++;
1287 if (insert_point) {
1288 tmp->b_next_free = insert_point->b_next_free;
1289 tmp->b_prev_free = insert_point;
1290 insert_point->b_next_free->b_prev_free = tmp;
1291 insert_point->b_next_free = tmp;
1292 } else {
1293 tmp->b_prev_free = tmp;
1294 tmp->b_next_free = tmp;
1295 }
1296 insert_point = tmp;
1297 ++nr_buffers;
1298 if (tmp->b_this_page)
1299 tmp = tmp->b_this_page;
1300 else
1301 break;
1302 }
1303 free_list[isize] = bh;
1304 buffer_pages[MAP_NR(page)] = bh;
1305 tmp->b_this_page = bh;
1306 wake_up(&buffer_wait);
1307 buffermem += PAGE_SIZE;
1308 return 1;
1309 }
1310
1311
1312
1313
1314
1315
1316
1317
1318 int try_to_free_buffer(struct buffer_head * bh, struct buffer_head ** bhp,
1319 int priority)
1320 {
1321 unsigned long page;
1322 struct buffer_head * tmp, * p;
1323 int isize = BUFSIZE_INDEX(bh->b_size);
1324
1325 *bhp = bh;
1326 page = (unsigned long) bh->b_data;
1327 page &= PAGE_MASK;
1328 tmp = bh;
1329 do {
1330 if (!tmp)
1331 return 0;
1332 if (tmp->b_count || buffer_protected(tmp) ||
1333 buffer_dirty(tmp) || buffer_locked(tmp) || tmp->b_wait)
1334 return 0;
1335 if (priority && buffer_touched(tmp))
1336 return 0;
1337 tmp = tmp->b_this_page;
1338 } while (tmp != bh);
1339 tmp = bh;
1340 do {
1341 p = tmp;
1342 tmp = tmp->b_this_page;
1343 nr_buffers--;
1344 nr_buffers_size[isize]--;
1345 if (p == *bhp)
1346 {
1347 *bhp = p->b_prev_free;
1348 if (p == *bhp)
1349 *bhp = NULL;
1350 }
1351 remove_from_queues(p);
1352 put_unused_buffer_head(p);
1353 } while (tmp != bh);
1354 buffermem -= PAGE_SIZE;
1355 buffer_pages[MAP_NR(page)] = NULL;
1356 free_page(page);
1357 return !mem_map[MAP_NR(page)].count;
1358 }
1359
1360
1361
1362 static inline void age_buffer(struct buffer_head *bh)
1363 {
1364 struct buffer_head *tmp = bh;
1365 int touched = 0;
1366
1367
1368
1369
1370
1371
1372
1373
1374 if (clear_bit(BH_Has_aged, &bh->b_state))
1375 return;
1376
1377 do {
1378 touched |= clear_bit(BH_Touched, &tmp->b_state);
1379 tmp = tmp->b_this_page;
1380 set_bit(BH_Has_aged, &tmp->b_state);
1381 } while (tmp != bh);
1382 clear_bit(BH_Has_aged, &bh->b_state);
1383
1384 if (touched)
1385 touch_page(mem_map + MAP_NR((unsigned long) bh->b_data));
1386 else
1387 age_page(mem_map + MAP_NR((unsigned long) bh->b_data));
1388 }
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402 static int maybe_shrink_lav_buffers(int size)
1403 {
1404 int nlist;
1405 int isize;
1406 int total_lav, total_n_buffers, n_sizes;
1407
1408
1409
1410
1411
1412
1413 total_lav = total_n_buffers = n_sizes = 0;
1414 for(nlist = 0; nlist < NR_SIZES; nlist++)
1415 {
1416 total_lav += buffers_lav[nlist];
1417 if(nr_buffers_size[nlist]) n_sizes++;
1418 total_n_buffers += nr_buffers_size[nlist];
1419 total_n_buffers -= nr_buffers_st[nlist][BUF_SHARED];
1420 }
1421
1422
1423
1424
1425 isize = (size ? BUFSIZE_INDEX(size) : -1);
1426
1427 if (n_sizes > 1)
1428 for(nlist = 0; nlist < NR_SIZES; nlist++)
1429 {
1430 if(nlist == isize) continue;
1431 if(nr_buffers_size[nlist] &&
1432 bdf_prm.b_un.lav_const * buffers_lav[nlist]*total_n_buffers <
1433 total_lav * (nr_buffers_size[nlist] - nr_buffers_st[nlist][BUF_SHARED]))
1434 if(shrink_specific_buffers(6, bufferindex_size[nlist]))
1435 return 1;
1436 }
1437 return 0;
1438 }
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452 static int shrink_specific_buffers(unsigned int priority, int size)
1453 {
1454 struct buffer_head *bh;
1455 int nlist;
1456 int i, isize, isize1;
1457
1458 #ifdef DEBUG
1459 if(size) printk("Shrinking buffers of size %d\n", size);
1460 #endif
1461
1462
1463 isize1 = (size ? BUFSIZE_INDEX(size) : -1);
1464
1465 for(isize = 0; isize<NR_SIZES; isize++){
1466 if(isize1 != -1 && isize1 != isize) continue;
1467 bh = free_list[isize];
1468 if(!bh) continue;
1469 for (i=0 ; !i || bh != free_list[isize]; bh = bh->b_next_free, i++) {
1470 if (bh->b_count || buffer_protected(bh) ||
1471 !bh->b_this_page)
1472 continue;
1473 if (!age_of((unsigned long) bh->b_data) &&
1474 try_to_free_buffer(bh, &bh, 6))
1475 return 1;
1476 if(!bh) break;
1477
1478
1479 }
1480 }
1481
1482
1483
1484 for(nlist = 0; nlist < NR_LIST; nlist++) {
1485 repeat1:
1486 if(priority > 2 && nlist == BUF_SHARED) continue;
1487 i = nr_buffers_type[nlist];
1488 i = ((BUFFEROUT_WEIGHT * i) >> 10) >> priority;
1489 for ( ; i > 0; i-- ) {
1490 bh = next_to_age[nlist];
1491 if (!bh)
1492 break;
1493 next_to_age[nlist] = bh->b_next_free;
1494
1495
1496 age_buffer(bh);
1497
1498
1499 if(bh->b_list != nlist) goto repeat1;
1500 if (bh->b_count || buffer_protected(bh) ||
1501 !bh->b_this_page)
1502 continue;
1503 if(size && bh->b_size != size) continue;
1504 if (buffer_locked(bh))
1505 if (priority)
1506 continue;
1507 else
1508 wait_on_buffer(bh);
1509 if (buffer_dirty(bh)) {
1510 bh->b_count++;
1511 bh->b_flushtime = 0;
1512 ll_rw_block(WRITEA, 1, &bh);
1513 bh->b_count--;
1514 continue;
1515 }
1516
1517
1518
1519 if ((age_of((unsigned long) bh->b_data) >>
1520 (6-priority)) > 0)
1521 continue;
1522 if (try_to_free_buffer(bh, &bh, 0))
1523 return 1;
1524 if(!bh) break;
1525 }
1526 }
1527 return 0;
1528 }
1529
1530
1531
1532
1533 void show_buffers(void)
1534 {
1535 struct buffer_head * bh;
1536 int found = 0, locked = 0, dirty = 0, used = 0, lastused = 0;
1537 int protected = 0;
1538 int shared;
1539 int nlist, isize;
1540
1541 printk("Buffer memory: %6dkB\n",buffermem>>10);
1542 printk("Buffer heads: %6d\n",nr_buffer_heads);
1543 printk("Buffer blocks: %6d\n",nr_buffers);
1544
1545 for(nlist = 0; nlist < NR_LIST; nlist++) {
1546 shared = found = locked = dirty = used = lastused = protected = 0;
1547 bh = lru_list[nlist];
1548 if(!bh) continue;
1549 do {
1550 found++;
1551 if (buffer_locked(bh))
1552 locked++;
1553 if (buffer_protected(bh))
1554 protected++;
1555 if (buffer_dirty(bh))
1556 dirty++;
1557 if(mem_map[MAP_NR(((unsigned long) bh->b_data))].count !=1) shared++;
1558 if (bh->b_count)
1559 used++, lastused = found;
1560 bh = bh->b_next_free;
1561 } while (bh != lru_list[nlist]);
1562 printk("Buffer[%d] mem: %d buffers, %d used (last=%d), %d locked, "
1563 "%d protected, %d dirty %d shrd\n",
1564 nlist, found, used, lastused, locked, protected, dirty, shared);
1565 };
1566 printk("Size [LAV] Free Clean Unshar Lck Lck1 Dirty Shared \n");
1567 for(isize = 0; isize<NR_SIZES; isize++){
1568 printk("%5d [%5d]: %7d ", bufferindex_size[isize],
1569 buffers_lav[isize], nr_free[isize]);
1570 for(nlist = 0; nlist < NR_LIST; nlist++)
1571 printk("%7d ", nr_buffers_st[isize][nlist]);
1572 printk("\n");
1573 }
1574 }
1575
1576
1577
1578
1579
1580
1581
1582
1583 static inline int try_to_reassign(struct buffer_head * bh, struct buffer_head ** bhp,
1584 kdev_t dev, unsigned int starting_block)
1585 {
1586 unsigned long page;
1587 struct buffer_head * tmp, * p;
1588
1589 *bhp = bh;
1590 page = (unsigned long) bh->b_data;
1591 page &= PAGE_MASK;
1592 if(mem_map[MAP_NR(page)].count != 1) return 0;
1593 tmp = bh;
1594 do {
1595 if (!tmp)
1596 return 0;
1597
1598 if (tmp->b_count || buffer_protected(tmp) ||
1599 buffer_dirty(tmp) || buffer_locked(tmp))
1600 return 0;
1601 tmp = tmp->b_this_page;
1602 } while (tmp != bh);
1603 tmp = bh;
1604
1605 while((unsigned long) tmp->b_data & (PAGE_SIZE - 1))
1606 tmp = tmp->b_this_page;
1607
1608
1609 bh = tmp;
1610 do {
1611 p = tmp;
1612 tmp = tmp->b_this_page;
1613 remove_from_queues(p);
1614 p->b_dev = dev;
1615 mark_buffer_uptodate(p, 0);
1616 clear_bit(BH_Req, &p->b_state);
1617 p->b_blocknr = starting_block++;
1618 insert_into_queues(p);
1619 } while (tmp != bh);
1620 return 1;
1621 }
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637 static int reassign_cluster(kdev_t dev,
1638 unsigned int starting_block, int size)
1639 {
1640 struct buffer_head *bh;
1641 int isize = BUFSIZE_INDEX(size);
1642 int i;
1643
1644
1645
1646
1647
1648 while(nr_free[isize] < 32) refill_freelist(size);
1649
1650 bh = free_list[isize];
1651 if(bh)
1652 for (i=0 ; !i || bh != free_list[isize] ; bh = bh->b_next_free, i++) {
1653 if (!bh->b_this_page) continue;
1654 if (try_to_reassign(bh, &bh, dev, starting_block))
1655 return 4;
1656 }
1657 return 0;
1658 }
1659
1660
1661
1662
1663
1664 static unsigned long try_to_generate_cluster(kdev_t dev, int block, int size)
1665 {
1666 struct buffer_head * bh, * tmp, * arr[MAX_BUF_PER_PAGE];
1667 int isize = BUFSIZE_INDEX(size);
1668 unsigned long offset;
1669 unsigned long page;
1670 int nblock;
1671
1672 page = get_free_page(GFP_NOBUFFER);
1673 if(!page) return 0;
1674
1675 bh = create_buffers(page, size);
1676 if (!bh) {
1677 free_page(page);
1678 return 0;
1679 };
1680 nblock = block;
1681 for (offset = 0 ; offset < PAGE_SIZE ; offset += size) {
1682 if (find_buffer(dev, nblock++, size))
1683 goto not_aligned;
1684 }
1685 tmp = bh;
1686 nblock = 0;
1687 while (1) {
1688 arr[nblock++] = bh;
1689 bh->b_count = 1;
1690 bh->b_flushtime = 0;
1691 bh->b_state = 0;
1692 bh->b_dev = dev;
1693 bh->b_list = BUF_CLEAN;
1694 bh->b_blocknr = block++;
1695 nr_buffers++;
1696 nr_buffers_size[isize]++;
1697 insert_into_queues(bh);
1698 if (bh->b_this_page)
1699 bh = bh->b_this_page;
1700 else
1701 break;
1702 }
1703 buffermem += PAGE_SIZE;
1704 buffer_pages[MAP_NR(page)] = bh;
1705 bh->b_this_page = tmp;
1706 while (nblock-- > 0)
1707 brelse(arr[nblock]);
1708 return 4;
1709 not_aligned:
1710 while ((tmp = bh) != NULL) {
1711 bh = bh->b_this_page;
1712 put_unused_buffer_head(tmp);
1713 }
1714 free_page(page);
1715 return 0;
1716 }
1717
1718 unsigned long generate_cluster(kdev_t dev, int b[], int size)
1719 {
1720 int i, offset;
1721
1722 for (i = 0, offset = 0 ; offset < PAGE_SIZE ; i++, offset += size) {
1723 if(i && b[i]-1 != b[i-1]) return 0;
1724 if(find_buffer(dev, b[i], size)) return 0;
1725 };
1726
1727
1728
1729
1730
1731 if(maybe_shrink_lav_buffers(size))
1732 {
1733 int retval;
1734 retval = try_to_generate_cluster(dev, b[0], size);
1735 if(retval) return retval;
1736 };
1737
1738 if (nr_free_pages > min_free_pages*2)
1739 return try_to_generate_cluster(dev, b[0], size);
1740 else
1741 return reassign_cluster(dev, b[0], size);
1742 }
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754 void buffer_init(void)
1755 {
1756 int i;
1757 int isize = BUFSIZE_INDEX(BLOCK_SIZE);
1758 long memsize = MAP_NR(high_memory) << PAGE_SHIFT;
1759
1760 if (memsize >= 4*1024*1024) {
1761 if(memsize >= 16*1024*1024)
1762 nr_hash = 16381;
1763 else
1764 nr_hash = 4093;
1765 } else {
1766 nr_hash = 997;
1767 };
1768
1769 hash_table = (struct buffer_head **) vmalloc(nr_hash *
1770 sizeof(struct buffer_head *));
1771
1772
1773 buffer_pages = (struct buffer_head **) vmalloc(MAP_NR(high_memory) *
1774 sizeof(struct buffer_head *));
1775 for (i = 0 ; i < MAP_NR(high_memory) ; i++)
1776 buffer_pages[i] = NULL;
1777
1778 for (i = 0 ; i < nr_hash ; i++)
1779 hash_table[i] = NULL;
1780 lru_list[BUF_CLEAN] = 0;
1781 grow_buffers(GFP_KERNEL, BLOCK_SIZE);
1782 if (!free_list[isize])
1783 panic("VFS: Unable to initialize buffer free list!");
1784 return;
1785 }
1786
1787
1788
1789
1790
1791
1792
1793
1794 struct wait_queue * bdflush_wait = NULL;
1795 struct wait_queue * bdflush_done = NULL;
1796
1797 static void wakeup_bdflush(int wait)
1798 {
1799 wake_up(&bdflush_wait);
1800 if(wait) sleep_on(&bdflush_done);
1801 }
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812 asmlinkage int sync_old_buffers(void)
1813 {
1814 int i, isize;
1815 int ndirty, nwritten;
1816 int nlist;
1817 int ncount;
1818 struct buffer_head * bh, *next;
1819
1820 sync_supers(0);
1821 sync_inodes(0);
1822
1823 ncount = 0;
1824 #ifdef DEBUG
1825 for(nlist = 0; nlist < NR_LIST; nlist++)
1826 #else
1827 for(nlist = BUF_DIRTY; nlist <= BUF_DIRTY; nlist++)
1828 #endif
1829 {
1830 ndirty = 0;
1831 nwritten = 0;
1832 repeat:
1833 bh = lru_list[nlist];
1834 if(bh)
1835 for (i = nr_buffers_type[nlist]; i-- > 0; bh = next) {
1836
1837 if(bh->b_list != nlist) goto repeat;
1838 next = bh->b_next_free;
1839 if(!lru_list[nlist]) {
1840 printk("Dirty list empty %d\n", i);
1841 break;
1842 }
1843
1844
1845 if (nlist == BUF_DIRTY && !buffer_dirty(bh) && !buffer_locked(bh))
1846 {
1847 refile_buffer(bh);
1848 continue;
1849 }
1850
1851 if (buffer_locked(bh) || !buffer_dirty(bh))
1852 continue;
1853 ndirty++;
1854 if(bh->b_flushtime > jiffies) continue;
1855 nwritten++;
1856 bh->b_count++;
1857 bh->b_flushtime = 0;
1858 #ifdef DEBUG
1859 if(nlist != BUF_DIRTY) ncount++;
1860 #endif
1861 ll_rw_block(WRITE, 1, &bh);
1862 bh->b_count--;
1863 }
1864 }
1865 #ifdef DEBUG
1866 if (ncount) printk("sync_old_buffers: %d dirty buffers not on dirty list\n", ncount);
1867 printk("Wrote %d/%d buffers\n", nwritten, ndirty);
1868 #endif
1869
1870
1871
1872
1873 for(isize = 0; isize<NR_SIZES; isize++){
1874 CALC_LOAD(buffers_lav[isize], bdf_prm.b_un.lav_const, buffer_usage[isize]);
1875 buffer_usage[isize] = 0;
1876 };
1877 return 0;
1878 }
1879
1880
1881
1882
1883
1884
1885
1886 asmlinkage int sys_bdflush(int func, long data)
1887 {
1888 int i, error;
1889
1890 if (!suser())
1891 return -EPERM;
1892
1893 if (func == 1)
1894 return sync_old_buffers();
1895
1896
1897 if (func >= 2) {
1898 i = (func-2) >> 1;
1899 if (i < 0 || i >= N_PARAM)
1900 return -EINVAL;
1901 if((func & 1) == 0) {
1902 error = verify_area(VERIFY_WRITE, (void *) data, sizeof(int));
1903 if (error)
1904 return error;
1905 put_user(bdf_prm.data[i], (int*)data);
1906 return 0;
1907 };
1908 if (data < bdflush_min[i] || data > bdflush_max[i])
1909 return -EINVAL;
1910 bdf_prm.data[i] = data;
1911 return 0;
1912 };
1913
1914
1915
1916
1917
1918 return 0;
1919 }
1920
1921
1922
1923
1924
1925 int bdflush(void * unused)
1926 {
1927 int i;
1928 int ndirty;
1929 int nlist;
1930 int ncount;
1931 struct buffer_head * bh, *next;
1932
1933
1934
1935
1936
1937
1938
1939 current->session = 1;
1940 current->pgrp = 1;
1941 sprintf(current->comm, "kflushd");
1942
1943
1944
1945
1946
1947
1948
1949 #ifdef __SMP__
1950 lock_kernel();
1951 syscall_count++;
1952 #endif
1953
1954 for (;;) {
1955 #ifdef DEBUG
1956 printk("bdflush() activated...");
1957 #endif
1958
1959 ncount = 0;
1960 #ifdef DEBUG
1961 for(nlist = 0; nlist < NR_LIST; nlist++)
1962 #else
1963 for(nlist = BUF_DIRTY; nlist <= BUF_DIRTY; nlist++)
1964 #endif
1965 {
1966 ndirty = 0;
1967 repeat:
1968 bh = lru_list[nlist];
1969 if(bh)
1970 for (i = nr_buffers_type[nlist]; i-- > 0 && ndirty < bdf_prm.b_un.ndirty;
1971 bh = next) {
1972
1973 if(bh->b_list != nlist) goto repeat;
1974 next = bh->b_next_free;
1975 if(!lru_list[nlist]) {
1976 printk("Dirty list empty %d\n", i);
1977 break;
1978 }
1979
1980
1981 if (nlist == BUF_DIRTY && !buffer_dirty(bh) && !buffer_locked(bh))
1982 {
1983 refile_buffer(bh);
1984 continue;
1985 }
1986
1987 if (buffer_locked(bh) || !buffer_dirty(bh))
1988 continue;
1989
1990
1991 bh->b_count++;
1992 ndirty++;
1993 bh->b_flushtime = 0;
1994 ll_rw_block(WRITE, 1, &bh);
1995 #ifdef DEBUG
1996 if(nlist != BUF_DIRTY) ncount++;
1997 #endif
1998 bh->b_count--;
1999 }
2000 }
2001 #ifdef DEBUG
2002 if (ncount) printk("sys_bdflush: %d dirty buffers not on dirty list\n", ncount);
2003 printk("sleeping again.\n");
2004 #endif
2005 wake_up(&bdflush_done);
2006
2007
2008
2009
2010 if(nr_buffers_type[BUF_DIRTY] <= (nr_buffers - nr_buffers_type[BUF_SHARED]) *
2011 bdf_prm.b_un.nfract/100) {
2012 current->signal = 0;
2013 interruptible_sleep_on(&bdflush_wait);
2014 }
2015 }
2016 }
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034