clean
[linux-2.4.21-pre4.git] / mm / filemap.c
1 /*
2  *      linux/mm/filemap.c
3  *
4  * Copyright (C) 1994-1999  Linus Torvalds
5  */
6
7 /*
8  * This file handles the generic file mmap semantics used by
9  * most "normal" filesystems (but you don't /have/ to use this:
10  * the NFS filesystem used to do this differently, for example)
11  */
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/shm.h>
15 #include <linux/mman.h>
16 #include <linux/locks.h>
17 #include <linux/pagemap.h>
18 #include <linux/swap.h>
19 #include <linux/smp_lock.h>
20 #include <linux/blkdev.h>
21 #include <linux/file.h>
22 #include <linux/swapctl.h>
23 #include <linux/init.h>
24 #include <linux/mm.h>
25 #include <linux/iobuf.h>
26
27 #include <asm/pgalloc.h>
28 #include <asm/uaccess.h>
29 #include <asm/mman.h>
30
31 #include <linux/highmem.h>
32
33 /*
34  * Shared mappings implemented 30.11.1994. It's not fully working yet,
35  * though.
36  *
37  * Shared mappings now work. 15.8.1995  Bruno.
38  *
39  * finished 'unifying' the page and buffer cache and SMP-threaded the
40  * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
41  *
42  * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
43  */
44
45 atomic_t page_cache_size = ATOMIC_INIT(0);
46 unsigned int page_hash_bits;
47 struct page **page_hash_table;
48
49 int vm_max_readahead = 31;
50 int vm_min_readahead = 3;
51 EXPORT_SYMBOL(vm_max_readahead);
52 EXPORT_SYMBOL(vm_min_readahead);
53
54
55 spinlock_cacheline_t pagecache_lock_cacheline  = {SPIN_LOCK_UNLOCKED};
56 /*
57  * NOTE: to avoid deadlocking you must never acquire the pagemap_lru_lock 
58  *      with the pagecache_lock held.
59  *
60  * Ordering:
61  *      swap_lock ->
62  *              pagemap_lru_lock ->
63  *                      pagecache_lock
64  */
65 spinlock_cacheline_t pagemap_lru_lock_cacheline = {SPIN_LOCK_UNLOCKED};
66
67 #define CLUSTER_PAGES           (1 << page_cluster)
68 #define CLUSTER_OFFSET(x)       (((x) >> page_cluster) << page_cluster)
69
70 static void FASTCALL(add_page_to_hash_queue(struct page * page, struct page **p));
71 static void add_page_to_hash_queue(struct page * page, struct page **p)
72 {
73         struct page *next = *p;
74
75         *p = page;
76         page->next_hash = next;
77         page->pprev_hash = p;
78         if (next)
79                 next->pprev_hash = &page->next_hash;
80         if (page->buffers)
81                 PAGE_BUG(page);
82         atomic_inc(&page_cache_size);
83 }
84
85 static inline void add_page_to_inode_queue(struct address_space *mapping, struct page * page)
86 {
87         struct list_head *head = &mapping->clean_pages;
88
89         mapping->nrpages++;
90         list_add(&page->list, head);
91         page->mapping = mapping;
92 }
93
94 static inline void remove_page_from_inode_queue(struct page * page)
95 {
96         struct address_space * mapping = page->mapping;
97
98         if (mapping->a_ops->removepage)
99                 mapping->a_ops->removepage(page);
100         
101         mapping->nrpages--;
102         list_del(&page->list);
103         page->mapping = NULL;
104 }
105
106 static inline void remove_page_from_hash_queue(struct page * page)
107 {
108         struct page *next = page->next_hash;
109         struct page **pprev = page->pprev_hash;
110
111         if (next)
112                 next->pprev_hash = pprev;
113         *pprev = next;
114         page->pprev_hash = NULL;
115         atomic_dec(&page_cache_size);
116 }
117
118 /*
119  * Remove a page from the page cache and free it. Caller has to make
120  * sure the page is locked and that nobody else uses it - or that usage
121  * is safe.
122  */
123 void __remove_inode_page(struct page *page)
124 {
125         if (PageDirty(page) && !PageSwapCache(page))
126                 BUG();
127         remove_page_from_inode_queue(page);
128         remove_page_from_hash_queue(page);
129 }
130
131 void remove_inode_page(struct page *page)
132 {
133         if (!PageLocked(page))
134                 PAGE_BUG(page);
135
136         spin_lock(&pagecache_lock);
137         __remove_inode_page(page);
138         spin_unlock(&pagecache_lock);
139 }
140
141 static inline int sync_page(struct page *page)
142 {
143         struct address_space *mapping = page->mapping;
144
145         if (mapping && mapping->a_ops && mapping->a_ops->sync_page)
146                 return mapping->a_ops->sync_page(page);
147         return 0;
148 }
149
150 /*
151  * Add a page to the dirty page list.
152  */
153 void set_page_dirty(struct page *page)
154 {
155         if (!test_and_set_bit(PG_dirty, &page->flags)) {
156                 struct address_space *mapping = page->mapping;
157
158                 if (mapping) {
159                         spin_lock(&pagecache_lock);
160                         mapping = page->mapping;
161                         if (mapping) {  /* may have been truncated */
162                                 list_del(&page->list);
163                                 list_add(&page->list, &mapping->dirty_pages);
164                         }
165                         spin_unlock(&pagecache_lock);
166
167                         if (mapping && mapping->host)
168                                 mark_inode_dirty_pages(mapping->host);
169                 }
170         }
171 }
172
173 /**
174  * invalidate_inode_pages - Invalidate all the unlocked pages of one inode
175  * @inode: the inode which pages we want to invalidate
176  *
177  * This function only removes the unlocked pages, if you want to
178  * remove all the pages of one inode, you must call truncate_inode_pages.
179  */
180
181 void invalidate_inode_pages(struct inode * inode)
182 {
183         struct list_head *head, *curr;
184         struct page * page;
185
186         head = &inode->i_mapping->clean_pages;
187
188         spin_lock(&pagemap_lru_lock);
189         spin_lock(&pagecache_lock);
190         curr = head->next;
191
192         while (curr != head) {
193                 page = list_entry(curr, struct page, list);
194                 curr = curr->next;
195
196                 /* We cannot invalidate something in dirty.. */
197                 if (PageDirty(page))
198                         continue;
199
200                 /* ..or locked */
201                 if (TryLockPage(page))
202                         continue;
203
204                 if (page->buffers && !try_to_free_buffers(page, 0))
205                         goto unlock;
206
207                 if (page_count(page) != 1)
208                         goto unlock;
209
210                 __lru_cache_del(page);
211                 __remove_inode_page(page);
212                 UnlockPage(page);
213                 page_cache_release(page);
214                 continue;
215 unlock:
216                 UnlockPage(page);
217                 continue;
218         }
219
220         spin_unlock(&pagecache_lock);
221         spin_unlock(&pagemap_lru_lock);
222 }
223
224 static int do_flushpage(struct page *page, unsigned long offset)
225 {
226         int (*flushpage) (struct page *, unsigned long);
227         flushpage = page->mapping->a_ops->flushpage;
228         if (flushpage)
229                 return (*flushpage)(page, offset);
230         return block_flushpage(page, offset);
231 }
232
233 static inline void truncate_partial_page(struct page *page, unsigned partial)
234 {
235         memclear_highpage_flush(page, partial, PAGE_CACHE_SIZE-partial);
236         if (page->buffers)
237                 do_flushpage(page, partial);
238 }
239
240 static void truncate_complete_page(struct page *page)
241 {
242         /* Leave it on the LRU if it gets converted into anonymous buffers */
243         if (!page->buffers || do_flushpage(page, 0))
244                 lru_cache_del(page);
245
246         /*
247          * We remove the page from the page cache _after_ we have
248          * destroyed all buffer-cache references to it. Otherwise some
249          * other process might think this inode page is not in the
250          * page cache and creates a buffer-cache alias to it causing
251          * all sorts of fun problems ...  
252          */
253         ClearPageDirty(page);
254         ClearPageUptodate(page);
255         remove_inode_page(page);
256         page_cache_release(page);
257 }
258
259 static int FASTCALL(truncate_list_pages(struct list_head *, unsigned long, unsigned *));
260 static int truncate_list_pages(struct list_head *head, unsigned long start, unsigned *partial)
261 {
262         struct list_head *curr;
263         struct page * page;
264         int unlocked = 0;
265
266  restart:
267         curr = head->prev;
268         while (curr != head) {
269                 unsigned long offset;
270
271                 page = list_entry(curr, struct page, list);
272                 offset = page->index;
273
274                 /* Is one of the pages to truncate? */
275                 if ((offset >= start) || (*partial && (offset + 1) == start)) {
276                         int failed;
277
278                         page_cache_get(page);
279                         failed = TryLockPage(page);
280
281                         list_del(head);
282                         if (!failed)
283                                 /* Restart after this page */
284                                 list_add_tail(head, curr);
285                         else
286                                 /* Restart on this page */
287                                 list_add(head, curr);
288
289                         spin_unlock(&pagecache_lock);
290                         unlocked = 1;
291
292                         if (!failed) {
293                                 if (*partial && (offset + 1) == start) {
294                                         truncate_partial_page(page, *partial);
295                                         *partial = 0;
296                                 } else 
297                                         truncate_complete_page(page);
298
299                                 UnlockPage(page);
300                         } else
301                                 wait_on_page(page);
302
303                         page_cache_release(page);
304
305                         if (current->need_resched) {
306                                 __set_current_state(TASK_RUNNING);
307                                 schedule();
308                         }
309
310                         spin_lock(&pagecache_lock);
311                         goto restart;
312                 }
313                 curr = curr->prev;
314         }
315         return unlocked;
316 }
317
318
319 /**
320  * truncate_inode_pages - truncate *all* the pages from an offset
321  * @mapping: mapping to truncate
322  * @lstart: offset from with to truncate
323  *
324  * Truncate the page cache at a set offset, removing the pages
325  * that are beyond that offset (and zeroing out partial pages).
326  * If any page is locked we wait for it to become unlocked.
327  */
328 void truncate_inode_pages(struct address_space * mapping, loff_t lstart) 
329 {
330         unsigned long start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
331         unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
332         int unlocked;
333
334         spin_lock(&pagecache_lock);
335         do {
336                 unlocked = truncate_list_pages(&mapping->clean_pages, start, &partial);
337                 unlocked |= truncate_list_pages(&mapping->dirty_pages, start, &partial);
338                 unlocked |= truncate_list_pages(&mapping->locked_pages, start, &partial);
339         } while (unlocked);
340         /* Traversed all three lists without dropping the lock */
341         spin_unlock(&pagecache_lock);
342 }
343
344 static inline int invalidate_this_page2(struct page * page,
345                                         struct list_head * curr,
346                                         struct list_head * head)
347 {
348         int unlocked = 1;
349
350         /*
351          * The page is locked and we hold the pagecache_lock as well
352          * so both page_count(page) and page->buffers stays constant here.
353          */
354         if (page_count(page) == 1 + !!page->buffers) {
355                 /* Restart after this page */
356                 list_del(head);
357                 list_add_tail(head, curr);
358
359                 page_cache_get(page);
360                 spin_unlock(&pagecache_lock);
361                 truncate_complete_page(page);
362         } else {
363                 if (page->buffers) {
364                         /* Restart after this page */
365                         list_del(head);
366                         list_add_tail(head, curr);
367
368                         page_cache_get(page);
369                         spin_unlock(&pagecache_lock);
370                         block_invalidate_page(page);
371                 } else
372                         unlocked = 0;
373
374                 ClearPageDirty(page);
375                 ClearPageUptodate(page);
376         }
377
378         return unlocked;
379 }
380
381 static int FASTCALL(invalidate_list_pages2(struct list_head *));
382 static int invalidate_list_pages2(struct list_head *head)
383 {
384         struct list_head *curr;
385         struct page * page;
386         int unlocked = 0;
387
388  restart:
389         curr = head->prev;
390         while (curr != head) {
391                 page = list_entry(curr, struct page, list);
392
393                 if (!TryLockPage(page)) {
394                         int __unlocked;
395
396                         __unlocked = invalidate_this_page2(page, curr, head);
397                         UnlockPage(page);
398                         unlocked |= __unlocked;
399                         if (!__unlocked) {
400                                 curr = curr->prev;
401                                 continue;
402                         }
403                 } else {
404                         /* Restart on this page */
405                         list_del(head);
406                         list_add(head, curr);
407
408                         page_cache_get(page);
409                         spin_unlock(&pagecache_lock);
410                         unlocked = 1;
411                         wait_on_page(page);
412                 }
413
414                 page_cache_release(page);
415                 if (current->need_resched) {
416                         __set_current_state(TASK_RUNNING);
417                         schedule();
418                 }
419
420                 spin_lock(&pagecache_lock);
421                 goto restart;
422         }
423         return unlocked;
424 }
425
426 /**
427  * invalidate_inode_pages2 - Clear all the dirty bits around if it can't
428  * free the pages because they're mapped.
429  * @mapping: the address_space which pages we want to invalidate
430  */
431 void invalidate_inode_pages2(struct address_space * mapping)
432 {
433         int unlocked;
434
435         spin_lock(&pagecache_lock);
436         do {
437                 unlocked = invalidate_list_pages2(&mapping->clean_pages);
438                 unlocked |= invalidate_list_pages2(&mapping->dirty_pages);
439                 unlocked |= invalidate_list_pages2(&mapping->locked_pages);
440         } while (unlocked);
441         spin_unlock(&pagecache_lock);
442 }
443
444 static inline struct page * __find_page_nolock(struct address_space *mapping, unsigned long offset, struct page *page)
445 {
446         goto inside;
447
448         for (;;) {
449                 page = page->next_hash;
450 inside:
451                 if (!page)
452                         goto not_found;
453                 if (page->mapping != mapping)
454                         continue;
455                 if (page->index == offset)
456                         break;
457         }
458
459 not_found:
460         return page;
461 }
462
463 static int do_buffer_fdatasync(struct list_head *head, unsigned long start, unsigned long end, int (*fn)(struct page *))
464 {
465         struct list_head *curr;
466         struct page *page;
467         int retval = 0;
468
469         spin_lock(&pagecache_lock);
470         curr = head->next;
471         while (curr != head) {
472                 page = list_entry(curr, struct page, list);
473                 curr = curr->next;
474                 if (!page->buffers)
475                         continue;
476                 if (page->index >= end)
477                         continue;
478                 if (page->index < start)
479                         continue;
480
481                 page_cache_get(page);
482                 spin_unlock(&pagecache_lock);
483                 lock_page(page);
484
485                 /* The buffers could have been free'd while we waited for the page lock */
486                 if (page->buffers)
487                         retval |= fn(page);
488
489                 UnlockPage(page);
490                 spin_lock(&pagecache_lock);
491                 curr = page->list.next;
492                 page_cache_release(page);
493         }
494         spin_unlock(&pagecache_lock);
495
496         return retval;
497 }
498
499 /*
500  * Two-stage data sync: first start the IO, then go back and
501  * collect the information..
502  */
503 int generic_buffer_fdatasync(struct inode *inode, unsigned long start_idx, unsigned long end_idx)
504 {
505         int retval;
506
507         /* writeout dirty buffers on pages from both clean and dirty lists */
508         retval = do_buffer_fdatasync(&inode->i_mapping->dirty_pages, start_idx, end_idx, writeout_one_page);
509         retval |= do_buffer_fdatasync(&inode->i_mapping->clean_pages, start_idx, end_idx, writeout_one_page);
510         retval |= do_buffer_fdatasync(&inode->i_mapping->locked_pages, start_idx, end_idx, writeout_one_page);
511
512         /* now wait for locked buffers on pages from both clean and dirty lists */
513         retval |= do_buffer_fdatasync(&inode->i_mapping->dirty_pages, start_idx, end_idx, waitfor_one_page);
514         retval |= do_buffer_fdatasync(&inode->i_mapping->clean_pages, start_idx, end_idx, waitfor_one_page);
515         retval |= do_buffer_fdatasync(&inode->i_mapping->locked_pages, start_idx, end_idx, waitfor_one_page);
516
517         return retval;
518 }
519
520 /*
521  * In-memory filesystems have to fail their
522  * writepage function - and this has to be
523  * worked around in the VM layer..
524  *
525  * We
526  *  - mark the page dirty again (but do NOT
527  *    add it back to the inode dirty list, as
528  *    that would livelock in fdatasync)
529  *  - activate the page so that the page stealer
530  *    doesn't try to write it out over and over
531  *    again.
532  */
533 int fail_writepage(struct page *page)
534 {
535         /* Only activate on memory-pressure, not fsync.. */
536         if (PageLaunder(page)) {
537                 activate_page(page);
538                 SetPageReferenced(page);
539         }
540
541         /* Set the page dirty again, unlock */
542         SetPageDirty(page);
543         UnlockPage(page);
544         return 0;
545 }
546
547 EXPORT_SYMBOL(fail_writepage);
548
549 /**
550  *      filemap_fdatasync - walk the list of dirty pages of the given address space
551  *      and writepage() all of them.
552  * 
553  *      @mapping: address space structure to write
554  *
555  */
556 int filemap_fdatasync(struct address_space * mapping)
557 {
558         int ret = 0;
559         int (*writepage)(struct page *) = mapping->a_ops->writepage;
560
561         spin_lock(&pagecache_lock);
562
563         while (!list_empty(&mapping->dirty_pages)) {
564                 struct page *page = list_entry(mapping->dirty_pages.prev, struct page, list);
565
566                 list_del(&page->list);
567                 list_add(&page->list, &mapping->locked_pages);
568
569                 if (!PageDirty(page))
570                         continue;
571
572                 page_cache_get(page);
573                 spin_unlock(&pagecache_lock);
574
575                 lock_page(page);
576
577                 if (PageDirty(page)) {
578                         int err;
579                         ClearPageDirty(page);
580                         err = writepage(page);
581                         if (err && !ret)
582                                 ret = err;
583                 } else
584                         UnlockPage(page);
585
586                 page_cache_release(page);
587                 spin_lock(&pagecache_lock);
588         }
589         spin_unlock(&pagecache_lock);
590         return ret;
591 }
592
593 /**
594  *      filemap_fdatawait - walk the list of locked pages of the given address space
595  *      and wait for all of them.
596  * 
597  *      @mapping: address space structure to wait for
598  *
599  */
600 int filemap_fdatawait(struct address_space * mapping)
601 {
602         int ret = 0;
603
604         spin_lock(&pagecache_lock);
605
606         while (!list_empty(&mapping->locked_pages)) {
607                 struct page *page = list_entry(mapping->locked_pages.next, struct page, list);
608
609                 list_del(&page->list);
610                 list_add(&page->list, &mapping->clean_pages);
611
612                 if (!PageLocked(page))
613                         continue;
614
615                 page_cache_get(page);
616                 spin_unlock(&pagecache_lock);
617
618                 ___wait_on_page(page);
619                 if (PageError(page))
620                         ret = -EIO;
621
622                 page_cache_release(page);
623                 spin_lock(&pagecache_lock);
624         }
625         spin_unlock(&pagecache_lock);
626         return ret;
627 }
628
629 /*
630  * Add a page to the inode page cache.
631  *
632  * The caller must have locked the page and 
633  * set all the page flags correctly..
634  */
635 void add_to_page_cache_locked(struct page * page, struct address_space *mapping, unsigned long index)
636 {
637         if (!PageLocked(page))
638                 BUG();
639
640         page->index = index;
641         page_cache_get(page);
642         spin_lock(&pagecache_lock);
643         add_page_to_inode_queue(mapping, page);
644         add_page_to_hash_queue(page, page_hash(mapping, index));
645         spin_unlock(&pagecache_lock);
646
647         lru_cache_add(page);
648 }
649
650 /*
651  * This adds a page to the page cache, starting out as locked,
652  * owned by us, but unreferenced, not uptodate and with no errors.
653  */
654 static inline void __add_to_page_cache(struct page * page,
655         struct address_space *mapping, unsigned long offset,
656         struct page **hash)
657 {
658         unsigned long flags;
659
660         flags = page->flags & ~(1 << PG_uptodate | 1 << PG_error | 1 << PG_dirty | 1 << PG_referenced | 1 << PG_arch_1 | 1 << PG_checked);
661         page->flags = flags | (1 << PG_locked);
662         page_cache_get(page);
663         page->index = offset;
664         add_page_to_inode_queue(mapping, page);
665         add_page_to_hash_queue(page, hash);
666 }
667
668 void add_to_page_cache(struct page * page, struct address_space * mapping, unsigned long offset)
669 {
670         spin_lock(&pagecache_lock);
671         __add_to_page_cache(page, mapping, offset, page_hash(mapping, offset));
672         spin_unlock(&pagecache_lock);
673         lru_cache_add(page);
674 }
675
676 int add_to_page_cache_unique(struct page * page,
677         struct address_space *mapping, unsigned long offset,
678         struct page **hash)
679 {
680         int err;
681         struct page *alias;
682
683         spin_lock(&pagecache_lock);
684         alias = __find_page_nolock(mapping, offset, *hash);
685
686         err = 1;
687         if (!alias) {
688                 __add_to_page_cache(page,mapping,offset,hash);
689                 err = 0;
690         }
691
692         spin_unlock(&pagecache_lock);
693         if (!err)
694                 lru_cache_add(page);
695         return err;
696 }
697
698 /*
699  * This adds the requested page to the page cache if it isn't already there,
700  * and schedules an I/O to read in its contents from disk.
701  */
702 static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
703 static int page_cache_read(struct file * file, unsigned long offset)
704 {
705         struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
706         struct page **hash = page_hash(mapping, offset);
707         struct page *page; 
708
709         spin_lock(&pagecache_lock);
710         page = __find_page_nolock(mapping, offset, *hash);
711         spin_unlock(&pagecache_lock);
712         if (page)
713                 return 0;
714
715         page = page_cache_alloc(mapping);
716         if (!page)
717                 return -ENOMEM;
718
719         if (!add_to_page_cache_unique(page, mapping, offset, hash)) {
720                 int error = mapping->a_ops->readpage(file, page);
721                 page_cache_release(page);
722                 return error;
723         }
724         /*
725          * We arrive here in the unlikely event that someone 
726          * raced with us and added our page to the cache first.
727          */
728         page_cache_release(page);
729         return 0;
730 }
731
732 /*
733  * Read in an entire cluster at once.  A cluster is usually a 64k-
734  * aligned block that includes the page requested in "offset."
735  */
736 static int FASTCALL(read_cluster_nonblocking(struct file * file, unsigned long offset,
737                                              unsigned long filesize));
738 static int read_cluster_nonblocking(struct file * file, unsigned long offset,
739         unsigned long filesize)
740 {
741         unsigned long pages = CLUSTER_PAGES;
742
743         offset = CLUSTER_OFFSET(offset);
744         while ((pages-- > 0) && (offset < filesize)) {
745                 int error = page_cache_read(file, offset);
746                 if (error < 0)
747                         return error;
748                 offset ++;
749         }
750
751         return 0;
752 }
753
754 /*
755  * Knuth recommends primes in approximately golden ratio to the maximum
756  * integer representable by a machine word for multiplicative hashing.
757  * Chuck Lever verified the effectiveness of this technique:
758  * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
759  *
760  * These primes are chosen to be bit-sparse, that is operations on
761  * them can use shifts and additions instead of multiplications for
762  * machines where multiplications are slow.
763  */
764 #if BITS_PER_LONG == 32
765 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
766 #define GOLDEN_RATIO_PRIME 0x9e370001UL
767 #elif BITS_PER_LONG == 64
768 /*  2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
769 #define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL
770 #else
771 #error Define GOLDEN_RATIO_PRIME for your wordsize.
772 #endif
773
774 /*
775  * In order to wait for pages to become available there must be
776  * waitqueues associated with pages. By using a hash table of
777  * waitqueues where the bucket discipline is to maintain all
778  * waiters on the same queue and wake all when any of the pages
779  * become available, and for the woken contexts to check to be
780  * sure the appropriate page became available, this saves space
781  * at a cost of "thundering herd" phenomena during rare hash
782  * collisions.
783  */
784 static inline wait_queue_head_t *page_waitqueue(struct page *page)
785 {
786         const zone_t *zone = page_zone(page);
787         wait_queue_head_t *wait = zone->wait_table;
788         unsigned long hash = (unsigned long)page;
789
790 #if BITS_PER_LONG == 64
791         /*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
792         unsigned long n = hash;
793         n <<= 18;
794         hash -= n;
795         n <<= 33;
796         hash -= n;
797         n <<= 3;
798         hash += n;
799         n <<= 3;
800         hash -= n;
801         n <<= 4;
802         hash += n;
803         n <<= 2;
804         hash += n;
805 #else
806         /* On some cpus multiply is faster, on others gcc will do shifts */
807         hash *= GOLDEN_RATIO_PRIME;
808 #endif
809         hash >>= zone->wait_table_shift;
810
811         return &wait[hash];
812 }
813
814 /* 
815  * Wait for a page to get unlocked.
816  *
817  * This must be called with the caller "holding" the page,
818  * ie with increased "page->count" so that the page won't
819  * go away during the wait..
820  *
821  * The waiting strategy is to get on a waitqueue determined
822  * by hashing. Waiters will then collide, and the newly woken
823  * task must then determine whether it was woken for the page
824  * it really wanted, and go back to sleep on the waitqueue if
825  * that wasn't it. With the waitqueue semantics, it never leaves
826  * the waitqueue unless it calls, so the loop moves forward one
827  * iteration every time there is
828  * (1) a collision 
829  * and
830  * (2) one of the colliding pages is woken
831  *
832  * This is the thundering herd problem, but it is expected to
833  * be very rare due to the few pages that are actually being
834  * waited on at any given time and the quality of the hash function.
835  */
836 void ___wait_on_page(struct page *page)
837 {
838         wait_queue_head_t *waitqueue = page_waitqueue(page);
839         struct task_struct *tsk = current;
840         DECLARE_WAITQUEUE(wait, tsk);
841
842         add_wait_queue(waitqueue, &wait);
843         do {
844                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
845                 if (!PageLocked(page))
846                         break;
847                 sync_page(page);
848                 schedule();
849         } while (PageLocked(page));
850         __set_task_state(tsk, TASK_RUNNING);
851         remove_wait_queue(waitqueue, &wait);
852 }
853
854 /*
855  * unlock_page() is the other half of the story just above
856  * __wait_on_page(). Here a couple of quick checks are done
857  * and a couple of flags are set on the page, and then all
858  * of the waiters for all of the pages in the appropriate
859  * wait queue are woken.
860  */
861 void unlock_page(struct page *page)
862 {
863         wait_queue_head_t *waitqueue = page_waitqueue(page);
864         ClearPageLaunder(page);
865         smp_mb__before_clear_bit();
866         if (!test_and_clear_bit(PG_locked, &(page)->flags))
867                 BUG();
868         smp_mb__after_clear_bit(); 
869
870         /*
871          * Although the default semantics of wake_up() are
872          * to wake all, here the specific function is used
873          * to make it even more explicit that a number of
874          * pages are being waited on here.
875          */
876         if (waitqueue_active(waitqueue))
877                 wake_up_all(waitqueue);
878 }
879
880 /*
881  * Get a lock on the page, assuming we need to sleep
882  * to get it..
883  */
884 static void __lock_page(struct page *page)
885 {
886         wait_queue_head_t *waitqueue = page_waitqueue(page);
887         struct task_struct *tsk = current;
888         DECLARE_WAITQUEUE(wait, tsk);
889
890         add_wait_queue_exclusive(waitqueue, &wait);
891         for (;;) {
892                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
893                 if (PageLocked(page)) {
894                         sync_page(page);
895                         schedule();
896                 }
897                 if (!TryLockPage(page))
898                         break;
899         }
900         __set_task_state(tsk, TASK_RUNNING);
901         remove_wait_queue(waitqueue, &wait);
902 }
903
904 /*
905  * Get an exclusive lock on the page, optimistically
906  * assuming it's not locked..
907  */
908 void lock_page(struct page *page)
909 {
910         if (TryLockPage(page))
911                 __lock_page(page);
912 }
913
914 /*
915  * a rather lightweight function, finding and getting a reference to a
916  * hashed page atomically.
917  */
918 struct page * __find_get_page(struct address_space *mapping,
919                               unsigned long offset, struct page **hash)
920 {
921         struct page *page;
922
923         /*
924          * We scan the hash list read-only. Addition to and removal from
925          * the hash-list needs a held write-lock.
926          */
927         spin_lock(&pagecache_lock);
928         page = __find_page_nolock(mapping, offset, *hash);
929         if (page)
930                 page_cache_get(page);
931         spin_unlock(&pagecache_lock);
932         return page;
933 }
934
935 /*
936  * Same as above, but trylock it instead of incrementing the count.
937  */
938 struct page *find_trylock_page(struct address_space *mapping, unsigned long offset)
939 {
940         struct page *page;
941         struct page **hash = page_hash(mapping, offset);
942
943         spin_lock(&pagecache_lock);
944         page = __find_page_nolock(mapping, offset, *hash);
945         if (page) {
946                 if (TryLockPage(page))
947                         page = NULL;
948         }
949         spin_unlock(&pagecache_lock);
950         return page;
951 }
952
953 /*
954  * Must be called with the pagecache lock held,
955  * will return with it held (but it may be dropped
956  * during blocking operations..
957  */
958 static struct page * FASTCALL(__find_lock_page_helper(struct address_space *, unsigned long, struct page *));
959 static struct page * __find_lock_page_helper(struct address_space *mapping,
960                                         unsigned long offset, struct page *hash)
961 {
962         struct page *page;
963
964         /*
965          * We scan the hash list read-only. Addition to and removal from
966          * the hash-list needs a held write-lock.
967          */
968 repeat:
969         page = __find_page_nolock(mapping, offset, hash);
970         if (page) {
971                 page_cache_get(page);
972                 if (TryLockPage(page)) {
973                         spin_unlock(&pagecache_lock);
974                         lock_page(page);
975                         spin_lock(&pagecache_lock);
976
977                         /* Has the page been re-allocated while we slept? */
978                         if (page->mapping != mapping || page->index != offset) {
979                                 UnlockPage(page);
980                                 page_cache_release(page);
981                                 goto repeat;
982                         }
983                 }
984         }
985         return page;
986 }
987
988 /*
989  * Same as the above, but lock the page too, verifying that
990  * it's still valid once we own it.
991  */
992 struct page * __find_lock_page (struct address_space *mapping,
993                                 unsigned long offset, struct page **hash)
994 {
995         struct page *page;
996
997         spin_lock(&pagecache_lock);
998         page = __find_lock_page_helper(mapping, offset, *hash);
999         spin_unlock(&pagecache_lock);
1000         return page;
1001 }
1002
1003 /*
1004  * Same as above, but create the page if required..
1005  */
1006 struct page * find_or_create_page(struct address_space *mapping, unsigned long index, unsigned int gfp_mask)
1007 {
1008         struct page *page;
1009         struct page **hash = page_hash(mapping, index);
1010
1011         spin_lock(&pagecache_lock);
1012         page = __find_lock_page_helper(mapping, index, *hash);
1013         spin_unlock(&pagecache_lock);
1014         if (!page) {
1015                 struct page *newpage = alloc_page(gfp_mask);
1016                 if (newpage) {
1017                         spin_lock(&pagecache_lock);
1018                         page = __find_lock_page_helper(mapping, index, *hash);
1019                         if (likely(!page)) {
1020                                 page = newpage;
1021                                 __add_to_page_cache(page, mapping, index, hash);
1022                                 newpage = NULL;
1023                         }
1024                         spin_unlock(&pagecache_lock);
1025                         if (newpage == NULL)
1026                                 lru_cache_add(page);
1027                         else 
1028                                 page_cache_release(newpage);
1029                 }
1030         }
1031         return page;    
1032 }
1033
1034 /*
1035  * Same as grab_cache_page, but do not wait if the page is unavailable.
1036  * This is intended for speculative data generators, where the data can
1037  * be regenerated if the page couldn't be grabbed.  This routine should
1038  * be safe to call while holding the lock for another page.
1039  */
1040 struct page *grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
1041 {
1042         struct page *page, **hash;
1043
1044         hash = page_hash(mapping, index);
1045         page = __find_get_page(mapping, index, hash);
1046
1047         if ( page ) {
1048                 if ( !TryLockPage(page) ) {
1049                         /* Page found and locked */
1050                         /* This test is overly paranoid, but what the heck... */
1051                         if ( unlikely(page->mapping != mapping || page->index != index) ) {
1052                                 /* Someone reallocated this page under us. */
1053                                 UnlockPage(page);
1054                                 page_cache_release(page);
1055                                 return NULL;
1056                         } else {
1057                                 return page;
1058                         }
1059                 } else {
1060                         /* Page locked by someone else */
1061                         page_cache_release(page);
1062                         return NULL;
1063                 }
1064         }
1065
1066         page = page_cache_alloc(mapping);
1067         if ( unlikely(!page) )
1068                 return NULL;    /* Failed to allocate a page */
1069
1070         if ( unlikely(add_to_page_cache_unique(page, mapping, index, hash)) ) {
1071                 /* Someone else grabbed the page already. */
1072                 page_cache_release(page);
1073                 return NULL;
1074         }
1075
1076         return page;
1077 }
1078
1079 #if 0
1080 #define PROFILE_READAHEAD
1081 #define DEBUG_READAHEAD
1082 #endif
1083
1084 /*
1085  * Read-ahead profiling information
1086  * --------------------------------
1087  * Every PROFILE_MAXREADCOUNT, the following information is written 
1088  * to the syslog:
1089  *   Percentage of asynchronous read-ahead.
1090  *   Average of read-ahead fields context value.
1091  * If DEBUG_READAHEAD is defined, a snapshot of these fields is written 
1092  * to the syslog.
1093  */
1094
1095 #ifdef PROFILE_READAHEAD
1096
1097 #define PROFILE_MAXREADCOUNT 1000
1098
1099 static unsigned long total_reada;
1100 static unsigned long total_async;
1101 static unsigned long total_ramax;
1102 static unsigned long total_ralen;
1103 static unsigned long total_rawin;
1104
1105 static void profile_readahead(int async, struct file *filp)
1106 {
1107         unsigned long flags;
1108
1109         ++total_reada;
1110         if (async)
1111                 ++total_async;
1112
1113         total_ramax     += filp->f_ramax;
1114         total_ralen     += filp->f_ralen;
1115         total_rawin     += filp->f_rawin;
1116
1117         if (total_reada > PROFILE_MAXREADCOUNT) {
1118                 save_flags(flags);
1119                 cli();
1120                 if (!(total_reada > PROFILE_MAXREADCOUNT)) {
1121                         restore_flags(flags);
1122                         return;
1123                 }
1124
1125                 printk("Readahead average:  max=%ld, len=%ld, win=%ld, async=%ld%%\n",
1126                         total_ramax/total_reada,
1127                         total_ralen/total_reada,
1128                         total_rawin/total_reada,
1129                         (total_async*100)/total_reada);
1130 #ifdef DEBUG_READAHEAD
1131                 printk("Readahead snapshot: max=%ld, len=%ld, win=%ld, raend=%Ld\n",
1132                         filp->f_ramax, filp->f_ralen, filp->f_rawin, filp->f_raend);
1133 #endif
1134
1135                 total_reada     = 0;
1136                 total_async     = 0;
1137                 total_ramax     = 0;
1138                 total_ralen     = 0;
1139                 total_rawin     = 0;
1140
1141                 restore_flags(flags);
1142         }
1143 }
1144 #endif  /* defined PROFILE_READAHEAD */
1145
1146 /*
1147  * Read-ahead context:
1148  * -------------------
1149  * The read ahead context fields of the "struct file" are the following:
1150  * - f_raend : position of the first byte after the last page we tried to
1151  *             read ahead.
1152  * - f_ramax : current read-ahead maximum size.
1153  * - f_ralen : length of the current IO read block we tried to read-ahead.
1154  * - f_rawin : length of the current read-ahead window.
1155  *              if last read-ahead was synchronous then
1156  *                      f_rawin = f_ralen
1157  *              otherwise (was asynchronous)
1158  *                      f_rawin = previous value of f_ralen + f_ralen
1159  *
1160  * Read-ahead limits:
1161  * ------------------
1162  * MIN_READAHEAD   : minimum read-ahead size when read-ahead.
1163  * MAX_READAHEAD   : maximum read-ahead size when read-ahead.
1164  *
1165  * Synchronous read-ahead benefits:
1166  * --------------------------------
1167  * Using reasonable IO xfer length from peripheral devices increase system 
1168  * performances.
1169  * Reasonable means, in this context, not too large but not too small.
1170  * The actual maximum value is:
1171  *      MAX_READAHEAD + PAGE_CACHE_SIZE = 76k is CONFIG_READA_SMALL is undefined
1172  *      and 32K if defined (4K page size assumed).
1173  *
1174  * Asynchronous read-ahead benefits:
1175  * ---------------------------------
1176  * Overlapping next read request and user process execution increase system 
1177  * performance.
1178  *
1179  * Read-ahead risks:
1180  * -----------------
1181  * We have to guess which further data are needed by the user process.
1182  * If these data are often not really needed, it's bad for system 
1183  * performances.
1184  * However, we know that files are often accessed sequentially by 
1185  * application programs and it seems that it is possible to have some good 
1186  * strategy in that guessing.
1187  * We only try to read-ahead files that seems to be read sequentially.
1188  *
1189  * Asynchronous read-ahead risks:
1190  * ------------------------------
1191  * In order to maximize overlapping, we must start some asynchronous read 
1192  * request from the device, as soon as possible.
1193  * We must be very careful about:
1194  * - The number of effective pending IO read requests.
1195  *   ONE seems to be the only reasonable value.
1196  * - The total memory pool usage for the file access stream.
1197  *   This maximum memory usage is implicitly 2 IO read chunks:
1198  *   2*(MAX_READAHEAD + PAGE_CACHE_SIZE) = 156K if CONFIG_READA_SMALL is undefined,
1199  *   64k if defined (4K page size assumed).
1200  */
1201
1202 static inline int get_max_readahead(struct inode * inode)
1203 {
1204         if (!inode->i_dev || !max_readahead[MAJOR(inode->i_dev)])
1205                 return vm_max_readahead;
1206         return max_readahead[MAJOR(inode->i_dev)][MINOR(inode->i_dev)];
1207 }
1208
1209 static void generic_file_readahead(int reada_ok,
1210         struct file * filp, struct inode * inode,
1211         struct page * page)
1212 {
1213         unsigned long end_index;
1214         unsigned long index = page->index;
1215         unsigned long max_ahead, ahead;
1216         unsigned long raend;
1217         int max_readahead = get_max_readahead(inode);
1218
1219         end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1220
1221         raend = filp->f_raend;
1222         max_ahead = 0;
1223
1224 /*
1225  * The current page is locked.
1226  * If the current position is inside the previous read IO request, do not
1227  * try to reread previously read ahead pages.
1228  * Otherwise decide or not to read ahead some pages synchronously.
1229  * If we are not going to read ahead, set the read ahead context for this 
1230  * page only.
1231  */
1232         if (PageLocked(page)) {
1233                 if (!filp->f_ralen || index >= raend || index + filp->f_rawin < raend) {
1234                         raend = index;
1235                         if (raend < end_index)
1236                                 max_ahead = filp->f_ramax;
1237                         filp->f_rawin = 0;
1238                         filp->f_ralen = 1;
1239                         if (!max_ahead) {
1240                                 filp->f_raend  = index + filp->f_ralen;
1241                                 filp->f_rawin += filp->f_ralen;
1242                         }
1243                 }
1244         }
1245 /*
1246  * The current page is not locked.
1247  * If we were reading ahead and,
1248  * if the current max read ahead size is not zero and,
1249  * if the current position is inside the last read-ahead IO request,
1250  *   it is the moment to try to read ahead asynchronously.
1251  * We will later force unplug device in order to force asynchronous read IO.
1252  */
1253         else if (reada_ok && filp->f_ramax && raend >= 1 &&
1254                  index <= raend && index + filp->f_ralen >= raend) {
1255 /*
1256  * Add ONE page to max_ahead in order to try to have about the same IO max size
1257  * as synchronous read-ahead (MAX_READAHEAD + 1)*PAGE_CACHE_SIZE.
1258  * Compute the position of the last page we have tried to read in order to 
1259  * begin to read ahead just at the next page.
1260  */
1261                 raend -= 1;
1262                 if (raend < end_index)
1263                         max_ahead = filp->f_ramax + 1;
1264
1265                 if (max_ahead) {
1266                         filp->f_rawin = filp->f_ralen;
1267                         filp->f_ralen = 0;
1268                         reada_ok      = 2;
1269                 }
1270         }
1271 /*
1272  * Try to read ahead pages.
1273  * We hope that ll_rw_blk() plug/unplug, coalescence, requests sort and the
1274  * scheduler, will work enough for us to avoid too bad actuals IO requests.
1275  */
1276         ahead = 0;
1277         while (ahead < max_ahead) {
1278                 ahead ++;
1279                 if ((raend + ahead) >= end_index)
1280                         break;
1281                 if (page_cache_read(filp, raend + ahead) < 0)
1282                         break;
1283         }
1284 /*
1285  * If we tried to read ahead some pages,
1286  * If we tried to read ahead asynchronously,
1287  *   Try to force unplug of the device in order to start an asynchronous
1288  *   read IO request.
1289  * Update the read-ahead context.
1290  * Store the length of the current read-ahead window.
1291  * Double the current max read ahead size.
1292  *   That heuristic avoid to do some large IO for files that are not really
1293  *   accessed sequentially.
1294  */
1295         if (ahead) {
1296                 filp->f_ralen += ahead;
1297                 filp->f_rawin += filp->f_ralen;
1298                 filp->f_raend = raend + ahead + 1;
1299
1300                 filp->f_ramax += filp->f_ramax;
1301
1302                 if (filp->f_ramax > max_readahead)
1303                         filp->f_ramax = max_readahead;
1304
1305 #ifdef PROFILE_READAHEAD
1306                 profile_readahead((reada_ok == 2), filp);
1307 #endif
1308         }
1309
1310         return;
1311 }
1312
1313 /*
1314  * Mark a page as having seen activity.
1315  *
1316  * If it was already so marked, move it to the active queue and drop
1317  * the referenced bit.  Otherwise, just mark it for future action..
1318  */
1319 void mark_page_accessed(struct page *page)
1320 {
1321         if (!PageActive(page) && PageReferenced(page)) {
1322                 activate_page(page);
1323                 ClearPageReferenced(page);
1324         } else
1325                 SetPageReferenced(page);
1326 }
1327
1328 /*
1329  * This is a generic file read routine, and uses the
1330  * inode->i_op->readpage() function for the actual low-level
1331  * stuff.
1332  *
1333  * This is really ugly. But the goto's actually try to clarify some
1334  * of the logic when it comes to error handling etc.
1335  */
1336 void do_generic_file_read(struct file * filp, loff_t *ppos, read_descriptor_t * desc, read_actor_t actor)
1337 {
1338         struct address_space *mapping = filp->f_dentry->d_inode->i_mapping;
1339         struct inode *inode = mapping->host;
1340         unsigned long index, offset;
1341         struct page *cached_page;
1342         int reada_ok;
1343         int error;
1344         int max_readahead = get_max_readahead(inode);
1345
1346         cached_page = NULL;
1347         index = *ppos >> PAGE_CACHE_SHIFT;
1348         offset = *ppos & ~PAGE_CACHE_MASK;
1349
1350 /*
1351  * If the current position is outside the previous read-ahead window, 
1352  * we reset the current read-ahead context and set read ahead max to zero
1353  * (will be set to just needed value later),
1354  * otherwise, we assume that the file accesses are sequential enough to
1355  * continue read-ahead.
1356  */
1357         if (index > filp->f_raend || index + filp->f_rawin < filp->f_raend) {
1358                 reada_ok = 0;
1359                 filp->f_raend = 0;
1360                 filp->f_ralen = 0;
1361                 filp->f_ramax = 0;
1362                 filp->f_rawin = 0;
1363         } else {
1364                 reada_ok = 1;
1365         }
1366 /*
1367  * Adjust the current value of read-ahead max.
1368  * If the read operation stay in the first half page, force no readahead.
1369  * Otherwise try to increase read ahead max just enough to do the read request.
1370  * Then, at least MIN_READAHEAD if read ahead is ok,
1371  * and at most MAX_READAHEAD in all cases.
1372  */
1373         if (!index && offset + desc->count <= (PAGE_CACHE_SIZE >> 1)) {
1374                 filp->f_ramax = 0;
1375         } else {
1376                 unsigned long needed;
1377
1378                 needed = ((offset + desc->count) >> PAGE_CACHE_SHIFT) + 1;
1379
1380                 if (filp->f_ramax < needed)
1381                         filp->f_ramax = needed;
1382
1383                 if (reada_ok && filp->f_ramax < vm_min_readahead)
1384                                 filp->f_ramax = vm_min_readahead;
1385                 if (filp->f_ramax > max_readahead)
1386                         filp->f_ramax = max_readahead;
1387         }
1388
1389         for (;;) {
1390                 struct page *page, **hash;
1391                 unsigned long end_index, nr, ret;
1392
1393                 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1394                         
1395                 if (index > end_index)
1396                         break;
1397                 nr = PAGE_CACHE_SIZE;
1398                 if (index == end_index) {
1399                         nr = inode->i_size & ~PAGE_CACHE_MASK;
1400                         if (nr <= offset)
1401                                 break;
1402                 }
1403
1404                 nr = nr - offset;
1405
1406                 /*
1407                  * Try to find the data in the page cache..
1408                  */
1409                 hash = page_hash(mapping, index);
1410
1411                 spin_lock(&pagecache_lock);
1412                 page = __find_page_nolock(mapping, index, *hash);
1413                 if (!page)
1414                         goto no_cached_page;
1415 found_page:
1416                 page_cache_get(page);
1417                 spin_unlock(&pagecache_lock);
1418
1419                 if (!Page_Uptodate(page))
1420                         goto page_not_up_to_date;
1421                 generic_file_readahead(reada_ok, filp, inode, page);
1422 page_ok:
1423                 /* If users can be writing to this page using arbitrary
1424                  * virtual addresses, take care about potential aliasing
1425                  * before reading the page on the kernel side.
1426                  */
1427                 if (mapping->i_mmap_shared != NULL)
1428                         flush_dcache_page(page);
1429
1430                 /*
1431                  * Mark the page accessed if we read the
1432                  * beginning or we just did an lseek.
1433                  */
1434                 if (!offset || !filp->f_reada)
1435                         mark_page_accessed(page);
1436
1437                 /*
1438                  * Ok, we have the page, and it's up-to-date, so
1439                  * now we can copy it to user space...
1440                  *
1441                  * The actor routine returns how many bytes were actually used..
1442                  * NOTE! This may not be the same as how much of a user buffer
1443                  * we filled up (we may be padding etc), so we can only update
1444                  * "pos" here (the actor routine has to update the user buffer
1445                  * pointers and the remaining count).
1446                  */
1447                 ret = actor(desc, page, offset, nr);
1448                 offset += ret;
1449                 index += offset >> PAGE_CACHE_SHIFT;
1450                 offset &= ~PAGE_CACHE_MASK;
1451
1452                 page_cache_release(page);
1453                 if (ret == nr && desc->count)
1454                         continue;
1455                 break;
1456
1457 /*
1458  * Ok, the page was not immediately readable, so let's try to read ahead while we're at it..
1459  */
1460 page_not_up_to_date:
1461                 generic_file_readahead(reada_ok, filp, inode, page);
1462
1463                 if (Page_Uptodate(page))
1464                         goto page_ok;
1465
1466                 /* Get exclusive access to the page ... */
1467                 lock_page(page);
1468
1469                 /* Did it get unhashed before we got the lock? */
1470                 if (!page->mapping) {
1471                         UnlockPage(page);
1472                         page_cache_release(page);
1473                         continue;
1474                 }
1475
1476                 /* Did somebody else fill it already? */
1477                 if (Page_Uptodate(page)) {
1478                         UnlockPage(page);
1479                         goto page_ok;
1480                 }
1481
1482 readpage:
1483                 /* ... and start the actual read. The read will unlock the page. */
1484                 error = mapping->a_ops->readpage(filp, page);
1485
1486                 if (!error) {
1487                         if (Page_Uptodate(page))
1488                                 goto page_ok;
1489
1490                         /* Again, try some read-ahead while waiting for the page to finish.. */
1491                         generic_file_readahead(reada_ok, filp, inode, page);
1492                         wait_on_page(page);
1493                         if (Page_Uptodate(page))
1494                                 goto page_ok;
1495                         error = -EIO;
1496                 }
1497
1498                 /* UHHUH! A synchronous read error occurred. Report it */
1499                 desc->error = error;
1500                 page_cache_release(page);
1501                 break;
1502
1503 no_cached_page:
1504                 /*
1505                  * Ok, it wasn't cached, so we need to create a new
1506                  * page..
1507                  *
1508                  * We get here with the page cache lock held.
1509                  */
1510                 if (!cached_page) {
1511                         spin_unlock(&pagecache_lock);
1512                         cached_page = page_cache_alloc(mapping);
1513                         if (!cached_page) {
1514                                 desc->error = -ENOMEM;
1515                                 break;
1516                         }
1517
1518                         /*
1519                          * Somebody may have added the page while we
1520                          * dropped the page cache lock. Check for that.
1521                          */
1522                         spin_lock(&pagecache_lock);
1523                         page = __find_page_nolock(mapping, index, *hash);
1524                         if (page)
1525                                 goto found_page;
1526                 }
1527
1528                 /*
1529                  * Ok, add the new page to the hash-queues...
1530                  */
1531                 page = cached_page;
1532                 __add_to_page_cache(page, mapping, index, hash);
1533                 spin_unlock(&pagecache_lock);
1534                 lru_cache_add(page);            
1535                 cached_page = NULL;
1536
1537                 goto readpage;
1538         }
1539
1540         *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1541         filp->f_reada = 1;
1542         if (cached_page)
1543                 page_cache_release(cached_page);
1544         UPDATE_ATIME(inode);
1545 }
1546
1547 static ssize_t generic_file_direct_IO(int rw, struct file * filp, char * buf, size_t count, loff_t offset)
1548 {
1549         ssize_t retval;
1550         int new_iobuf, chunk_size, blocksize_mask, blocksize, blocksize_bits, iosize, progress;
1551         struct kiobuf * iobuf;
1552         struct address_space * mapping = filp->f_dentry->d_inode->i_mapping;
1553         struct inode * inode = mapping->host;
1554         loff_t size = inode->i_size;
1555
1556         new_iobuf = 0;
1557         iobuf = filp->f_iobuf;
1558         if (test_and_set_bit(0, &filp->f_iobuf_lock)) {
1559                 /*
1560                  * A parallel read/write is using the preallocated iobuf
1561                  * so just run slow and allocate a new one.
1562                  */
1563                 retval = alloc_kiovec(1, &iobuf);
1564                 if (retval)
1565                         goto out;
1566                 new_iobuf = 1;
1567         }
1568
1569         blocksize = 1 << inode->i_blkbits;
1570         blocksize_bits = inode->i_blkbits;
1571         blocksize_mask = blocksize - 1;
1572         chunk_size = KIO_MAX_ATOMIC_IO << 10;
1573
1574         retval = -EINVAL;
1575         if ((offset & blocksize_mask) || (count & blocksize_mask) || ((unsigned long) buf & blocksize_mask))
1576                 goto out_free;
1577         if (!mapping->a_ops->direct_IO)
1578                 goto out_free;
1579
1580         if ((rw == READ) && (offset + count > size))
1581                 count = size - offset;
1582
1583         /*
1584          * Flush to disk exclusively the _data_, metadata must remain
1585          * completly asynchronous or performance will go to /dev/null.
1586          */
1587         retval = filemap_fdatasync(mapping);
1588         if (retval == 0)
1589                 retval = fsync_inode_data_buffers(inode);
1590         if (retval == 0)
1591                 retval = filemap_fdatawait(mapping);
1592         if (retval < 0)
1593                 goto out_free;
1594
1595         progress = retval = 0;
1596         while (count > 0) {
1597                 iosize = count;
1598                 if (iosize > chunk_size)
1599                         iosize = chunk_size;
1600
1601                 retval = map_user_kiobuf(rw, iobuf, (unsigned long) buf, iosize);
1602                 if (retval)
1603                         break;
1604
1605                 retval = mapping->a_ops->direct_IO(rw, inode, iobuf, (offset+progress) >> blocksize_bits, blocksize);
1606
1607                 if (rw == READ && retval > 0)
1608                         mark_dirty_kiobuf(iobuf, retval);
1609                 
1610                 if (retval >= 0) {
1611                         count -= retval;
1612                         buf += retval;
1613                         /* warning: weird semantics here, we're reporting a read behind the end of the file */
1614                         progress += retval;
1615                 }
1616
1617                 unmap_kiobuf(iobuf);
1618
1619                 if (retval != iosize)
1620                         break;
1621         }
1622
1623         if (progress)
1624                 retval = progress;
1625
1626  out_free:
1627         if (!new_iobuf)
1628                 clear_bit(0, &filp->f_iobuf_lock);
1629         else
1630                 free_kiovec(1, &iobuf);
1631  out:   
1632         return retval;
1633 }
1634
1635 int file_read_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
1636 {
1637         char *kaddr;
1638         unsigned long left, count = desc->count;
1639
1640         if (size > count)
1641                 size = count;
1642
1643         kaddr = kmap(page);
1644         left = __copy_to_user(desc->buf, kaddr + offset, size);
1645         kunmap(page);
1646         
1647         if (left) {
1648                 size -= left;
1649                 desc->error = -EFAULT;
1650         }
1651         desc->count = count - size;
1652         desc->written += size;
1653         desc->buf += size;
1654         return size;
1655 }
1656
1657 /*
1658  * This is the "read()" routine for all filesystems
1659  * that can use the page cache directly.
1660  */
1661 ssize_t generic_file_read(struct file * filp, char * buf, size_t count, loff_t *ppos)
1662 {
1663         ssize_t retval;
1664
1665         if ((ssize_t) count < 0)
1666                 return -EINVAL;
1667
1668         if (filp->f_flags & O_DIRECT)
1669                 goto o_direct;
1670
1671         retval = -EFAULT;
1672         if (access_ok(VERIFY_WRITE, buf, count)) {
1673                 retval = 0;
1674
1675                 if (count) {
1676                         read_descriptor_t desc;
1677
1678                         desc.written = 0;
1679                         desc.count = count;
1680                         desc.buf = buf;
1681                         desc.error = 0;
1682                         do_generic_file_read(filp, ppos, &desc, file_read_actor);
1683
1684                         retval = desc.written;
1685                         if (!retval)
1686                                 retval = desc.error;
1687                 }
1688         }
1689  out:
1690         return retval;
1691
1692  o_direct:
1693         {
1694                 loff_t pos = *ppos, size;
1695                 struct address_space *mapping = filp->f_dentry->d_inode->i_mapping;
1696                 struct inode *inode = mapping->host;
1697
1698                 retval = 0;
1699                 if (!count)
1700                         goto out; /* skip atime */
1701                 size = inode->i_size;
1702                 if (pos < size) {
1703                         retval = generic_file_direct_IO(READ, filp, buf, count, pos);
1704                         if (retval > 0)
1705                                 *ppos = pos + retval;
1706                 }
1707                 UPDATE_ATIME(filp->f_dentry->d_inode);
1708                 goto out;
1709         }
1710 }
1711
1712 static int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset , unsigned long size)
1713 {
1714         ssize_t written;
1715         unsigned long count = desc->count;
1716         struct file *file = (struct file *) desc->buf;
1717
1718         if (size > count)
1719                 size = count;
1720
1721         if (file->f_op->sendpage) {
1722                 written = file->f_op->sendpage(file, page, offset,
1723                                                size, &file->f_pos, size<count);
1724         } else {
1725                 char *kaddr;
1726                 mm_segment_t old_fs;
1727
1728                 old_fs = get_fs();
1729                 set_fs(KERNEL_DS);
1730
1731                 kaddr = kmap(page);
1732                 written = file->f_op->write(file, kaddr + offset, size, &file->f_pos);
1733                 kunmap(page);
1734
1735                 set_fs(old_fs);
1736         }
1737         if (written < 0) {
1738                 desc->error = written;
1739                 written = 0;
1740         }
1741         desc->count = count - written;
1742         desc->written += written;
1743         return written;
1744 }
1745
1746 asmlinkage ssize_t sys_sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
1747 {
1748         ssize_t retval;
1749         struct file * in_file, * out_file;
1750         struct inode * in_inode, * out_inode;
1751
1752         /*
1753          * Get input file, and verify that it is ok..
1754          */
1755         retval = -EBADF;
1756         in_file = fget(in_fd);
1757         if (!in_file)
1758                 goto out;
1759         if (!(in_file->f_mode & FMODE_READ))
1760                 goto fput_in;
1761         retval = -EINVAL;
1762         in_inode = in_file->f_dentry->d_inode;
1763         if (!in_inode)
1764                 goto fput_in;
1765         if (!in_inode->i_mapping->a_ops->readpage)
1766                 goto fput_in;
1767         retval = locks_verify_area(FLOCK_VERIFY_READ, in_inode, in_file, in_file->f_pos, count);
1768         if (retval)
1769                 goto fput_in;
1770
1771         /*
1772          * Get output file, and verify that it is ok..
1773          */
1774         retval = -EBADF;
1775         out_file = fget(out_fd);
1776         if (!out_file)
1777                 goto fput_in;
1778         if (!(out_file->f_mode & FMODE_WRITE))
1779                 goto fput_out;
1780         retval = -EINVAL;
1781         if (!out_file->f_op || !out_file->f_op->write)
1782                 goto fput_out;
1783         out_inode = out_file->f_dentry->d_inode;
1784         retval = locks_verify_area(FLOCK_VERIFY_WRITE, out_inode, out_file, out_file->f_pos, count);
1785         if (retval)
1786                 goto fput_out;
1787
1788         retval = 0;
1789         if (count) {
1790                 read_descriptor_t desc;
1791                 loff_t pos = 0, *ppos;
1792
1793                 retval = -EFAULT;
1794                 ppos = &in_file->f_pos;
1795                 if (offset) {
1796                         if (get_user(pos, offset))
1797                                 goto fput_out;
1798                         ppos = &pos;
1799                 }
1800
1801                 desc.written = 0;
1802                 desc.count = count;
1803                 desc.buf = (char *) out_file;
1804                 desc.error = 0;
1805                 do_generic_file_read(in_file, ppos, &desc, file_send_actor);
1806
1807                 retval = desc.written;
1808                 if (!retval)
1809                         retval = desc.error;
1810                 if (offset)
1811                         put_user(pos, offset);
1812         }
1813
1814 fput_out:
1815         fput(out_file);
1816 fput_in:
1817         fput(in_file);
1818 out:
1819         return retval;
1820 }
1821
1822 static ssize_t do_readahead(struct file *file, unsigned long index, unsigned long nr)
1823 {
1824         struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
1825         unsigned long max;
1826
1827         if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1828                 return -EINVAL;
1829
1830         /* Limit it to the size of the file.. */
1831         max = (mapping->host->i_size + ~PAGE_CACHE_MASK) >> PAGE_CACHE_SHIFT;
1832         if (index > max)
1833                 return 0;
1834         max -= index;
1835         if (nr > max)
1836                 nr = max;
1837
1838         /* And limit it to a sane percentage of the inactive list.. */
1839         max = nr_inactive_pages / 2;
1840         if (nr > max)
1841                 nr = max;
1842
1843         while (nr) {
1844                 page_cache_read(file, index);
1845                 index++;
1846                 nr--;
1847         }
1848         return 0;
1849 }
1850
1851 asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
1852 {
1853         ssize_t ret;
1854         struct file *file;
1855
1856         ret = -EBADF;
1857         file = fget(fd);
1858         if (file) {
1859                 if (file->f_mode & FMODE_READ) {
1860                         unsigned long start = offset >> PAGE_CACHE_SHIFT;
1861                         unsigned long len = (count + ((long)offset & ~PAGE_CACHE_MASK)) >> PAGE_CACHE_SHIFT;
1862                         ret = do_readahead(file, start, len);
1863                 }
1864                 fput(file);
1865         }
1866         return ret;
1867 }
1868
1869 /*
1870  * Read-ahead and flush behind for MADV_SEQUENTIAL areas.  Since we are
1871  * sure this is sequential access, we don't need a flexible read-ahead
1872  * window size -- we can always use a large fixed size window.
1873  */
1874 static void nopage_sequential_readahead(struct vm_area_struct * vma,
1875         unsigned long pgoff, unsigned long filesize)
1876 {
1877         unsigned long ra_window;
1878
1879         ra_window = get_max_readahead(vma->vm_file->f_dentry->d_inode);
1880         ra_window = CLUSTER_OFFSET(ra_window + CLUSTER_PAGES - 1);
1881
1882         /* vm_raend is zero if we haven't read ahead in this area yet.  */
1883         if (vma->vm_raend == 0)
1884                 vma->vm_raend = vma->vm_pgoff + ra_window;
1885
1886         /*
1887          * If we've just faulted the page half-way through our window,
1888          * then schedule reads for the next window, and release the
1889          * pages in the previous window.
1890          */
1891         if ((pgoff + (ra_window >> 1)) == vma->vm_raend) {
1892                 unsigned long start = vma->vm_pgoff + vma->vm_raend;
1893                 unsigned long end = start + ra_window;
1894
1895                 if (end > ((vma->vm_end >> PAGE_SHIFT) + vma->vm_pgoff))
1896                         end = (vma->vm_end >> PAGE_SHIFT) + vma->vm_pgoff;
1897                 if (start > end)
1898                         return;
1899
1900                 while ((start < end) && (start < filesize)) {
1901                         if (read_cluster_nonblocking(vma->vm_file,
1902                                                         start, filesize) < 0)
1903                                 break;
1904                         start += CLUSTER_PAGES;
1905                 }
1906                 run_task_queue(&tq_disk);
1907
1908                 /* if we're far enough past the beginning of this area,
1909                    recycle pages that are in the previous window. */
1910                 if (vma->vm_raend > (vma->vm_pgoff + ra_window + ra_window)) {
1911                         unsigned long window = ra_window << PAGE_SHIFT;
1912
1913                         end = vma->vm_start + (vma->vm_raend << PAGE_SHIFT);
1914                         end -= window + window;
1915                         filemap_sync(vma, end - window, window, MS_INVALIDATE);
1916                 }
1917
1918                 vma->vm_raend += ra_window;
1919         }
1920
1921         return;
1922 }
1923
1924 /*
1925  * filemap_nopage() is invoked via the vma operations vector for a
1926  * mapped memory region to read in file data during a page fault.
1927  *
1928  * The goto's are kind of ugly, but this streamlines the normal case of having
1929  * it in the page cache, and handles the special cases reasonably without
1930  * having a lot of duplicated code.
1931  */
1932 struct page * filemap_nopage(struct vm_area_struct * area, unsigned long address, int unused)
1933 {
1934         int error;
1935         struct file *file = area->vm_file;
1936         struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
1937         struct inode *inode = mapping->host;
1938         struct page *page, **hash;
1939         unsigned long size, pgoff, endoff;
1940
1941         pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1942         endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
1943
1944 retry_all:
1945         /*
1946          * An external ptracer can access pages that normally aren't
1947          * accessible..
1948          */
1949         size = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1950         if ((pgoff >= size) && (area->vm_mm == current->mm))
1951                 return NULL;
1952
1953         /* The "size" of the file, as far as mmap is concerned, isn't bigger than the mapping */
1954         if (size > endoff)
1955                 size = endoff;
1956
1957         /*
1958          * Do we have something in the page cache already?
1959          */
1960         hash = page_hash(mapping, pgoff);
1961 retry_find:
1962         page = __find_get_page(mapping, pgoff, hash);
1963         if (!page)
1964                 goto no_cached_page;
1965
1966         /*
1967          * Ok, found a page in the page cache, now we need to check
1968          * that it's up-to-date.
1969          */
1970         if (!Page_Uptodate(page))
1971                 goto page_not_uptodate;
1972
1973 success:
1974         /*
1975          * Try read-ahead for sequential areas.
1976          */
1977         if (VM_SequentialReadHint(area))
1978                 nopage_sequential_readahead(area, pgoff, size);
1979
1980         /*
1981          * Found the page and have a reference on it, need to check sharing
1982          * and possibly copy it over to another page..
1983          */
1984         mark_page_accessed(page);
1985         flush_page_to_ram(page);
1986         return page;
1987
1988 no_cached_page:
1989         /*
1990          * If the requested offset is within our file, try to read a whole 
1991          * cluster of pages at once.
1992          *
1993          * Otherwise, we're off the end of a privately mapped file,
1994          * so we need to map a zero page.
1995          */
1996         if ((pgoff < size) && !VM_RandomReadHint(area))
1997                 error = read_cluster_nonblocking(file, pgoff, size);
1998         else
1999                 error = page_cache_read(file, pgoff);
2000
2001         /*
2002          * The page we want has now been added to the page cache.
2003          * In the unlikely event that someone removed it in the
2004          * meantime, we'll just come back here and read it again.
2005          */
2006         if (error >= 0)
2007                 goto retry_find;
2008
2009         /*
2010          * An error return from page_cache_read can result if the
2011          * system is low on memory, or a problem occurs while trying
2012          * to schedule I/O.
2013          */
2014         if (error == -ENOMEM)
2015                 return NOPAGE_OOM;
2016         return NULL;
2017
2018 page_not_uptodate:
2019         lock_page(page);
2020
2021         /* Did it get unhashed while we waited for it? */
2022         if (!page->mapping) {
2023                 UnlockPage(page);
2024                 page_cache_release(page);
2025                 goto retry_all;
2026         }
2027
2028         /* Did somebody else get it up-to-date? */
2029         if (Page_Uptodate(page)) {
2030                 UnlockPage(page);
2031                 goto success;
2032         }
2033
2034         if (!mapping->a_ops->readpage(file, page)) {
2035                 wait_on_page(page);
2036                 if (Page_Uptodate(page))
2037                         goto success;
2038         }
2039
2040         /*
2041          * Umm, take care of errors if the page isn't up-to-date.
2042          * Try to re-read it _once_. We do this synchronously,
2043          * because there really aren't any performance issues here
2044          * and we need to check for errors.
2045          */
2046         lock_page(page);
2047
2048         /* Somebody truncated the page on us? */
2049         if (!page->mapping) {
2050                 UnlockPage(page);
2051                 page_cache_release(page);
2052                 goto retry_all;
2053         }
2054
2055         /* Somebody else successfully read it in? */
2056         if (Page_Uptodate(page)) {
2057                 UnlockPage(page);
2058                 goto success;
2059         }
2060         ClearPageError(page);
2061         if (!mapping->a_ops->readpage(file, page)) {
2062                 wait_on_page(page);
2063                 if (Page_Uptodate(page))
2064                         goto success;
2065         }
2066
2067         /*
2068          * Things didn't work out. Return zero to tell the
2069          * mm layer so, possibly freeing the page cache page first.
2070          */
2071         page_cache_release(page);
2072         return NULL;
2073 }
2074
2075 /* Called with mm->page_table_lock held to protect against other
2076  * threads/the swapper from ripping pte's out from under us.
2077  */
2078 static inline int filemap_sync_pte(pte_t * ptep, struct vm_area_struct *vma,
2079         unsigned long address, unsigned int flags)
2080 {
2081         pte_t pte = *ptep;
2082
2083         if (pte_present(pte)) {
2084                 struct page *page = pte_page(pte);
2085                 if (VALID_PAGE(page) && !PageReserved(page) && ptep_test_and_clear_dirty(ptep)) {
2086                         flush_tlb_page(vma, address);
2087                         set_page_dirty(page);
2088                 }
2089         }
2090         return 0;
2091 }
2092
2093 static inline int filemap_sync_pte_range(pmd_t * pmd,
2094         unsigned long address, unsigned long size, 
2095         struct vm_area_struct *vma, unsigned long offset, unsigned int flags)
2096 {
2097         pte_t * pte;
2098         unsigned long end;
2099         int error;
2100
2101         if (pmd_none(*pmd))
2102                 return 0;
2103         if (pmd_bad(*pmd)) {
2104                 pmd_ERROR(*pmd);
2105                 pmd_clear(pmd);
2106                 return 0;
2107         }
2108         pte = pte_offset(pmd, address);
2109         offset += address & PMD_MASK;
2110         address &= ~PMD_MASK;
2111         end = address + size;
2112         if (end > PMD_SIZE)
2113                 end = PMD_SIZE;
2114         error = 0;
2115         do {
2116                 error |= filemap_sync_pte(pte, vma, address + offset, flags);
2117                 address += PAGE_SIZE;
2118                 pte++;
2119         } while (address && (address < end));
2120         return error;
2121 }
2122
2123 static inline int filemap_sync_pmd_range(pgd_t * pgd,
2124         unsigned long address, unsigned long size, 
2125         struct vm_area_struct *vma, unsigned int flags)
2126 {
2127         pmd_t * pmd;
2128         unsigned long offset, end;
2129         int error;
2130
2131         if (pgd_none(*pgd))
2132                 return 0;
2133         if (pgd_bad(*pgd)) {
2134                 pgd_ERROR(*pgd);
2135                 pgd_clear(pgd);
2136                 return 0;
2137         }
2138         pmd = pmd_offset(pgd, address);
2139         offset = address & PGDIR_MASK;
2140         address &= ~PGDIR_MASK;
2141         end = address + size;
2142         if (end > PGDIR_SIZE)
2143                 end = PGDIR_SIZE;
2144         error = 0;
2145         do {
2146                 error |= filemap_sync_pte_range(pmd, address, end - address, vma, offset, flags);
2147                 address = (address + PMD_SIZE) & PMD_MASK;
2148                 pmd++;
2149         } while (address && (address < end));
2150         return error;
2151 }
2152
2153 int filemap_sync(struct vm_area_struct * vma, unsigned long address,
2154         size_t size, unsigned int flags)
2155 {
2156         pgd_t * dir;
2157         unsigned long end = address + size;
2158         int error = 0;
2159
2160         /* Aquire the lock early; it may be possible to avoid dropping
2161          * and reaquiring it repeatedly.
2162          */
2163         spin_lock(&vma->vm_mm->page_table_lock);
2164
2165         dir = pgd_offset(vma->vm_mm, address);
2166         flush_cache_range(vma->vm_mm, end - size, end);
2167         if (address >= end)
2168                 BUG();
2169         do {
2170                 error |= filemap_sync_pmd_range(dir, address, end - address, vma, flags);
2171                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
2172                 dir++;
2173         } while (address && (address < end));
2174         flush_tlb_range(vma->vm_mm, end - size, end);
2175
2176         spin_unlock(&vma->vm_mm->page_table_lock);
2177
2178         return error;
2179 }
2180
2181 static struct vm_operations_struct generic_file_vm_ops = {
2182         nopage:         filemap_nopage,
2183 };
2184
2185 /* This is used for a general mmap of a disk file */
2186
2187 int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
2188 {
2189         struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
2190         struct inode *inode = mapping->host;
2191
2192         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
2193                 if (!mapping->a_ops->writepage)
2194                         return -EINVAL;
2195         }
2196         if (!mapping->a_ops->readpage)
2197                 return -ENOEXEC;
2198         UPDATE_ATIME(inode);
2199         vma->vm_ops = &generic_file_vm_ops;
2200         return 0;
2201 }
2202
2203 /*
2204  * The msync() system call.
2205  */
2206
2207 /*
2208  * MS_SYNC syncs the entire file - including mappings.
2209  *
2210  * MS_ASYNC initiates writeout of just the dirty mapped data.
2211  * This provides no guarantee of file integrity - things like indirect
2212  * blocks may not have started writeout.  MS_ASYNC is primarily useful
2213  * where the application knows that it has finished with the data and
2214  * wishes to intelligently schedule its own I/O traffic.
2215  */
2216 static int msync_interval(struct vm_area_struct * vma,
2217         unsigned long start, unsigned long end, int flags)
2218 {
2219         int ret = 0;
2220         struct file * file = vma->vm_file;
2221
2222         if ( (flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED) )
2223                 return -EBUSY;
2224
2225         if (file && (vma->vm_flags & VM_SHARED)) {
2226                 ret = filemap_sync(vma, start, end-start, flags);
2227
2228                 if (!ret && (flags & (MS_SYNC|MS_ASYNC))) {
2229                         struct inode * inode = file->f_dentry->d_inode;
2230
2231                         down(&inode->i_sem);
2232                         ret = filemap_fdatasync(inode->i_mapping);
2233                         if (flags & MS_SYNC) {
2234                                 int err;
2235
2236                                 if (file->f_op && file->f_op->fsync) {
2237                                         err = file->f_op->fsync(file, file->f_dentry, 1);
2238                                         if (err && !ret)
2239                                                 ret = err;
2240                                 }
2241                                 err = filemap_fdatawait(inode->i_mapping);
2242                                 if (err && !ret)
2243                                         ret = err;
2244                         }
2245                         up(&inode->i_sem);
2246                 }
2247         }
2248         return ret;
2249 }
2250
2251 asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
2252 {
2253         unsigned long end;
2254         struct vm_area_struct * vma;
2255         int unmapped_error, error = -EINVAL;
2256
2257         down_read(&current->mm->mmap_sem);
2258         if (start & ~PAGE_MASK)
2259                 goto out;
2260         len = (len + ~PAGE_MASK) & PAGE_MASK;
2261         end = start + len;
2262         if (end < start)
2263                 goto out;
2264         if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
2265                 goto out;
2266         if ((flags & MS_ASYNC) && (flags & MS_SYNC))
2267                 goto out;
2268
2269         error = 0;
2270         if (end == start)
2271                 goto out;
2272         /*
2273          * If the interval [start,end) covers some unmapped address ranges,
2274          * just ignore them, but return -ENOMEM at the end.
2275          */
2276         vma = find_vma(current->mm, start);
2277         unmapped_error = 0;
2278         for (;;) {
2279                 /* Still start < end. */
2280                 error = -ENOMEM;
2281                 if (!vma)
2282                         goto out;
2283                 /* Here start < vma->vm_end. */
2284                 if (start < vma->vm_start) {
2285                         unmapped_error = -ENOMEM;
2286                         start = vma->vm_start;
2287                 }
2288                 /* Here vma->vm_start <= start < vma->vm_end. */
2289                 if (end <= vma->vm_end) {
2290                         if (start < end) {
2291                                 error = msync_interval(vma, start, end, flags);
2292                                 if (error)
2293                                         goto out;
2294                         }
2295                         error = unmapped_error;
2296                         goto out;
2297                 }
2298                 /* Here vma->vm_start <= start < vma->vm_end < end. */
2299                 error = msync_interval(vma, start, vma->vm_end, flags);
2300                 if (error)
2301                         goto out;
2302                 start = vma->vm_end;
2303                 vma = vma->vm_next;
2304         }
2305 out:
2306         up_read(&current->mm->mmap_sem);
2307         return error;
2308 }
2309
2310 static inline void setup_read_behavior(struct vm_area_struct * vma,
2311         int behavior)
2312 {
2313         VM_ClearReadHint(vma);
2314         switch(behavior) {
2315                 case MADV_SEQUENTIAL:
2316                         vma->vm_flags |= VM_SEQ_READ;
2317                         break;
2318                 case MADV_RANDOM:
2319                         vma->vm_flags |= VM_RAND_READ;
2320                         break;
2321                 default:
2322                         break;
2323         }
2324         return;
2325 }
2326
2327 static long madvise_fixup_start(struct vm_area_struct * vma,
2328         unsigned long end, int behavior)
2329 {
2330         struct vm_area_struct * n;
2331         struct mm_struct * mm = vma->vm_mm;
2332
2333         n = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2334         if (!n)
2335                 return -EAGAIN;
2336         *n = *vma;
2337         n->vm_end = end;
2338         setup_read_behavior(n, behavior);
2339         n->vm_raend = 0;
2340         if (n->vm_file)
2341                 get_file(n->vm_file);
2342         if (n->vm_ops && n->vm_ops->open)
2343                 n->vm_ops->open(n);
2344         vma->vm_pgoff += (end - vma->vm_start) >> PAGE_SHIFT;
2345         lock_vma_mappings(vma);
2346         spin_lock(&mm->page_table_lock);
2347         vma->vm_start = end;
2348         __insert_vm_struct(mm, n);
2349         spin_unlock(&mm->page_table_lock);
2350         unlock_vma_mappings(vma);
2351         return 0;
2352 }
2353
2354 static long madvise_fixup_end(struct vm_area_struct * vma,
2355         unsigned long start, int behavior)
2356 {
2357         struct vm_area_struct * n;
2358         struct mm_struct * mm = vma->vm_mm;
2359
2360         n = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2361         if (!n)
2362                 return -EAGAIN;
2363         *n = *vma;
2364         n->vm_start = start;
2365         n->vm_pgoff += (n->vm_start - vma->vm_start) >> PAGE_SHIFT;
2366         setup_read_behavior(n, behavior);
2367         n->vm_raend = 0;
2368         if (n->vm_file)
2369                 get_file(n->vm_file);
2370         if (n->vm_ops && n->vm_ops->open)
2371                 n->vm_ops->open(n);
2372         lock_vma_mappings(vma);
2373         spin_lock(&mm->page_table_lock);
2374         vma->vm_end = start;
2375         __insert_vm_struct(mm, n);
2376         spin_unlock(&mm->page_table_lock);
2377         unlock_vma_mappings(vma);
2378         return 0;
2379 }
2380
2381 static long madvise_fixup_middle(struct vm_area_struct * vma,
2382         unsigned long start, unsigned long end, int behavior)
2383 {
2384         struct vm_area_struct * left, * right;
2385         struct mm_struct * mm = vma->vm_mm;
2386
2387         left = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2388         if (!left)
2389                 return -EAGAIN;
2390         right = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2391         if (!right) {
2392                 kmem_cache_free(vm_area_cachep, left);
2393                 return -EAGAIN;
2394         }
2395         *left = *vma;
2396         *right = *vma;
2397         left->vm_end = start;
2398         right->vm_start = end;
2399         right->vm_pgoff += (right->vm_start - left->vm_start) >> PAGE_SHIFT;
2400         left->vm_raend = 0;
2401         right->vm_raend = 0;
2402         if (vma->vm_file)
2403                 atomic_add(2, &vma->vm_file->f_count);
2404
2405         if (vma->vm_ops && vma->vm_ops->open) {
2406                 vma->vm_ops->open(left);
2407                 vma->vm_ops->open(right);
2408         }
2409         vma->vm_pgoff += (start - vma->vm_start) >> PAGE_SHIFT;
2410         vma->vm_raend = 0;
2411         lock_vma_mappings(vma);
2412         spin_lock(&mm->page_table_lock);
2413         vma->vm_start = start;
2414         vma->vm_end = end;
2415         setup_read_behavior(vma, behavior);
2416         __insert_vm_struct(mm, left);
2417         __insert_vm_struct(mm, right);
2418         spin_unlock(&mm->page_table_lock);
2419         unlock_vma_mappings(vma);
2420         return 0;
2421 }
2422
2423 /*
2424  * We can potentially split a vm area into separate
2425  * areas, each area with its own behavior.
2426  */
2427 static long madvise_behavior(struct vm_area_struct * vma,
2428         unsigned long start, unsigned long end, int behavior)
2429 {
2430         int error = 0;
2431
2432         /* This caps the number of vma's this process can own */
2433         if (vma->vm_mm->map_count > max_map_count)
2434                 return -ENOMEM;
2435
2436         if (start == vma->vm_start) {
2437                 if (end == vma->vm_end) {
2438                         setup_read_behavior(vma, behavior);
2439                         vma->vm_raend = 0;
2440                 } else
2441                         error = madvise_fixup_start(vma, end, behavior);
2442         } else {
2443                 if (end == vma->vm_end)
2444                         error = madvise_fixup_end(vma, start, behavior);
2445                 else
2446                         error = madvise_fixup_middle(vma, start, end, behavior);
2447         }
2448
2449         return error;
2450 }
2451
2452 /*
2453  * Schedule all required I/O operations, then run the disk queue
2454  * to make sure they are started.  Do not wait for completion.
2455  */
2456 static long madvise_willneed(struct vm_area_struct * vma,
2457         unsigned long start, unsigned long end)
2458 {
2459         long error = -EBADF;
2460         struct file * file;
2461         unsigned long size, rlim_rss;
2462
2463         /* Doesn't work if there's no mapped file. */
2464         if (!vma->vm_file)
2465                 return error;
2466         file = vma->vm_file;
2467         size = (file->f_dentry->d_inode->i_size + PAGE_CACHE_SIZE - 1) >>
2468                                                         PAGE_CACHE_SHIFT;
2469
2470         start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2471         if (end > vma->vm_end)
2472                 end = vma->vm_end;
2473         end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2474
2475         /* Make sure this doesn't exceed the process's max rss. */
2476         error = -EIO;
2477         rlim_rss = current->rlim ?  current->rlim[RLIMIT_RSS].rlim_cur :
2478                                 LONG_MAX; /* default: see resource.h */
2479         if ((vma->vm_mm->rss + (end - start)) > rlim_rss)
2480                 return error;
2481
2482         /* round to cluster boundaries if this isn't a "random" area. */
2483         if (!VM_RandomReadHint(vma)) {
2484                 start = CLUSTER_OFFSET(start);
2485                 end = CLUSTER_OFFSET(end + CLUSTER_PAGES - 1);
2486
2487                 while ((start < end) && (start < size)) {
2488                         error = read_cluster_nonblocking(file, start, size);
2489                         start += CLUSTER_PAGES;
2490                         if (error < 0)
2491                                 break;
2492                 }
2493         } else {
2494                 while ((start < end) && (start < size)) {
2495                         error = page_cache_read(file, start);
2496                         start++;
2497                         if (error < 0)
2498                                 break;
2499                 }
2500         }
2501
2502         /* Don't wait for someone else to push these requests. */
2503         run_task_queue(&tq_disk);
2504
2505         return error;
2506 }
2507
2508 /*
2509  * Application no longer needs these pages.  If the pages are dirty,
2510  * it's OK to just throw them away.  The app will be more careful about
2511  * data it wants to keep.  Be sure to free swap resources too.  The
2512  * zap_page_range call sets things up for refill_inactive to actually free
2513  * these pages later if no one else has touched them in the meantime,
2514  * although we could add these pages to a global reuse list for
2515  * refill_inactive to pick up before reclaiming other pages.
2516  *
2517  * NB: This interface discards data rather than pushes it out to swap,
2518  * as some implementations do.  This has performance implications for
2519  * applications like large transactional databases which want to discard
2520  * pages in anonymous maps after committing to backing store the data
2521  * that was kept in them.  There is no reason to write this data out to
2522  * the swap area if the application is discarding it.
2523  *
2524  * An interface that causes the system to free clean pages and flush
2525  * dirty pages is already available as msync(MS_INVALIDATE).
2526  */
2527 static long madvise_dontneed(struct vm_area_struct * vma,
2528         unsigned long start, unsigned long end)
2529 {
2530         if (vma->vm_flags & VM_LOCKED)
2531                 return -EINVAL;
2532
2533         zap_page_range(vma->vm_mm, start, end - start);
2534         return 0;
2535 }
2536
2537 static long madvise_vma(struct vm_area_struct * vma, unsigned long start,
2538         unsigned long end, int behavior)
2539 {
2540         long error = -EBADF;
2541
2542         switch (behavior) {
2543         case MADV_NORMAL:
2544         case MADV_SEQUENTIAL:
2545         case MADV_RANDOM:
2546                 error = madvise_behavior(vma, start, end, behavior);
2547                 break;
2548
2549         case MADV_WILLNEED:
2550                 error = madvise_willneed(vma, start, end);
2551                 break;
2552
2553         case MADV_DONTNEED:
2554                 error = madvise_dontneed(vma, start, end);
2555                 break;
2556
2557         default:
2558                 error = -EINVAL;
2559                 break;
2560         }
2561                 
2562         return error;
2563 }
2564
2565 /*
2566  * The madvise(2) system call.
2567  *
2568  * Applications can use madvise() to advise the kernel how it should
2569  * handle paging I/O in this VM area.  The idea is to help the kernel
2570  * use appropriate read-ahead and caching techniques.  The information
2571  * provided is advisory only, and can be safely disregarded by the
2572  * kernel without affecting the correct operation of the application.
2573  *
2574  * behavior values:
2575  *  MADV_NORMAL - the default behavior is to read clusters.  This
2576  *              results in some read-ahead and read-behind.
2577  *  MADV_RANDOM - the system should read the minimum amount of data
2578  *              on any access, since it is unlikely that the appli-
2579  *              cation will need more than what it asks for.
2580  *  MADV_SEQUENTIAL - pages in the given range will probably be accessed
2581  *              once, so they can be aggressively read ahead, and
2582  *              can be freed soon after they are accessed.
2583  *  MADV_WILLNEED - the application is notifying the system to read
2584  *              some pages ahead.
2585  *  MADV_DONTNEED - the application is finished with the given range,
2586  *              so the kernel can free resources associated with it.
2587  *
2588  * return values:
2589  *  zero    - success
2590  *  -EINVAL - start + len < 0, start is not page-aligned,
2591  *              "behavior" is not a valid value, or application
2592  *              is attempting to release locked or shared pages.
2593  *  -ENOMEM - addresses in the specified range are not currently
2594  *              mapped, or are outside the AS of the process.
2595  *  -EIO    - an I/O error occurred while paging in data.
2596  *  -EBADF  - map exists, but area maps something that isn't a file.
2597  *  -EAGAIN - a kernel resource was temporarily unavailable.
2598  */
2599 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior)
2600 {
2601         unsigned long end;
2602         struct vm_area_struct * vma;
2603         int unmapped_error = 0;
2604         int error = -EINVAL;
2605
2606         down_write(&current->mm->mmap_sem);
2607
2608         if (start & ~PAGE_MASK)
2609                 goto out;
2610         len = (len + ~PAGE_MASK) & PAGE_MASK;
2611         end = start + len;
2612         if (end < start)
2613                 goto out;
2614
2615         error = 0;
2616         if (end == start)
2617                 goto out;
2618
2619         /*
2620          * If the interval [start,end) covers some unmapped address
2621          * ranges, just ignore them, but return -ENOMEM at the end.
2622          */
2623         vma = find_vma(current->mm, start);
2624         for (;;) {
2625                 /* Still start < end. */
2626                 error = -ENOMEM;
2627                 if (!vma)
2628                         goto out;
2629
2630                 /* Here start < vma->vm_end. */
2631                 if (start < vma->vm_start) {
2632                         unmapped_error = -ENOMEM;
2633                         start = vma->vm_start;
2634                 }
2635
2636                 /* Here vma->vm_start <= start < vma->vm_end. */
2637                 if (end <= vma->vm_end) {
2638                         if (start < end) {
2639                                 error = madvise_vma(vma, start, end,
2640                                                         behavior);
2641                                 if (error)
2642                                         goto out;
2643                         }
2644                         error = unmapped_error;
2645                         goto out;
2646                 }
2647
2648                 /* Here vma->vm_start <= start < vma->vm_end < end. */
2649                 error = madvise_vma(vma, start, vma->vm_end, behavior);
2650                 if (error)
2651                         goto out;
2652                 start = vma->vm_end;
2653                 vma = vma->vm_next;
2654         }
2655
2656 out:
2657         up_write(&current->mm->mmap_sem);
2658         return error;
2659 }
2660
2661 /*
2662  * Later we can get more picky about what "in core" means precisely.
2663  * For now, simply check to see if the page is in the page cache,
2664  * and is up to date; i.e. that no page-in operation would be required
2665  * at this time if an application were to map and access this page.
2666  */
2667 static unsigned char mincore_page(struct vm_area_struct * vma,
2668         unsigned long pgoff)
2669 {
2670         unsigned char present = 0;
2671         struct address_space * as = vma->vm_file->f_dentry->d_inode->i_mapping;
2672         struct page * page, ** hash = page_hash(as, pgoff);
2673
2674         spin_lock(&pagecache_lock);
2675         page = __find_page_nolock(as, pgoff, *hash);
2676         if ((page) && (Page_Uptodate(page)))
2677                 present = 1;
2678         spin_unlock(&pagecache_lock);
2679
2680         return present;
2681 }
2682
2683 static long mincore_vma(struct vm_area_struct * vma,
2684         unsigned long start, unsigned long end, unsigned char * vec)
2685 {
2686         long error, i, remaining;
2687         unsigned char * tmp;
2688
2689         error = -ENOMEM;
2690         if (!vma->vm_file)
2691                 return error;
2692
2693         start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2694         if (end > vma->vm_end)
2695                 end = vma->vm_end;
2696         end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2697
2698         error = -EAGAIN;
2699         tmp = (unsigned char *) __get_free_page(GFP_KERNEL);
2700         if (!tmp)
2701                 return error;
2702
2703         /* (end - start) is # of pages, and also # of bytes in "vec */
2704         remaining = (end - start),
2705
2706         error = 0;
2707         for (i = 0; remaining > 0; remaining -= PAGE_SIZE, i++) {
2708                 int j = 0;
2709                 long thispiece = (remaining < PAGE_SIZE) ?
2710                                                 remaining : PAGE_SIZE;
2711
2712                 while (j < thispiece)
2713                         tmp[j++] = mincore_page(vma, start++);
2714
2715                 if (copy_to_user(vec + PAGE_SIZE * i, tmp, thispiece)) {
2716                         error = -EFAULT;
2717                         break;
2718                 }
2719         }
2720
2721         free_page((unsigned long) tmp);
2722         return error;
2723 }
2724
2725 /*
2726  * The mincore(2) system call.
2727  *
2728  * mincore() returns the memory residency status of the pages in the
2729  * current process's address space specified by [addr, addr + len).
2730  * The status is returned in a vector of bytes.  The least significant
2731  * bit of each byte is 1 if the referenced page is in memory, otherwise
2732  * it is zero.
2733  *
2734  * Because the status of a page can change after mincore() checks it
2735  * but before it returns to the application, the returned vector may
2736  * contain stale information.  Only locked pages are guaranteed to
2737  * remain in memory.
2738  *
2739  * return values:
2740  *  zero    - success
2741  *  -EFAULT - vec points to an illegal address
2742  *  -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE,
2743  *              or len has a nonpositive value
2744  *  -ENOMEM - Addresses in the range [addr, addr + len] are
2745  *              invalid for the address space of this process, or
2746  *              specify one or more pages which are not currently
2747  *              mapped
2748  *  -EAGAIN - A kernel resource was temporarily unavailable.
2749  */
2750 asmlinkage long sys_mincore(unsigned long start, size_t len,
2751         unsigned char * vec)
2752 {
2753         int index = 0;
2754         unsigned long end;
2755         struct vm_area_struct * vma;
2756         int unmapped_error = 0;
2757         long error = -EINVAL;
2758
2759         down_read(&current->mm->mmap_sem);
2760
2761         if (start & ~PAGE_CACHE_MASK)
2762                 goto out;
2763         len = (len + ~PAGE_CACHE_MASK) & PAGE_CACHE_MASK;
2764         end = start + len;
2765         if (end < start)
2766                 goto out;
2767
2768         error = 0;
2769         if (end == start)
2770                 goto out;
2771
2772         /*
2773          * If the interval [start,end) covers some unmapped address
2774          * ranges, just ignore them, but return -ENOMEM at the end.
2775          */
2776         vma = find_vma(current->mm, start);
2777         for (;;) {
2778                 /* Still start < end. */
2779                 error = -ENOMEM;
2780                 if (!vma)
2781                         goto out;
2782
2783                 /* Here start < vma->vm_end. */
2784                 if (start < vma->vm_start) {
2785                         unmapped_error = -ENOMEM;
2786                         start = vma->vm_start;
2787                 }
2788
2789                 /* Here vma->vm_start <= start < vma->vm_end. */
2790                 if (end <= vma->vm_end) {
2791                         if (start < end) {
2792                                 error = mincore_vma(vma, start, end,
2793                                                         &vec[index]);
2794                                 if (error)
2795                                         goto out;
2796                         }
2797                         error = unmapped_error;
2798                         goto out;
2799                 }
2800
2801                 /* Here vma->vm_start <= start < vma->vm_end < end. */
2802                 error = mincore_vma(vma, start, vma->vm_end, &vec[index]);
2803                 if (error)
2804                         goto out;
2805                 index += (vma->vm_end - start) >> PAGE_CACHE_SHIFT;
2806                 start = vma->vm_end;
2807                 vma = vma->vm_next;
2808         }
2809
2810 out:
2811         up_read(&current->mm->mmap_sem);
2812         return error;
2813 }
2814
2815 static inline
2816 struct page *__read_cache_page(struct address_space *mapping,
2817                                 unsigned long index,
2818                                 int (*filler)(void *,struct page*),
2819                                 void *data)
2820 {
2821         struct page **hash = page_hash(mapping, index);
2822         struct page *page, *cached_page = NULL;
2823         int err;
2824 repeat:
2825         page = __find_get_page(mapping, index, hash);
2826         if (!page) {
2827                 if (!cached_page) {
2828                         cached_page = page_cache_alloc(mapping);
2829                         if (!cached_page)
2830                                 return ERR_PTR(-ENOMEM);
2831                 }
2832                 page = cached_page;
2833                 if (add_to_page_cache_unique(page, mapping, index, hash))
2834                         goto repeat;
2835                 cached_page = NULL;
2836                 err = filler(data, page);
2837                 if (err < 0) {
2838                         page_cache_release(page);
2839                         page = ERR_PTR(err);
2840                 }
2841         }
2842         if (cached_page)
2843                 page_cache_release(cached_page);
2844         return page;
2845 }
2846
2847 /*
2848  * Read into the page cache. If a page already exists,
2849  * and Page_Uptodate() is not set, try to fill the page.
2850  */
2851 struct page *read_cache_page(struct address_space *mapping,
2852                                 unsigned long index,
2853                                 int (*filler)(void *,struct page*),
2854                                 void *data)
2855 {
2856         struct page *page;
2857         int err;
2858
2859 retry:
2860         page = __read_cache_page(mapping, index, filler, data);
2861         if (IS_ERR(page))
2862                 goto out;
2863         mark_page_accessed(page);
2864         if (Page_Uptodate(page))
2865                 goto out;
2866
2867         lock_page(page);
2868         if (!page->mapping) {
2869                 UnlockPage(page);
2870                 page_cache_release(page);
2871                 goto retry;
2872         }
2873         if (Page_Uptodate(page)) {
2874                 UnlockPage(page);
2875                 goto out;
2876         }
2877         err = filler(data, page);
2878         if (err < 0) {
2879                 page_cache_release(page);
2880                 page = ERR_PTR(err);
2881         }
2882  out:
2883         return page;
2884 }
2885
2886 static inline struct page * __grab_cache_page(struct address_space *mapping,
2887                                 unsigned long index, struct page **cached_page)
2888 {
2889         struct page *page, **hash = page_hash(mapping, index);
2890 repeat:
2891         page = __find_lock_page(mapping, index, hash);
2892         if (!page) {
2893                 if (!*cached_page) {
2894                         *cached_page = page_cache_alloc(mapping);
2895                         if (!*cached_page)
2896                                 return NULL;
2897                 }
2898                 page = *cached_page;
2899                 if (add_to_page_cache_unique(page, mapping, index, hash))
2900                         goto repeat;
2901                 *cached_page = NULL;
2902         }
2903         return page;
2904 }
2905
2906 inline void remove_suid(struct inode *inode)
2907 {
2908         unsigned int mode;
2909
2910         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
2911         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
2912
2913         /* was any of the uid bits set? */
2914         mode &= inode->i_mode;
2915         if (mode && !capable(CAP_FSETID)) {
2916                 inode->i_mode &= ~mode;
2917                 mark_inode_dirty(inode);
2918         }
2919 }
2920
2921 /*
2922  * Write to a file through the page cache. 
2923  *
2924  * We currently put everything into the page cache prior to writing it.
2925  * This is not a problem when writing full pages. With partial pages,
2926  * however, we first have to read the data into the cache, then
2927  * dirty the page, and finally schedule it for writing. Alternatively, we
2928  * could write-through just the portion of data that would go into that
2929  * page, but that would kill performance for applications that write data
2930  * line by line, and it's prone to race conditions.
2931  *
2932  * Note that this routine doesn't try to keep track of dirty pages. Each
2933  * file system has to do this all by itself, unfortunately.
2934  *                                                      okir@monad.swb.de
2935  */
2936 ssize_t
2937 generic_file_write(struct file *file,const char *buf,size_t count, loff_t *ppos)
2938 {
2939         struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
2940         struct inode    *inode = mapping->host;
2941         unsigned long   limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
2942         loff_t          pos;
2943         struct page     *page, *cached_page;
2944         ssize_t         written;
2945         long            status = 0;
2946         int             err;
2947         unsigned        bytes;
2948
2949         if ((ssize_t) count < 0)
2950                 return -EINVAL;
2951
2952         if (!access_ok(VERIFY_READ, buf, count))
2953                 return -EFAULT;
2954
2955         cached_page = NULL;
2956
2957         down(&inode->i_sem);
2958
2959         pos = *ppos;
2960         err = -EINVAL;
2961         if (pos < 0)
2962                 goto out;
2963
2964         err = file->f_error;
2965         if (err) {
2966                 file->f_error = 0;
2967                 goto out;
2968         }
2969
2970         written = 0;
2971
2972         /* FIXME: this is for backwards compatibility with 2.4 */
2973         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND)
2974                 pos = inode->i_size;
2975
2976         /*
2977          * Check whether we've reached the file size limit.
2978          */
2979         err = -EFBIG;
2980         
2981         if (!S_ISBLK(inode->i_mode) && limit != RLIM_INFINITY) {
2982                 if (pos >= limit) {
2983                         send_sig(SIGXFSZ, current, 0);
2984                         goto out;
2985                 }
2986                 if (pos > 0xFFFFFFFFULL || count > limit - (u32)pos) {
2987                         /* send_sig(SIGXFSZ, current, 0); */
2988                         count = limit - (u32)pos;
2989                 }
2990         }
2991
2992         /*
2993          *      LFS rule 
2994          */
2995         if ( pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
2996                 if (pos >= MAX_NON_LFS) {
2997                         send_sig(SIGXFSZ, current, 0);
2998                         goto out;
2999                 }
3000                 if (count > MAX_NON_LFS - (u32)pos) {
3001                         /* send_sig(SIGXFSZ, current, 0); */
3002                         count = MAX_NON_LFS - (u32)pos;
3003                 }
3004         }
3005
3006         /*
3007          *      Are we about to exceed the fs block limit ?
3008          *
3009          *      If we have written data it becomes a short write
3010          *      If we have exceeded without writing data we send
3011          *      a signal and give them an EFBIG.
3012          *
3013          *      Linus frestrict idea will clean these up nicely..
3014          */
3015          
3016         if (!S_ISBLK(inode->i_mode)) {
3017                 if (pos >= inode->i_sb->s_maxbytes)
3018                 {
3019                         if (count || pos > inode->i_sb->s_maxbytes) {
3020                                 send_sig(SIGXFSZ, current, 0);
3021                                 err = -EFBIG;
3022                                 goto out;
3023                         }
3024                         /* zero-length writes at ->s_maxbytes are OK */
3025                 }
3026
3027                 if (pos + count > inode->i_sb->s_maxbytes)
3028                         count = inode->i_sb->s_maxbytes - pos;
3029         } else {
3030                 if (is_read_only(inode->i_rdev)) {
3031                         err = -EPERM;
3032                         goto out;
3033                 }
3034                 if (pos >= inode->i_size) {
3035                         if (count || pos > inode->i_size) {
3036                                 err = -ENOSPC;
3037                                 goto out;
3038                         }
3039                 }
3040
3041                 if (pos + count > inode->i_size)
3042                         count = inode->i_size - pos;
3043         }
3044
3045         err = 0;
3046         if (count == 0)
3047                 goto out;
3048
3049         remove_suid(inode);
3050         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
3051         mark_inode_dirty_sync(inode);
3052
3053         if (file->f_flags & O_DIRECT)
3054                 goto o_direct;
3055
3056         do {
3057                 unsigned long index, offset;
3058                 long page_fault;
3059                 char *kaddr;
3060
3061                 /*
3062                  * Try to find the page in the cache. If it isn't there,
3063                  * allocate a free page.
3064                  */
3065                 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
3066                 index = pos >> PAGE_CACHE_SHIFT;
3067                 bytes = PAGE_CACHE_SIZE - offset;
3068                 if (bytes > count)
3069                         bytes = count;
3070
3071                 /*
3072                  * Bring in the user page that we will copy from _first_.
3073                  * Otherwise there's a nasty deadlock on copying from the
3074                  * same page as we're writing to, without it being marked
3075                  * up-to-date.
3076                  */
3077                 { volatile unsigned char dummy;
3078                         __get_user(dummy, buf);
3079                         __get_user(dummy, buf+bytes-1);
3080                 }
3081
3082                 status = -ENOMEM;       /* we'll assign it later anyway */
3083                 page = __grab_cache_page(mapping, index, &cached_page);
3084                 if (!page)
3085                         break;
3086
3087                 /* We have exclusive IO access to the page.. */
3088                 if (!PageLocked(page)) {
3089                         PAGE_BUG(page);
3090                 }
3091
3092                 kaddr = kmap(page);
3093                 status = mapping->a_ops->prepare_write(file, page, offset, offset+bytes);
3094                 if (status)
3095                         goto sync_failure;
3096                 page_fault = __copy_from_user(kaddr+offset, buf, bytes);
3097                 flush_dcache_page(page);
3098                 status = mapping->a_ops->commit_write(file, page, offset, offset+bytes);
3099                 if (page_fault)
3100                         goto fail_write;
3101                 if (!status)
3102                         status = bytes;
3103
3104                 if (status >= 0) {
3105                         written += status;
3106                         count -= status;
3107                         pos += status;
3108                         buf += status;
3109                 }
3110 unlock:
3111                 kunmap(page);
3112                 /* Mark it unlocked again and drop the page.. */
3113                 SetPageReferenced(page);
3114                 UnlockPage(page);
3115                 page_cache_release(page);
3116
3117                 if (status < 0)
3118                         break;
3119         } while (count);
3120 done:
3121         *ppos = pos;
3122
3123         if (cached_page)
3124                 page_cache_release(cached_page);
3125
3126         /* For now, when the user asks for O_SYNC, we'll actually
3127          * provide O_DSYNC. */
3128         if (status >= 0) {
3129                 if ((file->f_flags & O_SYNC) || IS_SYNC(inode))
3130                         status = generic_osync_inode(inode, OSYNC_METADATA|OSYNC_DATA);
3131         }
3132         
3133 out_status:     
3134         err = written ? written : status;
3135 out:
3136
3137         up(&inode->i_sem);
3138         return err;
3139 fail_write:
3140         status = -EFAULT;
3141         goto unlock;
3142
3143 sync_failure:
3144         /*
3145          * If blocksize < pagesize, prepare_write() may have instantiated a
3146          * few blocks outside i_size.  Trim these off again.
3147          */
3148         kunmap(page);
3149         UnlockPage(page);
3150         page_cache_release(page);
3151         if (pos + bytes > inode->i_size)
3152                 vmtruncate(inode, inode->i_size);
3153         goto done;
3154
3155 o_direct:
3156         written = generic_file_direct_IO(WRITE, file, (char *) buf, count, pos);
3157         if (written > 0) {
3158                 loff_t end = pos + written;
3159                 if (end > inode->i_size && !S_ISBLK(inode->i_mode)) {
3160                         inode->i_size = end;
3161                         mark_inode_dirty(inode);
3162                 }
3163                 *ppos = end;
3164                 invalidate_inode_pages2(mapping);
3165         }
3166         /*
3167          * Sync the fs metadata but not the minor inode changes and
3168          * of course not the data as we did direct DMA for the IO.
3169          */
3170         if (written >= 0 && file->f_flags & O_SYNC)
3171                 status = generic_osync_inode(inode, OSYNC_METADATA);
3172         goto out_status;
3173 }
3174
3175 void __init page_cache_init(unsigned long mempages)
3176 {
3177         unsigned long htable_size, order;
3178
3179         htable_size = mempages;
3180         htable_size *= sizeof(struct page *);
3181         for(order = 0; (PAGE_SIZE << order) < htable_size; order++)
3182                 ;
3183
3184         do {
3185                 unsigned long tmp = (PAGE_SIZE << order) / sizeof(struct page *);
3186
3187                 page_hash_bits = 0;
3188                 while((tmp >>= 1UL) != 0UL)
3189                         page_hash_bits++;
3190
3191                 page_hash_table = (struct page **)
3192                         __get_free_pages(GFP_ATOMIC, order);
3193         } while(page_hash_table == NULL && --order > 0);
3194
3195         printk("Page-cache hash table entries: %d (order: %ld, %ld bytes)\n",
3196                (1 << page_hash_bits), order, (PAGE_SIZE << order));
3197         if (!page_hash_table)
3198                 panic("Failed to allocate page hash table\n");
3199         memset((void *)page_hash_table, 0, PAGE_HASH_SIZE * sizeof(struct page *));
3200 }