smb2inode.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  50. if (!utf16_path)
  51. return -ENOMEM;
  52. rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
  53. desired_access, create_disposition, file_attributes,
  54. create_options, NULL);
  55. if (rc) {
  56. kfree(utf16_path);
  57. return rc;
  58. }
  59. switch (command) {
  60. case SMB2_OP_DELETE:
  61. break;
  62. case SMB2_OP_QUERY_INFO:
  63. tmprc = SMB2_query_info(xid, tcon, persistent_fid,
  64. volatile_fid,
  65. (struct smb2_file_all_info *)data);
  66. break;
  67. case SMB2_OP_MKDIR:
  68. /*
  69. * Directories are created through parameters in the
  70. * SMB2_open() call.
  71. */
  72. break;
  73. case SMB2_OP_RENAME:
  74. tmprc = SMB2_rename(xid, tcon, persistent_fid, volatile_fid,
  75. (__le16 *)data);
  76. break;
  77. case SMB2_OP_HARDLINK:
  78. tmprc = SMB2_set_hardlink(xid, tcon, persistent_fid,
  79. volatile_fid, (__le16 *)data);
  80. break;
  81. default:
  82. cERROR(1, "Invalid command");
  83. break;
  84. }
  85. rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
  86. if (tmprc)
  87. rc = tmprc;
  88. kfree(utf16_path);
  89. return rc;
  90. }
  91. void
  92. move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
  93. {
  94. memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
  95. dst->CurrentByteOffset = src->CurrentByteOffset;
  96. dst->Mode = src->Mode;
  97. dst->AlignmentRequirement = src->AlignmentRequirement;
  98. dst->IndexNumber1 = 0; /* we don't use it */
  99. }
  100. int
  101. smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
  102. struct cifs_sb_info *cifs_sb, const char *full_path,
  103. FILE_ALL_INFO *data, bool *adjust_tz)
  104. {
  105. int rc;
  106. struct smb2_file_all_info *smb2_data;
  107. *adjust_tz = false;
  108. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  109. GFP_KERNEL);
  110. if (smb2_data == NULL)
  111. return -ENOMEM;
  112. rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
  113. FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0,
  114. smb2_data, SMB2_OP_QUERY_INFO);
  115. if (rc)
  116. goto out;
  117. move_smb2_info_to_cifs(data, smb2_data);
  118. out:
  119. kfree(smb2_data);
  120. return rc;
  121. }
  122. int
  123. smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  124. struct cifs_sb_info *cifs_sb)
  125. {
  126. return smb2_open_op_close(xid, tcon, cifs_sb, name,
  127. FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
  128. CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
  129. }
  130. void
  131. smb2_mkdir_setinfo(struct inode *inode, const char *name,
  132. struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
  133. const unsigned int xid)
  134. {
  135. FILE_BASIC_INFO data;
  136. struct cifsInodeInfo *cifs_i;
  137. u32 dosattrs;
  138. int tmprc;
  139. memset(&data, 0, sizeof(data));
  140. cifs_i = CIFS_I(inode);
  141. dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
  142. data.Attributes = cpu_to_le32(dosattrs);
  143. tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
  144. FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
  145. CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
  146. if (tmprc == 0)
  147. cifs_i->cifsAttrs = dosattrs;
  148. }
  149. int
  150. smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  151. struct cifs_sb_info *cifs_sb)
  152. {
  153. return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
  154. 0, CREATE_NOT_FILE | CREATE_DELETE_ON_CLOSE,
  155. NULL, SMB2_OP_DELETE);
  156. }
  157. int
  158. smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  159. struct cifs_sb_info *cifs_sb)
  160. {
  161. return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
  162. 0, CREATE_DELETE_ON_CLOSE, NULL,
  163. SMB2_OP_DELETE);
  164. }
  165. static int
  166. smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
  167. const char *from_name, const char *to_name,
  168. struct cifs_sb_info *cifs_sb, __u32 access, int command)
  169. {
  170. __le16 *smb2_to_name = NULL;
  171. int rc;
  172. smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
  173. if (smb2_to_name == NULL) {
  174. rc = -ENOMEM;
  175. goto smb2_rename_path;
  176. }
  177. rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
  178. FILE_OPEN, 0, 0, smb2_to_name, command);
  179. smb2_rename_path:
  180. kfree(smb2_to_name);
  181. return rc;
  182. }
  183. int
  184. smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
  185. const char *from_name, const char *to_name,
  186. struct cifs_sb_info *cifs_sb)
  187. {
  188. return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
  189. DELETE, SMB2_OP_RENAME);
  190. }
  191. int
  192. smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  193. const char *from_name, const char *to_name,
  194. struct cifs_sb_info *cifs_sb)
  195. {
  196. return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
  197. FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
  198. }