nfs4state.c 115 KB

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