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