smb2pdu.c 65 KB

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