This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / jfs / jfs_metapage.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2003
3  *   Portions Copyright (C) Christoph Hellwig, 2001-2002
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or 
8  *   (at your option) any later version.
9  * 
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software 
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 #include <linux/fs.h>
21 #include <linux/init.h>
22 #include <linux/buffer_head.h>
23 #include <linux/mempool.h>
24 #include "jfs_incore.h"
25 #include "jfs_superblock.h"
26 #include "jfs_filsys.h"
27 #include "jfs_metapage.h"
28 #include "jfs_txnmgr.h"
29 #include "jfs_debug.h"
30
31 static spinlock_t meta_lock = SPIN_LOCK_UNLOCKED;
32
33 #ifdef CONFIG_JFS_STATISTICS
34 struct {
35         uint    pagealloc;      /* # of page allocations */
36         uint    pagefree;       /* # of page frees */
37         uint    lockwait;       /* # of sleeping lock_metapage() calls */
38 } mpStat;
39 #endif
40
41
42 #define HASH_BITS 10            /* This makes hash_table 1 4K page */
43 #define HASH_SIZE (1 << HASH_BITS)
44 static struct metapage **hash_table = NULL;
45 static unsigned long hash_order;
46
47
48 static inline int metapage_locked(struct metapage *mp)
49 {
50         return test_bit(META_locked, &mp->flag);
51 }
52
53 static inline int trylock_metapage(struct metapage *mp)
54 {
55         return test_and_set_bit(META_locked, &mp->flag);
56 }
57
58 static inline void unlock_metapage(struct metapage *mp)
59 {
60         clear_bit(META_locked, &mp->flag);
61         wake_up(&mp->wait);
62 }
63
64 static void __lock_metapage(struct metapage *mp)
65 {
66         DECLARE_WAITQUEUE(wait, current);
67
68         INCREMENT(mpStat.lockwait);
69
70         add_wait_queue_exclusive(&mp->wait, &wait);
71         do {
72                 set_current_state(TASK_UNINTERRUPTIBLE);
73                 if (metapage_locked(mp)) {
74                         spin_unlock(&meta_lock);
75                         schedule();
76                         spin_lock(&meta_lock);
77                 }
78         } while (trylock_metapage(mp));
79         __set_current_state(TASK_RUNNING);
80         remove_wait_queue(&mp->wait, &wait);
81 }
82
83 /* needs meta_lock */
84 static inline void lock_metapage(struct metapage *mp)
85 {
86         if (trylock_metapage(mp))
87                 __lock_metapage(mp);
88 }
89
90 #define METAPOOL_MIN_PAGES 32
91 static kmem_cache_t *metapage_cache;
92 static mempool_t *metapage_mempool;
93
94 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
95 {
96         struct metapage *mp = (struct metapage *)foo;
97
98         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
99             SLAB_CTOR_CONSTRUCTOR) {
100                 mp->lid = 0;
101                 mp->lsn = 0;
102                 mp->flag = 0;
103                 mp->data = NULL;
104                 mp->clsn = 0;
105                 mp->log = NULL;
106                 set_bit(META_free, &mp->flag);
107                 init_waitqueue_head(&mp->wait);
108         }
109 }
110
111 static inline struct metapage *alloc_metapage(int no_wait)
112 {
113         return mempool_alloc(metapage_mempool, no_wait ? GFP_ATOMIC : GFP_NOFS);
114 }
115
116 static inline void free_metapage(struct metapage *mp)
117 {
118         mp->flag = 0;
119         set_bit(META_free, &mp->flag);
120
121         mempool_free(mp, metapage_mempool);
122 }
123
124 int __init metapage_init(void)
125 {
126         /*
127          * Allocate the metapage structures
128          */
129         metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage),
130                                            0, 0, init_once, NULL);
131         if (metapage_cache == NULL)
132                 return -ENOMEM;
133
134         metapage_mempool = mempool_create(METAPOOL_MIN_PAGES, mempool_alloc_slab,
135                                           mempool_free_slab, metapage_cache);
136
137         if (metapage_mempool == NULL) {
138                 kmem_cache_destroy(metapage_cache);
139                 return -ENOMEM;
140         }
141         /*
142          * Now the hash list
143          */
144         for (hash_order = 0;
145              ((PAGE_SIZE << hash_order) / sizeof(void *)) < HASH_SIZE;
146              hash_order++);
147         hash_table =
148             (struct metapage **) __get_free_pages(GFP_KERNEL, hash_order);
149         assert(hash_table);
150         memset(hash_table, 0, PAGE_SIZE << hash_order);
151
152         return 0;
153 }
154
155 void metapage_exit(void)
156 {
157         mempool_destroy(metapage_mempool);
158         kmem_cache_destroy(metapage_cache);
159 }
160
161 /*
162  * Basically same hash as in pagemap.h, but using our hash table
163  */
164 static struct metapage **meta_hash(struct address_space *mapping,
165                                    unsigned long index)
166 {
167 #define i (((unsigned long)mapping)/ \
168            (sizeof(struct inode) & ~(sizeof(struct inode) -1 )))
169 #define s(x) ((x) + ((x) >> HASH_BITS))
170         return hash_table + (s(i + index) & (HASH_SIZE - 1));
171 #undef i
172 #undef s
173 }
174
175 static struct metapage *search_hash(struct metapage ** hash_ptr,
176                                     struct address_space *mapping,
177                                unsigned long index)
178 {
179         struct metapage *ptr;
180
181         for (ptr = *hash_ptr; ptr; ptr = ptr->hash_next) {
182                 if ((ptr->mapping == mapping) && (ptr->index == index))
183                         return ptr;
184         }
185
186         return NULL;
187 }
188
189 static void add_to_hash(struct metapage * mp, struct metapage ** hash_ptr)
190 {
191         if (*hash_ptr)
192                 (*hash_ptr)->hash_prev = mp;
193
194         mp->hash_prev = NULL;
195         mp->hash_next = *hash_ptr;
196         *hash_ptr = mp;
197 }
198
199 static void remove_from_hash(struct metapage * mp, struct metapage ** hash_ptr)
200 {
201         if (mp->hash_prev)
202                 mp->hash_prev->hash_next = mp->hash_next;
203         else {
204                 assert(*hash_ptr == mp);
205                 *hash_ptr = mp->hash_next;
206         }
207
208         if (mp->hash_next)
209                 mp->hash_next->hash_prev = mp->hash_prev;
210 }
211
212 struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
213                                 unsigned int size, int absolute,
214                                 unsigned long new)
215 {
216         struct metapage **hash_ptr;
217         int l2BlocksPerPage;
218         int l2bsize;
219         struct address_space *mapping;
220         struct metapage *mp;
221         unsigned long page_index;
222         unsigned long page_offset;
223
224         jfs_info("__get_metapage: inode = 0x%p, lblock = 0x%lx", inode, lblock);
225
226         if (absolute)
227                 mapping = inode->i_sb->s_bdev->bd_inode->i_mapping;
228         else
229                 mapping = inode->i_mapping;
230
231         hash_ptr = meta_hash(mapping, lblock);
232 again:
233         spin_lock(&meta_lock);
234         mp = search_hash(hash_ptr, mapping, lblock);
235         if (mp) {
236               page_found:
237                 mp->count++;
238                 lock_metapage(mp);
239                 spin_unlock(&meta_lock);
240                 if (test_bit(META_stale, &mp->flag)) {
241                         release_metapage(mp);
242                         goto again;
243                 }
244                 if (test_bit(META_discard, &mp->flag)) {
245                         if (!new) {
246                                 jfs_error(inode->i_sb,
247                                           "__get_metapage: using a "
248                                           "discarded metapage");
249                                 release_metapage(mp);
250                                 return NULL;
251                         }
252                         clear_bit(META_discard, &mp->flag);
253                 }
254                 jfs_info("__get_metapage: found 0x%p, in hash", mp);
255                 if (mp->logical_size != size) {
256                         jfs_error(inode->i_sb,
257                                   "__get_metapage: mp->logical_size != size");
258                         release_metapage(mp);
259                         return NULL;
260                 }
261         } else {
262                 l2bsize = inode->i_blkbits;
263                 l2BlocksPerPage = PAGE_CACHE_SHIFT - l2bsize;
264                 page_index = lblock >> l2BlocksPerPage;
265                 page_offset = (lblock - (page_index << l2BlocksPerPage)) <<
266                     l2bsize;
267                 if ((page_offset + size) > PAGE_CACHE_SIZE) {
268                         spin_unlock(&meta_lock);
269                         jfs_err("MetaData crosses page boundary!!");
270                         return NULL;
271                 }
272                 
273                 /*
274                  * Locks held on aggregate inode pages are usually
275                  * not held long, and they are taken in critical code
276                  * paths (committing dirty inodes, txCommit thread) 
277                  * 
278                  * Attempt to get metapage without blocking, tapping into
279                  * reserves if necessary.
280                  */
281                 mp = NULL;
282                 if (JFS_IP(inode)->fileset == AGGREGATE_I) {
283                         mp =  mempool_alloc(metapage_mempool, GFP_ATOMIC);
284                         if (!mp) {
285                                 /*
286                                  * mempool is supposed to protect us from
287                                  * failing here.  We will try a blocking
288                                  * call, but a deadlock is possible here
289                                  */
290                                 printk(KERN_WARNING
291                                        "__get_metapage: atomic call to mempool_alloc failed.\n");
292                                 printk(KERN_WARNING
293                                        "Will attempt blocking call\n");
294                         }
295                 }
296                 if (!mp) {
297                         struct metapage *mp2;
298
299                         spin_unlock(&meta_lock);
300                         mp =  mempool_alloc(metapage_mempool, GFP_NOFS);
301                         spin_lock(&meta_lock);
302
303                         /* we dropped the meta_lock, we need to search the
304                          * hash again.
305                          */
306                         mp2 = search_hash(hash_ptr, mapping, lblock);
307                         if (mp2) {
308                                 free_metapage(mp);
309                                 mp = mp2;
310                                 goto page_found;
311                         }
312                 }
313                 mp->flag = 0;
314                 lock_metapage(mp);
315                 if (absolute)
316                         set_bit(META_absolute, &mp->flag);
317                 mp->xflag = COMMIT_PAGE;
318                 mp->count = 1;
319                 atomic_set(&mp->nohomeok,0);
320                 mp->mapping = mapping;
321                 mp->index = lblock;
322                 mp->page = 0;
323                 mp->logical_size = size;
324                 add_to_hash(mp, hash_ptr);
325                 spin_unlock(&meta_lock);
326
327                 if (new) {
328                         jfs_info("__get_metapage: Calling grab_cache_page");
329                         mp->page = grab_cache_page(mapping, page_index);
330                         if (!mp->page) {
331                                 jfs_err("grab_cache_page failed!");
332                                 goto freeit;
333                         } else {
334                                 INCREMENT(mpStat.pagealloc);
335                                 unlock_page(mp->page);
336                         }
337                 } else {
338                         jfs_info("__get_metapage: Calling read_cache_page");
339                         mp->page = read_cache_page(mapping, lblock,
340                                     (filler_t *)mapping->a_ops->readpage, NULL);
341                         if (IS_ERR(mp->page)) {
342                                 jfs_err("read_cache_page failed!");
343                                 goto freeit;
344                         } else
345                                 INCREMENT(mpStat.pagealloc);
346                 }
347                 mp->data = kmap(mp->page) + page_offset;
348         }
349
350         if (new)
351                 memset(mp->data, 0, PSIZE);
352
353         jfs_info("__get_metapage: returning = 0x%p", mp);
354         return mp;
355
356 freeit:
357         spin_lock(&meta_lock);
358         remove_from_hash(mp, hash_ptr);
359         free_metapage(mp);
360         spin_unlock(&meta_lock);
361         return NULL;
362 }
363
364 void hold_metapage(struct metapage * mp, int force)
365 {
366         spin_lock(&meta_lock);
367
368         mp->count++;
369
370         if (force) {
371                 ASSERT (!(test_bit(META_forced, &mp->flag)));
372                 if (trylock_metapage(mp))
373                         set_bit(META_forced, &mp->flag);
374         } else
375                 lock_metapage(mp);
376
377         spin_unlock(&meta_lock);
378 }
379
380 static void __write_metapage(struct metapage * mp)
381 {
382         int l2bsize = mp->mapping->host->i_blkbits;
383         int l2BlocksPerPage = PAGE_CACHE_SHIFT - l2bsize;
384         unsigned long page_index;
385         unsigned long page_offset;
386         int rc;
387
388         jfs_info("__write_metapage: mp = 0x%p", mp);
389
390         if (test_bit(META_discard, &mp->flag)) {
391                 /*
392                  * This metadata is no longer valid
393                  */
394                 clear_bit(META_dirty, &mp->flag);
395                 return;
396         }
397
398         page_index = mp->page->index;
399         page_offset =
400             (mp->index - (page_index << l2BlocksPerPage)) << l2bsize;
401
402         lock_page(mp->page);
403         rc = mp->mapping->a_ops->prepare_write(NULL, mp->page, page_offset,
404                                                page_offset +
405                                                mp->logical_size);
406         if (rc) {
407                 jfs_err("prepare_write return %d!", rc);
408                 ClearPageUptodate(mp->page);
409                 unlock_page(mp->page);
410                 clear_bit(META_dirty, &mp->flag);
411                 return;
412         }
413         rc = mp->mapping->a_ops->commit_write(NULL, mp->page, page_offset,
414                                               page_offset +
415                                               mp->logical_size);
416         if (rc) {
417                 jfs_err("commit_write returned %d", rc);
418         }
419
420         unlock_page(mp->page);
421         clear_bit(META_dirty, &mp->flag);
422
423         jfs_info("__write_metapage done");
424 }
425
426 static inline void sync_metapage(struct metapage *mp)
427 {
428         struct page *page = mp->page;
429
430         page_cache_get(page);
431         lock_page(page);
432
433         /* we're done with this page - no need to check for errors */
434         if (page_has_buffers(page))
435                 write_one_page(page, 1);
436         else
437                 unlock_page(page);
438         page_cache_release(page);
439 }
440
441 void release_metapage(struct metapage * mp)
442 {
443         struct jfs_log *log;
444
445         jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);
446
447         spin_lock(&meta_lock);
448         if (test_bit(META_forced, &mp->flag)) {
449                 clear_bit(META_forced, &mp->flag);
450                 mp->count--;
451                 spin_unlock(&meta_lock);
452                 return;
453         }
454
455         assert(mp->count);
456         if (--mp->count || atomic_read(&mp->nohomeok)) {
457                 unlock_metapage(mp);
458                 spin_unlock(&meta_lock);
459                 return;
460         }
461
462         if (mp->page) {
463                 /* Releasing spinlock, we have to check mp->count later */
464                 set_bit(META_stale, &mp->flag);
465                 spin_unlock(&meta_lock);
466                 kunmap(mp->page);
467                 mp->data = 0;
468                 if (test_bit(META_dirty, &mp->flag))
469                         __write_metapage(mp);
470                 if (test_bit(META_sync, &mp->flag)) {
471                         sync_metapage(mp);
472                         clear_bit(META_sync, &mp->flag);
473                 }
474
475                 if (test_bit(META_discard, &mp->flag)) {
476                         lock_page(mp->page);
477                         block_invalidatepage(mp->page, 0);
478                         unlock_page(mp->page);
479                 }
480
481                 page_cache_release(mp->page);
482                 mp->page = NULL;
483                 INCREMENT(mpStat.pagefree);
484                 spin_lock(&meta_lock);
485         }
486
487         if (mp->lsn) {
488                 /*
489                  * Remove metapage from logsynclist.
490                  */
491                 log = mp->log;
492                 LOGSYNC_LOCK(log);
493                 mp->log = 0;
494                 mp->lsn = 0;
495                 mp->clsn = 0;
496                 log->count--;
497                 list_del(&mp->synclist);
498                 LOGSYNC_UNLOCK(log);
499         }
500         if (mp->count) {
501                 /* Someone else is trying to get this metpage */
502                 unlock_metapage(mp);
503                 spin_unlock(&meta_lock);
504                 return;
505         }
506         remove_from_hash(mp, meta_hash(mp->mapping, mp->index));
507         spin_unlock(&meta_lock);
508
509         free_metapage(mp);
510 }
511
512 void __invalidate_metapages(struct inode *ip, s64 addr, int len)
513 {
514         struct metapage **hash_ptr;
515         unsigned long lblock;
516         int l2BlocksPerPage = PAGE_CACHE_SHIFT - ip->i_blkbits;
517         /* All callers are interested in block device's mapping */
518         struct address_space *mapping = ip->i_sb->s_bdev->bd_inode->i_mapping;
519         struct metapage *mp;
520         struct page *page;
521
522         /*
523          * First, mark metapages to discard.  They will eventually be
524          * released, but should not be written.
525          */
526         for (lblock = addr; lblock < addr + len;
527              lblock += 1 << l2BlocksPerPage) {
528                 hash_ptr = meta_hash(mapping, lblock);
529 again:
530                 spin_lock(&meta_lock);
531                 mp = search_hash(hash_ptr, mapping, lblock);
532                 if (mp) {
533                         if (test_bit(META_stale, &mp->flag)) {
534                                 /* Racing with release_metapage */
535                                 mp->count++;
536                                 lock_metapage(mp);
537                                 spin_unlock(&meta_lock);
538                                 /* racing release_metapage should be done now */
539                                 release_metapage(mp);
540                                 goto again;
541                         }
542
543                         set_bit(META_discard, &mp->flag);
544                         spin_unlock(&meta_lock);
545                 } else {
546                         spin_unlock(&meta_lock);
547                         page = find_lock_page(mapping, lblock>>l2BlocksPerPage);
548                         if (page) {
549                                 block_invalidatepage(page, 0);
550                                 unlock_page(page);
551                         }
552                 }
553         }
554 }
555
556 #ifdef CONFIG_JFS_STATISTICS
557 int jfs_mpstat_read(char *buffer, char **start, off_t offset, int length,
558                     int *eof, void *data)
559 {
560         int len = 0;
561         off_t begin;
562
563         len += sprintf(buffer,
564                        "JFS Metapage statistics\n"
565                        "=======================\n"
566                        "page allocations = %d\n"
567                        "page frees = %d\n"
568                        "lock waits = %d\n",
569                        mpStat.pagealloc,
570                        mpStat.pagefree,
571                        mpStat.lockwait);
572
573         begin = offset;
574         *start = buffer + begin;
575         len -= begin;
576
577         if (len > length)
578                 len = length;
579         else
580                 *eof = 1;
581
582         if (len < 0)
583                 len = 0;
584
585         return len;
586 }
587 #endif