ioctl.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "cifspdu.h"
  29. #include "cifsglob.h"
  30. #include "cifsproto.h"
  31. #include "cifs_debug.h"
  32. #include "cifsfs.h"
  33. #include <linux/btrfs.h>
  34. #define CIFS_IOCTL_MAGIC 0xCF
  35. #define CIFS_IOC_COPYCHUNK_FILE _IOW(CIFS_IOCTL_MAGIC, 3, int)
  36. #define CIFS_IOC_SET_INTEGRITY _IO(CIFS_IOCTL_MAGIC, 4)
  37. static long cifs_ioctl_clone(unsigned int xid, struct file *dst_file,
  38. unsigned long srcfd, u64 off, u64 len, u64 destoff,
  39. bool dup_extents)
  40. {
  41. int rc;
  42. struct cifsFileInfo *smb_file_target = dst_file->private_data;
  43. struct inode *target_inode = file_inode(dst_file);
  44. struct cifs_tcon *target_tcon;
  45. struct fd src_file;
  46. struct cifsFileInfo *smb_file_src;
  47. struct inode *src_inode;
  48. struct cifs_tcon *src_tcon;
  49. cifs_dbg(FYI, "ioctl clone range\n");
  50. /* the destination must be opened for writing */
  51. if (!(dst_file->f_mode & FMODE_WRITE)) {
  52. cifs_dbg(FYI, "file target not open for write\n");
  53. return -EINVAL;
  54. }
  55. /* check if target volume is readonly and take reference */
  56. rc = mnt_want_write_file(dst_file);
  57. if (rc) {
  58. cifs_dbg(FYI, "mnt_want_write failed with rc %d\n", rc);
  59. return rc;
  60. }
  61. src_file = fdget(srcfd);
  62. if (!src_file.file) {
  63. rc = -EBADF;
  64. goto out_drop_write;
  65. }
  66. if ((!src_file.file->private_data) || (!dst_file->private_data)) {
  67. rc = -EBADF;
  68. cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
  69. goto out_fput;
  70. }
  71. rc = -EXDEV;
  72. smb_file_target = dst_file->private_data;
  73. smb_file_src = src_file.file->private_data;
  74. src_tcon = tlink_tcon(smb_file_src->tlink);
  75. target_tcon = tlink_tcon(smb_file_target->tlink);
  76. /* check if source and target are on same tree connection */
  77. if (src_tcon != target_tcon) {
  78. cifs_dbg(VFS, "file copy src and target on different volume\n");
  79. goto out_fput;
  80. }
  81. src_inode = file_inode(src_file.file);
  82. rc = -EINVAL;
  83. if (S_ISDIR(src_inode->i_mode))
  84. goto out_fput;
  85. /*
  86. * Note: cifs case is easier than btrfs since server responsible for
  87. * checks for proper open modes and file type and if it wants
  88. * server could even support copy of range where source = target
  89. */
  90. lock_two_nondirectories(target_inode, src_inode);
  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 (dup_extents && target_tcon->ses->server->ops->duplicate_extents)
  102. rc = target_tcon->ses->server->ops->duplicate_extents(xid,
  103. smb_file_src, smb_file_target, off, len, destoff);
  104. else if (!dup_extents && target_tcon->ses->server->ops->clone_range)
  105. rc = target_tcon->ses->server->ops->clone_range(xid,
  106. smb_file_src, smb_file_target, off, len, destoff);
  107. else
  108. rc = -EOPNOTSUPP;
  109. /* force revalidate of size and timestamps of target file now
  110. that target is updated on the server */
  111. CIFS_I(target_inode)->time = 0;
  112. out_unlock:
  113. /* although unlocking in the reverse order from locking is not
  114. strictly necessary here it is a little cleaner to be consistent */
  115. unlock_two_nondirectories(src_inode, target_inode);
  116. out_fput:
  117. fdput(src_file);
  118. out_drop_write:
  119. mnt_drop_write_file(dst_file);
  120. return rc;
  121. }
  122. long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
  123. {
  124. struct inode *inode = file_inode(filep);
  125. int rc = -ENOTTY; /* strange error - but the precedent */
  126. unsigned int xid;
  127. struct cifs_sb_info *cifs_sb;
  128. struct cifsFileInfo *pSMBFile = filep->private_data;
  129. struct cifs_tcon *tcon;
  130. __u64 ExtAttrBits = 0;
  131. __u64 caps;
  132. xid = get_xid();
  133. cifs_dbg(FYI, "ioctl file %p cmd %u arg %lu\n", filep, command, arg);
  134. cifs_sb = CIFS_SB(inode->i_sb);
  135. switch (command) {
  136. case FS_IOC_GETFLAGS:
  137. if (pSMBFile == NULL)
  138. break;
  139. tcon = tlink_tcon(pSMBFile->tlink);
  140. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  141. #ifdef CONFIG_CIFS_POSIX
  142. if (CIFS_UNIX_EXTATTR_CAP & caps) {
  143. __u64 ExtAttrMask = 0;
  144. rc = CIFSGetExtAttr(xid, tcon,
  145. pSMBFile->fid.netfid,
  146. &ExtAttrBits, &ExtAttrMask);
  147. if (rc == 0)
  148. rc = put_user(ExtAttrBits &
  149. FS_FL_USER_VISIBLE,
  150. (int __user *)arg);
  151. if (rc != EOPNOTSUPP)
  152. break;
  153. }
  154. #endif /* CONFIG_CIFS_POSIX */
  155. rc = 0;
  156. if (CIFS_I(inode)->cifsAttrs & ATTR_COMPRESSED) {
  157. /* add in the compressed bit */
  158. ExtAttrBits = FS_COMPR_FL;
  159. rc = put_user(ExtAttrBits & FS_FL_USER_VISIBLE,
  160. (int __user *)arg);
  161. }
  162. break;
  163. case FS_IOC_SETFLAGS:
  164. if (pSMBFile == NULL)
  165. break;
  166. tcon = tlink_tcon(pSMBFile->tlink);
  167. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  168. if (get_user(ExtAttrBits, (int __user *)arg)) {
  169. rc = -EFAULT;
  170. break;
  171. }
  172. /*
  173. * if (CIFS_UNIX_EXTATTR_CAP & caps)
  174. * rc = CIFSSetExtAttr(xid, tcon,
  175. * pSMBFile->fid.netfid,
  176. * extAttrBits,
  177. * &ExtAttrMask);
  178. * if (rc != EOPNOTSUPP)
  179. * break;
  180. */
  181. /* Currently only flag we can set is compressed flag */
  182. if ((ExtAttrBits & FS_COMPR_FL) == 0)
  183. break;
  184. /* Try to set compress flag */
  185. if (tcon->ses->server->ops->set_compression) {
  186. rc = tcon->ses->server->ops->set_compression(
  187. xid, tcon, pSMBFile);
  188. cifs_dbg(FYI, "set compress flag rc %d\n", rc);
  189. }
  190. break;
  191. case CIFS_IOC_COPYCHUNK_FILE:
  192. rc = cifs_ioctl_clone(xid, filep, arg, 0, 0, 0, false);
  193. break;
  194. case BTRFS_IOC_CLONE:
  195. rc = cifs_ioctl_clone(xid, filep, arg, 0, 0, 0, true);
  196. break;
  197. case CIFS_IOC_SET_INTEGRITY:
  198. if (pSMBFile == NULL)
  199. break;
  200. tcon = tlink_tcon(pSMBFile->tlink);
  201. if (tcon->ses->server->ops->set_integrity)
  202. rc = tcon->ses->server->ops->set_integrity(xid,
  203. tcon, pSMBFile);
  204. else
  205. rc = -EOPNOTSUPP;
  206. break;
  207. default:
  208. cifs_dbg(FYI, "unsupported ioctl\n");
  209. break;
  210. }
  211. free_xid(xid);
  212. return rc;
  213. }