smb2inode.c 7.8 KB

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