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 affs_bmap
, /* get_block */
78 block_read_full_page
, /* readpage */
81 affs_truncate
, /* truncate */
82 NULL
, /* permission */
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 */
95 NULL
, /* no special open */
98 file_fsync
, /* brute force, but works */
100 NULL
, /* check_media_change */
101 NULL
, /* revalidate */
105 struct inode_operations affs_file_inode_operations_ofs
= {
106 &affs_file_operations_ofs
, /* default file operations */
117 NULL
, /* follow_link */
118 NULL
, /* get_block */
120 NULL
, /* writepage */
121 NULL
, /* flushpage */
122 affs_truncate
, /* truncate */
123 NULL
, /* permission */
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.
143 seqnum_to_index(int seqnum
)
145 /* All of the first 127 keys are stored */
150 /* Of the next 384 keys, every 2nd is kept */
151 if (seqnum
< (192 * 2))
152 return 128 + (seqnum
>> 1);
155 /* Every 4th of the next 512 */
156 if (seqnum
< (128 * 4))
157 return 128 + 192 + (seqnum
>> 2);
160 /* Every 16th of the next 1024 */
161 if (seqnum
< (64 * 16))
162 return 128 + 192 + 128 + (seqnum
>> 4);
165 /* Every 64th of the next 2048 */
166 if (seqnum
< (32 * 64))
167 return 128 + 192 + 128 + 64 + (seqnum
>> 6);
170 /* Every 256th of the next 16384 */
171 if (seqnum
< (64 * 256))
172 return 128 + 192 + 128 + 64 + 32 + (seqnum
>> 8);
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))
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.
193 index_to_seqnum(int index
)
199 return 128 + (index
<< 1);
202 return 128 + 192 * 2 + (index
<< 2);
205 return 128 + 192 * 2 + 128 * 4 + (index
<< 4);
208 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + (index
<< 6);
211 return 128 + 192 * 2 + 128 * 4 + 64 * 16 + 32 * 64 + (index
<< 8);
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
)
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) {
228 return kc
->kc_next_key
;
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
];
243 affs_bmap(struct inode
*inode
, int block
)
245 struct buffer_head
*bh
;
251 struct key_cache
*kc
;
252 struct key_cache
*tkc
;
257 pr_debug("AFFS: bmap(%lu,%d)\n",inode
->i_ino
,block
);
260 affs_error(inode
->i_sb
,"bmap","Block < 0");
263 if (!inode
->u
.affs_i
.i_ec
) {
264 if (alloc_ext_cache(inode
)) {
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
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
];
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
)
288 if (tkc
->kc_lru_time
.tv_sec
< tv
.tv_sec
||
289 tkc
->kc_lru_time
.tv_usec
< tv
.tv_usec
) {
291 tv
= tkc
->kc_lru_time
;
294 if (!kc
) /* Really shouldn't happen */
296 kc
->kc_lru_time
= xtime
;
298 kc
->kc_first
= block
;
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
307 ext
= block
/ AFFS_I2HSIZE(inode
);
308 key
= calc_key(inode
,&ext
);
309 block
-= ext
* AFFS_I2HSIZE(inode
);
312 bh
= affs_bread(inode
->i_dev
,key
,AFFS_I2BSIZE(inode
));
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
)) {
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 */
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;
335 block
-= AFFS_I2HSIZE(inode
);
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
;
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
));
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].
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
;
374 pr_debug("AFFS: getblock(%lu,%d)\n",inode
->i_ino
,block
);
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
));
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");
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) {
406 s32 k
= AFFS_BLOCK(bh
->b_data
, inode
,
408 pbh
= affs_bread(inode
->i_dev
,
410 AFFS_I2BSIZE(inode
));
412 pbh
= affs_getblock(inode
,inode
->u
.affs_i
.i_lastblock
);
414 affs_error(sb
,"getblock",
415 "Cannot get last block in file");
419 nkey
= affs_new_data(inode
);
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
);
428 AFFS_BLOCK(bh
->b_data
,inode
,j
) = cpu_to_be32(nkey
);
430 ebh
= affs_bread(inode
->i_dev
,nkey
,AFFS_I2BSIZE(inode
));
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;
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
),
443 mark_buffer_dirty(ebh
, 0);
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);
455 /* N.B. May need to release pbh after here */
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);
470 if (j
< AFFS_I2HSIZE(inode
)) {
471 /* N.B. What about pbh here? */
475 block
-= AFFS_I2HSIZE(inode
);
476 key
= be32_to_cpu(FILE_END(bh
->b_data
,inode
)->extension
);
478 key
= affs_new_header(inode
);
481 ebh
= affs_bread(inode
->i_dev
,key
,AFFS_I2BSIZE(inode
));
483 /* N.B. must free bh here */
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);
500 index
= seqnum_to_index(ext
);
501 if (index
> inode
->u
.affs_i
.i_ec
->max_ext
&&
503 inode
->u
.affs_i
.i_ec
->ec
[index
] = key
;
504 inode
->u
.affs_i
.i_ec
->max_ext
= index
;
509 /* Invalidate key cache */
510 for (j
= 0; j
< 4; j
++) {
511 kc
= &inode
->u
.affs_i
.i_ec
->kc
[j
];
514 key
= be32_to_cpu(AFFS_BLOCK(bh
->b_data
,inode
,block
));
519 bh
= affs_bread(inode
->i_dev
, key
, AFFS_I2BSIZE(inode
));
523 affs_free_block(sb
, key
);
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
;
535 ssize_t left
, offset
, size
, sector
;
537 struct buffer_head
*bh
;
540 pr_debug("AFFS: file_read_ofs(ino=%lu,pos=%lu,%d)\n",inode
->i_ino
,
541 (unsigned long)*ppos
,count
);
544 affs_error(inode
->i_sb
,"file_read_ofs","Inode = NULL");
547 blocksize
= AFFS_I2BSIZE(inode
) - 24;
548 if (!(S_ISREG(inode
->i_mode
))) {
549 pr_debug("AFFS: file_read: mode = %07o",inode
->i_mode
);
552 if (*ppos
>= inode
->i_size
|| count
<= 0)
557 left
= MIN (inode
->i_size
- *ppos
,count
- (buf
- start
));
560 sector
= affs_bmap(inode
,(u32
)*ppos
/ blocksize
);
563 offset
= (u32
)*ppos
% blocksize
;
564 bh
= affs_bread(inode
->i_dev
,sector
,AFFS_I2BSIZE(inode
));
567 data
= bh
->b_data
+ 24;
568 size
= MIN(blocksize
- offset
,left
);
570 copy_to_user(buf
,data
+ offset
,size
);
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
;
587 struct buffer_head
*bh
;
592 pr_debug("AFFS: file_write(ino=%lu,pos=%lu,count=%d)\n",inode
->i_ino
,
593 (unsigned long)*ppos
,count
);
596 affs_error(inode
->i_sb
,"file_write","Inode = NULL");
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)",
605 if (!inode
->u
.affs_i
.i_ec
&& alloc_ext_cache(inode
))
607 if (filp
->f_flags
& O_APPEND
)
612 blocksize
= AFFS_I2BSIZE(inode
);
614 while (written
< count
) {
615 bh
= affs_getblock(inode
,pos
/ blocksize
);
621 c
= blocksize
- (pos
% blocksize
);
622 if (c
> count
- written
)
624 if (c
!= blocksize
&& !buffer_uptodate(bh
)) {
625 ll_rw_block(READ
,1,&bh
);
627 if (!buffer_uptodate(bh
)) {
634 p
= (pos
% blocksize
) + bh
->b_data
;
635 c
-= copy_from_user(p
,buf
,c
);
642 update_vm_cache(inode
,pos
,p
,c
);
643 mark_buffer_uptodate(bh
,1);
644 mark_buffer_dirty(bh
,0);
650 if (pos
> inode
->i_size
)
652 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
654 mark_inode_dirty(inode
);
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
;
666 struct buffer_head
*bh
;
669 pr_debug("AFFS: file_write_ofs(ino=%lu,pos=%lu,count=%d)\n",inode
->i_ino
,
670 (unsigned long)*ppos
,count
);
675 affs_error(inode
->i_sb
,"file_write_ofs","Inode = NULL");
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)",
684 if (!inode
->u
.affs_i
.i_ec
&& alloc_ext_cache(inode
))
686 if (filp
->f_flags
& O_APPEND
)
692 blocksize
= AFFS_I2BSIZE(inode
) - 24;
694 while (written
< count
) {
695 bh
= affs_getblock(inode
,pos
/ blocksize
);
701 c
= blocksize
- (pos
% blocksize
);
702 if (c
> count
- written
)
704 if (c
!= blocksize
&& !buffer_uptodate(bh
)) {
705 ll_rw_block(READ
,1,&bh
);
707 if (!buffer_uptodate(bh
)) {
714 p
= (pos
% blocksize
) + bh
->b_data
+ 24;
715 c
-= copy_from_user(p
,buf
,c
);
722 update_vm_cache(inode
,pos
,p
,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);
733 if (pos
> inode
->i_size
)
736 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
737 mark_inode_dirty(inode
);
741 /* Free any preallocated blocks. */
744 affs_free_prealloc(struct inode
*inode
)
746 struct super_block
*sb
= inode
->i_sb
;
747 struct affs_zone
*zone
;
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
)
765 /* Truncate (or enlarge) a file to the requested size. */
768 affs_truncate(struct inode
*inode
)
770 struct buffer_head
*bh
= NULL
;
771 int first
; /* First block to be thrown away */
779 int blocksize
= AFFS_I2BSIZE(inode
);
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);
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);
808 /* Free all blocks starting at 'first' and all then-empty
809 * extension blocks. Do not free the header block, though.
812 if (!(bh
= affs_bread(inode
->i_dev
,ekey
,blocksize
))) {
813 affs_error(inode
->i_sb
,"truncate","Cannot read block %d",ekey
);
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",
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
);
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
);
839 affs_free_block(inode
->i_sb
,key
);
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
);
855 affs_fix_checksum(blocksize
,bh
->b_data
,5);
856 mark_buffer_dirty(bh
,1);
858 first
-= AFFS_I2HSIZE(inode
);
861 if (freethis
) /* Don't bother fixing checksum */
862 affs_free_block(inode
->i_sb
,ekey
);
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
;
875 if ((inode
->i_sb
->u
.affs_sb
.s_flags
& SF_OFS
))
877 pr_debug("AFFS: Zeroing from offset %d in block %d\n",rem
,block
);
878 bh
= affs_getblock(inode
,block
);
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);
888 affs_error(inode
->i_sb
,"truncate","Cannot read block %d",block
);
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.
909 alloc_ext_cache(struct inode
*inode
)
913 unsigned long cache_page
;
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
)
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.
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;
942 free_page(cache_page
);
946 affs_error(inode
->i_sb
,"alloc_ext_cache","Cache allocation failed");