nfs4state.c 127 KB

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