nfs4state.c 117 KB

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