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
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>
28 #include <linux/amigaffs.h>
30 #include <linux/pagemap.h>
32 #define MIN(a,b) (((a)<(b))?(a):(b))
33 #define MAX(a,b) (((a)>(b))?(a):(b))
36 #error PAGE_SIZE must be at least 4096
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 */
57 file_fsync
, /* brute force, but works */
59 NULL
, /* check_media_change */
60 NULL
, /* revalidate */
64 struct inode_operations affs_file_inode_operations
= {
65 &affs_file_operations
, /* default file operations */
76 NULL
, /* follow_link */
77 generic_readpage
, /* readpage */
80 affs_truncate
, /* truncate */
81 NULL
, /* permission */
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 */
94 NULL
, /* no special open */
97 file_fsync
, /* brute force, but works */
99 NULL
, /* check_media_change */
100 NULL
, /* revalidate */
104 struct inode_operations affs_file_inode_operations_ofs
= {
105 &affs_file_operations_ofs
, /* default file operations */
116 NULL
, /* follow_link */
118 NULL
, /* writepage */
120 affs_truncate
, /* truncate */
121 NULL
, /* permission */
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.
141 seqnum_to_index(int seqnum
)
143 /* All of the first 127 keys are stored */
148 /* Of the next 384 keys, every 2nd is kept */
149 if (seqnum
< (192 * 2))
150 return 128 + (seqnum
>> 1);
153 /* Every 4th of the next 512 */
154 if (seqnum
< (128 * 4))
155 return 128 + 192 + (seqnum
>> 2);
158 /* Every 16th of the next 1024 */
159 if (seqnum
< (64 * 16))
160 return 128 + 192 + 128 + (seqnum
>> 4);
163 /* Every 64th of the next 2048 */
164 if (seqnum
< (32 * 64))
165 return 128 + 192 + 128 + 64 + (seqnum
>> 6);
168 /* Every 256th of the next 16384 */
169 if (seqnum
< (64 * 256))
170 return 128 + 192 + 128 + 64 + 32 + (seqnum
>> 8);
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))
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.
191 index_to_seqnum(int index
)
197 return 128 + (index
<< 1);
200 return 128 + 192 * 2 + (index
<< 2);
203 return 128 + 192 * 2 + 128 * 4 + (index
<< 4);
206 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + (index
<< 6);
209 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + 32 * 64 + (index
<< 8);
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
)
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) {
226 return kc
->kc_next_key
;
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
];
241 affs_bmap(struct inode
*inode
, int block
)
243 struct buffer_head
*bh
;
249 struct key_cache
*kc
;
250 struct key_cache
*tkc
;
255 pr_debug("AFFS: bmap(%lu,%d)\n",inode
->i_ino
,block
);
258 affs_error(inode
->i_sb
,"bmap","Block < 0");
261 if (!inode
->u
.affs_i
.i_ec
) {
262 if (alloc_ext_cache(inode
)) {
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
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
];
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
)
286 if (tkc
->kc_lru_time
.tv_sec
< tv
.tv_sec
||
287 tkc
->kc_lru_time
.tv_usec
< tv
.tv_usec
) {
289 tv
= tkc
->kc_lru_time
;
292 if (!kc
) /* Really shouldn't happen */
294 kc
->kc_lru_time
= xtime
;
296 kc
->kc_first
= block
;
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
305 ext
= block
/ AFFS_I2HSIZE(inode
);
306 key
= calc_key(inode
,&ext
);
307 block
-= ext
* AFFS_I2HSIZE(inode
);
310 bh
= affs_bread(inode
->i_dev
,key
,AFFS_I2BSIZE(inode
));
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
)) {
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 */
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;
333 block
-= AFFS_I2HSIZE(inode
);
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
;
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
));
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].
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
;
372 pr_debug("AFFS: getblock(%lu,%d)\n",inode
->i_ino
,block
);
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
));
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");
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) {
404 s32 k
= AFFS_BLOCK(bh
->b_data
, inode
,
406 pbh
= affs_bread(inode
->i_dev
,
408 AFFS_I2BSIZE(inode
));
410 pbh
= affs_getblock(inode
,inode
->u
.affs_i
.i_lastblock
);
412 affs_error(sb
,"getblock",
413 "Cannot get last block in file");
417 nkey
= affs_new_data(inode
);
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
);
426 AFFS_BLOCK(bh
->b_data
,inode
,j
) = cpu_to_be32(nkey
);
428 ebh
= affs_bread(inode
->i_dev
,nkey
,AFFS_I2BSIZE(inode
));
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;
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
),
441 mark_buffer_dirty(ebh
, 0);
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);
453 /* N.B. May need to release pbh after here */
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);
468 if (j
< AFFS_I2HSIZE(inode
)) {
469 /* N.B. What about pbh here? */
473 block
-= AFFS_I2HSIZE(inode
);
474 key
= be32_to_cpu(FILE_END(bh
->b_data
,inode
)->extension
);
476 key
= affs_new_header(inode
);
479 ebh
= affs_bread(inode
->i_dev
,key
,AFFS_I2BSIZE(inode
));
481 /* N.B. must free bh here */
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);
498 index
= seqnum_to_index(ext
);
499 if (index
> inode
->u
.affs_i
.i_ec
->max_ext
&&
501 inode
->u
.affs_i
.i_ec
->ec
[index
] = key
;
502 inode
->u
.affs_i
.i_ec
->max_ext
= index
;
507 /* Invalidate key cache */
508 for (j
= 0; j
< 4; j
++) {
509 kc
= &inode
->u
.affs_i
.i_ec
->kc
[j
];
512 key
= be32_to_cpu(AFFS_BLOCK(bh
->b_data
,inode
,block
));
517 bh
= affs_bread(inode
->i_dev
, key
, AFFS_I2BSIZE(inode
));
521 affs_free_block(sb
, key
);
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
;
533 ssize_t left
, offset
, size
, sector
;
535 struct buffer_head
*bh
;
538 pr_debug("AFFS: file_read_ofs(ino=%lu,pos=%lu,%d)\n",inode
->i_ino
,
539 (unsigned long)*ppos
,count
);
542 affs_error(inode
->i_sb
,"file_read_ofs","Inode = NULL");
545 blocksize
= AFFS_I2BSIZE(inode
) - 24;
546 if (!(S_ISREG(inode
->i_mode
))) {
547 pr_debug("AFFS: file_read: mode = %07o",inode
->i_mode
);
550 if (*ppos
>= inode
->i_size
|| count
<= 0)
555 left
= MIN (inode
->i_size
- *ppos
,count
- (buf
- start
));
558 sector
= affs_bmap(inode
,(u32
)*ppos
/ blocksize
);
561 offset
= (u32
)*ppos
% blocksize
;
562 bh
= affs_bread(inode
->i_dev
,sector
,AFFS_I2BSIZE(inode
));
565 data
= bh
->b_data
+ 24;
566 size
= MIN(blocksize
- offset
,left
);
568 copy_to_user(buf
,data
+ offset
,size
);
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
;
585 struct buffer_head
*bh
;
590 pr_debug("AFFS: file_write(ino=%lu,pos=%lu,count=%d)\n",inode
->i_ino
,
591 (unsigned long)*ppos
,count
);
594 affs_error(inode
->i_sb
,"file_write","Inode = NULL");
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)",
603 if (!inode
->u
.affs_i
.i_ec
&& alloc_ext_cache(inode
))
605 if (filp
->f_flags
& O_APPEND
)
610 blocksize
= AFFS_I2BSIZE(inode
);
612 while (written
< count
) {
613 bh
= affs_getblock(inode
,pos
/ blocksize
);
619 c
= blocksize
- (pos
% blocksize
);
620 if (c
> count
- written
)
622 if (c
!= blocksize
&& !buffer_uptodate(bh
)) {
623 ll_rw_block(READ
,1,&bh
);
625 if (!buffer_uptodate(bh
)) {
632 p
= (pos
% blocksize
) + bh
->b_data
;
633 c
-= copy_from_user(p
,buf
,c
);
640 update_vm_cache(inode
,pos
,p
,c
);
641 mark_buffer_uptodate(bh
,1);
642 mark_buffer_dirty(bh
,0);
648 if (pos
> inode
->i_size
)
650 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
652 mark_inode_dirty(inode
);
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
;
664 struct buffer_head
*bh
;
667 pr_debug("AFFS: file_write_ofs(ino=%lu,pos=%lu,count=%d)\n",inode
->i_ino
,
668 (unsigned long)*ppos
,count
);
673 affs_error(inode
->i_sb
,"file_write_ofs","Inode = NULL");
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)",
682 if (!inode
->u
.affs_i
.i_ec
&& alloc_ext_cache(inode
))
684 if (filp
->f_flags
& O_APPEND
)
690 blocksize
= AFFS_I2BSIZE(inode
) - 24;
692 while (written
< count
) {
693 bh
= affs_getblock(inode
,pos
/ blocksize
);
699 c
= blocksize
- (pos
% blocksize
);
700 if (c
> count
- written
)
702 if (c
!= blocksize
&& !buffer_uptodate(bh
)) {
703 ll_rw_block(READ
,1,&bh
);
705 if (!buffer_uptodate(bh
)) {
712 p
= (pos
% blocksize
) + bh
->b_data
+ 24;
713 c
-= copy_from_user(p
,buf
,c
);
720 update_vm_cache(inode
,pos
,p
,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);
731 if (pos
> inode
->i_size
)
734 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
735 mark_inode_dirty(inode
);
739 /* Free any preallocated blocks. */
742 affs_free_prealloc(struct inode
*inode
)
744 struct super_block
*sb
= inode
->i_sb
;
745 struct affs_zone
*zone
;
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
)
763 /* Truncate (or enlarge) a file to the requested size. */
766 affs_truncate(struct inode
*inode
)
768 struct buffer_head
*bh
= NULL
;
769 int first
; /* First block to be thrown away */
777 int blocksize
= AFFS_I2BSIZE(inode
);
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);
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);
806 /* Free all blocks starting at 'first' and all then-empty
807 * extension blocks. Do not free the header block, though.
810 if (!(bh
= affs_bread(inode
->i_dev
,ekey
,blocksize
))) {
811 affs_error(inode
->i_sb
,"truncate","Cannot read block %d",ekey
);
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",
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
);
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
);
837 affs_free_block(inode
->i_sb
,key
);
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
);
853 affs_fix_checksum(blocksize
,bh
->b_data
,5);
854 mark_buffer_dirty(bh
,1);
856 first
-= AFFS_I2HSIZE(inode
);
859 if (freethis
) /* Don't bother fixing checksum */
860 affs_free_block(inode
->i_sb
,ekey
);
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
;
873 if ((inode
->i_sb
->u
.affs_sb
.s_flags
& SF_OFS
))
875 pr_debug("AFFS: Zeroing from offset %d in block %d\n",rem
,block
);
876 bh
= affs_getblock(inode
,block
);
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);
886 affs_error(inode
->i_sb
,"truncate","Cannot read block %d",block
);
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.
907 alloc_ext_cache(struct inode
*inode
)
911 unsigned long cache_page
;
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
)
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.
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;
940 free_page(cache_page
);
944 affs_error(inode
->i_sb
,"alloc_ext_cache","Cache allocation failed");