blob: 8faa6593ca6defe4ab77c169f3fe9673b84a9ec3 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018static struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20static int
21affs_file_open(struct inode *inode, struct file *filp)
22{
Fabian Frederick9606d9a2014-06-06 14:37:25 -070023 pr_debug("open(%lu,%d)\n",
Roman Zippeldca3c332008-04-29 17:02:20 +020024 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
25 atomic_inc(&AFFS_I(inode)->i_opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 return 0;
27}
28
29static int
30affs_file_release(struct inode *inode, struct file *filp)
31{
Fabian Frederick9606d9a2014-06-06 14:37:25 -070032 pr_debug("release(%lu, %d)\n",
Roman Zippeldca3c332008-04-29 17:02:20 +020033 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
34
35 if (atomic_dec_and_test(&AFFS_I(inode)->i_opencnt)) {
36 mutex_lock(&inode->i_mutex);
37 if (inode->i_size != AFFS_I(inode)->mmu_private)
38 affs_truncate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 affs_free_prealloc(inode);
Roman Zippeldca3c332008-04-29 17:02:20 +020040 mutex_unlock(&inode->i_mutex);
41 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 return 0;
44}
45
46static int
47affs_grow_extcache(struct inode *inode, u32 lc_idx)
48{
49 struct super_block *sb = inode->i_sb;
50 struct buffer_head *bh;
51 u32 lc_max;
52 int i, j, key;
53
54 if (!AFFS_I(inode)->i_lc) {
55 char *ptr = (char *)get_zeroed_page(GFP_NOFS);
56 if (!ptr)
57 return -ENOMEM;
58 AFFS_I(inode)->i_lc = (u32 *)ptr;
59 AFFS_I(inode)->i_ac = (struct affs_ext_key *)(ptr + AFFS_CACHE_SIZE / 2);
60 }
61
62 lc_max = AFFS_LC_SIZE << AFFS_I(inode)->i_lc_shift;
63
64 if (AFFS_I(inode)->i_extcnt > lc_max) {
65 u32 lc_shift, lc_mask, tmp, off;
66
67 /* need to recalculate linear cache, start from old size */
68 lc_shift = AFFS_I(inode)->i_lc_shift;
69 tmp = (AFFS_I(inode)->i_extcnt / AFFS_LC_SIZE) >> lc_shift;
70 for (; tmp; tmp >>= 1)
71 lc_shift++;
72 lc_mask = (1 << lc_shift) - 1;
73
74 /* fix idx and old size to new shift */
75 lc_idx >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
76 AFFS_I(inode)->i_lc_size >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
77
78 /* first shrink old cache to make more space */
79 off = 1 << (lc_shift - AFFS_I(inode)->i_lc_shift);
80 for (i = 1, j = off; j < AFFS_LC_SIZE; i++, j += off)
81 AFFS_I(inode)->i_ac[i] = AFFS_I(inode)->i_ac[j];
82
83 AFFS_I(inode)->i_lc_shift = lc_shift;
84 AFFS_I(inode)->i_lc_mask = lc_mask;
85 }
86
87 /* fill cache to the needed index */
88 i = AFFS_I(inode)->i_lc_size;
89 AFFS_I(inode)->i_lc_size = lc_idx + 1;
90 for (; i <= lc_idx; i++) {
91 if (!i) {
92 AFFS_I(inode)->i_lc[0] = inode->i_ino;
93 continue;
94 }
95 key = AFFS_I(inode)->i_lc[i - 1];
96 j = AFFS_I(inode)->i_lc_mask + 1;
97 // unlock cache
98 for (; j > 0; j--) {
99 bh = affs_bread(sb, key);
100 if (!bh)
101 goto err;
102 key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
103 affs_brelse(bh);
104 }
105 // lock cache
106 AFFS_I(inode)->i_lc[i] = key;
107 }
108
109 return 0;
110
111err:
112 // lock cache
113 return -EIO;
114}
115
116static struct buffer_head *
117affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext)
118{
119 struct super_block *sb = inode->i_sb;
120 struct buffer_head *new_bh;
121 u32 blocknr, tmp;
122
123 blocknr = affs_alloc_block(inode, bh->b_blocknr);
124 if (!blocknr)
125 return ERR_PTR(-ENOSPC);
126
127 new_bh = affs_getzeroblk(sb, blocknr);
128 if (!new_bh) {
129 affs_free_block(sb, blocknr);
130 return ERR_PTR(-EIO);
131 }
132
133 AFFS_HEAD(new_bh)->ptype = cpu_to_be32(T_LIST);
134 AFFS_HEAD(new_bh)->key = cpu_to_be32(blocknr);
135 AFFS_TAIL(sb, new_bh)->stype = cpu_to_be32(ST_FILE);
136 AFFS_TAIL(sb, new_bh)->parent = cpu_to_be32(inode->i_ino);
137 affs_fix_checksum(sb, new_bh);
138
139 mark_buffer_dirty_inode(new_bh, inode);
140
141 tmp = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
142 if (tmp)
143 affs_warning(sb, "alloc_ext", "previous extension set (%x)", tmp);
144 AFFS_TAIL(sb, bh)->extension = cpu_to_be32(blocknr);
145 affs_adjust_checksum(bh, blocknr - tmp);
146 mark_buffer_dirty_inode(bh, inode);
147
148 AFFS_I(inode)->i_extcnt++;
149 mark_inode_dirty(inode);
150
151 return new_bh;
152}
153
154static inline struct buffer_head *
155affs_get_extblock(struct inode *inode, u32 ext)
156{
157 /* inline the simplest case: same extended block as last time */
158 struct buffer_head *bh = AFFS_I(inode)->i_ext_bh;
159 if (ext == AFFS_I(inode)->i_ext_last)
Roman Zippeldca3c332008-04-29 17:02:20 +0200160 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 else
162 /* we have to do more (not inlined) */
163 bh = affs_get_extblock_slow(inode, ext);
164
165 return bh;
166}
167
168static struct buffer_head *
169affs_get_extblock_slow(struct inode *inode, u32 ext)
170{
171 struct super_block *sb = inode->i_sb;
172 struct buffer_head *bh;
173 u32 ext_key;
174 u32 lc_idx, lc_off, ac_idx;
175 u32 tmp, idx;
176
177 if (ext == AFFS_I(inode)->i_ext_last + 1) {
178 /* read the next extended block from the current one */
179 bh = AFFS_I(inode)->i_ext_bh;
180 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
181 if (ext < AFFS_I(inode)->i_extcnt)
182 goto read_ext;
183 if (ext > AFFS_I(inode)->i_extcnt)
184 BUG();
185 bh = affs_alloc_extblock(inode, bh, ext);
186 if (IS_ERR(bh))
187 return bh;
188 goto store_ext;
189 }
190
191 if (ext == 0) {
192 /* we seek back to the file header block */
193 ext_key = inode->i_ino;
194 goto read_ext;
195 }
196
197 if (ext >= AFFS_I(inode)->i_extcnt) {
198 struct buffer_head *prev_bh;
199
200 /* allocate a new extended block */
201 if (ext > AFFS_I(inode)->i_extcnt)
202 BUG();
203
204 /* get previous extended block */
205 prev_bh = affs_get_extblock(inode, ext - 1);
206 if (IS_ERR(prev_bh))
207 return prev_bh;
208 bh = affs_alloc_extblock(inode, prev_bh, ext);
209 affs_brelse(prev_bh);
210 if (IS_ERR(bh))
211 return bh;
212 goto store_ext;
213 }
214
215again:
216 /* check if there is an extended cache and whether it's large enough */
217 lc_idx = ext >> AFFS_I(inode)->i_lc_shift;
218 lc_off = ext & AFFS_I(inode)->i_lc_mask;
219
220 if (lc_idx >= AFFS_I(inode)->i_lc_size) {
221 int err;
222
223 err = affs_grow_extcache(inode, lc_idx);
224 if (err)
225 return ERR_PTR(err);
226 goto again;
227 }
228
229 /* every n'th key we find in the linear cache */
230 if (!lc_off) {
231 ext_key = AFFS_I(inode)->i_lc[lc_idx];
232 goto read_ext;
233 }
234
235 /* maybe it's still in the associative cache */
236 ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK;
237 if (AFFS_I(inode)->i_ac[ac_idx].ext == ext) {
238 ext_key = AFFS_I(inode)->i_ac[ac_idx].key;
239 goto read_ext;
240 }
241
242 /* try to find one of the previous extended blocks */
243 tmp = ext;
244 idx = ac_idx;
245 while (--tmp, --lc_off > 0) {
246 idx = (idx - 1) & AFFS_AC_MASK;
247 if (AFFS_I(inode)->i_ac[idx].ext == tmp) {
248 ext_key = AFFS_I(inode)->i_ac[idx].key;
249 goto find_ext;
250 }
251 }
252
253 /* fall back to the linear cache */
254 ext_key = AFFS_I(inode)->i_lc[lc_idx];
255find_ext:
256 /* read all extended blocks until we find the one we need */
257 //unlock cache
258 do {
259 bh = affs_bread(sb, ext_key);
260 if (!bh)
261 goto err_bread;
262 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
263 affs_brelse(bh);
264 tmp++;
265 } while (tmp < ext);
266 //lock cache
267
268 /* store it in the associative cache */
269 // recalculate ac_idx?
270 AFFS_I(inode)->i_ac[ac_idx].ext = ext;
271 AFFS_I(inode)->i_ac[ac_idx].key = ext_key;
272
273read_ext:
274 /* finally read the right extended block */
275 //unlock cache
276 bh = affs_bread(sb, ext_key);
277 if (!bh)
278 goto err_bread;
279 //lock cache
280
281store_ext:
282 /* release old cached extended block and store the new one */
283 affs_brelse(AFFS_I(inode)->i_ext_bh);
284 AFFS_I(inode)->i_ext_last = ext;
285 AFFS_I(inode)->i_ext_bh = bh;
Roman Zippeldca3c332008-04-29 17:02:20 +0200286 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 return bh;
289
290err_bread:
291 affs_brelse(bh);
292 return ERR_PTR(-EIO);
293}
294
295static int
296affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
297{
298 struct super_block *sb = inode->i_sb;
299 struct buffer_head *ext_bh;
300 u32 ext;
301
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700302 pr_debug("%s(%u, %lu)\n",
303 __func__, (u32)inode->i_ino, (unsigned long)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Julia Lawall8d4b6902008-04-29 00:59:12 -0700305 BUG_ON(block > (sector_t)0x7fffffffUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (block >= AFFS_I(inode)->i_blkcnt) {
308 if (block > AFFS_I(inode)->i_blkcnt || !create)
309 goto err_big;
310 } else
311 create = 0;
312
313 //lock cache
314 affs_lock_ext(inode);
315
316 ext = (u32)block / AFFS_SB(sb)->s_hashsize;
317 block -= ext * AFFS_SB(sb)->s_hashsize;
318 ext_bh = affs_get_extblock(inode, ext);
319 if (IS_ERR(ext_bh))
320 goto err_ext;
321 map_bh(bh_result, sb, (sector_t)be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block)));
322
323 if (create) {
324 u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr);
325 if (!blocknr)
326 goto err_alloc;
327 set_buffer_new(bh_result);
328 AFFS_I(inode)->mmu_private += AFFS_SB(sb)->s_data_blksize;
329 AFFS_I(inode)->i_blkcnt++;
330
331 /* store new block */
332 if (bh_result->b_blocknr)
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800333 affs_warning(sb, "get_block", "block already set (%lx)",
334 (unsigned long)bh_result->b_blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
336 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
337 affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
338 bh_result->b_blocknr = blocknr;
339
340 if (!block) {
341 /* insert first block into header block */
342 u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
343 if (tmp)
344 affs_warning(sb, "get_block", "first block already set (%d)", tmp);
345 AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
346 affs_adjust_checksum(ext_bh, blocknr - tmp);
347 }
348 }
349
350 affs_brelse(ext_bh);
351 //unlock cache
352 affs_unlock_ext(inode);
353 return 0;
354
355err_big:
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800356 affs_error(inode->i_sb, "get_block", "strange block request %d",
357 (int)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -EIO;
359err_ext:
360 // unlock cache
361 affs_unlock_ext(inode);
362 return PTR_ERR(ext_bh);
363err_alloc:
364 brelse(ext_bh);
365 clear_buffer_mapped(bh_result);
366 bh_result->b_bdev = NULL;
367 // unlock cache
368 affs_unlock_ext(inode);
369 return -ENOSPC;
370}
371
372static int affs_writepage(struct page *page, struct writeback_control *wbc)
373{
374 return block_write_full_page(page, affs_get_block, wbc);
375}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static int affs_readpage(struct file *file, struct page *page)
378{
379 return block_read_full_page(page, affs_get_block);
380}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700381
Marco Stornelli1dc18342012-12-15 11:51:53 +0100382static void affs_write_failed(struct address_space *mapping, loff_t to)
383{
384 struct inode *inode = mapping->host;
385
386 if (to > inode->i_size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700387 truncate_pagecache(inode, inode->i_size);
Marco Stornelli1dc18342012-12-15 11:51:53 +0100388 affs_truncate(inode);
389 }
390}
391
Fabian Frederick9abb4082014-12-12 16:57:52 -0800392static ssize_t
393affs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter,
394 loff_t offset)
395{
396 struct file *file = iocb->ki_filp;
397 struct address_space *mapping = file->f_mapping;
398 struct inode *inode = mapping->host;
399 size_t count = iov_iter_count(iter);
400 ssize_t ret;
401
402 ret = blockdev_direct_IO(rw, iocb, inode, iter, offset, affs_get_block);
403 if (ret < 0 && (rw & WRITE))
404 affs_write_failed(mapping, offset + count);
405 return ret;
406}
407
Nick Pigginf2b6a162007-10-16 01:25:24 -0700408static int affs_write_begin(struct file *file, struct address_space *mapping,
409 loff_t pos, unsigned len, unsigned flags,
410 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Christoph Hellwig282dc172010-06-04 11:29:55 +0200412 int ret;
413
Nick Pigginf2b6a162007-10-16 01:25:24 -0700414 *pagep = NULL;
Christoph Hellwig282dc172010-06-04 11:29:55 +0200415 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700416 affs_get_block,
417 &AFFS_I(mapping->host)->mmu_private);
Marco Stornelli1dc18342012-12-15 11:51:53 +0100418 if (unlikely(ret))
419 affs_write_failed(mapping, pos + len);
Christoph Hellwig282dc172010-06-04 11:29:55 +0200420
421 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
425{
426 return generic_block_bmap(mapping,block,affs_get_block);
427}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700428
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700429const struct address_space_operations affs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 .readpage = affs_readpage,
431 .writepage = affs_writepage,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700432 .write_begin = affs_write_begin,
433 .write_end = generic_write_end,
Fabian Frederick9abb4082014-12-12 16:57:52 -0800434 .direct_IO = affs_direct_IO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 .bmap = _affs_bmap
436};
437
438static inline struct buffer_head *
439affs_bread_ino(struct inode *inode, int block, int create)
440{
441 struct buffer_head *bh, tmp_bh;
442 int err;
443
444 tmp_bh.b_state = 0;
445 err = affs_get_block(inode, block, &tmp_bh, create);
446 if (!err) {
447 bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
448 if (bh) {
449 bh->b_state |= tmp_bh.b_state;
450 return bh;
451 }
452 err = -EIO;
453 }
454 return ERR_PTR(err);
455}
456
457static inline struct buffer_head *
458affs_getzeroblk_ino(struct inode *inode, int block)
459{
460 struct buffer_head *bh, tmp_bh;
461 int err;
462
463 tmp_bh.b_state = 0;
464 err = affs_get_block(inode, block, &tmp_bh, 1);
465 if (!err) {
466 bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
467 if (bh) {
468 bh->b_state |= tmp_bh.b_state;
469 return bh;
470 }
471 err = -EIO;
472 }
473 return ERR_PTR(err);
474}
475
476static inline struct buffer_head *
477affs_getemptyblk_ino(struct inode *inode, int block)
478{
479 struct buffer_head *bh, tmp_bh;
480 int err;
481
482 tmp_bh.b_state = 0;
483 err = affs_get_block(inode, block, &tmp_bh, 1);
484 if (!err) {
485 bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
486 if (bh) {
487 bh->b_state |= tmp_bh.b_state;
488 return bh;
489 }
490 err = -EIO;
491 }
492 return ERR_PTR(err);
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static int
Fabian Frederick0c89d672014-06-06 14:37:23 -0700496affs_do_readpage_ofs(struct page *page, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct inode *inode = page->mapping->host;
499 struct super_block *sb = inode->i_sb;
500 struct buffer_head *bh;
501 char *data;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700502 unsigned pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 u32 bidx, boff, bsize;
504 u32 tmp;
505
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700506 pr_debug("%s(%u, %ld, 0, %d)\n", __func__, (u32)inode->i_ino,
Fabian Frederick0c89d672014-06-06 14:37:23 -0700507 page->index, to);
508 BUG_ON(to > PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 kmap(page);
510 data = page_address(page);
511 bsize = AFFS_SB(sb)->s_data_blksize;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700512 tmp = page->index << PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 bidx = tmp / bsize;
514 boff = tmp % bsize;
515
Fabian Frederick0c89d672014-06-06 14:37:23 -0700516 while (pos < to) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 bh = affs_bread_ino(inode, bidx, 0);
518 if (IS_ERR(bh))
519 return PTR_ERR(bh);
Fabian Frederick0c89d672014-06-06 14:37:23 -0700520 tmp = min(bsize - boff, to - pos);
521 BUG_ON(pos + tmp > to || tmp > bsize);
522 memcpy(data + pos, AFFS_DATA(bh) + boff, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 affs_brelse(bh);
524 bidx++;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700525 pos += tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 boff = 0;
527 }
528 flush_dcache_page(page);
529 kunmap(page);
530 return 0;
531}
532
533static int
534affs_extent_file_ofs(struct inode *inode, u32 newsize)
535{
536 struct super_block *sb = inode->i_sb;
537 struct buffer_head *bh, *prev_bh;
538 u32 bidx, boff;
539 u32 size, bsize;
540 u32 tmp;
541
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700542 pr_debug("%s(%u, %d)\n", __func__, (u32)inode->i_ino, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 bsize = AFFS_SB(sb)->s_data_blksize;
544 bh = NULL;
545 size = AFFS_I(inode)->mmu_private;
546 bidx = size / bsize;
547 boff = size % bsize;
548 if (boff) {
549 bh = affs_bread_ino(inode, bidx, 0);
550 if (IS_ERR(bh))
551 return PTR_ERR(bh);
552 tmp = min(bsize - boff, newsize - size);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700553 BUG_ON(boff + tmp > bsize || tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 memset(AFFS_DATA(bh) + boff, 0, tmp);
Marcin Slusarz6369a4a2008-04-30 00:54:47 -0700555 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 affs_fix_checksum(sb, bh);
557 mark_buffer_dirty_inode(bh, inode);
558 size += tmp;
559 bidx++;
560 } else if (bidx) {
561 bh = affs_bread_ino(inode, bidx - 1, 0);
562 if (IS_ERR(bh))
563 return PTR_ERR(bh);
564 }
565
566 while (size < newsize) {
567 prev_bh = bh;
568 bh = affs_getzeroblk_ino(inode, bidx);
569 if (IS_ERR(bh))
570 goto out;
571 tmp = min(bsize, newsize - size);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700572 BUG_ON(tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
574 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
575 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
576 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
577 affs_fix_checksum(sb, bh);
578 bh->b_state &= ~(1UL << BH_New);
579 mark_buffer_dirty_inode(bh, inode);
580 if (prev_bh) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700581 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
582
583 if (tmp_next)
584 affs_warning(sb, "extent_file_ofs",
585 "next block already set for %d (%d)",
586 bidx, tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700588 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 mark_buffer_dirty_inode(prev_bh, inode);
590 affs_brelse(prev_bh);
591 }
592 size += bsize;
593 bidx++;
594 }
595 affs_brelse(bh);
596 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
597 return 0;
598
599out:
600 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
601 return PTR_ERR(bh);
602}
603
604static int
605affs_readpage_ofs(struct file *file, struct page *page)
606{
607 struct inode *inode = page->mapping->host;
608 u32 to;
609 int err;
610
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700611 pr_debug("%s(%u, %ld)\n", __func__, (u32)inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 to = PAGE_CACHE_SIZE;
613 if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) {
614 to = inode->i_size & ~PAGE_CACHE_MASK;
615 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
616 }
617
Fabian Frederick0c89d672014-06-06 14:37:23 -0700618 err = affs_do_readpage_ofs(page, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!err)
620 SetPageUptodate(page);
621 unlock_page(page);
622 return err;
623}
624
Nick Pigginf2b6a162007-10-16 01:25:24 -0700625static int affs_write_begin_ofs(struct file *file, struct address_space *mapping,
626 loff_t pos, unsigned len, unsigned flags,
627 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Nick Pigginf2b6a162007-10-16 01:25:24 -0700629 struct inode *inode = mapping->host;
630 struct page *page;
631 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int err = 0;
633
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700634 pr_debug("%s(%u, %llu, %llu)\n", __func__, (u32)inode->i_ino,
635 (unsigned long long)pos, (unsigned long long)pos + len);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700636 if (pos > AFFS_I(inode)->mmu_private) {
637 /* XXX: this probably leaves a too-big i_size in case of
638 * failure. Should really be updating i_size at write_end time
639 */
640 err = affs_extent_file_ofs(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (err)
642 return err;
643 }
Nick Pigginf2b6a162007-10-16 01:25:24 -0700644
645 index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin54566b22009-01-04 12:00:53 -0800646 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700647 if (!page)
648 return -ENOMEM;
649 *pagep = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (PageUptodate(page))
652 return 0;
653
Nick Pigginf2b6a162007-10-16 01:25:24 -0700654 /* XXX: inefficient but safe in the face of short writes */
Fabian Frederick0c89d672014-06-06 14:37:23 -0700655 err = affs_do_readpage_ofs(page, PAGE_CACHE_SIZE);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700656 if (err) {
657 unlock_page(page);
658 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 return err;
661}
662
Nick Pigginf2b6a162007-10-16 01:25:24 -0700663static int affs_write_end_ofs(struct file *file, struct address_space *mapping,
664 loff_t pos, unsigned len, unsigned copied,
665 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Nick Pigginf2b6a162007-10-16 01:25:24 -0700667 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct super_block *sb = inode->i_sb;
669 struct buffer_head *bh, *prev_bh;
670 char *data;
671 u32 bidx, boff, bsize;
Nick Pigginf2b6a162007-10-16 01:25:24 -0700672 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 u32 tmp;
674 int written;
675
Nick Pigginf2b6a162007-10-16 01:25:24 -0700676 from = pos & (PAGE_CACHE_SIZE - 1);
677 to = pos + len;
678 /*
679 * XXX: not sure if this can handle short copies (len < copied), but
680 * we don't have to, because the page should always be uptodate here,
681 * due to write_begin.
682 */
683
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700684 pr_debug("%s(%u, %llu, %llu)\n",
685 __func__, (u32)inode->i_ino, (unsigned long long)pos,
686 (unsigned long long)pos + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 bsize = AFFS_SB(sb)->s_data_blksize;
688 data = page_address(page);
689
690 bh = NULL;
691 written = 0;
692 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
693 bidx = tmp / bsize;
694 boff = tmp % bsize;
695 if (boff) {
696 bh = affs_bread_ino(inode, bidx, 0);
697 if (IS_ERR(bh))
698 return PTR_ERR(bh);
699 tmp = min(bsize - boff, to - from);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700700 BUG_ON(boff + tmp > bsize || tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
Marcin Slusarz6369a4a2008-04-30 00:54:47 -0700702 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 affs_fix_checksum(sb, bh);
704 mark_buffer_dirty_inode(bh, inode);
705 written += tmp;
706 from += tmp;
707 bidx++;
708 } else if (bidx) {
709 bh = affs_bread_ino(inode, bidx - 1, 0);
710 if (IS_ERR(bh))
711 return PTR_ERR(bh);
712 }
713 while (from + bsize <= to) {
714 prev_bh = bh;
715 bh = affs_getemptyblk_ino(inode, bidx);
716 if (IS_ERR(bh))
717 goto out;
718 memcpy(AFFS_DATA(bh), data + from, bsize);
719 if (buffer_new(bh)) {
720 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
721 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
722 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
723 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize);
724 AFFS_DATA_HEAD(bh)->next = 0;
725 bh->b_state &= ~(1UL << BH_New);
726 if (prev_bh) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700727 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
728
729 if (tmp_next)
730 affs_warning(sb, "commit_write_ofs",
731 "next block already set for %d (%d)",
732 bidx, tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700734 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 mark_buffer_dirty_inode(prev_bh, inode);
736 }
737 }
738 affs_brelse(prev_bh);
739 affs_fix_checksum(sb, bh);
740 mark_buffer_dirty_inode(bh, inode);
741 written += bsize;
742 from += bsize;
743 bidx++;
744 }
745 if (from < to) {
746 prev_bh = bh;
747 bh = affs_bread_ino(inode, bidx, 1);
748 if (IS_ERR(bh))
749 goto out;
750 tmp = min(bsize, to - from);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700751 BUG_ON(tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 memcpy(AFFS_DATA(bh), data + from, tmp);
753 if (buffer_new(bh)) {
754 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
755 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
756 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
757 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
758 AFFS_DATA_HEAD(bh)->next = 0;
759 bh->b_state &= ~(1UL << BH_New);
760 if (prev_bh) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700761 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
762
763 if (tmp_next)
764 affs_warning(sb, "commit_write_ofs",
765 "next block already set for %d (%d)",
766 bidx, tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700768 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 mark_buffer_dirty_inode(prev_bh, inode);
770 }
771 } else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp)
772 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
773 affs_brelse(prev_bh);
774 affs_fix_checksum(sb, bh);
775 mark_buffer_dirty_inode(bh, inode);
776 written += tmp;
777 from += tmp;
778 bidx++;
779 }
780 SetPageUptodate(page);
781
782done:
783 affs_brelse(bh);
784 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
785 if (tmp > inode->i_size)
786 inode->i_size = AFFS_I(inode)->mmu_private = tmp;
787
Nick Pigginf2b6a162007-10-16 01:25:24 -0700788 unlock_page(page);
789 page_cache_release(page);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return written;
792
793out:
794 bh = prev_bh;
795 if (!written)
796 written = PTR_ERR(bh);
797 goto done;
798}
799
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700800const struct address_space_operations affs_aops_ofs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .readpage = affs_readpage_ofs,
802 //.writepage = affs_writepage_ofs,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700803 .write_begin = affs_write_begin_ofs,
804 .write_end = affs_write_end_ofs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805};
806
807/* Free any preallocated blocks. */
808
809void
810affs_free_prealloc(struct inode *inode)
811{
812 struct super_block *sb = inode->i_sb;
813
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700814 pr_debug("free_prealloc(ino=%lu)\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 while (AFFS_I(inode)->i_pa_cnt) {
817 AFFS_I(inode)->i_pa_cnt--;
818 affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc);
819 }
820}
821
822/* Truncate (or enlarge) a file to the requested size. */
823
824void
825affs_truncate(struct inode *inode)
826{
827 struct super_block *sb = inode->i_sb;
828 u32 ext, ext_key;
829 u32 last_blk, blkcnt, blk;
830 u32 size;
831 struct buffer_head *ext_bh;
832 int i;
833
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700834 pr_debug("truncate(inode=%d, oldsize=%u, newsize=%u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 (u32)inode->i_ino, (u32)AFFS_I(inode)->mmu_private, (u32)inode->i_size);
836
837 last_blk = 0;
838 ext = 0;
839 if (inode->i_size) {
840 last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize;
841 ext = last_blk / AFFS_SB(sb)->s_hashsize;
842 }
843
844 if (inode->i_size > AFFS_I(inode)->mmu_private) {
845 struct address_space *mapping = inode->i_mapping;
846 struct page *page;
Nick Pigginf2b6a162007-10-16 01:25:24 -0700847 void *fsdata;
Fabian Frederick73516ac2014-10-13 15:53:54 -0700848 loff_t isize = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 int res;
850
Fabian Frederick73516ac2014-10-13 15:53:54 -0700851 res = mapping->a_ops->write_begin(NULL, mapping, isize, 0, 0, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!res)
Fabian Frederick73516ac2014-10-13 15:53:54 -0700853 res = mapping->a_ops->write_end(NULL, mapping, isize, 0, 0, page, fsdata);
Roman Zippeldca3c332008-04-29 17:02:20 +0200854 else
855 inode->i_size = AFFS_I(inode)->mmu_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 mark_inode_dirty(inode);
857 return;
858 } else if (inode->i_size == AFFS_I(inode)->mmu_private)
859 return;
860
861 // lock cache
862 ext_bh = affs_get_extblock(inode, ext);
863 if (IS_ERR(ext_bh)) {
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800864 affs_warning(sb, "truncate",
865 "unexpected read error for ext block %u (%ld)",
866 (unsigned int)ext, PTR_ERR(ext_bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return;
868 }
869 if (AFFS_I(inode)->i_lc) {
870 /* clear linear cache */
871 i = (ext + 1) >> AFFS_I(inode)->i_lc_shift;
872 if (AFFS_I(inode)->i_lc_size > i) {
873 AFFS_I(inode)->i_lc_size = i;
874 for (; i < AFFS_LC_SIZE; i++)
875 AFFS_I(inode)->i_lc[i] = 0;
876 }
877 /* clear associative cache */
878 for (i = 0; i < AFFS_AC_SIZE; i++)
879 if (AFFS_I(inode)->i_ac[i].ext >= ext)
880 AFFS_I(inode)->i_ac[i].ext = 0;
881 }
882 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
883
884 blkcnt = AFFS_I(inode)->i_blkcnt;
885 i = 0;
886 blk = last_blk;
887 if (inode->i_size) {
888 i = last_blk % AFFS_SB(sb)->s_hashsize + 1;
889 blk++;
890 } else
891 AFFS_HEAD(ext_bh)->first_data = 0;
Roman Zippeldca3c332008-04-29 17:02:20 +0200892 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 size = AFFS_SB(sb)->s_hashsize;
894 if (size > blkcnt - blk + i)
895 size = blkcnt - blk + i;
896 for (; i < size; i++, blk++) {
897 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
898 AFFS_BLOCK(sb, ext_bh, i) = 0;
899 }
900 AFFS_TAIL(sb, ext_bh)->extension = 0;
901 affs_fix_checksum(sb, ext_bh);
902 mark_buffer_dirty_inode(ext_bh, inode);
903 affs_brelse(ext_bh);
904
905 if (inode->i_size) {
906 AFFS_I(inode)->i_blkcnt = last_blk + 1;
907 AFFS_I(inode)->i_extcnt = ext + 1;
908 if (AFFS_SB(sb)->s_flags & SF_OFS) {
909 struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
910 u32 tmp;
Dan Carpenter0e45b672010-08-25 09:05:27 +0200911 if (IS_ERR(bh)) {
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800912 affs_warning(sb, "truncate",
913 "unexpected read error for last block %u (%ld)",
914 (unsigned int)ext, PTR_ERR(bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return;
916 }
917 tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
918 AFFS_DATA_HEAD(bh)->next = 0;
919 affs_adjust_checksum(bh, -tmp);
920 affs_brelse(bh);
921 }
922 } else {
923 AFFS_I(inode)->i_blkcnt = 0;
924 AFFS_I(inode)->i_extcnt = 1;
925 }
926 AFFS_I(inode)->mmu_private = inode->i_size;
927 // unlock cache
928
929 while (ext_key) {
930 ext_bh = affs_bread(sb, ext_key);
931 size = AFFS_SB(sb)->s_hashsize;
932 if (size > blkcnt - blk)
933 size = blkcnt - blk;
934 for (i = 0; i < size; i++, blk++)
935 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
936 affs_free_block(sb, ext_key);
937 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
938 affs_brelse(ext_bh);
939 }
940 affs_free_prealloc(inode);
941}
Al Viroc4758792009-06-08 01:22:00 -0400942
Josef Bacik02c24a82011-07-16 20:44:56 -0400943int affs_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Al Viroc4758792009-06-08 01:22:00 -0400944{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200945 struct inode *inode = filp->f_mapping->host;
Al Viroc4758792009-06-08 01:22:00 -0400946 int ret, err;
947
Josef Bacik02c24a82011-07-16 20:44:56 -0400948 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
949 if (err)
950 return err;
951
952 mutex_lock(&inode->i_mutex);
Al Viroc4758792009-06-08 01:22:00 -0400953 ret = write_inode_now(inode, 0);
954 err = sync_blockdev(inode->i_sb->s_bdev);
955 if (!ret)
956 ret = err;
Josef Bacik02c24a82011-07-16 20:44:56 -0400957 mutex_unlock(&inode->i_mutex);
Al Viroc4758792009-06-08 01:22:00 -0400958 return ret;
959}
Fabian Frederick76339782014-12-12 16:57:47 -0800960const struct file_operations affs_file_operations = {
961 .llseek = generic_file_llseek,
962 .read = new_sync_read,
963 .read_iter = generic_file_read_iter,
964 .write = new_sync_write,
965 .write_iter = generic_file_write_iter,
966 .mmap = generic_file_mmap,
967 .open = affs_file_open,
968 .release = affs_file_release,
969 .fsync = affs_file_fsync,
970 .splice_read = generic_file_splice_read,
971};
972
973const struct inode_operations affs_file_inode_operations = {
974 .setattr = affs_notify_change,
975};