smb2pdu.c 61 KB

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