smb2pdu.c 64 KB

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