smb2pdu.c 64 KB

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