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