nfs4state.c 128 KB

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