ioctl.c 8.2 KB

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