file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * file.c
  3. *
  4. * PURPOSE
  5. * File handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1998-1999 Dave Boynton
  14. * (C) 1998-2004 Ben Fennema
  15. * (C) 1999-2000 Stelias Computing Inc
  16. *
  17. * HISTORY
  18. *
  19. * 10/02/98 dgb Attempt to integrate into udf.o
  20. * 10/07/98 Switched to using generic_readpage, etc., like isofs
  21. * And it works!
  22. * 12/06/98 blf Added udf_file_read. uses generic_file_read for all cases but
  23. * ICBTAG_FLAG_AD_IN_ICB.
  24. * 04/06/99 64 bit file handling on 32 bit systems taken from ext2 file.c
  25. * 05/12/99 Preliminary file write support
  26. */
  27. #include "udfdecl.h"
  28. #include <linux/fs.h>
  29. #include <linux/udf_fs.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/kernel.h>
  32. #include <linux/string.h> /* memset */
  33. #include <linux/errno.h>
  34. #include <linux/smp_lock.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/buffer_head.h>
  37. #include "udf_i.h"
  38. #include "udf_sb.h"
  39. static int udf_adinicb_readpage(struct file *file, struct page * page)
  40. {
  41. struct inode *inode = page->mapping->host;
  42. char *kaddr;
  43. BUG_ON(!PageLocked(page));
  44. kaddr = kmap(page);
  45. memset(kaddr, 0, PAGE_CACHE_SIZE);
  46. memcpy(kaddr, UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), inode->i_size);
  47. flush_dcache_page(page);
  48. SetPageUptodate(page);
  49. kunmap(page);
  50. unlock_page(page);
  51. return 0;
  52. }
  53. static int udf_adinicb_writepage(struct page *page, struct writeback_control *wbc)
  54. {
  55. struct inode *inode = page->mapping->host;
  56. char *kaddr;
  57. BUG_ON(!PageLocked(page));
  58. kaddr = kmap(page);
  59. memcpy(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), kaddr, inode->i_size);
  60. mark_inode_dirty(inode);
  61. SetPageUptodate(page);
  62. kunmap(page);
  63. unlock_page(page);
  64. return 0;
  65. }
  66. static int udf_adinicb_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  67. {
  68. kmap(page);
  69. return 0;
  70. }
  71. static int udf_adinicb_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  72. {
  73. struct inode *inode = page->mapping->host;
  74. char *kaddr = page_address(page);
  75. memcpy(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode) + offset,
  76. kaddr + offset, to - offset);
  77. mark_inode_dirty(inode);
  78. SetPageUptodate(page);
  79. kunmap(page);
  80. /* only one page here */
  81. if (to > inode->i_size)
  82. inode->i_size = to;
  83. return 0;
  84. }
  85. struct address_space_operations udf_adinicb_aops = {
  86. .readpage = udf_adinicb_readpage,
  87. .writepage = udf_adinicb_writepage,
  88. .sync_page = block_sync_page,
  89. .prepare_write = udf_adinicb_prepare_write,
  90. .commit_write = udf_adinicb_commit_write,
  91. };
  92. static ssize_t udf_file_write(struct file * file, const char __user * buf,
  93. size_t count, loff_t *ppos)
  94. {
  95. ssize_t retval;
  96. struct inode *inode = file->f_dentry->d_inode;
  97. int err, pos;
  98. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
  99. {
  100. if (file->f_flags & O_APPEND)
  101. pos = inode->i_size;
  102. else
  103. pos = *ppos;
  104. if (inode->i_sb->s_blocksize < (udf_file_entry_alloc_offset(inode) +
  105. pos + count))
  106. {
  107. udf_expand_file_adinicb(inode, pos + count, &err);
  108. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
  109. {
  110. udf_debug("udf_expand_adinicb: err=%d\n", err);
  111. return err;
  112. }
  113. }
  114. else
  115. {
  116. if (pos + count > inode->i_size)
  117. UDF_I_LENALLOC(inode) = pos + count;
  118. else
  119. UDF_I_LENALLOC(inode) = inode->i_size;
  120. }
  121. }
  122. retval = generic_file_write(file, buf, count, ppos);
  123. if (retval > 0)
  124. mark_inode_dirty(inode);
  125. return retval;
  126. }
  127. /*
  128. * udf_ioctl
  129. *
  130. * PURPOSE
  131. * Issue an ioctl.
  132. *
  133. * DESCRIPTION
  134. * Optional - sys_ioctl() will return -ENOTTY if this routine is not
  135. * available, and the ioctl cannot be handled without filesystem help.
  136. *
  137. * sys_ioctl() handles these ioctls that apply only to regular files:
  138. * FIBMAP [requires udf_block_map()], FIGETBSZ, FIONREAD
  139. * These ioctls are also handled by sys_ioctl():
  140. * FIOCLEX, FIONCLEX, FIONBIO, FIOASYNC
  141. * All other ioctls are passed to the filesystem.
  142. *
  143. * Refer to sys_ioctl() in fs/ioctl.c
  144. * sys_ioctl() -> .
  145. *
  146. * PRE-CONDITIONS
  147. * inode Pointer to inode that ioctl was issued on.
  148. * filp Pointer to file that ioctl was issued on.
  149. * cmd The ioctl command.
  150. * arg The ioctl argument [can be interpreted as a
  151. * user-space pointer if desired].
  152. *
  153. * POST-CONDITIONS
  154. * <return> Success (>=0) or an error code (<=0) that
  155. * sys_ioctl() will return.
  156. *
  157. * HISTORY
  158. * July 1, 1997 - Andrew E. Mileski
  159. * Written, tested, and released.
  160. */
  161. int udf_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  162. unsigned long arg)
  163. {
  164. int result = -EINVAL;
  165. if ( file_permission(filp, MAY_READ) != 0 )
  166. {
  167. udf_debug("no permission to access inode %lu\n",
  168. inode->i_ino);
  169. return -EPERM;
  170. }
  171. if ( !arg )
  172. {
  173. udf_debug("invalid argument to udf_ioctl\n");
  174. return -EINVAL;
  175. }
  176. switch (cmd)
  177. {
  178. case UDF_GETVOLIDENT:
  179. return copy_to_user((char __user *)arg,
  180. UDF_SB_VOLIDENT(inode->i_sb), 32) ? -EFAULT : 0;
  181. case UDF_RELOCATE_BLOCKS:
  182. {
  183. long old, new;
  184. if (!capable(CAP_SYS_ADMIN)) return -EACCES;
  185. if (get_user(old, (long __user *)arg)) return -EFAULT;
  186. if ((result = udf_relocate_blocks(inode->i_sb,
  187. old, &new)) == 0)
  188. result = put_user(new, (long __user *)arg);
  189. return result;
  190. }
  191. case UDF_GETEASIZE:
  192. result = put_user(UDF_I_LENEATTR(inode), (int __user *)arg);
  193. break;
  194. case UDF_GETEABLOCK:
  195. result = copy_to_user((char __user *)arg, UDF_I_DATA(inode),
  196. UDF_I_LENEATTR(inode)) ? -EFAULT : 0;
  197. break;
  198. }
  199. return result;
  200. }
  201. /*
  202. * udf_release_file
  203. *
  204. * PURPOSE
  205. * Called when all references to the file are closed
  206. *
  207. * DESCRIPTION
  208. * Discard prealloced blocks
  209. *
  210. * HISTORY
  211. *
  212. */
  213. static int udf_release_file(struct inode * inode, struct file * filp)
  214. {
  215. if (filp->f_mode & FMODE_WRITE)
  216. {
  217. lock_kernel();
  218. udf_discard_prealloc(inode);
  219. unlock_kernel();
  220. }
  221. return 0;
  222. }
  223. struct file_operations udf_file_operations = {
  224. .read = generic_file_read,
  225. .ioctl = udf_ioctl,
  226. .open = generic_file_open,
  227. .mmap = generic_file_mmap,
  228. .write = udf_file_write,
  229. .release = udf_release_file,
  230. .fsync = udf_fsync_file,
  231. .sendfile = generic_file_sendfile,
  232. };
  233. struct inode_operations udf_file_inode_operations = {
  234. .truncate = udf_truncate,
  235. };