nfs4state.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086
  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. spin_unlock(&clp->cl_lock);
  658. nfsd4_probe_callback(clp);
  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. 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. if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
  911. return;
  912. if (is_client_expired(clp)) {
  913. free_client(clp);
  914. session->se_client = NULL;
  915. } else
  916. renew_client_locked(clp);
  917. spin_unlock(&nn->client_lock);
  918. }
  919. /* must be called under the client_lock */
  920. static inline void
  921. unhash_client_locked(struct nfs4_client *clp)
  922. {
  923. struct nfsd4_session *ses;
  924. mark_client_expired(clp);
  925. list_del(&clp->cl_lru);
  926. spin_lock(&clp->cl_lock);
  927. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  928. list_del_init(&ses->se_hash);
  929. spin_unlock(&clp->cl_lock);
  930. }
  931. static void
  932. destroy_client(struct nfs4_client *clp)
  933. {
  934. struct nfs4_openowner *oo;
  935. struct nfs4_delegation *dp;
  936. struct list_head reaplist;
  937. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  938. INIT_LIST_HEAD(&reaplist);
  939. spin_lock(&recall_lock);
  940. while (!list_empty(&clp->cl_delegations)) {
  941. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  942. list_del_init(&dp->dl_perclnt);
  943. list_move(&dp->dl_recall_lru, &reaplist);
  944. }
  945. spin_unlock(&recall_lock);
  946. while (!list_empty(&reaplist)) {
  947. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  948. unhash_delegation(dp);
  949. }
  950. while (!list_empty(&clp->cl_openowners)) {
  951. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  952. release_openowner(oo);
  953. }
  954. nfsd4_shutdown_callback(clp);
  955. if (clp->cl_cb_conn.cb_xprt)
  956. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  957. list_del(&clp->cl_idhash);
  958. if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  959. rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
  960. else
  961. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  962. spin_lock(&nn->client_lock);
  963. unhash_client_locked(clp);
  964. if (atomic_read(&clp->cl_refcount) == 0)
  965. free_client(clp);
  966. spin_unlock(&nn->client_lock);
  967. }
  968. static void expire_client(struct nfs4_client *clp)
  969. {
  970. nfsd4_client_record_remove(clp);
  971. destroy_client(clp);
  972. }
  973. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  974. {
  975. memcpy(target->cl_verifier.data, source->data,
  976. sizeof(target->cl_verifier.data));
  977. }
  978. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  979. {
  980. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  981. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  982. }
  983. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  984. {
  985. if (source->cr_principal) {
  986. target->cr_principal =
  987. kstrdup(source->cr_principal, GFP_KERNEL);
  988. if (target->cr_principal == NULL)
  989. return -ENOMEM;
  990. } else
  991. target->cr_principal = NULL;
  992. target->cr_flavor = source->cr_flavor;
  993. target->cr_uid = source->cr_uid;
  994. target->cr_gid = source->cr_gid;
  995. target->cr_group_info = source->cr_group_info;
  996. get_group_info(target->cr_group_info);
  997. return 0;
  998. }
  999. static long long
  1000. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  1001. {
  1002. long long res;
  1003. res = o1->len - o2->len;
  1004. if (res)
  1005. return res;
  1006. return (long long)memcmp(o1->data, o2->data, o1->len);
  1007. }
  1008. static int same_name(const char *n1, const char *n2)
  1009. {
  1010. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  1011. }
  1012. static int
  1013. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  1014. {
  1015. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  1016. }
  1017. static int
  1018. same_clid(clientid_t *cl1, clientid_t *cl2)
  1019. {
  1020. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1021. }
  1022. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  1023. {
  1024. int i;
  1025. if (g1->ngroups != g2->ngroups)
  1026. return false;
  1027. for (i=0; i<g1->ngroups; i++)
  1028. if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
  1029. return false;
  1030. return true;
  1031. }
  1032. /*
  1033. * RFC 3530 language requires clid_inuse be returned when the
  1034. * "principal" associated with a requests differs from that previously
  1035. * used. We use uid, gid's, and gss principal string as our best
  1036. * approximation. We also don't want to allow non-gss use of a client
  1037. * established using gss: in theory cr_principal should catch that
  1038. * change, but in practice cr_principal can be null even in the gss case
  1039. * since gssd doesn't always pass down a principal string.
  1040. */
  1041. static bool is_gss_cred(struct svc_cred *cr)
  1042. {
  1043. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  1044. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  1045. }
  1046. static bool
  1047. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1048. {
  1049. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  1050. || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
  1051. || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
  1052. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  1053. return false;
  1054. if (cr1->cr_principal == cr2->cr_principal)
  1055. return true;
  1056. if (!cr1->cr_principal || !cr2->cr_principal)
  1057. return false;
  1058. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  1059. }
  1060. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  1061. {
  1062. static u32 current_clientid = 1;
  1063. clp->cl_clientid.cl_boot = nn->boot_time;
  1064. clp->cl_clientid.cl_id = current_clientid++;
  1065. }
  1066. static void gen_confirm(struct nfs4_client *clp)
  1067. {
  1068. __be32 verf[2];
  1069. static u32 i;
  1070. verf[0] = (__be32)get_seconds();
  1071. verf[1] = (__be32)i++;
  1072. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1073. }
  1074. static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
  1075. {
  1076. struct nfs4_stid *ret;
  1077. ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1078. if (!ret || !ret->sc_type)
  1079. return NULL;
  1080. return ret;
  1081. }
  1082. static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1083. {
  1084. struct nfs4_stid *s;
  1085. s = find_stateid(cl, t);
  1086. if (!s)
  1087. return NULL;
  1088. if (typemask & s->sc_type)
  1089. return s;
  1090. return NULL;
  1091. }
  1092. static struct nfs4_client *create_client(struct xdr_netobj name,
  1093. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1094. {
  1095. struct nfs4_client *clp;
  1096. struct sockaddr *sa = svc_addr(rqstp);
  1097. int ret;
  1098. struct net *net = SVC_NET(rqstp);
  1099. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1100. clp = alloc_client(name);
  1101. if (clp == NULL)
  1102. return NULL;
  1103. INIT_LIST_HEAD(&clp->cl_sessions);
  1104. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1105. if (ret) {
  1106. spin_lock(&nn->client_lock);
  1107. free_client(clp);
  1108. spin_unlock(&nn->client_lock);
  1109. return NULL;
  1110. }
  1111. idr_init(&clp->cl_stateids);
  1112. atomic_set(&clp->cl_refcount, 0);
  1113. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1114. INIT_LIST_HEAD(&clp->cl_idhash);
  1115. INIT_LIST_HEAD(&clp->cl_openowners);
  1116. INIT_LIST_HEAD(&clp->cl_delegations);
  1117. INIT_LIST_HEAD(&clp->cl_lru);
  1118. INIT_LIST_HEAD(&clp->cl_callbacks);
  1119. spin_lock_init(&clp->cl_lock);
  1120. nfsd4_init_callback(&clp->cl_cb_null);
  1121. clp->cl_time = get_seconds();
  1122. clear_bit(0, &clp->cl_cb_slot_busy);
  1123. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1124. copy_verf(clp, verf);
  1125. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1126. gen_confirm(clp);
  1127. clp->cl_cb_session = NULL;
  1128. clp->net = net;
  1129. return clp;
  1130. }
  1131. static void
  1132. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  1133. {
  1134. struct rb_node **new = &(root->rb_node), *parent = NULL;
  1135. struct nfs4_client *clp;
  1136. while (*new) {
  1137. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  1138. parent = *new;
  1139. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  1140. new = &((*new)->rb_left);
  1141. else
  1142. new = &((*new)->rb_right);
  1143. }
  1144. rb_link_node(&new_clp->cl_namenode, parent, new);
  1145. rb_insert_color(&new_clp->cl_namenode, root);
  1146. }
  1147. static struct nfs4_client *
  1148. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  1149. {
  1150. long long cmp;
  1151. struct rb_node *node = root->rb_node;
  1152. struct nfs4_client *clp;
  1153. while (node) {
  1154. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  1155. cmp = compare_blob(&clp->cl_name, name);
  1156. if (cmp > 0)
  1157. node = node->rb_left;
  1158. else if (cmp < 0)
  1159. node = node->rb_right;
  1160. else
  1161. return clp;
  1162. }
  1163. return NULL;
  1164. }
  1165. static void
  1166. add_to_unconfirmed(struct nfs4_client *clp)
  1167. {
  1168. unsigned int idhashval;
  1169. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1170. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1171. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  1172. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1173. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  1174. renew_client(clp);
  1175. }
  1176. static void
  1177. move_to_confirmed(struct nfs4_client *clp)
  1178. {
  1179. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1180. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1181. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1182. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  1183. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1184. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  1185. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1186. renew_client(clp);
  1187. }
  1188. static struct nfs4_client *
  1189. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1190. {
  1191. struct nfs4_client *clp;
  1192. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1193. list_for_each_entry(clp, &nn->conf_id_hashtbl[idhashval], cl_idhash) {
  1194. if (same_clid(&clp->cl_clientid, clid)) {
  1195. if ((bool)clp->cl_minorversion != sessions)
  1196. return NULL;
  1197. renew_client(clp);
  1198. return clp;
  1199. }
  1200. }
  1201. return NULL;
  1202. }
  1203. static struct nfs4_client *
  1204. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1205. {
  1206. struct nfs4_client *clp;
  1207. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1208. list_for_each_entry(clp, &nn->unconf_id_hashtbl[idhashval], cl_idhash) {
  1209. if (same_clid(&clp->cl_clientid, clid)) {
  1210. if ((bool)clp->cl_minorversion != sessions)
  1211. return NULL;
  1212. return clp;
  1213. }
  1214. }
  1215. return NULL;
  1216. }
  1217. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1218. {
  1219. return clp->cl_exchange_flags != 0;
  1220. }
  1221. static struct nfs4_client *
  1222. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1223. {
  1224. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  1225. }
  1226. static struct nfs4_client *
  1227. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1228. {
  1229. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  1230. }
  1231. static void
  1232. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1233. {
  1234. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1235. struct sockaddr *sa = svc_addr(rqstp);
  1236. u32 scopeid = rpc_get_scope_id(sa);
  1237. unsigned short expected_family;
  1238. /* Currently, we only support tcp and tcp6 for the callback channel */
  1239. if (se->se_callback_netid_len == 3 &&
  1240. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1241. expected_family = AF_INET;
  1242. else if (se->se_callback_netid_len == 4 &&
  1243. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1244. expected_family = AF_INET6;
  1245. else
  1246. goto out_err;
  1247. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  1248. se->se_callback_addr_len,
  1249. (struct sockaddr *)&conn->cb_addr,
  1250. sizeof(conn->cb_addr));
  1251. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1252. goto out_err;
  1253. if (conn->cb_addr.ss_family == AF_INET6)
  1254. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1255. conn->cb_prog = se->se_callback_prog;
  1256. conn->cb_ident = se->se_callback_ident;
  1257. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1258. return;
  1259. out_err:
  1260. conn->cb_addr.ss_family = AF_UNSPEC;
  1261. conn->cb_addrlen = 0;
  1262. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1263. "will not receive delegations\n",
  1264. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1265. return;
  1266. }
  1267. /*
  1268. * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
  1269. */
  1270. void
  1271. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1272. {
  1273. struct nfsd4_slot *slot = resp->cstate.slot;
  1274. unsigned int base;
  1275. dprintk("--> %s slot %p\n", __func__, slot);
  1276. slot->sl_opcnt = resp->opcnt;
  1277. slot->sl_status = resp->cstate.status;
  1278. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1279. if (nfsd4_not_cached(resp)) {
  1280. slot->sl_datalen = 0;
  1281. return;
  1282. }
  1283. slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
  1284. base = (char *)resp->cstate.datap -
  1285. (char *)resp->xbuf->head[0].iov_base;
  1286. if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
  1287. slot->sl_datalen))
  1288. WARN("%s: sessions DRC could not cache compound\n", __func__);
  1289. return;
  1290. }
  1291. /*
  1292. * Encode the replay sequence operation from the slot values.
  1293. * If cachethis is FALSE encode the uncached rep error on the next
  1294. * operation which sets resp->p and increments resp->opcnt for
  1295. * nfs4svc_encode_compoundres.
  1296. *
  1297. */
  1298. static __be32
  1299. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1300. struct nfsd4_compoundres *resp)
  1301. {
  1302. struct nfsd4_op *op;
  1303. struct nfsd4_slot *slot = resp->cstate.slot;
  1304. /* Encode the replayed sequence operation */
  1305. op = &args->ops[resp->opcnt - 1];
  1306. nfsd4_encode_operation(resp, op);
  1307. /* Return nfserr_retry_uncached_rep in next operation. */
  1308. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1309. op = &args->ops[resp->opcnt++];
  1310. op->status = nfserr_retry_uncached_rep;
  1311. nfsd4_encode_operation(resp, op);
  1312. }
  1313. return op->status;
  1314. }
  1315. /*
  1316. * The sequence operation is not cached because we can use the slot and
  1317. * session values.
  1318. */
  1319. __be32
  1320. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1321. struct nfsd4_sequence *seq)
  1322. {
  1323. struct nfsd4_slot *slot = resp->cstate.slot;
  1324. __be32 status;
  1325. dprintk("--> %s slot %p\n", __func__, slot);
  1326. /* Either returns 0 or nfserr_retry_uncached */
  1327. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  1328. if (status == nfserr_retry_uncached_rep)
  1329. return status;
  1330. /* The sequence operation has been encoded, cstate->datap set. */
  1331. memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
  1332. resp->opcnt = slot->sl_opcnt;
  1333. resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
  1334. status = slot->sl_status;
  1335. return status;
  1336. }
  1337. /*
  1338. * Set the exchange_id flags returned by the server.
  1339. */
  1340. static void
  1341. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  1342. {
  1343. /* pNFS is not supported */
  1344. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  1345. /* Referrals are supported, Migration is not. */
  1346. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  1347. /* set the wire flags to return to client. */
  1348. clid->flags = new->cl_exchange_flags;
  1349. }
  1350. static bool client_has_state(struct nfs4_client *clp)
  1351. {
  1352. /*
  1353. * Note clp->cl_openowners check isn't quite right: there's no
  1354. * need to count owners without stateid's.
  1355. *
  1356. * Also note we should probably be using this in 4.0 case too.
  1357. */
  1358. return !list_empty(&clp->cl_openowners)
  1359. || !list_empty(&clp->cl_delegations)
  1360. || !list_empty(&clp->cl_sessions);
  1361. }
  1362. __be32
  1363. nfsd4_exchange_id(struct svc_rqst *rqstp,
  1364. struct nfsd4_compound_state *cstate,
  1365. struct nfsd4_exchange_id *exid)
  1366. {
  1367. struct nfs4_client *unconf, *conf, *new;
  1368. __be32 status;
  1369. char addr_str[INET6_ADDRSTRLEN];
  1370. nfs4_verifier verf = exid->verifier;
  1371. struct sockaddr *sa = svc_addr(rqstp);
  1372. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  1373. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1374. rpc_ntop(sa, addr_str, sizeof(addr_str));
  1375. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  1376. "ip_addr=%s flags %x, spa_how %d\n",
  1377. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  1378. addr_str, exid->flags, exid->spa_how);
  1379. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  1380. return nfserr_inval;
  1381. /* Currently only support SP4_NONE */
  1382. switch (exid->spa_how) {
  1383. case SP4_NONE:
  1384. break;
  1385. default: /* checked by xdr code */
  1386. WARN_ON_ONCE(1);
  1387. case SP4_SSV:
  1388. case SP4_MACH_CRED:
  1389. return nfserr_serverfault; /* no excuse :-/ */
  1390. }
  1391. /* Cases below refer to rfc 5661 section 18.35.4: */
  1392. nfs4_lock_state();
  1393. conf = find_confirmed_client_by_name(&exid->clname, nn);
  1394. if (conf) {
  1395. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  1396. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  1397. if (update) {
  1398. if (!clp_used_exchangeid(conf)) { /* buggy client */
  1399. status = nfserr_inval;
  1400. goto out;
  1401. }
  1402. if (!creds_match) { /* case 9 */
  1403. status = nfserr_perm;
  1404. goto out;
  1405. }
  1406. if (!verfs_match) { /* case 8 */
  1407. status = nfserr_not_same;
  1408. goto out;
  1409. }
  1410. /* case 6 */
  1411. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1412. new = conf;
  1413. goto out_copy;
  1414. }
  1415. if (!creds_match) { /* case 3 */
  1416. if (client_has_state(conf)) {
  1417. status = nfserr_clid_inuse;
  1418. goto out;
  1419. }
  1420. expire_client(conf);
  1421. goto out_new;
  1422. }
  1423. if (verfs_match) { /* case 2 */
  1424. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1425. new = conf;
  1426. goto out_copy;
  1427. }
  1428. /* case 5, client reboot */
  1429. goto out_new;
  1430. }
  1431. if (update) { /* case 7 */
  1432. status = nfserr_noent;
  1433. goto out;
  1434. }
  1435. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  1436. if (unconf) /* case 4, possible retry or client restart */
  1437. expire_client(unconf);
  1438. /* case 1 (normal case) */
  1439. out_new:
  1440. new = create_client(exid->clname, rqstp, &verf);
  1441. if (new == NULL) {
  1442. status = nfserr_jukebox;
  1443. goto out;
  1444. }
  1445. new->cl_minorversion = 1;
  1446. gen_clid(new, nn);
  1447. add_to_unconfirmed(new);
  1448. out_copy:
  1449. exid->clientid.cl_boot = new->cl_clientid.cl_boot;
  1450. exid->clientid.cl_id = new->cl_clientid.cl_id;
  1451. exid->seqid = new->cl_cs_slot.sl_seqid + 1;
  1452. nfsd4_set_ex_flags(new, exid);
  1453. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  1454. new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
  1455. status = nfs_ok;
  1456. out:
  1457. nfs4_unlock_state();
  1458. return status;
  1459. }
  1460. static __be32
  1461. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  1462. {
  1463. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  1464. slot_seqid);
  1465. /* The slot is in use, and no response has been sent. */
  1466. if (slot_inuse) {
  1467. if (seqid == slot_seqid)
  1468. return nfserr_jukebox;
  1469. else
  1470. return nfserr_seq_misordered;
  1471. }
  1472. /* Note unsigned 32-bit arithmetic handles wraparound: */
  1473. if (likely(seqid == slot_seqid + 1))
  1474. return nfs_ok;
  1475. if (seqid == slot_seqid)
  1476. return nfserr_replay_cache;
  1477. return nfserr_seq_misordered;
  1478. }
  1479. /*
  1480. * Cache the create session result into the create session single DRC
  1481. * slot cache by saving the xdr structure. sl_seqid has been set.
  1482. * Do this for solo or embedded create session operations.
  1483. */
  1484. static void
  1485. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  1486. struct nfsd4_clid_slot *slot, __be32 nfserr)
  1487. {
  1488. slot->sl_status = nfserr;
  1489. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  1490. }
  1491. static __be32
  1492. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  1493. struct nfsd4_clid_slot *slot)
  1494. {
  1495. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  1496. return slot->sl_status;
  1497. }
  1498. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  1499. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  1500. 1 + /* MIN tag is length with zero, only length */ \
  1501. 3 + /* version, opcount, opcode */ \
  1502. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1503. /* seqid, slotID, slotID, cache */ \
  1504. 4 ) * sizeof(__be32))
  1505. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  1506. 2 + /* verifier: AUTH_NULL, length 0 */\
  1507. 1 + /* status */ \
  1508. 1 + /* MIN tag is length with zero, only length */ \
  1509. 3 + /* opcount, opcode, opstatus*/ \
  1510. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1511. /* seqid, slotID, slotID, slotID, status */ \
  1512. 5 ) * sizeof(__be32))
  1513. static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
  1514. {
  1515. return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
  1516. || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
  1517. }
  1518. __be32
  1519. nfsd4_create_session(struct svc_rqst *rqstp,
  1520. struct nfsd4_compound_state *cstate,
  1521. struct nfsd4_create_session *cr_ses)
  1522. {
  1523. struct sockaddr *sa = svc_addr(rqstp);
  1524. struct nfs4_client *conf, *unconf;
  1525. struct nfsd4_session *new;
  1526. struct nfsd4_conn *conn;
  1527. struct nfsd4_clid_slot *cs_slot = NULL;
  1528. __be32 status = 0;
  1529. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1530. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  1531. return nfserr_inval;
  1532. if (check_forechannel_attrs(cr_ses->fore_channel))
  1533. return nfserr_toosmall;
  1534. new = alloc_session(&cr_ses->fore_channel, nn);
  1535. if (!new)
  1536. return nfserr_jukebox;
  1537. status = nfserr_jukebox;
  1538. conn = alloc_conn_from_crses(rqstp, cr_ses);
  1539. if (!conn)
  1540. goto out_free_session;
  1541. nfs4_lock_state();
  1542. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  1543. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  1544. if (conf) {
  1545. cs_slot = &conf->cl_cs_slot;
  1546. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1547. if (status == nfserr_replay_cache) {
  1548. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  1549. goto out_free_conn;
  1550. } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
  1551. status = nfserr_seq_misordered;
  1552. goto out_free_conn;
  1553. }
  1554. } else if (unconf) {
  1555. struct nfs4_client *old;
  1556. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  1557. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  1558. status = nfserr_clid_inuse;
  1559. goto out_free_conn;
  1560. }
  1561. cs_slot = &unconf->cl_cs_slot;
  1562. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1563. if (status) {
  1564. /* an unconfirmed replay returns misordered */
  1565. status = nfserr_seq_misordered;
  1566. goto out_free_conn;
  1567. }
  1568. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1569. if (old)
  1570. expire_client(old);
  1571. move_to_confirmed(unconf);
  1572. conf = unconf;
  1573. } else {
  1574. status = nfserr_stale_clientid;
  1575. goto out_free_conn;
  1576. }
  1577. status = nfs_ok;
  1578. /*
  1579. * We do not support RDMA or persistent sessions
  1580. */
  1581. cr_ses->flags &= ~SESSION4_PERSIST;
  1582. cr_ses->flags &= ~SESSION4_RDMA;
  1583. init_session(rqstp, new, conf, cr_ses);
  1584. nfsd4_init_conn(rqstp, conn, new);
  1585. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  1586. NFS4_MAX_SESSIONID_LEN);
  1587. memcpy(&cr_ses->fore_channel, &new->se_fchannel,
  1588. sizeof(struct nfsd4_channel_attrs));
  1589. cs_slot->sl_seqid++;
  1590. cr_ses->seqid = cs_slot->sl_seqid;
  1591. /* cache solo and embedded create sessions under the state lock */
  1592. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  1593. nfs4_unlock_state();
  1594. out:
  1595. dprintk("%s returns %d\n", __func__, ntohl(status));
  1596. return status;
  1597. out_free_conn:
  1598. nfs4_unlock_state();
  1599. free_conn(conn);
  1600. out_free_session:
  1601. __free_session(new);
  1602. goto out;
  1603. }
  1604. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  1605. {
  1606. switch (*dir) {
  1607. case NFS4_CDFC4_FORE:
  1608. case NFS4_CDFC4_BACK:
  1609. return nfs_ok;
  1610. case NFS4_CDFC4_FORE_OR_BOTH:
  1611. case NFS4_CDFC4_BACK_OR_BOTH:
  1612. *dir = NFS4_CDFC4_BOTH;
  1613. return nfs_ok;
  1614. };
  1615. return nfserr_inval;
  1616. }
  1617. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
  1618. {
  1619. struct nfsd4_session *session = cstate->session;
  1620. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1621. spin_lock(&nn->client_lock);
  1622. session->se_cb_prog = bc->bc_cb_program;
  1623. session->se_cb_sec = bc->bc_cb_sec;
  1624. spin_unlock(&nn->client_lock);
  1625. nfsd4_probe_callback(session->se_client);
  1626. return nfs_ok;
  1627. }
  1628. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  1629. struct nfsd4_compound_state *cstate,
  1630. struct nfsd4_bind_conn_to_session *bcts)
  1631. {
  1632. __be32 status;
  1633. struct nfsd4_conn *conn;
  1634. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1635. if (!nfsd4_last_compound_op(rqstp))
  1636. return nfserr_not_only_op;
  1637. spin_lock(&nn->client_lock);
  1638. cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
  1639. /* Sorta weird: we only need the refcnt'ing because new_conn acquires
  1640. * client_lock iself: */
  1641. if (cstate->session) {
  1642. nfsd4_get_session(cstate->session);
  1643. atomic_inc(&cstate->session->se_client->cl_refcount);
  1644. }
  1645. spin_unlock(&nn->client_lock);
  1646. if (!cstate->session)
  1647. return nfserr_badsession;
  1648. status = nfsd4_map_bcts_dir(&bcts->dir);
  1649. if (status)
  1650. return status;
  1651. conn = alloc_conn(rqstp, bcts->dir);
  1652. if (!conn)
  1653. return nfserr_jukebox;
  1654. nfsd4_init_conn(rqstp, conn, cstate->session);
  1655. return nfs_ok;
  1656. }
  1657. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  1658. {
  1659. if (!session)
  1660. return 0;
  1661. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  1662. }
  1663. __be32
  1664. nfsd4_destroy_session(struct svc_rqst *r,
  1665. struct nfsd4_compound_state *cstate,
  1666. struct nfsd4_destroy_session *sessionid)
  1667. {
  1668. struct nfsd4_session *ses;
  1669. __be32 status = nfserr_badsession;
  1670. struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
  1671. /* Notes:
  1672. * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
  1673. * - Should we return nfserr_back_chan_busy if waiting for
  1674. * callbacks on to-be-destroyed session?
  1675. * - Do we need to clear any callback info from previous session?
  1676. */
  1677. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  1678. if (!nfsd4_last_compound_op(r))
  1679. return nfserr_not_only_op;
  1680. }
  1681. dump_sessionid(__func__, &sessionid->sessionid);
  1682. spin_lock(&nn->client_lock);
  1683. ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
  1684. if (!ses) {
  1685. spin_unlock(&nn->client_lock);
  1686. goto out;
  1687. }
  1688. unhash_session(ses);
  1689. spin_unlock(&nn->client_lock);
  1690. nfs4_lock_state();
  1691. nfsd4_probe_callback_sync(ses->se_client);
  1692. nfs4_unlock_state();
  1693. spin_lock(&nn->client_lock);
  1694. nfsd4_del_conns(ses);
  1695. nfsd4_put_session_locked(ses);
  1696. spin_unlock(&nn->client_lock);
  1697. status = nfs_ok;
  1698. out:
  1699. dprintk("%s returns %d\n", __func__, ntohl(status));
  1700. return status;
  1701. }
  1702. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  1703. {
  1704. struct nfsd4_conn *c;
  1705. list_for_each_entry(c, &s->se_conns, cn_persession) {
  1706. if (c->cn_xprt == xpt) {
  1707. return c;
  1708. }
  1709. }
  1710. return NULL;
  1711. }
  1712. static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  1713. {
  1714. struct nfs4_client *clp = ses->se_client;
  1715. struct nfsd4_conn *c;
  1716. int ret;
  1717. spin_lock(&clp->cl_lock);
  1718. c = __nfsd4_find_conn(new->cn_xprt, ses);
  1719. if (c) {
  1720. spin_unlock(&clp->cl_lock);
  1721. free_conn(new);
  1722. return;
  1723. }
  1724. __nfsd4_hash_conn(new, ses);
  1725. spin_unlock(&clp->cl_lock);
  1726. ret = nfsd4_register_conn(new);
  1727. if (ret)
  1728. /* oops; xprt is already down: */
  1729. nfsd4_conn_lost(&new->cn_xpt_user);
  1730. return;
  1731. }
  1732. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  1733. {
  1734. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1735. return args->opcnt > session->se_fchannel.maxops;
  1736. }
  1737. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  1738. struct nfsd4_session *session)
  1739. {
  1740. struct xdr_buf *xb = &rqstp->rq_arg;
  1741. return xb->len > session->se_fchannel.maxreq_sz;
  1742. }
  1743. __be32
  1744. nfsd4_sequence(struct svc_rqst *rqstp,
  1745. struct nfsd4_compound_state *cstate,
  1746. struct nfsd4_sequence *seq)
  1747. {
  1748. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1749. struct nfsd4_session *session;
  1750. struct nfsd4_slot *slot;
  1751. struct nfsd4_conn *conn;
  1752. __be32 status;
  1753. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1754. if (resp->opcnt != 1)
  1755. return nfserr_sequence_pos;
  1756. /*
  1757. * Will be either used or freed by nfsd4_sequence_check_conn
  1758. * below.
  1759. */
  1760. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  1761. if (!conn)
  1762. return nfserr_jukebox;
  1763. spin_lock(&nn->client_lock);
  1764. status = nfserr_badsession;
  1765. session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
  1766. if (!session)
  1767. goto out;
  1768. status = nfserr_too_many_ops;
  1769. if (nfsd4_session_too_many_ops(rqstp, session))
  1770. goto out;
  1771. status = nfserr_req_too_big;
  1772. if (nfsd4_request_too_big(rqstp, session))
  1773. goto out;
  1774. status = nfserr_badslot;
  1775. if (seq->slotid >= session->se_fchannel.maxreqs)
  1776. goto out;
  1777. slot = session->se_slots[seq->slotid];
  1778. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  1779. /* We do not negotiate the number of slots yet, so set the
  1780. * maxslots to the session maxreqs which is used to encode
  1781. * sr_highest_slotid and the sr_target_slot id to maxslots */
  1782. seq->maxslots = session->se_fchannel.maxreqs;
  1783. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  1784. slot->sl_flags & NFSD4_SLOT_INUSE);
  1785. if (status == nfserr_replay_cache) {
  1786. status = nfserr_seq_misordered;
  1787. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  1788. goto out;
  1789. cstate->slot = slot;
  1790. cstate->session = session;
  1791. /* Return the cached reply status and set cstate->status
  1792. * for nfsd4_proc_compound processing */
  1793. status = nfsd4_replay_cache_entry(resp, seq);
  1794. cstate->status = nfserr_replay_cache;
  1795. goto out;
  1796. }
  1797. if (status)
  1798. goto out;
  1799. nfsd4_sequence_check_conn(conn, session);
  1800. conn = NULL;
  1801. /* Success! bump slot seqid */
  1802. slot->sl_seqid = seq->seqid;
  1803. slot->sl_flags |= NFSD4_SLOT_INUSE;
  1804. if (seq->cachethis)
  1805. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  1806. else
  1807. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  1808. cstate->slot = slot;
  1809. cstate->session = session;
  1810. out:
  1811. /* Hold a session reference until done processing the compound. */
  1812. if (cstate->session) {
  1813. struct nfs4_client *clp = session->se_client;
  1814. nfsd4_get_session(cstate->session);
  1815. atomic_inc(&clp->cl_refcount);
  1816. switch (clp->cl_cb_state) {
  1817. case NFSD4_CB_DOWN:
  1818. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  1819. break;
  1820. case NFSD4_CB_FAULT:
  1821. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  1822. break;
  1823. default:
  1824. seq->status_flags = 0;
  1825. }
  1826. }
  1827. kfree(conn);
  1828. spin_unlock(&nn->client_lock);
  1829. dprintk("%s: return %d\n", __func__, ntohl(status));
  1830. return status;
  1831. }
  1832. __be32
  1833. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  1834. {
  1835. struct nfs4_client *conf, *unconf, *clp;
  1836. __be32 status = 0;
  1837. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1838. nfs4_lock_state();
  1839. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  1840. conf = find_confirmed_client(&dc->clientid, true, nn);
  1841. if (conf) {
  1842. clp = conf;
  1843. if (!is_client_expired(conf) && client_has_state(conf)) {
  1844. status = nfserr_clientid_busy;
  1845. goto out;
  1846. }
  1847. /* rfc5661 18.50.3 */
  1848. if (cstate->session && conf == cstate->session->se_client) {
  1849. status = nfserr_clientid_busy;
  1850. goto out;
  1851. }
  1852. } else if (unconf)
  1853. clp = unconf;
  1854. else {
  1855. status = nfserr_stale_clientid;
  1856. goto out;
  1857. }
  1858. expire_client(clp);
  1859. out:
  1860. nfs4_unlock_state();
  1861. dprintk("%s return %d\n", __func__, ntohl(status));
  1862. return status;
  1863. }
  1864. __be32
  1865. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  1866. {
  1867. __be32 status = 0;
  1868. if (rc->rca_one_fs) {
  1869. if (!cstate->current_fh.fh_dentry)
  1870. return nfserr_nofilehandle;
  1871. /*
  1872. * We don't take advantage of the rca_one_fs case.
  1873. * That's OK, it's optional, we can safely ignore it.
  1874. */
  1875. return nfs_ok;
  1876. }
  1877. nfs4_lock_state();
  1878. status = nfserr_complete_already;
  1879. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  1880. &cstate->session->se_client->cl_flags))
  1881. goto out;
  1882. status = nfserr_stale_clientid;
  1883. if (is_client_expired(cstate->session->se_client))
  1884. /*
  1885. * The following error isn't really legal.
  1886. * But we only get here if the client just explicitly
  1887. * destroyed the client. Surely it no longer cares what
  1888. * error it gets back on an operation for the dead
  1889. * client.
  1890. */
  1891. goto out;
  1892. status = nfs_ok;
  1893. nfsd4_client_record_create(cstate->session->se_client);
  1894. out:
  1895. nfs4_unlock_state();
  1896. return status;
  1897. }
  1898. __be32
  1899. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1900. struct nfsd4_setclientid *setclid)
  1901. {
  1902. struct xdr_netobj clname = setclid->se_name;
  1903. nfs4_verifier clverifier = setclid->se_verf;
  1904. struct nfs4_client *conf, *unconf, *new;
  1905. __be32 status;
  1906. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1907. /* Cases below refer to rfc 3530 section 14.2.33: */
  1908. nfs4_lock_state();
  1909. conf = find_confirmed_client_by_name(&clname, nn);
  1910. if (conf) {
  1911. /* case 0: */
  1912. status = nfserr_clid_inuse;
  1913. if (clp_used_exchangeid(conf))
  1914. goto out;
  1915. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  1916. char addr_str[INET6_ADDRSTRLEN];
  1917. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  1918. sizeof(addr_str));
  1919. dprintk("NFSD: setclientid: string in use by client "
  1920. "at %s\n", addr_str);
  1921. goto out;
  1922. }
  1923. }
  1924. unconf = find_unconfirmed_client_by_name(&clname, nn);
  1925. if (unconf)
  1926. expire_client(unconf);
  1927. status = nfserr_jukebox;
  1928. new = create_client(clname, rqstp, &clverifier);
  1929. if (new == NULL)
  1930. goto out;
  1931. if (conf && same_verf(&conf->cl_verifier, &clverifier))
  1932. /* case 1: probable callback update */
  1933. copy_clid(new, conf);
  1934. else /* case 4 (new client) or cases 2, 3 (client reboot): */
  1935. gen_clid(new, nn);
  1936. new->cl_minorversion = 0;
  1937. gen_callback(new, setclid, rqstp);
  1938. add_to_unconfirmed(new);
  1939. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  1940. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  1941. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  1942. status = nfs_ok;
  1943. out:
  1944. nfs4_unlock_state();
  1945. return status;
  1946. }
  1947. __be32
  1948. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  1949. struct nfsd4_compound_state *cstate,
  1950. struct nfsd4_setclientid_confirm *setclientid_confirm)
  1951. {
  1952. struct nfs4_client *conf, *unconf;
  1953. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  1954. clientid_t * clid = &setclientid_confirm->sc_clientid;
  1955. __be32 status;
  1956. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1957. if (STALE_CLIENTID(clid, nn))
  1958. return nfserr_stale_clientid;
  1959. nfs4_lock_state();
  1960. conf = find_confirmed_client(clid, false, nn);
  1961. unconf = find_unconfirmed_client(clid, false, nn);
  1962. /*
  1963. * We try hard to give out unique clientid's, so if we get an
  1964. * attempt to confirm the same clientid with a different cred,
  1965. * there's a bug somewhere. Let's charitably assume it's our
  1966. * bug.
  1967. */
  1968. status = nfserr_serverfault;
  1969. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
  1970. goto out;
  1971. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
  1972. goto out;
  1973. /* cases below refer to rfc 3530 section 14.2.34: */
  1974. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  1975. if (conf && !unconf) /* case 2: probable retransmit */
  1976. status = nfs_ok;
  1977. else /* case 4: client hasn't noticed we rebooted yet? */
  1978. status = nfserr_stale_clientid;
  1979. goto out;
  1980. }
  1981. status = nfs_ok;
  1982. if (conf) { /* case 1: callback update */
  1983. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  1984. nfsd4_probe_callback(conf);
  1985. expire_client(unconf);
  1986. } else { /* case 3: normal case; new or rebooted client */
  1987. conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1988. if (conf)
  1989. expire_client(conf);
  1990. move_to_confirmed(unconf);
  1991. nfsd4_probe_callback(unconf);
  1992. }
  1993. out:
  1994. nfs4_unlock_state();
  1995. return status;
  1996. }
  1997. static struct nfs4_file *nfsd4_alloc_file(void)
  1998. {
  1999. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  2000. }
  2001. /* OPEN Share state helper functions */
  2002. static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
  2003. {
  2004. unsigned int hashval = file_hashval(ino);
  2005. atomic_set(&fp->fi_ref, 1);
  2006. INIT_LIST_HEAD(&fp->fi_hash);
  2007. INIT_LIST_HEAD(&fp->fi_stateids);
  2008. INIT_LIST_HEAD(&fp->fi_delegations);
  2009. fp->fi_inode = igrab(ino);
  2010. fp->fi_had_conflict = false;
  2011. fp->fi_lease = NULL;
  2012. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2013. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2014. spin_lock(&recall_lock);
  2015. list_add(&fp->fi_hash, &file_hashtbl[hashval]);
  2016. spin_unlock(&recall_lock);
  2017. }
  2018. static void
  2019. nfsd4_free_slab(struct kmem_cache **slab)
  2020. {
  2021. if (*slab == NULL)
  2022. return;
  2023. kmem_cache_destroy(*slab);
  2024. *slab = NULL;
  2025. }
  2026. void
  2027. nfsd4_free_slabs(void)
  2028. {
  2029. nfsd4_free_slab(&openowner_slab);
  2030. nfsd4_free_slab(&lockowner_slab);
  2031. nfsd4_free_slab(&file_slab);
  2032. nfsd4_free_slab(&stateid_slab);
  2033. nfsd4_free_slab(&deleg_slab);
  2034. }
  2035. int
  2036. nfsd4_init_slabs(void)
  2037. {
  2038. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2039. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2040. if (openowner_slab == NULL)
  2041. goto out_nomem;
  2042. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2043. sizeof(struct nfs4_lockowner), 0, 0, NULL);
  2044. if (lockowner_slab == NULL)
  2045. goto out_nomem;
  2046. file_slab = kmem_cache_create("nfsd4_files",
  2047. sizeof(struct nfs4_file), 0, 0, NULL);
  2048. if (file_slab == NULL)
  2049. goto out_nomem;
  2050. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2051. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2052. if (stateid_slab == NULL)
  2053. goto out_nomem;
  2054. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2055. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2056. if (deleg_slab == NULL)
  2057. goto out_nomem;
  2058. return 0;
  2059. out_nomem:
  2060. nfsd4_free_slabs();
  2061. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2062. return -ENOMEM;
  2063. }
  2064. void nfs4_free_openowner(struct nfs4_openowner *oo)
  2065. {
  2066. kfree(oo->oo_owner.so_owner.data);
  2067. kmem_cache_free(openowner_slab, oo);
  2068. }
  2069. void nfs4_free_lockowner(struct nfs4_lockowner *lo)
  2070. {
  2071. kfree(lo->lo_owner.so_owner.data);
  2072. kmem_cache_free(lockowner_slab, lo);
  2073. }
  2074. static void init_nfs4_replay(struct nfs4_replay *rp)
  2075. {
  2076. rp->rp_status = nfserr_serverfault;
  2077. rp->rp_buflen = 0;
  2078. rp->rp_buf = rp->rp_ibuf;
  2079. }
  2080. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2081. {
  2082. struct nfs4_stateowner *sop;
  2083. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2084. if (!sop)
  2085. return NULL;
  2086. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2087. if (!sop->so_owner.data) {
  2088. kmem_cache_free(slab, sop);
  2089. return NULL;
  2090. }
  2091. sop->so_owner.len = owner->len;
  2092. INIT_LIST_HEAD(&sop->so_stateids);
  2093. sop->so_client = clp;
  2094. init_nfs4_replay(&sop->so_replay);
  2095. return sop;
  2096. }
  2097. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  2098. {
  2099. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2100. list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  2101. list_add(&oo->oo_perclient, &clp->cl_openowners);
  2102. }
  2103. static struct nfs4_openowner *
  2104. alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
  2105. struct nfs4_openowner *oo;
  2106. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  2107. if (!oo)
  2108. return NULL;
  2109. oo->oo_owner.so_is_open_owner = 1;
  2110. oo->oo_owner.so_seqid = open->op_seqid;
  2111. oo->oo_flags = NFS4_OO_NEW;
  2112. oo->oo_time = 0;
  2113. oo->oo_last_closed_stid = NULL;
  2114. INIT_LIST_HEAD(&oo->oo_close_lru);
  2115. hash_openowner(oo, clp, strhashval);
  2116. return oo;
  2117. }
  2118. static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  2119. struct nfs4_openowner *oo = open->op_openowner;
  2120. stp->st_stid.sc_type = NFS4_OPEN_STID;
  2121. INIT_LIST_HEAD(&stp->st_lockowners);
  2122. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  2123. list_add(&stp->st_perfile, &fp->fi_stateids);
  2124. stp->st_stateowner = &oo->oo_owner;
  2125. get_nfs4_file(fp);
  2126. stp->st_file = fp;
  2127. stp->st_access_bmap = 0;
  2128. stp->st_deny_bmap = 0;
  2129. set_access(open->op_share_access, stp);
  2130. set_deny(open->op_share_deny, stp);
  2131. stp->st_openstp = NULL;
  2132. }
  2133. static void
  2134. move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
  2135. {
  2136. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2137. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  2138. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  2139. oo->oo_time = get_seconds();
  2140. }
  2141. static int
  2142. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
  2143. clientid_t *clid)
  2144. {
  2145. return (sop->so_owner.len == owner->len) &&
  2146. 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
  2147. (sop->so_client->cl_clientid.cl_id == clid->cl_id);
  2148. }
  2149. static struct nfs4_openowner *
  2150. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
  2151. bool sessions, struct nfsd_net *nn)
  2152. {
  2153. struct nfs4_stateowner *so;
  2154. struct nfs4_openowner *oo;
  2155. struct nfs4_client *clp;
  2156. list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  2157. if (!so->so_is_open_owner)
  2158. continue;
  2159. if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
  2160. oo = openowner(so);
  2161. clp = oo->oo_owner.so_client;
  2162. if ((bool)clp->cl_minorversion != sessions)
  2163. return NULL;
  2164. renew_client(oo->oo_owner.so_client);
  2165. return oo;
  2166. }
  2167. }
  2168. return NULL;
  2169. }
  2170. /* search file_hashtbl[] for file */
  2171. static struct nfs4_file *
  2172. find_file(struct inode *ino)
  2173. {
  2174. unsigned int hashval = file_hashval(ino);
  2175. struct nfs4_file *fp;
  2176. spin_lock(&recall_lock);
  2177. list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  2178. if (fp->fi_inode == ino) {
  2179. get_nfs4_file(fp);
  2180. spin_unlock(&recall_lock);
  2181. return fp;
  2182. }
  2183. }
  2184. spin_unlock(&recall_lock);
  2185. return NULL;
  2186. }
  2187. /*
  2188. * Called to check deny when READ with all zero stateid or
  2189. * WRITE with all zero or all one stateid
  2190. */
  2191. static __be32
  2192. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  2193. {
  2194. struct inode *ino = current_fh->fh_dentry->d_inode;
  2195. struct nfs4_file *fp;
  2196. struct nfs4_ol_stateid *stp;
  2197. __be32 ret;
  2198. dprintk("NFSD: nfs4_share_conflict\n");
  2199. fp = find_file(ino);
  2200. if (!fp)
  2201. return nfs_ok;
  2202. ret = nfserr_locked;
  2203. /* Search for conflicting share reservations */
  2204. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  2205. if (test_deny(deny_type, stp) ||
  2206. test_deny(NFS4_SHARE_DENY_BOTH, stp))
  2207. goto out;
  2208. }
  2209. ret = nfs_ok;
  2210. out:
  2211. put_nfs4_file(fp);
  2212. return ret;
  2213. }
  2214. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  2215. {
  2216. /* We're assuming the state code never drops its reference
  2217. * without first removing the lease. Since we're in this lease
  2218. * callback (and since the lease code is serialized by the kernel
  2219. * lock) we know the server hasn't removed the lease yet, we know
  2220. * it's safe to take a reference: */
  2221. atomic_inc(&dp->dl_count);
  2222. list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
  2223. /* only place dl_time is set. protected by lock_flocks*/
  2224. dp->dl_time = get_seconds();
  2225. nfsd4_cb_recall(dp);
  2226. }
  2227. /* Called from break_lease() with lock_flocks() held. */
  2228. static void nfsd_break_deleg_cb(struct file_lock *fl)
  2229. {
  2230. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  2231. struct nfs4_delegation *dp;
  2232. if (!fp) {
  2233. WARN(1, "(%p)->fl_owner NULL\n", fl);
  2234. return;
  2235. }
  2236. if (fp->fi_had_conflict) {
  2237. WARN(1, "duplicate break on %p\n", fp);
  2238. return;
  2239. }
  2240. /*
  2241. * We don't want the locks code to timeout the lease for us;
  2242. * we'll remove it ourself if a delegation isn't returned
  2243. * in time:
  2244. */
  2245. fl->fl_break_time = 0;
  2246. spin_lock(&recall_lock);
  2247. fp->fi_had_conflict = true;
  2248. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  2249. nfsd_break_one_deleg(dp);
  2250. spin_unlock(&recall_lock);
  2251. }
  2252. static
  2253. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  2254. {
  2255. if (arg & F_UNLCK)
  2256. return lease_modify(onlist, arg);
  2257. else
  2258. return -EAGAIN;
  2259. }
  2260. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  2261. .lm_break = nfsd_break_deleg_cb,
  2262. .lm_change = nfsd_change_deleg_cb,
  2263. };
  2264. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  2265. {
  2266. if (nfsd4_has_session(cstate))
  2267. return nfs_ok;
  2268. if (seqid == so->so_seqid - 1)
  2269. return nfserr_replay_me;
  2270. if (seqid == so->so_seqid)
  2271. return nfs_ok;
  2272. return nfserr_bad_seqid;
  2273. }
  2274. __be32
  2275. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  2276. struct nfsd4_open *open, struct nfsd_net *nn)
  2277. {
  2278. clientid_t *clientid = &open->op_clientid;
  2279. struct nfs4_client *clp = NULL;
  2280. unsigned int strhashval;
  2281. struct nfs4_openowner *oo = NULL;
  2282. __be32 status;
  2283. if (STALE_CLIENTID(&open->op_clientid, nn))
  2284. return nfserr_stale_clientid;
  2285. /*
  2286. * In case we need it later, after we've already created the
  2287. * file and don't want to risk a further failure:
  2288. */
  2289. open->op_file = nfsd4_alloc_file();
  2290. if (open->op_file == NULL)
  2291. return nfserr_jukebox;
  2292. strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
  2293. oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
  2294. open->op_openowner = oo;
  2295. if (!oo) {
  2296. clp = find_confirmed_client(clientid, cstate->minorversion,
  2297. nn);
  2298. if (clp == NULL)
  2299. return nfserr_expired;
  2300. goto new_owner;
  2301. }
  2302. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  2303. /* Replace unconfirmed owners without checking for replay. */
  2304. clp = oo->oo_owner.so_client;
  2305. release_openowner(oo);
  2306. open->op_openowner = NULL;
  2307. goto new_owner;
  2308. }
  2309. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  2310. if (status)
  2311. return status;
  2312. clp = oo->oo_owner.so_client;
  2313. goto alloc_stateid;
  2314. new_owner:
  2315. oo = alloc_init_open_stateowner(strhashval, clp, open);
  2316. if (oo == NULL)
  2317. return nfserr_jukebox;
  2318. open->op_openowner = oo;
  2319. alloc_stateid:
  2320. open->op_stp = nfs4_alloc_stateid(clp);
  2321. if (!open->op_stp)
  2322. return nfserr_jukebox;
  2323. return nfs_ok;
  2324. }
  2325. static inline __be32
  2326. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  2327. {
  2328. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  2329. return nfserr_openmode;
  2330. else
  2331. return nfs_ok;
  2332. }
  2333. static int share_access_to_flags(u32 share_access)
  2334. {
  2335. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  2336. }
  2337. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  2338. {
  2339. struct nfs4_stid *ret;
  2340. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  2341. if (!ret)
  2342. return NULL;
  2343. return delegstateid(ret);
  2344. }
  2345. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  2346. {
  2347. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  2348. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  2349. }
  2350. static __be32
  2351. nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
  2352. struct nfs4_delegation **dp)
  2353. {
  2354. int flags;
  2355. __be32 status = nfserr_bad_stateid;
  2356. *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
  2357. if (*dp == NULL)
  2358. goto out;
  2359. flags = share_access_to_flags(open->op_share_access);
  2360. status = nfs4_check_delegmode(*dp, flags);
  2361. if (status)
  2362. *dp = NULL;
  2363. out:
  2364. if (!nfsd4_is_deleg_cur(open))
  2365. return nfs_ok;
  2366. if (status)
  2367. return status;
  2368. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2369. return nfs_ok;
  2370. }
  2371. static __be32
  2372. nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
  2373. {
  2374. struct nfs4_ol_stateid *local;
  2375. struct nfs4_openowner *oo = open->op_openowner;
  2376. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  2377. /* ignore lock owners */
  2378. if (local->st_stateowner->so_is_open_owner == 0)
  2379. continue;
  2380. /* remember if we have seen this open owner */
  2381. if (local->st_stateowner == &oo->oo_owner)
  2382. *stpp = local;
  2383. /* check for conflicting share reservations */
  2384. if (!test_share(local, open))
  2385. return nfserr_share_denied;
  2386. }
  2387. return nfs_ok;
  2388. }
  2389. static inline int nfs4_access_to_access(u32 nfs4_access)
  2390. {
  2391. int flags = 0;
  2392. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  2393. flags |= NFSD_MAY_READ;
  2394. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  2395. flags |= NFSD_MAY_WRITE;
  2396. return flags;
  2397. }
  2398. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  2399. struct svc_fh *cur_fh, struct nfsd4_open *open)
  2400. {
  2401. __be32 status;
  2402. int oflag = nfs4_access_to_omode(open->op_share_access);
  2403. int access = nfs4_access_to_access(open->op_share_access);
  2404. if (!fp->fi_fds[oflag]) {
  2405. status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
  2406. &fp->fi_fds[oflag]);
  2407. if (status)
  2408. return status;
  2409. }
  2410. nfs4_file_get_access(fp, oflag);
  2411. return nfs_ok;
  2412. }
  2413. static inline __be32
  2414. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  2415. struct nfsd4_open *open)
  2416. {
  2417. struct iattr iattr = {
  2418. .ia_valid = ATTR_SIZE,
  2419. .ia_size = 0,
  2420. };
  2421. if (!open->op_truncate)
  2422. return 0;
  2423. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  2424. return nfserr_inval;
  2425. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  2426. }
  2427. static __be32
  2428. 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)
  2429. {
  2430. u32 op_share_access = open->op_share_access;
  2431. bool new_access;
  2432. __be32 status;
  2433. new_access = !test_access(op_share_access, stp);
  2434. if (new_access) {
  2435. status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
  2436. if (status)
  2437. return status;
  2438. }
  2439. status = nfsd4_truncate(rqstp, cur_fh, open);
  2440. if (status) {
  2441. if (new_access) {
  2442. int oflag = nfs4_access_to_omode(op_share_access);
  2443. nfs4_file_put_access(fp, oflag);
  2444. }
  2445. return status;
  2446. }
  2447. /* remember the open */
  2448. set_access(op_share_access, stp);
  2449. set_deny(open->op_share_deny, stp);
  2450. return nfs_ok;
  2451. }
  2452. static void
  2453. nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
  2454. {
  2455. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2456. }
  2457. /* Should we give out recallable state?: */
  2458. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  2459. {
  2460. if (clp->cl_cb_state == NFSD4_CB_UP)
  2461. return true;
  2462. /*
  2463. * In the sessions case, since we don't have to establish a
  2464. * separate connection for callbacks, we assume it's OK
  2465. * until we hear otherwise:
  2466. */
  2467. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  2468. }
  2469. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
  2470. {
  2471. struct file_lock *fl;
  2472. fl = locks_alloc_lock();
  2473. if (!fl)
  2474. return NULL;
  2475. locks_init_lock(fl);
  2476. fl->fl_lmops = &nfsd_lease_mng_ops;
  2477. fl->fl_flags = FL_LEASE;
  2478. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  2479. fl->fl_end = OFFSET_MAX;
  2480. fl->fl_owner = (fl_owner_t)(dp->dl_file);
  2481. fl->fl_pid = current->tgid;
  2482. return fl;
  2483. }
  2484. static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
  2485. {
  2486. struct nfs4_file *fp = dp->dl_file;
  2487. struct file_lock *fl;
  2488. int status;
  2489. fl = nfs4_alloc_init_lease(dp, flag);
  2490. if (!fl)
  2491. return -ENOMEM;
  2492. fl->fl_file = find_readable_file(fp);
  2493. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2494. status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
  2495. if (status) {
  2496. list_del_init(&dp->dl_perclnt);
  2497. locks_free_lock(fl);
  2498. return -ENOMEM;
  2499. }
  2500. fp->fi_lease = fl;
  2501. fp->fi_deleg_file = get_file(fl->fl_file);
  2502. atomic_set(&fp->fi_delegees, 1);
  2503. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2504. return 0;
  2505. }
  2506. static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
  2507. {
  2508. struct nfs4_file *fp = dp->dl_file;
  2509. if (!fp->fi_lease)
  2510. return nfs4_setlease(dp, flag);
  2511. spin_lock(&recall_lock);
  2512. if (fp->fi_had_conflict) {
  2513. spin_unlock(&recall_lock);
  2514. return -EAGAIN;
  2515. }
  2516. atomic_inc(&fp->fi_delegees);
  2517. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2518. spin_unlock(&recall_lock);
  2519. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2520. return 0;
  2521. }
  2522. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  2523. {
  2524. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2525. if (status == -EAGAIN)
  2526. open->op_why_no_deleg = WND4_CONTENTION;
  2527. else {
  2528. open->op_why_no_deleg = WND4_RESOURCE;
  2529. switch (open->op_deleg_want) {
  2530. case NFS4_SHARE_WANT_READ_DELEG:
  2531. case NFS4_SHARE_WANT_WRITE_DELEG:
  2532. case NFS4_SHARE_WANT_ANY_DELEG:
  2533. break;
  2534. case NFS4_SHARE_WANT_CANCEL:
  2535. open->op_why_no_deleg = WND4_CANCELLED;
  2536. break;
  2537. case NFS4_SHARE_WANT_NO_DELEG:
  2538. WARN_ON_ONCE(1);
  2539. }
  2540. }
  2541. }
  2542. /*
  2543. * Attempt to hand out a delegation.
  2544. */
  2545. static void
  2546. nfs4_open_delegation(struct net *net, struct svc_fh *fh,
  2547. struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
  2548. {
  2549. struct nfs4_delegation *dp;
  2550. struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
  2551. int cb_up;
  2552. int status = 0, flag = 0;
  2553. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  2554. flag = NFS4_OPEN_DELEGATE_NONE;
  2555. open->op_recall = 0;
  2556. switch (open->op_claim_type) {
  2557. case NFS4_OPEN_CLAIM_PREVIOUS:
  2558. if (!cb_up)
  2559. open->op_recall = 1;
  2560. flag = open->op_delegate_type;
  2561. if (flag == NFS4_OPEN_DELEGATE_NONE)
  2562. goto out;
  2563. break;
  2564. case NFS4_OPEN_CLAIM_NULL:
  2565. /* Let's not give out any delegations till everyone's
  2566. * had the chance to reclaim theirs.... */
  2567. if (locks_in_grace(net))
  2568. goto out;
  2569. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  2570. goto out;
  2571. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  2572. flag = NFS4_OPEN_DELEGATE_WRITE;
  2573. else
  2574. flag = NFS4_OPEN_DELEGATE_READ;
  2575. break;
  2576. default:
  2577. goto out;
  2578. }
  2579. dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
  2580. if (dp == NULL)
  2581. goto out_no_deleg;
  2582. status = nfs4_set_delegation(dp, flag);
  2583. if (status)
  2584. goto out_free;
  2585. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  2586. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  2587. STATEID_VAL(&dp->dl_stid.sc_stateid));
  2588. out:
  2589. open->op_delegate_type = flag;
  2590. if (flag == NFS4_OPEN_DELEGATE_NONE) {
  2591. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  2592. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
  2593. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  2594. /* 4.1 client asking for a delegation? */
  2595. if (open->op_deleg_want)
  2596. nfsd4_open_deleg_none_ext(open, status);
  2597. }
  2598. return;
  2599. out_free:
  2600. unhash_stid(&dp->dl_stid);
  2601. nfs4_put_delegation(dp);
  2602. out_no_deleg:
  2603. flag = NFS4_OPEN_DELEGATE_NONE;
  2604. goto out;
  2605. }
  2606. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  2607. struct nfs4_delegation *dp)
  2608. {
  2609. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  2610. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2611. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2612. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  2613. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  2614. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2615. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2616. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  2617. }
  2618. /* Otherwise the client must be confused wanting a delegation
  2619. * it already has, therefore we don't return
  2620. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  2621. */
  2622. }
  2623. /*
  2624. * called with nfs4_lock_state() held.
  2625. */
  2626. __be32
  2627. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  2628. {
  2629. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2630. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  2631. struct nfs4_file *fp = NULL;
  2632. struct inode *ino = current_fh->fh_dentry->d_inode;
  2633. struct nfs4_ol_stateid *stp = NULL;
  2634. struct nfs4_delegation *dp = NULL;
  2635. __be32 status;
  2636. /*
  2637. * Lookup file; if found, lookup stateid and check open request,
  2638. * and check for delegations in the process of being recalled.
  2639. * If not found, create the nfs4_file struct
  2640. */
  2641. fp = find_file(ino);
  2642. if (fp) {
  2643. if ((status = nfs4_check_open(fp, open, &stp)))
  2644. goto out;
  2645. status = nfs4_check_deleg(cl, fp, open, &dp);
  2646. if (status)
  2647. goto out;
  2648. } else {
  2649. status = nfserr_bad_stateid;
  2650. if (nfsd4_is_deleg_cur(open))
  2651. goto out;
  2652. status = nfserr_jukebox;
  2653. fp = open->op_file;
  2654. open->op_file = NULL;
  2655. nfsd4_init_file(fp, ino);
  2656. }
  2657. /*
  2658. * OPEN the file, or upgrade an existing OPEN.
  2659. * If truncate fails, the OPEN fails.
  2660. */
  2661. if (stp) {
  2662. /* Stateid was found, this is an OPEN upgrade */
  2663. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  2664. if (status)
  2665. goto out;
  2666. } else {
  2667. status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
  2668. if (status)
  2669. goto out;
  2670. status = nfsd4_truncate(rqstp, current_fh, open);
  2671. if (status)
  2672. goto out;
  2673. stp = open->op_stp;
  2674. open->op_stp = NULL;
  2675. init_open_stateid(stp, fp, open);
  2676. }
  2677. update_stateid(&stp->st_stid.sc_stateid);
  2678. memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  2679. if (nfsd4_has_session(&resp->cstate)) {
  2680. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2681. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  2682. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2683. open->op_why_no_deleg = WND4_NOT_WANTED;
  2684. goto nodeleg;
  2685. }
  2686. }
  2687. /*
  2688. * Attempt to hand out a delegation. No error return, because the
  2689. * OPEN succeeds even if we fail.
  2690. */
  2691. nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
  2692. nodeleg:
  2693. status = nfs_ok;
  2694. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  2695. STATEID_VAL(&stp->st_stid.sc_stateid));
  2696. out:
  2697. /* 4.1 client trying to upgrade/downgrade delegation? */
  2698. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  2699. open->op_deleg_want)
  2700. nfsd4_deleg_xgrade_none_ext(open, dp);
  2701. if (fp)
  2702. put_nfs4_file(fp);
  2703. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  2704. nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
  2705. /*
  2706. * To finish the open response, we just need to set the rflags.
  2707. */
  2708. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  2709. if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
  2710. !nfsd4_has_session(&resp->cstate))
  2711. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  2712. return status;
  2713. }
  2714. void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
  2715. {
  2716. if (open->op_openowner) {
  2717. struct nfs4_openowner *oo = open->op_openowner;
  2718. if (!list_empty(&oo->oo_owner.so_stateids))
  2719. list_del_init(&oo->oo_close_lru);
  2720. if (oo->oo_flags & NFS4_OO_NEW) {
  2721. if (status) {
  2722. release_openowner(oo);
  2723. open->op_openowner = NULL;
  2724. } else
  2725. oo->oo_flags &= ~NFS4_OO_NEW;
  2726. }
  2727. }
  2728. if (open->op_file)
  2729. nfsd4_free_file(open->op_file);
  2730. if (open->op_stp)
  2731. free_generic_stateid(open->op_stp);
  2732. }
  2733. static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
  2734. {
  2735. struct nfs4_client *found;
  2736. if (STALE_CLIENTID(clid, nn))
  2737. return nfserr_stale_clientid;
  2738. found = find_confirmed_client(clid, session, nn);
  2739. if (clp)
  2740. *clp = found;
  2741. return found ? nfs_ok : nfserr_expired;
  2742. }
  2743. __be32
  2744. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2745. clientid_t *clid)
  2746. {
  2747. struct nfs4_client *clp;
  2748. __be32 status;
  2749. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2750. nfs4_lock_state();
  2751. dprintk("process_renew(%08x/%08x): starting\n",
  2752. clid->cl_boot, clid->cl_id);
  2753. status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
  2754. if (status)
  2755. goto out;
  2756. status = nfserr_cb_path_down;
  2757. if (!list_empty(&clp->cl_delegations)
  2758. && clp->cl_cb_state != NFSD4_CB_UP)
  2759. goto out;
  2760. status = nfs_ok;
  2761. out:
  2762. nfs4_unlock_state();
  2763. return status;
  2764. }
  2765. static void
  2766. nfsd4_end_grace(struct nfsd_net *nn)
  2767. {
  2768. /* do nothing if grace period already ended */
  2769. if (nn->grace_ended)
  2770. return;
  2771. dprintk("NFSD: end of grace period\n");
  2772. nn->grace_ended = true;
  2773. nfsd4_record_grace_done(nn, nn->boot_time);
  2774. locks_end_grace(&nn->nfsd4_manager);
  2775. /*
  2776. * Now that every NFSv4 client has had the chance to recover and
  2777. * to see the (possibly new, possibly shorter) lease time, we
  2778. * can safely set the next grace time to the current lease time:
  2779. */
  2780. nn->nfsd4_grace = nn->nfsd4_lease;
  2781. }
  2782. static time_t
  2783. nfs4_laundromat(struct nfsd_net *nn)
  2784. {
  2785. struct nfs4_client *clp;
  2786. struct nfs4_openowner *oo;
  2787. struct nfs4_delegation *dp;
  2788. struct list_head *pos, *next, reaplist;
  2789. time_t cutoff = get_seconds() - nn->nfsd4_lease;
  2790. time_t t, clientid_val = nn->nfsd4_lease;
  2791. time_t u, test_val = nn->nfsd4_lease;
  2792. nfs4_lock_state();
  2793. dprintk("NFSD: laundromat service - starting\n");
  2794. nfsd4_end_grace(nn);
  2795. INIT_LIST_HEAD(&reaplist);
  2796. spin_lock(&nn->client_lock);
  2797. list_for_each_safe(pos, next, &nn->client_lru) {
  2798. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2799. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  2800. t = clp->cl_time - cutoff;
  2801. if (clientid_val > t)
  2802. clientid_val = t;
  2803. break;
  2804. }
  2805. if (atomic_read(&clp->cl_refcount)) {
  2806. dprintk("NFSD: client in use (clientid %08x)\n",
  2807. clp->cl_clientid.cl_id);
  2808. continue;
  2809. }
  2810. unhash_client_locked(clp);
  2811. list_add(&clp->cl_lru, &reaplist);
  2812. }
  2813. spin_unlock(&nn->client_lock);
  2814. list_for_each_safe(pos, next, &reaplist) {
  2815. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2816. dprintk("NFSD: purging unused client (clientid %08x)\n",
  2817. clp->cl_clientid.cl_id);
  2818. expire_client(clp);
  2819. }
  2820. spin_lock(&recall_lock);
  2821. list_for_each_safe(pos, next, &del_recall_lru) {
  2822. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2823. if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
  2824. continue;
  2825. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  2826. u = dp->dl_time - cutoff;
  2827. if (test_val > u)
  2828. test_val = u;
  2829. break;
  2830. }
  2831. list_move(&dp->dl_recall_lru, &reaplist);
  2832. }
  2833. spin_unlock(&recall_lock);
  2834. list_for_each_safe(pos, next, &reaplist) {
  2835. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2836. unhash_delegation(dp);
  2837. }
  2838. test_val = nn->nfsd4_lease;
  2839. list_for_each_safe(pos, next, &nn->close_lru) {
  2840. oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
  2841. if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
  2842. u = oo->oo_time - cutoff;
  2843. if (test_val > u)
  2844. test_val = u;
  2845. break;
  2846. }
  2847. release_openowner(oo);
  2848. }
  2849. if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
  2850. clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
  2851. nfs4_unlock_state();
  2852. return clientid_val;
  2853. }
  2854. static struct workqueue_struct *laundry_wq;
  2855. static void laundromat_main(struct work_struct *);
  2856. static void
  2857. laundromat_main(struct work_struct *laundry)
  2858. {
  2859. time_t t;
  2860. struct delayed_work *dwork = container_of(laundry, struct delayed_work,
  2861. work);
  2862. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  2863. laundromat_work);
  2864. t = nfs4_laundromat(nn);
  2865. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  2866. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  2867. }
  2868. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
  2869. {
  2870. if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
  2871. return nfserr_bad_stateid;
  2872. return nfs_ok;
  2873. }
  2874. static int
  2875. STALE_STATEID(stateid_t *stateid, struct nfsd_net *nn)
  2876. {
  2877. if (stateid->si_opaque.so_clid.cl_boot == nn->boot_time)
  2878. return 0;
  2879. dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
  2880. STATEID_VAL(stateid));
  2881. return 1;
  2882. }
  2883. static inline int
  2884. access_permit_read(struct nfs4_ol_stateid *stp)
  2885. {
  2886. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  2887. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  2888. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  2889. }
  2890. static inline int
  2891. access_permit_write(struct nfs4_ol_stateid *stp)
  2892. {
  2893. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  2894. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  2895. }
  2896. static
  2897. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  2898. {
  2899. __be32 status = nfserr_openmode;
  2900. /* For lock stateid's, we test the parent open, not the lock: */
  2901. if (stp->st_openstp)
  2902. stp = stp->st_openstp;
  2903. if ((flags & WR_STATE) && !access_permit_write(stp))
  2904. goto out;
  2905. if ((flags & RD_STATE) && !access_permit_read(stp))
  2906. goto out;
  2907. status = nfs_ok;
  2908. out:
  2909. return status;
  2910. }
  2911. static inline __be32
  2912. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  2913. {
  2914. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  2915. return nfs_ok;
  2916. else if (locks_in_grace(net)) {
  2917. /* Answer in remaining cases depends on existence of
  2918. * conflicting state; so we must wait out the grace period. */
  2919. return nfserr_grace;
  2920. } else if (flags & WR_STATE)
  2921. return nfs4_share_conflict(current_fh,
  2922. NFS4_SHARE_DENY_WRITE);
  2923. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  2924. return nfs4_share_conflict(current_fh,
  2925. NFS4_SHARE_DENY_READ);
  2926. }
  2927. /*
  2928. * Allow READ/WRITE during grace period on recovered state only for files
  2929. * that are not able to provide mandatory locking.
  2930. */
  2931. static inline int
  2932. grace_disallows_io(struct net *net, struct inode *inode)
  2933. {
  2934. return locks_in_grace(net) && mandatory_lock(inode);
  2935. }
  2936. /* Returns true iff a is later than b: */
  2937. static bool stateid_generation_after(stateid_t *a, stateid_t *b)
  2938. {
  2939. return (s32)a->si_generation - (s32)b->si_generation > 0;
  2940. }
  2941. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  2942. {
  2943. /*
  2944. * When sessions are used the stateid generation number is ignored
  2945. * when it is zero.
  2946. */
  2947. if (has_session && in->si_generation == 0)
  2948. return nfs_ok;
  2949. if (in->si_generation == ref->si_generation)
  2950. return nfs_ok;
  2951. /* If the client sends us a stateid from the future, it's buggy: */
  2952. if (stateid_generation_after(in, ref))
  2953. return nfserr_bad_stateid;
  2954. /*
  2955. * However, we could see a stateid from the past, even from a
  2956. * non-buggy client. For example, if the client sends a lock
  2957. * while some IO is outstanding, the lock may bump si_generation
  2958. * while the IO is still in flight. The client could avoid that
  2959. * situation by waiting for responses on all the IO requests,
  2960. * but better performance may result in retrying IO that
  2961. * receives an old_stateid error if requests are rarely
  2962. * reordered in flight:
  2963. */
  2964. return nfserr_old_stateid;
  2965. }
  2966. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  2967. {
  2968. struct nfs4_stid *s;
  2969. struct nfs4_ol_stateid *ols;
  2970. __be32 status;
  2971. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  2972. return nfserr_bad_stateid;
  2973. /* Client debugging aid. */
  2974. if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
  2975. char addr_str[INET6_ADDRSTRLEN];
  2976. rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
  2977. sizeof(addr_str));
  2978. pr_warn_ratelimited("NFSD: client %s testing state ID "
  2979. "with incorrect client ID\n", addr_str);
  2980. return nfserr_bad_stateid;
  2981. }
  2982. s = find_stateid(cl, stateid);
  2983. if (!s)
  2984. return nfserr_bad_stateid;
  2985. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  2986. if (status)
  2987. return status;
  2988. if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
  2989. return nfs_ok;
  2990. ols = openlockstateid(s);
  2991. if (ols->st_stateowner->so_is_open_owner
  2992. && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  2993. return nfserr_bad_stateid;
  2994. return nfs_ok;
  2995. }
  2996. static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
  2997. struct nfs4_stid **s, bool sessions,
  2998. struct nfsd_net *nn)
  2999. {
  3000. struct nfs4_client *cl;
  3001. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3002. return nfserr_bad_stateid;
  3003. if (STALE_STATEID(stateid, nn))
  3004. return nfserr_stale_stateid;
  3005. cl = find_confirmed_client(&stateid->si_opaque.so_clid, sessions, nn);
  3006. if (!cl)
  3007. return nfserr_expired;
  3008. *s = find_stateid_by_type(cl, stateid, typemask);
  3009. if (!*s)
  3010. return nfserr_bad_stateid;
  3011. return nfs_ok;
  3012. }
  3013. /*
  3014. * Checks for stateid operations
  3015. */
  3016. __be32
  3017. nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
  3018. stateid_t *stateid, int flags, struct file **filpp)
  3019. {
  3020. struct nfs4_stid *s;
  3021. struct nfs4_ol_stateid *stp = NULL;
  3022. struct nfs4_delegation *dp = NULL;
  3023. struct svc_fh *current_fh = &cstate->current_fh;
  3024. struct inode *ino = current_fh->fh_dentry->d_inode;
  3025. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3026. __be32 status;
  3027. if (filpp)
  3028. *filpp = NULL;
  3029. if (grace_disallows_io(net, ino))
  3030. return nfserr_grace;
  3031. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3032. return check_special_stateids(net, current_fh, stateid, flags);
  3033. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
  3034. &s, cstate->minorversion, nn);
  3035. if (status)
  3036. return status;
  3037. status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
  3038. if (status)
  3039. goto out;
  3040. switch (s->sc_type) {
  3041. case NFS4_DELEG_STID:
  3042. dp = delegstateid(s);
  3043. status = nfs4_check_delegmode(dp, flags);
  3044. if (status)
  3045. goto out;
  3046. if (filpp) {
  3047. *filpp = dp->dl_file->fi_deleg_file;
  3048. if (!*filpp) {
  3049. WARN_ON_ONCE(1);
  3050. status = nfserr_serverfault;
  3051. goto out;
  3052. }
  3053. }
  3054. break;
  3055. case NFS4_OPEN_STID:
  3056. case NFS4_LOCK_STID:
  3057. stp = openlockstateid(s);
  3058. status = nfs4_check_fh(current_fh, stp);
  3059. if (status)
  3060. goto out;
  3061. if (stp->st_stateowner->so_is_open_owner
  3062. && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3063. goto out;
  3064. status = nfs4_check_openmode(stp, flags);
  3065. if (status)
  3066. goto out;
  3067. if (filpp) {
  3068. if (flags & RD_STATE)
  3069. *filpp = find_readable_file(stp->st_file);
  3070. else
  3071. *filpp = find_writeable_file(stp->st_file);
  3072. }
  3073. break;
  3074. default:
  3075. return nfserr_bad_stateid;
  3076. }
  3077. status = nfs_ok;
  3078. out:
  3079. return status;
  3080. }
  3081. static __be32
  3082. nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
  3083. {
  3084. if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
  3085. return nfserr_locks_held;
  3086. release_lock_stateid(stp);
  3087. return nfs_ok;
  3088. }
  3089. /*
  3090. * Test if the stateid is valid
  3091. */
  3092. __be32
  3093. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3094. struct nfsd4_test_stateid *test_stateid)
  3095. {
  3096. struct nfsd4_test_stateid_id *stateid;
  3097. struct nfs4_client *cl = cstate->session->se_client;
  3098. nfs4_lock_state();
  3099. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  3100. stateid->ts_id_status =
  3101. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  3102. nfs4_unlock_state();
  3103. return nfs_ok;
  3104. }
  3105. __be32
  3106. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3107. struct nfsd4_free_stateid *free_stateid)
  3108. {
  3109. stateid_t *stateid = &free_stateid->fr_stateid;
  3110. struct nfs4_stid *s;
  3111. struct nfs4_client *cl = cstate->session->se_client;
  3112. __be32 ret = nfserr_bad_stateid;
  3113. nfs4_lock_state();
  3114. s = find_stateid(cl, stateid);
  3115. if (!s)
  3116. goto out;
  3117. switch (s->sc_type) {
  3118. case NFS4_DELEG_STID:
  3119. ret = nfserr_locks_held;
  3120. goto out;
  3121. case NFS4_OPEN_STID:
  3122. case NFS4_LOCK_STID:
  3123. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3124. if (ret)
  3125. goto out;
  3126. if (s->sc_type == NFS4_LOCK_STID)
  3127. ret = nfsd4_free_lock_stateid(openlockstateid(s));
  3128. else
  3129. ret = nfserr_locks_held;
  3130. break;
  3131. default:
  3132. ret = nfserr_bad_stateid;
  3133. }
  3134. out:
  3135. nfs4_unlock_state();
  3136. return ret;
  3137. }
  3138. static inline int
  3139. setlkflg (int type)
  3140. {
  3141. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  3142. RD_STATE : WR_STATE;
  3143. }
  3144. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  3145. {
  3146. struct svc_fh *current_fh = &cstate->current_fh;
  3147. struct nfs4_stateowner *sop = stp->st_stateowner;
  3148. __be32 status;
  3149. status = nfsd4_check_seqid(cstate, sop, seqid);
  3150. if (status)
  3151. return status;
  3152. if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
  3153. /*
  3154. * "Closed" stateid's exist *only* to return
  3155. * nfserr_replay_me from the previous step.
  3156. */
  3157. return nfserr_bad_stateid;
  3158. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  3159. if (status)
  3160. return status;
  3161. return nfs4_check_fh(current_fh, stp);
  3162. }
  3163. /*
  3164. * Checks for sequence id mutating operations.
  3165. */
  3166. static __be32
  3167. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3168. stateid_t *stateid, char typemask,
  3169. struct nfs4_ol_stateid **stpp,
  3170. struct nfsd_net *nn)
  3171. {
  3172. __be32 status;
  3173. struct nfs4_stid *s;
  3174. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  3175. seqid, STATEID_VAL(stateid));
  3176. *stpp = NULL;
  3177. status = nfsd4_lookup_stateid(stateid, typemask, &s,
  3178. cstate->minorversion, nn);
  3179. if (status)
  3180. return status;
  3181. *stpp = openlockstateid(s);
  3182. cstate->replay_owner = (*stpp)->st_stateowner;
  3183. return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
  3184. }
  3185. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3186. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  3187. {
  3188. __be32 status;
  3189. struct nfs4_openowner *oo;
  3190. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  3191. NFS4_OPEN_STID, stpp, nn);
  3192. if (status)
  3193. return status;
  3194. oo = openowner((*stpp)->st_stateowner);
  3195. if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
  3196. return nfserr_bad_stateid;
  3197. return nfs_ok;
  3198. }
  3199. __be32
  3200. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3201. struct nfsd4_open_confirm *oc)
  3202. {
  3203. __be32 status;
  3204. struct nfs4_openowner *oo;
  3205. struct nfs4_ol_stateid *stp;
  3206. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3207. dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
  3208. (int)cstate->current_fh.fh_dentry->d_name.len,
  3209. cstate->current_fh.fh_dentry->d_name.name);
  3210. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  3211. if (status)
  3212. return status;
  3213. nfs4_lock_state();
  3214. status = nfs4_preprocess_seqid_op(cstate,
  3215. oc->oc_seqid, &oc->oc_req_stateid,
  3216. NFS4_OPEN_STID, &stp, nn);
  3217. if (status)
  3218. goto out;
  3219. oo = openowner(stp->st_stateowner);
  3220. status = nfserr_bad_stateid;
  3221. if (oo->oo_flags & NFS4_OO_CONFIRMED)
  3222. goto out;
  3223. oo->oo_flags |= NFS4_OO_CONFIRMED;
  3224. update_stateid(&stp->st_stid.sc_stateid);
  3225. memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3226. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  3227. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  3228. nfsd4_client_record_create(oo->oo_owner.so_client);
  3229. status = nfs_ok;
  3230. out:
  3231. if (!cstate->replay_owner)
  3232. nfs4_unlock_state();
  3233. return status;
  3234. }
  3235. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  3236. {
  3237. if (!test_access(access, stp))
  3238. return;
  3239. nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
  3240. clear_access(access, stp);
  3241. }
  3242. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  3243. {
  3244. switch (to_access) {
  3245. case NFS4_SHARE_ACCESS_READ:
  3246. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  3247. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3248. break;
  3249. case NFS4_SHARE_ACCESS_WRITE:
  3250. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  3251. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3252. break;
  3253. case NFS4_SHARE_ACCESS_BOTH:
  3254. break;
  3255. default:
  3256. WARN_ON_ONCE(1);
  3257. }
  3258. }
  3259. static void
  3260. reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
  3261. {
  3262. int i;
  3263. for (i = 0; i < 4; i++) {
  3264. if ((i & deny) != i)
  3265. clear_deny(i, stp);
  3266. }
  3267. }
  3268. __be32
  3269. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  3270. struct nfsd4_compound_state *cstate,
  3271. struct nfsd4_open_downgrade *od)
  3272. {
  3273. __be32 status;
  3274. struct nfs4_ol_stateid *stp;
  3275. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3276. dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
  3277. (int)cstate->current_fh.fh_dentry->d_name.len,
  3278. cstate->current_fh.fh_dentry->d_name.name);
  3279. /* We don't yet support WANT bits: */
  3280. if (od->od_deleg_want)
  3281. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  3282. od->od_deleg_want);
  3283. nfs4_lock_state();
  3284. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  3285. &od->od_stateid, &stp, nn);
  3286. if (status)
  3287. goto out;
  3288. status = nfserr_inval;
  3289. if (!test_access(od->od_share_access, stp)) {
  3290. dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
  3291. stp->st_access_bmap, od->od_share_access);
  3292. goto out;
  3293. }
  3294. if (!test_deny(od->od_share_deny, stp)) {
  3295. dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
  3296. stp->st_deny_bmap, od->od_share_deny);
  3297. goto out;
  3298. }
  3299. nfs4_stateid_downgrade(stp, od->od_share_access);
  3300. reset_union_bmap_deny(od->od_share_deny, stp);
  3301. update_stateid(&stp->st_stid.sc_stateid);
  3302. memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3303. status = nfs_ok;
  3304. out:
  3305. if (!cstate->replay_owner)
  3306. nfs4_unlock_state();
  3307. return status;
  3308. }
  3309. void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
  3310. {
  3311. struct nfs4_openowner *oo;
  3312. struct nfs4_ol_stateid *s;
  3313. if (!so->so_is_open_owner)
  3314. return;
  3315. oo = openowner(so);
  3316. s = oo->oo_last_closed_stid;
  3317. if (!s)
  3318. return;
  3319. if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
  3320. /* Release the last_closed_stid on the next seqid bump: */
  3321. oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
  3322. return;
  3323. }
  3324. oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
  3325. release_last_closed_stateid(oo);
  3326. }
  3327. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  3328. {
  3329. unhash_open_stateid(s);
  3330. s->st_stid.sc_type = NFS4_CLOSED_STID;
  3331. }
  3332. /*
  3333. * nfs4_unlock_state() called after encode
  3334. */
  3335. __be32
  3336. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3337. struct nfsd4_close *close)
  3338. {
  3339. __be32 status;
  3340. struct nfs4_openowner *oo;
  3341. struct nfs4_ol_stateid *stp;
  3342. struct net *net = SVC_NET(rqstp);
  3343. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3344. dprintk("NFSD: nfsd4_close on file %.*s\n",
  3345. (int)cstate->current_fh.fh_dentry->d_name.len,
  3346. cstate->current_fh.fh_dentry->d_name.name);
  3347. nfs4_lock_state();
  3348. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  3349. &close->cl_stateid,
  3350. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  3351. &stp, nn);
  3352. if (status)
  3353. goto out;
  3354. oo = openowner(stp->st_stateowner);
  3355. status = nfs_ok;
  3356. update_stateid(&stp->st_stid.sc_stateid);
  3357. memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3358. nfsd4_close_open_stateid(stp);
  3359. release_last_closed_stateid(oo);
  3360. oo->oo_last_closed_stid = stp;
  3361. if (list_empty(&oo->oo_owner.so_stateids)) {
  3362. if (cstate->minorversion) {
  3363. release_openowner(oo);
  3364. cstate->replay_owner = NULL;
  3365. } else {
  3366. /*
  3367. * In the 4.0 case we need to keep the owners around a
  3368. * little while to handle CLOSE replay.
  3369. */
  3370. if (list_empty(&oo->oo_owner.so_stateids))
  3371. move_to_close_lru(oo, SVC_NET(rqstp));
  3372. }
  3373. }
  3374. out:
  3375. if (!cstate->replay_owner)
  3376. nfs4_unlock_state();
  3377. return status;
  3378. }
  3379. __be32
  3380. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3381. struct nfsd4_delegreturn *dr)
  3382. {
  3383. struct nfs4_delegation *dp;
  3384. stateid_t *stateid = &dr->dr_stateid;
  3385. struct nfs4_stid *s;
  3386. __be32 status;
  3387. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3388. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3389. return status;
  3390. nfs4_lock_state();
  3391. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
  3392. cstate->minorversion, nn);
  3393. if (status)
  3394. goto out;
  3395. dp = delegstateid(s);
  3396. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  3397. if (status)
  3398. goto out;
  3399. unhash_delegation(dp);
  3400. out:
  3401. nfs4_unlock_state();
  3402. return status;
  3403. }
  3404. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  3405. #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
  3406. static inline u64
  3407. end_offset(u64 start, u64 len)
  3408. {
  3409. u64 end;
  3410. end = start + len;
  3411. return end >= start ? end: NFS4_MAX_UINT64;
  3412. }
  3413. /* last octet in a range */
  3414. static inline u64
  3415. last_byte_offset(u64 start, u64 len)
  3416. {
  3417. u64 end;
  3418. WARN_ON_ONCE(!len);
  3419. end = start + len;
  3420. return end > start ? end - 1: NFS4_MAX_UINT64;
  3421. }
  3422. static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
  3423. {
  3424. return (file_hashval(inode) + cl_id
  3425. + opaque_hashval(ownername->data, ownername->len))
  3426. & LOCKOWNER_INO_HASH_MASK;
  3427. }
  3428. /*
  3429. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  3430. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  3431. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  3432. * locking, this prevents us from being completely protocol-compliant. The
  3433. * real solution to this problem is to start using unsigned file offsets in
  3434. * the VFS, but this is a very deep change!
  3435. */
  3436. static inline void
  3437. nfs4_transform_lock_offset(struct file_lock *lock)
  3438. {
  3439. if (lock->fl_start < 0)
  3440. lock->fl_start = OFFSET_MAX;
  3441. if (lock->fl_end < 0)
  3442. lock->fl_end = OFFSET_MAX;
  3443. }
  3444. /* Hack!: For now, we're defining this just so we can use a pointer to it
  3445. * as a unique cookie to identify our (NFSv4's) posix locks. */
  3446. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  3447. };
  3448. static inline void
  3449. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  3450. {
  3451. struct nfs4_lockowner *lo;
  3452. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  3453. lo = (struct nfs4_lockowner *) fl->fl_owner;
  3454. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  3455. lo->lo_owner.so_owner.len, GFP_KERNEL);
  3456. if (!deny->ld_owner.data)
  3457. /* We just don't care that much */
  3458. goto nevermind;
  3459. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  3460. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  3461. } else {
  3462. nevermind:
  3463. deny->ld_owner.len = 0;
  3464. deny->ld_owner.data = NULL;
  3465. deny->ld_clientid.cl_boot = 0;
  3466. deny->ld_clientid.cl_id = 0;
  3467. }
  3468. deny->ld_start = fl->fl_start;
  3469. deny->ld_length = NFS4_MAX_UINT64;
  3470. if (fl->fl_end != NFS4_MAX_UINT64)
  3471. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  3472. deny->ld_type = NFS4_READ_LT;
  3473. if (fl->fl_type != F_RDLCK)
  3474. deny->ld_type = NFS4_WRITE_LT;
  3475. }
  3476. static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
  3477. {
  3478. struct nfs4_ol_stateid *lst;
  3479. if (!same_owner_str(&lo->lo_owner, owner, clid))
  3480. return false;
  3481. lst = list_first_entry(&lo->lo_owner.so_stateids,
  3482. struct nfs4_ol_stateid, st_perstateowner);
  3483. return lst->st_file->fi_inode == inode;
  3484. }
  3485. static struct nfs4_lockowner *
  3486. find_lockowner_str(struct inode *inode, clientid_t *clid,
  3487. struct xdr_netobj *owner, struct nfsd_net *nn)
  3488. {
  3489. unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
  3490. struct nfs4_lockowner *lo;
  3491. list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
  3492. if (same_lockowner_ino(lo, inode, clid, owner))
  3493. return lo;
  3494. }
  3495. return NULL;
  3496. }
  3497. static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
  3498. {
  3499. struct inode *inode = open_stp->st_file->fi_inode;
  3500. unsigned int inohash = lockowner_ino_hashval(inode,
  3501. clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
  3502. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  3503. list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  3504. list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
  3505. list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
  3506. }
  3507. /*
  3508. * Alloc a lock owner structure.
  3509. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  3510. * occurred.
  3511. *
  3512. * strhashval = ownerstr_hashval
  3513. */
  3514. static struct nfs4_lockowner *
  3515. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
  3516. struct nfs4_lockowner *lo;
  3517. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  3518. if (!lo)
  3519. return NULL;
  3520. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  3521. lo->lo_owner.so_is_open_owner = 0;
  3522. /* It is the openowner seqid that will be incremented in encode in the
  3523. * case of new lockowners; so increment the lock seqid manually: */
  3524. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
  3525. hash_lockowner(lo, strhashval, clp, open_stp);
  3526. return lo;
  3527. }
  3528. static struct nfs4_ol_stateid *
  3529. alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
  3530. {
  3531. struct nfs4_ol_stateid *stp;
  3532. struct nfs4_client *clp = lo->lo_owner.so_client;
  3533. stp = nfs4_alloc_stateid(clp);
  3534. if (stp == NULL)
  3535. return NULL;
  3536. stp->st_stid.sc_type = NFS4_LOCK_STID;
  3537. list_add(&stp->st_perfile, &fp->fi_stateids);
  3538. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  3539. stp->st_stateowner = &lo->lo_owner;
  3540. get_nfs4_file(fp);
  3541. stp->st_file = fp;
  3542. stp->st_access_bmap = 0;
  3543. stp->st_deny_bmap = open_stp->st_deny_bmap;
  3544. stp->st_openstp = open_stp;
  3545. return stp;
  3546. }
  3547. static int
  3548. check_lock_length(u64 offset, u64 length)
  3549. {
  3550. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  3551. LOFF_OVERFLOW(offset, length)));
  3552. }
  3553. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  3554. {
  3555. struct nfs4_file *fp = lock_stp->st_file;
  3556. int oflag = nfs4_access_to_omode(access);
  3557. if (test_access(access, lock_stp))
  3558. return;
  3559. nfs4_file_get_access(fp, oflag);
  3560. set_access(access, lock_stp);
  3561. }
  3562. 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)
  3563. {
  3564. struct nfs4_file *fi = ost->st_file;
  3565. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  3566. struct nfs4_client *cl = oo->oo_owner.so_client;
  3567. struct nfs4_lockowner *lo;
  3568. unsigned int strhashval;
  3569. struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
  3570. lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
  3571. &lock->v.new.owner, nn);
  3572. if (lo) {
  3573. if (!cstate->minorversion)
  3574. return nfserr_bad_seqid;
  3575. /* XXX: a lockowner always has exactly one stateid: */
  3576. *lst = list_first_entry(&lo->lo_owner.so_stateids,
  3577. struct nfs4_ol_stateid, st_perstateowner);
  3578. return nfs_ok;
  3579. }
  3580. strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
  3581. &lock->v.new.owner);
  3582. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  3583. if (lo == NULL)
  3584. return nfserr_jukebox;
  3585. *lst = alloc_init_lock_stateid(lo, fi, ost);
  3586. if (*lst == NULL) {
  3587. release_lockowner(lo);
  3588. return nfserr_jukebox;
  3589. }
  3590. *new = true;
  3591. return nfs_ok;
  3592. }
  3593. /*
  3594. * LOCK operation
  3595. */
  3596. __be32
  3597. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3598. struct nfsd4_lock *lock)
  3599. {
  3600. struct nfs4_openowner *open_sop = NULL;
  3601. struct nfs4_lockowner *lock_sop = NULL;
  3602. struct nfs4_ol_stateid *lock_stp;
  3603. struct file *filp = NULL;
  3604. struct file_lock *file_lock = NULL;
  3605. struct file_lock *conflock = NULL;
  3606. __be32 status = 0;
  3607. bool new_state = false;
  3608. int lkflg;
  3609. int err;
  3610. struct net *net = SVC_NET(rqstp);
  3611. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3612. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  3613. (long long) lock->lk_offset,
  3614. (long long) lock->lk_length);
  3615. if (check_lock_length(lock->lk_offset, lock->lk_length))
  3616. return nfserr_inval;
  3617. if ((status = fh_verify(rqstp, &cstate->current_fh,
  3618. S_IFREG, NFSD_MAY_LOCK))) {
  3619. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  3620. return status;
  3621. }
  3622. nfs4_lock_state();
  3623. if (lock->lk_is_new) {
  3624. struct nfs4_ol_stateid *open_stp = NULL;
  3625. if (nfsd4_has_session(cstate))
  3626. /* See rfc 5661 18.10.3: given clientid is ignored: */
  3627. memcpy(&lock->v.new.clientid,
  3628. &cstate->session->se_client->cl_clientid,
  3629. sizeof(clientid_t));
  3630. status = nfserr_stale_clientid;
  3631. if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
  3632. goto out;
  3633. /* validate and update open stateid and open seqid */
  3634. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  3635. lock->lk_new_open_seqid,
  3636. &lock->lk_new_open_stateid,
  3637. &open_stp, nn);
  3638. if (status)
  3639. goto out;
  3640. open_sop = openowner(open_stp->st_stateowner);
  3641. status = nfserr_bad_stateid;
  3642. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  3643. &lock->v.new.clientid))
  3644. goto out;
  3645. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  3646. &lock_stp, &new_state);
  3647. } else
  3648. status = nfs4_preprocess_seqid_op(cstate,
  3649. lock->lk_old_lock_seqid,
  3650. &lock->lk_old_lock_stateid,
  3651. NFS4_LOCK_STID, &lock_stp, nn);
  3652. if (status)
  3653. goto out;
  3654. lock_sop = lockowner(lock_stp->st_stateowner);
  3655. lkflg = setlkflg(lock->lk_type);
  3656. status = nfs4_check_openmode(lock_stp, lkflg);
  3657. if (status)
  3658. goto out;
  3659. status = nfserr_grace;
  3660. if (locks_in_grace(net) && !lock->lk_reclaim)
  3661. goto out;
  3662. status = nfserr_no_grace;
  3663. if (!locks_in_grace(net) && lock->lk_reclaim)
  3664. goto out;
  3665. file_lock = locks_alloc_lock();
  3666. if (!file_lock) {
  3667. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3668. status = nfserr_jukebox;
  3669. goto out;
  3670. }
  3671. locks_init_lock(file_lock);
  3672. switch (lock->lk_type) {
  3673. case NFS4_READ_LT:
  3674. case NFS4_READW_LT:
  3675. filp = find_readable_file(lock_stp->st_file);
  3676. if (filp)
  3677. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  3678. file_lock->fl_type = F_RDLCK;
  3679. break;
  3680. case NFS4_WRITE_LT:
  3681. case NFS4_WRITEW_LT:
  3682. filp = find_writeable_file(lock_stp->st_file);
  3683. if (filp)
  3684. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  3685. file_lock->fl_type = F_WRLCK;
  3686. break;
  3687. default:
  3688. status = nfserr_inval;
  3689. goto out;
  3690. }
  3691. if (!filp) {
  3692. status = nfserr_openmode;
  3693. goto out;
  3694. }
  3695. file_lock->fl_owner = (fl_owner_t)lock_sop;
  3696. file_lock->fl_pid = current->tgid;
  3697. file_lock->fl_file = filp;
  3698. file_lock->fl_flags = FL_POSIX;
  3699. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3700. file_lock->fl_start = lock->lk_offset;
  3701. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  3702. nfs4_transform_lock_offset(file_lock);
  3703. conflock = locks_alloc_lock();
  3704. if (!conflock) {
  3705. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3706. status = nfserr_jukebox;
  3707. goto out;
  3708. }
  3709. err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
  3710. switch (-err) {
  3711. case 0: /* success! */
  3712. update_stateid(&lock_stp->st_stid.sc_stateid);
  3713. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
  3714. sizeof(stateid_t));
  3715. status = 0;
  3716. break;
  3717. case (EAGAIN): /* conflock holds conflicting lock */
  3718. status = nfserr_denied;
  3719. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  3720. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  3721. break;
  3722. case (EDEADLK):
  3723. status = nfserr_deadlock;
  3724. break;
  3725. default:
  3726. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  3727. status = nfserrno(err);
  3728. break;
  3729. }
  3730. out:
  3731. if (status && new_state)
  3732. release_lockowner(lock_sop);
  3733. if (!cstate->replay_owner)
  3734. nfs4_unlock_state();
  3735. if (file_lock)
  3736. locks_free_lock(file_lock);
  3737. if (conflock)
  3738. locks_free_lock(conflock);
  3739. return status;
  3740. }
  3741. /*
  3742. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  3743. * so we do a temporary open here just to get an open file to pass to
  3744. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  3745. * inode operation.)
  3746. */
  3747. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  3748. {
  3749. struct file *file;
  3750. __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  3751. if (!err) {
  3752. err = nfserrno(vfs_test_lock(file, lock));
  3753. nfsd_close(file);
  3754. }
  3755. return err;
  3756. }
  3757. /*
  3758. * LOCKT operation
  3759. */
  3760. __be32
  3761. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3762. struct nfsd4_lockt *lockt)
  3763. {
  3764. struct inode *inode;
  3765. struct file_lock *file_lock = NULL;
  3766. struct nfs4_lockowner *lo;
  3767. __be32 status;
  3768. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3769. if (locks_in_grace(SVC_NET(rqstp)))
  3770. return nfserr_grace;
  3771. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  3772. return nfserr_inval;
  3773. nfs4_lock_state();
  3774. if (!nfsd4_has_session(cstate)) {
  3775. status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
  3776. if (status)
  3777. goto out;
  3778. }
  3779. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3780. goto out;
  3781. inode = cstate->current_fh.fh_dentry->d_inode;
  3782. file_lock = locks_alloc_lock();
  3783. if (!file_lock) {
  3784. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3785. status = nfserr_jukebox;
  3786. goto out;
  3787. }
  3788. locks_init_lock(file_lock);
  3789. switch (lockt->lt_type) {
  3790. case NFS4_READ_LT:
  3791. case NFS4_READW_LT:
  3792. file_lock->fl_type = F_RDLCK;
  3793. break;
  3794. case NFS4_WRITE_LT:
  3795. case NFS4_WRITEW_LT:
  3796. file_lock->fl_type = F_WRLCK;
  3797. break;
  3798. default:
  3799. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  3800. status = nfserr_inval;
  3801. goto out;
  3802. }
  3803. lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
  3804. if (lo)
  3805. file_lock->fl_owner = (fl_owner_t)lo;
  3806. file_lock->fl_pid = current->tgid;
  3807. file_lock->fl_flags = FL_POSIX;
  3808. file_lock->fl_start = lockt->lt_offset;
  3809. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  3810. nfs4_transform_lock_offset(file_lock);
  3811. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  3812. if (status)
  3813. goto out;
  3814. if (file_lock->fl_type != F_UNLCK) {
  3815. status = nfserr_denied;
  3816. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  3817. }
  3818. out:
  3819. nfs4_unlock_state();
  3820. if (file_lock)
  3821. locks_free_lock(file_lock);
  3822. return status;
  3823. }
  3824. __be32
  3825. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3826. struct nfsd4_locku *locku)
  3827. {
  3828. struct nfs4_ol_stateid *stp;
  3829. struct file *filp = NULL;
  3830. struct file_lock *file_lock = NULL;
  3831. __be32 status;
  3832. int err;
  3833. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3834. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  3835. (long long) locku->lu_offset,
  3836. (long long) locku->lu_length);
  3837. if (check_lock_length(locku->lu_offset, locku->lu_length))
  3838. return nfserr_inval;
  3839. nfs4_lock_state();
  3840. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  3841. &locku->lu_stateid, NFS4_LOCK_STID,
  3842. &stp, nn);
  3843. if (status)
  3844. goto out;
  3845. filp = find_any_file(stp->st_file);
  3846. if (!filp) {
  3847. status = nfserr_lock_range;
  3848. goto out;
  3849. }
  3850. file_lock = locks_alloc_lock();
  3851. if (!file_lock) {
  3852. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3853. status = nfserr_jukebox;
  3854. goto out;
  3855. }
  3856. locks_init_lock(file_lock);
  3857. file_lock->fl_type = F_UNLCK;
  3858. file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
  3859. file_lock->fl_pid = current->tgid;
  3860. file_lock->fl_file = filp;
  3861. file_lock->fl_flags = FL_POSIX;
  3862. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3863. file_lock->fl_start = locku->lu_offset;
  3864. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  3865. locku->lu_length);
  3866. nfs4_transform_lock_offset(file_lock);
  3867. /*
  3868. * Try to unlock the file in the VFS.
  3869. */
  3870. err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
  3871. if (err) {
  3872. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  3873. goto out_nfserr;
  3874. }
  3875. /*
  3876. * OK, unlock succeeded; the only thing left to do is update the stateid.
  3877. */
  3878. update_stateid(&stp->st_stid.sc_stateid);
  3879. memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3880. out:
  3881. if (!cstate->replay_owner)
  3882. nfs4_unlock_state();
  3883. if (file_lock)
  3884. locks_free_lock(file_lock);
  3885. return status;
  3886. out_nfserr:
  3887. status = nfserrno(err);
  3888. goto out;
  3889. }
  3890. /*
  3891. * returns
  3892. * 1: locks held by lockowner
  3893. * 0: no locks held by lockowner
  3894. */
  3895. static int
  3896. check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
  3897. {
  3898. struct file_lock **flpp;
  3899. struct inode *inode = filp->fi_inode;
  3900. int status = 0;
  3901. lock_flocks();
  3902. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  3903. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  3904. status = 1;
  3905. goto out;
  3906. }
  3907. }
  3908. out:
  3909. unlock_flocks();
  3910. return status;
  3911. }
  3912. __be32
  3913. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  3914. struct nfsd4_compound_state *cstate,
  3915. struct nfsd4_release_lockowner *rlockowner)
  3916. {
  3917. clientid_t *clid = &rlockowner->rl_clientid;
  3918. struct nfs4_stateowner *sop;
  3919. struct nfs4_lockowner *lo;
  3920. struct nfs4_ol_stateid *stp;
  3921. struct xdr_netobj *owner = &rlockowner->rl_owner;
  3922. struct list_head matches;
  3923. unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
  3924. __be32 status;
  3925. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3926. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  3927. clid->cl_boot, clid->cl_id);
  3928. nfs4_lock_state();
  3929. status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
  3930. if (status)
  3931. goto out;
  3932. status = nfserr_locks_held;
  3933. INIT_LIST_HEAD(&matches);
  3934. list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  3935. if (sop->so_is_open_owner)
  3936. continue;
  3937. if (!same_owner_str(sop, owner, clid))
  3938. continue;
  3939. list_for_each_entry(stp, &sop->so_stateids,
  3940. st_perstateowner) {
  3941. lo = lockowner(sop);
  3942. if (check_for_locks(stp->st_file, lo))
  3943. goto out;
  3944. list_add(&lo->lo_list, &matches);
  3945. }
  3946. }
  3947. /* Clients probably won't expect us to return with some (but not all)
  3948. * of the lockowner state released; so don't release any until all
  3949. * have been checked. */
  3950. status = nfs_ok;
  3951. while (!list_empty(&matches)) {
  3952. lo = list_entry(matches.next, struct nfs4_lockowner,
  3953. lo_list);
  3954. /* unhash_stateowner deletes so_perclient only
  3955. * for openowners. */
  3956. list_del(&lo->lo_list);
  3957. release_lockowner(lo);
  3958. }
  3959. out:
  3960. nfs4_unlock_state();
  3961. return status;
  3962. }
  3963. static inline struct nfs4_client_reclaim *
  3964. alloc_reclaim(void)
  3965. {
  3966. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  3967. }
  3968. bool
  3969. nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
  3970. {
  3971. struct nfs4_client_reclaim *crp;
  3972. crp = nfsd4_find_reclaim_client(name, nn);
  3973. return (crp && crp->cr_clp);
  3974. }
  3975. /*
  3976. * failure => all reset bets are off, nfserr_no_grace...
  3977. */
  3978. struct nfs4_client_reclaim *
  3979. nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
  3980. {
  3981. unsigned int strhashval;
  3982. struct nfs4_client_reclaim *crp;
  3983. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  3984. crp = alloc_reclaim();
  3985. if (crp) {
  3986. strhashval = clientstr_hashval(name);
  3987. INIT_LIST_HEAD(&crp->cr_strhash);
  3988. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  3989. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  3990. crp->cr_clp = NULL;
  3991. nn->reclaim_str_hashtbl_size++;
  3992. }
  3993. return crp;
  3994. }
  3995. void
  3996. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  3997. {
  3998. list_del(&crp->cr_strhash);
  3999. kfree(crp);
  4000. nn->reclaim_str_hashtbl_size--;
  4001. }
  4002. void
  4003. nfs4_release_reclaim(struct nfsd_net *nn)
  4004. {
  4005. struct nfs4_client_reclaim *crp = NULL;
  4006. int i;
  4007. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4008. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  4009. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  4010. struct nfs4_client_reclaim, cr_strhash);
  4011. nfs4_remove_reclaim_record(crp, nn);
  4012. }
  4013. }
  4014. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  4015. }
  4016. /*
  4017. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  4018. struct nfs4_client_reclaim *
  4019. nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
  4020. {
  4021. unsigned int strhashval;
  4022. struct nfs4_client_reclaim *crp = NULL;
  4023. dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
  4024. strhashval = clientstr_hashval(recdir);
  4025. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  4026. if (same_name(crp->cr_recdir, recdir)) {
  4027. return crp;
  4028. }
  4029. }
  4030. return NULL;
  4031. }
  4032. /*
  4033. * Called from OPEN. Look for clientid in reclaim list.
  4034. */
  4035. __be32
  4036. nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  4037. {
  4038. struct nfs4_client *clp;
  4039. /* find clientid in conf_id_hashtbl */
  4040. clp = find_confirmed_client(clid, sessions, nn);
  4041. if (clp == NULL)
  4042. return nfserr_reclaim_bad;
  4043. return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
  4044. }
  4045. #ifdef CONFIG_NFSD_FAULT_INJECTION
  4046. u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
  4047. {
  4048. expire_client(clp);
  4049. return 1;
  4050. }
  4051. u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
  4052. {
  4053. char buf[INET6_ADDRSTRLEN];
  4054. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4055. printk(KERN_INFO "NFS Client: %s\n", buf);
  4056. return 1;
  4057. }
  4058. static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
  4059. const char *type)
  4060. {
  4061. char buf[INET6_ADDRSTRLEN];
  4062. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4063. printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
  4064. }
  4065. static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
  4066. {
  4067. struct nfs4_openowner *oop;
  4068. struct nfs4_lockowner *lop, *lo_next;
  4069. struct nfs4_ol_stateid *stp, *st_next;
  4070. u64 count = 0;
  4071. list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
  4072. list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
  4073. list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
  4074. if (func)
  4075. func(lop);
  4076. if (++count == max)
  4077. return count;
  4078. }
  4079. }
  4080. }
  4081. return count;
  4082. }
  4083. u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
  4084. {
  4085. return nfsd_foreach_client_lock(clp, max, release_lockowner);
  4086. }
  4087. u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
  4088. {
  4089. u64 count = nfsd_foreach_client_lock(clp, max, NULL);
  4090. nfsd_print_count(clp, count, "locked files");
  4091. return count;
  4092. }
  4093. static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
  4094. {
  4095. struct nfs4_openowner *oop, *next;
  4096. u64 count = 0;
  4097. list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
  4098. if (func)
  4099. func(oop);
  4100. if (++count == max)
  4101. break;
  4102. }
  4103. return count;
  4104. }
  4105. u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
  4106. {
  4107. return nfsd_foreach_client_open(clp, max, release_openowner);
  4108. }
  4109. u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
  4110. {
  4111. u64 count = nfsd_foreach_client_open(clp, max, NULL);
  4112. nfsd_print_count(clp, count, "open files");
  4113. return count;
  4114. }
  4115. static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
  4116. struct list_head *victims)
  4117. {
  4118. struct nfs4_delegation *dp, *next;
  4119. u64 count = 0;
  4120. list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
  4121. if (victims)
  4122. list_move(&dp->dl_recall_lru, victims);
  4123. if (++count == max)
  4124. break;
  4125. }
  4126. return count;
  4127. }
  4128. u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
  4129. {
  4130. struct nfs4_delegation *dp, *next;
  4131. LIST_HEAD(victims);
  4132. u64 count;
  4133. spin_lock(&recall_lock);
  4134. count = nfsd_find_all_delegations(clp, max, &victims);
  4135. spin_unlock(&recall_lock);
  4136. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4137. unhash_delegation(dp);
  4138. return count;
  4139. }
  4140. u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
  4141. {
  4142. struct nfs4_delegation *dp, *next;
  4143. LIST_HEAD(victims);
  4144. u64 count;
  4145. spin_lock(&recall_lock);
  4146. count = nfsd_find_all_delegations(clp, max, &victims);
  4147. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4148. nfsd_break_one_deleg(dp);
  4149. spin_unlock(&recall_lock);
  4150. return count;
  4151. }
  4152. u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
  4153. {
  4154. u64 count = 0;
  4155. spin_lock(&recall_lock);
  4156. count = nfsd_find_all_delegations(clp, max, NULL);
  4157. spin_unlock(&recall_lock);
  4158. nfsd_print_count(clp, count, "delegations");
  4159. return count;
  4160. }
  4161. u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
  4162. {
  4163. struct nfs4_client *clp, *next;
  4164. u64 count = 0;
  4165. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4166. if (!nfsd_netns_ready(nn))
  4167. return 0;
  4168. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  4169. count += func(clp, max - count);
  4170. if ((max != 0) && (count >= max))
  4171. break;
  4172. }
  4173. return count;
  4174. }
  4175. struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
  4176. {
  4177. struct nfs4_client *clp;
  4178. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4179. if (!nfsd_netns_ready(nn))
  4180. return NULL;
  4181. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  4182. if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
  4183. return clp;
  4184. }
  4185. return NULL;
  4186. }
  4187. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  4188. /* initialization to perform at module load time: */
  4189. void
  4190. nfs4_state_init(void)
  4191. {
  4192. int i;
  4193. for (i = 0; i < FILE_HASH_SIZE; i++) {
  4194. INIT_LIST_HEAD(&file_hashtbl[i]);
  4195. }
  4196. INIT_LIST_HEAD(&del_recall_lru);
  4197. }
  4198. /*
  4199. * Since the lifetime of a delegation isn't limited to that of an open, a
  4200. * client may quite reasonably hang on to a delegation as long as it has
  4201. * the inode cached. This becomes an obvious problem the first time a
  4202. * client's inode cache approaches the size of the server's total memory.
  4203. *
  4204. * For now we avoid this problem by imposing a hard limit on the number
  4205. * of delegations, which varies according to the server's memory size.
  4206. */
  4207. static void
  4208. set_max_delegations(void)
  4209. {
  4210. /*
  4211. * Allow at most 4 delegations per megabyte of RAM. Quick
  4212. * estimates suggest that in the worst case (where every delegation
  4213. * is for a different inode), a delegation could take about 1.5K,
  4214. * giving a worst case usage of about 6% of memory.
  4215. */
  4216. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  4217. }
  4218. static int nfs4_state_create_net(struct net *net)
  4219. {
  4220. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4221. int i;
  4222. nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4223. CLIENT_HASH_SIZE, GFP_KERNEL);
  4224. if (!nn->conf_id_hashtbl)
  4225. goto err;
  4226. nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4227. CLIENT_HASH_SIZE, GFP_KERNEL);
  4228. if (!nn->unconf_id_hashtbl)
  4229. goto err_unconf_id;
  4230. nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
  4231. OWNER_HASH_SIZE, GFP_KERNEL);
  4232. if (!nn->ownerstr_hashtbl)
  4233. goto err_ownerstr;
  4234. nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
  4235. LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
  4236. if (!nn->lockowner_ino_hashtbl)
  4237. goto err_lockowner_ino;
  4238. nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
  4239. SESSION_HASH_SIZE, GFP_KERNEL);
  4240. if (!nn->sessionid_hashtbl)
  4241. goto err_sessionid;
  4242. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4243. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  4244. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  4245. }
  4246. for (i = 0; i < OWNER_HASH_SIZE; i++)
  4247. INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
  4248. for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
  4249. INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
  4250. for (i = 0; i < SESSION_HASH_SIZE; i++)
  4251. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  4252. nn->conf_name_tree = RB_ROOT;
  4253. nn->unconf_name_tree = RB_ROOT;
  4254. INIT_LIST_HEAD(&nn->client_lru);
  4255. INIT_LIST_HEAD(&nn->close_lru);
  4256. spin_lock_init(&nn->client_lock);
  4257. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  4258. get_net(net);
  4259. return 0;
  4260. err_sessionid:
  4261. kfree(nn->lockowner_ino_hashtbl);
  4262. err_lockowner_ino:
  4263. kfree(nn->ownerstr_hashtbl);
  4264. err_ownerstr:
  4265. kfree(nn->unconf_id_hashtbl);
  4266. err_unconf_id:
  4267. kfree(nn->conf_id_hashtbl);
  4268. err:
  4269. return -ENOMEM;
  4270. }
  4271. static void
  4272. nfs4_state_destroy_net(struct net *net)
  4273. {
  4274. int i;
  4275. struct nfs4_client *clp = NULL;
  4276. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4277. struct rb_node *node, *tmp;
  4278. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4279. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  4280. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  4281. destroy_client(clp);
  4282. }
  4283. }
  4284. node = rb_first(&nn->unconf_name_tree);
  4285. while (node != NULL) {
  4286. tmp = node;
  4287. node = rb_next(tmp);
  4288. clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
  4289. rb_erase(tmp, &nn->unconf_name_tree);
  4290. destroy_client(clp);
  4291. }
  4292. kfree(nn->sessionid_hashtbl);
  4293. kfree(nn->lockowner_ino_hashtbl);
  4294. kfree(nn->ownerstr_hashtbl);
  4295. kfree(nn->unconf_id_hashtbl);
  4296. kfree(nn->conf_id_hashtbl);
  4297. put_net(net);
  4298. }
  4299. int
  4300. nfs4_state_start_net(struct net *net)
  4301. {
  4302. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4303. int ret;
  4304. ret = nfs4_state_create_net(net);
  4305. if (ret)
  4306. return ret;
  4307. nfsd4_client_tracking_init(net);
  4308. nn->boot_time = get_seconds();
  4309. locks_start_grace(net, &nn->nfsd4_manager);
  4310. nn->grace_ended = false;
  4311. printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
  4312. nn->nfsd4_grace, net);
  4313. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
  4314. return 0;
  4315. }
  4316. /* initialization to perform when the nfsd service is started: */
  4317. int
  4318. nfs4_state_start(void)
  4319. {
  4320. int ret;
  4321. ret = set_callback_cred();
  4322. if (ret)
  4323. return -ENOMEM;
  4324. laundry_wq = create_singlethread_workqueue("nfsd4");
  4325. if (laundry_wq == NULL) {
  4326. ret = -ENOMEM;
  4327. goto out_recovery;
  4328. }
  4329. ret = nfsd4_create_callback_queue();
  4330. if (ret)
  4331. goto out_free_laundry;
  4332. set_max_delegations();
  4333. return 0;
  4334. out_free_laundry:
  4335. destroy_workqueue(laundry_wq);
  4336. out_recovery:
  4337. return ret;
  4338. }
  4339. /* should be called with the state lock held */
  4340. void
  4341. nfs4_state_shutdown_net(struct net *net)
  4342. {
  4343. struct nfs4_delegation *dp = NULL;
  4344. struct list_head *pos, *next, reaplist;
  4345. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4346. cancel_delayed_work_sync(&nn->laundromat_work);
  4347. locks_end_grace(&nn->nfsd4_manager);
  4348. INIT_LIST_HEAD(&reaplist);
  4349. spin_lock(&recall_lock);
  4350. list_for_each_safe(pos, next, &del_recall_lru) {
  4351. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4352. if (dp->dl_stid.sc_client->net != net)
  4353. continue;
  4354. list_move(&dp->dl_recall_lru, &reaplist);
  4355. }
  4356. spin_unlock(&recall_lock);
  4357. list_for_each_safe(pos, next, &reaplist) {
  4358. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4359. unhash_delegation(dp);
  4360. }
  4361. nfsd4_client_tracking_exit(net);
  4362. nfs4_state_destroy_net(net);
  4363. }
  4364. void
  4365. nfs4_state_shutdown(void)
  4366. {
  4367. destroy_workqueue(laundry_wq);
  4368. nfsd4_destroy_callback_queue();
  4369. }
  4370. static void
  4371. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4372. {
  4373. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  4374. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  4375. }
  4376. static void
  4377. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4378. {
  4379. if (cstate->minorversion) {
  4380. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  4381. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4382. }
  4383. }
  4384. void
  4385. clear_current_stateid(struct nfsd4_compound_state *cstate)
  4386. {
  4387. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4388. }
  4389. /*
  4390. * functions to set current state id
  4391. */
  4392. void
  4393. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4394. {
  4395. put_stateid(cstate, &odp->od_stateid);
  4396. }
  4397. void
  4398. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  4399. {
  4400. put_stateid(cstate, &open->op_stateid);
  4401. }
  4402. void
  4403. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4404. {
  4405. put_stateid(cstate, &close->cl_stateid);
  4406. }
  4407. void
  4408. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  4409. {
  4410. put_stateid(cstate, &lock->lk_resp_stateid);
  4411. }
  4412. /*
  4413. * functions to consume current state id
  4414. */
  4415. void
  4416. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4417. {
  4418. get_stateid(cstate, &odp->od_stateid);
  4419. }
  4420. void
  4421. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  4422. {
  4423. get_stateid(cstate, &drp->dr_stateid);
  4424. }
  4425. void
  4426. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  4427. {
  4428. get_stateid(cstate, &fsp->fr_stateid);
  4429. }
  4430. void
  4431. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  4432. {
  4433. get_stateid(cstate, &setattr->sa_stateid);
  4434. }
  4435. void
  4436. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4437. {
  4438. get_stateid(cstate, &close->cl_stateid);
  4439. }
  4440. void
  4441. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  4442. {
  4443. get_stateid(cstate, &locku->lu_stateid);
  4444. }
  4445. void
  4446. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  4447. {
  4448. get_stateid(cstate, &read->rd_stateid);
  4449. }
  4450. void
  4451. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  4452. {
  4453. get_stateid(cstate, &write->wr_stateid);
  4454. }