nfs4state.c 127 KB

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