nfs4state.c 118 KB

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