ioctl.c 8.3 KB

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