blob: 8e2e60ea1e2333f3e1eeb900f921b855a91772dd [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)
336 affs_warning(sb, "get_block", "block already set (%x)", bh_result->b_blocknr);
337 AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
338 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
339 affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
340 bh_result->b_blocknr = blocknr;
341
342 if (!block) {
343 /* insert first block into header block */
344 u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
345 if (tmp)
346 affs_warning(sb, "get_block", "first block already set (%d)", tmp);
347 AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
348 affs_adjust_checksum(ext_bh, blocknr - tmp);
349 }
350 }
351
352 affs_brelse(ext_bh);
353 //unlock cache
354 affs_unlock_ext(inode);
355 return 0;
356
357err_big:
358 affs_error(inode->i_sb,"get_block","strange block request %d", block);
359 return -EIO;
360err_ext:
361 // unlock cache
362 affs_unlock_ext(inode);
363 return PTR_ERR(ext_bh);
364err_alloc:
365 brelse(ext_bh);
366 clear_buffer_mapped(bh_result);
367 bh_result->b_bdev = NULL;
368 // unlock cache
369 affs_unlock_ext(inode);
370 return -ENOSPC;
371}
372
373static int affs_writepage(struct page *page, struct writeback_control *wbc)
374{
375 return block_write_full_page(page, affs_get_block, wbc);
376}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static int affs_readpage(struct file *file, struct page *page)
379{
380 return block_read_full_page(page, affs_get_block);
381}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700382
Marco Stornelli1dc18342012-12-15 11:51:53 +0100383static void affs_write_failed(struct address_space *mapping, loff_t to)
384{
385 struct inode *inode = mapping->host;
386
387 if (to > inode->i_size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700388 truncate_pagecache(inode, inode->i_size);
Marco Stornelli1dc18342012-12-15 11:51:53 +0100389 affs_truncate(inode);
390 }
391}
392
Nick Pigginf2b6a162007-10-16 01:25:24 -0700393static int affs_write_begin(struct file *file, struct address_space *mapping,
394 loff_t pos, unsigned len, unsigned flags,
395 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Christoph Hellwig282dc172010-06-04 11:29:55 +0200397 int ret;
398
Nick Pigginf2b6a162007-10-16 01:25:24 -0700399 *pagep = NULL;
Christoph Hellwig282dc172010-06-04 11:29:55 +0200400 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700401 affs_get_block,
402 &AFFS_I(mapping->host)->mmu_private);
Marco Stornelli1dc18342012-12-15 11:51:53 +0100403 if (unlikely(ret))
404 affs_write_failed(mapping, pos + len);
Christoph Hellwig282dc172010-06-04 11:29:55 +0200405
406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
410{
411 return generic_block_bmap(mapping,block,affs_get_block);
412}
Nick Pigginf2b6a162007-10-16 01:25:24 -0700413
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700414const struct address_space_operations affs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 .readpage = affs_readpage,
416 .writepage = affs_writepage,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700417 .write_begin = affs_write_begin,
418 .write_end = generic_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 .bmap = _affs_bmap
420};
421
422static inline struct buffer_head *
423affs_bread_ino(struct inode *inode, int block, int create)
424{
425 struct buffer_head *bh, tmp_bh;
426 int err;
427
428 tmp_bh.b_state = 0;
429 err = affs_get_block(inode, block, &tmp_bh, create);
430 if (!err) {
431 bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
432 if (bh) {
433 bh->b_state |= tmp_bh.b_state;
434 return bh;
435 }
436 err = -EIO;
437 }
438 return ERR_PTR(err);
439}
440
441static inline struct buffer_head *
442affs_getzeroblk_ino(struct inode *inode, int block)
443{
444 struct buffer_head *bh, tmp_bh;
445 int err;
446
447 tmp_bh.b_state = 0;
448 err = affs_get_block(inode, block, &tmp_bh, 1);
449 if (!err) {
450 bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
451 if (bh) {
452 bh->b_state |= tmp_bh.b_state;
453 return bh;
454 }
455 err = -EIO;
456 }
457 return ERR_PTR(err);
458}
459
460static inline struct buffer_head *
461affs_getemptyblk_ino(struct inode *inode, int block)
462{
463 struct buffer_head *bh, tmp_bh;
464 int err;
465
466 tmp_bh.b_state = 0;
467 err = affs_get_block(inode, block, &tmp_bh, 1);
468 if (!err) {
469 bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
470 if (bh) {
471 bh->b_state |= tmp_bh.b_state;
472 return bh;
473 }
474 err = -EIO;
475 }
476 return ERR_PTR(err);
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static int
Fabian Frederick0c89d672014-06-06 14:37:23 -0700480affs_do_readpage_ofs(struct page *page, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct inode *inode = page->mapping->host;
483 struct super_block *sb = inode->i_sb;
484 struct buffer_head *bh;
485 char *data;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700486 unsigned pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 u32 bidx, boff, bsize;
488 u32 tmp;
489
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700490 pr_debug("%s(%u, %ld, 0, %d)\n", __func__, (u32)inode->i_ino,
Fabian Frederick0c89d672014-06-06 14:37:23 -0700491 page->index, to);
492 BUG_ON(to > PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 kmap(page);
494 data = page_address(page);
495 bsize = AFFS_SB(sb)->s_data_blksize;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700496 tmp = page->index << PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 bidx = tmp / bsize;
498 boff = tmp % bsize;
499
Fabian Frederick0c89d672014-06-06 14:37:23 -0700500 while (pos < to) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 bh = affs_bread_ino(inode, bidx, 0);
502 if (IS_ERR(bh))
503 return PTR_ERR(bh);
Fabian Frederick0c89d672014-06-06 14:37:23 -0700504 tmp = min(bsize - boff, to - pos);
505 BUG_ON(pos + tmp > to || tmp > bsize);
506 memcpy(data + pos, AFFS_DATA(bh) + boff, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 affs_brelse(bh);
508 bidx++;
Fabian Frederick0c89d672014-06-06 14:37:23 -0700509 pos += tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 boff = 0;
511 }
512 flush_dcache_page(page);
513 kunmap(page);
514 return 0;
515}
516
517static int
518affs_extent_file_ofs(struct inode *inode, u32 newsize)
519{
520 struct super_block *sb = inode->i_sb;
521 struct buffer_head *bh, *prev_bh;
522 u32 bidx, boff;
523 u32 size, bsize;
524 u32 tmp;
525
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700526 pr_debug("%s(%u, %d)\n", __func__, (u32)inode->i_ino, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 bsize = AFFS_SB(sb)->s_data_blksize;
528 bh = NULL;
529 size = AFFS_I(inode)->mmu_private;
530 bidx = size / bsize;
531 boff = size % bsize;
532 if (boff) {
533 bh = affs_bread_ino(inode, bidx, 0);
534 if (IS_ERR(bh))
535 return PTR_ERR(bh);
536 tmp = min(bsize - boff, newsize - size);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700537 BUG_ON(boff + tmp > bsize || tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 memset(AFFS_DATA(bh) + boff, 0, tmp);
Marcin Slusarz6369a4a2008-04-30 00:54:47 -0700539 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 affs_fix_checksum(sb, bh);
541 mark_buffer_dirty_inode(bh, inode);
542 size += tmp;
543 bidx++;
544 } else if (bidx) {
545 bh = affs_bread_ino(inode, bidx - 1, 0);
546 if (IS_ERR(bh))
547 return PTR_ERR(bh);
548 }
549
550 while (size < newsize) {
551 prev_bh = bh;
552 bh = affs_getzeroblk_ino(inode, bidx);
553 if (IS_ERR(bh))
554 goto out;
555 tmp = min(bsize, newsize - size);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700556 BUG_ON(tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
558 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
559 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
560 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
561 affs_fix_checksum(sb, bh);
562 bh->b_state &= ~(1UL << BH_New);
563 mark_buffer_dirty_inode(bh, inode);
564 if (prev_bh) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700565 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
566
567 if (tmp_next)
568 affs_warning(sb, "extent_file_ofs",
569 "next block already set for %d (%d)",
570 bidx, tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700572 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 mark_buffer_dirty_inode(prev_bh, inode);
574 affs_brelse(prev_bh);
575 }
576 size += bsize;
577 bidx++;
578 }
579 affs_brelse(bh);
580 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
581 return 0;
582
583out:
584 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
585 return PTR_ERR(bh);
586}
587
588static int
589affs_readpage_ofs(struct file *file, struct page *page)
590{
591 struct inode *inode = page->mapping->host;
592 u32 to;
593 int err;
594
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700595 pr_debug("%s(%u, %ld)\n", __func__, (u32)inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 to = PAGE_CACHE_SIZE;
597 if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) {
598 to = inode->i_size & ~PAGE_CACHE_MASK;
599 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
600 }
601
Fabian Frederick0c89d672014-06-06 14:37:23 -0700602 err = affs_do_readpage_ofs(page, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!err)
604 SetPageUptodate(page);
605 unlock_page(page);
606 return err;
607}
608
Nick Pigginf2b6a162007-10-16 01:25:24 -0700609static int affs_write_begin_ofs(struct file *file, struct address_space *mapping,
610 loff_t pos, unsigned len, unsigned flags,
611 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Nick Pigginf2b6a162007-10-16 01:25:24 -0700613 struct inode *inode = mapping->host;
614 struct page *page;
615 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int err = 0;
617
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700618 pr_debug("%s(%u, %llu, %llu)\n", __func__, (u32)inode->i_ino,
619 (unsigned long long)pos, (unsigned long long)pos + len);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700620 if (pos > AFFS_I(inode)->mmu_private) {
621 /* XXX: this probably leaves a too-big i_size in case of
622 * failure. Should really be updating i_size at write_end time
623 */
624 err = affs_extent_file_ofs(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (err)
626 return err;
627 }
Nick Pigginf2b6a162007-10-16 01:25:24 -0700628
629 index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin54566b22009-01-04 12:00:53 -0800630 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700631 if (!page)
632 return -ENOMEM;
633 *pagep = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 if (PageUptodate(page))
636 return 0;
637
Nick Pigginf2b6a162007-10-16 01:25:24 -0700638 /* XXX: inefficient but safe in the face of short writes */
Fabian Frederick0c89d672014-06-06 14:37:23 -0700639 err = affs_do_readpage_ofs(page, PAGE_CACHE_SIZE);
Nick Pigginf2b6a162007-10-16 01:25:24 -0700640 if (err) {
641 unlock_page(page);
642 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644 return err;
645}
646
Nick Pigginf2b6a162007-10-16 01:25:24 -0700647static int affs_write_end_ofs(struct file *file, struct address_space *mapping,
648 loff_t pos, unsigned len, unsigned copied,
649 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Nick Pigginf2b6a162007-10-16 01:25:24 -0700651 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct super_block *sb = inode->i_sb;
653 struct buffer_head *bh, *prev_bh;
654 char *data;
655 u32 bidx, boff, bsize;
Nick Pigginf2b6a162007-10-16 01:25:24 -0700656 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 u32 tmp;
658 int written;
659
Nick Pigginf2b6a162007-10-16 01:25:24 -0700660 from = pos & (PAGE_CACHE_SIZE - 1);
661 to = pos + len;
662 /*
663 * XXX: not sure if this can handle short copies (len < copied), but
664 * we don't have to, because the page should always be uptodate here,
665 * due to write_begin.
666 */
667
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700668 pr_debug("%s(%u, %llu, %llu)\n",
669 __func__, (u32)inode->i_ino, (unsigned long long)pos,
670 (unsigned long long)pos + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 bsize = AFFS_SB(sb)->s_data_blksize;
672 data = page_address(page);
673
674 bh = NULL;
675 written = 0;
676 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
677 bidx = tmp / bsize;
678 boff = tmp % bsize;
679 if (boff) {
680 bh = affs_bread_ino(inode, bidx, 0);
681 if (IS_ERR(bh))
682 return PTR_ERR(bh);
683 tmp = min(bsize - boff, to - from);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700684 BUG_ON(boff + tmp > bsize || tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
Marcin Slusarz6369a4a2008-04-30 00:54:47 -0700686 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 affs_fix_checksum(sb, bh);
688 mark_buffer_dirty_inode(bh, inode);
689 written += tmp;
690 from += tmp;
691 bidx++;
692 } else if (bidx) {
693 bh = affs_bread_ino(inode, bidx - 1, 0);
694 if (IS_ERR(bh))
695 return PTR_ERR(bh);
696 }
697 while (from + bsize <= to) {
698 prev_bh = bh;
699 bh = affs_getemptyblk_ino(inode, bidx);
700 if (IS_ERR(bh))
701 goto out;
702 memcpy(AFFS_DATA(bh), data + from, bsize);
703 if (buffer_new(bh)) {
704 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
705 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
706 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
707 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize);
708 AFFS_DATA_HEAD(bh)->next = 0;
709 bh->b_state &= ~(1UL << BH_New);
710 if (prev_bh) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700711 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
712
713 if (tmp_next)
714 affs_warning(sb, "commit_write_ofs",
715 "next block already set for %d (%d)",
716 bidx, tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700718 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 mark_buffer_dirty_inode(prev_bh, inode);
720 }
721 }
722 affs_brelse(prev_bh);
723 affs_fix_checksum(sb, bh);
724 mark_buffer_dirty_inode(bh, inode);
725 written += bsize;
726 from += bsize;
727 bidx++;
728 }
729 if (from < to) {
730 prev_bh = bh;
731 bh = affs_bread_ino(inode, bidx, 1);
732 if (IS_ERR(bh))
733 goto out;
734 tmp = min(bsize, to - from);
Julia Lawall8d4b6902008-04-29 00:59:12 -0700735 BUG_ON(tmp > bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 memcpy(AFFS_DATA(bh), data + from, tmp);
737 if (buffer_new(bh)) {
738 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
739 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
740 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
741 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
742 AFFS_DATA_HEAD(bh)->next = 0;
743 bh->b_state &= ~(1UL << BH_New);
744 if (prev_bh) {
Fabian Frederick73516ac2014-10-13 15:53:54 -0700745 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
746
747 if (tmp_next)
748 affs_warning(sb, "commit_write_ofs",
749 "next block already set for %d (%d)",
750 bidx, tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
Fabian Frederick73516ac2014-10-13 15:53:54 -0700752 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 mark_buffer_dirty_inode(prev_bh, inode);
754 }
755 } else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp)
756 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
757 affs_brelse(prev_bh);
758 affs_fix_checksum(sb, bh);
759 mark_buffer_dirty_inode(bh, inode);
760 written += tmp;
761 from += tmp;
762 bidx++;
763 }
764 SetPageUptodate(page);
765
766done:
767 affs_brelse(bh);
768 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
769 if (tmp > inode->i_size)
770 inode->i_size = AFFS_I(inode)->mmu_private = tmp;
771
Nick Pigginf2b6a162007-10-16 01:25:24 -0700772 unlock_page(page);
773 page_cache_release(page);
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return written;
776
777out:
778 bh = prev_bh;
779 if (!written)
780 written = PTR_ERR(bh);
781 goto done;
782}
783
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700784const struct address_space_operations affs_aops_ofs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 .readpage = affs_readpage_ofs,
786 //.writepage = affs_writepage_ofs,
Nick Pigginf2b6a162007-10-16 01:25:24 -0700787 .write_begin = affs_write_begin_ofs,
788 .write_end = affs_write_end_ofs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789};
790
791/* Free any preallocated blocks. */
792
793void
794affs_free_prealloc(struct inode *inode)
795{
796 struct super_block *sb = inode->i_sb;
797
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700798 pr_debug("free_prealloc(ino=%lu)\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 while (AFFS_I(inode)->i_pa_cnt) {
801 AFFS_I(inode)->i_pa_cnt--;
802 affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc);
803 }
804}
805
806/* Truncate (or enlarge) a file to the requested size. */
807
808void
809affs_truncate(struct inode *inode)
810{
811 struct super_block *sb = inode->i_sb;
812 u32 ext, ext_key;
813 u32 last_blk, blkcnt, blk;
814 u32 size;
815 struct buffer_head *ext_bh;
816 int i;
817
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700818 pr_debug("truncate(inode=%d, oldsize=%u, newsize=%u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 (u32)inode->i_ino, (u32)AFFS_I(inode)->mmu_private, (u32)inode->i_size);
820
821 last_blk = 0;
822 ext = 0;
823 if (inode->i_size) {
824 last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize;
825 ext = last_blk / AFFS_SB(sb)->s_hashsize;
826 }
827
828 if (inode->i_size > AFFS_I(inode)->mmu_private) {
829 struct address_space *mapping = inode->i_mapping;
830 struct page *page;
Nick Pigginf2b6a162007-10-16 01:25:24 -0700831 void *fsdata;
Fabian Frederick73516ac2014-10-13 15:53:54 -0700832 loff_t isize = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int res;
834
Fabian Frederick73516ac2014-10-13 15:53:54 -0700835 res = mapping->a_ops->write_begin(NULL, mapping, isize, 0, 0, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!res)
Fabian Frederick73516ac2014-10-13 15:53:54 -0700837 res = mapping->a_ops->write_end(NULL, mapping, isize, 0, 0, page, fsdata);
Roman Zippeldca3c332008-04-29 17:02:20 +0200838 else
839 inode->i_size = AFFS_I(inode)->mmu_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 mark_inode_dirty(inode);
841 return;
842 } else if (inode->i_size == AFFS_I(inode)->mmu_private)
843 return;
844
845 // lock cache
846 ext_bh = affs_get_extblock(inode, ext);
847 if (IS_ERR(ext_bh)) {
848 affs_warning(sb, "truncate", "unexpected read error for ext block %u (%d)",
849 ext, PTR_ERR(ext_bh));
850 return;
851 }
852 if (AFFS_I(inode)->i_lc) {
853 /* clear linear cache */
854 i = (ext + 1) >> AFFS_I(inode)->i_lc_shift;
855 if (AFFS_I(inode)->i_lc_size > i) {
856 AFFS_I(inode)->i_lc_size = i;
857 for (; i < AFFS_LC_SIZE; i++)
858 AFFS_I(inode)->i_lc[i] = 0;
859 }
860 /* clear associative cache */
861 for (i = 0; i < AFFS_AC_SIZE; i++)
862 if (AFFS_I(inode)->i_ac[i].ext >= ext)
863 AFFS_I(inode)->i_ac[i].ext = 0;
864 }
865 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
866
867 blkcnt = AFFS_I(inode)->i_blkcnt;
868 i = 0;
869 blk = last_blk;
870 if (inode->i_size) {
871 i = last_blk % AFFS_SB(sb)->s_hashsize + 1;
872 blk++;
873 } else
874 AFFS_HEAD(ext_bh)->first_data = 0;
Roman Zippeldca3c332008-04-29 17:02:20 +0200875 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 size = AFFS_SB(sb)->s_hashsize;
877 if (size > blkcnt - blk + i)
878 size = blkcnt - blk + i;
879 for (; i < size; i++, blk++) {
880 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
881 AFFS_BLOCK(sb, ext_bh, i) = 0;
882 }
883 AFFS_TAIL(sb, ext_bh)->extension = 0;
884 affs_fix_checksum(sb, ext_bh);
885 mark_buffer_dirty_inode(ext_bh, inode);
886 affs_brelse(ext_bh);
887
888 if (inode->i_size) {
889 AFFS_I(inode)->i_blkcnt = last_blk + 1;
890 AFFS_I(inode)->i_extcnt = ext + 1;
891 if (AFFS_SB(sb)->s_flags & SF_OFS) {
892 struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
893 u32 tmp;
Dan Carpenter0e45b672010-08-25 09:05:27 +0200894 if (IS_ERR(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 affs_warning(sb, "truncate", "unexpected read error for last block %u (%d)",
Dan Carpenter0e45b672010-08-25 09:05:27 +0200896 ext, PTR_ERR(bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return;
898 }
899 tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
900 AFFS_DATA_HEAD(bh)->next = 0;
901 affs_adjust_checksum(bh, -tmp);
902 affs_brelse(bh);
903 }
904 } else {
905 AFFS_I(inode)->i_blkcnt = 0;
906 AFFS_I(inode)->i_extcnt = 1;
907 }
908 AFFS_I(inode)->mmu_private = inode->i_size;
909 // unlock cache
910
911 while (ext_key) {
912 ext_bh = affs_bread(sb, ext_key);
913 size = AFFS_SB(sb)->s_hashsize;
914 if (size > blkcnt - blk)
915 size = blkcnt - blk;
916 for (i = 0; i < size; i++, blk++)
917 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
918 affs_free_block(sb, ext_key);
919 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
920 affs_brelse(ext_bh);
921 }
922 affs_free_prealloc(inode);
923}
Al Viroc4758792009-06-08 01:22:00 -0400924
Josef Bacik02c24a82011-07-16 20:44:56 -0400925int affs_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Al Viroc4758792009-06-08 01:22:00 -0400926{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200927 struct inode *inode = filp->f_mapping->host;
Al Viroc4758792009-06-08 01:22:00 -0400928 int ret, err;
929
Josef Bacik02c24a82011-07-16 20:44:56 -0400930 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
931 if (err)
932 return err;
933
934 mutex_lock(&inode->i_mutex);
Al Viroc4758792009-06-08 01:22:00 -0400935 ret = write_inode_now(inode, 0);
936 err = sync_blockdev(inode->i_sb->s_bdev);
937 if (!ret)
938 ret = err;
Josef Bacik02c24a82011-07-16 20:44:56 -0400939 mutex_unlock(&inode->i_mutex);
Al Viroc4758792009-06-08 01:22:00 -0400940 return ret;
941}
Fabian Frederick76339782014-12-12 16:57:47 -0800942const struct file_operations affs_file_operations = {
943 .llseek = generic_file_llseek,
944 .read = new_sync_read,
945 .read_iter = generic_file_read_iter,
946 .write = new_sync_write,
947 .write_iter = generic_file_write_iter,
948 .mmap = generic_file_mmap,
949 .open = affs_file_open,
950 .release = affs_file_release,
951 .fsync = affs_file_fsync,
952 .splice_read = generic_file_splice_read,
953};
954
955const struct inode_operations affs_file_inode_operations = {
956 .setattr = affs_notify_change,
957};