xref: /linux/fs/affs/file.c (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/affs/file.c
4  *
5  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
6  *
7  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
8  *
9  *  (C) 1992  Eric Youngdale Modified for ISO 9660 filesystem.
10  *
11  *  (C) 1991  Linus Torvalds - minix filesystem
12  *
13  *  affs regular file handling primitives
14  */
15 
16 #include <linux/uio.h>
17 #include <linux/blkdev.h>
18 #include <linux/filelock.h>
19 #include <linux/mpage.h>
20 #include "affs.h"
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(%llu,%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(%llu, %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 		inode_lock(inode);
41 		if (inode->i_size != AFFS_I(inode)->mmu_private)
42 			affs_truncate(inode);
43 		affs_free_prealloc(inode);
44 		inode_unlock(inode);
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 	mmb_mark_buffer_dirty(new_bh, &AFFS_I(inode)->i_metadata_bhs);
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 	mmb_mark_buffer_dirty(bh, &AFFS_I(inode)->i_metadata_bhs);
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 		BUG_ON(ext > AFFS_I(inode)->i_extcnt);
188 		bh = affs_alloc_extblock(inode, bh, ext);
189 		if (IS_ERR(bh))
190 			return bh;
191 		goto store_ext;
192 	}
193 
194 	if (ext == 0) {
195 		/* we seek back to the file header block */
196 		ext_key = inode->i_ino;
197 		goto read_ext;
198 	}
199 
200 	if (ext >= AFFS_I(inode)->i_extcnt) {
201 		struct buffer_head *prev_bh;
202 
203 		/* allocate a new extended block */
204 		BUG_ON(ext > AFFS_I(inode)->i_extcnt);
205 
206 		/* get previous extended block */
207 		prev_bh = affs_get_extblock(inode, ext - 1);
208 		if (IS_ERR(prev_bh))
209 			return prev_bh;
210 		bh = affs_alloc_extblock(inode, prev_bh, ext);
211 		affs_brelse(prev_bh);
212 		if (IS_ERR(bh))
213 			return bh;
214 		goto store_ext;
215 	}
216 
217 again:
218 	/* check if there is an extended cache and whether it's large enough */
219 	lc_idx = ext >> AFFS_I(inode)->i_lc_shift;
220 	lc_off = ext & AFFS_I(inode)->i_lc_mask;
221 
222 	if (lc_idx >= AFFS_I(inode)->i_lc_size) {
223 		int err;
224 
225 		err = affs_grow_extcache(inode, lc_idx);
226 		if (err)
227 			return ERR_PTR(err);
228 		goto again;
229 	}
230 
231 	/* every n'th key we find in the linear cache */
232 	if (!lc_off) {
233 		ext_key = AFFS_I(inode)->i_lc[lc_idx];
234 		goto read_ext;
235 	}
236 
237 	/* maybe it's still in the associative cache */
238 	ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK;
239 	if (AFFS_I(inode)->i_ac[ac_idx].ext == ext) {
240 		ext_key = AFFS_I(inode)->i_ac[ac_idx].key;
241 		goto read_ext;
242 	}
243 
244 	/* try to find one of the previous extended blocks */
245 	tmp = ext;
246 	idx = ac_idx;
247 	while (--tmp, --lc_off > 0) {
248 		idx = (idx - 1) & AFFS_AC_MASK;
249 		if (AFFS_I(inode)->i_ac[idx].ext == tmp) {
250 			ext_key = AFFS_I(inode)->i_ac[idx].key;
251 			goto find_ext;
252 		}
253 	}
254 
255 	/* fall back to the linear cache */
256 	ext_key = AFFS_I(inode)->i_lc[lc_idx];
257 find_ext:
258 	/* read all extended blocks until we find the one we need */
259 	//unlock cache
260 	do {
261 		bh = affs_bread(sb, ext_key);
262 		if (!bh)
263 			goto err_bread;
264 		ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
265 		affs_brelse(bh);
266 		tmp++;
267 	} while (tmp < ext);
268 	//lock cache
269 
270 	/* store it in the associative cache */
271 	// recalculate ac_idx?
272 	AFFS_I(inode)->i_ac[ac_idx].ext = ext;
273 	AFFS_I(inode)->i_ac[ac_idx].key = ext_key;
274 
275 read_ext:
276 	/* finally read the right extended block */
277 	//unlock cache
278 	bh = affs_bread(sb, ext_key);
279 	if (!bh)
280 		goto err_bread;
281 	//lock cache
282 
283 store_ext:
284 	/* release old cached extended block and store the new one */
285 	affs_brelse(AFFS_I(inode)->i_ext_bh);
286 	AFFS_I(inode)->i_ext_last = ext;
287 	AFFS_I(inode)->i_ext_bh = bh;
288 	get_bh(bh);
289 
290 	return bh;
291 
292 err_bread:
293 	affs_brelse(bh);
294 	return ERR_PTR(-EIO);
295 }
296 
297 static int
298 affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
299 {
300 	struct super_block	*sb = inode->i_sb;
301 	struct buffer_head	*ext_bh;
302 	u32			 ext;
303 
304 	pr_debug("%s(%llu, %llu)\n", __func__, inode->i_ino,
305 		 (unsigned long long)block);
306 
307 	BUG_ON(block > (sector_t)0x7fffffffUL);
308 
309 	if (block >= AFFS_I(inode)->i_blkcnt) {
310 		if (block > AFFS_I(inode)->i_blkcnt || !create)
311 			goto err_big;
312 	} else
313 		create = 0;
314 
315 	//lock cache
316 	affs_lock_ext(inode);
317 
318 	ext = (u32)block / AFFS_SB(sb)->s_hashsize;
319 	block -= ext * AFFS_SB(sb)->s_hashsize;
320 	ext_bh = affs_get_extblock(inode, ext);
321 	if (IS_ERR(ext_bh))
322 		goto err_ext;
323 	map_bh(bh_result, sb, (sector_t)be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block)));
324 
325 	if (create) {
326 		u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr);
327 		if (!blocknr)
328 			goto err_alloc;
329 		set_buffer_new(bh_result);
330 		AFFS_I(inode)->mmu_private += AFFS_SB(sb)->s_data_blksize;
331 		AFFS_I(inode)->i_blkcnt++;
332 
333 		/* store new block */
334 		if (bh_result->b_blocknr)
335 			affs_warning(sb, "get_block",
336 				     "block already set (%llx)",
337 				     (unsigned long long)bh_result->b_blocknr);
338 		AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
339 		AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
340 		affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
341 		bh_result->b_blocknr = blocknr;
342 
343 		if (!block) {
344 			/* insert first block into header block */
345 			u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
346 			if (tmp)
347 				affs_warning(sb, "get_block", "first block already set (%d)", tmp);
348 			AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
349 			affs_adjust_checksum(ext_bh, blocknr - tmp);
350 		}
351 	}
352 
353 	affs_brelse(ext_bh);
354 	//unlock cache
355 	affs_unlock_ext(inode);
356 	return 0;
357 
358 err_big:
359 	affs_error(inode->i_sb, "get_block", "strange block request %llu",
360 		   (unsigned long long)block);
361 	return -EIO;
362 err_ext:
363 	// unlock cache
364 	affs_unlock_ext(inode);
365 	return PTR_ERR(ext_bh);
366 err_alloc:
367 	brelse(ext_bh);
368 	clear_buffer_mapped(bh_result);
369 	bh_result->b_bdev = NULL;
370 	// unlock cache
371 	affs_unlock_ext(inode);
372 	return -ENOSPC;
373 }
374 
375 static int affs_writepages(struct address_space *mapping,
376 			   struct writeback_control *wbc)
377 {
378 	return mpage_writepages(mapping, wbc, affs_get_block);
379 }
380 
381 static int affs_read_folio(struct file *file, struct folio *folio)
382 {
383 	return block_read_full_folio(folio, 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(struct kiocb *iocb, struct iov_iter *iter)
398 {
399 	struct file *file = iocb->ki_filp;
400 	struct address_space *mapping = file->f_mapping;
401 	struct inode *inode = mapping->host;
402 	size_t count = iov_iter_count(iter);
403 	loff_t offset = iocb->ki_pos;
404 	ssize_t ret;
405 
406 	if (iov_iter_rw(iter) == WRITE) {
407 		loff_t size = offset + count;
408 
409 		if (AFFS_I(inode)->mmu_private < size)
410 			return 0;
411 	}
412 
413 	ret = blockdev_direct_IO(iocb, inode, iter, affs_get_block);
414 	if (ret < 0 && iov_iter_rw(iter) == WRITE)
415 		affs_write_failed(mapping, offset + count);
416 	return ret;
417 }
418 
419 static int affs_write_begin(const struct kiocb *iocb,
420 			    struct address_space *mapping,
421 			    loff_t pos, unsigned len,
422 			    struct folio **foliop, void **fsdata)
423 {
424 	int ret;
425 
426 	ret = cont_write_begin(iocb, mapping, pos, len, foliop, fsdata,
427 				affs_get_block,
428 				&AFFS_I(mapping->host)->mmu_private);
429 	if (unlikely(ret))
430 		affs_write_failed(mapping, pos + len);
431 
432 	return ret;
433 }
434 
435 static int affs_write_end(const struct kiocb *iocb,
436 			  struct address_space *mapping, loff_t pos,
437 			  unsigned int len, unsigned int copied,
438 			  struct folio *folio, void *fsdata)
439 {
440 	struct inode *inode = mapping->host;
441 	int ret;
442 
443 	ret = generic_write_end(iocb, mapping, pos, len, copied, folio, fsdata);
444 
445 	/* Clear Archived bit on file writes, as AmigaOS would do */
446 	if (AFFS_I(inode)->i_protect & FIBF_ARCHIVED) {
447 		AFFS_I(inode)->i_protect &= ~FIBF_ARCHIVED;
448 		mark_inode_dirty(inode);
449 	}
450 
451 	return ret;
452 }
453 
454 static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
455 {
456 	return generic_block_bmap(mapping,block,affs_get_block);
457 }
458 
459 const struct address_space_operations affs_aops = {
460 	.dirty_folio	= block_dirty_folio,
461 	.invalidate_folio = block_invalidate_folio,
462 	.read_folio = affs_read_folio,
463 	.writepages = affs_writepages,
464 	.write_begin = affs_write_begin,
465 	.write_end = affs_write_end,
466 	.direct_IO = affs_direct_IO,
467 	.migrate_folio = buffer_migrate_folio,
468 	.bmap = _affs_bmap
469 };
470 
471 static inline struct buffer_head *
472 affs_bread_ino(struct inode *inode, int block, int create)
473 {
474 	struct buffer_head *bh, tmp_bh;
475 	int err;
476 
477 	tmp_bh.b_state = 0;
478 	err = affs_get_block(inode, block, &tmp_bh, create);
479 	if (!err) {
480 		bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
481 		if (bh) {
482 			bh->b_state |= tmp_bh.b_state;
483 			return bh;
484 		}
485 		err = -EIO;
486 	}
487 	return ERR_PTR(err);
488 }
489 
490 static inline struct buffer_head *
491 affs_getzeroblk_ino(struct inode *inode, int block)
492 {
493 	struct buffer_head *bh, tmp_bh;
494 	int err;
495 
496 	tmp_bh.b_state = 0;
497 	err = affs_get_block(inode, block, &tmp_bh, 1);
498 	if (!err) {
499 		bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
500 		if (bh) {
501 			bh->b_state |= tmp_bh.b_state;
502 			return bh;
503 		}
504 		err = -EIO;
505 	}
506 	return ERR_PTR(err);
507 }
508 
509 static inline struct buffer_head *
510 affs_getemptyblk_ino(struct inode *inode, int block)
511 {
512 	struct buffer_head *bh, tmp_bh;
513 	int err;
514 
515 	tmp_bh.b_state = 0;
516 	err = affs_get_block(inode, block, &tmp_bh, 1);
517 	if (!err) {
518 		bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
519 		if (bh) {
520 			bh->b_state |= tmp_bh.b_state;
521 			return bh;
522 		}
523 		err = -EIO;
524 	}
525 	return ERR_PTR(err);
526 }
527 
528 static int affs_do_read_folio_ofs(struct folio *folio, size_t to, int create)
529 {
530 	struct inode *inode = folio->mapping->host;
531 	struct super_block *sb = inode->i_sb;
532 	struct buffer_head *bh;
533 	size_t pos = 0;
534 	size_t bidx, boff, bsize;
535 	u32 tmp;
536 
537 	pr_debug("%s(%llu, %ld, 0, %zu)\n", __func__, inode->i_ino,
538 		 folio->index, to);
539 	BUG_ON(to > folio_size(folio));
540 	bsize = AFFS_SB(sb)->s_data_blksize;
541 	tmp = folio_pos(folio);
542 	bidx = tmp / bsize;
543 	boff = tmp % bsize;
544 
545 	while (pos < to) {
546 		bh = affs_bread_ino(inode, bidx, create);
547 		if (IS_ERR(bh))
548 			return PTR_ERR(bh);
549 		tmp = min(bsize - boff, to - pos);
550 		BUG_ON(pos + tmp > to || tmp > bsize);
551 		memcpy_to_folio(folio, pos, AFFS_DATA(bh) + boff, tmp);
552 		affs_brelse(bh);
553 		bidx++;
554 		pos += tmp;
555 		boff = 0;
556 	}
557 	return 0;
558 }
559 
560 static int
561 affs_extent_file_ofs(struct inode *inode, u32 newsize)
562 {
563 	struct super_block *sb = inode->i_sb;
564 	struct buffer_head *bh, *prev_bh;
565 	u32 bidx, boff;
566 	u32 size, bsize;
567 	u32 tmp;
568 
569 	pr_debug("%s(%llu, %d)\n", __func__, inode->i_ino, newsize);
570 	bsize = AFFS_SB(sb)->s_data_blksize;
571 	bh = NULL;
572 	size = AFFS_I(inode)->mmu_private;
573 	bidx = size / bsize;
574 	boff = size % bsize;
575 	if (boff) {
576 		bh = affs_bread_ino(inode, bidx, 0);
577 		if (IS_ERR(bh))
578 			return PTR_ERR(bh);
579 		tmp = min(bsize - boff, newsize - size);
580 		BUG_ON(boff + tmp > bsize || tmp > bsize);
581 		memset(AFFS_DATA(bh) + boff, 0, tmp);
582 		be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
583 		affs_fix_checksum(sb, bh);
584 		mmb_mark_buffer_dirty(bh, &AFFS_I(inode)->i_metadata_bhs);
585 		size += tmp;
586 		bidx++;
587 	} else if (bidx) {
588 		bh = affs_bread_ino(inode, bidx - 1, 0);
589 		if (IS_ERR(bh))
590 			return PTR_ERR(bh);
591 	}
592 
593 	while (size < newsize) {
594 		prev_bh = bh;
595 		bh = affs_getzeroblk_ino(inode, bidx);
596 		if (IS_ERR(bh))
597 			goto out;
598 		tmp = min(bsize, newsize - size);
599 		BUG_ON(tmp > bsize);
600 		AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
601 		AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
602 		AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx + 1);
603 		AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
604 		affs_fix_checksum(sb, bh);
605 		bh->b_state &= ~(1UL << BH_New);
606 		mmb_mark_buffer_dirty(bh, &AFFS_I(inode)->i_metadata_bhs);
607 		if (prev_bh) {
608 			u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
609 
610 			if (tmp_next)
611 				affs_warning(sb, "extent_file_ofs",
612 					     "next block already set for %d (%d)",
613 					     bidx, tmp_next);
614 			AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
615 			affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
616 			mmb_mark_buffer_dirty(prev_bh,
617 					      &AFFS_I(inode)->i_metadata_bhs);
618 			affs_brelse(prev_bh);
619 		}
620 		size += bsize;
621 		bidx++;
622 	}
623 	affs_brelse(bh);
624 	inode->i_size = AFFS_I(inode)->mmu_private = newsize;
625 	return 0;
626 
627 out:
628 	inode->i_size = AFFS_I(inode)->mmu_private = newsize;
629 	return PTR_ERR(bh);
630 }
631 
632 static int affs_read_folio_ofs(struct file *file, struct folio *folio)
633 {
634 	struct inode *inode = folio->mapping->host;
635 	size_t to;
636 	int err;
637 
638 	pr_debug("%s(%llu, %ld)\n", __func__, inode->i_ino, folio->index);
639 	to = folio_size(folio);
640 	if (folio_pos(folio) + to > inode->i_size) {
641 		to = inode->i_size - folio_pos(folio);
642 		folio_zero_segment(folio, to, folio_size(folio));
643 	}
644 
645 	err = affs_do_read_folio_ofs(folio, to, 0);
646 	if (!err)
647 		folio_mark_uptodate(folio);
648 	folio_unlock(folio);
649 	return err;
650 }
651 
652 static int affs_write_begin_ofs(const struct kiocb *iocb,
653 				struct address_space *mapping,
654 				loff_t pos, unsigned len,
655 				struct folio **foliop, void **fsdata)
656 {
657 	struct inode *inode = mapping->host;
658 	struct folio *folio;
659 	pgoff_t index;
660 	int err = 0;
661 
662 	pr_debug("%s(%llu, %llu, %llu)\n", __func__, inode->i_ino, pos,
663 		 pos + len);
664 	if (pos > AFFS_I(inode)->mmu_private) {
665 		/* XXX: this probably leaves a too-big i_size in case of
666 		 * failure. Should really be updating i_size at write_end time
667 		 */
668 		err = affs_extent_file_ofs(inode, pos);
669 		if (err)
670 			return err;
671 	}
672 
673 	index = pos >> PAGE_SHIFT;
674 	folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
675 			mapping_gfp_mask(mapping));
676 	if (IS_ERR(folio))
677 		return PTR_ERR(folio);
678 	*foliop = folio;
679 
680 	if (folio_test_uptodate(folio))
681 		return 0;
682 
683 	/* XXX: inefficient but safe in the face of short writes */
684 	err = affs_do_read_folio_ofs(folio, folio_size(folio), 1);
685 	if (err) {
686 		folio_unlock(folio);
687 		folio_put(folio);
688 	}
689 	return err;
690 }
691 
692 static int affs_write_end_ofs(const struct kiocb *iocb,
693 			      struct address_space *mapping,
694 			      loff_t pos, unsigned len, unsigned copied,
695 			      struct folio *folio, void *fsdata)
696 {
697 	struct inode *inode = mapping->host;
698 	struct super_block *sb = inode->i_sb;
699 	struct buffer_head *bh, *prev_bh;
700 	char *data;
701 	u32 bidx, boff, bsize;
702 	unsigned from, to;
703 	u32 tmp;
704 	int written;
705 
706 	from = pos & (PAGE_SIZE - 1);
707 	to = from + len;
708 	/*
709 	 * XXX: not sure if this can handle short copies (len < copied), but
710 	 * we don't have to, because the folio should always be uptodate here,
711 	 * due to write_begin.
712 	 */
713 
714 	pr_debug("%s(%llu, %llu, %llu)\n", __func__, inode->i_ino, pos,
715 		 pos + len);
716 	bsize = AFFS_SB(sb)->s_data_blksize;
717 	data = folio_address(folio);
718 
719 	bh = NULL;
720 	written = 0;
721 	tmp = (folio->index << PAGE_SHIFT) + from;
722 	bidx = tmp / bsize;
723 	boff = tmp % bsize;
724 	if (boff) {
725 		bh = affs_bread_ino(inode, bidx, 0);
726 		if (IS_ERR(bh)) {
727 			written = PTR_ERR(bh);
728 			goto err_first_bh;
729 		}
730 		tmp = min(bsize - boff, to - from);
731 		BUG_ON(boff + tmp > bsize || tmp > bsize);
732 		memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
733 		AFFS_DATA_HEAD(bh)->size = cpu_to_be32(
734 			max(boff + tmp, be32_to_cpu(AFFS_DATA_HEAD(bh)->size)));
735 		affs_fix_checksum(sb, bh);
736 		mmb_mark_buffer_dirty(bh, &AFFS_I(inode)->i_metadata_bhs);
737 		written += tmp;
738 		from += tmp;
739 		bidx++;
740 	} else if (bidx) {
741 		bh = affs_bread_ino(inode, bidx - 1, 0);
742 		if (IS_ERR(bh)) {
743 			written = PTR_ERR(bh);
744 			goto err_first_bh;
745 		}
746 	}
747 	while (from + bsize <= to) {
748 		prev_bh = bh;
749 		bh = affs_getemptyblk_ino(inode, bidx);
750 		if (IS_ERR(bh))
751 			goto err_bh;
752 		memcpy(AFFS_DATA(bh), data + from, bsize);
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 + 1);
757 			AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize);
758 			AFFS_DATA_HEAD(bh)->next = 0;
759 			bh->b_state &= ~(1UL << BH_New);
760 			if (prev_bh) {
761 				u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
762 
763 				if (tmp_next)
764 					affs_warning(sb, "commit_write_ofs",
765 						     "next block already set for %d (%d)",
766 						     bidx, tmp_next);
767 				AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
768 				affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
769 				mmb_mark_buffer_dirty(prev_bh,
770 					&AFFS_I(inode)->i_metadata_bhs);
771 			}
772 		}
773 		affs_brelse(prev_bh);
774 		affs_fix_checksum(sb, bh);
775 		mmb_mark_buffer_dirty(bh, &AFFS_I(inode)->i_metadata_bhs);
776 		written += bsize;
777 		from += bsize;
778 		bidx++;
779 	}
780 	if (from < to) {
781 		prev_bh = bh;
782 		bh = affs_bread_ino(inode, bidx, 1);
783 		if (IS_ERR(bh))
784 			goto err_bh;
785 		tmp = min(bsize, to - from);
786 		BUG_ON(tmp > bsize);
787 		memcpy(AFFS_DATA(bh), data + from, tmp);
788 		if (buffer_new(bh)) {
789 			AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
790 			AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
791 			AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx + 1);
792 			AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
793 			AFFS_DATA_HEAD(bh)->next = 0;
794 			bh->b_state &= ~(1UL << BH_New);
795 			if (prev_bh) {
796 				u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
797 
798 				if (tmp_next)
799 					affs_warning(sb, "commit_write_ofs",
800 						     "next block already set for %d (%d)",
801 						     bidx, tmp_next);
802 				AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
803 				affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
804 				mmb_mark_buffer_dirty(prev_bh,
805 						&AFFS_I(inode)->i_metadata_bhs);
806 			}
807 		} else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp)
808 			AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
809 		affs_brelse(prev_bh);
810 		affs_fix_checksum(sb, bh);
811 		mmb_mark_buffer_dirty(bh, &AFFS_I(inode)->i_metadata_bhs);
812 		written += tmp;
813 		from += tmp;
814 		bidx++;
815 	}
816 	folio_mark_uptodate(folio);
817 
818 done:
819 	affs_brelse(bh);
820 	tmp = (folio->index << PAGE_SHIFT) + from;
821 	if (tmp > inode->i_size)
822 		inode->i_size = AFFS_I(inode)->mmu_private = tmp;
823 
824 	/* Clear Archived bit on file writes, as AmigaOS would do */
825 	if (AFFS_I(inode)->i_protect & FIBF_ARCHIVED) {
826 		AFFS_I(inode)->i_protect &= ~FIBF_ARCHIVED;
827 		mark_inode_dirty(inode);
828 	}
829 
830 err_first_bh:
831 	folio_unlock(folio);
832 	folio_put(folio);
833 
834 	return written;
835 
836 err_bh:
837 	bh = prev_bh;
838 	if (!written)
839 		written = PTR_ERR(bh);
840 	goto done;
841 }
842 
843 const struct address_space_operations affs_aops_ofs = {
844 	.dirty_folio	= block_dirty_folio,
845 	.invalidate_folio = block_invalidate_folio,
846 	.read_folio = affs_read_folio_ofs,
847 	//.writepages = affs_writepages_ofs,
848 	.write_begin = affs_write_begin_ofs,
849 	.write_end = affs_write_end_ofs,
850 	.migrate_folio = filemap_migrate_folio,
851 };
852 
853 /* Free any preallocated blocks. */
854 
855 void
856 affs_free_prealloc(struct inode *inode)
857 {
858 	struct super_block *sb = inode->i_sb;
859 
860 	pr_debug("free_prealloc(ino=%llu)\n", inode->i_ino);
861 
862 	while (AFFS_I(inode)->i_pa_cnt) {
863 		AFFS_I(inode)->i_pa_cnt--;
864 		affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc);
865 	}
866 }
867 
868 /* Truncate (or enlarge) a file to the requested size. */
869 
870 void
871 affs_truncate(struct inode *inode)
872 {
873 	struct super_block *sb = inode->i_sb;
874 	u32 ext, ext_key;
875 	u32 last_blk, blkcnt, blk;
876 	u32 size;
877 	struct buffer_head *ext_bh;
878 	int i;
879 
880 	pr_debug("truncate(inode=%llu, oldsize=%llu, newsize=%llu)\n",
881 		 inode->i_ino, AFFS_I(inode)->mmu_private, inode->i_size);
882 
883 	last_blk = 0;
884 	ext = 0;
885 	if (inode->i_size) {
886 		last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize;
887 		ext = last_blk / AFFS_SB(sb)->s_hashsize;
888 	}
889 
890 	if (inode->i_size > AFFS_I(inode)->mmu_private) {
891 		struct address_space *mapping = inode->i_mapping;
892 		struct folio *folio;
893 		void *fsdata = NULL;
894 		loff_t isize = inode->i_size;
895 		int res;
896 
897 		res = mapping->a_ops->write_begin(NULL, mapping, isize, 0, &folio, &fsdata);
898 		if (!res)
899 			res = mapping->a_ops->write_end(NULL, mapping, isize, 0, 0, folio, fsdata);
900 		else
901 			inode->i_size = AFFS_I(inode)->mmu_private;
902 		mark_inode_dirty(inode);
903 		return;
904 	} else if (inode->i_size == AFFS_I(inode)->mmu_private)
905 		return;
906 
907 	// lock cache
908 	ext_bh = affs_get_extblock(inode, ext);
909 	if (IS_ERR(ext_bh)) {
910 		affs_warning(sb, "truncate",
911 			     "unexpected read error for ext block %u (%ld)",
912 			     ext, PTR_ERR(ext_bh));
913 		return;
914 	}
915 	if (AFFS_I(inode)->i_lc) {
916 		/* clear linear cache */
917 		i = (ext + 1) >> AFFS_I(inode)->i_lc_shift;
918 		if (AFFS_I(inode)->i_lc_size > i) {
919 			AFFS_I(inode)->i_lc_size = i;
920 			for (; i < AFFS_LC_SIZE; i++)
921 				AFFS_I(inode)->i_lc[i] = 0;
922 		}
923 		/* clear associative cache */
924 		for (i = 0; i < AFFS_AC_SIZE; i++)
925 			if (AFFS_I(inode)->i_ac[i].ext >= ext)
926 				AFFS_I(inode)->i_ac[i].ext = 0;
927 	}
928 	ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
929 
930 	blkcnt = AFFS_I(inode)->i_blkcnt;
931 	i = 0;
932 	blk = last_blk;
933 	if (inode->i_size) {
934 		i = last_blk % AFFS_SB(sb)->s_hashsize + 1;
935 		blk++;
936 	} else
937 		AFFS_HEAD(ext_bh)->first_data = 0;
938 	AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i);
939 	size = AFFS_SB(sb)->s_hashsize;
940 	if (size > blkcnt - blk + i)
941 		size = blkcnt - blk + i;
942 	for (; i < size; i++, blk++) {
943 		affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
944 		AFFS_BLOCK(sb, ext_bh, i) = 0;
945 	}
946 	AFFS_TAIL(sb, ext_bh)->extension = 0;
947 	affs_fix_checksum(sb, ext_bh);
948 	mmb_mark_buffer_dirty(ext_bh, &AFFS_I(inode)->i_metadata_bhs);
949 	affs_brelse(ext_bh);
950 
951 	if (inode->i_size) {
952 		AFFS_I(inode)->i_blkcnt = last_blk + 1;
953 		AFFS_I(inode)->i_extcnt = ext + 1;
954 		if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS)) {
955 			struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
956 			u32 tmp;
957 			if (IS_ERR(bh)) {
958 				affs_warning(sb, "truncate",
959 					     "unexpected read error for last block %u (%ld)",
960 					     ext, PTR_ERR(bh));
961 				return;
962 			}
963 			tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
964 			AFFS_DATA_HEAD(bh)->next = 0;
965 			affs_adjust_checksum(bh, -tmp);
966 			affs_brelse(bh);
967 		}
968 	} else {
969 		AFFS_I(inode)->i_blkcnt = 0;
970 		AFFS_I(inode)->i_extcnt = 1;
971 	}
972 	AFFS_I(inode)->mmu_private = inode->i_size;
973 	// unlock cache
974 
975 	while (ext_key) {
976 		ext_bh = affs_bread(sb, ext_key);
977 		size = AFFS_SB(sb)->s_hashsize;
978 		if (size > blkcnt - blk)
979 			size = blkcnt - blk;
980 		for (i = 0; i < size; i++, blk++)
981 			affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
982 		affs_free_block(sb, ext_key);
983 		ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
984 		affs_brelse(ext_bh);
985 	}
986 	affs_free_prealloc(inode);
987 }
988 
989 int affs_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
990 {
991 	struct inode *inode = filp->f_mapping->host;
992 	int ret, err;
993 
994 	err = file_write_and_wait_range(filp, start, end);
995 	if (err)
996 		return err;
997 
998 	inode_lock(inode);
999 	ret = write_inode_now(inode, 0);
1000 	err = sync_blockdev(inode->i_sb->s_bdev);
1001 	if (!ret)
1002 		ret = err;
1003 	inode_unlock(inode);
1004 	return ret;
1005 }
1006 const struct file_operations affs_file_operations = {
1007 	.llseek		= generic_file_llseek,
1008 	.read_iter	= generic_file_read_iter,
1009 	.write_iter	= generic_file_write_iter,
1010 	.mmap_prepare	= generic_file_mmap_prepare,
1011 	.open		= affs_file_open,
1012 	.release	= affs_file_release,
1013 	.fsync		= affs_file_fsync,
1014 	.splice_read	= filemap_splice_read,
1015 	.setlease	= generic_setlease,
1016 };
1017 
1018 const struct inode_operations affs_file_inode_operations = {
1019 	.setattr	= affs_setattr,
1020 };
1021