Import 2.1.118
[davej-history.git] / fs / affs / file.c
blob1961b4ec34935a2eece56ba69ab4dd4e42b0c82e
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, /* updatepage */
84 NULL /* revalidate */
87 static struct file_operations affs_file_operations_ofs = {
88 NULL, /* lseek - default */
89 affs_file_read_ofs, /* read */
90 affs_file_write_ofs, /* write */
91 NULL, /* readdir - bad */
92 NULL, /* poll - default */
93 NULL, /* ioctl - default */
94 NULL, /* mmap */
95 NULL, /* no special open */
96 NULL, /* flush */
97 NULL, /* release */
98 file_fsync, /* brute force, but works */
99 NULL, /* fasync */
100 NULL, /* check_media_change */
101 NULL, /* revalidate */
102 NULL /* lock */
105 struct inode_operations affs_file_inode_operations_ofs = {
106 &affs_file_operations_ofs, /* default file operations */
107 NULL, /* create */
108 NULL, /* lookup */
109 NULL, /* link */
110 NULL, /* unlink */
111 NULL, /* symlink */
112 NULL, /* mkdir */
113 NULL, /* rmdir */
114 NULL, /* mknod */
115 NULL, /* rename */
116 NULL, /* readlink */
117 NULL, /* follow_link */
118 NULL, /* readpage */
119 NULL, /* writepage */
120 NULL, /* bmap */
121 affs_truncate, /* truncate */
122 NULL, /* permission */
123 NULL, /* smap */
124 NULL, /* updatepage */
125 NULL /* revalidate */
128 #define AFFS_ISINDEX(x) ((x < 129) || \
129 (x < 512 && (x & 1) == 0) || \
130 (x < 1024 && (x & 3) == 0) || \
131 (x < 2048 && (x & 15) == 0) || \
132 (x < 4096 && (x & 63) == 0) || \
133 (x < 20480 && (x & 255) == 0) || \
134 (x < 36864 && (x & 511) == 0))
136 /* The keys of the extension blocks are stored in a 512-entry
137 * deep cache. In order to save memory, not every key of later
138 * extension blocks is stored - the larger the file gets, the
139 * bigger the holes in between.
142 static int
143 seqnum_to_index(int seqnum)
145 /* All of the first 127 keys are stored */
146 if (seqnum < 128)
147 return seqnum;
148 seqnum -= 128;
150 /* Of the next 384 keys, every 2nd is kept */
151 if (seqnum < (192 * 2))
152 return 128 + (seqnum >> 1);
153 seqnum -= 192 * 2;
155 /* Every 4th of the next 512 */
156 if (seqnum < (128 * 4))
157 return 128 + 192 + (seqnum >> 2);
158 seqnum -= 128 * 4;
160 /* Every 16th of the next 1024 */
161 if (seqnum < (64 * 16))
162 return 128 + 192 + 128 + (seqnum >> 4);
163 seqnum -= 64 * 16;
165 /* Every 64th of the next 2048 */
166 if (seqnum < (32 * 64))
167 return 128 + 192 + 128 + 64 + (seqnum >> 6);
168 seqnum -= 32 * 64;
170 /* Every 256th of the next 16384 */
171 if (seqnum < (64 * 256))
172 return 128 + 192 + 128 + 64 + 32 + (seqnum >> 8);
173 seqnum -= 64 * 256;
175 /* Every 512th upto 36479 (1.3 GB with 512 byte blocks).
176 * Seeking to positions behind this will get slower
177 * than dead snails nailed to the ground. But if
178 * someone uses files that large with 512-byte blocks,
179 * he or she deserves no better.
182 if (seqnum > (31 * 512))
183 seqnum = 31 * 512;
184 return 128 + 192 + 128 + 64 + 32 + 64 + (seqnum >> 9);
187 /* Now the other way round: Calculate the sequence
188 * number of an extension block of a key at the
189 * given index in the cache.
192 static int
193 index_to_seqnum(int index)
195 if (index < 128)
196 return index;
197 index -= 128;
198 if (index < 192)
199 return 128 + (index << 1);
200 index -= 192;
201 if (index < 128)
202 return 128 + 192 * 2 + (index << 2);
203 index -= 128;
204 if (index < 64)
205 return 128 + 192 * 2 + 128 * 4 + (index << 4);
206 index -= 64;
207 if (index < 32)
208 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + (index << 6);
209 index -= 32;
210 if (index < 64)
211 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + 32 * 64 + (index << 8);
212 index -= 64;
213 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + 32 * 64 + 64 * 256 + (index << 9);
216 static s32 __inline__
217 calc_key(struct inode *inode, int *ext)
219 int index;
220 struct key_cache *kc;
222 for (index = 0; index < 4; index++) {
223 kc = &inode->u.affs_i.i_ec->kc[index];
224 if (*ext == kc->kc_this_seq) {
225 return kc->kc_this_key;
226 } else if (*ext == kc->kc_this_seq + 1) {
227 if (kc->kc_next_key)
228 return kc->kc_next_key;
229 else {
230 (*ext)--;
231 return kc->kc_this_key;
235 index = seqnum_to_index(*ext);
236 if (index > inode->u.affs_i.i_ec->max_ext)
237 index = inode->u.affs_i.i_ec->max_ext;
238 *ext = index_to_seqnum(index);
239 return inode->u.affs_i.i_ec->ec[index];
242 static int
243 affs_bmap(struct inode *inode, int block)
245 struct buffer_head *bh;
246 s32 key, nkey;
247 s32 ptype, stype;
248 int ext;
249 int index;
250 int keycount;
251 struct key_cache *kc;
252 struct key_cache *tkc;
253 struct timeval tv;
254 s32 *keyp;
255 int i;
257 pr_debug("AFFS: bmap(%lu,%d)\n",inode->i_ino,block);
259 if (block < 0) {
260 affs_error(inode->i_sb,"bmap","Block < 0");
261 return 0;
263 if (!inode->u.affs_i.i_ec) {
264 if (alloc_ext_cache(inode)) {
265 return 0;
269 /* Try to find the requested key in the cache.
270 * In order to speed this up as much as possible,
271 * the cache line lookup is done in a separate
272 * step.
275 for (i = 0; i < 4; i++) {
276 tkc = &inode->u.affs_i.i_ec->kc[i];
277 /* Look in any cache if the key is there */
278 if (block <= tkc->kc_last && block >= tkc->kc_first) {
279 return tkc->kc_keys[block - tkc->kc_first];
282 kc = NULL;
283 tv = xtime;
284 for (i = 0; i < 4; i++) {
285 tkc = &inode->u.affs_i.i_ec->kc[i];
286 if (tkc->kc_lru_time.tv_sec > tv.tv_sec)
287 continue;
288 if (tkc->kc_lru_time.tv_sec < tv.tv_sec ||
289 tkc->kc_lru_time.tv_usec < tv.tv_usec) {
290 kc = tkc;
291 tv = tkc->kc_lru_time;
294 if (!kc) /* Really shouldn't happen */
295 kc = tkc;
296 kc->kc_lru_time = xtime;
297 keyp = kc->kc_keys;
298 kc->kc_first = block;
299 kc->kc_last = -1;
300 keycount = AFFS_KCSIZE;
302 /* Calculate sequence number of the extension block where the
303 * number of the requested block is stored. 0 means it's in
304 * the file header.
307 ext = block / AFFS_I2HSIZE(inode);
308 key = calc_key(inode,&ext);
309 block -= ext * AFFS_I2HSIZE(inode);
311 for (;;) {
312 bh = affs_bread(inode->i_dev,key,AFFS_I2BSIZE(inode));
313 if (!bh)
314 return 0;
315 index = seqnum_to_index(ext);
316 if (index > inode->u.affs_i.i_ec->max_ext &&
317 (affs_checksum_block(AFFS_I2BSIZE(inode),bh->b_data,&ptype,&stype) ||
318 (ptype != T_SHORT && ptype != T_LIST) || stype != ST_FILE)) {
319 affs_brelse(bh);
320 return 0;
322 nkey = be32_to_cpu(FILE_END(bh->b_data,inode)->extension);
323 if (block < AFFS_I2HSIZE(inode)) {
324 /* Fill cache as much as possible */
325 if (keycount) {
326 kc->kc_first = ext * AFFS_I2HSIZE(inode) + block;
327 keycount = keycount < AFFS_I2HSIZE(inode) - block ? keycount :
328 AFFS_I2HSIZE(inode) - block;
329 for (i = 0; i < keycount; i++)
330 kc->kc_keys[i] = be32_to_cpu(AFFS_BLOCK(bh->b_data,inode,block + i));
331 kc->kc_last = kc->kc_first + i - 1;
333 break;
335 block -= AFFS_I2HSIZE(inode);
336 affs_brelse(bh);
337 ext++;
338 if (index > inode->u.affs_i.i_ec->max_ext && AFFS_ISINDEX(ext)) {
339 inode->u.affs_i.i_ec->ec[index] = nkey;
340 inode->u.affs_i.i_ec->max_ext = index;
342 key = nkey;
344 kc->kc_this_key = key;
345 kc->kc_this_seq = ext;
346 kc->kc_next_key = nkey;
347 key = be32_to_cpu(AFFS_BLOCK(bh->b_data,inode,block));
348 affs_brelse(bh);
349 return key;
352 /* With the affs, getting a random block from a file is not
353 * a simple business. Since this fs does not allow holes,
354 * it may be necessary to allocate all the missing blocks
355 * in between, as well as some new extension blocks. The OFS
356 * is even worse: All data blocks contain pointers to the
357 * next ones, so you have to fix [n-1] after allocating [n].
358 * What a mess.
361 static struct buffer_head *
362 affs_getblock(struct inode *inode, s32 block)
364 struct super_block *sb = inode->i_sb;
365 int ofs = sb->u.affs_sb.s_flags & SF_OFS;
366 int ext = block / AFFS_I2HSIZE(inode);
367 struct buffer_head *bh, *ebh, *pbh = NULL;
368 struct key_cache *kc;
369 s32 key, nkey;
370 int cf, j, pt;
371 int index;
372 int err;
374 pr_debug("AFFS: getblock(%lu,%d)\n",inode->i_ino,block);
376 if (block < 0)
377 goto out_fail;
379 key = calc_key(inode,&ext);
380 block -= ext * AFFS_I2HSIZE(inode);
381 pt = ext ? T_LIST : T_SHORT;
383 /* Key refers now to the last known extension block,
384 * ext is its sequence number (if 0, key refers to the
385 * header block), and block is the block number relative
386 * to the first block stored in that extension block.
388 for (;;) { /* Loop over header block and extension blocks */
389 struct file_front *fdp;
391 bh = affs_bread(inode->i_dev,key,AFFS_I2BSIZE(inode));
392 if (!bh)
393 goto out_fail;
394 fdp = (struct file_front *) bh->b_data;
395 err = affs_checksum_block(AFFS_I2BSIZE(inode),bh->b_data,&cf,&j);
396 if (err || cf != pt || j != ST_FILE) {
397 affs_error(sb, "getblock",
398 "Block %d is not a valid %s", key,
399 pt == T_SHORT ? "file header" : "ext block");
400 goto out_free_bh;
402 j = be32_to_cpu(((struct file_front *)bh->b_data)->block_count);
403 for (cf = 0; j < AFFS_I2HSIZE(inode) && j <= block; j++) {
404 if (ofs && !pbh && inode->u.affs_i.i_lastblock >= 0) {
405 if (j > 0) {
406 s32 k = AFFS_BLOCK(bh->b_data, inode,
407 j - 1);
408 pbh = affs_bread(inode->i_dev,
409 be32_to_cpu(k),
410 AFFS_I2BSIZE(inode));
411 } else
412 pbh = affs_getblock(inode,inode->u.affs_i.i_lastblock);
413 if (!pbh) {
414 affs_error(sb,"getblock",
415 "Cannot get last block in file");
416 break;
419 nkey = affs_new_data(inode);
420 if (!nkey)
421 break;
422 inode->u.affs_i.i_lastblock++;
423 if (AFFS_BLOCK(bh->b_data,inode,j)) {
424 affs_warning(sb,"getblock","Block already allocated");
425 affs_free_block(sb,nkey);
426 continue;
428 AFFS_BLOCK(bh->b_data,inode,j) = cpu_to_be32(nkey);
429 if (ofs) {
430 ebh = affs_bread(inode->i_dev,nkey,AFFS_I2BSIZE(inode));
431 if (!ebh) {
432 affs_error(sb,"getblock",
433 "Cannot get block %d",nkey);
434 affs_free_block(sb,nkey);
435 AFFS_BLOCK(bh->b_data,inode,j) = 0;
436 break;
438 DATA_FRONT(ebh)->primary_type = cpu_to_be32(T_DATA);
439 DATA_FRONT(ebh)->header_key = cpu_to_be32(inode->i_ino);
440 DATA_FRONT(ebh)->sequence_number = cpu_to_be32(inode->u.affs_i.i_lastblock + 1);
441 affs_fix_checksum(AFFS_I2BSIZE(inode),
442 ebh->b_data, 5);
443 mark_buffer_dirty(ebh, 0);
444 if (pbh) {
445 DATA_FRONT(pbh)->data_size = cpu_to_be32(AFFS_I2BSIZE(inode) - 24);
446 DATA_FRONT(pbh)->next_data = cpu_to_be32(nkey);
447 affs_fix_checksum(AFFS_I2BSIZE(inode),pbh->b_data,5);
448 mark_buffer_dirty(pbh,0);
449 affs_brelse(pbh);
451 pbh = ebh;
453 cf = 1;
455 /* N.B. May need to release pbh after here */
457 if (cf) {
458 if (pt == T_SHORT)
459 fdp->first_data = AFFS_BLOCK(bh->b_data,inode,0);
460 fdp->block_count = cpu_to_be32(j);
461 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
462 mark_buffer_dirty(bh,1);
465 if (block < j) {
466 if (pbh)
467 affs_brelse(pbh);
468 break;
470 if (j < AFFS_I2HSIZE(inode)) {
471 /* N.B. What about pbh here? */
472 goto out_free_bh;
475 block -= AFFS_I2HSIZE(inode);
476 key = be32_to_cpu(FILE_END(bh->b_data,inode)->extension);
477 if (!key) {
478 key = affs_new_header(inode);
479 if (!key)
480 goto out_free_bh;
481 ebh = affs_bread(inode->i_dev,key,AFFS_I2BSIZE(inode));
482 if (!ebh) {
483 /* N.B. must free bh here */
484 goto out_free_block;
486 ((struct file_front *)ebh->b_data)->primary_type = cpu_to_be32(T_LIST);
487 ((struct file_front *)ebh->b_data)->own_key = cpu_to_be32(key);
488 FILE_END(ebh->b_data,inode)->secondary_type = cpu_to_be32(ST_FILE);
489 FILE_END(ebh->b_data,inode)->parent = cpu_to_be32(inode->i_ino);
490 affs_fix_checksum(AFFS_I2BSIZE(inode),ebh->b_data,5);
491 mark_buffer_dirty(ebh, 1);
492 FILE_END(bh->b_data,inode)->extension = cpu_to_be32(key);
493 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
494 mark_buffer_dirty(bh,1);
495 affs_brelse(bh);
496 bh = ebh;
498 pt = T_LIST;
499 ext++;
500 index = seqnum_to_index(ext);
501 if (index > inode->u.affs_i.i_ec->max_ext &&
502 AFFS_ISINDEX(ext)) {
503 inode->u.affs_i.i_ec->ec[index] = key;
504 inode->u.affs_i.i_ec->max_ext = index;
506 affs_brelse(bh);
509 /* Invalidate key cache */
510 for (j = 0; j < 4; j++) {
511 kc = &inode->u.affs_i.i_ec->kc[j];
512 kc->kc_last = -1;
514 key = be32_to_cpu(AFFS_BLOCK(bh->b_data,inode,block));
515 affs_brelse(bh);
516 if (!key)
517 goto out_fail;
519 bh = affs_bread(inode->i_dev, key, AFFS_I2BSIZE(inode));
520 return bh;
522 out_free_block:
523 affs_free_block(sb, key);
524 out_free_bh:
525 affs_brelse(bh);
526 out_fail:
527 return NULL;
530 static ssize_t
531 affs_file_read_ofs(struct file *filp, char *buf, size_t count, loff_t *ppos)
533 struct inode *inode = filp->f_dentry->d_inode;
534 char *start;
535 ssize_t left, offset, size, sector;
536 ssize_t blocksize;
537 struct buffer_head *bh;
538 void *data;
540 pr_debug("AFFS: file_read_ofs(ino=%lu,pos=%lu,%d)\n",inode->i_ino,
541 (unsigned long)*ppos,count);
543 if (!inode) {
544 affs_error(inode->i_sb,"file_read_ofs","Inode = NULL");
545 return -EINVAL;
547 blocksize = AFFS_I2BSIZE(inode) - 24;
548 if (!(S_ISREG(inode->i_mode))) {
549 pr_debug("AFFS: file_read: mode = %07o",inode->i_mode);
550 return -EINVAL;
552 if (*ppos >= inode->i_size || count <= 0)
553 return 0;
555 start = buf;
556 for (;;) {
557 left = MIN (inode->i_size - *ppos,count - (buf - start));
558 if (!left)
559 break;
560 sector = affs_bmap(inode,(u32)*ppos / blocksize);
561 if (!sector)
562 break;
563 offset = (u32)*ppos % blocksize;
564 bh = affs_bread(inode->i_dev,sector,AFFS_I2BSIZE(inode));
565 if (!bh)
566 break;
567 data = bh->b_data + 24;
568 size = MIN(blocksize - offset,left);
569 *ppos += size;
570 copy_to_user(buf,data + offset,size);
571 buf += size;
572 affs_brelse(bh);
574 if (start == buf)
575 return -EIO;
576 return buf - start;
579 static ssize_t
580 affs_file_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
582 struct inode *inode = filp->f_dentry->d_inode;
583 off_t pos;
584 ssize_t written;
585 ssize_t c;
586 ssize_t blocksize;
587 struct buffer_head *bh;
588 char *p;
590 if (!count)
591 return 0;
592 pr_debug("AFFS: file_write(ino=%lu,pos=%lu,count=%d)\n",inode->i_ino,
593 (unsigned long)*ppos,count);
595 if (!inode) {
596 affs_error(inode->i_sb,"file_write","Inode = NULL");
597 return -EINVAL;
599 if (!S_ISREG(inode->i_mode)) {
600 affs_error(inode->i_sb,"file_write",
601 "Trying to write to non-regular file (mode=%07o)",
602 inode->i_mode);
603 return -EINVAL;
605 if (!inode->u.affs_i.i_ec && alloc_ext_cache(inode))
606 return -ENOMEM;
607 if (filp->f_flags & O_APPEND)
608 pos = inode->i_size;
609 else
610 pos = *ppos;
611 written = 0;
612 blocksize = AFFS_I2BSIZE(inode);
614 while (written < count) {
615 bh = affs_getblock(inode,pos / blocksize);
616 if (!bh) {
617 if (!written)
618 written = -ENOSPC;
619 break;
621 c = blocksize - (pos % blocksize);
622 if (c > count - written)
623 c = count - written;
624 if (c != blocksize && !buffer_uptodate(bh)) {
625 ll_rw_block(READ,1,&bh);
626 wait_on_buffer(bh);
627 if (!buffer_uptodate(bh)) {
628 affs_brelse(bh);
629 if (!written)
630 written = -EIO;
631 break;
634 p = (pos % blocksize) + bh->b_data;
635 c -= copy_from_user(p,buf,c);
636 if (!c) {
637 affs_brelse(bh);
638 if (!written)
639 written = -EFAULT;
640 break;
642 update_vm_cache(inode,pos,p,c);
643 mark_buffer_uptodate(bh,1);
644 mark_buffer_dirty(bh,0);
645 affs_brelse(bh);
646 pos += c;
647 written += c;
648 buf += c;
650 if (pos > inode->i_size)
651 inode->i_size = pos;
652 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
653 *ppos = pos;
654 mark_inode_dirty(inode);
655 return written;
658 static ssize_t
659 affs_file_write_ofs(struct file *filp, const char *buf, size_t count, loff_t *ppos)
661 struct inode *inode = filp->f_dentry->d_inode;
662 off_t pos;
663 ssize_t written;
664 ssize_t c;
665 ssize_t blocksize;
666 struct buffer_head *bh;
667 char *p;
669 pr_debug("AFFS: file_write_ofs(ino=%lu,pos=%lu,count=%d)\n",inode->i_ino,
670 (unsigned long)*ppos,count);
672 if (!count)
673 return 0;
674 if (!inode) {
675 affs_error(inode->i_sb,"file_write_ofs","Inode = NULL");
676 return -EINVAL;
678 if (!S_ISREG(inode->i_mode)) {
679 affs_error(inode->i_sb,"file_write_ofs",
680 "Trying to write to non-regular file (mode=%07o)",
681 inode->i_mode);
682 return -EINVAL;
684 if (!inode->u.affs_i.i_ec && alloc_ext_cache(inode))
685 return -ENOMEM;
686 if (filp->f_flags & O_APPEND)
687 pos = inode->i_size;
688 else
689 pos = *ppos;
691 bh = NULL;
692 blocksize = AFFS_I2BSIZE(inode) - 24;
693 written = 0;
694 while (written < count) {
695 bh = affs_getblock(inode,pos / blocksize);
696 if (!bh) {
697 if (!written)
698 written = -ENOSPC;
699 break;
701 c = blocksize - (pos % blocksize);
702 if (c > count - written)
703 c = count - written;
704 if (c != blocksize && !buffer_uptodate(bh)) {
705 ll_rw_block(READ,1,&bh);
706 wait_on_buffer(bh);
707 if (!buffer_uptodate(bh)) {
708 affs_brelse(bh);
709 if (!written)
710 written = -EIO;
711 break;
714 p = (pos % blocksize) + bh->b_data + 24;
715 c -= copy_from_user(p,buf,c);
716 if (!c) {
717 affs_brelse(bh);
718 if (!written)
719 written = -EFAULT;
720 break;
722 update_vm_cache(inode,pos,p,c);
724 pos += c;
725 buf += c;
726 written += c;
727 DATA_FRONT(bh)->data_size = cpu_to_be32(be32_to_cpu(DATA_FRONT(bh)->data_size) + c);
728 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
729 mark_buffer_uptodate(bh,1);
730 mark_buffer_dirty(bh,0);
731 affs_brelse(bh);
733 if (pos > inode->i_size)
734 inode->i_size = pos;
735 *ppos = pos;
736 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
737 mark_inode_dirty(inode);
738 return written;
741 /* Free any preallocated blocks. */
743 void
744 affs_free_prealloc(struct inode *inode)
746 struct super_block *sb = inode->i_sb;
747 struct affs_zone *zone;
748 int block;
750 pr_debug("AFFS: free_prealloc(ino=%lu)\n", inode->i_ino);
752 while (inode->u.affs_i.i_pa_cnt) {
753 block = inode->u.affs_i.i_data[inode->u.affs_i.i_pa_next++];
754 inode->u.affs_i.i_pa_next &= AFFS_MAX_PREALLOC - 1;
755 inode->u.affs_i.i_pa_cnt--;
756 affs_free_block(sb, block);
758 if (inode->u.affs_i.i_zone) {
759 zone = &sb->u.affs_sb.s_zones[inode->u.affs_i.i_zone];
760 if (zone->z_ino == inode->i_ino)
761 zone->z_ino = 0;
765 /* Truncate (or enlarge) a file to the requested size. */
767 void
768 affs_truncate(struct inode *inode)
770 struct buffer_head *bh = NULL;
771 int first; /* First block to be thrown away */
772 int block;
773 s32 key;
774 s32 *keyp;
775 s32 ekey;
776 s32 ptype, stype;
777 int freethis;
778 int net_blocksize;
779 int blocksize = AFFS_I2BSIZE(inode);
780 int rem;
781 int ext;
783 pr_debug("AFFS: truncate(inode=%ld,size=%lu)\n",inode->i_ino,inode->i_size);
785 net_blocksize = blocksize - ((inode->i_sb->u.affs_sb.s_flags & SF_OFS) ? 24 : 0);
786 first = (inode->i_size + net_blocksize - 1) / net_blocksize;
787 if (inode->u.affs_i.i_lastblock < first - 1) {
788 /* There has to be at least one new block to be allocated */
789 if (!inode->u.affs_i.i_ec && alloc_ext_cache(inode)) {
790 /* XXX Fine! No way to indicate an error. */
791 return /* -ENOSPC */;
793 bh = affs_getblock(inode,first - 1);
794 if (!bh) {
795 affs_warning(inode->i_sb,"truncate","Cannot extend file");
796 inode->i_size = net_blocksize * (inode->u.affs_i.i_lastblock + 1);
797 } else if (inode->i_sb->u.affs_sb.s_flags & SF_OFS) {
798 rem = inode->i_size % net_blocksize;
799 DATA_FRONT(bh)->data_size = cpu_to_be32(rem ? rem : net_blocksize);
800 affs_fix_checksum(blocksize,bh->b_data,5);
801 mark_buffer_dirty(bh,0);
803 goto out_truncate;
805 ekey = inode->i_ino;
806 ext = 0;
808 /* Free all blocks starting at 'first' and all then-empty
809 * extension blocks. Do not free the header block, though.
811 while (ekey) {
812 if (!(bh = affs_bread(inode->i_dev,ekey,blocksize))) {
813 affs_error(inode->i_sb,"truncate","Cannot read block %d",ekey);
814 goto out_truncate;
816 if (affs_checksum_block(blocksize,bh->b_data,&ptype,&stype)) {
817 affs_error(inode->i_sb,"truncate","Checksum error in header/ext block %d",
818 ekey);
819 goto out_truncate;
821 if (stype != ST_FILE || (ptype != T_SHORT && ptype != T_LIST)) {
822 affs_error(inode->i_sb,"truncate",
823 "Bad block (key=%d, ptype=%d, stype=%d)",ekey,ptype,stype);
824 goto out_truncate;
826 /* Do we have to free this extension block after
827 * freeing the data blocks pointed to?
829 freethis = first == 0 && ekey != inode->i_ino;
831 /* Free the data blocks. 'first' is relative to this
832 * extension block and may well lie behind this block.
834 for (block = first; block < AFFS_I2HSIZE(inode); block++) {
835 keyp = &AFFS_BLOCK(bh->b_data,inode,block);
836 key = be32_to_cpu(*keyp);
837 if (key) {
838 *keyp = 0;
839 affs_free_block(inode->i_sb,key);
840 } else
841 break;
843 keyp = &GET_END_PTR(struct file_end,bh->b_data,blocksize)->extension;
844 key = be32_to_cpu(*keyp);
846 /* If 'first' is in this block or is the first
847 * in the next one, this will be the last in
848 * the list, thus we have to adjust the count
849 * and zero the pointer to the next ext block.
851 if (first <= AFFS_I2HSIZE(inode)) {
852 ((struct file_front *)bh->b_data)->block_count = cpu_to_be32(first);
853 first = 0;
854 *keyp = 0;
855 affs_fix_checksum(blocksize,bh->b_data,5);
856 mark_buffer_dirty(bh,1);
857 } else
858 first -= AFFS_I2HSIZE(inode);
859 affs_brelse(bh);
860 bh = NULL;
861 if (freethis) /* Don't bother fixing checksum */
862 affs_free_block(inode->i_sb,ekey);
863 ekey = key;
865 block = ((inode->i_size + net_blocksize - 1) / net_blocksize) - 1;
866 inode->u.affs_i.i_lastblock = block;
868 /* If the file is not truncated to a block boundary,
869 * the partial block after the EOF must be zeroed
870 * so it cannot become accessible again.
873 rem = inode->i_size % net_blocksize;
874 if (rem) {
875 if ((inode->i_sb->u.affs_sb.s_flags & SF_OFS))
876 rem += 24;
877 pr_debug("AFFS: Zeroing from offset %d in block %d\n",rem,block);
878 bh = affs_getblock(inode,block);
879 if (bh) {
880 memset(bh->b_data + rem,0,blocksize - rem);
881 if ((inode->i_sb->u.affs_sb.s_flags & SF_OFS)) {
882 ((struct data_front *)bh->b_data)->data_size = cpu_to_be32(rem);
883 ((struct data_front *)bh->b_data)->next_data = 0;
884 affs_fix_checksum(blocksize,bh->b_data,5);
886 mark_buffer_dirty(bh,1);
887 } else
888 affs_error(inode->i_sb,"truncate","Cannot read block %d",block);
891 out_truncate:
892 affs_brelse(bh);
893 /* Invalidate cache */
894 if (inode->u.affs_i.i_ec) {
895 inode->u.affs_i.i_ec->max_ext = 0;
896 for (key = 0; key < 3; key++) {
897 inode->u.affs_i.i_ec->kc[key].kc_next_key = 0;
898 inode->u.affs_i.i_ec->kc[key].kc_last = -1;
901 mark_inode_dirty(inode);
905 * Called only when we need to allocate the extension cache.
908 static int
909 alloc_ext_cache(struct inode *inode)
911 s32 key;
912 int i;
913 unsigned long cache_page;
914 int error = 0;
916 pr_debug("AFFS: alloc_ext_cache(ino=%lu)\n",inode->i_ino);
918 cache_page = get_free_page(GFP_KERNEL);
920 * Check whether somebody else allocated it for us ...
922 if (inode->u.affs_i.i_ec)
923 goto out_free;
924 if (!cache_page)
925 goto out_error;
927 inode->u.affs_i.i_ec = (struct ext_cache *) cache_page;
928 /* We only have to initialize non-zero values.
929 * get_free_page() zeroed the page already.
931 key = inode->i_ino;
932 inode->u.affs_i.i_ec->ec[0] = key;
933 for (i = 0; i < 4; i++) {
934 inode->u.affs_i.i_ec->kc[i].kc_this_key = key;
935 inode->u.affs_i.i_ec->kc[i].kc_last = -1;
937 out:
938 return error;
940 out_free:
941 if (cache_page)
942 free_page(cache_page);
943 goto out;
945 out_error:
946 affs_error(inode->i_sb,"alloc_ext_cache","Cache allocation failed");
947 error = -ENOMEM;
948 goto out;