nfs4state.c 133 KB

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