ioctl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_copychunk(unsigned int xid, struct file *dst_file,
  36. unsigned long srcfd)
  37. {
  38. int rc;
  39. struct fd src_file;
  40. struct inode *src_inode;
  41. cifs_dbg(FYI, "ioctl copychunk range\n");
  42. /* the destination must be opened for writing */
  43. if (!(dst_file->f_mode & FMODE_WRITE)) {
  44. cifs_dbg(FYI, "file target not open for write\n");
  45. return -EINVAL;
  46. }
  47. /* check if target volume is readonly and take reference */
  48. rc = mnt_want_write_file(dst_file);
  49. if (rc) {
  50. cifs_dbg(FYI, "mnt_want_write failed with rc %d\n", rc);
  51. return rc;
  52. }
  53. src_file = fdget(srcfd);
  54. if (!src_file.file) {
  55. rc = -EBADF;
  56. goto out_drop_write;
  57. }
  58. if (src_file.file->f_op->unlocked_ioctl != cifs_ioctl) {
  59. rc = -EBADF;
  60. cifs_dbg(VFS, "src file seems to be from a different filesystem type\n");
  61. goto out_fput;
  62. }
  63. src_inode = file_inode(src_file.file);
  64. rc = -EINVAL;
  65. if (S_ISDIR(src_inode->i_mode))
  66. goto out_fput;
  67. rc = cifs_file_copychunk_range(xid, src_file.file, 0, dst_file, 0,
  68. src_inode->i_size, 0);
  69. if (rc > 0)
  70. rc = 0;
  71. out_fput:
  72. fdput(src_file);
  73. out_drop_write:
  74. mnt_drop_write_file(dst_file);
  75. return rc;
  76. }
  77. static long smb_mnt_get_fsinfo(unsigned int xid, struct cifs_tcon *tcon,
  78. void __user *arg)
  79. {
  80. int rc = 0;
  81. struct smb_mnt_fs_info *fsinf;
  82. fsinf = kzalloc(sizeof(struct smb_mnt_fs_info), GFP_KERNEL);
  83. if (fsinf == NULL)
  84. return -ENOMEM;
  85. fsinf->version = 1;
  86. fsinf->protocol_id = tcon->ses->server->vals->protocol_id;
  87. fsinf->device_characteristics =
  88. le32_to_cpu(tcon->fsDevInfo.DeviceCharacteristics);
  89. fsinf->device_type = le32_to_cpu(tcon->fsDevInfo.DeviceType);
  90. fsinf->fs_attributes = le32_to_cpu(tcon->fsAttrInfo.Attributes);
  91. fsinf->max_path_component =
  92. le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
  93. fsinf->vol_serial_number = tcon->vol_serial_number;
  94. fsinf->vol_create_time = le64_to_cpu(tcon->vol_create_time);
  95. fsinf->share_flags = tcon->share_flags;
  96. fsinf->share_caps = le32_to_cpu(tcon->capabilities);
  97. fsinf->sector_flags = tcon->ss_flags;
  98. fsinf->optimal_sector_size = tcon->perf_sector_size;
  99. fsinf->max_bytes_chunk = tcon->max_bytes_chunk;
  100. fsinf->maximal_access = tcon->maximal_access;
  101. fsinf->cifs_posix_caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  102. if (copy_to_user(arg, fsinf, sizeof(struct smb_mnt_fs_info)))
  103. rc = -EFAULT;
  104. kfree(fsinf);
  105. return rc;
  106. }
  107. long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
  108. {
  109. struct inode *inode = file_inode(filep);
  110. int rc = -ENOTTY; /* strange error - but the precedent */
  111. unsigned int xid;
  112. struct cifs_sb_info *cifs_sb;
  113. struct cifsFileInfo *pSMBFile = filep->private_data;
  114. struct cifs_tcon *tcon;
  115. __u64 ExtAttrBits = 0;
  116. __u64 caps;
  117. xid = get_xid();
  118. cifs_sb = CIFS_SB(inode->i_sb);
  119. cifs_dbg(FYI, "cifs ioctl 0x%x\n", command);
  120. switch (command) {
  121. case FS_IOC_GETFLAGS:
  122. if (pSMBFile == NULL)
  123. break;
  124. tcon = tlink_tcon(pSMBFile->tlink);
  125. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  126. #ifdef CONFIG_CIFS_POSIX
  127. if (CIFS_UNIX_EXTATTR_CAP & caps) {
  128. __u64 ExtAttrMask = 0;
  129. rc = CIFSGetExtAttr(xid, tcon,
  130. pSMBFile->fid.netfid,
  131. &ExtAttrBits, &ExtAttrMask);
  132. if (rc == 0)
  133. rc = put_user(ExtAttrBits &
  134. FS_FL_USER_VISIBLE,
  135. (int __user *)arg);
  136. if (rc != EOPNOTSUPP)
  137. break;
  138. }
  139. #endif /* CONFIG_CIFS_POSIX */
  140. rc = 0;
  141. if (CIFS_I(inode)->cifsAttrs & ATTR_COMPRESSED) {
  142. /* add in the compressed bit */
  143. ExtAttrBits = FS_COMPR_FL;
  144. rc = put_user(ExtAttrBits & FS_FL_USER_VISIBLE,
  145. (int __user *)arg);
  146. }
  147. break;
  148. case FS_IOC_SETFLAGS:
  149. if (pSMBFile == NULL)
  150. break;
  151. tcon = tlink_tcon(pSMBFile->tlink);
  152. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  153. if (get_user(ExtAttrBits, (int __user *)arg)) {
  154. rc = -EFAULT;
  155. break;
  156. }
  157. /*
  158. * if (CIFS_UNIX_EXTATTR_CAP & caps)
  159. * rc = CIFSSetExtAttr(xid, tcon,
  160. * pSMBFile->fid.netfid,
  161. * extAttrBits,
  162. * &ExtAttrMask);
  163. * if (rc != EOPNOTSUPP)
  164. * break;
  165. */
  166. /* Currently only flag we can set is compressed flag */
  167. if ((ExtAttrBits & FS_COMPR_FL) == 0)
  168. break;
  169. /* Try to set compress flag */
  170. if (tcon->ses->server->ops->set_compression) {
  171. rc = tcon->ses->server->ops->set_compression(
  172. xid, tcon, pSMBFile);
  173. cifs_dbg(FYI, "set compress flag rc %d\n", rc);
  174. }
  175. break;
  176. case CIFS_IOC_COPYCHUNK_FILE:
  177. rc = cifs_ioctl_copychunk(xid, filep, arg);
  178. break;
  179. case CIFS_IOC_SET_INTEGRITY:
  180. if (pSMBFile == NULL)
  181. break;
  182. tcon = tlink_tcon(pSMBFile->tlink);
  183. if (tcon->ses->server->ops->set_integrity)
  184. rc = tcon->ses->server->ops->set_integrity(xid,
  185. tcon, pSMBFile);
  186. else
  187. rc = -EOPNOTSUPP;
  188. break;
  189. case CIFS_IOC_GET_MNT_INFO:
  190. if (pSMBFile == NULL)
  191. break;
  192. tcon = tlink_tcon(pSMBFile->tlink);
  193. rc = smb_mnt_get_fsinfo(xid, tcon, (void __user *)arg);
  194. break;
  195. case CIFS_ENUMERATE_SNAPSHOTS:
  196. if (pSMBFile == NULL)
  197. break;
  198. if (arg == 0) {
  199. rc = -EINVAL;
  200. goto cifs_ioc_exit;
  201. }
  202. tcon = tlink_tcon(pSMBFile->tlink);
  203. if (tcon->ses->server->ops->enum_snapshots)
  204. rc = tcon->ses->server->ops->enum_snapshots(xid, tcon,
  205. pSMBFile, (void __user *)arg);
  206. else
  207. rc = -EOPNOTSUPP;
  208. break;
  209. default:
  210. cifs_dbg(FYI, "unsupported ioctl\n");
  211. break;
  212. }
  213. cifs_ioc_exit:
  214. free_xid(xid);
  215. return rc;
  216. }