~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/udf/file.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * file.c
  3  *
  4  * PURPOSE
  5  *  File handling routines for the OSTA-UDF(tm) filesystem.
  6  *
  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
 11  *
 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.
 17  *
 18  *  (C) 1998-1999 Dave Boynton
 19  *  (C) 1998-2000 Ben Fennema
 20  *  (C) 1999-2000 Stelias Computing Inc
 21  *
 22  * HISTORY
 23  *
 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
 31  */
 32 
 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>
 41 #include <linux/smp_lock.h>
 42 
 43 #include "udf_i.h"
 44 #include "udf_sb.h"
 45 
 46 static int udf_adinicb_readpage(struct file *file, struct page * page)
 47 {
 48         struct inode *inode = page->mapping->host;
 49 
 50         struct buffer_head *bh;
 51         int block;
 52         char *kaddr;
 53 
 54         if (!PageLocked(page))
 55                 PAGE_BUG(page);
 56 
 57         kaddr = kmap(page);
 58         memset(kaddr, 0, PAGE_CACHE_SIZE);
 59         block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0);
 60         bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize);
 61         memcpy(kaddr, bh->b_data + udf_ext0_offset(inode), inode->i_size);
 62         brelse(bh);
 63         flush_dcache_page(page);
 64         SetPageUptodate(page);
 65         kunmap(page);
 66         UnlockPage(page);
 67         return 0;
 68 }
 69 
 70 static int udf_adinicb_writepage(struct page *page)
 71 {
 72         struct inode *inode = page->mapping->host;
 73 
 74         struct buffer_head *bh;
 75         int block;
 76         char *kaddr;
 77 
 78         if (!PageLocked(page))
 79                 PAGE_BUG(page);
 80 
 81         kaddr = kmap(page);
 82         block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0);
 83         bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize);
 84         memcpy(bh->b_data + udf_ext0_offset(inode), kaddr, inode->i_size);
 85         mark_buffer_dirty(bh);
 86         brelse(bh);
 87         SetPageUptodate(page);
 88         kunmap(page);
 89         return 0;
 90 }
 91 
 92 static int udf_adinicb_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
 93 {
 94         kmap(page);
 95         return 0;
 96 }
 97 
 98 static int udf_adinicb_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
 99 {
100         struct inode *inode = page->mapping->host;
101 
102         struct buffer_head *bh;
103         int block;
104         char *kaddr = page_address(page);
105 
106         block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0);
107         bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize);
108         memcpy(bh->b_data + udf_file_entry_alloc_offset(inode) + offset,
109                 kaddr + offset, to-offset);
110         mark_buffer_dirty(bh);
111         brelse(bh);
112         SetPageUptodate(page);
113         kunmap(page);
114         /* only one page here */
115         if (to > inode->i_size)
116                 inode->i_size = to;
117         return 0;
118 }
119 
120 struct address_space_operations udf_adinicb_aops = {
121         readpage:                       udf_adinicb_readpage,
122         writepage:                      udf_adinicb_writepage,
123         sync_page:              block_sync_page,
124         prepare_write:          udf_adinicb_prepare_write,
125         commit_write:           udf_adinicb_commit_write,
126 };
127 
128 static ssize_t udf_file_write(struct file * file, const char * buf,
129         size_t count, loff_t *ppos)
130 {
131         ssize_t retval;
132         struct inode *inode = file->f_dentry->d_inode;
133         int err, pos;
134 
135         if (UDF_I_ALLOCTYPE(inode) == ICB_FLAG_AD_IN_ICB)
136         {
137                 if (file->f_flags & O_APPEND)
138                         pos = inode->i_size;
139                 else
140                         pos = *ppos;
141 
142                 if (inode->i_sb->s_blocksize < (udf_file_entry_alloc_offset(inode) +
143                         pos + count))
144                 {
145                         udf_expand_file_adinicb(inode, pos + count, &err);
146                         if (UDF_I_ALLOCTYPE(inode) == ICB_FLAG_AD_IN_ICB)
147                         {
148                                 udf_debug("udf_expand_adinicb: err=%d\n", err);
149                                 return err;
150                         }
151                 }
152                 else
153                 {
154                         if (pos + count > inode->i_size)
155                                 UDF_I_LENALLOC(inode) = pos + count;
156                         else
157                                 UDF_I_LENALLOC(inode) = inode->i_size;
158                 }
159         }
160 
161         retval = generic_file_write(file, buf, count, ppos);
162 
163         if (retval > 0)
164         {
165                 UDF_I_UCTIME(inode) = UDF_I_UMTIME(inode) = CURRENT_UTIME;
166                 mark_inode_dirty(inode);
167         }
168         return retval;
169 }
170 
171 /*
172  * udf_ioctl
173  *
174  * PURPOSE
175  *      Issue an ioctl.
176  *
177  * DESCRIPTION
178  *      Optional - sys_ioctl() will return -ENOTTY if this routine is not
179  *      available, and the ioctl cannot be handled without filesystem help.
180  *
181  *      sys_ioctl() handles these ioctls that apply only to regular files:
182  *              FIBMAP [requires udf_block_map()], FIGETBSZ, FIONREAD
183  *      These ioctls are also handled by sys_ioctl():
184  *              FIOCLEX, FIONCLEX, FIONBIO, FIOASYNC
185  *      All other ioctls are passed to the filesystem.
186  *
187  *      Refer to sys_ioctl() in fs/ioctl.c
188  *      sys_ioctl() -> .
189  *
190  * PRE-CONDITIONS
191  *      inode                   Pointer to inode that ioctl was issued on.
192  *      filp                    Pointer to file that ioctl was issued on.
193  *      cmd                     The ioctl command.
194  *      arg                     The ioctl argument [can be interpreted as a
195  *                              user-space pointer if desired].
196  *
197  * POST-CONDITIONS
198  *      <return>                Success (>=0) or an error code (<=0) that
199  *                              sys_ioctl() will return.
200  *
201  * HISTORY
202  *      July 1, 1997 - Andrew E. Mileski
203  *      Written, tested, and released.
204  */
205 int udf_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
206         unsigned long arg)
207 {
208         int result = -1;
209         struct buffer_head *bh = NULL;
210         Uint16 ident;
211         long_ad eaicb;
212         Uint8 *ea = NULL;
213 
214         if ( permission(inode, MAY_READ) != 0 )
215         {
216                 udf_debug("no permission to access inode %lu\n",
217                                                 inode->i_ino);
218                 return -EPERM;
219         }
220 
221         if ( !arg )
222         {
223                 udf_debug("invalid argument to udf_ioctl\n");
224                 return -EINVAL;
225         }
226 
227         /* first, do ioctls that don't need to udf_read */
228         switch (cmd)
229         {
230                 case UDF_GETVOLIDENT:
231                         if ( (result == verify_area(VERIFY_WRITE, (char *)arg, 32)) == 0)
232                                 result = copy_to_user((char *)arg, UDF_SB_VOLIDENT(inode->i_sb), 32);
233                         return result;
234 
235         }
236 
237         /* ok, we need to read the inode */
238         bh = udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 0, &ident);
239 
240         if (!bh || (ident != TID_FILE_ENTRY && ident != TID_EXTENDED_FILE_ENTRY))
241         {
242                 udf_debug("bread failed (ino=%ld) or ident (%d) != TID_(EXTENDED_)FILE_ENTRY",
243                         inode->i_ino, ident);
244                 return -EFAULT;
245         }
246 
247         if (UDF_I_EXTENDED_FE(inode) == 0)
248         {
249                 struct FileEntry *fe;
250 
251                 fe = (struct FileEntry *)bh->b_data;
252                 eaicb = lela_to_cpu(fe->extendedAttrICB);
253                 if (UDF_I_LENEATTR(inode))
254                         ea = fe->extendedAttr;
255         }
256         else
257         {
258                 struct ExtendedFileEntry *efe;
259 
260                 efe = (struct ExtendedFileEntry *)bh->b_data;
261                 eaicb = lela_to_cpu(efe->extendedAttrICB);
262                 if (UDF_I_LENEATTR(inode))
263                         ea = efe->extendedAttr;
264         }
265 
266         switch (cmd) 
267         {
268                 case UDF_GETEASIZE:
269                         if ( (result = verify_area(VERIFY_WRITE, (char *)arg, 4)) == 0) 
270                                 result = put_user(UDF_I_LENEATTR(inode), (int *)arg);
271                         break;
272 
273                 case UDF_GETEABLOCK:
274                         if ( (result = verify_area(VERIFY_WRITE, (char *)arg, UDF_I_LENEATTR(inode))) == 0) 
275                                 result = copy_to_user((char *)arg, ea, UDF_I_LENEATTR(inode));
276                         break;
277 
278                 default:
279                         udf_debug("ino=%ld, cmd=%d\n", inode->i_ino, cmd);
280                         break;
281         }
282 
283         udf_release_data(bh);
284         return result;
285 }
286 
287 /*
288  * udf_release_file
289  *
290  * PURPOSE
291  *  Called when all references to the file are closed
292  *
293  * DESCRIPTION
294  *  Discard prealloced blocks
295  *
296  * HISTORY
297  *
298  */
299 static int udf_release_file(struct inode * inode, struct file * filp)
300 {
301         if (filp->f_mode & FMODE_WRITE) {
302                 lock_kernel();
303                 udf_discard_prealloc(inode);
304                 unlock_kernel();
305         }
306         return 0;
307 }
308 
309 /*
310  * udf_open_file
311  *
312  * PURPOSE
313  *  Called when an inode is about to be open.
314  *
315  * DESCRIPTION
316  *  Use this to disallow opening RW large files on 32 bit systems.
317  *  On 64 bit systems we force on O_LARGEFILE in sys_open.
318  *
319  * HISTORY
320  *
321  */
322 static int udf_open_file(struct inode * inode, struct file * filp)
323 {
324         if ((inode->i_size & 0xFFFFFFFF00000000ULL) && !(filp->f_flags & O_LARGEFILE))
325                 return -EFBIG;
326         return 0;
327 }
328 
329 struct file_operations udf_file_operations = {
330         read:                           generic_file_read,
331         ioctl:                          udf_ioctl,
332         open:                           udf_open_file,
333         mmap:                           generic_file_mmap,
334         write:                          udf_file_write,
335         release:                        udf_release_file,
336         fsync:                          udf_sync_file,
337 };
338 
339 struct inode_operations udf_file_inode_operations = {
340         truncate:                       udf_truncate,
341 };
342 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.