nfs4state.c 126 KB

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