smb2ops.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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. /* set it to the maximum buffer size value we can send with 1 credit */
  171. wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
  172. return wsize;
  173. }
  174. static unsigned int
  175. smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  176. {
  177. struct TCP_Server_Info *server = tcon->ses->server;
  178. unsigned int rsize;
  179. /* start with specified rsize, or default */
  180. rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
  181. rsize = min_t(unsigned int, rsize, server->max_read);
  182. /* set it to the maximum buffer size value we can send with 1 credit */
  183. rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
  184. return rsize;
  185. }
  186. #ifdef CONFIG_CIFS_STATS2
  187. static int
  188. SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
  189. {
  190. int rc;
  191. unsigned int ret_data_len = 0;
  192. struct network_interface_info_ioctl_rsp *out_buf;
  193. rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
  194. FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
  195. NULL /* no data input */, 0 /* no data input */,
  196. (char **)&out_buf, &ret_data_len);
  197. if ((rc == 0) && (ret_data_len > 0)) {
  198. /* Dump info on first interface */
  199. cifs_dbg(FYI, "Adapter Capability 0x%x\t",
  200. le32_to_cpu(out_buf->Capability));
  201. cifs_dbg(FYI, "Link Speed %lld\n",
  202. le64_to_cpu(out_buf->LinkSpeed));
  203. } else
  204. cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
  205. return rc;
  206. }
  207. #endif /* STATS2 */
  208. static void
  209. smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
  210. {
  211. int rc;
  212. __le16 srch_path = 0; /* Null - open root of share */
  213. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  214. struct cifs_open_parms oparms;
  215. struct cifs_fid fid;
  216. oparms.tcon = tcon;
  217. oparms.desired_access = FILE_READ_ATTRIBUTES;
  218. oparms.disposition = FILE_OPEN;
  219. oparms.create_options = 0;
  220. oparms.fid = &fid;
  221. oparms.reconnect = false;
  222. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  223. if (rc)
  224. return;
  225. #ifdef CONFIG_CIFS_STATS2
  226. SMB3_request_interfaces(xid, tcon);
  227. #endif /* STATS2 */
  228. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  229. FS_ATTRIBUTE_INFORMATION);
  230. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  231. FS_DEVICE_INFORMATION);
  232. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  233. FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
  234. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  235. return;
  236. }
  237. static void
  238. smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
  239. {
  240. int rc;
  241. __le16 srch_path = 0; /* Null - open root of share */
  242. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  243. struct cifs_open_parms oparms;
  244. struct cifs_fid fid;
  245. oparms.tcon = tcon;
  246. oparms.desired_access = FILE_READ_ATTRIBUTES;
  247. oparms.disposition = FILE_OPEN;
  248. oparms.create_options = 0;
  249. oparms.fid = &fid;
  250. oparms.reconnect = false;
  251. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  252. if (rc)
  253. return;
  254. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  255. FS_ATTRIBUTE_INFORMATION);
  256. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  257. FS_DEVICE_INFORMATION);
  258. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  259. return;
  260. }
  261. static int
  262. smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  263. struct cifs_sb_info *cifs_sb, const char *full_path)
  264. {
  265. int rc;
  266. __le16 *utf16_path;
  267. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  268. struct cifs_open_parms oparms;
  269. struct cifs_fid fid;
  270. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  271. if (!utf16_path)
  272. return -ENOMEM;
  273. oparms.tcon = tcon;
  274. oparms.desired_access = FILE_READ_ATTRIBUTES;
  275. oparms.disposition = FILE_OPEN;
  276. oparms.create_options = 0;
  277. oparms.fid = &fid;
  278. oparms.reconnect = false;
  279. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  280. if (rc) {
  281. kfree(utf16_path);
  282. return rc;
  283. }
  284. rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  285. kfree(utf16_path);
  286. return rc;
  287. }
  288. static int
  289. smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  290. struct cifs_sb_info *cifs_sb, const char *full_path,
  291. u64 *uniqueid, FILE_ALL_INFO *data)
  292. {
  293. *uniqueid = le64_to_cpu(data->IndexNumber);
  294. return 0;
  295. }
  296. static int
  297. smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  298. struct cifs_fid *fid, FILE_ALL_INFO *data)
  299. {
  300. int rc;
  301. struct smb2_file_all_info *smb2_data;
  302. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  303. GFP_KERNEL);
  304. if (smb2_data == NULL)
  305. return -ENOMEM;
  306. rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
  307. smb2_data);
  308. if (!rc)
  309. move_smb2_info_to_cifs(data, smb2_data);
  310. kfree(smb2_data);
  311. return rc;
  312. }
  313. static bool
  314. smb2_can_echo(struct TCP_Server_Info *server)
  315. {
  316. return server->echoes;
  317. }
  318. static void
  319. smb2_clear_stats(struct cifs_tcon *tcon)
  320. {
  321. #ifdef CONFIG_CIFS_STATS
  322. int i;
  323. for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
  324. atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
  325. atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
  326. }
  327. #endif
  328. }
  329. static void
  330. smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
  331. {
  332. seq_puts(m, "\n\tShare Capabilities:");
  333. if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
  334. seq_puts(m, " DFS,");
  335. if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  336. seq_puts(m, " CONTINUOUS AVAILABILITY,");
  337. if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
  338. seq_puts(m, " SCALEOUT,");
  339. if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
  340. seq_puts(m, " CLUSTER,");
  341. if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
  342. seq_puts(m, " ASYMMETRIC,");
  343. if (tcon->capabilities == 0)
  344. seq_puts(m, " None");
  345. if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
  346. seq_puts(m, " Aligned,");
  347. if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
  348. seq_puts(m, " Partition Aligned,");
  349. if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
  350. seq_puts(m, " SSD,");
  351. if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
  352. seq_puts(m, " TRIM-support,");
  353. seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
  354. if (tcon->perf_sector_size)
  355. seq_printf(m, "\tOptimal sector size: 0x%x",
  356. tcon->perf_sector_size);
  357. }
  358. static void
  359. smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  360. {
  361. #ifdef CONFIG_CIFS_STATS
  362. atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
  363. atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
  364. seq_printf(m, "\nNegotiates: %d sent %d failed",
  365. atomic_read(&sent[SMB2_NEGOTIATE_HE]),
  366. atomic_read(&failed[SMB2_NEGOTIATE_HE]));
  367. seq_printf(m, "\nSessionSetups: %d sent %d failed",
  368. atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
  369. atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
  370. seq_printf(m, "\nLogoffs: %d sent %d failed",
  371. atomic_read(&sent[SMB2_LOGOFF_HE]),
  372. atomic_read(&failed[SMB2_LOGOFF_HE]));
  373. seq_printf(m, "\nTreeConnects: %d sent %d failed",
  374. atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
  375. atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
  376. seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
  377. atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
  378. atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
  379. seq_printf(m, "\nCreates: %d sent %d failed",
  380. atomic_read(&sent[SMB2_CREATE_HE]),
  381. atomic_read(&failed[SMB2_CREATE_HE]));
  382. seq_printf(m, "\nCloses: %d sent %d failed",
  383. atomic_read(&sent[SMB2_CLOSE_HE]),
  384. atomic_read(&failed[SMB2_CLOSE_HE]));
  385. seq_printf(m, "\nFlushes: %d sent %d failed",
  386. atomic_read(&sent[SMB2_FLUSH_HE]),
  387. atomic_read(&failed[SMB2_FLUSH_HE]));
  388. seq_printf(m, "\nReads: %d sent %d failed",
  389. atomic_read(&sent[SMB2_READ_HE]),
  390. atomic_read(&failed[SMB2_READ_HE]));
  391. seq_printf(m, "\nWrites: %d sent %d failed",
  392. atomic_read(&sent[SMB2_WRITE_HE]),
  393. atomic_read(&failed[SMB2_WRITE_HE]));
  394. seq_printf(m, "\nLocks: %d sent %d failed",
  395. atomic_read(&sent[SMB2_LOCK_HE]),
  396. atomic_read(&failed[SMB2_LOCK_HE]));
  397. seq_printf(m, "\nIOCTLs: %d sent %d failed",
  398. atomic_read(&sent[SMB2_IOCTL_HE]),
  399. atomic_read(&failed[SMB2_IOCTL_HE]));
  400. seq_printf(m, "\nCancels: %d sent %d failed",
  401. atomic_read(&sent[SMB2_CANCEL_HE]),
  402. atomic_read(&failed[SMB2_CANCEL_HE]));
  403. seq_printf(m, "\nEchos: %d sent %d failed",
  404. atomic_read(&sent[SMB2_ECHO_HE]),
  405. atomic_read(&failed[SMB2_ECHO_HE]));
  406. seq_printf(m, "\nQueryDirectories: %d sent %d failed",
  407. atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
  408. atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
  409. seq_printf(m, "\nChangeNotifies: %d sent %d failed",
  410. atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
  411. atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
  412. seq_printf(m, "\nQueryInfos: %d sent %d failed",
  413. atomic_read(&sent[SMB2_QUERY_INFO_HE]),
  414. atomic_read(&failed[SMB2_QUERY_INFO_HE]));
  415. seq_printf(m, "\nSetInfos: %d sent %d failed",
  416. atomic_read(&sent[SMB2_SET_INFO_HE]),
  417. atomic_read(&failed[SMB2_SET_INFO_HE]));
  418. seq_printf(m, "\nOplockBreaks: %d sent %d failed",
  419. atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
  420. atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
  421. #endif
  422. }
  423. static void
  424. smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  425. {
  426. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  427. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  428. cfile->fid.persistent_fid = fid->persistent_fid;
  429. cfile->fid.volatile_fid = fid->volatile_fid;
  430. server->ops->set_oplock_level(cinode, oplock, fid->epoch,
  431. &fid->purge_cache);
  432. cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
  433. }
  434. static void
  435. smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  436. struct cifs_fid *fid)
  437. {
  438. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  439. }
  440. static int
  441. SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
  442. u64 persistent_fid, u64 volatile_fid,
  443. struct copychunk_ioctl *pcchunk)
  444. {
  445. int rc;
  446. unsigned int ret_data_len;
  447. struct resume_key_req *res_key;
  448. rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
  449. FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
  450. NULL, 0 /* no input */,
  451. (char **)&res_key, &ret_data_len);
  452. if (rc) {
  453. cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
  454. goto req_res_key_exit;
  455. }
  456. if (ret_data_len < sizeof(struct resume_key_req)) {
  457. cifs_dbg(VFS, "Invalid refcopy resume key length\n");
  458. rc = -EINVAL;
  459. goto req_res_key_exit;
  460. }
  461. memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
  462. req_res_key_exit:
  463. kfree(res_key);
  464. return rc;
  465. }
  466. static int
  467. smb2_clone_range(const unsigned int xid,
  468. struct cifsFileInfo *srcfile,
  469. struct cifsFileInfo *trgtfile, u64 src_off,
  470. u64 len, u64 dest_off)
  471. {
  472. int rc;
  473. unsigned int ret_data_len;
  474. struct copychunk_ioctl *pcchunk;
  475. struct copychunk_ioctl_rsp *retbuf = NULL;
  476. struct cifs_tcon *tcon;
  477. int chunks_copied = 0;
  478. bool chunk_sizes_updated = false;
  479. pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
  480. if (pcchunk == NULL)
  481. return -ENOMEM;
  482. cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
  483. /* Request a key from the server to identify the source of the copy */
  484. rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
  485. srcfile->fid.persistent_fid,
  486. srcfile->fid.volatile_fid, pcchunk);
  487. /* Note: request_res_key sets res_key null only if rc !=0 */
  488. if (rc)
  489. goto cchunk_out;
  490. /* For now array only one chunk long, will make more flexible later */
  491. pcchunk->ChunkCount = __constant_cpu_to_le32(1);
  492. pcchunk->Reserved = 0;
  493. pcchunk->Reserved2 = 0;
  494. tcon = tlink_tcon(trgtfile->tlink);
  495. while (len > 0) {
  496. pcchunk->SourceOffset = cpu_to_le64(src_off);
  497. pcchunk->TargetOffset = cpu_to_le64(dest_off);
  498. pcchunk->Length =
  499. cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
  500. /* Request server copy to target from src identified by key */
  501. rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
  502. trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
  503. true /* is_fsctl */, (char *)pcchunk,
  504. sizeof(struct copychunk_ioctl), (char **)&retbuf,
  505. &ret_data_len);
  506. if (rc == 0) {
  507. if (ret_data_len !=
  508. sizeof(struct copychunk_ioctl_rsp)) {
  509. cifs_dbg(VFS, "invalid cchunk response size\n");
  510. rc = -EIO;
  511. goto cchunk_out;
  512. }
  513. if (retbuf->TotalBytesWritten == 0) {
  514. cifs_dbg(FYI, "no bytes copied\n");
  515. rc = -EIO;
  516. goto cchunk_out;
  517. }
  518. /*
  519. * Check if server claimed to write more than we asked
  520. */
  521. if (le32_to_cpu(retbuf->TotalBytesWritten) >
  522. le32_to_cpu(pcchunk->Length)) {
  523. cifs_dbg(VFS, "invalid copy chunk response\n");
  524. rc = -EIO;
  525. goto cchunk_out;
  526. }
  527. if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
  528. cifs_dbg(VFS, "invalid num chunks written\n");
  529. rc = -EIO;
  530. goto cchunk_out;
  531. }
  532. chunks_copied++;
  533. src_off += le32_to_cpu(retbuf->TotalBytesWritten);
  534. dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
  535. len -= le32_to_cpu(retbuf->TotalBytesWritten);
  536. cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
  537. le32_to_cpu(retbuf->ChunksWritten),
  538. le32_to_cpu(retbuf->ChunkBytesWritten),
  539. le32_to_cpu(retbuf->TotalBytesWritten));
  540. } else if (rc == -EINVAL) {
  541. if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
  542. goto cchunk_out;
  543. cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
  544. le32_to_cpu(retbuf->ChunksWritten),
  545. le32_to_cpu(retbuf->ChunkBytesWritten),
  546. le32_to_cpu(retbuf->TotalBytesWritten));
  547. /*
  548. * Check if this is the first request using these sizes,
  549. * (ie check if copy succeed once with original sizes
  550. * and check if the server gave us different sizes after
  551. * we already updated max sizes on previous request).
  552. * if not then why is the server returning an error now
  553. */
  554. if ((chunks_copied != 0) || chunk_sizes_updated)
  555. goto cchunk_out;
  556. /* Check that server is not asking us to grow size */
  557. if (le32_to_cpu(retbuf->ChunkBytesWritten) <
  558. tcon->max_bytes_chunk)
  559. tcon->max_bytes_chunk =
  560. le32_to_cpu(retbuf->ChunkBytesWritten);
  561. else
  562. goto cchunk_out; /* server gave us bogus size */
  563. /* No need to change MaxChunks since already set to 1 */
  564. chunk_sizes_updated = true;
  565. }
  566. }
  567. cchunk_out:
  568. kfree(pcchunk);
  569. return rc;
  570. }
  571. static int
  572. smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  573. struct cifs_fid *fid)
  574. {
  575. return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  576. }
  577. static unsigned int
  578. smb2_read_data_offset(char *buf)
  579. {
  580. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  581. return rsp->DataOffset;
  582. }
  583. static unsigned int
  584. smb2_read_data_length(char *buf)
  585. {
  586. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  587. return le32_to_cpu(rsp->DataLength);
  588. }
  589. static int
  590. smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
  591. struct cifs_io_parms *parms, unsigned int *bytes_read,
  592. char **buf, int *buf_type)
  593. {
  594. parms->persistent_fid = cfile->fid.persistent_fid;
  595. parms->volatile_fid = cfile->fid.volatile_fid;
  596. return SMB2_read(xid, parms, bytes_read, buf, buf_type);
  597. }
  598. static int
  599. smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
  600. struct cifs_io_parms *parms, unsigned int *written,
  601. struct kvec *iov, unsigned long nr_segs)
  602. {
  603. parms->persistent_fid = cfile->fid.persistent_fid;
  604. parms->volatile_fid = cfile->fid.volatile_fid;
  605. return SMB2_write(xid, parms, written, iov, nr_segs);
  606. }
  607. static int
  608. smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
  609. struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
  610. {
  611. __le64 eof = cpu_to_le64(size);
  612. return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  613. cfile->fid.volatile_fid, cfile->pid, &eof);
  614. }
  615. static int
  616. smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  617. struct cifsFileInfo *cfile)
  618. {
  619. return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
  620. cfile->fid.volatile_fid);
  621. }
  622. static int
  623. smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  624. const char *path, struct cifs_sb_info *cifs_sb,
  625. struct cifs_fid *fid, __u16 search_flags,
  626. struct cifs_search_info *srch_inf)
  627. {
  628. __le16 *utf16_path;
  629. int rc;
  630. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  631. struct cifs_open_parms oparms;
  632. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  633. if (!utf16_path)
  634. return -ENOMEM;
  635. oparms.tcon = tcon;
  636. oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
  637. oparms.disposition = FILE_OPEN;
  638. oparms.create_options = 0;
  639. oparms.fid = fid;
  640. oparms.reconnect = false;
  641. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  642. kfree(utf16_path);
  643. if (rc) {
  644. cifs_dbg(VFS, "open dir failed\n");
  645. return rc;
  646. }
  647. srch_inf->entries_in_buffer = 0;
  648. srch_inf->index_of_last_entry = 0;
  649. rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
  650. fid->volatile_fid, 0, srch_inf);
  651. if (rc) {
  652. cifs_dbg(VFS, "query directory failed\n");
  653. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  654. }
  655. return rc;
  656. }
  657. static int
  658. smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  659. struct cifs_fid *fid, __u16 search_flags,
  660. struct cifs_search_info *srch_inf)
  661. {
  662. return SMB2_query_directory(xid, tcon, fid->persistent_fid,
  663. fid->volatile_fid, 0, srch_inf);
  664. }
  665. static int
  666. smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  667. struct cifs_fid *fid)
  668. {
  669. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  670. }
  671. /*
  672. * If we negotiate SMB2 protocol and get STATUS_PENDING - update
  673. * the number of credits and return true. Otherwise - return false.
  674. */
  675. static bool
  676. smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
  677. {
  678. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  679. if (hdr->Status != STATUS_PENDING)
  680. return false;
  681. if (!length) {
  682. spin_lock(&server->req_lock);
  683. server->credits += le16_to_cpu(hdr->CreditRequest);
  684. spin_unlock(&server->req_lock);
  685. wake_up(&server->request_q);
  686. }
  687. return true;
  688. }
  689. static int
  690. smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
  691. struct cifsInodeInfo *cinode)
  692. {
  693. if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
  694. return SMB2_lease_break(0, tcon, cinode->lease_key,
  695. smb2_get_lease_state(cinode));
  696. return SMB2_oplock_break(0, tcon, fid->persistent_fid,
  697. fid->volatile_fid,
  698. CIFS_CACHE_READ(cinode) ? 1 : 0);
  699. }
  700. static int
  701. smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  702. struct kstatfs *buf)
  703. {
  704. int rc;
  705. __le16 srch_path = 0; /* Null - open root of share */
  706. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  707. struct cifs_open_parms oparms;
  708. struct cifs_fid fid;
  709. oparms.tcon = tcon;
  710. oparms.desired_access = FILE_READ_ATTRIBUTES;
  711. oparms.disposition = FILE_OPEN;
  712. oparms.create_options = 0;
  713. oparms.fid = &fid;
  714. oparms.reconnect = false;
  715. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  716. if (rc)
  717. return rc;
  718. buf->f_type = SMB2_MAGIC_NUMBER;
  719. rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  720. buf);
  721. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  722. return rc;
  723. }
  724. static bool
  725. smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
  726. {
  727. return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
  728. ob1->fid.volatile_fid == ob2->fid.volatile_fid;
  729. }
  730. static int
  731. smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
  732. __u64 length, __u32 type, int lock, int unlock, bool wait)
  733. {
  734. if (unlock && !lock)
  735. type = SMB2_LOCKFLAG_UNLOCK;
  736. return SMB2_lock(xid, tlink_tcon(cfile->tlink),
  737. cfile->fid.persistent_fid, cfile->fid.volatile_fid,
  738. current->tgid, length, offset, type, wait);
  739. }
  740. static void
  741. smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
  742. {
  743. memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
  744. }
  745. static void
  746. smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
  747. {
  748. memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  749. }
  750. static void
  751. smb2_new_lease_key(struct cifs_fid *fid)
  752. {
  753. get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
  754. }
  755. static int
  756. smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
  757. const char *full_path, char **target_path,
  758. struct cifs_sb_info *cifs_sb)
  759. {
  760. int rc;
  761. __le16 *utf16_path;
  762. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  763. struct cifs_open_parms oparms;
  764. struct cifs_fid fid;
  765. struct smb2_err_rsp *err_buf = NULL;
  766. struct smb2_symlink_err_rsp *symlink;
  767. unsigned int sub_len, sub_offset;
  768. cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
  769. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  770. if (!utf16_path)
  771. return -ENOMEM;
  772. oparms.tcon = tcon;
  773. oparms.desired_access = FILE_READ_ATTRIBUTES;
  774. oparms.disposition = FILE_OPEN;
  775. oparms.create_options = 0;
  776. oparms.fid = &fid;
  777. oparms.reconnect = false;
  778. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
  779. if (!rc || !err_buf) {
  780. kfree(utf16_path);
  781. return -ENOENT;
  782. }
  783. /* open must fail on symlink - reset rc */
  784. rc = 0;
  785. symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
  786. sub_len = le16_to_cpu(symlink->SubstituteNameLength);
  787. sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
  788. *target_path = cifs_strndup_from_utf16(
  789. (char *)symlink->PathBuffer + sub_offset,
  790. sub_len, true, cifs_sb->local_nls);
  791. if (!(*target_path)) {
  792. kfree(utf16_path);
  793. return -ENOMEM;
  794. }
  795. convert_delimiter(*target_path, '/');
  796. cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
  797. kfree(utf16_path);
  798. return rc;
  799. }
  800. static void
  801. smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  802. unsigned int epoch, bool *purge_cache)
  803. {
  804. oplock &= 0xFF;
  805. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  806. return;
  807. if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  808. cinode->oplock = CIFS_CACHE_RHW_FLG;
  809. cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
  810. &cinode->vfs_inode);
  811. } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
  812. cinode->oplock = CIFS_CACHE_RW_FLG;
  813. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  814. &cinode->vfs_inode);
  815. } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
  816. cinode->oplock = CIFS_CACHE_READ_FLG;
  817. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  818. &cinode->vfs_inode);
  819. } else
  820. cinode->oplock = 0;
  821. }
  822. static void
  823. smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  824. unsigned int epoch, bool *purge_cache)
  825. {
  826. char message[5] = {0};
  827. oplock &= 0xFF;
  828. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  829. return;
  830. cinode->oplock = 0;
  831. if (oplock & SMB2_LEASE_READ_CACHING_HE) {
  832. cinode->oplock |= CIFS_CACHE_READ_FLG;
  833. strcat(message, "R");
  834. }
  835. if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
  836. cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
  837. strcat(message, "H");
  838. }
  839. if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
  840. cinode->oplock |= CIFS_CACHE_WRITE_FLG;
  841. strcat(message, "W");
  842. }
  843. if (!cinode->oplock)
  844. strcat(message, "None");
  845. cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
  846. &cinode->vfs_inode);
  847. }
  848. static void
  849. smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  850. unsigned int epoch, bool *purge_cache)
  851. {
  852. unsigned int old_oplock = cinode->oplock;
  853. smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
  854. if (purge_cache) {
  855. *purge_cache = false;
  856. if (old_oplock == CIFS_CACHE_READ_FLG) {
  857. if (cinode->oplock == CIFS_CACHE_READ_FLG &&
  858. (epoch - cinode->epoch > 0))
  859. *purge_cache = true;
  860. else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  861. (epoch - cinode->epoch > 1))
  862. *purge_cache = true;
  863. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  864. (epoch - cinode->epoch > 1))
  865. *purge_cache = true;
  866. else if (cinode->oplock == 0 &&
  867. (epoch - cinode->epoch > 0))
  868. *purge_cache = true;
  869. } else if (old_oplock == CIFS_CACHE_RH_FLG) {
  870. if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  871. (epoch - cinode->epoch > 0))
  872. *purge_cache = true;
  873. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  874. (epoch - cinode->epoch > 1))
  875. *purge_cache = true;
  876. }
  877. cinode->epoch = epoch;
  878. }
  879. }
  880. static bool
  881. smb2_is_read_op(__u32 oplock)
  882. {
  883. return oplock == SMB2_OPLOCK_LEVEL_II;
  884. }
  885. static bool
  886. smb21_is_read_op(__u32 oplock)
  887. {
  888. return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
  889. !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
  890. }
  891. static __le32
  892. map_oplock_to_lease(u8 oplock)
  893. {
  894. if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  895. return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
  896. else if (oplock == SMB2_OPLOCK_LEVEL_II)
  897. return SMB2_LEASE_READ_CACHING;
  898. else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
  899. return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
  900. SMB2_LEASE_WRITE_CACHING;
  901. return 0;
  902. }
  903. static char *
  904. smb2_create_lease_buf(u8 *lease_key, u8 oplock)
  905. {
  906. struct create_lease *buf;
  907. buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
  908. if (!buf)
  909. return NULL;
  910. buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
  911. buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
  912. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  913. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  914. (struct create_lease, lcontext));
  915. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
  916. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  917. (struct create_lease, Name));
  918. buf->ccontext.NameLength = cpu_to_le16(4);
  919. buf->Name[0] = 'R';
  920. buf->Name[1] = 'q';
  921. buf->Name[2] = 'L';
  922. buf->Name[3] = 's';
  923. return (char *)buf;
  924. }
  925. static char *
  926. smb3_create_lease_buf(u8 *lease_key, u8 oplock)
  927. {
  928. struct create_lease_v2 *buf;
  929. buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
  930. if (!buf)
  931. return NULL;
  932. buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
  933. buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
  934. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  935. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  936. (struct create_lease_v2, lcontext));
  937. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
  938. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  939. (struct create_lease_v2, Name));
  940. buf->ccontext.NameLength = cpu_to_le16(4);
  941. buf->Name[0] = 'R';
  942. buf->Name[1] = 'q';
  943. buf->Name[2] = 'L';
  944. buf->Name[3] = 's';
  945. return (char *)buf;
  946. }
  947. static __u8
  948. smb2_parse_lease_buf(void *buf, unsigned int *epoch)
  949. {
  950. struct create_lease *lc = (struct create_lease *)buf;
  951. *epoch = 0; /* not used */
  952. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  953. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  954. return le32_to_cpu(lc->lcontext.LeaseState);
  955. }
  956. static __u8
  957. smb3_parse_lease_buf(void *buf, unsigned int *epoch)
  958. {
  959. struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
  960. *epoch = le16_to_cpu(lc->lcontext.Epoch);
  961. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  962. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  963. return le32_to_cpu(lc->lcontext.LeaseState);
  964. }
  965. struct smb_version_operations smb20_operations = {
  966. .compare_fids = smb2_compare_fids,
  967. .setup_request = smb2_setup_request,
  968. .setup_async_request = smb2_setup_async_request,
  969. .check_receive = smb2_check_receive,
  970. .add_credits = smb2_add_credits,
  971. .set_credits = smb2_set_credits,
  972. .get_credits_field = smb2_get_credits_field,
  973. .get_credits = smb2_get_credits,
  974. .get_next_mid = smb2_get_next_mid,
  975. .read_data_offset = smb2_read_data_offset,
  976. .read_data_length = smb2_read_data_length,
  977. .map_error = map_smb2_to_linux_error,
  978. .find_mid = smb2_find_mid,
  979. .check_message = smb2_check_message,
  980. .dump_detail = smb2_dump_detail,
  981. .clear_stats = smb2_clear_stats,
  982. .print_stats = smb2_print_stats,
  983. .is_oplock_break = smb2_is_valid_oplock_break,
  984. .need_neg = smb2_need_neg,
  985. .negotiate = smb2_negotiate,
  986. .negotiate_wsize = smb2_negotiate_wsize,
  987. .negotiate_rsize = smb2_negotiate_rsize,
  988. .sess_setup = SMB2_sess_setup,
  989. .logoff = SMB2_logoff,
  990. .tree_connect = SMB2_tcon,
  991. .tree_disconnect = SMB2_tdis,
  992. .qfs_tcon = smb2_qfs_tcon,
  993. .is_path_accessible = smb2_is_path_accessible,
  994. .can_echo = smb2_can_echo,
  995. .echo = SMB2_echo,
  996. .query_path_info = smb2_query_path_info,
  997. .get_srv_inum = smb2_get_srv_inum,
  998. .query_file_info = smb2_query_file_info,
  999. .set_path_size = smb2_set_path_size,
  1000. .set_file_size = smb2_set_file_size,
  1001. .set_file_info = smb2_set_file_info,
  1002. .set_compression = smb2_set_compression,
  1003. .mkdir = smb2_mkdir,
  1004. .mkdir_setinfo = smb2_mkdir_setinfo,
  1005. .rmdir = smb2_rmdir,
  1006. .unlink = smb2_unlink,
  1007. .rename = smb2_rename_path,
  1008. .create_hardlink = smb2_create_hardlink,
  1009. .query_symlink = smb2_query_symlink,
  1010. .open = smb2_open_file,
  1011. .set_fid = smb2_set_fid,
  1012. .close = smb2_close_file,
  1013. .flush = smb2_flush_file,
  1014. .async_readv = smb2_async_readv,
  1015. .async_writev = smb2_async_writev,
  1016. .sync_read = smb2_sync_read,
  1017. .sync_write = smb2_sync_write,
  1018. .query_dir_first = smb2_query_dir_first,
  1019. .query_dir_next = smb2_query_dir_next,
  1020. .close_dir = smb2_close_dir,
  1021. .calc_smb_size = smb2_calc_size,
  1022. .is_status_pending = smb2_is_status_pending,
  1023. .oplock_response = smb2_oplock_response,
  1024. .queryfs = smb2_queryfs,
  1025. .mand_lock = smb2_mand_lock,
  1026. .mand_unlock_range = smb2_unlock_range,
  1027. .push_mand_locks = smb2_push_mandatory_locks,
  1028. .get_lease_key = smb2_get_lease_key,
  1029. .set_lease_key = smb2_set_lease_key,
  1030. .new_lease_key = smb2_new_lease_key,
  1031. .calc_signature = smb2_calc_signature,
  1032. .is_read_op = smb2_is_read_op,
  1033. .set_oplock_level = smb2_set_oplock_level,
  1034. .create_lease_buf = smb2_create_lease_buf,
  1035. .parse_lease_buf = smb2_parse_lease_buf,
  1036. .clone_range = smb2_clone_range,
  1037. };
  1038. struct smb_version_operations smb21_operations = {
  1039. .compare_fids = smb2_compare_fids,
  1040. .setup_request = smb2_setup_request,
  1041. .setup_async_request = smb2_setup_async_request,
  1042. .check_receive = smb2_check_receive,
  1043. .add_credits = smb2_add_credits,
  1044. .set_credits = smb2_set_credits,
  1045. .get_credits_field = smb2_get_credits_field,
  1046. .get_credits = smb2_get_credits,
  1047. .get_next_mid = smb2_get_next_mid,
  1048. .read_data_offset = smb2_read_data_offset,
  1049. .read_data_length = smb2_read_data_length,
  1050. .map_error = map_smb2_to_linux_error,
  1051. .find_mid = smb2_find_mid,
  1052. .check_message = smb2_check_message,
  1053. .dump_detail = smb2_dump_detail,
  1054. .clear_stats = smb2_clear_stats,
  1055. .print_stats = smb2_print_stats,
  1056. .is_oplock_break = smb2_is_valid_oplock_break,
  1057. .need_neg = smb2_need_neg,
  1058. .negotiate = smb2_negotiate,
  1059. .negotiate_wsize = smb2_negotiate_wsize,
  1060. .negotiate_rsize = smb2_negotiate_rsize,
  1061. .sess_setup = SMB2_sess_setup,
  1062. .logoff = SMB2_logoff,
  1063. .tree_connect = SMB2_tcon,
  1064. .tree_disconnect = SMB2_tdis,
  1065. .qfs_tcon = smb2_qfs_tcon,
  1066. .is_path_accessible = smb2_is_path_accessible,
  1067. .can_echo = smb2_can_echo,
  1068. .echo = SMB2_echo,
  1069. .query_path_info = smb2_query_path_info,
  1070. .get_srv_inum = smb2_get_srv_inum,
  1071. .query_file_info = smb2_query_file_info,
  1072. .set_path_size = smb2_set_path_size,
  1073. .set_file_size = smb2_set_file_size,
  1074. .set_file_info = smb2_set_file_info,
  1075. .set_compression = smb2_set_compression,
  1076. .mkdir = smb2_mkdir,
  1077. .mkdir_setinfo = smb2_mkdir_setinfo,
  1078. .rmdir = smb2_rmdir,
  1079. .unlink = smb2_unlink,
  1080. .rename = smb2_rename_path,
  1081. .create_hardlink = smb2_create_hardlink,
  1082. .query_symlink = smb2_query_symlink,
  1083. .open = smb2_open_file,
  1084. .set_fid = smb2_set_fid,
  1085. .close = smb2_close_file,
  1086. .flush = smb2_flush_file,
  1087. .async_readv = smb2_async_readv,
  1088. .async_writev = smb2_async_writev,
  1089. .sync_read = smb2_sync_read,
  1090. .sync_write = smb2_sync_write,
  1091. .query_dir_first = smb2_query_dir_first,
  1092. .query_dir_next = smb2_query_dir_next,
  1093. .close_dir = smb2_close_dir,
  1094. .calc_smb_size = smb2_calc_size,
  1095. .is_status_pending = smb2_is_status_pending,
  1096. .oplock_response = smb2_oplock_response,
  1097. .queryfs = smb2_queryfs,
  1098. .mand_lock = smb2_mand_lock,
  1099. .mand_unlock_range = smb2_unlock_range,
  1100. .push_mand_locks = smb2_push_mandatory_locks,
  1101. .get_lease_key = smb2_get_lease_key,
  1102. .set_lease_key = smb2_set_lease_key,
  1103. .new_lease_key = smb2_new_lease_key,
  1104. .calc_signature = smb2_calc_signature,
  1105. .is_read_op = smb21_is_read_op,
  1106. .set_oplock_level = smb21_set_oplock_level,
  1107. .create_lease_buf = smb2_create_lease_buf,
  1108. .parse_lease_buf = smb2_parse_lease_buf,
  1109. .clone_range = smb2_clone_range,
  1110. };
  1111. struct smb_version_operations smb30_operations = {
  1112. .compare_fids = smb2_compare_fids,
  1113. .setup_request = smb2_setup_request,
  1114. .setup_async_request = smb2_setup_async_request,
  1115. .check_receive = smb2_check_receive,
  1116. .add_credits = smb2_add_credits,
  1117. .set_credits = smb2_set_credits,
  1118. .get_credits_field = smb2_get_credits_field,
  1119. .get_credits = smb2_get_credits,
  1120. .get_next_mid = smb2_get_next_mid,
  1121. .read_data_offset = smb2_read_data_offset,
  1122. .read_data_length = smb2_read_data_length,
  1123. .map_error = map_smb2_to_linux_error,
  1124. .find_mid = smb2_find_mid,
  1125. .check_message = smb2_check_message,
  1126. .dump_detail = smb2_dump_detail,
  1127. .clear_stats = smb2_clear_stats,
  1128. .print_stats = smb2_print_stats,
  1129. .dump_share_caps = smb2_dump_share_caps,
  1130. .is_oplock_break = smb2_is_valid_oplock_break,
  1131. .need_neg = smb2_need_neg,
  1132. .negotiate = smb2_negotiate,
  1133. .negotiate_wsize = smb2_negotiate_wsize,
  1134. .negotiate_rsize = smb2_negotiate_rsize,
  1135. .sess_setup = SMB2_sess_setup,
  1136. .logoff = SMB2_logoff,
  1137. .tree_connect = SMB2_tcon,
  1138. .tree_disconnect = SMB2_tdis,
  1139. .qfs_tcon = smb3_qfs_tcon,
  1140. .is_path_accessible = smb2_is_path_accessible,
  1141. .can_echo = smb2_can_echo,
  1142. .echo = SMB2_echo,
  1143. .query_path_info = smb2_query_path_info,
  1144. .get_srv_inum = smb2_get_srv_inum,
  1145. .query_file_info = smb2_query_file_info,
  1146. .set_path_size = smb2_set_path_size,
  1147. .set_file_size = smb2_set_file_size,
  1148. .set_file_info = smb2_set_file_info,
  1149. .set_compression = smb2_set_compression,
  1150. .mkdir = smb2_mkdir,
  1151. .mkdir_setinfo = smb2_mkdir_setinfo,
  1152. .rmdir = smb2_rmdir,
  1153. .unlink = smb2_unlink,
  1154. .rename = smb2_rename_path,
  1155. .create_hardlink = smb2_create_hardlink,
  1156. .query_symlink = smb2_query_symlink,
  1157. .open = smb2_open_file,
  1158. .set_fid = smb2_set_fid,
  1159. .close = smb2_close_file,
  1160. .flush = smb2_flush_file,
  1161. .async_readv = smb2_async_readv,
  1162. .async_writev = smb2_async_writev,
  1163. .sync_read = smb2_sync_read,
  1164. .sync_write = smb2_sync_write,
  1165. .query_dir_first = smb2_query_dir_first,
  1166. .query_dir_next = smb2_query_dir_next,
  1167. .close_dir = smb2_close_dir,
  1168. .calc_smb_size = smb2_calc_size,
  1169. .is_status_pending = smb2_is_status_pending,
  1170. .oplock_response = smb2_oplock_response,
  1171. .queryfs = smb2_queryfs,
  1172. .mand_lock = smb2_mand_lock,
  1173. .mand_unlock_range = smb2_unlock_range,
  1174. .push_mand_locks = smb2_push_mandatory_locks,
  1175. .get_lease_key = smb2_get_lease_key,
  1176. .set_lease_key = smb2_set_lease_key,
  1177. .new_lease_key = smb2_new_lease_key,
  1178. .generate_signingkey = generate_smb3signingkey,
  1179. .calc_signature = smb3_calc_signature,
  1180. .is_read_op = smb21_is_read_op,
  1181. .set_oplock_level = smb3_set_oplock_level,
  1182. .create_lease_buf = smb3_create_lease_buf,
  1183. .parse_lease_buf = smb3_parse_lease_buf,
  1184. .clone_range = smb2_clone_range,
  1185. .validate_negotiate = smb3_validate_negotiate,
  1186. };
  1187. struct smb_version_values smb20_values = {
  1188. .version_string = SMB20_VERSION_STRING,
  1189. .protocol_id = SMB20_PROT_ID,
  1190. .req_capabilities = 0, /* MBZ */
  1191. .large_lock_type = 0,
  1192. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1193. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1194. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1195. .header_size = sizeof(struct smb2_hdr),
  1196. .max_header_size = MAX_SMB2_HDR_SIZE,
  1197. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1198. .lock_cmd = SMB2_LOCK,
  1199. .cap_unix = 0,
  1200. .cap_nt_find = SMB2_NT_FIND,
  1201. .cap_large_files = SMB2_LARGE_FILES,
  1202. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1203. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1204. .create_lease_size = sizeof(struct create_lease),
  1205. };
  1206. struct smb_version_values smb21_values = {
  1207. .version_string = SMB21_VERSION_STRING,
  1208. .protocol_id = SMB21_PROT_ID,
  1209. .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
  1210. .large_lock_type = 0,
  1211. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1212. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1213. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1214. .header_size = sizeof(struct smb2_hdr),
  1215. .max_header_size = MAX_SMB2_HDR_SIZE,
  1216. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1217. .lock_cmd = SMB2_LOCK,
  1218. .cap_unix = 0,
  1219. .cap_nt_find = SMB2_NT_FIND,
  1220. .cap_large_files = SMB2_LARGE_FILES,
  1221. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1222. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1223. .create_lease_size = sizeof(struct create_lease),
  1224. };
  1225. struct smb_version_values smb30_values = {
  1226. .version_string = SMB30_VERSION_STRING,
  1227. .protocol_id = SMB30_PROT_ID,
  1228. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
  1229. .large_lock_type = 0,
  1230. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1231. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1232. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1233. .header_size = sizeof(struct smb2_hdr),
  1234. .max_header_size = MAX_SMB2_HDR_SIZE,
  1235. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1236. .lock_cmd = SMB2_LOCK,
  1237. .cap_unix = 0,
  1238. .cap_nt_find = SMB2_NT_FIND,
  1239. .cap_large_files = SMB2_LARGE_FILES,
  1240. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1241. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1242. .create_lease_size = sizeof(struct create_lease_v2),
  1243. };
  1244. struct smb_version_values smb302_values = {
  1245. .version_string = SMB302_VERSION_STRING,
  1246. .protocol_id = SMB302_PROT_ID,
  1247. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
  1248. .large_lock_type = 0,
  1249. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1250. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1251. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1252. .header_size = sizeof(struct smb2_hdr),
  1253. .max_header_size = MAX_SMB2_HDR_SIZE,
  1254. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1255. .lock_cmd = SMB2_LOCK,
  1256. .cap_unix = 0,
  1257. .cap_nt_find = SMB2_NT_FIND,
  1258. .cap_large_files = SMB2_LARGE_FILES,
  1259. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1260. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1261. .create_lease_size = sizeof(struct create_lease_v2),
  1262. };