ioctl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * fs/cifs/ioctl.c
  3. *
  4. * vfs operations that deal with io control
  5. *
  6. * Copyright (C) International Business Machines Corp., 2005,2013
  7. * Author(s): Steve French (sfrench@us.ibm.com)
  8. *
  9. * This library is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published
  11. * by the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  17. * the GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/file.h>
  25. #include <linux/mount.h>
  26. #include <linux/mm.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/btrfs.h>
  29. #include "cifspdu.h"
  30. #include "cifsglob.h"
  31. #include "cifsproto.h"
  32. #include "cifs_debug.h"
  33. #include "cifsfs.h"
  34. static long cifs_ioctl_clone(unsigned int xid, struct file *dst_file,
  35. unsigned long srcfd, u64 off, u64 len, u64 destoff)
  36. {
  37. int rc;
  38. struct cifsFileInfo *smb_file_target = dst_file->private_data;
  39. struct inode *target_inode = file_inode(dst_file);
  40. struct cifs_tcon *target_tcon;
  41. struct fd src_file;
  42. struct cifsFileInfo *smb_file_src;
  43. struct inode *src_inode;
  44. struct cifs_tcon *src_tcon;
  45. cifs_dbg(FYI, "ioctl clone range\n");
  46. /* the destination must be opened for writing */
  47. if (!(dst_file->f_mode & FMODE_WRITE)) {
  48. cifs_dbg(FYI, "file target not open for write\n");
  49. return -EINVAL;
  50. }
  51. /* check if target volume is readonly and take reference */
  52. rc = mnt_want_write_file(dst_file);
  53. if (rc) {
  54. cifs_dbg(FYI, "mnt_want_write failed with rc %d\n", rc);
  55. return rc;
  56. }
  57. src_file = fdget(srcfd);
  58. if (!src_file.file) {
  59. rc = -EBADF;
  60. goto out_drop_write;
  61. }
  62. if ((!src_file.file->private_data) || (!dst_file->private_data)) {
  63. rc = -EBADF;
  64. cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
  65. goto out_fput;
  66. }
  67. rc = -EXDEV;
  68. smb_file_target = dst_file->private_data;
  69. smb_file_src = src_file.file->private_data;
  70. src_tcon = tlink_tcon(smb_file_src->tlink);
  71. target_tcon = tlink_tcon(smb_file_target->tlink);
  72. /* check if source and target are on same tree connection */
  73. if (src_tcon != target_tcon) {
  74. cifs_dbg(VFS, "file copy src and target on different volume\n");
  75. goto out_fput;
  76. }
  77. src_inode = src_file.file->f_dentry->d_inode;
  78. /*
  79. * Note: cifs case is easier than btrfs since server responsible for
  80. * checks for proper open modes and file type and if it wants
  81. * server could even support copy of range where source = target
  82. */
  83. /* so we do not deadlock racing two ioctls on same files */
  84. if (target_inode < src_inode) {
  85. mutex_lock_nested(&target_inode->i_mutex, I_MUTEX_PARENT);
  86. mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD);
  87. } else {
  88. mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT);
  89. mutex_lock_nested(&target_inode->i_mutex, I_MUTEX_CHILD);
  90. }
  91. /* determine range to clone */
  92. rc = -EINVAL;
  93. if (off + len > src_inode->i_size || off + len < off)
  94. goto out_unlock;
  95. if (len == 0)
  96. len = src_inode->i_size - off;
  97. cifs_dbg(FYI, "about to flush pages\n");
  98. /* should we flush first and last page first */
  99. truncate_inode_pages_range(&target_inode->i_data, destoff,
  100. PAGE_CACHE_ALIGN(destoff + len)-1);
  101. if (target_tcon->ses->server->ops->clone_range)
  102. rc = target_tcon->ses->server->ops->clone_range(xid,
  103. smb_file_src, smb_file_target, off, len, destoff);
  104. /* force revalidate of size and timestamps of target file now
  105. that target is updated on the server */
  106. CIFS_I(target_inode)->time = 0;
  107. out_unlock:
  108. /* although unlocking in the reverse order from locking is not
  109. strictly necessary here it is a little cleaner to be consistent */
  110. if (target_inode < src_inode) {
  111. mutex_unlock(&src_inode->i_mutex);
  112. mutex_unlock(&target_inode->i_mutex);
  113. } else {
  114. mutex_unlock(&target_inode->i_mutex);
  115. mutex_unlock(&src_inode->i_mutex);
  116. }
  117. out_fput:
  118. fdput(src_file);
  119. out_drop_write:
  120. mnt_drop_write_file(dst_file);
  121. return rc;
  122. }
  123. long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
  124. {
  125. struct inode *inode = file_inode(filep);
  126. int rc = -ENOTTY; /* strange error - but the precedent */
  127. unsigned int xid;
  128. struct cifs_sb_info *cifs_sb;
  129. struct cifsFileInfo *pSMBFile = filep->private_data;
  130. struct cifs_tcon *tcon;
  131. __u64 ExtAttrBits = 0;
  132. __u64 caps;
  133. xid = get_xid();
  134. cifs_dbg(FYI, "ioctl file %p cmd %u arg %lu\n", filep, command, arg);
  135. cifs_sb = CIFS_SB(inode->i_sb);
  136. switch (command) {
  137. case FS_IOC_GETFLAGS:
  138. if (pSMBFile == NULL)
  139. break;
  140. tcon = tlink_tcon(pSMBFile->tlink);
  141. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  142. #ifdef CONFIG_CIFS_POSIX
  143. if (CIFS_UNIX_EXTATTR_CAP & caps) {
  144. __u64 ExtAttrMask = 0;
  145. rc = CIFSGetExtAttr(xid, tcon,
  146. pSMBFile->fid.netfid,
  147. &ExtAttrBits, &ExtAttrMask);
  148. if (rc == 0)
  149. rc = put_user(ExtAttrBits &
  150. FS_FL_USER_VISIBLE,
  151. (int __user *)arg);
  152. if (rc != EOPNOTSUPP)
  153. break;
  154. }
  155. #endif /* CONFIG_CIFS_POSIX */
  156. rc = 0;
  157. if (CIFS_I(inode)->cifsAttrs & ATTR_COMPRESSED) {
  158. /* add in the compressed bit */
  159. ExtAttrBits = FS_COMPR_FL;
  160. rc = put_user(ExtAttrBits & FS_FL_USER_VISIBLE,
  161. (int __user *)arg);
  162. }
  163. break;
  164. case FS_IOC_SETFLAGS:
  165. if (pSMBFile == NULL)
  166. break;
  167. tcon = tlink_tcon(pSMBFile->tlink);
  168. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  169. if (get_user(ExtAttrBits, (int __user *)arg)) {
  170. rc = -EFAULT;
  171. break;
  172. }
  173. /*
  174. * if (CIFS_UNIX_EXTATTR_CAP & caps)
  175. * rc = CIFSSetExtAttr(xid, tcon,
  176. * pSMBFile->fid.netfid,
  177. * extAttrBits,
  178. * &ExtAttrMask);
  179. * if (rc != EOPNOTSUPP)
  180. * break;
  181. */
  182. /* Currently only flag we can set is compressed flag */
  183. if ((ExtAttrBits & FS_COMPR_FL) == 0)
  184. break;
  185. /* Try to set compress flag */
  186. if (tcon->ses->server->ops->set_compression) {
  187. rc = tcon->ses->server->ops->set_compression(
  188. xid, tcon, pSMBFile);
  189. cifs_dbg(FYI, "set compress flag rc %d\n", rc);
  190. }
  191. break;
  192. case BTRFS_IOC_CLONE:
  193. rc = cifs_ioctl_clone(xid, filep, arg, 0, 0, 0);
  194. break;
  195. default:
  196. cifs_dbg(FYI, "unsupported ioctl\n");
  197. break;
  198. }
  199. free_xid(xid);
  200. return rc;
  201. }