blob: 05005bd2621ad4b1bad3eb2cd5c3aab8e73c46d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Fabian Frederick9abb4082014-12-12 16:57:52 -080015#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "affs.h"
17
18#if PAGE_SIZE < 4096
19#error PAGE_SIZE must be at least 4096
20#endif
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static int
25affs_file_open(struct inode *inode, struct file *filp)
26{
Fabian Frederick9606d9a2014-06-06 14:37:25 -070027 pr_debug("open(%lu,%d)\n",
Roman Zippeldca3c332008-04-29 17:02:20 +020028 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
29 atomic_inc(&AFFS_I(inode)->i_opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return 0;
31}
32
33static int
34affs_file_release(struct inode *inode, struct file *filp)
35{
Fabian Frederick9606d9a2014-06-06 14:37:25 -070036 pr_debug("release(%lu, %d)\n",
Roman Zippeldca3c332008-04-29 17:02:20 +020037 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 affs_free_prealloc(inode);
Roman Zippeldca3c332008-04-29 17:02:20 +020044 mutex_unlock(&inode->i_mutex);
45 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 return 0;
48}
49
50static int
51affs_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
115err:
116 // lock cache
117 return -EIO;
118}
119
120static struct buffer_head *
121affs_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
158static inline struct buffer_head *
159affs_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)
Roman Zippeldca3c332008-04-29 17:02:20 +0200164 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 else
166 /* we have to do more (not inlined) */
167 bh = affs_get_extblock_slow(inode, ext);
168
169 return bh;
170}
171
172static struct buffer_head *
173affs_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
219again:
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];
259find_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
277read_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
285store_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;
Roman Zippeldca3c332008-04-29 17:02:20 +0200290 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 return bh;
293
294err_bread:
295 affs_brelse(bh);
296 return ERR_PTR(-EIO);
297}
298
299static int
300affs_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
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700306 pr_debug("%s(%u, %lu)\n",
307 __func__, (u32)inode->i_ino, (unsigned long)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Julia Lawall8d4b6902008-04-29 00:59:12 -0700309 BUG_ON(block > (sector_t)0x7fffffffUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
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)
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800337 affs_warning(sb, "get_block", "block already set (%lx)",
338 (unsigned long)bh_result->b_blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 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
359err_big:
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800360 affs_error(inode->i_sb, "get_block", "strange block request %d",
361 (int)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return -EIO;
363err_ext:
364 // unlock cache
365 affs_unlock_ext(inode);
366 return PTR_ERR(ext_bh);
367err_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
376static int affs_writepage(struct page *page, struct writeback_control *wbc)
377{
378 return block_write_full_page(page, affs_get_block, wbc);
379}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static int affs_readpage(struct file *file, struct page *page)
382{
383 return block_read_full_page(page, affs_get_block);
384}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700385
Marco Stornelli1dc18342012-12-15 11:51:53 +0100386static 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) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700391 truncate_pagecache(inode, inode->i_size);
Marco Stornelli1dc18342012-12-15 11:51:53 +0100392 affs_truncate(inode);
393 }
394}
395
Fabian Frederick9abb4082014-12-12 16:57:52 -0800396static ssize_t
397affs_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
Nick Pigginf2b6a162007-10-16 01:25:24 -0700412static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Christoph Hellwig282dc172010-06-04 11:29:55 +0200416 int ret;
417
Nick Pigginf2b6a162007-10-16 01:25:24 -0700418 *pagep = NULL;
Christoph Hellwig282dc172010-06-04 11:29:55 +0200419 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700420 affs_get_block,
421 &AFFS_I(mapping->host)->mmu_private);
Marco Stornelli1dc18342012-12-15 11:51:53 +0100422 if (unlikely(ret))
423 affs_write_failed(mapping, pos + len);
Christoph Hellwig282dc172010-06-04 11:29:55 +0200424
425 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
429{
430 return generic_block_bmap(mapping,block,affs_get_block);
431}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700432
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700433const struct address_space_operations affs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .readpage = affs_readpage,
435 .writepage = affs_writepage,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700436 .write_begin = affs_write_begin,
437 .write_end = generic_write_end,
Fabian Frederick9abb4082014-12-12 16:57:52 -0800438 .direct_IO = affs_direct_IO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 .bmap = _affs_bmap
440};
441
442static inline struct buffer_head *
443affs_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
461static inline struct buffer_head *
462affs_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
480static inline struct buffer_head *
481affs_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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static int
Fabian Frederick0c89d672014-06-06 14:37:23 -0700500affs_do_readpage_ofs(struct page *page, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct inode *inode = page->mapping->host;
503 struct super_block *sb = inode->i_sb;
504 struct buffer_head *bh;
505 char *data;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700506 unsigned pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 u32 bidx, boff, bsize;
508 u32 tmp;
509
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700510 pr_debug("%s(%u, %ld, 0, %d)\n", __func__, (u32)inode->i_ino,
Fabian Frederick0c89d672014-06-06 14:37:23 -0700511 page->index, to);
512 BUG_ON(to > PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 kmap(page);
514 data = page_address(page);
515 bsize = AFFS_SB(sb)->s_data_blksize;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700516 tmp = page->index << PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 bidx = tmp / bsize;
518 boff = tmp % bsize;
519
Fabian Frederick0c89d672014-06-06 14:37:23 -0700520 while (pos < to) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 bh = affs_bread_ino(inode, bidx, 0);
522 if (IS_ERR(bh))
523 return PTR_ERR(bh);
Fabian Frederick0c89d672014-06-06 14:37:23 -0700524 tmp = min(bsize - boff, to - pos);
525 BUG_ON(pos + tmp > to || tmp > bsize);
526 memcpy(data + pos, AFFS_DATA(bh) + boff, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 affs_brelse(bh);
528 bidx++;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700529 pos += tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 boff = 0;
531 }
532 flush_dcache_page(page);
533 kunmap(page);
534 return 0;
535}
536
537static int
538affs_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
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700546 pr_debug("%s(%u, %d)\n", __func__, (u32)inode->i_ino, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 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);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700557 BUG_ON(boff + tmp > bsize || tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 memset(AFFS_DATA(bh) + boff, 0, tmp);
Marcin Slusarz6369a4a2008-04-30 00:54:47 -0700559 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 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);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700576 BUG_ON(tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 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) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700585 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700592 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 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
603out:
604 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
605 return PTR_ERR(bh);
606}
607
608static int
609affs_readpage_ofs(struct file *file, struct page *page)
610{
611 struct inode *inode = page->mapping->host;
612 u32 to;
613 int err;
614
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700615 pr_debug("%s(%u, %ld)\n", __func__, (u32)inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 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
Fabian Frederick0c89d672014-06-06 14:37:23 -0700622 err = affs_do_readpage_ofs(page, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!err)
624 SetPageUptodate(page);
625 unlock_page(page);
626 return err;
627}
628
Nick Pigginf2b6a162007-10-16 01:25:24 -0700629static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Nick Pigginf2b6a162007-10-16 01:25:24 -0700633 struct inode *inode = mapping->host;
634 struct page *page;
635 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int err = 0;
637
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700638 pr_debug("%s(%u, %llu, %llu)\n", __func__, (u32)inode->i_ino,
639 (unsigned long long)pos, (unsigned long long)pos + len);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700640 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (err)
646 return err;
647 }
Nick Pigginf2b6a162007-10-16 01:25:24 -0700648
649 index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin54566b22009-01-04 12:00:53 -0800650 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700651 if (!page)
652 return -ENOMEM;
653 *pagep = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (PageUptodate(page))
656 return 0;
657
Nick Pigginf2b6a162007-10-16 01:25:24 -0700658 /* XXX: inefficient but safe in the face of short writes */
Fabian Frederick0c89d672014-06-06 14:37:23 -0700659 err = affs_do_readpage_ofs(page, PAGE_CACHE_SIZE);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700660 if (err) {
661 unlock_page(page);
662 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 return err;
665}
666
Nick Pigginf2b6a162007-10-16 01:25:24 -0700667static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Nick Pigginf2b6a162007-10-16 01:25:24 -0700671 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct super_block *sb = inode->i_sb;
673 struct buffer_head *bh, *prev_bh;
674 char *data;
675 u32 bidx, boff, bsize;
Nick Pigginf2b6a162007-10-16 01:25:24 -0700676 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 u32 tmp;
678 int written;
679
Nick Pigginf2b6a162007-10-16 01:25:24 -0700680 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
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700688 pr_debug("%s(%u, %llu, %llu)\n",
689 __func__, (u32)inode->i_ino, (unsigned long long)pos,
690 (unsigned long long)pos + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 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);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700704 BUG_ON(boff + tmp > bsize || tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
Marcin Slusarz6369a4a2008-04-30 00:54:47 -0700706 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 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) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700731 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700738 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 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);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700755 BUG_ON(tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 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) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700765 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700772 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 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
786done:
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
Nick Pigginf2b6a162007-10-16 01:25:24 -0700792 unlock_page(page);
793 page_cache_release(page);
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return written;
796
797out:
798 bh = prev_bh;
799 if (!written)
800 written = PTR_ERR(bh);
801 goto done;
802}
803
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700804const struct address_space_operations affs_aops_ofs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 .readpage = affs_readpage_ofs,
806 //.writepage = affs_writepage_ofs,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700807 .write_begin = affs_write_begin_ofs,
808 .write_end = affs_write_end_ofs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809};
810
811/* Free any preallocated blocks. */
812
813void
814affs_free_prealloc(struct inode *inode)
815{
816 struct super_block *sb = inode->i_sb;
817
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700818 pr_debug("free_prealloc(ino=%lu)\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
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
828void
829affs_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
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700838 pr_debug("truncate(inode=%d, oldsize=%u, newsize=%u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 (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;
Nick Pigginf2b6a162007-10-16 01:25:24 -0700851 void *fsdata;
Fabian Frederick73516ac2014-10-13 15:53:54 -0700852 loff_t isize = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int res;
854
Fabian Frederick73516ac2014-10-13 15:53:54 -0700855 res = mapping->a_ops->write_begin(NULL, mapping, isize, 0, 0, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!res)
Fabian Frederick73516ac2014-10-13 15:53:54 -0700857 res = mapping->a_ops->write_end(NULL, mapping, isize, 0, 0, page, fsdata);
Roman Zippeldca3c332008-04-29 17:02:20 +0200858 else
859 inode->i_size = AFFS_I(inode)->mmu_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 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)) {
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800868 affs_warning(sb, "truncate",
869 "unexpected read error for ext block %u (%ld)",
870 (unsigned int)ext, PTR_ERR(ext_bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 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;
Roman Zippeldca3c332008-04-29 17:02:20 +0200896 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 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;
Dan Carpenter0e45b672010-08-25 09:05:27 +0200915 if (IS_ERR(bh)) {
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800916 affs_warning(sb, "truncate",
917 "unexpected read error for last block %u (%ld)",
918 (unsigned int)ext, PTR_ERR(bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 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}
Al Viroc4758792009-06-08 01:22:00 -0400946
Josef Bacik02c24a82011-07-16 20:44:56 -0400947int affs_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Al Viroc4758792009-06-08 01:22:00 -0400948{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200949 struct inode *inode = filp->f_mapping->host;
Al Viroc4758792009-06-08 01:22:00 -0400950 int ret, err;
951
Josef Bacik02c24a82011-07-16 20:44:56 -0400952 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
953 if (err)
954 return err;
955
956 mutex_lock(&inode->i_mutex);
Al Viroc4758792009-06-08 01:22:00 -0400957 ret = write_inode_now(inode, 0);
958 err = sync_blockdev(inode->i_sb->s_bdev);
959 if (!ret)
960 ret = err;
Josef Bacik02c24a82011-07-16 20:44:56 -0400961 mutex_unlock(&inode->i_mutex);
Al Viroc4758792009-06-08 01:22:00 -0400962 return ret;
963}
Fabian Frederick76339782014-12-12 16:57:47 -0800964const 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
977const struct inode_operations affs_file_inode_operations = {
978 .setattr = affs_notify_change,
979};