Import 2.3.48
[davej-history.git] / fs / udf / file.c
blobcea86257ba26ff84b0220f6ee407e803927e653e
1 /*
2 * file.c
4 * PURPOSE
5 * File handling routines for the OSTA-UDF(tm) filesystem.
7 * CONTACTS
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
12 * COPYRIGHT
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
22 * HISTORY
24 * 10/02/98 dgb Attempt to integrate into udf.o
25 * 10/07/98 Switched to using generic_readpage, etc., like isofs
26 * And it works!
27 * 12/06/98 blf Added udf_file_read. uses generic_file_read for all cases but
28 * ICB_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
33 #include "udfdecl.h"
34 #include <linux/config.h>
35 #include <linux/fs.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>
43 #include "udf_i.h"
44 #include "udf_sb.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;
53 switch (origin)
55 case 2:
57 offset += inode->i_size;
58 break;
60 case 1:
62 offset += file->f_pos;
63 break;
66 if (offset != file->f_pos)
68 file->f_pos = offset;
69 file->f_reada = 0;
70 file->f_version = ++event;
72 return offset;
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))
83 PAGE_BUG(page);
85 kaddr = kmap(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);
89 wait_on_buffer(bh);
90 memcpy((char *)kaddr, bh->b_data + udf_ext0_offset(inode),
91 inode->i_size);
92 brelse(bh);
93 SetPageUptodate(page);
94 kunmap(page);
95 UnlockPage(page);
96 return 0;
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))
107 BUG();
109 kaddr = kmap(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);
114 wait_on_buffer(bh);
116 memcpy(bh->b_data + udf_ext0_offset(inode), (char *)kaddr,
117 inode->i_size);
118 ll_rw_block (WRITE, 1, &bh);
119 wait_on_buffer(bh);
120 brelse(bh);
121 SetPageUptodate(page);
122 kunmap(page);
123 return 0;
126 static int udf_adinicb_prepare_write(struct page *page, unsigned offset, unsigned to)
128 kmap(page);
129 return 0;
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);
140 wait_on_buffer(bh);
142 memcpy(bh->b_data + udf_file_entry_alloc_offset(inode) + offset,
143 kaddr + offset, to-offset);
144 mark_buffer_dirty(bh, 0);
145 brelse(bh);
146 kunmap(page);
147 SetPageUptodate(page);
148 /* only one page here */
149 if (to > inode->i_size)
150 inode->i_size = to;
151 return 0;
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)
164 ssize_t retval;
165 struct inode *inode = file->f_dentry->d_inode;
166 int err, pos;
168 if (UDF_I_ALLOCTYPE(inode) == ICB_FLAG_AD_IN_ICB) {
169 if (file->f_flags & O_APPEND)
170 pos = inode->i_size;
171 else
172 pos = *ppos;
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);
179 return err;
181 } else {
182 if (pos + count > inode->i_size)
183 UDF_I_LENALLOC(inode) = pos + count;
184 else
185 UDF_I_LENALLOC(inode) = inode->i_size;
189 retval = generic_file_write(file, buf, count, ppos);
190 if (retval > 0) {
191 UDF_I_UCTIME(inode) = UDF_I_UMTIME(inode) = CURRENT_UTIME;
192 mark_inode_dirty(inode);
194 return retval;
198 * udf_ioctl
200 * PURPOSE
201 * Issue an ioctl.
203 * DESCRIPTION
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
214 * sys_ioctl() -> .
216 * PRE-CONDITIONS
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].
223 * POST-CONDITIONS
224 * <return> Success (>=0) or an error code (<=0) that
225 * sys_ioctl() will return.
227 * HISTORY
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,
232 unsigned long arg)
234 int result = -1;
235 struct buffer_head *bh = NULL;
236 Uint16 ident;
237 long_ad eaicb;
238 Uint8 *ea = NULL;
240 if ( permission(inode, MAY_READ) != 0 )
242 udf_debug("no permission to access inode %lu\n",
243 inode->i_ino);
244 return -EPERM;
247 if ( !arg )
249 udf_debug("invalid argument to udf_ioctl\n");
250 return -EINVAL;
253 /* first, do ioctls that don't need to udf_read */
254 switch (cmd)
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);
259 return result;
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);
270 return -EFAULT;
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;
282 else
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;
292 switch (cmd)
294 case UDF_GETEASIZE:
295 if ( (result = verify_area(VERIFY_WRITE, (char *)arg, 4)) == 0)
296 result = put_user(UDF_I_LENEATTR(inode), (int *)arg);
297 break;
299 case UDF_GETEABLOCK:
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));
302 break;
304 default:
305 udf_debug("ino=%ld, cmd=%d\n", inode->i_ino, cmd);
306 break;
309 udf_release_data(bh);
310 return result;
314 * udf_release_file
316 * PURPOSE
317 * Called when all references to the file are closed
319 * DESCRIPTION
320 * Discard prealloced blocks
322 * HISTORY
325 static int udf_release_file(struct inode * inode, struct file * filp)
327 if (filp->f_mode & FMODE_WRITE)
328 udf_discard_prealloc(inode);
329 return 0;
333 * udf_open_file
335 * PURPOSE
336 * Called when an inode is about to be open.
338 * DESCRIPTION
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.
342 * HISTORY
345 static int udf_open_file(struct inode * inode, struct file * filp)
347 if ((inode->i_size & 0xFFFFFFFF00000000UL) && !(filp->f_flags & O_LARGEFILE))
348 return -EFBIG;
349 return 0;
352 struct file_operations udf_file_operations = {
353 llseek: udf_file_llseek,
354 read: generic_file_read,
355 write: udf_file_write,
356 ioctl: udf_ioctl,
357 mmap: generic_file_mmap,
358 open: udf_open_file,
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,
366 #endif