[PATCH] ufs: directory and page cache: from blocks to pages
[linux-2.6-block.git] / fs / ufs / util.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/ufs/util.c
3 *
4 * Copyright (C) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
7 */
8
9#include <linux/string.h>
10#include <linux/slab.h>
11#include <linux/ufs_fs.h>
12#include <linux/buffer_head.h>
13
14#include "swab.h"
15#include "util.h"
16
17#undef UFS_UTILS_DEBUG
18
19#ifdef UFS_UTILS_DEBUG
20#define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
21#else
22#define UFSD(x)
23#endif
24
25
26struct ufs_buffer_head * _ubh_bread_ (struct ufs_sb_private_info * uspi,
27 struct super_block *sb, u64 fragment, u64 size)
28{
29 struct ufs_buffer_head * ubh;
30 unsigned i, j ;
31 u64 count = 0;
32 if (size & ~uspi->s_fmask)
33 return NULL;
34 count = size >> uspi->s_fshift;
35 if (count > UFS_MAXFRAG)
36 return NULL;
37 ubh = (struct ufs_buffer_head *)
38 kmalloc (sizeof (struct ufs_buffer_head), GFP_KERNEL);
39 if (!ubh)
40 return NULL;
41 ubh->fragment = fragment;
42 ubh->count = count;
43 for (i = 0; i < count; i++)
44 if (!(ubh->bh[i] = sb_bread(sb, fragment + i)))
45 goto failed;
46 for (; i < UFS_MAXFRAG; i++)
47 ubh->bh[i] = NULL;
48 return ubh;
49failed:
50 for (j = 0; j < i; j++)
51 brelse (ubh->bh[j]);
52 kfree(ubh);
53 return NULL;
54}
55
56struct ufs_buffer_head * ubh_bread_uspi (struct ufs_sb_private_info * uspi,
57 struct super_block *sb, u64 fragment, u64 size)
58{
59 unsigned i, j;
60 u64 count = 0;
61 if (size & ~uspi->s_fmask)
62 return NULL;
63 count = size >> uspi->s_fshift;
64 if (count <= 0 || count > UFS_MAXFRAG)
65 return NULL;
66 USPI_UBH->fragment = fragment;
67 USPI_UBH->count = count;
68 for (i = 0; i < count; i++)
69 if (!(USPI_UBH->bh[i] = sb_bread(sb, fragment + i)))
70 goto failed;
71 for (; i < UFS_MAXFRAG; i++)
72 USPI_UBH->bh[i] = NULL;
73 return USPI_UBH;
74failed:
75 for (j = 0; j < i; j++)
76 brelse (USPI_UBH->bh[j]);
77 return NULL;
78}
79
80void ubh_brelse (struct ufs_buffer_head * ubh)
81{
82 unsigned i;
83 if (!ubh)
84 return;
85 for (i = 0; i < ubh->count; i++)
86 brelse (ubh->bh[i]);
87 kfree (ubh);
88}
89
90void ubh_brelse_uspi (struct ufs_sb_private_info * uspi)
91{
92 unsigned i;
93 if (!USPI_UBH)
94 return;
95 for ( i = 0; i < USPI_UBH->count; i++ ) {
96 brelse (USPI_UBH->bh[i]);
97 USPI_UBH->bh[i] = NULL;
98 }
99}
100
101void ubh_mark_buffer_dirty (struct ufs_buffer_head * ubh)
102{
103 unsigned i;
104 if (!ubh)
105 return;
106 for ( i = 0; i < ubh->count; i++ )
107 mark_buffer_dirty (ubh->bh[i]);
108}
109
110void ubh_mark_buffer_uptodate (struct ufs_buffer_head * ubh, int flag)
111{
112 unsigned i;
113 if (!ubh)
114 return;
115 if (flag) {
116 for ( i = 0; i < ubh->count; i++ )
117 set_buffer_uptodate (ubh->bh[i]);
118 } else {
119 for ( i = 0; i < ubh->count; i++ )
120 clear_buffer_uptodate (ubh->bh[i]);
121 }
122}
123
124void ubh_ll_rw_block (int rw, unsigned nr, struct ufs_buffer_head * ubh[])
125{
126 unsigned i;
127 if (!ubh)
128 return;
129 for ( i = 0; i < nr; i++ )
130 ll_rw_block (rw, ubh[i]->count, ubh[i]->bh);
131}
132
133void ubh_wait_on_buffer (struct ufs_buffer_head * ubh)
134{
135 unsigned i;
136 if (!ubh)
137 return;
138 for ( i = 0; i < ubh->count; i++ )
139 wait_on_buffer (ubh->bh[i]);
140}
141
1da177e4
LT
142void ubh_bforget (struct ufs_buffer_head * ubh)
143{
144 unsigned i;
145 if (!ubh)
146 return;
147 for ( i = 0; i < ubh->count; i++ ) if ( ubh->bh[i] )
148 bforget (ubh->bh[i]);
149}
150
151int ubh_buffer_dirty (struct ufs_buffer_head * ubh)
152{
153 unsigned i;
154 unsigned result = 0;
155 if (!ubh)
156 return 0;
157 for ( i = 0; i < ubh->count; i++ )
158 result |= buffer_dirty(ubh->bh[i]);
159 return result;
160}
161
162void _ubh_ubhcpymem_(struct ufs_sb_private_info * uspi,
163 unsigned char * mem, struct ufs_buffer_head * ubh, unsigned size)
164{
165 unsigned len, bhno;
166 if (size > (ubh->count << uspi->s_fshift))
167 size = ubh->count << uspi->s_fshift;
168 bhno = 0;
169 while (size) {
170 len = min_t(unsigned int, size, uspi->s_fsize);
171 memcpy (mem, ubh->bh[bhno]->b_data, len);
172 mem += uspi->s_fsize;
173 size -= len;
174 bhno++;
175 }
176}
177
178void _ubh_memcpyubh_(struct ufs_sb_private_info * uspi,
179 struct ufs_buffer_head * ubh, unsigned char * mem, unsigned size)
180{
181 unsigned len, bhno;
182 if (size > (ubh->count << uspi->s_fshift))
183 size = ubh->count << uspi->s_fshift;
184 bhno = 0;
185 while (size) {
186 len = min_t(unsigned int, size, uspi->s_fsize);
187 memcpy (ubh->bh[bhno]->b_data, mem, len);
188 mem += uspi->s_fsize;
189 size -= len;
190 bhno++;
191 }
192}
193
194dev_t
195ufs_get_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi)
196{
197 __fs32 fs32;
198 dev_t dev;
199
200 if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
201 fs32 = ufsi->i_u1.i_data[1];
202 else
203 fs32 = ufsi->i_u1.i_data[0];
204 fs32 = fs32_to_cpu(sb, fs32);
205 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
206 case UFS_ST_SUNx86:
207 case UFS_ST_SUN:
208 if ((fs32 & 0xffff0000) == 0 ||
209 (fs32 & 0xffff0000) == 0xffff0000)
210 dev = old_decode_dev(fs32 & 0x7fff);
211 else
212 dev = MKDEV(sysv_major(fs32), sysv_minor(fs32));
213 break;
214
215 default:
216 dev = old_decode_dev(fs32);
217 break;
218 }
219 return dev;
220}
221
222void
223ufs_set_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi, dev_t dev)
224{
225 __fs32 fs32;
226
227 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
228 case UFS_ST_SUNx86:
229 case UFS_ST_SUN:
230 fs32 = sysv_encode_dev(dev);
231 if ((fs32 & 0xffff8000) == 0) {
232 fs32 = old_encode_dev(dev);
233 }
234 break;
235
236 default:
237 fs32 = old_encode_dev(dev);
238 break;
239 }
240 fs32 = cpu_to_fs32(sb, fs32);
241 if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
242 ufsi->i_u1.i_data[1] = fs32;
243 else
244 ufsi->i_u1.i_data[0] = fs32;
245}