smb2pdu.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  1. /*
  2. * fs/cifs/smb2pdu.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2009, 2013
  5. * Etersoft, 2012
  6. * Author(s): Steve French (sfrench@us.ibm.com)
  7. * Pavel Shilovsky (pshilovsky@samba.org) 2012
  8. *
  9. * Contains the routines for constructing the SMB2 PDUs themselves
  10. *
  11. * This library is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published
  13. * by the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  19. * the GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
  26. /* Note that there are handle based routines which must be */
  27. /* treated slightly differently for reconnection purposes since we never */
  28. /* want to reuse a stale file handle and only the caller knows the file info */
  29. #include <linux/fs.h>
  30. #include <linux/kernel.h>
  31. #include <linux/vfs.h>
  32. #include <linux/task_io_accounting_ops.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/xattr.h>
  36. #include "smb2pdu.h"
  37. #include "cifsglob.h"
  38. #include "cifsacl.h"
  39. #include "cifsproto.h"
  40. #include "smb2proto.h"
  41. #include "cifs_unicode.h"
  42. #include "cifs_debug.h"
  43. #include "ntlmssp.h"
  44. #include "smb2status.h"
  45. #include "smb2glob.h"
  46. #include "cifspdu.h"
  47. /*
  48. * The following table defines the expected "StructureSize" of SMB2 requests
  49. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
  50. *
  51. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  52. * indexed by command in host byte order.
  53. */
  54. static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  55. /* SMB2_NEGOTIATE */ 36,
  56. /* SMB2_SESSION_SETUP */ 25,
  57. /* SMB2_LOGOFF */ 4,
  58. /* SMB2_TREE_CONNECT */ 9,
  59. /* SMB2_TREE_DISCONNECT */ 4,
  60. /* SMB2_CREATE */ 57,
  61. /* SMB2_CLOSE */ 24,
  62. /* SMB2_FLUSH */ 24,
  63. /* SMB2_READ */ 49,
  64. /* SMB2_WRITE */ 49,
  65. /* SMB2_LOCK */ 48,
  66. /* SMB2_IOCTL */ 57,
  67. /* SMB2_CANCEL */ 4,
  68. /* SMB2_ECHO */ 4,
  69. /* SMB2_QUERY_DIRECTORY */ 33,
  70. /* SMB2_CHANGE_NOTIFY */ 32,
  71. /* SMB2_QUERY_INFO */ 41,
  72. /* SMB2_SET_INFO */ 33,
  73. /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
  74. };
  75. static void
  76. smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
  77. const struct cifs_tcon *tcon)
  78. {
  79. struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
  80. char *temp = (char *)hdr;
  81. /* lookup word count ie StructureSize from table */
  82. __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
  83. /*
  84. * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
  85. * largest operations (Create)
  86. */
  87. memset(temp, 0, 256);
  88. /* Note this is only network field converted to big endian */
  89. hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
  90. - 4 /* RFC 1001 length field itself not counted */);
  91. hdr->ProtocolId[0] = 0xFE;
  92. hdr->ProtocolId[1] = 'S';
  93. hdr->ProtocolId[2] = 'M';
  94. hdr->ProtocolId[3] = 'B';
  95. hdr->StructureSize = cpu_to_le16(64);
  96. hdr->Command = smb2_cmd;
  97. hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
  98. hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
  99. if (!tcon)
  100. goto out;
  101. /* BB FIXME when we do write > 64K add +1 for every 64K in req or rsp */
  102. /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
  103. /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
  104. if ((tcon->ses) &&
  105. (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  106. hdr->CreditCharge = cpu_to_le16(1);
  107. /* else CreditCharge MBZ */
  108. hdr->TreeId = tcon->tid;
  109. /* Uid is not converted */
  110. if (tcon->ses)
  111. hdr->SessionId = tcon->ses->Suid;
  112. /*
  113. * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
  114. * to pass the path on the Open SMB prefixed by \\server\share.
  115. * Not sure when we would need to do the augmented path (if ever) and
  116. * setting this flag breaks the SMB2 open operation since it is
  117. * illegal to send an empty path name (without \\server\share prefix)
  118. * when the DFS flag is set in the SMB open header. We could
  119. * consider setting the flag on all operations other than open
  120. * but it is safer to net set it for now.
  121. */
  122. /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
  123. hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
  124. if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
  125. hdr->Flags |= SMB2_FLAGS_SIGNED;
  126. out:
  127. pdu->StructureSize2 = cpu_to_le16(parmsize);
  128. return;
  129. }
  130. static int
  131. smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
  132. {
  133. int rc = 0;
  134. struct nls_table *nls_codepage;
  135. struct cifs_ses *ses;
  136. struct TCP_Server_Info *server;
  137. /*
  138. * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
  139. * check for tcp and smb session status done differently
  140. * for those three - in the calling routine.
  141. */
  142. if (tcon == NULL)
  143. return rc;
  144. if (smb2_command == SMB2_TREE_CONNECT)
  145. return rc;
  146. if (tcon->tidStatus == CifsExiting) {
  147. /*
  148. * only tree disconnect, open, and write,
  149. * (and ulogoff which does not have tcon)
  150. * are allowed as we start force umount.
  151. */
  152. if ((smb2_command != SMB2_WRITE) &&
  153. (smb2_command != SMB2_CREATE) &&
  154. (smb2_command != SMB2_TREE_DISCONNECT)) {
  155. cifs_dbg(FYI, "can not send cmd %d while umounting\n",
  156. smb2_command);
  157. return -ENODEV;
  158. }
  159. }
  160. if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
  161. (!tcon->ses->server))
  162. return -EIO;
  163. ses = tcon->ses;
  164. server = ses->server;
  165. /*
  166. * Give demultiplex thread up to 10 seconds to reconnect, should be
  167. * greater than cifs socket timeout which is 7 seconds
  168. */
  169. while (server->tcpStatus == CifsNeedReconnect) {
  170. /*
  171. * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
  172. * here since they are implicitly done when session drops.
  173. */
  174. switch (smb2_command) {
  175. /*
  176. * BB Should we keep oplock break and add flush to exceptions?
  177. */
  178. case SMB2_TREE_DISCONNECT:
  179. case SMB2_CANCEL:
  180. case SMB2_CLOSE:
  181. case SMB2_OPLOCK_BREAK:
  182. return -EAGAIN;
  183. }
  184. wait_event_interruptible_timeout(server->response_q,
  185. (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
  186. /* are we still trying to reconnect? */
  187. if (server->tcpStatus != CifsNeedReconnect)
  188. break;
  189. /*
  190. * on "soft" mounts we wait once. Hard mounts keep
  191. * retrying until process is killed or server comes
  192. * back on-line
  193. */
  194. if (!tcon->retry) {
  195. cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
  196. return -EHOSTDOWN;
  197. }
  198. }
  199. if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
  200. return rc;
  201. nls_codepage = load_nls_default();
  202. /*
  203. * need to prevent multiple threads trying to simultaneously reconnect
  204. * the same SMB session
  205. */
  206. mutex_lock(&tcon->ses->session_mutex);
  207. rc = cifs_negotiate_protocol(0, tcon->ses);
  208. if (!rc && tcon->ses->need_reconnect)
  209. rc = cifs_setup_session(0, tcon->ses, nls_codepage);
  210. if (rc || !tcon->need_reconnect) {
  211. mutex_unlock(&tcon->ses->session_mutex);
  212. goto out;
  213. }
  214. cifs_mark_open_files_invalid(tcon);
  215. rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
  216. mutex_unlock(&tcon->ses->session_mutex);
  217. cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
  218. if (rc)
  219. goto out;
  220. atomic_inc(&tconInfoReconnectCount);
  221. /*
  222. * BB FIXME add code to check if wsize needs update due to negotiated
  223. * smb buffer size shrinking.
  224. */
  225. out:
  226. /*
  227. * Check if handle based operation so we know whether we can continue
  228. * or not without returning to caller to reset file handle.
  229. */
  230. /*
  231. * BB Is flush done by server on drop of tcp session? Should we special
  232. * case it and skip above?
  233. */
  234. switch (smb2_command) {
  235. case SMB2_FLUSH:
  236. case SMB2_READ:
  237. case SMB2_WRITE:
  238. case SMB2_LOCK:
  239. case SMB2_IOCTL:
  240. case SMB2_QUERY_DIRECTORY:
  241. case SMB2_CHANGE_NOTIFY:
  242. case SMB2_QUERY_INFO:
  243. case SMB2_SET_INFO:
  244. return -EAGAIN;
  245. }
  246. unload_nls(nls_codepage);
  247. return rc;
  248. }
  249. /*
  250. * Allocate and return pointer to an SMB request hdr, and set basic
  251. * SMB information in the SMB header. If the return code is zero, this
  252. * function must have filled in request_buf pointer.
  253. */
  254. static int
  255. small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
  256. void **request_buf)
  257. {
  258. int rc = 0;
  259. rc = smb2_reconnect(smb2_command, tcon);
  260. if (rc)
  261. return rc;
  262. /* BB eventually switch this to SMB2 specific small buf size */
  263. *request_buf = cifs_small_buf_get();
  264. if (*request_buf == NULL) {
  265. /* BB should we add a retry in here if not a writepage? */
  266. return -ENOMEM;
  267. }
  268. smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
  269. if (tcon != NULL) {
  270. #ifdef CONFIG_CIFS_STATS2
  271. uint16_t com_code = le16_to_cpu(smb2_command);
  272. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
  273. #endif
  274. cifs_stats_inc(&tcon->num_smbs_sent);
  275. }
  276. return rc;
  277. }
  278. static void
  279. free_rsp_buf(int resp_buftype, void *rsp)
  280. {
  281. if (resp_buftype == CIFS_SMALL_BUFFER)
  282. cifs_small_buf_release(rsp);
  283. else if (resp_buftype == CIFS_LARGE_BUFFER)
  284. cifs_buf_release(rsp);
  285. }
  286. /*
  287. *
  288. * SMB2 Worker functions follow:
  289. *
  290. * The general structure of the worker functions is:
  291. * 1) Call smb2_init (assembles SMB2 header)
  292. * 2) Initialize SMB2 command specific fields in fixed length area of SMB
  293. * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
  294. * 4) Decode SMB2 command specific fields in the fixed length area
  295. * 5) Decode variable length data area (if any for this SMB2 command type)
  296. * 6) Call free smb buffer
  297. * 7) return
  298. *
  299. */
  300. int
  301. SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  302. {
  303. struct smb2_negotiate_req *req;
  304. struct smb2_negotiate_rsp *rsp;
  305. struct kvec iov[1];
  306. int rc = 0;
  307. int resp_buftype;
  308. struct TCP_Server_Info *server = ses->server;
  309. int blob_offset, blob_length;
  310. char *security_blob;
  311. int flags = CIFS_NEG_OP;
  312. cifs_dbg(FYI, "Negotiate protocol\n");
  313. if (!server) {
  314. WARN(1, "%s: server is NULL!\n", __func__);
  315. return -EIO;
  316. }
  317. rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
  318. if (rc)
  319. return rc;
  320. req->hdr.SessionId = 0;
  321. req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
  322. req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
  323. inc_rfc1001_len(req, 2);
  324. /* only one of SMB2 signing flags may be set in SMB2 request */
  325. if (ses->sign)
  326. req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
  327. else if (global_secflags & CIFSSEC_MAY_SIGN)
  328. req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
  329. else
  330. req->SecurityMode = 0;
  331. req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
  332. memcpy(req->ClientGUID, cifs_client_guid, SMB2_CLIENT_GUID_SIZE);
  333. iov[0].iov_base = (char *)req;
  334. /* 4 for rfc1002 length field */
  335. iov[0].iov_len = get_rfc1002_length(req) + 4;
  336. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
  337. rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
  338. /*
  339. * No tcon so can't do
  340. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  341. */
  342. if (rc != 0)
  343. goto neg_exit;
  344. cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
  345. /* BB we may eventually want to match the negotiated vs. requested
  346. dialect, even though we are only requesting one at a time */
  347. if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
  348. cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
  349. else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
  350. cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
  351. else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
  352. cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
  353. else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
  354. cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
  355. else {
  356. cifs_dbg(VFS, "Illegal dialect returned by server %d\n",
  357. le16_to_cpu(rsp->DialectRevision));
  358. rc = -EIO;
  359. goto neg_exit;
  360. }
  361. server->dialect = le16_to_cpu(rsp->DialectRevision);
  362. /* SMB2 only has an extended negflavor */
  363. server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
  364. server->maxBuf = le32_to_cpu(rsp->MaxTransactSize);
  365. server->max_read = le32_to_cpu(rsp->MaxReadSize);
  366. server->max_write = le32_to_cpu(rsp->MaxWriteSize);
  367. /* BB Do we need to validate the SecurityMode? */
  368. server->sec_mode = le16_to_cpu(rsp->SecurityMode);
  369. server->capabilities = le32_to_cpu(rsp->Capabilities);
  370. /* Internal types */
  371. server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
  372. security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
  373. &rsp->hdr);
  374. /*
  375. * See MS-SMB2 section 2.2.4: if no blob, client picks default which
  376. * for us will be
  377. * ses->sectype = RawNTLMSSP;
  378. * but for time being this is our only auth choice so doesn't matter.
  379. * We just found a server which sets blob length to zero expecting raw.
  380. */
  381. if (blob_length == 0)
  382. cifs_dbg(FYI, "missing security blob on negprot\n");
  383. rc = cifs_enable_signing(server, ses->sign);
  384. #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
  385. if (rc)
  386. goto neg_exit;
  387. if (blob_length)
  388. rc = decode_neg_token_init(security_blob, blob_length,
  389. &server->sec_type);
  390. if (rc == 1)
  391. rc = 0;
  392. else if (rc == 0) {
  393. rc = -EIO;
  394. goto neg_exit;
  395. }
  396. #endif
  397. neg_exit:
  398. free_rsp_buf(resp_buftype, rsp);
  399. return rc;
  400. }
  401. int
  402. SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
  403. const struct nls_table *nls_cp)
  404. {
  405. struct smb2_sess_setup_req *req;
  406. struct smb2_sess_setup_rsp *rsp = NULL;
  407. struct kvec iov[2];
  408. int rc = 0;
  409. int resp_buftype;
  410. __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
  411. struct TCP_Server_Info *server = ses->server;
  412. u16 blob_length = 0;
  413. char *security_blob;
  414. char *ntlmssp_blob = NULL;
  415. bool use_spnego = false; /* else use raw ntlmssp */
  416. cifs_dbg(FYI, "Session Setup\n");
  417. if (!server) {
  418. WARN(1, "%s: server is NULL!\n", __func__);
  419. return -EIO;
  420. }
  421. /*
  422. * If memory allocation is successful, caller of this function
  423. * frees it.
  424. */
  425. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  426. if (!ses->ntlmssp)
  427. return -ENOMEM;
  428. /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
  429. ses->sectype = RawNTLMSSP;
  430. ssetup_ntlmssp_authenticate:
  431. if (phase == NtLmChallenge)
  432. phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
  433. rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
  434. if (rc)
  435. return rc;
  436. req->hdr.SessionId = 0; /* First session, not a reauthenticate */
  437. req->VcNumber = 0; /* MBZ */
  438. /* to enable echos and oplocks */
  439. req->hdr.CreditRequest = cpu_to_le16(3);
  440. /* only one of SMB2 signing flags may be set in SMB2 request */
  441. if (server->sign)
  442. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  443. else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
  444. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
  445. else
  446. req->SecurityMode = 0;
  447. req->Capabilities = 0;
  448. req->Channel = 0; /* MBZ */
  449. iov[0].iov_base = (char *)req;
  450. /* 4 for rfc1002 length field and 1 for pad */
  451. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  452. if (phase == NtLmNegotiate) {
  453. ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
  454. GFP_KERNEL);
  455. if (ntlmssp_blob == NULL) {
  456. rc = -ENOMEM;
  457. goto ssetup_exit;
  458. }
  459. build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
  460. if (use_spnego) {
  461. /* blob_length = build_spnego_ntlmssp_blob(
  462. &security_blob,
  463. sizeof(struct _NEGOTIATE_MESSAGE),
  464. ntlmssp_blob); */
  465. /* BB eventually need to add this */
  466. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  467. rc = -EOPNOTSUPP;
  468. kfree(ntlmssp_blob);
  469. goto ssetup_exit;
  470. } else {
  471. blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
  472. /* with raw NTLMSSP we don't encapsulate in SPNEGO */
  473. security_blob = ntlmssp_blob;
  474. }
  475. } else if (phase == NtLmAuthenticate) {
  476. req->hdr.SessionId = ses->Suid;
  477. ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
  478. GFP_KERNEL);
  479. if (ntlmssp_blob == NULL) {
  480. rc = -ENOMEM;
  481. goto ssetup_exit;
  482. }
  483. rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
  484. nls_cp);
  485. if (rc) {
  486. cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
  487. rc);
  488. goto ssetup_exit; /* BB double check error handling */
  489. }
  490. if (use_spnego) {
  491. /* blob_length = build_spnego_ntlmssp_blob(
  492. &security_blob,
  493. blob_length,
  494. ntlmssp_blob); */
  495. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  496. rc = -EOPNOTSUPP;
  497. kfree(ntlmssp_blob);
  498. goto ssetup_exit;
  499. } else {
  500. security_blob = ntlmssp_blob;
  501. }
  502. } else {
  503. cifs_dbg(VFS, "illegal ntlmssp phase\n");
  504. rc = -EIO;
  505. goto ssetup_exit;
  506. }
  507. /* Testing shows that buffer offset must be at location of Buffer[0] */
  508. req->SecurityBufferOffset =
  509. cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
  510. 1 /* pad */ - 4 /* rfc1001 len */);
  511. req->SecurityBufferLength = cpu_to_le16(blob_length);
  512. iov[1].iov_base = security_blob;
  513. iov[1].iov_len = blob_length;
  514. inc_rfc1001_len(req, blob_length - 1 /* pad */);
  515. /* BB add code to build os and lm fields */
  516. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
  517. CIFS_LOG_ERROR | CIFS_NEG_OP);
  518. kfree(security_blob);
  519. rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
  520. if (resp_buftype != CIFS_NO_BUFFER &&
  521. rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
  522. if (phase != NtLmNegotiate) {
  523. cifs_dbg(VFS, "Unexpected more processing error\n");
  524. goto ssetup_exit;
  525. }
  526. if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
  527. le16_to_cpu(rsp->SecurityBufferOffset)) {
  528. cifs_dbg(VFS, "Invalid security buffer offset %d\n",
  529. le16_to_cpu(rsp->SecurityBufferOffset));
  530. rc = -EIO;
  531. goto ssetup_exit;
  532. }
  533. /* NTLMSSP Negotiate sent now processing challenge (response) */
  534. phase = NtLmChallenge; /* process ntlmssp challenge */
  535. rc = 0; /* MORE_PROCESSING is not an error here but expected */
  536. ses->Suid = rsp->hdr.SessionId;
  537. rc = decode_ntlmssp_challenge(rsp->Buffer,
  538. le16_to_cpu(rsp->SecurityBufferLength), ses);
  539. }
  540. /*
  541. * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
  542. * but at least the raw NTLMSSP case works.
  543. */
  544. /*
  545. * No tcon so can't do
  546. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  547. */
  548. if (rc != 0)
  549. goto ssetup_exit;
  550. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  551. ssetup_exit:
  552. free_rsp_buf(resp_buftype, rsp);
  553. /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
  554. if ((phase == NtLmChallenge) && (rc == 0))
  555. goto ssetup_ntlmssp_authenticate;
  556. return rc;
  557. }
  558. int
  559. SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
  560. {
  561. struct smb2_logoff_req *req; /* response is also trivial struct */
  562. int rc = 0;
  563. struct TCP_Server_Info *server;
  564. cifs_dbg(FYI, "disconnect session %p\n", ses);
  565. if (ses && (ses->server))
  566. server = ses->server;
  567. else
  568. return -EIO;
  569. rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
  570. if (rc)
  571. return rc;
  572. /* since no tcon, smb2_init can not do this, so do here */
  573. req->hdr.SessionId = ses->Suid;
  574. if (server->sign)
  575. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  576. rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
  577. /*
  578. * No tcon so can't do
  579. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  580. */
  581. return rc;
  582. }
  583. static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
  584. {
  585. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
  586. }
  587. #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
  588. int
  589. SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
  590. struct cifs_tcon *tcon, const struct nls_table *cp)
  591. {
  592. struct smb2_tree_connect_req *req;
  593. struct smb2_tree_connect_rsp *rsp = NULL;
  594. struct kvec iov[2];
  595. int rc = 0;
  596. int resp_buftype;
  597. int unc_path_len;
  598. struct TCP_Server_Info *server;
  599. __le16 *unc_path = NULL;
  600. cifs_dbg(FYI, "TCON\n");
  601. if ((ses->server) && tree)
  602. server = ses->server;
  603. else
  604. return -EIO;
  605. if (tcon && tcon->bad_network_name)
  606. return -ENOENT;
  607. unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
  608. if (unc_path == NULL)
  609. return -ENOMEM;
  610. unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
  611. unc_path_len *= 2;
  612. if (unc_path_len < 2) {
  613. kfree(unc_path);
  614. return -EINVAL;
  615. }
  616. rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
  617. if (rc) {
  618. kfree(unc_path);
  619. return rc;
  620. }
  621. if (tcon == NULL) {
  622. /* since no tcon, smb2_init can not do this, so do here */
  623. req->hdr.SessionId = ses->Suid;
  624. /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
  625. req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
  626. }
  627. iov[0].iov_base = (char *)req;
  628. /* 4 for rfc1002 length field and 1 for pad */
  629. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  630. /* Testing shows that buffer offset must be at location of Buffer[0] */
  631. req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
  632. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  633. req->PathLength = cpu_to_le16(unc_path_len - 2);
  634. iov[1].iov_base = unc_path;
  635. iov[1].iov_len = unc_path_len;
  636. inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
  637. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  638. rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
  639. if (rc != 0) {
  640. if (tcon) {
  641. cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
  642. tcon->need_reconnect = true;
  643. }
  644. goto tcon_error_exit;
  645. }
  646. if (tcon == NULL) {
  647. ses->ipc_tid = rsp->hdr.TreeId;
  648. goto tcon_exit;
  649. }
  650. if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
  651. cifs_dbg(FYI, "connection to disk share\n");
  652. else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
  653. tcon->ipc = true;
  654. cifs_dbg(FYI, "connection to pipe share\n");
  655. } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
  656. tcon->print = true;
  657. cifs_dbg(FYI, "connection to printer\n");
  658. } else {
  659. cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
  660. rc = -EOPNOTSUPP;
  661. goto tcon_error_exit;
  662. }
  663. tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
  664. tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
  665. tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
  666. tcon->tidStatus = CifsGood;
  667. tcon->need_reconnect = false;
  668. tcon->tid = rsp->hdr.TreeId;
  669. strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
  670. if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
  671. ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
  672. cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
  673. tcon_exit:
  674. free_rsp_buf(resp_buftype, rsp);
  675. kfree(unc_path);
  676. return rc;
  677. tcon_error_exit:
  678. if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
  679. cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
  680. tcon->bad_network_name = true;
  681. }
  682. goto tcon_exit;
  683. }
  684. int
  685. SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
  686. {
  687. struct smb2_tree_disconnect_req *req; /* response is trivial */
  688. int rc = 0;
  689. struct TCP_Server_Info *server;
  690. struct cifs_ses *ses = tcon->ses;
  691. cifs_dbg(FYI, "Tree Disconnect\n");
  692. if (ses && (ses->server))
  693. server = ses->server;
  694. else
  695. return -EIO;
  696. if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
  697. return 0;
  698. rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
  699. if (rc)
  700. return rc;
  701. rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
  702. if (rc)
  703. cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
  704. return rc;
  705. }
  706. static struct create_lease *
  707. create_lease_buf(u8 *lease_key, u8 oplock)
  708. {
  709. struct create_lease *buf;
  710. buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
  711. if (!buf)
  712. return NULL;
  713. buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
  714. buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
  715. if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  716. buf->lcontext.LeaseState = SMB2_LEASE_WRITE_CACHING |
  717. SMB2_LEASE_READ_CACHING;
  718. else if (oplock == SMB2_OPLOCK_LEVEL_II)
  719. buf->lcontext.LeaseState = SMB2_LEASE_READ_CACHING;
  720. else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
  721. buf->lcontext.LeaseState = SMB2_LEASE_HANDLE_CACHING |
  722. SMB2_LEASE_READ_CACHING |
  723. SMB2_LEASE_WRITE_CACHING;
  724. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  725. (struct create_lease, lcontext));
  726. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
  727. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  728. (struct create_lease, Name));
  729. buf->ccontext.NameLength = cpu_to_le16(4);
  730. buf->Name[0] = 'R';
  731. buf->Name[1] = 'q';
  732. buf->Name[2] = 'L';
  733. buf->Name[3] = 's';
  734. return buf;
  735. }
  736. static __u8
  737. parse_lease_state(struct smb2_create_rsp *rsp)
  738. {
  739. char *data_offset;
  740. struct create_lease *lc;
  741. bool found = false;
  742. unsigned int next = 0;
  743. char *name;
  744. data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
  745. lc = (struct create_lease *)data_offset;
  746. do {
  747. lc = (struct create_lease *)((char *)lc + next);
  748. name = le16_to_cpu(lc->ccontext.NameOffset) + (char *)lc;
  749. if (le16_to_cpu(lc->ccontext.NameLength) != 4 ||
  750. strncmp(name, "RqLs", 4)) {
  751. next = le32_to_cpu(lc->ccontext.Next);
  752. continue;
  753. }
  754. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  755. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  756. found = true;
  757. break;
  758. } while (next != 0);
  759. if (!found)
  760. return 0;
  761. return smb2_map_lease_to_oplock(lc->lcontext.LeaseState);
  762. }
  763. static int
  764. add_lease_context(struct kvec *iov, unsigned int *num_iovec, __u8 *oplock)
  765. {
  766. struct smb2_create_req *req = iov[0].iov_base;
  767. unsigned int num = *num_iovec;
  768. iov[num].iov_base = create_lease_buf(oplock+1, *oplock);
  769. if (iov[num].iov_base == NULL)
  770. return -ENOMEM;
  771. iov[num].iov_len = sizeof(struct create_lease);
  772. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
  773. if (!req->CreateContextsOffset)
  774. req->CreateContextsOffset = cpu_to_le32(
  775. sizeof(struct smb2_create_req) - 4 +
  776. iov[num - 1].iov_len);
  777. req->CreateContextsLength = cpu_to_le32(
  778. le32_to_cpu(req->CreateContextsLength) +
  779. sizeof(struct create_lease));
  780. inc_rfc1001_len(&req->hdr, sizeof(struct create_lease));
  781. *num_iovec = num + 1;
  782. return 0;
  783. }
  784. int
  785. SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path,
  786. u64 *persistent_fid, u64 *volatile_fid, __u32 desired_access,
  787. __u32 create_disposition, __u32 create_options, __u8 *oplock,
  788. struct smb2_file_all_info *buf)
  789. {
  790. struct smb2_create_req *req;
  791. struct smb2_create_rsp *rsp;
  792. struct TCP_Server_Info *server;
  793. struct cifs_ses *ses = tcon->ses;
  794. struct kvec iov[3];
  795. int resp_buftype;
  796. int uni_path_len;
  797. __le16 *copy_path = NULL;
  798. int copy_size;
  799. int rc = 0;
  800. unsigned int num_iovecs = 2;
  801. __u32 file_attributes = 0;
  802. cifs_dbg(FYI, "create/open\n");
  803. if (ses && (ses->server))
  804. server = ses->server;
  805. else
  806. return -EIO;
  807. rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
  808. if (rc)
  809. return rc;
  810. if (create_options & CREATE_OPTION_READONLY)
  811. file_attributes |= ATTR_READONLY;
  812. req->ImpersonationLevel = IL_IMPERSONATION;
  813. req->DesiredAccess = cpu_to_le32(desired_access);
  814. /* File attributes ignored on open (used in create though) */
  815. req->FileAttributes = cpu_to_le32(file_attributes);
  816. req->ShareAccess = FILE_SHARE_ALL_LE;
  817. req->CreateDisposition = cpu_to_le32(create_disposition);
  818. req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
  819. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  820. /* do not count rfc1001 len field */
  821. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
  822. iov[0].iov_base = (char *)req;
  823. /* 4 for rfc1002 length field */
  824. iov[0].iov_len = get_rfc1002_length(req) + 4;
  825. /* MUST set path len (NameLength) to 0 opening root of share */
  826. req->NameLength = cpu_to_le16(uni_path_len - 2);
  827. /* -1 since last byte is buf[0] which is sent below (path) */
  828. iov[0].iov_len--;
  829. if (uni_path_len % 8 != 0) {
  830. copy_size = uni_path_len / 8 * 8;
  831. if (copy_size < uni_path_len)
  832. copy_size += 8;
  833. copy_path = kzalloc(copy_size, GFP_KERNEL);
  834. if (!copy_path)
  835. return -ENOMEM;
  836. memcpy((char *)copy_path, (const char *)path,
  837. uni_path_len);
  838. uni_path_len = copy_size;
  839. path = copy_path;
  840. }
  841. iov[1].iov_len = uni_path_len;
  842. iov[1].iov_base = path;
  843. /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
  844. inc_rfc1001_len(req, uni_path_len - 1);
  845. if (!server->oplocks)
  846. *oplock = SMB2_OPLOCK_LEVEL_NONE;
  847. if (!(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
  848. *oplock == SMB2_OPLOCK_LEVEL_NONE)
  849. req->RequestedOplockLevel = *oplock;
  850. else {
  851. rc = add_lease_context(iov, &num_iovecs, oplock);
  852. if (rc) {
  853. cifs_small_buf_release(req);
  854. kfree(copy_path);
  855. return rc;
  856. }
  857. }
  858. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  859. rsp = (struct smb2_create_rsp *)iov[0].iov_base;
  860. if (rc != 0) {
  861. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  862. goto creat_exit;
  863. }
  864. *persistent_fid = rsp->PersistentFileId;
  865. *volatile_fid = rsp->VolatileFileId;
  866. if (buf) {
  867. memcpy(buf, &rsp->CreationTime, 32);
  868. buf->AllocationSize = rsp->AllocationSize;
  869. buf->EndOfFile = rsp->EndofFile;
  870. buf->Attributes = rsp->FileAttributes;
  871. buf->NumberOfLinks = cpu_to_le32(1);
  872. buf->DeletePending = 0;
  873. }
  874. if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
  875. *oplock = parse_lease_state(rsp);
  876. else
  877. *oplock = rsp->OplockLevel;
  878. creat_exit:
  879. kfree(copy_path);
  880. free_rsp_buf(resp_buftype, rsp);
  881. return rc;
  882. }
  883. /*
  884. * SMB2 IOCTL is used for both IOCTLs and FSCTLs
  885. */
  886. int
  887. SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  888. u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
  889. u32 indatalen, char **out_data, u32 *plen /* returned data len */)
  890. {
  891. struct smb2_ioctl_req *req;
  892. struct smb2_ioctl_rsp *rsp;
  893. struct TCP_Server_Info *server;
  894. struct cifs_ses *ses = tcon->ses;
  895. struct kvec iov[2];
  896. int resp_buftype;
  897. int num_iovecs;
  898. int rc = 0;
  899. cifs_dbg(FYI, "SMB2 IOCTL\n");
  900. /* zero out returned data len, in case of error */
  901. if (plen)
  902. *plen = 0;
  903. if (ses && (ses->server))
  904. server = ses->server;
  905. else
  906. return -EIO;
  907. rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
  908. if (rc)
  909. return rc;
  910. req->CtlCode = cpu_to_le32(opcode);
  911. req->PersistentFileId = persistent_fid;
  912. req->VolatileFileId = volatile_fid;
  913. if (indatalen) {
  914. req->InputCount = cpu_to_le32(indatalen);
  915. /* do not set InputOffset if no input data */
  916. req->InputOffset =
  917. cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
  918. iov[1].iov_base = in_data;
  919. iov[1].iov_len = indatalen;
  920. num_iovecs = 2;
  921. } else
  922. num_iovecs = 1;
  923. req->OutputOffset = 0;
  924. req->OutputCount = 0; /* MBZ */
  925. /*
  926. * Could increase MaxOutputResponse, but that would require more
  927. * than one credit. Windows typically sets this smaller, but for some
  928. * ioctls it may be useful to allow server to send more. No point
  929. * limiting what the server can send as long as fits in one credit
  930. */
  931. req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
  932. if (is_fsctl)
  933. req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
  934. else
  935. req->Flags = 0;
  936. iov[0].iov_base = (char *)req;
  937. /* 4 for rfc1002 length field */
  938. iov[0].iov_len = get_rfc1002_length(req) + 4;
  939. if (indatalen)
  940. inc_rfc1001_len(req, indatalen);
  941. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  942. rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
  943. if (rc != 0) {
  944. if (tcon)
  945. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  946. goto ioctl_exit;
  947. }
  948. /* check if caller wants to look at return data or just return rc */
  949. if ((plen == NULL) || (out_data == NULL))
  950. goto ioctl_exit;
  951. *plen = le32_to_cpu(rsp->OutputCount);
  952. /* We check for obvious errors in the output buffer length and offset */
  953. if (*plen == 0)
  954. goto ioctl_exit; /* server returned no data */
  955. else if (*plen > 0xFF00) {
  956. cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
  957. *plen = 0;
  958. rc = -EIO;
  959. goto ioctl_exit;
  960. }
  961. if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
  962. cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
  963. le32_to_cpu(rsp->OutputOffset));
  964. *plen = 0;
  965. rc = -EIO;
  966. goto ioctl_exit;
  967. }
  968. *out_data = kmalloc(*plen, GFP_KERNEL);
  969. if (*out_data == NULL) {
  970. rc = -ENOMEM;
  971. goto ioctl_exit;
  972. }
  973. memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
  974. *plen);
  975. ioctl_exit:
  976. free_rsp_buf(resp_buftype, rsp);
  977. return rc;
  978. }
  979. int
  980. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  981. u64 persistent_fid, u64 volatile_fid)
  982. {
  983. struct smb2_close_req *req;
  984. struct smb2_close_rsp *rsp;
  985. struct TCP_Server_Info *server;
  986. struct cifs_ses *ses = tcon->ses;
  987. struct kvec iov[1];
  988. int resp_buftype;
  989. int rc = 0;
  990. cifs_dbg(FYI, "Close\n");
  991. if (ses && (ses->server))
  992. server = ses->server;
  993. else
  994. return -EIO;
  995. rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
  996. if (rc)
  997. return rc;
  998. req->PersistentFileId = persistent_fid;
  999. req->VolatileFileId = volatile_fid;
  1000. iov[0].iov_base = (char *)req;
  1001. /* 4 for rfc1002 length field */
  1002. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1003. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1004. rsp = (struct smb2_close_rsp *)iov[0].iov_base;
  1005. if (rc != 0) {
  1006. if (tcon)
  1007. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  1008. goto close_exit;
  1009. }
  1010. /* BB FIXME - decode close response, update inode for caching */
  1011. close_exit:
  1012. free_rsp_buf(resp_buftype, rsp);
  1013. return rc;
  1014. }
  1015. static int
  1016. validate_buf(unsigned int offset, unsigned int buffer_length,
  1017. struct smb2_hdr *hdr, unsigned int min_buf_size)
  1018. {
  1019. unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
  1020. char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
  1021. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1022. char *end_of_buf = begin_of_buf + buffer_length;
  1023. if (buffer_length < min_buf_size) {
  1024. cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
  1025. buffer_length, min_buf_size);
  1026. return -EINVAL;
  1027. }
  1028. /* check if beyond RFC1001 maximum length */
  1029. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  1030. cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
  1031. buffer_length, smb_len);
  1032. return -EINVAL;
  1033. }
  1034. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  1035. cifs_dbg(VFS, "illegal server response, bad offset to data\n");
  1036. return -EINVAL;
  1037. }
  1038. return 0;
  1039. }
  1040. /*
  1041. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  1042. * Caller must free buffer.
  1043. */
  1044. static int
  1045. validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
  1046. struct smb2_hdr *hdr, unsigned int minbufsize,
  1047. char *data)
  1048. {
  1049. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1050. int rc;
  1051. if (!data)
  1052. return -EINVAL;
  1053. rc = validate_buf(offset, buffer_length, hdr, minbufsize);
  1054. if (rc)
  1055. return rc;
  1056. memcpy(data, begin_of_buf, buffer_length);
  1057. return 0;
  1058. }
  1059. static int
  1060. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1061. u64 persistent_fid, u64 volatile_fid, u8 info_class,
  1062. size_t output_len, size_t min_len, void *data)
  1063. {
  1064. struct smb2_query_info_req *req;
  1065. struct smb2_query_info_rsp *rsp = NULL;
  1066. struct kvec iov[2];
  1067. int rc = 0;
  1068. int resp_buftype;
  1069. struct TCP_Server_Info *server;
  1070. struct cifs_ses *ses = tcon->ses;
  1071. cifs_dbg(FYI, "Query Info\n");
  1072. if (ses && (ses->server))
  1073. server = ses->server;
  1074. else
  1075. return -EIO;
  1076. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1077. if (rc)
  1078. return rc;
  1079. req->InfoType = SMB2_O_INFO_FILE;
  1080. req->FileInfoClass = info_class;
  1081. req->PersistentFileId = persistent_fid;
  1082. req->VolatileFileId = volatile_fid;
  1083. /* 4 for rfc1002 length field and 1 for Buffer */
  1084. req->InputBufferOffset =
  1085. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1086. req->OutputBufferLength = cpu_to_le32(output_len);
  1087. iov[0].iov_base = (char *)req;
  1088. /* 4 for rfc1002 length field */
  1089. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1090. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1091. rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
  1092. if (rc) {
  1093. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1094. goto qinf_exit;
  1095. }
  1096. rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1097. le32_to_cpu(rsp->OutputBufferLength),
  1098. &rsp->hdr, min_len, data);
  1099. qinf_exit:
  1100. free_rsp_buf(resp_buftype, rsp);
  1101. return rc;
  1102. }
  1103. int
  1104. SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1105. u64 persistent_fid, u64 volatile_fid,
  1106. struct smb2_file_all_info *data)
  1107. {
  1108. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1109. FILE_ALL_INFORMATION,
  1110. sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  1111. sizeof(struct smb2_file_all_info), data);
  1112. }
  1113. int
  1114. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  1115. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  1116. {
  1117. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1118. FILE_INTERNAL_INFORMATION,
  1119. sizeof(struct smb2_file_internal_info),
  1120. sizeof(struct smb2_file_internal_info), uniqueid);
  1121. }
  1122. /*
  1123. * This is a no-op for now. We're not really interested in the reply, but
  1124. * rather in the fact that the server sent one and that server->lstrp
  1125. * gets updated.
  1126. *
  1127. * FIXME: maybe we should consider checking that the reply matches request?
  1128. */
  1129. static void
  1130. smb2_echo_callback(struct mid_q_entry *mid)
  1131. {
  1132. struct TCP_Server_Info *server = mid->callback_data;
  1133. struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
  1134. unsigned int credits_received = 1;
  1135. if (mid->mid_state == MID_RESPONSE_RECEIVED)
  1136. credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
  1137. DeleteMidQEntry(mid);
  1138. add_credits(server, credits_received, CIFS_ECHO_OP);
  1139. }
  1140. int
  1141. SMB2_echo(struct TCP_Server_Info *server)
  1142. {
  1143. struct smb2_echo_req *req;
  1144. int rc = 0;
  1145. struct kvec iov;
  1146. struct smb_rqst rqst = { .rq_iov = &iov,
  1147. .rq_nvec = 1 };
  1148. cifs_dbg(FYI, "In echo request\n");
  1149. rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
  1150. if (rc)
  1151. return rc;
  1152. req->hdr.CreditRequest = cpu_to_le16(1);
  1153. iov.iov_base = (char *)req;
  1154. /* 4 for rfc1002 length field */
  1155. iov.iov_len = get_rfc1002_length(req) + 4;
  1156. rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
  1157. CIFS_ECHO_OP);
  1158. if (rc)
  1159. cifs_dbg(FYI, "Echo request failed: %d\n", rc);
  1160. cifs_small_buf_release(req);
  1161. return rc;
  1162. }
  1163. int
  1164. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1165. u64 volatile_fid)
  1166. {
  1167. struct smb2_flush_req *req;
  1168. struct TCP_Server_Info *server;
  1169. struct cifs_ses *ses = tcon->ses;
  1170. struct kvec iov[1];
  1171. int resp_buftype;
  1172. int rc = 0;
  1173. cifs_dbg(FYI, "Flush\n");
  1174. if (ses && (ses->server))
  1175. server = ses->server;
  1176. else
  1177. return -EIO;
  1178. rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
  1179. if (rc)
  1180. return rc;
  1181. req->PersistentFileId = persistent_fid;
  1182. req->VolatileFileId = volatile_fid;
  1183. iov[0].iov_base = (char *)req;
  1184. /* 4 for rfc1002 length field */
  1185. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1186. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1187. if ((rc != 0) && tcon)
  1188. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  1189. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1190. return rc;
  1191. }
  1192. /*
  1193. * To form a chain of read requests, any read requests after the first should
  1194. * have the end_of_chain boolean set to true.
  1195. */
  1196. static int
  1197. smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
  1198. unsigned int remaining_bytes, int request_type)
  1199. {
  1200. int rc = -EACCES;
  1201. struct smb2_read_req *req = NULL;
  1202. rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
  1203. if (rc)
  1204. return rc;
  1205. if (io_parms->tcon->ses->server == NULL)
  1206. return -ECONNABORTED;
  1207. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1208. req->PersistentFileId = io_parms->persistent_fid;
  1209. req->VolatileFileId = io_parms->volatile_fid;
  1210. req->ReadChannelInfoOffset = 0; /* reserved */
  1211. req->ReadChannelInfoLength = 0; /* reserved */
  1212. req->Channel = 0; /* reserved */
  1213. req->MinimumCount = 0;
  1214. req->Length = cpu_to_le32(io_parms->length);
  1215. req->Offset = cpu_to_le64(io_parms->offset);
  1216. if (request_type & CHAINED_REQUEST) {
  1217. if (!(request_type & END_OF_CHAIN)) {
  1218. /* 4 for rfc1002 length field */
  1219. req->hdr.NextCommand =
  1220. cpu_to_le32(get_rfc1002_length(req) + 4);
  1221. } else /* END_OF_CHAIN */
  1222. req->hdr.NextCommand = 0;
  1223. if (request_type & RELATED_REQUEST) {
  1224. req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1225. /*
  1226. * Related requests use info from previous read request
  1227. * in chain.
  1228. */
  1229. req->hdr.SessionId = 0xFFFFFFFF;
  1230. req->hdr.TreeId = 0xFFFFFFFF;
  1231. req->PersistentFileId = 0xFFFFFFFF;
  1232. req->VolatileFileId = 0xFFFFFFFF;
  1233. }
  1234. }
  1235. if (remaining_bytes > io_parms->length)
  1236. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  1237. else
  1238. req->RemainingBytes = 0;
  1239. iov[0].iov_base = (char *)req;
  1240. /* 4 for rfc1002 length field */
  1241. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1242. return rc;
  1243. }
  1244. static void
  1245. smb2_readv_callback(struct mid_q_entry *mid)
  1246. {
  1247. struct cifs_readdata *rdata = mid->callback_data;
  1248. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1249. struct TCP_Server_Info *server = tcon->ses->server;
  1250. struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1251. unsigned int credits_received = 1;
  1252. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1253. .rq_nvec = 1,
  1254. .rq_pages = rdata->pages,
  1255. .rq_npages = rdata->nr_pages,
  1256. .rq_pagesz = rdata->pagesz,
  1257. .rq_tailsz = rdata->tailsz };
  1258. cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
  1259. __func__, mid->mid, mid->mid_state, rdata->result,
  1260. rdata->bytes);
  1261. switch (mid->mid_state) {
  1262. case MID_RESPONSE_RECEIVED:
  1263. credits_received = le16_to_cpu(buf->CreditRequest);
  1264. /* result already set, check signature */
  1265. if (server->sign) {
  1266. int rc;
  1267. rc = smb2_verify_signature(&rqst, server);
  1268. if (rc)
  1269. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  1270. rc);
  1271. }
  1272. /* FIXME: should this be counted toward the initiating task? */
  1273. task_io_account_read(rdata->bytes);
  1274. cifs_stats_bytes_read(tcon, rdata->bytes);
  1275. break;
  1276. case MID_REQUEST_SUBMITTED:
  1277. case MID_RETRY_NEEDED:
  1278. rdata->result = -EAGAIN;
  1279. break;
  1280. default:
  1281. if (rdata->result != -ENODATA)
  1282. rdata->result = -EIO;
  1283. }
  1284. if (rdata->result)
  1285. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  1286. queue_work(cifsiod_wq, &rdata->work);
  1287. DeleteMidQEntry(mid);
  1288. add_credits(server, credits_received, 0);
  1289. }
  1290. /* smb2_async_readv - send an async write, and set up mid to handle result */
  1291. int
  1292. smb2_async_readv(struct cifs_readdata *rdata)
  1293. {
  1294. int rc;
  1295. struct smb2_hdr *buf;
  1296. struct cifs_io_parms io_parms;
  1297. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1298. .rq_nvec = 1 };
  1299. cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
  1300. __func__, rdata->offset, rdata->bytes);
  1301. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  1302. io_parms.offset = rdata->offset;
  1303. io_parms.length = rdata->bytes;
  1304. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  1305. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  1306. io_parms.pid = rdata->pid;
  1307. rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
  1308. if (rc)
  1309. return rc;
  1310. buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1311. /* 4 for rfc1002 length field */
  1312. rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
  1313. kref_get(&rdata->refcount);
  1314. rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
  1315. cifs_readv_receive, smb2_readv_callback,
  1316. rdata, 0);
  1317. if (rc) {
  1318. kref_put(&rdata->refcount, cifs_readdata_release);
  1319. cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
  1320. }
  1321. cifs_small_buf_release(buf);
  1322. return rc;
  1323. }
  1324. int
  1325. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  1326. unsigned int *nbytes, char **buf, int *buf_type)
  1327. {
  1328. int resp_buftype, rc = -EACCES;
  1329. struct smb2_read_rsp *rsp = NULL;
  1330. struct kvec iov[1];
  1331. *nbytes = 0;
  1332. rc = smb2_new_read_req(iov, io_parms, 0, 0);
  1333. if (rc)
  1334. return rc;
  1335. rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
  1336. &resp_buftype, CIFS_LOG_ERROR);
  1337. rsp = (struct smb2_read_rsp *)iov[0].iov_base;
  1338. if (rsp->hdr.Status == STATUS_END_OF_FILE) {
  1339. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1340. return 0;
  1341. }
  1342. if (rc) {
  1343. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  1344. cifs_dbg(VFS, "Send error in read = %d\n", rc);
  1345. } else {
  1346. *nbytes = le32_to_cpu(rsp->DataLength);
  1347. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  1348. (*nbytes > io_parms->length)) {
  1349. cifs_dbg(FYI, "bad length %d for count %d\n",
  1350. *nbytes, io_parms->length);
  1351. rc = -EIO;
  1352. *nbytes = 0;
  1353. }
  1354. }
  1355. if (*buf) {
  1356. memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
  1357. *nbytes);
  1358. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1359. } else if (resp_buftype != CIFS_NO_BUFFER) {
  1360. *buf = iov[0].iov_base;
  1361. if (resp_buftype == CIFS_SMALL_BUFFER)
  1362. *buf_type = CIFS_SMALL_BUFFER;
  1363. else if (resp_buftype == CIFS_LARGE_BUFFER)
  1364. *buf_type = CIFS_LARGE_BUFFER;
  1365. }
  1366. return rc;
  1367. }
  1368. /*
  1369. * Check the mid_state and signature on received buffer (if any), and queue the
  1370. * workqueue completion task.
  1371. */
  1372. static void
  1373. smb2_writev_callback(struct mid_q_entry *mid)
  1374. {
  1375. struct cifs_writedata *wdata = mid->callback_data;
  1376. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1377. unsigned int written;
  1378. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  1379. unsigned int credits_received = 1;
  1380. switch (mid->mid_state) {
  1381. case MID_RESPONSE_RECEIVED:
  1382. credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
  1383. wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
  1384. if (wdata->result != 0)
  1385. break;
  1386. written = le32_to_cpu(rsp->DataLength);
  1387. /*
  1388. * Mask off high 16 bits when bytes written as returned
  1389. * by the server is greater than bytes requested by the
  1390. * client. OS/2 servers are known to set incorrect
  1391. * CountHigh values.
  1392. */
  1393. if (written > wdata->bytes)
  1394. written &= 0xFFFF;
  1395. if (written < wdata->bytes)
  1396. wdata->result = -ENOSPC;
  1397. else
  1398. wdata->bytes = written;
  1399. break;
  1400. case MID_REQUEST_SUBMITTED:
  1401. case MID_RETRY_NEEDED:
  1402. wdata->result = -EAGAIN;
  1403. break;
  1404. default:
  1405. wdata->result = -EIO;
  1406. break;
  1407. }
  1408. if (wdata->result)
  1409. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1410. queue_work(cifsiod_wq, &wdata->work);
  1411. DeleteMidQEntry(mid);
  1412. add_credits(tcon->ses->server, credits_received, 0);
  1413. }
  1414. /* smb2_async_writev - send an async write, and set up mid to handle result */
  1415. int
  1416. smb2_async_writev(struct cifs_writedata *wdata)
  1417. {
  1418. int rc = -EACCES;
  1419. struct smb2_write_req *req = NULL;
  1420. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1421. struct kvec iov;
  1422. struct smb_rqst rqst;
  1423. rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
  1424. if (rc)
  1425. goto async_writev_out;
  1426. req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
  1427. req->PersistentFileId = wdata->cfile->fid.persistent_fid;
  1428. req->VolatileFileId = wdata->cfile->fid.volatile_fid;
  1429. req->WriteChannelInfoOffset = 0;
  1430. req->WriteChannelInfoLength = 0;
  1431. req->Channel = 0;
  1432. req->Offset = cpu_to_le64(wdata->offset);
  1433. /* 4 for rfc1002 length field */
  1434. req->DataOffset = cpu_to_le16(
  1435. offsetof(struct smb2_write_req, Buffer) - 4);
  1436. req->RemainingBytes = 0;
  1437. /* 4 for rfc1002 length field and 1 for Buffer */
  1438. iov.iov_len = get_rfc1002_length(req) + 4 - 1;
  1439. iov.iov_base = req;
  1440. rqst.rq_iov = &iov;
  1441. rqst.rq_nvec = 1;
  1442. rqst.rq_pages = wdata->pages;
  1443. rqst.rq_npages = wdata->nr_pages;
  1444. rqst.rq_pagesz = wdata->pagesz;
  1445. rqst.rq_tailsz = wdata->tailsz;
  1446. cifs_dbg(FYI, "async write at %llu %u bytes\n",
  1447. wdata->offset, wdata->bytes);
  1448. req->Length = cpu_to_le32(wdata->bytes);
  1449. inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
  1450. kref_get(&wdata->refcount);
  1451. rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
  1452. smb2_writev_callback, wdata, 0);
  1453. if (rc) {
  1454. kref_put(&wdata->refcount, cifs_writedata_release);
  1455. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1456. }
  1457. async_writev_out:
  1458. cifs_small_buf_release(req);
  1459. return rc;
  1460. }
  1461. /*
  1462. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  1463. * The length field from io_parms must be at least 1 and indicates a number of
  1464. * elements with data to write that begins with position 1 in iov array. All
  1465. * data length is specified by count.
  1466. */
  1467. int
  1468. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  1469. unsigned int *nbytes, struct kvec *iov, int n_vec)
  1470. {
  1471. int rc = 0;
  1472. struct smb2_write_req *req = NULL;
  1473. struct smb2_write_rsp *rsp = NULL;
  1474. int resp_buftype;
  1475. *nbytes = 0;
  1476. if (n_vec < 1)
  1477. return rc;
  1478. rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
  1479. if (rc)
  1480. return rc;
  1481. if (io_parms->tcon->ses->server == NULL)
  1482. return -ECONNABORTED;
  1483. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1484. req->PersistentFileId = io_parms->persistent_fid;
  1485. req->VolatileFileId = io_parms->volatile_fid;
  1486. req->WriteChannelInfoOffset = 0;
  1487. req->WriteChannelInfoLength = 0;
  1488. req->Channel = 0;
  1489. req->Length = cpu_to_le32(io_parms->length);
  1490. req->Offset = cpu_to_le64(io_parms->offset);
  1491. /* 4 for rfc1002 length field */
  1492. req->DataOffset = cpu_to_le16(
  1493. offsetof(struct smb2_write_req, Buffer) - 4);
  1494. req->RemainingBytes = 0;
  1495. iov[0].iov_base = (char *)req;
  1496. /* 4 for rfc1002 length field and 1 for Buffer */
  1497. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1498. /* length of entire message including data to be written */
  1499. inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
  1500. rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
  1501. &resp_buftype, 0);
  1502. rsp = (struct smb2_write_rsp *)iov[0].iov_base;
  1503. if (rc) {
  1504. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  1505. cifs_dbg(VFS, "Send error in write = %d\n", rc);
  1506. } else
  1507. *nbytes = le32_to_cpu(rsp->DataLength);
  1508. free_rsp_buf(resp_buftype, rsp);
  1509. return rc;
  1510. }
  1511. static unsigned int
  1512. num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
  1513. {
  1514. int len;
  1515. unsigned int entrycount = 0;
  1516. unsigned int next_offset = 0;
  1517. FILE_DIRECTORY_INFO *entryptr;
  1518. if (bufstart == NULL)
  1519. return 0;
  1520. entryptr = (FILE_DIRECTORY_INFO *)bufstart;
  1521. while (1) {
  1522. entryptr = (FILE_DIRECTORY_INFO *)
  1523. ((char *)entryptr + next_offset);
  1524. if ((char *)entryptr + size > end_of_buf) {
  1525. cifs_dbg(VFS, "malformed search entry would overflow\n");
  1526. break;
  1527. }
  1528. len = le32_to_cpu(entryptr->FileNameLength);
  1529. if ((char *)entryptr + len + size > end_of_buf) {
  1530. cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
  1531. end_of_buf);
  1532. break;
  1533. }
  1534. *lastentry = (char *)entryptr;
  1535. entrycount++;
  1536. next_offset = le32_to_cpu(entryptr->NextEntryOffset);
  1537. if (!next_offset)
  1538. break;
  1539. }
  1540. return entrycount;
  1541. }
  1542. /*
  1543. * Readdir/FindFirst
  1544. */
  1545. int
  1546. SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
  1547. u64 persistent_fid, u64 volatile_fid, int index,
  1548. struct cifs_search_info *srch_inf)
  1549. {
  1550. struct smb2_query_directory_req *req;
  1551. struct smb2_query_directory_rsp *rsp = NULL;
  1552. struct kvec iov[2];
  1553. int rc = 0;
  1554. int len;
  1555. int resp_buftype;
  1556. unsigned char *bufptr;
  1557. struct TCP_Server_Info *server;
  1558. struct cifs_ses *ses = tcon->ses;
  1559. __le16 asteriks = cpu_to_le16('*');
  1560. char *end_of_smb;
  1561. unsigned int output_size = CIFSMaxBufSize;
  1562. size_t info_buf_size;
  1563. if (ses && (ses->server))
  1564. server = ses->server;
  1565. else
  1566. return -EIO;
  1567. rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
  1568. if (rc)
  1569. return rc;
  1570. switch (srch_inf->info_level) {
  1571. case SMB_FIND_FILE_DIRECTORY_INFO:
  1572. req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
  1573. info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
  1574. break;
  1575. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  1576. req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
  1577. info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
  1578. break;
  1579. default:
  1580. cifs_dbg(VFS, "info level %u isn't supported\n",
  1581. srch_inf->info_level);
  1582. rc = -EINVAL;
  1583. goto qdir_exit;
  1584. }
  1585. req->FileIndex = cpu_to_le32(index);
  1586. req->PersistentFileId = persistent_fid;
  1587. req->VolatileFileId = volatile_fid;
  1588. len = 0x2;
  1589. bufptr = req->Buffer;
  1590. memcpy(bufptr, &asteriks, len);
  1591. req->FileNameOffset =
  1592. cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
  1593. req->FileNameLength = cpu_to_le16(len);
  1594. /*
  1595. * BB could be 30 bytes or so longer if we used SMB2 specific
  1596. * buffer lengths, but this is safe and close enough.
  1597. */
  1598. output_size = min_t(unsigned int, output_size, server->maxBuf);
  1599. output_size = min_t(unsigned int, output_size, 2 << 15);
  1600. req->OutputBufferLength = cpu_to_le32(output_size);
  1601. iov[0].iov_base = (char *)req;
  1602. /* 4 for RFC1001 length and 1 for Buffer */
  1603. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1604. iov[1].iov_base = (char *)(req->Buffer);
  1605. iov[1].iov_len = len;
  1606. inc_rfc1001_len(req, len - 1 /* Buffer */);
  1607. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  1608. rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
  1609. if (rc) {
  1610. cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
  1611. goto qdir_exit;
  1612. }
  1613. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1614. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1615. info_buf_size);
  1616. if (rc)
  1617. goto qdir_exit;
  1618. srch_inf->unicode = true;
  1619. if (srch_inf->ntwrk_buf_start) {
  1620. if (srch_inf->smallBuf)
  1621. cifs_small_buf_release(srch_inf->ntwrk_buf_start);
  1622. else
  1623. cifs_buf_release(srch_inf->ntwrk_buf_start);
  1624. }
  1625. srch_inf->ntwrk_buf_start = (char *)rsp;
  1626. srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
  1627. (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
  1628. /* 4 for rfc1002 length field */
  1629. end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
  1630. srch_inf->entries_in_buffer =
  1631. num_entries(srch_inf->srch_entries_start, end_of_smb,
  1632. &srch_inf->last_entry, info_buf_size);
  1633. srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
  1634. cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
  1635. srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
  1636. srch_inf->srch_entries_start, srch_inf->last_entry);
  1637. if (resp_buftype == CIFS_LARGE_BUFFER)
  1638. srch_inf->smallBuf = false;
  1639. else if (resp_buftype == CIFS_SMALL_BUFFER)
  1640. srch_inf->smallBuf = true;
  1641. else
  1642. cifs_dbg(VFS, "illegal search buffer type\n");
  1643. if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
  1644. srch_inf->endOfSearch = 1;
  1645. else
  1646. srch_inf->endOfSearch = 0;
  1647. return rc;
  1648. qdir_exit:
  1649. free_rsp_buf(resp_buftype, rsp);
  1650. return rc;
  1651. }
  1652. static int
  1653. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1654. u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
  1655. unsigned int num, void **data, unsigned int *size)
  1656. {
  1657. struct smb2_set_info_req *req;
  1658. struct smb2_set_info_rsp *rsp = NULL;
  1659. struct kvec *iov;
  1660. int rc = 0;
  1661. int resp_buftype;
  1662. unsigned int i;
  1663. struct TCP_Server_Info *server;
  1664. struct cifs_ses *ses = tcon->ses;
  1665. if (ses && (ses->server))
  1666. server = ses->server;
  1667. else
  1668. return -EIO;
  1669. if (!num)
  1670. return -EINVAL;
  1671. iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
  1672. if (!iov)
  1673. return -ENOMEM;
  1674. rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
  1675. if (rc) {
  1676. kfree(iov);
  1677. return rc;
  1678. }
  1679. req->hdr.ProcessId = cpu_to_le32(pid);
  1680. req->InfoType = SMB2_O_INFO_FILE;
  1681. req->FileInfoClass = info_class;
  1682. req->PersistentFileId = persistent_fid;
  1683. req->VolatileFileId = volatile_fid;
  1684. /* 4 for RFC1001 length and 1 for Buffer */
  1685. req->BufferOffset =
  1686. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
  1687. req->BufferLength = cpu_to_le32(*size);
  1688. inc_rfc1001_len(req, *size - 1 /* Buffer */);
  1689. memcpy(req->Buffer, *data, *size);
  1690. iov[0].iov_base = (char *)req;
  1691. /* 4 for RFC1001 length */
  1692. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1693. for (i = 1; i < num; i++) {
  1694. inc_rfc1001_len(req, size[i]);
  1695. le32_add_cpu(&req->BufferLength, size[i]);
  1696. iov[i].iov_base = (char *)data[i];
  1697. iov[i].iov_len = size[i];
  1698. }
  1699. rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
  1700. rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
  1701. if (rc != 0) {
  1702. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  1703. goto out;
  1704. }
  1705. out:
  1706. free_rsp_buf(resp_buftype, rsp);
  1707. kfree(iov);
  1708. return rc;
  1709. }
  1710. int
  1711. SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
  1712. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1713. {
  1714. struct smb2_file_rename_info info;
  1715. void **data;
  1716. unsigned int size[2];
  1717. int rc;
  1718. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1719. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1720. if (!data)
  1721. return -ENOMEM;
  1722. info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
  1723. /* 0 = fail if target already exists */
  1724. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1725. info.FileNameLength = cpu_to_le32(len);
  1726. data[0] = &info;
  1727. size[0] = sizeof(struct smb2_file_rename_info);
  1728. data[1] = target_file;
  1729. size[1] = len + 2 /* null */;
  1730. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1731. current->tgid, FILE_RENAME_INFORMATION, 2, data,
  1732. size);
  1733. kfree(data);
  1734. return rc;
  1735. }
  1736. int
  1737. SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  1738. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1739. {
  1740. struct smb2_file_link_info info;
  1741. void **data;
  1742. unsigned int size[2];
  1743. int rc;
  1744. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1745. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1746. if (!data)
  1747. return -ENOMEM;
  1748. info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
  1749. /* 0 = fail if link already exists */
  1750. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1751. info.FileNameLength = cpu_to_le32(len);
  1752. data[0] = &info;
  1753. size[0] = sizeof(struct smb2_file_link_info);
  1754. data[1] = target_file;
  1755. size[1] = len + 2 /* null */;
  1756. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1757. current->tgid, FILE_LINK_INFORMATION, 2, data, size);
  1758. kfree(data);
  1759. return rc;
  1760. }
  1761. int
  1762. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1763. u64 volatile_fid, u32 pid, __le64 *eof)
  1764. {
  1765. struct smb2_file_eof_info info;
  1766. void *data;
  1767. unsigned int size;
  1768. info.EndOfFile = *eof;
  1769. data = &info;
  1770. size = sizeof(struct smb2_file_eof_info);
  1771. return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
  1772. FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
  1773. }
  1774. int
  1775. SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1776. u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
  1777. {
  1778. unsigned int size;
  1779. size = sizeof(FILE_BASIC_INFO);
  1780. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1781. current->tgid, FILE_BASIC_INFORMATION, 1,
  1782. (void **)&buf, &size);
  1783. }
  1784. int
  1785. SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
  1786. const u64 persistent_fid, const u64 volatile_fid,
  1787. __u8 oplock_level)
  1788. {
  1789. int rc;
  1790. struct smb2_oplock_break *req = NULL;
  1791. cifs_dbg(FYI, "SMB2_oplock_break\n");
  1792. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  1793. if (rc)
  1794. return rc;
  1795. req->VolatileFid = volatile_fid;
  1796. req->PersistentFid = persistent_fid;
  1797. req->OplockLevel = oplock_level;
  1798. req->hdr.CreditRequest = cpu_to_le16(1);
  1799. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  1800. /* SMB2 buffer freed by function above */
  1801. if (rc) {
  1802. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  1803. cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
  1804. }
  1805. return rc;
  1806. }
  1807. static void
  1808. copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
  1809. struct kstatfs *kst)
  1810. {
  1811. kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
  1812. le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
  1813. kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
  1814. kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
  1815. kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
  1816. return;
  1817. }
  1818. static int
  1819. build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
  1820. int outbuf_len, u64 persistent_fid, u64 volatile_fid)
  1821. {
  1822. int rc;
  1823. struct smb2_query_info_req *req;
  1824. cifs_dbg(FYI, "Query FSInfo level %d\n", level);
  1825. if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
  1826. return -EIO;
  1827. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1828. if (rc)
  1829. return rc;
  1830. req->InfoType = SMB2_O_INFO_FILESYSTEM;
  1831. req->FileInfoClass = level;
  1832. req->PersistentFileId = persistent_fid;
  1833. req->VolatileFileId = volatile_fid;
  1834. /* 4 for rfc1002 length field and 1 for pad */
  1835. req->InputBufferOffset =
  1836. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1837. req->OutputBufferLength = cpu_to_le32(
  1838. outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
  1839. iov->iov_base = (char *)req;
  1840. /* 4 for rfc1002 length field */
  1841. iov->iov_len = get_rfc1002_length(req) + 4;
  1842. return 0;
  1843. }
  1844. int
  1845. SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
  1846. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  1847. {
  1848. struct smb2_query_info_rsp *rsp = NULL;
  1849. struct kvec iov;
  1850. int rc = 0;
  1851. int resp_buftype;
  1852. struct cifs_ses *ses = tcon->ses;
  1853. struct smb2_fs_full_size_info *info = NULL;
  1854. rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
  1855. sizeof(struct smb2_fs_full_size_info),
  1856. persistent_fid, volatile_fid);
  1857. if (rc)
  1858. return rc;
  1859. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  1860. if (rc) {
  1861. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1862. goto qinf_exit;
  1863. }
  1864. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  1865. info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
  1866. le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
  1867. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1868. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1869. sizeof(struct smb2_fs_full_size_info));
  1870. if (!rc)
  1871. copy_fs_info_to_kstatfs(info, fsdata);
  1872. qinf_exit:
  1873. free_rsp_buf(resp_buftype, iov.iov_base);
  1874. return rc;
  1875. }
  1876. int
  1877. smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
  1878. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  1879. const __u32 num_lock, struct smb2_lock_element *buf)
  1880. {
  1881. int rc = 0;
  1882. struct smb2_lock_req *req = NULL;
  1883. struct kvec iov[2];
  1884. int resp_buf_type;
  1885. unsigned int count;
  1886. cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
  1887. rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
  1888. if (rc)
  1889. return rc;
  1890. req->hdr.ProcessId = cpu_to_le32(pid);
  1891. req->LockCount = cpu_to_le16(num_lock);
  1892. req->PersistentFileId = persist_fid;
  1893. req->VolatileFileId = volatile_fid;
  1894. count = num_lock * sizeof(struct smb2_lock_element);
  1895. inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
  1896. iov[0].iov_base = (char *)req;
  1897. /* 4 for rfc1002 length field and count for all locks */
  1898. iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
  1899. iov[1].iov_base = (char *)buf;
  1900. iov[1].iov_len = count;
  1901. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  1902. rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
  1903. if (rc) {
  1904. cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
  1905. cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
  1906. }
  1907. return rc;
  1908. }
  1909. int
  1910. SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
  1911. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  1912. const __u64 length, const __u64 offset, const __u32 lock_flags,
  1913. const bool wait)
  1914. {
  1915. struct smb2_lock_element lock;
  1916. lock.Offset = cpu_to_le64(offset);
  1917. lock.Length = cpu_to_le64(length);
  1918. lock.Flags = cpu_to_le32(lock_flags);
  1919. if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
  1920. lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  1921. return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
  1922. }
  1923. int
  1924. SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
  1925. __u8 *lease_key, const __le32 lease_state)
  1926. {
  1927. int rc;
  1928. struct smb2_lease_ack *req = NULL;
  1929. cifs_dbg(FYI, "SMB2_lease_break\n");
  1930. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  1931. if (rc)
  1932. return rc;
  1933. req->hdr.CreditRequest = cpu_to_le16(1);
  1934. req->StructureSize = cpu_to_le16(36);
  1935. inc_rfc1001_len(req, 12);
  1936. memcpy(req->LeaseKey, lease_key, 16);
  1937. req->LeaseState = lease_state;
  1938. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  1939. /* SMB2 buffer freed by function above */
  1940. if (rc) {
  1941. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  1942. cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
  1943. }
  1944. return rc;
  1945. }