nfs4state.c 118 KB

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