smb2ops.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901
  1. /*
  2. * SMB2 version specific operations
  3. *
  4. * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License v2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pagemap.h>
  20. #include <linux/vfs.h>
  21. #include <linux/falloc.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/uuid.h>
  24. #include <crypto/aead.h>
  25. #include "cifsglob.h"
  26. #include "smb2pdu.h"
  27. #include "smb2proto.h"
  28. #include "cifsproto.h"
  29. #include "cifs_debug.h"
  30. #include "cifs_unicode.h"
  31. #include "smb2status.h"
  32. #include "smb2glob.h"
  33. #include "cifs_ioctl.h"
  34. #include "smbdirect.h"
  35. /* Change credits for different ops and return the total number of credits */
  36. static int
  37. change_conf(struct TCP_Server_Info *server)
  38. {
  39. server->credits += server->echo_credits + server->oplock_credits;
  40. server->oplock_credits = server->echo_credits = 0;
  41. switch (server->credits) {
  42. case 0:
  43. return 0;
  44. case 1:
  45. server->echoes = false;
  46. server->oplocks = false;
  47. break;
  48. case 2:
  49. server->echoes = true;
  50. server->oplocks = false;
  51. server->echo_credits = 1;
  52. break;
  53. default:
  54. server->echoes = true;
  55. if (enable_oplocks) {
  56. server->oplocks = true;
  57. server->oplock_credits = 1;
  58. } else
  59. server->oplocks = false;
  60. server->echo_credits = 1;
  61. }
  62. server->credits -= server->echo_credits + server->oplock_credits;
  63. return server->credits + server->echo_credits + server->oplock_credits;
  64. }
  65. static void
  66. smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
  67. const int optype)
  68. {
  69. int *val, rc = -1;
  70. spin_lock(&server->req_lock);
  71. val = server->ops->get_credits_field(server, optype);
  72. *val += add;
  73. if (*val > 65000) {
  74. *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
  75. printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
  76. }
  77. server->in_flight--;
  78. if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
  79. rc = change_conf(server);
  80. /*
  81. * Sometimes server returns 0 credits on oplock break ack - we need to
  82. * rebalance credits in this case.
  83. */
  84. else if (server->in_flight > 0 && server->oplock_credits == 0 &&
  85. server->oplocks) {
  86. if (server->credits > 1) {
  87. server->credits--;
  88. server->oplock_credits++;
  89. }
  90. }
  91. spin_unlock(&server->req_lock);
  92. wake_up(&server->request_q);
  93. if (server->tcpStatus == CifsNeedReconnect)
  94. return;
  95. switch (rc) {
  96. case -1:
  97. /* change_conf hasn't been executed */
  98. break;
  99. case 0:
  100. cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
  101. break;
  102. case 1:
  103. cifs_dbg(VFS, "disabling echoes and oplocks\n");
  104. break;
  105. case 2:
  106. cifs_dbg(FYI, "disabling oplocks\n");
  107. break;
  108. default:
  109. cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
  110. }
  111. }
  112. static void
  113. smb2_set_credits(struct TCP_Server_Info *server, const int val)
  114. {
  115. spin_lock(&server->req_lock);
  116. server->credits = val;
  117. spin_unlock(&server->req_lock);
  118. }
  119. static int *
  120. smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
  121. {
  122. switch (optype) {
  123. case CIFS_ECHO_OP:
  124. return &server->echo_credits;
  125. case CIFS_OBREAK_OP:
  126. return &server->oplock_credits;
  127. default:
  128. return &server->credits;
  129. }
  130. }
  131. static unsigned int
  132. smb2_get_credits(struct mid_q_entry *mid)
  133. {
  134. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
  135. return le16_to_cpu(shdr->CreditRequest);
  136. }
  137. static int
  138. smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
  139. unsigned int *num, unsigned int *credits)
  140. {
  141. int rc = 0;
  142. unsigned int scredits;
  143. spin_lock(&server->req_lock);
  144. while (1) {
  145. if (server->credits <= 0) {
  146. spin_unlock(&server->req_lock);
  147. cifs_num_waiters_inc(server);
  148. rc = wait_event_killable(server->request_q,
  149. has_credits(server, &server->credits));
  150. cifs_num_waiters_dec(server);
  151. if (rc)
  152. return rc;
  153. spin_lock(&server->req_lock);
  154. } else {
  155. if (server->tcpStatus == CifsExiting) {
  156. spin_unlock(&server->req_lock);
  157. return -ENOENT;
  158. }
  159. scredits = server->credits;
  160. /* can deadlock with reopen */
  161. if (scredits <= 8) {
  162. *num = SMB2_MAX_BUFFER_SIZE;
  163. *credits = 0;
  164. break;
  165. }
  166. /* leave some credits for reopen and other ops */
  167. scredits -= 8;
  168. *num = min_t(unsigned int, size,
  169. scredits * SMB2_MAX_BUFFER_SIZE);
  170. *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
  171. server->credits -= *credits;
  172. server->in_flight++;
  173. break;
  174. }
  175. }
  176. spin_unlock(&server->req_lock);
  177. return rc;
  178. }
  179. static __u64
  180. smb2_get_next_mid(struct TCP_Server_Info *server)
  181. {
  182. __u64 mid;
  183. /* for SMB2 we need the current value */
  184. spin_lock(&GlobalMid_Lock);
  185. mid = server->CurrentMid++;
  186. spin_unlock(&GlobalMid_Lock);
  187. return mid;
  188. }
  189. static void
  190. smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
  191. {
  192. spin_lock(&GlobalMid_Lock);
  193. if (server->CurrentMid >= val)
  194. server->CurrentMid -= val;
  195. spin_unlock(&GlobalMid_Lock);
  196. }
  197. static struct mid_q_entry *
  198. smb2_find_mid(struct TCP_Server_Info *server, char *buf)
  199. {
  200. struct mid_q_entry *mid;
  201. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
  202. __u64 wire_mid = le64_to_cpu(shdr->MessageId);
  203. if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
  204. cifs_dbg(VFS, "encrypted frame parsing not supported yet");
  205. return NULL;
  206. }
  207. spin_lock(&GlobalMid_Lock);
  208. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  209. if ((mid->mid == wire_mid) &&
  210. (mid->mid_state == MID_REQUEST_SUBMITTED) &&
  211. (mid->command == shdr->Command)) {
  212. kref_get(&mid->refcount);
  213. spin_unlock(&GlobalMid_Lock);
  214. return mid;
  215. }
  216. }
  217. spin_unlock(&GlobalMid_Lock);
  218. return NULL;
  219. }
  220. static void
  221. smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
  222. {
  223. #ifdef CONFIG_CIFS_DEBUG2
  224. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
  225. cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
  226. shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
  227. shdr->ProcessId);
  228. cifs_dbg(VFS, "smb buf %p len %u\n", buf,
  229. server->ops->calc_smb_size(buf, server));
  230. #endif
  231. }
  232. static bool
  233. smb2_need_neg(struct TCP_Server_Info *server)
  234. {
  235. return server->max_read == 0;
  236. }
  237. static int
  238. smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  239. {
  240. int rc;
  241. ses->server->CurrentMid = 0;
  242. rc = SMB2_negotiate(xid, ses);
  243. /* BB we probably don't need to retry with modern servers */
  244. if (rc == -EAGAIN)
  245. rc = -EHOSTDOWN;
  246. return rc;
  247. }
  248. static unsigned int
  249. smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  250. {
  251. struct TCP_Server_Info *server = tcon->ses->server;
  252. unsigned int wsize;
  253. /* start with specified wsize, or default */
  254. wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
  255. wsize = min_t(unsigned int, wsize, server->max_write);
  256. #ifdef CONFIG_CIFS_SMB_DIRECT
  257. if (server->rdma) {
  258. if (server->sign)
  259. wsize = min_t(unsigned int,
  260. wsize, server->smbd_conn->max_fragmented_send_size);
  261. else
  262. wsize = min_t(unsigned int,
  263. wsize, server->smbd_conn->max_readwrite_size);
  264. }
  265. #endif
  266. if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  267. wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
  268. return wsize;
  269. }
  270. static unsigned int
  271. smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  272. {
  273. struct TCP_Server_Info *server = tcon->ses->server;
  274. unsigned int rsize;
  275. /* start with specified rsize, or default */
  276. rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
  277. rsize = min_t(unsigned int, rsize, server->max_read);
  278. #ifdef CONFIG_CIFS_SMB_DIRECT
  279. if (server->rdma) {
  280. if (server->sign)
  281. rsize = min_t(unsigned int,
  282. rsize, server->smbd_conn->max_fragmented_recv_size);
  283. else
  284. rsize = min_t(unsigned int,
  285. rsize, server->smbd_conn->max_readwrite_size);
  286. }
  287. #endif
  288. if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  289. rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
  290. return rsize;
  291. }
  292. static int
  293. parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
  294. size_t buf_len,
  295. struct cifs_server_iface **iface_list,
  296. size_t *iface_count)
  297. {
  298. struct network_interface_info_ioctl_rsp *p;
  299. struct sockaddr_in *addr4;
  300. struct sockaddr_in6 *addr6;
  301. struct iface_info_ipv4 *p4;
  302. struct iface_info_ipv6 *p6;
  303. struct cifs_server_iface *info;
  304. ssize_t bytes_left;
  305. size_t next = 0;
  306. int nb_iface = 0;
  307. int rc = 0;
  308. *iface_list = NULL;
  309. *iface_count = 0;
  310. /*
  311. * Fist pass: count and sanity check
  312. */
  313. bytes_left = buf_len;
  314. p = buf;
  315. while (bytes_left >= sizeof(*p)) {
  316. nb_iface++;
  317. next = le32_to_cpu(p->Next);
  318. if (!next) {
  319. bytes_left -= sizeof(*p);
  320. break;
  321. }
  322. p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
  323. bytes_left -= next;
  324. }
  325. if (!nb_iface) {
  326. cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
  327. rc = -EINVAL;
  328. goto out;
  329. }
  330. /* Azure rounds the buffer size up 8, to a 16 byte boundary */
  331. if ((bytes_left > 8) || p->Next)
  332. cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
  333. /*
  334. * Second pass: extract info to internal structure
  335. */
  336. *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
  337. if (!*iface_list) {
  338. rc = -ENOMEM;
  339. goto out;
  340. }
  341. info = *iface_list;
  342. bytes_left = buf_len;
  343. p = buf;
  344. while (bytes_left >= sizeof(*p)) {
  345. info->speed = le64_to_cpu(p->LinkSpeed);
  346. info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
  347. info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
  348. cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
  349. cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
  350. cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
  351. le32_to_cpu(p->Capability));
  352. switch (p->Family) {
  353. /*
  354. * The kernel and wire socket structures have the same
  355. * layout and use network byte order but make the
  356. * conversion explicit in case either one changes.
  357. */
  358. case INTERNETWORK:
  359. addr4 = (struct sockaddr_in *)&info->sockaddr;
  360. p4 = (struct iface_info_ipv4 *)p->Buffer;
  361. addr4->sin_family = AF_INET;
  362. memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
  363. /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
  364. addr4->sin_port = cpu_to_be16(CIFS_PORT);
  365. cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
  366. &addr4->sin_addr);
  367. break;
  368. case INTERNETWORKV6:
  369. addr6 = (struct sockaddr_in6 *)&info->sockaddr;
  370. p6 = (struct iface_info_ipv6 *)p->Buffer;
  371. addr6->sin6_family = AF_INET6;
  372. memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
  373. /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
  374. addr6->sin6_flowinfo = 0;
  375. addr6->sin6_scope_id = 0;
  376. addr6->sin6_port = cpu_to_be16(CIFS_PORT);
  377. cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
  378. &addr6->sin6_addr);
  379. break;
  380. default:
  381. cifs_dbg(VFS,
  382. "%s: skipping unsupported socket family\n",
  383. __func__);
  384. goto next_iface;
  385. }
  386. (*iface_count)++;
  387. info++;
  388. next_iface:
  389. next = le32_to_cpu(p->Next);
  390. if (!next)
  391. break;
  392. p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
  393. bytes_left -= next;
  394. }
  395. if (!*iface_count) {
  396. rc = -EINVAL;
  397. goto out;
  398. }
  399. out:
  400. if (rc) {
  401. kfree(*iface_list);
  402. *iface_count = 0;
  403. *iface_list = NULL;
  404. }
  405. return rc;
  406. }
  407. static int
  408. SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
  409. {
  410. int rc;
  411. unsigned int ret_data_len = 0;
  412. struct network_interface_info_ioctl_rsp *out_buf = NULL;
  413. struct cifs_server_iface *iface_list;
  414. size_t iface_count;
  415. struct cifs_ses *ses = tcon->ses;
  416. rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
  417. FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
  418. NULL /* no data input */, 0 /* no data input */,
  419. (char **)&out_buf, &ret_data_len);
  420. if (rc == -EOPNOTSUPP) {
  421. cifs_dbg(FYI,
  422. "server does not support query network interfaces\n");
  423. goto out;
  424. } else if (rc != 0) {
  425. cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
  426. goto out;
  427. }
  428. rc = parse_server_interfaces(out_buf, ret_data_len,
  429. &iface_list, &iface_count);
  430. if (rc)
  431. goto out;
  432. spin_lock(&ses->iface_lock);
  433. kfree(ses->iface_list);
  434. ses->iface_list = iface_list;
  435. ses->iface_count = iface_count;
  436. ses->iface_last_update = jiffies;
  437. spin_unlock(&ses->iface_lock);
  438. out:
  439. kfree(out_buf);
  440. return rc;
  441. }
  442. static void
  443. smb2_close_cached_fid(struct kref *ref)
  444. {
  445. struct cached_fid *cfid = container_of(ref, struct cached_fid,
  446. refcount);
  447. if (cfid->is_valid) {
  448. cifs_dbg(FYI, "clear cached root file handle\n");
  449. SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
  450. cfid->fid->volatile_fid);
  451. cfid->is_valid = false;
  452. }
  453. }
  454. void close_shroot(struct cached_fid *cfid)
  455. {
  456. mutex_lock(&cfid->fid_mutex);
  457. kref_put(&cfid->refcount, smb2_close_cached_fid);
  458. mutex_unlock(&cfid->fid_mutex);
  459. }
  460. void
  461. smb2_cached_lease_break(struct work_struct *work)
  462. {
  463. struct cached_fid *cfid = container_of(work,
  464. struct cached_fid, lease_break);
  465. close_shroot(cfid);
  466. }
  467. /*
  468. * Open the directory at the root of a share
  469. */
  470. int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
  471. {
  472. struct cifs_open_parms oparams;
  473. int rc;
  474. __le16 srch_path = 0; /* Null - since an open of top of share */
  475. u8 oplock = SMB2_OPLOCK_LEVEL_II;
  476. mutex_lock(&tcon->crfid.fid_mutex);
  477. if (tcon->crfid.is_valid) {
  478. cifs_dbg(FYI, "found a cached root file handle\n");
  479. memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
  480. kref_get(&tcon->crfid.refcount);
  481. mutex_unlock(&tcon->crfid.fid_mutex);
  482. return 0;
  483. }
  484. oparams.tcon = tcon;
  485. oparams.create_options = 0;
  486. oparams.desired_access = FILE_READ_ATTRIBUTES;
  487. oparams.disposition = FILE_OPEN;
  488. oparams.fid = pfid;
  489. oparams.reconnect = false;
  490. /*
  491. * We do not hold the lock for the open because in case
  492. * SMB2_open needs to reconnect, it will end up calling
  493. * cifs_mark_open_files_invalid() which takes the lock again
  494. * thus causing a deadlock
  495. */
  496. mutex_unlock(&tcon->crfid.fid_mutex);
  497. rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL, NULL);
  498. mutex_lock(&tcon->crfid.fid_mutex);
  499. /*
  500. * Now we need to check again as the cached root might have
  501. * been successfully re-opened from a concurrent process
  502. */
  503. if (tcon->crfid.is_valid) {
  504. /* work was already done */
  505. /* stash fids for close() later */
  506. struct cifs_fid fid = {
  507. .persistent_fid = pfid->persistent_fid,
  508. .volatile_fid = pfid->volatile_fid,
  509. };
  510. /*
  511. * Caller expects this func to set pfid to a valid
  512. * cached root, so we copy the existing one and get a
  513. * reference
  514. */
  515. memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
  516. kref_get(&tcon->crfid.refcount);
  517. mutex_unlock(&tcon->crfid.fid_mutex);
  518. if (rc == 0) {
  519. /* close extra handle outside of critical section */
  520. SMB2_close(xid, tcon, fid.persistent_fid,
  521. fid.volatile_fid);
  522. }
  523. return 0;
  524. }
  525. /* Cached root is still invalid, continue normaly */
  526. if (rc == 0) {
  527. memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
  528. tcon->crfid.tcon = tcon;
  529. tcon->crfid.is_valid = true;
  530. kref_init(&tcon->crfid.refcount);
  531. kref_get(&tcon->crfid.refcount);
  532. }
  533. mutex_unlock(&tcon->crfid.fid_mutex);
  534. return rc;
  535. }
  536. static void
  537. smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
  538. {
  539. int rc;
  540. __le16 srch_path = 0; /* Null - open root of share */
  541. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  542. struct cifs_open_parms oparms;
  543. struct cifs_fid fid;
  544. bool no_cached_open = tcon->nohandlecache;
  545. oparms.tcon = tcon;
  546. oparms.desired_access = FILE_READ_ATTRIBUTES;
  547. oparms.disposition = FILE_OPEN;
  548. oparms.create_options = 0;
  549. oparms.fid = &fid;
  550. oparms.reconnect = false;
  551. if (no_cached_open)
  552. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
  553. NULL);
  554. else
  555. rc = open_shroot(xid, tcon, &fid);
  556. if (rc)
  557. return;
  558. SMB3_request_interfaces(xid, tcon);
  559. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  560. FS_ATTRIBUTE_INFORMATION);
  561. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  562. FS_DEVICE_INFORMATION);
  563. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  564. FS_VOLUME_INFORMATION);
  565. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  566. FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
  567. if (no_cached_open)
  568. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  569. else
  570. close_shroot(&tcon->crfid);
  571. return;
  572. }
  573. static void
  574. smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
  575. {
  576. int rc;
  577. __le16 srch_path = 0; /* Null - open root of share */
  578. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  579. struct cifs_open_parms oparms;
  580. struct cifs_fid fid;
  581. oparms.tcon = tcon;
  582. oparms.desired_access = FILE_READ_ATTRIBUTES;
  583. oparms.disposition = FILE_OPEN;
  584. oparms.create_options = 0;
  585. oparms.fid = &fid;
  586. oparms.reconnect = false;
  587. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
  588. if (rc)
  589. return;
  590. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  591. FS_ATTRIBUTE_INFORMATION);
  592. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  593. FS_DEVICE_INFORMATION);
  594. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  595. return;
  596. }
  597. static int
  598. smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  599. struct cifs_sb_info *cifs_sb, const char *full_path)
  600. {
  601. int rc;
  602. __le16 *utf16_path;
  603. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  604. struct cifs_open_parms oparms;
  605. struct cifs_fid fid;
  606. if ((*full_path == 0) && tcon->crfid.is_valid)
  607. return 0;
  608. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  609. if (!utf16_path)
  610. return -ENOMEM;
  611. oparms.tcon = tcon;
  612. oparms.desired_access = FILE_READ_ATTRIBUTES;
  613. oparms.disposition = FILE_OPEN;
  614. if (backup_cred(cifs_sb))
  615. oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
  616. else
  617. oparms.create_options = 0;
  618. oparms.fid = &fid;
  619. oparms.reconnect = false;
  620. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
  621. if (rc) {
  622. kfree(utf16_path);
  623. return rc;
  624. }
  625. rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  626. kfree(utf16_path);
  627. return rc;
  628. }
  629. static int
  630. smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  631. struct cifs_sb_info *cifs_sb, const char *full_path,
  632. u64 *uniqueid, FILE_ALL_INFO *data)
  633. {
  634. *uniqueid = le64_to_cpu(data->IndexNumber);
  635. return 0;
  636. }
  637. static int
  638. smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  639. struct cifs_fid *fid, FILE_ALL_INFO *data)
  640. {
  641. int rc;
  642. struct smb2_file_all_info *smb2_data;
  643. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
  644. GFP_KERNEL);
  645. if (smb2_data == NULL)
  646. return -ENOMEM;
  647. rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
  648. smb2_data);
  649. if (!rc)
  650. move_smb2_info_to_cifs(data, smb2_data);
  651. kfree(smb2_data);
  652. return rc;
  653. }
  654. #ifdef CONFIG_CIFS_XATTR
  655. static ssize_t
  656. move_smb2_ea_to_cifs(char *dst, size_t dst_size,
  657. struct smb2_file_full_ea_info *src, size_t src_size,
  658. const unsigned char *ea_name)
  659. {
  660. int rc = 0;
  661. unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
  662. char *name, *value;
  663. size_t buf_size = dst_size;
  664. size_t name_len, value_len, user_name_len;
  665. while (src_size > 0) {
  666. name = &src->ea_data[0];
  667. name_len = (size_t)src->ea_name_length;
  668. value = &src->ea_data[src->ea_name_length + 1];
  669. value_len = (size_t)le16_to_cpu(src->ea_value_length);
  670. if (name_len == 0) {
  671. break;
  672. }
  673. if (src_size < 8 + name_len + 1 + value_len) {
  674. cifs_dbg(FYI, "EA entry goes beyond length of list\n");
  675. rc = -EIO;
  676. goto out;
  677. }
  678. if (ea_name) {
  679. if (ea_name_len == name_len &&
  680. memcmp(ea_name, name, name_len) == 0) {
  681. rc = value_len;
  682. if (dst_size == 0)
  683. goto out;
  684. if (dst_size < value_len) {
  685. rc = -ERANGE;
  686. goto out;
  687. }
  688. memcpy(dst, value, value_len);
  689. goto out;
  690. }
  691. } else {
  692. /* 'user.' plus a terminating null */
  693. user_name_len = 5 + 1 + name_len;
  694. if (buf_size == 0) {
  695. /* skip copy - calc size only */
  696. rc += user_name_len;
  697. } else if (dst_size >= user_name_len) {
  698. dst_size -= user_name_len;
  699. memcpy(dst, "user.", 5);
  700. dst += 5;
  701. memcpy(dst, src->ea_data, name_len);
  702. dst += name_len;
  703. *dst = 0;
  704. ++dst;
  705. rc += user_name_len;
  706. } else {
  707. /* stop before overrun buffer */
  708. rc = -ERANGE;
  709. break;
  710. }
  711. }
  712. if (!src->next_entry_offset)
  713. break;
  714. if (src_size < le32_to_cpu(src->next_entry_offset)) {
  715. /* stop before overrun buffer */
  716. rc = -ERANGE;
  717. break;
  718. }
  719. src_size -= le32_to_cpu(src->next_entry_offset);
  720. src = (void *)((char *)src +
  721. le32_to_cpu(src->next_entry_offset));
  722. }
  723. /* didn't find the named attribute */
  724. if (ea_name)
  725. rc = -ENODATA;
  726. out:
  727. return (ssize_t)rc;
  728. }
  729. static ssize_t
  730. smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
  731. const unsigned char *path, const unsigned char *ea_name,
  732. char *ea_data, size_t buf_size,
  733. struct cifs_sb_info *cifs_sb)
  734. {
  735. int rc;
  736. __le16 *utf16_path;
  737. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  738. struct cifs_open_parms oparms;
  739. struct cifs_fid fid;
  740. struct smb2_file_full_ea_info *smb2_data;
  741. int ea_buf_size = SMB2_MIN_EA_BUF;
  742. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  743. if (!utf16_path)
  744. return -ENOMEM;
  745. oparms.tcon = tcon;
  746. oparms.desired_access = FILE_READ_EA;
  747. oparms.disposition = FILE_OPEN;
  748. if (backup_cred(cifs_sb))
  749. oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
  750. else
  751. oparms.create_options = 0;
  752. oparms.fid = &fid;
  753. oparms.reconnect = false;
  754. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
  755. kfree(utf16_path);
  756. if (rc) {
  757. cifs_dbg(FYI, "open failed rc=%d\n", rc);
  758. return rc;
  759. }
  760. while (1) {
  761. smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
  762. if (smb2_data == NULL) {
  763. SMB2_close(xid, tcon, fid.persistent_fid,
  764. fid.volatile_fid);
  765. return -ENOMEM;
  766. }
  767. rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
  768. fid.volatile_fid,
  769. ea_buf_size, smb2_data);
  770. if (rc != -E2BIG)
  771. break;
  772. kfree(smb2_data);
  773. ea_buf_size <<= 1;
  774. if (ea_buf_size > SMB2_MAX_EA_BUF) {
  775. cifs_dbg(VFS, "EA size is too large\n");
  776. SMB2_close(xid, tcon, fid.persistent_fid,
  777. fid.volatile_fid);
  778. return -ENOMEM;
  779. }
  780. }
  781. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  782. /*
  783. * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
  784. * not an error. Otherwise, the specified ea_name was not found.
  785. */
  786. if (!rc)
  787. rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
  788. SMB2_MAX_EA_BUF, ea_name);
  789. else if (!ea_name && rc == -ENODATA)
  790. rc = 0;
  791. kfree(smb2_data);
  792. return rc;
  793. }
  794. static int
  795. smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
  796. const char *path, const char *ea_name, const void *ea_value,
  797. const __u16 ea_value_len, const struct nls_table *nls_codepage,
  798. struct cifs_sb_info *cifs_sb)
  799. {
  800. int rc;
  801. __le16 *utf16_path;
  802. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  803. struct cifs_open_parms oparms;
  804. struct cifs_fid fid;
  805. struct smb2_file_full_ea_info *ea;
  806. int ea_name_len = strlen(ea_name);
  807. int len;
  808. if (ea_name_len > 255)
  809. return -EINVAL;
  810. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  811. if (!utf16_path)
  812. return -ENOMEM;
  813. oparms.tcon = tcon;
  814. oparms.desired_access = FILE_WRITE_EA;
  815. oparms.disposition = FILE_OPEN;
  816. if (backup_cred(cifs_sb))
  817. oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
  818. else
  819. oparms.create_options = 0;
  820. oparms.fid = &fid;
  821. oparms.reconnect = false;
  822. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
  823. kfree(utf16_path);
  824. if (rc) {
  825. cifs_dbg(FYI, "open failed rc=%d\n", rc);
  826. return rc;
  827. }
  828. len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
  829. ea = kzalloc(len, GFP_KERNEL);
  830. if (ea == NULL) {
  831. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  832. return -ENOMEM;
  833. }
  834. ea->ea_name_length = ea_name_len;
  835. ea->ea_value_length = cpu_to_le16(ea_value_len);
  836. memcpy(ea->ea_data, ea_name, ea_name_len + 1);
  837. memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
  838. rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
  839. len);
  840. kfree(ea);
  841. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  842. return rc;
  843. }
  844. #endif
  845. static bool
  846. smb2_can_echo(struct TCP_Server_Info *server)
  847. {
  848. return server->echoes;
  849. }
  850. static void
  851. smb2_clear_stats(struct cifs_tcon *tcon)
  852. {
  853. int i;
  854. for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
  855. atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
  856. atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
  857. }
  858. }
  859. static void
  860. smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
  861. {
  862. seq_puts(m, "\n\tShare Capabilities:");
  863. if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
  864. seq_puts(m, " DFS,");
  865. if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  866. seq_puts(m, " CONTINUOUS AVAILABILITY,");
  867. if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
  868. seq_puts(m, " SCALEOUT,");
  869. if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
  870. seq_puts(m, " CLUSTER,");
  871. if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
  872. seq_puts(m, " ASYMMETRIC,");
  873. if (tcon->capabilities == 0)
  874. seq_puts(m, " None");
  875. if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
  876. seq_puts(m, " Aligned,");
  877. if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
  878. seq_puts(m, " Partition Aligned,");
  879. if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
  880. seq_puts(m, " SSD,");
  881. if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
  882. seq_puts(m, " TRIM-support,");
  883. seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
  884. seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
  885. if (tcon->perf_sector_size)
  886. seq_printf(m, "\tOptimal sector size: 0x%x",
  887. tcon->perf_sector_size);
  888. seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
  889. }
  890. static void
  891. smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  892. {
  893. atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
  894. atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
  895. /*
  896. * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
  897. * totals (requests sent) since those SMBs are per-session not per tcon
  898. */
  899. seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
  900. (long long)(tcon->bytes_read),
  901. (long long)(tcon->bytes_written));
  902. seq_printf(m, "\nTreeConnects: %d total %d failed",
  903. atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
  904. atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
  905. seq_printf(m, "\nTreeDisconnects: %d total %d failed",
  906. atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
  907. atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
  908. seq_printf(m, "\nCreates: %d total %d failed",
  909. atomic_read(&sent[SMB2_CREATE_HE]),
  910. atomic_read(&failed[SMB2_CREATE_HE]));
  911. seq_printf(m, "\nCloses: %d total %d failed",
  912. atomic_read(&sent[SMB2_CLOSE_HE]),
  913. atomic_read(&failed[SMB2_CLOSE_HE]));
  914. seq_printf(m, "\nFlushes: %d total %d failed",
  915. atomic_read(&sent[SMB2_FLUSH_HE]),
  916. atomic_read(&failed[SMB2_FLUSH_HE]));
  917. seq_printf(m, "\nReads: %d total %d failed",
  918. atomic_read(&sent[SMB2_READ_HE]),
  919. atomic_read(&failed[SMB2_READ_HE]));
  920. seq_printf(m, "\nWrites: %d total %d failed",
  921. atomic_read(&sent[SMB2_WRITE_HE]),
  922. atomic_read(&failed[SMB2_WRITE_HE]));
  923. seq_printf(m, "\nLocks: %d total %d failed",
  924. atomic_read(&sent[SMB2_LOCK_HE]),
  925. atomic_read(&failed[SMB2_LOCK_HE]));
  926. seq_printf(m, "\nIOCTLs: %d total %d failed",
  927. atomic_read(&sent[SMB2_IOCTL_HE]),
  928. atomic_read(&failed[SMB2_IOCTL_HE]));
  929. seq_printf(m, "\nQueryDirectories: %d total %d failed",
  930. atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
  931. atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
  932. seq_printf(m, "\nChangeNotifies: %d total %d failed",
  933. atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
  934. atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
  935. seq_printf(m, "\nQueryInfos: %d total %d failed",
  936. atomic_read(&sent[SMB2_QUERY_INFO_HE]),
  937. atomic_read(&failed[SMB2_QUERY_INFO_HE]));
  938. seq_printf(m, "\nSetInfos: %d total %d failed",
  939. atomic_read(&sent[SMB2_SET_INFO_HE]),
  940. atomic_read(&failed[SMB2_SET_INFO_HE]));
  941. seq_printf(m, "\nOplockBreaks: %d sent %d failed",
  942. atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
  943. atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
  944. }
  945. static void
  946. smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  947. {
  948. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  949. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  950. cfile->fid.persistent_fid = fid->persistent_fid;
  951. cfile->fid.volatile_fid = fid->volatile_fid;
  952. server->ops->set_oplock_level(cinode, oplock, fid->epoch,
  953. &fid->purge_cache);
  954. cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
  955. memcpy(cfile->fid.create_guid, fid->create_guid, 16);
  956. }
  957. static void
  958. smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  959. struct cifs_fid *fid)
  960. {
  961. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  962. }
  963. static int
  964. SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
  965. u64 persistent_fid, u64 volatile_fid,
  966. struct copychunk_ioctl *pcchunk)
  967. {
  968. int rc;
  969. unsigned int ret_data_len;
  970. struct resume_key_req *res_key;
  971. rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
  972. FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
  973. NULL, 0 /* no input */,
  974. (char **)&res_key, &ret_data_len);
  975. if (rc) {
  976. cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
  977. goto req_res_key_exit;
  978. }
  979. if (ret_data_len < sizeof(struct resume_key_req)) {
  980. cifs_dbg(VFS, "Invalid refcopy resume key length\n");
  981. rc = -EINVAL;
  982. goto req_res_key_exit;
  983. }
  984. memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
  985. req_res_key_exit:
  986. kfree(res_key);
  987. return rc;
  988. }
  989. static ssize_t
  990. smb2_copychunk_range(const unsigned int xid,
  991. struct cifsFileInfo *srcfile,
  992. struct cifsFileInfo *trgtfile, u64 src_off,
  993. u64 len, u64 dest_off)
  994. {
  995. int rc;
  996. unsigned int ret_data_len;
  997. struct copychunk_ioctl *pcchunk;
  998. struct copychunk_ioctl_rsp *retbuf = NULL;
  999. struct cifs_tcon *tcon;
  1000. int chunks_copied = 0;
  1001. bool chunk_sizes_updated = false;
  1002. ssize_t bytes_written, total_bytes_written = 0;
  1003. pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
  1004. if (pcchunk == NULL)
  1005. return -ENOMEM;
  1006. cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
  1007. /* Request a key from the server to identify the source of the copy */
  1008. rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
  1009. srcfile->fid.persistent_fid,
  1010. srcfile->fid.volatile_fid, pcchunk);
  1011. /* Note: request_res_key sets res_key null only if rc !=0 */
  1012. if (rc)
  1013. goto cchunk_out;
  1014. /* For now array only one chunk long, will make more flexible later */
  1015. pcchunk->ChunkCount = cpu_to_le32(1);
  1016. pcchunk->Reserved = 0;
  1017. pcchunk->Reserved2 = 0;
  1018. tcon = tlink_tcon(trgtfile->tlink);
  1019. while (len > 0) {
  1020. pcchunk->SourceOffset = cpu_to_le64(src_off);
  1021. pcchunk->TargetOffset = cpu_to_le64(dest_off);
  1022. pcchunk->Length =
  1023. cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
  1024. /* Request server copy to target from src identified by key */
  1025. kfree(retbuf);
  1026. retbuf = NULL;
  1027. rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
  1028. trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
  1029. true /* is_fsctl */, (char *)pcchunk,
  1030. sizeof(struct copychunk_ioctl), (char **)&retbuf,
  1031. &ret_data_len);
  1032. if (rc == 0) {
  1033. if (ret_data_len !=
  1034. sizeof(struct copychunk_ioctl_rsp)) {
  1035. cifs_dbg(VFS, "invalid cchunk response size\n");
  1036. rc = -EIO;
  1037. goto cchunk_out;
  1038. }
  1039. if (retbuf->TotalBytesWritten == 0) {
  1040. cifs_dbg(FYI, "no bytes copied\n");
  1041. rc = -EIO;
  1042. goto cchunk_out;
  1043. }
  1044. /*
  1045. * Check if server claimed to write more than we asked
  1046. */
  1047. if (le32_to_cpu(retbuf->TotalBytesWritten) >
  1048. le32_to_cpu(pcchunk->Length)) {
  1049. cifs_dbg(VFS, "invalid copy chunk response\n");
  1050. rc = -EIO;
  1051. goto cchunk_out;
  1052. }
  1053. if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
  1054. cifs_dbg(VFS, "invalid num chunks written\n");
  1055. rc = -EIO;
  1056. goto cchunk_out;
  1057. }
  1058. chunks_copied++;
  1059. bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
  1060. src_off += bytes_written;
  1061. dest_off += bytes_written;
  1062. len -= bytes_written;
  1063. total_bytes_written += bytes_written;
  1064. cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
  1065. le32_to_cpu(retbuf->ChunksWritten),
  1066. le32_to_cpu(retbuf->ChunkBytesWritten),
  1067. bytes_written);
  1068. } else if (rc == -EINVAL) {
  1069. if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
  1070. goto cchunk_out;
  1071. cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
  1072. le32_to_cpu(retbuf->ChunksWritten),
  1073. le32_to_cpu(retbuf->ChunkBytesWritten),
  1074. le32_to_cpu(retbuf->TotalBytesWritten));
  1075. /*
  1076. * Check if this is the first request using these sizes,
  1077. * (ie check if copy succeed once with original sizes
  1078. * and check if the server gave us different sizes after
  1079. * we already updated max sizes on previous request).
  1080. * if not then why is the server returning an error now
  1081. */
  1082. if ((chunks_copied != 0) || chunk_sizes_updated)
  1083. goto cchunk_out;
  1084. /* Check that server is not asking us to grow size */
  1085. if (le32_to_cpu(retbuf->ChunkBytesWritten) <
  1086. tcon->max_bytes_chunk)
  1087. tcon->max_bytes_chunk =
  1088. le32_to_cpu(retbuf->ChunkBytesWritten);
  1089. else
  1090. goto cchunk_out; /* server gave us bogus size */
  1091. /* No need to change MaxChunks since already set to 1 */
  1092. chunk_sizes_updated = true;
  1093. } else
  1094. goto cchunk_out;
  1095. }
  1096. cchunk_out:
  1097. kfree(pcchunk);
  1098. kfree(retbuf);
  1099. if (rc)
  1100. return rc;
  1101. else
  1102. return total_bytes_written;
  1103. }
  1104. static int
  1105. smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  1106. struct cifs_fid *fid)
  1107. {
  1108. return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  1109. }
  1110. static unsigned int
  1111. smb2_read_data_offset(char *buf)
  1112. {
  1113. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  1114. return rsp->DataOffset;
  1115. }
  1116. static unsigned int
  1117. smb2_read_data_length(char *buf, bool in_remaining)
  1118. {
  1119. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  1120. if (in_remaining)
  1121. return le32_to_cpu(rsp->DataRemaining);
  1122. return le32_to_cpu(rsp->DataLength);
  1123. }
  1124. static int
  1125. smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
  1126. struct cifs_io_parms *parms, unsigned int *bytes_read,
  1127. char **buf, int *buf_type)
  1128. {
  1129. parms->persistent_fid = pfid->persistent_fid;
  1130. parms->volatile_fid = pfid->volatile_fid;
  1131. return SMB2_read(xid, parms, bytes_read, buf, buf_type);
  1132. }
  1133. static int
  1134. smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
  1135. struct cifs_io_parms *parms, unsigned int *written,
  1136. struct kvec *iov, unsigned long nr_segs)
  1137. {
  1138. parms->persistent_fid = pfid->persistent_fid;
  1139. parms->volatile_fid = pfid->volatile_fid;
  1140. return SMB2_write(xid, parms, written, iov, nr_segs);
  1141. }
  1142. /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
  1143. static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
  1144. struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
  1145. {
  1146. struct cifsInodeInfo *cifsi;
  1147. int rc;
  1148. cifsi = CIFS_I(inode);
  1149. /* if file already sparse don't bother setting sparse again */
  1150. if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
  1151. return true; /* already sparse */
  1152. if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
  1153. return true; /* already not sparse */
  1154. /*
  1155. * Can't check for sparse support on share the usual way via the
  1156. * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
  1157. * since Samba server doesn't set the flag on the share, yet
  1158. * supports the set sparse FSCTL and returns sparse correctly
  1159. * in the file attributes. If we fail setting sparse though we
  1160. * mark that server does not support sparse files for this share
  1161. * to avoid repeatedly sending the unsupported fsctl to server
  1162. * if the file is repeatedly extended.
  1163. */
  1164. if (tcon->broken_sparse_sup)
  1165. return false;
  1166. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1167. cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
  1168. true /* is_fctl */,
  1169. &setsparse, 1, NULL, NULL);
  1170. if (rc) {
  1171. tcon->broken_sparse_sup = true;
  1172. cifs_dbg(FYI, "set sparse rc = %d\n", rc);
  1173. return false;
  1174. }
  1175. if (setsparse)
  1176. cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
  1177. else
  1178. cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
  1179. return true;
  1180. }
  1181. static int
  1182. smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
  1183. struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
  1184. {
  1185. __le64 eof = cpu_to_le64(size);
  1186. struct inode *inode;
  1187. /*
  1188. * If extending file more than one page make sparse. Many Linux fs
  1189. * make files sparse by default when extending via ftruncate
  1190. */
  1191. inode = d_inode(cfile->dentry);
  1192. if (!set_alloc && (size > inode->i_size + 8192)) {
  1193. __u8 set_sparse = 1;
  1194. /* whether set sparse succeeds or not, extend the file */
  1195. smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
  1196. }
  1197. return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  1198. cfile->fid.volatile_fid, cfile->pid, &eof, false);
  1199. }
  1200. static int
  1201. smb2_duplicate_extents(const unsigned int xid,
  1202. struct cifsFileInfo *srcfile,
  1203. struct cifsFileInfo *trgtfile, u64 src_off,
  1204. u64 len, u64 dest_off)
  1205. {
  1206. int rc;
  1207. unsigned int ret_data_len;
  1208. struct duplicate_extents_to_file dup_ext_buf;
  1209. struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
  1210. /* server fileays advertise duplicate extent support with this flag */
  1211. if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
  1212. FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
  1213. return -EOPNOTSUPP;
  1214. dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
  1215. dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
  1216. dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
  1217. dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
  1218. dup_ext_buf.ByteCount = cpu_to_le64(len);
  1219. cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
  1220. src_off, dest_off, len);
  1221. rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
  1222. if (rc)
  1223. goto duplicate_extents_out;
  1224. rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
  1225. trgtfile->fid.volatile_fid,
  1226. FSCTL_DUPLICATE_EXTENTS_TO_FILE,
  1227. true /* is_fsctl */,
  1228. (char *)&dup_ext_buf,
  1229. sizeof(struct duplicate_extents_to_file),
  1230. NULL,
  1231. &ret_data_len);
  1232. if (ret_data_len > 0)
  1233. cifs_dbg(FYI, "non-zero response length in duplicate extents");
  1234. duplicate_extents_out:
  1235. return rc;
  1236. }
  1237. static int
  1238. smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  1239. struct cifsFileInfo *cfile)
  1240. {
  1241. return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
  1242. cfile->fid.volatile_fid);
  1243. }
  1244. static int
  1245. smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
  1246. struct cifsFileInfo *cfile)
  1247. {
  1248. struct fsctl_set_integrity_information_req integr_info;
  1249. unsigned int ret_data_len;
  1250. integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
  1251. integr_info.Flags = 0;
  1252. integr_info.Reserved = 0;
  1253. return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1254. cfile->fid.volatile_fid,
  1255. FSCTL_SET_INTEGRITY_INFORMATION,
  1256. true /* is_fsctl */,
  1257. (char *)&integr_info,
  1258. sizeof(struct fsctl_set_integrity_information_req),
  1259. NULL,
  1260. &ret_data_len);
  1261. }
  1262. /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
  1263. #define GMT_TOKEN_SIZE 50
  1264. /*
  1265. * Input buffer contains (empty) struct smb_snapshot array with size filled in
  1266. * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
  1267. */
  1268. static int
  1269. smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
  1270. struct cifsFileInfo *cfile, void __user *ioc_buf)
  1271. {
  1272. char *retbuf = NULL;
  1273. unsigned int ret_data_len = 0;
  1274. int rc;
  1275. struct smb_snapshot_array snapshot_in;
  1276. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1277. cfile->fid.volatile_fid,
  1278. FSCTL_SRV_ENUMERATE_SNAPSHOTS,
  1279. true /* is_fsctl */,
  1280. NULL, 0 /* no input data */,
  1281. (char **)&retbuf,
  1282. &ret_data_len);
  1283. cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
  1284. rc, ret_data_len);
  1285. if (rc)
  1286. return rc;
  1287. if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
  1288. /* Fixup buffer */
  1289. if (copy_from_user(&snapshot_in, ioc_buf,
  1290. sizeof(struct smb_snapshot_array))) {
  1291. rc = -EFAULT;
  1292. kfree(retbuf);
  1293. return rc;
  1294. }
  1295. /*
  1296. * Check for min size, ie not large enough to fit even one GMT
  1297. * token (snapshot). On the first ioctl some users may pass in
  1298. * smaller size (or zero) to simply get the size of the array
  1299. * so the user space caller can allocate sufficient memory
  1300. * and retry the ioctl again with larger array size sufficient
  1301. * to hold all of the snapshot GMT tokens on the second try.
  1302. */
  1303. if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
  1304. ret_data_len = sizeof(struct smb_snapshot_array);
  1305. /*
  1306. * We return struct SRV_SNAPSHOT_ARRAY, followed by
  1307. * the snapshot array (of 50 byte GMT tokens) each
  1308. * representing an available previous version of the data
  1309. */
  1310. if (ret_data_len > (snapshot_in.snapshot_array_size +
  1311. sizeof(struct smb_snapshot_array)))
  1312. ret_data_len = snapshot_in.snapshot_array_size +
  1313. sizeof(struct smb_snapshot_array);
  1314. if (copy_to_user(ioc_buf, retbuf, ret_data_len))
  1315. rc = -EFAULT;
  1316. }
  1317. kfree(retbuf);
  1318. return rc;
  1319. }
  1320. static int
  1321. smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  1322. const char *path, struct cifs_sb_info *cifs_sb,
  1323. struct cifs_fid *fid, __u16 search_flags,
  1324. struct cifs_search_info *srch_inf)
  1325. {
  1326. __le16 *utf16_path;
  1327. int rc;
  1328. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  1329. struct cifs_open_parms oparms;
  1330. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  1331. if (!utf16_path)
  1332. return -ENOMEM;
  1333. oparms.tcon = tcon;
  1334. oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
  1335. oparms.disposition = FILE_OPEN;
  1336. if (backup_cred(cifs_sb))
  1337. oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
  1338. else
  1339. oparms.create_options = 0;
  1340. oparms.fid = fid;
  1341. oparms.reconnect = false;
  1342. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
  1343. kfree(utf16_path);
  1344. if (rc) {
  1345. cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
  1346. return rc;
  1347. }
  1348. srch_inf->entries_in_buffer = 0;
  1349. srch_inf->index_of_last_entry = 2;
  1350. rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
  1351. fid->volatile_fid, 0, srch_inf);
  1352. if (rc) {
  1353. cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
  1354. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  1355. }
  1356. return rc;
  1357. }
  1358. static int
  1359. smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  1360. struct cifs_fid *fid, __u16 search_flags,
  1361. struct cifs_search_info *srch_inf)
  1362. {
  1363. return SMB2_query_directory(xid, tcon, fid->persistent_fid,
  1364. fid->volatile_fid, 0, srch_inf);
  1365. }
  1366. static int
  1367. smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  1368. struct cifs_fid *fid)
  1369. {
  1370. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  1371. }
  1372. /*
  1373. * If we negotiate SMB2 protocol and get STATUS_PENDING - update
  1374. * the number of credits and return true. Otherwise - return false.
  1375. */
  1376. static bool
  1377. smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
  1378. {
  1379. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
  1380. if (shdr->Status != STATUS_PENDING)
  1381. return false;
  1382. if (!length) {
  1383. spin_lock(&server->req_lock);
  1384. server->credits += le16_to_cpu(shdr->CreditRequest);
  1385. spin_unlock(&server->req_lock);
  1386. wake_up(&server->request_q);
  1387. }
  1388. return true;
  1389. }
  1390. static bool
  1391. smb2_is_session_expired(char *buf)
  1392. {
  1393. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
  1394. if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
  1395. shdr->Status != STATUS_USER_SESSION_DELETED)
  1396. return false;
  1397. trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
  1398. le16_to_cpu(shdr->Command),
  1399. le64_to_cpu(shdr->MessageId));
  1400. cifs_dbg(FYI, "Session expired or deleted\n");
  1401. return true;
  1402. }
  1403. static int
  1404. smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
  1405. struct cifsInodeInfo *cinode)
  1406. {
  1407. if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
  1408. return SMB2_lease_break(0, tcon, cinode->lease_key,
  1409. smb2_get_lease_state(cinode));
  1410. return SMB2_oplock_break(0, tcon, fid->persistent_fid,
  1411. fid->volatile_fid,
  1412. CIFS_CACHE_READ(cinode) ? 1 : 0);
  1413. }
  1414. static void
  1415. smb2_set_related(struct smb_rqst *rqst)
  1416. {
  1417. struct smb2_sync_hdr *shdr;
  1418. shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
  1419. shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1420. }
  1421. char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
  1422. static void
  1423. smb2_set_next_command(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  1424. {
  1425. struct smb2_sync_hdr *shdr;
  1426. unsigned long len = smb_rqst_len(server, rqst);
  1427. /* SMB headers in a compound are 8 byte aligned. */
  1428. if (len & 7) {
  1429. rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
  1430. rqst->rq_iov[rqst->rq_nvec].iov_len = 8 - (len & 7);
  1431. rqst->rq_nvec++;
  1432. len = smb_rqst_len(server, rqst);
  1433. }
  1434. shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
  1435. shdr->NextCommand = cpu_to_le32(len);
  1436. }
  1437. static int
  1438. smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  1439. struct kstatfs *buf)
  1440. {
  1441. struct smb2_query_info_rsp *rsp;
  1442. struct smb2_fs_full_size_info *info = NULL;
  1443. struct smb_rqst rqst[3];
  1444. int resp_buftype[3];
  1445. struct kvec rsp_iov[3];
  1446. struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
  1447. struct kvec qi_iov[1];
  1448. struct kvec close_iov[1];
  1449. struct cifs_ses *ses = tcon->ses;
  1450. struct TCP_Server_Info *server = ses->server;
  1451. __le16 srch_path = 0; /* Null - open root of share */
  1452. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  1453. struct cifs_open_parms oparms;
  1454. struct cifs_fid fid;
  1455. int flags = 0;
  1456. int rc;
  1457. if (smb3_encryption_required(tcon))
  1458. flags |= CIFS_TRANSFORM_REQ;
  1459. memset(rqst, 0, sizeof(rqst));
  1460. memset(resp_buftype, 0, sizeof(resp_buftype));
  1461. memset(rsp_iov, 0, sizeof(rsp_iov));
  1462. memset(&open_iov, 0, sizeof(open_iov));
  1463. rqst[0].rq_iov = open_iov;
  1464. rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
  1465. oparms.tcon = tcon;
  1466. oparms.desired_access = FILE_READ_ATTRIBUTES;
  1467. oparms.disposition = FILE_OPEN;
  1468. oparms.create_options = 0;
  1469. oparms.fid = &fid;
  1470. oparms.reconnect = false;
  1471. rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &srch_path);
  1472. if (rc)
  1473. goto qfs_exit;
  1474. smb2_set_next_command(server, &rqst[0]);
  1475. memset(&qi_iov, 0, sizeof(qi_iov));
  1476. rqst[1].rq_iov = qi_iov;
  1477. rqst[1].rq_nvec = 1;
  1478. rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
  1479. FS_FULL_SIZE_INFORMATION,
  1480. SMB2_O_INFO_FILESYSTEM, 0,
  1481. sizeof(struct smb2_fs_full_size_info));
  1482. if (rc)
  1483. goto qfs_exit;
  1484. smb2_set_next_command(server, &rqst[1]);
  1485. smb2_set_related(&rqst[1]);
  1486. memset(&close_iov, 0, sizeof(close_iov));
  1487. rqst[2].rq_iov = close_iov;
  1488. rqst[2].rq_nvec = 1;
  1489. rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
  1490. if (rc)
  1491. goto qfs_exit;
  1492. smb2_set_related(&rqst[2]);
  1493. rc = compound_send_recv(xid, ses, flags, 3, rqst,
  1494. resp_buftype, rsp_iov);
  1495. if (rc)
  1496. goto qfs_exit;
  1497. rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
  1498. buf->f_type = SMB2_MAGIC_NUMBER;
  1499. info = (struct smb2_fs_full_size_info *)(
  1500. le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
  1501. rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
  1502. le32_to_cpu(rsp->OutputBufferLength),
  1503. &rsp_iov[1],
  1504. sizeof(struct smb2_fs_full_size_info));
  1505. if (!rc)
  1506. smb2_copy_fs_info_to_kstatfs(info, buf);
  1507. qfs_exit:
  1508. SMB2_open_free(&rqst[0]);
  1509. SMB2_query_info_free(&rqst[1]);
  1510. SMB2_close_free(&rqst[2]);
  1511. free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
  1512. free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
  1513. free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
  1514. return rc;
  1515. }
  1516. static int
  1517. smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  1518. struct kstatfs *buf)
  1519. {
  1520. int rc;
  1521. __le16 srch_path = 0; /* Null - open root of share */
  1522. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  1523. struct cifs_open_parms oparms;
  1524. struct cifs_fid fid;
  1525. if (!tcon->posix_extensions)
  1526. return smb2_queryfs(xid, tcon, buf);
  1527. oparms.tcon = tcon;
  1528. oparms.desired_access = FILE_READ_ATTRIBUTES;
  1529. oparms.disposition = FILE_OPEN;
  1530. oparms.create_options = 0;
  1531. oparms.fid = &fid;
  1532. oparms.reconnect = false;
  1533. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
  1534. if (rc)
  1535. return rc;
  1536. rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
  1537. fid.volatile_fid, buf);
  1538. buf->f_type = SMB2_MAGIC_NUMBER;
  1539. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  1540. return rc;
  1541. }
  1542. static bool
  1543. smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
  1544. {
  1545. return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
  1546. ob1->fid.volatile_fid == ob2->fid.volatile_fid;
  1547. }
  1548. static int
  1549. smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
  1550. __u64 length, __u32 type, int lock, int unlock, bool wait)
  1551. {
  1552. if (unlock && !lock)
  1553. type = SMB2_LOCKFLAG_UNLOCK;
  1554. return SMB2_lock(xid, tlink_tcon(cfile->tlink),
  1555. cfile->fid.persistent_fid, cfile->fid.volatile_fid,
  1556. current->tgid, length, offset, type, wait);
  1557. }
  1558. static void
  1559. smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
  1560. {
  1561. memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
  1562. }
  1563. static void
  1564. smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
  1565. {
  1566. memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  1567. }
  1568. static void
  1569. smb2_new_lease_key(struct cifs_fid *fid)
  1570. {
  1571. generate_random_uuid(fid->lease_key);
  1572. }
  1573. static int
  1574. smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
  1575. const char *search_name,
  1576. struct dfs_info3_param **target_nodes,
  1577. unsigned int *num_of_nodes,
  1578. const struct nls_table *nls_codepage, int remap)
  1579. {
  1580. int rc;
  1581. __le16 *utf16_path = NULL;
  1582. int utf16_path_len = 0;
  1583. struct cifs_tcon *tcon;
  1584. struct fsctl_get_dfs_referral_req *dfs_req = NULL;
  1585. struct get_dfs_referral_rsp *dfs_rsp = NULL;
  1586. u32 dfs_req_size = 0, dfs_rsp_size = 0;
  1587. cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
  1588. /*
  1589. * Try to use the IPC tcon, otherwise just use any
  1590. */
  1591. tcon = ses->tcon_ipc;
  1592. if (tcon == NULL) {
  1593. spin_lock(&cifs_tcp_ses_lock);
  1594. tcon = list_first_entry_or_null(&ses->tcon_list,
  1595. struct cifs_tcon,
  1596. tcon_list);
  1597. if (tcon)
  1598. tcon->tc_count++;
  1599. spin_unlock(&cifs_tcp_ses_lock);
  1600. }
  1601. if (tcon == NULL) {
  1602. cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
  1603. ses);
  1604. rc = -ENOTCONN;
  1605. goto out;
  1606. }
  1607. utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
  1608. &utf16_path_len,
  1609. nls_codepage, remap);
  1610. if (!utf16_path) {
  1611. rc = -ENOMEM;
  1612. goto out;
  1613. }
  1614. dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
  1615. dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
  1616. if (!dfs_req) {
  1617. rc = -ENOMEM;
  1618. goto out;
  1619. }
  1620. /* Highest DFS referral version understood */
  1621. dfs_req->MaxReferralLevel = DFS_VERSION;
  1622. /* Path to resolve in an UTF-16 null-terminated string */
  1623. memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
  1624. do {
  1625. rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
  1626. FSCTL_DFS_GET_REFERRALS,
  1627. true /* is_fsctl */,
  1628. (char *)dfs_req, dfs_req_size,
  1629. (char **)&dfs_rsp, &dfs_rsp_size);
  1630. } while (rc == -EAGAIN);
  1631. if (rc) {
  1632. if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
  1633. cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
  1634. goto out;
  1635. }
  1636. rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
  1637. num_of_nodes, target_nodes,
  1638. nls_codepage, remap, search_name,
  1639. true /* is_unicode */);
  1640. if (rc) {
  1641. cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
  1642. goto out;
  1643. }
  1644. out:
  1645. if (tcon && !tcon->ipc) {
  1646. /* ipc tcons are not refcounted */
  1647. spin_lock(&cifs_tcp_ses_lock);
  1648. tcon->tc_count--;
  1649. spin_unlock(&cifs_tcp_ses_lock);
  1650. }
  1651. kfree(utf16_path);
  1652. kfree(dfs_req);
  1653. kfree(dfs_rsp);
  1654. return rc;
  1655. }
  1656. #define SMB2_SYMLINK_STRUCT_SIZE \
  1657. (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
  1658. static int
  1659. smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
  1660. const char *full_path, char **target_path,
  1661. struct cifs_sb_info *cifs_sb)
  1662. {
  1663. int rc;
  1664. __le16 *utf16_path;
  1665. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  1666. struct cifs_open_parms oparms;
  1667. struct cifs_fid fid;
  1668. struct kvec err_iov = {NULL, 0};
  1669. struct smb2_err_rsp *err_buf = NULL;
  1670. int resp_buftype;
  1671. struct smb2_symlink_err_rsp *symlink;
  1672. unsigned int sub_len;
  1673. unsigned int sub_offset;
  1674. unsigned int print_len;
  1675. unsigned int print_offset;
  1676. cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
  1677. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  1678. if (!utf16_path)
  1679. return -ENOMEM;
  1680. oparms.tcon = tcon;
  1681. oparms.desired_access = FILE_READ_ATTRIBUTES;
  1682. oparms.disposition = FILE_OPEN;
  1683. if (backup_cred(cifs_sb))
  1684. oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
  1685. else
  1686. oparms.create_options = 0;
  1687. oparms.fid = &fid;
  1688. oparms.reconnect = false;
  1689. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
  1690. &resp_buftype);
  1691. if (!rc)
  1692. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  1693. if (!rc || !err_iov.iov_base) {
  1694. rc = -ENOENT;
  1695. goto free_path;
  1696. }
  1697. err_buf = err_iov.iov_base;
  1698. if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
  1699. err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
  1700. rc = -ENOENT;
  1701. goto querty_exit;
  1702. }
  1703. /* open must fail on symlink - reset rc */
  1704. rc = 0;
  1705. symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
  1706. sub_len = le16_to_cpu(symlink->SubstituteNameLength);
  1707. sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
  1708. print_len = le16_to_cpu(symlink->PrintNameLength);
  1709. print_offset = le16_to_cpu(symlink->PrintNameOffset);
  1710. if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
  1711. rc = -ENOENT;
  1712. goto querty_exit;
  1713. }
  1714. if (err_iov.iov_len <
  1715. SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
  1716. rc = -ENOENT;
  1717. goto querty_exit;
  1718. }
  1719. *target_path = cifs_strndup_from_utf16(
  1720. (char *)symlink->PathBuffer + sub_offset,
  1721. sub_len, true, cifs_sb->local_nls);
  1722. if (!(*target_path)) {
  1723. rc = -ENOMEM;
  1724. goto querty_exit;
  1725. }
  1726. convert_delimiter(*target_path, '/');
  1727. cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
  1728. querty_exit:
  1729. free_rsp_buf(resp_buftype, err_buf);
  1730. free_path:
  1731. kfree(utf16_path);
  1732. return rc;
  1733. }
  1734. #ifdef CONFIG_CIFS_ACL
  1735. static struct cifs_ntsd *
  1736. get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
  1737. const struct cifs_fid *cifsfid, u32 *pacllen)
  1738. {
  1739. struct cifs_ntsd *pntsd = NULL;
  1740. unsigned int xid;
  1741. int rc = -EOPNOTSUPP;
  1742. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1743. if (IS_ERR(tlink))
  1744. return ERR_CAST(tlink);
  1745. xid = get_xid();
  1746. cifs_dbg(FYI, "trying to get acl\n");
  1747. rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
  1748. cifsfid->volatile_fid, (void **)&pntsd, pacllen);
  1749. free_xid(xid);
  1750. cifs_put_tlink(tlink);
  1751. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  1752. if (rc)
  1753. return ERR_PTR(rc);
  1754. return pntsd;
  1755. }
  1756. static struct cifs_ntsd *
  1757. get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
  1758. const char *path, u32 *pacllen)
  1759. {
  1760. struct cifs_ntsd *pntsd = NULL;
  1761. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  1762. unsigned int xid;
  1763. int rc;
  1764. struct cifs_tcon *tcon;
  1765. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1766. struct cifs_fid fid;
  1767. struct cifs_open_parms oparms;
  1768. __le16 *utf16_path;
  1769. cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
  1770. if (IS_ERR(tlink))
  1771. return ERR_CAST(tlink);
  1772. tcon = tlink_tcon(tlink);
  1773. xid = get_xid();
  1774. if (backup_cred(cifs_sb))
  1775. oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
  1776. else
  1777. oparms.create_options = 0;
  1778. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  1779. if (!utf16_path) {
  1780. rc = -ENOMEM;
  1781. free_xid(xid);
  1782. return ERR_PTR(rc);
  1783. }
  1784. oparms.tcon = tcon;
  1785. oparms.desired_access = READ_CONTROL;
  1786. oparms.disposition = FILE_OPEN;
  1787. oparms.fid = &fid;
  1788. oparms.reconnect = false;
  1789. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
  1790. kfree(utf16_path);
  1791. if (!rc) {
  1792. rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
  1793. fid.volatile_fid, (void **)&pntsd, pacllen);
  1794. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  1795. }
  1796. cifs_put_tlink(tlink);
  1797. free_xid(xid);
  1798. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  1799. if (rc)
  1800. return ERR_PTR(rc);
  1801. return pntsd;
  1802. }
  1803. #ifdef CONFIG_CIFS_ACL
  1804. static int
  1805. set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
  1806. struct inode *inode, const char *path, int aclflag)
  1807. {
  1808. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  1809. unsigned int xid;
  1810. int rc, access_flags = 0;
  1811. struct cifs_tcon *tcon;
  1812. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1813. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1814. struct cifs_fid fid;
  1815. struct cifs_open_parms oparms;
  1816. __le16 *utf16_path;
  1817. cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
  1818. if (IS_ERR(tlink))
  1819. return PTR_ERR(tlink);
  1820. tcon = tlink_tcon(tlink);
  1821. xid = get_xid();
  1822. if (backup_cred(cifs_sb))
  1823. oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
  1824. else
  1825. oparms.create_options = 0;
  1826. if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
  1827. access_flags = WRITE_OWNER;
  1828. else
  1829. access_flags = WRITE_DAC;
  1830. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  1831. if (!utf16_path) {
  1832. rc = -ENOMEM;
  1833. free_xid(xid);
  1834. return rc;
  1835. }
  1836. oparms.tcon = tcon;
  1837. oparms.desired_access = access_flags;
  1838. oparms.disposition = FILE_OPEN;
  1839. oparms.path = path;
  1840. oparms.fid = &fid;
  1841. oparms.reconnect = false;
  1842. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
  1843. kfree(utf16_path);
  1844. if (!rc) {
  1845. rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
  1846. fid.volatile_fid, pnntsd, acllen, aclflag);
  1847. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  1848. }
  1849. cifs_put_tlink(tlink);
  1850. free_xid(xid);
  1851. return rc;
  1852. }
  1853. #endif /* CIFS_ACL */
  1854. /* Retrieve an ACL from the server */
  1855. static struct cifs_ntsd *
  1856. get_smb2_acl(struct cifs_sb_info *cifs_sb,
  1857. struct inode *inode, const char *path,
  1858. u32 *pacllen)
  1859. {
  1860. struct cifs_ntsd *pntsd = NULL;
  1861. struct cifsFileInfo *open_file = NULL;
  1862. if (inode)
  1863. open_file = find_readable_file(CIFS_I(inode), true);
  1864. if (!open_file)
  1865. return get_smb2_acl_by_path(cifs_sb, path, pacllen);
  1866. pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
  1867. cifsFileInfo_put(open_file);
  1868. return pntsd;
  1869. }
  1870. #endif
  1871. static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
  1872. loff_t offset, loff_t len, bool keep_size)
  1873. {
  1874. struct inode *inode;
  1875. struct cifsInodeInfo *cifsi;
  1876. struct cifsFileInfo *cfile = file->private_data;
  1877. struct file_zero_data_information fsctl_buf;
  1878. long rc;
  1879. unsigned int xid;
  1880. xid = get_xid();
  1881. inode = d_inode(cfile->dentry);
  1882. cifsi = CIFS_I(inode);
  1883. /*
  1884. * We zero the range through ioctl, so we need remove the page caches
  1885. * first, otherwise the data may be inconsistent with the server.
  1886. */
  1887. truncate_pagecache_range(inode, offset, offset + len - 1);
  1888. /* if file not oplocked can't be sure whether asking to extend size */
  1889. if (!CIFS_CACHE_READ(cifsi))
  1890. if (keep_size == false) {
  1891. rc = -EOPNOTSUPP;
  1892. free_xid(xid);
  1893. return rc;
  1894. }
  1895. /*
  1896. * Must check if file sparse since fallocate -z (zero range) assumes
  1897. * non-sparse allocation
  1898. */
  1899. if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
  1900. rc = -EOPNOTSUPP;
  1901. free_xid(xid);
  1902. return rc;
  1903. }
  1904. /*
  1905. * need to make sure we are not asked to extend the file since the SMB3
  1906. * fsctl does not change the file size. In the future we could change
  1907. * this to zero the first part of the range then set the file size
  1908. * which for a non sparse file would zero the newly extended range
  1909. */
  1910. if (keep_size == false)
  1911. if (i_size_read(inode) < offset + len) {
  1912. rc = -EOPNOTSUPP;
  1913. free_xid(xid);
  1914. return rc;
  1915. }
  1916. cifs_dbg(FYI, "offset %lld len %lld", offset, len);
  1917. fsctl_buf.FileOffset = cpu_to_le64(offset);
  1918. fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
  1919. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1920. cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
  1921. true /* is_fctl */, (char *)&fsctl_buf,
  1922. sizeof(struct file_zero_data_information), NULL, NULL);
  1923. free_xid(xid);
  1924. return rc;
  1925. }
  1926. static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
  1927. loff_t offset, loff_t len)
  1928. {
  1929. struct inode *inode;
  1930. struct cifsInodeInfo *cifsi;
  1931. struct cifsFileInfo *cfile = file->private_data;
  1932. struct file_zero_data_information fsctl_buf;
  1933. long rc;
  1934. unsigned int xid;
  1935. __u8 set_sparse = 1;
  1936. xid = get_xid();
  1937. inode = d_inode(cfile->dentry);
  1938. cifsi = CIFS_I(inode);
  1939. /* Need to make file sparse, if not already, before freeing range. */
  1940. /* Consider adding equivalent for compressed since it could also work */
  1941. if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
  1942. rc = -EOPNOTSUPP;
  1943. free_xid(xid);
  1944. return rc;
  1945. }
  1946. /*
  1947. * We implement the punch hole through ioctl, so we need remove the page
  1948. * caches first, otherwise the data may be inconsistent with the server.
  1949. */
  1950. truncate_pagecache_range(inode, offset, offset + len - 1);
  1951. cifs_dbg(FYI, "offset %lld len %lld", offset, len);
  1952. fsctl_buf.FileOffset = cpu_to_le64(offset);
  1953. fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
  1954. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1955. cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
  1956. true /* is_fctl */, (char *)&fsctl_buf,
  1957. sizeof(struct file_zero_data_information), NULL, NULL);
  1958. free_xid(xid);
  1959. return rc;
  1960. }
  1961. static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
  1962. loff_t off, loff_t len, bool keep_size)
  1963. {
  1964. struct inode *inode;
  1965. struct cifsInodeInfo *cifsi;
  1966. struct cifsFileInfo *cfile = file->private_data;
  1967. long rc = -EOPNOTSUPP;
  1968. unsigned int xid;
  1969. xid = get_xid();
  1970. inode = d_inode(cfile->dentry);
  1971. cifsi = CIFS_I(inode);
  1972. /* if file not oplocked can't be sure whether asking to extend size */
  1973. if (!CIFS_CACHE_READ(cifsi))
  1974. if (keep_size == false) {
  1975. free_xid(xid);
  1976. return rc;
  1977. }
  1978. /*
  1979. * Files are non-sparse by default so falloc may be a no-op
  1980. * Must check if file sparse. If not sparse, and not extending
  1981. * then no need to do anything since file already allocated
  1982. */
  1983. if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
  1984. if (keep_size == true)
  1985. rc = 0;
  1986. /* check if extending file */
  1987. else if (i_size_read(inode) >= off + len)
  1988. /* not extending file and already not sparse */
  1989. rc = 0;
  1990. /* BB: in future add else clause to extend file */
  1991. else
  1992. rc = -EOPNOTSUPP;
  1993. free_xid(xid);
  1994. return rc;
  1995. }
  1996. if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
  1997. /*
  1998. * Check if falloc starts within first few pages of file
  1999. * and ends within a few pages of the end of file to
  2000. * ensure that most of file is being forced to be
  2001. * fallocated now. If so then setting whole file sparse
  2002. * ie potentially making a few extra pages at the beginning
  2003. * or end of the file non-sparse via set_sparse is harmless.
  2004. */
  2005. if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
  2006. rc = -EOPNOTSUPP;
  2007. free_xid(xid);
  2008. return rc;
  2009. }
  2010. rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
  2011. }
  2012. /* BB: else ... in future add code to extend file and set sparse */
  2013. free_xid(xid);
  2014. return rc;
  2015. }
  2016. static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
  2017. loff_t off, loff_t len)
  2018. {
  2019. /* KEEP_SIZE already checked for by do_fallocate */
  2020. if (mode & FALLOC_FL_PUNCH_HOLE)
  2021. return smb3_punch_hole(file, tcon, off, len);
  2022. else if (mode & FALLOC_FL_ZERO_RANGE) {
  2023. if (mode & FALLOC_FL_KEEP_SIZE)
  2024. return smb3_zero_range(file, tcon, off, len, true);
  2025. return smb3_zero_range(file, tcon, off, len, false);
  2026. } else if (mode == FALLOC_FL_KEEP_SIZE)
  2027. return smb3_simple_falloc(file, tcon, off, len, true);
  2028. else if (mode == 0)
  2029. return smb3_simple_falloc(file, tcon, off, len, false);
  2030. return -EOPNOTSUPP;
  2031. }
  2032. static void
  2033. smb2_downgrade_oplock(struct TCP_Server_Info *server,
  2034. struct cifsInodeInfo *cinode, __u32 oplock,
  2035. unsigned int epoch, bool *purge_cache)
  2036. {
  2037. server->ops->set_oplock_level(cinode, oplock, 0, NULL);
  2038. }
  2039. static void
  2040. smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  2041. unsigned int epoch, bool *purge_cache);
  2042. static void
  2043. smb3_downgrade_oplock(struct TCP_Server_Info *server,
  2044. struct cifsInodeInfo *cinode, __u32 oplock,
  2045. unsigned int epoch, bool *purge_cache)
  2046. {
  2047. unsigned int old_state = cinode->oplock;
  2048. unsigned int old_epoch = cinode->epoch;
  2049. unsigned int new_state;
  2050. if (epoch > old_epoch) {
  2051. smb21_set_oplock_level(cinode, oplock, 0, NULL);
  2052. cinode->epoch = epoch;
  2053. }
  2054. new_state = cinode->oplock;
  2055. *purge_cache = false;
  2056. if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
  2057. (new_state & CIFS_CACHE_READ_FLG) == 0)
  2058. *purge_cache = true;
  2059. else if (old_state == new_state && (epoch - old_epoch > 1))
  2060. *purge_cache = true;
  2061. }
  2062. static void
  2063. smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  2064. unsigned int epoch, bool *purge_cache)
  2065. {
  2066. oplock &= 0xFF;
  2067. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  2068. return;
  2069. if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  2070. cinode->oplock = CIFS_CACHE_RHW_FLG;
  2071. cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
  2072. &cinode->vfs_inode);
  2073. } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
  2074. cinode->oplock = CIFS_CACHE_RW_FLG;
  2075. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  2076. &cinode->vfs_inode);
  2077. } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
  2078. cinode->oplock = CIFS_CACHE_READ_FLG;
  2079. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  2080. &cinode->vfs_inode);
  2081. } else
  2082. cinode->oplock = 0;
  2083. }
  2084. static void
  2085. smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  2086. unsigned int epoch, bool *purge_cache)
  2087. {
  2088. char message[5] = {0};
  2089. unsigned int new_oplock = 0;
  2090. oplock &= 0xFF;
  2091. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  2092. return;
  2093. /* Check if the server granted an oplock rather than a lease */
  2094. if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  2095. return smb2_set_oplock_level(cinode, oplock, epoch,
  2096. purge_cache);
  2097. if (oplock & SMB2_LEASE_READ_CACHING_HE) {
  2098. new_oplock |= CIFS_CACHE_READ_FLG;
  2099. strcat(message, "R");
  2100. }
  2101. if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
  2102. new_oplock |= CIFS_CACHE_HANDLE_FLG;
  2103. strcat(message, "H");
  2104. }
  2105. if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
  2106. new_oplock |= CIFS_CACHE_WRITE_FLG;
  2107. strcat(message, "W");
  2108. }
  2109. if (!new_oplock)
  2110. strncpy(message, "None", sizeof(message));
  2111. cinode->oplock = new_oplock;
  2112. cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
  2113. &cinode->vfs_inode);
  2114. }
  2115. static void
  2116. smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  2117. unsigned int epoch, bool *purge_cache)
  2118. {
  2119. unsigned int old_oplock = cinode->oplock;
  2120. smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
  2121. if (purge_cache) {
  2122. *purge_cache = false;
  2123. if (old_oplock == CIFS_CACHE_READ_FLG) {
  2124. if (cinode->oplock == CIFS_CACHE_READ_FLG &&
  2125. (epoch - cinode->epoch > 0))
  2126. *purge_cache = true;
  2127. else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  2128. (epoch - cinode->epoch > 1))
  2129. *purge_cache = true;
  2130. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  2131. (epoch - cinode->epoch > 1))
  2132. *purge_cache = true;
  2133. else if (cinode->oplock == 0 &&
  2134. (epoch - cinode->epoch > 0))
  2135. *purge_cache = true;
  2136. } else if (old_oplock == CIFS_CACHE_RH_FLG) {
  2137. if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  2138. (epoch - cinode->epoch > 0))
  2139. *purge_cache = true;
  2140. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  2141. (epoch - cinode->epoch > 1))
  2142. *purge_cache = true;
  2143. }
  2144. cinode->epoch = epoch;
  2145. }
  2146. }
  2147. static bool
  2148. smb2_is_read_op(__u32 oplock)
  2149. {
  2150. return oplock == SMB2_OPLOCK_LEVEL_II;
  2151. }
  2152. static bool
  2153. smb21_is_read_op(__u32 oplock)
  2154. {
  2155. return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
  2156. !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
  2157. }
  2158. static __le32
  2159. map_oplock_to_lease(u8 oplock)
  2160. {
  2161. if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  2162. return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
  2163. else if (oplock == SMB2_OPLOCK_LEVEL_II)
  2164. return SMB2_LEASE_READ_CACHING;
  2165. else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
  2166. return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
  2167. SMB2_LEASE_WRITE_CACHING;
  2168. return 0;
  2169. }
  2170. static char *
  2171. smb2_create_lease_buf(u8 *lease_key, u8 oplock)
  2172. {
  2173. struct create_lease *buf;
  2174. buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
  2175. if (!buf)
  2176. return NULL;
  2177. memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
  2178. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  2179. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  2180. (struct create_lease, lcontext));
  2181. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
  2182. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  2183. (struct create_lease, Name));
  2184. buf->ccontext.NameLength = cpu_to_le16(4);
  2185. /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
  2186. buf->Name[0] = 'R';
  2187. buf->Name[1] = 'q';
  2188. buf->Name[2] = 'L';
  2189. buf->Name[3] = 's';
  2190. return (char *)buf;
  2191. }
  2192. static char *
  2193. smb3_create_lease_buf(u8 *lease_key, u8 oplock)
  2194. {
  2195. struct create_lease_v2 *buf;
  2196. buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
  2197. if (!buf)
  2198. return NULL;
  2199. memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
  2200. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  2201. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  2202. (struct create_lease_v2, lcontext));
  2203. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
  2204. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  2205. (struct create_lease_v2, Name));
  2206. buf->ccontext.NameLength = cpu_to_le16(4);
  2207. /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
  2208. buf->Name[0] = 'R';
  2209. buf->Name[1] = 'q';
  2210. buf->Name[2] = 'L';
  2211. buf->Name[3] = 's';
  2212. return (char *)buf;
  2213. }
  2214. static __u8
  2215. smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
  2216. {
  2217. struct create_lease *lc = (struct create_lease *)buf;
  2218. *epoch = 0; /* not used */
  2219. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  2220. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  2221. return le32_to_cpu(lc->lcontext.LeaseState);
  2222. }
  2223. static __u8
  2224. smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
  2225. {
  2226. struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
  2227. *epoch = le16_to_cpu(lc->lcontext.Epoch);
  2228. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  2229. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  2230. if (lease_key)
  2231. memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
  2232. return le32_to_cpu(lc->lcontext.LeaseState);
  2233. }
  2234. static unsigned int
  2235. smb2_wp_retry_size(struct inode *inode)
  2236. {
  2237. return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
  2238. SMB2_MAX_BUFFER_SIZE);
  2239. }
  2240. static bool
  2241. smb2_dir_needs_close(struct cifsFileInfo *cfile)
  2242. {
  2243. return !cfile->invalidHandle;
  2244. }
  2245. static void
  2246. fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
  2247. struct smb_rqst *old_rq)
  2248. {
  2249. struct smb2_sync_hdr *shdr =
  2250. (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
  2251. memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
  2252. tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
  2253. tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
  2254. tr_hdr->Flags = cpu_to_le16(0x01);
  2255. get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
  2256. memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
  2257. }
  2258. /* We can not use the normal sg_set_buf() as we will sometimes pass a
  2259. * stack object as buf.
  2260. */
  2261. static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
  2262. unsigned int buflen)
  2263. {
  2264. void *addr;
  2265. /*
  2266. * VMAP_STACK (at least) puts stack into the vmalloc address space
  2267. */
  2268. if (is_vmalloc_addr(buf))
  2269. addr = vmalloc_to_page(buf);
  2270. else
  2271. addr = virt_to_page(buf);
  2272. sg_set_page(sg, addr, buflen, offset_in_page(buf));
  2273. }
  2274. /* Assumes the first rqst has a transform header as the first iov.
  2275. * I.e.
  2276. * rqst[0].rq_iov[0] is transform header
  2277. * rqst[0].rq_iov[1+] data to be encrypted/decrypted
  2278. * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
  2279. */
  2280. static struct scatterlist *
  2281. init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
  2282. {
  2283. unsigned int sg_len;
  2284. struct scatterlist *sg;
  2285. unsigned int i;
  2286. unsigned int j;
  2287. unsigned int idx = 0;
  2288. int skip;
  2289. sg_len = 1;
  2290. for (i = 0; i < num_rqst; i++)
  2291. sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
  2292. sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
  2293. if (!sg)
  2294. return NULL;
  2295. sg_init_table(sg, sg_len);
  2296. for (i = 0; i < num_rqst; i++) {
  2297. for (j = 0; j < rqst[i].rq_nvec; j++) {
  2298. /*
  2299. * The first rqst has a transform header where the
  2300. * first 20 bytes are not part of the encrypted blob
  2301. */
  2302. skip = (i == 0) && (j == 0) ? 20 : 0;
  2303. smb2_sg_set_buf(&sg[idx++],
  2304. rqst[i].rq_iov[j].iov_base + skip,
  2305. rqst[i].rq_iov[j].iov_len - skip);
  2306. }
  2307. for (j = 0; j < rqst[i].rq_npages; j++) {
  2308. unsigned int len, offset;
  2309. rqst_page_get_length(&rqst[i], j, &len, &offset);
  2310. sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
  2311. }
  2312. }
  2313. smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
  2314. return sg;
  2315. }
  2316. static int
  2317. smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
  2318. {
  2319. struct cifs_ses *ses;
  2320. u8 *ses_enc_key;
  2321. spin_lock(&cifs_tcp_ses_lock);
  2322. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  2323. if (ses->Suid != ses_id)
  2324. continue;
  2325. ses_enc_key = enc ? ses->smb3encryptionkey :
  2326. ses->smb3decryptionkey;
  2327. memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
  2328. spin_unlock(&cifs_tcp_ses_lock);
  2329. return 0;
  2330. }
  2331. spin_unlock(&cifs_tcp_ses_lock);
  2332. return -EAGAIN;
  2333. }
  2334. /*
  2335. * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
  2336. * iov[0] - transform header (associate data),
  2337. * iov[1-N] - SMB2 header and pages - data to encrypt.
  2338. * On success return encrypted data in iov[1-N] and pages, leave iov[0]
  2339. * untouched.
  2340. */
  2341. static int
  2342. crypt_message(struct TCP_Server_Info *server, int num_rqst,
  2343. struct smb_rqst *rqst, int enc)
  2344. {
  2345. struct smb2_transform_hdr *tr_hdr =
  2346. (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
  2347. unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
  2348. int rc = 0;
  2349. struct scatterlist *sg;
  2350. u8 sign[SMB2_SIGNATURE_SIZE] = {};
  2351. u8 key[SMB3_SIGN_KEY_SIZE];
  2352. struct aead_request *req;
  2353. char *iv;
  2354. unsigned int iv_len;
  2355. DECLARE_CRYPTO_WAIT(wait);
  2356. struct crypto_aead *tfm;
  2357. unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
  2358. rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
  2359. if (rc) {
  2360. cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
  2361. enc ? "en" : "de");
  2362. return rc;
  2363. }
  2364. rc = smb3_crypto_aead_allocate(server);
  2365. if (rc) {
  2366. cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
  2367. return rc;
  2368. }
  2369. tfm = enc ? server->secmech.ccmaesencrypt :
  2370. server->secmech.ccmaesdecrypt;
  2371. rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
  2372. if (rc) {
  2373. cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
  2374. return rc;
  2375. }
  2376. rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
  2377. if (rc) {
  2378. cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
  2379. return rc;
  2380. }
  2381. req = aead_request_alloc(tfm, GFP_KERNEL);
  2382. if (!req) {
  2383. cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
  2384. return -ENOMEM;
  2385. }
  2386. if (!enc) {
  2387. memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
  2388. crypt_len += SMB2_SIGNATURE_SIZE;
  2389. }
  2390. sg = init_sg(num_rqst, rqst, sign);
  2391. if (!sg) {
  2392. cifs_dbg(VFS, "%s: Failed to init sg", __func__);
  2393. rc = -ENOMEM;
  2394. goto free_req;
  2395. }
  2396. iv_len = crypto_aead_ivsize(tfm);
  2397. iv = kzalloc(iv_len, GFP_KERNEL);
  2398. if (!iv) {
  2399. cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
  2400. rc = -ENOMEM;
  2401. goto free_sg;
  2402. }
  2403. iv[0] = 3;
  2404. memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
  2405. aead_request_set_crypt(req, sg, sg, crypt_len, iv);
  2406. aead_request_set_ad(req, assoc_data_len);
  2407. aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  2408. crypto_req_done, &wait);
  2409. rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
  2410. : crypto_aead_decrypt(req), &wait);
  2411. if (!rc && enc)
  2412. memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
  2413. kfree(iv);
  2414. free_sg:
  2415. kfree(sg);
  2416. free_req:
  2417. kfree(req);
  2418. return rc;
  2419. }
  2420. void
  2421. smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
  2422. {
  2423. int i, j;
  2424. for (i = 0; i < num_rqst; i++) {
  2425. if (rqst[i].rq_pages) {
  2426. for (j = rqst[i].rq_npages - 1; j >= 0; j--)
  2427. put_page(rqst[i].rq_pages[j]);
  2428. kfree(rqst[i].rq_pages);
  2429. }
  2430. }
  2431. }
  2432. /*
  2433. * This function will initialize new_rq and encrypt the content.
  2434. * The first entry, new_rq[0], only contains a single iov which contains
  2435. * a smb2_transform_hdr and is pre-allocated by the caller.
  2436. * This function then populates new_rq[1+] with the content from olq_rq[0+].
  2437. *
  2438. * The end result is an array of smb_rqst structures where the first structure
  2439. * only contains a single iov for the transform header which we then can pass
  2440. * to crypt_message().
  2441. *
  2442. * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
  2443. * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
  2444. */
  2445. static int
  2446. smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
  2447. struct smb_rqst *new_rq, struct smb_rqst *old_rq)
  2448. {
  2449. struct page **pages;
  2450. struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
  2451. unsigned int npages;
  2452. unsigned int orig_len = 0;
  2453. int i, j;
  2454. int rc = -ENOMEM;
  2455. for (i = 1; i < num_rqst; i++) {
  2456. npages = old_rq[i - 1].rq_npages;
  2457. pages = kmalloc_array(npages, sizeof(struct page *),
  2458. GFP_KERNEL);
  2459. if (!pages)
  2460. goto err_free;
  2461. new_rq[i].rq_pages = pages;
  2462. new_rq[i].rq_npages = npages;
  2463. new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
  2464. new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
  2465. new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
  2466. new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
  2467. new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
  2468. orig_len += smb_rqst_len(server, &old_rq[i - 1]);
  2469. for (j = 0; j < npages; j++) {
  2470. pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
  2471. if (!pages[j])
  2472. goto err_free;
  2473. }
  2474. /* copy pages form the old */
  2475. for (j = 0; j < npages; j++) {
  2476. char *dst, *src;
  2477. unsigned int offset, len;
  2478. rqst_page_get_length(&new_rq[i], j, &len, &offset);
  2479. dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
  2480. src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
  2481. memcpy(dst, src, len);
  2482. kunmap(new_rq[i].rq_pages[j]);
  2483. kunmap(old_rq[i - 1].rq_pages[j]);
  2484. }
  2485. }
  2486. /* fill the 1st iov with a transform header */
  2487. fill_transform_hdr(tr_hdr, orig_len, old_rq);
  2488. rc = crypt_message(server, num_rqst, new_rq, 1);
  2489. cifs_dbg(FYI, "encrypt message returned %d", rc);
  2490. if (rc)
  2491. goto err_free;
  2492. return rc;
  2493. err_free:
  2494. smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
  2495. return rc;
  2496. }
  2497. static int
  2498. smb3_is_transform_hdr(void *buf)
  2499. {
  2500. struct smb2_transform_hdr *trhdr = buf;
  2501. return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
  2502. }
  2503. static int
  2504. decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
  2505. unsigned int buf_data_size, struct page **pages,
  2506. unsigned int npages, unsigned int page_data_size)
  2507. {
  2508. struct kvec iov[2];
  2509. struct smb_rqst rqst = {NULL};
  2510. int rc;
  2511. iov[0].iov_base = buf;
  2512. iov[0].iov_len = sizeof(struct smb2_transform_hdr);
  2513. iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
  2514. iov[1].iov_len = buf_data_size;
  2515. rqst.rq_iov = iov;
  2516. rqst.rq_nvec = 2;
  2517. rqst.rq_pages = pages;
  2518. rqst.rq_npages = npages;
  2519. rqst.rq_pagesz = PAGE_SIZE;
  2520. rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
  2521. rc = crypt_message(server, 1, &rqst, 0);
  2522. cifs_dbg(FYI, "decrypt message returned %d\n", rc);
  2523. if (rc)
  2524. return rc;
  2525. memmove(buf, iov[1].iov_base, buf_data_size);
  2526. server->total_read = buf_data_size + page_data_size;
  2527. return rc;
  2528. }
  2529. static int
  2530. read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
  2531. unsigned int npages, unsigned int len)
  2532. {
  2533. int i;
  2534. int length;
  2535. for (i = 0; i < npages; i++) {
  2536. struct page *page = pages[i];
  2537. size_t n;
  2538. n = len;
  2539. if (len >= PAGE_SIZE) {
  2540. /* enough data to fill the page */
  2541. n = PAGE_SIZE;
  2542. len -= n;
  2543. } else {
  2544. zero_user(page, len, PAGE_SIZE - len);
  2545. len = 0;
  2546. }
  2547. length = cifs_read_page_from_socket(server, page, 0, n);
  2548. if (length < 0)
  2549. return length;
  2550. server->total_read += length;
  2551. }
  2552. return 0;
  2553. }
  2554. static int
  2555. init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
  2556. unsigned int cur_off, struct bio_vec **page_vec)
  2557. {
  2558. struct bio_vec *bvec;
  2559. int i;
  2560. bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
  2561. if (!bvec)
  2562. return -ENOMEM;
  2563. for (i = 0; i < npages; i++) {
  2564. bvec[i].bv_page = pages[i];
  2565. bvec[i].bv_offset = (i == 0) ? cur_off : 0;
  2566. bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
  2567. data_size -= bvec[i].bv_len;
  2568. }
  2569. if (data_size != 0) {
  2570. cifs_dbg(VFS, "%s: something went wrong\n", __func__);
  2571. kfree(bvec);
  2572. return -EIO;
  2573. }
  2574. *page_vec = bvec;
  2575. return 0;
  2576. }
  2577. static int
  2578. handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
  2579. char *buf, unsigned int buf_len, struct page **pages,
  2580. unsigned int npages, unsigned int page_data_size)
  2581. {
  2582. unsigned int data_offset;
  2583. unsigned int data_len;
  2584. unsigned int cur_off;
  2585. unsigned int cur_page_idx;
  2586. unsigned int pad_len;
  2587. struct cifs_readdata *rdata = mid->callback_data;
  2588. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
  2589. struct bio_vec *bvec = NULL;
  2590. struct iov_iter iter;
  2591. struct kvec iov;
  2592. int length;
  2593. bool use_rdma_mr = false;
  2594. if (shdr->Command != SMB2_READ) {
  2595. cifs_dbg(VFS, "only big read responses are supported\n");
  2596. return -ENOTSUPP;
  2597. }
  2598. if (server->ops->is_session_expired &&
  2599. server->ops->is_session_expired(buf)) {
  2600. cifs_reconnect(server);
  2601. wake_up(&server->response_q);
  2602. return -1;
  2603. }
  2604. if (server->ops->is_status_pending &&
  2605. server->ops->is_status_pending(buf, server, 0))
  2606. return -1;
  2607. /* set up first two iov to get credits */
  2608. rdata->iov[0].iov_base = buf;
  2609. rdata->iov[0].iov_len = 0;
  2610. rdata->iov[1].iov_base = buf;
  2611. rdata->iov[1].iov_len =
  2612. min_t(unsigned int, buf_len, server->vals->read_rsp_size);
  2613. cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
  2614. rdata->iov[0].iov_base, rdata->iov[0].iov_len);
  2615. cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
  2616. rdata->iov[1].iov_base, rdata->iov[1].iov_len);
  2617. rdata->result = server->ops->map_error(buf, true);
  2618. if (rdata->result != 0) {
  2619. cifs_dbg(FYI, "%s: server returned error %d\n",
  2620. __func__, rdata->result);
  2621. /* normal error on read response */
  2622. dequeue_mid(mid, false);
  2623. return 0;
  2624. }
  2625. data_offset = server->ops->read_data_offset(buf);
  2626. #ifdef CONFIG_CIFS_SMB_DIRECT
  2627. use_rdma_mr = rdata->mr;
  2628. #endif
  2629. data_len = server->ops->read_data_length(buf, use_rdma_mr);
  2630. if (data_offset < server->vals->read_rsp_size) {
  2631. /*
  2632. * win2k8 sometimes sends an offset of 0 when the read
  2633. * is beyond the EOF. Treat it as if the data starts just after
  2634. * the header.
  2635. */
  2636. cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
  2637. __func__, data_offset);
  2638. data_offset = server->vals->read_rsp_size;
  2639. } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
  2640. /* data_offset is beyond the end of smallbuf */
  2641. cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
  2642. __func__, data_offset);
  2643. rdata->result = -EIO;
  2644. dequeue_mid(mid, rdata->result);
  2645. return 0;
  2646. }
  2647. pad_len = data_offset - server->vals->read_rsp_size;
  2648. if (buf_len <= data_offset) {
  2649. /* read response payload is in pages */
  2650. cur_page_idx = pad_len / PAGE_SIZE;
  2651. cur_off = pad_len % PAGE_SIZE;
  2652. if (cur_page_idx != 0) {
  2653. /* data offset is beyond the 1st page of response */
  2654. cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
  2655. __func__, data_offset);
  2656. rdata->result = -EIO;
  2657. dequeue_mid(mid, rdata->result);
  2658. return 0;
  2659. }
  2660. if (data_len > page_data_size - pad_len) {
  2661. /* data_len is corrupt -- discard frame */
  2662. rdata->result = -EIO;
  2663. dequeue_mid(mid, rdata->result);
  2664. return 0;
  2665. }
  2666. rdata->result = init_read_bvec(pages, npages, page_data_size,
  2667. cur_off, &bvec);
  2668. if (rdata->result != 0) {
  2669. dequeue_mid(mid, rdata->result);
  2670. return 0;
  2671. }
  2672. iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
  2673. } else if (buf_len >= data_offset + data_len) {
  2674. /* read response payload is in buf */
  2675. WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
  2676. iov.iov_base = buf + data_offset;
  2677. iov.iov_len = data_len;
  2678. iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
  2679. } else {
  2680. /* read response payload cannot be in both buf and pages */
  2681. WARN_ONCE(1, "buf can not contain only a part of read data");
  2682. rdata->result = -EIO;
  2683. dequeue_mid(mid, rdata->result);
  2684. return 0;
  2685. }
  2686. length = rdata->copy_into_pages(server, rdata, &iter);
  2687. kfree(bvec);
  2688. if (length < 0)
  2689. return length;
  2690. dequeue_mid(mid, false);
  2691. return length;
  2692. }
  2693. static int
  2694. receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
  2695. {
  2696. char *buf = server->smallbuf;
  2697. struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
  2698. unsigned int npages;
  2699. struct page **pages;
  2700. unsigned int len;
  2701. unsigned int buflen = server->pdu_size;
  2702. int rc;
  2703. int i = 0;
  2704. len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
  2705. sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
  2706. rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
  2707. if (rc < 0)
  2708. return rc;
  2709. server->total_read += rc;
  2710. len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
  2711. server->vals->read_rsp_size;
  2712. npages = DIV_ROUND_UP(len, PAGE_SIZE);
  2713. pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
  2714. if (!pages) {
  2715. rc = -ENOMEM;
  2716. goto discard_data;
  2717. }
  2718. for (; i < npages; i++) {
  2719. pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
  2720. if (!pages[i]) {
  2721. rc = -ENOMEM;
  2722. goto discard_data;
  2723. }
  2724. }
  2725. /* read read data into pages */
  2726. rc = read_data_into_pages(server, pages, npages, len);
  2727. if (rc)
  2728. goto free_pages;
  2729. rc = cifs_discard_remaining_data(server);
  2730. if (rc)
  2731. goto free_pages;
  2732. rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
  2733. pages, npages, len);
  2734. if (rc)
  2735. goto free_pages;
  2736. *mid = smb2_find_mid(server, buf);
  2737. if (*mid == NULL)
  2738. cifs_dbg(FYI, "mid not found\n");
  2739. else {
  2740. cifs_dbg(FYI, "mid found\n");
  2741. (*mid)->decrypted = true;
  2742. rc = handle_read_data(server, *mid, buf,
  2743. server->vals->read_rsp_size,
  2744. pages, npages, len);
  2745. }
  2746. free_pages:
  2747. for (i = i - 1; i >= 0; i--)
  2748. put_page(pages[i]);
  2749. kfree(pages);
  2750. return rc;
  2751. discard_data:
  2752. cifs_discard_remaining_data(server);
  2753. goto free_pages;
  2754. }
  2755. static int
  2756. receive_encrypted_standard(struct TCP_Server_Info *server,
  2757. struct mid_q_entry **mids, char **bufs,
  2758. int *num_mids)
  2759. {
  2760. int ret, length;
  2761. char *buf = server->smallbuf;
  2762. struct smb2_sync_hdr *shdr;
  2763. unsigned int pdu_length = server->pdu_size;
  2764. unsigned int buf_size;
  2765. struct mid_q_entry *mid_entry;
  2766. int next_is_large;
  2767. char *next_buffer = NULL;
  2768. *num_mids = 0;
  2769. /* switch to large buffer if too big for a small one */
  2770. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
  2771. server->large_buf = true;
  2772. memcpy(server->bigbuf, buf, server->total_read);
  2773. buf = server->bigbuf;
  2774. }
  2775. /* now read the rest */
  2776. length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
  2777. pdu_length - HEADER_SIZE(server) + 1);
  2778. if (length < 0)
  2779. return length;
  2780. server->total_read += length;
  2781. buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
  2782. length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
  2783. if (length)
  2784. return length;
  2785. next_is_large = server->large_buf;
  2786. one_more:
  2787. shdr = (struct smb2_sync_hdr *)buf;
  2788. if (shdr->NextCommand) {
  2789. if (next_is_large)
  2790. next_buffer = (char *)cifs_buf_get();
  2791. else
  2792. next_buffer = (char *)cifs_small_buf_get();
  2793. memcpy(next_buffer,
  2794. buf + le32_to_cpu(shdr->NextCommand),
  2795. pdu_length - le32_to_cpu(shdr->NextCommand));
  2796. }
  2797. mid_entry = smb2_find_mid(server, buf);
  2798. if (mid_entry == NULL)
  2799. cifs_dbg(FYI, "mid not found\n");
  2800. else {
  2801. cifs_dbg(FYI, "mid found\n");
  2802. mid_entry->decrypted = true;
  2803. mid_entry->resp_buf_size = server->pdu_size;
  2804. }
  2805. if (*num_mids >= MAX_COMPOUND) {
  2806. cifs_dbg(VFS, "too many PDUs in compound\n");
  2807. return -1;
  2808. }
  2809. bufs[*num_mids] = buf;
  2810. mids[(*num_mids)++] = mid_entry;
  2811. if (mid_entry && mid_entry->handle)
  2812. ret = mid_entry->handle(server, mid_entry);
  2813. else
  2814. ret = cifs_handle_standard(server, mid_entry);
  2815. if (ret == 0 && shdr->NextCommand) {
  2816. pdu_length -= le32_to_cpu(shdr->NextCommand);
  2817. server->large_buf = next_is_large;
  2818. if (next_is_large)
  2819. server->bigbuf = buf = next_buffer;
  2820. else
  2821. server->smallbuf = buf = next_buffer;
  2822. goto one_more;
  2823. } else if (ret != 0) {
  2824. /*
  2825. * ret != 0 here means that we didn't get to handle_mid() thus
  2826. * server->smallbuf and server->bigbuf are still valid. We need
  2827. * to free next_buffer because it is not going to be used
  2828. * anywhere.
  2829. */
  2830. if (next_is_large)
  2831. free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
  2832. else
  2833. free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
  2834. }
  2835. return ret;
  2836. }
  2837. static int
  2838. smb3_receive_transform(struct TCP_Server_Info *server,
  2839. struct mid_q_entry **mids, char **bufs, int *num_mids)
  2840. {
  2841. char *buf = server->smallbuf;
  2842. unsigned int pdu_length = server->pdu_size;
  2843. struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
  2844. unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
  2845. if (pdu_length < sizeof(struct smb2_transform_hdr) +
  2846. sizeof(struct smb2_sync_hdr)) {
  2847. cifs_dbg(VFS, "Transform message is too small (%u)\n",
  2848. pdu_length);
  2849. cifs_reconnect(server);
  2850. wake_up(&server->response_q);
  2851. return -ECONNABORTED;
  2852. }
  2853. if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
  2854. cifs_dbg(VFS, "Transform message is broken\n");
  2855. cifs_reconnect(server);
  2856. wake_up(&server->response_q);
  2857. return -ECONNABORTED;
  2858. }
  2859. /* TODO: add support for compounds containing READ. */
  2860. if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
  2861. *num_mids = 1;
  2862. return receive_encrypted_read(server, &mids[0]);
  2863. }
  2864. return receive_encrypted_standard(server, mids, bufs, num_mids);
  2865. }
  2866. int
  2867. smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  2868. {
  2869. char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
  2870. return handle_read_data(server, mid, buf, server->pdu_size,
  2871. NULL, 0, 0);
  2872. }
  2873. static int
  2874. smb2_next_header(char *buf)
  2875. {
  2876. struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
  2877. struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
  2878. if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
  2879. return sizeof(struct smb2_transform_hdr) +
  2880. le32_to_cpu(t_hdr->OriginalMessageSize);
  2881. return le32_to_cpu(hdr->NextCommand);
  2882. }
  2883. struct smb_version_operations smb20_operations = {
  2884. .compare_fids = smb2_compare_fids,
  2885. .setup_request = smb2_setup_request,
  2886. .setup_async_request = smb2_setup_async_request,
  2887. .check_receive = smb2_check_receive,
  2888. .add_credits = smb2_add_credits,
  2889. .set_credits = smb2_set_credits,
  2890. .get_credits_field = smb2_get_credits_field,
  2891. .get_credits = smb2_get_credits,
  2892. .wait_mtu_credits = cifs_wait_mtu_credits,
  2893. .get_next_mid = smb2_get_next_mid,
  2894. .revert_current_mid = smb2_revert_current_mid,
  2895. .read_data_offset = smb2_read_data_offset,
  2896. .read_data_length = smb2_read_data_length,
  2897. .map_error = map_smb2_to_linux_error,
  2898. .find_mid = smb2_find_mid,
  2899. .check_message = smb2_check_message,
  2900. .dump_detail = smb2_dump_detail,
  2901. .clear_stats = smb2_clear_stats,
  2902. .print_stats = smb2_print_stats,
  2903. .is_oplock_break = smb2_is_valid_oplock_break,
  2904. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  2905. .downgrade_oplock = smb2_downgrade_oplock,
  2906. .need_neg = smb2_need_neg,
  2907. .negotiate = smb2_negotiate,
  2908. .negotiate_wsize = smb2_negotiate_wsize,
  2909. .negotiate_rsize = smb2_negotiate_rsize,
  2910. .sess_setup = SMB2_sess_setup,
  2911. .logoff = SMB2_logoff,
  2912. .tree_connect = SMB2_tcon,
  2913. .tree_disconnect = SMB2_tdis,
  2914. .qfs_tcon = smb2_qfs_tcon,
  2915. .is_path_accessible = smb2_is_path_accessible,
  2916. .can_echo = smb2_can_echo,
  2917. .echo = SMB2_echo,
  2918. .query_path_info = smb2_query_path_info,
  2919. .get_srv_inum = smb2_get_srv_inum,
  2920. .query_file_info = smb2_query_file_info,
  2921. .set_path_size = smb2_set_path_size,
  2922. .set_file_size = smb2_set_file_size,
  2923. .set_file_info = smb2_set_file_info,
  2924. .set_compression = smb2_set_compression,
  2925. .mkdir = smb2_mkdir,
  2926. .mkdir_setinfo = smb2_mkdir_setinfo,
  2927. .rmdir = smb2_rmdir,
  2928. .unlink = smb2_unlink,
  2929. .rename = smb2_rename_path,
  2930. .create_hardlink = smb2_create_hardlink,
  2931. .query_symlink = smb2_query_symlink,
  2932. .query_mf_symlink = smb3_query_mf_symlink,
  2933. .create_mf_symlink = smb3_create_mf_symlink,
  2934. .open = smb2_open_file,
  2935. .set_fid = smb2_set_fid,
  2936. .close = smb2_close_file,
  2937. .flush = smb2_flush_file,
  2938. .async_readv = smb2_async_readv,
  2939. .async_writev = smb2_async_writev,
  2940. .sync_read = smb2_sync_read,
  2941. .sync_write = smb2_sync_write,
  2942. .query_dir_first = smb2_query_dir_first,
  2943. .query_dir_next = smb2_query_dir_next,
  2944. .close_dir = smb2_close_dir,
  2945. .calc_smb_size = smb2_calc_size,
  2946. .is_status_pending = smb2_is_status_pending,
  2947. .is_session_expired = smb2_is_session_expired,
  2948. .oplock_response = smb2_oplock_response,
  2949. .queryfs = smb2_queryfs,
  2950. .mand_lock = smb2_mand_lock,
  2951. .mand_unlock_range = smb2_unlock_range,
  2952. .push_mand_locks = smb2_push_mandatory_locks,
  2953. .get_lease_key = smb2_get_lease_key,
  2954. .set_lease_key = smb2_set_lease_key,
  2955. .new_lease_key = smb2_new_lease_key,
  2956. .calc_signature = smb2_calc_signature,
  2957. .is_read_op = smb2_is_read_op,
  2958. .set_oplock_level = smb2_set_oplock_level,
  2959. .create_lease_buf = smb2_create_lease_buf,
  2960. .parse_lease_buf = smb2_parse_lease_buf,
  2961. .copychunk_range = smb2_copychunk_range,
  2962. .wp_retry_size = smb2_wp_retry_size,
  2963. .dir_needs_close = smb2_dir_needs_close,
  2964. .get_dfs_refer = smb2_get_dfs_refer,
  2965. .select_sectype = smb2_select_sectype,
  2966. #ifdef CONFIG_CIFS_XATTR
  2967. .query_all_EAs = smb2_query_eas,
  2968. .set_EA = smb2_set_ea,
  2969. #endif /* CIFS_XATTR */
  2970. #ifdef CONFIG_CIFS_ACL
  2971. .get_acl = get_smb2_acl,
  2972. .get_acl_by_fid = get_smb2_acl_by_fid,
  2973. .set_acl = set_smb2_acl,
  2974. #endif /* CIFS_ACL */
  2975. .next_header = smb2_next_header,
  2976. };
  2977. struct smb_version_operations smb21_operations = {
  2978. .compare_fids = smb2_compare_fids,
  2979. .setup_request = smb2_setup_request,
  2980. .setup_async_request = smb2_setup_async_request,
  2981. .check_receive = smb2_check_receive,
  2982. .add_credits = smb2_add_credits,
  2983. .set_credits = smb2_set_credits,
  2984. .get_credits_field = smb2_get_credits_field,
  2985. .get_credits = smb2_get_credits,
  2986. .wait_mtu_credits = smb2_wait_mtu_credits,
  2987. .get_next_mid = smb2_get_next_mid,
  2988. .revert_current_mid = smb2_revert_current_mid,
  2989. .read_data_offset = smb2_read_data_offset,
  2990. .read_data_length = smb2_read_data_length,
  2991. .map_error = map_smb2_to_linux_error,
  2992. .find_mid = smb2_find_mid,
  2993. .check_message = smb2_check_message,
  2994. .dump_detail = smb2_dump_detail,
  2995. .clear_stats = smb2_clear_stats,
  2996. .print_stats = smb2_print_stats,
  2997. .is_oplock_break = smb2_is_valid_oplock_break,
  2998. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  2999. .downgrade_oplock = smb2_downgrade_oplock,
  3000. .need_neg = smb2_need_neg,
  3001. .negotiate = smb2_negotiate,
  3002. .negotiate_wsize = smb2_negotiate_wsize,
  3003. .negotiate_rsize = smb2_negotiate_rsize,
  3004. .sess_setup = SMB2_sess_setup,
  3005. .logoff = SMB2_logoff,
  3006. .tree_connect = SMB2_tcon,
  3007. .tree_disconnect = SMB2_tdis,
  3008. .qfs_tcon = smb2_qfs_tcon,
  3009. .is_path_accessible = smb2_is_path_accessible,
  3010. .can_echo = smb2_can_echo,
  3011. .echo = SMB2_echo,
  3012. .query_path_info = smb2_query_path_info,
  3013. .get_srv_inum = smb2_get_srv_inum,
  3014. .query_file_info = smb2_query_file_info,
  3015. .set_path_size = smb2_set_path_size,
  3016. .set_file_size = smb2_set_file_size,
  3017. .set_file_info = smb2_set_file_info,
  3018. .set_compression = smb2_set_compression,
  3019. .mkdir = smb2_mkdir,
  3020. .mkdir_setinfo = smb2_mkdir_setinfo,
  3021. .rmdir = smb2_rmdir,
  3022. .unlink = smb2_unlink,
  3023. .rename = smb2_rename_path,
  3024. .create_hardlink = smb2_create_hardlink,
  3025. .query_symlink = smb2_query_symlink,
  3026. .query_mf_symlink = smb3_query_mf_symlink,
  3027. .create_mf_symlink = smb3_create_mf_symlink,
  3028. .open = smb2_open_file,
  3029. .set_fid = smb2_set_fid,
  3030. .close = smb2_close_file,
  3031. .flush = smb2_flush_file,
  3032. .async_readv = smb2_async_readv,
  3033. .async_writev = smb2_async_writev,
  3034. .sync_read = smb2_sync_read,
  3035. .sync_write = smb2_sync_write,
  3036. .query_dir_first = smb2_query_dir_first,
  3037. .query_dir_next = smb2_query_dir_next,
  3038. .close_dir = smb2_close_dir,
  3039. .calc_smb_size = smb2_calc_size,
  3040. .is_status_pending = smb2_is_status_pending,
  3041. .is_session_expired = smb2_is_session_expired,
  3042. .oplock_response = smb2_oplock_response,
  3043. .queryfs = smb2_queryfs,
  3044. .mand_lock = smb2_mand_lock,
  3045. .mand_unlock_range = smb2_unlock_range,
  3046. .push_mand_locks = smb2_push_mandatory_locks,
  3047. .get_lease_key = smb2_get_lease_key,
  3048. .set_lease_key = smb2_set_lease_key,
  3049. .new_lease_key = smb2_new_lease_key,
  3050. .calc_signature = smb2_calc_signature,
  3051. .is_read_op = smb21_is_read_op,
  3052. .set_oplock_level = smb21_set_oplock_level,
  3053. .create_lease_buf = smb2_create_lease_buf,
  3054. .parse_lease_buf = smb2_parse_lease_buf,
  3055. .copychunk_range = smb2_copychunk_range,
  3056. .wp_retry_size = smb2_wp_retry_size,
  3057. .dir_needs_close = smb2_dir_needs_close,
  3058. .enum_snapshots = smb3_enum_snapshots,
  3059. .get_dfs_refer = smb2_get_dfs_refer,
  3060. .select_sectype = smb2_select_sectype,
  3061. #ifdef CONFIG_CIFS_XATTR
  3062. .query_all_EAs = smb2_query_eas,
  3063. .set_EA = smb2_set_ea,
  3064. #endif /* CIFS_XATTR */
  3065. #ifdef CONFIG_CIFS_ACL
  3066. .get_acl = get_smb2_acl,
  3067. .get_acl_by_fid = get_smb2_acl_by_fid,
  3068. .set_acl = set_smb2_acl,
  3069. #endif /* CIFS_ACL */
  3070. .next_header = smb2_next_header,
  3071. };
  3072. struct smb_version_operations smb30_operations = {
  3073. .compare_fids = smb2_compare_fids,
  3074. .setup_request = smb2_setup_request,
  3075. .setup_async_request = smb2_setup_async_request,
  3076. .check_receive = smb2_check_receive,
  3077. .add_credits = smb2_add_credits,
  3078. .set_credits = smb2_set_credits,
  3079. .get_credits_field = smb2_get_credits_field,
  3080. .get_credits = smb2_get_credits,
  3081. .wait_mtu_credits = smb2_wait_mtu_credits,
  3082. .get_next_mid = smb2_get_next_mid,
  3083. .revert_current_mid = smb2_revert_current_mid,
  3084. .read_data_offset = smb2_read_data_offset,
  3085. .read_data_length = smb2_read_data_length,
  3086. .map_error = map_smb2_to_linux_error,
  3087. .find_mid = smb2_find_mid,
  3088. .check_message = smb2_check_message,
  3089. .dump_detail = smb2_dump_detail,
  3090. .clear_stats = smb2_clear_stats,
  3091. .print_stats = smb2_print_stats,
  3092. .dump_share_caps = smb2_dump_share_caps,
  3093. .is_oplock_break = smb2_is_valid_oplock_break,
  3094. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  3095. .downgrade_oplock = smb3_downgrade_oplock,
  3096. .need_neg = smb2_need_neg,
  3097. .negotiate = smb2_negotiate,
  3098. .negotiate_wsize = smb2_negotiate_wsize,
  3099. .negotiate_rsize = smb2_negotiate_rsize,
  3100. .sess_setup = SMB2_sess_setup,
  3101. .logoff = SMB2_logoff,
  3102. .tree_connect = SMB2_tcon,
  3103. .tree_disconnect = SMB2_tdis,
  3104. .qfs_tcon = smb3_qfs_tcon,
  3105. .is_path_accessible = smb2_is_path_accessible,
  3106. .can_echo = smb2_can_echo,
  3107. .echo = SMB2_echo,
  3108. .query_path_info = smb2_query_path_info,
  3109. .get_srv_inum = smb2_get_srv_inum,
  3110. .query_file_info = smb2_query_file_info,
  3111. .set_path_size = smb2_set_path_size,
  3112. .set_file_size = smb2_set_file_size,
  3113. .set_file_info = smb2_set_file_info,
  3114. .set_compression = smb2_set_compression,
  3115. .mkdir = smb2_mkdir,
  3116. .mkdir_setinfo = smb2_mkdir_setinfo,
  3117. .rmdir = smb2_rmdir,
  3118. .unlink = smb2_unlink,
  3119. .rename = smb2_rename_path,
  3120. .create_hardlink = smb2_create_hardlink,
  3121. .query_symlink = smb2_query_symlink,
  3122. .query_mf_symlink = smb3_query_mf_symlink,
  3123. .create_mf_symlink = smb3_create_mf_symlink,
  3124. .open = smb2_open_file,
  3125. .set_fid = smb2_set_fid,
  3126. .close = smb2_close_file,
  3127. .flush = smb2_flush_file,
  3128. .async_readv = smb2_async_readv,
  3129. .async_writev = smb2_async_writev,
  3130. .sync_read = smb2_sync_read,
  3131. .sync_write = smb2_sync_write,
  3132. .query_dir_first = smb2_query_dir_first,
  3133. .query_dir_next = smb2_query_dir_next,
  3134. .close_dir = smb2_close_dir,
  3135. .calc_smb_size = smb2_calc_size,
  3136. .is_status_pending = smb2_is_status_pending,
  3137. .is_session_expired = smb2_is_session_expired,
  3138. .oplock_response = smb2_oplock_response,
  3139. .queryfs = smb2_queryfs,
  3140. .mand_lock = smb2_mand_lock,
  3141. .mand_unlock_range = smb2_unlock_range,
  3142. .push_mand_locks = smb2_push_mandatory_locks,
  3143. .get_lease_key = smb2_get_lease_key,
  3144. .set_lease_key = smb2_set_lease_key,
  3145. .new_lease_key = smb2_new_lease_key,
  3146. .generate_signingkey = generate_smb30signingkey,
  3147. .calc_signature = smb3_calc_signature,
  3148. .set_integrity = smb3_set_integrity,
  3149. .is_read_op = smb21_is_read_op,
  3150. .set_oplock_level = smb3_set_oplock_level,
  3151. .create_lease_buf = smb3_create_lease_buf,
  3152. .parse_lease_buf = smb3_parse_lease_buf,
  3153. .copychunk_range = smb2_copychunk_range,
  3154. .duplicate_extents = smb2_duplicate_extents,
  3155. .validate_negotiate = smb3_validate_negotiate,
  3156. .wp_retry_size = smb2_wp_retry_size,
  3157. .dir_needs_close = smb2_dir_needs_close,
  3158. .fallocate = smb3_fallocate,
  3159. .enum_snapshots = smb3_enum_snapshots,
  3160. .init_transform_rq = smb3_init_transform_rq,
  3161. .is_transform_hdr = smb3_is_transform_hdr,
  3162. .receive_transform = smb3_receive_transform,
  3163. .get_dfs_refer = smb2_get_dfs_refer,
  3164. .select_sectype = smb2_select_sectype,
  3165. #ifdef CONFIG_CIFS_XATTR
  3166. .query_all_EAs = smb2_query_eas,
  3167. .set_EA = smb2_set_ea,
  3168. #endif /* CIFS_XATTR */
  3169. #ifdef CONFIG_CIFS_ACL
  3170. .get_acl = get_smb2_acl,
  3171. .get_acl_by_fid = get_smb2_acl_by_fid,
  3172. .set_acl = set_smb2_acl,
  3173. #endif /* CIFS_ACL */
  3174. .next_header = smb2_next_header,
  3175. };
  3176. struct smb_version_operations smb311_operations = {
  3177. .compare_fids = smb2_compare_fids,
  3178. .setup_request = smb2_setup_request,
  3179. .setup_async_request = smb2_setup_async_request,
  3180. .check_receive = smb2_check_receive,
  3181. .add_credits = smb2_add_credits,
  3182. .set_credits = smb2_set_credits,
  3183. .get_credits_field = smb2_get_credits_field,
  3184. .get_credits = smb2_get_credits,
  3185. .wait_mtu_credits = smb2_wait_mtu_credits,
  3186. .get_next_mid = smb2_get_next_mid,
  3187. .revert_current_mid = smb2_revert_current_mid,
  3188. .read_data_offset = smb2_read_data_offset,
  3189. .read_data_length = smb2_read_data_length,
  3190. .map_error = map_smb2_to_linux_error,
  3191. .find_mid = smb2_find_mid,
  3192. .check_message = smb2_check_message,
  3193. .dump_detail = smb2_dump_detail,
  3194. .clear_stats = smb2_clear_stats,
  3195. .print_stats = smb2_print_stats,
  3196. .dump_share_caps = smb2_dump_share_caps,
  3197. .is_oplock_break = smb2_is_valid_oplock_break,
  3198. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  3199. .downgrade_oplock = smb3_downgrade_oplock,
  3200. .need_neg = smb2_need_neg,
  3201. .negotiate = smb2_negotiate,
  3202. .negotiate_wsize = smb2_negotiate_wsize,
  3203. .negotiate_rsize = smb2_negotiate_rsize,
  3204. .sess_setup = SMB2_sess_setup,
  3205. .logoff = SMB2_logoff,
  3206. .tree_connect = SMB2_tcon,
  3207. .tree_disconnect = SMB2_tdis,
  3208. .qfs_tcon = smb3_qfs_tcon,
  3209. .is_path_accessible = smb2_is_path_accessible,
  3210. .can_echo = smb2_can_echo,
  3211. .echo = SMB2_echo,
  3212. .query_path_info = smb2_query_path_info,
  3213. .get_srv_inum = smb2_get_srv_inum,
  3214. .query_file_info = smb2_query_file_info,
  3215. .set_path_size = smb2_set_path_size,
  3216. .set_file_size = smb2_set_file_size,
  3217. .set_file_info = smb2_set_file_info,
  3218. .set_compression = smb2_set_compression,
  3219. .mkdir = smb2_mkdir,
  3220. .mkdir_setinfo = smb2_mkdir_setinfo,
  3221. .posix_mkdir = smb311_posix_mkdir,
  3222. .rmdir = smb2_rmdir,
  3223. .unlink = smb2_unlink,
  3224. .rename = smb2_rename_path,
  3225. .create_hardlink = smb2_create_hardlink,
  3226. .query_symlink = smb2_query_symlink,
  3227. .query_mf_symlink = smb3_query_mf_symlink,
  3228. .create_mf_symlink = smb3_create_mf_symlink,
  3229. .open = smb2_open_file,
  3230. .set_fid = smb2_set_fid,
  3231. .close = smb2_close_file,
  3232. .flush = smb2_flush_file,
  3233. .async_readv = smb2_async_readv,
  3234. .async_writev = smb2_async_writev,
  3235. .sync_read = smb2_sync_read,
  3236. .sync_write = smb2_sync_write,
  3237. .query_dir_first = smb2_query_dir_first,
  3238. .query_dir_next = smb2_query_dir_next,
  3239. .close_dir = smb2_close_dir,
  3240. .calc_smb_size = smb2_calc_size,
  3241. .is_status_pending = smb2_is_status_pending,
  3242. .is_session_expired = smb2_is_session_expired,
  3243. .oplock_response = smb2_oplock_response,
  3244. .queryfs = smb311_queryfs,
  3245. .mand_lock = smb2_mand_lock,
  3246. .mand_unlock_range = smb2_unlock_range,
  3247. .push_mand_locks = smb2_push_mandatory_locks,
  3248. .get_lease_key = smb2_get_lease_key,
  3249. .set_lease_key = smb2_set_lease_key,
  3250. .new_lease_key = smb2_new_lease_key,
  3251. .generate_signingkey = generate_smb311signingkey,
  3252. .calc_signature = smb3_calc_signature,
  3253. .set_integrity = smb3_set_integrity,
  3254. .is_read_op = smb21_is_read_op,
  3255. .set_oplock_level = smb3_set_oplock_level,
  3256. .create_lease_buf = smb3_create_lease_buf,
  3257. .parse_lease_buf = smb3_parse_lease_buf,
  3258. .copychunk_range = smb2_copychunk_range,
  3259. .duplicate_extents = smb2_duplicate_extents,
  3260. /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
  3261. .wp_retry_size = smb2_wp_retry_size,
  3262. .dir_needs_close = smb2_dir_needs_close,
  3263. .fallocate = smb3_fallocate,
  3264. .enum_snapshots = smb3_enum_snapshots,
  3265. .init_transform_rq = smb3_init_transform_rq,
  3266. .is_transform_hdr = smb3_is_transform_hdr,
  3267. .receive_transform = smb3_receive_transform,
  3268. .get_dfs_refer = smb2_get_dfs_refer,
  3269. .select_sectype = smb2_select_sectype,
  3270. #ifdef CONFIG_CIFS_XATTR
  3271. .query_all_EAs = smb2_query_eas,
  3272. .set_EA = smb2_set_ea,
  3273. #endif /* CIFS_XATTR */
  3274. #ifdef CONFIG_CIFS_ACL
  3275. .get_acl = get_smb2_acl,
  3276. .get_acl_by_fid = get_smb2_acl_by_fid,
  3277. .set_acl = set_smb2_acl,
  3278. #endif /* CIFS_ACL */
  3279. .next_header = smb2_next_header,
  3280. };
  3281. struct smb_version_values smb20_values = {
  3282. .version_string = SMB20_VERSION_STRING,
  3283. .protocol_id = SMB20_PROT_ID,
  3284. .req_capabilities = 0, /* MBZ */
  3285. .large_lock_type = 0,
  3286. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  3287. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  3288. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  3289. .header_size = sizeof(struct smb2_sync_hdr),
  3290. .header_preamble_size = 0,
  3291. .max_header_size = MAX_SMB2_HDR_SIZE,
  3292. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  3293. .lock_cmd = SMB2_LOCK,
  3294. .cap_unix = 0,
  3295. .cap_nt_find = SMB2_NT_FIND,
  3296. .cap_large_files = SMB2_LARGE_FILES,
  3297. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3298. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3299. .create_lease_size = sizeof(struct create_lease),
  3300. };
  3301. struct smb_version_values smb21_values = {
  3302. .version_string = SMB21_VERSION_STRING,
  3303. .protocol_id = SMB21_PROT_ID,
  3304. .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
  3305. .large_lock_type = 0,
  3306. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  3307. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  3308. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  3309. .header_size = sizeof(struct smb2_sync_hdr),
  3310. .header_preamble_size = 0,
  3311. .max_header_size = MAX_SMB2_HDR_SIZE,
  3312. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  3313. .lock_cmd = SMB2_LOCK,
  3314. .cap_unix = 0,
  3315. .cap_nt_find = SMB2_NT_FIND,
  3316. .cap_large_files = SMB2_LARGE_FILES,
  3317. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3318. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3319. .create_lease_size = sizeof(struct create_lease),
  3320. };
  3321. struct smb_version_values smb3any_values = {
  3322. .version_string = SMB3ANY_VERSION_STRING,
  3323. .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
  3324. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  3325. .large_lock_type = 0,
  3326. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  3327. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  3328. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  3329. .header_size = sizeof(struct smb2_sync_hdr),
  3330. .header_preamble_size = 0,
  3331. .max_header_size = MAX_SMB2_HDR_SIZE,
  3332. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  3333. .lock_cmd = SMB2_LOCK,
  3334. .cap_unix = 0,
  3335. .cap_nt_find = SMB2_NT_FIND,
  3336. .cap_large_files = SMB2_LARGE_FILES,
  3337. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3338. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3339. .create_lease_size = sizeof(struct create_lease_v2),
  3340. };
  3341. struct smb_version_values smbdefault_values = {
  3342. .version_string = SMBDEFAULT_VERSION_STRING,
  3343. .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
  3344. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  3345. .large_lock_type = 0,
  3346. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  3347. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  3348. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  3349. .header_size = sizeof(struct smb2_sync_hdr),
  3350. .header_preamble_size = 0,
  3351. .max_header_size = MAX_SMB2_HDR_SIZE,
  3352. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  3353. .lock_cmd = SMB2_LOCK,
  3354. .cap_unix = 0,
  3355. .cap_nt_find = SMB2_NT_FIND,
  3356. .cap_large_files = SMB2_LARGE_FILES,
  3357. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3358. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3359. .create_lease_size = sizeof(struct create_lease_v2),
  3360. };
  3361. struct smb_version_values smb30_values = {
  3362. .version_string = SMB30_VERSION_STRING,
  3363. .protocol_id = SMB30_PROT_ID,
  3364. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  3365. .large_lock_type = 0,
  3366. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  3367. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  3368. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  3369. .header_size = sizeof(struct smb2_sync_hdr),
  3370. .header_preamble_size = 0,
  3371. .max_header_size = MAX_SMB2_HDR_SIZE,
  3372. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  3373. .lock_cmd = SMB2_LOCK,
  3374. .cap_unix = 0,
  3375. .cap_nt_find = SMB2_NT_FIND,
  3376. .cap_large_files = SMB2_LARGE_FILES,
  3377. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3378. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3379. .create_lease_size = sizeof(struct create_lease_v2),
  3380. };
  3381. struct smb_version_values smb302_values = {
  3382. .version_string = SMB302_VERSION_STRING,
  3383. .protocol_id = SMB302_PROT_ID,
  3384. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  3385. .large_lock_type = 0,
  3386. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  3387. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  3388. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  3389. .header_size = sizeof(struct smb2_sync_hdr),
  3390. .header_preamble_size = 0,
  3391. .max_header_size = MAX_SMB2_HDR_SIZE,
  3392. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  3393. .lock_cmd = SMB2_LOCK,
  3394. .cap_unix = 0,
  3395. .cap_nt_find = SMB2_NT_FIND,
  3396. .cap_large_files = SMB2_LARGE_FILES,
  3397. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3398. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3399. .create_lease_size = sizeof(struct create_lease_v2),
  3400. };
  3401. struct smb_version_values smb311_values = {
  3402. .version_string = SMB311_VERSION_STRING,
  3403. .protocol_id = SMB311_PROT_ID,
  3404. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  3405. .large_lock_type = 0,
  3406. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  3407. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  3408. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  3409. .header_size = sizeof(struct smb2_sync_hdr),
  3410. .header_preamble_size = 0,
  3411. .max_header_size = MAX_SMB2_HDR_SIZE,
  3412. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  3413. .lock_cmd = SMB2_LOCK,
  3414. .cap_unix = 0,
  3415. .cap_nt_find = SMB2_NT_FIND,
  3416. .cap_large_files = SMB2_LARGE_FILES,
  3417. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3418. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  3419. .create_lease_size = sizeof(struct create_lease_v2),
  3420. };