drm: bridge/dw_hdmi: clear i2cmphy_stat0 reg in hdmi_phy_wait_i2c_done
[linux-2.6/btrfs-unstable.git] / fs / affs / file.c
blob8faa6593ca6defe4ab77c169f3fe9673b84a9ec3
1 /*
2 * linux/fs/affs/file.c
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
8 * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
10 * (C) 1991 Linus Torvalds - minix filesystem
12 * affs regular file handling primitives
15 #include <linux/aio.h>
16 #include "affs.h"
18 static struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext);
20 static int
21 affs_file_open(struct inode *inode, struct file *filp)
23 pr_debug("open(%lu,%d)\n",
24 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
25 atomic_inc(&AFFS_I(inode)->i_opencnt);
26 return 0;
29 static int
30 affs_file_release(struct inode *inode, struct file *filp)
32 pr_debug("release(%lu, %d)\n",
33 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
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);
39 affs_free_prealloc(inode);
40 mutex_unlock(&inode->i_mutex);
43 return 0;
46 static int
47 affs_grow_extcache(struct inode *inode, u32 lc_idx)
49 struct super_block *sb = inode->i_sb;
50 struct buffer_head *bh;
51 u32 lc_max;
52 int i, j, key;
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);
62 lc_max = AFFS_LC_SIZE << AFFS_I(inode)->i_lc_shift;
64 if (AFFS_I(inode)->i_extcnt > lc_max) {
65 u32 lc_shift, lc_mask, tmp, off;
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;
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);
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];
83 AFFS_I(inode)->i_lc_shift = lc_shift;
84 AFFS_I(inode)->i_lc_mask = lc_mask;
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;
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);
105 // lock cache
106 AFFS_I(inode)->i_lc[i] = key;
109 return 0;
111 err:
112 // lock cache
113 return -EIO;
116 static struct buffer_head *
117 affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext)
119 struct super_block *sb = inode->i_sb;
120 struct buffer_head *new_bh;
121 u32 blocknr, tmp;
123 blocknr = affs_alloc_block(inode, bh->b_blocknr);
124 if (!blocknr)
125 return ERR_PTR(-ENOSPC);
127 new_bh = affs_getzeroblk(sb, blocknr);
128 if (!new_bh) {
129 affs_free_block(sb, blocknr);
130 return ERR_PTR(-EIO);
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);
139 mark_buffer_dirty_inode(new_bh, inode);
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);
148 AFFS_I(inode)->i_extcnt++;
149 mark_inode_dirty(inode);
151 return new_bh;
154 static inline struct buffer_head *
155 affs_get_extblock(struct inode *inode, u32 ext)
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)
160 get_bh(bh);
161 else
162 /* we have to do more (not inlined) */
163 bh = affs_get_extblock_slow(inode, ext);
165 return bh;
168 static struct buffer_head *
169 affs_get_extblock_slow(struct inode *inode, u32 ext)
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;
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;
191 if (ext == 0) {
192 /* we seek back to the file header block */
193 ext_key = inode->i_ino;
194 goto read_ext;
197 if (ext >= AFFS_I(inode)->i_extcnt) {
198 struct buffer_head *prev_bh;
200 /* allocate a new extended block */
201 if (ext > AFFS_I(inode)->i_extcnt)
202 BUG();
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;
215 again:
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;
220 if (lc_idx >= AFFS_I(inode)->i_lc_size) {
221 int err;
223 err = affs_grow_extcache(inode, lc_idx);
224 if (err)
225 return ERR_PTR(err);
226 goto again;
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;
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;
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;
253 /* fall back to the linear cache */
254 ext_key = AFFS_I(inode)->i_lc[lc_idx];
255 find_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
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;
273 read_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
281 store_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;
286 get_bh(bh);
288 return bh;
290 err_bread:
291 affs_brelse(bh);
292 return ERR_PTR(-EIO);
295 static int
296 affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
298 struct super_block *sb = inode->i_sb;
299 struct buffer_head *ext_bh;
300 u32 ext;
302 pr_debug("%s(%u, %lu)\n",
303 __func__, (u32)inode->i_ino, (unsigned long)block);
305 BUG_ON(block > (sector_t)0x7fffffffUL);
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;
313 //lock cache
314 affs_lock_ext(inode);
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)));
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++;
331 /* store new block */
332 if (bh_result->b_blocknr)
333 affs_warning(sb, "get_block", "block already set (%lx)",
334 (unsigned long)bh_result->b_blocknr);
335 AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
336 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
337 affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
338 bh_result->b_blocknr = blocknr;
340 if (!block) {
341 /* insert first block into header block */
342 u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
343 if (tmp)
344 affs_warning(sb, "get_block", "first block already set (%d)", tmp);
345 AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
346 affs_adjust_checksum(ext_bh, blocknr - tmp);
350 affs_brelse(ext_bh);
351 //unlock cache
352 affs_unlock_ext(inode);
353 return 0;
355 err_big:
356 affs_error(inode->i_sb, "get_block", "strange block request %d",
357 (int)block);
358 return -EIO;
359 err_ext:
360 // unlock cache
361 affs_unlock_ext(inode);
362 return PTR_ERR(ext_bh);
363 err_alloc:
364 brelse(ext_bh);
365 clear_buffer_mapped(bh_result);
366 bh_result->b_bdev = NULL;
367 // unlock cache
368 affs_unlock_ext(inode);
369 return -ENOSPC;
372 static int affs_writepage(struct page *page, struct writeback_control *wbc)
374 return block_write_full_page(page, affs_get_block, wbc);
377 static int affs_readpage(struct file *file, struct page *page)
379 return block_read_full_page(page, affs_get_block);
382 static void affs_write_failed(struct address_space *mapping, loff_t to)
384 struct inode *inode = mapping->host;
386 if (to > inode->i_size) {
387 truncate_pagecache(inode, inode->i_size);
388 affs_truncate(inode);
392 static ssize_t
393 affs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter,
394 loff_t offset)
396 struct file *file = iocb->ki_filp;
397 struct address_space *mapping = file->f_mapping;
398 struct inode *inode = mapping->host;
399 size_t count = iov_iter_count(iter);
400 ssize_t ret;
402 ret = blockdev_direct_IO(rw, iocb, inode, iter, offset, affs_get_block);
403 if (ret < 0 && (rw & WRITE))
404 affs_write_failed(mapping, offset + count);
405 return ret;
408 static int affs_write_begin(struct file *file, struct address_space *mapping,
409 loff_t pos, unsigned len, unsigned flags,
410 struct page **pagep, void **fsdata)
412 int ret;
414 *pagep = NULL;
415 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
416 affs_get_block,
417 &AFFS_I(mapping->host)->mmu_private);
418 if (unlikely(ret))
419 affs_write_failed(mapping, pos + len);
421 return ret;
424 static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
426 return generic_block_bmap(mapping,block,affs_get_block);
429 const struct address_space_operations affs_aops = {
430 .readpage = affs_readpage,
431 .writepage = affs_writepage,
432 .write_begin = affs_write_begin,
433 .write_end = generic_write_end,
434 .direct_IO = affs_direct_IO,
435 .bmap = _affs_bmap
438 static inline struct buffer_head *
439 affs_bread_ino(struct inode *inode, int block, int create)
441 struct buffer_head *bh, tmp_bh;
442 int err;
444 tmp_bh.b_state = 0;
445 err = affs_get_block(inode, block, &tmp_bh, create);
446 if (!err) {
447 bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
448 if (bh) {
449 bh->b_state |= tmp_bh.b_state;
450 return bh;
452 err = -EIO;
454 return ERR_PTR(err);
457 static inline struct buffer_head *
458 affs_getzeroblk_ino(struct inode *inode, int block)
460 struct buffer_head *bh, tmp_bh;
461 int err;
463 tmp_bh.b_state = 0;
464 err = affs_get_block(inode, block, &tmp_bh, 1);
465 if (!err) {
466 bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
467 if (bh) {
468 bh->b_state |= tmp_bh.b_state;
469 return bh;
471 err = -EIO;
473 return ERR_PTR(err);
476 static inline struct buffer_head *
477 affs_getemptyblk_ino(struct inode *inode, int block)
479 struct buffer_head *bh, tmp_bh;
480 int err;
482 tmp_bh.b_state = 0;
483 err = affs_get_block(inode, block, &tmp_bh, 1);
484 if (!err) {
485 bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
486 if (bh) {
487 bh->b_state |= tmp_bh.b_state;
488 return bh;
490 err = -EIO;
492 return ERR_PTR(err);
495 static int
496 affs_do_readpage_ofs(struct page *page, unsigned to)
498 struct inode *inode = page->mapping->host;
499 struct super_block *sb = inode->i_sb;
500 struct buffer_head *bh;
501 char *data;
502 unsigned pos = 0;
503 u32 bidx, boff, bsize;
504 u32 tmp;
506 pr_debug("%s(%u, %ld, 0, %d)\n", __func__, (u32)inode->i_ino,
507 page->index, to);
508 BUG_ON(to > PAGE_CACHE_SIZE);
509 kmap(page);
510 data = page_address(page);
511 bsize = AFFS_SB(sb)->s_data_blksize;
512 tmp = page->index << PAGE_CACHE_SHIFT;
513 bidx = tmp / bsize;
514 boff = tmp % bsize;
516 while (pos < to) {
517 bh = affs_bread_ino(inode, bidx, 0);
518 if (IS_ERR(bh))
519 return PTR_ERR(bh);
520 tmp = min(bsize - boff, to - pos);
521 BUG_ON(pos + tmp > to || tmp > bsize);
522 memcpy(data + pos, AFFS_DATA(bh) + boff, tmp);
523 affs_brelse(bh);
524 bidx++;
525 pos += tmp;
526 boff = 0;
528 flush_dcache_page(page);
529 kunmap(page);
530 return 0;
533 static int
534 affs_extent_file_ofs(struct inode *inode, u32 newsize)
536 struct super_block *sb = inode->i_sb;
537 struct buffer_head *bh, *prev_bh;
538 u32 bidx, boff;
539 u32 size, bsize;
540 u32 tmp;
542 pr_debug("%s(%u, %d)\n", __func__, (u32)inode->i_ino, newsize);
543 bsize = AFFS_SB(sb)->s_data_blksize;
544 bh = NULL;
545 size = AFFS_I(inode)->mmu_private;
546 bidx = size / bsize;
547 boff = size % bsize;
548 if (boff) {
549 bh = affs_bread_ino(inode, bidx, 0);
550 if (IS_ERR(bh))
551 return PTR_ERR(bh);
552 tmp = min(bsize - boff, newsize - size);
553 BUG_ON(boff + tmp > bsize || tmp > bsize);
554 memset(AFFS_DATA(bh) + boff, 0, tmp);
555 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
556 affs_fix_checksum(sb, bh);
557 mark_buffer_dirty_inode(bh, inode);
558 size += tmp;
559 bidx++;
560 } else if (bidx) {
561 bh = affs_bread_ino(inode, bidx - 1, 0);
562 if (IS_ERR(bh))
563 return PTR_ERR(bh);
566 while (size < newsize) {
567 prev_bh = bh;
568 bh = affs_getzeroblk_ino(inode, bidx);
569 if (IS_ERR(bh))
570 goto out;
571 tmp = min(bsize, newsize - size);
572 BUG_ON(tmp > bsize);
573 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
574 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
575 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
576 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
577 affs_fix_checksum(sb, bh);
578 bh->b_state &= ~(1UL << BH_New);
579 mark_buffer_dirty_inode(bh, inode);
580 if (prev_bh) {
581 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
583 if (tmp_next)
584 affs_warning(sb, "extent_file_ofs",
585 "next block already set for %d (%d)",
586 bidx, tmp_next);
587 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
588 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
589 mark_buffer_dirty_inode(prev_bh, inode);
590 affs_brelse(prev_bh);
592 size += bsize;
593 bidx++;
595 affs_brelse(bh);
596 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
597 return 0;
599 out:
600 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
601 return PTR_ERR(bh);
604 static int
605 affs_readpage_ofs(struct file *file, struct page *page)
607 struct inode *inode = page->mapping->host;
608 u32 to;
609 int err;
611 pr_debug("%s(%u, %ld)\n", __func__, (u32)inode->i_ino, page->index);
612 to = PAGE_CACHE_SIZE;
613 if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) {
614 to = inode->i_size & ~PAGE_CACHE_MASK;
615 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
618 err = affs_do_readpage_ofs(page, to);
619 if (!err)
620 SetPageUptodate(page);
621 unlock_page(page);
622 return err;
625 static int affs_write_begin_ofs(struct file *file, struct address_space *mapping,
626 loff_t pos, unsigned len, unsigned flags,
627 struct page **pagep, void **fsdata)
629 struct inode *inode = mapping->host;
630 struct page *page;
631 pgoff_t index;
632 int err = 0;
634 pr_debug("%s(%u, %llu, %llu)\n", __func__, (u32)inode->i_ino,
635 (unsigned long long)pos, (unsigned long long)pos + len);
636 if (pos > AFFS_I(inode)->mmu_private) {
637 /* XXX: this probably leaves a too-big i_size in case of
638 * failure. Should really be updating i_size at write_end time
640 err = affs_extent_file_ofs(inode, pos);
641 if (err)
642 return err;
645 index = pos >> PAGE_CACHE_SHIFT;
646 page = grab_cache_page_write_begin(mapping, index, flags);
647 if (!page)
648 return -ENOMEM;
649 *pagep = page;
651 if (PageUptodate(page))
652 return 0;
654 /* XXX: inefficient but safe in the face of short writes */
655 err = affs_do_readpage_ofs(page, PAGE_CACHE_SIZE);
656 if (err) {
657 unlock_page(page);
658 page_cache_release(page);
660 return err;
663 static int affs_write_end_ofs(struct file *file, struct address_space *mapping,
664 loff_t pos, unsigned len, unsigned copied,
665 struct page *page, void *fsdata)
667 struct inode *inode = mapping->host;
668 struct super_block *sb = inode->i_sb;
669 struct buffer_head *bh, *prev_bh;
670 char *data;
671 u32 bidx, boff, bsize;
672 unsigned from, to;
673 u32 tmp;
674 int written;
676 from = pos & (PAGE_CACHE_SIZE - 1);
677 to = pos + len;
679 * XXX: not sure if this can handle short copies (len < copied), but
680 * we don't have to, because the page should always be uptodate here,
681 * due to write_begin.
684 pr_debug("%s(%u, %llu, %llu)\n",
685 __func__, (u32)inode->i_ino, (unsigned long long)pos,
686 (unsigned long long)pos + len);
687 bsize = AFFS_SB(sb)->s_data_blksize;
688 data = page_address(page);
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);
700 BUG_ON(boff + tmp > bsize || tmp > bsize);
701 memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
702 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
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);
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) {
727 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
729 if (tmp_next)
730 affs_warning(sb, "commit_write_ofs",
731 "next block already set for %d (%d)",
732 bidx, tmp_next);
733 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
734 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
735 mark_buffer_dirty_inode(prev_bh, inode);
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++;
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);
751 BUG_ON(tmp > bsize);
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) {
761 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
763 if (tmp_next)
764 affs_warning(sb, "commit_write_ofs",
765 "next block already set for %d (%d)",
766 bidx, tmp_next);
767 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
768 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
769 mark_buffer_dirty_inode(prev_bh, inode);
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++;
780 SetPageUptodate(page);
782 done:
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;
788 unlock_page(page);
789 page_cache_release(page);
791 return written;
793 out:
794 bh = prev_bh;
795 if (!written)
796 written = PTR_ERR(bh);
797 goto done;
800 const struct address_space_operations affs_aops_ofs = {
801 .readpage = affs_readpage_ofs,
802 //.writepage = affs_writepage_ofs,
803 .write_begin = affs_write_begin_ofs,
804 .write_end = affs_write_end_ofs
807 /* Free any preallocated blocks. */
809 void
810 affs_free_prealloc(struct inode *inode)
812 struct super_block *sb = inode->i_sb;
814 pr_debug("free_prealloc(ino=%lu)\n", inode->i_ino);
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);
822 /* Truncate (or enlarge) a file to the requested size. */
824 void
825 affs_truncate(struct inode *inode)
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;
834 pr_debug("truncate(inode=%d, oldsize=%u, newsize=%u)\n",
835 (u32)inode->i_ino, (u32)AFFS_I(inode)->mmu_private, (u32)inode->i_size);
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;
844 if (inode->i_size > AFFS_I(inode)->mmu_private) {
845 struct address_space *mapping = inode->i_mapping;
846 struct page *page;
847 void *fsdata;
848 loff_t isize = inode->i_size;
849 int res;
851 res = mapping->a_ops->write_begin(NULL, mapping, isize, 0, 0, &page, &fsdata);
852 if (!res)
853 res = mapping->a_ops->write_end(NULL, mapping, isize, 0, 0, page, fsdata);
854 else
855 inode->i_size = AFFS_I(inode)->mmu_private;
856 mark_inode_dirty(inode);
857 return;
858 } else if (inode->i_size == AFFS_I(inode)->mmu_private)
859 return;
861 // lock cache
862 ext_bh = affs_get_extblock(inode, ext);
863 if (IS_ERR(ext_bh)) {
864 affs_warning(sb, "truncate",
865 "unexpected read error for ext block %u (%ld)",
866 (unsigned int)ext, PTR_ERR(ext_bh));
867 return;
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;
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;
882 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
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;
892 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i);
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;
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);
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;
911 if (IS_ERR(bh)) {
912 affs_warning(sb, "truncate",
913 "unexpected read error for last block %u (%ld)",
914 (unsigned int)ext, PTR_ERR(bh));
915 return;
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);
922 } else {
923 AFFS_I(inode)->i_blkcnt = 0;
924 AFFS_I(inode)->i_extcnt = 1;
926 AFFS_I(inode)->mmu_private = inode->i_size;
927 // unlock cache
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);
940 affs_free_prealloc(inode);
943 int affs_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
945 struct inode *inode = filp->f_mapping->host;
946 int ret, err;
948 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
949 if (err)
950 return err;
952 mutex_lock(&inode->i_mutex);
953 ret = write_inode_now(inode, 0);
954 err = sync_blockdev(inode->i_sb->s_bdev);
955 if (!ret)
956 ret = err;
957 mutex_unlock(&inode->i_mutex);
958 return ret;
960 const 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,
973 const struct inode_operations affs_file_inode_operations = {
974 .setattr = affs_notify_change,