ioctl.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "cifs_ioctl.h"
  34. #include <linux/btrfs.h>
  35. static long cifs_ioctl_clone(unsigned int xid, struct file *dst_file,
  36. unsigned long srcfd, u64 off, u64 len, u64 destoff,
  37. bool dup_extents)
  38. {
  39. int rc;
  40. struct cifsFileInfo *smb_file_target = dst_file->private_data;
  41. struct inode *target_inode = file_inode(dst_file);
  42. struct cifs_tcon *target_tcon;
  43. struct fd src_file;
  44. struct cifsFileInfo *smb_file_src;
  45. struct inode *src_inode;
  46. struct cifs_tcon *src_tcon;
  47. cifs_dbg(FYI, "ioctl clone range\n");
  48. /* the destination must be opened for writing */
  49. if (!(dst_file->f_mode & FMODE_WRITE)) {
  50. cifs_dbg(FYI, "file target not open for write\n");
  51. return -EINVAL;
  52. }
  53. /* check if target volume is readonly and take reference */
  54. rc = mnt_want_write_file(dst_file);
  55. if (rc) {
  56. cifs_dbg(FYI, "mnt_want_write failed with rc %d\n", rc);
  57. return rc;
  58. }
  59. src_file = fdget(srcfd);
  60. if (!src_file.file) {
  61. rc = -EBADF;
  62. goto out_drop_write;
  63. }
  64. if ((!src_file.file->private_data) || (!dst_file->private_data)) {
  65. rc = -EBADF;
  66. cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
  67. goto out_fput;
  68. }
  69. rc = -EXDEV;
  70. smb_file_target = dst_file->private_data;
  71. smb_file_src = src_file.file->private_data;
  72. src_tcon = tlink_tcon(smb_file_src->tlink);
  73. target_tcon = tlink_tcon(smb_file_target->tlink);
  74. /* check if source and target are on same tree connection */
  75. if (src_tcon != target_tcon) {
  76. cifs_dbg(VFS, "file copy src and target on different volume\n");
  77. goto out_fput;
  78. }
  79. src_inode = file_inode(src_file.file);
  80. rc = -EINVAL;
  81. if (S_ISDIR(src_inode->i_mode))
  82. goto out_fput;
  83. /*
  84. * Note: cifs case is easier than btrfs since server responsible for
  85. * checks for proper open modes and file type and if it wants
  86. * server could even support copy of range where source = target
  87. */
  88. lock_two_nondirectories(target_inode, src_inode);
  89. /* determine range to clone */
  90. rc = -EINVAL;
  91. if (off + len > src_inode->i_size || off + len < off)
  92. goto out_unlock;
  93. if (len == 0)
  94. len = src_inode->i_size - off;
  95. cifs_dbg(FYI, "about to flush pages\n");
  96. /* should we flush first and last page first */
  97. truncate_inode_pages_range(&target_inode->i_data, destoff,
  98. PAGE_CACHE_ALIGN(destoff + len)-1);
  99. if (dup_extents && target_tcon->ses->server->ops->duplicate_extents)
  100. rc = target_tcon->ses->server->ops->duplicate_extents(xid,
  101. smb_file_src, smb_file_target, off, len, destoff);
  102. else if (!dup_extents && target_tcon->ses->server->ops->clone_range)
  103. rc = target_tcon->ses->server->ops->clone_range(xid,
  104. smb_file_src, smb_file_target, off, len, destoff);
  105. else
  106. rc = -EOPNOTSUPP;
  107. /* force revalidate of size and timestamps of target file now
  108. that target is updated on the server */
  109. CIFS_I(target_inode)->time = 0;
  110. out_unlock:
  111. /* although unlocking in the reverse order from locking is not
  112. strictly necessary here it is a little cleaner to be consistent */
  113. unlock_two_nondirectories(src_inode, target_inode);
  114. out_fput:
  115. fdput(src_file);
  116. out_drop_write:
  117. mnt_drop_write_file(dst_file);
  118. return rc;
  119. }
  120. static long smb_mnt_get_fsinfo(unsigned int xid, struct cifs_tcon *tcon,
  121. void __user *arg)
  122. {
  123. int rc = 0;
  124. struct smb_mnt_fs_info *fsinf;
  125. fsinf = kzalloc(sizeof(struct smb_mnt_fs_info), GFP_KERNEL);
  126. if (fsinf == NULL)
  127. return -ENOMEM;
  128. fsinf->version = 1;
  129. fsinf->protocol_id = tcon->ses->server->vals->protocol_id;
  130. fsinf->device_characteristics =
  131. le32_to_cpu(tcon->fsDevInfo.DeviceCharacteristics);
  132. fsinf->device_type = le32_to_cpu(tcon->fsDevInfo.DeviceType);
  133. fsinf->fs_attributes = le32_to_cpu(tcon->fsAttrInfo.Attributes);
  134. fsinf->max_path_component =
  135. le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
  136. #ifdef CONFIG_CIFS_SMB2
  137. fsinf->vol_serial_number = tcon->vol_serial_number;
  138. fsinf->vol_create_time = le64_to_cpu(tcon->vol_create_time);
  139. fsinf->share_flags = tcon->share_flags;
  140. fsinf->share_caps = le32_to_cpu(tcon->capabilities);
  141. fsinf->sector_flags = tcon->ss_flags;
  142. fsinf->optimal_sector_size = tcon->perf_sector_size;
  143. fsinf->max_bytes_chunk = tcon->max_bytes_chunk;
  144. fsinf->maximal_access = tcon->maximal_access;
  145. #endif /* SMB2 */
  146. fsinf->cifs_posix_caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  147. if (copy_to_user(arg, fsinf, sizeof(struct smb_mnt_fs_info)))
  148. rc = -EFAULT;
  149. kfree(fsinf);
  150. return rc;
  151. }
  152. long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
  153. {
  154. struct inode *inode = file_inode(filep);
  155. int rc = -ENOTTY; /* strange error - but the precedent */
  156. unsigned int xid;
  157. struct cifs_sb_info *cifs_sb;
  158. struct cifsFileInfo *pSMBFile = filep->private_data;
  159. struct cifs_tcon *tcon;
  160. __u64 ExtAttrBits = 0;
  161. __u64 caps;
  162. xid = get_xid();
  163. cifs_sb = CIFS_SB(inode->i_sb);
  164. switch (command) {
  165. case FS_IOC_GETFLAGS:
  166. if (pSMBFile == NULL)
  167. break;
  168. tcon = tlink_tcon(pSMBFile->tlink);
  169. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  170. #ifdef CONFIG_CIFS_POSIX
  171. if (CIFS_UNIX_EXTATTR_CAP & caps) {
  172. __u64 ExtAttrMask = 0;
  173. rc = CIFSGetExtAttr(xid, tcon,
  174. pSMBFile->fid.netfid,
  175. &ExtAttrBits, &ExtAttrMask);
  176. if (rc == 0)
  177. rc = put_user(ExtAttrBits &
  178. FS_FL_USER_VISIBLE,
  179. (int __user *)arg);
  180. if (rc != EOPNOTSUPP)
  181. break;
  182. }
  183. #endif /* CONFIG_CIFS_POSIX */
  184. rc = 0;
  185. if (CIFS_I(inode)->cifsAttrs & ATTR_COMPRESSED) {
  186. /* add in the compressed bit */
  187. ExtAttrBits = FS_COMPR_FL;
  188. rc = put_user(ExtAttrBits & FS_FL_USER_VISIBLE,
  189. (int __user *)arg);
  190. }
  191. break;
  192. case FS_IOC_SETFLAGS:
  193. if (pSMBFile == NULL)
  194. break;
  195. tcon = tlink_tcon(pSMBFile->tlink);
  196. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  197. if (get_user(ExtAttrBits, (int __user *)arg)) {
  198. rc = -EFAULT;
  199. break;
  200. }
  201. /*
  202. * if (CIFS_UNIX_EXTATTR_CAP & caps)
  203. * rc = CIFSSetExtAttr(xid, tcon,
  204. * pSMBFile->fid.netfid,
  205. * extAttrBits,
  206. * &ExtAttrMask);
  207. * if (rc != EOPNOTSUPP)
  208. * break;
  209. */
  210. /* Currently only flag we can set is compressed flag */
  211. if ((ExtAttrBits & FS_COMPR_FL) == 0)
  212. break;
  213. /* Try to set compress flag */
  214. if (tcon->ses->server->ops->set_compression) {
  215. rc = tcon->ses->server->ops->set_compression(
  216. xid, tcon, pSMBFile);
  217. cifs_dbg(FYI, "set compress flag rc %d\n", rc);
  218. }
  219. break;
  220. case CIFS_IOC_COPYCHUNK_FILE:
  221. rc = cifs_ioctl_clone(xid, filep, arg, 0, 0, 0, false);
  222. break;
  223. case BTRFS_IOC_CLONE:
  224. rc = cifs_ioctl_clone(xid, filep, arg, 0, 0, 0, true);
  225. break;
  226. case CIFS_IOC_SET_INTEGRITY:
  227. if (pSMBFile == NULL)
  228. break;
  229. tcon = tlink_tcon(pSMBFile->tlink);
  230. if (tcon->ses->server->ops->set_integrity)
  231. rc = tcon->ses->server->ops->set_integrity(xid,
  232. tcon, pSMBFile);
  233. else
  234. rc = -EOPNOTSUPP;
  235. break;
  236. case CIFS_IOC_GET_MNT_INFO:
  237. tcon = tlink_tcon(pSMBFile->tlink);
  238. rc = smb_mnt_get_fsinfo(xid, tcon, (void __user *)arg);
  239. break;
  240. default:
  241. cifs_dbg(FYI, "unsupported ioctl\n");
  242. break;
  243. }
  244. free_xid(xid);
  245. return rc;
  246. }