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