smb2inode.c 7.2 KB

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