5 * File handling routines for the OSTA-UDF(tm) filesystem.
8 * E-mail regarding any portion of the Linux UDF file system should be
9 * directed to the development team mailing list (run by majordomo):
10 * linux_udf@hootie.lvld.hp.com
13 * This file is distributed under the terms of the GNU General Public
14 * License (GPL). Copies of the GPL can be obtained from:
15 * ftp://prep.ai.mit.edu/pub/gnu/GPL
16 * Each contributing author retains all rights to their own work.
18 * (C) 1998-1999 Dave Boynton
19 * (C) 1998-1999 Ben Fennema
20 * (C) 1999 Stelias Computing Inc
24 * 10/02/98 dgb Attempt to integrate into udf.o
25 * 10/07/98 Switched to using generic_readpage, etc., like isofs
27 * 12/06/98 blf Added udf_file_read. uses generic_file_read for all cases but
29 * 04/06/99 64 bit file handling on 32 bit systems taken from ext2 file.c
30 * 05/12/99 Preliminary file write support
34 #include <linux/config.h>
36 #include <linux/udf_fs.h>
37 #include <asm/uaccess.h>
38 #include <linux/kernel.h>
39 #include <linux/string.h> /* memset */
40 #include <linux/errno.h>
41 #include <linux/locks.h>
47 * Make sure the offset never goes beyond the 32-bit mark..
49 static loff_t
udf_file_llseek(struct file
* file
, loff_t offset
, int origin
)
51 struct inode
* inode
= file
->f_dentry
->d_inode
;
57 offset
+= inode
->i_size
;
62 offset
+= file
->f_pos
;
66 if (offset
!= file
->f_pos
)
70 file
->f_version
= ++event
;
75 static int udf_adinicb_readpage(struct dentry
*dentry
, struct page
* page
)
77 struct inode
*inode
= dentry
->d_inode
;
79 struct buffer_head
*bh
;
80 unsigned long kaddr
= 0;
82 if (!PageLocked(page
))
86 memset((char *)kaddr
, 0, PAGE_CACHE_SIZE
);
87 bh
= getblk (inode
->i_dev
, inode
->i_ino
, inode
->i_sb
->s_blocksize
);
88 ll_rw_block (READ
, 1, &bh
);
90 memcpy((char *)kaddr
, bh
->b_data
+ udf_ext0_offset(inode
),
93 SetPageUptodate(page
);
99 static int udf_adinicb_writepage(struct dentry
*dentry
, struct page
*page
)
101 struct inode
*inode
= dentry
->d_inode
;
103 struct buffer_head
*bh
;
104 unsigned long kaddr
= 0;
106 if (!PageLocked(page
))
110 bh
= getblk (inode
->i_dev
, inode
->i_ino
, inode
->i_sb
->s_blocksize
);
111 if (!buffer_uptodate(bh
))
113 ll_rw_block (READ
, 1, &bh
);
116 memcpy(bh
->b_data
+ udf_ext0_offset(inode
), (char *)kaddr
,
118 ll_rw_block (WRITE
, 1, &bh
);
121 SetPageUptodate(page
);
126 static int udf_adinicb_prepare_write(struct page
*page
, unsigned offset
, unsigned to
)
132 static int udf_adinicb_commit_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
134 struct inode
*inode
= file
->f_dentry
->d_inode
;
135 struct buffer_head
*bh
;
136 char *kaddr
= (char*)page_address(page
);
137 bh
= bread (inode
->i_dev
, inode
->i_ino
, inode
->i_sb
->s_blocksize
);
138 if (!buffer_uptodate(bh
)) {
139 ll_rw_block (READ
, 1, &bh
);
142 memcpy(bh
->b_data
+ udf_file_entry_alloc_offset(inode
) + offset
,
143 kaddr
+ offset
, to
-offset
);
144 mark_buffer_dirty(bh
, 0);
147 SetPageUptodate(page
);
148 /* only one page here */
149 if (to
> inode
->i_size
)
154 struct address_space_operations udf_adinicb_aops
= {
155 readpage
: udf_adinicb_readpage
,
156 writepage
: udf_adinicb_writepage
,
157 prepare_write
: udf_adinicb_prepare_write
,
158 commit_write
: udf_adinicb_commit_write
161 static ssize_t
udf_file_write(struct file
* file
, const char * buf
,
162 size_t count
, loff_t
*ppos
)
165 struct inode
*inode
= file
->f_dentry
->d_inode
;
168 if (UDF_I_ALLOCTYPE(inode
) == ICB_FLAG_AD_IN_ICB
) {
169 if (file
->f_flags
& O_APPEND
)
174 if (inode
->i_sb
->s_blocksize
<
175 (udf_file_entry_alloc_offset(inode
) + pos
+ count
)) {
176 udf_expand_file_adinicb(file
, pos
+ count
, &err
);
177 if (UDF_I_ALLOCTYPE(inode
) == ICB_FLAG_AD_IN_ICB
) {
178 udf_debug("udf_expand_adinicb: err=%d\n", err
);
182 if (pos
+ count
> inode
->i_size
)
183 UDF_I_LENALLOC(inode
) = pos
+ count
;
185 UDF_I_LENALLOC(inode
) = inode
->i_size
;
189 retval
= generic_file_write(file
, buf
, count
, ppos
);
191 UDF_I_UCTIME(inode
) = UDF_I_UMTIME(inode
) = CURRENT_UTIME
;
192 mark_inode_dirty(inode
);
204 * Optional - sys_ioctl() will return -ENOTTY if this routine is not
205 * available, and the ioctl cannot be handled without filesystem help.
207 * sys_ioctl() handles these ioctls that apply only to regular files:
208 * FIBMAP [requires udf_block_map()], FIGETBSZ, FIONREAD
209 * These ioctls are also handled by sys_ioctl():
210 * FIOCLEX, FIONCLEX, FIONBIO, FIOASYNC
211 * All other ioctls are passed to the filesystem.
213 * Refer to sys_ioctl() in fs/ioctl.c
217 * inode Pointer to inode that ioctl was issued on.
218 * filp Pointer to file that ioctl was issued on.
219 * cmd The ioctl command.
220 * arg The ioctl argument [can be interpreted as a
221 * user-space pointer if desired].
224 * <return> Success (>=0) or an error code (<=0) that
225 * sys_ioctl() will return.
228 * July 1, 1997 - Andrew E. Mileski
229 * Written, tested, and released.
231 int udf_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
235 struct buffer_head
*bh
= NULL
;
240 if ( permission(inode
, MAY_READ
) != 0 )
242 udf_debug("no permission to access inode %lu\n",
249 udf_debug("invalid argument to udf_ioctl\n");
253 /* first, do ioctls that don't need to udf_read */
256 case UDF_GETVOLIDENT
:
257 if ( (result
== verify_area(VERIFY_WRITE
, (char *)arg
, 32)) == 0)
258 result
= copy_to_user((char *)arg
, UDF_SB_VOLIDENT(inode
->i_sb
), 32);
263 /* ok, we need to read the inode */
264 bh
= udf_read_ptagged(inode
->i_sb
, UDF_I_LOCATION(inode
), 0, &ident
);
266 if (!bh
|| (ident
!= TID_FILE_ENTRY
&& ident
!= TID_EXTENDED_FILE_ENTRY
))
268 udf_debug("bread failed (ino=%ld) or ident (%d) != TID_(EXTENDED_)FILE_ENTRY",
269 inode
->i_ino
, ident
);
273 if (UDF_I_EXTENDED_FE(inode
) == 0)
275 struct FileEntry
*fe
;
277 fe
= (struct FileEntry
*)bh
->b_data
;
278 eaicb
= fe
->extendedAttrICB
;
279 if (UDF_I_LENEATTR(inode
))
280 ea
= fe
->extendedAttr
;
284 struct ExtendedFileEntry
*efe
;
286 efe
= (struct ExtendedFileEntry
*)bh
->b_data
;
287 eaicb
= efe
->extendedAttrICB
;
288 if (UDF_I_LENEATTR(inode
))
289 ea
= efe
->extendedAttr
;
295 if ( (result
= verify_area(VERIFY_WRITE
, (char *)arg
, 4)) == 0)
296 result
= put_user(UDF_I_LENEATTR(inode
), (int *)arg
);
300 if ( (result
= verify_area(VERIFY_WRITE
, (char *)arg
, UDF_I_LENEATTR(inode
))) == 0)
301 result
= copy_to_user((char *)arg
, ea
, UDF_I_LENEATTR(inode
));
305 udf_debug("ino=%ld, cmd=%d\n", inode
->i_ino
, cmd
);
309 udf_release_data(bh
);
317 * Called when all references to the file are closed
320 * Discard prealloced blocks
325 static int udf_release_file(struct inode
* inode
, struct file
* filp
)
327 if (filp
->f_mode
& FMODE_WRITE
)
328 udf_discard_prealloc(inode
);
336 * Called when an inode is about to be open.
339 * Use this to disallow opening RW large files on 32 bit systems.
340 * On 64 bit systems we force on O_LARGEFILE in sys_open.
345 static int udf_open_file(struct inode
* inode
, struct file
* filp
)
347 if ((inode
->i_size
& 0xFFFFFFFF00000000UL
) && !(filp
->f_flags
& O_LARGEFILE
))
352 struct file_operations udf_file_operations
= {
353 llseek
: udf_file_llseek
,
354 read
: generic_file_read
,
355 write
: udf_file_write
,
357 mmap
: generic_file_mmap
,
359 release
: udf_release_file
,
360 fsync
: udf_sync_file
,
363 struct inode_operations udf_file_inode_operations
= {
364 #if CONFIG_UDF_RW == 1
365 truncate
: udf_truncate
,