smb2pdu.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396
  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 struct create_durable *
  737. create_durable_buf(void)
  738. {
  739. struct create_durable *buf;
  740. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  741. if (!buf)
  742. return NULL;
  743. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  744. (struct create_durable, Data));
  745. buf->ccontext.DataLength = cpu_to_le32(16);
  746. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  747. (struct create_durable, Name));
  748. buf->ccontext.NameLength = cpu_to_le16(4);
  749. buf->Name[0] = 'D';
  750. buf->Name[1] = 'H';
  751. buf->Name[2] = 'n';
  752. buf->Name[3] = 'Q';
  753. return buf;
  754. }
  755. static struct create_durable *
  756. create_reconnect_durable_buf(struct cifs_fid *fid)
  757. {
  758. struct create_durable *buf;
  759. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  760. if (!buf)
  761. return NULL;
  762. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  763. (struct create_durable, Data));
  764. buf->ccontext.DataLength = cpu_to_le32(16);
  765. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  766. (struct create_durable, Name));
  767. buf->ccontext.NameLength = cpu_to_le16(4);
  768. buf->Data.Fid.PersistentFileId = fid->persistent_fid;
  769. buf->Data.Fid.VolatileFileId = fid->volatile_fid;
  770. buf->Name[0] = 'D';
  771. buf->Name[1] = 'H';
  772. buf->Name[2] = 'n';
  773. buf->Name[3] = 'C';
  774. return buf;
  775. }
  776. static __u8
  777. parse_lease_state(struct smb2_create_rsp *rsp)
  778. {
  779. char *data_offset;
  780. struct create_lease *lc;
  781. bool found = false;
  782. unsigned int next = 0;
  783. char *name;
  784. data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
  785. lc = (struct create_lease *)data_offset;
  786. do {
  787. lc = (struct create_lease *)((char *)lc + next);
  788. name = le16_to_cpu(lc->ccontext.NameOffset) + (char *)lc;
  789. if (le16_to_cpu(lc->ccontext.NameLength) != 4 ||
  790. strncmp(name, "RqLs", 4)) {
  791. next = le32_to_cpu(lc->ccontext.Next);
  792. continue;
  793. }
  794. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  795. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  796. found = true;
  797. break;
  798. } while (next != 0);
  799. if (!found)
  800. return 0;
  801. return smb2_map_lease_to_oplock(lc->lcontext.LeaseState);
  802. }
  803. static int
  804. add_lease_context(struct kvec *iov, unsigned int *num_iovec, __u8 *oplock)
  805. {
  806. struct smb2_create_req *req = iov[0].iov_base;
  807. unsigned int num = *num_iovec;
  808. iov[num].iov_base = create_lease_buf(oplock+1, *oplock);
  809. if (iov[num].iov_base == NULL)
  810. return -ENOMEM;
  811. iov[num].iov_len = sizeof(struct create_lease);
  812. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
  813. if (!req->CreateContextsOffset)
  814. req->CreateContextsOffset = cpu_to_le32(
  815. sizeof(struct smb2_create_req) - 4 +
  816. iov[num - 1].iov_len);
  817. req->CreateContextsLength = cpu_to_le32(
  818. le32_to_cpu(req->CreateContextsLength) +
  819. sizeof(struct create_lease));
  820. inc_rfc1001_len(&req->hdr, sizeof(struct create_lease));
  821. *num_iovec = num + 1;
  822. return 0;
  823. }
  824. static int
  825. add_durable_context(struct kvec *iov, unsigned int *num_iovec,
  826. struct cifs_open_parms *oparms)
  827. {
  828. struct smb2_create_req *req = iov[0].iov_base;
  829. unsigned int num = *num_iovec;
  830. if (oparms->reconnect) {
  831. iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
  832. /* indicate that we don't need to relock the file */
  833. oparms->reconnect = false;
  834. } else
  835. iov[num].iov_base = create_durable_buf();
  836. if (iov[num].iov_base == NULL)
  837. return -ENOMEM;
  838. iov[num].iov_len = sizeof(struct create_durable);
  839. if (!req->CreateContextsOffset)
  840. req->CreateContextsOffset =
  841. cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
  842. iov[1].iov_len);
  843. req->CreateContextsLength =
  844. cpu_to_le32(le32_to_cpu(req->CreateContextsLength) +
  845. sizeof(struct create_durable));
  846. inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
  847. *num_iovec = num + 1;
  848. return 0;
  849. }
  850. int
  851. SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
  852. __u8 *oplock, struct smb2_file_all_info *buf)
  853. {
  854. struct smb2_create_req *req;
  855. struct smb2_create_rsp *rsp;
  856. struct TCP_Server_Info *server;
  857. struct cifs_tcon *tcon = oparms->tcon;
  858. struct cifs_ses *ses = tcon->ses;
  859. struct kvec iov[4];
  860. int resp_buftype;
  861. int uni_path_len;
  862. __le16 *copy_path = NULL;
  863. int copy_size;
  864. int rc = 0;
  865. unsigned int num_iovecs = 2;
  866. __u32 file_attributes = 0;
  867. cifs_dbg(FYI, "create/open\n");
  868. if (ses && (ses->server))
  869. server = ses->server;
  870. else
  871. return -EIO;
  872. rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
  873. if (rc)
  874. return rc;
  875. if (oparms->create_options & CREATE_OPTION_READONLY)
  876. file_attributes |= ATTR_READONLY;
  877. req->ImpersonationLevel = IL_IMPERSONATION;
  878. req->DesiredAccess = cpu_to_le32(oparms->desired_access);
  879. /* File attributes ignored on open (used in create though) */
  880. req->FileAttributes = cpu_to_le32(file_attributes);
  881. req->ShareAccess = FILE_SHARE_ALL_LE;
  882. req->CreateDisposition = cpu_to_le32(oparms->disposition);
  883. req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
  884. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  885. /* do not count rfc1001 len field */
  886. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
  887. iov[0].iov_base = (char *)req;
  888. /* 4 for rfc1002 length field */
  889. iov[0].iov_len = get_rfc1002_length(req) + 4;
  890. /* MUST set path len (NameLength) to 0 opening root of share */
  891. req->NameLength = cpu_to_le16(uni_path_len - 2);
  892. /* -1 since last byte is buf[0] which is sent below (path) */
  893. iov[0].iov_len--;
  894. if (uni_path_len % 8 != 0) {
  895. copy_size = uni_path_len / 8 * 8;
  896. if (copy_size < uni_path_len)
  897. copy_size += 8;
  898. copy_path = kzalloc(copy_size, GFP_KERNEL);
  899. if (!copy_path)
  900. return -ENOMEM;
  901. memcpy((char *)copy_path, (const char *)path,
  902. uni_path_len);
  903. uni_path_len = copy_size;
  904. path = copy_path;
  905. }
  906. iov[1].iov_len = uni_path_len;
  907. iov[1].iov_base = path;
  908. /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
  909. inc_rfc1001_len(req, uni_path_len - 1);
  910. if (!server->oplocks)
  911. *oplock = SMB2_OPLOCK_LEVEL_NONE;
  912. if (!(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
  913. *oplock == SMB2_OPLOCK_LEVEL_NONE)
  914. req->RequestedOplockLevel = *oplock;
  915. else {
  916. rc = add_lease_context(iov, &num_iovecs, oplock);
  917. if (rc) {
  918. cifs_small_buf_release(req);
  919. kfree(copy_path);
  920. return rc;
  921. }
  922. }
  923. if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  924. /* need to set Next field of lease context if we request it */
  925. if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
  926. struct create_context *ccontext =
  927. (struct create_context *)iov[num_iovecs-1].iov_base;
  928. ccontext->Next =
  929. cpu_to_le32(sizeof(struct create_lease));
  930. }
  931. rc = add_durable_context(iov, &num_iovecs, oparms);
  932. if (rc) {
  933. cifs_small_buf_release(req);
  934. kfree(copy_path);
  935. kfree(iov[num_iovecs-1].iov_base);
  936. return rc;
  937. }
  938. }
  939. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  940. rsp = (struct smb2_create_rsp *)iov[0].iov_base;
  941. if (rc != 0) {
  942. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  943. goto creat_exit;
  944. }
  945. oparms->fid->persistent_fid = rsp->PersistentFileId;
  946. oparms->fid->volatile_fid = rsp->VolatileFileId;
  947. if (buf) {
  948. memcpy(buf, &rsp->CreationTime, 32);
  949. buf->AllocationSize = rsp->AllocationSize;
  950. buf->EndOfFile = rsp->EndofFile;
  951. buf->Attributes = rsp->FileAttributes;
  952. buf->NumberOfLinks = cpu_to_le32(1);
  953. buf->DeletePending = 0;
  954. }
  955. if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
  956. *oplock = parse_lease_state(rsp);
  957. else
  958. *oplock = rsp->OplockLevel;
  959. creat_exit:
  960. kfree(copy_path);
  961. free_rsp_buf(resp_buftype, rsp);
  962. return rc;
  963. }
  964. /*
  965. * SMB2 IOCTL is used for both IOCTLs and FSCTLs
  966. */
  967. int
  968. SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  969. u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
  970. u32 indatalen, char **out_data, u32 *plen /* returned data len */)
  971. {
  972. struct smb2_ioctl_req *req;
  973. struct smb2_ioctl_rsp *rsp;
  974. struct TCP_Server_Info *server;
  975. struct cifs_ses *ses = tcon->ses;
  976. struct kvec iov[2];
  977. int resp_buftype;
  978. int num_iovecs;
  979. int rc = 0;
  980. cifs_dbg(FYI, "SMB2 IOCTL\n");
  981. /* zero out returned data len, in case of error */
  982. if (plen)
  983. *plen = 0;
  984. if (ses && (ses->server))
  985. server = ses->server;
  986. else
  987. return -EIO;
  988. rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
  989. if (rc)
  990. return rc;
  991. req->CtlCode = cpu_to_le32(opcode);
  992. req->PersistentFileId = persistent_fid;
  993. req->VolatileFileId = volatile_fid;
  994. if (indatalen) {
  995. req->InputCount = cpu_to_le32(indatalen);
  996. /* do not set InputOffset if no input data */
  997. req->InputOffset =
  998. cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
  999. iov[1].iov_base = in_data;
  1000. iov[1].iov_len = indatalen;
  1001. num_iovecs = 2;
  1002. } else
  1003. num_iovecs = 1;
  1004. req->OutputOffset = 0;
  1005. req->OutputCount = 0; /* MBZ */
  1006. /*
  1007. * Could increase MaxOutputResponse, but that would require more
  1008. * than one credit. Windows typically sets this smaller, but for some
  1009. * ioctls it may be useful to allow server to send more. No point
  1010. * limiting what the server can send as long as fits in one credit
  1011. */
  1012. req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
  1013. if (is_fsctl)
  1014. req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
  1015. else
  1016. req->Flags = 0;
  1017. iov[0].iov_base = (char *)req;
  1018. /* 4 for rfc1002 length field */
  1019. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1020. if (indatalen)
  1021. inc_rfc1001_len(req, indatalen);
  1022. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  1023. rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
  1024. if (rc != 0) {
  1025. if (tcon)
  1026. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  1027. goto ioctl_exit;
  1028. }
  1029. /* check if caller wants to look at return data or just return rc */
  1030. if ((plen == NULL) || (out_data == NULL))
  1031. goto ioctl_exit;
  1032. *plen = le32_to_cpu(rsp->OutputCount);
  1033. /* We check for obvious errors in the output buffer length and offset */
  1034. if (*plen == 0)
  1035. goto ioctl_exit; /* server returned no data */
  1036. else if (*plen > 0xFF00) {
  1037. cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
  1038. *plen = 0;
  1039. rc = -EIO;
  1040. goto ioctl_exit;
  1041. }
  1042. if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
  1043. cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
  1044. le32_to_cpu(rsp->OutputOffset));
  1045. *plen = 0;
  1046. rc = -EIO;
  1047. goto ioctl_exit;
  1048. }
  1049. *out_data = kmalloc(*plen, GFP_KERNEL);
  1050. if (*out_data == NULL) {
  1051. rc = -ENOMEM;
  1052. goto ioctl_exit;
  1053. }
  1054. memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
  1055. *plen);
  1056. ioctl_exit:
  1057. free_rsp_buf(resp_buftype, rsp);
  1058. return rc;
  1059. }
  1060. int
  1061. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  1062. u64 persistent_fid, u64 volatile_fid)
  1063. {
  1064. struct smb2_close_req *req;
  1065. struct smb2_close_rsp *rsp;
  1066. struct TCP_Server_Info *server;
  1067. struct cifs_ses *ses = tcon->ses;
  1068. struct kvec iov[1];
  1069. int resp_buftype;
  1070. int rc = 0;
  1071. cifs_dbg(FYI, "Close\n");
  1072. if (ses && (ses->server))
  1073. server = ses->server;
  1074. else
  1075. return -EIO;
  1076. rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
  1077. if (rc)
  1078. return rc;
  1079. req->PersistentFileId = persistent_fid;
  1080. req->VolatileFileId = volatile_fid;
  1081. iov[0].iov_base = (char *)req;
  1082. /* 4 for rfc1002 length field */
  1083. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1084. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1085. rsp = (struct smb2_close_rsp *)iov[0].iov_base;
  1086. if (rc != 0) {
  1087. if (tcon)
  1088. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  1089. goto close_exit;
  1090. }
  1091. /* BB FIXME - decode close response, update inode for caching */
  1092. close_exit:
  1093. free_rsp_buf(resp_buftype, rsp);
  1094. return rc;
  1095. }
  1096. static int
  1097. validate_buf(unsigned int offset, unsigned int buffer_length,
  1098. struct smb2_hdr *hdr, unsigned int min_buf_size)
  1099. {
  1100. unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
  1101. char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
  1102. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1103. char *end_of_buf = begin_of_buf + buffer_length;
  1104. if (buffer_length < min_buf_size) {
  1105. cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
  1106. buffer_length, min_buf_size);
  1107. return -EINVAL;
  1108. }
  1109. /* check if beyond RFC1001 maximum length */
  1110. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  1111. cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
  1112. buffer_length, smb_len);
  1113. return -EINVAL;
  1114. }
  1115. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  1116. cifs_dbg(VFS, "illegal server response, bad offset to data\n");
  1117. return -EINVAL;
  1118. }
  1119. return 0;
  1120. }
  1121. /*
  1122. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  1123. * Caller must free buffer.
  1124. */
  1125. static int
  1126. validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
  1127. struct smb2_hdr *hdr, unsigned int minbufsize,
  1128. char *data)
  1129. {
  1130. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  1131. int rc;
  1132. if (!data)
  1133. return -EINVAL;
  1134. rc = validate_buf(offset, buffer_length, hdr, minbufsize);
  1135. if (rc)
  1136. return rc;
  1137. memcpy(data, begin_of_buf, buffer_length);
  1138. return 0;
  1139. }
  1140. static int
  1141. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1142. u64 persistent_fid, u64 volatile_fid, u8 info_class,
  1143. size_t output_len, size_t min_len, void *data)
  1144. {
  1145. struct smb2_query_info_req *req;
  1146. struct smb2_query_info_rsp *rsp = NULL;
  1147. struct kvec iov[2];
  1148. int rc = 0;
  1149. int resp_buftype;
  1150. struct TCP_Server_Info *server;
  1151. struct cifs_ses *ses = tcon->ses;
  1152. cifs_dbg(FYI, "Query Info\n");
  1153. if (ses && (ses->server))
  1154. server = ses->server;
  1155. else
  1156. return -EIO;
  1157. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1158. if (rc)
  1159. return rc;
  1160. req->InfoType = SMB2_O_INFO_FILE;
  1161. req->FileInfoClass = info_class;
  1162. req->PersistentFileId = persistent_fid;
  1163. req->VolatileFileId = volatile_fid;
  1164. /* 4 for rfc1002 length field and 1 for Buffer */
  1165. req->InputBufferOffset =
  1166. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1167. req->OutputBufferLength = cpu_to_le32(output_len);
  1168. iov[0].iov_base = (char *)req;
  1169. /* 4 for rfc1002 length field */
  1170. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1171. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1172. rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
  1173. if (rc) {
  1174. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1175. goto qinf_exit;
  1176. }
  1177. rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1178. le32_to_cpu(rsp->OutputBufferLength),
  1179. &rsp->hdr, min_len, data);
  1180. qinf_exit:
  1181. free_rsp_buf(resp_buftype, rsp);
  1182. return rc;
  1183. }
  1184. int
  1185. SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  1186. u64 persistent_fid, u64 volatile_fid,
  1187. struct smb2_file_all_info *data)
  1188. {
  1189. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1190. FILE_ALL_INFORMATION,
  1191. sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  1192. sizeof(struct smb2_file_all_info), data);
  1193. }
  1194. int
  1195. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  1196. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  1197. {
  1198. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1199. FILE_INTERNAL_INFORMATION,
  1200. sizeof(struct smb2_file_internal_info),
  1201. sizeof(struct smb2_file_internal_info), uniqueid);
  1202. }
  1203. /*
  1204. * This is a no-op for now. We're not really interested in the reply, but
  1205. * rather in the fact that the server sent one and that server->lstrp
  1206. * gets updated.
  1207. *
  1208. * FIXME: maybe we should consider checking that the reply matches request?
  1209. */
  1210. static void
  1211. smb2_echo_callback(struct mid_q_entry *mid)
  1212. {
  1213. struct TCP_Server_Info *server = mid->callback_data;
  1214. struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
  1215. unsigned int credits_received = 1;
  1216. if (mid->mid_state == MID_RESPONSE_RECEIVED)
  1217. credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
  1218. DeleteMidQEntry(mid);
  1219. add_credits(server, credits_received, CIFS_ECHO_OP);
  1220. }
  1221. int
  1222. SMB2_echo(struct TCP_Server_Info *server)
  1223. {
  1224. struct smb2_echo_req *req;
  1225. int rc = 0;
  1226. struct kvec iov;
  1227. struct smb_rqst rqst = { .rq_iov = &iov,
  1228. .rq_nvec = 1 };
  1229. cifs_dbg(FYI, "In echo request\n");
  1230. rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
  1231. if (rc)
  1232. return rc;
  1233. req->hdr.CreditRequest = cpu_to_le16(1);
  1234. iov.iov_base = (char *)req;
  1235. /* 4 for rfc1002 length field */
  1236. iov.iov_len = get_rfc1002_length(req) + 4;
  1237. rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
  1238. CIFS_ECHO_OP);
  1239. if (rc)
  1240. cifs_dbg(FYI, "Echo request failed: %d\n", rc);
  1241. cifs_small_buf_release(req);
  1242. return rc;
  1243. }
  1244. int
  1245. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1246. u64 volatile_fid)
  1247. {
  1248. struct smb2_flush_req *req;
  1249. struct TCP_Server_Info *server;
  1250. struct cifs_ses *ses = tcon->ses;
  1251. struct kvec iov[1];
  1252. int resp_buftype;
  1253. int rc = 0;
  1254. cifs_dbg(FYI, "Flush\n");
  1255. if (ses && (ses->server))
  1256. server = ses->server;
  1257. else
  1258. return -EIO;
  1259. rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
  1260. if (rc)
  1261. return rc;
  1262. req->PersistentFileId = persistent_fid;
  1263. req->VolatileFileId = volatile_fid;
  1264. iov[0].iov_base = (char *)req;
  1265. /* 4 for rfc1002 length field */
  1266. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1267. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1268. if ((rc != 0) && tcon)
  1269. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  1270. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1271. return rc;
  1272. }
  1273. /*
  1274. * To form a chain of read requests, any read requests after the first should
  1275. * have the end_of_chain boolean set to true.
  1276. */
  1277. static int
  1278. smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
  1279. unsigned int remaining_bytes, int request_type)
  1280. {
  1281. int rc = -EACCES;
  1282. struct smb2_read_req *req = NULL;
  1283. rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
  1284. if (rc)
  1285. return rc;
  1286. if (io_parms->tcon->ses->server == NULL)
  1287. return -ECONNABORTED;
  1288. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1289. req->PersistentFileId = io_parms->persistent_fid;
  1290. req->VolatileFileId = io_parms->volatile_fid;
  1291. req->ReadChannelInfoOffset = 0; /* reserved */
  1292. req->ReadChannelInfoLength = 0; /* reserved */
  1293. req->Channel = 0; /* reserved */
  1294. req->MinimumCount = 0;
  1295. req->Length = cpu_to_le32(io_parms->length);
  1296. req->Offset = cpu_to_le64(io_parms->offset);
  1297. if (request_type & CHAINED_REQUEST) {
  1298. if (!(request_type & END_OF_CHAIN)) {
  1299. /* 4 for rfc1002 length field */
  1300. req->hdr.NextCommand =
  1301. cpu_to_le32(get_rfc1002_length(req) + 4);
  1302. } else /* END_OF_CHAIN */
  1303. req->hdr.NextCommand = 0;
  1304. if (request_type & RELATED_REQUEST) {
  1305. req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1306. /*
  1307. * Related requests use info from previous read request
  1308. * in chain.
  1309. */
  1310. req->hdr.SessionId = 0xFFFFFFFF;
  1311. req->hdr.TreeId = 0xFFFFFFFF;
  1312. req->PersistentFileId = 0xFFFFFFFF;
  1313. req->VolatileFileId = 0xFFFFFFFF;
  1314. }
  1315. }
  1316. if (remaining_bytes > io_parms->length)
  1317. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  1318. else
  1319. req->RemainingBytes = 0;
  1320. iov[0].iov_base = (char *)req;
  1321. /* 4 for rfc1002 length field */
  1322. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1323. return rc;
  1324. }
  1325. static void
  1326. smb2_readv_callback(struct mid_q_entry *mid)
  1327. {
  1328. struct cifs_readdata *rdata = mid->callback_data;
  1329. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1330. struct TCP_Server_Info *server = tcon->ses->server;
  1331. struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1332. unsigned int credits_received = 1;
  1333. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1334. .rq_nvec = 1,
  1335. .rq_pages = rdata->pages,
  1336. .rq_npages = rdata->nr_pages,
  1337. .rq_pagesz = rdata->pagesz,
  1338. .rq_tailsz = rdata->tailsz };
  1339. cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
  1340. __func__, mid->mid, mid->mid_state, rdata->result,
  1341. rdata->bytes);
  1342. switch (mid->mid_state) {
  1343. case MID_RESPONSE_RECEIVED:
  1344. credits_received = le16_to_cpu(buf->CreditRequest);
  1345. /* result already set, check signature */
  1346. if (server->sign) {
  1347. int rc;
  1348. rc = smb2_verify_signature(&rqst, server);
  1349. if (rc)
  1350. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  1351. rc);
  1352. }
  1353. /* FIXME: should this be counted toward the initiating task? */
  1354. task_io_account_read(rdata->bytes);
  1355. cifs_stats_bytes_read(tcon, rdata->bytes);
  1356. break;
  1357. case MID_REQUEST_SUBMITTED:
  1358. case MID_RETRY_NEEDED:
  1359. rdata->result = -EAGAIN;
  1360. break;
  1361. default:
  1362. if (rdata->result != -ENODATA)
  1363. rdata->result = -EIO;
  1364. }
  1365. if (rdata->result)
  1366. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  1367. queue_work(cifsiod_wq, &rdata->work);
  1368. DeleteMidQEntry(mid);
  1369. add_credits(server, credits_received, 0);
  1370. }
  1371. /* smb2_async_readv - send an async write, and set up mid to handle result */
  1372. int
  1373. smb2_async_readv(struct cifs_readdata *rdata)
  1374. {
  1375. int rc;
  1376. struct smb2_hdr *buf;
  1377. struct cifs_io_parms io_parms;
  1378. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1379. .rq_nvec = 1 };
  1380. cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
  1381. __func__, rdata->offset, rdata->bytes);
  1382. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  1383. io_parms.offset = rdata->offset;
  1384. io_parms.length = rdata->bytes;
  1385. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  1386. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  1387. io_parms.pid = rdata->pid;
  1388. rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
  1389. if (rc)
  1390. return rc;
  1391. buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1392. /* 4 for rfc1002 length field */
  1393. rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
  1394. kref_get(&rdata->refcount);
  1395. rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
  1396. cifs_readv_receive, smb2_readv_callback,
  1397. rdata, 0);
  1398. if (rc) {
  1399. kref_put(&rdata->refcount, cifs_readdata_release);
  1400. cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
  1401. }
  1402. cifs_small_buf_release(buf);
  1403. return rc;
  1404. }
  1405. int
  1406. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  1407. unsigned int *nbytes, char **buf, int *buf_type)
  1408. {
  1409. int resp_buftype, rc = -EACCES;
  1410. struct smb2_read_rsp *rsp = NULL;
  1411. struct kvec iov[1];
  1412. *nbytes = 0;
  1413. rc = smb2_new_read_req(iov, io_parms, 0, 0);
  1414. if (rc)
  1415. return rc;
  1416. rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
  1417. &resp_buftype, CIFS_LOG_ERROR);
  1418. rsp = (struct smb2_read_rsp *)iov[0].iov_base;
  1419. if (rsp->hdr.Status == STATUS_END_OF_FILE) {
  1420. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1421. return 0;
  1422. }
  1423. if (rc) {
  1424. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  1425. cifs_dbg(VFS, "Send error in read = %d\n", rc);
  1426. } else {
  1427. *nbytes = le32_to_cpu(rsp->DataLength);
  1428. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  1429. (*nbytes > io_parms->length)) {
  1430. cifs_dbg(FYI, "bad length %d for count %d\n",
  1431. *nbytes, io_parms->length);
  1432. rc = -EIO;
  1433. *nbytes = 0;
  1434. }
  1435. }
  1436. if (*buf) {
  1437. memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
  1438. *nbytes);
  1439. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1440. } else if (resp_buftype != CIFS_NO_BUFFER) {
  1441. *buf = iov[0].iov_base;
  1442. if (resp_buftype == CIFS_SMALL_BUFFER)
  1443. *buf_type = CIFS_SMALL_BUFFER;
  1444. else if (resp_buftype == CIFS_LARGE_BUFFER)
  1445. *buf_type = CIFS_LARGE_BUFFER;
  1446. }
  1447. return rc;
  1448. }
  1449. /*
  1450. * Check the mid_state and signature on received buffer (if any), and queue the
  1451. * workqueue completion task.
  1452. */
  1453. static void
  1454. smb2_writev_callback(struct mid_q_entry *mid)
  1455. {
  1456. struct cifs_writedata *wdata = mid->callback_data;
  1457. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1458. unsigned int written;
  1459. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  1460. unsigned int credits_received = 1;
  1461. switch (mid->mid_state) {
  1462. case MID_RESPONSE_RECEIVED:
  1463. credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
  1464. wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
  1465. if (wdata->result != 0)
  1466. break;
  1467. written = le32_to_cpu(rsp->DataLength);
  1468. /*
  1469. * Mask off high 16 bits when bytes written as returned
  1470. * by the server is greater than bytes requested by the
  1471. * client. OS/2 servers are known to set incorrect
  1472. * CountHigh values.
  1473. */
  1474. if (written > wdata->bytes)
  1475. written &= 0xFFFF;
  1476. if (written < wdata->bytes)
  1477. wdata->result = -ENOSPC;
  1478. else
  1479. wdata->bytes = written;
  1480. break;
  1481. case MID_REQUEST_SUBMITTED:
  1482. case MID_RETRY_NEEDED:
  1483. wdata->result = -EAGAIN;
  1484. break;
  1485. default:
  1486. wdata->result = -EIO;
  1487. break;
  1488. }
  1489. if (wdata->result)
  1490. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1491. queue_work(cifsiod_wq, &wdata->work);
  1492. DeleteMidQEntry(mid);
  1493. add_credits(tcon->ses->server, credits_received, 0);
  1494. }
  1495. /* smb2_async_writev - send an async write, and set up mid to handle result */
  1496. int
  1497. smb2_async_writev(struct cifs_writedata *wdata)
  1498. {
  1499. int rc = -EACCES;
  1500. struct smb2_write_req *req = NULL;
  1501. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1502. struct kvec iov;
  1503. struct smb_rqst rqst;
  1504. rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
  1505. if (rc)
  1506. goto async_writev_out;
  1507. req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
  1508. req->PersistentFileId = wdata->cfile->fid.persistent_fid;
  1509. req->VolatileFileId = wdata->cfile->fid.volatile_fid;
  1510. req->WriteChannelInfoOffset = 0;
  1511. req->WriteChannelInfoLength = 0;
  1512. req->Channel = 0;
  1513. req->Offset = cpu_to_le64(wdata->offset);
  1514. /* 4 for rfc1002 length field */
  1515. req->DataOffset = cpu_to_le16(
  1516. offsetof(struct smb2_write_req, Buffer) - 4);
  1517. req->RemainingBytes = 0;
  1518. /* 4 for rfc1002 length field and 1 for Buffer */
  1519. iov.iov_len = get_rfc1002_length(req) + 4 - 1;
  1520. iov.iov_base = req;
  1521. rqst.rq_iov = &iov;
  1522. rqst.rq_nvec = 1;
  1523. rqst.rq_pages = wdata->pages;
  1524. rqst.rq_npages = wdata->nr_pages;
  1525. rqst.rq_pagesz = wdata->pagesz;
  1526. rqst.rq_tailsz = wdata->tailsz;
  1527. cifs_dbg(FYI, "async write at %llu %u bytes\n",
  1528. wdata->offset, wdata->bytes);
  1529. req->Length = cpu_to_le32(wdata->bytes);
  1530. inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
  1531. kref_get(&wdata->refcount);
  1532. rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
  1533. smb2_writev_callback, wdata, 0);
  1534. if (rc) {
  1535. kref_put(&wdata->refcount, cifs_writedata_release);
  1536. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1537. }
  1538. async_writev_out:
  1539. cifs_small_buf_release(req);
  1540. return rc;
  1541. }
  1542. /*
  1543. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  1544. * The length field from io_parms must be at least 1 and indicates a number of
  1545. * elements with data to write that begins with position 1 in iov array. All
  1546. * data length is specified by count.
  1547. */
  1548. int
  1549. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  1550. unsigned int *nbytes, struct kvec *iov, int n_vec)
  1551. {
  1552. int rc = 0;
  1553. struct smb2_write_req *req = NULL;
  1554. struct smb2_write_rsp *rsp = NULL;
  1555. int resp_buftype;
  1556. *nbytes = 0;
  1557. if (n_vec < 1)
  1558. return rc;
  1559. rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
  1560. if (rc)
  1561. return rc;
  1562. if (io_parms->tcon->ses->server == NULL)
  1563. return -ECONNABORTED;
  1564. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1565. req->PersistentFileId = io_parms->persistent_fid;
  1566. req->VolatileFileId = io_parms->volatile_fid;
  1567. req->WriteChannelInfoOffset = 0;
  1568. req->WriteChannelInfoLength = 0;
  1569. req->Channel = 0;
  1570. req->Length = cpu_to_le32(io_parms->length);
  1571. req->Offset = cpu_to_le64(io_parms->offset);
  1572. /* 4 for rfc1002 length field */
  1573. req->DataOffset = cpu_to_le16(
  1574. offsetof(struct smb2_write_req, Buffer) - 4);
  1575. req->RemainingBytes = 0;
  1576. iov[0].iov_base = (char *)req;
  1577. /* 4 for rfc1002 length field and 1 for Buffer */
  1578. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1579. /* length of entire message including data to be written */
  1580. inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
  1581. rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
  1582. &resp_buftype, 0);
  1583. rsp = (struct smb2_write_rsp *)iov[0].iov_base;
  1584. if (rc) {
  1585. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  1586. cifs_dbg(VFS, "Send error in write = %d\n", rc);
  1587. } else
  1588. *nbytes = le32_to_cpu(rsp->DataLength);
  1589. free_rsp_buf(resp_buftype, rsp);
  1590. return rc;
  1591. }
  1592. static unsigned int
  1593. num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
  1594. {
  1595. int len;
  1596. unsigned int entrycount = 0;
  1597. unsigned int next_offset = 0;
  1598. FILE_DIRECTORY_INFO *entryptr;
  1599. if (bufstart == NULL)
  1600. return 0;
  1601. entryptr = (FILE_DIRECTORY_INFO *)bufstart;
  1602. while (1) {
  1603. entryptr = (FILE_DIRECTORY_INFO *)
  1604. ((char *)entryptr + next_offset);
  1605. if ((char *)entryptr + size > end_of_buf) {
  1606. cifs_dbg(VFS, "malformed search entry would overflow\n");
  1607. break;
  1608. }
  1609. len = le32_to_cpu(entryptr->FileNameLength);
  1610. if ((char *)entryptr + len + size > end_of_buf) {
  1611. cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
  1612. end_of_buf);
  1613. break;
  1614. }
  1615. *lastentry = (char *)entryptr;
  1616. entrycount++;
  1617. next_offset = le32_to_cpu(entryptr->NextEntryOffset);
  1618. if (!next_offset)
  1619. break;
  1620. }
  1621. return entrycount;
  1622. }
  1623. /*
  1624. * Readdir/FindFirst
  1625. */
  1626. int
  1627. SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
  1628. u64 persistent_fid, u64 volatile_fid, int index,
  1629. struct cifs_search_info *srch_inf)
  1630. {
  1631. struct smb2_query_directory_req *req;
  1632. struct smb2_query_directory_rsp *rsp = NULL;
  1633. struct kvec iov[2];
  1634. int rc = 0;
  1635. int len;
  1636. int resp_buftype;
  1637. unsigned char *bufptr;
  1638. struct TCP_Server_Info *server;
  1639. struct cifs_ses *ses = tcon->ses;
  1640. __le16 asteriks = cpu_to_le16('*');
  1641. char *end_of_smb;
  1642. unsigned int output_size = CIFSMaxBufSize;
  1643. size_t info_buf_size;
  1644. if (ses && (ses->server))
  1645. server = ses->server;
  1646. else
  1647. return -EIO;
  1648. rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
  1649. if (rc)
  1650. return rc;
  1651. switch (srch_inf->info_level) {
  1652. case SMB_FIND_FILE_DIRECTORY_INFO:
  1653. req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
  1654. info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
  1655. break;
  1656. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  1657. req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
  1658. info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
  1659. break;
  1660. default:
  1661. cifs_dbg(VFS, "info level %u isn't supported\n",
  1662. srch_inf->info_level);
  1663. rc = -EINVAL;
  1664. goto qdir_exit;
  1665. }
  1666. req->FileIndex = cpu_to_le32(index);
  1667. req->PersistentFileId = persistent_fid;
  1668. req->VolatileFileId = volatile_fid;
  1669. len = 0x2;
  1670. bufptr = req->Buffer;
  1671. memcpy(bufptr, &asteriks, len);
  1672. req->FileNameOffset =
  1673. cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
  1674. req->FileNameLength = cpu_to_le16(len);
  1675. /*
  1676. * BB could be 30 bytes or so longer if we used SMB2 specific
  1677. * buffer lengths, but this is safe and close enough.
  1678. */
  1679. output_size = min_t(unsigned int, output_size, server->maxBuf);
  1680. output_size = min_t(unsigned int, output_size, 2 << 15);
  1681. req->OutputBufferLength = cpu_to_le32(output_size);
  1682. iov[0].iov_base = (char *)req;
  1683. /* 4 for RFC1001 length and 1 for Buffer */
  1684. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1685. iov[1].iov_base = (char *)(req->Buffer);
  1686. iov[1].iov_len = len;
  1687. inc_rfc1001_len(req, len - 1 /* Buffer */);
  1688. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  1689. rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
  1690. if (rc) {
  1691. cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
  1692. goto qdir_exit;
  1693. }
  1694. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1695. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1696. info_buf_size);
  1697. if (rc)
  1698. goto qdir_exit;
  1699. srch_inf->unicode = true;
  1700. if (srch_inf->ntwrk_buf_start) {
  1701. if (srch_inf->smallBuf)
  1702. cifs_small_buf_release(srch_inf->ntwrk_buf_start);
  1703. else
  1704. cifs_buf_release(srch_inf->ntwrk_buf_start);
  1705. }
  1706. srch_inf->ntwrk_buf_start = (char *)rsp;
  1707. srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
  1708. (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
  1709. /* 4 for rfc1002 length field */
  1710. end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
  1711. srch_inf->entries_in_buffer =
  1712. num_entries(srch_inf->srch_entries_start, end_of_smb,
  1713. &srch_inf->last_entry, info_buf_size);
  1714. srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
  1715. cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
  1716. srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
  1717. srch_inf->srch_entries_start, srch_inf->last_entry);
  1718. if (resp_buftype == CIFS_LARGE_BUFFER)
  1719. srch_inf->smallBuf = false;
  1720. else if (resp_buftype == CIFS_SMALL_BUFFER)
  1721. srch_inf->smallBuf = true;
  1722. else
  1723. cifs_dbg(VFS, "illegal search buffer type\n");
  1724. if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
  1725. srch_inf->endOfSearch = 1;
  1726. else
  1727. srch_inf->endOfSearch = 0;
  1728. return rc;
  1729. qdir_exit:
  1730. free_rsp_buf(resp_buftype, rsp);
  1731. return rc;
  1732. }
  1733. static int
  1734. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1735. u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
  1736. unsigned int num, void **data, unsigned int *size)
  1737. {
  1738. struct smb2_set_info_req *req;
  1739. struct smb2_set_info_rsp *rsp = NULL;
  1740. struct kvec *iov;
  1741. int rc = 0;
  1742. int resp_buftype;
  1743. unsigned int i;
  1744. struct TCP_Server_Info *server;
  1745. struct cifs_ses *ses = tcon->ses;
  1746. if (ses && (ses->server))
  1747. server = ses->server;
  1748. else
  1749. return -EIO;
  1750. if (!num)
  1751. return -EINVAL;
  1752. iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
  1753. if (!iov)
  1754. return -ENOMEM;
  1755. rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
  1756. if (rc) {
  1757. kfree(iov);
  1758. return rc;
  1759. }
  1760. req->hdr.ProcessId = cpu_to_le32(pid);
  1761. req->InfoType = SMB2_O_INFO_FILE;
  1762. req->FileInfoClass = info_class;
  1763. req->PersistentFileId = persistent_fid;
  1764. req->VolatileFileId = volatile_fid;
  1765. /* 4 for RFC1001 length and 1 for Buffer */
  1766. req->BufferOffset =
  1767. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
  1768. req->BufferLength = cpu_to_le32(*size);
  1769. inc_rfc1001_len(req, *size - 1 /* Buffer */);
  1770. memcpy(req->Buffer, *data, *size);
  1771. iov[0].iov_base = (char *)req;
  1772. /* 4 for RFC1001 length */
  1773. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1774. for (i = 1; i < num; i++) {
  1775. inc_rfc1001_len(req, size[i]);
  1776. le32_add_cpu(&req->BufferLength, size[i]);
  1777. iov[i].iov_base = (char *)data[i];
  1778. iov[i].iov_len = size[i];
  1779. }
  1780. rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
  1781. rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
  1782. if (rc != 0) {
  1783. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  1784. goto out;
  1785. }
  1786. out:
  1787. free_rsp_buf(resp_buftype, rsp);
  1788. kfree(iov);
  1789. return rc;
  1790. }
  1791. int
  1792. SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
  1793. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1794. {
  1795. struct smb2_file_rename_info info;
  1796. void **data;
  1797. unsigned int size[2];
  1798. int rc;
  1799. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1800. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1801. if (!data)
  1802. return -ENOMEM;
  1803. info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
  1804. /* 0 = fail if target already exists */
  1805. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1806. info.FileNameLength = cpu_to_le32(len);
  1807. data[0] = &info;
  1808. size[0] = sizeof(struct smb2_file_rename_info);
  1809. data[1] = target_file;
  1810. size[1] = len + 2 /* null */;
  1811. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1812. current->tgid, FILE_RENAME_INFORMATION, 2, data,
  1813. size);
  1814. kfree(data);
  1815. return rc;
  1816. }
  1817. int
  1818. SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  1819. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1820. {
  1821. struct smb2_file_link_info info;
  1822. void **data;
  1823. unsigned int size[2];
  1824. int rc;
  1825. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1826. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1827. if (!data)
  1828. return -ENOMEM;
  1829. info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
  1830. /* 0 = fail if link already exists */
  1831. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1832. info.FileNameLength = cpu_to_le32(len);
  1833. data[0] = &info;
  1834. size[0] = sizeof(struct smb2_file_link_info);
  1835. data[1] = target_file;
  1836. size[1] = len + 2 /* null */;
  1837. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1838. current->tgid, FILE_LINK_INFORMATION, 2, data, size);
  1839. kfree(data);
  1840. return rc;
  1841. }
  1842. int
  1843. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1844. u64 volatile_fid, u32 pid, __le64 *eof)
  1845. {
  1846. struct smb2_file_eof_info info;
  1847. void *data;
  1848. unsigned int size;
  1849. info.EndOfFile = *eof;
  1850. data = &info;
  1851. size = sizeof(struct smb2_file_eof_info);
  1852. return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
  1853. FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
  1854. }
  1855. int
  1856. SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1857. u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
  1858. {
  1859. unsigned int size;
  1860. size = sizeof(FILE_BASIC_INFO);
  1861. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1862. current->tgid, FILE_BASIC_INFORMATION, 1,
  1863. (void **)&buf, &size);
  1864. }
  1865. int
  1866. SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
  1867. const u64 persistent_fid, const u64 volatile_fid,
  1868. __u8 oplock_level)
  1869. {
  1870. int rc;
  1871. struct smb2_oplock_break *req = NULL;
  1872. cifs_dbg(FYI, "SMB2_oplock_break\n");
  1873. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  1874. if (rc)
  1875. return rc;
  1876. req->VolatileFid = volatile_fid;
  1877. req->PersistentFid = persistent_fid;
  1878. req->OplockLevel = oplock_level;
  1879. req->hdr.CreditRequest = cpu_to_le16(1);
  1880. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  1881. /* SMB2 buffer freed by function above */
  1882. if (rc) {
  1883. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  1884. cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
  1885. }
  1886. return rc;
  1887. }
  1888. static void
  1889. copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
  1890. struct kstatfs *kst)
  1891. {
  1892. kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
  1893. le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
  1894. kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
  1895. kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
  1896. kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
  1897. return;
  1898. }
  1899. static int
  1900. build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
  1901. int outbuf_len, u64 persistent_fid, u64 volatile_fid)
  1902. {
  1903. int rc;
  1904. struct smb2_query_info_req *req;
  1905. cifs_dbg(FYI, "Query FSInfo level %d\n", level);
  1906. if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
  1907. return -EIO;
  1908. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1909. if (rc)
  1910. return rc;
  1911. req->InfoType = SMB2_O_INFO_FILESYSTEM;
  1912. req->FileInfoClass = level;
  1913. req->PersistentFileId = persistent_fid;
  1914. req->VolatileFileId = volatile_fid;
  1915. /* 4 for rfc1002 length field and 1 for pad */
  1916. req->InputBufferOffset =
  1917. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1918. req->OutputBufferLength = cpu_to_le32(
  1919. outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
  1920. iov->iov_base = (char *)req;
  1921. /* 4 for rfc1002 length field */
  1922. iov->iov_len = get_rfc1002_length(req) + 4;
  1923. return 0;
  1924. }
  1925. int
  1926. SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
  1927. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  1928. {
  1929. struct smb2_query_info_rsp *rsp = NULL;
  1930. struct kvec iov;
  1931. int rc = 0;
  1932. int resp_buftype;
  1933. struct cifs_ses *ses = tcon->ses;
  1934. struct smb2_fs_full_size_info *info = NULL;
  1935. rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
  1936. sizeof(struct smb2_fs_full_size_info),
  1937. persistent_fid, volatile_fid);
  1938. if (rc)
  1939. return rc;
  1940. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  1941. if (rc) {
  1942. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1943. goto qinf_exit;
  1944. }
  1945. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  1946. info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
  1947. le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
  1948. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1949. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1950. sizeof(struct smb2_fs_full_size_info));
  1951. if (!rc)
  1952. copy_fs_info_to_kstatfs(info, fsdata);
  1953. qinf_exit:
  1954. free_rsp_buf(resp_buftype, iov.iov_base);
  1955. return rc;
  1956. }
  1957. int
  1958. smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
  1959. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  1960. const __u32 num_lock, struct smb2_lock_element *buf)
  1961. {
  1962. int rc = 0;
  1963. struct smb2_lock_req *req = NULL;
  1964. struct kvec iov[2];
  1965. int resp_buf_type;
  1966. unsigned int count;
  1967. cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
  1968. rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
  1969. if (rc)
  1970. return rc;
  1971. req->hdr.ProcessId = cpu_to_le32(pid);
  1972. req->LockCount = cpu_to_le16(num_lock);
  1973. req->PersistentFileId = persist_fid;
  1974. req->VolatileFileId = volatile_fid;
  1975. count = num_lock * sizeof(struct smb2_lock_element);
  1976. inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
  1977. iov[0].iov_base = (char *)req;
  1978. /* 4 for rfc1002 length field and count for all locks */
  1979. iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
  1980. iov[1].iov_base = (char *)buf;
  1981. iov[1].iov_len = count;
  1982. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  1983. rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
  1984. if (rc) {
  1985. cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
  1986. cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
  1987. }
  1988. return rc;
  1989. }
  1990. int
  1991. SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
  1992. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  1993. const __u64 length, const __u64 offset, const __u32 lock_flags,
  1994. const bool wait)
  1995. {
  1996. struct smb2_lock_element lock;
  1997. lock.Offset = cpu_to_le64(offset);
  1998. lock.Length = cpu_to_le64(length);
  1999. lock.Flags = cpu_to_le32(lock_flags);
  2000. if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
  2001. lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  2002. return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
  2003. }
  2004. int
  2005. SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
  2006. __u8 *lease_key, const __le32 lease_state)
  2007. {
  2008. int rc;
  2009. struct smb2_lease_ack *req = NULL;
  2010. cifs_dbg(FYI, "SMB2_lease_break\n");
  2011. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  2012. if (rc)
  2013. return rc;
  2014. req->hdr.CreditRequest = cpu_to_le16(1);
  2015. req->StructureSize = cpu_to_le16(36);
  2016. inc_rfc1001_len(req, 12);
  2017. memcpy(req->LeaseKey, lease_key, 16);
  2018. req->LeaseState = lease_state;
  2019. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  2020. /* SMB2 buffer freed by function above */
  2021. if (rc) {
  2022. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  2023. cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
  2024. }
  2025. return rc;
  2026. }