original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
7.5 KiB

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