4 * Copyright (C) 1995, 1996 Paul H. Hargrove
5 * This file may be distributed under the terms of the GNU Public License.
7 * This file contains the file-related functions which are independent of
8 * which scheme is being used to represent forks.
10 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
12 * "XXX" in a comment is a note to myself to consider changing something.
14 * In function preconditions the term "valid" applied to a pointer to
15 * a structure means that the pointer is non-NULL and the structure it
16 * points to has all fields initialized to consistent values.
20 #include <linux/hfs_fs_sb.h>
21 #include <linux/hfs_fs_i.h>
22 #include <linux/hfs_fs.h>
24 /*================ Forward declarations ================*/
26 static hfs_rwret_t
hfs_file_read(struct file
*, char *, hfs_rwarg_t
,
28 static hfs_rwret_t
hfs_file_write(struct file
*, const char *, hfs_rwarg_t
,
30 static void hfs_file_truncate(struct inode
*);
31 static int hfs_get_block(struct inode
*, long, struct buffer_head
*, int);
33 /*================ Global variables ================*/
35 static struct file_operations hfs_file_operations
= {
36 NULL
, /* lseek - default */
37 hfs_file_read
, /* read */
38 hfs_file_write
, /* write */
39 NULL
, /* readdir - bad */
40 NULL
, /* select - default */
41 NULL
, /* ioctl - default */
42 generic_file_mmap
, /* mmap */
46 file_fsync
, /* fsync - default */
47 NULL
, /* fasync - default */
48 NULL
/* lock - none */
51 struct inode_operations hfs_file_inode_operations
= {
52 &hfs_file_operations
, /* default file operations */
63 NULL
, /* follow_link */
64 hfs_get_block
, /* get_block */
65 block_read_full_page
, /* readpage */
67 hfs_file_truncate
, /* truncate */
68 NULL
, /* permission */
72 /*================ Variable-like macros ================*/
74 /* maximum number of blocks to try to read in at once */
77 /*================ File-local functions ================*/
82 * Given an hfs_fork and a block number return the buffer_head for
83 * that block from the fork. If 'create' is non-zero then allocate
84 * the necessary block(s) to the fork.
86 struct buffer_head
*hfs_getblk(struct hfs_fork
*fork
, int block
, int create
)
89 kdev_t dev
= fork
->entry
->mdb
->sys_mdb
->s_dev
;
91 tmp
= hfs_extent_map(fork
, block
, create
);
94 /* If writing the block, then we have exclusive access
95 to the file until we return, so it can't have moved.
98 hfs_cat_mark_dirty(fork
->entry
);
99 return getblk(dev
, tmp
, HFS_SECTOR_SIZE
);
103 /* If reading the block, then retry since the
104 location on disk could have changed while
105 we waited on the I/O in getblk to complete.
108 struct buffer_head
*bh
=
109 getblk(dev
, tmp
, HFS_SECTOR_SIZE
);
110 int tmp2
= hfs_extent_map(fork
, block
, 0);
115 /* The block moved or no longer exists. */
121 /* The block no longer exists. */
129 * This is the hfs_get_block() field in the inode_operations structure for
130 * "regular" (non-header) files. The purpose is to translate an inode
131 * and a block number within the corresponding file into a physical
132 * block number. This function just calls hfs_extent_map() to do the
133 * real work and then stuffs the appropriate info into the buffer_head.
135 int hfs_get_block(struct inode
*inode
, long iblock
, struct buffer_head
*bh_result
, int create
)
139 phys
= hfs_extent_map(HFS_I(inode
)->fork
, iblock
, create
);
141 bh_result
->b_dev
= inode
->i_dev
;
142 bh_result
->b_blocknr
= phys
;
143 bh_result
->b_state
|= (1UL << BH_Mapped
);
145 bh_result
->b_state
|= (1UL << BH_New
);
152 /* we tried to add stuff, but we couldn't. send back an out-of-space
161 * This is the read field in the inode_operations structure for
162 * "regular" (non-header) files. The purpose is to transfer up to
163 * 'count' bytes from the file corresponding to 'inode', beginning at
164 * 'filp->offset' bytes into the file. The data is transfered to
165 * user-space at the address 'buf'. Returns the number of bytes
166 * successfully transfered. This function checks the arguments, does
167 * some setup and then calls hfs_do_read() to do the actual transfer. */
168 static hfs_rwret_t
hfs_file_read(struct file
* filp
, char * buf
,
169 hfs_rwarg_t count
, loff_t
*ppos
)
171 struct inode
*inode
= filp
->f_dentry
->d_inode
;
172 hfs_s32 read
, left
, pos
, size
;
174 if (!S_ISREG(inode
->i_mode
)) {
175 hfs_warn("hfs_file_read: mode = %07o\n",inode
->i_mode
);
179 if (pos
>= HFS_FORK_MAX
) {
182 size
= inode
->i_size
;
194 if ((read
= hfs_do_read(inode
, HFS_I(inode
)->fork
, pos
,
195 buf
, left
, filp
->f_reada
!= 0)) > 0) {
206 * This is the write() entry in the file_operations structure for
207 * "regular" files. The purpose is to transfer up to 'count' bytes
208 * to the file corresponding to 'inode' beginning at offset
209 * 'file->f_pos' from user-space at the address 'buf'. The return
210 * value is the number of bytes actually transferred.
212 static hfs_rwret_t
hfs_file_write(struct file
* filp
, const char * buf
,
213 hfs_rwarg_t count
, loff_t
*ppos
)
215 struct inode
*inode
= filp
->f_dentry
->d_inode
;
216 struct hfs_fork
*fork
= HFS_I(inode
)->fork
;
217 hfs_s32 written
, pos
;
219 if (!S_ISREG(inode
->i_mode
)) {
220 hfs_warn("hfs_file_write: mode = %07o\n", inode
->i_mode
);
224 pos
= (filp
->f_flags
& O_APPEND
) ? inode
->i_size
: *ppos
;
226 if (pos
>= HFS_FORK_MAX
) {
229 if (count
> HFS_FORK_MAX
) {
230 count
= HFS_FORK_MAX
;
232 if ((written
= hfs_do_write(inode
, fork
, pos
, buf
, count
)) > 0)
236 if (*ppos
> inode
->i_size
) {
237 inode
->i_size
= *ppos
;
238 mark_inode_dirty(inode
);
245 * hfs_file_truncate()
247 * This is the truncate() entry in the file_operations structure for
248 * "regular" files. The purpose is to change the length of the file
249 * corresponding to the given inode. Changes can either lengthen or
252 static void hfs_file_truncate(struct inode
* inode
)
254 struct hfs_fork
*fork
= HFS_I(inode
)->fork
;
256 fork
->lsize
= inode
->i_size
;
257 hfs_extent_adj(fork
);
258 hfs_cat_mark_dirty(HFS_I(inode
)->entry
);
260 inode
->i_size
= fork
->lsize
;
261 inode
->i_blocks
= fork
->psize
;
262 mark_inode_dirty(inode
);
268 * Like copy_to_user() while translating CR->NL.
270 static inline void xlate_to_user(char *buf
, const char *data
, int count
)
276 put_user((ch
== '\r') ? '\n' : ch
, buf
++);
283 * Like copy_from_user() while translating NL->CR;
285 static inline int xlate_from_user(char *data
, const char *buf
, int count
)
289 i
= copy_from_user(data
, buf
, count
);
300 /*================ Global functions ================*/
305 * This function transfers actual data from disk to user-space memory,
306 * returning the number of bytes successfully transfered. 'fork' tells
307 * which file on the disk to read from. 'pos' gives the offset into
308 * the Linux file at which to begin the transfer. Note that this will
309 * differ from 'filp->offset' in the case of an AppleDouble header file
310 * due to the block of metadata at the beginning of the file, which has
311 * no corresponding place in the HFS file. 'count' tells how many
312 * bytes to transfer. 'buf' gives an address in user-space to transfer
315 * This is based on Linus's minix_file_read().
316 * It has been changed to take into account that HFS files have no holes.
318 hfs_s32
hfs_do_read(struct inode
*inode
, struct hfs_fork
* fork
, hfs_u32 pos
,
319 char * buf
, hfs_u32 count
, int reada
)
321 kdev_t dev
= inode
->i_dev
;
322 hfs_s32 size
, chars
, offset
, block
, blocks
, read
= 0;
323 int bhrequest
, uptodate
;
324 int convert
= HFS_I(inode
)->convert
;
325 struct buffer_head
** bhb
, ** bhe
;
326 struct buffer_head
* bhreq
[NBUF
];
327 struct buffer_head
* buflist
[NBUF
];
329 /* split 'pos' in to block and (byte) offset components */
330 block
= pos
>> HFS_SECTOR_SIZE_BITS
;
331 offset
= pos
& (HFS_SECTOR_SIZE
-1);
333 /* compute the logical size of the fork in blocks */
334 size
= (fork
->lsize
+ (HFS_SECTOR_SIZE
-1)) >> HFS_SECTOR_SIZE_BITS
;
336 /* compute the number of physical blocks to be transferred */
337 blocks
= (count
+offset
+HFS_SECTOR_SIZE
-1) >> HFS_SECTOR_SIZE_BITS
;
341 if (blocks
< read_ahead
[MAJOR(dev
)] / (HFS_SECTOR_SIZE
>>9)) {
342 blocks
= read_ahead
[MAJOR(dev
)] / (HFS_SECTOR_SIZE
>>9);
344 if (block
+ blocks
> size
) {
345 blocks
= size
- block
;
349 /* We do this in a two stage process. We first try and
350 request as many blocks as we can, then we wait for the
351 first one to complete, and then we try and wrap up as many
352 as are actually done.
354 This routine is optimized to make maximum use of the
355 various buffers and caches. */
362 *bhb
= hfs_getblk(fork
, block
++, 0);
365 /* Since there are no holes in HFS files
366 we must have encountered an error.
367 So, stop adding blocks to the queue. */
372 if (!buffer_uptodate(*bhb
)) {
374 bhreq
[bhrequest
++] = *bhb
;
377 if (++bhb
== &buflist
[NBUF
]) {
381 /* If the block we have on hand is uptodate,
382 go ahead and complete processing. */
391 /* If the only block in the queue is bad then quit */
396 /* Now request them all */
398 ll_rw_block(READ
, bhrequest
, bhreq
);
401 do { /* Finish off all I/O that has actually completed */
404 wait_on_buffer(*bhe
);
406 if (!buffer_uptodate(*bhe
)) {
409 if (++bhe
== &buflist
[NBUF
]) {
416 if (count
< HFS_SECTOR_SIZE
- offset
) {
419 chars
= HFS_SECTOR_SIZE
- offset
;
421 p
= (*bhe
)->b_data
+ offset
;
423 xlate_to_user(buf
, p
, chars
);
425 chars
-= copy_to_user(buf
, p
, chars
);
439 if (++bhe
== &buflist
[NBUF
]) {
442 } while (count
&& (bhe
!= bhb
) && !buffer_locked(*bhe
));
445 /* Release the read-ahead blocks */
448 if (++bhe
== &buflist
[NBUF
]) {
461 * This function transfers actual data from user-space memory to disk,
462 * returning the number of bytes successfully transfered. 'fork' tells
463 * which file on the disk to write to. 'pos' gives the offset into
464 * the Linux file at which to begin the transfer. Note that this will
465 * differ from 'filp->offset' in the case of an AppleDouble header file
466 * due to the block of metadata at the beginning of the file, which has
467 * no corresponding place in the HFS file. 'count' tells how many
468 * bytes to transfer. 'buf' gives an address in user-space to transfer
471 * This is just a minor edit of Linus's minix_file_write().
473 hfs_s32
hfs_do_write(struct inode
*inode
, struct hfs_fork
* fork
, hfs_u32 pos
,
474 const char * buf
, hfs_u32 count
)
477 struct buffer_head
* bh
;
479 int convert
= HFS_I(inode
)->convert
;
482 while (written
< count
) {
483 bh
= hfs_getblk(fork
, pos
/HFS_SECTOR_SIZE
, 1);
490 c
= HFS_SECTOR_SIZE
- (pos
% HFS_SECTOR_SIZE
);
491 if (c
> count
- written
) {
494 if (c
!= HFS_SECTOR_SIZE
&& !buffer_uptodate(bh
)) {
495 ll_rw_block(READ
, 1, &bh
);
497 if (!buffer_uptodate(bh
)) {
505 p
= (pos
% HFS_SECTOR_SIZE
) + bh
->b_data
;
506 c
-= convert
? xlate_from_user(p
, buf
, c
) :
507 copy_from_user(p
, buf
, c
);
517 mark_buffer_uptodate(bh
, 1);
518 mark_buffer_dirty(bh
, 0);
522 struct hfs_cat_entry
*entry
= fork
->entry
;
524 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
525 if (pos
> fork
->lsize
) {
528 entry
->modify_date
= hfs_u_to_mtime(CURRENT_TIME
);
529 hfs_cat_mark_dirty(entry
);
535 * hfs_file_fix_mode()
537 * Fixes up the permissions on a file after changing the write-inhibit bit.
539 void hfs_file_fix_mode(struct hfs_cat_entry
*entry
)
541 struct dentry
**de
= entry
->sys_entry
;
544 if (entry
->u
.file
.flags
& HFS_FIL_LOCK
) {
545 for (i
= 0; i
< 4; ++i
) {
547 de
[i
]->d_inode
->i_mode
&= ~S_IWUGO
;
551 for (i
= 0; i
< 4; ++i
) {
553 struct inode
*inode
= de
[i
]->d_inode
;
554 inode
->i_mode
|= S_IWUGO
;
556 ~HFS_SB(inode
->i_sb
)->s_umask
;