nfs4state.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065
  1. /*
  2. * Copyright (c) 2001 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Kendrick Smith <kmsmith@umich.edu>
  6. * Andy Adamson <kandros@umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/file.h>
  35. #include <linux/fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/namei.h>
  38. #include <linux/swap.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/ratelimit.h>
  41. #include <linux/sunrpc/svcauth_gss.h>
  42. #include <linux/sunrpc/addr.h>
  43. #include "xdr4.h"
  44. #include "vfs.h"
  45. #include "current_stateid.h"
  46. #include "netns.h"
  47. #define NFSDDBG_FACILITY NFSDDBG_PROC
  48. #define all_ones {{~0,~0},~0}
  49. static const stateid_t one_stateid = {
  50. .si_generation = ~0,
  51. .si_opaque = all_ones,
  52. };
  53. static const stateid_t zero_stateid = {
  54. /* all fields zero */
  55. };
  56. static const stateid_t currentstateid = {
  57. .si_generation = 1,
  58. };
  59. static u64 current_sessionid = 1;
  60. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  61. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  62. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  63. /* forward declarations */
  64. static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
  65. /* Locking: */
  66. /* Currently used for almost all code touching nfsv4 state: */
  67. static DEFINE_MUTEX(client_mutex);
  68. /*
  69. * Currently used for the del_recall_lru and file hash table. In an
  70. * effort to decrease the scope of the client_mutex, this spinlock may
  71. * eventually cover more:
  72. */
  73. static DEFINE_SPINLOCK(recall_lock);
  74. static struct kmem_cache *openowner_slab = NULL;
  75. static struct kmem_cache *lockowner_slab = NULL;
  76. static struct kmem_cache *file_slab = NULL;
  77. static struct kmem_cache *stateid_slab = NULL;
  78. static struct kmem_cache *deleg_slab = NULL;
  79. void
  80. nfs4_lock_state(void)
  81. {
  82. mutex_lock(&client_mutex);
  83. }
  84. static void free_session(struct kref *);
  85. /* Must be called under the client_lock */
  86. static void nfsd4_put_session_locked(struct nfsd4_session *ses)
  87. {
  88. kref_put(&ses->se_ref, free_session);
  89. }
  90. static void nfsd4_get_session(struct nfsd4_session *ses)
  91. {
  92. kref_get(&ses->se_ref);
  93. }
  94. void
  95. nfs4_unlock_state(void)
  96. {
  97. mutex_unlock(&client_mutex);
  98. }
  99. static inline u32
  100. opaque_hashval(const void *ptr, int nbytes)
  101. {
  102. unsigned char *cptr = (unsigned char *) ptr;
  103. u32 x = 0;
  104. while (nbytes--) {
  105. x *= 37;
  106. x += *cptr++;
  107. }
  108. return x;
  109. }
  110. static struct list_head del_recall_lru;
  111. static void nfsd4_free_file(struct nfs4_file *f)
  112. {
  113. kmem_cache_free(file_slab, f);
  114. }
  115. static inline void
  116. put_nfs4_file(struct nfs4_file *fi)
  117. {
  118. if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
  119. list_del(&fi->fi_hash);
  120. spin_unlock(&recall_lock);
  121. iput(fi->fi_inode);
  122. nfsd4_free_file(fi);
  123. }
  124. }
  125. static inline void
  126. get_nfs4_file(struct nfs4_file *fi)
  127. {
  128. atomic_inc(&fi->fi_ref);
  129. }
  130. static int num_delegations;
  131. unsigned long max_delegations;
  132. /*
  133. * Open owner state (share locks)
  134. */
  135. /* hash tables for lock and open owners */
  136. #define OWNER_HASH_BITS 8
  137. #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
  138. #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
  139. static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
  140. {
  141. unsigned int ret;
  142. ret = opaque_hashval(ownername->data, ownername->len);
  143. ret += clientid;
  144. return ret & OWNER_HASH_MASK;
  145. }
  146. /* hash table for nfs4_file */
  147. #define FILE_HASH_BITS 8
  148. #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
  149. static unsigned int file_hashval(struct inode *ino)
  150. {
  151. /* XXX: why are we hashing on inode pointer, anyway? */
  152. return hash_ptr(ino, FILE_HASH_BITS);
  153. }
  154. static struct list_head file_hashtbl[FILE_HASH_SIZE];
  155. static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  156. {
  157. WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
  158. atomic_inc(&fp->fi_access[oflag]);
  159. }
  160. static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  161. {
  162. if (oflag == O_RDWR) {
  163. __nfs4_file_get_access(fp, O_RDONLY);
  164. __nfs4_file_get_access(fp, O_WRONLY);
  165. } else
  166. __nfs4_file_get_access(fp, oflag);
  167. }
  168. static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
  169. {
  170. if (fp->fi_fds[oflag]) {
  171. fput(fp->fi_fds[oflag]);
  172. fp->fi_fds[oflag] = NULL;
  173. }
  174. }
  175. static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  176. {
  177. if (atomic_dec_and_test(&fp->fi_access[oflag])) {
  178. nfs4_file_put_fd(fp, oflag);
  179. /*
  180. * It's also safe to get rid of the RDWR open *if*
  181. * we no longer have need of the other kind of access
  182. * or if we already have the other kind of open:
  183. */
  184. if (fp->fi_fds[1-oflag]
  185. || atomic_read(&fp->fi_access[1 - oflag]) == 0)
  186. nfs4_file_put_fd(fp, O_RDWR);
  187. }
  188. }
  189. static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  190. {
  191. if (oflag == O_RDWR) {
  192. __nfs4_file_put_access(fp, O_RDONLY);
  193. __nfs4_file_put_access(fp, O_WRONLY);
  194. } else
  195. __nfs4_file_put_access(fp, oflag);
  196. }
  197. static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
  198. kmem_cache *slab)
  199. {
  200. struct idr *stateids = &cl->cl_stateids;
  201. static int min_stateid = 0;
  202. struct nfs4_stid *stid;
  203. int new_id;
  204. stid = kmem_cache_alloc(slab, GFP_KERNEL);
  205. if (!stid)
  206. return NULL;
  207. new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
  208. if (new_id < 0)
  209. goto out_free;
  210. stid->sc_client = cl;
  211. stid->sc_type = 0;
  212. stid->sc_stateid.si_opaque.so_id = new_id;
  213. stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
  214. /* Will be incremented before return to client: */
  215. stid->sc_stateid.si_generation = 0;
  216. /*
  217. * It shouldn't be a problem to reuse an opaque stateid value.
  218. * I don't think it is for 4.1. But with 4.0 I worry that, for
  219. * example, a stray write retransmission could be accepted by
  220. * the server when it should have been rejected. Therefore,
  221. * adopt a trick from the sctp code to attempt to maximize the
  222. * amount of time until an id is reused, by ensuring they always
  223. * "increase" (mod INT_MAX):
  224. */
  225. min_stateid = new_id+1;
  226. if (min_stateid == INT_MAX)
  227. min_stateid = 0;
  228. return stid;
  229. out_free:
  230. kfree(stid);
  231. return NULL;
  232. }
  233. static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
  234. {
  235. return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
  236. }
  237. static struct nfs4_delegation *
  238. alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
  239. {
  240. struct nfs4_delegation *dp;
  241. struct nfs4_file *fp = stp->st_file;
  242. dprintk("NFSD alloc_init_deleg\n");
  243. /*
  244. * Major work on the lease subsystem (for example, to support
  245. * calbacks on stat) will be required before we can support
  246. * write delegations properly.
  247. */
  248. if (type != NFS4_OPEN_DELEGATE_READ)
  249. return NULL;
  250. if (fp->fi_had_conflict)
  251. return NULL;
  252. if (num_delegations > max_delegations)
  253. return NULL;
  254. dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
  255. if (dp == NULL)
  256. return dp;
  257. dp->dl_stid.sc_type = NFS4_DELEG_STID;
  258. /*
  259. * delegation seqid's are never incremented. The 4.1 special
  260. * meaning of seqid 0 isn't meaningful, really, but let's avoid
  261. * 0 anyway just for consistency and use 1:
  262. */
  263. dp->dl_stid.sc_stateid.si_generation = 1;
  264. num_delegations++;
  265. INIT_LIST_HEAD(&dp->dl_perfile);
  266. INIT_LIST_HEAD(&dp->dl_perclnt);
  267. INIT_LIST_HEAD(&dp->dl_recall_lru);
  268. get_nfs4_file(fp);
  269. dp->dl_file = fp;
  270. dp->dl_type = type;
  271. fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
  272. dp->dl_time = 0;
  273. atomic_set(&dp->dl_count, 1);
  274. nfsd4_init_callback(&dp->dl_recall);
  275. return dp;
  276. }
  277. static void free_stid(struct nfs4_stid *s, struct kmem_cache *slab)
  278. {
  279. struct idr *stateids = &s->sc_client->cl_stateids;
  280. idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
  281. kmem_cache_free(slab, s);
  282. }
  283. void
  284. nfs4_put_delegation(struct nfs4_delegation *dp)
  285. {
  286. if (atomic_dec_and_test(&dp->dl_count)) {
  287. dprintk("NFSD: freeing dp %p\n",dp);
  288. put_nfs4_file(dp->dl_file);
  289. free_stid(&dp->dl_stid, deleg_slab);
  290. num_delegations--;
  291. }
  292. }
  293. static void nfs4_put_deleg_lease(struct nfs4_file *fp)
  294. {
  295. if (atomic_dec_and_test(&fp->fi_delegees)) {
  296. vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
  297. fp->fi_lease = NULL;
  298. fput(fp->fi_deleg_file);
  299. fp->fi_deleg_file = NULL;
  300. }
  301. }
  302. static void unhash_stid(struct nfs4_stid *s)
  303. {
  304. s->sc_type = 0;
  305. }
  306. /* Called under the state lock. */
  307. static void
  308. unhash_delegation(struct nfs4_delegation *dp)
  309. {
  310. unhash_stid(&dp->dl_stid);
  311. list_del_init(&dp->dl_perclnt);
  312. spin_lock(&recall_lock);
  313. list_del_init(&dp->dl_perfile);
  314. list_del_init(&dp->dl_recall_lru);
  315. spin_unlock(&recall_lock);
  316. nfs4_put_deleg_lease(dp->dl_file);
  317. nfs4_put_delegation(dp);
  318. }
  319. /*
  320. * SETCLIENTID state
  321. */
  322. static unsigned int clientid_hashval(u32 id)
  323. {
  324. return id & CLIENT_HASH_MASK;
  325. }
  326. static unsigned int clientstr_hashval(const char *name)
  327. {
  328. return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
  329. }
  330. /*
  331. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  332. * st_{access,deny}_bmap field of the stateid, in order to track not
  333. * only what share bits are currently in force, but also what
  334. * combinations of share bits previous opens have used. This allows us
  335. * to enforce the recommendation of rfc 3530 14.2.19 that the server
  336. * return an error if the client attempt to downgrade to a combination
  337. * of share bits not explicable by closing some of its previous opens.
  338. *
  339. * XXX: This enforcement is actually incomplete, since we don't keep
  340. * track of access/deny bit combinations; so, e.g., we allow:
  341. *
  342. * OPEN allow read, deny write
  343. * OPEN allow both, deny none
  344. * DOWNGRADE allow read, deny none
  345. *
  346. * which we should reject.
  347. */
  348. static unsigned int
  349. bmap_to_share_mode(unsigned long bmap) {
  350. int i;
  351. unsigned int access = 0;
  352. for (i = 1; i < 4; i++) {
  353. if (test_bit(i, &bmap))
  354. access |= i;
  355. }
  356. return access;
  357. }
  358. static bool
  359. test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
  360. unsigned int access, deny;
  361. access = bmap_to_share_mode(stp->st_access_bmap);
  362. deny = bmap_to_share_mode(stp->st_deny_bmap);
  363. if ((access & open->op_share_deny) || (deny & open->op_share_access))
  364. return false;
  365. return true;
  366. }
  367. /* set share access for a given stateid */
  368. static inline void
  369. set_access(u32 access, struct nfs4_ol_stateid *stp)
  370. {
  371. __set_bit(access, &stp->st_access_bmap);
  372. }
  373. /* clear share access for a given stateid */
  374. static inline void
  375. clear_access(u32 access, struct nfs4_ol_stateid *stp)
  376. {
  377. __clear_bit(access, &stp->st_access_bmap);
  378. }
  379. /* test whether a given stateid has access */
  380. static inline bool
  381. test_access(u32 access, struct nfs4_ol_stateid *stp)
  382. {
  383. return test_bit(access, &stp->st_access_bmap);
  384. }
  385. /* set share deny for a given stateid */
  386. static inline void
  387. set_deny(u32 access, struct nfs4_ol_stateid *stp)
  388. {
  389. __set_bit(access, &stp->st_deny_bmap);
  390. }
  391. /* clear share deny for a given stateid */
  392. static inline void
  393. clear_deny(u32 access, struct nfs4_ol_stateid *stp)
  394. {
  395. __clear_bit(access, &stp->st_deny_bmap);
  396. }
  397. /* test whether a given stateid is denying specific access */
  398. static inline bool
  399. test_deny(u32 access, struct nfs4_ol_stateid *stp)
  400. {
  401. return test_bit(access, &stp->st_deny_bmap);
  402. }
  403. static int nfs4_access_to_omode(u32 access)
  404. {
  405. switch (access & NFS4_SHARE_ACCESS_BOTH) {
  406. case NFS4_SHARE_ACCESS_READ:
  407. return O_RDONLY;
  408. case NFS4_SHARE_ACCESS_WRITE:
  409. return O_WRONLY;
  410. case NFS4_SHARE_ACCESS_BOTH:
  411. return O_RDWR;
  412. }
  413. WARN_ON_ONCE(1);
  414. return O_RDONLY;
  415. }
  416. /* release all access and file references for a given stateid */
  417. static void
  418. release_all_access(struct nfs4_ol_stateid *stp)
  419. {
  420. int i;
  421. for (i = 1; i < 4; i++) {
  422. if (test_access(i, stp))
  423. nfs4_file_put_access(stp->st_file,
  424. nfs4_access_to_omode(i));
  425. clear_access(i, stp);
  426. }
  427. }
  428. static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
  429. {
  430. list_del(&stp->st_perfile);
  431. list_del(&stp->st_perstateowner);
  432. }
  433. static void close_generic_stateid(struct nfs4_ol_stateid *stp)
  434. {
  435. release_all_access(stp);
  436. put_nfs4_file(stp->st_file);
  437. stp->st_file = NULL;
  438. }
  439. static void free_generic_stateid(struct nfs4_ol_stateid *stp)
  440. {
  441. free_stid(&stp->st_stid, stateid_slab);
  442. }
  443. static void release_lock_stateid(struct nfs4_ol_stateid *stp)
  444. {
  445. struct file *file;
  446. unhash_generic_stateid(stp);
  447. unhash_stid(&stp->st_stid);
  448. file = find_any_file(stp->st_file);
  449. if (file)
  450. locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
  451. close_generic_stateid(stp);
  452. free_generic_stateid(stp);
  453. }
  454. static void unhash_lockowner(struct nfs4_lockowner *lo)
  455. {
  456. struct nfs4_ol_stateid *stp;
  457. list_del(&lo->lo_owner.so_strhash);
  458. list_del(&lo->lo_perstateid);
  459. list_del(&lo->lo_owner_ino_hash);
  460. while (!list_empty(&lo->lo_owner.so_stateids)) {
  461. stp = list_first_entry(&lo->lo_owner.so_stateids,
  462. struct nfs4_ol_stateid, st_perstateowner);
  463. release_lock_stateid(stp);
  464. }
  465. }
  466. static void release_lockowner(struct nfs4_lockowner *lo)
  467. {
  468. unhash_lockowner(lo);
  469. nfs4_free_lockowner(lo);
  470. }
  471. static void
  472. release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
  473. {
  474. struct nfs4_lockowner *lo;
  475. while (!list_empty(&open_stp->st_lockowners)) {
  476. lo = list_entry(open_stp->st_lockowners.next,
  477. struct nfs4_lockowner, lo_perstateid);
  478. release_lockowner(lo);
  479. }
  480. }
  481. static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
  482. {
  483. unhash_generic_stateid(stp);
  484. release_stateid_lockowners(stp);
  485. close_generic_stateid(stp);
  486. }
  487. static void release_open_stateid(struct nfs4_ol_stateid *stp)
  488. {
  489. unhash_open_stateid(stp);
  490. unhash_stid(&stp->st_stid);
  491. free_generic_stateid(stp);
  492. }
  493. static void unhash_openowner(struct nfs4_openowner *oo)
  494. {
  495. struct nfs4_ol_stateid *stp;
  496. list_del(&oo->oo_owner.so_strhash);
  497. list_del(&oo->oo_perclient);
  498. while (!list_empty(&oo->oo_owner.so_stateids)) {
  499. stp = list_first_entry(&oo->oo_owner.so_stateids,
  500. struct nfs4_ol_stateid, st_perstateowner);
  501. release_open_stateid(stp);
  502. }
  503. }
  504. static void release_last_closed_stateid(struct nfs4_openowner *oo)
  505. {
  506. struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
  507. if (s) {
  508. unhash_stid(&s->st_stid);
  509. free_generic_stateid(s);
  510. oo->oo_last_closed_stid = NULL;
  511. }
  512. }
  513. static void release_openowner(struct nfs4_openowner *oo)
  514. {
  515. unhash_openowner(oo);
  516. list_del(&oo->oo_close_lru);
  517. release_last_closed_stateid(oo);
  518. nfs4_free_openowner(oo);
  519. }
  520. static inline int
  521. hash_sessionid(struct nfs4_sessionid *sessionid)
  522. {
  523. struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
  524. return sid->sequence % SESSION_HASH_SIZE;
  525. }
  526. #ifdef NFSD_DEBUG
  527. static inline void
  528. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  529. {
  530. u32 *ptr = (u32 *)(&sessionid->data[0]);
  531. dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
  532. }
  533. #else
  534. static inline void
  535. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  536. {
  537. }
  538. #endif
  539. static void
  540. gen_sessionid(struct nfsd4_session *ses)
  541. {
  542. struct nfs4_client *clp = ses->se_client;
  543. struct nfsd4_sessionid *sid;
  544. sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
  545. sid->clientid = clp->cl_clientid;
  546. sid->sequence = current_sessionid++;
  547. sid->reserved = 0;
  548. }
  549. /*
  550. * The protocol defines ca_maxresponssize_cached to include the size of
  551. * the rpc header, but all we need to cache is the data starting after
  552. * the end of the initial SEQUENCE operation--the rest we regenerate
  553. * each time. Therefore we can advertise a ca_maxresponssize_cached
  554. * value that is the number of bytes in our cache plus a few additional
  555. * bytes. In order to stay on the safe side, and not promise more than
  556. * we can cache, those additional bytes must be the minimum possible: 24
  557. * bytes of rpc header (xid through accept state, with AUTH_NULL
  558. * verifier), 12 for the compound header (with zero-length tag), and 44
  559. * for the SEQUENCE op response:
  560. */
  561. #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
  562. static void
  563. free_session_slots(struct nfsd4_session *ses)
  564. {
  565. int i;
  566. for (i = 0; i < ses->se_fchannel.maxreqs; i++)
  567. kfree(ses->se_slots[i]);
  568. }
  569. /*
  570. * We don't actually need to cache the rpc and session headers, so we
  571. * can allocate a little less for each slot:
  572. */
  573. static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
  574. {
  575. return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
  576. }
  577. static int nfsd4_sanitize_slot_size(u32 size)
  578. {
  579. size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
  580. size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
  581. return size;
  582. }
  583. /*
  584. * XXX: If we run out of reserved DRC memory we could (up to a point)
  585. * re-negotiate active sessions and reduce their slot usage to make
  586. * room for new connections. For now we just fail the create session.
  587. */
  588. static int nfsd4_get_drc_mem(int slotsize, u32 num)
  589. {
  590. int avail;
  591. num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
  592. spin_lock(&nfsd_drc_lock);
  593. avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
  594. nfsd_drc_max_mem - nfsd_drc_mem_used);
  595. num = min_t(int, num, avail / slotsize);
  596. nfsd_drc_mem_used += num * slotsize;
  597. spin_unlock(&nfsd_drc_lock);
  598. return num;
  599. }
  600. static void nfsd4_put_drc_mem(int slotsize, int num)
  601. {
  602. spin_lock(&nfsd_drc_lock);
  603. nfsd_drc_mem_used -= slotsize * num;
  604. spin_unlock(&nfsd_drc_lock);
  605. }
  606. static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
  607. {
  608. struct nfsd4_session *new;
  609. int mem, i;
  610. BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
  611. + sizeof(struct nfsd4_session) > PAGE_SIZE);
  612. mem = numslots * sizeof(struct nfsd4_slot *);
  613. new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
  614. if (!new)
  615. return NULL;
  616. /* allocate each struct nfsd4_slot and data cache in one piece */
  617. for (i = 0; i < numslots; i++) {
  618. mem = sizeof(struct nfsd4_slot) + slotsize;
  619. new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
  620. if (!new->se_slots[i])
  621. goto out_free;
  622. }
  623. return new;
  624. out_free:
  625. while (i--)
  626. kfree(new->se_slots[i]);
  627. kfree(new);
  628. return NULL;
  629. }
  630. static void init_forechannel_attrs(struct nfsd4_channel_attrs *new,
  631. struct nfsd4_channel_attrs *req,
  632. int numslots, int slotsize,
  633. struct nfsd_net *nn)
  634. {
  635. u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
  636. new->maxreqs = numslots;
  637. new->maxresp_cached = min_t(u32, req->maxresp_cached,
  638. slotsize + NFSD_MIN_HDR_SEQ_SZ);
  639. new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
  640. new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
  641. new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  642. }
  643. static void free_conn(struct nfsd4_conn *c)
  644. {
  645. svc_xprt_put(c->cn_xprt);
  646. kfree(c);
  647. }
  648. static void nfsd4_conn_lost(struct svc_xpt_user *u)
  649. {
  650. struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
  651. struct nfs4_client *clp = c->cn_session->se_client;
  652. spin_lock(&clp->cl_lock);
  653. if (!list_empty(&c->cn_persession)) {
  654. list_del(&c->cn_persession);
  655. free_conn(c);
  656. }
  657. nfsd4_probe_callback(clp);
  658. spin_unlock(&clp->cl_lock);
  659. }
  660. static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
  661. {
  662. struct nfsd4_conn *conn;
  663. conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
  664. if (!conn)
  665. return NULL;
  666. svc_xprt_get(rqstp->rq_xprt);
  667. conn->cn_xprt = rqstp->rq_xprt;
  668. conn->cn_flags = flags;
  669. INIT_LIST_HEAD(&conn->cn_xpt_user.list);
  670. return conn;
  671. }
  672. static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  673. {
  674. conn->cn_session = ses;
  675. list_add(&conn->cn_persession, &ses->se_conns);
  676. }
  677. static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  678. {
  679. struct nfs4_client *clp = ses->se_client;
  680. spin_lock(&clp->cl_lock);
  681. __nfsd4_hash_conn(conn, ses);
  682. spin_unlock(&clp->cl_lock);
  683. }
  684. static int nfsd4_register_conn(struct nfsd4_conn *conn)
  685. {
  686. conn->cn_xpt_user.callback = nfsd4_conn_lost;
  687. return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
  688. }
  689. static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
  690. {
  691. int ret;
  692. nfsd4_hash_conn(conn, ses);
  693. ret = nfsd4_register_conn(conn);
  694. if (ret)
  695. /* oops; xprt is already down: */
  696. nfsd4_conn_lost(&conn->cn_xpt_user);
  697. if (conn->cn_flags & NFS4_CDFC4_BACK) {
  698. /* callback channel may be back up */
  699. nfsd4_probe_callback(ses->se_client);
  700. }
  701. }
  702. static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
  703. {
  704. u32 dir = NFS4_CDFC4_FORE;
  705. if (cses->flags & SESSION4_BACK_CHAN)
  706. dir |= NFS4_CDFC4_BACK;
  707. return alloc_conn(rqstp, dir);
  708. }
  709. /* must be called under client_lock */
  710. static void nfsd4_del_conns(struct nfsd4_session *s)
  711. {
  712. struct nfs4_client *clp = s->se_client;
  713. struct nfsd4_conn *c;
  714. spin_lock(&clp->cl_lock);
  715. while (!list_empty(&s->se_conns)) {
  716. c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
  717. list_del_init(&c->cn_persession);
  718. spin_unlock(&clp->cl_lock);
  719. unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
  720. free_conn(c);
  721. spin_lock(&clp->cl_lock);
  722. }
  723. spin_unlock(&clp->cl_lock);
  724. }
  725. static void __free_session(struct nfsd4_session *ses)
  726. {
  727. nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
  728. free_session_slots(ses);
  729. kfree(ses);
  730. }
  731. static void free_session(struct kref *kref)
  732. {
  733. struct nfsd4_session *ses;
  734. struct nfsd_net *nn;
  735. ses = container_of(kref, struct nfsd4_session, se_ref);
  736. nn = net_generic(ses->se_client->net, nfsd_net_id);
  737. lockdep_assert_held(&nn->client_lock);
  738. nfsd4_del_conns(ses);
  739. __free_session(ses);
  740. }
  741. static void nfsd4_put_session(struct nfsd4_session *ses)
  742. {
  743. struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
  744. spin_lock(&nn->client_lock);
  745. nfsd4_put_session_locked(ses);
  746. spin_unlock(&nn->client_lock);
  747. }
  748. static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan,
  749. struct nfsd_net *nn)
  750. {
  751. struct nfsd4_session *new;
  752. int numslots, slotsize;
  753. /*
  754. * Note decreasing slot size below client's request may
  755. * make it difficult for client to function correctly, whereas
  756. * decreasing the number of slots will (just?) affect
  757. * performance. When short on memory we therefore prefer to
  758. * decrease number of slots instead of their size.
  759. */
  760. slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
  761. numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
  762. if (numslots < 1)
  763. return NULL;
  764. new = __alloc_session(slotsize, numslots);
  765. if (!new) {
  766. nfsd4_put_drc_mem(slotsize, numslots);
  767. return NULL;
  768. }
  769. init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize, nn);
  770. return new;
  771. }
  772. static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
  773. {
  774. int idx;
  775. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  776. new->se_client = clp;
  777. gen_sessionid(new);
  778. INIT_LIST_HEAD(&new->se_conns);
  779. new->se_cb_seq_nr = 1;
  780. new->se_flags = cses->flags;
  781. new->se_cb_prog = cses->callback_prog;
  782. new->se_cb_sec = cses->cb_sec;
  783. kref_init(&new->se_ref);
  784. idx = hash_sessionid(&new->se_sessionid);
  785. spin_lock(&nn->client_lock);
  786. list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
  787. spin_lock(&clp->cl_lock);
  788. list_add(&new->se_perclnt, &clp->cl_sessions);
  789. spin_unlock(&clp->cl_lock);
  790. spin_unlock(&nn->client_lock);
  791. if (cses->flags & SESSION4_BACK_CHAN) {
  792. struct sockaddr *sa = svc_addr(rqstp);
  793. /*
  794. * This is a little silly; with sessions there's no real
  795. * use for the callback address. Use the peer address
  796. * as a reasonable default for now, but consider fixing
  797. * the rpc client not to require an address in the
  798. * future:
  799. */
  800. rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
  801. clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
  802. }
  803. }
  804. /* caller must hold client_lock */
  805. static struct nfsd4_session *
  806. find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
  807. {
  808. struct nfsd4_session *elem;
  809. int idx;
  810. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  811. dump_sessionid(__func__, sessionid);
  812. idx = hash_sessionid(sessionid);
  813. /* Search in the appropriate list */
  814. list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
  815. if (!memcmp(elem->se_sessionid.data, sessionid->data,
  816. NFS4_MAX_SESSIONID_LEN)) {
  817. return elem;
  818. }
  819. }
  820. dprintk("%s: session not found\n", __func__);
  821. return NULL;
  822. }
  823. /* caller must hold client_lock */
  824. static void
  825. unhash_session(struct nfsd4_session *ses)
  826. {
  827. list_del(&ses->se_hash);
  828. spin_lock(&ses->se_client->cl_lock);
  829. list_del(&ses->se_perclnt);
  830. spin_unlock(&ses->se_client->cl_lock);
  831. }
  832. /* must be called under the client_lock */
  833. static inline void
  834. renew_client_locked(struct nfs4_client *clp)
  835. {
  836. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  837. if (is_client_expired(clp)) {
  838. WARN_ON(1);
  839. printk("%s: client (clientid %08x/%08x) already expired\n",
  840. __func__,
  841. clp->cl_clientid.cl_boot,
  842. clp->cl_clientid.cl_id);
  843. return;
  844. }
  845. dprintk("renewing client (clientid %08x/%08x)\n",
  846. clp->cl_clientid.cl_boot,
  847. clp->cl_clientid.cl_id);
  848. list_move_tail(&clp->cl_lru, &nn->client_lru);
  849. clp->cl_time = get_seconds();
  850. }
  851. static inline void
  852. renew_client(struct nfs4_client *clp)
  853. {
  854. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  855. spin_lock(&nn->client_lock);
  856. renew_client_locked(clp);
  857. spin_unlock(&nn->client_lock);
  858. }
  859. /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
  860. static int
  861. STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
  862. {
  863. if (clid->cl_boot == nn->boot_time)
  864. return 0;
  865. dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
  866. clid->cl_boot, clid->cl_id, nn->boot_time);
  867. return 1;
  868. }
  869. /*
  870. * XXX Should we use a slab cache ?
  871. * This type of memory management is somewhat inefficient, but we use it
  872. * anyway since SETCLIENTID is not a common operation.
  873. */
  874. static struct nfs4_client *alloc_client(struct xdr_netobj name)
  875. {
  876. struct nfs4_client *clp;
  877. clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
  878. if (clp == NULL)
  879. return NULL;
  880. clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
  881. if (clp->cl_name.data == NULL) {
  882. kfree(clp);
  883. return NULL;
  884. }
  885. clp->cl_name.len = name.len;
  886. return clp;
  887. }
  888. static inline void
  889. free_client(struct nfs4_client *clp)
  890. {
  891. struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
  892. lockdep_assert_held(&nn->client_lock);
  893. while (!list_empty(&clp->cl_sessions)) {
  894. struct nfsd4_session *ses;
  895. ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
  896. se_perclnt);
  897. list_del(&ses->se_perclnt);
  898. nfsd4_put_session_locked(ses);
  899. }
  900. free_svc_cred(&clp->cl_cred);
  901. kfree(clp->cl_name.data);
  902. idr_destroy(&clp->cl_stateids);
  903. kfree(clp);
  904. }
  905. void
  906. release_session_client(struct nfsd4_session *session)
  907. {
  908. struct nfs4_client *clp = session->se_client;
  909. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  910. nfsd4_put_session(session);
  911. if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
  912. return;
  913. /*
  914. * At this point we also know all sessions have refcnt 1,
  915. * so free_client will delete them all if necessary:
  916. */
  917. if (is_client_expired(clp))
  918. free_client(clp);
  919. else
  920. renew_client_locked(clp);
  921. spin_unlock(&nn->client_lock);
  922. }
  923. /* must be called under the client_lock */
  924. static inline void
  925. unhash_client_locked(struct nfs4_client *clp)
  926. {
  927. struct nfsd4_session *ses;
  928. mark_client_expired(clp);
  929. list_del(&clp->cl_lru);
  930. spin_lock(&clp->cl_lock);
  931. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  932. list_del_init(&ses->se_hash);
  933. spin_unlock(&clp->cl_lock);
  934. }
  935. static void
  936. destroy_client(struct nfs4_client *clp)
  937. {
  938. struct nfs4_openowner *oo;
  939. struct nfs4_delegation *dp;
  940. struct list_head reaplist;
  941. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  942. INIT_LIST_HEAD(&reaplist);
  943. spin_lock(&recall_lock);
  944. while (!list_empty(&clp->cl_delegations)) {
  945. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  946. list_del_init(&dp->dl_perclnt);
  947. list_move(&dp->dl_recall_lru, &reaplist);
  948. }
  949. spin_unlock(&recall_lock);
  950. while (!list_empty(&reaplist)) {
  951. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  952. unhash_delegation(dp);
  953. }
  954. while (!list_empty(&clp->cl_openowners)) {
  955. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  956. release_openowner(oo);
  957. }
  958. nfsd4_shutdown_callback(clp);
  959. if (clp->cl_cb_conn.cb_xprt)
  960. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  961. list_del(&clp->cl_idhash);
  962. if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  963. rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
  964. else
  965. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  966. spin_lock(&nn->client_lock);
  967. unhash_client_locked(clp);
  968. if (atomic_read(&clp->cl_refcount) == 0)
  969. free_client(clp);
  970. spin_unlock(&nn->client_lock);
  971. }
  972. static void expire_client(struct nfs4_client *clp)
  973. {
  974. nfsd4_client_record_remove(clp);
  975. destroy_client(clp);
  976. }
  977. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  978. {
  979. memcpy(target->cl_verifier.data, source->data,
  980. sizeof(target->cl_verifier.data));
  981. }
  982. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  983. {
  984. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  985. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  986. }
  987. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  988. {
  989. if (source->cr_principal) {
  990. target->cr_principal =
  991. kstrdup(source->cr_principal, GFP_KERNEL);
  992. if (target->cr_principal == NULL)
  993. return -ENOMEM;
  994. } else
  995. target->cr_principal = NULL;
  996. target->cr_flavor = source->cr_flavor;
  997. target->cr_uid = source->cr_uid;
  998. target->cr_gid = source->cr_gid;
  999. target->cr_group_info = source->cr_group_info;
  1000. get_group_info(target->cr_group_info);
  1001. return 0;
  1002. }
  1003. static long long
  1004. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  1005. {
  1006. long long res;
  1007. res = o1->len - o2->len;
  1008. if (res)
  1009. return res;
  1010. return (long long)memcmp(o1->data, o2->data, o1->len);
  1011. }
  1012. static int same_name(const char *n1, const char *n2)
  1013. {
  1014. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  1015. }
  1016. static int
  1017. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  1018. {
  1019. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  1020. }
  1021. static int
  1022. same_clid(clientid_t *cl1, clientid_t *cl2)
  1023. {
  1024. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1025. }
  1026. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  1027. {
  1028. int i;
  1029. if (g1->ngroups != g2->ngroups)
  1030. return false;
  1031. for (i=0; i<g1->ngroups; i++)
  1032. if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
  1033. return false;
  1034. return true;
  1035. }
  1036. /*
  1037. * RFC 3530 language requires clid_inuse be returned when the
  1038. * "principal" associated with a requests differs from that previously
  1039. * used. We use uid, gid's, and gss principal string as our best
  1040. * approximation. We also don't want to allow non-gss use of a client
  1041. * established using gss: in theory cr_principal should catch that
  1042. * change, but in practice cr_principal can be null even in the gss case
  1043. * since gssd doesn't always pass down a principal string.
  1044. */
  1045. static bool is_gss_cred(struct svc_cred *cr)
  1046. {
  1047. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  1048. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  1049. }
  1050. static bool
  1051. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1052. {
  1053. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  1054. || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
  1055. || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
  1056. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  1057. return false;
  1058. if (cr1->cr_principal == cr2->cr_principal)
  1059. return true;
  1060. if (!cr1->cr_principal || !cr2->cr_principal)
  1061. return false;
  1062. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  1063. }
  1064. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  1065. {
  1066. static u32 current_clientid = 1;
  1067. clp->cl_clientid.cl_boot = nn->boot_time;
  1068. clp->cl_clientid.cl_id = current_clientid++;
  1069. }
  1070. static void gen_confirm(struct nfs4_client *clp)
  1071. {
  1072. __be32 verf[2];
  1073. static u32 i;
  1074. verf[0] = (__be32)get_seconds();
  1075. verf[1] = (__be32)i++;
  1076. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1077. }
  1078. static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
  1079. {
  1080. struct nfs4_stid *ret;
  1081. ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1082. if (!ret || !ret->sc_type)
  1083. return NULL;
  1084. return ret;
  1085. }
  1086. static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1087. {
  1088. struct nfs4_stid *s;
  1089. s = find_stateid(cl, t);
  1090. if (!s)
  1091. return NULL;
  1092. if (typemask & s->sc_type)
  1093. return s;
  1094. return NULL;
  1095. }
  1096. static struct nfs4_client *create_client(struct xdr_netobj name,
  1097. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1098. {
  1099. struct nfs4_client *clp;
  1100. struct sockaddr *sa = svc_addr(rqstp);
  1101. int ret;
  1102. struct net *net = SVC_NET(rqstp);
  1103. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1104. clp = alloc_client(name);
  1105. if (clp == NULL)
  1106. return NULL;
  1107. INIT_LIST_HEAD(&clp->cl_sessions);
  1108. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1109. if (ret) {
  1110. spin_lock(&nn->client_lock);
  1111. free_client(clp);
  1112. spin_unlock(&nn->client_lock);
  1113. return NULL;
  1114. }
  1115. idr_init(&clp->cl_stateids);
  1116. atomic_set(&clp->cl_refcount, 0);
  1117. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1118. INIT_LIST_HEAD(&clp->cl_idhash);
  1119. INIT_LIST_HEAD(&clp->cl_openowners);
  1120. INIT_LIST_HEAD(&clp->cl_delegations);
  1121. INIT_LIST_HEAD(&clp->cl_lru);
  1122. INIT_LIST_HEAD(&clp->cl_callbacks);
  1123. spin_lock_init(&clp->cl_lock);
  1124. nfsd4_init_callback(&clp->cl_cb_null);
  1125. clp->cl_time = get_seconds();
  1126. clear_bit(0, &clp->cl_cb_slot_busy);
  1127. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1128. copy_verf(clp, verf);
  1129. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1130. gen_confirm(clp);
  1131. clp->cl_cb_session = NULL;
  1132. clp->net = net;
  1133. return clp;
  1134. }
  1135. static void
  1136. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  1137. {
  1138. struct rb_node **new = &(root->rb_node), *parent = NULL;
  1139. struct nfs4_client *clp;
  1140. while (*new) {
  1141. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  1142. parent = *new;
  1143. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  1144. new = &((*new)->rb_left);
  1145. else
  1146. new = &((*new)->rb_right);
  1147. }
  1148. rb_link_node(&new_clp->cl_namenode, parent, new);
  1149. rb_insert_color(&new_clp->cl_namenode, root);
  1150. }
  1151. static struct nfs4_client *
  1152. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  1153. {
  1154. long long cmp;
  1155. struct rb_node *node = root->rb_node;
  1156. struct nfs4_client *clp;
  1157. while (node) {
  1158. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  1159. cmp = compare_blob(&clp->cl_name, name);
  1160. if (cmp > 0)
  1161. node = node->rb_left;
  1162. else if (cmp < 0)
  1163. node = node->rb_right;
  1164. else
  1165. return clp;
  1166. }
  1167. return NULL;
  1168. }
  1169. static void
  1170. add_to_unconfirmed(struct nfs4_client *clp)
  1171. {
  1172. unsigned int idhashval;
  1173. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1174. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1175. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  1176. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1177. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  1178. renew_client(clp);
  1179. }
  1180. static void
  1181. move_to_confirmed(struct nfs4_client *clp)
  1182. {
  1183. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1184. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1185. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1186. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  1187. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1188. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  1189. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1190. renew_client(clp);
  1191. }
  1192. static struct nfs4_client *
  1193. find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
  1194. {
  1195. struct nfs4_client *clp;
  1196. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1197. list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
  1198. if (same_clid(&clp->cl_clientid, clid)) {
  1199. if ((bool)clp->cl_minorversion != sessions)
  1200. return NULL;
  1201. renew_client(clp);
  1202. return clp;
  1203. }
  1204. }
  1205. return NULL;
  1206. }
  1207. static struct nfs4_client *
  1208. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1209. {
  1210. struct list_head *tbl = nn->conf_id_hashtbl;
  1211. return find_client_in_id_table(tbl, clid, sessions);
  1212. }
  1213. static struct nfs4_client *
  1214. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1215. {
  1216. struct list_head *tbl = nn->unconf_id_hashtbl;
  1217. return find_client_in_id_table(tbl, clid, sessions);
  1218. }
  1219. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1220. {
  1221. return clp->cl_exchange_flags != 0;
  1222. }
  1223. static struct nfs4_client *
  1224. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1225. {
  1226. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  1227. }
  1228. static struct nfs4_client *
  1229. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1230. {
  1231. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  1232. }
  1233. static void
  1234. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1235. {
  1236. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1237. struct sockaddr *sa = svc_addr(rqstp);
  1238. u32 scopeid = rpc_get_scope_id(sa);
  1239. unsigned short expected_family;
  1240. /* Currently, we only support tcp and tcp6 for the callback channel */
  1241. if (se->se_callback_netid_len == 3 &&
  1242. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1243. expected_family = AF_INET;
  1244. else if (se->se_callback_netid_len == 4 &&
  1245. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1246. expected_family = AF_INET6;
  1247. else
  1248. goto out_err;
  1249. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  1250. se->se_callback_addr_len,
  1251. (struct sockaddr *)&conn->cb_addr,
  1252. sizeof(conn->cb_addr));
  1253. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1254. goto out_err;
  1255. if (conn->cb_addr.ss_family == AF_INET6)
  1256. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1257. conn->cb_prog = se->se_callback_prog;
  1258. conn->cb_ident = se->se_callback_ident;
  1259. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1260. return;
  1261. out_err:
  1262. conn->cb_addr.ss_family = AF_UNSPEC;
  1263. conn->cb_addrlen = 0;
  1264. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1265. "will not receive delegations\n",
  1266. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1267. return;
  1268. }
  1269. /*
  1270. * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
  1271. */
  1272. void
  1273. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1274. {
  1275. struct nfsd4_slot *slot = resp->cstate.slot;
  1276. unsigned int base;
  1277. dprintk("--> %s slot %p\n", __func__, slot);
  1278. slot->sl_opcnt = resp->opcnt;
  1279. slot->sl_status = resp->cstate.status;
  1280. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1281. if (nfsd4_not_cached(resp)) {
  1282. slot->sl_datalen = 0;
  1283. return;
  1284. }
  1285. slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
  1286. base = (char *)resp->cstate.datap -
  1287. (char *)resp->xbuf->head[0].iov_base;
  1288. if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
  1289. slot->sl_datalen))
  1290. WARN("%s: sessions DRC could not cache compound\n", __func__);
  1291. return;
  1292. }
  1293. /*
  1294. * Encode the replay sequence operation from the slot values.
  1295. * If cachethis is FALSE encode the uncached rep error on the next
  1296. * operation which sets resp->p and increments resp->opcnt for
  1297. * nfs4svc_encode_compoundres.
  1298. *
  1299. */
  1300. static __be32
  1301. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1302. struct nfsd4_compoundres *resp)
  1303. {
  1304. struct nfsd4_op *op;
  1305. struct nfsd4_slot *slot = resp->cstate.slot;
  1306. /* Encode the replayed sequence operation */
  1307. op = &args->ops[resp->opcnt - 1];
  1308. nfsd4_encode_operation(resp, op);
  1309. /* Return nfserr_retry_uncached_rep in next operation. */
  1310. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1311. op = &args->ops[resp->opcnt++];
  1312. op->status = nfserr_retry_uncached_rep;
  1313. nfsd4_encode_operation(resp, op);
  1314. }
  1315. return op->status;
  1316. }
  1317. /*
  1318. * The sequence operation is not cached because we can use the slot and
  1319. * session values.
  1320. */
  1321. __be32
  1322. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1323. struct nfsd4_sequence *seq)
  1324. {
  1325. struct nfsd4_slot *slot = resp->cstate.slot;
  1326. __be32 status;
  1327. dprintk("--> %s slot %p\n", __func__, slot);
  1328. /* Either returns 0 or nfserr_retry_uncached */
  1329. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  1330. if (status == nfserr_retry_uncached_rep)
  1331. return status;
  1332. /* The sequence operation has been encoded, cstate->datap set. */
  1333. memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
  1334. resp->opcnt = slot->sl_opcnt;
  1335. resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
  1336. status = slot->sl_status;
  1337. return status;
  1338. }
  1339. /*
  1340. * Set the exchange_id flags returned by the server.
  1341. */
  1342. static void
  1343. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  1344. {
  1345. /* pNFS is not supported */
  1346. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  1347. /* Referrals are supported, Migration is not. */
  1348. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  1349. /* set the wire flags to return to client. */
  1350. clid->flags = new->cl_exchange_flags;
  1351. }
  1352. static bool client_has_state(struct nfs4_client *clp)
  1353. {
  1354. /*
  1355. * Note clp->cl_openowners check isn't quite right: there's no
  1356. * need to count owners without stateid's.
  1357. *
  1358. * Also note we should probably be using this in 4.0 case too.
  1359. */
  1360. return !list_empty(&clp->cl_openowners)
  1361. || !list_empty(&clp->cl_delegations)
  1362. || !list_empty(&clp->cl_sessions);
  1363. }
  1364. __be32
  1365. nfsd4_exchange_id(struct svc_rqst *rqstp,
  1366. struct nfsd4_compound_state *cstate,
  1367. struct nfsd4_exchange_id *exid)
  1368. {
  1369. struct nfs4_client *unconf, *conf, *new;
  1370. __be32 status;
  1371. char addr_str[INET6_ADDRSTRLEN];
  1372. nfs4_verifier verf = exid->verifier;
  1373. struct sockaddr *sa = svc_addr(rqstp);
  1374. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  1375. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1376. rpc_ntop(sa, addr_str, sizeof(addr_str));
  1377. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  1378. "ip_addr=%s flags %x, spa_how %d\n",
  1379. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  1380. addr_str, exid->flags, exid->spa_how);
  1381. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  1382. return nfserr_inval;
  1383. /* Currently only support SP4_NONE */
  1384. switch (exid->spa_how) {
  1385. case SP4_NONE:
  1386. break;
  1387. default: /* checked by xdr code */
  1388. WARN_ON_ONCE(1);
  1389. case SP4_SSV:
  1390. case SP4_MACH_CRED:
  1391. return nfserr_serverfault; /* no excuse :-/ */
  1392. }
  1393. /* Cases below refer to rfc 5661 section 18.35.4: */
  1394. nfs4_lock_state();
  1395. conf = find_confirmed_client_by_name(&exid->clname, nn);
  1396. if (conf) {
  1397. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  1398. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  1399. if (update) {
  1400. if (!clp_used_exchangeid(conf)) { /* buggy client */
  1401. status = nfserr_inval;
  1402. goto out;
  1403. }
  1404. if (!creds_match) { /* case 9 */
  1405. status = nfserr_perm;
  1406. goto out;
  1407. }
  1408. if (!verfs_match) { /* case 8 */
  1409. status = nfserr_not_same;
  1410. goto out;
  1411. }
  1412. /* case 6 */
  1413. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1414. new = conf;
  1415. goto out_copy;
  1416. }
  1417. if (!creds_match) { /* case 3 */
  1418. if (client_has_state(conf)) {
  1419. status = nfserr_clid_inuse;
  1420. goto out;
  1421. }
  1422. expire_client(conf);
  1423. goto out_new;
  1424. }
  1425. if (verfs_match) { /* case 2 */
  1426. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1427. new = conf;
  1428. goto out_copy;
  1429. }
  1430. /* case 5, client reboot */
  1431. goto out_new;
  1432. }
  1433. if (update) { /* case 7 */
  1434. status = nfserr_noent;
  1435. goto out;
  1436. }
  1437. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  1438. if (unconf) /* case 4, possible retry or client restart */
  1439. expire_client(unconf);
  1440. /* case 1 (normal case) */
  1441. out_new:
  1442. new = create_client(exid->clname, rqstp, &verf);
  1443. if (new == NULL) {
  1444. status = nfserr_jukebox;
  1445. goto out;
  1446. }
  1447. new->cl_minorversion = 1;
  1448. gen_clid(new, nn);
  1449. add_to_unconfirmed(new);
  1450. out_copy:
  1451. exid->clientid.cl_boot = new->cl_clientid.cl_boot;
  1452. exid->clientid.cl_id = new->cl_clientid.cl_id;
  1453. exid->seqid = new->cl_cs_slot.sl_seqid + 1;
  1454. nfsd4_set_ex_flags(new, exid);
  1455. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  1456. new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
  1457. status = nfs_ok;
  1458. out:
  1459. nfs4_unlock_state();
  1460. return status;
  1461. }
  1462. static __be32
  1463. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  1464. {
  1465. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  1466. slot_seqid);
  1467. /* The slot is in use, and no response has been sent. */
  1468. if (slot_inuse) {
  1469. if (seqid == slot_seqid)
  1470. return nfserr_jukebox;
  1471. else
  1472. return nfserr_seq_misordered;
  1473. }
  1474. /* Note unsigned 32-bit arithmetic handles wraparound: */
  1475. if (likely(seqid == slot_seqid + 1))
  1476. return nfs_ok;
  1477. if (seqid == slot_seqid)
  1478. return nfserr_replay_cache;
  1479. return nfserr_seq_misordered;
  1480. }
  1481. /*
  1482. * Cache the create session result into the create session single DRC
  1483. * slot cache by saving the xdr structure. sl_seqid has been set.
  1484. * Do this for solo or embedded create session operations.
  1485. */
  1486. static void
  1487. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  1488. struct nfsd4_clid_slot *slot, __be32 nfserr)
  1489. {
  1490. slot->sl_status = nfserr;
  1491. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  1492. }
  1493. static __be32
  1494. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  1495. struct nfsd4_clid_slot *slot)
  1496. {
  1497. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  1498. return slot->sl_status;
  1499. }
  1500. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  1501. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  1502. 1 + /* MIN tag is length with zero, only length */ \
  1503. 3 + /* version, opcount, opcode */ \
  1504. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1505. /* seqid, slotID, slotID, cache */ \
  1506. 4 ) * sizeof(__be32))
  1507. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  1508. 2 + /* verifier: AUTH_NULL, length 0 */\
  1509. 1 + /* status */ \
  1510. 1 + /* MIN tag is length with zero, only length */ \
  1511. 3 + /* opcount, opcode, opstatus*/ \
  1512. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1513. /* seqid, slotID, slotID, slotID, status */ \
  1514. 5 ) * sizeof(__be32))
  1515. static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
  1516. {
  1517. return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
  1518. || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
  1519. }
  1520. __be32
  1521. nfsd4_create_session(struct svc_rqst *rqstp,
  1522. struct nfsd4_compound_state *cstate,
  1523. struct nfsd4_create_session *cr_ses)
  1524. {
  1525. struct sockaddr *sa = svc_addr(rqstp);
  1526. struct nfs4_client *conf, *unconf;
  1527. struct nfsd4_session *new;
  1528. struct nfsd4_conn *conn;
  1529. struct nfsd4_clid_slot *cs_slot = NULL;
  1530. __be32 status = 0;
  1531. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1532. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  1533. return nfserr_inval;
  1534. if (check_forechannel_attrs(cr_ses->fore_channel))
  1535. return nfserr_toosmall;
  1536. new = alloc_session(&cr_ses->fore_channel, nn);
  1537. if (!new)
  1538. return nfserr_jukebox;
  1539. status = nfserr_jukebox;
  1540. conn = alloc_conn_from_crses(rqstp, cr_ses);
  1541. if (!conn)
  1542. goto out_free_session;
  1543. nfs4_lock_state();
  1544. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  1545. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  1546. WARN_ON_ONCE(conf && unconf);
  1547. if (conf) {
  1548. cs_slot = &conf->cl_cs_slot;
  1549. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1550. if (status == nfserr_replay_cache) {
  1551. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  1552. goto out_free_conn;
  1553. } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
  1554. status = nfserr_seq_misordered;
  1555. goto out_free_conn;
  1556. }
  1557. } else if (unconf) {
  1558. struct nfs4_client *old;
  1559. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  1560. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  1561. status = nfserr_clid_inuse;
  1562. goto out_free_conn;
  1563. }
  1564. cs_slot = &unconf->cl_cs_slot;
  1565. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1566. if (status) {
  1567. /* an unconfirmed replay returns misordered */
  1568. status = nfserr_seq_misordered;
  1569. goto out_free_conn;
  1570. }
  1571. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1572. if (old)
  1573. expire_client(old);
  1574. move_to_confirmed(unconf);
  1575. conf = unconf;
  1576. } else {
  1577. status = nfserr_stale_clientid;
  1578. goto out_free_conn;
  1579. }
  1580. status = nfs_ok;
  1581. /*
  1582. * We do not support RDMA or persistent sessions
  1583. */
  1584. cr_ses->flags &= ~SESSION4_PERSIST;
  1585. cr_ses->flags &= ~SESSION4_RDMA;
  1586. init_session(rqstp, new, conf, cr_ses);
  1587. nfsd4_init_conn(rqstp, conn, new);
  1588. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  1589. NFS4_MAX_SESSIONID_LEN);
  1590. memcpy(&cr_ses->fore_channel, &new->se_fchannel,
  1591. sizeof(struct nfsd4_channel_attrs));
  1592. cs_slot->sl_seqid++;
  1593. cr_ses->seqid = cs_slot->sl_seqid;
  1594. /* cache solo and embedded create sessions under the state lock */
  1595. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  1596. nfs4_unlock_state();
  1597. return status;
  1598. out_free_conn:
  1599. nfs4_unlock_state();
  1600. free_conn(conn);
  1601. out_free_session:
  1602. __free_session(new);
  1603. return status;
  1604. }
  1605. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  1606. {
  1607. switch (*dir) {
  1608. case NFS4_CDFC4_FORE:
  1609. case NFS4_CDFC4_BACK:
  1610. return nfs_ok;
  1611. case NFS4_CDFC4_FORE_OR_BOTH:
  1612. case NFS4_CDFC4_BACK_OR_BOTH:
  1613. *dir = NFS4_CDFC4_BOTH;
  1614. return nfs_ok;
  1615. };
  1616. return nfserr_inval;
  1617. }
  1618. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
  1619. {
  1620. struct nfsd4_session *session = cstate->session;
  1621. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1622. spin_lock(&nn->client_lock);
  1623. session->se_cb_prog = bc->bc_cb_program;
  1624. session->se_cb_sec = bc->bc_cb_sec;
  1625. spin_unlock(&nn->client_lock);
  1626. nfsd4_probe_callback(session->se_client);
  1627. return nfs_ok;
  1628. }
  1629. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  1630. struct nfsd4_compound_state *cstate,
  1631. struct nfsd4_bind_conn_to_session *bcts)
  1632. {
  1633. __be32 status;
  1634. struct nfsd4_conn *conn;
  1635. struct nfsd4_session *session;
  1636. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1637. if (!nfsd4_last_compound_op(rqstp))
  1638. return nfserr_not_only_op;
  1639. nfs4_lock_state();
  1640. spin_lock(&nn->client_lock);
  1641. session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
  1642. spin_unlock(&nn->client_lock);
  1643. status = nfserr_badsession;
  1644. if (!session)
  1645. goto out;
  1646. status = nfsd4_map_bcts_dir(&bcts->dir);
  1647. if (status)
  1648. goto out;
  1649. conn = alloc_conn(rqstp, bcts->dir);
  1650. status = nfserr_jukebox;
  1651. if (!conn)
  1652. goto out;
  1653. nfsd4_init_conn(rqstp, conn, session);
  1654. status = nfs_ok;
  1655. out:
  1656. nfs4_unlock_state();
  1657. return status;
  1658. }
  1659. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  1660. {
  1661. if (!session)
  1662. return 0;
  1663. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  1664. }
  1665. __be32
  1666. nfsd4_destroy_session(struct svc_rqst *r,
  1667. struct nfsd4_compound_state *cstate,
  1668. struct nfsd4_destroy_session *sessionid)
  1669. {
  1670. struct nfsd4_session *ses;
  1671. __be32 status;
  1672. struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
  1673. nfs4_lock_state();
  1674. status = nfserr_not_only_op;
  1675. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  1676. if (!nfsd4_last_compound_op(r))
  1677. goto out;
  1678. }
  1679. dump_sessionid(__func__, &sessionid->sessionid);
  1680. spin_lock(&nn->client_lock);
  1681. ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
  1682. status = nfserr_badsession;
  1683. if (!ses)
  1684. goto out_client_lock;
  1685. unhash_session(ses);
  1686. spin_unlock(&nn->client_lock);
  1687. nfsd4_probe_callback_sync(ses->se_client);
  1688. spin_lock(&nn->client_lock);
  1689. nfsd4_del_conns(ses);
  1690. nfsd4_put_session_locked(ses);
  1691. status = nfs_ok;
  1692. out_client_lock:
  1693. spin_unlock(&nn->client_lock);
  1694. out:
  1695. nfs4_unlock_state();
  1696. return status;
  1697. }
  1698. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  1699. {
  1700. struct nfsd4_conn *c;
  1701. list_for_each_entry(c, &s->se_conns, cn_persession) {
  1702. if (c->cn_xprt == xpt) {
  1703. return c;
  1704. }
  1705. }
  1706. return NULL;
  1707. }
  1708. static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  1709. {
  1710. struct nfs4_client *clp = ses->se_client;
  1711. struct nfsd4_conn *c;
  1712. int ret;
  1713. spin_lock(&clp->cl_lock);
  1714. c = __nfsd4_find_conn(new->cn_xprt, ses);
  1715. if (c) {
  1716. spin_unlock(&clp->cl_lock);
  1717. free_conn(new);
  1718. return;
  1719. }
  1720. __nfsd4_hash_conn(new, ses);
  1721. spin_unlock(&clp->cl_lock);
  1722. ret = nfsd4_register_conn(new);
  1723. if (ret)
  1724. /* oops; xprt is already down: */
  1725. nfsd4_conn_lost(&new->cn_xpt_user);
  1726. return;
  1727. }
  1728. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  1729. {
  1730. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1731. return args->opcnt > session->se_fchannel.maxops;
  1732. }
  1733. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  1734. struct nfsd4_session *session)
  1735. {
  1736. struct xdr_buf *xb = &rqstp->rq_arg;
  1737. return xb->len > session->se_fchannel.maxreq_sz;
  1738. }
  1739. __be32
  1740. nfsd4_sequence(struct svc_rqst *rqstp,
  1741. struct nfsd4_compound_state *cstate,
  1742. struct nfsd4_sequence *seq)
  1743. {
  1744. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1745. struct nfsd4_session *session;
  1746. struct nfsd4_slot *slot;
  1747. struct nfsd4_conn *conn;
  1748. __be32 status;
  1749. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1750. if (resp->opcnt != 1)
  1751. return nfserr_sequence_pos;
  1752. /*
  1753. * Will be either used or freed by nfsd4_sequence_check_conn
  1754. * below.
  1755. */
  1756. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  1757. if (!conn)
  1758. return nfserr_jukebox;
  1759. spin_lock(&nn->client_lock);
  1760. status = nfserr_badsession;
  1761. session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
  1762. if (!session)
  1763. goto out;
  1764. status = nfserr_too_many_ops;
  1765. if (nfsd4_session_too_many_ops(rqstp, session))
  1766. goto out;
  1767. status = nfserr_req_too_big;
  1768. if (nfsd4_request_too_big(rqstp, session))
  1769. goto out;
  1770. status = nfserr_badslot;
  1771. if (seq->slotid >= session->se_fchannel.maxreqs)
  1772. goto out;
  1773. slot = session->se_slots[seq->slotid];
  1774. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  1775. /* We do not negotiate the number of slots yet, so set the
  1776. * maxslots to the session maxreqs which is used to encode
  1777. * sr_highest_slotid and the sr_target_slot id to maxslots */
  1778. seq->maxslots = session->se_fchannel.maxreqs;
  1779. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  1780. slot->sl_flags & NFSD4_SLOT_INUSE);
  1781. if (status == nfserr_replay_cache) {
  1782. status = nfserr_seq_misordered;
  1783. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  1784. goto out;
  1785. cstate->slot = slot;
  1786. cstate->session = session;
  1787. /* Return the cached reply status and set cstate->status
  1788. * for nfsd4_proc_compound processing */
  1789. status = nfsd4_replay_cache_entry(resp, seq);
  1790. cstate->status = nfserr_replay_cache;
  1791. goto out;
  1792. }
  1793. if (status)
  1794. goto out;
  1795. nfsd4_sequence_check_conn(conn, session);
  1796. conn = NULL;
  1797. /* Success! bump slot seqid */
  1798. slot->sl_seqid = seq->seqid;
  1799. slot->sl_flags |= NFSD4_SLOT_INUSE;
  1800. if (seq->cachethis)
  1801. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  1802. else
  1803. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  1804. cstate->slot = slot;
  1805. cstate->session = session;
  1806. out:
  1807. /* Hold a session reference until done processing the compound. */
  1808. if (cstate->session) {
  1809. struct nfs4_client *clp = session->se_client;
  1810. nfsd4_get_session(cstate->session);
  1811. atomic_inc(&clp->cl_refcount);
  1812. switch (clp->cl_cb_state) {
  1813. case NFSD4_CB_DOWN:
  1814. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  1815. break;
  1816. case NFSD4_CB_FAULT:
  1817. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  1818. break;
  1819. default:
  1820. seq->status_flags = 0;
  1821. }
  1822. }
  1823. kfree(conn);
  1824. spin_unlock(&nn->client_lock);
  1825. return status;
  1826. }
  1827. __be32
  1828. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  1829. {
  1830. struct nfs4_client *conf, *unconf, *clp;
  1831. __be32 status = 0;
  1832. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1833. nfs4_lock_state();
  1834. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  1835. conf = find_confirmed_client(&dc->clientid, true, nn);
  1836. WARN_ON_ONCE(conf && unconf);
  1837. if (conf) {
  1838. clp = conf;
  1839. if (client_has_state(conf)) {
  1840. status = nfserr_clientid_busy;
  1841. goto out;
  1842. }
  1843. } else if (unconf)
  1844. clp = unconf;
  1845. else {
  1846. status = nfserr_stale_clientid;
  1847. goto out;
  1848. }
  1849. expire_client(clp);
  1850. out:
  1851. nfs4_unlock_state();
  1852. return status;
  1853. }
  1854. __be32
  1855. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  1856. {
  1857. __be32 status = 0;
  1858. if (rc->rca_one_fs) {
  1859. if (!cstate->current_fh.fh_dentry)
  1860. return nfserr_nofilehandle;
  1861. /*
  1862. * We don't take advantage of the rca_one_fs case.
  1863. * That's OK, it's optional, we can safely ignore it.
  1864. */
  1865. return nfs_ok;
  1866. }
  1867. nfs4_lock_state();
  1868. status = nfserr_complete_already;
  1869. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  1870. &cstate->session->se_client->cl_flags))
  1871. goto out;
  1872. status = nfserr_stale_clientid;
  1873. if (is_client_expired(cstate->session->se_client))
  1874. /*
  1875. * The following error isn't really legal.
  1876. * But we only get here if the client just explicitly
  1877. * destroyed the client. Surely it no longer cares what
  1878. * error it gets back on an operation for the dead
  1879. * client.
  1880. */
  1881. goto out;
  1882. status = nfs_ok;
  1883. nfsd4_client_record_create(cstate->session->se_client);
  1884. out:
  1885. nfs4_unlock_state();
  1886. return status;
  1887. }
  1888. __be32
  1889. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1890. struct nfsd4_setclientid *setclid)
  1891. {
  1892. struct xdr_netobj clname = setclid->se_name;
  1893. nfs4_verifier clverifier = setclid->se_verf;
  1894. struct nfs4_client *conf, *unconf, *new;
  1895. __be32 status;
  1896. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1897. /* Cases below refer to rfc 3530 section 14.2.33: */
  1898. nfs4_lock_state();
  1899. conf = find_confirmed_client_by_name(&clname, nn);
  1900. if (conf) {
  1901. /* case 0: */
  1902. status = nfserr_clid_inuse;
  1903. if (clp_used_exchangeid(conf))
  1904. goto out;
  1905. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  1906. char addr_str[INET6_ADDRSTRLEN];
  1907. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  1908. sizeof(addr_str));
  1909. dprintk("NFSD: setclientid: string in use by client "
  1910. "at %s\n", addr_str);
  1911. goto out;
  1912. }
  1913. }
  1914. unconf = find_unconfirmed_client_by_name(&clname, nn);
  1915. if (unconf)
  1916. expire_client(unconf);
  1917. status = nfserr_jukebox;
  1918. new = create_client(clname, rqstp, &clverifier);
  1919. if (new == NULL)
  1920. goto out;
  1921. if (conf && same_verf(&conf->cl_verifier, &clverifier))
  1922. /* case 1: probable callback update */
  1923. copy_clid(new, conf);
  1924. else /* case 4 (new client) or cases 2, 3 (client reboot): */
  1925. gen_clid(new, nn);
  1926. new->cl_minorversion = 0;
  1927. gen_callback(new, setclid, rqstp);
  1928. add_to_unconfirmed(new);
  1929. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  1930. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  1931. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  1932. status = nfs_ok;
  1933. out:
  1934. nfs4_unlock_state();
  1935. return status;
  1936. }
  1937. __be32
  1938. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  1939. struct nfsd4_compound_state *cstate,
  1940. struct nfsd4_setclientid_confirm *setclientid_confirm)
  1941. {
  1942. struct nfs4_client *conf, *unconf;
  1943. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  1944. clientid_t * clid = &setclientid_confirm->sc_clientid;
  1945. __be32 status;
  1946. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1947. if (STALE_CLIENTID(clid, nn))
  1948. return nfserr_stale_clientid;
  1949. nfs4_lock_state();
  1950. conf = find_confirmed_client(clid, false, nn);
  1951. unconf = find_unconfirmed_client(clid, false, nn);
  1952. /*
  1953. * We try hard to give out unique clientid's, so if we get an
  1954. * attempt to confirm the same clientid with a different cred,
  1955. * there's a bug somewhere. Let's charitably assume it's our
  1956. * bug.
  1957. */
  1958. status = nfserr_serverfault;
  1959. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
  1960. goto out;
  1961. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
  1962. goto out;
  1963. /* cases below refer to rfc 3530 section 14.2.34: */
  1964. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  1965. if (conf && !unconf) /* case 2: probable retransmit */
  1966. status = nfs_ok;
  1967. else /* case 4: client hasn't noticed we rebooted yet? */
  1968. status = nfserr_stale_clientid;
  1969. goto out;
  1970. }
  1971. status = nfs_ok;
  1972. if (conf) { /* case 1: callback update */
  1973. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  1974. nfsd4_probe_callback(conf);
  1975. expire_client(unconf);
  1976. } else { /* case 3: normal case; new or rebooted client */
  1977. conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1978. if (conf)
  1979. expire_client(conf);
  1980. move_to_confirmed(unconf);
  1981. nfsd4_probe_callback(unconf);
  1982. }
  1983. out:
  1984. nfs4_unlock_state();
  1985. return status;
  1986. }
  1987. static struct nfs4_file *nfsd4_alloc_file(void)
  1988. {
  1989. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  1990. }
  1991. /* OPEN Share state helper functions */
  1992. static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
  1993. {
  1994. unsigned int hashval = file_hashval(ino);
  1995. atomic_set(&fp->fi_ref, 1);
  1996. INIT_LIST_HEAD(&fp->fi_hash);
  1997. INIT_LIST_HEAD(&fp->fi_stateids);
  1998. INIT_LIST_HEAD(&fp->fi_delegations);
  1999. fp->fi_inode = igrab(ino);
  2000. fp->fi_had_conflict = false;
  2001. fp->fi_lease = NULL;
  2002. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2003. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2004. spin_lock(&recall_lock);
  2005. list_add(&fp->fi_hash, &file_hashtbl[hashval]);
  2006. spin_unlock(&recall_lock);
  2007. }
  2008. static void
  2009. nfsd4_free_slab(struct kmem_cache **slab)
  2010. {
  2011. if (*slab == NULL)
  2012. return;
  2013. kmem_cache_destroy(*slab);
  2014. *slab = NULL;
  2015. }
  2016. void
  2017. nfsd4_free_slabs(void)
  2018. {
  2019. nfsd4_free_slab(&openowner_slab);
  2020. nfsd4_free_slab(&lockowner_slab);
  2021. nfsd4_free_slab(&file_slab);
  2022. nfsd4_free_slab(&stateid_slab);
  2023. nfsd4_free_slab(&deleg_slab);
  2024. }
  2025. int
  2026. nfsd4_init_slabs(void)
  2027. {
  2028. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2029. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2030. if (openowner_slab == NULL)
  2031. goto out_nomem;
  2032. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2033. sizeof(struct nfs4_lockowner), 0, 0, NULL);
  2034. if (lockowner_slab == NULL)
  2035. goto out_nomem;
  2036. file_slab = kmem_cache_create("nfsd4_files",
  2037. sizeof(struct nfs4_file), 0, 0, NULL);
  2038. if (file_slab == NULL)
  2039. goto out_nomem;
  2040. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2041. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2042. if (stateid_slab == NULL)
  2043. goto out_nomem;
  2044. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2045. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2046. if (deleg_slab == NULL)
  2047. goto out_nomem;
  2048. return 0;
  2049. out_nomem:
  2050. nfsd4_free_slabs();
  2051. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2052. return -ENOMEM;
  2053. }
  2054. void nfs4_free_openowner(struct nfs4_openowner *oo)
  2055. {
  2056. kfree(oo->oo_owner.so_owner.data);
  2057. kmem_cache_free(openowner_slab, oo);
  2058. }
  2059. void nfs4_free_lockowner(struct nfs4_lockowner *lo)
  2060. {
  2061. kfree(lo->lo_owner.so_owner.data);
  2062. kmem_cache_free(lockowner_slab, lo);
  2063. }
  2064. static void init_nfs4_replay(struct nfs4_replay *rp)
  2065. {
  2066. rp->rp_status = nfserr_serverfault;
  2067. rp->rp_buflen = 0;
  2068. rp->rp_buf = rp->rp_ibuf;
  2069. }
  2070. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2071. {
  2072. struct nfs4_stateowner *sop;
  2073. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2074. if (!sop)
  2075. return NULL;
  2076. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2077. if (!sop->so_owner.data) {
  2078. kmem_cache_free(slab, sop);
  2079. return NULL;
  2080. }
  2081. sop->so_owner.len = owner->len;
  2082. INIT_LIST_HEAD(&sop->so_stateids);
  2083. sop->so_client = clp;
  2084. init_nfs4_replay(&sop->so_replay);
  2085. return sop;
  2086. }
  2087. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  2088. {
  2089. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2090. list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  2091. list_add(&oo->oo_perclient, &clp->cl_openowners);
  2092. }
  2093. static struct nfs4_openowner *
  2094. alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
  2095. struct nfs4_openowner *oo;
  2096. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  2097. if (!oo)
  2098. return NULL;
  2099. oo->oo_owner.so_is_open_owner = 1;
  2100. oo->oo_owner.so_seqid = open->op_seqid;
  2101. oo->oo_flags = NFS4_OO_NEW;
  2102. oo->oo_time = 0;
  2103. oo->oo_last_closed_stid = NULL;
  2104. INIT_LIST_HEAD(&oo->oo_close_lru);
  2105. hash_openowner(oo, clp, strhashval);
  2106. return oo;
  2107. }
  2108. static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  2109. struct nfs4_openowner *oo = open->op_openowner;
  2110. stp->st_stid.sc_type = NFS4_OPEN_STID;
  2111. INIT_LIST_HEAD(&stp->st_lockowners);
  2112. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  2113. list_add(&stp->st_perfile, &fp->fi_stateids);
  2114. stp->st_stateowner = &oo->oo_owner;
  2115. get_nfs4_file(fp);
  2116. stp->st_file = fp;
  2117. stp->st_access_bmap = 0;
  2118. stp->st_deny_bmap = 0;
  2119. set_access(open->op_share_access, stp);
  2120. set_deny(open->op_share_deny, stp);
  2121. stp->st_openstp = NULL;
  2122. }
  2123. static void
  2124. move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
  2125. {
  2126. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2127. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  2128. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  2129. oo->oo_time = get_seconds();
  2130. }
  2131. static int
  2132. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
  2133. clientid_t *clid)
  2134. {
  2135. return (sop->so_owner.len == owner->len) &&
  2136. 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
  2137. (sop->so_client->cl_clientid.cl_id == clid->cl_id);
  2138. }
  2139. static struct nfs4_openowner *
  2140. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
  2141. bool sessions, struct nfsd_net *nn)
  2142. {
  2143. struct nfs4_stateowner *so;
  2144. struct nfs4_openowner *oo;
  2145. struct nfs4_client *clp;
  2146. list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  2147. if (!so->so_is_open_owner)
  2148. continue;
  2149. if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
  2150. oo = openowner(so);
  2151. clp = oo->oo_owner.so_client;
  2152. if ((bool)clp->cl_minorversion != sessions)
  2153. return NULL;
  2154. renew_client(oo->oo_owner.so_client);
  2155. return oo;
  2156. }
  2157. }
  2158. return NULL;
  2159. }
  2160. /* search file_hashtbl[] for file */
  2161. static struct nfs4_file *
  2162. find_file(struct inode *ino)
  2163. {
  2164. unsigned int hashval = file_hashval(ino);
  2165. struct nfs4_file *fp;
  2166. spin_lock(&recall_lock);
  2167. list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  2168. if (fp->fi_inode == ino) {
  2169. get_nfs4_file(fp);
  2170. spin_unlock(&recall_lock);
  2171. return fp;
  2172. }
  2173. }
  2174. spin_unlock(&recall_lock);
  2175. return NULL;
  2176. }
  2177. /*
  2178. * Called to check deny when READ with all zero stateid or
  2179. * WRITE with all zero or all one stateid
  2180. */
  2181. static __be32
  2182. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  2183. {
  2184. struct inode *ino = current_fh->fh_dentry->d_inode;
  2185. struct nfs4_file *fp;
  2186. struct nfs4_ol_stateid *stp;
  2187. __be32 ret;
  2188. fp = find_file(ino);
  2189. if (!fp)
  2190. return nfs_ok;
  2191. ret = nfserr_locked;
  2192. /* Search for conflicting share reservations */
  2193. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  2194. if (test_deny(deny_type, stp) ||
  2195. test_deny(NFS4_SHARE_DENY_BOTH, stp))
  2196. goto out;
  2197. }
  2198. ret = nfs_ok;
  2199. out:
  2200. put_nfs4_file(fp);
  2201. return ret;
  2202. }
  2203. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  2204. {
  2205. /* We're assuming the state code never drops its reference
  2206. * without first removing the lease. Since we're in this lease
  2207. * callback (and since the lease code is serialized by the kernel
  2208. * lock) we know the server hasn't removed the lease yet, we know
  2209. * it's safe to take a reference: */
  2210. atomic_inc(&dp->dl_count);
  2211. list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
  2212. /* only place dl_time is set. protected by lock_flocks*/
  2213. dp->dl_time = get_seconds();
  2214. nfsd4_cb_recall(dp);
  2215. }
  2216. /* Called from break_lease() with lock_flocks() held. */
  2217. static void nfsd_break_deleg_cb(struct file_lock *fl)
  2218. {
  2219. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  2220. struct nfs4_delegation *dp;
  2221. if (!fp) {
  2222. WARN(1, "(%p)->fl_owner NULL\n", fl);
  2223. return;
  2224. }
  2225. if (fp->fi_had_conflict) {
  2226. WARN(1, "duplicate break on %p\n", fp);
  2227. return;
  2228. }
  2229. /*
  2230. * We don't want the locks code to timeout the lease for us;
  2231. * we'll remove it ourself if a delegation isn't returned
  2232. * in time:
  2233. */
  2234. fl->fl_break_time = 0;
  2235. spin_lock(&recall_lock);
  2236. fp->fi_had_conflict = true;
  2237. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  2238. nfsd_break_one_deleg(dp);
  2239. spin_unlock(&recall_lock);
  2240. }
  2241. static
  2242. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  2243. {
  2244. if (arg & F_UNLCK)
  2245. return lease_modify(onlist, arg);
  2246. else
  2247. return -EAGAIN;
  2248. }
  2249. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  2250. .lm_break = nfsd_break_deleg_cb,
  2251. .lm_change = nfsd_change_deleg_cb,
  2252. };
  2253. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  2254. {
  2255. if (nfsd4_has_session(cstate))
  2256. return nfs_ok;
  2257. if (seqid == so->so_seqid - 1)
  2258. return nfserr_replay_me;
  2259. if (seqid == so->so_seqid)
  2260. return nfs_ok;
  2261. return nfserr_bad_seqid;
  2262. }
  2263. __be32
  2264. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  2265. struct nfsd4_open *open, struct nfsd_net *nn)
  2266. {
  2267. clientid_t *clientid = &open->op_clientid;
  2268. struct nfs4_client *clp = NULL;
  2269. unsigned int strhashval;
  2270. struct nfs4_openowner *oo = NULL;
  2271. __be32 status;
  2272. if (STALE_CLIENTID(&open->op_clientid, nn))
  2273. return nfserr_stale_clientid;
  2274. /*
  2275. * In case we need it later, after we've already created the
  2276. * file and don't want to risk a further failure:
  2277. */
  2278. open->op_file = nfsd4_alloc_file();
  2279. if (open->op_file == NULL)
  2280. return nfserr_jukebox;
  2281. strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
  2282. oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
  2283. open->op_openowner = oo;
  2284. if (!oo) {
  2285. clp = find_confirmed_client(clientid, cstate->minorversion,
  2286. nn);
  2287. if (clp == NULL)
  2288. return nfserr_expired;
  2289. goto new_owner;
  2290. }
  2291. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  2292. /* Replace unconfirmed owners without checking for replay. */
  2293. clp = oo->oo_owner.so_client;
  2294. release_openowner(oo);
  2295. open->op_openowner = NULL;
  2296. goto new_owner;
  2297. }
  2298. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  2299. if (status)
  2300. return status;
  2301. clp = oo->oo_owner.so_client;
  2302. goto alloc_stateid;
  2303. new_owner:
  2304. oo = alloc_init_open_stateowner(strhashval, clp, open);
  2305. if (oo == NULL)
  2306. return nfserr_jukebox;
  2307. open->op_openowner = oo;
  2308. alloc_stateid:
  2309. open->op_stp = nfs4_alloc_stateid(clp);
  2310. if (!open->op_stp)
  2311. return nfserr_jukebox;
  2312. return nfs_ok;
  2313. }
  2314. static inline __be32
  2315. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  2316. {
  2317. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  2318. return nfserr_openmode;
  2319. else
  2320. return nfs_ok;
  2321. }
  2322. static int share_access_to_flags(u32 share_access)
  2323. {
  2324. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  2325. }
  2326. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  2327. {
  2328. struct nfs4_stid *ret;
  2329. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  2330. if (!ret)
  2331. return NULL;
  2332. return delegstateid(ret);
  2333. }
  2334. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  2335. {
  2336. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  2337. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  2338. }
  2339. static __be32
  2340. nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
  2341. struct nfs4_delegation **dp)
  2342. {
  2343. int flags;
  2344. __be32 status = nfserr_bad_stateid;
  2345. *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
  2346. if (*dp == NULL)
  2347. goto out;
  2348. flags = share_access_to_flags(open->op_share_access);
  2349. status = nfs4_check_delegmode(*dp, flags);
  2350. if (status)
  2351. *dp = NULL;
  2352. out:
  2353. if (!nfsd4_is_deleg_cur(open))
  2354. return nfs_ok;
  2355. if (status)
  2356. return status;
  2357. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2358. return nfs_ok;
  2359. }
  2360. static __be32
  2361. nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
  2362. {
  2363. struct nfs4_ol_stateid *local;
  2364. struct nfs4_openowner *oo = open->op_openowner;
  2365. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  2366. /* ignore lock owners */
  2367. if (local->st_stateowner->so_is_open_owner == 0)
  2368. continue;
  2369. /* remember if we have seen this open owner */
  2370. if (local->st_stateowner == &oo->oo_owner)
  2371. *stpp = local;
  2372. /* check for conflicting share reservations */
  2373. if (!test_share(local, open))
  2374. return nfserr_share_denied;
  2375. }
  2376. return nfs_ok;
  2377. }
  2378. static inline int nfs4_access_to_access(u32 nfs4_access)
  2379. {
  2380. int flags = 0;
  2381. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  2382. flags |= NFSD_MAY_READ;
  2383. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  2384. flags |= NFSD_MAY_WRITE;
  2385. return flags;
  2386. }
  2387. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  2388. struct svc_fh *cur_fh, struct nfsd4_open *open)
  2389. {
  2390. __be32 status;
  2391. int oflag = nfs4_access_to_omode(open->op_share_access);
  2392. int access = nfs4_access_to_access(open->op_share_access);
  2393. if (!fp->fi_fds[oflag]) {
  2394. status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
  2395. &fp->fi_fds[oflag]);
  2396. if (status)
  2397. return status;
  2398. }
  2399. nfs4_file_get_access(fp, oflag);
  2400. return nfs_ok;
  2401. }
  2402. static inline __be32
  2403. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  2404. struct nfsd4_open *open)
  2405. {
  2406. struct iattr iattr = {
  2407. .ia_valid = ATTR_SIZE,
  2408. .ia_size = 0,
  2409. };
  2410. if (!open->op_truncate)
  2411. return 0;
  2412. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  2413. return nfserr_inval;
  2414. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  2415. }
  2416. static __be32
  2417. nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
  2418. {
  2419. u32 op_share_access = open->op_share_access;
  2420. bool new_access;
  2421. __be32 status;
  2422. new_access = !test_access(op_share_access, stp);
  2423. if (new_access) {
  2424. status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
  2425. if (status)
  2426. return status;
  2427. }
  2428. status = nfsd4_truncate(rqstp, cur_fh, open);
  2429. if (status) {
  2430. if (new_access) {
  2431. int oflag = nfs4_access_to_omode(op_share_access);
  2432. nfs4_file_put_access(fp, oflag);
  2433. }
  2434. return status;
  2435. }
  2436. /* remember the open */
  2437. set_access(op_share_access, stp);
  2438. set_deny(open->op_share_deny, stp);
  2439. return nfs_ok;
  2440. }
  2441. static void
  2442. nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
  2443. {
  2444. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2445. }
  2446. /* Should we give out recallable state?: */
  2447. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  2448. {
  2449. if (clp->cl_cb_state == NFSD4_CB_UP)
  2450. return true;
  2451. /*
  2452. * In the sessions case, since we don't have to establish a
  2453. * separate connection for callbacks, we assume it's OK
  2454. * until we hear otherwise:
  2455. */
  2456. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  2457. }
  2458. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
  2459. {
  2460. struct file_lock *fl;
  2461. fl = locks_alloc_lock();
  2462. if (!fl)
  2463. return NULL;
  2464. locks_init_lock(fl);
  2465. fl->fl_lmops = &nfsd_lease_mng_ops;
  2466. fl->fl_flags = FL_LEASE;
  2467. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  2468. fl->fl_end = OFFSET_MAX;
  2469. fl->fl_owner = (fl_owner_t)(dp->dl_file);
  2470. fl->fl_pid = current->tgid;
  2471. return fl;
  2472. }
  2473. static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
  2474. {
  2475. struct nfs4_file *fp = dp->dl_file;
  2476. struct file_lock *fl;
  2477. int status;
  2478. fl = nfs4_alloc_init_lease(dp, flag);
  2479. if (!fl)
  2480. return -ENOMEM;
  2481. fl->fl_file = find_readable_file(fp);
  2482. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2483. status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
  2484. if (status) {
  2485. list_del_init(&dp->dl_perclnt);
  2486. locks_free_lock(fl);
  2487. return -ENOMEM;
  2488. }
  2489. fp->fi_lease = fl;
  2490. fp->fi_deleg_file = get_file(fl->fl_file);
  2491. atomic_set(&fp->fi_delegees, 1);
  2492. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2493. return 0;
  2494. }
  2495. static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
  2496. {
  2497. struct nfs4_file *fp = dp->dl_file;
  2498. if (!fp->fi_lease)
  2499. return nfs4_setlease(dp, flag);
  2500. spin_lock(&recall_lock);
  2501. if (fp->fi_had_conflict) {
  2502. spin_unlock(&recall_lock);
  2503. return -EAGAIN;
  2504. }
  2505. atomic_inc(&fp->fi_delegees);
  2506. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2507. spin_unlock(&recall_lock);
  2508. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2509. return 0;
  2510. }
  2511. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  2512. {
  2513. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2514. if (status == -EAGAIN)
  2515. open->op_why_no_deleg = WND4_CONTENTION;
  2516. else {
  2517. open->op_why_no_deleg = WND4_RESOURCE;
  2518. switch (open->op_deleg_want) {
  2519. case NFS4_SHARE_WANT_READ_DELEG:
  2520. case NFS4_SHARE_WANT_WRITE_DELEG:
  2521. case NFS4_SHARE_WANT_ANY_DELEG:
  2522. break;
  2523. case NFS4_SHARE_WANT_CANCEL:
  2524. open->op_why_no_deleg = WND4_CANCELLED;
  2525. break;
  2526. case NFS4_SHARE_WANT_NO_DELEG:
  2527. WARN_ON_ONCE(1);
  2528. }
  2529. }
  2530. }
  2531. /*
  2532. * Attempt to hand out a delegation.
  2533. */
  2534. static void
  2535. nfs4_open_delegation(struct net *net, struct svc_fh *fh,
  2536. struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
  2537. {
  2538. struct nfs4_delegation *dp;
  2539. struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
  2540. int cb_up;
  2541. int status = 0, flag = 0;
  2542. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  2543. flag = NFS4_OPEN_DELEGATE_NONE;
  2544. open->op_recall = 0;
  2545. switch (open->op_claim_type) {
  2546. case NFS4_OPEN_CLAIM_PREVIOUS:
  2547. if (!cb_up)
  2548. open->op_recall = 1;
  2549. flag = open->op_delegate_type;
  2550. if (flag == NFS4_OPEN_DELEGATE_NONE)
  2551. goto out;
  2552. break;
  2553. case NFS4_OPEN_CLAIM_NULL:
  2554. /* Let's not give out any delegations till everyone's
  2555. * had the chance to reclaim theirs.... */
  2556. if (locks_in_grace(net))
  2557. goto out;
  2558. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  2559. goto out;
  2560. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  2561. flag = NFS4_OPEN_DELEGATE_WRITE;
  2562. else
  2563. flag = NFS4_OPEN_DELEGATE_READ;
  2564. break;
  2565. default:
  2566. goto out;
  2567. }
  2568. dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
  2569. if (dp == NULL)
  2570. goto out_no_deleg;
  2571. status = nfs4_set_delegation(dp, flag);
  2572. if (status)
  2573. goto out_free;
  2574. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  2575. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  2576. STATEID_VAL(&dp->dl_stid.sc_stateid));
  2577. out:
  2578. open->op_delegate_type = flag;
  2579. if (flag == NFS4_OPEN_DELEGATE_NONE) {
  2580. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  2581. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
  2582. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  2583. /* 4.1 client asking for a delegation? */
  2584. if (open->op_deleg_want)
  2585. nfsd4_open_deleg_none_ext(open, status);
  2586. }
  2587. return;
  2588. out_free:
  2589. unhash_stid(&dp->dl_stid);
  2590. nfs4_put_delegation(dp);
  2591. out_no_deleg:
  2592. flag = NFS4_OPEN_DELEGATE_NONE;
  2593. goto out;
  2594. }
  2595. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  2596. struct nfs4_delegation *dp)
  2597. {
  2598. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  2599. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2600. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2601. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  2602. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  2603. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2604. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2605. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  2606. }
  2607. /* Otherwise the client must be confused wanting a delegation
  2608. * it already has, therefore we don't return
  2609. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  2610. */
  2611. }
  2612. /*
  2613. * called with nfs4_lock_state() held.
  2614. */
  2615. __be32
  2616. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  2617. {
  2618. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2619. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  2620. struct nfs4_file *fp = NULL;
  2621. struct inode *ino = current_fh->fh_dentry->d_inode;
  2622. struct nfs4_ol_stateid *stp = NULL;
  2623. struct nfs4_delegation *dp = NULL;
  2624. __be32 status;
  2625. /*
  2626. * Lookup file; if found, lookup stateid and check open request,
  2627. * and check for delegations in the process of being recalled.
  2628. * If not found, create the nfs4_file struct
  2629. */
  2630. fp = find_file(ino);
  2631. if (fp) {
  2632. if ((status = nfs4_check_open(fp, open, &stp)))
  2633. goto out;
  2634. status = nfs4_check_deleg(cl, fp, open, &dp);
  2635. if (status)
  2636. goto out;
  2637. } else {
  2638. status = nfserr_bad_stateid;
  2639. if (nfsd4_is_deleg_cur(open))
  2640. goto out;
  2641. status = nfserr_jukebox;
  2642. fp = open->op_file;
  2643. open->op_file = NULL;
  2644. nfsd4_init_file(fp, ino);
  2645. }
  2646. /*
  2647. * OPEN the file, or upgrade an existing OPEN.
  2648. * If truncate fails, the OPEN fails.
  2649. */
  2650. if (stp) {
  2651. /* Stateid was found, this is an OPEN upgrade */
  2652. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  2653. if (status)
  2654. goto out;
  2655. } else {
  2656. status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
  2657. if (status)
  2658. goto out;
  2659. status = nfsd4_truncate(rqstp, current_fh, open);
  2660. if (status)
  2661. goto out;
  2662. stp = open->op_stp;
  2663. open->op_stp = NULL;
  2664. init_open_stateid(stp, fp, open);
  2665. }
  2666. update_stateid(&stp->st_stid.sc_stateid);
  2667. memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  2668. if (nfsd4_has_session(&resp->cstate)) {
  2669. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2670. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  2671. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2672. open->op_why_no_deleg = WND4_NOT_WANTED;
  2673. goto nodeleg;
  2674. }
  2675. }
  2676. /*
  2677. * Attempt to hand out a delegation. No error return, because the
  2678. * OPEN succeeds even if we fail.
  2679. */
  2680. nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
  2681. nodeleg:
  2682. status = nfs_ok;
  2683. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  2684. STATEID_VAL(&stp->st_stid.sc_stateid));
  2685. out:
  2686. /* 4.1 client trying to upgrade/downgrade delegation? */
  2687. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  2688. open->op_deleg_want)
  2689. nfsd4_deleg_xgrade_none_ext(open, dp);
  2690. if (fp)
  2691. put_nfs4_file(fp);
  2692. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  2693. nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
  2694. /*
  2695. * To finish the open response, we just need to set the rflags.
  2696. */
  2697. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  2698. if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
  2699. !nfsd4_has_session(&resp->cstate))
  2700. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  2701. return status;
  2702. }
  2703. void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
  2704. {
  2705. if (open->op_openowner) {
  2706. struct nfs4_openowner *oo = open->op_openowner;
  2707. if (!list_empty(&oo->oo_owner.so_stateids))
  2708. list_del_init(&oo->oo_close_lru);
  2709. if (oo->oo_flags & NFS4_OO_NEW) {
  2710. if (status) {
  2711. release_openowner(oo);
  2712. open->op_openowner = NULL;
  2713. } else
  2714. oo->oo_flags &= ~NFS4_OO_NEW;
  2715. }
  2716. }
  2717. if (open->op_file)
  2718. nfsd4_free_file(open->op_file);
  2719. if (open->op_stp)
  2720. free_generic_stateid(open->op_stp);
  2721. }
  2722. static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
  2723. {
  2724. struct nfs4_client *found;
  2725. if (STALE_CLIENTID(clid, nn))
  2726. return nfserr_stale_clientid;
  2727. found = find_confirmed_client(clid, session, nn);
  2728. if (clp)
  2729. *clp = found;
  2730. return found ? nfs_ok : nfserr_expired;
  2731. }
  2732. __be32
  2733. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2734. clientid_t *clid)
  2735. {
  2736. struct nfs4_client *clp;
  2737. __be32 status;
  2738. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2739. nfs4_lock_state();
  2740. dprintk("process_renew(%08x/%08x): starting\n",
  2741. clid->cl_boot, clid->cl_id);
  2742. status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
  2743. if (status)
  2744. goto out;
  2745. status = nfserr_cb_path_down;
  2746. if (!list_empty(&clp->cl_delegations)
  2747. && clp->cl_cb_state != NFSD4_CB_UP)
  2748. goto out;
  2749. status = nfs_ok;
  2750. out:
  2751. nfs4_unlock_state();
  2752. return status;
  2753. }
  2754. static void
  2755. nfsd4_end_grace(struct nfsd_net *nn)
  2756. {
  2757. /* do nothing if grace period already ended */
  2758. if (nn->grace_ended)
  2759. return;
  2760. dprintk("NFSD: end of grace period\n");
  2761. nn->grace_ended = true;
  2762. nfsd4_record_grace_done(nn, nn->boot_time);
  2763. locks_end_grace(&nn->nfsd4_manager);
  2764. /*
  2765. * Now that every NFSv4 client has had the chance to recover and
  2766. * to see the (possibly new, possibly shorter) lease time, we
  2767. * can safely set the next grace time to the current lease time:
  2768. */
  2769. nn->nfsd4_grace = nn->nfsd4_lease;
  2770. }
  2771. static time_t
  2772. nfs4_laundromat(struct nfsd_net *nn)
  2773. {
  2774. struct nfs4_client *clp;
  2775. struct nfs4_openowner *oo;
  2776. struct nfs4_delegation *dp;
  2777. struct list_head *pos, *next, reaplist;
  2778. time_t cutoff = get_seconds() - nn->nfsd4_lease;
  2779. time_t t, clientid_val = nn->nfsd4_lease;
  2780. time_t u, test_val = nn->nfsd4_lease;
  2781. nfs4_lock_state();
  2782. dprintk("NFSD: laundromat service - starting\n");
  2783. nfsd4_end_grace(nn);
  2784. INIT_LIST_HEAD(&reaplist);
  2785. spin_lock(&nn->client_lock);
  2786. list_for_each_safe(pos, next, &nn->client_lru) {
  2787. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2788. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  2789. t = clp->cl_time - cutoff;
  2790. if (clientid_val > t)
  2791. clientid_val = t;
  2792. break;
  2793. }
  2794. if (atomic_read(&clp->cl_refcount)) {
  2795. dprintk("NFSD: client in use (clientid %08x)\n",
  2796. clp->cl_clientid.cl_id);
  2797. continue;
  2798. }
  2799. unhash_client_locked(clp);
  2800. list_add(&clp->cl_lru, &reaplist);
  2801. }
  2802. spin_unlock(&nn->client_lock);
  2803. list_for_each_safe(pos, next, &reaplist) {
  2804. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2805. dprintk("NFSD: purging unused client (clientid %08x)\n",
  2806. clp->cl_clientid.cl_id);
  2807. expire_client(clp);
  2808. }
  2809. spin_lock(&recall_lock);
  2810. list_for_each_safe(pos, next, &del_recall_lru) {
  2811. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2812. if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
  2813. continue;
  2814. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  2815. u = dp->dl_time - cutoff;
  2816. if (test_val > u)
  2817. test_val = u;
  2818. break;
  2819. }
  2820. list_move(&dp->dl_recall_lru, &reaplist);
  2821. }
  2822. spin_unlock(&recall_lock);
  2823. list_for_each_safe(pos, next, &reaplist) {
  2824. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2825. unhash_delegation(dp);
  2826. }
  2827. test_val = nn->nfsd4_lease;
  2828. list_for_each_safe(pos, next, &nn->close_lru) {
  2829. oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
  2830. if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
  2831. u = oo->oo_time - cutoff;
  2832. if (test_val > u)
  2833. test_val = u;
  2834. break;
  2835. }
  2836. release_openowner(oo);
  2837. }
  2838. if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
  2839. clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
  2840. nfs4_unlock_state();
  2841. return clientid_val;
  2842. }
  2843. static struct workqueue_struct *laundry_wq;
  2844. static void laundromat_main(struct work_struct *);
  2845. static void
  2846. laundromat_main(struct work_struct *laundry)
  2847. {
  2848. time_t t;
  2849. struct delayed_work *dwork = container_of(laundry, struct delayed_work,
  2850. work);
  2851. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  2852. laundromat_work);
  2853. t = nfs4_laundromat(nn);
  2854. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  2855. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  2856. }
  2857. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
  2858. {
  2859. if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
  2860. return nfserr_bad_stateid;
  2861. return nfs_ok;
  2862. }
  2863. static inline int
  2864. access_permit_read(struct nfs4_ol_stateid *stp)
  2865. {
  2866. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  2867. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  2868. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  2869. }
  2870. static inline int
  2871. access_permit_write(struct nfs4_ol_stateid *stp)
  2872. {
  2873. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  2874. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  2875. }
  2876. static
  2877. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  2878. {
  2879. __be32 status = nfserr_openmode;
  2880. /* For lock stateid's, we test the parent open, not the lock: */
  2881. if (stp->st_openstp)
  2882. stp = stp->st_openstp;
  2883. if ((flags & WR_STATE) && !access_permit_write(stp))
  2884. goto out;
  2885. if ((flags & RD_STATE) && !access_permit_read(stp))
  2886. goto out;
  2887. status = nfs_ok;
  2888. out:
  2889. return status;
  2890. }
  2891. static inline __be32
  2892. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  2893. {
  2894. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  2895. return nfs_ok;
  2896. else if (locks_in_grace(net)) {
  2897. /* Answer in remaining cases depends on existence of
  2898. * conflicting state; so we must wait out the grace period. */
  2899. return nfserr_grace;
  2900. } else if (flags & WR_STATE)
  2901. return nfs4_share_conflict(current_fh,
  2902. NFS4_SHARE_DENY_WRITE);
  2903. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  2904. return nfs4_share_conflict(current_fh,
  2905. NFS4_SHARE_DENY_READ);
  2906. }
  2907. /*
  2908. * Allow READ/WRITE during grace period on recovered state only for files
  2909. * that are not able to provide mandatory locking.
  2910. */
  2911. static inline int
  2912. grace_disallows_io(struct net *net, struct inode *inode)
  2913. {
  2914. return locks_in_grace(net) && mandatory_lock(inode);
  2915. }
  2916. /* Returns true iff a is later than b: */
  2917. static bool stateid_generation_after(stateid_t *a, stateid_t *b)
  2918. {
  2919. return (s32)a->si_generation - (s32)b->si_generation > 0;
  2920. }
  2921. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  2922. {
  2923. /*
  2924. * When sessions are used the stateid generation number is ignored
  2925. * when it is zero.
  2926. */
  2927. if (has_session && in->si_generation == 0)
  2928. return nfs_ok;
  2929. if (in->si_generation == ref->si_generation)
  2930. return nfs_ok;
  2931. /* If the client sends us a stateid from the future, it's buggy: */
  2932. if (stateid_generation_after(in, ref))
  2933. return nfserr_bad_stateid;
  2934. /*
  2935. * However, we could see a stateid from the past, even from a
  2936. * non-buggy client. For example, if the client sends a lock
  2937. * while some IO is outstanding, the lock may bump si_generation
  2938. * while the IO is still in flight. The client could avoid that
  2939. * situation by waiting for responses on all the IO requests,
  2940. * but better performance may result in retrying IO that
  2941. * receives an old_stateid error if requests are rarely
  2942. * reordered in flight:
  2943. */
  2944. return nfserr_old_stateid;
  2945. }
  2946. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  2947. {
  2948. struct nfs4_stid *s;
  2949. struct nfs4_ol_stateid *ols;
  2950. __be32 status;
  2951. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  2952. return nfserr_bad_stateid;
  2953. /* Client debugging aid. */
  2954. if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
  2955. char addr_str[INET6_ADDRSTRLEN];
  2956. rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
  2957. sizeof(addr_str));
  2958. pr_warn_ratelimited("NFSD: client %s testing state ID "
  2959. "with incorrect client ID\n", addr_str);
  2960. return nfserr_bad_stateid;
  2961. }
  2962. s = find_stateid(cl, stateid);
  2963. if (!s)
  2964. return nfserr_bad_stateid;
  2965. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  2966. if (status)
  2967. return status;
  2968. if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
  2969. return nfs_ok;
  2970. ols = openlockstateid(s);
  2971. if (ols->st_stateowner->so_is_open_owner
  2972. && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  2973. return nfserr_bad_stateid;
  2974. return nfs_ok;
  2975. }
  2976. static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
  2977. struct nfs4_stid **s, bool sessions,
  2978. struct nfsd_net *nn)
  2979. {
  2980. struct nfs4_client *cl;
  2981. __be32 status;
  2982. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  2983. return nfserr_bad_stateid;
  2984. status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
  2985. nn, &cl);
  2986. if (status == nfserr_stale_clientid)
  2987. return nfserr_stale_stateid;
  2988. if (status)
  2989. return status;
  2990. *s = find_stateid_by_type(cl, stateid, typemask);
  2991. if (!*s)
  2992. return nfserr_bad_stateid;
  2993. return nfs_ok;
  2994. }
  2995. /*
  2996. * Checks for stateid operations
  2997. */
  2998. __be32
  2999. nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
  3000. stateid_t *stateid, int flags, struct file **filpp)
  3001. {
  3002. struct nfs4_stid *s;
  3003. struct nfs4_ol_stateid *stp = NULL;
  3004. struct nfs4_delegation *dp = NULL;
  3005. struct svc_fh *current_fh = &cstate->current_fh;
  3006. struct inode *ino = current_fh->fh_dentry->d_inode;
  3007. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3008. __be32 status;
  3009. if (filpp)
  3010. *filpp = NULL;
  3011. if (grace_disallows_io(net, ino))
  3012. return nfserr_grace;
  3013. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3014. return check_special_stateids(net, current_fh, stateid, flags);
  3015. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
  3016. &s, cstate->minorversion, nn);
  3017. if (status)
  3018. return status;
  3019. status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
  3020. if (status)
  3021. goto out;
  3022. switch (s->sc_type) {
  3023. case NFS4_DELEG_STID:
  3024. dp = delegstateid(s);
  3025. status = nfs4_check_delegmode(dp, flags);
  3026. if (status)
  3027. goto out;
  3028. if (filpp) {
  3029. *filpp = dp->dl_file->fi_deleg_file;
  3030. if (!*filpp) {
  3031. WARN_ON_ONCE(1);
  3032. status = nfserr_serverfault;
  3033. goto out;
  3034. }
  3035. }
  3036. break;
  3037. case NFS4_OPEN_STID:
  3038. case NFS4_LOCK_STID:
  3039. stp = openlockstateid(s);
  3040. status = nfs4_check_fh(current_fh, stp);
  3041. if (status)
  3042. goto out;
  3043. if (stp->st_stateowner->so_is_open_owner
  3044. && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3045. goto out;
  3046. status = nfs4_check_openmode(stp, flags);
  3047. if (status)
  3048. goto out;
  3049. if (filpp) {
  3050. if (flags & RD_STATE)
  3051. *filpp = find_readable_file(stp->st_file);
  3052. else
  3053. *filpp = find_writeable_file(stp->st_file);
  3054. }
  3055. break;
  3056. default:
  3057. return nfserr_bad_stateid;
  3058. }
  3059. status = nfs_ok;
  3060. out:
  3061. return status;
  3062. }
  3063. static __be32
  3064. nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
  3065. {
  3066. if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
  3067. return nfserr_locks_held;
  3068. release_lock_stateid(stp);
  3069. return nfs_ok;
  3070. }
  3071. /*
  3072. * Test if the stateid is valid
  3073. */
  3074. __be32
  3075. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3076. struct nfsd4_test_stateid *test_stateid)
  3077. {
  3078. struct nfsd4_test_stateid_id *stateid;
  3079. struct nfs4_client *cl = cstate->session->se_client;
  3080. nfs4_lock_state();
  3081. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  3082. stateid->ts_id_status =
  3083. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  3084. nfs4_unlock_state();
  3085. return nfs_ok;
  3086. }
  3087. __be32
  3088. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3089. struct nfsd4_free_stateid *free_stateid)
  3090. {
  3091. stateid_t *stateid = &free_stateid->fr_stateid;
  3092. struct nfs4_stid *s;
  3093. struct nfs4_client *cl = cstate->session->se_client;
  3094. __be32 ret = nfserr_bad_stateid;
  3095. nfs4_lock_state();
  3096. s = find_stateid(cl, stateid);
  3097. if (!s)
  3098. goto out;
  3099. switch (s->sc_type) {
  3100. case NFS4_DELEG_STID:
  3101. ret = nfserr_locks_held;
  3102. goto out;
  3103. case NFS4_OPEN_STID:
  3104. case NFS4_LOCK_STID:
  3105. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3106. if (ret)
  3107. goto out;
  3108. if (s->sc_type == NFS4_LOCK_STID)
  3109. ret = nfsd4_free_lock_stateid(openlockstateid(s));
  3110. else
  3111. ret = nfserr_locks_held;
  3112. break;
  3113. default:
  3114. ret = nfserr_bad_stateid;
  3115. }
  3116. out:
  3117. nfs4_unlock_state();
  3118. return ret;
  3119. }
  3120. static inline int
  3121. setlkflg (int type)
  3122. {
  3123. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  3124. RD_STATE : WR_STATE;
  3125. }
  3126. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  3127. {
  3128. struct svc_fh *current_fh = &cstate->current_fh;
  3129. struct nfs4_stateowner *sop = stp->st_stateowner;
  3130. __be32 status;
  3131. status = nfsd4_check_seqid(cstate, sop, seqid);
  3132. if (status)
  3133. return status;
  3134. if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
  3135. /*
  3136. * "Closed" stateid's exist *only* to return
  3137. * nfserr_replay_me from the previous step.
  3138. */
  3139. return nfserr_bad_stateid;
  3140. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  3141. if (status)
  3142. return status;
  3143. return nfs4_check_fh(current_fh, stp);
  3144. }
  3145. /*
  3146. * Checks for sequence id mutating operations.
  3147. */
  3148. static __be32
  3149. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3150. stateid_t *stateid, char typemask,
  3151. struct nfs4_ol_stateid **stpp,
  3152. struct nfsd_net *nn)
  3153. {
  3154. __be32 status;
  3155. struct nfs4_stid *s;
  3156. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  3157. seqid, STATEID_VAL(stateid));
  3158. *stpp = NULL;
  3159. status = nfsd4_lookup_stateid(stateid, typemask, &s,
  3160. cstate->minorversion, nn);
  3161. if (status)
  3162. return status;
  3163. *stpp = openlockstateid(s);
  3164. cstate->replay_owner = (*stpp)->st_stateowner;
  3165. return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
  3166. }
  3167. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3168. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  3169. {
  3170. __be32 status;
  3171. struct nfs4_openowner *oo;
  3172. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  3173. NFS4_OPEN_STID, stpp, nn);
  3174. if (status)
  3175. return status;
  3176. oo = openowner((*stpp)->st_stateowner);
  3177. if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
  3178. return nfserr_bad_stateid;
  3179. return nfs_ok;
  3180. }
  3181. __be32
  3182. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3183. struct nfsd4_open_confirm *oc)
  3184. {
  3185. __be32 status;
  3186. struct nfs4_openowner *oo;
  3187. struct nfs4_ol_stateid *stp;
  3188. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3189. dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
  3190. (int)cstate->current_fh.fh_dentry->d_name.len,
  3191. cstate->current_fh.fh_dentry->d_name.name);
  3192. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  3193. if (status)
  3194. return status;
  3195. nfs4_lock_state();
  3196. status = nfs4_preprocess_seqid_op(cstate,
  3197. oc->oc_seqid, &oc->oc_req_stateid,
  3198. NFS4_OPEN_STID, &stp, nn);
  3199. if (status)
  3200. goto out;
  3201. oo = openowner(stp->st_stateowner);
  3202. status = nfserr_bad_stateid;
  3203. if (oo->oo_flags & NFS4_OO_CONFIRMED)
  3204. goto out;
  3205. oo->oo_flags |= NFS4_OO_CONFIRMED;
  3206. update_stateid(&stp->st_stid.sc_stateid);
  3207. memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3208. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  3209. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  3210. nfsd4_client_record_create(oo->oo_owner.so_client);
  3211. status = nfs_ok;
  3212. out:
  3213. if (!cstate->replay_owner)
  3214. nfs4_unlock_state();
  3215. return status;
  3216. }
  3217. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  3218. {
  3219. if (!test_access(access, stp))
  3220. return;
  3221. nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
  3222. clear_access(access, stp);
  3223. }
  3224. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  3225. {
  3226. switch (to_access) {
  3227. case NFS4_SHARE_ACCESS_READ:
  3228. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  3229. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3230. break;
  3231. case NFS4_SHARE_ACCESS_WRITE:
  3232. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  3233. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3234. break;
  3235. case NFS4_SHARE_ACCESS_BOTH:
  3236. break;
  3237. default:
  3238. WARN_ON_ONCE(1);
  3239. }
  3240. }
  3241. static void
  3242. reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
  3243. {
  3244. int i;
  3245. for (i = 0; i < 4; i++) {
  3246. if ((i & deny) != i)
  3247. clear_deny(i, stp);
  3248. }
  3249. }
  3250. __be32
  3251. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  3252. struct nfsd4_compound_state *cstate,
  3253. struct nfsd4_open_downgrade *od)
  3254. {
  3255. __be32 status;
  3256. struct nfs4_ol_stateid *stp;
  3257. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3258. dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
  3259. (int)cstate->current_fh.fh_dentry->d_name.len,
  3260. cstate->current_fh.fh_dentry->d_name.name);
  3261. /* We don't yet support WANT bits: */
  3262. if (od->od_deleg_want)
  3263. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  3264. od->od_deleg_want);
  3265. nfs4_lock_state();
  3266. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  3267. &od->od_stateid, &stp, nn);
  3268. if (status)
  3269. goto out;
  3270. status = nfserr_inval;
  3271. if (!test_access(od->od_share_access, stp)) {
  3272. dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
  3273. stp->st_access_bmap, od->od_share_access);
  3274. goto out;
  3275. }
  3276. if (!test_deny(od->od_share_deny, stp)) {
  3277. dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
  3278. stp->st_deny_bmap, od->od_share_deny);
  3279. goto out;
  3280. }
  3281. nfs4_stateid_downgrade(stp, od->od_share_access);
  3282. reset_union_bmap_deny(od->od_share_deny, stp);
  3283. update_stateid(&stp->st_stid.sc_stateid);
  3284. memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3285. status = nfs_ok;
  3286. out:
  3287. if (!cstate->replay_owner)
  3288. nfs4_unlock_state();
  3289. return status;
  3290. }
  3291. void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
  3292. {
  3293. struct nfs4_openowner *oo;
  3294. struct nfs4_ol_stateid *s;
  3295. if (!so->so_is_open_owner)
  3296. return;
  3297. oo = openowner(so);
  3298. s = oo->oo_last_closed_stid;
  3299. if (!s)
  3300. return;
  3301. if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
  3302. /* Release the last_closed_stid on the next seqid bump: */
  3303. oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
  3304. return;
  3305. }
  3306. oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
  3307. release_last_closed_stateid(oo);
  3308. }
  3309. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  3310. {
  3311. unhash_open_stateid(s);
  3312. s->st_stid.sc_type = NFS4_CLOSED_STID;
  3313. }
  3314. /*
  3315. * nfs4_unlock_state() called after encode
  3316. */
  3317. __be32
  3318. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3319. struct nfsd4_close *close)
  3320. {
  3321. __be32 status;
  3322. struct nfs4_openowner *oo;
  3323. struct nfs4_ol_stateid *stp;
  3324. struct net *net = SVC_NET(rqstp);
  3325. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3326. dprintk("NFSD: nfsd4_close on file %.*s\n",
  3327. (int)cstate->current_fh.fh_dentry->d_name.len,
  3328. cstate->current_fh.fh_dentry->d_name.name);
  3329. nfs4_lock_state();
  3330. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  3331. &close->cl_stateid,
  3332. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  3333. &stp, nn);
  3334. if (status)
  3335. goto out;
  3336. oo = openowner(stp->st_stateowner);
  3337. status = nfs_ok;
  3338. update_stateid(&stp->st_stid.sc_stateid);
  3339. memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3340. nfsd4_close_open_stateid(stp);
  3341. release_last_closed_stateid(oo);
  3342. oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
  3343. oo->oo_last_closed_stid = stp;
  3344. if (list_empty(&oo->oo_owner.so_stateids)) {
  3345. if (cstate->minorversion) {
  3346. release_openowner(oo);
  3347. cstate->replay_owner = NULL;
  3348. } else {
  3349. /*
  3350. * In the 4.0 case we need to keep the owners around a
  3351. * little while to handle CLOSE replay.
  3352. */
  3353. if (list_empty(&oo->oo_owner.so_stateids))
  3354. move_to_close_lru(oo, SVC_NET(rqstp));
  3355. }
  3356. }
  3357. out:
  3358. if (!cstate->replay_owner)
  3359. nfs4_unlock_state();
  3360. return status;
  3361. }
  3362. __be32
  3363. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3364. struct nfsd4_delegreturn *dr)
  3365. {
  3366. struct nfs4_delegation *dp;
  3367. stateid_t *stateid = &dr->dr_stateid;
  3368. struct nfs4_stid *s;
  3369. __be32 status;
  3370. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3371. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3372. return status;
  3373. nfs4_lock_state();
  3374. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
  3375. cstate->minorversion, nn);
  3376. if (status)
  3377. goto out;
  3378. dp = delegstateid(s);
  3379. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  3380. if (status)
  3381. goto out;
  3382. unhash_delegation(dp);
  3383. out:
  3384. nfs4_unlock_state();
  3385. return status;
  3386. }
  3387. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  3388. #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
  3389. static inline u64
  3390. end_offset(u64 start, u64 len)
  3391. {
  3392. u64 end;
  3393. end = start + len;
  3394. return end >= start ? end: NFS4_MAX_UINT64;
  3395. }
  3396. /* last octet in a range */
  3397. static inline u64
  3398. last_byte_offset(u64 start, u64 len)
  3399. {
  3400. u64 end;
  3401. WARN_ON_ONCE(!len);
  3402. end = start + len;
  3403. return end > start ? end - 1: NFS4_MAX_UINT64;
  3404. }
  3405. static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
  3406. {
  3407. return (file_hashval(inode) + cl_id
  3408. + opaque_hashval(ownername->data, ownername->len))
  3409. & LOCKOWNER_INO_HASH_MASK;
  3410. }
  3411. /*
  3412. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  3413. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  3414. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  3415. * locking, this prevents us from being completely protocol-compliant. The
  3416. * real solution to this problem is to start using unsigned file offsets in
  3417. * the VFS, but this is a very deep change!
  3418. */
  3419. static inline void
  3420. nfs4_transform_lock_offset(struct file_lock *lock)
  3421. {
  3422. if (lock->fl_start < 0)
  3423. lock->fl_start = OFFSET_MAX;
  3424. if (lock->fl_end < 0)
  3425. lock->fl_end = OFFSET_MAX;
  3426. }
  3427. /* Hack!: For now, we're defining this just so we can use a pointer to it
  3428. * as a unique cookie to identify our (NFSv4's) posix locks. */
  3429. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  3430. };
  3431. static inline void
  3432. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  3433. {
  3434. struct nfs4_lockowner *lo;
  3435. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  3436. lo = (struct nfs4_lockowner *) fl->fl_owner;
  3437. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  3438. lo->lo_owner.so_owner.len, GFP_KERNEL);
  3439. if (!deny->ld_owner.data)
  3440. /* We just don't care that much */
  3441. goto nevermind;
  3442. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  3443. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  3444. } else {
  3445. nevermind:
  3446. deny->ld_owner.len = 0;
  3447. deny->ld_owner.data = NULL;
  3448. deny->ld_clientid.cl_boot = 0;
  3449. deny->ld_clientid.cl_id = 0;
  3450. }
  3451. deny->ld_start = fl->fl_start;
  3452. deny->ld_length = NFS4_MAX_UINT64;
  3453. if (fl->fl_end != NFS4_MAX_UINT64)
  3454. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  3455. deny->ld_type = NFS4_READ_LT;
  3456. if (fl->fl_type != F_RDLCK)
  3457. deny->ld_type = NFS4_WRITE_LT;
  3458. }
  3459. static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
  3460. {
  3461. struct nfs4_ol_stateid *lst;
  3462. if (!same_owner_str(&lo->lo_owner, owner, clid))
  3463. return false;
  3464. lst = list_first_entry(&lo->lo_owner.so_stateids,
  3465. struct nfs4_ol_stateid, st_perstateowner);
  3466. return lst->st_file->fi_inode == inode;
  3467. }
  3468. static struct nfs4_lockowner *
  3469. find_lockowner_str(struct inode *inode, clientid_t *clid,
  3470. struct xdr_netobj *owner, struct nfsd_net *nn)
  3471. {
  3472. unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
  3473. struct nfs4_lockowner *lo;
  3474. list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
  3475. if (same_lockowner_ino(lo, inode, clid, owner))
  3476. return lo;
  3477. }
  3478. return NULL;
  3479. }
  3480. static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
  3481. {
  3482. struct inode *inode = open_stp->st_file->fi_inode;
  3483. unsigned int inohash = lockowner_ino_hashval(inode,
  3484. clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
  3485. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  3486. list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  3487. list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
  3488. list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
  3489. }
  3490. /*
  3491. * Alloc a lock owner structure.
  3492. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  3493. * occurred.
  3494. *
  3495. * strhashval = ownerstr_hashval
  3496. */
  3497. static struct nfs4_lockowner *
  3498. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
  3499. struct nfs4_lockowner *lo;
  3500. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  3501. if (!lo)
  3502. return NULL;
  3503. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  3504. lo->lo_owner.so_is_open_owner = 0;
  3505. /* It is the openowner seqid that will be incremented in encode in the
  3506. * case of new lockowners; so increment the lock seqid manually: */
  3507. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
  3508. hash_lockowner(lo, strhashval, clp, open_stp);
  3509. return lo;
  3510. }
  3511. static struct nfs4_ol_stateid *
  3512. alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
  3513. {
  3514. struct nfs4_ol_stateid *stp;
  3515. struct nfs4_client *clp = lo->lo_owner.so_client;
  3516. stp = nfs4_alloc_stateid(clp);
  3517. if (stp == NULL)
  3518. return NULL;
  3519. stp->st_stid.sc_type = NFS4_LOCK_STID;
  3520. list_add(&stp->st_perfile, &fp->fi_stateids);
  3521. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  3522. stp->st_stateowner = &lo->lo_owner;
  3523. get_nfs4_file(fp);
  3524. stp->st_file = fp;
  3525. stp->st_access_bmap = 0;
  3526. stp->st_deny_bmap = open_stp->st_deny_bmap;
  3527. stp->st_openstp = open_stp;
  3528. return stp;
  3529. }
  3530. static int
  3531. check_lock_length(u64 offset, u64 length)
  3532. {
  3533. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  3534. LOFF_OVERFLOW(offset, length)));
  3535. }
  3536. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  3537. {
  3538. struct nfs4_file *fp = lock_stp->st_file;
  3539. int oflag = nfs4_access_to_omode(access);
  3540. if (test_access(access, lock_stp))
  3541. return;
  3542. nfs4_file_get_access(fp, oflag);
  3543. set_access(access, lock_stp);
  3544. }
  3545. static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
  3546. {
  3547. struct nfs4_file *fi = ost->st_file;
  3548. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  3549. struct nfs4_client *cl = oo->oo_owner.so_client;
  3550. struct nfs4_lockowner *lo;
  3551. unsigned int strhashval;
  3552. struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
  3553. lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
  3554. &lock->v.new.owner, nn);
  3555. if (lo) {
  3556. if (!cstate->minorversion)
  3557. return nfserr_bad_seqid;
  3558. /* XXX: a lockowner always has exactly one stateid: */
  3559. *lst = list_first_entry(&lo->lo_owner.so_stateids,
  3560. struct nfs4_ol_stateid, st_perstateowner);
  3561. return nfs_ok;
  3562. }
  3563. strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
  3564. &lock->v.new.owner);
  3565. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  3566. if (lo == NULL)
  3567. return nfserr_jukebox;
  3568. *lst = alloc_init_lock_stateid(lo, fi, ost);
  3569. if (*lst == NULL) {
  3570. release_lockowner(lo);
  3571. return nfserr_jukebox;
  3572. }
  3573. *new = true;
  3574. return nfs_ok;
  3575. }
  3576. /*
  3577. * LOCK operation
  3578. */
  3579. __be32
  3580. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3581. struct nfsd4_lock *lock)
  3582. {
  3583. struct nfs4_openowner *open_sop = NULL;
  3584. struct nfs4_lockowner *lock_sop = NULL;
  3585. struct nfs4_ol_stateid *lock_stp;
  3586. struct file *filp = NULL;
  3587. struct file_lock *file_lock = NULL;
  3588. struct file_lock *conflock = NULL;
  3589. __be32 status = 0;
  3590. bool new_state = false;
  3591. int lkflg;
  3592. int err;
  3593. struct net *net = SVC_NET(rqstp);
  3594. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3595. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  3596. (long long) lock->lk_offset,
  3597. (long long) lock->lk_length);
  3598. if (check_lock_length(lock->lk_offset, lock->lk_length))
  3599. return nfserr_inval;
  3600. if ((status = fh_verify(rqstp, &cstate->current_fh,
  3601. S_IFREG, NFSD_MAY_LOCK))) {
  3602. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  3603. return status;
  3604. }
  3605. nfs4_lock_state();
  3606. if (lock->lk_is_new) {
  3607. struct nfs4_ol_stateid *open_stp = NULL;
  3608. if (nfsd4_has_session(cstate))
  3609. /* See rfc 5661 18.10.3: given clientid is ignored: */
  3610. memcpy(&lock->v.new.clientid,
  3611. &cstate->session->se_client->cl_clientid,
  3612. sizeof(clientid_t));
  3613. status = nfserr_stale_clientid;
  3614. if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
  3615. goto out;
  3616. /* validate and update open stateid and open seqid */
  3617. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  3618. lock->lk_new_open_seqid,
  3619. &lock->lk_new_open_stateid,
  3620. &open_stp, nn);
  3621. if (status)
  3622. goto out;
  3623. open_sop = openowner(open_stp->st_stateowner);
  3624. status = nfserr_bad_stateid;
  3625. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  3626. &lock->v.new.clientid))
  3627. goto out;
  3628. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  3629. &lock_stp, &new_state);
  3630. } else
  3631. status = nfs4_preprocess_seqid_op(cstate,
  3632. lock->lk_old_lock_seqid,
  3633. &lock->lk_old_lock_stateid,
  3634. NFS4_LOCK_STID, &lock_stp, nn);
  3635. if (status)
  3636. goto out;
  3637. lock_sop = lockowner(lock_stp->st_stateowner);
  3638. lkflg = setlkflg(lock->lk_type);
  3639. status = nfs4_check_openmode(lock_stp, lkflg);
  3640. if (status)
  3641. goto out;
  3642. status = nfserr_grace;
  3643. if (locks_in_grace(net) && !lock->lk_reclaim)
  3644. goto out;
  3645. status = nfserr_no_grace;
  3646. if (!locks_in_grace(net) && lock->lk_reclaim)
  3647. goto out;
  3648. file_lock = locks_alloc_lock();
  3649. if (!file_lock) {
  3650. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3651. status = nfserr_jukebox;
  3652. goto out;
  3653. }
  3654. locks_init_lock(file_lock);
  3655. switch (lock->lk_type) {
  3656. case NFS4_READ_LT:
  3657. case NFS4_READW_LT:
  3658. filp = find_readable_file(lock_stp->st_file);
  3659. if (filp)
  3660. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  3661. file_lock->fl_type = F_RDLCK;
  3662. break;
  3663. case NFS4_WRITE_LT:
  3664. case NFS4_WRITEW_LT:
  3665. filp = find_writeable_file(lock_stp->st_file);
  3666. if (filp)
  3667. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  3668. file_lock->fl_type = F_WRLCK;
  3669. break;
  3670. default:
  3671. status = nfserr_inval;
  3672. goto out;
  3673. }
  3674. if (!filp) {
  3675. status = nfserr_openmode;
  3676. goto out;
  3677. }
  3678. file_lock->fl_owner = (fl_owner_t)lock_sop;
  3679. file_lock->fl_pid = current->tgid;
  3680. file_lock->fl_file = filp;
  3681. file_lock->fl_flags = FL_POSIX;
  3682. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3683. file_lock->fl_start = lock->lk_offset;
  3684. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  3685. nfs4_transform_lock_offset(file_lock);
  3686. conflock = locks_alloc_lock();
  3687. if (!conflock) {
  3688. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3689. status = nfserr_jukebox;
  3690. goto out;
  3691. }
  3692. err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
  3693. switch (-err) {
  3694. case 0: /* success! */
  3695. update_stateid(&lock_stp->st_stid.sc_stateid);
  3696. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
  3697. sizeof(stateid_t));
  3698. status = 0;
  3699. break;
  3700. case (EAGAIN): /* conflock holds conflicting lock */
  3701. status = nfserr_denied;
  3702. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  3703. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  3704. break;
  3705. case (EDEADLK):
  3706. status = nfserr_deadlock;
  3707. break;
  3708. default:
  3709. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  3710. status = nfserrno(err);
  3711. break;
  3712. }
  3713. out:
  3714. if (status && new_state)
  3715. release_lockowner(lock_sop);
  3716. if (!cstate->replay_owner)
  3717. nfs4_unlock_state();
  3718. if (file_lock)
  3719. locks_free_lock(file_lock);
  3720. if (conflock)
  3721. locks_free_lock(conflock);
  3722. return status;
  3723. }
  3724. /*
  3725. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  3726. * so we do a temporary open here just to get an open file to pass to
  3727. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  3728. * inode operation.)
  3729. */
  3730. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  3731. {
  3732. struct file *file;
  3733. __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  3734. if (!err) {
  3735. err = nfserrno(vfs_test_lock(file, lock));
  3736. nfsd_close(file);
  3737. }
  3738. return err;
  3739. }
  3740. /*
  3741. * LOCKT operation
  3742. */
  3743. __be32
  3744. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3745. struct nfsd4_lockt *lockt)
  3746. {
  3747. struct inode *inode;
  3748. struct file_lock *file_lock = NULL;
  3749. struct nfs4_lockowner *lo;
  3750. __be32 status;
  3751. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3752. if (locks_in_grace(SVC_NET(rqstp)))
  3753. return nfserr_grace;
  3754. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  3755. return nfserr_inval;
  3756. nfs4_lock_state();
  3757. if (!nfsd4_has_session(cstate)) {
  3758. status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
  3759. if (status)
  3760. goto out;
  3761. }
  3762. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3763. goto out;
  3764. inode = cstate->current_fh.fh_dentry->d_inode;
  3765. file_lock = locks_alloc_lock();
  3766. if (!file_lock) {
  3767. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3768. status = nfserr_jukebox;
  3769. goto out;
  3770. }
  3771. locks_init_lock(file_lock);
  3772. switch (lockt->lt_type) {
  3773. case NFS4_READ_LT:
  3774. case NFS4_READW_LT:
  3775. file_lock->fl_type = F_RDLCK;
  3776. break;
  3777. case NFS4_WRITE_LT:
  3778. case NFS4_WRITEW_LT:
  3779. file_lock->fl_type = F_WRLCK;
  3780. break;
  3781. default:
  3782. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  3783. status = nfserr_inval;
  3784. goto out;
  3785. }
  3786. lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
  3787. if (lo)
  3788. file_lock->fl_owner = (fl_owner_t)lo;
  3789. file_lock->fl_pid = current->tgid;
  3790. file_lock->fl_flags = FL_POSIX;
  3791. file_lock->fl_start = lockt->lt_offset;
  3792. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  3793. nfs4_transform_lock_offset(file_lock);
  3794. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  3795. if (status)
  3796. goto out;
  3797. if (file_lock->fl_type != F_UNLCK) {
  3798. status = nfserr_denied;
  3799. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  3800. }
  3801. out:
  3802. nfs4_unlock_state();
  3803. if (file_lock)
  3804. locks_free_lock(file_lock);
  3805. return status;
  3806. }
  3807. __be32
  3808. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3809. struct nfsd4_locku *locku)
  3810. {
  3811. struct nfs4_ol_stateid *stp;
  3812. struct file *filp = NULL;
  3813. struct file_lock *file_lock = NULL;
  3814. __be32 status;
  3815. int err;
  3816. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3817. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  3818. (long long) locku->lu_offset,
  3819. (long long) locku->lu_length);
  3820. if (check_lock_length(locku->lu_offset, locku->lu_length))
  3821. return nfserr_inval;
  3822. nfs4_lock_state();
  3823. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  3824. &locku->lu_stateid, NFS4_LOCK_STID,
  3825. &stp, nn);
  3826. if (status)
  3827. goto out;
  3828. filp = find_any_file(stp->st_file);
  3829. if (!filp) {
  3830. status = nfserr_lock_range;
  3831. goto out;
  3832. }
  3833. file_lock = locks_alloc_lock();
  3834. if (!file_lock) {
  3835. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3836. status = nfserr_jukebox;
  3837. goto out;
  3838. }
  3839. locks_init_lock(file_lock);
  3840. file_lock->fl_type = F_UNLCK;
  3841. file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
  3842. file_lock->fl_pid = current->tgid;
  3843. file_lock->fl_file = filp;
  3844. file_lock->fl_flags = FL_POSIX;
  3845. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3846. file_lock->fl_start = locku->lu_offset;
  3847. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  3848. locku->lu_length);
  3849. nfs4_transform_lock_offset(file_lock);
  3850. /*
  3851. * Try to unlock the file in the VFS.
  3852. */
  3853. err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
  3854. if (err) {
  3855. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  3856. goto out_nfserr;
  3857. }
  3858. /*
  3859. * OK, unlock succeeded; the only thing left to do is update the stateid.
  3860. */
  3861. update_stateid(&stp->st_stid.sc_stateid);
  3862. memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3863. out:
  3864. if (!cstate->replay_owner)
  3865. nfs4_unlock_state();
  3866. if (file_lock)
  3867. locks_free_lock(file_lock);
  3868. return status;
  3869. out_nfserr:
  3870. status = nfserrno(err);
  3871. goto out;
  3872. }
  3873. /*
  3874. * returns
  3875. * 1: locks held by lockowner
  3876. * 0: no locks held by lockowner
  3877. */
  3878. static int
  3879. check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
  3880. {
  3881. struct file_lock **flpp;
  3882. struct inode *inode = filp->fi_inode;
  3883. int status = 0;
  3884. lock_flocks();
  3885. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  3886. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  3887. status = 1;
  3888. goto out;
  3889. }
  3890. }
  3891. out:
  3892. unlock_flocks();
  3893. return status;
  3894. }
  3895. __be32
  3896. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  3897. struct nfsd4_compound_state *cstate,
  3898. struct nfsd4_release_lockowner *rlockowner)
  3899. {
  3900. clientid_t *clid = &rlockowner->rl_clientid;
  3901. struct nfs4_stateowner *sop;
  3902. struct nfs4_lockowner *lo;
  3903. struct nfs4_ol_stateid *stp;
  3904. struct xdr_netobj *owner = &rlockowner->rl_owner;
  3905. struct list_head matches;
  3906. unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
  3907. __be32 status;
  3908. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3909. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  3910. clid->cl_boot, clid->cl_id);
  3911. nfs4_lock_state();
  3912. status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
  3913. if (status)
  3914. goto out;
  3915. status = nfserr_locks_held;
  3916. INIT_LIST_HEAD(&matches);
  3917. list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  3918. if (sop->so_is_open_owner)
  3919. continue;
  3920. if (!same_owner_str(sop, owner, clid))
  3921. continue;
  3922. list_for_each_entry(stp, &sop->so_stateids,
  3923. st_perstateowner) {
  3924. lo = lockowner(sop);
  3925. if (check_for_locks(stp->st_file, lo))
  3926. goto out;
  3927. list_add(&lo->lo_list, &matches);
  3928. }
  3929. }
  3930. /* Clients probably won't expect us to return with some (but not all)
  3931. * of the lockowner state released; so don't release any until all
  3932. * have been checked. */
  3933. status = nfs_ok;
  3934. while (!list_empty(&matches)) {
  3935. lo = list_entry(matches.next, struct nfs4_lockowner,
  3936. lo_list);
  3937. /* unhash_stateowner deletes so_perclient only
  3938. * for openowners. */
  3939. list_del(&lo->lo_list);
  3940. release_lockowner(lo);
  3941. }
  3942. out:
  3943. nfs4_unlock_state();
  3944. return status;
  3945. }
  3946. static inline struct nfs4_client_reclaim *
  3947. alloc_reclaim(void)
  3948. {
  3949. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  3950. }
  3951. bool
  3952. nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
  3953. {
  3954. struct nfs4_client_reclaim *crp;
  3955. crp = nfsd4_find_reclaim_client(name, nn);
  3956. return (crp && crp->cr_clp);
  3957. }
  3958. /*
  3959. * failure => all reset bets are off, nfserr_no_grace...
  3960. */
  3961. struct nfs4_client_reclaim *
  3962. nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
  3963. {
  3964. unsigned int strhashval;
  3965. struct nfs4_client_reclaim *crp;
  3966. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  3967. crp = alloc_reclaim();
  3968. if (crp) {
  3969. strhashval = clientstr_hashval(name);
  3970. INIT_LIST_HEAD(&crp->cr_strhash);
  3971. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  3972. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  3973. crp->cr_clp = NULL;
  3974. nn->reclaim_str_hashtbl_size++;
  3975. }
  3976. return crp;
  3977. }
  3978. void
  3979. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  3980. {
  3981. list_del(&crp->cr_strhash);
  3982. kfree(crp);
  3983. nn->reclaim_str_hashtbl_size--;
  3984. }
  3985. void
  3986. nfs4_release_reclaim(struct nfsd_net *nn)
  3987. {
  3988. struct nfs4_client_reclaim *crp = NULL;
  3989. int i;
  3990. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  3991. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  3992. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  3993. struct nfs4_client_reclaim, cr_strhash);
  3994. nfs4_remove_reclaim_record(crp, nn);
  3995. }
  3996. }
  3997. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  3998. }
  3999. /*
  4000. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  4001. struct nfs4_client_reclaim *
  4002. nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
  4003. {
  4004. unsigned int strhashval;
  4005. struct nfs4_client_reclaim *crp = NULL;
  4006. dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
  4007. strhashval = clientstr_hashval(recdir);
  4008. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  4009. if (same_name(crp->cr_recdir, recdir)) {
  4010. return crp;
  4011. }
  4012. }
  4013. return NULL;
  4014. }
  4015. /*
  4016. * Called from OPEN. Look for clientid in reclaim list.
  4017. */
  4018. __be32
  4019. nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  4020. {
  4021. struct nfs4_client *clp;
  4022. /* find clientid in conf_id_hashtbl */
  4023. clp = find_confirmed_client(clid, sessions, nn);
  4024. if (clp == NULL)
  4025. return nfserr_reclaim_bad;
  4026. return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
  4027. }
  4028. #ifdef CONFIG_NFSD_FAULT_INJECTION
  4029. u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
  4030. {
  4031. expire_client(clp);
  4032. return 1;
  4033. }
  4034. u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
  4035. {
  4036. char buf[INET6_ADDRSTRLEN];
  4037. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4038. printk(KERN_INFO "NFS Client: %s\n", buf);
  4039. return 1;
  4040. }
  4041. static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
  4042. const char *type)
  4043. {
  4044. char buf[INET6_ADDRSTRLEN];
  4045. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4046. printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
  4047. }
  4048. static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
  4049. {
  4050. struct nfs4_openowner *oop;
  4051. struct nfs4_lockowner *lop, *lo_next;
  4052. struct nfs4_ol_stateid *stp, *st_next;
  4053. u64 count = 0;
  4054. list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
  4055. list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
  4056. list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
  4057. if (func)
  4058. func(lop);
  4059. if (++count == max)
  4060. return count;
  4061. }
  4062. }
  4063. }
  4064. return count;
  4065. }
  4066. u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
  4067. {
  4068. return nfsd_foreach_client_lock(clp, max, release_lockowner);
  4069. }
  4070. u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
  4071. {
  4072. u64 count = nfsd_foreach_client_lock(clp, max, NULL);
  4073. nfsd_print_count(clp, count, "locked files");
  4074. return count;
  4075. }
  4076. static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
  4077. {
  4078. struct nfs4_openowner *oop, *next;
  4079. u64 count = 0;
  4080. list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
  4081. if (func)
  4082. func(oop);
  4083. if (++count == max)
  4084. break;
  4085. }
  4086. return count;
  4087. }
  4088. u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
  4089. {
  4090. return nfsd_foreach_client_open(clp, max, release_openowner);
  4091. }
  4092. u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
  4093. {
  4094. u64 count = nfsd_foreach_client_open(clp, max, NULL);
  4095. nfsd_print_count(clp, count, "open files");
  4096. return count;
  4097. }
  4098. static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
  4099. struct list_head *victims)
  4100. {
  4101. struct nfs4_delegation *dp, *next;
  4102. u64 count = 0;
  4103. list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
  4104. if (victims)
  4105. list_move(&dp->dl_recall_lru, victims);
  4106. if (++count == max)
  4107. break;
  4108. }
  4109. return count;
  4110. }
  4111. u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
  4112. {
  4113. struct nfs4_delegation *dp, *next;
  4114. LIST_HEAD(victims);
  4115. u64 count;
  4116. spin_lock(&recall_lock);
  4117. count = nfsd_find_all_delegations(clp, max, &victims);
  4118. spin_unlock(&recall_lock);
  4119. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4120. unhash_delegation(dp);
  4121. return count;
  4122. }
  4123. u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
  4124. {
  4125. struct nfs4_delegation *dp, *next;
  4126. LIST_HEAD(victims);
  4127. u64 count;
  4128. spin_lock(&recall_lock);
  4129. count = nfsd_find_all_delegations(clp, max, &victims);
  4130. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4131. nfsd_break_one_deleg(dp);
  4132. spin_unlock(&recall_lock);
  4133. return count;
  4134. }
  4135. u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
  4136. {
  4137. u64 count = 0;
  4138. spin_lock(&recall_lock);
  4139. count = nfsd_find_all_delegations(clp, max, NULL);
  4140. spin_unlock(&recall_lock);
  4141. nfsd_print_count(clp, count, "delegations");
  4142. return count;
  4143. }
  4144. u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
  4145. {
  4146. struct nfs4_client *clp, *next;
  4147. u64 count = 0;
  4148. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4149. if (!nfsd_netns_ready(nn))
  4150. return 0;
  4151. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  4152. count += func(clp, max - count);
  4153. if ((max != 0) && (count >= max))
  4154. break;
  4155. }
  4156. return count;
  4157. }
  4158. struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
  4159. {
  4160. struct nfs4_client *clp;
  4161. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4162. if (!nfsd_netns_ready(nn))
  4163. return NULL;
  4164. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  4165. if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
  4166. return clp;
  4167. }
  4168. return NULL;
  4169. }
  4170. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  4171. /* initialization to perform at module load time: */
  4172. void
  4173. nfs4_state_init(void)
  4174. {
  4175. int i;
  4176. for (i = 0; i < FILE_HASH_SIZE; i++) {
  4177. INIT_LIST_HEAD(&file_hashtbl[i]);
  4178. }
  4179. INIT_LIST_HEAD(&del_recall_lru);
  4180. }
  4181. /*
  4182. * Since the lifetime of a delegation isn't limited to that of an open, a
  4183. * client may quite reasonably hang on to a delegation as long as it has
  4184. * the inode cached. This becomes an obvious problem the first time a
  4185. * client's inode cache approaches the size of the server's total memory.
  4186. *
  4187. * For now we avoid this problem by imposing a hard limit on the number
  4188. * of delegations, which varies according to the server's memory size.
  4189. */
  4190. static void
  4191. set_max_delegations(void)
  4192. {
  4193. /*
  4194. * Allow at most 4 delegations per megabyte of RAM. Quick
  4195. * estimates suggest that in the worst case (where every delegation
  4196. * is for a different inode), a delegation could take about 1.5K,
  4197. * giving a worst case usage of about 6% of memory.
  4198. */
  4199. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  4200. }
  4201. static int nfs4_state_create_net(struct net *net)
  4202. {
  4203. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4204. int i;
  4205. nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4206. CLIENT_HASH_SIZE, GFP_KERNEL);
  4207. if (!nn->conf_id_hashtbl)
  4208. goto err;
  4209. nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4210. CLIENT_HASH_SIZE, GFP_KERNEL);
  4211. if (!nn->unconf_id_hashtbl)
  4212. goto err_unconf_id;
  4213. nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
  4214. OWNER_HASH_SIZE, GFP_KERNEL);
  4215. if (!nn->ownerstr_hashtbl)
  4216. goto err_ownerstr;
  4217. nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
  4218. LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
  4219. if (!nn->lockowner_ino_hashtbl)
  4220. goto err_lockowner_ino;
  4221. nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
  4222. SESSION_HASH_SIZE, GFP_KERNEL);
  4223. if (!nn->sessionid_hashtbl)
  4224. goto err_sessionid;
  4225. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4226. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  4227. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  4228. }
  4229. for (i = 0; i < OWNER_HASH_SIZE; i++)
  4230. INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
  4231. for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
  4232. INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
  4233. for (i = 0; i < SESSION_HASH_SIZE; i++)
  4234. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  4235. nn->conf_name_tree = RB_ROOT;
  4236. nn->unconf_name_tree = RB_ROOT;
  4237. INIT_LIST_HEAD(&nn->client_lru);
  4238. INIT_LIST_HEAD(&nn->close_lru);
  4239. spin_lock_init(&nn->client_lock);
  4240. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  4241. get_net(net);
  4242. return 0;
  4243. err_sessionid:
  4244. kfree(nn->lockowner_ino_hashtbl);
  4245. err_lockowner_ino:
  4246. kfree(nn->ownerstr_hashtbl);
  4247. err_ownerstr:
  4248. kfree(nn->unconf_id_hashtbl);
  4249. err_unconf_id:
  4250. kfree(nn->conf_id_hashtbl);
  4251. err:
  4252. return -ENOMEM;
  4253. }
  4254. static void
  4255. nfs4_state_destroy_net(struct net *net)
  4256. {
  4257. int i;
  4258. struct nfs4_client *clp = NULL;
  4259. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4260. struct rb_node *node, *tmp;
  4261. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4262. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  4263. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  4264. destroy_client(clp);
  4265. }
  4266. }
  4267. node = rb_first(&nn->unconf_name_tree);
  4268. while (node != NULL) {
  4269. tmp = node;
  4270. node = rb_next(tmp);
  4271. clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
  4272. rb_erase(tmp, &nn->unconf_name_tree);
  4273. destroy_client(clp);
  4274. }
  4275. kfree(nn->sessionid_hashtbl);
  4276. kfree(nn->lockowner_ino_hashtbl);
  4277. kfree(nn->ownerstr_hashtbl);
  4278. kfree(nn->unconf_id_hashtbl);
  4279. kfree(nn->conf_id_hashtbl);
  4280. put_net(net);
  4281. }
  4282. int
  4283. nfs4_state_start_net(struct net *net)
  4284. {
  4285. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4286. int ret;
  4287. ret = nfs4_state_create_net(net);
  4288. if (ret)
  4289. return ret;
  4290. nfsd4_client_tracking_init(net);
  4291. nn->boot_time = get_seconds();
  4292. locks_start_grace(net, &nn->nfsd4_manager);
  4293. nn->grace_ended = false;
  4294. printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
  4295. nn->nfsd4_grace, net);
  4296. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
  4297. return 0;
  4298. }
  4299. /* initialization to perform when the nfsd service is started: */
  4300. int
  4301. nfs4_state_start(void)
  4302. {
  4303. int ret;
  4304. ret = set_callback_cred();
  4305. if (ret)
  4306. return -ENOMEM;
  4307. laundry_wq = create_singlethread_workqueue("nfsd4");
  4308. if (laundry_wq == NULL) {
  4309. ret = -ENOMEM;
  4310. goto out_recovery;
  4311. }
  4312. ret = nfsd4_create_callback_queue();
  4313. if (ret)
  4314. goto out_free_laundry;
  4315. set_max_delegations();
  4316. return 0;
  4317. out_free_laundry:
  4318. destroy_workqueue(laundry_wq);
  4319. out_recovery:
  4320. return ret;
  4321. }
  4322. /* should be called with the state lock held */
  4323. void
  4324. nfs4_state_shutdown_net(struct net *net)
  4325. {
  4326. struct nfs4_delegation *dp = NULL;
  4327. struct list_head *pos, *next, reaplist;
  4328. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4329. cancel_delayed_work_sync(&nn->laundromat_work);
  4330. locks_end_grace(&nn->nfsd4_manager);
  4331. INIT_LIST_HEAD(&reaplist);
  4332. spin_lock(&recall_lock);
  4333. list_for_each_safe(pos, next, &del_recall_lru) {
  4334. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4335. if (dp->dl_stid.sc_client->net != net)
  4336. continue;
  4337. list_move(&dp->dl_recall_lru, &reaplist);
  4338. }
  4339. spin_unlock(&recall_lock);
  4340. list_for_each_safe(pos, next, &reaplist) {
  4341. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4342. unhash_delegation(dp);
  4343. }
  4344. nfsd4_client_tracking_exit(net);
  4345. nfs4_state_destroy_net(net);
  4346. }
  4347. void
  4348. nfs4_state_shutdown(void)
  4349. {
  4350. destroy_workqueue(laundry_wq);
  4351. nfsd4_destroy_callback_queue();
  4352. }
  4353. static void
  4354. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4355. {
  4356. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  4357. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  4358. }
  4359. static void
  4360. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4361. {
  4362. if (cstate->minorversion) {
  4363. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  4364. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4365. }
  4366. }
  4367. void
  4368. clear_current_stateid(struct nfsd4_compound_state *cstate)
  4369. {
  4370. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4371. }
  4372. /*
  4373. * functions to set current state id
  4374. */
  4375. void
  4376. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4377. {
  4378. put_stateid(cstate, &odp->od_stateid);
  4379. }
  4380. void
  4381. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  4382. {
  4383. put_stateid(cstate, &open->op_stateid);
  4384. }
  4385. void
  4386. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4387. {
  4388. put_stateid(cstate, &close->cl_stateid);
  4389. }
  4390. void
  4391. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  4392. {
  4393. put_stateid(cstate, &lock->lk_resp_stateid);
  4394. }
  4395. /*
  4396. * functions to consume current state id
  4397. */
  4398. void
  4399. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4400. {
  4401. get_stateid(cstate, &odp->od_stateid);
  4402. }
  4403. void
  4404. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  4405. {
  4406. get_stateid(cstate, &drp->dr_stateid);
  4407. }
  4408. void
  4409. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  4410. {
  4411. get_stateid(cstate, &fsp->fr_stateid);
  4412. }
  4413. void
  4414. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  4415. {
  4416. get_stateid(cstate, &setattr->sa_stateid);
  4417. }
  4418. void
  4419. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4420. {
  4421. get_stateid(cstate, &close->cl_stateid);
  4422. }
  4423. void
  4424. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  4425. {
  4426. get_stateid(cstate, &locku->lu_stateid);
  4427. }
  4428. void
  4429. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  4430. {
  4431. get_stateid(cstate, &read->rd_stateid);
  4432. }
  4433. void
  4434. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  4435. {
  4436. get_stateid(cstate, &write->wr_stateid);
  4437. }