connect.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904
  1. /*
  2. * fs/cifs/connect.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2009
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/net.h>
  23. #include <linux/string.h>
  24. #include <linux/list.h>
  25. #include <linux/wait.h>
  26. #include <linux/slab.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/ctype.h>
  29. #include <linux/utsname.h>
  30. #include <linux/mempool.h>
  31. #include <linux/delay.h>
  32. #include <linux/completion.h>
  33. #include <linux/kthread.h>
  34. #include <linux/pagevec.h>
  35. #include <linux/freezer.h>
  36. #include <linux/namei.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/processor.h>
  39. #include <linux/inet.h>
  40. #include <linux/module.h>
  41. #include <net/ipv6.h>
  42. #include "cifspdu.h"
  43. #include "cifsglob.h"
  44. #include "cifsproto.h"
  45. #include "cifs_unicode.h"
  46. #include "cifs_debug.h"
  47. #include "cifs_fs_sb.h"
  48. #include "ntlmssp.h"
  49. #include "nterr.h"
  50. #include "rfc1002pdu.h"
  51. #include "fscache.h"
  52. #define CIFS_PORT 445
  53. #define RFC1001_PORT 139
  54. /* SMB echo "timeout" -- FIXME: tunable? */
  55. #define SMB_ECHO_INTERVAL (60 * HZ)
  56. extern mempool_t *cifs_req_poolp;
  57. /* FIXME: should these be tunable? */
  58. #define TLINK_ERROR_EXPIRE (1 * HZ)
  59. #define TLINK_IDLE_EXPIRE (600 * HZ)
  60. static int ip_connect(struct TCP_Server_Info *server);
  61. static int generic_ip_connect(struct TCP_Server_Info *server);
  62. static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
  63. static void cifs_prune_tlinks(struct work_struct *work);
  64. static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
  65. const char *devname);
  66. /*
  67. * cifs tcp session reconnection
  68. *
  69. * mark tcp session as reconnecting so temporarily locked
  70. * mark all smb sessions as reconnecting for tcp session
  71. * reconnect tcp session
  72. * wake up waiters on reconnection? - (not needed currently)
  73. */
  74. static int
  75. cifs_reconnect(struct TCP_Server_Info *server)
  76. {
  77. int rc = 0;
  78. struct list_head *tmp, *tmp2;
  79. struct cifs_ses *ses;
  80. struct cifs_tcon *tcon;
  81. struct mid_q_entry *mid_entry;
  82. struct list_head retry_list;
  83. spin_lock(&GlobalMid_Lock);
  84. if (server->tcpStatus == CifsExiting) {
  85. /* the demux thread will exit normally
  86. next time through the loop */
  87. spin_unlock(&GlobalMid_Lock);
  88. return rc;
  89. } else
  90. server->tcpStatus = CifsNeedReconnect;
  91. spin_unlock(&GlobalMid_Lock);
  92. server->maxBuf = 0;
  93. cFYI(1, "Reconnecting tcp session");
  94. /* before reconnecting the tcp session, mark the smb session (uid)
  95. and the tid bad so they are not used until reconnected */
  96. cFYI(1, "%s: marking sessions and tcons for reconnect", __func__);
  97. spin_lock(&cifs_tcp_ses_lock);
  98. list_for_each(tmp, &server->smb_ses_list) {
  99. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  100. ses->need_reconnect = true;
  101. ses->ipc_tid = 0;
  102. list_for_each(tmp2, &ses->tcon_list) {
  103. tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
  104. tcon->need_reconnect = true;
  105. }
  106. }
  107. spin_unlock(&cifs_tcp_ses_lock);
  108. /* do not want to be sending data on a socket we are freeing */
  109. cFYI(1, "%s: tearing down socket", __func__);
  110. mutex_lock(&server->srv_mutex);
  111. if (server->ssocket) {
  112. cFYI(1, "State: 0x%x Flags: 0x%lx", server->ssocket->state,
  113. server->ssocket->flags);
  114. kernel_sock_shutdown(server->ssocket, SHUT_WR);
  115. cFYI(1, "Post shutdown state: 0x%x Flags: 0x%lx",
  116. server->ssocket->state,
  117. server->ssocket->flags);
  118. sock_release(server->ssocket);
  119. server->ssocket = NULL;
  120. }
  121. server->sequence_number = 0;
  122. server->session_estab = false;
  123. kfree(server->session_key.response);
  124. server->session_key.response = NULL;
  125. server->session_key.len = 0;
  126. server->lstrp = jiffies;
  127. mutex_unlock(&server->srv_mutex);
  128. /* mark submitted MIDs for retry and issue callback */
  129. INIT_LIST_HEAD(&retry_list);
  130. cFYI(1, "%s: moving mids to private list", __func__);
  131. spin_lock(&GlobalMid_Lock);
  132. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  133. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  134. if (mid_entry->midState == MID_REQUEST_SUBMITTED)
  135. mid_entry->midState = MID_RETRY_NEEDED;
  136. list_move(&mid_entry->qhead, &retry_list);
  137. }
  138. spin_unlock(&GlobalMid_Lock);
  139. cFYI(1, "%s: issuing mid callbacks", __func__);
  140. list_for_each_safe(tmp, tmp2, &retry_list) {
  141. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  142. list_del_init(&mid_entry->qhead);
  143. mid_entry->callback(mid_entry);
  144. }
  145. do {
  146. try_to_freeze();
  147. /* we should try only the port we connected to before */
  148. rc = generic_ip_connect(server);
  149. if (rc) {
  150. cFYI(1, "reconnect error %d", rc);
  151. msleep(3000);
  152. } else {
  153. atomic_inc(&tcpSesReconnectCount);
  154. spin_lock(&GlobalMid_Lock);
  155. if (server->tcpStatus != CifsExiting)
  156. server->tcpStatus = CifsNeedNegotiate;
  157. spin_unlock(&GlobalMid_Lock);
  158. }
  159. } while (server->tcpStatus == CifsNeedReconnect);
  160. return rc;
  161. }
  162. /*
  163. return codes:
  164. 0 not a transact2, or all data present
  165. >0 transact2 with that much data missing
  166. -EINVAL = invalid transact2
  167. */
  168. static int check2ndT2(struct smb_hdr *pSMB)
  169. {
  170. struct smb_t2_rsp *pSMBt;
  171. int remaining;
  172. __u16 total_data_size, data_in_this_rsp;
  173. if (pSMB->Command != SMB_COM_TRANSACTION2)
  174. return 0;
  175. /* check for plausible wct, bcc and t2 data and parm sizes */
  176. /* check for parm and data offset going beyond end of smb */
  177. if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
  178. cFYI(1, "invalid transact2 word count");
  179. return -EINVAL;
  180. }
  181. pSMBt = (struct smb_t2_rsp *)pSMB;
  182. total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  183. data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  184. if (total_data_size == data_in_this_rsp)
  185. return 0;
  186. else if (total_data_size < data_in_this_rsp) {
  187. cFYI(1, "total data %d smaller than data in frame %d",
  188. total_data_size, data_in_this_rsp);
  189. return -EINVAL;
  190. }
  191. remaining = total_data_size - data_in_this_rsp;
  192. cFYI(1, "missing %d bytes from transact2, check next response",
  193. remaining);
  194. if (total_data_size > CIFSMaxBufSize) {
  195. cERROR(1, "TotalDataSize %d is over maximum buffer %d",
  196. total_data_size, CIFSMaxBufSize);
  197. return -EINVAL;
  198. }
  199. return remaining;
  200. }
  201. static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
  202. {
  203. struct smb_t2_rsp *pSMB2 = (struct smb_t2_rsp *)psecond;
  204. struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)pTargetSMB;
  205. char *data_area_of_target;
  206. char *data_area_of_buf2;
  207. int remaining;
  208. unsigned int byte_count, total_in_buf;
  209. __u16 total_data_size, total_in_buf2;
  210. total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  211. if (total_data_size !=
  212. get_unaligned_le16(&pSMB2->t2_rsp.TotalDataCount))
  213. cFYI(1, "total data size of primary and secondary t2 differ");
  214. total_in_buf = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  215. remaining = total_data_size - total_in_buf;
  216. if (remaining < 0)
  217. return -EPROTO;
  218. if (remaining == 0) /* nothing to do, ignore */
  219. return 0;
  220. total_in_buf2 = get_unaligned_le16(&pSMB2->t2_rsp.DataCount);
  221. if (remaining < total_in_buf2) {
  222. cFYI(1, "transact2 2nd response contains too much data");
  223. }
  224. /* find end of first SMB data area */
  225. data_area_of_target = (char *)&pSMBt->hdr.Protocol +
  226. get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
  227. /* validate target area */
  228. data_area_of_buf2 = (char *)&pSMB2->hdr.Protocol +
  229. get_unaligned_le16(&pSMB2->t2_rsp.DataOffset);
  230. data_area_of_target += total_in_buf;
  231. /* copy second buffer into end of first buffer */
  232. total_in_buf += total_in_buf2;
  233. /* is the result too big for the field? */
  234. if (total_in_buf > USHRT_MAX)
  235. return -EPROTO;
  236. put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
  237. /* fix up the BCC */
  238. byte_count = get_bcc(pTargetSMB);
  239. byte_count += total_in_buf2;
  240. /* is the result too big for the field? */
  241. if (byte_count > USHRT_MAX)
  242. return -EPROTO;
  243. put_bcc(byte_count, pTargetSMB);
  244. byte_count = be32_to_cpu(pTargetSMB->smb_buf_length);
  245. byte_count += total_in_buf2;
  246. /* don't allow buffer to overflow */
  247. if (byte_count > CIFSMaxBufSize)
  248. return -ENOBUFS;
  249. pTargetSMB->smb_buf_length = cpu_to_be32(byte_count);
  250. memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
  251. if (remaining == total_in_buf2) {
  252. cFYI(1, "found the last secondary response");
  253. return 0; /* we are done */
  254. } else /* more responses to go */
  255. return 1;
  256. }
  257. static void
  258. cifs_echo_request(struct work_struct *work)
  259. {
  260. int rc;
  261. struct TCP_Server_Info *server = container_of(work,
  262. struct TCP_Server_Info, echo.work);
  263. /*
  264. * We cannot send an echo until the NEGOTIATE_PROTOCOL request is
  265. * done, which is indicated by maxBuf != 0. Also, no need to ping if
  266. * we got a response recently
  267. */
  268. if (server->maxBuf == 0 ||
  269. time_before(jiffies, server->lstrp + SMB_ECHO_INTERVAL - HZ))
  270. goto requeue_echo;
  271. rc = CIFSSMBEcho(server);
  272. if (rc)
  273. cFYI(1, "Unable to send echo request to server: %s",
  274. server->hostname);
  275. requeue_echo:
  276. queue_delayed_work(system_nrt_wq, &server->echo, SMB_ECHO_INTERVAL);
  277. }
  278. static bool
  279. allocate_buffers(struct TCP_Server_Info *server)
  280. {
  281. if (!server->bigbuf) {
  282. server->bigbuf = (char *)cifs_buf_get();
  283. if (!server->bigbuf) {
  284. cERROR(1, "No memory for large SMB response");
  285. msleep(3000);
  286. /* retry will check if exiting */
  287. return false;
  288. }
  289. } else if (server->large_buf) {
  290. /* we are reusing a dirty large buf, clear its start */
  291. memset(server->bigbuf, 0, sizeof(struct smb_hdr));
  292. }
  293. if (!server->smallbuf) {
  294. server->smallbuf = (char *)cifs_small_buf_get();
  295. if (!server->smallbuf) {
  296. cERROR(1, "No memory for SMB response");
  297. msleep(1000);
  298. /* retry will check if exiting */
  299. return false;
  300. }
  301. /* beginning of smb buffer is cleared in our buf_get */
  302. } else {
  303. /* if existing small buf clear beginning */
  304. memset(server->smallbuf, 0, sizeof(struct smb_hdr));
  305. }
  306. return true;
  307. }
  308. static bool
  309. server_unresponsive(struct TCP_Server_Info *server)
  310. {
  311. if (echo_retries > 0 && server->tcpStatus == CifsGood &&
  312. time_after(jiffies, server->lstrp +
  313. (echo_retries * SMB_ECHO_INTERVAL))) {
  314. cERROR(1, "Server %s has not responded in %d seconds. "
  315. "Reconnecting...", server->hostname,
  316. (echo_retries * SMB_ECHO_INTERVAL / HZ));
  317. cifs_reconnect(server);
  318. wake_up(&server->response_q);
  319. return true;
  320. }
  321. return false;
  322. }
  323. /*
  324. * kvec_array_init - clone a kvec array, and advance into it
  325. * @new: pointer to memory for cloned array
  326. * @iov: pointer to original array
  327. * @nr_segs: number of members in original array
  328. * @bytes: number of bytes to advance into the cloned array
  329. *
  330. * This function will copy the array provided in iov to a section of memory
  331. * and advance the specified number of bytes into the new array. It returns
  332. * the number of segments in the new array. "new" must be at least as big as
  333. * the original iov array.
  334. */
  335. static unsigned int
  336. kvec_array_init(struct kvec *new, struct kvec *iov, unsigned int nr_segs,
  337. size_t bytes)
  338. {
  339. size_t base = 0;
  340. while (bytes || !iov->iov_len) {
  341. int copy = min(bytes, iov->iov_len);
  342. bytes -= copy;
  343. base += copy;
  344. if (iov->iov_len == base) {
  345. iov++;
  346. nr_segs--;
  347. base = 0;
  348. }
  349. }
  350. memcpy(new, iov, sizeof(*iov) * nr_segs);
  351. new->iov_base += base;
  352. new->iov_len -= base;
  353. return nr_segs;
  354. }
  355. static struct kvec *
  356. get_server_iovec(struct TCP_Server_Info *server, unsigned int nr_segs)
  357. {
  358. struct kvec *new_iov;
  359. if (server->iov && nr_segs <= server->nr_iov)
  360. return server->iov;
  361. /* not big enough -- allocate a new one and release the old */
  362. new_iov = kmalloc(sizeof(*new_iov) * nr_segs, GFP_NOFS);
  363. if (new_iov) {
  364. kfree(server->iov);
  365. server->iov = new_iov;
  366. server->nr_iov = nr_segs;
  367. }
  368. return new_iov;
  369. }
  370. int
  371. cifs_readv_from_socket(struct TCP_Server_Info *server, struct kvec *iov_orig,
  372. unsigned int nr_segs, unsigned int to_read)
  373. {
  374. int length = 0;
  375. int total_read;
  376. unsigned int segs;
  377. struct msghdr smb_msg;
  378. struct kvec *iov;
  379. iov = get_server_iovec(server, nr_segs);
  380. if (!iov)
  381. return -ENOMEM;
  382. smb_msg.msg_control = NULL;
  383. smb_msg.msg_controllen = 0;
  384. for (total_read = 0; to_read; total_read += length, to_read -= length) {
  385. if (server_unresponsive(server)) {
  386. total_read = -EAGAIN;
  387. break;
  388. }
  389. segs = kvec_array_init(iov, iov_orig, nr_segs, total_read);
  390. length = kernel_recvmsg(server->ssocket, &smb_msg,
  391. iov, segs, to_read, 0);
  392. if (server->tcpStatus == CifsExiting) {
  393. total_read = -ESHUTDOWN;
  394. break;
  395. } else if (server->tcpStatus == CifsNeedReconnect) {
  396. cifs_reconnect(server);
  397. total_read = -EAGAIN;
  398. break;
  399. } else if (length == -ERESTARTSYS ||
  400. length == -EAGAIN ||
  401. length == -EINTR) {
  402. /*
  403. * Minimum sleep to prevent looping, allowing socket
  404. * to clear and app threads to set tcpStatus
  405. * CifsNeedReconnect if server hung.
  406. */
  407. usleep_range(1000, 2000);
  408. length = 0;
  409. continue;
  410. } else if (length <= 0) {
  411. cFYI(1, "Received no data or error: expecting %d "
  412. "got %d", to_read, length);
  413. cifs_reconnect(server);
  414. total_read = -EAGAIN;
  415. break;
  416. }
  417. }
  418. return total_read;
  419. }
  420. int
  421. cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
  422. unsigned int to_read)
  423. {
  424. struct kvec iov;
  425. iov.iov_base = buf;
  426. iov.iov_len = to_read;
  427. return cifs_readv_from_socket(server, &iov, 1, to_read);
  428. }
  429. static bool
  430. is_smb_response(struct TCP_Server_Info *server, unsigned char type)
  431. {
  432. /*
  433. * The first byte big endian of the length field,
  434. * is actually not part of the length but the type
  435. * with the most common, zero, as regular data.
  436. */
  437. switch (type) {
  438. case RFC1002_SESSION_MESSAGE:
  439. /* Regular SMB response */
  440. return true;
  441. case RFC1002_SESSION_KEEP_ALIVE:
  442. cFYI(1, "RFC 1002 session keep alive");
  443. break;
  444. case RFC1002_POSITIVE_SESSION_RESPONSE:
  445. cFYI(1, "RFC 1002 positive session response");
  446. break;
  447. case RFC1002_NEGATIVE_SESSION_RESPONSE:
  448. /*
  449. * We get this from Windows 98 instead of an error on
  450. * SMB negprot response.
  451. */
  452. cFYI(1, "RFC 1002 negative session response");
  453. /* give server a second to clean up */
  454. msleep(1000);
  455. /*
  456. * Always try 445 first on reconnect since we get NACK
  457. * on some if we ever connected to port 139 (the NACK
  458. * is since we do not begin with RFC1001 session
  459. * initialize frame).
  460. */
  461. cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
  462. cifs_reconnect(server);
  463. wake_up(&server->response_q);
  464. break;
  465. default:
  466. cERROR(1, "RFC 1002 unknown response type 0x%x", type);
  467. cifs_reconnect(server);
  468. }
  469. return false;
  470. }
  471. static struct mid_q_entry *
  472. find_mid(struct TCP_Server_Info *server, struct smb_hdr *buf)
  473. {
  474. struct mid_q_entry *mid;
  475. spin_lock(&GlobalMid_Lock);
  476. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  477. if (mid->mid == buf->Mid &&
  478. mid->midState == MID_REQUEST_SUBMITTED &&
  479. mid->command == buf->Command) {
  480. spin_unlock(&GlobalMid_Lock);
  481. return mid;
  482. }
  483. }
  484. spin_unlock(&GlobalMid_Lock);
  485. return NULL;
  486. }
  487. void
  488. dequeue_mid(struct mid_q_entry *mid, bool malformed)
  489. {
  490. #ifdef CONFIG_CIFS_STATS2
  491. mid->when_received = jiffies;
  492. #endif
  493. spin_lock(&GlobalMid_Lock);
  494. if (!malformed)
  495. mid->midState = MID_RESPONSE_RECEIVED;
  496. else
  497. mid->midState = MID_RESPONSE_MALFORMED;
  498. list_del_init(&mid->qhead);
  499. spin_unlock(&GlobalMid_Lock);
  500. }
  501. static void
  502. handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  503. struct smb_hdr *buf, int malformed)
  504. {
  505. if (malformed == 0 && check2ndT2(buf) > 0) {
  506. mid->multiRsp = true;
  507. if (mid->resp_buf) {
  508. /* merge response - fix up 1st*/
  509. malformed = coalesce_t2(buf, mid->resp_buf);
  510. if (malformed > 0)
  511. return;
  512. /* All parts received or packet is malformed. */
  513. mid->multiEnd = true;
  514. return dequeue_mid(mid, malformed);
  515. }
  516. if (!server->large_buf) {
  517. /*FIXME: switch to already allocated largebuf?*/
  518. cERROR(1, "1st trans2 resp needs bigbuf");
  519. } else {
  520. /* Have first buffer */
  521. mid->resp_buf = buf;
  522. mid->largeBuf = true;
  523. server->bigbuf = NULL;
  524. }
  525. return;
  526. }
  527. mid->resp_buf = buf;
  528. mid->largeBuf = server->large_buf;
  529. /* Was previous buf put in mpx struct for multi-rsp? */
  530. if (!mid->multiRsp) {
  531. /* smb buffer will be freed by user thread */
  532. if (server->large_buf)
  533. server->bigbuf = NULL;
  534. else
  535. server->smallbuf = NULL;
  536. }
  537. dequeue_mid(mid, malformed);
  538. }
  539. static void clean_demultiplex_info(struct TCP_Server_Info *server)
  540. {
  541. int length;
  542. /* take it off the list, if it's not already */
  543. spin_lock(&cifs_tcp_ses_lock);
  544. list_del_init(&server->tcp_ses_list);
  545. spin_unlock(&cifs_tcp_ses_lock);
  546. spin_lock(&GlobalMid_Lock);
  547. server->tcpStatus = CifsExiting;
  548. spin_unlock(&GlobalMid_Lock);
  549. wake_up_all(&server->response_q);
  550. /*
  551. * Check if we have blocked requests that need to free. Note that
  552. * cifs_max_pending is normally 50, but can be set at module install
  553. * time to as little as two.
  554. */
  555. spin_lock(&GlobalMid_Lock);
  556. if (atomic_read(&server->inFlight) >= cifs_max_pending)
  557. atomic_set(&server->inFlight, cifs_max_pending - 1);
  558. /*
  559. * We do not want to set the max_pending too low or we could end up
  560. * with the counter going negative.
  561. */
  562. spin_unlock(&GlobalMid_Lock);
  563. /*
  564. * Although there should not be any requests blocked on this queue it
  565. * can not hurt to be paranoid and try to wake up requests that may
  566. * haven been blocked when more than 50 at time were on the wire to the
  567. * same server - they now will see the session is in exit state and get
  568. * out of SendReceive.
  569. */
  570. wake_up_all(&server->request_q);
  571. /* give those requests time to exit */
  572. msleep(125);
  573. if (server->ssocket) {
  574. sock_release(server->ssocket);
  575. server->ssocket = NULL;
  576. }
  577. if (!list_empty(&server->pending_mid_q)) {
  578. struct list_head dispose_list;
  579. struct mid_q_entry *mid_entry;
  580. struct list_head *tmp, *tmp2;
  581. INIT_LIST_HEAD(&dispose_list);
  582. spin_lock(&GlobalMid_Lock);
  583. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  584. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  585. cFYI(1, "Clearing mid 0x%x", mid_entry->mid);
  586. mid_entry->midState = MID_SHUTDOWN;
  587. list_move(&mid_entry->qhead, &dispose_list);
  588. }
  589. spin_unlock(&GlobalMid_Lock);
  590. /* now walk dispose list and issue callbacks */
  591. list_for_each_safe(tmp, tmp2, &dispose_list) {
  592. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  593. cFYI(1, "Callback mid 0x%x", mid_entry->mid);
  594. list_del_init(&mid_entry->qhead);
  595. mid_entry->callback(mid_entry);
  596. }
  597. /* 1/8th of sec is more than enough time for them to exit */
  598. msleep(125);
  599. }
  600. if (!list_empty(&server->pending_mid_q)) {
  601. /*
  602. * mpx threads have not exited yet give them at least the smb
  603. * send timeout time for long ops.
  604. *
  605. * Due to delays on oplock break requests, we need to wait at
  606. * least 45 seconds before giving up on a request getting a
  607. * response and going ahead and killing cifsd.
  608. */
  609. cFYI(1, "Wait for exit from demultiplex thread");
  610. msleep(46000);
  611. /*
  612. * If threads still have not exited they are probably never
  613. * coming home not much else we can do but free the memory.
  614. */
  615. }
  616. kfree(server->hostname);
  617. kfree(server->iov);
  618. kfree(server);
  619. length = atomic_dec_return(&tcpSesAllocCount);
  620. if (length > 0)
  621. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  622. GFP_KERNEL);
  623. }
  624. static int
  625. standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  626. {
  627. int length;
  628. char *buf = server->smallbuf;
  629. struct smb_hdr *smb_buffer = (struct smb_hdr *)buf;
  630. unsigned int pdu_length = be32_to_cpu(smb_buffer->smb_buf_length);
  631. /* make sure this will fit in a large buffer */
  632. if (pdu_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  633. cERROR(1, "SMB response too long (%u bytes)",
  634. pdu_length);
  635. cifs_reconnect(server);
  636. wake_up(&server->response_q);
  637. return -EAGAIN;
  638. }
  639. /* switch to large buffer if too big for a small one */
  640. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
  641. server->large_buf = true;
  642. memcpy(server->bigbuf, server->smallbuf, server->total_read);
  643. buf = server->bigbuf;
  644. smb_buffer = (struct smb_hdr *)buf;
  645. }
  646. /* now read the rest */
  647. length = cifs_read_from_socket(server,
  648. buf + sizeof(struct smb_hdr) - 1,
  649. pdu_length - sizeof(struct smb_hdr) + 1 + 4);
  650. if (length < 0)
  651. return length;
  652. server->total_read += length;
  653. dump_smb(smb_buffer, server->total_read);
  654. /*
  655. * We know that we received enough to get to the MID as we
  656. * checked the pdu_length earlier. Now check to see
  657. * if the rest of the header is OK. We borrow the length
  658. * var for the rest of the loop to avoid a new stack var.
  659. *
  660. * 48 bytes is enough to display the header and a little bit
  661. * into the payload for debugging purposes.
  662. */
  663. length = checkSMB(smb_buffer, smb_buffer->Mid, server->total_read);
  664. if (length != 0)
  665. cifs_dump_mem("Bad SMB: ", buf,
  666. min_t(unsigned int, server->total_read, 48));
  667. if (mid)
  668. handle_mid(mid, server, smb_buffer, length);
  669. return length;
  670. }
  671. static int
  672. cifs_demultiplex_thread(void *p)
  673. {
  674. int length;
  675. struct TCP_Server_Info *server = p;
  676. unsigned int pdu_length;
  677. char *buf = NULL;
  678. struct smb_hdr *smb_buffer = NULL;
  679. struct task_struct *task_to_wake = NULL;
  680. struct mid_q_entry *mid_entry;
  681. current->flags |= PF_MEMALLOC;
  682. cFYI(1, "Demultiplex PID: %d", task_pid_nr(current));
  683. length = atomic_inc_return(&tcpSesAllocCount);
  684. if (length > 1)
  685. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  686. GFP_KERNEL);
  687. set_freezable();
  688. while (server->tcpStatus != CifsExiting) {
  689. if (try_to_freeze())
  690. continue;
  691. if (!allocate_buffers(server))
  692. continue;
  693. server->large_buf = false;
  694. smb_buffer = (struct smb_hdr *)server->smallbuf;
  695. buf = server->smallbuf;
  696. pdu_length = 4; /* enough to get RFC1001 header */
  697. length = cifs_read_from_socket(server, buf, pdu_length);
  698. if (length < 0)
  699. continue;
  700. server->total_read = length;
  701. /*
  702. * The right amount was read from socket - 4 bytes,
  703. * so we can now interpret the length field.
  704. */
  705. pdu_length = be32_to_cpu(smb_buffer->smb_buf_length);
  706. cFYI(1, "RFC1002 header 0x%x", pdu_length);
  707. if (!is_smb_response(server, buf[0]))
  708. continue;
  709. /* make sure we have enough to get to the MID */
  710. if (pdu_length < sizeof(struct smb_hdr) - 1 - 4) {
  711. cERROR(1, "SMB response too short (%u bytes)",
  712. pdu_length);
  713. cifs_reconnect(server);
  714. wake_up(&server->response_q);
  715. continue;
  716. }
  717. /* read down to the MID */
  718. length = cifs_read_from_socket(server, buf + 4,
  719. sizeof(struct smb_hdr) - 1 - 4);
  720. if (length < 0)
  721. continue;
  722. server->total_read += length;
  723. mid_entry = find_mid(server, smb_buffer);
  724. if (!mid_entry || !mid_entry->receive)
  725. length = standard_receive3(server, mid_entry);
  726. else
  727. length = mid_entry->receive(server, mid_entry);
  728. if (length < 0)
  729. continue;
  730. if (server->large_buf) {
  731. buf = server->bigbuf;
  732. smb_buffer = (struct smb_hdr *)buf;
  733. }
  734. server->lstrp = jiffies;
  735. if (mid_entry != NULL) {
  736. if (!mid_entry->multiRsp || mid_entry->multiEnd)
  737. mid_entry->callback(mid_entry);
  738. } else if (!is_valid_oplock_break(smb_buffer, server)) {
  739. cERROR(1, "No task to wake, unknown frame received! "
  740. "NumMids %d", atomic_read(&midCount));
  741. cifs_dump_mem("Received Data is: ", buf,
  742. sizeof(struct smb_hdr));
  743. #ifdef CONFIG_CIFS_DEBUG2
  744. cifs_dump_detail(smb_buffer);
  745. cifs_dump_mids(server);
  746. #endif /* CIFS_DEBUG2 */
  747. }
  748. } /* end while !EXITING */
  749. /* buffer usually freed in free_mid - need to free it here on exit */
  750. cifs_buf_release(server->bigbuf);
  751. if (server->smallbuf) /* no sense logging a debug message if NULL */
  752. cifs_small_buf_release(server->smallbuf);
  753. task_to_wake = xchg(&server->tsk, NULL);
  754. clean_demultiplex_info(server);
  755. /* if server->tsk was NULL then wait for a signal before exiting */
  756. if (!task_to_wake) {
  757. set_current_state(TASK_INTERRUPTIBLE);
  758. while (!signal_pending(current)) {
  759. schedule();
  760. set_current_state(TASK_INTERRUPTIBLE);
  761. }
  762. set_current_state(TASK_RUNNING);
  763. }
  764. module_put_and_exit(0);
  765. }
  766. /* extract the host portion of the UNC string */
  767. static char *
  768. extract_hostname(const char *unc)
  769. {
  770. const char *src;
  771. char *dst, *delim;
  772. unsigned int len;
  773. /* skip double chars at beginning of string */
  774. /* BB: check validity of these bytes? */
  775. src = unc + 2;
  776. /* delimiter between hostname and sharename is always '\\' now */
  777. delim = strchr(src, '\\');
  778. if (!delim)
  779. return ERR_PTR(-EINVAL);
  780. len = delim - src;
  781. dst = kmalloc((len + 1), GFP_KERNEL);
  782. if (dst == NULL)
  783. return ERR_PTR(-ENOMEM);
  784. memcpy(dst, src, len);
  785. dst[len] = '\0';
  786. return dst;
  787. }
  788. static int
  789. cifs_parse_mount_options(const char *mountdata, const char *devname,
  790. struct smb_vol *vol)
  791. {
  792. char *value, *data, *end;
  793. char *mountdata_copy = NULL, *options;
  794. int err;
  795. unsigned int temp_len, i, j;
  796. char separator[2];
  797. short int override_uid = -1;
  798. short int override_gid = -1;
  799. bool uid_specified = false;
  800. bool gid_specified = false;
  801. char *nodename = utsname()->nodename;
  802. separator[0] = ',';
  803. separator[1] = 0;
  804. /*
  805. * does not have to be perfect mapping since field is
  806. * informational, only used for servers that do not support
  807. * port 445 and it can be overridden at mount time
  808. */
  809. memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
  810. for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
  811. vol->source_rfc1001_name[i] = toupper(nodename[i]);
  812. vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
  813. /* null target name indicates to use *SMBSERVR default called name
  814. if we end up sending RFC1001 session initialize */
  815. vol->target_rfc1001_name[0] = 0;
  816. vol->cred_uid = current_uid();
  817. vol->linux_uid = current_uid();
  818. vol->linux_gid = current_gid();
  819. /* default to only allowing write access to owner of the mount */
  820. vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
  821. /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
  822. /* default is always to request posix paths. */
  823. vol->posix_paths = 1;
  824. /* default to using server inode numbers where available */
  825. vol->server_ino = 1;
  826. vol->actimeo = CIFS_DEF_ACTIMEO;
  827. if (!mountdata)
  828. goto cifs_parse_mount_err;
  829. mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
  830. if (!mountdata_copy)
  831. goto cifs_parse_mount_err;
  832. options = mountdata_copy;
  833. end = options + strlen(options);
  834. if (strncmp(options, "sep=", 4) == 0) {
  835. if (options[4] != 0) {
  836. separator[0] = options[4];
  837. options += 5;
  838. } else {
  839. cFYI(1, "Null separator not allowed");
  840. }
  841. }
  842. vol->backupuid_specified = false; /* no backup intent for a user */
  843. vol->backupgid_specified = false; /* no backup intent for a group */
  844. while ((data = strsep(&options, separator)) != NULL) {
  845. if (!*data)
  846. continue;
  847. if ((value = strchr(data, '=')) != NULL)
  848. *value++ = '\0';
  849. /* Have to parse this before we parse for "user" */
  850. if (strnicmp(data, "user_xattr", 10) == 0) {
  851. vol->no_xattr = 0;
  852. } else if (strnicmp(data, "nouser_xattr", 12) == 0) {
  853. vol->no_xattr = 1;
  854. } else if (strnicmp(data, "user", 4) == 0) {
  855. if (!value) {
  856. printk(KERN_WARNING
  857. "CIFS: invalid or missing username\n");
  858. goto cifs_parse_mount_err;
  859. } else if (!*value) {
  860. /* null user, ie anonymous, authentication */
  861. vol->nullauth = 1;
  862. }
  863. if (strnlen(value, MAX_USERNAME_SIZE) <
  864. MAX_USERNAME_SIZE) {
  865. vol->username = kstrdup(value, GFP_KERNEL);
  866. if (!vol->username) {
  867. printk(KERN_WARNING "CIFS: no memory "
  868. "for username\n");
  869. goto cifs_parse_mount_err;
  870. }
  871. } else {
  872. printk(KERN_WARNING "CIFS: username too long\n");
  873. goto cifs_parse_mount_err;
  874. }
  875. } else if (strnicmp(data, "pass", 4) == 0) {
  876. if (!value) {
  877. vol->password = NULL;
  878. continue;
  879. } else if (value[0] == 0) {
  880. /* check if string begins with double comma
  881. since that would mean the password really
  882. does start with a comma, and would not
  883. indicate an empty string */
  884. if (value[1] != separator[0]) {
  885. vol->password = NULL;
  886. continue;
  887. }
  888. }
  889. temp_len = strlen(value);
  890. /* removed password length check, NTLM passwords
  891. can be arbitrarily long */
  892. /* if comma in password, the string will be
  893. prematurely null terminated. Commas in password are
  894. specified across the cifs mount interface by a double
  895. comma ie ,, and a comma used as in other cases ie ','
  896. as a parameter delimiter/separator is single and due
  897. to the strsep above is temporarily zeroed. */
  898. /* NB: password legally can have multiple commas and
  899. the only illegal character in a password is null */
  900. if ((value[temp_len] == 0) &&
  901. (value + temp_len < end) &&
  902. (value[temp_len+1] == separator[0])) {
  903. /* reinsert comma */
  904. value[temp_len] = separator[0];
  905. temp_len += 2; /* move after second comma */
  906. while (value[temp_len] != 0) {
  907. if (value[temp_len] == separator[0]) {
  908. if (value[temp_len+1] ==
  909. separator[0]) {
  910. /* skip second comma */
  911. temp_len++;
  912. } else {
  913. /* single comma indicating start
  914. of next parm */
  915. break;
  916. }
  917. }
  918. temp_len++;
  919. }
  920. if (value[temp_len] == 0) {
  921. options = NULL;
  922. } else {
  923. value[temp_len] = 0;
  924. /* point option to start of next parm */
  925. options = value + temp_len + 1;
  926. }
  927. /* go from value to value + temp_len condensing
  928. double commas to singles. Note that this ends up
  929. allocating a few bytes too many, which is ok */
  930. vol->password = kzalloc(temp_len, GFP_KERNEL);
  931. if (vol->password == NULL) {
  932. printk(KERN_WARNING "CIFS: no memory "
  933. "for password\n");
  934. goto cifs_parse_mount_err;
  935. }
  936. for (i = 0, j = 0; i < temp_len; i++, j++) {
  937. vol->password[j] = value[i];
  938. if (value[i] == separator[0]
  939. && value[i+1] == separator[0]) {
  940. /* skip second comma */
  941. i++;
  942. }
  943. }
  944. vol->password[j] = 0;
  945. } else {
  946. vol->password = kzalloc(temp_len+1, GFP_KERNEL);
  947. if (vol->password == NULL) {
  948. printk(KERN_WARNING "CIFS: no memory "
  949. "for password\n");
  950. goto cifs_parse_mount_err;
  951. }
  952. strcpy(vol->password, value);
  953. }
  954. } else if (!strnicmp(data, "ip", 2) ||
  955. !strnicmp(data, "addr", 4)) {
  956. if (!value || !*value) {
  957. vol->UNCip = NULL;
  958. } else if (strnlen(value, INET6_ADDRSTRLEN) <
  959. INET6_ADDRSTRLEN) {
  960. vol->UNCip = kstrdup(value, GFP_KERNEL);
  961. if (!vol->UNCip) {
  962. printk(KERN_WARNING "CIFS: no memory "
  963. "for UNC IP\n");
  964. goto cifs_parse_mount_err;
  965. }
  966. } else {
  967. printk(KERN_WARNING "CIFS: ip address "
  968. "too long\n");
  969. goto cifs_parse_mount_err;
  970. }
  971. } else if (strnicmp(data, "sec", 3) == 0) {
  972. if (!value || !*value) {
  973. cERROR(1, "no security value specified");
  974. continue;
  975. } else if (strnicmp(value, "krb5i", 5) == 0) {
  976. vol->secFlg |= CIFSSEC_MAY_KRB5 |
  977. CIFSSEC_MUST_SIGN;
  978. } else if (strnicmp(value, "krb5p", 5) == 0) {
  979. /* vol->secFlg |= CIFSSEC_MUST_SEAL |
  980. CIFSSEC_MAY_KRB5; */
  981. cERROR(1, "Krb5 cifs privacy not supported");
  982. goto cifs_parse_mount_err;
  983. } else if (strnicmp(value, "krb5", 4) == 0) {
  984. vol->secFlg |= CIFSSEC_MAY_KRB5;
  985. } else if (strnicmp(value, "ntlmsspi", 8) == 0) {
  986. vol->secFlg |= CIFSSEC_MAY_NTLMSSP |
  987. CIFSSEC_MUST_SIGN;
  988. } else if (strnicmp(value, "ntlmssp", 7) == 0) {
  989. vol->secFlg |= CIFSSEC_MAY_NTLMSSP;
  990. } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
  991. vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
  992. CIFSSEC_MUST_SIGN;
  993. } else if (strnicmp(value, "ntlmv2", 6) == 0) {
  994. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  995. } else if (strnicmp(value, "ntlmi", 5) == 0) {
  996. vol->secFlg |= CIFSSEC_MAY_NTLM |
  997. CIFSSEC_MUST_SIGN;
  998. } else if (strnicmp(value, "ntlm", 4) == 0) {
  999. /* ntlm is default so can be turned off too */
  1000. vol->secFlg |= CIFSSEC_MAY_NTLM;
  1001. } else if (strnicmp(value, "nontlm", 6) == 0) {
  1002. /* BB is there a better way to do this? */
  1003. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  1004. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  1005. } else if (strnicmp(value, "lanman", 6) == 0) {
  1006. vol->secFlg |= CIFSSEC_MAY_LANMAN;
  1007. #endif
  1008. } else if (strnicmp(value, "none", 4) == 0) {
  1009. vol->nullauth = 1;
  1010. } else {
  1011. cERROR(1, "bad security option: %s", value);
  1012. goto cifs_parse_mount_err;
  1013. }
  1014. } else if (strnicmp(data, "vers", 3) == 0) {
  1015. if (!value || !*value) {
  1016. cERROR(1, "no protocol version specified"
  1017. " after vers= mount option");
  1018. } else if ((strnicmp(value, "cifs", 4) == 0) ||
  1019. (strnicmp(value, "1", 1) == 0)) {
  1020. /* this is the default */
  1021. continue;
  1022. }
  1023. } else if ((strnicmp(data, "unc", 3) == 0)
  1024. || (strnicmp(data, "target", 6) == 0)
  1025. || (strnicmp(data, "path", 4) == 0)) {
  1026. if (!value || !*value) {
  1027. printk(KERN_WARNING "CIFS: invalid path to "
  1028. "network resource\n");
  1029. goto cifs_parse_mount_err;
  1030. }
  1031. if ((temp_len = strnlen(value, 300)) < 300) {
  1032. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  1033. if (vol->UNC == NULL)
  1034. goto cifs_parse_mount_err;
  1035. strcpy(vol->UNC, value);
  1036. if (strncmp(vol->UNC, "//", 2) == 0) {
  1037. vol->UNC[0] = '\\';
  1038. vol->UNC[1] = '\\';
  1039. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  1040. printk(KERN_WARNING
  1041. "CIFS: UNC Path does not begin "
  1042. "with // or \\\\ \n");
  1043. goto cifs_parse_mount_err;
  1044. }
  1045. } else {
  1046. printk(KERN_WARNING "CIFS: UNC name too long\n");
  1047. goto cifs_parse_mount_err;
  1048. }
  1049. } else if ((strnicmp(data, "domain", 3) == 0)
  1050. || (strnicmp(data, "workgroup", 5) == 0)) {
  1051. if (!value || !*value) {
  1052. printk(KERN_WARNING "CIFS: invalid domain name\n");
  1053. goto cifs_parse_mount_err;
  1054. }
  1055. /* BB are there cases in which a comma can be valid in
  1056. a domain name and need special handling? */
  1057. if (strnlen(value, 256) < 256) {
  1058. vol->domainname = kstrdup(value, GFP_KERNEL);
  1059. if (!vol->domainname) {
  1060. printk(KERN_WARNING "CIFS: no memory "
  1061. "for domainname\n");
  1062. goto cifs_parse_mount_err;
  1063. }
  1064. cFYI(1, "Domain name set");
  1065. } else {
  1066. printk(KERN_WARNING "CIFS: domain name too "
  1067. "long\n");
  1068. goto cifs_parse_mount_err;
  1069. }
  1070. } else if (strnicmp(data, "srcaddr", 7) == 0) {
  1071. vol->srcaddr.ss_family = AF_UNSPEC;
  1072. if (!value || !*value) {
  1073. printk(KERN_WARNING "CIFS: srcaddr value"
  1074. " not specified.\n");
  1075. goto cifs_parse_mount_err;
  1076. }
  1077. i = cifs_convert_address((struct sockaddr *)&vol->srcaddr,
  1078. value, strlen(value));
  1079. if (i == 0) {
  1080. printk(KERN_WARNING "CIFS: Could not parse"
  1081. " srcaddr: %s\n",
  1082. value);
  1083. goto cifs_parse_mount_err;
  1084. }
  1085. } else if (strnicmp(data, "prefixpath", 10) == 0) {
  1086. if (!value || !*value) {
  1087. printk(KERN_WARNING
  1088. "CIFS: invalid path prefix\n");
  1089. goto cifs_parse_mount_err;
  1090. }
  1091. if ((temp_len = strnlen(value, 1024)) < 1024) {
  1092. if (value[0] != '/')
  1093. temp_len++; /* missing leading slash */
  1094. vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
  1095. if (vol->prepath == NULL)
  1096. goto cifs_parse_mount_err;
  1097. if (value[0] != '/') {
  1098. vol->prepath[0] = '/';
  1099. strcpy(vol->prepath+1, value);
  1100. } else
  1101. strcpy(vol->prepath, value);
  1102. cFYI(1, "prefix path %s", vol->prepath);
  1103. } else {
  1104. printk(KERN_WARNING "CIFS: prefix too long\n");
  1105. goto cifs_parse_mount_err;
  1106. }
  1107. } else if (strnicmp(data, "iocharset", 9) == 0) {
  1108. if (!value || !*value) {
  1109. printk(KERN_WARNING "CIFS: invalid iocharset "
  1110. "specified\n");
  1111. goto cifs_parse_mount_err;
  1112. }
  1113. if (strnlen(value, 65) < 65) {
  1114. if (strnicmp(value, "default", 7)) {
  1115. vol->iocharset = kstrdup(value,
  1116. GFP_KERNEL);
  1117. if (!vol->iocharset) {
  1118. printk(KERN_WARNING "CIFS: no "
  1119. "memory for"
  1120. "charset\n");
  1121. goto cifs_parse_mount_err;
  1122. }
  1123. }
  1124. /* if iocharset not set then load_nls_default
  1125. is used by caller */
  1126. cFYI(1, "iocharset set to %s", value);
  1127. } else {
  1128. printk(KERN_WARNING "CIFS: iocharset name "
  1129. "too long.\n");
  1130. goto cifs_parse_mount_err;
  1131. }
  1132. } else if (!strnicmp(data, "uid", 3) && value && *value) {
  1133. vol->linux_uid = simple_strtoul(value, &value, 0);
  1134. uid_specified = true;
  1135. } else if (!strnicmp(data, "cruid", 5) && value && *value) {
  1136. vol->cred_uid = simple_strtoul(value, &value, 0);
  1137. } else if (!strnicmp(data, "forceuid", 8)) {
  1138. override_uid = 1;
  1139. } else if (!strnicmp(data, "noforceuid", 10)) {
  1140. override_uid = 0;
  1141. } else if (!strnicmp(data, "gid", 3) && value && *value) {
  1142. vol->linux_gid = simple_strtoul(value, &value, 0);
  1143. gid_specified = true;
  1144. } else if (!strnicmp(data, "forcegid", 8)) {
  1145. override_gid = 1;
  1146. } else if (!strnicmp(data, "noforcegid", 10)) {
  1147. override_gid = 0;
  1148. } else if (strnicmp(data, "file_mode", 4) == 0) {
  1149. if (value && *value) {
  1150. vol->file_mode =
  1151. simple_strtoul(value, &value, 0);
  1152. }
  1153. } else if (strnicmp(data, "dir_mode", 4) == 0) {
  1154. if (value && *value) {
  1155. vol->dir_mode =
  1156. simple_strtoul(value, &value, 0);
  1157. }
  1158. } else if (strnicmp(data, "dirmode", 4) == 0) {
  1159. if (value && *value) {
  1160. vol->dir_mode =
  1161. simple_strtoul(value, &value, 0);
  1162. }
  1163. } else if (strnicmp(data, "port", 4) == 0) {
  1164. if (value && *value) {
  1165. vol->port =
  1166. simple_strtoul(value, &value, 0);
  1167. }
  1168. } else if (strnicmp(data, "rsize", 5) == 0) {
  1169. if (value && *value) {
  1170. vol->rsize =
  1171. simple_strtoul(value, &value, 0);
  1172. }
  1173. } else if (strnicmp(data, "wsize", 5) == 0) {
  1174. if (value && *value) {
  1175. vol->wsize =
  1176. simple_strtoul(value, &value, 0);
  1177. }
  1178. } else if (strnicmp(data, "sockopt", 5) == 0) {
  1179. if (!value || !*value) {
  1180. cERROR(1, "no socket option specified");
  1181. continue;
  1182. } else if (strnicmp(value, "TCP_NODELAY", 11) == 0) {
  1183. vol->sockopt_tcp_nodelay = 1;
  1184. }
  1185. } else if (strnicmp(data, "netbiosname", 4) == 0) {
  1186. if (!value || !*value || (*value == ' ')) {
  1187. cFYI(1, "invalid (empty) netbiosname");
  1188. } else {
  1189. memset(vol->source_rfc1001_name, 0x20,
  1190. RFC1001_NAME_LEN);
  1191. /*
  1192. * FIXME: are there cases in which a comma can
  1193. * be valid in workstation netbios name (and
  1194. * need special handling)?
  1195. */
  1196. for (i = 0; i < RFC1001_NAME_LEN; i++) {
  1197. /* don't ucase netbiosname for user */
  1198. if (value[i] == 0)
  1199. break;
  1200. vol->source_rfc1001_name[i] = value[i];
  1201. }
  1202. /* The string has 16th byte zero still from
  1203. set at top of the function */
  1204. if (i == RFC1001_NAME_LEN && value[i] != 0)
  1205. printk(KERN_WARNING "CIFS: netbiosname"
  1206. " longer than 15 truncated.\n");
  1207. }
  1208. } else if (strnicmp(data, "servern", 7) == 0) {
  1209. /* servernetbiosname specified override *SMBSERVER */
  1210. if (!value || !*value || (*value == ' ')) {
  1211. cFYI(1, "empty server netbiosname specified");
  1212. } else {
  1213. /* last byte, type, is 0x20 for servr type */
  1214. memset(vol->target_rfc1001_name, 0x20,
  1215. RFC1001_NAME_LEN_WITH_NULL);
  1216. for (i = 0; i < 15; i++) {
  1217. /* BB are there cases in which a comma can be
  1218. valid in this workstation netbios name
  1219. (and need special handling)? */
  1220. /* user or mount helper must uppercase
  1221. the netbiosname */
  1222. if (value[i] == 0)
  1223. break;
  1224. else
  1225. vol->target_rfc1001_name[i] =
  1226. value[i];
  1227. }
  1228. /* The string has 16th byte zero still from
  1229. set at top of the function */
  1230. if (i == RFC1001_NAME_LEN && value[i] != 0)
  1231. printk(KERN_WARNING "CIFS: server net"
  1232. "biosname longer than 15 truncated.\n");
  1233. }
  1234. } else if (strnicmp(data, "actimeo", 7) == 0) {
  1235. if (value && *value) {
  1236. vol->actimeo = HZ * simple_strtoul(value,
  1237. &value, 0);
  1238. if (vol->actimeo > CIFS_MAX_ACTIMEO) {
  1239. cERROR(1, "CIFS: attribute cache"
  1240. "timeout too large");
  1241. goto cifs_parse_mount_err;
  1242. }
  1243. }
  1244. } else if (strnicmp(data, "credentials", 4) == 0) {
  1245. /* ignore */
  1246. } else if (strnicmp(data, "version", 3) == 0) {
  1247. /* ignore */
  1248. } else if (strnicmp(data, "guest", 5) == 0) {
  1249. /* ignore */
  1250. } else if (strnicmp(data, "rw", 2) == 0 && strlen(data) == 2) {
  1251. /* ignore */
  1252. } else if (strnicmp(data, "ro", 2) == 0) {
  1253. /* ignore */
  1254. } else if (strnicmp(data, "noblocksend", 11) == 0) {
  1255. vol->noblocksnd = 1;
  1256. } else if (strnicmp(data, "noautotune", 10) == 0) {
  1257. vol->noautotune = 1;
  1258. } else if ((strnicmp(data, "suid", 4) == 0) ||
  1259. (strnicmp(data, "nosuid", 6) == 0) ||
  1260. (strnicmp(data, "exec", 4) == 0) ||
  1261. (strnicmp(data, "noexec", 6) == 0) ||
  1262. (strnicmp(data, "nodev", 5) == 0) ||
  1263. (strnicmp(data, "noauto", 6) == 0) ||
  1264. (strnicmp(data, "dev", 3) == 0)) {
  1265. /* The mount tool or mount.cifs helper (if present)
  1266. uses these opts to set flags, and the flags are read
  1267. by the kernel vfs layer before we get here (ie
  1268. before read super) so there is no point trying to
  1269. parse these options again and set anything and it
  1270. is ok to just ignore them */
  1271. continue;
  1272. } else if (strnicmp(data, "hard", 4) == 0) {
  1273. vol->retry = 1;
  1274. } else if (strnicmp(data, "soft", 4) == 0) {
  1275. vol->retry = 0;
  1276. } else if (strnicmp(data, "perm", 4) == 0) {
  1277. vol->noperm = 0;
  1278. } else if (strnicmp(data, "noperm", 6) == 0) {
  1279. vol->noperm = 1;
  1280. } else if (strnicmp(data, "mapchars", 8) == 0) {
  1281. vol->remap = 1;
  1282. } else if (strnicmp(data, "nomapchars", 10) == 0) {
  1283. vol->remap = 0;
  1284. } else if (strnicmp(data, "sfu", 3) == 0) {
  1285. vol->sfu_emul = 1;
  1286. } else if (strnicmp(data, "nosfu", 5) == 0) {
  1287. vol->sfu_emul = 0;
  1288. } else if (strnicmp(data, "nodfs", 5) == 0) {
  1289. vol->nodfs = 1;
  1290. } else if (strnicmp(data, "posixpaths", 10) == 0) {
  1291. vol->posix_paths = 1;
  1292. } else if (strnicmp(data, "noposixpaths", 12) == 0) {
  1293. vol->posix_paths = 0;
  1294. } else if (strnicmp(data, "nounix", 6) == 0) {
  1295. vol->no_linux_ext = 1;
  1296. } else if (strnicmp(data, "nolinux", 7) == 0) {
  1297. vol->no_linux_ext = 1;
  1298. } else if ((strnicmp(data, "nocase", 6) == 0) ||
  1299. (strnicmp(data, "ignorecase", 10) == 0)) {
  1300. vol->nocase = 1;
  1301. } else if (strnicmp(data, "mand", 4) == 0) {
  1302. /* ignore */
  1303. } else if (strnicmp(data, "nomand", 6) == 0) {
  1304. /* ignore */
  1305. } else if (strnicmp(data, "_netdev", 7) == 0) {
  1306. /* ignore */
  1307. } else if (strnicmp(data, "brl", 3) == 0) {
  1308. vol->nobrl = 0;
  1309. } else if ((strnicmp(data, "nobrl", 5) == 0) ||
  1310. (strnicmp(data, "nolock", 6) == 0)) {
  1311. vol->nobrl = 1;
  1312. /* turn off mandatory locking in mode
  1313. if remote locking is turned off since the
  1314. local vfs will do advisory */
  1315. if (vol->file_mode ==
  1316. (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
  1317. vol->file_mode = S_IALLUGO;
  1318. } else if (strnicmp(data, "forcemandatorylock", 9) == 0) {
  1319. /* will take the shorter form "forcemand" as well */
  1320. /* This mount option will force use of mandatory
  1321. (DOS/Windows style) byte range locks, instead of
  1322. using posix advisory byte range locks, even if the
  1323. Unix extensions are available and posix locks would
  1324. be supported otherwise. If Unix extensions are not
  1325. negotiated this has no effect since mandatory locks
  1326. would be used (mandatory locks is all that those
  1327. those servers support) */
  1328. vol->mand_lock = 1;
  1329. } else if (strnicmp(data, "setuids", 7) == 0) {
  1330. vol->setuids = 1;
  1331. } else if (strnicmp(data, "nosetuids", 9) == 0) {
  1332. vol->setuids = 0;
  1333. } else if (strnicmp(data, "dynperm", 7) == 0) {
  1334. vol->dynperm = true;
  1335. } else if (strnicmp(data, "nodynperm", 9) == 0) {
  1336. vol->dynperm = false;
  1337. } else if (strnicmp(data, "nohard", 6) == 0) {
  1338. vol->retry = 0;
  1339. } else if (strnicmp(data, "nosoft", 6) == 0) {
  1340. vol->retry = 1;
  1341. } else if (strnicmp(data, "nointr", 6) == 0) {
  1342. vol->intr = 0;
  1343. } else if (strnicmp(data, "intr", 4) == 0) {
  1344. vol->intr = 1;
  1345. } else if (strnicmp(data, "nostrictsync", 12) == 0) {
  1346. vol->nostrictsync = 1;
  1347. } else if (strnicmp(data, "strictsync", 10) == 0) {
  1348. vol->nostrictsync = 0;
  1349. } else if (strnicmp(data, "serverino", 7) == 0) {
  1350. vol->server_ino = 1;
  1351. } else if (strnicmp(data, "noserverino", 9) == 0) {
  1352. vol->server_ino = 0;
  1353. } else if (strnicmp(data, "rwpidforward", 12) == 0) {
  1354. vol->rwpidforward = 1;
  1355. } else if (strnicmp(data, "cifsacl", 7) == 0) {
  1356. vol->cifs_acl = 1;
  1357. } else if (strnicmp(data, "nocifsacl", 9) == 0) {
  1358. vol->cifs_acl = 0;
  1359. } else if (strnicmp(data, "acl", 3) == 0) {
  1360. vol->no_psx_acl = 0;
  1361. } else if (strnicmp(data, "noacl", 5) == 0) {
  1362. vol->no_psx_acl = 1;
  1363. } else if (strnicmp(data, "locallease", 6) == 0) {
  1364. vol->local_lease = 1;
  1365. } else if (strnicmp(data, "sign", 4) == 0) {
  1366. vol->secFlg |= CIFSSEC_MUST_SIGN;
  1367. } else if (strnicmp(data, "seal", 4) == 0) {
  1368. /* we do not do the following in secFlags because seal
  1369. is a per tree connection (mount) not a per socket
  1370. or per-smb connection option in the protocol */
  1371. /* vol->secFlg |= CIFSSEC_MUST_SEAL; */
  1372. vol->seal = 1;
  1373. } else if (strnicmp(data, "direct", 6) == 0) {
  1374. vol->direct_io = 1;
  1375. } else if (strnicmp(data, "forcedirectio", 13) == 0) {
  1376. vol->direct_io = 1;
  1377. } else if (strnicmp(data, "strictcache", 11) == 0) {
  1378. vol->strict_io = 1;
  1379. } else if (strnicmp(data, "noac", 4) == 0) {
  1380. printk(KERN_WARNING "CIFS: Mount option noac not "
  1381. "supported. Instead set "
  1382. "/proc/fs/cifs/LookupCacheEnabled to 0\n");
  1383. } else if (strnicmp(data, "fsc", 3) == 0) {
  1384. #ifndef CONFIG_CIFS_FSCACHE
  1385. cERROR(1, "FS-Cache support needs CONFIG_CIFS_FSCACHE "
  1386. "kernel config option set");
  1387. goto cifs_parse_mount_err;
  1388. #endif
  1389. vol->fsc = true;
  1390. } else if (strnicmp(data, "mfsymlinks", 10) == 0) {
  1391. vol->mfsymlinks = true;
  1392. } else if (strnicmp(data, "multiuser", 8) == 0) {
  1393. vol->multiuser = true;
  1394. } else if (!strnicmp(data, "backupuid", 9) && value && *value) {
  1395. err = kstrtouint(value, 0, &vol->backupuid);
  1396. if (err < 0) {
  1397. cERROR(1, "%s: Invalid backupuid value",
  1398. __func__);
  1399. goto cifs_parse_mount_err;
  1400. }
  1401. vol->backupuid_specified = true;
  1402. } else if (!strnicmp(data, "backupgid", 9) && value && *value) {
  1403. err = kstrtouint(value, 0, &vol->backupgid);
  1404. if (err < 0) {
  1405. cERROR(1, "%s: Invalid backupgid value",
  1406. __func__);
  1407. goto cifs_parse_mount_err;
  1408. }
  1409. vol->backupgid_specified = true;
  1410. } else
  1411. printk(KERN_WARNING "CIFS: Unknown mount option %s\n",
  1412. data);
  1413. }
  1414. if (vol->UNC == NULL) {
  1415. if (devname == NULL) {
  1416. printk(KERN_WARNING "CIFS: Missing UNC name for mount "
  1417. "target\n");
  1418. goto cifs_parse_mount_err;
  1419. }
  1420. if ((temp_len = strnlen(devname, 300)) < 300) {
  1421. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  1422. if (vol->UNC == NULL)
  1423. goto cifs_parse_mount_err;
  1424. strcpy(vol->UNC, devname);
  1425. if (strncmp(vol->UNC, "//", 2) == 0) {
  1426. vol->UNC[0] = '\\';
  1427. vol->UNC[1] = '\\';
  1428. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  1429. printk(KERN_WARNING "CIFS: UNC Path does not "
  1430. "begin with // or \\\\ \n");
  1431. goto cifs_parse_mount_err;
  1432. }
  1433. value = strpbrk(vol->UNC+2, "/\\");
  1434. if (value)
  1435. *value = '\\';
  1436. } else {
  1437. printk(KERN_WARNING "CIFS: UNC name too long\n");
  1438. goto cifs_parse_mount_err;
  1439. }
  1440. }
  1441. if (vol->multiuser && !(vol->secFlg & CIFSSEC_MAY_KRB5)) {
  1442. cERROR(1, "Multiuser mounts currently require krb5 "
  1443. "authentication!");
  1444. goto cifs_parse_mount_err;
  1445. }
  1446. if (vol->UNCip == NULL)
  1447. vol->UNCip = &vol->UNC[2];
  1448. if (uid_specified)
  1449. vol->override_uid = override_uid;
  1450. else if (override_uid == 1)
  1451. printk(KERN_NOTICE "CIFS: ignoring forceuid mount option "
  1452. "specified with no uid= option.\n");
  1453. if (gid_specified)
  1454. vol->override_gid = override_gid;
  1455. else if (override_gid == 1)
  1456. printk(KERN_NOTICE "CIFS: ignoring forcegid mount option "
  1457. "specified with no gid= option.\n");
  1458. kfree(mountdata_copy);
  1459. return 0;
  1460. cifs_parse_mount_err:
  1461. kfree(mountdata_copy);
  1462. return 1;
  1463. }
  1464. /** Returns true if srcaddr isn't specified and rhs isn't
  1465. * specified, or if srcaddr is specified and
  1466. * matches the IP address of the rhs argument.
  1467. */
  1468. static bool
  1469. srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
  1470. {
  1471. switch (srcaddr->sa_family) {
  1472. case AF_UNSPEC:
  1473. return (rhs->sa_family == AF_UNSPEC);
  1474. case AF_INET: {
  1475. struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
  1476. struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
  1477. return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
  1478. }
  1479. case AF_INET6: {
  1480. struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
  1481. struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)&rhs;
  1482. return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
  1483. }
  1484. default:
  1485. WARN_ON(1);
  1486. return false; /* don't expect to be here */
  1487. }
  1488. }
  1489. /*
  1490. * If no port is specified in addr structure, we try to match with 445 port
  1491. * and if it fails - with 139 ports. It should be called only if address
  1492. * families of server and addr are equal.
  1493. */
  1494. static bool
  1495. match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
  1496. {
  1497. __be16 port, *sport;
  1498. switch (addr->sa_family) {
  1499. case AF_INET:
  1500. sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
  1501. port = ((struct sockaddr_in *) addr)->sin_port;
  1502. break;
  1503. case AF_INET6:
  1504. sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
  1505. port = ((struct sockaddr_in6 *) addr)->sin6_port;
  1506. break;
  1507. default:
  1508. WARN_ON(1);
  1509. return false;
  1510. }
  1511. if (!port) {
  1512. port = htons(CIFS_PORT);
  1513. if (port == *sport)
  1514. return true;
  1515. port = htons(RFC1001_PORT);
  1516. }
  1517. return port == *sport;
  1518. }
  1519. static bool
  1520. match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
  1521. struct sockaddr *srcaddr)
  1522. {
  1523. switch (addr->sa_family) {
  1524. case AF_INET: {
  1525. struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
  1526. struct sockaddr_in *srv_addr4 =
  1527. (struct sockaddr_in *)&server->dstaddr;
  1528. if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
  1529. return false;
  1530. break;
  1531. }
  1532. case AF_INET6: {
  1533. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  1534. struct sockaddr_in6 *srv_addr6 =
  1535. (struct sockaddr_in6 *)&server->dstaddr;
  1536. if (!ipv6_addr_equal(&addr6->sin6_addr,
  1537. &srv_addr6->sin6_addr))
  1538. return false;
  1539. if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
  1540. return false;
  1541. break;
  1542. }
  1543. default:
  1544. WARN_ON(1);
  1545. return false; /* don't expect to be here */
  1546. }
  1547. if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
  1548. return false;
  1549. return true;
  1550. }
  1551. static bool
  1552. match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
  1553. {
  1554. unsigned int secFlags;
  1555. if (vol->secFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  1556. secFlags = vol->secFlg;
  1557. else
  1558. secFlags = global_secflags | vol->secFlg;
  1559. switch (server->secType) {
  1560. case LANMAN:
  1561. if (!(secFlags & (CIFSSEC_MAY_LANMAN|CIFSSEC_MAY_PLNTXT)))
  1562. return false;
  1563. break;
  1564. case NTLMv2:
  1565. if (!(secFlags & CIFSSEC_MAY_NTLMV2))
  1566. return false;
  1567. break;
  1568. case NTLM:
  1569. if (!(secFlags & CIFSSEC_MAY_NTLM))
  1570. return false;
  1571. break;
  1572. case Kerberos:
  1573. if (!(secFlags & CIFSSEC_MAY_KRB5))
  1574. return false;
  1575. break;
  1576. case RawNTLMSSP:
  1577. if (!(secFlags & CIFSSEC_MAY_NTLMSSP))
  1578. return false;
  1579. break;
  1580. default:
  1581. /* shouldn't happen */
  1582. return false;
  1583. }
  1584. /* now check if signing mode is acceptable */
  1585. if ((secFlags & CIFSSEC_MAY_SIGN) == 0 &&
  1586. (server->sec_mode & SECMODE_SIGN_REQUIRED))
  1587. return false;
  1588. else if (((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) &&
  1589. (server->sec_mode &
  1590. (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED)) == 0)
  1591. return false;
  1592. return true;
  1593. }
  1594. static int match_server(struct TCP_Server_Info *server, struct sockaddr *addr,
  1595. struct smb_vol *vol)
  1596. {
  1597. if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
  1598. return 0;
  1599. if (!match_address(server, addr,
  1600. (struct sockaddr *)&vol->srcaddr))
  1601. return 0;
  1602. if (!match_port(server, addr))
  1603. return 0;
  1604. if (!match_security(server, vol))
  1605. return 0;
  1606. return 1;
  1607. }
  1608. static struct TCP_Server_Info *
  1609. cifs_find_tcp_session(struct sockaddr *addr, struct smb_vol *vol)
  1610. {
  1611. struct TCP_Server_Info *server;
  1612. spin_lock(&cifs_tcp_ses_lock);
  1613. list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
  1614. if (!match_server(server, addr, vol))
  1615. continue;
  1616. ++server->srv_count;
  1617. spin_unlock(&cifs_tcp_ses_lock);
  1618. cFYI(1, "Existing tcp session with server found");
  1619. return server;
  1620. }
  1621. spin_unlock(&cifs_tcp_ses_lock);
  1622. return NULL;
  1623. }
  1624. static void
  1625. cifs_put_tcp_session(struct TCP_Server_Info *server)
  1626. {
  1627. struct task_struct *task;
  1628. spin_lock(&cifs_tcp_ses_lock);
  1629. if (--server->srv_count > 0) {
  1630. spin_unlock(&cifs_tcp_ses_lock);
  1631. return;
  1632. }
  1633. put_net(cifs_net_ns(server));
  1634. list_del_init(&server->tcp_ses_list);
  1635. spin_unlock(&cifs_tcp_ses_lock);
  1636. cancel_delayed_work_sync(&server->echo);
  1637. spin_lock(&GlobalMid_Lock);
  1638. server->tcpStatus = CifsExiting;
  1639. spin_unlock(&GlobalMid_Lock);
  1640. cifs_crypto_shash_release(server);
  1641. cifs_fscache_release_client_cookie(server);
  1642. kfree(server->session_key.response);
  1643. server->session_key.response = NULL;
  1644. server->session_key.len = 0;
  1645. task = xchg(&server->tsk, NULL);
  1646. if (task)
  1647. force_sig(SIGKILL, task);
  1648. }
  1649. static struct TCP_Server_Info *
  1650. cifs_get_tcp_session(struct smb_vol *volume_info)
  1651. {
  1652. struct TCP_Server_Info *tcp_ses = NULL;
  1653. struct sockaddr_storage addr;
  1654. struct sockaddr_in *sin_server = (struct sockaddr_in *) &addr;
  1655. struct sockaddr_in6 *sin_server6 = (struct sockaddr_in6 *) &addr;
  1656. int rc;
  1657. memset(&addr, 0, sizeof(struct sockaddr_storage));
  1658. cFYI(1, "UNC: %s ip: %s", volume_info->UNC, volume_info->UNCip);
  1659. if (volume_info->UNCip && volume_info->UNC) {
  1660. rc = cifs_fill_sockaddr((struct sockaddr *)&addr,
  1661. volume_info->UNCip,
  1662. strlen(volume_info->UNCip),
  1663. volume_info->port);
  1664. if (!rc) {
  1665. /* we failed translating address */
  1666. rc = -EINVAL;
  1667. goto out_err;
  1668. }
  1669. } else if (volume_info->UNCip) {
  1670. /* BB using ip addr as tcp_ses name to connect to the
  1671. DFS root below */
  1672. cERROR(1, "Connecting to DFS root not implemented yet");
  1673. rc = -EINVAL;
  1674. goto out_err;
  1675. } else /* which tcp_sess DFS root would we conect to */ {
  1676. cERROR(1, "CIFS mount error: No UNC path (e.g. -o "
  1677. "unc=//192.168.1.100/public) specified");
  1678. rc = -EINVAL;
  1679. goto out_err;
  1680. }
  1681. /* see if we already have a matching tcp_ses */
  1682. tcp_ses = cifs_find_tcp_session((struct sockaddr *)&addr, volume_info);
  1683. if (tcp_ses)
  1684. return tcp_ses;
  1685. tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
  1686. if (!tcp_ses) {
  1687. rc = -ENOMEM;
  1688. goto out_err;
  1689. }
  1690. rc = cifs_crypto_shash_allocate(tcp_ses);
  1691. if (rc) {
  1692. cERROR(1, "could not setup hash structures rc %d", rc);
  1693. goto out_err;
  1694. }
  1695. cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
  1696. tcp_ses->hostname = extract_hostname(volume_info->UNC);
  1697. if (IS_ERR(tcp_ses->hostname)) {
  1698. rc = PTR_ERR(tcp_ses->hostname);
  1699. goto out_err_crypto_release;
  1700. }
  1701. tcp_ses->noblocksnd = volume_info->noblocksnd;
  1702. tcp_ses->noautotune = volume_info->noautotune;
  1703. tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
  1704. atomic_set(&tcp_ses->inFlight, 0);
  1705. init_waitqueue_head(&tcp_ses->response_q);
  1706. init_waitqueue_head(&tcp_ses->request_q);
  1707. INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
  1708. mutex_init(&tcp_ses->srv_mutex);
  1709. memcpy(tcp_ses->workstation_RFC1001_name,
  1710. volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1711. memcpy(tcp_ses->server_RFC1001_name,
  1712. volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1713. tcp_ses->session_estab = false;
  1714. tcp_ses->sequence_number = 0;
  1715. tcp_ses->lstrp = jiffies;
  1716. INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
  1717. INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
  1718. INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
  1719. /*
  1720. * at this point we are the only ones with the pointer
  1721. * to the struct since the kernel thread not created yet
  1722. * no need to spinlock this init of tcpStatus or srv_count
  1723. */
  1724. tcp_ses->tcpStatus = CifsNew;
  1725. memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
  1726. sizeof(tcp_ses->srcaddr));
  1727. ++tcp_ses->srv_count;
  1728. if (addr.ss_family == AF_INET6) {
  1729. cFYI(1, "attempting ipv6 connect");
  1730. /* BB should we allow ipv6 on port 139? */
  1731. /* other OS never observed in Wild doing 139 with v6 */
  1732. memcpy(&tcp_ses->dstaddr, sin_server6,
  1733. sizeof(struct sockaddr_in6));
  1734. } else
  1735. memcpy(&tcp_ses->dstaddr, sin_server,
  1736. sizeof(struct sockaddr_in));
  1737. rc = ip_connect(tcp_ses);
  1738. if (rc < 0) {
  1739. cERROR(1, "Error connecting to socket. Aborting operation");
  1740. goto out_err_crypto_release;
  1741. }
  1742. /*
  1743. * since we're in a cifs function already, we know that
  1744. * this will succeed. No need for try_module_get().
  1745. */
  1746. __module_get(THIS_MODULE);
  1747. tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
  1748. tcp_ses, "cifsd");
  1749. if (IS_ERR(tcp_ses->tsk)) {
  1750. rc = PTR_ERR(tcp_ses->tsk);
  1751. cERROR(1, "error %d create cifsd thread", rc);
  1752. module_put(THIS_MODULE);
  1753. goto out_err_crypto_release;
  1754. }
  1755. tcp_ses->tcpStatus = CifsNeedNegotiate;
  1756. /* thread spawned, put it on the list */
  1757. spin_lock(&cifs_tcp_ses_lock);
  1758. list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
  1759. spin_unlock(&cifs_tcp_ses_lock);
  1760. cifs_fscache_get_client_cookie(tcp_ses);
  1761. /* queue echo request delayed work */
  1762. queue_delayed_work(system_nrt_wq, &tcp_ses->echo, SMB_ECHO_INTERVAL);
  1763. return tcp_ses;
  1764. out_err_crypto_release:
  1765. cifs_crypto_shash_release(tcp_ses);
  1766. put_net(cifs_net_ns(tcp_ses));
  1767. out_err:
  1768. if (tcp_ses) {
  1769. if (!IS_ERR(tcp_ses->hostname))
  1770. kfree(tcp_ses->hostname);
  1771. if (tcp_ses->ssocket)
  1772. sock_release(tcp_ses->ssocket);
  1773. kfree(tcp_ses);
  1774. }
  1775. return ERR_PTR(rc);
  1776. }
  1777. static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
  1778. {
  1779. switch (ses->server->secType) {
  1780. case Kerberos:
  1781. if (vol->cred_uid != ses->cred_uid)
  1782. return 0;
  1783. break;
  1784. default:
  1785. /* anything else takes username/password */
  1786. if (ses->user_name == NULL)
  1787. return 0;
  1788. if (strncmp(ses->user_name, vol->username,
  1789. MAX_USERNAME_SIZE))
  1790. return 0;
  1791. if (strlen(vol->username) != 0 &&
  1792. ses->password != NULL &&
  1793. strncmp(ses->password,
  1794. vol->password ? vol->password : "",
  1795. MAX_PASSWORD_SIZE))
  1796. return 0;
  1797. }
  1798. return 1;
  1799. }
  1800. static struct cifs_ses *
  1801. cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
  1802. {
  1803. struct cifs_ses *ses;
  1804. spin_lock(&cifs_tcp_ses_lock);
  1805. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  1806. if (!match_session(ses, vol))
  1807. continue;
  1808. ++ses->ses_count;
  1809. spin_unlock(&cifs_tcp_ses_lock);
  1810. return ses;
  1811. }
  1812. spin_unlock(&cifs_tcp_ses_lock);
  1813. return NULL;
  1814. }
  1815. static void
  1816. cifs_put_smb_ses(struct cifs_ses *ses)
  1817. {
  1818. int xid;
  1819. struct TCP_Server_Info *server = ses->server;
  1820. cFYI(1, "%s: ses_count=%d\n", __func__, ses->ses_count);
  1821. spin_lock(&cifs_tcp_ses_lock);
  1822. if (--ses->ses_count > 0) {
  1823. spin_unlock(&cifs_tcp_ses_lock);
  1824. return;
  1825. }
  1826. list_del_init(&ses->smb_ses_list);
  1827. spin_unlock(&cifs_tcp_ses_lock);
  1828. if (ses->status == CifsGood) {
  1829. xid = GetXid();
  1830. CIFSSMBLogoff(xid, ses);
  1831. _FreeXid(xid);
  1832. }
  1833. sesInfoFree(ses);
  1834. cifs_put_tcp_session(server);
  1835. }
  1836. static bool warned_on_ntlm; /* globals init to false automatically */
  1837. static struct cifs_ses *
  1838. cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
  1839. {
  1840. int rc = -ENOMEM, xid;
  1841. struct cifs_ses *ses;
  1842. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  1843. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  1844. xid = GetXid();
  1845. ses = cifs_find_smb_ses(server, volume_info);
  1846. if (ses) {
  1847. cFYI(1, "Existing smb sess found (status=%d)", ses->status);
  1848. mutex_lock(&ses->session_mutex);
  1849. rc = cifs_negotiate_protocol(xid, ses);
  1850. if (rc) {
  1851. mutex_unlock(&ses->session_mutex);
  1852. /* problem -- put our ses reference */
  1853. cifs_put_smb_ses(ses);
  1854. FreeXid(xid);
  1855. return ERR_PTR(rc);
  1856. }
  1857. if (ses->need_reconnect) {
  1858. cFYI(1, "Session needs reconnect");
  1859. rc = cifs_setup_session(xid, ses,
  1860. volume_info->local_nls);
  1861. if (rc) {
  1862. mutex_unlock(&ses->session_mutex);
  1863. /* problem -- put our reference */
  1864. cifs_put_smb_ses(ses);
  1865. FreeXid(xid);
  1866. return ERR_PTR(rc);
  1867. }
  1868. }
  1869. mutex_unlock(&ses->session_mutex);
  1870. /* existing SMB ses has a server reference already */
  1871. cifs_put_tcp_session(server);
  1872. FreeXid(xid);
  1873. return ses;
  1874. }
  1875. cFYI(1, "Existing smb sess not found");
  1876. ses = sesInfoAlloc();
  1877. if (ses == NULL)
  1878. goto get_ses_fail;
  1879. /* new SMB session uses our server ref */
  1880. ses->server = server;
  1881. if (server->dstaddr.ss_family == AF_INET6)
  1882. sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
  1883. else
  1884. sprintf(ses->serverName, "%pI4", &addr->sin_addr);
  1885. if (volume_info->username) {
  1886. ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
  1887. if (!ses->user_name)
  1888. goto get_ses_fail;
  1889. }
  1890. /* volume_info->password freed at unmount */
  1891. if (volume_info->password) {
  1892. ses->password = kstrdup(volume_info->password, GFP_KERNEL);
  1893. if (!ses->password)
  1894. goto get_ses_fail;
  1895. }
  1896. if (volume_info->domainname) {
  1897. ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
  1898. if (!ses->domainName)
  1899. goto get_ses_fail;
  1900. }
  1901. ses->cred_uid = volume_info->cred_uid;
  1902. ses->linux_uid = volume_info->linux_uid;
  1903. /* ntlmv2 is much stronger than ntlm security, and has been broadly
  1904. supported for many years, time to update default security mechanism */
  1905. if ((volume_info->secFlg == 0) && warned_on_ntlm == false) {
  1906. warned_on_ntlm = true;
  1907. cERROR(1, "default security mechanism requested. The default "
  1908. "security mechanism will be upgraded from ntlm to "
  1909. "ntlmv2 in kernel release 3.2");
  1910. }
  1911. ses->overrideSecFlg = volume_info->secFlg;
  1912. mutex_lock(&ses->session_mutex);
  1913. rc = cifs_negotiate_protocol(xid, ses);
  1914. if (!rc)
  1915. rc = cifs_setup_session(xid, ses, volume_info->local_nls);
  1916. mutex_unlock(&ses->session_mutex);
  1917. if (rc)
  1918. goto get_ses_fail;
  1919. /* success, put it on the list */
  1920. spin_lock(&cifs_tcp_ses_lock);
  1921. list_add(&ses->smb_ses_list, &server->smb_ses_list);
  1922. spin_unlock(&cifs_tcp_ses_lock);
  1923. FreeXid(xid);
  1924. return ses;
  1925. get_ses_fail:
  1926. sesInfoFree(ses);
  1927. FreeXid(xid);
  1928. return ERR_PTR(rc);
  1929. }
  1930. static int match_tcon(struct cifs_tcon *tcon, const char *unc)
  1931. {
  1932. if (tcon->tidStatus == CifsExiting)
  1933. return 0;
  1934. if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
  1935. return 0;
  1936. return 1;
  1937. }
  1938. static struct cifs_tcon *
  1939. cifs_find_tcon(struct cifs_ses *ses, const char *unc)
  1940. {
  1941. struct list_head *tmp;
  1942. struct cifs_tcon *tcon;
  1943. spin_lock(&cifs_tcp_ses_lock);
  1944. list_for_each(tmp, &ses->tcon_list) {
  1945. tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
  1946. if (!match_tcon(tcon, unc))
  1947. continue;
  1948. ++tcon->tc_count;
  1949. spin_unlock(&cifs_tcp_ses_lock);
  1950. return tcon;
  1951. }
  1952. spin_unlock(&cifs_tcp_ses_lock);
  1953. return NULL;
  1954. }
  1955. static void
  1956. cifs_put_tcon(struct cifs_tcon *tcon)
  1957. {
  1958. int xid;
  1959. struct cifs_ses *ses = tcon->ses;
  1960. cFYI(1, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  1961. spin_lock(&cifs_tcp_ses_lock);
  1962. if (--tcon->tc_count > 0) {
  1963. spin_unlock(&cifs_tcp_ses_lock);
  1964. return;
  1965. }
  1966. list_del_init(&tcon->tcon_list);
  1967. spin_unlock(&cifs_tcp_ses_lock);
  1968. xid = GetXid();
  1969. CIFSSMBTDis(xid, tcon);
  1970. _FreeXid(xid);
  1971. cifs_fscache_release_super_cookie(tcon);
  1972. tconInfoFree(tcon);
  1973. cifs_put_smb_ses(ses);
  1974. }
  1975. static struct cifs_tcon *
  1976. cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
  1977. {
  1978. int rc, xid;
  1979. struct cifs_tcon *tcon;
  1980. tcon = cifs_find_tcon(ses, volume_info->UNC);
  1981. if (tcon) {
  1982. cFYI(1, "Found match on UNC path");
  1983. /* existing tcon already has a reference */
  1984. cifs_put_smb_ses(ses);
  1985. if (tcon->seal != volume_info->seal)
  1986. cERROR(1, "transport encryption setting "
  1987. "conflicts with existing tid");
  1988. return tcon;
  1989. }
  1990. tcon = tconInfoAlloc();
  1991. if (tcon == NULL) {
  1992. rc = -ENOMEM;
  1993. goto out_fail;
  1994. }
  1995. tcon->ses = ses;
  1996. if (volume_info->password) {
  1997. tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
  1998. if (!tcon->password) {
  1999. rc = -ENOMEM;
  2000. goto out_fail;
  2001. }
  2002. }
  2003. if (strchr(volume_info->UNC + 3, '\\') == NULL
  2004. && strchr(volume_info->UNC + 3, '/') == NULL) {
  2005. cERROR(1, "Missing share name");
  2006. rc = -ENODEV;
  2007. goto out_fail;
  2008. }
  2009. /* BB Do we need to wrap session_mutex around
  2010. * this TCon call and Unix SetFS as
  2011. * we do on SessSetup and reconnect? */
  2012. xid = GetXid();
  2013. rc = CIFSTCon(xid, ses, volume_info->UNC, tcon, volume_info->local_nls);
  2014. FreeXid(xid);
  2015. cFYI(1, "CIFS Tcon rc = %d", rc);
  2016. if (rc)
  2017. goto out_fail;
  2018. if (volume_info->nodfs) {
  2019. tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
  2020. cFYI(1, "DFS disabled (%d)", tcon->Flags);
  2021. }
  2022. tcon->seal = volume_info->seal;
  2023. /* we can have only one retry value for a connection
  2024. to a share so for resources mounted more than once
  2025. to the same server share the last value passed in
  2026. for the retry flag is used */
  2027. tcon->retry = volume_info->retry;
  2028. tcon->nocase = volume_info->nocase;
  2029. tcon->local_lease = volume_info->local_lease;
  2030. spin_lock(&cifs_tcp_ses_lock);
  2031. list_add(&tcon->tcon_list, &ses->tcon_list);
  2032. spin_unlock(&cifs_tcp_ses_lock);
  2033. cifs_fscache_get_super_cookie(tcon);
  2034. return tcon;
  2035. out_fail:
  2036. tconInfoFree(tcon);
  2037. return ERR_PTR(rc);
  2038. }
  2039. void
  2040. cifs_put_tlink(struct tcon_link *tlink)
  2041. {
  2042. if (!tlink || IS_ERR(tlink))
  2043. return;
  2044. if (!atomic_dec_and_test(&tlink->tl_count) ||
  2045. test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
  2046. tlink->tl_time = jiffies;
  2047. return;
  2048. }
  2049. if (!IS_ERR(tlink_tcon(tlink)))
  2050. cifs_put_tcon(tlink_tcon(tlink));
  2051. kfree(tlink);
  2052. return;
  2053. }
  2054. static inline struct tcon_link *
  2055. cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
  2056. {
  2057. return cifs_sb->master_tlink;
  2058. }
  2059. static int
  2060. compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
  2061. {
  2062. struct cifs_sb_info *old = CIFS_SB(sb);
  2063. struct cifs_sb_info *new = mnt_data->cifs_sb;
  2064. if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
  2065. return 0;
  2066. if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
  2067. (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
  2068. return 0;
  2069. /*
  2070. * We want to share sb only if we don't specify an r/wsize or
  2071. * specified r/wsize is greater than or equal to existing one.
  2072. */
  2073. if (new->wsize && new->wsize < old->wsize)
  2074. return 0;
  2075. if (new->rsize && new->rsize < old->rsize)
  2076. return 0;
  2077. if (old->mnt_uid != new->mnt_uid || old->mnt_gid != new->mnt_gid)
  2078. return 0;
  2079. if (old->mnt_file_mode != new->mnt_file_mode ||
  2080. old->mnt_dir_mode != new->mnt_dir_mode)
  2081. return 0;
  2082. if (strcmp(old->local_nls->charset, new->local_nls->charset))
  2083. return 0;
  2084. if (old->actimeo != new->actimeo)
  2085. return 0;
  2086. return 1;
  2087. }
  2088. int
  2089. cifs_match_super(struct super_block *sb, void *data)
  2090. {
  2091. struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
  2092. struct smb_vol *volume_info;
  2093. struct cifs_sb_info *cifs_sb;
  2094. struct TCP_Server_Info *tcp_srv;
  2095. struct cifs_ses *ses;
  2096. struct cifs_tcon *tcon;
  2097. struct tcon_link *tlink;
  2098. struct sockaddr_storage addr;
  2099. int rc = 0;
  2100. memset(&addr, 0, sizeof(struct sockaddr_storage));
  2101. spin_lock(&cifs_tcp_ses_lock);
  2102. cifs_sb = CIFS_SB(sb);
  2103. tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  2104. if (IS_ERR(tlink)) {
  2105. spin_unlock(&cifs_tcp_ses_lock);
  2106. return rc;
  2107. }
  2108. tcon = tlink_tcon(tlink);
  2109. ses = tcon->ses;
  2110. tcp_srv = ses->server;
  2111. volume_info = mnt_data->vol;
  2112. if (!volume_info->UNCip || !volume_info->UNC)
  2113. goto out;
  2114. rc = cifs_fill_sockaddr((struct sockaddr *)&addr,
  2115. volume_info->UNCip,
  2116. strlen(volume_info->UNCip),
  2117. volume_info->port);
  2118. if (!rc)
  2119. goto out;
  2120. if (!match_server(tcp_srv, (struct sockaddr *)&addr, volume_info) ||
  2121. !match_session(ses, volume_info) ||
  2122. !match_tcon(tcon, volume_info->UNC)) {
  2123. rc = 0;
  2124. goto out;
  2125. }
  2126. rc = compare_mount_options(sb, mnt_data);
  2127. out:
  2128. spin_unlock(&cifs_tcp_ses_lock);
  2129. cifs_put_tlink(tlink);
  2130. return rc;
  2131. }
  2132. int
  2133. get_dfs_path(int xid, struct cifs_ses *pSesInfo, const char *old_path,
  2134. const struct nls_table *nls_codepage, unsigned int *pnum_referrals,
  2135. struct dfs_info3_param **preferrals, int remap)
  2136. {
  2137. char *temp_unc;
  2138. int rc = 0;
  2139. *pnum_referrals = 0;
  2140. *preferrals = NULL;
  2141. if (pSesInfo->ipc_tid == 0) {
  2142. temp_unc = kmalloc(2 /* for slashes */ +
  2143. strnlen(pSesInfo->serverName,
  2144. SERVER_NAME_LEN_WITH_NULL * 2)
  2145. + 1 + 4 /* slash IPC$ */ + 2,
  2146. GFP_KERNEL);
  2147. if (temp_unc == NULL)
  2148. return -ENOMEM;
  2149. temp_unc[0] = '\\';
  2150. temp_unc[1] = '\\';
  2151. strcpy(temp_unc + 2, pSesInfo->serverName);
  2152. strcpy(temp_unc + 2 + strlen(pSesInfo->serverName), "\\IPC$");
  2153. rc = CIFSTCon(xid, pSesInfo, temp_unc, NULL, nls_codepage);
  2154. cFYI(1, "CIFS Tcon rc = %d ipc_tid = %d", rc, pSesInfo->ipc_tid);
  2155. kfree(temp_unc);
  2156. }
  2157. if (rc == 0)
  2158. rc = CIFSGetDFSRefer(xid, pSesInfo, old_path, preferrals,
  2159. pnum_referrals, nls_codepage, remap);
  2160. /* BB map targetUNCs to dfs_info3 structures, here or
  2161. in CIFSGetDFSRefer BB */
  2162. return rc;
  2163. }
  2164. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2165. static struct lock_class_key cifs_key[2];
  2166. static struct lock_class_key cifs_slock_key[2];
  2167. static inline void
  2168. cifs_reclassify_socket4(struct socket *sock)
  2169. {
  2170. struct sock *sk = sock->sk;
  2171. BUG_ON(sock_owned_by_user(sk));
  2172. sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
  2173. &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
  2174. }
  2175. static inline void
  2176. cifs_reclassify_socket6(struct socket *sock)
  2177. {
  2178. struct sock *sk = sock->sk;
  2179. BUG_ON(sock_owned_by_user(sk));
  2180. sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
  2181. &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
  2182. }
  2183. #else
  2184. static inline void
  2185. cifs_reclassify_socket4(struct socket *sock)
  2186. {
  2187. }
  2188. static inline void
  2189. cifs_reclassify_socket6(struct socket *sock)
  2190. {
  2191. }
  2192. #endif
  2193. /* See RFC1001 section 14 on representation of Netbios names */
  2194. static void rfc1002mangle(char *target, char *source, unsigned int length)
  2195. {
  2196. unsigned int i, j;
  2197. for (i = 0, j = 0; i < (length); i++) {
  2198. /* mask a nibble at a time and encode */
  2199. target[j] = 'A' + (0x0F & (source[i] >> 4));
  2200. target[j+1] = 'A' + (0x0F & source[i]);
  2201. j += 2;
  2202. }
  2203. }
  2204. static int
  2205. bind_socket(struct TCP_Server_Info *server)
  2206. {
  2207. int rc = 0;
  2208. if (server->srcaddr.ss_family != AF_UNSPEC) {
  2209. /* Bind to the specified local IP address */
  2210. struct socket *socket = server->ssocket;
  2211. rc = socket->ops->bind(socket,
  2212. (struct sockaddr *) &server->srcaddr,
  2213. sizeof(server->srcaddr));
  2214. if (rc < 0) {
  2215. struct sockaddr_in *saddr4;
  2216. struct sockaddr_in6 *saddr6;
  2217. saddr4 = (struct sockaddr_in *)&server->srcaddr;
  2218. saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
  2219. if (saddr6->sin6_family == AF_INET6)
  2220. cERROR(1, "cifs: "
  2221. "Failed to bind to: %pI6c, error: %d\n",
  2222. &saddr6->sin6_addr, rc);
  2223. else
  2224. cERROR(1, "cifs: "
  2225. "Failed to bind to: %pI4, error: %d\n",
  2226. &saddr4->sin_addr.s_addr, rc);
  2227. }
  2228. }
  2229. return rc;
  2230. }
  2231. static int
  2232. ip_rfc1001_connect(struct TCP_Server_Info *server)
  2233. {
  2234. int rc = 0;
  2235. /*
  2236. * some servers require RFC1001 sessinit before sending
  2237. * negprot - BB check reconnection in case where second
  2238. * sessinit is sent but no second negprot
  2239. */
  2240. struct rfc1002_session_packet *ses_init_buf;
  2241. struct smb_hdr *smb_buf;
  2242. ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
  2243. GFP_KERNEL);
  2244. if (ses_init_buf) {
  2245. ses_init_buf->trailer.session_req.called_len = 32;
  2246. if (server->server_RFC1001_name &&
  2247. server->server_RFC1001_name[0] != 0)
  2248. rfc1002mangle(ses_init_buf->trailer.
  2249. session_req.called_name,
  2250. server->server_RFC1001_name,
  2251. RFC1001_NAME_LEN_WITH_NULL);
  2252. else
  2253. rfc1002mangle(ses_init_buf->trailer.
  2254. session_req.called_name,
  2255. DEFAULT_CIFS_CALLED_NAME,
  2256. RFC1001_NAME_LEN_WITH_NULL);
  2257. ses_init_buf->trailer.session_req.calling_len = 32;
  2258. /*
  2259. * calling name ends in null (byte 16) from old smb
  2260. * convention.
  2261. */
  2262. if (server->workstation_RFC1001_name &&
  2263. server->workstation_RFC1001_name[0] != 0)
  2264. rfc1002mangle(ses_init_buf->trailer.
  2265. session_req.calling_name,
  2266. server->workstation_RFC1001_name,
  2267. RFC1001_NAME_LEN_WITH_NULL);
  2268. else
  2269. rfc1002mangle(ses_init_buf->trailer.
  2270. session_req.calling_name,
  2271. "LINUX_CIFS_CLNT",
  2272. RFC1001_NAME_LEN_WITH_NULL);
  2273. ses_init_buf->trailer.session_req.scope1 = 0;
  2274. ses_init_buf->trailer.session_req.scope2 = 0;
  2275. smb_buf = (struct smb_hdr *)ses_init_buf;
  2276. /* sizeof RFC1002_SESSION_REQUEST with no scope */
  2277. smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
  2278. rc = smb_send(server, smb_buf, 0x44);
  2279. kfree(ses_init_buf);
  2280. /*
  2281. * RFC1001 layer in at least one server
  2282. * requires very short break before negprot
  2283. * presumably because not expecting negprot
  2284. * to follow so fast. This is a simple
  2285. * solution that works without
  2286. * complicating the code and causes no
  2287. * significant slowing down on mount
  2288. * for everyone else
  2289. */
  2290. usleep_range(1000, 2000);
  2291. }
  2292. /*
  2293. * else the negprot may still work without this
  2294. * even though malloc failed
  2295. */
  2296. return rc;
  2297. }
  2298. static int
  2299. generic_ip_connect(struct TCP_Server_Info *server)
  2300. {
  2301. int rc = 0;
  2302. __be16 sport;
  2303. int slen, sfamily;
  2304. struct socket *socket = server->ssocket;
  2305. struct sockaddr *saddr;
  2306. saddr = (struct sockaddr *) &server->dstaddr;
  2307. if (server->dstaddr.ss_family == AF_INET6) {
  2308. sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
  2309. slen = sizeof(struct sockaddr_in6);
  2310. sfamily = AF_INET6;
  2311. } else {
  2312. sport = ((struct sockaddr_in *) saddr)->sin_port;
  2313. slen = sizeof(struct sockaddr_in);
  2314. sfamily = AF_INET;
  2315. }
  2316. if (socket == NULL) {
  2317. rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
  2318. IPPROTO_TCP, &socket, 1);
  2319. if (rc < 0) {
  2320. cERROR(1, "Error %d creating socket", rc);
  2321. server->ssocket = NULL;
  2322. return rc;
  2323. }
  2324. /* BB other socket options to set KEEPALIVE, NODELAY? */
  2325. cFYI(1, "Socket created");
  2326. server->ssocket = socket;
  2327. socket->sk->sk_allocation = GFP_NOFS;
  2328. if (sfamily == AF_INET6)
  2329. cifs_reclassify_socket6(socket);
  2330. else
  2331. cifs_reclassify_socket4(socket);
  2332. }
  2333. rc = bind_socket(server);
  2334. if (rc < 0)
  2335. return rc;
  2336. /*
  2337. * Eventually check for other socket options to change from
  2338. * the default. sock_setsockopt not used because it expects
  2339. * user space buffer
  2340. */
  2341. socket->sk->sk_rcvtimeo = 7 * HZ;
  2342. socket->sk->sk_sndtimeo = 5 * HZ;
  2343. /* make the bufsizes depend on wsize/rsize and max requests */
  2344. if (server->noautotune) {
  2345. if (socket->sk->sk_sndbuf < (200 * 1024))
  2346. socket->sk->sk_sndbuf = 200 * 1024;
  2347. if (socket->sk->sk_rcvbuf < (140 * 1024))
  2348. socket->sk->sk_rcvbuf = 140 * 1024;
  2349. }
  2350. if (server->tcp_nodelay) {
  2351. int val = 1;
  2352. rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
  2353. (char *)&val, sizeof(val));
  2354. if (rc)
  2355. cFYI(1, "set TCP_NODELAY socket option error %d", rc);
  2356. }
  2357. cFYI(1, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx",
  2358. socket->sk->sk_sndbuf,
  2359. socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
  2360. rc = socket->ops->connect(socket, saddr, slen, 0);
  2361. if (rc < 0) {
  2362. cFYI(1, "Error %d connecting to server", rc);
  2363. sock_release(socket);
  2364. server->ssocket = NULL;
  2365. return rc;
  2366. }
  2367. if (sport == htons(RFC1001_PORT))
  2368. rc = ip_rfc1001_connect(server);
  2369. return rc;
  2370. }
  2371. static int
  2372. ip_connect(struct TCP_Server_Info *server)
  2373. {
  2374. __be16 *sport;
  2375. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  2376. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  2377. if (server->dstaddr.ss_family == AF_INET6)
  2378. sport = &addr6->sin6_port;
  2379. else
  2380. sport = &addr->sin_port;
  2381. if (*sport == 0) {
  2382. int rc;
  2383. /* try with 445 port at first */
  2384. *sport = htons(CIFS_PORT);
  2385. rc = generic_ip_connect(server);
  2386. if (rc >= 0)
  2387. return rc;
  2388. /* if it failed, try with 139 port */
  2389. *sport = htons(RFC1001_PORT);
  2390. }
  2391. return generic_ip_connect(server);
  2392. }
  2393. void reset_cifs_unix_caps(int xid, struct cifs_tcon *tcon,
  2394. struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
  2395. {
  2396. /* if we are reconnecting then should we check to see if
  2397. * any requested capabilities changed locally e.g. via
  2398. * remount but we can not do much about it here
  2399. * if they have (even if we could detect it by the following)
  2400. * Perhaps we could add a backpointer to array of sb from tcon
  2401. * or if we change to make all sb to same share the same
  2402. * sb as NFS - then we only have one backpointer to sb.
  2403. * What if we wanted to mount the server share twice once with
  2404. * and once without posixacls or posix paths? */
  2405. __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2406. if (vol_info && vol_info->no_linux_ext) {
  2407. tcon->fsUnixInfo.Capability = 0;
  2408. tcon->unix_ext = 0; /* Unix Extensions disabled */
  2409. cFYI(1, "Linux protocol extensions disabled");
  2410. return;
  2411. } else if (vol_info)
  2412. tcon->unix_ext = 1; /* Unix Extensions supported */
  2413. if (tcon->unix_ext == 0) {
  2414. cFYI(1, "Unix extensions disabled so not set on reconnect");
  2415. return;
  2416. }
  2417. if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
  2418. __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2419. cFYI(1, "unix caps which server supports %lld", cap);
  2420. /* check for reconnect case in which we do not
  2421. want to change the mount behavior if we can avoid it */
  2422. if (vol_info == NULL) {
  2423. /* turn off POSIX ACL and PATHNAMES if not set
  2424. originally at mount time */
  2425. if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
  2426. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2427. if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2428. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2429. cERROR(1, "POSIXPATH support change");
  2430. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2431. } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2432. cERROR(1, "possible reconnect error");
  2433. cERROR(1, "server disabled POSIX path support");
  2434. }
  2435. }
  2436. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  2437. cERROR(1, "per-share encryption not supported yet");
  2438. cap &= CIFS_UNIX_CAP_MASK;
  2439. if (vol_info && vol_info->no_psx_acl)
  2440. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2441. else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
  2442. cFYI(1, "negotiated posix acl support");
  2443. if (cifs_sb)
  2444. cifs_sb->mnt_cifs_flags |=
  2445. CIFS_MOUNT_POSIXACL;
  2446. }
  2447. if (vol_info && vol_info->posix_paths == 0)
  2448. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2449. else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
  2450. cFYI(1, "negotiate posix pathnames");
  2451. if (cifs_sb)
  2452. cifs_sb->mnt_cifs_flags |=
  2453. CIFS_MOUNT_POSIX_PATHS;
  2454. }
  2455. cFYI(1, "Negotiate caps 0x%x", (int)cap);
  2456. #ifdef CONFIG_CIFS_DEBUG2
  2457. if (cap & CIFS_UNIX_FCNTL_CAP)
  2458. cFYI(1, "FCNTL cap");
  2459. if (cap & CIFS_UNIX_EXTATTR_CAP)
  2460. cFYI(1, "EXTATTR cap");
  2461. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2462. cFYI(1, "POSIX path cap");
  2463. if (cap & CIFS_UNIX_XATTR_CAP)
  2464. cFYI(1, "XATTR cap");
  2465. if (cap & CIFS_UNIX_POSIX_ACL_CAP)
  2466. cFYI(1, "POSIX ACL cap");
  2467. if (cap & CIFS_UNIX_LARGE_READ_CAP)
  2468. cFYI(1, "very large read cap");
  2469. if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
  2470. cFYI(1, "very large write cap");
  2471. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
  2472. cFYI(1, "transport encryption cap");
  2473. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  2474. cFYI(1, "mandatory transport encryption cap");
  2475. #endif /* CIFS_DEBUG2 */
  2476. if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
  2477. if (vol_info == NULL) {
  2478. cFYI(1, "resetting capabilities failed");
  2479. } else
  2480. cERROR(1, "Negotiating Unix capabilities "
  2481. "with the server failed. Consider "
  2482. "mounting with the Unix Extensions\n"
  2483. "disabled, if problems are found, "
  2484. "by specifying the nounix mount "
  2485. "option.");
  2486. }
  2487. }
  2488. }
  2489. void cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
  2490. struct cifs_sb_info *cifs_sb)
  2491. {
  2492. INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
  2493. spin_lock_init(&cifs_sb->tlink_tree_lock);
  2494. cifs_sb->tlink_tree = RB_ROOT;
  2495. /*
  2496. * Temporarily set r/wsize for matching superblock. If we end up using
  2497. * new sb then client will later negotiate it downward if needed.
  2498. */
  2499. cifs_sb->rsize = pvolume_info->rsize;
  2500. cifs_sb->wsize = pvolume_info->wsize;
  2501. cifs_sb->mnt_uid = pvolume_info->linux_uid;
  2502. cifs_sb->mnt_gid = pvolume_info->linux_gid;
  2503. if (pvolume_info->backupuid_specified)
  2504. cifs_sb->mnt_backupuid = pvolume_info->backupuid;
  2505. if (pvolume_info->backupgid_specified)
  2506. cifs_sb->mnt_backupgid = pvolume_info->backupgid;
  2507. cifs_sb->mnt_file_mode = pvolume_info->file_mode;
  2508. cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
  2509. cFYI(1, "file mode: 0x%x dir mode: 0x%x",
  2510. cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
  2511. cifs_sb->actimeo = pvolume_info->actimeo;
  2512. cifs_sb->local_nls = pvolume_info->local_nls;
  2513. if (pvolume_info->noperm)
  2514. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
  2515. if (pvolume_info->setuids)
  2516. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
  2517. if (pvolume_info->server_ino)
  2518. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
  2519. if (pvolume_info->remap)
  2520. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
  2521. if (pvolume_info->no_xattr)
  2522. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
  2523. if (pvolume_info->sfu_emul)
  2524. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
  2525. if (pvolume_info->nobrl)
  2526. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
  2527. if (pvolume_info->nostrictsync)
  2528. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
  2529. if (pvolume_info->mand_lock)
  2530. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
  2531. if (pvolume_info->rwpidforward)
  2532. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
  2533. if (pvolume_info->cifs_acl)
  2534. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
  2535. if (pvolume_info->backupuid_specified)
  2536. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
  2537. if (pvolume_info->backupgid_specified)
  2538. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
  2539. if (pvolume_info->override_uid)
  2540. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
  2541. if (pvolume_info->override_gid)
  2542. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
  2543. if (pvolume_info->dynperm)
  2544. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
  2545. if (pvolume_info->fsc)
  2546. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
  2547. if (pvolume_info->multiuser)
  2548. cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
  2549. CIFS_MOUNT_NO_PERM);
  2550. if (pvolume_info->strict_io)
  2551. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
  2552. if (pvolume_info->direct_io) {
  2553. cFYI(1, "mounting share using direct i/o");
  2554. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
  2555. }
  2556. if (pvolume_info->mfsymlinks) {
  2557. if (pvolume_info->sfu_emul) {
  2558. cERROR(1, "mount option mfsymlinks ignored if sfu "
  2559. "mount option is used");
  2560. } else {
  2561. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
  2562. }
  2563. }
  2564. if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
  2565. cERROR(1, "mount option dynperm ignored if cifsacl "
  2566. "mount option supported");
  2567. }
  2568. /*
  2569. * When the server supports very large reads and writes via POSIX extensions,
  2570. * we can allow up to 2^24-1, minus the size of a READ/WRITE_AND_X header, not
  2571. * including the RFC1001 length.
  2572. *
  2573. * Note that this might make for "interesting" allocation problems during
  2574. * writeback however as we have to allocate an array of pointers for the
  2575. * pages. A 16M write means ~32kb page array with PAGE_CACHE_SIZE == 4096.
  2576. *
  2577. * For reads, there is a similar problem as we need to allocate an array
  2578. * of kvecs to handle the receive, though that should only need to be done
  2579. * once.
  2580. */
  2581. #define CIFS_MAX_WSIZE ((1<<24) - 1 - sizeof(WRITE_REQ) + 4)
  2582. #define CIFS_MAX_RSIZE ((1<<24) - sizeof(READ_RSP) + 4)
  2583. /*
  2584. * When the server doesn't allow large posix writes, only allow a rsize/wsize
  2585. * of 2^17-1 minus the size of the call header. That allows for a read or
  2586. * write up to the maximum size described by RFC1002.
  2587. */
  2588. #define CIFS_MAX_RFC1002_WSIZE ((1<<17) - 1 - sizeof(WRITE_REQ) + 4)
  2589. #define CIFS_MAX_RFC1002_RSIZE ((1<<17) - 1 - sizeof(READ_RSP) + 4)
  2590. /*
  2591. * The default wsize is 1M. find_get_pages seems to return a maximum of 256
  2592. * pages in a single call. With PAGE_CACHE_SIZE == 4k, this means we can fill
  2593. * a single wsize request with a single call.
  2594. */
  2595. #define CIFS_DEFAULT_IOSIZE (1024 * 1024)
  2596. /*
  2597. * Windows only supports a max of 60k reads. Default to that when posix
  2598. * extensions aren't in force.
  2599. */
  2600. #define CIFS_DEFAULT_NON_POSIX_RSIZE (60 * 1024)
  2601. static unsigned int
  2602. cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *pvolume_info)
  2603. {
  2604. __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2605. struct TCP_Server_Info *server = tcon->ses->server;
  2606. unsigned int wsize = pvolume_info->wsize ? pvolume_info->wsize :
  2607. CIFS_DEFAULT_IOSIZE;
  2608. /* can server support 24-bit write sizes? (via UNIX extensions) */
  2609. if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
  2610. wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
  2611. /*
  2612. * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
  2613. * Limit it to max buffer offered by the server, minus the size of the
  2614. * WRITEX header, not including the 4 byte RFC1001 length.
  2615. */
  2616. if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
  2617. (!(server->capabilities & CAP_UNIX) &&
  2618. (server->sec_mode & (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED))))
  2619. wsize = min_t(unsigned int, wsize,
  2620. server->maxBuf - sizeof(WRITE_REQ) + 4);
  2621. /* hard limit of CIFS_MAX_WSIZE */
  2622. wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
  2623. return wsize;
  2624. }
  2625. static unsigned int
  2626. cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *pvolume_info)
  2627. {
  2628. __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2629. struct TCP_Server_Info *server = tcon->ses->server;
  2630. unsigned int rsize, defsize;
  2631. /*
  2632. * Set default value...
  2633. *
  2634. * HACK alert! Ancient servers have very small buffers. Even though
  2635. * MS-CIFS indicates that servers are only limited by the client's
  2636. * bufsize for reads, testing against win98se shows that it throws
  2637. * INVALID_PARAMETER errors if you try to request too large a read.
  2638. *
  2639. * If the server advertises a MaxBufferSize of less than one page,
  2640. * assume that it also can't satisfy reads larger than that either.
  2641. *
  2642. * FIXME: Is there a better heuristic for this?
  2643. */
  2644. if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
  2645. defsize = CIFS_DEFAULT_IOSIZE;
  2646. else if (server->capabilities & CAP_LARGE_READ_X)
  2647. defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
  2648. else if (server->maxBuf >= PAGE_CACHE_SIZE)
  2649. defsize = CIFSMaxBufSize;
  2650. else
  2651. defsize = server->maxBuf - sizeof(READ_RSP);
  2652. rsize = pvolume_info->rsize ? pvolume_info->rsize : defsize;
  2653. /*
  2654. * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
  2655. * the client's MaxBufferSize.
  2656. */
  2657. if (!(server->capabilities & CAP_LARGE_READ_X))
  2658. rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
  2659. /* hard limit of CIFS_MAX_RSIZE */
  2660. rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
  2661. return rsize;
  2662. }
  2663. static int
  2664. is_path_accessible(int xid, struct cifs_tcon *tcon,
  2665. struct cifs_sb_info *cifs_sb, const char *full_path)
  2666. {
  2667. int rc;
  2668. FILE_ALL_INFO *pfile_info;
  2669. pfile_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  2670. if (pfile_info == NULL)
  2671. return -ENOMEM;
  2672. rc = CIFSSMBQPathInfo(xid, tcon, full_path, pfile_info,
  2673. 0 /* not legacy */, cifs_sb->local_nls,
  2674. cifs_sb->mnt_cifs_flags &
  2675. CIFS_MOUNT_MAP_SPECIAL_CHR);
  2676. if (rc == -EOPNOTSUPP || rc == -EINVAL)
  2677. rc = SMBQueryInformation(xid, tcon, full_path, pfile_info,
  2678. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  2679. CIFS_MOUNT_MAP_SPECIAL_CHR);
  2680. kfree(pfile_info);
  2681. return rc;
  2682. }
  2683. static void
  2684. cleanup_volume_info_contents(struct smb_vol *volume_info)
  2685. {
  2686. kfree(volume_info->username);
  2687. kzfree(volume_info->password);
  2688. if (volume_info->UNCip != volume_info->UNC + 2)
  2689. kfree(volume_info->UNCip);
  2690. kfree(volume_info->UNC);
  2691. kfree(volume_info->domainname);
  2692. kfree(volume_info->iocharset);
  2693. kfree(volume_info->prepath);
  2694. }
  2695. void
  2696. cifs_cleanup_volume_info(struct smb_vol *volume_info)
  2697. {
  2698. if (!volume_info)
  2699. return;
  2700. cleanup_volume_info_contents(volume_info);
  2701. kfree(volume_info);
  2702. }
  2703. #ifdef CONFIG_CIFS_DFS_UPCALL
  2704. /* build_path_to_root returns full path to root when
  2705. * we do not have an exiting connection (tcon) */
  2706. static char *
  2707. build_unc_path_to_root(const struct smb_vol *vol,
  2708. const struct cifs_sb_info *cifs_sb)
  2709. {
  2710. char *full_path, *pos;
  2711. unsigned int pplen = vol->prepath ? strlen(vol->prepath) : 0;
  2712. unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
  2713. full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
  2714. if (full_path == NULL)
  2715. return ERR_PTR(-ENOMEM);
  2716. strncpy(full_path, vol->UNC, unc_len);
  2717. pos = full_path + unc_len;
  2718. if (pplen) {
  2719. strncpy(pos, vol->prepath, pplen);
  2720. pos += pplen;
  2721. }
  2722. *pos = '\0'; /* add trailing null */
  2723. convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
  2724. cFYI(1, "%s: full_path=%s", __func__, full_path);
  2725. return full_path;
  2726. }
  2727. /*
  2728. * Perform a dfs referral query for a share and (optionally) prefix
  2729. *
  2730. * If a referral is found, cifs_sb->mountdata will be (re-)allocated
  2731. * to a string containing updated options for the submount. Otherwise it
  2732. * will be left untouched.
  2733. *
  2734. * Returns the rc from get_dfs_path to the caller, which can be used to
  2735. * determine whether there were referrals.
  2736. */
  2737. static int
  2738. expand_dfs_referral(int xid, struct cifs_ses *pSesInfo,
  2739. struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
  2740. int check_prefix)
  2741. {
  2742. int rc;
  2743. unsigned int num_referrals = 0;
  2744. struct dfs_info3_param *referrals = NULL;
  2745. char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
  2746. full_path = build_unc_path_to_root(volume_info, cifs_sb);
  2747. if (IS_ERR(full_path))
  2748. return PTR_ERR(full_path);
  2749. /* For DFS paths, skip the first '\' of the UNC */
  2750. ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
  2751. rc = get_dfs_path(xid, pSesInfo , ref_path, cifs_sb->local_nls,
  2752. &num_referrals, &referrals,
  2753. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  2754. if (!rc && num_referrals > 0) {
  2755. char *fake_devname = NULL;
  2756. mdata = cifs_compose_mount_options(cifs_sb->mountdata,
  2757. full_path + 1, referrals,
  2758. &fake_devname);
  2759. free_dfs_info_array(referrals, num_referrals);
  2760. if (IS_ERR(mdata)) {
  2761. rc = PTR_ERR(mdata);
  2762. mdata = NULL;
  2763. } else {
  2764. cleanup_volume_info_contents(volume_info);
  2765. memset(volume_info, '\0', sizeof(*volume_info));
  2766. rc = cifs_setup_volume_info(volume_info, mdata,
  2767. fake_devname);
  2768. }
  2769. kfree(fake_devname);
  2770. kfree(cifs_sb->mountdata);
  2771. cifs_sb->mountdata = mdata;
  2772. }
  2773. kfree(full_path);
  2774. return rc;
  2775. }
  2776. #endif
  2777. static int
  2778. cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
  2779. const char *devname)
  2780. {
  2781. int rc = 0;
  2782. if (cifs_parse_mount_options(mount_data, devname, volume_info))
  2783. return -EINVAL;
  2784. if (volume_info->nullauth) {
  2785. cFYI(1, "null user");
  2786. volume_info->username = kzalloc(1, GFP_KERNEL);
  2787. if (volume_info->username == NULL)
  2788. return -ENOMEM;
  2789. } else if (volume_info->username) {
  2790. /* BB fixme parse for domain name here */
  2791. cFYI(1, "Username: %s", volume_info->username);
  2792. } else {
  2793. cifserror("No username specified");
  2794. /* In userspace mount helper we can get user name from alternate
  2795. locations such as env variables and files on disk */
  2796. return -EINVAL;
  2797. }
  2798. /* this is needed for ASCII cp to Unicode converts */
  2799. if (volume_info->iocharset == NULL) {
  2800. /* load_nls_default cannot return null */
  2801. volume_info->local_nls = load_nls_default();
  2802. } else {
  2803. volume_info->local_nls = load_nls(volume_info->iocharset);
  2804. if (volume_info->local_nls == NULL) {
  2805. cERROR(1, "CIFS mount error: iocharset %s not found",
  2806. volume_info->iocharset);
  2807. return -ELIBACC;
  2808. }
  2809. }
  2810. return rc;
  2811. }
  2812. struct smb_vol *
  2813. cifs_get_volume_info(char *mount_data, const char *devname)
  2814. {
  2815. int rc;
  2816. struct smb_vol *volume_info;
  2817. volume_info = kzalloc(sizeof(struct smb_vol), GFP_KERNEL);
  2818. if (!volume_info)
  2819. return ERR_PTR(-ENOMEM);
  2820. rc = cifs_setup_volume_info(volume_info, mount_data, devname);
  2821. if (rc) {
  2822. cifs_cleanup_volume_info(volume_info);
  2823. volume_info = ERR_PTR(rc);
  2824. }
  2825. return volume_info;
  2826. }
  2827. /* make sure ra_pages is a multiple of rsize */
  2828. static inline unsigned int
  2829. cifs_ra_pages(struct cifs_sb_info *cifs_sb)
  2830. {
  2831. unsigned int reads;
  2832. unsigned int rsize_pages = cifs_sb->rsize / PAGE_CACHE_SIZE;
  2833. if (rsize_pages >= default_backing_dev_info.ra_pages)
  2834. return default_backing_dev_info.ra_pages;
  2835. else if (rsize_pages == 0)
  2836. return rsize_pages;
  2837. reads = default_backing_dev_info.ra_pages / rsize_pages;
  2838. return reads * rsize_pages;
  2839. }
  2840. int
  2841. cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
  2842. {
  2843. int rc = 0;
  2844. int xid;
  2845. struct cifs_ses *pSesInfo;
  2846. struct cifs_tcon *tcon;
  2847. struct TCP_Server_Info *srvTcp;
  2848. char *full_path;
  2849. struct tcon_link *tlink;
  2850. #ifdef CONFIG_CIFS_DFS_UPCALL
  2851. int referral_walks_count = 0;
  2852. #endif
  2853. rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs", BDI_CAP_MAP_COPY);
  2854. if (rc)
  2855. return rc;
  2856. #ifdef CONFIG_CIFS_DFS_UPCALL
  2857. try_mount_again:
  2858. /* cleanup activities if we're chasing a referral */
  2859. if (referral_walks_count) {
  2860. if (tcon)
  2861. cifs_put_tcon(tcon);
  2862. else if (pSesInfo)
  2863. cifs_put_smb_ses(pSesInfo);
  2864. FreeXid(xid);
  2865. }
  2866. #endif
  2867. tcon = NULL;
  2868. pSesInfo = NULL;
  2869. srvTcp = NULL;
  2870. full_path = NULL;
  2871. tlink = NULL;
  2872. xid = GetXid();
  2873. /* get a reference to a tcp session */
  2874. srvTcp = cifs_get_tcp_session(volume_info);
  2875. if (IS_ERR(srvTcp)) {
  2876. rc = PTR_ERR(srvTcp);
  2877. bdi_destroy(&cifs_sb->bdi);
  2878. goto out;
  2879. }
  2880. /* get a reference to a SMB session */
  2881. pSesInfo = cifs_get_smb_ses(srvTcp, volume_info);
  2882. if (IS_ERR(pSesInfo)) {
  2883. rc = PTR_ERR(pSesInfo);
  2884. pSesInfo = NULL;
  2885. goto mount_fail_check;
  2886. }
  2887. /* search for existing tcon to this server share */
  2888. tcon = cifs_get_tcon(pSesInfo, volume_info);
  2889. if (IS_ERR(tcon)) {
  2890. rc = PTR_ERR(tcon);
  2891. tcon = NULL;
  2892. goto remote_path_check;
  2893. }
  2894. /* tell server which Unix caps we support */
  2895. if (tcon->ses->capabilities & CAP_UNIX) {
  2896. /* reset of caps checks mount to see if unix extensions
  2897. disabled for just this mount */
  2898. reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
  2899. if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
  2900. (le64_to_cpu(tcon->fsUnixInfo.Capability) &
  2901. CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
  2902. rc = -EACCES;
  2903. goto mount_fail_check;
  2904. }
  2905. } else
  2906. tcon->unix_ext = 0; /* server does not support them */
  2907. /* do not care if following two calls succeed - informational */
  2908. if (!tcon->ipc) {
  2909. CIFSSMBQFSDeviceInfo(xid, tcon);
  2910. CIFSSMBQFSAttributeInfo(xid, tcon);
  2911. }
  2912. cifs_sb->wsize = cifs_negotiate_wsize(tcon, volume_info);
  2913. cifs_sb->rsize = cifs_negotiate_rsize(tcon, volume_info);
  2914. /* tune readahead according to rsize */
  2915. cifs_sb->bdi.ra_pages = cifs_ra_pages(cifs_sb);
  2916. remote_path_check:
  2917. #ifdef CONFIG_CIFS_DFS_UPCALL
  2918. /*
  2919. * Perform an unconditional check for whether there are DFS
  2920. * referrals for this path without prefix, to provide support
  2921. * for DFS referrals from w2k8 servers which don't seem to respond
  2922. * with PATH_NOT_COVERED to requests that include the prefix.
  2923. * Chase the referral if found, otherwise continue normally.
  2924. */
  2925. if (referral_walks_count == 0) {
  2926. int refrc = expand_dfs_referral(xid, pSesInfo, volume_info,
  2927. cifs_sb, false);
  2928. if (!refrc) {
  2929. referral_walks_count++;
  2930. goto try_mount_again;
  2931. }
  2932. }
  2933. #endif
  2934. /* check if a whole path is not remote */
  2935. if (!rc && tcon) {
  2936. /* build_path_to_root works only when we have a valid tcon */
  2937. full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon);
  2938. if (full_path == NULL) {
  2939. rc = -ENOMEM;
  2940. goto mount_fail_check;
  2941. }
  2942. rc = is_path_accessible(xid, tcon, cifs_sb, full_path);
  2943. if (rc != 0 && rc != -EREMOTE) {
  2944. kfree(full_path);
  2945. goto mount_fail_check;
  2946. }
  2947. kfree(full_path);
  2948. }
  2949. /* get referral if needed */
  2950. if (rc == -EREMOTE) {
  2951. #ifdef CONFIG_CIFS_DFS_UPCALL
  2952. if (referral_walks_count > MAX_NESTED_LINKS) {
  2953. /*
  2954. * BB: when we implement proper loop detection,
  2955. * we will remove this check. But now we need it
  2956. * to prevent an indefinite loop if 'DFS tree' is
  2957. * misconfigured (i.e. has loops).
  2958. */
  2959. rc = -ELOOP;
  2960. goto mount_fail_check;
  2961. }
  2962. rc = expand_dfs_referral(xid, pSesInfo, volume_info, cifs_sb,
  2963. true);
  2964. if (!rc) {
  2965. referral_walks_count++;
  2966. goto try_mount_again;
  2967. }
  2968. goto mount_fail_check;
  2969. #else /* No DFS support, return error on mount */
  2970. rc = -EOPNOTSUPP;
  2971. #endif
  2972. }
  2973. if (rc)
  2974. goto mount_fail_check;
  2975. /* now, hang the tcon off of the superblock */
  2976. tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
  2977. if (tlink == NULL) {
  2978. rc = -ENOMEM;
  2979. goto mount_fail_check;
  2980. }
  2981. tlink->tl_uid = pSesInfo->linux_uid;
  2982. tlink->tl_tcon = tcon;
  2983. tlink->tl_time = jiffies;
  2984. set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
  2985. set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  2986. cifs_sb->master_tlink = tlink;
  2987. spin_lock(&cifs_sb->tlink_tree_lock);
  2988. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  2989. spin_unlock(&cifs_sb->tlink_tree_lock);
  2990. queue_delayed_work(system_nrt_wq, &cifs_sb->prune_tlinks,
  2991. TLINK_IDLE_EXPIRE);
  2992. mount_fail_check:
  2993. /* on error free sesinfo and tcon struct if needed */
  2994. if (rc) {
  2995. /* If find_unc succeeded then rc == 0 so we can not end */
  2996. /* up accidentally freeing someone elses tcon struct */
  2997. if (tcon)
  2998. cifs_put_tcon(tcon);
  2999. else if (pSesInfo)
  3000. cifs_put_smb_ses(pSesInfo);
  3001. else
  3002. cifs_put_tcp_session(srvTcp);
  3003. bdi_destroy(&cifs_sb->bdi);
  3004. }
  3005. out:
  3006. FreeXid(xid);
  3007. return rc;
  3008. }
  3009. /*
  3010. * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
  3011. * pointer may be NULL.
  3012. */
  3013. int
  3014. CIFSTCon(unsigned int xid, struct cifs_ses *ses,
  3015. const char *tree, struct cifs_tcon *tcon,
  3016. const struct nls_table *nls_codepage)
  3017. {
  3018. struct smb_hdr *smb_buffer;
  3019. struct smb_hdr *smb_buffer_response;
  3020. TCONX_REQ *pSMB;
  3021. TCONX_RSP *pSMBr;
  3022. unsigned char *bcc_ptr;
  3023. int rc = 0;
  3024. int length;
  3025. __u16 bytes_left, count;
  3026. if (ses == NULL)
  3027. return -EIO;
  3028. smb_buffer = cifs_buf_get();
  3029. if (smb_buffer == NULL)
  3030. return -ENOMEM;
  3031. smb_buffer_response = smb_buffer;
  3032. header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
  3033. NULL /*no tid */ , 4 /*wct */ );
  3034. smb_buffer->Mid = GetNextMid(ses->server);
  3035. smb_buffer->Uid = ses->Suid;
  3036. pSMB = (TCONX_REQ *) smb_buffer;
  3037. pSMBr = (TCONX_RSP *) smb_buffer_response;
  3038. pSMB->AndXCommand = 0xFF;
  3039. pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
  3040. bcc_ptr = &pSMB->Password[0];
  3041. if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
  3042. pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
  3043. *bcc_ptr = 0; /* password is null byte */
  3044. bcc_ptr++; /* skip password */
  3045. /* already aligned so no need to do it below */
  3046. } else {
  3047. pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  3048. /* BB FIXME add code to fail this if NTLMv2 or Kerberos
  3049. specified as required (when that support is added to
  3050. the vfs in the future) as only NTLM or the much
  3051. weaker LANMAN (which we do not send by default) is accepted
  3052. by Samba (not sure whether other servers allow
  3053. NTLMv2 password here) */
  3054. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  3055. if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
  3056. (ses->server->secType == LANMAN))
  3057. calc_lanman_hash(tcon->password, ses->server->cryptkey,
  3058. ses->server->sec_mode &
  3059. SECMODE_PW_ENCRYPT ? true : false,
  3060. bcc_ptr);
  3061. else
  3062. #endif /* CIFS_WEAK_PW_HASH */
  3063. rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
  3064. bcc_ptr, nls_codepage);
  3065. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  3066. if (ses->capabilities & CAP_UNICODE) {
  3067. /* must align unicode strings */
  3068. *bcc_ptr = 0; /* null byte password */
  3069. bcc_ptr++;
  3070. }
  3071. }
  3072. if (ses->server->sec_mode &
  3073. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  3074. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  3075. if (ses->capabilities & CAP_STATUS32) {
  3076. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  3077. }
  3078. if (ses->capabilities & CAP_DFS) {
  3079. smb_buffer->Flags2 |= SMBFLG2_DFS;
  3080. }
  3081. if (ses->capabilities & CAP_UNICODE) {
  3082. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  3083. length =
  3084. cifs_strtoUCS((__le16 *) bcc_ptr, tree,
  3085. 6 /* max utf8 char length in bytes */ *
  3086. (/* server len*/ + 256 /* share len */), nls_codepage);
  3087. bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
  3088. bcc_ptr += 2; /* skip trailing null */
  3089. } else { /* ASCII */
  3090. strcpy(bcc_ptr, tree);
  3091. bcc_ptr += strlen(tree) + 1;
  3092. }
  3093. strcpy(bcc_ptr, "?????");
  3094. bcc_ptr += strlen("?????");
  3095. bcc_ptr += 1;
  3096. count = bcc_ptr - &pSMB->Password[0];
  3097. pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
  3098. pSMB->hdr.smb_buf_length) + count);
  3099. pSMB->ByteCount = cpu_to_le16(count);
  3100. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
  3101. 0);
  3102. /* above now done in SendReceive */
  3103. if ((rc == 0) && (tcon != NULL)) {
  3104. bool is_unicode;
  3105. tcon->tidStatus = CifsGood;
  3106. tcon->need_reconnect = false;
  3107. tcon->tid = smb_buffer_response->Tid;
  3108. bcc_ptr = pByteArea(smb_buffer_response);
  3109. bytes_left = get_bcc(smb_buffer_response);
  3110. length = strnlen(bcc_ptr, bytes_left - 2);
  3111. if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
  3112. is_unicode = true;
  3113. else
  3114. is_unicode = false;
  3115. /* skip service field (NB: this field is always ASCII) */
  3116. if (length == 3) {
  3117. if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
  3118. (bcc_ptr[2] == 'C')) {
  3119. cFYI(1, "IPC connection");
  3120. tcon->ipc = 1;
  3121. }
  3122. } else if (length == 2) {
  3123. if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
  3124. /* the most common case */
  3125. cFYI(1, "disk share connection");
  3126. }
  3127. }
  3128. bcc_ptr += length + 1;
  3129. bytes_left -= (length + 1);
  3130. strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
  3131. /* mostly informational -- no need to fail on error here */
  3132. kfree(tcon->nativeFileSystem);
  3133. tcon->nativeFileSystem = cifs_strndup_from_ucs(bcc_ptr,
  3134. bytes_left, is_unicode,
  3135. nls_codepage);
  3136. cFYI(1, "nativeFileSystem=%s", tcon->nativeFileSystem);
  3137. if ((smb_buffer_response->WordCount == 3) ||
  3138. (smb_buffer_response->WordCount == 7))
  3139. /* field is in same location */
  3140. tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
  3141. else
  3142. tcon->Flags = 0;
  3143. cFYI(1, "Tcon flags: 0x%x ", tcon->Flags);
  3144. } else if ((rc == 0) && tcon == NULL) {
  3145. /* all we need to save for IPC$ connection */
  3146. ses->ipc_tid = smb_buffer_response->Tid;
  3147. }
  3148. cifs_buf_release(smb_buffer);
  3149. return rc;
  3150. }
  3151. void
  3152. cifs_umount(struct cifs_sb_info *cifs_sb)
  3153. {
  3154. struct rb_root *root = &cifs_sb->tlink_tree;
  3155. struct rb_node *node;
  3156. struct tcon_link *tlink;
  3157. cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
  3158. spin_lock(&cifs_sb->tlink_tree_lock);
  3159. while ((node = rb_first(root))) {
  3160. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  3161. cifs_get_tlink(tlink);
  3162. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3163. rb_erase(node, root);
  3164. spin_unlock(&cifs_sb->tlink_tree_lock);
  3165. cifs_put_tlink(tlink);
  3166. spin_lock(&cifs_sb->tlink_tree_lock);
  3167. }
  3168. spin_unlock(&cifs_sb->tlink_tree_lock);
  3169. bdi_destroy(&cifs_sb->bdi);
  3170. kfree(cifs_sb->mountdata);
  3171. unload_nls(cifs_sb->local_nls);
  3172. kfree(cifs_sb);
  3173. }
  3174. int cifs_negotiate_protocol(unsigned int xid, struct cifs_ses *ses)
  3175. {
  3176. int rc = 0;
  3177. struct TCP_Server_Info *server = ses->server;
  3178. /* only send once per connect */
  3179. if (server->maxBuf != 0)
  3180. return 0;
  3181. rc = CIFSSMBNegotiate(xid, ses);
  3182. if (rc == -EAGAIN) {
  3183. /* retry only once on 1st time connection */
  3184. rc = CIFSSMBNegotiate(xid, ses);
  3185. if (rc == -EAGAIN)
  3186. rc = -EHOSTDOWN;
  3187. }
  3188. if (rc == 0) {
  3189. spin_lock(&GlobalMid_Lock);
  3190. if (server->tcpStatus == CifsNeedNegotiate)
  3191. server->tcpStatus = CifsGood;
  3192. else
  3193. rc = -EHOSTDOWN;
  3194. spin_unlock(&GlobalMid_Lock);
  3195. }
  3196. return rc;
  3197. }
  3198. int cifs_setup_session(unsigned int xid, struct cifs_ses *ses,
  3199. struct nls_table *nls_info)
  3200. {
  3201. int rc = 0;
  3202. struct TCP_Server_Info *server = ses->server;
  3203. ses->flags = 0;
  3204. ses->capabilities = server->capabilities;
  3205. if (linuxExtEnabled == 0)
  3206. ses->capabilities &= (~CAP_UNIX);
  3207. cFYI(1, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
  3208. server->sec_mode, server->capabilities, server->timeAdj);
  3209. rc = CIFS_SessSetup(xid, ses, nls_info);
  3210. if (rc) {
  3211. cERROR(1, "Send error in SessSetup = %d", rc);
  3212. } else {
  3213. mutex_lock(&ses->server->srv_mutex);
  3214. if (!server->session_estab) {
  3215. server->session_key.response = ses->auth_key.response;
  3216. server->session_key.len = ses->auth_key.len;
  3217. server->sequence_number = 0x2;
  3218. server->session_estab = true;
  3219. ses->auth_key.response = NULL;
  3220. }
  3221. mutex_unlock(&server->srv_mutex);
  3222. cFYI(1, "CIFS Session Established successfully");
  3223. spin_lock(&GlobalMid_Lock);
  3224. ses->status = CifsGood;
  3225. ses->need_reconnect = false;
  3226. spin_unlock(&GlobalMid_Lock);
  3227. }
  3228. kfree(ses->auth_key.response);
  3229. ses->auth_key.response = NULL;
  3230. ses->auth_key.len = 0;
  3231. kfree(ses->ntlmssp);
  3232. ses->ntlmssp = NULL;
  3233. return rc;
  3234. }
  3235. static struct cifs_tcon *
  3236. cifs_construct_tcon(struct cifs_sb_info *cifs_sb, uid_t fsuid)
  3237. {
  3238. struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
  3239. struct cifs_ses *ses;
  3240. struct cifs_tcon *tcon = NULL;
  3241. struct smb_vol *vol_info;
  3242. char username[28]; /* big enough for "krb50x" + hex of ULONG_MAX 6+16 */
  3243. /* We used to have this as MAX_USERNAME which is */
  3244. /* way too big now (256 instead of 32) */
  3245. vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
  3246. if (vol_info == NULL) {
  3247. tcon = ERR_PTR(-ENOMEM);
  3248. goto out;
  3249. }
  3250. snprintf(username, sizeof(username), "krb50x%x", fsuid);
  3251. vol_info->username = username;
  3252. vol_info->local_nls = cifs_sb->local_nls;
  3253. vol_info->linux_uid = fsuid;
  3254. vol_info->cred_uid = fsuid;
  3255. vol_info->UNC = master_tcon->treeName;
  3256. vol_info->retry = master_tcon->retry;
  3257. vol_info->nocase = master_tcon->nocase;
  3258. vol_info->local_lease = master_tcon->local_lease;
  3259. vol_info->no_linux_ext = !master_tcon->unix_ext;
  3260. /* FIXME: allow for other secFlg settings */
  3261. vol_info->secFlg = CIFSSEC_MUST_KRB5;
  3262. /* get a reference for the same TCP session */
  3263. spin_lock(&cifs_tcp_ses_lock);
  3264. ++master_tcon->ses->server->srv_count;
  3265. spin_unlock(&cifs_tcp_ses_lock);
  3266. ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
  3267. if (IS_ERR(ses)) {
  3268. tcon = (struct cifs_tcon *)ses;
  3269. cifs_put_tcp_session(master_tcon->ses->server);
  3270. goto out;
  3271. }
  3272. tcon = cifs_get_tcon(ses, vol_info);
  3273. if (IS_ERR(tcon)) {
  3274. cifs_put_smb_ses(ses);
  3275. goto out;
  3276. }
  3277. if (ses->capabilities & CAP_UNIX)
  3278. reset_cifs_unix_caps(0, tcon, NULL, vol_info);
  3279. out:
  3280. kfree(vol_info);
  3281. return tcon;
  3282. }
  3283. struct cifs_tcon *
  3284. cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
  3285. {
  3286. return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
  3287. }
  3288. static int
  3289. cifs_sb_tcon_pending_wait(void *unused)
  3290. {
  3291. schedule();
  3292. return signal_pending(current) ? -ERESTARTSYS : 0;
  3293. }
  3294. /* find and return a tlink with given uid */
  3295. static struct tcon_link *
  3296. tlink_rb_search(struct rb_root *root, uid_t uid)
  3297. {
  3298. struct rb_node *node = root->rb_node;
  3299. struct tcon_link *tlink;
  3300. while (node) {
  3301. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  3302. if (tlink->tl_uid > uid)
  3303. node = node->rb_left;
  3304. else if (tlink->tl_uid < uid)
  3305. node = node->rb_right;
  3306. else
  3307. return tlink;
  3308. }
  3309. return NULL;
  3310. }
  3311. /* insert a tcon_link into the tree */
  3312. static void
  3313. tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
  3314. {
  3315. struct rb_node **new = &(root->rb_node), *parent = NULL;
  3316. struct tcon_link *tlink;
  3317. while (*new) {
  3318. tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
  3319. parent = *new;
  3320. if (tlink->tl_uid > new_tlink->tl_uid)
  3321. new = &((*new)->rb_left);
  3322. else
  3323. new = &((*new)->rb_right);
  3324. }
  3325. rb_link_node(&new_tlink->tl_rbnode, parent, new);
  3326. rb_insert_color(&new_tlink->tl_rbnode, root);
  3327. }
  3328. /*
  3329. * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
  3330. * current task.
  3331. *
  3332. * If the superblock doesn't refer to a multiuser mount, then just return
  3333. * the master tcon for the mount.
  3334. *
  3335. * First, search the rbtree for an existing tcon for this fsuid. If one
  3336. * exists, then check to see if it's pending construction. If it is then wait
  3337. * for construction to complete. Once it's no longer pending, check to see if
  3338. * it failed and either return an error or retry construction, depending on
  3339. * the timeout.
  3340. *
  3341. * If one doesn't exist then insert a new tcon_link struct into the tree and
  3342. * try to construct a new one.
  3343. */
  3344. struct tcon_link *
  3345. cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
  3346. {
  3347. int ret;
  3348. uid_t fsuid = current_fsuid();
  3349. struct tcon_link *tlink, *newtlink;
  3350. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  3351. return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  3352. spin_lock(&cifs_sb->tlink_tree_lock);
  3353. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  3354. if (tlink)
  3355. cifs_get_tlink(tlink);
  3356. spin_unlock(&cifs_sb->tlink_tree_lock);
  3357. if (tlink == NULL) {
  3358. newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
  3359. if (newtlink == NULL)
  3360. return ERR_PTR(-ENOMEM);
  3361. newtlink->tl_uid = fsuid;
  3362. newtlink->tl_tcon = ERR_PTR(-EACCES);
  3363. set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
  3364. set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
  3365. cifs_get_tlink(newtlink);
  3366. spin_lock(&cifs_sb->tlink_tree_lock);
  3367. /* was one inserted after previous search? */
  3368. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  3369. if (tlink) {
  3370. cifs_get_tlink(tlink);
  3371. spin_unlock(&cifs_sb->tlink_tree_lock);
  3372. kfree(newtlink);
  3373. goto wait_for_construction;
  3374. }
  3375. tlink = newtlink;
  3376. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  3377. spin_unlock(&cifs_sb->tlink_tree_lock);
  3378. } else {
  3379. wait_for_construction:
  3380. ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
  3381. cifs_sb_tcon_pending_wait,
  3382. TASK_INTERRUPTIBLE);
  3383. if (ret) {
  3384. cifs_put_tlink(tlink);
  3385. return ERR_PTR(ret);
  3386. }
  3387. /* if it's good, return it */
  3388. if (!IS_ERR(tlink->tl_tcon))
  3389. return tlink;
  3390. /* return error if we tried this already recently */
  3391. if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
  3392. cifs_put_tlink(tlink);
  3393. return ERR_PTR(-EACCES);
  3394. }
  3395. if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
  3396. goto wait_for_construction;
  3397. }
  3398. tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
  3399. clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
  3400. wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
  3401. if (IS_ERR(tlink->tl_tcon)) {
  3402. cifs_put_tlink(tlink);
  3403. return ERR_PTR(-EACCES);
  3404. }
  3405. return tlink;
  3406. }
  3407. /*
  3408. * periodic workqueue job that scans tcon_tree for a superblock and closes
  3409. * out tcons.
  3410. */
  3411. static void
  3412. cifs_prune_tlinks(struct work_struct *work)
  3413. {
  3414. struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
  3415. prune_tlinks.work);
  3416. struct rb_root *root = &cifs_sb->tlink_tree;
  3417. struct rb_node *node = rb_first(root);
  3418. struct rb_node *tmp;
  3419. struct tcon_link *tlink;
  3420. /*
  3421. * Because we drop the spinlock in the loop in order to put the tlink
  3422. * it's not guarded against removal of links from the tree. The only
  3423. * places that remove entries from the tree are this function and
  3424. * umounts. Because this function is non-reentrant and is canceled
  3425. * before umount can proceed, this is safe.
  3426. */
  3427. spin_lock(&cifs_sb->tlink_tree_lock);
  3428. node = rb_first(root);
  3429. while (node != NULL) {
  3430. tmp = node;
  3431. node = rb_next(tmp);
  3432. tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
  3433. if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
  3434. atomic_read(&tlink->tl_count) != 0 ||
  3435. time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
  3436. continue;
  3437. cifs_get_tlink(tlink);
  3438. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3439. rb_erase(tmp, root);
  3440. spin_unlock(&cifs_sb->tlink_tree_lock);
  3441. cifs_put_tlink(tlink);
  3442. spin_lock(&cifs_sb->tlink_tree_lock);
  3443. }
  3444. spin_unlock(&cifs_sb->tlink_tree_lock);
  3445. queue_delayed_work(system_nrt_wq, &cifs_sb->prune_tlinks,
  3446. TLINK_IDLE_EXPIRE);
  3447. }