smb2inode.c 8.3 KB

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