file.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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@hpesjro.fc.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-2004 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. * ICBTAG_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. #include "udfdecl.h"
  33. #include <linux/fs.h>
  34. #include <linux/udf_fs.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/kernel.h>
  37. #include <linux/string.h> /* memset */
  38. #include <linux/errno.h>
  39. #include <linux/smp_lock.h>
  40. #include <linux/pagemap.h>
  41. #include <linux/buffer_head.h>
  42. #include "udf_i.h"
  43. #include "udf_sb.h"
  44. static int udf_adinicb_readpage(struct file *file, struct page * page)
  45. {
  46. struct inode *inode = page->mapping->host;
  47. char *kaddr;
  48. BUG_ON(!PageLocked(page));
  49. kaddr = kmap(page);
  50. memset(kaddr, 0, PAGE_CACHE_SIZE);
  51. memcpy(kaddr, UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), inode->i_size);
  52. flush_dcache_page(page);
  53. SetPageUptodate(page);
  54. kunmap(page);
  55. unlock_page(page);
  56. return 0;
  57. }
  58. static int udf_adinicb_writepage(struct page *page, struct writeback_control *wbc)
  59. {
  60. struct inode *inode = page->mapping->host;
  61. char *kaddr;
  62. BUG_ON(!PageLocked(page));
  63. kaddr = kmap(page);
  64. memcpy(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), kaddr, inode->i_size);
  65. mark_inode_dirty(inode);
  66. SetPageUptodate(page);
  67. kunmap(page);
  68. unlock_page(page);
  69. return 0;
  70. }
  71. static int udf_adinicb_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  72. {
  73. kmap(page);
  74. return 0;
  75. }
  76. static int udf_adinicb_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  77. {
  78. struct inode *inode = page->mapping->host;
  79. char *kaddr = page_address(page);
  80. memcpy(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode) + offset,
  81. kaddr + offset, to - offset);
  82. mark_inode_dirty(inode);
  83. SetPageUptodate(page);
  84. kunmap(page);
  85. /* only one page here */
  86. if (to > inode->i_size)
  87. inode->i_size = to;
  88. return 0;
  89. }
  90. struct address_space_operations udf_adinicb_aops = {
  91. .readpage = udf_adinicb_readpage,
  92. .writepage = udf_adinicb_writepage,
  93. .sync_page = block_sync_page,
  94. .prepare_write = udf_adinicb_prepare_write,
  95. .commit_write = udf_adinicb_commit_write,
  96. };
  97. static ssize_t udf_file_write(struct file * file, const char __user * buf,
  98. size_t count, loff_t *ppos)
  99. {
  100. ssize_t retval;
  101. struct inode *inode = file->f_dentry->d_inode;
  102. int err, pos;
  103. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
  104. {
  105. if (file->f_flags & O_APPEND)
  106. pos = inode->i_size;
  107. else
  108. pos = *ppos;
  109. if (inode->i_sb->s_blocksize < (udf_file_entry_alloc_offset(inode) +
  110. pos + count))
  111. {
  112. udf_expand_file_adinicb(inode, pos + count, &err);
  113. if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
  114. {
  115. udf_debug("udf_expand_adinicb: err=%d\n", err);
  116. return err;
  117. }
  118. }
  119. else
  120. {
  121. if (pos + count > inode->i_size)
  122. UDF_I_LENALLOC(inode) = pos + count;
  123. else
  124. UDF_I_LENALLOC(inode) = inode->i_size;
  125. }
  126. }
  127. retval = generic_file_write(file, buf, count, ppos);
  128. if (retval > 0)
  129. mark_inode_dirty(inode);
  130. return retval;
  131. }
  132. /*
  133. * udf_ioctl
  134. *
  135. * PURPOSE
  136. * Issue an ioctl.
  137. *
  138. * DESCRIPTION
  139. * Optional - sys_ioctl() will return -ENOTTY if this routine is not
  140. * available, and the ioctl cannot be handled without filesystem help.
  141. *
  142. * sys_ioctl() handles these ioctls that apply only to regular files:
  143. * FIBMAP [requires udf_block_map()], FIGETBSZ, FIONREAD
  144. * These ioctls are also handled by sys_ioctl():
  145. * FIOCLEX, FIONCLEX, FIONBIO, FIOASYNC
  146. * All other ioctls are passed to the filesystem.
  147. *
  148. * Refer to sys_ioctl() in fs/ioctl.c
  149. * sys_ioctl() -> .
  150. *
  151. * PRE-CONDITIONS
  152. * inode Pointer to inode that ioctl was issued on.
  153. * filp Pointer to file that ioctl was issued on.
  154. * cmd The ioctl command.
  155. * arg The ioctl argument [can be interpreted as a
  156. * user-space pointer if desired].
  157. *
  158. * POST-CONDITIONS
  159. * <return> Success (>=0) or an error code (<=0) that
  160. * sys_ioctl() will return.
  161. *
  162. * HISTORY
  163. * July 1, 1997 - Andrew E. Mileski
  164. * Written, tested, and released.
  165. */
  166. int udf_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  167. unsigned long arg)
  168. {
  169. int result = -EINVAL;
  170. if ( permission(inode, MAY_READ, NULL) != 0 )
  171. {
  172. udf_debug("no permission to access inode %lu\n",
  173. inode->i_ino);
  174. return -EPERM;
  175. }
  176. if ( !arg )
  177. {
  178. udf_debug("invalid argument to udf_ioctl\n");
  179. return -EINVAL;
  180. }
  181. switch (cmd)
  182. {
  183. case UDF_GETVOLIDENT:
  184. return copy_to_user((char __user *)arg,
  185. UDF_SB_VOLIDENT(inode->i_sb), 32) ? -EFAULT : 0;
  186. case UDF_RELOCATE_BLOCKS:
  187. {
  188. long old, new;
  189. if (!capable(CAP_SYS_ADMIN)) return -EACCES;
  190. if (get_user(old, (long __user *)arg)) return -EFAULT;
  191. if ((result = udf_relocate_blocks(inode->i_sb,
  192. old, &new)) == 0)
  193. result = put_user(new, (long __user *)arg);
  194. return result;
  195. }
  196. case UDF_GETEASIZE:
  197. result = put_user(UDF_I_LENEATTR(inode), (int __user *)arg);
  198. break;
  199. case UDF_GETEABLOCK:
  200. result = copy_to_user((char __user *)arg, UDF_I_DATA(inode),
  201. UDF_I_LENEATTR(inode)) ? -EFAULT : 0;
  202. break;
  203. }
  204. return result;
  205. }
  206. /*
  207. * udf_release_file
  208. *
  209. * PURPOSE
  210. * Called when all references to the file are closed
  211. *
  212. * DESCRIPTION
  213. * Discard prealloced blocks
  214. *
  215. * HISTORY
  216. *
  217. */
  218. static int udf_release_file(struct inode * inode, struct file * filp)
  219. {
  220. if (filp->f_mode & FMODE_WRITE)
  221. {
  222. lock_kernel();
  223. udf_discard_prealloc(inode);
  224. unlock_kernel();
  225. }
  226. return 0;
  227. }
  228. struct file_operations udf_file_operations = {
  229. .read = generic_file_read,
  230. .ioctl = udf_ioctl,
  231. .open = generic_file_open,
  232. .mmap = generic_file_mmap,
  233. .write = udf_file_write,
  234. .release = udf_release_file,
  235. .fsync = udf_fsync_file,
  236. .sendfile = generic_file_sendfile,
  237. };
  238. struct inode_operations udf_file_inode_operations = {
  239. .truncate = udf_truncate,
  240. };