ioctl.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "smb2proto.h"
  35. #include <linux/btrfs.h>
  36. static long cifs_ioctl_query_info(unsigned int xid, struct file *filep,
  37. unsigned long p)
  38. {
  39. struct inode *inode = file_inode(filep);
  40. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  41. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  42. struct dentry *dentry = filep->f_path.dentry;
  43. unsigned char *path;
  44. __le16 *utf16_path = NULL, root_path;
  45. int rc = 0;
  46. path = build_path_from_dentry(dentry);
  47. if (path == NULL)
  48. return -ENOMEM;
  49. cifs_dbg(FYI, "%s %s\n", __func__, path);
  50. if (!path[0]) {
  51. root_path = 0;
  52. utf16_path = &root_path;
  53. } else {
  54. utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
  55. if (!utf16_path) {
  56. rc = -ENOMEM;
  57. goto ici_exit;
  58. }
  59. }
  60. if (tcon->ses->server->ops->ioctl_query_info)
  61. rc = tcon->ses->server->ops->ioctl_query_info(
  62. xid, tcon, cifs_sb, utf16_path,
  63. filep->private_data ? 0 : 1, p);
  64. else
  65. rc = -EOPNOTSUPP;
  66. ici_exit:
  67. if (utf16_path != &root_path)
  68. kfree(utf16_path);
  69. kfree(path);
  70. return rc;
  71. }
  72. static long cifs_ioctl_copychunk(unsigned int xid, struct file *dst_file,
  73. unsigned long srcfd)
  74. {
  75. int rc;
  76. struct fd src_file;
  77. struct inode *src_inode;
  78. cifs_dbg(FYI, "ioctl copychunk range\n");
  79. /* the destination must be opened for writing */
  80. if (!(dst_file->f_mode & FMODE_WRITE)) {
  81. cifs_dbg(FYI, "file target not open for write\n");
  82. return -EINVAL;
  83. }
  84. /* check if target volume is readonly and take reference */
  85. rc = mnt_want_write_file(dst_file);
  86. if (rc) {
  87. cifs_dbg(FYI, "mnt_want_write failed with rc %d\n", rc);
  88. return rc;
  89. }
  90. src_file = fdget(srcfd);
  91. if (!src_file.file) {
  92. rc = -EBADF;
  93. goto out_drop_write;
  94. }
  95. if (src_file.file->f_op->unlocked_ioctl != cifs_ioctl) {
  96. rc = -EBADF;
  97. cifs_dbg(VFS, "src file seems to be from a different filesystem type\n");
  98. goto out_fput;
  99. }
  100. src_inode = file_inode(src_file.file);
  101. rc = -EINVAL;
  102. if (S_ISDIR(src_inode->i_mode))
  103. goto out_fput;
  104. rc = cifs_file_copychunk_range(xid, src_file.file, 0, dst_file, 0,
  105. src_inode->i_size, 0);
  106. if (rc > 0)
  107. rc = 0;
  108. out_fput:
  109. fdput(src_file);
  110. out_drop_write:
  111. mnt_drop_write_file(dst_file);
  112. return rc;
  113. }
  114. static long smb_mnt_get_fsinfo(unsigned int xid, struct cifs_tcon *tcon,
  115. void __user *arg)
  116. {
  117. int rc = 0;
  118. struct smb_mnt_fs_info *fsinf;
  119. fsinf = kzalloc(sizeof(struct smb_mnt_fs_info), GFP_KERNEL);
  120. if (fsinf == NULL)
  121. return -ENOMEM;
  122. fsinf->version = 1;
  123. fsinf->protocol_id = tcon->ses->server->vals->protocol_id;
  124. fsinf->device_characteristics =
  125. le32_to_cpu(tcon->fsDevInfo.DeviceCharacteristics);
  126. fsinf->device_type = le32_to_cpu(tcon->fsDevInfo.DeviceType);
  127. fsinf->fs_attributes = le32_to_cpu(tcon->fsAttrInfo.Attributes);
  128. fsinf->max_path_component =
  129. le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
  130. fsinf->vol_serial_number = tcon->vol_serial_number;
  131. fsinf->vol_create_time = le64_to_cpu(tcon->vol_create_time);
  132. fsinf->share_flags = tcon->share_flags;
  133. fsinf->share_caps = le32_to_cpu(tcon->capabilities);
  134. fsinf->sector_flags = tcon->ss_flags;
  135. fsinf->optimal_sector_size = tcon->perf_sector_size;
  136. fsinf->max_bytes_chunk = tcon->max_bytes_chunk;
  137. fsinf->maximal_access = tcon->maximal_access;
  138. fsinf->cifs_posix_caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  139. if (copy_to_user(arg, fsinf, sizeof(struct smb_mnt_fs_info)))
  140. rc = -EFAULT;
  141. kfree(fsinf);
  142. return rc;
  143. }
  144. long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
  145. {
  146. struct inode *inode = file_inode(filep);
  147. struct smb3_key_debug_info pkey_inf;
  148. int rc = -ENOTTY; /* strange error - but the precedent */
  149. unsigned int xid;
  150. struct cifsFileInfo *pSMBFile = filep->private_data;
  151. struct cifs_tcon *tcon;
  152. struct tcon_link *tlink;
  153. struct cifs_sb_info *cifs_sb;
  154. __u64 ExtAttrBits = 0;
  155. __u64 caps;
  156. xid = get_xid();
  157. cifs_dbg(FYI, "cifs ioctl 0x%x\n", command);
  158. switch (command) {
  159. case FS_IOC_GETFLAGS:
  160. if (pSMBFile == NULL)
  161. break;
  162. tcon = tlink_tcon(pSMBFile->tlink);
  163. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  164. #ifdef CONFIG_CIFS_POSIX
  165. if (CIFS_UNIX_EXTATTR_CAP & caps) {
  166. __u64 ExtAttrMask = 0;
  167. rc = CIFSGetExtAttr(xid, tcon,
  168. pSMBFile->fid.netfid,
  169. &ExtAttrBits, &ExtAttrMask);
  170. if (rc == 0)
  171. rc = put_user(ExtAttrBits &
  172. FS_FL_USER_VISIBLE,
  173. (int __user *)arg);
  174. if (rc != EOPNOTSUPP)
  175. break;
  176. }
  177. #endif /* CONFIG_CIFS_POSIX */
  178. rc = 0;
  179. if (CIFS_I(inode)->cifsAttrs & ATTR_COMPRESSED) {
  180. /* add in the compressed bit */
  181. ExtAttrBits = FS_COMPR_FL;
  182. rc = put_user(ExtAttrBits & FS_FL_USER_VISIBLE,
  183. (int __user *)arg);
  184. }
  185. break;
  186. case FS_IOC_SETFLAGS:
  187. if (pSMBFile == NULL)
  188. break;
  189. tcon = tlink_tcon(pSMBFile->tlink);
  190. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  191. if (get_user(ExtAttrBits, (int __user *)arg)) {
  192. rc = -EFAULT;
  193. break;
  194. }
  195. /*
  196. * if (CIFS_UNIX_EXTATTR_CAP & caps)
  197. * rc = CIFSSetExtAttr(xid, tcon,
  198. * pSMBFile->fid.netfid,
  199. * extAttrBits,
  200. * &ExtAttrMask);
  201. * if (rc != EOPNOTSUPP)
  202. * break;
  203. */
  204. /* Currently only flag we can set is compressed flag */
  205. if ((ExtAttrBits & FS_COMPR_FL) == 0)
  206. break;
  207. /* Try to set compress flag */
  208. if (tcon->ses->server->ops->set_compression) {
  209. rc = tcon->ses->server->ops->set_compression(
  210. xid, tcon, pSMBFile);
  211. cifs_dbg(FYI, "set compress flag rc %d\n", rc);
  212. }
  213. break;
  214. case CIFS_IOC_COPYCHUNK_FILE:
  215. rc = cifs_ioctl_copychunk(xid, filep, arg);
  216. break;
  217. case CIFS_QUERY_INFO:
  218. rc = cifs_ioctl_query_info(xid, filep, arg);
  219. break;
  220. case CIFS_IOC_SET_INTEGRITY:
  221. if (pSMBFile == NULL)
  222. break;
  223. tcon = tlink_tcon(pSMBFile->tlink);
  224. if (tcon->ses->server->ops->set_integrity)
  225. rc = tcon->ses->server->ops->set_integrity(xid,
  226. tcon, pSMBFile);
  227. else
  228. rc = -EOPNOTSUPP;
  229. break;
  230. case CIFS_IOC_GET_MNT_INFO:
  231. if (pSMBFile == NULL)
  232. break;
  233. tcon = tlink_tcon(pSMBFile->tlink);
  234. rc = smb_mnt_get_fsinfo(xid, tcon, (void __user *)arg);
  235. break;
  236. case CIFS_ENUMERATE_SNAPSHOTS:
  237. if (pSMBFile == NULL)
  238. break;
  239. if (arg == 0) {
  240. rc = -EINVAL;
  241. goto cifs_ioc_exit;
  242. }
  243. tcon = tlink_tcon(pSMBFile->tlink);
  244. if (tcon->ses->server->ops->enum_snapshots)
  245. rc = tcon->ses->server->ops->enum_snapshots(xid, tcon,
  246. pSMBFile, (void __user *)arg);
  247. else
  248. rc = -EOPNOTSUPP;
  249. break;
  250. case CIFS_DUMP_KEY:
  251. if (pSMBFile == NULL)
  252. break;
  253. if (!capable(CAP_SYS_ADMIN)) {
  254. rc = -EACCES;
  255. break;
  256. }
  257. tcon = tlink_tcon(pSMBFile->tlink);
  258. if (!smb3_encryption_required(tcon)) {
  259. rc = -EOPNOTSUPP;
  260. break;
  261. }
  262. pkey_inf.cipher_type =
  263. le16_to_cpu(tcon->ses->server->cipher_type);
  264. pkey_inf.Suid = tcon->ses->Suid;
  265. memcpy(pkey_inf.auth_key, tcon->ses->auth_key.response,
  266. 16 /* SMB2_NTLMV2_SESSKEY_SIZE */);
  267. memcpy(pkey_inf.smb3decryptionkey,
  268. tcon->ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
  269. memcpy(pkey_inf.smb3encryptionkey,
  270. tcon->ses->smb3encryptionkey, SMB3_SIGN_KEY_SIZE);
  271. if (copy_to_user((void __user *)arg, &pkey_inf,
  272. sizeof(struct smb3_key_debug_info)))
  273. rc = -EFAULT;
  274. else
  275. rc = 0;
  276. break;
  277. case CIFS_IOC_NOTIFY:
  278. if (!S_ISDIR(inode->i_mode)) {
  279. /* Notify can only be done on directories */
  280. rc = -EOPNOTSUPP;
  281. break;
  282. }
  283. cifs_sb = CIFS_SB(inode->i_sb);
  284. tlink = cifs_sb_tlink(cifs_sb);
  285. if (IS_ERR(tlink)) {
  286. rc = PTR_ERR(tlink);
  287. break;
  288. }
  289. tcon = tlink_tcon(tlink);
  290. if (tcon && tcon->ses->server->ops->notify) {
  291. rc = tcon->ses->server->ops->notify(xid,
  292. filep, (void __user *)arg);
  293. cifs_dbg(FYI, "ioctl notify rc %d\n", rc);
  294. } else
  295. rc = -EOPNOTSUPP;
  296. cifs_put_tlink(tlink);
  297. break;
  298. default:
  299. cifs_dbg(FYI, "unsupported ioctl\n");
  300. break;
  301. }
  302. cifs_ioc_exit:
  303. free_xid(xid);
  304. return rc;
  305. }