nfs4state.c 136 KB

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