smb2pdu.c 62 KB

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