Import 2.3.26pre2
[davej-history.git] / fs / udf / file.c
blob0a9642b08595a5727975692a698c638f5e157092
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/fs.h>
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>
42 #include "udf_i.h"
43 #include "udf_sb.h"
45 #define NBUF 32
47 typedef void * poll_table;
49 static long long udf_file_llseek(struct file *, long long, int);
50 static ssize_t udf_file_read_adinicb (struct file *, char *, size_t, loff_t *);
51 static ssize_t udf_file_write (struct file *, const char *, size_t, loff_t *);
52 #if BITS_PER_LONG < 64
53 static int udf_open_file(struct inode *, struct file *);
54 #endif
55 static int udf_release_file(struct inode *, struct file *);
57 static struct file_operations udf_file_operations = {
58 udf_file_llseek, /* llseek */
59 generic_file_read, /* read */
60 udf_file_write, /* write */
61 NULL, /* readdir */
62 NULL, /* poll */
63 udf_ioctl, /* ioctl */
64 generic_file_mmap, /* mmap */
65 #if BITS_PER_LONG == 64
66 NULL, /* open */
67 #else
68 udf_open_file, /* open */
69 #endif
70 NULL, /* flush */
71 udf_release_file, /* release */
72 udf_sync_file, /* fsync */
73 NULL, /* fasync */
74 NULL, /* check_media_change */
75 NULL, /* revalidate */
76 NULL /* lock */
79 struct inode_operations udf_file_inode_operations = {
80 &udf_file_operations,
81 NULL, /* create */
82 NULL, /* lookup */
83 NULL, /* link */
84 NULL, /* unlink */
85 NULL, /* symlink */
86 NULL, /* mkdir */
87 NULL, /* rmdir */
88 NULL, /* mknod */
89 NULL, /* rename */
90 NULL, /* readlink */
91 NULL, /* follow_link */
92 udf_get_block, /* get_block */
93 block_read_full_page, /* readpage */
94 block_write_full_page, /* writepage */
95 block_flushpage, /* flushpage */
96 #ifdef CONFIG_UDF_RW
97 udf_truncate, /* truncate */
98 #else
99 NULL, /* truncate */
100 #endif
101 NULL, /* permission */
102 NULL, /* smap */
103 NULL /* revalidate */
106 static struct file_operations udf_file_operations_adinicb = {
107 udf_file_llseek, /* llseek */
108 udf_file_read_adinicb,/* read */
109 udf_file_write, /* write */
110 NULL, /* readdir */
111 NULL, /* poll */
112 udf_ioctl, /* ioctl */
113 NULL, /* mmap */
114 NULL, /* open */
115 NULL, /* flush */
116 udf_release_file, /* release */
117 udf_sync_file, /* fsync */
118 NULL, /* fasync */
119 NULL, /* check_media_change */
120 NULL, /* revalidate */
121 NULL /* lock */
124 struct inode_operations udf_file_inode_operations_adinicb = {
125 &udf_file_operations_adinicb,
126 NULL, /* create */
127 NULL, /* lookup */
128 NULL, /* link */
129 NULL, /* unlink */
130 NULL, /* symlink */
131 NULL, /* mkdir */
132 NULL, /* rmdir */
133 NULL, /* mknod */
134 NULL, /* rename */
135 NULL, /* readlink */
136 NULL, /* follow_link */
137 udf_get_block, /* get_block */
138 block_read_full_page, /* readpage */
139 block_write_full_page, /* writepage */
140 block_flushpage, /* flushpage */
141 #ifdef CONFIG_UDF_RW
142 udf_truncate, /* truncate */
143 #else
144 NULL, /* truncate */
145 #endif
146 NULL, /* permission */
147 NULL, /* smap */
148 NULL /* revalidate */
152 * Make sure the offset never goes beyond the 32-bit mark..
154 static long long udf_file_llseek(struct file * file, long long offset, int origin)
156 struct inode * inode = file->f_dentry->d_inode;
158 switch (origin)
160 case 2:
162 offset += inode->i_size;
163 break;
165 case 1:
167 offset += file->f_pos;
168 break;
171 #if BITS_PER_LONG < 64
172 if (((unsigned long long) offset >> 32) != 0)
174 return -EINVAL;
176 #endif
177 if (offset != file->f_pos)
179 file->f_pos = offset;
180 file->f_reada = 0;
181 file->f_version = ++event;
183 return offset;
186 static inline void remove_suid(struct inode * inode)
188 unsigned int mode;
190 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
191 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
193 /* was any of the uid bits set? */
194 mode &= inode->i_mode;
195 if (mode && !capable(CAP_FSETID))
197 inode->i_mode &= ~mode;
198 mark_inode_dirty(inode);
202 static ssize_t udf_file_write(struct file * file, const char * buf,
203 size_t count, loff_t *ppos)
205 ssize_t retval;
206 struct inode *inode = file->f_dentry->d_inode;
207 remove_suid(inode);
209 if (UDF_I_ALLOCTYPE(inode) == ICB_FLAG_AD_IN_ICB)
211 int i, err;
212 struct buffer_head *bh;
214 if ((bh = udf_expand_adinicb(inode, &i, 0, &err)))
215 udf_release_data(bh);
216 else if (UDF_I_ALLOCTYPE(inode) == ICB_FLAG_AD_IN_ICB)
217 return err;
220 retval = generic_file_write(file, buf, count, ppos, block_write_partial_page);
222 if (retval > 0)
224 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
225 UDF_I_UCTIME(inode) = UDF_I_UMTIME(inode) = CURRENT_UTIME;
227 mark_inode_dirty(inode);
228 return retval;
232 * udf_file_read
234 * PURPOSE
235 * Read from an open file.
237 * DESCRIPTION
238 * Optional - sys_read() will return -EINVAL if this routine is not
239 * available.
241 * Refer to sys_read() in fs/read_write.c
242 * sys_read() -> .
244 * Note that you can use generic_file_read() instead, which requires that
245 * udf_readpage() be available, but you can use generic_readpage(), which
246 * requires that udf_block_map() be available. Reading will then be done by
247 * memory-mapping the file a page at a time. This is not suitable for
248 * devices that don't handle read-ahead [example: CD-R/RW that may have
249 * blank sectors that shouldn't be read].
251 * Refer to generic_file_read() in mm/filemap.c and to generic_readpage()
252 * in fs/buffer.c
254 * Block devices can use block_read() instead. Refer to fs/block_dev.c
256 * PRE-CONDITIONS
257 * inode Pointer to inode to read from (never NULL).
258 * filp Pointer to file to read from (never NULL).
259 * buf Point to read buffer (validated).
260 * bufsize Size of read buffer.
262 * POST-CONDITIONS
263 * <return> Bytes read (>=0) or an error code (<0) that
264 * sys_read() will return.
266 * HISTORY
267 * July 1, 1997 - Andrew E. Mileski
268 * Written, tested, and released.
270 static ssize_t udf_file_read_adinicb(struct file * filp, char * buf,
271 size_t bufsize, loff_t * loff)
273 struct inode *inode = filp->f_dentry->d_inode;
274 Uint32 size, left, pos, block;
275 struct buffer_head *bh = NULL;
277 size = inode->i_size;
278 if (*loff > size)
279 left = 0;
280 else
281 left = size - *loff;
282 if (left > bufsize)
283 left = bufsize;
285 if (left <= 0)
286 return 0;
288 pos = *loff + UDF_I_EXT0OFFS(inode);
289 block = udf_block_map(inode, 0);
290 if (!(bh = udf_tread(inode->i_sb,
291 udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0),
292 inode->i_sb->s_blocksize)))
294 return 0;
296 if (!copy_to_user(buf, bh->b_data + pos, left))
297 *loff += left;
298 else
299 left = -EFAULT;
301 udf_release_data(bh);
303 return left;
307 * udf_ioctl
309 * PURPOSE
310 * Issue an ioctl.
312 * DESCRIPTION
313 * Optional - sys_ioctl() will return -ENOTTY if this routine is not
314 * available, and the ioctl cannot be handled without filesystem help.
316 * sys_ioctl() handles these ioctls that apply only to regular files:
317 * FIBMAP [requires udf_block_map()], FIGETBSZ, FIONREAD
318 * These ioctls are also handled by sys_ioctl():
319 * FIOCLEX, FIONCLEX, FIONBIO, FIOASYNC
320 * All other ioctls are passed to the filesystem.
322 * Refer to sys_ioctl() in fs/ioctl.c
323 * sys_ioctl() -> .
325 * PRE-CONDITIONS
326 * inode Pointer to inode that ioctl was issued on.
327 * filp Pointer to file that ioctl was issued on.
328 * cmd The ioctl command.
329 * arg The ioctl argument [can be interpreted as a
330 * user-space pointer if desired].
332 * POST-CONDITIONS
333 * <return> Success (>=0) or an error code (<=0) that
334 * sys_ioctl() will return.
336 * HISTORY
337 * July 1, 1997 - Andrew E. Mileski
338 * Written, tested, and released.
340 int udf_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
341 unsigned long arg)
343 int result = -1;
344 struct buffer_head *bh = NULL;
345 Uint16 ident;
346 long_ad eaicb;
347 Uint8 *ea = NULL;
349 if ( permission(inode, MAY_READ) != 0 )
351 udf_debug("no permission to access inode %lu\n",
352 inode->i_ino);
353 return -EPERM;
356 if ( !arg )
358 udf_debug("invalid argument to udf_ioctl\n");
359 return -EINVAL;
362 /* first, do ioctls that don't need to udf_read */
363 switch (cmd)
365 case UDF_GETVOLIDENT:
366 if ( (result == verify_area(VERIFY_WRITE, (char *)arg, 32)) == 0)
367 result = copy_to_user((char *)arg, UDF_SB_VOLIDENT(inode->i_sb), 32);
368 return result;
372 /* ok, we need to read the inode */
373 bh = udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 0, &ident);
375 if (!bh || (ident != TID_FILE_ENTRY && ident != TID_EXTENDED_FILE_ENTRY))
377 udf_debug("bread failed (ino=%ld) or ident (%d) != TID_(EXTENDED_)FILE_ENTRY",
378 inode->i_ino, ident);
379 return -EFAULT;
382 if (UDF_I_EXTENDED_FE(inode) == 0)
384 struct FileEntry *fe;
386 fe = (struct FileEntry *)bh->b_data;
387 eaicb = fe->extendedAttrICB;
388 if (UDF_I_LENEATTR(inode))
389 ea = fe->extendedAttr;
391 else
393 struct ExtendedFileEntry *efe;
395 efe = (struct ExtendedFileEntry *)bh->b_data;
396 eaicb = efe->extendedAttrICB;
397 if (UDF_I_LENEATTR(inode))
398 ea = efe->extendedAttr;
401 switch (cmd)
403 case UDF_GETEASIZE:
404 if ( (result = verify_area(VERIFY_WRITE, (char *)arg, 4)) == 0)
405 result = put_user(UDF_I_LENEATTR(inode), (int *)arg);
406 break;
408 case UDF_GETEABLOCK:
409 if ( (result = verify_area(VERIFY_WRITE, (char *)arg, UDF_I_LENEATTR(inode))) == 0)
410 result = copy_to_user((char *)arg, ea, UDF_I_LENEATTR(inode));
411 break;
413 default:
414 udf_debug("ino=%ld, cmd=%d\n", inode->i_ino, cmd);
415 break;
418 udf_release_data(bh);
419 return result;
423 * udf_release_file
425 * PURPOSE
426 * Called when all references to the file are closed
428 * DESCRIPTION
429 * Discard prealloced blocks
431 * HISTORY
434 static int udf_release_file(struct inode * inode, struct file * filp)
436 if (filp->f_mode & FMODE_WRITE)
437 udf_discard_prealloc(inode);
438 return 0;
441 #if BITS_PER_LONG < 64
443 * udf_open_file
445 * PURPOSE
446 * Called when an inode is about to be open.
448 * DESCRIPTION
449 * Use this to disallow opening RW large files on 32 bit systems.
451 * HISTORY
454 static int udf_open_file(struct inode * inode, struct file * filp)
456 if (inode->i_size == (Uint32)-1 && (filp->f_mode & FMODE_WRITE))
457 return -EFBIG;
458 return 0;
460 #endif