OSDN Git Service

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