Commit | Line | Data |
---|---|---|
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 | ||
1da177e4 LT |
17 | struct ufs_buffer_head * _ubh_bread_ (struct ufs_sb_private_info * uspi, |
18 | struct super_block *sb, u64 fragment, u64 size) | |
19 | { | |
20 | struct ufs_buffer_head * ubh; | |
21 | unsigned i, j ; | |
22 | u64 count = 0; | |
23 | if (size & ~uspi->s_fmask) | |
24 | return NULL; | |
25 | count = size >> uspi->s_fshift; | |
26 | if (count > UFS_MAXFRAG) | |
27 | return NULL; | |
28 | ubh = (struct ufs_buffer_head *) | |
29 | kmalloc (sizeof (struct ufs_buffer_head), GFP_KERNEL); | |
30 | if (!ubh) | |
31 | return NULL; | |
32 | ubh->fragment = fragment; | |
33 | ubh->count = count; | |
34 | for (i = 0; i < count; i++) | |
35 | if (!(ubh->bh[i] = sb_bread(sb, fragment + i))) | |
36 | goto failed; | |
37 | for (; i < UFS_MAXFRAG; i++) | |
38 | ubh->bh[i] = NULL; | |
39 | return ubh; | |
40 | failed: | |
41 | for (j = 0; j < i; j++) | |
42 | brelse (ubh->bh[j]); | |
43 | kfree(ubh); | |
44 | return NULL; | |
45 | } | |
46 | ||
47 | struct ufs_buffer_head * ubh_bread_uspi (struct ufs_sb_private_info * uspi, | |
48 | struct super_block *sb, u64 fragment, u64 size) | |
49 | { | |
50 | unsigned i, j; | |
51 | u64 count = 0; | |
52 | if (size & ~uspi->s_fmask) | |
53 | return NULL; | |
54 | count = size >> uspi->s_fshift; | |
55 | if (count <= 0 || count > UFS_MAXFRAG) | |
56 | return NULL; | |
9695ef16 ED |
57 | USPI_UBH(uspi)->fragment = fragment; |
58 | USPI_UBH(uspi)->count = count; | |
1da177e4 | 59 | for (i = 0; i < count; i++) |
9695ef16 | 60 | if (!(USPI_UBH(uspi)->bh[i] = sb_bread(sb, fragment + i))) |
1da177e4 LT |
61 | goto failed; |
62 | for (; i < UFS_MAXFRAG; i++) | |
9695ef16 ED |
63 | USPI_UBH(uspi)->bh[i] = NULL; |
64 | return USPI_UBH(uspi); | |
1da177e4 LT |
65 | failed: |
66 | for (j = 0; j < i; j++) | |
9695ef16 | 67 | brelse (USPI_UBH(uspi)->bh[j]); |
1da177e4 LT |
68 | return NULL; |
69 | } | |
70 | ||
71 | void ubh_brelse (struct ufs_buffer_head * ubh) | |
72 | { | |
73 | unsigned i; | |
74 | if (!ubh) | |
75 | return; | |
76 | for (i = 0; i < ubh->count; i++) | |
77 | brelse (ubh->bh[i]); | |
78 | kfree (ubh); | |
79 | } | |
80 | ||
81 | void ubh_brelse_uspi (struct ufs_sb_private_info * uspi) | |
82 | { | |
83 | unsigned i; | |
9695ef16 | 84 | if (!USPI_UBH(uspi)) |
1da177e4 | 85 | return; |
9695ef16 ED |
86 | for ( i = 0; i < USPI_UBH(uspi)->count; i++ ) { |
87 | brelse (USPI_UBH(uspi)->bh[i]); | |
88 | USPI_UBH(uspi)->bh[i] = NULL; | |
1da177e4 LT |
89 | } |
90 | } | |
91 | ||
92 | void ubh_mark_buffer_dirty (struct ufs_buffer_head * ubh) | |
93 | { | |
94 | unsigned i; | |
95 | if (!ubh) | |
96 | return; | |
97 | for ( i = 0; i < ubh->count; i++ ) | |
98 | mark_buffer_dirty (ubh->bh[i]); | |
99 | } | |
100 | ||
101 | void ubh_mark_buffer_uptodate (struct ufs_buffer_head * ubh, int flag) | |
102 | { | |
103 | unsigned i; | |
104 | if (!ubh) | |
105 | return; | |
106 | if (flag) { | |
107 | for ( i = 0; i < ubh->count; i++ ) | |
108 | set_buffer_uptodate (ubh->bh[i]); | |
109 | } else { | |
110 | for ( i = 0; i < ubh->count; i++ ) | |
111 | clear_buffer_uptodate (ubh->bh[i]); | |
112 | } | |
113 | } | |
114 | ||
115 | void ubh_ll_rw_block (int rw, unsigned nr, struct ufs_buffer_head * ubh[]) | |
116 | { | |
117 | unsigned i; | |
118 | if (!ubh) | |
119 | return; | |
120 | for ( i = 0; i < nr; i++ ) | |
121 | ll_rw_block (rw, ubh[i]->count, ubh[i]->bh); | |
122 | } | |
123 | ||
124 | void ubh_wait_on_buffer (struct ufs_buffer_head * ubh) | |
125 | { | |
126 | unsigned i; | |
127 | if (!ubh) | |
128 | return; | |
129 | for ( i = 0; i < ubh->count; i++ ) | |
130 | wait_on_buffer (ubh->bh[i]); | |
131 | } | |
132 | ||
1da177e4 LT |
133 | void ubh_bforget (struct ufs_buffer_head * ubh) |
134 | { | |
135 | unsigned i; | |
136 | if (!ubh) | |
137 | return; | |
138 | for ( i = 0; i < ubh->count; i++ ) if ( ubh->bh[i] ) | |
139 | bforget (ubh->bh[i]); | |
140 | } | |
141 | ||
142 | int ubh_buffer_dirty (struct ufs_buffer_head * ubh) | |
143 | { | |
144 | unsigned i; | |
145 | unsigned result = 0; | |
146 | if (!ubh) | |
147 | return 0; | |
148 | for ( i = 0; i < ubh->count; i++ ) | |
149 | result |= buffer_dirty(ubh->bh[i]); | |
150 | return result; | |
151 | } | |
152 | ||
153 | void _ubh_ubhcpymem_(struct ufs_sb_private_info * uspi, | |
154 | unsigned char * mem, struct ufs_buffer_head * ubh, unsigned size) | |
155 | { | |
156 | unsigned len, bhno; | |
157 | if (size > (ubh->count << uspi->s_fshift)) | |
158 | size = ubh->count << uspi->s_fshift; | |
159 | bhno = 0; | |
160 | while (size) { | |
161 | len = min_t(unsigned int, size, uspi->s_fsize); | |
162 | memcpy (mem, ubh->bh[bhno]->b_data, len); | |
163 | mem += uspi->s_fsize; | |
164 | size -= len; | |
165 | bhno++; | |
166 | } | |
167 | } | |
168 | ||
169 | void _ubh_memcpyubh_(struct ufs_sb_private_info * uspi, | |
170 | struct ufs_buffer_head * ubh, unsigned char * mem, unsigned size) | |
171 | { | |
172 | unsigned len, bhno; | |
173 | if (size > (ubh->count << uspi->s_fshift)) | |
174 | size = ubh->count << uspi->s_fshift; | |
175 | bhno = 0; | |
176 | while (size) { | |
177 | len = min_t(unsigned int, size, uspi->s_fsize); | |
178 | memcpy (ubh->bh[bhno]->b_data, mem, len); | |
179 | mem += uspi->s_fsize; | |
180 | size -= len; | |
181 | bhno++; | |
182 | } | |
183 | } | |
184 | ||
185 | dev_t | |
186 | ufs_get_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi) | |
187 | { | |
188 | __fs32 fs32; | |
189 | dev_t dev; | |
190 | ||
191 | if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86) | |
192 | fs32 = ufsi->i_u1.i_data[1]; | |
193 | else | |
194 | fs32 = ufsi->i_u1.i_data[0]; | |
195 | fs32 = fs32_to_cpu(sb, fs32); | |
196 | switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) { | |
197 | case UFS_ST_SUNx86: | |
198 | case UFS_ST_SUN: | |
199 | if ((fs32 & 0xffff0000) == 0 || | |
200 | (fs32 & 0xffff0000) == 0xffff0000) | |
201 | dev = old_decode_dev(fs32 & 0x7fff); | |
202 | else | |
203 | dev = MKDEV(sysv_major(fs32), sysv_minor(fs32)); | |
204 | break; | |
205 | ||
206 | default: | |
207 | dev = old_decode_dev(fs32); | |
208 | break; | |
209 | } | |
210 | return dev; | |
211 | } | |
212 | ||
213 | void | |
214 | ufs_set_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi, dev_t dev) | |
215 | { | |
216 | __fs32 fs32; | |
217 | ||
218 | switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) { | |
219 | case UFS_ST_SUNx86: | |
220 | case UFS_ST_SUN: | |
221 | fs32 = sysv_encode_dev(dev); | |
222 | if ((fs32 & 0xffff8000) == 0) { | |
223 | fs32 = old_encode_dev(dev); | |
224 | } | |
225 | break; | |
226 | ||
227 | default: | |
228 | fs32 = old_encode_dev(dev); | |
229 | break; | |
230 | } | |
231 | fs32 = cpu_to_fs32(sb, fs32); | |
232 | if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86) | |
233 | ufsi->i_u1.i_data[1] = fs32; | |
234 | else | |
235 | ufsi->i_u1.i_data[0] = fs32; | |
236 | } |