Linux-2.3.7.. Let's be careful out there..
[davej-history.git] / fs / affs / file.c
blobbb1ce69c85d4a917087b70b04534cdda2f575a2f
1 /*
2 * linux/fs/affs/file.c
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
8 * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
10 * (C) 1991 Linus Torvalds - minix filesystem
12 * affs regular file handling primitives
15 #define DEBUG 0
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <linux/sched.h>
19 #include <linux/affs_fs.h>
20 #include <linux/fcntl.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/malloc.h>
24 #include <linux/stat.h>
25 #include <linux/locks.h>
26 #include <linux/dirent.h>
27 #include <linux/fs.h>
28 #include <linux/amigaffs.h>
29 #include <linux/mm.h>
30 #include <linux/pagemap.h>
32 #define MIN(a,b) (((a)<(b))?(a):(b))
33 #define MAX(a,b) (((a)>(b))?(a):(b))
35 #if PAGE_SIZE < 4096
36 #error PAGE_SIZE must be at least 4096
37 #endif
39 static int affs_bmap(struct inode *inode, int block);
40 static struct buffer_head *affs_getblock(struct inode *inode, s32 block);
41 static ssize_t affs_file_read_ofs(struct file *filp, char *buf, size_t count, loff_t *ppos);
42 static ssize_t affs_file_write(struct file *filp, const char *buf, size_t count, loff_t *ppos);
43 static ssize_t affs_file_write_ofs(struct file *filp, const char *buf, size_t cnt, loff_t *ppos);
44 static int alloc_ext_cache(struct inode *inode);
46 static struct file_operations affs_file_operations = {
47 NULL, /* lseek - default */
48 generic_file_read, /* read */
49 affs_file_write, /* write */
50 NULL, /* readdir - bad */
51 NULL, /* poll - default */
52 NULL, /* ioctl - default */
53 generic_file_mmap, /* mmap */
54 NULL, /* no special open */
55 NULL, /* flush */
56 NULL, /* release */
57 file_fsync, /* brute force, but works */
58 NULL, /* fasync */
59 NULL, /* check_media_change */
60 NULL, /* revalidate */
61 NULL /* lock */
64 struct inode_operations affs_file_inode_operations = {
65 &affs_file_operations, /* default file operations */
66 NULL, /* create */
67 NULL, /* lookup */
68 NULL, /* link */
69 NULL, /* unlink */
70 NULL, /* symlink */
71 NULL, /* mkdir */
72 NULL, /* rmdir */
73 NULL, /* mknod */
74 NULL, /* rename */
75 NULL, /* readlink */
76 NULL, /* follow_link */
77 generic_readpage, /* readpage */
78 NULL, /* writepage */
79 affs_bmap, /* bmap */
80 affs_truncate, /* truncate */
81 NULL, /* permission */
82 NULL, /* smap */
83 NULL /* revalidate */
86 static struct file_operations affs_file_operations_ofs = {
87 NULL, /* lseek - default */
88 affs_file_read_ofs, /* read */
89 affs_file_write_ofs, /* write */
90 NULL, /* readdir - bad */
91 NULL, /* poll - default */
92 NULL, /* ioctl - default */
93 NULL, /* mmap */
94 NULL, /* no special open */
95 NULL, /* flush */
96 NULL, /* release */
97 file_fsync, /* brute force, but works */
98 NULL, /* fasync */
99 NULL, /* check_media_change */
100 NULL, /* revalidate */
101 NULL /* lock */
104 struct inode_operations affs_file_inode_operations_ofs = {
105 &affs_file_operations_ofs, /* default file operations */
106 NULL, /* create */
107 NULL, /* lookup */
108 NULL, /* link */
109 NULL, /* unlink */
110 NULL, /* symlink */
111 NULL, /* mkdir */
112 NULL, /* rmdir */
113 NULL, /* mknod */
114 NULL, /* rename */
115 NULL, /* readlink */
116 NULL, /* follow_link */
117 NULL, /* readpage */
118 NULL, /* writepage */
119 NULL, /* bmap */
120 affs_truncate, /* truncate */
121 NULL, /* permission */
122 NULL, /* smap */
123 NULL /* revalidate */
126 #define AFFS_ISINDEX(x) ((x < 129) || \
127 (x < 512 && (x & 1) == 0) || \
128 (x < 1024 && (x & 3) == 0) || \
129 (x < 2048 && (x & 15) == 0) || \
130 (x < 4096 && (x & 63) == 0) || \
131 (x < 20480 && (x & 255) == 0) || \
132 (x < 36864 && (x & 511) == 0))
134 /* The keys of the extension blocks are stored in a 512-entry
135 * deep cache. In order to save memory, not every key of later
136 * extension blocks is stored - the larger the file gets, the
137 * bigger the holes in between.
140 static int
141 seqnum_to_index(int seqnum)
143 /* All of the first 127 keys are stored */
144 if (seqnum < 128)
145 return seqnum;
146 seqnum -= 128;
148 /* Of the next 384 keys, every 2nd is kept */
149 if (seqnum < (192 * 2))
150 return 128 + (seqnum >> 1);
151 seqnum -= 192 * 2;
153 /* Every 4th of the next 512 */
154 if (seqnum < (128 * 4))
155 return 128 + 192 + (seqnum >> 2);
156 seqnum -= 128 * 4;
158 /* Every 16th of the next 1024 */
159 if (seqnum < (64 * 16))
160 return 128 + 192 + 128 + (seqnum >> 4);
161 seqnum -= 64 * 16;
163 /* Every 64th of the next 2048 */
164 if (seqnum < (32 * 64))
165 return 128 + 192 + 128 + 64 + (seqnum >> 6);
166 seqnum -= 32 * 64;
168 /* Every 256th of the next 16384 */
169 if (seqnum < (64 * 256))
170 return 128 + 192 + 128 + 64 + 32 + (seqnum >> 8);
171 seqnum -= 64 * 256;
173 /* Every 512th upto 36479 (1.3 GB with 512 byte blocks).
174 * Seeking to positions behind this will get slower
175 * than dead snails nailed to the ground. But if
176 * someone uses files that large with 512-byte blocks,
177 * he or she deserves no better.
180 if (seqnum > (31 * 512))
181 seqnum = 31 * 512;
182 return 128 + 192 + 128 + 64 + 32 + 64 + (seqnum >> 9);
185 /* Now the other way round: Calculate the sequence
186 * number of an extension block of a key at the
187 * given index in the cache.
190 static int
191 index_to_seqnum(int index)
193 if (index < 128)
194 return index;
195 index -= 128;
196 if (index < 192)
197 return 128 + (index << 1);
198 index -= 192;
199 if (index < 128)
200 return 128 + 192 * 2 + (index << 2);
201 index -= 128;
202 if (index < 64)
203 return 128 + 192 * 2 + 128 * 4 + (index << 4);
204 index -= 64;
205 if (index < 32)
206 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + (index << 6);
207 index -= 32;
208 if (index < 64)
209 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + 32 * 64 + (index << 8);
210 index -= 64;
211 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + 32 * 64 + 64 * 256 + (index << 9);
214 static s32 __inline__
215 calc_key(struct inode *inode, int *ext)
217 int index;
218 struct key_cache *kc;
220 for (index = 0; index < 4; index++) {
221 kc = &inode->u.affs_i.i_ec->kc[index];
222 if (*ext == kc->kc_this_seq) {
223 return kc->kc_this_key;
224 } else if (*ext == kc->kc_this_seq + 1) {
225 if (kc->kc_next_key)
226 return kc->kc_next_key;
227 else {
228 (*ext)--;
229 return kc->kc_this_key;
233 index = seqnum_to_index(*ext);
234 if (index > inode->u.affs_i.i_ec->max_ext)
235 index = inode->u.affs_i.i_ec->max_ext;
236 *ext = index_to_seqnum(index);
237 return inode->u.affs_i.i_ec->ec[index];
240 static int
241 affs_bmap(struct inode *inode, int block)
243 struct buffer_head *bh;
244 s32 key, nkey;
245 s32 ptype, stype;
246 int ext;
247 int index;
248 int keycount;
249 struct key_cache *kc;
250 struct key_cache *tkc;
251 struct timeval tv;
252 s32 *keyp;
253 int i;
255 pr_debug("AFFS: bmap(%lu,%d)\n",inode->i_ino,block);
257 if (block < 0) {
258 affs_error(inode->i_sb,"bmap","Block < 0");
259 return 0;
261 if (!inode->u.affs_i.i_ec) {
262 if (alloc_ext_cache(inode)) {
263 return 0;
267 /* Try to find the requested key in the cache.
268 * In order to speed this up as much as possible,
269 * the cache line lookup is done in a separate
270 * step.
273 for (i = 0; i < 4; i++) {
274 tkc = &inode->u.affs_i.i_ec->kc[i];
275 /* Look in any cache if the key is there */
276 if (block <= tkc->kc_last && block >= tkc->kc_first) {
277 return tkc->kc_keys[block - tkc->kc_first];
280 kc = NULL;
281 tv = xtime;
282 for (i = 0; i < 4; i++) {
283 tkc = &inode->u.affs_i.i_ec->kc[i];
284 if (tkc->kc_lru_time.tv_sec > tv.tv_sec)
285 continue;
286 if (tkc->kc_lru_time.tv_sec < tv.tv_sec ||
287 tkc->kc_lru_time.tv_usec < tv.tv_usec) {
288 kc = tkc;
289 tv = tkc->kc_lru_time;
292 if (!kc) /* Really shouldn't happen */
293 kc = tkc;
294 kc->kc_lru_time = xtime;
295 keyp = kc->kc_keys;
296 kc->kc_first = block;
297 kc->kc_last = -1;
298 keycount = AFFS_KCSIZE;
300 /* Calculate sequence number of the extension block where the
301 * number of the requested block is stored. 0 means it's in
302 * the file header.
305 ext = block / AFFS_I2HSIZE(inode);
306 key = calc_key(inode,&ext);
307 block -= ext * AFFS_I2HSIZE(inode);
309 for (;;) {
310 bh = affs_bread(inode->i_dev,key,AFFS_I2BSIZE(inode));
311 if (!bh)
312 return 0;
313 index = seqnum_to_index(ext);
314 if (index > inode->u.affs_i.i_ec->max_ext &&
315 (affs_checksum_block(AFFS_I2BSIZE(inode),bh->b_data,&ptype,&stype) ||
316 (ptype != T_SHORT && ptype != T_LIST) || stype != ST_FILE)) {
317 affs_brelse(bh);
318 return 0;
320 nkey = be32_to_cpu(FILE_END(bh->b_data,inode)->extension);
321 if (block < AFFS_I2HSIZE(inode)) {
322 /* Fill cache as much as possible */
323 if (keycount) {
324 kc->kc_first = ext * AFFS_I2HSIZE(inode) + block;
325 keycount = keycount < AFFS_I2HSIZE(inode) - block ? keycount :
326 AFFS_I2HSIZE(inode) - block;
327 for (i = 0; i < keycount; i++)
328 kc->kc_keys[i] = be32_to_cpu(AFFS_BLOCK(bh->b_data,inode,block + i));
329 kc->kc_last = kc->kc_first + i - 1;
331 break;
333 block -= AFFS_I2HSIZE(inode);
334 affs_brelse(bh);
335 ext++;
336 if (index > inode->u.affs_i.i_ec->max_ext && AFFS_ISINDEX(ext)) {
337 inode->u.affs_i.i_ec->ec[index] = nkey;
338 inode->u.affs_i.i_ec->max_ext = index;
340 key = nkey;
342 kc->kc_this_key = key;
343 kc->kc_this_seq = ext;
344 kc->kc_next_key = nkey;
345 key = be32_to_cpu(AFFS_BLOCK(bh->b_data,inode,block));
346 affs_brelse(bh);
347 return key;
350 /* With the affs, getting a random block from a file is not
351 * a simple business. Since this fs does not allow holes,
352 * it may be necessary to allocate all the missing blocks
353 * in between, as well as some new extension blocks. The OFS
354 * is even worse: All data blocks contain pointers to the
355 * next ones, so you have to fix [n-1] after allocating [n].
356 * What a mess.
359 static struct buffer_head *
360 affs_getblock(struct inode *inode, s32 block)
362 struct super_block *sb = inode->i_sb;
363 int ofs = sb->u.affs_sb.s_flags & SF_OFS;
364 int ext = block / AFFS_I2HSIZE(inode);
365 struct buffer_head *bh, *ebh, *pbh = NULL;
366 struct key_cache *kc;
367 s32 key, nkey;
368 int cf, j, pt;
369 int index;
370 int err;
372 pr_debug("AFFS: getblock(%lu,%d)\n",inode->i_ino,block);
374 if (block < 0)
375 goto out_fail;
377 key = calc_key(inode,&ext);
378 block -= ext * AFFS_I2HSIZE(inode);
379 pt = ext ? T_LIST : T_SHORT;
381 /* Key refers now to the last known extension block,
382 * ext is its sequence number (if 0, key refers to the
383 * header block), and block is the block number relative
384 * to the first block stored in that extension block.
386 for (;;) { /* Loop over header block and extension blocks */
387 struct file_front *fdp;
389 bh = affs_bread(inode->i_dev,key,AFFS_I2BSIZE(inode));
390 if (!bh)
391 goto out_fail;
392 fdp = (struct file_front *) bh->b_data;
393 err = affs_checksum_block(AFFS_I2BSIZE(inode),bh->b_data,&cf,&j);
394 if (err || cf != pt || j != ST_FILE) {
395 affs_error(sb, "getblock",
396 "Block %d is not a valid %s", key,
397 pt == T_SHORT ? "file header" : "ext block");
398 goto out_free_bh;
400 j = be32_to_cpu(((struct file_front *)bh->b_data)->block_count);
401 for (cf = 0; j < AFFS_I2HSIZE(inode) && j <= block; j++) {
402 if (ofs && !pbh && inode->u.affs_i.i_lastblock >= 0) {
403 if (j > 0) {
404 s32 k = AFFS_BLOCK(bh->b_data, inode,
405 j - 1);
406 pbh = affs_bread(inode->i_dev,
407 be32_to_cpu(k),
408 AFFS_I2BSIZE(inode));
409 } else
410 pbh = affs_getblock(inode,inode->u.affs_i.i_lastblock);
411 if (!pbh) {
412 affs_error(sb,"getblock",
413 "Cannot get last block in file");
414 break;
417 nkey = affs_new_data(inode);
418 if (!nkey)
419 break;
420 inode->u.affs_i.i_lastblock++;
421 if (AFFS_BLOCK(bh->b_data,inode,j)) {
422 affs_warning(sb,"getblock","Block already allocated");
423 affs_free_block(sb,nkey);
424 continue;
426 AFFS_BLOCK(bh->b_data,inode,j) = cpu_to_be32(nkey);
427 if (ofs) {
428 ebh = affs_bread(inode->i_dev,nkey,AFFS_I2BSIZE(inode));
429 if (!ebh) {
430 affs_error(sb,"getblock",
431 "Cannot get block %d",nkey);
432 affs_free_block(sb,nkey);
433 AFFS_BLOCK(bh->b_data,inode,j) = 0;
434 break;
436 DATA_FRONT(ebh)->primary_type = cpu_to_be32(T_DATA);
437 DATA_FRONT(ebh)->header_key = cpu_to_be32(inode->i_ino);
438 DATA_FRONT(ebh)->sequence_number = cpu_to_be32(inode->u.affs_i.i_lastblock + 1);
439 affs_fix_checksum(AFFS_I2BSIZE(inode),
440 ebh->b_data, 5);
441 mark_buffer_dirty(ebh, 0);
442 if (pbh) {
443 DATA_FRONT(pbh)->data_size = cpu_to_be32(AFFS_I2BSIZE(inode) - 24);
444 DATA_FRONT(pbh)->next_data = cpu_to_be32(nkey);
445 affs_fix_checksum(AFFS_I2BSIZE(inode),pbh->b_data,5);
446 mark_buffer_dirty(pbh,0);
447 affs_brelse(pbh);
449 pbh = ebh;
451 cf = 1;
453 /* N.B. May need to release pbh after here */
455 if (cf) {
456 if (pt == T_SHORT)
457 fdp->first_data = AFFS_BLOCK(bh->b_data,inode,0);
458 fdp->block_count = cpu_to_be32(j);
459 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
460 mark_buffer_dirty(bh,1);
463 if (block < j) {
464 if (pbh)
465 affs_brelse(pbh);
466 break;
468 if (j < AFFS_I2HSIZE(inode)) {
469 /* N.B. What about pbh here? */
470 goto out_free_bh;
473 block -= AFFS_I2HSIZE(inode);
474 key = be32_to_cpu(FILE_END(bh->b_data,inode)->extension);
475 if (!key) {
476 key = affs_new_header(inode);
477 if (!key)
478 goto out_free_bh;
479 ebh = affs_bread(inode->i_dev,key,AFFS_I2BSIZE(inode));
480 if (!ebh) {
481 /* N.B. must free bh here */
482 goto out_free_block;
484 ((struct file_front *)ebh->b_data)->primary_type = cpu_to_be32(T_LIST);
485 ((struct file_front *)ebh->b_data)->own_key = cpu_to_be32(key);
486 FILE_END(ebh->b_data,inode)->secondary_type = cpu_to_be32(ST_FILE);
487 FILE_END(ebh->b_data,inode)->parent = cpu_to_be32(inode->i_ino);
488 affs_fix_checksum(AFFS_I2BSIZE(inode),ebh->b_data,5);
489 mark_buffer_dirty(ebh, 1);
490 FILE_END(bh->b_data,inode)->extension = cpu_to_be32(key);
491 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
492 mark_buffer_dirty(bh,1);
493 affs_brelse(bh);
494 bh = ebh;
496 pt = T_LIST;
497 ext++;
498 index = seqnum_to_index(ext);
499 if (index > inode->u.affs_i.i_ec->max_ext &&
500 AFFS_ISINDEX(ext)) {
501 inode->u.affs_i.i_ec->ec[index] = key;
502 inode->u.affs_i.i_ec->max_ext = index;
504 affs_brelse(bh);
507 /* Invalidate key cache */
508 for (j = 0; j < 4; j++) {
509 kc = &inode->u.affs_i.i_ec->kc[j];
510 kc->kc_last = -1;
512 key = be32_to_cpu(AFFS_BLOCK(bh->b_data,inode,block));
513 affs_brelse(bh);
514 if (!key)
515 goto out_fail;
517 bh = affs_bread(inode->i_dev, key, AFFS_I2BSIZE(inode));
518 return bh;
520 out_free_block:
521 affs_free_block(sb, key);
522 out_free_bh:
523 affs_brelse(bh);
524 out_fail:
525 return NULL;
528 static ssize_t
529 affs_file_read_ofs(struct file *filp, char *buf, size_t count, loff_t *ppos)
531 struct inode *inode = filp->f_dentry->d_inode;
532 char *start;
533 ssize_t left, offset, size, sector;
534 ssize_t blocksize;
535 struct buffer_head *bh;
536 void *data;
538 pr_debug("AFFS: file_read_ofs(ino=%lu,pos=%lu,%d)\n",inode->i_ino,
539 (unsigned long)*ppos,count);
541 if (!inode) {
542 affs_error(inode->i_sb,"file_read_ofs","Inode = NULL");
543 return -EINVAL;
545 blocksize = AFFS_I2BSIZE(inode) - 24;
546 if (!(S_ISREG(inode->i_mode))) {
547 pr_debug("AFFS: file_read: mode = %07o",inode->i_mode);
548 return -EINVAL;
550 if (*ppos >= inode->i_size || count <= 0)
551 return 0;
553 start = buf;
554 for (;;) {
555 left = MIN (inode->i_size - *ppos,count - (buf - start));
556 if (!left)
557 break;
558 sector = affs_bmap(inode,(u32)*ppos / blocksize);
559 if (!sector)
560 break;
561 offset = (u32)*ppos % blocksize;
562 bh = affs_bread(inode->i_dev,sector,AFFS_I2BSIZE(inode));
563 if (!bh)
564 break;
565 data = bh->b_data + 24;
566 size = MIN(blocksize - offset,left);
567 *ppos += size;
568 copy_to_user(buf,data + offset,size);
569 buf += size;
570 affs_brelse(bh);
572 if (start == buf)
573 return -EIO;
574 return buf - start;
577 static ssize_t
578 affs_file_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
580 struct inode *inode = filp->f_dentry->d_inode;
581 off_t pos;
582 ssize_t written;
583 ssize_t c;
584 ssize_t blocksize;
585 struct buffer_head *bh;
586 char *p;
588 if (!count)
589 return 0;
590 pr_debug("AFFS: file_write(ino=%lu,pos=%lu,count=%d)\n",inode->i_ino,
591 (unsigned long)*ppos,count);
593 if (!inode) {
594 affs_error(inode->i_sb,"file_write","Inode = NULL");
595 return -EINVAL;
597 if (!S_ISREG(inode->i_mode)) {
598 affs_error(inode->i_sb,"file_write",
599 "Trying to write to non-regular file (mode=%07o)",
600 inode->i_mode);
601 return -EINVAL;
603 if (!inode->u.affs_i.i_ec && alloc_ext_cache(inode))
604 return -ENOMEM;
605 if (filp->f_flags & O_APPEND)
606 pos = inode->i_size;
607 else
608 pos = *ppos;
609 written = 0;
610 blocksize = AFFS_I2BSIZE(inode);
612 while (written < count) {
613 bh = affs_getblock(inode,pos / blocksize);
614 if (!bh) {
615 if (!written)
616 written = -ENOSPC;
617 break;
619 c = blocksize - (pos % blocksize);
620 if (c > count - written)
621 c = count - written;
622 if (c != blocksize && !buffer_uptodate(bh)) {
623 ll_rw_block(READ,1,&bh);
624 wait_on_buffer(bh);
625 if (!buffer_uptodate(bh)) {
626 affs_brelse(bh);
627 if (!written)
628 written = -EIO;
629 break;
632 p = (pos % blocksize) + bh->b_data;
633 c -= copy_from_user(p,buf,c);
634 if (!c) {
635 affs_brelse(bh);
636 if (!written)
637 written = -EFAULT;
638 break;
640 update_vm_cache(inode,pos,p,c);
641 mark_buffer_uptodate(bh,1);
642 mark_buffer_dirty(bh,0);
643 affs_brelse(bh);
644 pos += c;
645 written += c;
646 buf += c;
648 if (pos > inode->i_size)
649 inode->i_size = pos;
650 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
651 *ppos = pos;
652 mark_inode_dirty(inode);
653 return written;
656 static ssize_t
657 affs_file_write_ofs(struct file *filp, const char *buf, size_t count, loff_t *ppos)
659 struct inode *inode = filp->f_dentry->d_inode;
660 off_t pos;
661 ssize_t written;
662 ssize_t c;
663 ssize_t blocksize;
664 struct buffer_head *bh;
665 char *p;
667 pr_debug("AFFS: file_write_ofs(ino=%lu,pos=%lu,count=%d)\n",inode->i_ino,
668 (unsigned long)*ppos,count);
670 if (!count)
671 return 0;
672 if (!inode) {
673 affs_error(inode->i_sb,"file_write_ofs","Inode = NULL");
674 return -EINVAL;
676 if (!S_ISREG(inode->i_mode)) {
677 affs_error(inode->i_sb,"file_write_ofs",
678 "Trying to write to non-regular file (mode=%07o)",
679 inode->i_mode);
680 return -EINVAL;
682 if (!inode->u.affs_i.i_ec && alloc_ext_cache(inode))
683 return -ENOMEM;
684 if (filp->f_flags & O_APPEND)
685 pos = inode->i_size;
686 else
687 pos = *ppos;
689 bh = NULL;
690 blocksize = AFFS_I2BSIZE(inode) - 24;
691 written = 0;
692 while (written < count) {
693 bh = affs_getblock(inode,pos / blocksize);
694 if (!bh) {
695 if (!written)
696 written = -ENOSPC;
697 break;
699 c = blocksize - (pos % blocksize);
700 if (c > count - written)
701 c = count - written;
702 if (c != blocksize && !buffer_uptodate(bh)) {
703 ll_rw_block(READ,1,&bh);
704 wait_on_buffer(bh);
705 if (!buffer_uptodate(bh)) {
706 affs_brelse(bh);
707 if (!written)
708 written = -EIO;
709 break;
712 p = (pos % blocksize) + bh->b_data + 24;
713 c -= copy_from_user(p,buf,c);
714 if (!c) {
715 affs_brelse(bh);
716 if (!written)
717 written = -EFAULT;
718 break;
720 update_vm_cache(inode,pos,p,c);
722 pos += c;
723 buf += c;
724 written += c;
725 DATA_FRONT(bh)->data_size = cpu_to_be32(be32_to_cpu(DATA_FRONT(bh)->data_size) + c);
726 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
727 mark_buffer_uptodate(bh,1);
728 mark_buffer_dirty(bh,0);
729 affs_brelse(bh);
731 if (pos > inode->i_size)
732 inode->i_size = pos;
733 *ppos = pos;
734 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
735 mark_inode_dirty(inode);
736 return written;
739 /* Free any preallocated blocks. */
741 void
742 affs_free_prealloc(struct inode *inode)
744 struct super_block *sb = inode->i_sb;
745 struct affs_zone *zone;
746 int block;
748 pr_debug("AFFS: free_prealloc(ino=%lu)\n", inode->i_ino);
750 while (inode->u.affs_i.i_pa_cnt) {
751 block = inode->u.affs_i.i_data[inode->u.affs_i.i_pa_next++];
752 inode->u.affs_i.i_pa_next &= AFFS_MAX_PREALLOC - 1;
753 inode->u.affs_i.i_pa_cnt--;
754 affs_free_block(sb, block);
756 if (inode->u.affs_i.i_zone) {
757 zone = &sb->u.affs_sb.s_zones[inode->u.affs_i.i_zone];
758 if (zone->z_ino == inode->i_ino)
759 zone->z_ino = 0;
763 /* Truncate (or enlarge) a file to the requested size. */
765 void
766 affs_truncate(struct inode *inode)
768 struct buffer_head *bh = NULL;
769 int first; /* First block to be thrown away */
770 int block;
771 s32 key;
772 s32 *keyp;
773 s32 ekey;
774 s32 ptype, stype;
775 int freethis;
776 int net_blocksize;
777 int blocksize = AFFS_I2BSIZE(inode);
778 int rem;
779 int ext;
781 pr_debug("AFFS: truncate(inode=%ld,size=%lu)\n",inode->i_ino,inode->i_size);
783 net_blocksize = blocksize - ((inode->i_sb->u.affs_sb.s_flags & SF_OFS) ? 24 : 0);
784 first = (inode->i_size + net_blocksize - 1) / net_blocksize;
785 if (inode->u.affs_i.i_lastblock < first - 1) {
786 /* There has to be at least one new block to be allocated */
787 if (!inode->u.affs_i.i_ec && alloc_ext_cache(inode)) {
788 /* XXX Fine! No way to indicate an error. */
789 return /* -ENOSPC */;
791 bh = affs_getblock(inode,first - 1);
792 if (!bh) {
793 affs_warning(inode->i_sb,"truncate","Cannot extend file");
794 inode->i_size = net_blocksize * (inode->u.affs_i.i_lastblock + 1);
795 } else if (inode->i_sb->u.affs_sb.s_flags & SF_OFS) {
796 rem = inode->i_size % net_blocksize;
797 DATA_FRONT(bh)->data_size = cpu_to_be32(rem ? rem : net_blocksize);
798 affs_fix_checksum(blocksize,bh->b_data,5);
799 mark_buffer_dirty(bh,0);
801 goto out_truncate;
803 ekey = inode->i_ino;
804 ext = 0;
806 /* Free all blocks starting at 'first' and all then-empty
807 * extension blocks. Do not free the header block, though.
809 while (ekey) {
810 if (!(bh = affs_bread(inode->i_dev,ekey,blocksize))) {
811 affs_error(inode->i_sb,"truncate","Cannot read block %d",ekey);
812 goto out_truncate;
814 if (affs_checksum_block(blocksize,bh->b_data,&ptype,&stype)) {
815 affs_error(inode->i_sb,"truncate","Checksum error in header/ext block %d",
816 ekey);
817 goto out_truncate;
819 if (stype != ST_FILE || (ptype != T_SHORT && ptype != T_LIST)) {
820 affs_error(inode->i_sb,"truncate",
821 "Bad block (key=%d, ptype=%d, stype=%d)",ekey,ptype,stype);
822 goto out_truncate;
824 /* Do we have to free this extension block after
825 * freeing the data blocks pointed to?
827 freethis = first == 0 && ekey != inode->i_ino;
829 /* Free the data blocks. 'first' is relative to this
830 * extension block and may well lie behind this block.
832 for (block = first; block < AFFS_I2HSIZE(inode); block++) {
833 keyp = &AFFS_BLOCK(bh->b_data,inode,block);
834 key = be32_to_cpu(*keyp);
835 if (key) {
836 *keyp = 0;
837 affs_free_block(inode->i_sb,key);
838 } else
839 break;
841 keyp = &GET_END_PTR(struct file_end,bh->b_data,blocksize)->extension;
842 key = be32_to_cpu(*keyp);
844 /* If 'first' is in this block or is the first
845 * in the next one, this will be the last in
846 * the list, thus we have to adjust the count
847 * and zero the pointer to the next ext block.
849 if (first <= AFFS_I2HSIZE(inode)) {
850 ((struct file_front *)bh->b_data)->block_count = cpu_to_be32(first);
851 first = 0;
852 *keyp = 0;
853 affs_fix_checksum(blocksize,bh->b_data,5);
854 mark_buffer_dirty(bh,1);
855 } else
856 first -= AFFS_I2HSIZE(inode);
857 affs_brelse(bh);
858 bh = NULL;
859 if (freethis) /* Don't bother fixing checksum */
860 affs_free_block(inode->i_sb,ekey);
861 ekey = key;
863 block = ((inode->i_size + net_blocksize - 1) / net_blocksize) - 1;
864 inode->u.affs_i.i_lastblock = block;
866 /* If the file is not truncated to a block boundary,
867 * the partial block after the EOF must be zeroed
868 * so it cannot become accessible again.
871 rem = inode->i_size % net_blocksize;
872 if (rem) {
873 if ((inode->i_sb->u.affs_sb.s_flags & SF_OFS))
874 rem += 24;
875 pr_debug("AFFS: Zeroing from offset %d in block %d\n",rem,block);
876 bh = affs_getblock(inode,block);
877 if (bh) {
878 memset(bh->b_data + rem,0,blocksize - rem);
879 if ((inode->i_sb->u.affs_sb.s_flags & SF_OFS)) {
880 ((struct data_front *)bh->b_data)->data_size = cpu_to_be32(rem);
881 ((struct data_front *)bh->b_data)->next_data = 0;
882 affs_fix_checksum(blocksize,bh->b_data,5);
884 mark_buffer_dirty(bh,1);
885 } else
886 affs_error(inode->i_sb,"truncate","Cannot read block %d",block);
889 out_truncate:
890 affs_brelse(bh);
891 /* Invalidate cache */
892 if (inode->u.affs_i.i_ec) {
893 inode->u.affs_i.i_ec->max_ext = 0;
894 for (key = 0; key < 3; key++) {
895 inode->u.affs_i.i_ec->kc[key].kc_next_key = 0;
896 inode->u.affs_i.i_ec->kc[key].kc_last = -1;
899 mark_inode_dirty(inode);
903 * Called only when we need to allocate the extension cache.
906 static int
907 alloc_ext_cache(struct inode *inode)
909 s32 key;
910 int i;
911 unsigned long cache_page;
912 int error = 0;
914 pr_debug("AFFS: alloc_ext_cache(ino=%lu)\n",inode->i_ino);
916 cache_page = get_free_page(GFP_KERNEL);
918 * Check whether somebody else allocated it for us ...
920 if (inode->u.affs_i.i_ec)
921 goto out_free;
922 if (!cache_page)
923 goto out_error;
925 inode->u.affs_i.i_ec = (struct ext_cache *) cache_page;
926 /* We only have to initialize non-zero values.
927 * get_free_page() zeroed the page already.
929 key = inode->i_ino;
930 inode->u.affs_i.i_ec->ec[0] = key;
931 for (i = 0; i < 4; i++) {
932 inode->u.affs_i.i_ec->kc[i].kc_this_key = key;
933 inode->u.affs_i.i_ec->kc[i].kc_last = -1;
935 out:
936 return error;
938 out_free:
939 if (cache_page)
940 free_page(cache_page);
941 goto out;
943 out_error:
944 affs_error(inode->i_sb,"alloc_ext_cache","Cache allocation failed");
945 error = -ENOMEM;
946 goto out;