3 * (C) 2005 FUJITA Tomonori <tomof@acm.org>
4 * This code is licenced under the GPL.
11 static int tio_add_pages(struct tio *tio, int count)
16 dprintk(D_GENERIC, "%p %d (%d)\n", tio, count, tio->pg_cnt);
20 count *= sizeof(struct page *);
23 if (!(tio->pvec = kmalloc(count, GFP_KERNEL)))
27 memset(tio->pvec, 0, count);
29 for (i = 0; i < tio->pg_cnt; i++) {
31 if (!(page = alloc_page(GFP_KERNEL)))
39 static kmem_cache_t *tio_cache;
41 struct tio *tio_alloc(int count)
45 tio = kmem_cache_alloc(tio_cache, GFP_KERNEL | __GFP_NOFAIL);
53 atomic_set(&tio->count, 1);
56 tio_add_pages(tio, count);
61 static void tio_free(struct tio *tio)
64 for (i = 0; i < tio->pg_cnt; i++) {
66 __free_page(tio->pvec[i]);
69 kmem_cache_free(tio_cache, tio);
72 void tio_put(struct tio *tio)
74 assert(atomic_read(&tio->count));
75 if (atomic_dec_and_test(&tio->count))
79 void tio_get(struct tio *tio)
81 atomic_inc(&tio->count);
84 void tio_set(struct tio *tio, u32 size, loff_t offset)
86 tio->idx = offset >> PAGE_CACHE_SHIFT;
87 tio->offset = offset & ~PAGE_CACHE_MASK;
91 int tio_read(struct iet_volume *lu, struct tio *tio)
93 struct iotype *iot = lu->iotype;
95 return iot->make_request ? iot->make_request(lu, tio, READ) : 0;
98 int tio_write(struct iet_volume *lu, struct tio *tio)
100 struct iotype *iot = lu->iotype;
102 return iot->make_request ? iot->make_request(lu, tio, WRITE) : 0;
105 int tio_sync(struct iet_volume *lu, struct tio *tio)
107 struct iotype *iot = lu->iotype;
109 return iot->sync ? iot->sync(lu, tio) : 0;
114 tio_cache = kmem_cache_create("tio",
115 sizeof(struct tio), 0, 0, NULL, NULL);
116 return tio_cache ? 0 : -ENOMEM;
122 kmem_cache_destroy(tio_cache);