smb2pdu.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438
  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 we are here due to reconnect, free per-smb session key
  423. * in case signing was required.
  424. */
  425. kfree(ses->auth_key.response);
  426. ses->auth_key.response = NULL;
  427. /*
  428. * If memory allocation is successful, caller of this function
  429. * frees it.
  430. */
  431. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  432. if (!ses->ntlmssp)
  433. return -ENOMEM;
  434. ses->ntlmssp->sesskey_per_smbsess = true;
  435. /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
  436. ses->sectype = RawNTLMSSP;
  437. ssetup_ntlmssp_authenticate:
  438. if (phase == NtLmChallenge)
  439. phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
  440. rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
  441. if (rc)
  442. return rc;
  443. req->hdr.SessionId = 0; /* First session, not a reauthenticate */
  444. req->VcNumber = 0; /* MBZ */
  445. /* to enable echos and oplocks */
  446. req->hdr.CreditRequest = cpu_to_le16(3);
  447. /* only one of SMB2 signing flags may be set in SMB2 request */
  448. if (server->sign)
  449. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  450. else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
  451. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
  452. else
  453. req->SecurityMode = 0;
  454. req->Capabilities = 0;
  455. req->Channel = 0; /* MBZ */
  456. iov[0].iov_base = (char *)req;
  457. /* 4 for rfc1002 length field and 1 for pad */
  458. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  459. if (phase == NtLmNegotiate) {
  460. ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
  461. GFP_KERNEL);
  462. if (ntlmssp_blob == NULL) {
  463. rc = -ENOMEM;
  464. goto ssetup_exit;
  465. }
  466. build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
  467. if (use_spnego) {
  468. /* blob_length = build_spnego_ntlmssp_blob(
  469. &security_blob,
  470. sizeof(struct _NEGOTIATE_MESSAGE),
  471. ntlmssp_blob); */
  472. /* BB eventually need to add this */
  473. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  474. rc = -EOPNOTSUPP;
  475. kfree(ntlmssp_blob);
  476. goto ssetup_exit;
  477. } else {
  478. blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
  479. /* with raw NTLMSSP we don't encapsulate in SPNEGO */
  480. security_blob = ntlmssp_blob;
  481. }
  482. } else if (phase == NtLmAuthenticate) {
  483. req->hdr.SessionId = ses->Suid;
  484. ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
  485. GFP_KERNEL);
  486. if (ntlmssp_blob == NULL) {
  487. rc = -ENOMEM;
  488. goto ssetup_exit;
  489. }
  490. rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
  491. nls_cp);
  492. if (rc) {
  493. cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
  494. rc);
  495. goto ssetup_exit; /* BB double check error handling */
  496. }
  497. if (use_spnego) {
  498. /* blob_length = build_spnego_ntlmssp_blob(
  499. &security_blob,
  500. blob_length,
  501. ntlmssp_blob); */
  502. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  503. rc = -EOPNOTSUPP;
  504. kfree(ntlmssp_blob);
  505. goto ssetup_exit;
  506. } else {
  507. security_blob = ntlmssp_blob;
  508. }
  509. } else {
  510. cifs_dbg(VFS, "illegal ntlmssp phase\n");
  511. rc = -EIO;
  512. goto ssetup_exit;
  513. }
  514. /* Testing shows that buffer offset must be at location of Buffer[0] */
  515. req->SecurityBufferOffset =
  516. cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
  517. 1 /* pad */ - 4 /* rfc1001 len */);
  518. req->SecurityBufferLength = cpu_to_le16(blob_length);
  519. iov[1].iov_base = security_blob;
  520. iov[1].iov_len = blob_length;
  521. inc_rfc1001_len(req, blob_length - 1 /* pad */);
  522. /* BB add code to build os and lm fields */
  523. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
  524. CIFS_LOG_ERROR | CIFS_NEG_OP);
  525. kfree(security_blob);
  526. rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
  527. if (resp_buftype != CIFS_NO_BUFFER &&
  528. rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
  529. if (phase != NtLmNegotiate) {
  530. cifs_dbg(VFS, "Unexpected more processing error\n");
  531. goto ssetup_exit;
  532. }
  533. if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
  534. le16_to_cpu(rsp->SecurityBufferOffset)) {
  535. cifs_dbg(VFS, "Invalid security buffer offset %d\n",
  536. le16_to_cpu(rsp->SecurityBufferOffset));
  537. rc = -EIO;
  538. goto ssetup_exit;
  539. }
  540. /* NTLMSSP Negotiate sent now processing challenge (response) */
  541. phase = NtLmChallenge; /* process ntlmssp challenge */
  542. rc = 0; /* MORE_PROCESSING is not an error here but expected */
  543. ses->Suid = rsp->hdr.SessionId;
  544. rc = decode_ntlmssp_challenge(rsp->Buffer,
  545. le16_to_cpu(rsp->SecurityBufferLength), ses);
  546. }
  547. /*
  548. * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
  549. * but at least the raw NTLMSSP case works.
  550. */
  551. /*
  552. * No tcon so can't do
  553. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  554. */
  555. if (rc != 0)
  556. goto ssetup_exit;
  557. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  558. ssetup_exit:
  559. free_rsp_buf(resp_buftype, rsp);
  560. /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
  561. if ((phase == NtLmChallenge) && (rc == 0))
  562. goto ssetup_ntlmssp_authenticate;
  563. if (!rc) {
  564. mutex_lock(&server->srv_mutex);
  565. if (server->sign && server->ops->generate_signingkey) {
  566. rc = server->ops->generate_signingkey(ses);
  567. kfree(ses->auth_key.response);
  568. ses->auth_key.response = NULL;
  569. if (rc) {
  570. cifs_dbg(FYI,
  571. "SMB3 session key generation failed\n");
  572. mutex_unlock(&server->srv_mutex);
  573. goto keygen_exit;
  574. }
  575. }
  576. if (!server->session_estab) {
  577. server->sequence_number = 0x2;
  578. server->session_estab = true;
  579. }
  580. mutex_unlock(&server->srv_mutex);
  581. cifs_dbg(FYI, "SMB2/3 session established successfully\n");
  582. spin_lock(&GlobalMid_Lock);
  583. ses->status = CifsGood;
  584. ses->need_reconnect = false;
  585. spin_unlock(&GlobalMid_Lock);
  586. }
  587. keygen_exit:
  588. if (!server->sign) {
  589. kfree(ses->auth_key.response);
  590. ses->auth_key.response = NULL;
  591. }
  592. kfree(ses->ntlmssp);
  593. return rc;
  594. }
  595. int
  596. SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
  597. {
  598. struct smb2_logoff_req *req; /* response is also trivial struct */
  599. int rc = 0;
  600. struct TCP_Server_Info *server;
  601. cifs_dbg(FYI, "disconnect session %p\n", ses);
  602. if (ses && (ses->server))
  603. server = ses->server;
  604. else
  605. return -EIO;
  606. rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
  607. if (rc)
  608. return rc;
  609. /* since no tcon, smb2_init can not do this, so do here */
  610. req->hdr.SessionId = ses->Suid;
  611. if (server->sign)
  612. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  613. rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
  614. /*
  615. * No tcon so can't do
  616. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  617. */
  618. return rc;
  619. }
  620. static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
  621. {
  622. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
  623. }
  624. #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
  625. int
  626. SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
  627. struct cifs_tcon *tcon, const struct nls_table *cp)
  628. {
  629. struct smb2_tree_connect_req *req;
  630. struct smb2_tree_connect_rsp *rsp = NULL;
  631. struct kvec iov[2];
  632. int rc = 0;
  633. int resp_buftype;
  634. int unc_path_len;
  635. struct TCP_Server_Info *server;
  636. __le16 *unc_path = NULL;
  637. cifs_dbg(FYI, "TCON\n");
  638. if ((ses->server) && tree)
  639. server = ses->server;
  640. else
  641. return -EIO;
  642. if (tcon && tcon->bad_network_name)
  643. return -ENOENT;
  644. unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
  645. if (unc_path == NULL)
  646. return -ENOMEM;
  647. unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
  648. unc_path_len *= 2;
  649. if (unc_path_len < 2) {
  650. kfree(unc_path);
  651. return -EINVAL;
  652. }
  653. rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
  654. if (rc) {
  655. kfree(unc_path);
  656. return rc;
  657. }
  658. if (tcon == NULL) {
  659. /* since no tcon, smb2_init can not do this, so do here */
  660. req->hdr.SessionId = ses->Suid;
  661. /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
  662. req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
  663. }
  664. iov[0].iov_base = (char *)req;
  665. /* 4 for rfc1002 length field and 1 for pad */
  666. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  667. /* Testing shows that buffer offset must be at location of Buffer[0] */
  668. req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
  669. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  670. req->PathLength = cpu_to_le16(unc_path_len - 2);
  671. iov[1].iov_base = unc_path;
  672. iov[1].iov_len = unc_path_len;
  673. inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
  674. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  675. rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
  676. if (rc != 0) {
  677. if (tcon) {
  678. cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
  679. tcon->need_reconnect = true;
  680. }
  681. goto tcon_error_exit;
  682. }
  683. if (tcon == NULL) {
  684. ses->ipc_tid = rsp->hdr.TreeId;
  685. goto tcon_exit;
  686. }
  687. if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
  688. cifs_dbg(FYI, "connection to disk share\n");
  689. else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
  690. tcon->ipc = true;
  691. cifs_dbg(FYI, "connection to pipe share\n");
  692. } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
  693. tcon->print = true;
  694. cifs_dbg(FYI, "connection to printer\n");
  695. } else {
  696. cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
  697. rc = -EOPNOTSUPP;
  698. goto tcon_error_exit;
  699. }
  700. tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
  701. tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
  702. tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
  703. tcon->tidStatus = CifsGood;
  704. tcon->need_reconnect = false;
  705. tcon->tid = rsp->hdr.TreeId;
  706. strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
  707. if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
  708. ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
  709. cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
  710. tcon_exit:
  711. free_rsp_buf(resp_buftype, rsp);
  712. kfree(unc_path);
  713. return rc;
  714. tcon_error_exit:
  715. if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
  716. cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
  717. tcon->bad_network_name = true;
  718. }
  719. goto tcon_exit;
  720. }
  721. int
  722. SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
  723. {
  724. struct smb2_tree_disconnect_req *req; /* response is trivial */
  725. int rc = 0;
  726. struct TCP_Server_Info *server;
  727. struct cifs_ses *ses = tcon->ses;
  728. cifs_dbg(FYI, "Tree Disconnect\n");
  729. if (ses && (ses->server))
  730. server = ses->server;
  731. else
  732. return -EIO;
  733. if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
  734. return 0;
  735. rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
  736. if (rc)
  737. return rc;
  738. rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
  739. if (rc)
  740. cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
  741. return rc;
  742. }
  743. static struct create_lease *
  744. create_lease_buf(u8 *lease_key, u8 oplock)
  745. {
  746. struct create_lease *buf;
  747. buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
  748. if (!buf)
  749. return NULL;
  750. buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
  751. buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
  752. if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  753. buf->lcontext.LeaseState = SMB2_LEASE_WRITE_CACHING |
  754. SMB2_LEASE_READ_CACHING;
  755. else if (oplock == SMB2_OPLOCK_LEVEL_II)
  756. buf->lcontext.LeaseState = SMB2_LEASE_READ_CACHING;
  757. else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
  758. buf->lcontext.LeaseState = SMB2_LEASE_HANDLE_CACHING |
  759. SMB2_LEASE_READ_CACHING |
  760. SMB2_LEASE_WRITE_CACHING;
  761. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  762. (struct create_lease, lcontext));
  763. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
  764. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  765. (struct create_lease, Name));
  766. buf->ccontext.NameLength = cpu_to_le16(4);
  767. buf->Name[0] = 'R';
  768. buf->Name[1] = 'q';
  769. buf->Name[2] = 'L';
  770. buf->Name[3] = 's';
  771. return buf;
  772. }
  773. static struct create_durable *
  774. create_durable_buf(void)
  775. {
  776. struct create_durable *buf;
  777. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  778. if (!buf)
  779. return NULL;
  780. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  781. (struct create_durable, Data));
  782. buf->ccontext.DataLength = cpu_to_le32(16);
  783. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  784. (struct create_durable, Name));
  785. buf->ccontext.NameLength = cpu_to_le16(4);
  786. buf->Name[0] = 'D';
  787. buf->Name[1] = 'H';
  788. buf->Name[2] = 'n';
  789. buf->Name[3] = 'Q';
  790. return buf;
  791. }
  792. static struct create_durable *
  793. create_reconnect_durable_buf(struct cifs_fid *fid)
  794. {
  795. struct create_durable *buf;
  796. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  797. if (!buf)
  798. return NULL;
  799. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  800. (struct create_durable, Data));
  801. buf->ccontext.DataLength = cpu_to_le32(16);
  802. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  803. (struct create_durable, Name));
  804. buf->ccontext.NameLength = cpu_to_le16(4);
  805. buf->Data.Fid.PersistentFileId = fid->persistent_fid;
  806. buf->Data.Fid.VolatileFileId = fid->volatile_fid;
  807. buf->Name[0] = 'D';
  808. buf->Name[1] = 'H';
  809. buf->Name[2] = 'n';
  810. buf->Name[3] = 'C';
  811. return buf;
  812. }
  813. static __u8
  814. parse_lease_state(struct smb2_create_rsp *rsp)
  815. {
  816. char *data_offset;
  817. struct create_lease *lc;
  818. bool found = false;
  819. unsigned int next = 0;
  820. char *name;
  821. data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
  822. lc = (struct create_lease *)data_offset;
  823. do {
  824. lc = (struct create_lease *)((char *)lc + next);
  825. name = le16_to_cpu(lc->ccontext.NameOffset) + (char *)lc;
  826. if (le16_to_cpu(lc->ccontext.NameLength) != 4 ||
  827. strncmp(name, "RqLs", 4)) {
  828. next = le32_to_cpu(lc->ccontext.Next);
  829. continue;
  830. }
  831. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  832. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  833. found = true;
  834. break;
  835. } while (next != 0);
  836. if (!found)
  837. return 0;
  838. return smb2_map_lease_to_oplock(lc->lcontext.LeaseState);
  839. }
  840. static int
  841. add_lease_context(struct kvec *iov, unsigned int *num_iovec, __u8 *oplock)
  842. {
  843. struct smb2_create_req *req = iov[0].iov_base;
  844. unsigned int num = *num_iovec;
  845. iov[num].iov_base = create_lease_buf(oplock+1, *oplock);
  846. if (iov[num].iov_base == NULL)
  847. return -ENOMEM;
  848. iov[num].iov_len = sizeof(struct create_lease);
  849. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
  850. if (!req->CreateContextsOffset)
  851. req->CreateContextsOffset = cpu_to_le32(
  852. sizeof(struct smb2_create_req) - 4 +
  853. iov[num - 1].iov_len);
  854. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_lease));
  855. inc_rfc1001_len(&req->hdr, sizeof(struct create_lease));
  856. *num_iovec = num + 1;
  857. return 0;
  858. }
  859. static int
  860. add_durable_context(struct kvec *iov, unsigned int *num_iovec,
  861. struct cifs_open_parms *oparms)
  862. {
  863. struct smb2_create_req *req = iov[0].iov_base;
  864. unsigned int num = *num_iovec;
  865. if (oparms->reconnect) {
  866. iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
  867. /* indicate that we don't need to relock the file */
  868. oparms->reconnect = false;
  869. } else
  870. iov[num].iov_base = create_durable_buf();
  871. if (iov[num].iov_base == NULL)
  872. return -ENOMEM;
  873. iov[num].iov_len = sizeof(struct create_durable);
  874. if (!req->CreateContextsOffset)
  875. req->CreateContextsOffset =
  876. cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
  877. iov[1].iov_len);
  878. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
  879. inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
  880. *num_iovec = num + 1;
  881. return 0;
  882. }
  883. int
  884. SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
  885. __u8 *oplock, struct smb2_file_all_info *buf,
  886. struct smb2_err_rsp **err_buf)
  887. {
  888. struct smb2_create_req *req;
  889. struct smb2_create_rsp *rsp;
  890. struct TCP_Server_Info *server;
  891. struct cifs_tcon *tcon = oparms->tcon;
  892. struct cifs_ses *ses = tcon->ses;
  893. struct kvec iov[4];
  894. int resp_buftype;
  895. int uni_path_len;
  896. __le16 *copy_path = NULL;
  897. int copy_size;
  898. int rc = 0;
  899. unsigned int num_iovecs = 2;
  900. __u32 file_attributes = 0;
  901. cifs_dbg(FYI, "create/open\n");
  902. if (ses && (ses->server))
  903. server = ses->server;
  904. else
  905. return -EIO;
  906. rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
  907. if (rc)
  908. return rc;
  909. if (oparms->create_options & CREATE_OPTION_READONLY)
  910. file_attributes |= ATTR_READONLY;
  911. req->ImpersonationLevel = IL_IMPERSONATION;
  912. req->DesiredAccess = cpu_to_le32(oparms->desired_access);
  913. /* File attributes ignored on open (used in create though) */
  914. req->FileAttributes = cpu_to_le32(file_attributes);
  915. req->ShareAccess = FILE_SHARE_ALL_LE;
  916. req->CreateDisposition = cpu_to_le32(oparms->disposition);
  917. req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
  918. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  919. /* do not count rfc1001 len field */
  920. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
  921. iov[0].iov_base = (char *)req;
  922. /* 4 for rfc1002 length field */
  923. iov[0].iov_len = get_rfc1002_length(req) + 4;
  924. /* MUST set path len (NameLength) to 0 opening root of share */
  925. req->NameLength = cpu_to_le16(uni_path_len - 2);
  926. /* -1 since last byte is buf[0] which is sent below (path) */
  927. iov[0].iov_len--;
  928. if (uni_path_len % 8 != 0) {
  929. copy_size = uni_path_len / 8 * 8;
  930. if (copy_size < uni_path_len)
  931. copy_size += 8;
  932. copy_path = kzalloc(copy_size, GFP_KERNEL);
  933. if (!copy_path)
  934. return -ENOMEM;
  935. memcpy((char *)copy_path, (const char *)path,
  936. uni_path_len);
  937. uni_path_len = copy_size;
  938. path = copy_path;
  939. }
  940. iov[1].iov_len = uni_path_len;
  941. iov[1].iov_base = path;
  942. /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
  943. inc_rfc1001_len(req, uni_path_len - 1);
  944. if (!server->oplocks)
  945. *oplock = SMB2_OPLOCK_LEVEL_NONE;
  946. if (!(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
  947. *oplock == SMB2_OPLOCK_LEVEL_NONE)
  948. req->RequestedOplockLevel = *oplock;
  949. else {
  950. rc = add_lease_context(iov, &num_iovecs, oplock);
  951. if (rc) {
  952. cifs_small_buf_release(req);
  953. kfree(copy_path);
  954. return rc;
  955. }
  956. }
  957. if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  958. /* need to set Next field of lease context if we request it */
  959. if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
  960. struct create_context *ccontext =
  961. (struct create_context *)iov[num_iovecs-1].iov_base;
  962. ccontext->Next =
  963. cpu_to_le32(sizeof(struct create_lease));
  964. }
  965. rc = add_durable_context(iov, &num_iovecs, oparms);
  966. if (rc) {
  967. cifs_small_buf_release(req);
  968. kfree(copy_path);
  969. kfree(iov[num_iovecs-1].iov_base);
  970. return rc;
  971. }
  972. }
  973. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  974. rsp = (struct smb2_create_rsp *)iov[0].iov_base;
  975. if (rc != 0) {
  976. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  977. if (err_buf)
  978. *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
  979. GFP_KERNEL);
  980. goto creat_exit;
  981. }
  982. oparms->fid->persistent_fid = rsp->PersistentFileId;
  983. oparms->fid->volatile_fid = rsp->VolatileFileId;
  984. if (buf) {
  985. memcpy(buf, &rsp->CreationTime, 32);
  986. buf->AllocationSize = rsp->AllocationSize;
  987. buf->EndOfFile = rsp->EndofFile;
  988. buf->Attributes = rsp->FileAttributes;
  989. buf->NumberOfLinks = cpu_to_le32(1);
  990. buf->DeletePending = 0;
  991. }
  992. if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
  993. *oplock = parse_lease_state(rsp);
  994. else
  995. *oplock = rsp->OplockLevel;
  996. creat_exit:
  997. kfree(copy_path);
  998. free_rsp_buf(resp_buftype, rsp);
  999. return rc;
  1000. }
  1001. /*
  1002. * SMB2 IOCTL is used for both IOCTLs and FSCTLs
  1003. */
  1004. int
  1005. SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1006. u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
  1007. u32 indatalen, char **out_data, u32 *plen /* returned data len */)
  1008. {
  1009. struct smb2_ioctl_req *req;
  1010. struct smb2_ioctl_rsp *rsp;
  1011. struct TCP_Server_Info *server;
  1012. struct cifs_ses *ses = tcon->ses;
  1013. struct kvec iov[2];
  1014. int resp_buftype;
  1015. int num_iovecs;
  1016. int rc = 0;
  1017. cifs_dbg(FYI, "SMB2 IOCTL\n");
  1018. /* zero out returned data len, in case of error */
  1019. if (plen)
  1020. *plen = 0;
  1021. if (ses && (ses->server))
  1022. server = ses->server;
  1023. else
  1024. return -EIO;
  1025. rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
  1026. if (rc)
  1027. return rc;
  1028. req->CtlCode = cpu_to_le32(opcode);
  1029. req->PersistentFileId = persistent_fid;
  1030. req->VolatileFileId = volatile_fid;
  1031. if (indatalen) {
  1032. req->InputCount = cpu_to_le32(indatalen);
  1033. /* do not set InputOffset if no input data */
  1034. req->InputOffset =
  1035. cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
  1036. iov[1].iov_base = in_data;
  1037. iov[1].iov_len = indatalen;
  1038. num_iovecs = 2;
  1039. } else
  1040. num_iovecs = 1;
  1041. req->OutputOffset = 0;
  1042. req->OutputCount = 0; /* MBZ */
  1043. /*
  1044. * Could increase MaxOutputResponse, but that would require more
  1045. * than one credit. Windows typically sets this smaller, but for some
  1046. * ioctls it may be useful to allow server to send more. No point
  1047. * limiting what the server can send as long as fits in one credit
  1048. */
  1049. req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
  1050. if (is_fsctl)
  1051. req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
  1052. else
  1053. req->Flags = 0;
  1054. iov[0].iov_base = (char *)req;
  1055. /* 4 for rfc1002 length field */
  1056. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1057. if (indatalen)
  1058. inc_rfc1001_len(req, indatalen);
  1059. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  1060. rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
  1061. if (rc != 0) {
  1062. if (tcon)
  1063. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  1064. goto ioctl_exit;
  1065. }
  1066. /* check if caller wants to look at return data or just return rc */
  1067. if ((plen == NULL) || (out_data == NULL))
  1068. goto ioctl_exit;
  1069. *plen = le32_to_cpu(rsp->OutputCount);
  1070. /* We check for obvious errors in the output buffer length and offset */
  1071. if (*plen == 0)
  1072. goto ioctl_exit; /* server returned no data */
  1073. else if (*plen > 0xFF00) {
  1074. cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
  1075. *plen = 0;
  1076. rc = -EIO;
  1077. goto ioctl_exit;
  1078. }
  1079. if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
  1080. cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
  1081. le32_to_cpu(rsp->OutputOffset));
  1082. *plen = 0;
  1083. rc = -EIO;
  1084. goto ioctl_exit;
  1085. }
  1086. *out_data = kmalloc(*plen, GFP_KERNEL);
  1087. if (*out_data == NULL) {
  1088. rc = -ENOMEM;
  1089. goto ioctl_exit;
  1090. }
  1091. memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
  1092. *plen);
  1093. ioctl_exit:
  1094. free_rsp_buf(resp_buftype, rsp);
  1095. return rc;
  1096. }
  1097. int
  1098. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  1099. u64 persistent_fid, u64 volatile_fid)
  1100. {
  1101. struct smb2_close_req *req;
  1102. struct smb2_close_rsp *rsp;
  1103. struct TCP_Server_Info *server;
  1104. struct cifs_ses *ses = tcon->ses;
  1105. struct kvec iov[1];
  1106. int resp_buftype;
  1107. int rc = 0;
  1108. cifs_dbg(FYI, "Close\n");
  1109. if (ses && (ses->server))
  1110. server = ses->server;
  1111. else
  1112. return -EIO;
  1113. rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
  1114. if (rc)
  1115. return rc;
  1116. req->PersistentFileId = persistent_fid;
  1117. req->VolatileFileId = volatile_fid;
  1118. iov[0].iov_base = (char *)req;
  1119. /* 4 for rfc1002 length field */
  1120. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1121. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1122. rsp = (struct smb2_close_rsp *)iov[0].iov_base;
  1123. if (rc != 0) {
  1124. if (tcon)
  1125. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  1126. goto close_exit;
  1127. }
  1128. /* BB FIXME - decode close response, update inode for caching */
  1129. close_exit:
  1130. free_rsp_buf(resp_buftype, rsp);
  1131. return rc;
  1132. }
  1133. static int
  1134. validate_buf(unsigned int offset, unsigned int buffer_length,
  1135. struct smb2_hdr *hdr, unsigned int min_buf_size)
  1136. {
  1137. unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
  1138. char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
  1139. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1140. char *end_of_buf = begin_of_buf + buffer_length;
  1141. if (buffer_length < min_buf_size) {
  1142. cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
  1143. buffer_length, min_buf_size);
  1144. return -EINVAL;
  1145. }
  1146. /* check if beyond RFC1001 maximum length */
  1147. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  1148. cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
  1149. buffer_length, smb_len);
  1150. return -EINVAL;
  1151. }
  1152. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  1153. cifs_dbg(VFS, "illegal server response, bad offset to data\n");
  1154. return -EINVAL;
  1155. }
  1156. return 0;
  1157. }
  1158. /*
  1159. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  1160. * Caller must free buffer.
  1161. */
  1162. static int
  1163. validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
  1164. struct smb2_hdr *hdr, unsigned int minbufsize,
  1165. char *data)
  1166. {
  1167. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1168. int rc;
  1169. if (!data)
  1170. return -EINVAL;
  1171. rc = validate_buf(offset, buffer_length, hdr, minbufsize);
  1172. if (rc)
  1173. return rc;
  1174. memcpy(data, begin_of_buf, buffer_length);
  1175. return 0;
  1176. }
  1177. static int
  1178. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1179. u64 persistent_fid, u64 volatile_fid, u8 info_class,
  1180. size_t output_len, size_t min_len, void *data)
  1181. {
  1182. struct smb2_query_info_req *req;
  1183. struct smb2_query_info_rsp *rsp = NULL;
  1184. struct kvec iov[2];
  1185. int rc = 0;
  1186. int resp_buftype;
  1187. struct TCP_Server_Info *server;
  1188. struct cifs_ses *ses = tcon->ses;
  1189. cifs_dbg(FYI, "Query Info\n");
  1190. if (ses && (ses->server))
  1191. server = ses->server;
  1192. else
  1193. return -EIO;
  1194. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1195. if (rc)
  1196. return rc;
  1197. req->InfoType = SMB2_O_INFO_FILE;
  1198. req->FileInfoClass = info_class;
  1199. req->PersistentFileId = persistent_fid;
  1200. req->VolatileFileId = volatile_fid;
  1201. /* 4 for rfc1002 length field and 1 for Buffer */
  1202. req->InputBufferOffset =
  1203. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1204. req->OutputBufferLength = cpu_to_le32(output_len);
  1205. iov[0].iov_base = (char *)req;
  1206. /* 4 for rfc1002 length field */
  1207. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1208. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1209. rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
  1210. if (rc) {
  1211. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1212. goto qinf_exit;
  1213. }
  1214. rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1215. le32_to_cpu(rsp->OutputBufferLength),
  1216. &rsp->hdr, min_len, data);
  1217. qinf_exit:
  1218. free_rsp_buf(resp_buftype, rsp);
  1219. return rc;
  1220. }
  1221. int
  1222. SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1223. u64 persistent_fid, u64 volatile_fid,
  1224. struct smb2_file_all_info *data)
  1225. {
  1226. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1227. FILE_ALL_INFORMATION,
  1228. sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  1229. sizeof(struct smb2_file_all_info), data);
  1230. }
  1231. int
  1232. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  1233. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  1234. {
  1235. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1236. FILE_INTERNAL_INFORMATION,
  1237. sizeof(struct smb2_file_internal_info),
  1238. sizeof(struct smb2_file_internal_info), uniqueid);
  1239. }
  1240. /*
  1241. * This is a no-op for now. We're not really interested in the reply, but
  1242. * rather in the fact that the server sent one and that server->lstrp
  1243. * gets updated.
  1244. *
  1245. * FIXME: maybe we should consider checking that the reply matches request?
  1246. */
  1247. static void
  1248. smb2_echo_callback(struct mid_q_entry *mid)
  1249. {
  1250. struct TCP_Server_Info *server = mid->callback_data;
  1251. struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
  1252. unsigned int credits_received = 1;
  1253. if (mid->mid_state == MID_RESPONSE_RECEIVED)
  1254. credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
  1255. DeleteMidQEntry(mid);
  1256. add_credits(server, credits_received, CIFS_ECHO_OP);
  1257. }
  1258. int
  1259. SMB2_echo(struct TCP_Server_Info *server)
  1260. {
  1261. struct smb2_echo_req *req;
  1262. int rc = 0;
  1263. struct kvec iov;
  1264. struct smb_rqst rqst = { .rq_iov = &iov,
  1265. .rq_nvec = 1 };
  1266. cifs_dbg(FYI, "In echo request\n");
  1267. rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
  1268. if (rc)
  1269. return rc;
  1270. req->hdr.CreditRequest = cpu_to_le16(1);
  1271. iov.iov_base = (char *)req;
  1272. /* 4 for rfc1002 length field */
  1273. iov.iov_len = get_rfc1002_length(req) + 4;
  1274. rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
  1275. CIFS_ECHO_OP);
  1276. if (rc)
  1277. cifs_dbg(FYI, "Echo request failed: %d\n", rc);
  1278. cifs_small_buf_release(req);
  1279. return rc;
  1280. }
  1281. int
  1282. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1283. u64 volatile_fid)
  1284. {
  1285. struct smb2_flush_req *req;
  1286. struct TCP_Server_Info *server;
  1287. struct cifs_ses *ses = tcon->ses;
  1288. struct kvec iov[1];
  1289. int resp_buftype;
  1290. int rc = 0;
  1291. cifs_dbg(FYI, "Flush\n");
  1292. if (ses && (ses->server))
  1293. server = ses->server;
  1294. else
  1295. return -EIO;
  1296. rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
  1297. if (rc)
  1298. return rc;
  1299. req->PersistentFileId = persistent_fid;
  1300. req->VolatileFileId = volatile_fid;
  1301. iov[0].iov_base = (char *)req;
  1302. /* 4 for rfc1002 length field */
  1303. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1304. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1305. if ((rc != 0) && tcon)
  1306. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  1307. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1308. return rc;
  1309. }
  1310. /*
  1311. * To form a chain of read requests, any read requests after the first should
  1312. * have the end_of_chain boolean set to true.
  1313. */
  1314. static int
  1315. smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
  1316. unsigned int remaining_bytes, int request_type)
  1317. {
  1318. int rc = -EACCES;
  1319. struct smb2_read_req *req = NULL;
  1320. rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
  1321. if (rc)
  1322. return rc;
  1323. if (io_parms->tcon->ses->server == NULL)
  1324. return -ECONNABORTED;
  1325. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1326. req->PersistentFileId = io_parms->persistent_fid;
  1327. req->VolatileFileId = io_parms->volatile_fid;
  1328. req->ReadChannelInfoOffset = 0; /* reserved */
  1329. req->ReadChannelInfoLength = 0; /* reserved */
  1330. req->Channel = 0; /* reserved */
  1331. req->MinimumCount = 0;
  1332. req->Length = cpu_to_le32(io_parms->length);
  1333. req->Offset = cpu_to_le64(io_parms->offset);
  1334. if (request_type & CHAINED_REQUEST) {
  1335. if (!(request_type & END_OF_CHAIN)) {
  1336. /* 4 for rfc1002 length field */
  1337. req->hdr.NextCommand =
  1338. cpu_to_le32(get_rfc1002_length(req) + 4);
  1339. } else /* END_OF_CHAIN */
  1340. req->hdr.NextCommand = 0;
  1341. if (request_type & RELATED_REQUEST) {
  1342. req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1343. /*
  1344. * Related requests use info from previous read request
  1345. * in chain.
  1346. */
  1347. req->hdr.SessionId = 0xFFFFFFFF;
  1348. req->hdr.TreeId = 0xFFFFFFFF;
  1349. req->PersistentFileId = 0xFFFFFFFF;
  1350. req->VolatileFileId = 0xFFFFFFFF;
  1351. }
  1352. }
  1353. if (remaining_bytes > io_parms->length)
  1354. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  1355. else
  1356. req->RemainingBytes = 0;
  1357. iov[0].iov_base = (char *)req;
  1358. /* 4 for rfc1002 length field */
  1359. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1360. return rc;
  1361. }
  1362. static void
  1363. smb2_readv_callback(struct mid_q_entry *mid)
  1364. {
  1365. struct cifs_readdata *rdata = mid->callback_data;
  1366. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1367. struct TCP_Server_Info *server = tcon->ses->server;
  1368. struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1369. unsigned int credits_received = 1;
  1370. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1371. .rq_nvec = 1,
  1372. .rq_pages = rdata->pages,
  1373. .rq_npages = rdata->nr_pages,
  1374. .rq_pagesz = rdata->pagesz,
  1375. .rq_tailsz = rdata->tailsz };
  1376. cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
  1377. __func__, mid->mid, mid->mid_state, rdata->result,
  1378. rdata->bytes);
  1379. switch (mid->mid_state) {
  1380. case MID_RESPONSE_RECEIVED:
  1381. credits_received = le16_to_cpu(buf->CreditRequest);
  1382. /* result already set, check signature */
  1383. if (server->sign) {
  1384. int rc;
  1385. rc = smb2_verify_signature(&rqst, server);
  1386. if (rc)
  1387. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  1388. rc);
  1389. }
  1390. /* FIXME: should this be counted toward the initiating task? */
  1391. task_io_account_read(rdata->bytes);
  1392. cifs_stats_bytes_read(tcon, rdata->bytes);
  1393. break;
  1394. case MID_REQUEST_SUBMITTED:
  1395. case MID_RETRY_NEEDED:
  1396. rdata->result = -EAGAIN;
  1397. break;
  1398. default:
  1399. if (rdata->result != -ENODATA)
  1400. rdata->result = -EIO;
  1401. }
  1402. if (rdata->result)
  1403. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  1404. queue_work(cifsiod_wq, &rdata->work);
  1405. DeleteMidQEntry(mid);
  1406. add_credits(server, credits_received, 0);
  1407. }
  1408. /* smb2_async_readv - send an async write, and set up mid to handle result */
  1409. int
  1410. smb2_async_readv(struct cifs_readdata *rdata)
  1411. {
  1412. int rc;
  1413. struct smb2_hdr *buf;
  1414. struct cifs_io_parms io_parms;
  1415. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1416. .rq_nvec = 1 };
  1417. cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
  1418. __func__, rdata->offset, rdata->bytes);
  1419. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  1420. io_parms.offset = rdata->offset;
  1421. io_parms.length = rdata->bytes;
  1422. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  1423. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  1424. io_parms.pid = rdata->pid;
  1425. rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
  1426. if (rc)
  1427. return rc;
  1428. buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1429. /* 4 for rfc1002 length field */
  1430. rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
  1431. kref_get(&rdata->refcount);
  1432. rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
  1433. cifs_readv_receive, smb2_readv_callback,
  1434. rdata, 0);
  1435. if (rc) {
  1436. kref_put(&rdata->refcount, cifs_readdata_release);
  1437. cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
  1438. }
  1439. cifs_small_buf_release(buf);
  1440. return rc;
  1441. }
  1442. int
  1443. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  1444. unsigned int *nbytes, char **buf, int *buf_type)
  1445. {
  1446. int resp_buftype, rc = -EACCES;
  1447. struct smb2_read_rsp *rsp = NULL;
  1448. struct kvec iov[1];
  1449. *nbytes = 0;
  1450. rc = smb2_new_read_req(iov, io_parms, 0, 0);
  1451. if (rc)
  1452. return rc;
  1453. rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
  1454. &resp_buftype, CIFS_LOG_ERROR);
  1455. rsp = (struct smb2_read_rsp *)iov[0].iov_base;
  1456. if (rsp->hdr.Status == STATUS_END_OF_FILE) {
  1457. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1458. return 0;
  1459. }
  1460. if (rc) {
  1461. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  1462. cifs_dbg(VFS, "Send error in read = %d\n", rc);
  1463. } else {
  1464. *nbytes = le32_to_cpu(rsp->DataLength);
  1465. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  1466. (*nbytes > io_parms->length)) {
  1467. cifs_dbg(FYI, "bad length %d for count %d\n",
  1468. *nbytes, io_parms->length);
  1469. rc = -EIO;
  1470. *nbytes = 0;
  1471. }
  1472. }
  1473. if (*buf) {
  1474. memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
  1475. *nbytes);
  1476. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1477. } else if (resp_buftype != CIFS_NO_BUFFER) {
  1478. *buf = iov[0].iov_base;
  1479. if (resp_buftype == CIFS_SMALL_BUFFER)
  1480. *buf_type = CIFS_SMALL_BUFFER;
  1481. else if (resp_buftype == CIFS_LARGE_BUFFER)
  1482. *buf_type = CIFS_LARGE_BUFFER;
  1483. }
  1484. return rc;
  1485. }
  1486. /*
  1487. * Check the mid_state and signature on received buffer (if any), and queue the
  1488. * workqueue completion task.
  1489. */
  1490. static void
  1491. smb2_writev_callback(struct mid_q_entry *mid)
  1492. {
  1493. struct cifs_writedata *wdata = mid->callback_data;
  1494. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1495. unsigned int written;
  1496. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  1497. unsigned int credits_received = 1;
  1498. switch (mid->mid_state) {
  1499. case MID_RESPONSE_RECEIVED:
  1500. credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
  1501. wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
  1502. if (wdata->result != 0)
  1503. break;
  1504. written = le32_to_cpu(rsp->DataLength);
  1505. /*
  1506. * Mask off high 16 bits when bytes written as returned
  1507. * by the server is greater than bytes requested by the
  1508. * client. OS/2 servers are known to set incorrect
  1509. * CountHigh values.
  1510. */
  1511. if (written > wdata->bytes)
  1512. written &= 0xFFFF;
  1513. if (written < wdata->bytes)
  1514. wdata->result = -ENOSPC;
  1515. else
  1516. wdata->bytes = written;
  1517. break;
  1518. case MID_REQUEST_SUBMITTED:
  1519. case MID_RETRY_NEEDED:
  1520. wdata->result = -EAGAIN;
  1521. break;
  1522. default:
  1523. wdata->result = -EIO;
  1524. break;
  1525. }
  1526. if (wdata->result)
  1527. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1528. queue_work(cifsiod_wq, &wdata->work);
  1529. DeleteMidQEntry(mid);
  1530. add_credits(tcon->ses->server, credits_received, 0);
  1531. }
  1532. /* smb2_async_writev - send an async write, and set up mid to handle result */
  1533. int
  1534. smb2_async_writev(struct cifs_writedata *wdata)
  1535. {
  1536. int rc = -EACCES;
  1537. struct smb2_write_req *req = NULL;
  1538. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1539. struct kvec iov;
  1540. struct smb_rqst rqst;
  1541. rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
  1542. if (rc)
  1543. goto async_writev_out;
  1544. req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
  1545. req->PersistentFileId = wdata->cfile->fid.persistent_fid;
  1546. req->VolatileFileId = wdata->cfile->fid.volatile_fid;
  1547. req->WriteChannelInfoOffset = 0;
  1548. req->WriteChannelInfoLength = 0;
  1549. req->Channel = 0;
  1550. req->Offset = cpu_to_le64(wdata->offset);
  1551. /* 4 for rfc1002 length field */
  1552. req->DataOffset = cpu_to_le16(
  1553. offsetof(struct smb2_write_req, Buffer) - 4);
  1554. req->RemainingBytes = 0;
  1555. /* 4 for rfc1002 length field and 1 for Buffer */
  1556. iov.iov_len = get_rfc1002_length(req) + 4 - 1;
  1557. iov.iov_base = req;
  1558. rqst.rq_iov = &iov;
  1559. rqst.rq_nvec = 1;
  1560. rqst.rq_pages = wdata->pages;
  1561. rqst.rq_npages = wdata->nr_pages;
  1562. rqst.rq_pagesz = wdata->pagesz;
  1563. rqst.rq_tailsz = wdata->tailsz;
  1564. cifs_dbg(FYI, "async write at %llu %u bytes\n",
  1565. wdata->offset, wdata->bytes);
  1566. req->Length = cpu_to_le32(wdata->bytes);
  1567. inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
  1568. kref_get(&wdata->refcount);
  1569. rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
  1570. smb2_writev_callback, wdata, 0);
  1571. if (rc) {
  1572. kref_put(&wdata->refcount, cifs_writedata_release);
  1573. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1574. }
  1575. async_writev_out:
  1576. cifs_small_buf_release(req);
  1577. return rc;
  1578. }
  1579. /*
  1580. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  1581. * The length field from io_parms must be at least 1 and indicates a number of
  1582. * elements with data to write that begins with position 1 in iov array. All
  1583. * data length is specified by count.
  1584. */
  1585. int
  1586. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  1587. unsigned int *nbytes, struct kvec *iov, int n_vec)
  1588. {
  1589. int rc = 0;
  1590. struct smb2_write_req *req = NULL;
  1591. struct smb2_write_rsp *rsp = NULL;
  1592. int resp_buftype;
  1593. *nbytes = 0;
  1594. if (n_vec < 1)
  1595. return rc;
  1596. rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
  1597. if (rc)
  1598. return rc;
  1599. if (io_parms->tcon->ses->server == NULL)
  1600. return -ECONNABORTED;
  1601. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1602. req->PersistentFileId = io_parms->persistent_fid;
  1603. req->VolatileFileId = io_parms->volatile_fid;
  1604. req->WriteChannelInfoOffset = 0;
  1605. req->WriteChannelInfoLength = 0;
  1606. req->Channel = 0;
  1607. req->Length = cpu_to_le32(io_parms->length);
  1608. req->Offset = cpu_to_le64(io_parms->offset);
  1609. /* 4 for rfc1002 length field */
  1610. req->DataOffset = cpu_to_le16(
  1611. offsetof(struct smb2_write_req, Buffer) - 4);
  1612. req->RemainingBytes = 0;
  1613. iov[0].iov_base = (char *)req;
  1614. /* 4 for rfc1002 length field and 1 for Buffer */
  1615. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1616. /* length of entire message including data to be written */
  1617. inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
  1618. rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
  1619. &resp_buftype, 0);
  1620. rsp = (struct smb2_write_rsp *)iov[0].iov_base;
  1621. if (rc) {
  1622. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  1623. cifs_dbg(VFS, "Send error in write = %d\n", rc);
  1624. } else
  1625. *nbytes = le32_to_cpu(rsp->DataLength);
  1626. free_rsp_buf(resp_buftype, rsp);
  1627. return rc;
  1628. }
  1629. static unsigned int
  1630. num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
  1631. {
  1632. int len;
  1633. unsigned int entrycount = 0;
  1634. unsigned int next_offset = 0;
  1635. FILE_DIRECTORY_INFO *entryptr;
  1636. if (bufstart == NULL)
  1637. return 0;
  1638. entryptr = (FILE_DIRECTORY_INFO *)bufstart;
  1639. while (1) {
  1640. entryptr = (FILE_DIRECTORY_INFO *)
  1641. ((char *)entryptr + next_offset);
  1642. if ((char *)entryptr + size > end_of_buf) {
  1643. cifs_dbg(VFS, "malformed search entry would overflow\n");
  1644. break;
  1645. }
  1646. len = le32_to_cpu(entryptr->FileNameLength);
  1647. if ((char *)entryptr + len + size > end_of_buf) {
  1648. cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
  1649. end_of_buf);
  1650. break;
  1651. }
  1652. *lastentry = (char *)entryptr;
  1653. entrycount++;
  1654. next_offset = le32_to_cpu(entryptr->NextEntryOffset);
  1655. if (!next_offset)
  1656. break;
  1657. }
  1658. return entrycount;
  1659. }
  1660. /*
  1661. * Readdir/FindFirst
  1662. */
  1663. int
  1664. SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
  1665. u64 persistent_fid, u64 volatile_fid, int index,
  1666. struct cifs_search_info *srch_inf)
  1667. {
  1668. struct smb2_query_directory_req *req;
  1669. struct smb2_query_directory_rsp *rsp = NULL;
  1670. struct kvec iov[2];
  1671. int rc = 0;
  1672. int len;
  1673. int resp_buftype;
  1674. unsigned char *bufptr;
  1675. struct TCP_Server_Info *server;
  1676. struct cifs_ses *ses = tcon->ses;
  1677. __le16 asteriks = cpu_to_le16('*');
  1678. char *end_of_smb;
  1679. unsigned int output_size = CIFSMaxBufSize;
  1680. size_t info_buf_size;
  1681. if (ses && (ses->server))
  1682. server = ses->server;
  1683. else
  1684. return -EIO;
  1685. rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
  1686. if (rc)
  1687. return rc;
  1688. switch (srch_inf->info_level) {
  1689. case SMB_FIND_FILE_DIRECTORY_INFO:
  1690. req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
  1691. info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
  1692. break;
  1693. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  1694. req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
  1695. info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
  1696. break;
  1697. default:
  1698. cifs_dbg(VFS, "info level %u isn't supported\n",
  1699. srch_inf->info_level);
  1700. rc = -EINVAL;
  1701. goto qdir_exit;
  1702. }
  1703. req->FileIndex = cpu_to_le32(index);
  1704. req->PersistentFileId = persistent_fid;
  1705. req->VolatileFileId = volatile_fid;
  1706. len = 0x2;
  1707. bufptr = req->Buffer;
  1708. memcpy(bufptr, &asteriks, len);
  1709. req->FileNameOffset =
  1710. cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
  1711. req->FileNameLength = cpu_to_le16(len);
  1712. /*
  1713. * BB could be 30 bytes or so longer if we used SMB2 specific
  1714. * buffer lengths, but this is safe and close enough.
  1715. */
  1716. output_size = min_t(unsigned int, output_size, server->maxBuf);
  1717. output_size = min_t(unsigned int, output_size, 2 << 15);
  1718. req->OutputBufferLength = cpu_to_le32(output_size);
  1719. iov[0].iov_base = (char *)req;
  1720. /* 4 for RFC1001 length and 1 for Buffer */
  1721. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1722. iov[1].iov_base = (char *)(req->Buffer);
  1723. iov[1].iov_len = len;
  1724. inc_rfc1001_len(req, len - 1 /* Buffer */);
  1725. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  1726. rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
  1727. if (rc) {
  1728. cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
  1729. goto qdir_exit;
  1730. }
  1731. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1732. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1733. info_buf_size);
  1734. if (rc)
  1735. goto qdir_exit;
  1736. srch_inf->unicode = true;
  1737. if (srch_inf->ntwrk_buf_start) {
  1738. if (srch_inf->smallBuf)
  1739. cifs_small_buf_release(srch_inf->ntwrk_buf_start);
  1740. else
  1741. cifs_buf_release(srch_inf->ntwrk_buf_start);
  1742. }
  1743. srch_inf->ntwrk_buf_start = (char *)rsp;
  1744. srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
  1745. (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
  1746. /* 4 for rfc1002 length field */
  1747. end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
  1748. srch_inf->entries_in_buffer =
  1749. num_entries(srch_inf->srch_entries_start, end_of_smb,
  1750. &srch_inf->last_entry, info_buf_size);
  1751. srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
  1752. cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
  1753. srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
  1754. srch_inf->srch_entries_start, srch_inf->last_entry);
  1755. if (resp_buftype == CIFS_LARGE_BUFFER)
  1756. srch_inf->smallBuf = false;
  1757. else if (resp_buftype == CIFS_SMALL_BUFFER)
  1758. srch_inf->smallBuf = true;
  1759. else
  1760. cifs_dbg(VFS, "illegal search buffer type\n");
  1761. if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
  1762. srch_inf->endOfSearch = 1;
  1763. else
  1764. srch_inf->endOfSearch = 0;
  1765. return rc;
  1766. qdir_exit:
  1767. free_rsp_buf(resp_buftype, rsp);
  1768. return rc;
  1769. }
  1770. static int
  1771. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1772. u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
  1773. unsigned int num, void **data, unsigned int *size)
  1774. {
  1775. struct smb2_set_info_req *req;
  1776. struct smb2_set_info_rsp *rsp = NULL;
  1777. struct kvec *iov;
  1778. int rc = 0;
  1779. int resp_buftype;
  1780. unsigned int i;
  1781. struct TCP_Server_Info *server;
  1782. struct cifs_ses *ses = tcon->ses;
  1783. if (ses && (ses->server))
  1784. server = ses->server;
  1785. else
  1786. return -EIO;
  1787. if (!num)
  1788. return -EINVAL;
  1789. iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
  1790. if (!iov)
  1791. return -ENOMEM;
  1792. rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
  1793. if (rc) {
  1794. kfree(iov);
  1795. return rc;
  1796. }
  1797. req->hdr.ProcessId = cpu_to_le32(pid);
  1798. req->InfoType = SMB2_O_INFO_FILE;
  1799. req->FileInfoClass = info_class;
  1800. req->PersistentFileId = persistent_fid;
  1801. req->VolatileFileId = volatile_fid;
  1802. /* 4 for RFC1001 length and 1 for Buffer */
  1803. req->BufferOffset =
  1804. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
  1805. req->BufferLength = cpu_to_le32(*size);
  1806. inc_rfc1001_len(req, *size - 1 /* Buffer */);
  1807. memcpy(req->Buffer, *data, *size);
  1808. iov[0].iov_base = (char *)req;
  1809. /* 4 for RFC1001 length */
  1810. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1811. for (i = 1; i < num; i++) {
  1812. inc_rfc1001_len(req, size[i]);
  1813. le32_add_cpu(&req->BufferLength, size[i]);
  1814. iov[i].iov_base = (char *)data[i];
  1815. iov[i].iov_len = size[i];
  1816. }
  1817. rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
  1818. rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
  1819. if (rc != 0) {
  1820. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  1821. goto out;
  1822. }
  1823. out:
  1824. free_rsp_buf(resp_buftype, rsp);
  1825. kfree(iov);
  1826. return rc;
  1827. }
  1828. int
  1829. SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
  1830. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1831. {
  1832. struct smb2_file_rename_info info;
  1833. void **data;
  1834. unsigned int size[2];
  1835. int rc;
  1836. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1837. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1838. if (!data)
  1839. return -ENOMEM;
  1840. info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
  1841. /* 0 = fail if target already exists */
  1842. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1843. info.FileNameLength = cpu_to_le32(len);
  1844. data[0] = &info;
  1845. size[0] = sizeof(struct smb2_file_rename_info);
  1846. data[1] = target_file;
  1847. size[1] = len + 2 /* null */;
  1848. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1849. current->tgid, FILE_RENAME_INFORMATION, 2, data,
  1850. size);
  1851. kfree(data);
  1852. return rc;
  1853. }
  1854. int
  1855. SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  1856. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1857. {
  1858. struct smb2_file_link_info info;
  1859. void **data;
  1860. unsigned int size[2];
  1861. int rc;
  1862. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1863. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1864. if (!data)
  1865. return -ENOMEM;
  1866. info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
  1867. /* 0 = fail if link already exists */
  1868. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1869. info.FileNameLength = cpu_to_le32(len);
  1870. data[0] = &info;
  1871. size[0] = sizeof(struct smb2_file_link_info);
  1872. data[1] = target_file;
  1873. size[1] = len + 2 /* null */;
  1874. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1875. current->tgid, FILE_LINK_INFORMATION, 2, data, size);
  1876. kfree(data);
  1877. return rc;
  1878. }
  1879. int
  1880. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1881. u64 volatile_fid, u32 pid, __le64 *eof)
  1882. {
  1883. struct smb2_file_eof_info info;
  1884. void *data;
  1885. unsigned int size;
  1886. info.EndOfFile = *eof;
  1887. data = &info;
  1888. size = sizeof(struct smb2_file_eof_info);
  1889. return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
  1890. FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
  1891. }
  1892. int
  1893. SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1894. u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
  1895. {
  1896. unsigned int size;
  1897. size = sizeof(FILE_BASIC_INFO);
  1898. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1899. current->tgid, FILE_BASIC_INFORMATION, 1,
  1900. (void **)&buf, &size);
  1901. }
  1902. int
  1903. SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
  1904. const u64 persistent_fid, const u64 volatile_fid,
  1905. __u8 oplock_level)
  1906. {
  1907. int rc;
  1908. struct smb2_oplock_break *req = NULL;
  1909. cifs_dbg(FYI, "SMB2_oplock_break\n");
  1910. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  1911. if (rc)
  1912. return rc;
  1913. req->VolatileFid = volatile_fid;
  1914. req->PersistentFid = persistent_fid;
  1915. req->OplockLevel = oplock_level;
  1916. req->hdr.CreditRequest = cpu_to_le16(1);
  1917. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  1918. /* SMB2 buffer freed by function above */
  1919. if (rc) {
  1920. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  1921. cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
  1922. }
  1923. return rc;
  1924. }
  1925. static void
  1926. copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
  1927. struct kstatfs *kst)
  1928. {
  1929. kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
  1930. le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
  1931. kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
  1932. kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
  1933. kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
  1934. return;
  1935. }
  1936. static int
  1937. build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
  1938. int outbuf_len, u64 persistent_fid, u64 volatile_fid)
  1939. {
  1940. int rc;
  1941. struct smb2_query_info_req *req;
  1942. cifs_dbg(FYI, "Query FSInfo level %d\n", level);
  1943. if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
  1944. return -EIO;
  1945. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1946. if (rc)
  1947. return rc;
  1948. req->InfoType = SMB2_O_INFO_FILESYSTEM;
  1949. req->FileInfoClass = level;
  1950. req->PersistentFileId = persistent_fid;
  1951. req->VolatileFileId = volatile_fid;
  1952. /* 4 for rfc1002 length field and 1 for pad */
  1953. req->InputBufferOffset =
  1954. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1955. req->OutputBufferLength = cpu_to_le32(
  1956. outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
  1957. iov->iov_base = (char *)req;
  1958. /* 4 for rfc1002 length field */
  1959. iov->iov_len = get_rfc1002_length(req) + 4;
  1960. return 0;
  1961. }
  1962. int
  1963. SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
  1964. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  1965. {
  1966. struct smb2_query_info_rsp *rsp = NULL;
  1967. struct kvec iov;
  1968. int rc = 0;
  1969. int resp_buftype;
  1970. struct cifs_ses *ses = tcon->ses;
  1971. struct smb2_fs_full_size_info *info = NULL;
  1972. rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
  1973. sizeof(struct smb2_fs_full_size_info),
  1974. persistent_fid, volatile_fid);
  1975. if (rc)
  1976. return rc;
  1977. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  1978. if (rc) {
  1979. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1980. goto qinf_exit;
  1981. }
  1982. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  1983. info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
  1984. le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
  1985. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1986. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1987. sizeof(struct smb2_fs_full_size_info));
  1988. if (!rc)
  1989. copy_fs_info_to_kstatfs(info, fsdata);
  1990. qinf_exit:
  1991. free_rsp_buf(resp_buftype, iov.iov_base);
  1992. return rc;
  1993. }
  1994. int
  1995. smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
  1996. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  1997. const __u32 num_lock, struct smb2_lock_element *buf)
  1998. {
  1999. int rc = 0;
  2000. struct smb2_lock_req *req = NULL;
  2001. struct kvec iov[2];
  2002. int resp_buf_type;
  2003. unsigned int count;
  2004. cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
  2005. rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
  2006. if (rc)
  2007. return rc;
  2008. req->hdr.ProcessId = cpu_to_le32(pid);
  2009. req->LockCount = cpu_to_le16(num_lock);
  2010. req->PersistentFileId = persist_fid;
  2011. req->VolatileFileId = volatile_fid;
  2012. count = num_lock * sizeof(struct smb2_lock_element);
  2013. inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
  2014. iov[0].iov_base = (char *)req;
  2015. /* 4 for rfc1002 length field and count for all locks */
  2016. iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
  2017. iov[1].iov_base = (char *)buf;
  2018. iov[1].iov_len = count;
  2019. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  2020. rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
  2021. if (rc) {
  2022. cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
  2023. cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
  2024. }
  2025. return rc;
  2026. }
  2027. int
  2028. SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
  2029. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  2030. const __u64 length, const __u64 offset, const __u32 lock_flags,
  2031. const bool wait)
  2032. {
  2033. struct smb2_lock_element lock;
  2034. lock.Offset = cpu_to_le64(offset);
  2035. lock.Length = cpu_to_le64(length);
  2036. lock.Flags = cpu_to_le32(lock_flags);
  2037. if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
  2038. lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  2039. return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
  2040. }
  2041. int
  2042. SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
  2043. __u8 *lease_key, const __le32 lease_state)
  2044. {
  2045. int rc;
  2046. struct smb2_lease_ack *req = NULL;
  2047. cifs_dbg(FYI, "SMB2_lease_break\n");
  2048. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  2049. if (rc)
  2050. return rc;
  2051. req->hdr.CreditRequest = cpu_to_le16(1);
  2052. req->StructureSize = cpu_to_le16(36);
  2053. inc_rfc1001_len(req, 12);
  2054. memcpy(req->LeaseKey, lease_key, 16);
  2055. req->LeaseState = lease_state;
  2056. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  2057. /* SMB2 buffer freed by function above */
  2058. if (rc) {
  2059. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  2060. cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
  2061. }
  2062. return rc;
  2063. }