Import 2.2.8pre2
[davej-history.git] / fs / hfs / file.c
blob00bebd017d9ede4d229cf4941c15e0a1d1b83cd6
1 /*
2 * linux/fs/hfs/file.c
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.
19 #include "hfs.h"
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,
27 loff_t *);
28 static hfs_rwret_t hfs_file_write(struct file *, const char *, hfs_rwarg_t,
29 loff_t *);
30 static void hfs_file_truncate(struct inode *);
31 static int hfs_bmap(struct inode *, 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 */
43 NULL, /* open */
44 NULL, /* flush */
45 NULL, /* release */
46 file_fsync, /* fsync - default */
47 NULL, /* fasync - default */
48 NULL, /* check_media_change - none */
49 NULL, /* revalidate - none */
50 NULL /* lock - none */
53 struct inode_operations hfs_file_inode_operations = {
54 &hfs_file_operations, /* default file operations */
55 NULL, /* create */
56 NULL, /* lookup */
57 NULL, /* link */
58 NULL, /* unlink */
59 NULL, /* symlink */
60 NULL, /* mkdir */
61 NULL, /* rmdir */
62 NULL, /* mknod */
63 NULL, /* rename */
64 NULL, /* readlink */
65 NULL, /* follow_link */
66 generic_readpage, /* readpage */
67 NULL, /* writepage */
68 hfs_bmap, /* bmap */
69 hfs_file_truncate, /* truncate */
70 NULL, /* permission */
71 NULL, /* smap */
72 NULL, /* updatepage */
73 NULL /* revalidate */
76 /*================ Variable-like macros ================*/
78 /* maximum number of blocks to try to read in at once */
79 #define NBUF 32
81 /*================ File-local functions ================*/
84 * hfs_getblk()
86 * Given an hfs_fork and a block number return the buffer_head for
87 * that block from the fork. If 'create' is non-zero then allocate
88 * the necessary block(s) to the fork.
90 struct buffer_head *hfs_getblk(struct hfs_fork *fork, int block, int create)
92 int tmp;
93 kdev_t dev = fork->entry->mdb->sys_mdb->s_dev;
95 tmp = hfs_extent_map(fork, block, create);
97 if (create) {
98 /* If writing the block, then we have exclusive access
99 to the file until we return, so it can't have moved.
101 if (tmp) {
102 hfs_cat_mark_dirty(fork->entry);
103 return getblk(dev, tmp, HFS_SECTOR_SIZE);
105 return NULL;
106 } else {
107 /* If reading the block, then retry since the
108 location on disk could have changed while
109 we waited on the I/O in getblk to complete.
111 do {
112 struct buffer_head *bh =
113 getblk(dev, tmp, HFS_SECTOR_SIZE);
114 int tmp2 = hfs_extent_map(fork, block, 0);
116 if (tmp2 == tmp) {
117 return bh;
118 } else {
119 /* The block moved or no longer exists. */
120 brelse(bh);
121 tmp = tmp2;
123 } while (tmp != 0);
125 /* The block no longer exists. */
126 return NULL;
131 * hfs_bmap()
133 * This is the bmap() field in the inode_operations structure for
134 * "regular" (non-header) files. The purpose is to translate an inode
135 * and a block number within the corresponding file into a physical
136 * block number. This function just calls hfs_extent_map() to do the
137 * real work.
139 static int hfs_bmap(struct inode * inode, int block)
141 return hfs_extent_map(HFS_I(inode)->fork, block, 0);
145 * hfs_file_read()
147 * This is the read field in the inode_operations structure for
148 * "regular" (non-header) files. The purpose is to transfer up to
149 * 'count' bytes from the file corresponding to 'inode', beginning at
150 * 'filp->offset' bytes into the file. The data is transfered to
151 * user-space at the address 'buf'. Returns the number of bytes
152 * successfully transfered. This function checks the arguments, does
153 * some setup and then calls hfs_do_read() to do the actual transfer.
155 static hfs_rwret_t hfs_file_read(struct file * filp, char * buf,
156 hfs_rwarg_t count, loff_t *ppos)
158 struct inode *inode = filp->f_dentry->d_inode;
159 hfs_s32 read, left, pos, size;
161 if (!S_ISREG(inode->i_mode)) {
162 hfs_warn("hfs_file_read: mode = %07o\n",inode->i_mode);
163 return -EINVAL;
165 pos = *ppos;
166 if (pos >= HFS_FORK_MAX) {
167 return 0;
169 size = inode->i_size;
170 if (pos > size) {
171 left = 0;
172 } else {
173 left = size - pos;
175 if (left > count) {
176 left = count;
178 if (left <= 0) {
179 return 0;
181 if ((read = hfs_do_read(inode, HFS_I(inode)->fork, pos,
182 buf, left, filp->f_reada != 0)) > 0) {
183 *ppos += read;
184 filp->f_reada = 1;
187 return read;
191 * hfs_file_write()
193 * This is the write() entry in the file_operations structure for
194 * "regular" files. The purpose is to transfer up to 'count' bytes
195 * to the file corresponding to 'inode' beginning at offset
196 * 'file->f_pos' from user-space at the address 'buf'. The return
197 * value is the number of bytes actually transferred.
199 static hfs_rwret_t hfs_file_write(struct file * filp, const char * buf,
200 hfs_rwarg_t count, loff_t *ppos)
202 struct inode *inode = filp->f_dentry->d_inode;
203 struct hfs_fork *fork = HFS_I(inode)->fork;
204 hfs_s32 written, pos;
206 if (!S_ISREG(inode->i_mode)) {
207 hfs_warn("hfs_file_write: mode = %07o\n", inode->i_mode);
208 return -EINVAL;
211 pos = (filp->f_flags & O_APPEND) ? inode->i_size : *ppos;
213 if (pos >= HFS_FORK_MAX) {
214 return 0;
216 if (count > HFS_FORK_MAX) {
217 count = HFS_FORK_MAX;
219 if ((written = hfs_do_write(inode, fork, pos, buf, count)) > 0)
220 pos += written;
222 *ppos = pos;
223 if (*ppos > inode->i_size) {
224 inode->i_size = *ppos;
225 mark_inode_dirty(inode);
228 return written;
232 * hfs_file_truncate()
234 * This is the truncate() entry in the file_operations structure for
235 * "regular" files. The purpose is to change the length of the file
236 * corresponding to the given inode. Changes can either lengthen or
237 * shorten the file.
239 static void hfs_file_truncate(struct inode * inode)
241 struct hfs_fork *fork = HFS_I(inode)->fork;
243 fork->lsize = inode->i_size;
244 hfs_extent_adj(fork);
245 hfs_cat_mark_dirty(HFS_I(inode)->entry);
247 inode->i_size = fork->lsize;
248 inode->i_blocks = fork->psize;
249 mark_inode_dirty(inode);
253 * xlate_to_user()
255 * Like copy_to_user() while translating CR->NL.
257 static inline void xlate_to_user(char *buf, const char *data, int count)
259 char ch;
261 while (count--) {
262 ch = *(data++);
263 put_user((ch == '\r') ? '\n' : ch, buf++);
268 * xlate_from_user()
270 * Like copy_from_user() while translating NL->CR;
272 static inline int xlate_from_user(char *data, const char *buf, int count)
274 int i;
276 i = copy_from_user(data, buf, count);
277 count -= i;
278 while (count--) {
279 if (*data == '\n') {
280 *data = '\r';
282 ++data;
284 return i;
287 /*================ Global functions ================*/
290 * hfs_do_read()
292 * This function transfers actual data from disk to user-space memory,
293 * returning the number of bytes successfully transfered. 'fork' tells
294 * which file on the disk to read from. 'pos' gives the offset into
295 * the Linux file at which to begin the transfer. Note that this will
296 * differ from 'filp->offset' in the case of an AppleDouble header file
297 * due to the block of metadata at the beginning of the file, which has
298 * no corresponding place in the HFS file. 'count' tells how many
299 * bytes to transfer. 'buf' gives an address in user-space to transfer
300 * the data to.
302 * This is based on Linus's minix_file_read().
303 * It has been changed to take into account that HFS files have no holes.
305 hfs_s32 hfs_do_read(struct inode *inode, struct hfs_fork * fork, hfs_u32 pos,
306 char * buf, hfs_u32 count, int reada)
308 kdev_t dev = inode->i_dev;
309 hfs_s32 size, chars, offset, block, blocks, read = 0;
310 int bhrequest, uptodate;
311 int convert = HFS_I(inode)->convert;
312 struct buffer_head ** bhb, ** bhe;
313 struct buffer_head * bhreq[NBUF];
314 struct buffer_head * buflist[NBUF];
316 /* split 'pos' in to block and (byte) offset components */
317 block = pos >> HFS_SECTOR_SIZE_BITS;
318 offset = pos & (HFS_SECTOR_SIZE-1);
320 /* compute the logical size of the fork in blocks */
321 size = (fork->lsize + (HFS_SECTOR_SIZE-1)) >> HFS_SECTOR_SIZE_BITS;
323 /* compute the number of physical blocks to be transferred */
324 blocks = (count+offset+HFS_SECTOR_SIZE-1) >> HFS_SECTOR_SIZE_BITS;
326 bhb = bhe = buflist;
327 if (reada) {
328 if (blocks < read_ahead[MAJOR(dev)] / (HFS_SECTOR_SIZE>>9)) {
329 blocks = read_ahead[MAJOR(dev)] / (HFS_SECTOR_SIZE>>9);
331 if (block + blocks > size) {
332 blocks = size - block;
336 /* We do this in a two stage process. We first try and
337 request as many blocks as we can, then we wait for the
338 first one to complete, and then we try and wrap up as many
339 as are actually done.
341 This routine is optimized to make maximum use of the
342 various buffers and caches. */
344 do {
345 bhrequest = 0;
346 uptodate = 1;
347 while (blocks) {
348 --blocks;
349 *bhb = hfs_getblk(fork, block++, 0);
351 if (!(*bhb)) {
352 /* Since there are no holes in HFS files
353 we must have encountered an error.
354 So, stop adding blocks to the queue. */
355 blocks = 0;
356 break;
359 if (!buffer_uptodate(*bhb)) {
360 uptodate = 0;
361 bhreq[bhrequest++] = *bhb;
364 if (++bhb == &buflist[NBUF]) {
365 bhb = buflist;
368 /* If the block we have on hand is uptodate,
369 go ahead and complete processing. */
370 if (uptodate) {
371 break;
373 if (bhb == bhe) {
374 break;
378 /* If the only block in the queue is bad then quit */
379 if (!(*bhe)) {
380 break;
383 /* Now request them all */
384 if (bhrequest) {
385 ll_rw_block(READ, bhrequest, bhreq);
388 do { /* Finish off all I/O that has actually completed */
389 char *p;
391 wait_on_buffer(*bhe);
393 if (!buffer_uptodate(*bhe)) {
394 /* read error? */
395 brelse(*bhe);
396 if (++bhe == &buflist[NBUF]) {
397 bhe = buflist;
399 count = 0;
400 break;
403 if (count < HFS_SECTOR_SIZE - offset) {
404 chars = count;
405 } else {
406 chars = HFS_SECTOR_SIZE - offset;
408 p = (*bhe)->b_data + offset;
409 if (convert) {
410 xlate_to_user(buf, p, chars);
411 } else {
412 chars -= copy_to_user(buf, p, chars);
413 if (!chars) {
414 brelse(*bhe);
415 count = 0;
416 if (!read)
417 read = -EFAULT;
418 break;
421 brelse(*bhe);
422 count -= chars;
423 buf += chars;
424 read += chars;
425 offset = 0;
426 if (++bhe == &buflist[NBUF]) {
427 bhe = buflist;
429 } while (count && (bhe != bhb) && !buffer_locked(*bhe));
430 } while (count);
432 /* Release the read-ahead blocks */
433 while (bhe != bhb) {
434 brelse(*bhe);
435 if (++bhe == &buflist[NBUF]) {
436 bhe = buflist;
439 if (!read) {
440 return -EIO;
442 return read;
446 * hfs_do_write()
448 * This function transfers actual data from user-space memory to disk,
449 * returning the number of bytes successfully transfered. 'fork' tells
450 * which file on the disk to write to. 'pos' gives the offset into
451 * the Linux file at which to begin the transfer. Note that this will
452 * differ from 'filp->offset' in the case of an AppleDouble header file
453 * due to the block of metadata at the beginning of the file, which has
454 * no corresponding place in the HFS file. 'count' tells how many
455 * bytes to transfer. 'buf' gives an address in user-space to transfer
456 * the data from.
458 * This is just a minor edit of Linus's minix_file_write().
460 hfs_s32 hfs_do_write(struct inode *inode, struct hfs_fork * fork, hfs_u32 pos,
461 const char * buf, hfs_u32 count)
463 hfs_s32 written, c;
464 struct buffer_head * bh;
465 char * p;
466 int convert = HFS_I(inode)->convert;
468 written = 0;
469 while (written < count) {
470 bh = hfs_getblk(fork, pos/HFS_SECTOR_SIZE, 1);
471 if (!bh) {
472 if (!written) {
473 written = -ENOSPC;
475 break;
477 c = HFS_SECTOR_SIZE - (pos % HFS_SECTOR_SIZE);
478 if (c > count - written) {
479 c = count - written;
481 if (c != HFS_SECTOR_SIZE && !buffer_uptodate(bh)) {
482 ll_rw_block(READ, 1, &bh);
483 wait_on_buffer(bh);
484 if (!buffer_uptodate(bh)) {
485 brelse(bh);
486 if (!written) {
487 written = -EIO;
489 break;
492 p = (pos % HFS_SECTOR_SIZE) + bh->b_data;
493 c -= convert ? xlate_from_user(p, buf, c) :
494 copy_from_user(p, buf, c);
495 if (!c) {
496 brelse(bh);
497 if (!written)
498 written = -EFAULT;
499 break;
501 update_vm_cache(inode,pos,p,c);
502 pos += c;
503 written += c;
504 buf += c;
505 mark_buffer_uptodate(bh, 1);
506 mark_buffer_dirty(bh, 0);
507 brelse(bh);
509 if (written > 0) {
510 struct hfs_cat_entry *entry = fork->entry;
512 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
513 if (pos > fork->lsize) {
514 fork->lsize = pos;
516 entry->modify_date = hfs_u_to_mtime(CURRENT_TIME);
517 hfs_cat_mark_dirty(entry);
519 return written;
523 * hfs_file_fix_mode()
525 * Fixes up the permissions on a file after changing the write-inhibit bit.
527 void hfs_file_fix_mode(struct hfs_cat_entry *entry)
529 struct dentry **de = entry->sys_entry;
530 int i;
532 if (entry->u.file.flags & HFS_FIL_LOCK) {
533 for (i = 0; i < 4; ++i) {
534 if (de[i]) {
535 de[i]->d_inode->i_mode &= ~S_IWUGO;
538 } else {
539 for (i = 0; i < 4; ++i) {
540 if (de[i]) {
541 struct inode *inode = de[i]->d_inode;
542 inode->i_mode |= S_IWUGO;
543 inode->i_mode &=
544 ~HFS_SB(inode->i_sb)->s_umask;