nfs4state.c 127 KB

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