blob: 8e510854f487e979d657cebc30297f4dc7663e5c [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
15#include "affs.h"
16
17#if PAGE_SIZE < 4096
18#error PAGE_SIZE must be at least 4096
19#endif
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23static int
24affs_file_open(struct inode *inode, struct file *filp)
25{
Fabian Frederick9606d9a2014-06-06 14:37:25 -070026 pr_debug("open(%lu,%d)\n",
Roman Zippeldca3c332008-04-29 17:02:20 +020027 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
28 atomic_inc(&AFFS_I(inode)->i_opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 return 0;
30}
31
32static int
33affs_file_release(struct inode *inode, struct file *filp)
34{
Fabian Frederick9606d9a2014-06-06 14:37:25 -070035 pr_debug("release(%lu, %d)\n",
Roman Zippeldca3c332008-04-29 17:02:20 +020036 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
37
38 if (atomic_dec_and_test(&AFFS_I(inode)->i_opencnt)) {
39 mutex_lock(&inode->i_mutex);
40 if (inode->i_size != AFFS_I(inode)->mmu_private)
41 affs_truncate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 affs_free_prealloc(inode);
Roman Zippeldca3c332008-04-29 17:02:20 +020043 mutex_unlock(&inode->i_mutex);
44 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 return 0;
47}
48
49static int
50affs_grow_extcache(struct inode *inode, u32 lc_idx)
51{
52 struct super_block *sb = inode->i_sb;
53 struct buffer_head *bh;
54 u32 lc_max;
55 int i, j, key;
56
57 if (!AFFS_I(inode)->i_lc) {
58 char *ptr = (char *)get_zeroed_page(GFP_NOFS);
59 if (!ptr)
60 return -ENOMEM;
61 AFFS_I(inode)->i_lc = (u32 *)ptr;
62 AFFS_I(inode)->i_ac = (struct affs_ext_key *)(ptr + AFFS_CACHE_SIZE / 2);
63 }
64
65 lc_max = AFFS_LC_SIZE << AFFS_I(inode)->i_lc_shift;
66
67 if (AFFS_I(inode)->i_extcnt > lc_max) {
68 u32 lc_shift, lc_mask, tmp, off;
69
70 /* need to recalculate linear cache, start from old size */
71 lc_shift = AFFS_I(inode)->i_lc_shift;
72 tmp = (AFFS_I(inode)->i_extcnt / AFFS_LC_SIZE) >> lc_shift;
73 for (; tmp; tmp >>= 1)
74 lc_shift++;
75 lc_mask = (1 << lc_shift) - 1;
76
77 /* fix idx and old size to new shift */
78 lc_idx >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
79 AFFS_I(inode)->i_lc_size >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
80
81 /* first shrink old cache to make more space */
82 off = 1 << (lc_shift - AFFS_I(inode)->i_lc_shift);
83 for (i = 1, j = off; j < AFFS_LC_SIZE; i++, j += off)
84 AFFS_I(inode)->i_ac[i] = AFFS_I(inode)->i_ac[j];
85
86 AFFS_I(inode)->i_lc_shift = lc_shift;
87 AFFS_I(inode)->i_lc_mask = lc_mask;
88 }
89
90 /* fill cache to the needed index */
91 i = AFFS_I(inode)->i_lc_size;
92 AFFS_I(inode)->i_lc_size = lc_idx + 1;
93 for (; i <= lc_idx; i++) {
94 if (!i) {
95 AFFS_I(inode)->i_lc[0] = inode->i_ino;
96 continue;
97 }
98 key = AFFS_I(inode)->i_lc[i - 1];
99 j = AFFS_I(inode)->i_lc_mask + 1;
100 // unlock cache
101 for (; j > 0; j--) {
102 bh = affs_bread(sb, key);
103 if (!bh)
104 goto err;
105 key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
106 affs_brelse(bh);
107 }
108 // lock cache
109 AFFS_I(inode)->i_lc[i] = key;
110 }
111
112 return 0;
113
114err:
115 // lock cache
116 return -EIO;
117}
118
119static struct buffer_head *
120affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext)
121{
122 struct super_block *sb = inode->i_sb;
123 struct buffer_head *new_bh;
124 u32 blocknr, tmp;
125
126 blocknr = affs_alloc_block(inode, bh->b_blocknr);
127 if (!blocknr)
128 return ERR_PTR(-ENOSPC);
129
130 new_bh = affs_getzeroblk(sb, blocknr);
131 if (!new_bh) {
132 affs_free_block(sb, blocknr);
133 return ERR_PTR(-EIO);
134 }
135
136 AFFS_HEAD(new_bh)->ptype = cpu_to_be32(T_LIST);
137 AFFS_HEAD(new_bh)->key = cpu_to_be32(blocknr);
138 AFFS_TAIL(sb, new_bh)->stype = cpu_to_be32(ST_FILE);
139 AFFS_TAIL(sb, new_bh)->parent = cpu_to_be32(inode->i_ino);
140 affs_fix_checksum(sb, new_bh);
141
142 mark_buffer_dirty_inode(new_bh, inode);
143
144 tmp = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
145 if (tmp)
146 affs_warning(sb, "alloc_ext", "previous extension set (%x)", tmp);
147 AFFS_TAIL(sb, bh)->extension = cpu_to_be32(blocknr);
148 affs_adjust_checksum(bh, blocknr - tmp);
149 mark_buffer_dirty_inode(bh, inode);
150
151 AFFS_I(inode)->i_extcnt++;
152 mark_inode_dirty(inode);
153
154 return new_bh;
155}
156
157static inline struct buffer_head *
158affs_get_extblock(struct inode *inode, u32 ext)
159{
160 /* inline the simplest case: same extended block as last time */
161 struct buffer_head *bh = AFFS_I(inode)->i_ext_bh;
162 if (ext == AFFS_I(inode)->i_ext_last)
Roman Zippeldca3c332008-04-29 17:02:20 +0200163 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 else
165 /* we have to do more (not inlined) */
166 bh = affs_get_extblock_slow(inode, ext);
167
168 return bh;
169}
170
171static struct buffer_head *
172affs_get_extblock_slow(struct inode *inode, u32 ext)
173{
174 struct super_block *sb = inode->i_sb;
175 struct buffer_head *bh;
176 u32 ext_key;
177 u32 lc_idx, lc_off, ac_idx;
178 u32 tmp, idx;
179
180 if (ext == AFFS_I(inode)->i_ext_last + 1) {
181 /* read the next extended block from the current one */
182 bh = AFFS_I(inode)->i_ext_bh;
183 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
184 if (ext < AFFS_I(inode)->i_extcnt)
185 goto read_ext;
186 if (ext > AFFS_I(inode)->i_extcnt)
187 BUG();
188 bh = affs_alloc_extblock(inode, bh, ext);
189 if (IS_ERR(bh))
190 return bh;
191 goto store_ext;
192 }
193
194 if (ext == 0) {
195 /* we seek back to the file header block */
196 ext_key = inode->i_ino;
197 goto read_ext;
198 }
199
200 if (ext >= AFFS_I(inode)->i_extcnt) {
201 struct buffer_head *prev_bh;
202
203 /* allocate a new extended block */
204 if (ext > AFFS_I(inode)->i_extcnt)
205 BUG();
206
207 /* get previous extended block */
208 prev_bh = affs_get_extblock(inode, ext - 1);
209 if (IS_ERR(prev_bh))
210 return prev_bh;
211 bh = affs_alloc_extblock(inode, prev_bh, ext);
212 affs_brelse(prev_bh);
213 if (IS_ERR(bh))
214 return bh;
215 goto store_ext;
216 }
217
218again:
219 /* check if there is an extended cache and whether it's large enough */
220 lc_idx = ext >> AFFS_I(inode)->i_lc_shift;
221 lc_off = ext & AFFS_I(inode)->i_lc_mask;
222
223 if (lc_idx >= AFFS_I(inode)->i_lc_size) {
224 int err;
225
226 err = affs_grow_extcache(inode, lc_idx);
227 if (err)
228 return ERR_PTR(err);
229 goto again;
230 }
231
232 /* every n'th key we find in the linear cache */
233 if (!lc_off) {
234 ext_key = AFFS_I(inode)->i_lc[lc_idx];
235 goto read_ext;
236 }
237
238 /* maybe it's still in the associative cache */
239 ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK;
240 if (AFFS_I(inode)->i_ac[ac_idx].ext == ext) {
241 ext_key = AFFS_I(inode)->i_ac[ac_idx].key;
242 goto read_ext;
243 }
244
245 /* try to find one of the previous extended blocks */
246 tmp = ext;
247 idx = ac_idx;
248 while (--tmp, --lc_off > 0) {
249 idx = (idx - 1) & AFFS_AC_MASK;
250 if (AFFS_I(inode)->i_ac[idx].ext == tmp) {
251 ext_key = AFFS_I(inode)->i_ac[idx].key;
252 goto find_ext;
253 }
254 }
255
256 /* fall back to the linear cache */
257 ext_key = AFFS_I(inode)->i_lc[lc_idx];
258find_ext:
259 /* read all extended blocks until we find the one we need */
260 //unlock cache
261 do {
262 bh = affs_bread(sb, ext_key);
263 if (!bh)
264 goto err_bread;
265 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
266 affs_brelse(bh);
267 tmp++;
268 } while (tmp < ext);
269 //lock cache
270
271 /* store it in the associative cache */
272 // recalculate ac_idx?
273 AFFS_I(inode)->i_ac[ac_idx].ext = ext;
274 AFFS_I(inode)->i_ac[ac_idx].key = ext_key;
275
276read_ext:
277 /* finally read the right extended block */
278 //unlock cache
279 bh = affs_bread(sb, ext_key);
280 if (!bh)
281 goto err_bread;
282 //lock cache
283
284store_ext:
285 /* release old cached extended block and store the new one */
286 affs_brelse(AFFS_I(inode)->i_ext_bh);
287 AFFS_I(inode)->i_ext_last = ext;
288 AFFS_I(inode)->i_ext_bh = bh;
Roman Zippeldca3c332008-04-29 17:02:20 +0200289 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 return bh;
292
293err_bread:
294 affs_brelse(bh);
295 return ERR_PTR(-EIO);
296}
297
298static int
299affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
300{
301 struct super_block *sb = inode->i_sb;
302 struct buffer_head *ext_bh;
303 u32 ext;
304
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700305 pr_debug("%s(%u, %lu)\n",
306 __func__, (u32)inode->i_ino, (unsigned long)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Julia Lawall8d4b6902008-04-29 00:59:12 -0700308 BUG_ON(block > (sector_t)0x7fffffffUL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 if (block >= AFFS_I(inode)->i_blkcnt) {
311 if (block > AFFS_I(inode)->i_blkcnt || !create)
312 goto err_big;
313 } else
314 create = 0;
315
316 //lock cache
317 affs_lock_ext(inode);
318
319 ext = (u32)block / AFFS_SB(sb)->s_hashsize;
320 block -= ext * AFFS_SB(sb)->s_hashsize;
321 ext_bh = affs_get_extblock(inode, ext);
322 if (IS_ERR(ext_bh))
323 goto err_ext;
324 map_bh(bh_result, sb, (sector_t)be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block)));
325
326 if (create) {
327 u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr);
328 if (!blocknr)
329 goto err_alloc;
330 set_buffer_new(bh_result);
331 AFFS_I(inode)->mmu_private += AFFS_SB(sb)->s_data_blksize;
332 AFFS_I(inode)->i_blkcnt++;
333
334 /* store new block */
335 if (bh_result->b_blocknr)
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800336 affs_warning(sb, "get_block", "block already set (%lx)",
337 (unsigned long)bh_result->b_blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 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
358err_big:
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800359 affs_error(inode->i_sb, "get_block", "strange block request %d",
360 (int)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -EIO;
362err_ext:
363 // unlock cache
364 affs_unlock_ext(inode);
365 return PTR_ERR(ext_bh);
366err_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
375static int affs_writepage(struct page *page, struct writeback_control *wbc)
376{
377 return block_write_full_page(page, affs_get_block, wbc);
378}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static int affs_readpage(struct file *file, struct page *page)
381{
382 return block_read_full_page(page, affs_get_block);
383}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700384
Marco Stornelli1dc18342012-12-15 11:51:53 +0100385static void affs_write_failed(struct address_space *mapping, loff_t to)
386{
387 struct inode *inode = mapping->host;
388
389 if (to > inode->i_size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700390 truncate_pagecache(inode, inode->i_size);
Marco Stornelli1dc18342012-12-15 11:51:53 +0100391 affs_truncate(inode);
392 }
393}
394
Nick Pigginf2b6a162007-10-16 01:25:24 -0700395static int affs_write_begin(struct file *file, struct address_space *mapping,
396 loff_t pos, unsigned len, unsigned flags,
397 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Christoph Hellwig282dc172010-06-04 11:29:55 +0200399 int ret;
400
Nick Pigginf2b6a162007-10-16 01:25:24 -0700401 *pagep = NULL;
Christoph Hellwig282dc172010-06-04 11:29:55 +0200402 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700403 affs_get_block,
404 &AFFS_I(mapping->host)->mmu_private);
Marco Stornelli1dc18342012-12-15 11:51:53 +0100405 if (unlikely(ret))
406 affs_write_failed(mapping, pos + len);
Christoph Hellwig282dc172010-06-04 11:29:55 +0200407
408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
412{
413 return generic_block_bmap(mapping,block,affs_get_block);
414}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700415
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700416const struct address_space_operations affs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 .readpage = affs_readpage,
418 .writepage = affs_writepage,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700419 .write_begin = affs_write_begin,
420 .write_end = generic_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 .bmap = _affs_bmap
422};
423
424static inline struct buffer_head *
425affs_bread_ino(struct inode *inode, int block, int create)
426{
427 struct buffer_head *bh, tmp_bh;
428 int err;
429
430 tmp_bh.b_state = 0;
431 err = affs_get_block(inode, block, &tmp_bh, create);
432 if (!err) {
433 bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
434 if (bh) {
435 bh->b_state |= tmp_bh.b_state;
436 return bh;
437 }
438 err = -EIO;
439 }
440 return ERR_PTR(err);
441}
442
443static inline struct buffer_head *
444affs_getzeroblk_ino(struct inode *inode, int block)
445{
446 struct buffer_head *bh, tmp_bh;
447 int err;
448
449 tmp_bh.b_state = 0;
450 err = affs_get_block(inode, block, &tmp_bh, 1);
451 if (!err) {
452 bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
453 if (bh) {
454 bh->b_state |= tmp_bh.b_state;
455 return bh;
456 }
457 err = -EIO;
458 }
459 return ERR_PTR(err);
460}
461
462static inline struct buffer_head *
463affs_getemptyblk_ino(struct inode *inode, int block)
464{
465 struct buffer_head *bh, tmp_bh;
466 int err;
467
468 tmp_bh.b_state = 0;
469 err = affs_get_block(inode, block, &tmp_bh, 1);
470 if (!err) {
471 bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
472 if (bh) {
473 bh->b_state |= tmp_bh.b_state;
474 return bh;
475 }
476 err = -EIO;
477 }
478 return ERR_PTR(err);
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static int
Fabian Frederick0c89d672014-06-06 14:37:23 -0700482affs_do_readpage_ofs(struct page *page, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct inode *inode = page->mapping->host;
485 struct super_block *sb = inode->i_sb;
486 struct buffer_head *bh;
487 char *data;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700488 unsigned pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 u32 bidx, boff, bsize;
490 u32 tmp;
491
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700492 pr_debug("%s(%u, %ld, 0, %d)\n", __func__, (u32)inode->i_ino,
Fabian Frederick0c89d672014-06-06 14:37:23 -0700493 page->index, to);
494 BUG_ON(to > PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 kmap(page);
496 data = page_address(page);
497 bsize = AFFS_SB(sb)->s_data_blksize;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700498 tmp = page->index << PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 bidx = tmp / bsize;
500 boff = tmp % bsize;
501
Fabian Frederick0c89d672014-06-06 14:37:23 -0700502 while (pos < to) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 bh = affs_bread_ino(inode, bidx, 0);
504 if (IS_ERR(bh))
505 return PTR_ERR(bh);
Fabian Frederick0c89d672014-06-06 14:37:23 -0700506 tmp = min(bsize - boff, to - pos);
507 BUG_ON(pos + tmp > to || tmp > bsize);
508 memcpy(data + pos, AFFS_DATA(bh) + boff, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 affs_brelse(bh);
510 bidx++;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700511 pos += tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 boff = 0;
513 }
514 flush_dcache_page(page);
515 kunmap(page);
516 return 0;
517}
518
519static int
520affs_extent_file_ofs(struct inode *inode, u32 newsize)
521{
522 struct super_block *sb = inode->i_sb;
523 struct buffer_head *bh, *prev_bh;
524 u32 bidx, boff;
525 u32 size, bsize;
526 u32 tmp;
527
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700528 pr_debug("%s(%u, %d)\n", __func__, (u32)inode->i_ino, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 bsize = AFFS_SB(sb)->s_data_blksize;
530 bh = NULL;
531 size = AFFS_I(inode)->mmu_private;
532 bidx = size / bsize;
533 boff = size % bsize;
534 if (boff) {
535 bh = affs_bread_ino(inode, bidx, 0);
536 if (IS_ERR(bh))
537 return PTR_ERR(bh);
538 tmp = min(bsize - boff, newsize - size);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700539 BUG_ON(boff + tmp > bsize || tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 memset(AFFS_DATA(bh) + boff, 0, tmp);
Marcin Slusarz6369a4a2008-04-30 00:54:47 -0700541 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 affs_fix_checksum(sb, bh);
543 mark_buffer_dirty_inode(bh, inode);
544 size += tmp;
545 bidx++;
546 } else if (bidx) {
547 bh = affs_bread_ino(inode, bidx - 1, 0);
548 if (IS_ERR(bh))
549 return PTR_ERR(bh);
550 }
551
552 while (size < newsize) {
553 prev_bh = bh;
554 bh = affs_getzeroblk_ino(inode, bidx);
555 if (IS_ERR(bh))
556 goto out;
557 tmp = min(bsize, newsize - size);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700558 BUG_ON(tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
560 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
561 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
562 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
563 affs_fix_checksum(sb, bh);
564 bh->b_state &= ~(1UL << BH_New);
565 mark_buffer_dirty_inode(bh, inode);
566 if (prev_bh) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700567 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
568
569 if (tmp_next)
570 affs_warning(sb, "extent_file_ofs",
571 "next block already set for %d (%d)",
572 bidx, tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700574 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 mark_buffer_dirty_inode(prev_bh, inode);
576 affs_brelse(prev_bh);
577 }
578 size += bsize;
579 bidx++;
580 }
581 affs_brelse(bh);
582 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
583 return 0;
584
585out:
586 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
587 return PTR_ERR(bh);
588}
589
590static int
591affs_readpage_ofs(struct file *file, struct page *page)
592{
593 struct inode *inode = page->mapping->host;
594 u32 to;
595 int err;
596
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700597 pr_debug("%s(%u, %ld)\n", __func__, (u32)inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 to = PAGE_CACHE_SIZE;
599 if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) {
600 to = inode->i_size & ~PAGE_CACHE_MASK;
601 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
602 }
603
Fabian Frederick0c89d672014-06-06 14:37:23 -0700604 err = affs_do_readpage_ofs(page, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!err)
606 SetPageUptodate(page);
607 unlock_page(page);
608 return err;
609}
610
Nick Pigginf2b6a162007-10-16 01:25:24 -0700611static int affs_write_begin_ofs(struct file *file, struct address_space *mapping,
612 loff_t pos, unsigned len, unsigned flags,
613 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Nick Pigginf2b6a162007-10-16 01:25:24 -0700615 struct inode *inode = mapping->host;
616 struct page *page;
617 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int err = 0;
619
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700620 pr_debug("%s(%u, %llu, %llu)\n", __func__, (u32)inode->i_ino,
621 (unsigned long long)pos, (unsigned long long)pos + len);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700622 if (pos > AFFS_I(inode)->mmu_private) {
623 /* XXX: this probably leaves a too-big i_size in case of
624 * failure. Should really be updating i_size at write_end time
625 */
626 err = affs_extent_file_ofs(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (err)
628 return err;
629 }
Nick Pigginf2b6a162007-10-16 01:25:24 -0700630
631 index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin54566b22009-01-04 12:00:53 -0800632 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700633 if (!page)
634 return -ENOMEM;
635 *pagep = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (PageUptodate(page))
638 return 0;
639
Nick Pigginf2b6a162007-10-16 01:25:24 -0700640 /* XXX: inefficient but safe in the face of short writes */
Fabian Frederick0c89d672014-06-06 14:37:23 -0700641 err = affs_do_readpage_ofs(page, PAGE_CACHE_SIZE);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700642 if (err) {
643 unlock_page(page);
644 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 return err;
647}
648
Nick Pigginf2b6a162007-10-16 01:25:24 -0700649static int affs_write_end_ofs(struct file *file, struct address_space *mapping,
650 loff_t pos, unsigned len, unsigned copied,
651 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Nick Pigginf2b6a162007-10-16 01:25:24 -0700653 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct super_block *sb = inode->i_sb;
655 struct buffer_head *bh, *prev_bh;
656 char *data;
657 u32 bidx, boff, bsize;
Nick Pigginf2b6a162007-10-16 01:25:24 -0700658 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 u32 tmp;
660 int written;
661
Nick Pigginf2b6a162007-10-16 01:25:24 -0700662 from = pos & (PAGE_CACHE_SIZE - 1);
663 to = pos + len;
664 /*
665 * XXX: not sure if this can handle short copies (len < copied), but
666 * we don't have to, because the page should always be uptodate here,
667 * due to write_begin.
668 */
669
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700670 pr_debug("%s(%u, %llu, %llu)\n",
671 __func__, (u32)inode->i_ino, (unsigned long long)pos,
672 (unsigned long long)pos + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 bsize = AFFS_SB(sb)->s_data_blksize;
674 data = page_address(page);
675
676 bh = NULL;
677 written = 0;
678 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
679 bidx = tmp / bsize;
680 boff = tmp % bsize;
681 if (boff) {
682 bh = affs_bread_ino(inode, bidx, 0);
683 if (IS_ERR(bh))
684 return PTR_ERR(bh);
685 tmp = min(bsize - boff, to - from);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700686 BUG_ON(boff + tmp > bsize || tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
Marcin Slusarz6369a4a2008-04-30 00:54:47 -0700688 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 affs_fix_checksum(sb, bh);
690 mark_buffer_dirty_inode(bh, inode);
691 written += tmp;
692 from += tmp;
693 bidx++;
694 } else if (bidx) {
695 bh = affs_bread_ino(inode, bidx - 1, 0);
696 if (IS_ERR(bh))
697 return PTR_ERR(bh);
698 }
699 while (from + bsize <= to) {
700 prev_bh = bh;
701 bh = affs_getemptyblk_ino(inode, bidx);
702 if (IS_ERR(bh))
703 goto out;
704 memcpy(AFFS_DATA(bh), data + from, bsize);
705 if (buffer_new(bh)) {
706 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
707 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
708 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
709 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize);
710 AFFS_DATA_HEAD(bh)->next = 0;
711 bh->b_state &= ~(1UL << BH_New);
712 if (prev_bh) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700713 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
714
715 if (tmp_next)
716 affs_warning(sb, "commit_write_ofs",
717 "next block already set for %d (%d)",
718 bidx, tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700720 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 mark_buffer_dirty_inode(prev_bh, inode);
722 }
723 }
724 affs_brelse(prev_bh);
725 affs_fix_checksum(sb, bh);
726 mark_buffer_dirty_inode(bh, inode);
727 written += bsize;
728 from += bsize;
729 bidx++;
730 }
731 if (from < to) {
732 prev_bh = bh;
733 bh = affs_bread_ino(inode, bidx, 1);
734 if (IS_ERR(bh))
735 goto out;
736 tmp = min(bsize, to - from);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700737 BUG_ON(tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 memcpy(AFFS_DATA(bh), data + from, tmp);
739 if (buffer_new(bh)) {
740 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
741 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
742 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
743 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
744 AFFS_DATA_HEAD(bh)->next = 0;
745 bh->b_state &= ~(1UL << BH_New);
746 if (prev_bh) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700747 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
748
749 if (tmp_next)
750 affs_warning(sb, "commit_write_ofs",
751 "next block already set for %d (%d)",
752 bidx, tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700754 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 mark_buffer_dirty_inode(prev_bh, inode);
756 }
757 } else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp)
758 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
759 affs_brelse(prev_bh);
760 affs_fix_checksum(sb, bh);
761 mark_buffer_dirty_inode(bh, inode);
762 written += tmp;
763 from += tmp;
764 bidx++;
765 }
766 SetPageUptodate(page);
767
768done:
769 affs_brelse(bh);
770 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
771 if (tmp > inode->i_size)
772 inode->i_size = AFFS_I(inode)->mmu_private = tmp;
773
Nick Pigginf2b6a162007-10-16 01:25:24 -0700774 unlock_page(page);
775 page_cache_release(page);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return written;
778
779out:
780 bh = prev_bh;
781 if (!written)
782 written = PTR_ERR(bh);
783 goto done;
784}
785
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700786const struct address_space_operations affs_aops_ofs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 .readpage = affs_readpage_ofs,
788 //.writepage = affs_writepage_ofs,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700789 .write_begin = affs_write_begin_ofs,
790 .write_end = affs_write_end_ofs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791};
792
793/* Free any preallocated blocks. */
794
795void
796affs_free_prealloc(struct inode *inode)
797{
798 struct super_block *sb = inode->i_sb;
799
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700800 pr_debug("free_prealloc(ino=%lu)\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 while (AFFS_I(inode)->i_pa_cnt) {
803 AFFS_I(inode)->i_pa_cnt--;
804 affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc);
805 }
806}
807
808/* Truncate (or enlarge) a file to the requested size. */
809
810void
811affs_truncate(struct inode *inode)
812{
813 struct super_block *sb = inode->i_sb;
814 u32 ext, ext_key;
815 u32 last_blk, blkcnt, blk;
816 u32 size;
817 struct buffer_head *ext_bh;
818 int i;
819
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700820 pr_debug("truncate(inode=%d, oldsize=%u, newsize=%u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 (u32)inode->i_ino, (u32)AFFS_I(inode)->mmu_private, (u32)inode->i_size);
822
823 last_blk = 0;
824 ext = 0;
825 if (inode->i_size) {
826 last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize;
827 ext = last_blk / AFFS_SB(sb)->s_hashsize;
828 }
829
830 if (inode->i_size > AFFS_I(inode)->mmu_private) {
831 struct address_space *mapping = inode->i_mapping;
832 struct page *page;
Nick Pigginf2b6a162007-10-16 01:25:24 -0700833 void *fsdata;
Fabian Frederick73516ac2014-10-13 15:53:54 -0700834 loff_t isize = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int res;
836
Fabian Frederick73516ac2014-10-13 15:53:54 -0700837 res = mapping->a_ops->write_begin(NULL, mapping, isize, 0, 0, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!res)
Fabian Frederick73516ac2014-10-13 15:53:54 -0700839 res = mapping->a_ops->write_end(NULL, mapping, isize, 0, 0, page, fsdata);
Roman Zippeldca3c332008-04-29 17:02:20 +0200840 else
841 inode->i_size = AFFS_I(inode)->mmu_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 mark_inode_dirty(inode);
843 return;
844 } else if (inode->i_size == AFFS_I(inode)->mmu_private)
845 return;
846
847 // lock cache
848 ext_bh = affs_get_extblock(inode, ext);
849 if (IS_ERR(ext_bh)) {
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800850 affs_warning(sb, "truncate",
851 "unexpected read error for ext block %u (%ld)",
852 (unsigned int)ext, PTR_ERR(ext_bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return;
854 }
855 if (AFFS_I(inode)->i_lc) {
856 /* clear linear cache */
857 i = (ext + 1) >> AFFS_I(inode)->i_lc_shift;
858 if (AFFS_I(inode)->i_lc_size > i) {
859 AFFS_I(inode)->i_lc_size = i;
860 for (; i < AFFS_LC_SIZE; i++)
861 AFFS_I(inode)->i_lc[i] = 0;
862 }
863 /* clear associative cache */
864 for (i = 0; i < AFFS_AC_SIZE; i++)
865 if (AFFS_I(inode)->i_ac[i].ext >= ext)
866 AFFS_I(inode)->i_ac[i].ext = 0;
867 }
868 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
869
870 blkcnt = AFFS_I(inode)->i_blkcnt;
871 i = 0;
872 blk = last_blk;
873 if (inode->i_size) {
874 i = last_blk % AFFS_SB(sb)->s_hashsize + 1;
875 blk++;
876 } else
877 AFFS_HEAD(ext_bh)->first_data = 0;
Roman Zippeldca3c332008-04-29 17:02:20 +0200878 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 size = AFFS_SB(sb)->s_hashsize;
880 if (size > blkcnt - blk + i)
881 size = blkcnt - blk + i;
882 for (; i < size; i++, blk++) {
883 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
884 AFFS_BLOCK(sb, ext_bh, i) = 0;
885 }
886 AFFS_TAIL(sb, ext_bh)->extension = 0;
887 affs_fix_checksum(sb, ext_bh);
888 mark_buffer_dirty_inode(ext_bh, inode);
889 affs_brelse(ext_bh);
890
891 if (inode->i_size) {
892 AFFS_I(inode)->i_blkcnt = last_blk + 1;
893 AFFS_I(inode)->i_extcnt = ext + 1;
894 if (AFFS_SB(sb)->s_flags & SF_OFS) {
895 struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
896 u32 tmp;
Dan Carpenter0e45b672010-08-25 09:05:27 +0200897 if (IS_ERR(bh)) {
Fabian Frederick1ee54b02014-12-12 16:57:49 -0800898 affs_warning(sb, "truncate",
899 "unexpected read error for last block %u (%ld)",
900 (unsigned int)ext, PTR_ERR(bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return;
902 }
903 tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
904 AFFS_DATA_HEAD(bh)->next = 0;
905 affs_adjust_checksum(bh, -tmp);
906 affs_brelse(bh);
907 }
908 } else {
909 AFFS_I(inode)->i_blkcnt = 0;
910 AFFS_I(inode)->i_extcnt = 1;
911 }
912 AFFS_I(inode)->mmu_private = inode->i_size;
913 // unlock cache
914
915 while (ext_key) {
916 ext_bh = affs_bread(sb, ext_key);
917 size = AFFS_SB(sb)->s_hashsize;
918 if (size > blkcnt - blk)
919 size = blkcnt - blk;
920 for (i = 0; i < size; i++, blk++)
921 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
922 affs_free_block(sb, ext_key);
923 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
924 affs_brelse(ext_bh);
925 }
926 affs_free_prealloc(inode);
927}
Al Viroc4758792009-06-08 01:22:00 -0400928
Josef Bacik02c24a82011-07-16 20:44:56 -0400929int affs_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Al Viroc4758792009-06-08 01:22:00 -0400930{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200931 struct inode *inode = filp->f_mapping->host;
Al Viroc4758792009-06-08 01:22:00 -0400932 int ret, err;
933
Josef Bacik02c24a82011-07-16 20:44:56 -0400934 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
935 if (err)
936 return err;
937
938 mutex_lock(&inode->i_mutex);
Al Viroc4758792009-06-08 01:22:00 -0400939 ret = write_inode_now(inode, 0);
940 err = sync_blockdev(inode->i_sb->s_bdev);
941 if (!ret)
942 ret = err;
Josef Bacik02c24a82011-07-16 20:44:56 -0400943 mutex_unlock(&inode->i_mutex);
Al Viroc4758792009-06-08 01:22:00 -0400944 return ret;
945}
Fabian Frederick76339782014-12-12 16:57:47 -0800946const struct file_operations affs_file_operations = {
947 .llseek = generic_file_llseek,
948 .read = new_sync_read,
949 .read_iter = generic_file_read_iter,
950 .write = new_sync_write,
951 .write_iter = generic_file_write_iter,
952 .mmap = generic_file_mmap,
953 .open = affs_file_open,
954 .release = affs_file_release,
955 .fsync = affs_file_fsync,
956 .splice_read = generic_file_splice_read,
957};
958
959const struct inode_operations affs_file_inode_operations = {
960 .setattr = affs_notify_change,
961};