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