fs/affs: fix casting in printed messages
[deliverable/linux.git] / fs / affs / file.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/affs/file.c
3 *
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
5 *
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
7 *
8 * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
9 *
10 * (C) 1991 Linus Torvalds - minix filesystem
11 *
12 * affs regular file handling primitives
13 */
14
9abb4083 15#include <linux/aio.h>
1da177e4
LT
16#include "affs.h"
17
1da177e4 18static struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext);
1da177e4
LT
19
20static int
21affs_file_open(struct inode *inode, struct file *filp)
22{
9606d9aa 23 pr_debug("open(%lu,%d)\n",
dca3c336
RZ
24 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
25 atomic_inc(&AFFS_I(inode)->i_opencnt);
1da177e4
LT
26 return 0;
27}
28
29static int
30affs_file_release(struct inode *inode, struct file *filp)
31{
9606d9aa 32 pr_debug("release(%lu, %d)\n",
dca3c336
RZ
33 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
34
35 if (atomic_dec_and_test(&AFFS_I(inode)->i_opencnt)) {
36 mutex_lock(&inode->i_mutex);
37 if (inode->i_size != AFFS_I(inode)->mmu_private)
38 affs_truncate(inode);
1da177e4 39 affs_free_prealloc(inode);
dca3c336
RZ
40 mutex_unlock(&inode->i_mutex);
41 }
1da177e4
LT
42
43 return 0;
44}
45
46static int
47affs_grow_extcache(struct inode *inode, u32 lc_idx)
48{
49 struct super_block *sb = inode->i_sb;
50 struct buffer_head *bh;
51 u32 lc_max;
52 int i, j, key;
53
54 if (!AFFS_I(inode)->i_lc) {
55 char *ptr = (char *)get_zeroed_page(GFP_NOFS);
56 if (!ptr)
57 return -ENOMEM;
58 AFFS_I(inode)->i_lc = (u32 *)ptr;
59 AFFS_I(inode)->i_ac = (struct affs_ext_key *)(ptr + AFFS_CACHE_SIZE / 2);
60 }
61
62 lc_max = AFFS_LC_SIZE << AFFS_I(inode)->i_lc_shift;
63
64 if (AFFS_I(inode)->i_extcnt > lc_max) {
65 u32 lc_shift, lc_mask, tmp, off;
66
67 /* need to recalculate linear cache, start from old size */
68 lc_shift = AFFS_I(inode)->i_lc_shift;
69 tmp = (AFFS_I(inode)->i_extcnt / AFFS_LC_SIZE) >> lc_shift;
70 for (; tmp; tmp >>= 1)
71 lc_shift++;
72 lc_mask = (1 << lc_shift) - 1;
73
74 /* fix idx and old size to new shift */
75 lc_idx >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
76 AFFS_I(inode)->i_lc_size >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
77
78 /* first shrink old cache to make more space */
79 off = 1 << (lc_shift - AFFS_I(inode)->i_lc_shift);
80 for (i = 1, j = off; j < AFFS_LC_SIZE; i++, j += off)
81 AFFS_I(inode)->i_ac[i] = AFFS_I(inode)->i_ac[j];
82
83 AFFS_I(inode)->i_lc_shift = lc_shift;
84 AFFS_I(inode)->i_lc_mask = lc_mask;
85 }
86
87 /* fill cache to the needed index */
88 i = AFFS_I(inode)->i_lc_size;
89 AFFS_I(inode)->i_lc_size = lc_idx + 1;
90 for (; i <= lc_idx; i++) {
91 if (!i) {
92 AFFS_I(inode)->i_lc[0] = inode->i_ino;
93 continue;
94 }
95 key = AFFS_I(inode)->i_lc[i - 1];
96 j = AFFS_I(inode)->i_lc_mask + 1;
97 // unlock cache
98 for (; j > 0; j--) {
99 bh = affs_bread(sb, key);
100 if (!bh)
101 goto err;
102 key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
103 affs_brelse(bh);
104 }
105 // lock cache
106 AFFS_I(inode)->i_lc[i] = key;
107 }
108
109 return 0;
110
111err:
112 // lock cache
113 return -EIO;
114}
115
116static struct buffer_head *
117affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext)
118{
119 struct super_block *sb = inode->i_sb;
120 struct buffer_head *new_bh;
121 u32 blocknr, tmp;
122
123 blocknr = affs_alloc_block(inode, bh->b_blocknr);
124 if (!blocknr)
125 return ERR_PTR(-ENOSPC);
126
127 new_bh = affs_getzeroblk(sb, blocknr);
128 if (!new_bh) {
129 affs_free_block(sb, blocknr);
130 return ERR_PTR(-EIO);
131 }
132
133 AFFS_HEAD(new_bh)->ptype = cpu_to_be32(T_LIST);
134 AFFS_HEAD(new_bh)->key = cpu_to_be32(blocknr);
135 AFFS_TAIL(sb, new_bh)->stype = cpu_to_be32(ST_FILE);
136 AFFS_TAIL(sb, new_bh)->parent = cpu_to_be32(inode->i_ino);
137 affs_fix_checksum(sb, new_bh);
138
139 mark_buffer_dirty_inode(new_bh, inode);
140
141 tmp = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
142 if (tmp)
143 affs_warning(sb, "alloc_ext", "previous extension set (%x)", tmp);
144 AFFS_TAIL(sb, bh)->extension = cpu_to_be32(blocknr);
145 affs_adjust_checksum(bh, blocknr - tmp);
146 mark_buffer_dirty_inode(bh, inode);
147
148 AFFS_I(inode)->i_extcnt++;
149 mark_inode_dirty(inode);
150
151 return new_bh;
152}
153
154static inline struct buffer_head *
155affs_get_extblock(struct inode *inode, u32 ext)
156{
157 /* inline the simplest case: same extended block as last time */
158 struct buffer_head *bh = AFFS_I(inode)->i_ext_bh;
159 if (ext == AFFS_I(inode)->i_ext_last)
dca3c336 160 get_bh(bh);
1da177e4
LT
161 else
162 /* we have to do more (not inlined) */
163 bh = affs_get_extblock_slow(inode, ext);
164
165 return bh;
166}
167
168static struct buffer_head *
169affs_get_extblock_slow(struct inode *inode, u32 ext)
170{
171 struct super_block *sb = inode->i_sb;
172 struct buffer_head *bh;
173 u32 ext_key;
174 u32 lc_idx, lc_off, ac_idx;
175 u32 tmp, idx;
176
177 if (ext == AFFS_I(inode)->i_ext_last + 1) {
178 /* read the next extended block from the current one */
179 bh = AFFS_I(inode)->i_ext_bh;
180 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
181 if (ext < AFFS_I(inode)->i_extcnt)
182 goto read_ext;
183 if (ext > AFFS_I(inode)->i_extcnt)
184 BUG();
185 bh = affs_alloc_extblock(inode, bh, ext);
186 if (IS_ERR(bh))
187 return bh;
188 goto store_ext;
189 }
190
191 if (ext == 0) {
192 /* we seek back to the file header block */
193 ext_key = inode->i_ino;
194 goto read_ext;
195 }
196
197 if (ext >= AFFS_I(inode)->i_extcnt) {
198 struct buffer_head *prev_bh;
199
200 /* allocate a new extended block */
201 if (ext > AFFS_I(inode)->i_extcnt)
202 BUG();
203
204 /* get previous extended block */
205 prev_bh = affs_get_extblock(inode, ext - 1);
206 if (IS_ERR(prev_bh))
207 return prev_bh;
208 bh = affs_alloc_extblock(inode, prev_bh, ext);
209 affs_brelse(prev_bh);
210 if (IS_ERR(bh))
211 return bh;
212 goto store_ext;
213 }
214
215again:
216 /* check if there is an extended cache and whether it's large enough */
217 lc_idx = ext >> AFFS_I(inode)->i_lc_shift;
218 lc_off = ext & AFFS_I(inode)->i_lc_mask;
219
220 if (lc_idx >= AFFS_I(inode)->i_lc_size) {
221 int err;
222
223 err = affs_grow_extcache(inode, lc_idx);
224 if (err)
225 return ERR_PTR(err);
226 goto again;
227 }
228
229 /* every n'th key we find in the linear cache */
230 if (!lc_off) {
231 ext_key = AFFS_I(inode)->i_lc[lc_idx];
232 goto read_ext;
233 }
234
235 /* maybe it's still in the associative cache */
236 ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK;
237 if (AFFS_I(inode)->i_ac[ac_idx].ext == ext) {
238 ext_key = AFFS_I(inode)->i_ac[ac_idx].key;
239 goto read_ext;
240 }
241
242 /* try to find one of the previous extended blocks */
243 tmp = ext;
244 idx = ac_idx;
245 while (--tmp, --lc_off > 0) {
246 idx = (idx - 1) & AFFS_AC_MASK;
247 if (AFFS_I(inode)->i_ac[idx].ext == tmp) {
248 ext_key = AFFS_I(inode)->i_ac[idx].key;
249 goto find_ext;
250 }
251 }
252
253 /* fall back to the linear cache */
254 ext_key = AFFS_I(inode)->i_lc[lc_idx];
255find_ext:
256 /* read all extended blocks until we find the one we need */
257 //unlock cache
258 do {
259 bh = affs_bread(sb, ext_key);
260 if (!bh)
261 goto err_bread;
262 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
263 affs_brelse(bh);
264 tmp++;
265 } while (tmp < ext);
266 //lock cache
267
268 /* store it in the associative cache */
269 // recalculate ac_idx?
270 AFFS_I(inode)->i_ac[ac_idx].ext = ext;
271 AFFS_I(inode)->i_ac[ac_idx].key = ext_key;
272
273read_ext:
274 /* finally read the right extended block */
275 //unlock cache
276 bh = affs_bread(sb, ext_key);
277 if (!bh)
278 goto err_bread;
279 //lock cache
280
281store_ext:
282 /* release old cached extended block and store the new one */
283 affs_brelse(AFFS_I(inode)->i_ext_bh);
284 AFFS_I(inode)->i_ext_last = ext;
285 AFFS_I(inode)->i_ext_bh = bh;
dca3c336 286 get_bh(bh);
1da177e4
LT
287
288 return bh;
289
290err_bread:
291 affs_brelse(bh);
292 return ERR_PTR(-EIO);
293}
294
295static int
296affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
297{
298 struct super_block *sb = inode->i_sb;
299 struct buffer_head *ext_bh;
300 u32 ext;
301
08fe100d
GU
302 pr_debug("%s(%lu, %llu)\n", __func__, inode->i_ino,
303 (unsigned long long)block);
1da177e4 304
8d4b6900 305 BUG_ON(block > (sector_t)0x7fffffffUL);
1da177e4
LT
306
307 if (block >= AFFS_I(inode)->i_blkcnt) {
308 if (block > AFFS_I(inode)->i_blkcnt || !create)
309 goto err_big;
310 } else
311 create = 0;
312
313 //lock cache
314 affs_lock_ext(inode);
315
316 ext = (u32)block / AFFS_SB(sb)->s_hashsize;
317 block -= ext * AFFS_SB(sb)->s_hashsize;
318 ext_bh = affs_get_extblock(inode, ext);
319 if (IS_ERR(ext_bh))
320 goto err_ext;
321 map_bh(bh_result, sb, (sector_t)be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block)));
322
323 if (create) {
324 u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr);
325 if (!blocknr)
326 goto err_alloc;
327 set_buffer_new(bh_result);
328 AFFS_I(inode)->mmu_private += AFFS_SB(sb)->s_data_blksize;
329 AFFS_I(inode)->i_blkcnt++;
330
331 /* store new block */
332 if (bh_result->b_blocknr)
08fe100d
GU
333 affs_warning(sb, "get_block",
334 "block already set (%llx)",
335 (unsigned long long)bh_result->b_blocknr);
1da177e4
LT
336 AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
337 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
338 affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
339 bh_result->b_blocknr = blocknr;
340
341 if (!block) {
342 /* insert first block into header block */
343 u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
344 if (tmp)
345 affs_warning(sb, "get_block", "first block already set (%d)", tmp);
346 AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
347 affs_adjust_checksum(ext_bh, blocknr - tmp);
348 }
349 }
350
351 affs_brelse(ext_bh);
352 //unlock cache
353 affs_unlock_ext(inode);
354 return 0;
355
356err_big:
08fe100d
GU
357 affs_error(inode->i_sb, "get_block", "strange block request %llu",
358 (unsigned long long)block);
1da177e4
LT
359 return -EIO;
360err_ext:
361 // unlock cache
362 affs_unlock_ext(inode);
363 return PTR_ERR(ext_bh);
364err_alloc:
365 brelse(ext_bh);
366 clear_buffer_mapped(bh_result);
367 bh_result->b_bdev = NULL;
368 // unlock cache
369 affs_unlock_ext(inode);
370 return -ENOSPC;
371}
372
373static int affs_writepage(struct page *page, struct writeback_control *wbc)
374{
375 return block_write_full_page(page, affs_get_block, wbc);
376}
f2b6a16e 377
1da177e4
LT
378static int affs_readpage(struct file *file, struct page *page)
379{
380 return block_read_full_page(page, affs_get_block);
381}
f2b6a16e 382
1dc1834f
MS
383static void affs_write_failed(struct address_space *mapping, loff_t to)
384{
385 struct inode *inode = mapping->host;
386
387 if (to > inode->i_size) {
7caef267 388 truncate_pagecache(inode, inode->i_size);
1dc1834f
MS
389 affs_truncate(inode);
390 }
391}
392
9abb4083
FF
393static ssize_t
394affs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter,
395 loff_t offset)
396{
397 struct file *file = iocb->ki_filp;
398 struct address_space *mapping = file->f_mapping;
399 struct inode *inode = mapping->host;
400 size_t count = iov_iter_count(iter);
401 ssize_t ret;
402
403 ret = blockdev_direct_IO(rw, iocb, inode, iter, offset, affs_get_block);
404 if (ret < 0 && (rw & WRITE))
405 affs_write_failed(mapping, offset + count);
406 return ret;
407}
408
f2b6a16e
NP
409static int affs_write_begin(struct file *file, struct address_space *mapping,
410 loff_t pos, unsigned len, unsigned flags,
411 struct page **pagep, void **fsdata)
1da177e4 412{
282dc178
CH
413 int ret;
414
f2b6a16e 415 *pagep = NULL;
282dc178 416 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
f2b6a16e
NP
417 affs_get_block,
418 &AFFS_I(mapping->host)->mmu_private);
1dc1834f
MS
419 if (unlikely(ret))
420 affs_write_failed(mapping, pos + len);
282dc178
CH
421
422 return ret;
1da177e4 423}
f2b6a16e 424
1da177e4
LT
425static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
426{
427 return generic_block_bmap(mapping,block,affs_get_block);
428}
f2b6a16e 429
f5e54d6e 430const struct address_space_operations affs_aops = {
1da177e4
LT
431 .readpage = affs_readpage,
432 .writepage = affs_writepage,
f2b6a16e
NP
433 .write_begin = affs_write_begin,
434 .write_end = generic_write_end,
9abb4083 435 .direct_IO = affs_direct_IO,
1da177e4
LT
436 .bmap = _affs_bmap
437};
438
439static inline struct buffer_head *
440affs_bread_ino(struct inode *inode, int block, int create)
441{
442 struct buffer_head *bh, tmp_bh;
443 int err;
444
445 tmp_bh.b_state = 0;
446 err = affs_get_block(inode, block, &tmp_bh, create);
447 if (!err) {
448 bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
449 if (bh) {
450 bh->b_state |= tmp_bh.b_state;
451 return bh;
452 }
453 err = -EIO;
454 }
455 return ERR_PTR(err);
456}
457
458static inline struct buffer_head *
459affs_getzeroblk_ino(struct inode *inode, int block)
460{
461 struct buffer_head *bh, tmp_bh;
462 int err;
463
464 tmp_bh.b_state = 0;
465 err = affs_get_block(inode, block, &tmp_bh, 1);
466 if (!err) {
467 bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
468 if (bh) {
469 bh->b_state |= tmp_bh.b_state;
470 return bh;
471 }
472 err = -EIO;
473 }
474 return ERR_PTR(err);
475}
476
477static inline struct buffer_head *
478affs_getemptyblk_ino(struct inode *inode, int block)
479{
480 struct buffer_head *bh, tmp_bh;
481 int err;
482
483 tmp_bh.b_state = 0;
484 err = affs_get_block(inode, block, &tmp_bh, 1);
485 if (!err) {
486 bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
487 if (bh) {
488 bh->b_state |= tmp_bh.b_state;
489 return bh;
490 }
491 err = -EIO;
492 }
493 return ERR_PTR(err);
494}
495
1da177e4 496static int
0c89d670 497affs_do_readpage_ofs(struct page *page, unsigned to)
1da177e4
LT
498{
499 struct inode *inode = page->mapping->host;
500 struct super_block *sb = inode->i_sb;
501 struct buffer_head *bh;
502 char *data;
0c89d670 503 unsigned pos = 0;
1da177e4
LT
504 u32 bidx, boff, bsize;
505 u32 tmp;
506
08fe100d 507 pr_debug("%s(%lu, %ld, 0, %d)\n", __func__, inode->i_ino,
0c89d670
FF
508 page->index, to);
509 BUG_ON(to > PAGE_CACHE_SIZE);
1da177e4
LT
510 kmap(page);
511 data = page_address(page);
512 bsize = AFFS_SB(sb)->s_data_blksize;
0c89d670 513 tmp = page->index << PAGE_CACHE_SHIFT;
1da177e4
LT
514 bidx = tmp / bsize;
515 boff = tmp % bsize;
516
0c89d670 517 while (pos < to) {
1da177e4
LT
518 bh = affs_bread_ino(inode, bidx, 0);
519 if (IS_ERR(bh))
520 return PTR_ERR(bh);
0c89d670
FF
521 tmp = min(bsize - boff, to - pos);
522 BUG_ON(pos + tmp > to || tmp > bsize);
523 memcpy(data + pos, AFFS_DATA(bh) + boff, tmp);
1da177e4
LT
524 affs_brelse(bh);
525 bidx++;
0c89d670 526 pos += tmp;
1da177e4
LT
527 boff = 0;
528 }
529 flush_dcache_page(page);
530 kunmap(page);
531 return 0;
532}
533
534static int
535affs_extent_file_ofs(struct inode *inode, u32 newsize)
536{
537 struct super_block *sb = inode->i_sb;
538 struct buffer_head *bh, *prev_bh;
539 u32 bidx, boff;
540 u32 size, bsize;
541 u32 tmp;
542
08fe100d 543 pr_debug("%s(%lu, %d)\n", __func__, inode->i_ino, newsize);
1da177e4
LT
544 bsize = AFFS_SB(sb)->s_data_blksize;
545 bh = NULL;
546 size = AFFS_I(inode)->mmu_private;
547 bidx = size / bsize;
548 boff = size % bsize;
549 if (boff) {
550 bh = affs_bread_ino(inode, bidx, 0);
551 if (IS_ERR(bh))
552 return PTR_ERR(bh);
553 tmp = min(bsize - boff, newsize - size);
8d4b6900 554 BUG_ON(boff + tmp > bsize || tmp > bsize);
1da177e4 555 memset(AFFS_DATA(bh) + boff, 0, tmp);
6369a4ab 556 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
1da177e4
LT
557 affs_fix_checksum(sb, bh);
558 mark_buffer_dirty_inode(bh, inode);
559 size += tmp;
560 bidx++;
561 } else if (bidx) {
562 bh = affs_bread_ino(inode, bidx - 1, 0);
563 if (IS_ERR(bh))
564 return PTR_ERR(bh);
565 }
566
567 while (size < newsize) {
568 prev_bh = bh;
569 bh = affs_getzeroblk_ino(inode, bidx);
570 if (IS_ERR(bh))
571 goto out;
572 tmp = min(bsize, newsize - size);
8d4b6900 573 BUG_ON(tmp > bsize);
1da177e4
LT
574 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
575 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
576 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
577 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
578 affs_fix_checksum(sb, bh);
579 bh->b_state &= ~(1UL << BH_New);
580 mark_buffer_dirty_inode(bh, inode);
581 if (prev_bh) {
73516ace
FF
582 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
583
584 if (tmp_next)
585 affs_warning(sb, "extent_file_ofs",
586 "next block already set for %d (%d)",
587 bidx, tmp_next);
1da177e4 588 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
73516ace 589 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
1da177e4
LT
590 mark_buffer_dirty_inode(prev_bh, inode);
591 affs_brelse(prev_bh);
592 }
593 size += bsize;
594 bidx++;
595 }
596 affs_brelse(bh);
597 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
598 return 0;
599
600out:
601 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
602 return PTR_ERR(bh);
603}
604
605static int
606affs_readpage_ofs(struct file *file, struct page *page)
607{
608 struct inode *inode = page->mapping->host;
609 u32 to;
610 int err;
611
08fe100d 612 pr_debug("%s(%lu, %ld)\n", __func__, inode->i_ino, page->index);
1da177e4
LT
613 to = PAGE_CACHE_SIZE;
614 if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) {
615 to = inode->i_size & ~PAGE_CACHE_MASK;
616 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
617 }
618
0c89d670 619 err = affs_do_readpage_ofs(page, to);
1da177e4
LT
620 if (!err)
621 SetPageUptodate(page);
622 unlock_page(page);
623 return err;
624}
625
f2b6a16e
NP
626static int affs_write_begin_ofs(struct file *file, struct address_space *mapping,
627 loff_t pos, unsigned len, unsigned flags,
628 struct page **pagep, void **fsdata)
1da177e4 629{
f2b6a16e
NP
630 struct inode *inode = mapping->host;
631 struct page *page;
632 pgoff_t index;
1da177e4
LT
633 int err = 0;
634
08fe100d
GU
635 pr_debug("%s(%lu, %llu, %llu)\n", __func__, inode->i_ino, pos,
636 pos + len);
f2b6a16e
NP
637 if (pos > AFFS_I(inode)->mmu_private) {
638 /* XXX: this probably leaves a too-big i_size in case of
639 * failure. Should really be updating i_size at write_end time
640 */
641 err = affs_extent_file_ofs(inode, pos);
1da177e4
LT
642 if (err)
643 return err;
644 }
f2b6a16e
NP
645
646 index = pos >> PAGE_CACHE_SHIFT;
54566b2c 647 page = grab_cache_page_write_begin(mapping, index, flags);
f2b6a16e
NP
648 if (!page)
649 return -ENOMEM;
650 *pagep = page;
1da177e4
LT
651
652 if (PageUptodate(page))
653 return 0;
654
f2b6a16e 655 /* XXX: inefficient but safe in the face of short writes */
0c89d670 656 err = affs_do_readpage_ofs(page, PAGE_CACHE_SIZE);
f2b6a16e
NP
657 if (err) {
658 unlock_page(page);
659 page_cache_release(page);
1da177e4
LT
660 }
661 return err;
662}
663
f2b6a16e
NP
664static int affs_write_end_ofs(struct file *file, struct address_space *mapping,
665 loff_t pos, unsigned len, unsigned copied,
666 struct page *page, void *fsdata)
1da177e4 667{
f2b6a16e 668 struct inode *inode = mapping->host;
1da177e4
LT
669 struct super_block *sb = inode->i_sb;
670 struct buffer_head *bh, *prev_bh;
671 char *data;
672 u32 bidx, boff, bsize;
f2b6a16e 673 unsigned from, to;
1da177e4
LT
674 u32 tmp;
675 int written;
676
f2b6a16e
NP
677 from = pos & (PAGE_CACHE_SIZE - 1);
678 to = pos + len;
679 /*
680 * XXX: not sure if this can handle short copies (len < copied), but
681 * we don't have to, because the page should always be uptodate here,
682 * due to write_begin.
683 */
684
08fe100d
GU
685 pr_debug("%s(%lu, %llu, %llu)\n", __func__, inode->i_ino, pos,
686 pos + len);
1da177e4
LT
687 bsize = AFFS_SB(sb)->s_data_blksize;
688 data = page_address(page);
689
690 bh = NULL;
691 written = 0;
692 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
693 bidx = tmp / bsize;
694 boff = tmp % bsize;
695 if (boff) {
696 bh = affs_bread_ino(inode, bidx, 0);
697 if (IS_ERR(bh))
698 return PTR_ERR(bh);
699 tmp = min(bsize - boff, to - from);
8d4b6900 700 BUG_ON(boff + tmp > bsize || tmp > bsize);
1da177e4 701 memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
6369a4ab 702 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
1da177e4
LT
703 affs_fix_checksum(sb, bh);
704 mark_buffer_dirty_inode(bh, inode);
705 written += tmp;
706 from += tmp;
707 bidx++;
708 } else if (bidx) {
709 bh = affs_bread_ino(inode, bidx - 1, 0);
710 if (IS_ERR(bh))
711 return PTR_ERR(bh);
712 }
713 while (from + bsize <= to) {
714 prev_bh = bh;
715 bh = affs_getemptyblk_ino(inode, bidx);
716 if (IS_ERR(bh))
717 goto out;
718 memcpy(AFFS_DATA(bh), data + from, bsize);
719 if (buffer_new(bh)) {
720 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
721 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
722 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
723 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize);
724 AFFS_DATA_HEAD(bh)->next = 0;
725 bh->b_state &= ~(1UL << BH_New);
726 if (prev_bh) {
73516ace
FF
727 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
728
729 if (tmp_next)
730 affs_warning(sb, "commit_write_ofs",
731 "next block already set for %d (%d)",
732 bidx, tmp_next);
1da177e4 733 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
73516ace 734 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
1da177e4
LT
735 mark_buffer_dirty_inode(prev_bh, inode);
736 }
737 }
738 affs_brelse(prev_bh);
739 affs_fix_checksum(sb, bh);
740 mark_buffer_dirty_inode(bh, inode);
741 written += bsize;
742 from += bsize;
743 bidx++;
744 }
745 if (from < to) {
746 prev_bh = bh;
747 bh = affs_bread_ino(inode, bidx, 1);
748 if (IS_ERR(bh))
749 goto out;
750 tmp = min(bsize, to - from);
8d4b6900 751 BUG_ON(tmp > bsize);
1da177e4
LT
752 memcpy(AFFS_DATA(bh), data + from, tmp);
753 if (buffer_new(bh)) {
754 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
755 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
756 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
757 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
758 AFFS_DATA_HEAD(bh)->next = 0;
759 bh->b_state &= ~(1UL << BH_New);
760 if (prev_bh) {
73516ace
FF
761 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
762
763 if (tmp_next)
764 affs_warning(sb, "commit_write_ofs",
765 "next block already set for %d (%d)",
766 bidx, tmp_next);
1da177e4 767 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
73516ace 768 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
1da177e4
LT
769 mark_buffer_dirty_inode(prev_bh, inode);
770 }
771 } else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp)
772 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
773 affs_brelse(prev_bh);
774 affs_fix_checksum(sb, bh);
775 mark_buffer_dirty_inode(bh, inode);
776 written += tmp;
777 from += tmp;
778 bidx++;
779 }
780 SetPageUptodate(page);
781
782done:
783 affs_brelse(bh);
784 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
785 if (tmp > inode->i_size)
786 inode->i_size = AFFS_I(inode)->mmu_private = tmp;
787
f2b6a16e
NP
788 unlock_page(page);
789 page_cache_release(page);
790
1da177e4
LT
791 return written;
792
793out:
794 bh = prev_bh;
795 if (!written)
796 written = PTR_ERR(bh);
797 goto done;
798}
799
f5e54d6e 800const struct address_space_operations affs_aops_ofs = {
1da177e4
LT
801 .readpage = affs_readpage_ofs,
802 //.writepage = affs_writepage_ofs,
f2b6a16e
NP
803 .write_begin = affs_write_begin_ofs,
804 .write_end = affs_write_end_ofs
1da177e4
LT
805};
806
807/* Free any preallocated blocks. */
808
809void
810affs_free_prealloc(struct inode *inode)
811{
812 struct super_block *sb = inode->i_sb;
813
9606d9aa 814 pr_debug("free_prealloc(ino=%lu)\n", inode->i_ino);
1da177e4
LT
815
816 while (AFFS_I(inode)->i_pa_cnt) {
817 AFFS_I(inode)->i_pa_cnt--;
818 affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc);
819 }
820}
821
822/* Truncate (or enlarge) a file to the requested size. */
823
824void
825affs_truncate(struct inode *inode)
826{
827 struct super_block *sb = inode->i_sb;
828 u32 ext, ext_key;
829 u32 last_blk, blkcnt, blk;
830 u32 size;
831 struct buffer_head *ext_bh;
832 int i;
833
08fe100d
GU
834 pr_debug("truncate(inode=%lu, oldsize=%llu, newsize=%llu)\n",
835 inode->i_ino, AFFS_I(inode)->mmu_private, inode->i_size);
1da177e4
LT
836
837 last_blk = 0;
838 ext = 0;
839 if (inode->i_size) {
840 last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize;
841 ext = last_blk / AFFS_SB(sb)->s_hashsize;
842 }
843
844 if (inode->i_size > AFFS_I(inode)->mmu_private) {
845 struct address_space *mapping = inode->i_mapping;
846 struct page *page;
f2b6a16e 847 void *fsdata;
73516ace 848 loff_t isize = inode->i_size;
1da177e4
LT
849 int res;
850
73516ace 851 res = mapping->a_ops->write_begin(NULL, mapping, isize, 0, 0, &page, &fsdata);
1da177e4 852 if (!res)
73516ace 853 res = mapping->a_ops->write_end(NULL, mapping, isize, 0, 0, page, fsdata);
dca3c336
RZ
854 else
855 inode->i_size = AFFS_I(inode)->mmu_private;
1da177e4
LT
856 mark_inode_dirty(inode);
857 return;
858 } else if (inode->i_size == AFFS_I(inode)->mmu_private)
859 return;
860
861 // lock cache
862 ext_bh = affs_get_extblock(inode, ext);
863 if (IS_ERR(ext_bh)) {
1ee54b09
FF
864 affs_warning(sb, "truncate",
865 "unexpected read error for ext block %u (%ld)",
08fe100d 866 ext, PTR_ERR(ext_bh));
1da177e4
LT
867 return;
868 }
869 if (AFFS_I(inode)->i_lc) {
870 /* clear linear cache */
871 i = (ext + 1) >> AFFS_I(inode)->i_lc_shift;
872 if (AFFS_I(inode)->i_lc_size > i) {
873 AFFS_I(inode)->i_lc_size = i;
874 for (; i < AFFS_LC_SIZE; i++)
875 AFFS_I(inode)->i_lc[i] = 0;
876 }
877 /* clear associative cache */
878 for (i = 0; i < AFFS_AC_SIZE; i++)
879 if (AFFS_I(inode)->i_ac[i].ext >= ext)
880 AFFS_I(inode)->i_ac[i].ext = 0;
881 }
882 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
883
884 blkcnt = AFFS_I(inode)->i_blkcnt;
885 i = 0;
886 blk = last_blk;
887 if (inode->i_size) {
888 i = last_blk % AFFS_SB(sb)->s_hashsize + 1;
889 blk++;
890 } else
891 AFFS_HEAD(ext_bh)->first_data = 0;
dca3c336 892 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i);
1da177e4
LT
893 size = AFFS_SB(sb)->s_hashsize;
894 if (size > blkcnt - blk + i)
895 size = blkcnt - blk + i;
896 for (; i < size; i++, blk++) {
897 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
898 AFFS_BLOCK(sb, ext_bh, i) = 0;
899 }
900 AFFS_TAIL(sb, ext_bh)->extension = 0;
901 affs_fix_checksum(sb, ext_bh);
902 mark_buffer_dirty_inode(ext_bh, inode);
903 affs_brelse(ext_bh);
904
905 if (inode->i_size) {
906 AFFS_I(inode)->i_blkcnt = last_blk + 1;
907 AFFS_I(inode)->i_extcnt = ext + 1;
908 if (AFFS_SB(sb)->s_flags & SF_OFS) {
909 struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
910 u32 tmp;
0e45b67d 911 if (IS_ERR(bh)) {
1ee54b09
FF
912 affs_warning(sb, "truncate",
913 "unexpected read error for last block %u (%ld)",
08fe100d 914 ext, PTR_ERR(bh));
1da177e4
LT
915 return;
916 }
917 tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
918 AFFS_DATA_HEAD(bh)->next = 0;
919 affs_adjust_checksum(bh, -tmp);
920 affs_brelse(bh);
921 }
922 } else {
923 AFFS_I(inode)->i_blkcnt = 0;
924 AFFS_I(inode)->i_extcnt = 1;
925 }
926 AFFS_I(inode)->mmu_private = inode->i_size;
927 // unlock cache
928
929 while (ext_key) {
930 ext_bh = affs_bread(sb, ext_key);
931 size = AFFS_SB(sb)->s_hashsize;
932 if (size > blkcnt - blk)
933 size = blkcnt - blk;
934 for (i = 0; i < size; i++, blk++)
935 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
936 affs_free_block(sb, ext_key);
937 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
938 affs_brelse(ext_bh);
939 }
940 affs_free_prealloc(inode);
941}
c4758795 942
02c24a82 943int affs_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
c4758795 944{
7ea80859 945 struct inode *inode = filp->f_mapping->host;
c4758795
AV
946 int ret, err;
947
02c24a82
JB
948 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
949 if (err)
950 return err;
951
952 mutex_lock(&inode->i_mutex);
c4758795
AV
953 ret = write_inode_now(inode, 0);
954 err = sync_blockdev(inode->i_sb->s_bdev);
955 if (!ret)
956 ret = err;
02c24a82 957 mutex_unlock(&inode->i_mutex);
c4758795
AV
958 return ret;
959}
7633978b
FF
960const struct file_operations affs_file_operations = {
961 .llseek = generic_file_llseek,
962 .read = new_sync_read,
963 .read_iter = generic_file_read_iter,
964 .write = new_sync_write,
965 .write_iter = generic_file_write_iter,
966 .mmap = generic_file_mmap,
967 .open = affs_file_open,
968 .release = affs_file_release,
969 .fsync = affs_file_fsync,
970 .splice_read = generic_file_splice_read,
971};
972
973const struct inode_operations affs_file_inode_operations = {
974 .setattr = affs_notify_change,
975};
This page took 0.789619 seconds and 5 git commands to generate.