ioctl.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = src_file.file->f_dentry->d_inode;
  79. /*
  80. * Note: cifs case is easier than btrfs since server responsible for
  81. * checks for proper open modes and file type and if it wants
  82. * server could even support copy of range where source = target
  83. */
  84. /* so we do not deadlock racing two ioctls on same files */
  85. if (target_inode < src_inode) {
  86. mutex_lock_nested(&target_inode->i_mutex, I_MUTEX_PARENT);
  87. mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD);
  88. } else {
  89. mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT);
  90. mutex_lock_nested(&target_inode->i_mutex, I_MUTEX_CHILD);
  91. }
  92. /* determine range to clone */
  93. rc = -EINVAL;
  94. if (off + len > src_inode->i_size || off + len < off)
  95. goto out_unlock;
  96. if (len == 0)
  97. len = src_inode->i_size - off;
  98. cifs_dbg(FYI, "about to flush pages\n");
  99. /* should we flush first and last page first */
  100. truncate_inode_pages_range(&target_inode->i_data, destoff,
  101. PAGE_CACHE_ALIGN(destoff + len)-1);
  102. if (target_tcon->ses->server->ops->clone_range)
  103. rc = target_tcon->ses->server->ops->clone_range(xid,
  104. smb_file_src, smb_file_target, off, len, destoff);
  105. /* force revalidate of size and timestamps of target file now
  106. that target is updated on the server */
  107. CIFS_I(target_inode)->time = 0;
  108. out_unlock:
  109. /* although unlocking in the reverse order from locking is not
  110. strictly necessary here it is a little cleaner to be consistent */
  111. if (target_inode < src_inode) {
  112. mutex_unlock(&src_inode->i_mutex);
  113. mutex_unlock(&target_inode->i_mutex);
  114. } else {
  115. mutex_unlock(&target_inode->i_mutex);
  116. mutex_unlock(&src_inode->i_mutex);
  117. }
  118. out_fput:
  119. fdput(src_file);
  120. out_drop_write:
  121. mnt_drop_write_file(dst_file);
  122. return rc;
  123. }
  124. long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
  125. {
  126. struct inode *inode = file_inode(filep);
  127. int rc = -ENOTTY; /* strange error - but the precedent */
  128. unsigned int xid;
  129. struct cifs_sb_info *cifs_sb;
  130. struct cifsFileInfo *pSMBFile = filep->private_data;
  131. struct cifs_tcon *tcon;
  132. __u64 ExtAttrBits = 0;
  133. __u64 caps;
  134. xid = get_xid();
  135. cifs_dbg(FYI, "ioctl file %p cmd %u arg %lu\n", filep, command, arg);
  136. cifs_sb = CIFS_SB(inode->i_sb);
  137. switch (command) {
  138. case FS_IOC_GETFLAGS:
  139. if (pSMBFile == NULL)
  140. break;
  141. tcon = tlink_tcon(pSMBFile->tlink);
  142. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  143. #ifdef CONFIG_CIFS_POSIX
  144. if (CIFS_UNIX_EXTATTR_CAP & caps) {
  145. __u64 ExtAttrMask = 0;
  146. rc = CIFSGetExtAttr(xid, tcon,
  147. pSMBFile->fid.netfid,
  148. &ExtAttrBits, &ExtAttrMask);
  149. if (rc == 0)
  150. rc = put_user(ExtAttrBits &
  151. FS_FL_USER_VISIBLE,
  152. (int __user *)arg);
  153. if (rc != EOPNOTSUPP)
  154. break;
  155. }
  156. #endif /* CONFIG_CIFS_POSIX */
  157. rc = 0;
  158. if (CIFS_I(inode)->cifsAttrs & ATTR_COMPRESSED) {
  159. /* add in the compressed bit */
  160. ExtAttrBits = FS_COMPR_FL;
  161. rc = put_user(ExtAttrBits & FS_FL_USER_VISIBLE,
  162. (int __user *)arg);
  163. }
  164. break;
  165. case FS_IOC_SETFLAGS:
  166. if (pSMBFile == NULL)
  167. break;
  168. tcon = tlink_tcon(pSMBFile->tlink);
  169. caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
  170. if (get_user(ExtAttrBits, (int __user *)arg)) {
  171. rc = -EFAULT;
  172. break;
  173. }
  174. /*
  175. * if (CIFS_UNIX_EXTATTR_CAP & caps)
  176. * rc = CIFSSetExtAttr(xid, tcon,
  177. * pSMBFile->fid.netfid,
  178. * extAttrBits,
  179. * &ExtAttrMask);
  180. * if (rc != EOPNOTSUPP)
  181. * break;
  182. */
  183. /* Currently only flag we can set is compressed flag */
  184. if ((ExtAttrBits & FS_COMPR_FL) == 0)
  185. break;
  186. /* Try to set compress flag */
  187. if (tcon->ses->server->ops->set_compression) {
  188. rc = tcon->ses->server->ops->set_compression(
  189. xid, tcon, pSMBFile);
  190. cifs_dbg(FYI, "set compress flag rc %d\n", rc);
  191. }
  192. break;
  193. case CIFS_IOC_COPYCHUNK_FILE:
  194. rc = cifs_ioctl_clone(xid, filep, arg, 0, 0, 0);
  195. break;
  196. default:
  197. cifs_dbg(FYI, "unsupported ioctl\n");
  198. break;
  199. }
  200. free_xid(xid);
  201. return rc;
  202. }