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
- bread_page
- mark_buffer_uptodate
- generic_readpage
- bwrite_page
- 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 cli();
1002 while (reuse_list) {
1003 bh = reuse_list;
1004 reuse_list = bh->b_next_free;
1005 restore_flags(flags);
1006 put_unused_buffer_head(bh);
1007 cli();
1008 }
1009 }
1010
1011 static struct buffer_head * get_unused_buffer_head(void)
1012 {
1013 struct buffer_head * bh;
1014
1015 recover_reusable_buffer_heads();
1016 get_more_buffer_heads();
1017 if (!unused_list)
1018 return NULL;
1019 bh = unused_list;
1020 unused_list = bh->b_next_free;
1021 bh->b_next_free = NULL;
1022 bh->b_data = NULL;
1023 bh->b_size = 0;
1024 bh->b_state = 0;
1025 return bh;
1026 }
1027
1028
1029
1030
1031
1032
1033
1034 static struct buffer_head * create_buffers(unsigned long page, unsigned long size)
1035 {
1036 struct buffer_head *bh, *head;
1037 unsigned long offset;
1038
1039 head = NULL;
1040 offset = PAGE_SIZE;
1041 while ((offset -= size) < PAGE_SIZE) {
1042 bh = get_unused_buffer_head();
1043 if (!bh)
1044 goto no_grow;
1045 bh->b_this_page = head;
1046 head = bh;
1047 bh->b_data = (char *) (page+offset);
1048 bh->b_size = size;
1049 bh->b_dev = B_FREE;
1050 }
1051 return head;
1052
1053
1054
1055 no_grow:
1056 bh = head;
1057 while (bh) {
1058 head = bh;
1059 bh = bh->b_this_page;
1060 put_unused_buffer_head(head);
1061 }
1062 return NULL;
1063 }
1064
1065 static int bread_page(unsigned long address, kdev_t dev, int b[], int size)
1066 {
1067 struct buffer_head *bh, *prev, *next, *arr[MAX_BUF_PER_PAGE];
1068 int block, nr;
1069 struct page *page;
1070
1071 page = mem_map + MAP_NR(address);
1072 page->uptodate = 0;
1073 bh = create_buffers(address, size);
1074 if (!bh)
1075 return -ENOMEM;
1076 nr = 0;
1077 next = bh;
1078 do {
1079 struct buffer_head * tmp;
1080 block = *(b++);
1081
1082 set_bit(BH_FreeOnIO, &next->b_state);
1083 next->b_list = BUF_CLEAN;
1084 next->b_dev = dev;
1085 next->b_blocknr = block;
1086 next->b_count = 1;
1087 next->b_flushtime = 0;
1088 clear_bit(BH_Dirty, &next->b_state);
1089 clear_bit(BH_Req, &next->b_state);
1090 set_bit(BH_Uptodate, &next->b_state);
1091
1092 if (!block) {
1093 memset(next->b_data, 0, size);
1094 continue;
1095 }
1096 tmp = get_hash_table(dev, block, size);
1097 if (tmp) {
1098 if (!buffer_uptodate(tmp)) {
1099 ll_rw_block(READ, 1, &tmp);
1100 wait_on_buffer(tmp);
1101 }
1102 memcpy(next->b_data, tmp->b_data, size);
1103 brelse(tmp);
1104 continue;
1105 }
1106 clear_bit(BH_Uptodate, &next->b_state);
1107 arr[nr++] = next;
1108 } while (prev = next, (next = next->b_this_page) != NULL);
1109 prev->b_this_page = bh;
1110
1111 if (nr)
1112 ll_rw_block(READ, nr, arr);
1113 else {
1114 page->locked = 0;
1115 page->uptodate = 1;
1116 wake_up(&page->wait);
1117 }
1118 ++current->maj_flt;
1119 return 0;
1120 }
1121
1122 void mark_buffer_uptodate(struct buffer_head * bh, int on)
1123 {
1124 if (on) {
1125 struct buffer_head *tmp = bh;
1126 int page_uptodate = 1;
1127 set_bit(BH_Uptodate, &bh->b_state);
1128 do {
1129 if (!test_bit(BH_Uptodate, &tmp->b_state)) {
1130 page_uptodate = 0;
1131 break;
1132 }
1133 tmp=tmp->b_this_page;
1134 } while (tmp && tmp != bh);
1135 if (page_uptodate)
1136 mem_map[MAP_NR(bh->b_data)].uptodate = 1;
1137 } else
1138 clear_bit(BH_Uptodate, &bh->b_state);
1139 }
1140
1141
1142
1143
1144
1145
1146
1147
1148 int generic_readpage(struct inode * inode, struct page * page)
1149 {
1150 unsigned long block, address;
1151 int *p, nr[PAGE_SIZE/512];
1152 int i;
1153
1154 wait_on_page(page);
1155 page->locked = 1;
1156
1157 i = PAGE_SIZE >> inode->i_sb->s_blocksize_bits;
1158 block = page->offset >> inode->i_sb->s_blocksize_bits;
1159 p = nr;
1160 do {
1161 *p = inode->i_op->bmap(inode, block);
1162 i--;
1163 block++;
1164 p++;
1165 } while (i > 0);
1166
1167
1168 page->count++;
1169 address = page_address(page);
1170 bread_page(address, inode->i_dev, nr, inode->i_sb->s_blocksize);
1171 free_page(address);
1172 return 0;
1173 }
1174
1175 #if 0
1176
1177
1178
1179
1180 void bwrite_page(unsigned long address, kdev_t dev, int b[], int size)
1181 {
1182 struct buffer_head * bh[MAX_BUF_PER_PAGE];
1183 int i, j;
1184
1185 for (i=0, j=0; j<PAGE_SIZE ; i++, j+= size) {
1186 bh[i] = NULL;
1187 if (b[i])
1188 bh[i] = getblk(dev, b[i], size);
1189 }
1190 for (i=0, j=0; j<PAGE_SIZE ; i++, j += size, address += size) {
1191 if (bh[i]) {
1192 memcpy(bh[i]->b_data, (void *) address, size);
1193 mark_buffer_uptodate(bh[i], 1);
1194 mark_buffer_dirty(bh[i], 0);
1195 brelse(bh[i]);
1196 } else
1197 memset((void *) address, 0, size);
1198 }
1199 }
1200 #endif
1201
1202
1203
1204
1205
1206 static int grow_buffers(int pri, int size)
1207 {
1208 unsigned long page;
1209 struct buffer_head *bh, *tmp;
1210 struct buffer_head * insert_point;
1211 int isize;
1212
1213 if ((size & 511) || (size > PAGE_SIZE)) {
1214 printk("VFS: grow_buffers: size = %d\n",size);
1215 return 0;
1216 }
1217
1218 isize = BUFSIZE_INDEX(size);
1219
1220 if (!(page = __get_free_page(pri)))
1221 return 0;
1222 bh = create_buffers(page, size);
1223 if (!bh) {
1224 free_page(page);
1225 return 0;
1226 }
1227
1228 insert_point = free_list[isize];
1229
1230 tmp = bh;
1231 while (1) {
1232 nr_free[isize]++;
1233 if (insert_point) {
1234 tmp->b_next_free = insert_point->b_next_free;
1235 tmp->b_prev_free = insert_point;
1236 insert_point->b_next_free->b_prev_free = tmp;
1237 insert_point->b_next_free = tmp;
1238 } else {
1239 tmp->b_prev_free = tmp;
1240 tmp->b_next_free = tmp;
1241 }
1242 insert_point = tmp;
1243 ++nr_buffers;
1244 if (tmp->b_this_page)
1245 tmp = tmp->b_this_page;
1246 else
1247 break;
1248 }
1249 free_list[isize] = bh;
1250 buffer_pages[MAP_NR(page)] = bh;
1251 tmp->b_this_page = bh;
1252 wake_up(&buffer_wait);
1253 buffermem += PAGE_SIZE;
1254 return 1;
1255 }
1256
1257
1258
1259
1260
1261
1262
1263
1264 int try_to_free_buffer(struct buffer_head * bh, struct buffer_head ** bhp,
1265 int priority)
1266 {
1267 unsigned long page;
1268 struct buffer_head * tmp, * p;
1269 int isize = BUFSIZE_INDEX(bh->b_size);
1270
1271 *bhp = bh;
1272 page = (unsigned long) bh->b_data;
1273 page &= PAGE_MASK;
1274 tmp = bh;
1275 do {
1276 if (!tmp)
1277 return 0;
1278 if (tmp->b_count || buffer_protected(tmp) ||
1279 buffer_dirty(tmp) || buffer_locked(tmp) || tmp->b_wait)
1280 return 0;
1281 if (priority && buffer_touched(tmp))
1282 return 0;
1283 tmp = tmp->b_this_page;
1284 } while (tmp != bh);
1285 tmp = bh;
1286 do {
1287 p = tmp;
1288 tmp = tmp->b_this_page;
1289 nr_buffers--;
1290 nr_buffers_size[isize]--;
1291 if (p == *bhp)
1292 {
1293 *bhp = p->b_prev_free;
1294 if (p == *bhp)
1295 *bhp = NULL;
1296 }
1297 remove_from_queues(p);
1298 put_unused_buffer_head(p);
1299 } while (tmp != bh);
1300 buffermem -= PAGE_SIZE;
1301 buffer_pages[MAP_NR(page)] = NULL;
1302 free_page(page);
1303 return !mem_map[MAP_NR(page)].count;
1304 }
1305
1306
1307
1308 static inline void age_buffer(struct buffer_head *bh)
1309 {
1310 struct buffer_head *tmp = bh;
1311 int touched = 0;
1312
1313
1314
1315
1316
1317
1318
1319
1320 if (clear_bit(BH_Has_aged, &bh->b_state))
1321 return;
1322
1323 do {
1324 touched |= clear_bit(BH_Touched, &tmp->b_state);
1325 tmp = tmp->b_this_page;
1326 set_bit(BH_Has_aged, &tmp->b_state);
1327 } while (tmp != bh);
1328 clear_bit(BH_Has_aged, &bh->b_state);
1329
1330 if (touched)
1331 touch_page(mem_map + MAP_NR((unsigned long) bh->b_data));
1332 else
1333 age_page(mem_map + MAP_NR((unsigned long) bh->b_data));
1334 }
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348 static int maybe_shrink_lav_buffers(int size)
1349 {
1350 int nlist;
1351 int isize;
1352 int total_lav, total_n_buffers, n_sizes;
1353
1354
1355
1356
1357
1358
1359 total_lav = total_n_buffers = n_sizes = 0;
1360 for(nlist = 0; nlist < NR_SIZES; nlist++)
1361 {
1362 total_lav += buffers_lav[nlist];
1363 if(nr_buffers_size[nlist]) n_sizes++;
1364 total_n_buffers += nr_buffers_size[nlist];
1365 total_n_buffers -= nr_buffers_st[nlist][BUF_SHARED];
1366 }
1367
1368
1369
1370
1371 isize = (size ? BUFSIZE_INDEX(size) : -1);
1372
1373 if (n_sizes > 1)
1374 for(nlist = 0; nlist < NR_SIZES; nlist++)
1375 {
1376 if(nlist == isize) continue;
1377 if(nr_buffers_size[nlist] &&
1378 bdf_prm.b_un.lav_const * buffers_lav[nlist]*total_n_buffers <
1379 total_lav * (nr_buffers_size[nlist] - nr_buffers_st[nlist][BUF_SHARED]))
1380 if(shrink_specific_buffers(6, bufferindex_size[nlist]))
1381 return 1;
1382 }
1383 return 0;
1384 }
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398 static int shrink_specific_buffers(unsigned int priority, int size)
1399 {
1400 struct buffer_head *bh;
1401 int nlist;
1402 int i, isize, isize1;
1403
1404 #ifdef DEBUG
1405 if(size) printk("Shrinking buffers of size %d\n", size);
1406 #endif
1407
1408
1409 isize1 = (size ? BUFSIZE_INDEX(size) : -1);
1410
1411 for(isize = 0; isize<NR_SIZES; isize++){
1412 if(isize1 != -1 && isize1 != isize) continue;
1413 bh = free_list[isize];
1414 if(!bh) continue;
1415 for (i=0 ; !i || bh != free_list[isize]; bh = bh->b_next_free, i++) {
1416 if (bh->b_count || buffer_protected(bh) ||
1417 !bh->b_this_page)
1418 continue;
1419 if (!age_of((unsigned long) bh->b_data) &&
1420 try_to_free_buffer(bh, &bh, 6))
1421 return 1;
1422 if(!bh) break;
1423
1424
1425 }
1426 }
1427
1428
1429
1430 for(nlist = 0; nlist < NR_LIST; nlist++) {
1431 repeat1:
1432 if(priority > 2 && nlist == BUF_SHARED) continue;
1433 i = nr_buffers_type[nlist];
1434 i = ((BUFFEROUT_WEIGHT * i) >> 10) >> priority;
1435 for ( ; i > 0; i-- ) {
1436 bh = next_to_age[nlist];
1437 if (!bh)
1438 break;
1439 next_to_age[nlist] = bh->b_next_free;
1440
1441
1442 age_buffer(bh);
1443
1444
1445 if(bh->b_list != nlist) goto repeat1;
1446 if (bh->b_count || buffer_protected(bh) ||
1447 !bh->b_this_page)
1448 continue;
1449 if(size && bh->b_size != size) continue;
1450 if (buffer_locked(bh))
1451 if (priority)
1452 continue;
1453 else
1454 wait_on_buffer(bh);
1455 if (buffer_dirty(bh)) {
1456 bh->b_count++;
1457 bh->b_flushtime = 0;
1458 ll_rw_block(WRITEA, 1, &bh);
1459 bh->b_count--;
1460 continue;
1461 }
1462
1463
1464
1465 if ((age_of((unsigned long) bh->b_data) >>
1466 (6-priority)) > 0)
1467 continue;
1468 if (try_to_free_buffer(bh, &bh, 0))
1469 return 1;
1470 if(!bh) break;
1471 }
1472 }
1473 return 0;
1474 }
1475
1476
1477
1478
1479 void show_buffers(void)
1480 {
1481 struct buffer_head * bh;
1482 int found = 0, locked = 0, dirty = 0, used = 0, lastused = 0;
1483 int protected = 0;
1484 int shared;
1485 int nlist, isize;
1486
1487 printk("Buffer memory: %6dkB\n",buffermem>>10);
1488 printk("Buffer heads: %6d\n",nr_buffer_heads);
1489 printk("Buffer blocks: %6d\n",nr_buffers);
1490
1491 for(nlist = 0; nlist < NR_LIST; nlist++) {
1492 shared = found = locked = dirty = used = lastused = protected = 0;
1493 bh = lru_list[nlist];
1494 if(!bh) continue;
1495 do {
1496 found++;
1497 if (buffer_locked(bh))
1498 locked++;
1499 if (buffer_protected(bh))
1500 protected++;
1501 if (buffer_dirty(bh))
1502 dirty++;
1503 if(mem_map[MAP_NR(((unsigned long) bh->b_data))].count !=1) shared++;
1504 if (bh->b_count)
1505 used++, lastused = found;
1506 bh = bh->b_next_free;
1507 } while (bh != lru_list[nlist]);
1508 printk("Buffer[%d] mem: %d buffers, %d used (last=%d), %d locked, "
1509 "%d protected, %d dirty %d shrd\n",
1510 nlist, found, used, lastused, locked, protected, dirty, shared);
1511 };
1512 printk("Size [LAV] Free Clean Unshar Lck Lck1 Dirty Shared \n");
1513 for(isize = 0; isize<NR_SIZES; isize++){
1514 printk("%5d [%5d]: %7d ", bufferindex_size[isize],
1515 buffers_lav[isize], nr_free[isize]);
1516 for(nlist = 0; nlist < NR_LIST; nlist++)
1517 printk("%7d ", nr_buffers_st[isize][nlist]);
1518 printk("\n");
1519 }
1520 }
1521
1522
1523
1524
1525
1526
1527
1528
1529 static inline int try_to_reassign(struct buffer_head * bh, struct buffer_head ** bhp,
1530 kdev_t dev, unsigned int starting_block)
1531 {
1532 unsigned long page;
1533 struct buffer_head * tmp, * p;
1534
1535 *bhp = bh;
1536 page = (unsigned long) bh->b_data;
1537 page &= PAGE_MASK;
1538 if(mem_map[MAP_NR(page)].count != 1) return 0;
1539 tmp = bh;
1540 do {
1541 if (!tmp)
1542 return 0;
1543
1544 if (tmp->b_count || buffer_protected(tmp) ||
1545 buffer_dirty(tmp) || buffer_locked(tmp))
1546 return 0;
1547 tmp = tmp->b_this_page;
1548 } while (tmp != bh);
1549 tmp = bh;
1550
1551 while((unsigned long) tmp->b_data & (PAGE_SIZE - 1))
1552 tmp = tmp->b_this_page;
1553
1554
1555 bh = tmp;
1556 do {
1557 p = tmp;
1558 tmp = tmp->b_this_page;
1559 remove_from_queues(p);
1560 p->b_dev = dev;
1561 mark_buffer_uptodate(p, 0);
1562 clear_bit(BH_Req, &p->b_state);
1563 p->b_blocknr = starting_block++;
1564 insert_into_queues(p);
1565 } while (tmp != bh);
1566 return 1;
1567 }
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583 static int reassign_cluster(kdev_t dev,
1584 unsigned int starting_block, int size)
1585 {
1586 struct buffer_head *bh;
1587 int isize = BUFSIZE_INDEX(size);
1588 int i;
1589
1590
1591
1592
1593
1594 while(nr_free[isize] < 32) refill_freelist(size);
1595
1596 bh = free_list[isize];
1597 if(bh)
1598 for (i=0 ; !i || bh != free_list[isize] ; bh = bh->b_next_free, i++) {
1599 if (!bh->b_this_page) continue;
1600 if (try_to_reassign(bh, &bh, dev, starting_block))
1601 return 4;
1602 }
1603 return 0;
1604 }
1605
1606
1607
1608
1609
1610 static unsigned long try_to_generate_cluster(kdev_t dev, int block, int size)
1611 {
1612 struct buffer_head * bh, * tmp, * arr[MAX_BUF_PER_PAGE];
1613 int isize = BUFSIZE_INDEX(size);
1614 unsigned long offset;
1615 unsigned long page;
1616 int nblock;
1617
1618 page = get_free_page(GFP_NOBUFFER);
1619 if(!page) return 0;
1620
1621 bh = create_buffers(page, size);
1622 if (!bh) {
1623 free_page(page);
1624 return 0;
1625 };
1626 nblock = block;
1627 for (offset = 0 ; offset < PAGE_SIZE ; offset += size) {
1628 if (find_buffer(dev, nblock++, size))
1629 goto not_aligned;
1630 }
1631 tmp = bh;
1632 nblock = 0;
1633 while (1) {
1634 arr[nblock++] = bh;
1635 bh->b_count = 1;
1636 bh->b_flushtime = 0;
1637 bh->b_state = 0;
1638 bh->b_dev = dev;
1639 bh->b_list = BUF_CLEAN;
1640 bh->b_blocknr = block++;
1641 nr_buffers++;
1642 nr_buffers_size[isize]++;
1643 insert_into_queues(bh);
1644 if (bh->b_this_page)
1645 bh = bh->b_this_page;
1646 else
1647 break;
1648 }
1649 buffermem += PAGE_SIZE;
1650 buffer_pages[MAP_NR(page)] = bh;
1651 bh->b_this_page = tmp;
1652 while (nblock-- > 0)
1653 brelse(arr[nblock]);
1654 return 4;
1655 not_aligned:
1656 while ((tmp = bh) != NULL) {
1657 bh = bh->b_this_page;
1658 put_unused_buffer_head(tmp);
1659 }
1660 free_page(page);
1661 return 0;
1662 }
1663
1664 unsigned long generate_cluster(kdev_t dev, int b[], int size)
1665 {
1666 int i, offset;
1667
1668 for (i = 0, offset = 0 ; offset < PAGE_SIZE ; i++, offset += size) {
1669 if(i && b[i]-1 != b[i-1]) return 0;
1670 if(find_buffer(dev, b[i], size)) return 0;
1671 };
1672
1673
1674
1675
1676
1677 if(maybe_shrink_lav_buffers(size))
1678 {
1679 int retval;
1680 retval = try_to_generate_cluster(dev, b[0], size);
1681 if(retval) return retval;
1682 };
1683
1684 if (nr_free_pages > min_free_pages*2)
1685 return try_to_generate_cluster(dev, b[0], size);
1686 else
1687 return reassign_cluster(dev, b[0], size);
1688 }
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700 void buffer_init(void)
1701 {
1702 int i;
1703 int isize = BUFSIZE_INDEX(BLOCK_SIZE);
1704 long memsize = MAP_NR(high_memory) << PAGE_SHIFT;
1705
1706 if (memsize >= 4*1024*1024) {
1707 if(memsize >= 16*1024*1024)
1708 nr_hash = 16381;
1709 else
1710 nr_hash = 4093;
1711 } else {
1712 nr_hash = 997;
1713 };
1714
1715 hash_table = (struct buffer_head **) vmalloc(nr_hash *
1716 sizeof(struct buffer_head *));
1717
1718
1719 buffer_pages = (struct buffer_head **) vmalloc(MAP_NR(high_memory) *
1720 sizeof(struct buffer_head *));
1721 for (i = 0 ; i < MAP_NR(high_memory) ; i++)
1722 buffer_pages[i] = NULL;
1723
1724 for (i = 0 ; i < nr_hash ; i++)
1725 hash_table[i] = NULL;
1726 lru_list[BUF_CLEAN] = 0;
1727 grow_buffers(GFP_KERNEL, BLOCK_SIZE);
1728 if (!free_list[isize])
1729 panic("VFS: Unable to initialize buffer free list!");
1730 return;
1731 }
1732
1733
1734
1735
1736
1737
1738
1739
1740 struct wait_queue * bdflush_wait = NULL;
1741 struct wait_queue * bdflush_done = NULL;
1742
1743 static void wakeup_bdflush(int wait)
1744 {
1745 wake_up(&bdflush_wait);
1746 if(wait) sleep_on(&bdflush_done);
1747 }
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758 asmlinkage int sync_old_buffers(void)
1759 {
1760 int i, isize;
1761 int ndirty, nwritten;
1762 int nlist;
1763 int ncount;
1764 struct buffer_head * bh, *next;
1765
1766 sync_supers(0);
1767 sync_inodes(0);
1768
1769 ncount = 0;
1770 #ifdef DEBUG
1771 for(nlist = 0; nlist < NR_LIST; nlist++)
1772 #else
1773 for(nlist = BUF_DIRTY; nlist <= BUF_DIRTY; nlist++)
1774 #endif
1775 {
1776 ndirty = 0;
1777 nwritten = 0;
1778 repeat:
1779 bh = lru_list[nlist];
1780 if(bh)
1781 for (i = nr_buffers_type[nlist]; i-- > 0; bh = next) {
1782
1783 if(bh->b_list != nlist) goto repeat;
1784 next = bh->b_next_free;
1785 if(!lru_list[nlist]) {
1786 printk("Dirty list empty %d\n", i);
1787 break;
1788 }
1789
1790
1791 if (nlist == BUF_DIRTY && !buffer_dirty(bh) && !buffer_locked(bh))
1792 {
1793 refile_buffer(bh);
1794 continue;
1795 }
1796
1797 if (buffer_locked(bh) || !buffer_dirty(bh))
1798 continue;
1799 ndirty++;
1800 if(bh->b_flushtime > jiffies) continue;
1801 nwritten++;
1802 bh->b_count++;
1803 bh->b_flushtime = 0;
1804 #ifdef DEBUG
1805 if(nlist != BUF_DIRTY) ncount++;
1806 #endif
1807 ll_rw_block(WRITE, 1, &bh);
1808 bh->b_count--;
1809 }
1810 }
1811 #ifdef DEBUG
1812 if (ncount) printk("sync_old_buffers: %d dirty buffers not on dirty list\n", ncount);
1813 printk("Wrote %d/%d buffers\n", nwritten, ndirty);
1814 #endif
1815
1816
1817
1818
1819 for(isize = 0; isize<NR_SIZES; isize++){
1820 CALC_LOAD(buffers_lav[isize], bdf_prm.b_un.lav_const, buffer_usage[isize]);
1821 buffer_usage[isize] = 0;
1822 };
1823 return 0;
1824 }
1825
1826
1827
1828
1829
1830
1831
1832 asmlinkage int sys_bdflush(int func, long data)
1833 {
1834 int i, error;
1835
1836 if (!suser())
1837 return -EPERM;
1838
1839 if (func == 1)
1840 return sync_old_buffers();
1841
1842
1843 if (func >= 2) {
1844 i = (func-2) >> 1;
1845 if (i < 0 || i >= N_PARAM)
1846 return -EINVAL;
1847 if((func & 1) == 0) {
1848 error = verify_area(VERIFY_WRITE, (void *) data, sizeof(int));
1849 if (error)
1850 return error;
1851 put_user(bdf_prm.data[i], (int*)data);
1852 return 0;
1853 };
1854 if (data < bdflush_min[i] || data > bdflush_max[i])
1855 return -EINVAL;
1856 bdf_prm.data[i] = data;
1857 return 0;
1858 };
1859
1860
1861
1862
1863
1864 return 0;
1865 }
1866
1867
1868
1869
1870
1871 int bdflush(void * unused)
1872 {
1873 int i;
1874 int ndirty;
1875 int nlist;
1876 int ncount;
1877 struct buffer_head * bh, *next;
1878
1879
1880
1881
1882
1883
1884
1885 current->session = 1;
1886 current->pgrp = 1;
1887 sprintf(current->comm, "kflushd");
1888
1889
1890
1891
1892
1893
1894
1895 #ifdef __SMP__
1896 lock_kernel();
1897 syscall_count++;
1898 #endif
1899
1900 for (;;) {
1901 #ifdef DEBUG
1902 printk("bdflush() activated...");
1903 #endif
1904
1905 ncount = 0;
1906 #ifdef DEBUG
1907 for(nlist = 0; nlist < NR_LIST; nlist++)
1908 #else
1909 for(nlist = BUF_DIRTY; nlist <= BUF_DIRTY; nlist++)
1910 #endif
1911 {
1912 ndirty = 0;
1913 repeat:
1914 bh = lru_list[nlist];
1915 if(bh)
1916 for (i = nr_buffers_type[nlist]; i-- > 0 && ndirty < bdf_prm.b_un.ndirty;
1917 bh = next) {
1918
1919 if(bh->b_list != nlist) goto repeat;
1920 next = bh->b_next_free;
1921 if(!lru_list[nlist]) {
1922 printk("Dirty list empty %d\n", i);
1923 break;
1924 }
1925
1926
1927 if (nlist == BUF_DIRTY && !buffer_dirty(bh) && !buffer_locked(bh))
1928 {
1929 refile_buffer(bh);
1930 continue;
1931 }
1932
1933 if (buffer_locked(bh) || !buffer_dirty(bh))
1934 continue;
1935
1936
1937 bh->b_count++;
1938 ndirty++;
1939 bh->b_flushtime = 0;
1940 ll_rw_block(WRITE, 1, &bh);
1941 #ifdef DEBUG
1942 if(nlist != BUF_DIRTY) ncount++;
1943 #endif
1944 bh->b_count--;
1945 }
1946 }
1947 #ifdef DEBUG
1948 if (ncount) printk("sys_bdflush: %d dirty buffers not on dirty list\n", ncount);
1949 printk("sleeping again.\n");
1950 #endif
1951 wake_up(&bdflush_done);
1952
1953
1954
1955
1956 if(nr_buffers_type[BUF_DIRTY] <= (nr_buffers - nr_buffers_type[BUF_SHARED]) *
1957 bdf_prm.b_un.nfract/100) {
1958 current->signal = 0;
1959 interruptible_sleep_on(&bdflush_wait);
1960 }
1961 }
1962 }
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980