Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/ufs/inode.c | |
3 | * | |
4 | * Copyright (C) 1998 | |
5 | * Daniel Pirkl <daniel.pirkl@email.cz> | |
6 | * Charles University, Faculty of Mathematics and Physics | |
7 | * | |
8 | * from | |
9 | * | |
10 | * linux/fs/ext2/inode.c | |
11 | * | |
12 | * Copyright (C) 1992, 1993, 1994, 1995 | |
13 | * Remy Card (card@masi.ibp.fr) | |
14 | * Laboratoire MASI - Institut Blaise Pascal | |
15 | * Universite Pierre et Marie Curie (Paris VI) | |
16 | * | |
17 | * from | |
18 | * | |
19 | * linux/fs/minix/inode.c | |
20 | * | |
21 | * Copyright (C) 1991, 1992 Linus Torvalds | |
22 | * | |
23 | * Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 | |
24 | * Big-endian to little-endian byte-swapping/bitmaps by | |
25 | * David S. Miller (davem@caip.rutgers.edu), 1995 | |
26 | */ | |
27 | ||
28 | #include <asm/uaccess.h> | |
29 | #include <asm/system.h> | |
30 | ||
31 | #include <linux/errno.h> | |
32 | #include <linux/fs.h> | |
33 | #include <linux/ufs_fs.h> | |
34 | #include <linux/time.h> | |
35 | #include <linux/stat.h> | |
36 | #include <linux/string.h> | |
37 | #include <linux/mm.h> | |
38 | #include <linux/smp_lock.h> | |
39 | #include <linux/buffer_head.h> | |
40 | ||
41 | #include "swab.h" | |
42 | #include "util.h" | |
43 | ||
1da177e4 LT |
44 | static int ufs_block_to_path(struct inode *inode, sector_t i_block, sector_t offsets[4]) |
45 | { | |
46 | struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi; | |
47 | int ptrs = uspi->s_apb; | |
48 | int ptrs_bits = uspi->s_apbshift; | |
49 | const long direct_blocks = UFS_NDADDR, | |
50 | indirect_blocks = ptrs, | |
51 | double_blocks = (1 << (ptrs_bits * 2)); | |
52 | int n = 0; | |
53 | ||
54 | ||
abf5d15f | 55 | UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks); |
1da177e4 LT |
56 | if (i_block < 0) { |
57 | ufs_warning(inode->i_sb, "ufs_block_to_path", "block < 0"); | |
58 | } else if (i_block < direct_blocks) { | |
59 | offsets[n++] = i_block; | |
60 | } else if ((i_block -= direct_blocks) < indirect_blocks) { | |
61 | offsets[n++] = UFS_IND_BLOCK; | |
62 | offsets[n++] = i_block; | |
63 | } else if ((i_block -= indirect_blocks) < double_blocks) { | |
64 | offsets[n++] = UFS_DIND_BLOCK; | |
65 | offsets[n++] = i_block >> ptrs_bits; | |
66 | offsets[n++] = i_block & (ptrs - 1); | |
67 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { | |
68 | offsets[n++] = UFS_TIND_BLOCK; | |
69 | offsets[n++] = i_block >> (ptrs_bits * 2); | |
70 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); | |
71 | offsets[n++] = i_block & (ptrs - 1); | |
72 | } else { | |
73 | ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big"); | |
74 | } | |
75 | return n; | |
76 | } | |
77 | ||
78 | /* | |
79 | * Returns the location of the fragment from | |
80 | * the begining of the filesystem. | |
81 | */ | |
82 | ||
83 | u64 ufs_frag_map(struct inode *inode, sector_t frag) | |
84 | { | |
85 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
86 | struct super_block *sb = inode->i_sb; | |
87 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
88 | u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift; | |
89 | int shift = uspi->s_apbshift-uspi->s_fpbshift; | |
90 | sector_t offsets[4], *p; | |
91 | int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets); | |
92 | u64 ret = 0L; | |
93 | __fs32 block; | |
94 | __fs64 u2_block = 0L; | |
95 | unsigned flags = UFS_SB(sb)->s_flags; | |
96 | u64 temp = 0L; | |
97 | ||
abf5d15f ED |
98 | UFSD(": frag = %llu depth = %d\n", (unsigned long long)frag, depth); |
99 | UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",uspi->s_fpbshift,uspi->s_apbmask,mask); | |
1da177e4 LT |
100 | |
101 | if (depth == 0) | |
102 | return 0; | |
103 | ||
104 | p = offsets; | |
105 | ||
106 | lock_kernel(); | |
107 | if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) | |
108 | goto ufs2; | |
109 | ||
110 | block = ufsi->i_u1.i_data[*p++]; | |
111 | if (!block) | |
112 | goto out; | |
113 | while (--depth) { | |
114 | struct buffer_head *bh; | |
115 | sector_t n = *p++; | |
116 | ||
117 | bh = sb_bread(sb, uspi->s_sbbase + fs32_to_cpu(sb, block)+(n>>shift)); | |
118 | if (!bh) | |
119 | goto out; | |
120 | block = ((__fs32 *) bh->b_data)[n & mask]; | |
121 | brelse (bh); | |
122 | if (!block) | |
123 | goto out; | |
124 | } | |
125 | ret = (u64) (uspi->s_sbbase + fs32_to_cpu(sb, block) + (frag & uspi->s_fpbmask)); | |
126 | goto out; | |
127 | ufs2: | |
128 | u2_block = ufsi->i_u1.u2_i_data[*p++]; | |
129 | if (!u2_block) | |
130 | goto out; | |
131 | ||
132 | ||
133 | while (--depth) { | |
134 | struct buffer_head *bh; | |
135 | sector_t n = *p++; | |
136 | ||
137 | ||
138 | temp = (u64)(uspi->s_sbbase) + fs64_to_cpu(sb, u2_block); | |
139 | bh = sb_bread(sb, temp +(u64) (n>>shift)); | |
140 | if (!bh) | |
141 | goto out; | |
142 | u2_block = ((__fs64 *)bh->b_data)[n & mask]; | |
143 | brelse(bh); | |
144 | if (!u2_block) | |
145 | goto out; | |
146 | } | |
147 | temp = (u64)uspi->s_sbbase + fs64_to_cpu(sb, u2_block); | |
148 | ret = temp + (u64) (frag & uspi->s_fpbmask); | |
149 | ||
150 | out: | |
151 | unlock_kernel(); | |
152 | return ret; | |
153 | } | |
154 | ||
022a6dc5 | 155 | static void ufs_clear_frag(struct inode *inode, struct buffer_head *bh) |
c9a27b5d ED |
156 | { |
157 | lock_buffer(bh); | |
158 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); | |
159 | set_buffer_uptodate(bh); | |
160 | mark_buffer_dirty(bh); | |
161 | unlock_buffer(bh); | |
162 | if (IS_SYNC(inode)) | |
163 | sync_dirty_buffer(bh); | |
164 | } | |
165 | ||
022a6dc5 ED |
166 | static struct buffer_head * |
167 | ufs_clear_frags(struct inode *inode, sector_t beg, | |
168 | unsigned int n) | |
169 | { | |
170 | struct buffer_head *res, *bh; | |
171 | sector_t end = beg + n; | |
172 | ||
173 | res = sb_getblk(inode->i_sb, beg); | |
174 | ufs_clear_frag(inode, res); | |
175 | for (++beg; beg < end; ++beg) { | |
176 | bh = sb_getblk(inode->i_sb, beg); | |
177 | ufs_clear_frag(inode, bh); | |
f3914758 | 178 | brelse(bh); |
022a6dc5 ED |
179 | } |
180 | return res; | |
181 | } | |
182 | ||
183 | /** | |
184 | * ufs_inode_getfrag() - allocate new fragment(s) | |
185 | * @inode - pointer to inode | |
186 | * @fragment - number of `fragment' which hold pointer | |
187 | * to new allocated fragment(s) | |
188 | * @new_fragment - number of new allocated fragment(s) | |
189 | * @required - how many fragment(s) we require | |
190 | * @err - we set it if something wrong | |
191 | * @phys - pointer to where we save physical number of new allocated fragments, | |
192 | * NULL if we allocate not data(indirect blocks for example). | |
193 | * @new - we set it if we allocate new block | |
194 | * @locked_page - for ufs_new_fragments() | |
195 | */ | |
196 | static struct buffer_head * | |
197 | ufs_inode_getfrag(struct inode *inode, unsigned int fragment, | |
198 | sector_t new_fragment, unsigned int required, int *err, | |
199 | long *phys, int *new, struct page *locked_page) | |
1da177e4 LT |
200 | { |
201 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
022a6dc5 ED |
202 | struct super_block *sb = inode->i_sb; |
203 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
1da177e4 LT |
204 | struct buffer_head * result; |
205 | unsigned block, blockoff, lastfrag, lastblock, lastblockoff; | |
206 | unsigned tmp, goal; | |
207 | __fs32 * p, * p2; | |
1da177e4 | 208 | |
022a6dc5 ED |
209 | UFSD("ENTER, ino %lu, fragment %u, new_fragment %llu, required %u, " |
210 | "metadata %d\n", inode->i_ino, fragment, | |
211 | (unsigned long long)new_fragment, required, !phys); | |
1da177e4 | 212 | |
1da177e4 LT |
213 | /* TODO : to be done for write support |
214 | if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) | |
215 | goto ufs2; | |
216 | */ | |
217 | ||
218 | block = ufs_fragstoblks (fragment); | |
219 | blockoff = ufs_fragnum (fragment); | |
220 | p = ufsi->i_u1.i_data + block; | |
221 | goal = 0; | |
222 | ||
223 | repeat: | |
224 | tmp = fs32_to_cpu(sb, *p); | |
225 | lastfrag = ufsi->i_lastfrag; | |
226 | if (tmp && fragment < lastfrag) { | |
022a6dc5 | 227 | if (!phys) { |
1da177e4 LT |
228 | result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff); |
229 | if (tmp == fs32_to_cpu(sb, *p)) { | |
abf5d15f | 230 | UFSD("EXIT, result %u\n", tmp + blockoff); |
1da177e4 LT |
231 | return result; |
232 | } | |
233 | brelse (result); | |
234 | goto repeat; | |
235 | } else { | |
c9a27b5d | 236 | *phys = tmp + blockoff; |
1da177e4 LT |
237 | return NULL; |
238 | } | |
239 | } | |
240 | ||
241 | lastblock = ufs_fragstoblks (lastfrag); | |
242 | lastblockoff = ufs_fragnum (lastfrag); | |
243 | /* | |
244 | * We will extend file into new block beyond last allocated block | |
245 | */ | |
246 | if (lastblock < block) { | |
247 | /* | |
248 | * We must reallocate last allocated block | |
249 | */ | |
250 | if (lastblockoff) { | |
251 | p2 = ufsi->i_u1.i_data + lastblock; | |
252 | tmp = ufs_new_fragments (inode, p2, lastfrag, | |
6ef4d6bf ED |
253 | fs32_to_cpu(sb, *p2), uspi->s_fpb - lastblockoff, |
254 | err, locked_page); | |
1da177e4 LT |
255 | if (!tmp) { |
256 | if (lastfrag != ufsi->i_lastfrag) | |
257 | goto repeat; | |
258 | else | |
259 | return NULL; | |
260 | } | |
261 | lastfrag = ufsi->i_lastfrag; | |
262 | ||
263 | } | |
264 | goal = fs32_to_cpu(sb, ufsi->i_u1.i_data[lastblock]) + uspi->s_fpb; | |
265 | tmp = ufs_new_fragments (inode, p, fragment - blockoff, | |
6ef4d6bf ED |
266 | goal, required + blockoff, |
267 | err, locked_page); | |
1da177e4 LT |
268 | } |
269 | /* | |
270 | * We will extend last allocated block | |
271 | */ | |
272 | else if (lastblock == block) { | |
6ef4d6bf ED |
273 | tmp = ufs_new_fragments(inode, p, fragment - (blockoff - lastblockoff), |
274 | fs32_to_cpu(sb, *p), required + (blockoff - lastblockoff), | |
275 | err, locked_page); | |
1da177e4 LT |
276 | } |
277 | /* | |
278 | * We will allocate new block before last allocated block | |
279 | */ | |
280 | else /* (lastblock > block) */ { | |
281 | if (lastblock && (tmp = fs32_to_cpu(sb, ufsi->i_u1.i_data[lastblock-1]))) | |
282 | goal = tmp + uspi->s_fpb; | |
6ef4d6bf ED |
283 | tmp = ufs_new_fragments(inode, p, fragment - blockoff, |
284 | goal, uspi->s_fpb, err, locked_page); | |
1da177e4 LT |
285 | } |
286 | if (!tmp) { | |
287 | if ((!blockoff && *p) || | |
288 | (blockoff && lastfrag != ufsi->i_lastfrag)) | |
289 | goto repeat; | |
290 | *err = -ENOSPC; | |
291 | return NULL; | |
292 | } | |
293 | ||
022a6dc5 ED |
294 | if (!phys) { |
295 | result = ufs_clear_frags(inode, tmp + blockoff, required); | |
1da177e4 | 296 | } else { |
c9a27b5d | 297 | *phys = tmp + blockoff; |
1da177e4 LT |
298 | result = NULL; |
299 | *err = 0; | |
300 | *new = 1; | |
301 | } | |
302 | ||
303 | inode->i_ctime = CURRENT_TIME_SEC; | |
304 | if (IS_SYNC(inode)) | |
305 | ufs_sync_inode (inode); | |
306 | mark_inode_dirty(inode); | |
abf5d15f | 307 | UFSD("EXIT, result %u\n", tmp + blockoff); |
1da177e4 LT |
308 | return result; |
309 | ||
310 | /* This part : To be implemented .... | |
311 | Required only for writing, not required for READ-ONLY. | |
312 | ufs2: | |
313 | ||
314 | u2_block = ufs_fragstoblks(fragment); | |
315 | u2_blockoff = ufs_fragnum(fragment); | |
316 | p = ufsi->i_u1.u2_i_data + block; | |
317 | goal = 0; | |
318 | ||
319 | repeat2: | |
320 | tmp = fs32_to_cpu(sb, *p); | |
321 | lastfrag = ufsi->i_lastfrag; | |
322 | ||
323 | */ | |
324 | } | |
325 | ||
022a6dc5 ED |
326 | /** |
327 | * ufs_inode_getblock() - allocate new block | |
328 | * @inode - pointer to inode | |
329 | * @bh - pointer to block which hold "pointer" to new allocated block | |
330 | * @fragment - number of `fragment' which hold pointer | |
331 | * to new allocated block | |
332 | * @new_fragment - number of new allocated fragment | |
333 | * (block will hold this fragment and also uspi->s_fpb-1) | |
334 | * @err - see ufs_inode_getfrag() | |
335 | * @phys - see ufs_inode_getfrag() | |
336 | * @new - see ufs_inode_getfrag() | |
337 | * @locked_page - see ufs_inode_getfrag() | |
338 | */ | |
339 | static struct buffer_head * | |
340 | ufs_inode_getblock(struct inode *inode, struct buffer_head *bh, | |
341 | unsigned int fragment, sector_t new_fragment, int *err, | |
342 | long *phys, int *new, struct page *locked_page) | |
1da177e4 | 343 | { |
022a6dc5 ED |
344 | struct super_block *sb = inode->i_sb; |
345 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
1da177e4 LT |
346 | struct buffer_head * result; |
347 | unsigned tmp, goal, block, blockoff; | |
348 | __fs32 * p; | |
349 | ||
1da177e4 LT |
350 | block = ufs_fragstoblks (fragment); |
351 | blockoff = ufs_fragnum (fragment); | |
352 | ||
022a6dc5 ED |
353 | UFSD("ENTER, ino %lu, fragment %u, new_fragment %llu, metadata %d\n", |
354 | inode->i_ino, fragment, (unsigned long long)new_fragment, !phys); | |
1da177e4 LT |
355 | |
356 | result = NULL; | |
357 | if (!bh) | |
358 | goto out; | |
359 | if (!buffer_uptodate(bh)) { | |
360 | ll_rw_block (READ, 1, &bh); | |
361 | wait_on_buffer (bh); | |
362 | if (!buffer_uptodate(bh)) | |
363 | goto out; | |
364 | } | |
365 | ||
366 | p = (__fs32 *) bh->b_data + block; | |
367 | repeat: | |
368 | tmp = fs32_to_cpu(sb, *p); | |
369 | if (tmp) { | |
022a6dc5 | 370 | if (!phys) { |
1da177e4 LT |
371 | result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff); |
372 | if (tmp == fs32_to_cpu(sb, *p)) | |
373 | goto out; | |
374 | brelse (result); | |
375 | goto repeat; | |
376 | } else { | |
c9a27b5d | 377 | *phys = tmp + blockoff; |
1da177e4 LT |
378 | goto out; |
379 | } | |
380 | } | |
381 | ||
382 | if (block && (tmp = fs32_to_cpu(sb, ((__fs32*)bh->b_data)[block-1]) + uspi->s_fpb)) | |
383 | goal = tmp + uspi->s_fpb; | |
384 | else | |
385 | goal = bh->b_blocknr + uspi->s_fpb; | |
6ef4d6bf ED |
386 | tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal, |
387 | uspi->s_fpb, err, locked_page); | |
1da177e4 LT |
388 | if (!tmp) { |
389 | if (fs32_to_cpu(sb, *p)) | |
390 | goto repeat; | |
391 | goto out; | |
392 | } | |
393 | ||
c9a27b5d | 394 | |
022a6dc5 ED |
395 | if (!phys) { |
396 | result = ufs_clear_frags(inode, tmp + blockoff, uspi->s_fpb); | |
1da177e4 | 397 | } else { |
c9a27b5d | 398 | *phys = tmp + blockoff; |
1da177e4 LT |
399 | *new = 1; |
400 | } | |
401 | ||
402 | mark_buffer_dirty(bh); | |
403 | if (IS_SYNC(inode)) | |
404 | sync_dirty_buffer(bh); | |
405 | inode->i_ctime = CURRENT_TIME_SEC; | |
406 | mark_inode_dirty(inode); | |
abf5d15f | 407 | UFSD("result %u\n", tmp + blockoff); |
1da177e4 LT |
408 | out: |
409 | brelse (bh); | |
abf5d15f | 410 | UFSD("EXIT\n"); |
1da177e4 LT |
411 | return result; |
412 | } | |
413 | ||
022a6dc5 ED |
414 | /** |
415 | * ufs_getfrag_bloc() - `get_block_t' function, interface between UFS and | |
416 | * readpage, writepage and so on | |
1da177e4 LT |
417 | */ |
418 | ||
022a6dc5 | 419 | int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create) |
1da177e4 LT |
420 | { |
421 | struct super_block * sb = inode->i_sb; | |
422 | struct ufs_sb_private_info * uspi = UFS_SB(sb)->s_uspi; | |
423 | struct buffer_head * bh; | |
424 | int ret, err, new; | |
425 | unsigned long ptr,phys; | |
426 | u64 phys64 = 0; | |
427 | ||
428 | if (!create) { | |
429 | phys64 = ufs_frag_map(inode, fragment); | |
abf5d15f | 430 | UFSD("phys64 = %llu \n",phys64); |
1da177e4 LT |
431 | if (phys64) |
432 | map_bh(bh_result, sb, phys64); | |
433 | return 0; | |
434 | } | |
435 | ||
436 | /* This code entered only while writing ....? */ | |
437 | ||
438 | err = -EIO; | |
439 | new = 0; | |
440 | ret = 0; | |
441 | bh = NULL; | |
442 | ||
443 | lock_kernel(); | |
444 | ||
abf5d15f | 445 | UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment); |
1da177e4 LT |
446 | if (fragment < 0) |
447 | goto abort_negative; | |
448 | if (fragment > | |
449 | ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb) | |
450 | << uspi->s_fpbshift)) | |
451 | goto abort_too_big; | |
452 | ||
453 | err = 0; | |
454 | ptr = fragment; | |
455 | ||
456 | /* | |
457 | * ok, these macros clean the logic up a bit and make | |
458 | * it much more readable: | |
459 | */ | |
460 | #define GET_INODE_DATABLOCK(x) \ | |
022a6dc5 | 461 | ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new, bh_result->b_page) |
1da177e4 | 462 | #define GET_INODE_PTR(x) \ |
022a6dc5 | 463 | ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL, bh_result->b_page) |
1da177e4 | 464 | #define GET_INDIRECT_DATABLOCK(x) \ |
022a6dc5 ED |
465 | ufs_inode_getblock(inode, bh, x, fragment, \ |
466 | &err, &phys, &new, bh_result->b_page); | |
1da177e4 | 467 | #define GET_INDIRECT_PTR(x) \ |
022a6dc5 ED |
468 | ufs_inode_getblock(inode, bh, x, fragment, \ |
469 | &err, NULL, NULL, bh_result->b_page); | |
1da177e4 LT |
470 | |
471 | if (ptr < UFS_NDIR_FRAGMENT) { | |
472 | bh = GET_INODE_DATABLOCK(ptr); | |
473 | goto out; | |
474 | } | |
475 | ptr -= UFS_NDIR_FRAGMENT; | |
476 | if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) { | |
477 | bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift)); | |
478 | goto get_indirect; | |
479 | } | |
480 | ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift); | |
481 | if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) { | |
482 | bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift)); | |
483 | goto get_double; | |
484 | } | |
485 | ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift); | |
486 | bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift)); | |
487 | bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask); | |
488 | get_double: | |
489 | bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask); | |
490 | get_indirect: | |
491 | bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask); | |
492 | ||
493 | #undef GET_INODE_DATABLOCK | |
494 | #undef GET_INODE_PTR | |
495 | #undef GET_INDIRECT_DATABLOCK | |
496 | #undef GET_INDIRECT_PTR | |
497 | ||
498 | out: | |
499 | if (err) | |
500 | goto abort; | |
501 | if (new) | |
502 | set_buffer_new(bh_result); | |
503 | map_bh(bh_result, sb, phys); | |
504 | abort: | |
505 | unlock_kernel(); | |
506 | return err; | |
507 | ||
508 | abort_negative: | |
509 | ufs_warning(sb, "ufs_get_block", "block < 0"); | |
510 | goto abort; | |
511 | ||
512 | abort_too_big: | |
513 | ufs_warning(sb, "ufs_get_block", "block > big"); | |
514 | goto abort; | |
515 | } | |
516 | ||
517 | struct buffer_head *ufs_getfrag(struct inode *inode, unsigned int fragment, | |
518 | int create, int *err) | |
519 | { | |
520 | struct buffer_head dummy; | |
521 | int error; | |
522 | ||
523 | dummy.b_state = 0; | |
524 | dummy.b_blocknr = -1000; | |
525 | error = ufs_getfrag_block(inode, fragment, &dummy, create); | |
526 | *err = error; | |
527 | if (!error && buffer_mapped(&dummy)) { | |
528 | struct buffer_head *bh; | |
529 | bh = sb_getblk(inode->i_sb, dummy.b_blocknr); | |
530 | if (buffer_new(&dummy)) { | |
531 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); | |
532 | set_buffer_uptodate(bh); | |
533 | mark_buffer_dirty(bh); | |
534 | } | |
535 | return bh; | |
536 | } | |
537 | return NULL; | |
538 | } | |
539 | ||
540 | struct buffer_head * ufs_bread (struct inode * inode, unsigned fragment, | |
541 | int create, int * err) | |
542 | { | |
543 | struct buffer_head * bh; | |
544 | ||
abf5d15f | 545 | UFSD("ENTER, ino %lu, fragment %u\n", inode->i_ino, fragment); |
1da177e4 LT |
546 | bh = ufs_getfrag (inode, fragment, create, err); |
547 | if (!bh || buffer_uptodate(bh)) | |
548 | return bh; | |
549 | ll_rw_block (READ, 1, &bh); | |
550 | wait_on_buffer (bh); | |
551 | if (buffer_uptodate(bh)) | |
552 | return bh; | |
553 | brelse (bh); | |
554 | *err = -EIO; | |
555 | return NULL; | |
556 | } | |
557 | ||
558 | static int ufs_writepage(struct page *page, struct writeback_control *wbc) | |
559 | { | |
560 | return block_write_full_page(page,ufs_getfrag_block,wbc); | |
561 | } | |
562 | static int ufs_readpage(struct file *file, struct page *page) | |
563 | { | |
564 | return block_read_full_page(page,ufs_getfrag_block); | |
565 | } | |
566 | static int ufs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to) | |
567 | { | |
568 | return block_prepare_write(page,from,to,ufs_getfrag_block); | |
569 | } | |
570 | static sector_t ufs_bmap(struct address_space *mapping, sector_t block) | |
571 | { | |
572 | return generic_block_bmap(mapping,block,ufs_getfrag_block); | |
573 | } | |
574 | struct address_space_operations ufs_aops = { | |
575 | .readpage = ufs_readpage, | |
576 | .writepage = ufs_writepage, | |
577 | .sync_page = block_sync_page, | |
578 | .prepare_write = ufs_prepare_write, | |
579 | .commit_write = generic_commit_write, | |
580 | .bmap = ufs_bmap | |
581 | }; | |
582 | ||
826843a3 ED |
583 | static void ufs_set_inode_ops(struct inode *inode) |
584 | { | |
585 | if (S_ISREG(inode->i_mode)) { | |
586 | inode->i_op = &ufs_file_inode_operations; | |
587 | inode->i_fop = &ufs_file_operations; | |
588 | inode->i_mapping->a_ops = &ufs_aops; | |
589 | } else if (S_ISDIR(inode->i_mode)) { | |
590 | inode->i_op = &ufs_dir_inode_operations; | |
591 | inode->i_fop = &ufs_dir_operations; | |
592 | inode->i_mapping->a_ops = &ufs_aops; | |
593 | } else if (S_ISLNK(inode->i_mode)) { | |
594 | if (!inode->i_blocks) | |
595 | inode->i_op = &ufs_fast_symlink_inode_operations; | |
596 | else { | |
597 | inode->i_op = &page_symlink_inode_operations; | |
598 | inode->i_mapping->a_ops = &ufs_aops; | |
599 | } | |
600 | } else | |
601 | init_special_inode(inode, inode->i_mode, | |
602 | ufs_get_inode_dev(inode->i_sb, UFS_I(inode))); | |
603 | } | |
604 | ||
1da177e4 LT |
605 | void ufs_read_inode (struct inode * inode) |
606 | { | |
607 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
608 | struct super_block * sb; | |
609 | struct ufs_sb_private_info * uspi; | |
610 | struct ufs_inode * ufs_inode; | |
611 | struct ufs2_inode *ufs2_inode; | |
612 | struct buffer_head * bh; | |
613 | mode_t mode; | |
614 | unsigned i; | |
615 | unsigned flags; | |
616 | ||
abf5d15f | 617 | UFSD("ENTER, ino %lu\n", inode->i_ino); |
1da177e4 LT |
618 | |
619 | sb = inode->i_sb; | |
620 | uspi = UFS_SB(sb)->s_uspi; | |
621 | flags = UFS_SB(sb)->s_flags; | |
622 | ||
623 | if (inode->i_ino < UFS_ROOTINO || | |
624 | inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) { | |
625 | ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino); | |
626 | goto bad_inode; | |
627 | } | |
628 | ||
629 | bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino)); | |
630 | if (!bh) { | |
631 | ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino); | |
632 | goto bad_inode; | |
633 | } | |
634 | if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) | |
635 | goto ufs2_inode; | |
636 | ||
637 | ufs_inode = (struct ufs_inode *) (bh->b_data + sizeof(struct ufs_inode) * ufs_inotofsbo(inode->i_ino)); | |
638 | ||
639 | /* | |
640 | * Copy data to the in-core inode. | |
641 | */ | |
642 | inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode); | |
643 | inode->i_nlink = fs16_to_cpu(sb, ufs_inode->ui_nlink); | |
644 | if (inode->i_nlink == 0) | |
645 | ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino); | |
646 | ||
647 | /* | |
648 | * Linux now has 32-bit uid and gid, so we can support EFT. | |
649 | */ | |
650 | inode->i_uid = ufs_get_inode_uid(sb, ufs_inode); | |
651 | inode->i_gid = ufs_get_inode_gid(sb, ufs_inode); | |
652 | ||
653 | inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size); | |
654 | inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec); | |
655 | inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec); | |
656 | inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec); | |
657 | inode->i_mtime.tv_nsec = 0; | |
658 | inode->i_atime.tv_nsec = 0; | |
659 | inode->i_ctime.tv_nsec = 0; | |
660 | inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks); | |
661 | inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat) */ | |
662 | inode->i_version++; | |
663 | ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags); | |
664 | ufsi->i_gen = fs32_to_cpu(sb, ufs_inode->ui_gen); | |
665 | ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); | |
666 | ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); | |
667 | ufsi->i_lastfrag = (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift; | |
dd187a26 | 668 | ufsi->i_dir_start_lookup = 0; |
1da177e4 LT |
669 | |
670 | if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { | |
671 | for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++) | |
672 | ufsi->i_u1.i_data[i] = ufs_inode->ui_u2.ui_addr.ui_db[i]; | |
dd187a26 | 673 | } else { |
1da177e4 LT |
674 | for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++) |
675 | ufsi->i_u1.i_symlink[i] = ufs_inode->ui_u2.ui_symlink[i]; | |
676 | } | |
677 | ufsi->i_osync = 0; | |
678 | ||
826843a3 | 679 | ufs_set_inode_ops(inode); |
1da177e4 LT |
680 | |
681 | brelse (bh); | |
682 | ||
abf5d15f | 683 | UFSD("EXIT\n"); |
1da177e4 LT |
684 | return; |
685 | ||
686 | bad_inode: | |
687 | make_bad_inode(inode); | |
688 | return; | |
689 | ||
690 | ufs2_inode : | |
abf5d15f | 691 | UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino); |
1da177e4 LT |
692 | |
693 | ufs2_inode = (struct ufs2_inode *)(bh->b_data + sizeof(struct ufs2_inode) * ufs_inotofsbo(inode->i_ino)); | |
694 | ||
695 | /* | |
696 | * Copy data to the in-core inode. | |
697 | */ | |
698 | inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode); | |
699 | inode->i_nlink = fs16_to_cpu(sb, ufs2_inode->ui_nlink); | |
700 | if (inode->i_nlink == 0) | |
701 | ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino); | |
702 | ||
703 | /* | |
704 | * Linux now has 32-bit uid and gid, so we can support EFT. | |
705 | */ | |
706 | inode->i_uid = fs32_to_cpu(sb, ufs2_inode->ui_uid); | |
707 | inode->i_gid = fs32_to_cpu(sb, ufs2_inode->ui_gid); | |
708 | ||
709 | inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size); | |
710 | inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_atime.tv_sec); | |
711 | inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_ctime.tv_sec); | |
712 | inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs2_inode->ui_mtime.tv_sec); | |
713 | inode->i_mtime.tv_nsec = 0; | |
714 | inode->i_atime.tv_nsec = 0; | |
715 | inode->i_ctime.tv_nsec = 0; | |
716 | inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks); | |
717 | inode->i_blksize = PAGE_SIZE; /*This is the optimal IO size(for stat)*/ | |
718 | ||
719 | inode->i_version++; | |
720 | ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags); | |
721 | ufsi->i_gen = fs32_to_cpu(sb, ufs2_inode->ui_gen); | |
722 | /* | |
723 | ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); | |
724 | ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); | |
725 | */ | |
726 | ufsi->i_lastfrag= (inode->i_size + uspi->s_fsize- 1) >> uspi->s_fshift; | |
727 | ||
728 | if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { | |
729 | for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++) | |
730 | ufsi->i_u1.u2_i_data[i] = | |
731 | ufs2_inode->ui_u2.ui_addr.ui_db[i]; | |
732 | } | |
733 | else { | |
734 | for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++) | |
735 | ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i]; | |
736 | } | |
737 | ufsi->i_osync = 0; | |
738 | ||
826843a3 | 739 | ufs_set_inode_ops(inode); |
1da177e4 LT |
740 | |
741 | brelse(bh); | |
742 | ||
abf5d15f | 743 | UFSD("EXIT\n"); |
1da177e4 LT |
744 | return; |
745 | } | |
746 | ||
747 | static int ufs_update_inode(struct inode * inode, int do_sync) | |
748 | { | |
749 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
750 | struct super_block * sb; | |
751 | struct ufs_sb_private_info * uspi; | |
752 | struct buffer_head * bh; | |
753 | struct ufs_inode * ufs_inode; | |
754 | unsigned i; | |
755 | unsigned flags; | |
756 | ||
abf5d15f | 757 | UFSD("ENTER, ino %lu\n", inode->i_ino); |
1da177e4 LT |
758 | |
759 | sb = inode->i_sb; | |
760 | uspi = UFS_SB(sb)->s_uspi; | |
761 | flags = UFS_SB(sb)->s_flags; | |
762 | ||
763 | if (inode->i_ino < UFS_ROOTINO || | |
764 | inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) { | |
765 | ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino); | |
766 | return -1; | |
767 | } | |
768 | ||
769 | bh = sb_bread(sb, ufs_inotofsba(inode->i_ino)); | |
770 | if (!bh) { | |
771 | ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino); | |
772 | return -1; | |
773 | } | |
774 | ufs_inode = (struct ufs_inode *) (bh->b_data + ufs_inotofsbo(inode->i_ino) * sizeof(struct ufs_inode)); | |
775 | ||
776 | ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode); | |
777 | ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink); | |
778 | ||
779 | ufs_set_inode_uid(sb, ufs_inode, inode->i_uid); | |
780 | ufs_set_inode_gid(sb, ufs_inode, inode->i_gid); | |
781 | ||
782 | ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size); | |
783 | ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec); | |
784 | ufs_inode->ui_atime.tv_usec = 0; | |
785 | ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec); | |
786 | ufs_inode->ui_ctime.tv_usec = 0; | |
787 | ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec); | |
788 | ufs_inode->ui_mtime.tv_usec = 0; | |
789 | ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks); | |
790 | ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags); | |
791 | ufs_inode->ui_gen = cpu_to_fs32(sb, ufsi->i_gen); | |
792 | ||
793 | if ((flags & UFS_UID_MASK) == UFS_UID_EFT) { | |
794 | ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow); | |
795 | ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag); | |
796 | } | |
797 | ||
798 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | |
799 | /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */ | |
800 | ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0]; | |
801 | } else if (inode->i_blocks) { | |
802 | for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++) | |
803 | ufs_inode->ui_u2.ui_addr.ui_db[i] = ufsi->i_u1.i_data[i]; | |
804 | } | |
805 | else { | |
806 | for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++) | |
807 | ufs_inode->ui_u2.ui_symlink[i] = ufsi->i_u1.i_symlink[i]; | |
808 | } | |
809 | ||
810 | if (!inode->i_nlink) | |
811 | memset (ufs_inode, 0, sizeof(struct ufs_inode)); | |
812 | ||
813 | mark_buffer_dirty(bh); | |
814 | if (do_sync) | |
815 | sync_dirty_buffer(bh); | |
816 | brelse (bh); | |
817 | ||
abf5d15f | 818 | UFSD("EXIT\n"); |
1da177e4 LT |
819 | return 0; |
820 | } | |
821 | ||
822 | int ufs_write_inode (struct inode * inode, int wait) | |
823 | { | |
824 | int ret; | |
825 | lock_kernel(); | |
826 | ret = ufs_update_inode (inode, wait); | |
827 | unlock_kernel(); | |
828 | return ret; | |
829 | } | |
830 | ||
831 | int ufs_sync_inode (struct inode *inode) | |
832 | { | |
833 | return ufs_update_inode (inode, 1); | |
834 | } | |
835 | ||
836 | void ufs_delete_inode (struct inode * inode) | |
837 | { | |
fef26658 | 838 | truncate_inode_pages(&inode->i_data, 0); |
1da177e4 LT |
839 | /*UFS_I(inode)->i_dtime = CURRENT_TIME;*/ |
840 | lock_kernel(); | |
841 | mark_inode_dirty(inode); | |
842 | ufs_update_inode(inode, IS_SYNC(inode)); | |
843 | inode->i_size = 0; | |
844 | if (inode->i_blocks) | |
845 | ufs_truncate (inode); | |
846 | ufs_free_inode (inode); | |
847 | unlock_kernel(); | |
848 | } |