nfs4state.c 121 KB

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