smb2ops.c 24 KB

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