ioctl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #define CIFS_IOCTL_MAGIC 0xCF
  34. #define CIFS_IOC_COPYCHUNK_FILE _IOW(CIFS_IOCTL_MAGIC, 3, int)
  35. static long cifs_ioctl_clone(unsigned int xid, struct file *dst_file,
  36. unsigned long srcfd, u64 off, u64 len, u64 destoff)
  37. {
  38. int rc;
  39. struct cifsFileInfo *smb_file_target = dst_file->private_data;
  40. struct inode *target_inode = file_inode(dst_file);
  41. struct cifs_tcon *target_tcon;
  42. struct fd src_file;
  43. struct cifsFileInfo *smb_file_src;
  44. struct inode *src_inode;
  45. struct cifs_tcon *src_tcon;
  46. cifs_dbg(FYI, "ioctl clone range\n");
  47. /* the destination must be opened for writing */
  48. if (!(dst_file->f_mode & FMODE_WRITE)) {
  49. cifs_dbg(FYI, "file target not open for write\n");
  50. return -EINVAL;
  51. }
  52. /* check if target volume is readonly and take reference */
  53. rc = mnt_want_write_file(dst_file);
  54. if (rc) {
  55. cifs_dbg(FYI, "mnt_want_write failed with rc %d\n", rc);
  56. return rc;
  57. }
  58. src_file = fdget(srcfd);
  59. if (!src_file.file) {
  60. rc = -EBADF;
  61. goto out_drop_write;
  62. }
  63. if ((!src_file.file->private_data) || (!dst_file->private_data)) {
  64. rc = -EBADF;
  65. cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
  66. goto out_fput;
  67. }
  68. rc = -EXDEV;
  69. smb_file_target = dst_file->private_data;
  70. smb_file_src = src_file.file->private_data;
  71. src_tcon = tlink_tcon(smb_file_src->tlink);
  72. target_tcon = tlink_tcon(smb_file_target->tlink);
  73. /* check if source and target are on same tree connection */
  74. if (src_tcon != target_tcon) {
  75. cifs_dbg(VFS, "file copy src and target on different volume\n");
  76. goto out_fput;
  77. }
  78. src_inode = file_inode(src_file.file);
  79. rc = -EINVAL;
  80. if (S_ISDIR(src_inode->i_mode))
  81. goto out_fput;
  82. /*
  83. * Note: cifs case is easier than btrfs since server responsible for
  84. * checks for proper open modes and file type and if it wants
  85. * server could even support copy of range where source = target
  86. */
  87. lock_two_nondirectories(target_inode, src_inode);
  88. /* determine range to clone */
  89. rc = -EINVAL;
  90. if (off + len > src_inode->i_size || off + len < off)
  91. goto out_unlock;
  92. if (len == 0)
  93. len = src_inode->i_size - off;
  94. cifs_dbg(FYI, "about to flush pages\n");
  95. /* should we flush first and last page first */
  96. truncate_inode_pages_range(&target_inode->i_data, destoff,
  97. PAGE_CACHE_ALIGN(destoff + len)-1);
  98. if (target_tcon->ses->server->ops->clone_range)
  99. rc = target_tcon->ses->server->ops->clone_range(xid,
  100. smb_file_src, smb_file_target, off, len, destoff);
  101. /* force revalidate of size and timestamps of target file now
  102. that target is updated on the server */
  103. CIFS_I(target_inode)->time = 0;
  104. out_unlock:
  105. /* although unlocking in the reverse order from locking is not
  106. strictly necessary here it is a little cleaner to be consistent */
  107. unlock_two_nondirectories(src_inode, target_inode);
  108. out_fput:
  109. fdput(src_file);
  110. out_drop_write:
  111. mnt_drop_write_file(dst_file);
  112. return rc;
  113. }
  114. long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
  115. {
  116. struct inode *inode = file_inode(filep);
  117. int rc = -ENOTTY; /* strange error - but the precedent */
  118. unsigned int xid;
  119. struct cifs_sb_info *cifs_sb;
  120. struct cifsFileInfo *pSMBFile = filep->private_data;
  121. struct cifs_tcon *tcon;
  122. __u64 ExtAttrBits = 0;
  123. __u64 caps;
  124. xid = get_xid();
  125. cifs_dbg(FYI, "ioctl file %p cmd %u arg %lu\n", filep, command, arg);
  126. cifs_sb = CIFS_SB(inode->i_sb);
  127. switch (command) {
  128. case FS_IOC_GETFLAGS:
  129. if (pSMBFile == NULL)
  130. break;
  131. tcon = tlink_tcon(pSMBFile->tlink);
  132. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  133. #ifdef CONFIG_CIFS_POSIX
  134. if (CIFS_UNIX_EXTATTR_CAP & caps) {
  135. __u64 ExtAttrMask = 0;
  136. rc = CIFSGetExtAttr(xid, tcon,
  137. pSMBFile->fid.netfid,
  138. &ExtAttrBits, &ExtAttrMask);
  139. if (rc == 0)
  140. rc = put_user(ExtAttrBits &
  141. FS_FL_USER_VISIBLE,
  142. (int __user *)arg);
  143. if (rc != EOPNOTSUPP)
  144. break;
  145. }
  146. #endif /* CONFIG_CIFS_POSIX */
  147. rc = 0;
  148. if (CIFS_I(inode)->cifsAttrs & ATTR_COMPRESSED) {
  149. /* add in the compressed bit */
  150. ExtAttrBits = FS_COMPR_FL;
  151. rc = put_user(ExtAttrBits & FS_FL_USER_VISIBLE,
  152. (int __user *)arg);
  153. }
  154. break;
  155. case FS_IOC_SETFLAGS:
  156. if (pSMBFile == NULL)
  157. break;
  158. tcon = tlink_tcon(pSMBFile->tlink);
  159. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  160. if (get_user(ExtAttrBits, (int __user *)arg)) {
  161. rc = -EFAULT;
  162. break;
  163. }
  164. /*
  165. * if (CIFS_UNIX_EXTATTR_CAP & caps)
  166. * rc = CIFSSetExtAttr(xid, tcon,
  167. * pSMBFile->fid.netfid,
  168. * extAttrBits,
  169. * &ExtAttrMask);
  170. * if (rc != EOPNOTSUPP)
  171. * break;
  172. */
  173. /* Currently only flag we can set is compressed flag */
  174. if ((ExtAttrBits & FS_COMPR_FL) == 0)
  175. break;
  176. /* Try to set compress flag */
  177. if (tcon->ses->server->ops->set_compression) {
  178. rc = tcon->ses->server->ops->set_compression(
  179. xid, tcon, pSMBFile);
  180. cifs_dbg(FYI, "set compress flag rc %d\n", rc);
  181. }
  182. break;
  183. case CIFS_IOC_COPYCHUNK_FILE:
  184. rc = cifs_ioctl_clone(xid, filep, arg, 0, 0, 0);
  185. break;
  186. default:
  187. cifs_dbg(FYI, "unsupported ioctl\n");
  188. break;
  189. }
  190. free_xid(xid);
  191. return rc;
  192. }