nfs4state.c 129 KB

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