smb2ops.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * SMB2 version specific operations
  3. *
  4. * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License v2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pagemap.h>
  20. #include <linux/vfs.h>
  21. #include "cifsglob.h"
  22. #include "smb2pdu.h"
  23. #include "smb2proto.h"
  24. #include "cifsproto.h"
  25. #include "cifs_debug.h"
  26. #include "cifs_unicode.h"
  27. #include "smb2status.h"
  28. #include "smb2glob.h"
  29. static int
  30. change_conf(struct TCP_Server_Info *server)
  31. {
  32. server->credits += server->echo_credits + server->oplock_credits;
  33. server->oplock_credits = server->echo_credits = 0;
  34. switch (server->credits) {
  35. case 0:
  36. return -1;
  37. case 1:
  38. server->echoes = false;
  39. server->oplocks = false;
  40. cifs_dbg(VFS, "disabling echoes and oplocks\n");
  41. break;
  42. case 2:
  43. server->echoes = true;
  44. server->oplocks = false;
  45. server->echo_credits = 1;
  46. cifs_dbg(FYI, "disabling oplocks\n");
  47. break;
  48. default:
  49. server->echoes = true;
  50. server->oplocks = true;
  51. server->echo_credits = 1;
  52. server->oplock_credits = 1;
  53. }
  54. server->credits -= server->echo_credits + server->oplock_credits;
  55. return 0;
  56. }
  57. static void
  58. smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
  59. const int optype)
  60. {
  61. int *val, rc = 0;
  62. spin_lock(&server->req_lock);
  63. val = server->ops->get_credits_field(server, optype);
  64. *val += add;
  65. server->in_flight--;
  66. if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
  67. rc = change_conf(server);
  68. /*
  69. * Sometimes server returns 0 credits on oplock break ack - we need to
  70. * rebalance credits in this case.
  71. */
  72. else if (server->in_flight > 0 && server->oplock_credits == 0 &&
  73. server->oplocks) {
  74. if (server->credits > 1) {
  75. server->credits--;
  76. server->oplock_credits++;
  77. }
  78. }
  79. spin_unlock(&server->req_lock);
  80. wake_up(&server->request_q);
  81. if (rc)
  82. cifs_reconnect(server);
  83. }
  84. static void
  85. smb2_set_credits(struct TCP_Server_Info *server, const int val)
  86. {
  87. spin_lock(&server->req_lock);
  88. server->credits = val;
  89. spin_unlock(&server->req_lock);
  90. }
  91. static int *
  92. smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
  93. {
  94. switch (optype) {
  95. case CIFS_ECHO_OP:
  96. return &server->echo_credits;
  97. case CIFS_OBREAK_OP:
  98. return &server->oplock_credits;
  99. default:
  100. return &server->credits;
  101. }
  102. }
  103. static unsigned int
  104. smb2_get_credits(struct mid_q_entry *mid)
  105. {
  106. return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
  107. }
  108. static __u64
  109. smb2_get_next_mid(struct TCP_Server_Info *server)
  110. {
  111. __u64 mid;
  112. /* for SMB2 we need the current value */
  113. spin_lock(&GlobalMid_Lock);
  114. mid = server->CurrentMid++;
  115. spin_unlock(&GlobalMid_Lock);
  116. return mid;
  117. }
  118. static struct mid_q_entry *
  119. smb2_find_mid(struct TCP_Server_Info *server, char *buf)
  120. {
  121. struct mid_q_entry *mid;
  122. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  123. spin_lock(&GlobalMid_Lock);
  124. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  125. if ((mid->mid == hdr->MessageId) &&
  126. (mid->mid_state == MID_REQUEST_SUBMITTED) &&
  127. (mid->command == hdr->Command)) {
  128. spin_unlock(&GlobalMid_Lock);
  129. return mid;
  130. }
  131. }
  132. spin_unlock(&GlobalMid_Lock);
  133. return NULL;
  134. }
  135. static void
  136. smb2_dump_detail(void *buf)
  137. {
  138. #ifdef CONFIG_CIFS_DEBUG2
  139. struct smb2_hdr *smb = (struct smb2_hdr *)buf;
  140. cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
  141. smb->Command, smb->Status, smb->Flags, smb->MessageId,
  142. smb->ProcessId);
  143. cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
  144. #endif
  145. }
  146. static bool
  147. smb2_need_neg(struct TCP_Server_Info *server)
  148. {
  149. return server->max_read == 0;
  150. }
  151. static int
  152. smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  153. {
  154. int rc;
  155. ses->server->CurrentMid = 0;
  156. rc = SMB2_negotiate(xid, ses);
  157. /* BB we probably don't need to retry with modern servers */
  158. if (rc == -EAGAIN)
  159. rc = -EHOSTDOWN;
  160. return rc;
  161. }
  162. static unsigned int
  163. smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  164. {
  165. struct TCP_Server_Info *server = tcon->ses->server;
  166. unsigned int wsize;
  167. /* start with specified wsize, or default */
  168. wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
  169. wsize = min_t(unsigned int, wsize, server->max_write);
  170. /*
  171. * limit write size to 2 ** 16, because we don't support multicredit
  172. * requests now.
  173. */
  174. wsize = min_t(unsigned int, wsize, 2 << 15);
  175. return wsize;
  176. }
  177. static unsigned int
  178. smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  179. {
  180. struct TCP_Server_Info *server = tcon->ses->server;
  181. unsigned int rsize;
  182. /* start with specified rsize, or default */
  183. rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
  184. rsize = min_t(unsigned int, rsize, server->max_read);
  185. /*
  186. * limit write size to 2 ** 16, because we don't support multicredit
  187. * requests now.
  188. */
  189. rsize = min_t(unsigned int, rsize, 2 << 15);
  190. return rsize;
  191. }
  192. static int
  193. smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  194. struct cifs_sb_info *cifs_sb, const char *full_path)
  195. {
  196. int rc;
  197. __le16 *utf16_path;
  198. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  199. struct cifs_open_parms oparms;
  200. struct cifs_fid fid;
  201. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  202. if (!utf16_path)
  203. return -ENOMEM;
  204. oparms.tcon = tcon;
  205. oparms.desired_access = FILE_READ_ATTRIBUTES;
  206. oparms.disposition = FILE_OPEN;
  207. oparms.create_options = 0;
  208. oparms.fid = &fid;
  209. oparms.reconnect = false;
  210. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  211. if (rc) {
  212. kfree(utf16_path);
  213. return rc;
  214. }
  215. rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  216. kfree(utf16_path);
  217. return rc;
  218. }
  219. static int
  220. smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  221. struct cifs_sb_info *cifs_sb, const char *full_path,
  222. u64 *uniqueid, FILE_ALL_INFO *data)
  223. {
  224. *uniqueid = le64_to_cpu(data->IndexNumber);
  225. return 0;
  226. }
  227. static int
  228. smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  229. struct cifs_fid *fid, FILE_ALL_INFO *data)
  230. {
  231. int rc;
  232. struct smb2_file_all_info *smb2_data;
  233. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  234. GFP_KERNEL);
  235. if (smb2_data == NULL)
  236. return -ENOMEM;
  237. rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
  238. smb2_data);
  239. if (!rc)
  240. move_smb2_info_to_cifs(data, smb2_data);
  241. kfree(smb2_data);
  242. return rc;
  243. }
  244. static bool
  245. smb2_can_echo(struct TCP_Server_Info *server)
  246. {
  247. return server->echoes;
  248. }
  249. static void
  250. smb2_clear_stats(struct cifs_tcon *tcon)
  251. {
  252. #ifdef CONFIG_CIFS_STATS
  253. int i;
  254. for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
  255. atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
  256. atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
  257. }
  258. #endif
  259. }
  260. static void
  261. smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
  262. {
  263. seq_puts(m, "\n\tShare Capabilities:");
  264. if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
  265. seq_puts(m, " DFS,");
  266. if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  267. seq_puts(m, " CONTINUOUS AVAILABILITY,");
  268. if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
  269. seq_puts(m, " SCALEOUT,");
  270. if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
  271. seq_puts(m, " CLUSTER,");
  272. if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
  273. seq_puts(m, " ASYMMETRIC,");
  274. if (tcon->capabilities == 0)
  275. seq_puts(m, " None");
  276. seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
  277. }
  278. static void
  279. smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  280. {
  281. #ifdef CONFIG_CIFS_STATS
  282. atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
  283. atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
  284. seq_printf(m, "\nNegotiates: %d sent %d failed",
  285. atomic_read(&sent[SMB2_NEGOTIATE_HE]),
  286. atomic_read(&failed[SMB2_NEGOTIATE_HE]));
  287. seq_printf(m, "\nSessionSetups: %d sent %d failed",
  288. atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
  289. atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
  290. seq_printf(m, "\nLogoffs: %d sent %d failed",
  291. atomic_read(&sent[SMB2_LOGOFF_HE]),
  292. atomic_read(&failed[SMB2_LOGOFF_HE]));
  293. seq_printf(m, "\nTreeConnects: %d sent %d failed",
  294. atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
  295. atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
  296. seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
  297. atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
  298. atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
  299. seq_printf(m, "\nCreates: %d sent %d failed",
  300. atomic_read(&sent[SMB2_CREATE_HE]),
  301. atomic_read(&failed[SMB2_CREATE_HE]));
  302. seq_printf(m, "\nCloses: %d sent %d failed",
  303. atomic_read(&sent[SMB2_CLOSE_HE]),
  304. atomic_read(&failed[SMB2_CLOSE_HE]));
  305. seq_printf(m, "\nFlushes: %d sent %d failed",
  306. atomic_read(&sent[SMB2_FLUSH_HE]),
  307. atomic_read(&failed[SMB2_FLUSH_HE]));
  308. seq_printf(m, "\nReads: %d sent %d failed",
  309. atomic_read(&sent[SMB2_READ_HE]),
  310. atomic_read(&failed[SMB2_READ_HE]));
  311. seq_printf(m, "\nWrites: %d sent %d failed",
  312. atomic_read(&sent[SMB2_WRITE_HE]),
  313. atomic_read(&failed[SMB2_WRITE_HE]));
  314. seq_printf(m, "\nLocks: %d sent %d failed",
  315. atomic_read(&sent[SMB2_LOCK_HE]),
  316. atomic_read(&failed[SMB2_LOCK_HE]));
  317. seq_printf(m, "\nIOCTLs: %d sent %d failed",
  318. atomic_read(&sent[SMB2_IOCTL_HE]),
  319. atomic_read(&failed[SMB2_IOCTL_HE]));
  320. seq_printf(m, "\nCancels: %d sent %d failed",
  321. atomic_read(&sent[SMB2_CANCEL_HE]),
  322. atomic_read(&failed[SMB2_CANCEL_HE]));
  323. seq_printf(m, "\nEchos: %d sent %d failed",
  324. atomic_read(&sent[SMB2_ECHO_HE]),
  325. atomic_read(&failed[SMB2_ECHO_HE]));
  326. seq_printf(m, "\nQueryDirectories: %d sent %d failed",
  327. atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
  328. atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
  329. seq_printf(m, "\nChangeNotifies: %d sent %d failed",
  330. atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
  331. atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
  332. seq_printf(m, "\nQueryInfos: %d sent %d failed",
  333. atomic_read(&sent[SMB2_QUERY_INFO_HE]),
  334. atomic_read(&failed[SMB2_QUERY_INFO_HE]));
  335. seq_printf(m, "\nSetInfos: %d sent %d failed",
  336. atomic_read(&sent[SMB2_SET_INFO_HE]),
  337. atomic_read(&failed[SMB2_SET_INFO_HE]));
  338. seq_printf(m, "\nOplockBreaks: %d sent %d failed",
  339. atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
  340. atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
  341. #endif
  342. }
  343. static void
  344. smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  345. {
  346. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  347. cfile->fid.persistent_fid = fid->persistent_fid;
  348. cfile->fid.volatile_fid = fid->volatile_fid;
  349. smb2_set_oplock_level(cinode, oplock);
  350. cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
  351. }
  352. static void
  353. smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  354. struct cifs_fid *fid)
  355. {
  356. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  357. }
  358. static int
  359. smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  360. struct cifs_fid *fid)
  361. {
  362. return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  363. }
  364. static unsigned int
  365. smb2_read_data_offset(char *buf)
  366. {
  367. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  368. return rsp->DataOffset;
  369. }
  370. static unsigned int
  371. smb2_read_data_length(char *buf)
  372. {
  373. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  374. return le32_to_cpu(rsp->DataLength);
  375. }
  376. static int
  377. smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
  378. struct cifs_io_parms *parms, unsigned int *bytes_read,
  379. char **buf, int *buf_type)
  380. {
  381. parms->persistent_fid = cfile->fid.persistent_fid;
  382. parms->volatile_fid = cfile->fid.volatile_fid;
  383. return SMB2_read(xid, parms, bytes_read, buf, buf_type);
  384. }
  385. static int
  386. smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
  387. struct cifs_io_parms *parms, unsigned int *written,
  388. struct kvec *iov, unsigned long nr_segs)
  389. {
  390. parms->persistent_fid = cfile->fid.persistent_fid;
  391. parms->volatile_fid = cfile->fid.volatile_fid;
  392. return SMB2_write(xid, parms, written, iov, nr_segs);
  393. }
  394. static int
  395. smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
  396. struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
  397. {
  398. __le64 eof = cpu_to_le64(size);
  399. return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  400. cfile->fid.volatile_fid, cfile->pid, &eof);
  401. }
  402. static int
  403. smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  404. const char *path, struct cifs_sb_info *cifs_sb,
  405. struct cifs_fid *fid, __u16 search_flags,
  406. struct cifs_search_info *srch_inf)
  407. {
  408. __le16 *utf16_path;
  409. int rc;
  410. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  411. struct cifs_open_parms oparms;
  412. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  413. if (!utf16_path)
  414. return -ENOMEM;
  415. oparms.tcon = tcon;
  416. oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
  417. oparms.disposition = FILE_OPEN;
  418. oparms.create_options = 0;
  419. oparms.fid = fid;
  420. oparms.reconnect = false;
  421. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  422. kfree(utf16_path);
  423. if (rc) {
  424. cifs_dbg(VFS, "open dir failed\n");
  425. return rc;
  426. }
  427. srch_inf->entries_in_buffer = 0;
  428. srch_inf->index_of_last_entry = 0;
  429. rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
  430. fid->volatile_fid, 0, srch_inf);
  431. if (rc) {
  432. cifs_dbg(VFS, "query directory failed\n");
  433. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  434. }
  435. return rc;
  436. }
  437. static int
  438. smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  439. struct cifs_fid *fid, __u16 search_flags,
  440. struct cifs_search_info *srch_inf)
  441. {
  442. return SMB2_query_directory(xid, tcon, fid->persistent_fid,
  443. fid->volatile_fid, 0, srch_inf);
  444. }
  445. static int
  446. smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  447. struct cifs_fid *fid)
  448. {
  449. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  450. }
  451. /*
  452. * If we negotiate SMB2 protocol and get STATUS_PENDING - update
  453. * the number of credits and return true. Otherwise - return false.
  454. */
  455. static bool
  456. smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
  457. {
  458. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  459. if (hdr->Status != STATUS_PENDING)
  460. return false;
  461. if (!length) {
  462. spin_lock(&server->req_lock);
  463. server->credits += le16_to_cpu(hdr->CreditRequest);
  464. spin_unlock(&server->req_lock);
  465. wake_up(&server->request_q);
  466. }
  467. return true;
  468. }
  469. static int
  470. smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
  471. struct cifsInodeInfo *cinode)
  472. {
  473. if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
  474. return SMB2_lease_break(0, tcon, cinode->lease_key,
  475. smb2_get_lease_state(cinode));
  476. return SMB2_oplock_break(0, tcon, fid->persistent_fid,
  477. fid->volatile_fid,
  478. CIFS_CACHE_READ(cinode) ? 1 : 0);
  479. }
  480. static int
  481. smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  482. struct kstatfs *buf)
  483. {
  484. int rc;
  485. __le16 srch_path = 0; /* Null - open root of share */
  486. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  487. struct cifs_open_parms oparms;
  488. struct cifs_fid fid;
  489. oparms.tcon = tcon;
  490. oparms.desired_access = FILE_READ_ATTRIBUTES;
  491. oparms.disposition = FILE_OPEN;
  492. oparms.create_options = 0;
  493. oparms.fid = &fid;
  494. oparms.reconnect = false;
  495. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  496. if (rc)
  497. return rc;
  498. buf->f_type = SMB2_MAGIC_NUMBER;
  499. rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  500. buf);
  501. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  502. return rc;
  503. }
  504. static bool
  505. smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
  506. {
  507. return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
  508. ob1->fid.volatile_fid == ob2->fid.volatile_fid;
  509. }
  510. static int
  511. smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
  512. __u64 length, __u32 type, int lock, int unlock, bool wait)
  513. {
  514. if (unlock && !lock)
  515. type = SMB2_LOCKFLAG_UNLOCK;
  516. return SMB2_lock(xid, tlink_tcon(cfile->tlink),
  517. cfile->fid.persistent_fid, cfile->fid.volatile_fid,
  518. current->tgid, length, offset, type, wait);
  519. }
  520. static void
  521. smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
  522. {
  523. memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
  524. }
  525. static void
  526. smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
  527. {
  528. memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  529. }
  530. static void
  531. smb2_new_lease_key(struct cifs_fid *fid)
  532. {
  533. get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
  534. }
  535. static int
  536. smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
  537. const char *full_path, char **target_path,
  538. struct cifs_sb_info *cifs_sb)
  539. {
  540. int rc;
  541. __le16 *utf16_path;
  542. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  543. struct cifs_open_parms oparms;
  544. struct cifs_fid fid;
  545. struct smb2_err_rsp *err_buf = NULL;
  546. struct smb2_symlink_err_rsp *symlink;
  547. unsigned int sub_len, sub_offset;
  548. cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
  549. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  550. if (!utf16_path)
  551. return -ENOMEM;
  552. oparms.tcon = tcon;
  553. oparms.desired_access = FILE_READ_ATTRIBUTES;
  554. oparms.disposition = FILE_OPEN;
  555. oparms.create_options = 0;
  556. oparms.fid = &fid;
  557. oparms.reconnect = false;
  558. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
  559. if (!rc || !err_buf) {
  560. kfree(utf16_path);
  561. return -ENOENT;
  562. }
  563. /* open must fail on symlink - reset rc */
  564. rc = 0;
  565. symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
  566. sub_len = le16_to_cpu(symlink->SubstituteNameLength);
  567. sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
  568. *target_path = cifs_strndup_from_utf16(
  569. (char *)symlink->PathBuffer + sub_offset,
  570. sub_len, true, cifs_sb->local_nls);
  571. if (!(*target_path)) {
  572. kfree(utf16_path);
  573. return -ENOMEM;
  574. }
  575. convert_delimiter(*target_path, '/');
  576. cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
  577. kfree(utf16_path);
  578. return rc;
  579. }
  580. struct smb_version_operations smb21_operations = {
  581. .compare_fids = smb2_compare_fids,
  582. .setup_request = smb2_setup_request,
  583. .setup_async_request = smb2_setup_async_request,
  584. .check_receive = smb2_check_receive,
  585. .add_credits = smb2_add_credits,
  586. .set_credits = smb2_set_credits,
  587. .get_credits_field = smb2_get_credits_field,
  588. .get_credits = smb2_get_credits,
  589. .get_next_mid = smb2_get_next_mid,
  590. .read_data_offset = smb2_read_data_offset,
  591. .read_data_length = smb2_read_data_length,
  592. .map_error = map_smb2_to_linux_error,
  593. .find_mid = smb2_find_mid,
  594. .check_message = smb2_check_message,
  595. .dump_detail = smb2_dump_detail,
  596. .clear_stats = smb2_clear_stats,
  597. .print_stats = smb2_print_stats,
  598. .is_oplock_break = smb2_is_valid_oplock_break,
  599. .need_neg = smb2_need_neg,
  600. .negotiate = smb2_negotiate,
  601. .negotiate_wsize = smb2_negotiate_wsize,
  602. .negotiate_rsize = smb2_negotiate_rsize,
  603. .sess_setup = SMB2_sess_setup,
  604. .logoff = SMB2_logoff,
  605. .tree_connect = SMB2_tcon,
  606. .tree_disconnect = SMB2_tdis,
  607. .is_path_accessible = smb2_is_path_accessible,
  608. .can_echo = smb2_can_echo,
  609. .echo = SMB2_echo,
  610. .query_path_info = smb2_query_path_info,
  611. .get_srv_inum = smb2_get_srv_inum,
  612. .query_file_info = smb2_query_file_info,
  613. .set_path_size = smb2_set_path_size,
  614. .set_file_size = smb2_set_file_size,
  615. .set_file_info = smb2_set_file_info,
  616. .mkdir = smb2_mkdir,
  617. .mkdir_setinfo = smb2_mkdir_setinfo,
  618. .rmdir = smb2_rmdir,
  619. .unlink = smb2_unlink,
  620. .rename = smb2_rename_path,
  621. .create_hardlink = smb2_create_hardlink,
  622. .query_symlink = smb2_query_symlink,
  623. .open = smb2_open_file,
  624. .set_fid = smb2_set_fid,
  625. .close = smb2_close_file,
  626. .flush = smb2_flush_file,
  627. .async_readv = smb2_async_readv,
  628. .async_writev = smb2_async_writev,
  629. .sync_read = smb2_sync_read,
  630. .sync_write = smb2_sync_write,
  631. .query_dir_first = smb2_query_dir_first,
  632. .query_dir_next = smb2_query_dir_next,
  633. .close_dir = smb2_close_dir,
  634. .calc_smb_size = smb2_calc_size,
  635. .is_status_pending = smb2_is_status_pending,
  636. .oplock_response = smb2_oplock_response,
  637. .queryfs = smb2_queryfs,
  638. .mand_lock = smb2_mand_lock,
  639. .mand_unlock_range = smb2_unlock_range,
  640. .push_mand_locks = smb2_push_mandatory_locks,
  641. .get_lease_key = smb2_get_lease_key,
  642. .set_lease_key = smb2_set_lease_key,
  643. .new_lease_key = smb2_new_lease_key,
  644. .calc_signature = smb2_calc_signature,
  645. };
  646. struct smb_version_operations smb30_operations = {
  647. .compare_fids = smb2_compare_fids,
  648. .setup_request = smb2_setup_request,
  649. .setup_async_request = smb2_setup_async_request,
  650. .check_receive = smb2_check_receive,
  651. .add_credits = smb2_add_credits,
  652. .set_credits = smb2_set_credits,
  653. .get_credits_field = smb2_get_credits_field,
  654. .get_credits = smb2_get_credits,
  655. .get_next_mid = smb2_get_next_mid,
  656. .read_data_offset = smb2_read_data_offset,
  657. .read_data_length = smb2_read_data_length,
  658. .map_error = map_smb2_to_linux_error,
  659. .find_mid = smb2_find_mid,
  660. .check_message = smb2_check_message,
  661. .dump_detail = smb2_dump_detail,
  662. .clear_stats = smb2_clear_stats,
  663. .print_stats = smb2_print_stats,
  664. .dump_share_caps = smb2_dump_share_caps,
  665. .is_oplock_break = smb2_is_valid_oplock_break,
  666. .need_neg = smb2_need_neg,
  667. .negotiate = smb2_negotiate,
  668. .negotiate_wsize = smb2_negotiate_wsize,
  669. .negotiate_rsize = smb2_negotiate_rsize,
  670. .sess_setup = SMB2_sess_setup,
  671. .logoff = SMB2_logoff,
  672. .tree_connect = SMB2_tcon,
  673. .tree_disconnect = SMB2_tdis,
  674. .is_path_accessible = smb2_is_path_accessible,
  675. .can_echo = smb2_can_echo,
  676. .echo = SMB2_echo,
  677. .query_path_info = smb2_query_path_info,
  678. .get_srv_inum = smb2_get_srv_inum,
  679. .query_file_info = smb2_query_file_info,
  680. .set_path_size = smb2_set_path_size,
  681. .set_file_size = smb2_set_file_size,
  682. .set_file_info = smb2_set_file_info,
  683. .mkdir = smb2_mkdir,
  684. .mkdir_setinfo = smb2_mkdir_setinfo,
  685. .rmdir = smb2_rmdir,
  686. .unlink = smb2_unlink,
  687. .rename = smb2_rename_path,
  688. .create_hardlink = smb2_create_hardlink,
  689. .query_symlink = smb2_query_symlink,
  690. .open = smb2_open_file,
  691. .set_fid = smb2_set_fid,
  692. .close = smb2_close_file,
  693. .flush = smb2_flush_file,
  694. .async_readv = smb2_async_readv,
  695. .async_writev = smb2_async_writev,
  696. .sync_read = smb2_sync_read,
  697. .sync_write = smb2_sync_write,
  698. .query_dir_first = smb2_query_dir_first,
  699. .query_dir_next = smb2_query_dir_next,
  700. .close_dir = smb2_close_dir,
  701. .calc_smb_size = smb2_calc_size,
  702. .is_status_pending = smb2_is_status_pending,
  703. .oplock_response = smb2_oplock_response,
  704. .queryfs = smb2_queryfs,
  705. .mand_lock = smb2_mand_lock,
  706. .mand_unlock_range = smb2_unlock_range,
  707. .push_mand_locks = smb2_push_mandatory_locks,
  708. .get_lease_key = smb2_get_lease_key,
  709. .set_lease_key = smb2_set_lease_key,
  710. .new_lease_key = smb2_new_lease_key,
  711. .generate_signingkey = generate_smb3signingkey,
  712. .calc_signature = smb3_calc_signature,
  713. };
  714. struct smb_version_values smb20_values = {
  715. .version_string = SMB20_VERSION_STRING,
  716. .protocol_id = SMB20_PROT_ID,
  717. .req_capabilities = 0, /* MBZ */
  718. .large_lock_type = 0,
  719. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  720. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  721. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  722. .header_size = sizeof(struct smb2_hdr),
  723. .max_header_size = MAX_SMB2_HDR_SIZE,
  724. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  725. .lock_cmd = SMB2_LOCK,
  726. .cap_unix = 0,
  727. .cap_nt_find = SMB2_NT_FIND,
  728. .cap_large_files = SMB2_LARGE_FILES,
  729. .oplock_read = SMB2_OPLOCK_LEVEL_II,
  730. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  731. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  732. };
  733. struct smb_version_values smb21_values = {
  734. .version_string = SMB21_VERSION_STRING,
  735. .protocol_id = SMB21_PROT_ID,
  736. .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
  737. .large_lock_type = 0,
  738. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  739. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  740. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  741. .header_size = sizeof(struct smb2_hdr),
  742. .max_header_size = MAX_SMB2_HDR_SIZE,
  743. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  744. .lock_cmd = SMB2_LOCK,
  745. .cap_unix = 0,
  746. .cap_nt_find = SMB2_NT_FIND,
  747. .cap_large_files = SMB2_LARGE_FILES,
  748. .oplock_read = SMB2_OPLOCK_LEVEL_II,
  749. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  750. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  751. };
  752. struct smb_version_values smb30_values = {
  753. .version_string = SMB30_VERSION_STRING,
  754. .protocol_id = SMB30_PROT_ID,
  755. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
  756. .large_lock_type = 0,
  757. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  758. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  759. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  760. .header_size = sizeof(struct smb2_hdr),
  761. .max_header_size = MAX_SMB2_HDR_SIZE,
  762. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  763. .lock_cmd = SMB2_LOCK,
  764. .cap_unix = 0,
  765. .cap_nt_find = SMB2_NT_FIND,
  766. .cap_large_files = SMB2_LARGE_FILES,
  767. .oplock_read = SMB2_OPLOCK_LEVEL_II,
  768. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  769. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  770. };
  771. struct smb_version_values smb302_values = {
  772. .version_string = SMB302_VERSION_STRING,
  773. .protocol_id = SMB302_PROT_ID,
  774. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
  775. .large_lock_type = 0,
  776. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  777. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  778. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  779. .header_size = sizeof(struct smb2_hdr),
  780. .max_header_size = MAX_SMB2_HDR_SIZE,
  781. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  782. .lock_cmd = SMB2_LOCK,
  783. .cap_unix = 0,
  784. .cap_nt_find = SMB2_NT_FIND,
  785. .cap_large_files = SMB2_LARGE_FILES,
  786. .oplock_read = SMB2_OPLOCK_LEVEL_II,
  787. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  788. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  789. };