ioctl.c 8.1 KB

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