nfs4state.c 119 KB

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