smb2inode.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * fs/cifs/smb2inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002, 2011
  5. * Etersoft, 2012
  6. * Author(s): Pavel Shilovsky (pshilovsky@samba.org),
  7. * 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/stat.h>
  25. #include <linux/slab.h>
  26. #include <linux/pagemap.h>
  27. #include <asm/div64.h>
  28. #include "cifsfs.h"
  29. #include "cifspdu.h"
  30. #include "cifsglob.h"
  31. #include "cifsproto.h"
  32. #include "cifs_debug.h"
  33. #include "cifs_fs_sb.h"
  34. #include "cifs_unicode.h"
  35. #include "fscache.h"
  36. #include "smb2glob.h"
  37. #include "smb2pdu.h"
  38. #include "smb2proto.h"
  39. static int
  40. smb2_open_op_close(const unsigned int xid, struct cifs_tcon *tcon,
  41. struct cifs_sb_info *cifs_sb, const char *full_path,
  42. __u32 desired_access, __u32 create_disposition,
  43. __u32 file_attributes, __u32 create_options,
  44. void *data, int command)
  45. {
  46. int rc, tmprc = 0;
  47. u64 persistent_fid, volatile_fid;
  48. __le16 *utf16_path;
  49. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  50. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  51. if (!utf16_path)
  52. return -ENOMEM;
  53. rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
  54. desired_access, create_disposition, file_attributes,
  55. create_options, &oplock, NULL);
  56. if (rc) {
  57. kfree(utf16_path);
  58. return rc;
  59. }
  60. switch (command) {
  61. case SMB2_OP_DELETE:
  62. break;
  63. case SMB2_OP_QUERY_INFO:
  64. tmprc = SMB2_query_info(xid, tcon, persistent_fid,
  65. volatile_fid,
  66. (struct smb2_file_all_info *)data);
  67. break;
  68. case SMB2_OP_MKDIR:
  69. /*
  70. * Directories are created through parameters in the
  71. * SMB2_open() call.
  72. */
  73. break;
  74. case SMB2_OP_RENAME:
  75. tmprc = SMB2_rename(xid, tcon, persistent_fid, volatile_fid,
  76. (__le16 *)data);
  77. break;
  78. case SMB2_OP_HARDLINK:
  79. tmprc = SMB2_set_hardlink(xid, tcon, persistent_fid,
  80. volatile_fid, (__le16 *)data);
  81. break;
  82. case SMB2_OP_SET_EOF:
  83. tmprc = SMB2_set_eof(xid, tcon, persistent_fid, volatile_fid,
  84. current->tgid, (__le64 *)data);
  85. break;
  86. case SMB2_OP_SET_INFO:
  87. tmprc = SMB2_set_info(xid, tcon, persistent_fid, volatile_fid,
  88. (FILE_BASIC_INFO *)data);
  89. break;
  90. default:
  91. cERROR(1, "Invalid command");
  92. break;
  93. }
  94. rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
  95. if (tmprc)
  96. rc = tmprc;
  97. kfree(utf16_path);
  98. return rc;
  99. }
  100. void
  101. move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
  102. {
  103. memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
  104. dst->CurrentByteOffset = src->CurrentByteOffset;
  105. dst->Mode = src->Mode;
  106. dst->AlignmentRequirement = src->AlignmentRequirement;
  107. dst->IndexNumber1 = 0; /* we don't use it */
  108. }
  109. int
  110. smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
  111. struct cifs_sb_info *cifs_sb, const char *full_path,
  112. FILE_ALL_INFO *data, bool *adjust_tz)
  113. {
  114. int rc;
  115. struct smb2_file_all_info *smb2_data;
  116. *adjust_tz = false;
  117. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  118. GFP_KERNEL);
  119. if (smb2_data == NULL)
  120. return -ENOMEM;
  121. rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
  122. FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0,
  123. smb2_data, SMB2_OP_QUERY_INFO);
  124. if (rc)
  125. goto out;
  126. move_smb2_info_to_cifs(data, smb2_data);
  127. out:
  128. kfree(smb2_data);
  129. return rc;
  130. }
  131. int
  132. smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  133. struct cifs_sb_info *cifs_sb)
  134. {
  135. return smb2_open_op_close(xid, tcon, cifs_sb, name,
  136. FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
  137. CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
  138. }
  139. void
  140. smb2_mkdir_setinfo(struct inode *inode, const char *name,
  141. struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
  142. const unsigned int xid)
  143. {
  144. FILE_BASIC_INFO data;
  145. struct cifsInodeInfo *cifs_i;
  146. u32 dosattrs;
  147. int tmprc;
  148. memset(&data, 0, sizeof(data));
  149. cifs_i = CIFS_I(inode);
  150. dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
  151. data.Attributes = cpu_to_le32(dosattrs);
  152. tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
  153. FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
  154. CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
  155. if (tmprc == 0)
  156. cifs_i->cifsAttrs = dosattrs;
  157. }
  158. int
  159. smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  160. struct cifs_sb_info *cifs_sb)
  161. {
  162. return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
  163. 0, CREATE_NOT_FILE | CREATE_DELETE_ON_CLOSE,
  164. NULL, SMB2_OP_DELETE);
  165. }
  166. int
  167. smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  168. struct cifs_sb_info *cifs_sb)
  169. {
  170. return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
  171. 0, CREATE_DELETE_ON_CLOSE, NULL,
  172. SMB2_OP_DELETE);
  173. }
  174. static int
  175. smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
  176. const char *from_name, const char *to_name,
  177. struct cifs_sb_info *cifs_sb, __u32 access, int command)
  178. {
  179. __le16 *smb2_to_name = NULL;
  180. int rc;
  181. smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
  182. if (smb2_to_name == NULL) {
  183. rc = -ENOMEM;
  184. goto smb2_rename_path;
  185. }
  186. rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
  187. FILE_OPEN, 0, 0, smb2_to_name, command);
  188. smb2_rename_path:
  189. kfree(smb2_to_name);
  190. return rc;
  191. }
  192. int
  193. smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
  194. const char *from_name, const char *to_name,
  195. struct cifs_sb_info *cifs_sb)
  196. {
  197. return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
  198. DELETE, SMB2_OP_RENAME);
  199. }
  200. int
  201. smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  202. const char *from_name, const char *to_name,
  203. struct cifs_sb_info *cifs_sb)
  204. {
  205. return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
  206. FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
  207. }
  208. int
  209. smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
  210. const char *full_path, __u64 size,
  211. struct cifs_sb_info *cifs_sb, bool set_alloc)
  212. {
  213. __le64 eof = cpu_to_le64(size);
  214. return smb2_open_op_close(xid, tcon, cifs_sb, full_path,
  215. FILE_WRITE_DATA, FILE_OPEN, 0, 0, &eof,
  216. SMB2_OP_SET_EOF);
  217. }
  218. int
  219. smb2_set_file_info(struct inode *inode, const char *full_path,
  220. FILE_BASIC_INFO *buf, const unsigned int xid)
  221. {
  222. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  223. struct tcon_link *tlink;
  224. int rc;
  225. tlink = cifs_sb_tlink(cifs_sb);
  226. if (IS_ERR(tlink))
  227. return PTR_ERR(tlink);
  228. rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path,
  229. FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, 0, buf,
  230. SMB2_OP_SET_INFO);
  231. cifs_put_tlink(tlink);
  232. return rc;
  233. }