file.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <asm/uaccess.h>
  30. #include <linux/kernel.h>
  31. #include <linux/string.h> /* memset */
  32. #include <linux/capability.h>
  33. #include <linux/errno.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/buffer_head.h>
  36. #include <linux/aio.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. struct udf_inode_info *iinfo = UDF_I(inode);
  44. BUG_ON(!PageLocked(page));
  45. kaddr = kmap(page);
  46. memset(kaddr, 0, PAGE_CACHE_SIZE);
  47. memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr, inode->i_size);
  48. flush_dcache_page(page);
  49. SetPageUptodate(page);
  50. kunmap(page);
  51. unlock_page(page);
  52. return 0;
  53. }
  54. static int udf_adinicb_writepage(struct page *page,
  55. struct writeback_control *wbc)
  56. {
  57. struct inode *inode = page->mapping->host;
  58. char *kaddr;
  59. struct udf_inode_info *iinfo = UDF_I(inode);
  60. BUG_ON(!PageLocked(page));
  61. kaddr = kmap(page);
  62. memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr, inode->i_size);
  63. mark_inode_dirty(inode);
  64. SetPageUptodate(page);
  65. kunmap(page);
  66. unlock_page(page);
  67. return 0;
  68. }
  69. static int udf_adinicb_write_end(struct file *file,
  70. struct address_space *mapping,
  71. loff_t pos, unsigned len, unsigned copied,
  72. struct page *page, void *fsdata)
  73. {
  74. struct inode *inode = mapping->host;
  75. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  76. char *kaddr;
  77. struct udf_inode_info *iinfo = UDF_I(inode);
  78. kaddr = kmap_atomic(page, KM_USER0);
  79. memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
  80. kaddr + offset, copied);
  81. kunmap_atomic(kaddr, KM_USER0);
  82. return simple_write_end(file, mapping, pos, len, copied, page, fsdata);
  83. }
  84. const struct address_space_operations udf_adinicb_aops = {
  85. .readpage = udf_adinicb_readpage,
  86. .writepage = udf_adinicb_writepage,
  87. .write_begin = simple_write_begin,
  88. .write_end = udf_adinicb_write_end,
  89. };
  90. static ssize_t udf_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  91. unsigned long nr_segs, loff_t ppos)
  92. {
  93. ssize_t retval;
  94. struct file *file = iocb->ki_filp;
  95. struct inode *inode = file->f_path.dentry->d_inode;
  96. int err, pos;
  97. size_t count = iocb->ki_left;
  98. struct udf_inode_info *iinfo = UDF_I(inode);
  99. down_write(&iinfo->i_data_sem);
  100. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  101. if (file->f_flags & O_APPEND)
  102. pos = inode->i_size;
  103. else
  104. pos = ppos;
  105. if (inode->i_sb->s_blocksize <
  106. (udf_file_entry_alloc_offset(inode) +
  107. pos + count)) {
  108. err = udf_expand_file_adinicb(inode);
  109. if (err) {
  110. udf_debug("udf_expand_adinicb: err=%d\n", err);
  111. return err;
  112. }
  113. } else {
  114. if (pos + count > inode->i_size)
  115. iinfo->i_lenAlloc = pos + count;
  116. else
  117. iinfo->i_lenAlloc = inode->i_size;
  118. up_write(&iinfo->i_data_sem);
  119. }
  120. } else
  121. up_write(&iinfo->i_data_sem);
  122. retval = generic_file_aio_write(iocb, iov, nr_segs, ppos);
  123. if (retval > 0)
  124. mark_inode_dirty(inode);
  125. return retval;
  126. }
  127. long udf_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  128. {
  129. struct inode *inode = filp->f_dentry->d_inode;
  130. long old_block, new_block;
  131. int result = -EINVAL;
  132. if (file_permission(filp, MAY_READ) != 0) {
  133. udf_debug("no permission to access inode %lu\n", inode->i_ino);
  134. result = -EPERM;
  135. goto out;
  136. }
  137. if (!arg) {
  138. udf_debug("invalid argument to udf_ioctl\n");
  139. result = -EINVAL;
  140. goto out;
  141. }
  142. switch (cmd) {
  143. case UDF_GETVOLIDENT:
  144. if (copy_to_user((char __user *)arg,
  145. UDF_SB(inode->i_sb)->s_volume_ident, 32))
  146. result = -EFAULT;
  147. else
  148. result = 0;
  149. goto out;
  150. case UDF_RELOCATE_BLOCKS:
  151. if (!capable(CAP_SYS_ADMIN)) {
  152. result = -EACCES;
  153. goto out;
  154. }
  155. if (get_user(old_block, (long __user *)arg)) {
  156. result = -EFAULT;
  157. goto out;
  158. }
  159. result = udf_relocate_blocks(inode->i_sb,
  160. old_block, &new_block);
  161. if (result == 0)
  162. result = put_user(new_block, (long __user *)arg);
  163. goto out;
  164. case UDF_GETEASIZE:
  165. result = put_user(UDF_I(inode)->i_lenEAttr, (int __user *)arg);
  166. goto out;
  167. case UDF_GETEABLOCK:
  168. result = copy_to_user((char __user *)arg,
  169. UDF_I(inode)->i_ext.i_data,
  170. UDF_I(inode)->i_lenEAttr) ? -EFAULT : 0;
  171. goto out;
  172. }
  173. out:
  174. return result;
  175. }
  176. static int udf_release_file(struct inode *inode, struct file *filp)
  177. {
  178. if (filp->f_mode & FMODE_WRITE) {
  179. down_write(&UDF_I(inode)->i_data_sem);
  180. udf_discard_prealloc(inode);
  181. udf_truncate_tail_extent(inode);
  182. up_write(&UDF_I(inode)->i_data_sem);
  183. }
  184. return 0;
  185. }
  186. const struct file_operations udf_file_operations = {
  187. .read = do_sync_read,
  188. .aio_read = generic_file_aio_read,
  189. .unlocked_ioctl = udf_ioctl,
  190. .open = generic_file_open,
  191. .mmap = generic_file_mmap,
  192. .write = do_sync_write,
  193. .aio_write = udf_file_aio_write,
  194. .release = udf_release_file,
  195. .fsync = generic_file_fsync,
  196. .splice_read = generic_file_splice_read,
  197. .llseek = generic_file_llseek,
  198. };
  199. static int udf_setattr(struct dentry *dentry, struct iattr *attr)
  200. {
  201. struct inode *inode = dentry->d_inode;
  202. int error;
  203. error = inode_change_ok(inode, attr);
  204. if (error)
  205. return error;
  206. if ((attr->ia_valid & ATTR_SIZE) &&
  207. attr->ia_size != i_size_read(inode)) {
  208. error = udf_setsize(inode, attr->ia_size);
  209. if (error)
  210. return error;
  211. }
  212. setattr_copy(inode, attr);
  213. mark_inode_dirty(inode);
  214. return 0;
  215. }
  216. const struct inode_operations udf_file_inode_operations = {
  217. .setattr = udf_setattr,
  218. };