smb2file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002, 2011
  5. * Author(s): Steve French ([email protected]),
  6. * Pavel Shilovsky (([email protected]) 2012
  7. *
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/stat.h>
  11. #include <linux/slab.h>
  12. #include <linux/pagemap.h>
  13. #include <asm/div64.h>
  14. #include "cifsfs.h"
  15. #include "cifspdu.h"
  16. #include "cifsglob.h"
  17. #include "cifsproto.h"
  18. #include "cifs_debug.h"
  19. #include "cifs_fs_sb.h"
  20. #include "cifs_unicode.h"
  21. #include "fscache.h"
  22. #include "smb2proto.h"
  23. #include "smb2status.h"
  24. static struct smb2_symlink_err_rsp *symlink_data(const struct kvec *iov)
  25. {
  26. struct smb2_err_rsp *err = iov->iov_base;
  27. struct smb2_symlink_err_rsp *sym = ERR_PTR(-EINVAL);
  28. u32 len;
  29. if (err->ErrorContextCount) {
  30. struct smb2_error_context_rsp *p, *end;
  31. len = (u32)err->ErrorContextCount * (offsetof(struct smb2_error_context_rsp,
  32. ErrorContextData) +
  33. sizeof(struct smb2_symlink_err_rsp));
  34. if (le32_to_cpu(err->ByteCount) < len || iov->iov_len < len + sizeof(*err))
  35. return ERR_PTR(-EINVAL);
  36. p = (struct smb2_error_context_rsp *)err->ErrorData;
  37. end = (struct smb2_error_context_rsp *)((u8 *)err + iov->iov_len);
  38. do {
  39. if (le32_to_cpu(p->ErrorId) == SMB2_ERROR_ID_DEFAULT) {
  40. sym = (struct smb2_symlink_err_rsp *)&p->ErrorContextData;
  41. break;
  42. }
  43. cifs_dbg(FYI, "%s: skipping unhandled error context: 0x%x\n",
  44. __func__, le32_to_cpu(p->ErrorId));
  45. len = ALIGN(le32_to_cpu(p->ErrorDataLength), 8);
  46. p = (struct smb2_error_context_rsp *)((u8 *)&p->ErrorContextData + len);
  47. } while (p < end);
  48. } else if (le32_to_cpu(err->ByteCount) >= sizeof(*sym) &&
  49. iov->iov_len >= SMB2_SYMLINK_STRUCT_SIZE) {
  50. sym = (struct smb2_symlink_err_rsp *)err->ErrorData;
  51. }
  52. if (!IS_ERR(sym) && (le32_to_cpu(sym->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
  53. le32_to_cpu(sym->ReparseTag) != IO_REPARSE_TAG_SYMLINK))
  54. sym = ERR_PTR(-EINVAL);
  55. return sym;
  56. }
  57. int smb2_parse_symlink_response(struct cifs_sb_info *cifs_sb, const struct kvec *iov, char **path)
  58. {
  59. struct smb2_symlink_err_rsp *sym;
  60. unsigned int sub_offs, sub_len;
  61. unsigned int print_offs, print_len;
  62. char *s;
  63. if (!cifs_sb || !iov || !iov->iov_base || !iov->iov_len || !path)
  64. return -EINVAL;
  65. sym = symlink_data(iov);
  66. if (IS_ERR(sym))
  67. return PTR_ERR(sym);
  68. sub_len = le16_to_cpu(sym->SubstituteNameLength);
  69. sub_offs = le16_to_cpu(sym->SubstituteNameOffset);
  70. print_len = le16_to_cpu(sym->PrintNameLength);
  71. print_offs = le16_to_cpu(sym->PrintNameOffset);
  72. if (iov->iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offs + sub_len ||
  73. iov->iov_len < SMB2_SYMLINK_STRUCT_SIZE + print_offs + print_len)
  74. return -EINVAL;
  75. s = cifs_strndup_from_utf16((char *)sym->PathBuffer + sub_offs, sub_len, true,
  76. cifs_sb->local_nls);
  77. if (!s)
  78. return -ENOMEM;
  79. convert_delimiter(s, '/');
  80. cifs_dbg(FYI, "%s: symlink target: %s\n", __func__, s);
  81. *path = s;
  82. return 0;
  83. }
  84. int smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms, __u32 *oplock, void *buf)
  85. {
  86. int rc;
  87. __le16 *smb2_path;
  88. __u8 smb2_oplock;
  89. struct cifs_open_info_data *data = buf;
  90. struct smb2_file_all_info file_info = {};
  91. struct smb2_file_all_info *smb2_data = data ? &file_info : NULL;
  92. struct kvec err_iov = {};
  93. int err_buftype = CIFS_NO_BUFFER;
  94. struct cifs_fid *fid = oparms->fid;
  95. struct network_resiliency_req nr_ioctl_req;
  96. smb2_path = cifs_convert_path_to_utf16(oparms->path, oparms->cifs_sb);
  97. if (smb2_path == NULL)
  98. return -ENOMEM;
  99. oparms->desired_access |= FILE_READ_ATTRIBUTES;
  100. smb2_oplock = SMB2_OPLOCK_LEVEL_BATCH;
  101. rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL, &err_iov,
  102. &err_buftype);
  103. if (rc && data) {
  104. struct smb2_hdr *hdr = err_iov.iov_base;
  105. if (unlikely(!err_iov.iov_base || err_buftype == CIFS_NO_BUFFER))
  106. goto out;
  107. if (hdr->Status == STATUS_STOPPED_ON_SYMLINK) {
  108. rc = smb2_parse_symlink_response(oparms->cifs_sb, &err_iov,
  109. &data->symlink_target);
  110. if (!rc) {
  111. memset(smb2_data, 0, sizeof(*smb2_data));
  112. oparms->create_options |= OPEN_REPARSE_POINT;
  113. rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data,
  114. NULL, NULL, NULL);
  115. oparms->create_options &= ~OPEN_REPARSE_POINT;
  116. }
  117. }
  118. }
  119. if (rc)
  120. goto out;
  121. if (oparms->tcon->use_resilient) {
  122. /* default timeout is 0, servers pick default (120 seconds) */
  123. nr_ioctl_req.Timeout =
  124. cpu_to_le32(oparms->tcon->handle_timeout);
  125. nr_ioctl_req.Reserved = 0;
  126. rc = SMB2_ioctl(xid, oparms->tcon, fid->persistent_fid,
  127. fid->volatile_fid, FSCTL_LMR_REQUEST_RESILIENCY,
  128. (char *)&nr_ioctl_req, sizeof(nr_ioctl_req),
  129. CIFSMaxBufSize, NULL, NULL /* no return info */);
  130. if (rc == -EOPNOTSUPP) {
  131. cifs_dbg(VFS,
  132. "resiliency not supported by server, disabling\n");
  133. oparms->tcon->use_resilient = false;
  134. } else if (rc)
  135. cifs_dbg(FYI, "error %d setting resiliency\n", rc);
  136. rc = 0;
  137. }
  138. if (smb2_data) {
  139. /* if open response does not have IndexNumber field - get it */
  140. if (smb2_data->IndexNumber == 0) {
  141. rc = SMB2_get_srv_num(xid, oparms->tcon,
  142. fid->persistent_fid,
  143. fid->volatile_fid,
  144. &smb2_data->IndexNumber);
  145. if (rc) {
  146. /*
  147. * let get_inode_info disable server inode
  148. * numbers
  149. */
  150. smb2_data->IndexNumber = 0;
  151. rc = 0;
  152. }
  153. }
  154. memcpy(&data->fi, smb2_data, sizeof(data->fi));
  155. }
  156. *oplock = smb2_oplock;
  157. out:
  158. free_rsp_buf(err_buftype, err_iov.iov_base);
  159. kfree(smb2_path);
  160. return rc;
  161. }
  162. int
  163. smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
  164. const unsigned int xid)
  165. {
  166. int rc = 0, stored_rc;
  167. unsigned int max_num, num = 0, max_buf;
  168. struct smb2_lock_element *buf, *cur;
  169. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  170. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  171. struct cifsLockInfo *li, *tmp;
  172. __u64 length = 1 + flock->fl_end - flock->fl_start;
  173. struct list_head tmp_llist;
  174. INIT_LIST_HEAD(&tmp_llist);
  175. /*
  176. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  177. * and check it before using.
  178. */
  179. max_buf = tcon->ses->server->maxBuf;
  180. if (max_buf < sizeof(struct smb2_lock_element))
  181. return -EINVAL;
  182. BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
  183. max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
  184. max_num = max_buf / sizeof(struct smb2_lock_element);
  185. buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
  186. if (!buf)
  187. return -ENOMEM;
  188. cur = buf;
  189. cifs_down_write(&cinode->lock_sem);
  190. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  191. if (flock->fl_start > li->offset ||
  192. (flock->fl_start + length) <
  193. (li->offset + li->length))
  194. continue;
  195. if (current->tgid != li->pid)
  196. /*
  197. * flock and OFD lock are associated with an open
  198. * file description, not the process.
  199. */
  200. if (!(flock->fl_flags & (FL_FLOCK | FL_OFDLCK)))
  201. continue;
  202. if (cinode->can_cache_brlcks) {
  203. /*
  204. * We can cache brlock requests - simply remove a lock
  205. * from the file's list.
  206. */
  207. list_del(&li->llist);
  208. cifs_del_lock_waiters(li);
  209. kfree(li);
  210. continue;
  211. }
  212. cur->Length = cpu_to_le64(li->length);
  213. cur->Offset = cpu_to_le64(li->offset);
  214. cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);
  215. /*
  216. * We need to save a lock here to let us add it again to the
  217. * file's list if the unlock range request fails on the server.
  218. */
  219. list_move(&li->llist, &tmp_llist);
  220. if (++num == max_num) {
  221. stored_rc = smb2_lockv(xid, tcon,
  222. cfile->fid.persistent_fid,
  223. cfile->fid.volatile_fid,
  224. current->tgid, num, buf);
  225. if (stored_rc) {
  226. /*
  227. * We failed on the unlock range request - add
  228. * all locks from the tmp list to the head of
  229. * the file's list.
  230. */
  231. cifs_move_llist(&tmp_llist,
  232. &cfile->llist->locks);
  233. rc = stored_rc;
  234. } else
  235. /*
  236. * The unlock range request succeed - free the
  237. * tmp list.
  238. */
  239. cifs_free_llist(&tmp_llist);
  240. cur = buf;
  241. num = 0;
  242. } else
  243. cur++;
  244. }
  245. if (num) {
  246. stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,
  247. cfile->fid.volatile_fid, current->tgid,
  248. num, buf);
  249. if (stored_rc) {
  250. cifs_move_llist(&tmp_llist, &cfile->llist->locks);
  251. rc = stored_rc;
  252. } else
  253. cifs_free_llist(&tmp_llist);
  254. }
  255. up_write(&cinode->lock_sem);
  256. kfree(buf);
  257. return rc;
  258. }
  259. static int
  260. smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,
  261. struct smb2_lock_element *buf, unsigned int max_num)
  262. {
  263. int rc = 0, stored_rc;
  264. struct cifsFileInfo *cfile = fdlocks->cfile;
  265. struct cifsLockInfo *li;
  266. unsigned int num = 0;
  267. struct smb2_lock_element *cur = buf;
  268. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  269. list_for_each_entry(li, &fdlocks->locks, llist) {
  270. cur->Length = cpu_to_le64(li->length);
  271. cur->Offset = cpu_to_le64(li->offset);
  272. cur->Flags = cpu_to_le32(li->type |
  273. SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  274. if (++num == max_num) {
  275. stored_rc = smb2_lockv(xid, tcon,
  276. cfile->fid.persistent_fid,
  277. cfile->fid.volatile_fid,
  278. current->tgid, num, buf);
  279. if (stored_rc)
  280. rc = stored_rc;
  281. cur = buf;
  282. num = 0;
  283. } else
  284. cur++;
  285. }
  286. if (num) {
  287. stored_rc = smb2_lockv(xid, tcon,
  288. cfile->fid.persistent_fid,
  289. cfile->fid.volatile_fid,
  290. current->tgid, num, buf);
  291. if (stored_rc)
  292. rc = stored_rc;
  293. }
  294. return rc;
  295. }
  296. int
  297. smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
  298. {
  299. int rc = 0, stored_rc;
  300. unsigned int xid;
  301. unsigned int max_num, max_buf;
  302. struct smb2_lock_element *buf;
  303. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  304. struct cifs_fid_locks *fdlocks;
  305. xid = get_xid();
  306. /*
  307. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  308. * and check it for zero before using.
  309. */
  310. max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
  311. if (max_buf < sizeof(struct smb2_lock_element)) {
  312. free_xid(xid);
  313. return -EINVAL;
  314. }
  315. BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
  316. max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
  317. max_num = max_buf / sizeof(struct smb2_lock_element);
  318. buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
  319. if (!buf) {
  320. free_xid(xid);
  321. return -ENOMEM;
  322. }
  323. list_for_each_entry(fdlocks, &cinode->llist, llist) {
  324. stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);
  325. if (stored_rc)
  326. rc = stored_rc;
  327. }
  328. kfree(buf);
  329. free_xid(xid);
  330. return rc;
  331. }