nfs4state.c 127 KB

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