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@hpesjro.fc.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-2001 Ben Fennema
20 * (C) 1999-2000 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
28 * ICBTAG_FLAG_AD_IN_ICB.
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
35 #include <linux/udf_fs.h>
36 #include <asm/uaccess.h>
37 #include <linux/kernel.h>
38 #include <linux/string.h> /* memset */
39 #include <linux/errno.h>
40 #include <linux/locks.h>
41 #include <linux/smp_lock.h>
46 static int udf_adinicb_readpage(struct file *file, struct page * page)
48 struct inode *inode = page->mapping->host;
50 struct buffer_head *bh;
55 if (!PageLocked(page))
59 memset(kaddr, 0, PAGE_CACHE_SIZE);
60 block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0);
61 bh = sb_bread(inode->i_sb, block);
68 memcpy(kaddr, bh->b_data + udf_ext0_offset(inode), inode->i_size);
70 flush_dcache_page(page);
71 SetPageUptodate(page);
78 static int udf_adinicb_writepage(struct page *page)
80 struct inode *inode = page->mapping->host;
82 struct buffer_head *bh;
87 if (!PageLocked(page))
91 block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0);
92 bh = sb_bread(inode->i_sb, block);
99 memcpy(bh->b_data + udf_ext0_offset(inode), kaddr, inode->i_size);
100 mark_buffer_dirty(bh);
102 SetPageUptodate(page);
109 static int udf_adinicb_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
115 static int udf_adinicb_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
117 struct inode *inode = page->mapping->host;
119 struct buffer_head *bh;
121 char *kaddr = page_address(page);
124 block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0);
125 bh = sb_bread(inode->i_sb, block);
132 memcpy(bh->b_data + udf_file_entry_alloc_offset(inode) + offset,
133 kaddr + offset, to - offset);
134 mark_buffer_dirty(bh);
136 SetPageUptodate(page);
139 /* only one page here */
140 if (to > inode->i_size)
145 struct address_space_operations udf_adinicb_aops = {
146 readpage: udf_adinicb_readpage,
147 writepage: udf_adinicb_writepage,
148 sync_page: block_sync_page,
149 prepare_write: udf_adinicb_prepare_write,
150 commit_write: udf_adinicb_commit_write,
153 static ssize_t udf_file_write(struct file * file, const char * buf,
154 size_t count, loff_t *ppos)
157 struct inode *inode = file->f_dentry->d_inode;
161 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
163 if (file->f_flags & O_APPEND)
168 if (pos < 0 || pos + count < pos)
171 if (inode->i_sb->s_blocksize - udf_file_entry_alloc_offset(inode) <
174 udf_expand_file_adinicb(inode, pos + count, &err);
175 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
177 udf_debug("udf_expand_adinicb: err=%d\n", err);
183 if (pos + count > inode->i_size)
184 UDF_I_LENALLOC(inode) = pos + count;
186 UDF_I_LENALLOC(inode) = inode->i_size;
190 retval = generic_file_write(file, buf, count, ppos);
194 UDF_I_UCTIME(inode) = UDF_I_UMTIME(inode) = CURRENT_UTIME;
195 mark_inode_dirty(inode);
207 * Optional - sys_ioctl() will return -ENOTTY if this routine is not
208 * available, and the ioctl cannot be handled without filesystem help.
210 * sys_ioctl() handles these ioctls that apply only to regular files:
211 * FIBMAP [requires udf_block_map()], FIGETBSZ, FIONREAD
212 * These ioctls are also handled by sys_ioctl():
213 * FIOCLEX, FIONCLEX, FIONBIO, FIOASYNC
214 * All other ioctls are passed to the filesystem.
216 * Refer to sys_ioctl() in fs/ioctl.c
220 * inode Pointer to inode that ioctl was issued on.
221 * filp Pointer to file that ioctl was issued on.
222 * cmd The ioctl command.
223 * arg The ioctl argument [can be interpreted as a
224 * user-space pointer if desired].
227 * <return> Success (>=0) or an error code (<=0) that
228 * sys_ioctl() will return.
231 * July 1, 1997 - Andrew E. Mileski
232 * Written, tested, and released.
234 int udf_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
237 int result = -EINVAL;
238 struct buffer_head *bh = NULL;
242 if ( permission(inode, MAY_READ) != 0 )
244 udf_debug("no permission to access inode %lu\n",
251 udf_debug("invalid argument to udf_ioctl\n");
255 /* first, do ioctls that don't need to udf_read */
258 case UDF_GETVOLIDENT:
259 return copy_to_user((char *)arg,
260 UDF_SB_VOLIDENT(inode->i_sb), 32) ? -EFAULT : 0;
261 case UDF_RELOCATE_BLOCKS:
265 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
266 if (get_user(old, (long *)arg)) return -EFAULT;
267 if ((result = udf_relocate_blocks(inode->i_sb,
269 result = put_user(new, (long *)arg);
275 /* ok, we need to read the inode */
276 bh = udf_tread(inode->i_sb,
277 udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0));
281 udf_debug("bread failed (inode=%ld)\n", inode->i_ino);
285 if (UDF_I_EXTENDED_FE(inode) == 0)
287 struct fileEntry *fe;
289 fe = (struct fileEntry *)bh->b_data;
290 eaicb = lela_to_cpu(fe->extendedAttrICB);
291 if (UDF_I_LENEATTR(inode))
292 ea = fe->extendedAttr;
296 struct extendedFileEntry *efe;
298 efe = (struct extendedFileEntry *)bh->b_data;
299 eaicb = lela_to_cpu(efe->extendedAttrICB);
300 if (UDF_I_LENEATTR(inode))
301 ea = efe->extendedAttr;
307 result = put_user(UDF_I_LENEATTR(inode), (int *)arg);
311 result = copy_to_user((char *)arg, ea,
312 UDF_I_LENEATTR(inode)) ? -EFAULT : 0;
316 udf_release_data(bh);
324 * Called when all references to the file are closed
327 * Discard prealloced blocks
332 static int udf_release_file(struct inode * inode, struct file * filp)
334 if (filp->f_mode & FMODE_WRITE)
337 udf_discard_prealloc(inode);
347 * Called when an inode is about to be open.
350 * Use this to disallow opening RW large files on 32 bit systems.
351 * On 64 bit systems we force on O_LARGEFILE in sys_open.
356 static int udf_open_file(struct inode * inode, struct file * filp)
358 if ((inode->i_size & 0xFFFFFFFF80000000ULL) && !(filp->f_flags & O_LARGEFILE))
363 struct file_operations udf_file_operations = {
364 read: generic_file_read,
367 mmap: generic_file_mmap,
368 write: udf_file_write,
369 release: udf_release_file,
370 fsync: udf_fsync_file,
373 struct inode_operations udf_file_inode_operations = {
374 truncate: udf_truncate,