nfs4state.c 137 KB

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