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
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>
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
*);
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 */
63 udf_ioctl
, /* ioctl */
64 generic_file_mmap
, /* mmap */
65 #if BITS_PER_LONG == 64
68 udf_open_file
, /* open */
71 udf_release_file
, /* release */
72 udf_sync_file
, /* fsync */
74 NULL
, /* check_media_change */
75 NULL
, /* revalidate */
79 struct inode_operations udf_file_inode_operations
= {
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 */
97 udf_truncate
, /* truncate */
101 NULL
, /* permission */
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 */
112 udf_ioctl
, /* ioctl */
116 udf_release_file
, /* release */
117 udf_sync_file
, /* fsync */
119 NULL
, /* check_media_change */
120 NULL
, /* revalidate */
124 struct inode_operations udf_file_inode_operations_adinicb
= {
125 &udf_file_operations_adinicb
,
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 */
142 udf_truncate
, /* truncate */
146 NULL
, /* permission */
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
;
162 offset
+= inode
->i_size
;
167 offset
+= file
->f_pos
;
171 #if BITS_PER_LONG < 64
172 if (((unsigned long long) offset
>> 32) != 0)
177 if (offset
!= file
->f_pos
)
179 file
->f_pos
= offset
;
181 file
->f_version
= ++event
;
186 static inline void remove_suid(struct inode
* inode
)
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
)
206 struct inode
*inode
= file
->f_dentry
->d_inode
;
209 if (UDF_I_ALLOCTYPE(inode
) == ICB_FLAG_AD_IN_ICB
)
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
)
220 retval
= generic_file_write(file
, buf
, count
, ppos
, block_write_partial_page
);
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
);
235 * Read from an open file.
238 * Optional - sys_read() will return -EINVAL if this routine is not
241 * Refer to sys_read() in fs/read_write.c
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()
254 * Block devices can use block_read() instead. Refer to fs/block_dev.c
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.
263 * <return> Bytes read (>=0) or an error code (<0) that
264 * sys_read() will return.
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
;
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
)))
296 if (!copy_to_user(buf
, bh
->b_data
+ pos
, left
))
301 udf_release_data(bh
);
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
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].
333 * <return> Success (>=0) or an error code (<=0) that
334 * sys_ioctl() will return.
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
,
344 struct buffer_head
*bh
= NULL
;
349 if ( permission(inode
, MAY_READ
) != 0 )
351 udf_debug("no permission to access inode %lu\n",
358 udf_debug("invalid argument to udf_ioctl\n");
362 /* first, do ioctls that don't need to udf_read */
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);
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
);
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
;
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
;
404 if ( (result
= verify_area(VERIFY_WRITE
, (char *)arg
, 4)) == 0)
405 result
= put_user(UDF_I_LENEATTR(inode
), (int *)arg
);
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
));
414 udf_debug("ino=%ld, cmd=%d\n", inode
->i_ino
, cmd
);
418 udf_release_data(bh
);
426 * Called when all references to the file are closed
429 * Discard prealloced blocks
434 static int udf_release_file(struct inode
* inode
, struct file
* filp
)
436 if (filp
->f_mode
& FMODE_WRITE
)
437 udf_discard_prealloc(inode
);
441 #if BITS_PER_LONG < 64
446 * Called when an inode is about to be open.
449 * Use this to disallow opening RW large files on 32 bit systems.
454 static int udf_open_file(struct inode
* inode
, struct file
* filp
)
456 if (inode
->i_size
== (Uint32
)-1 && (filp
->f_mode
& FMODE_WRITE
))