nfs4state.c 116 KB

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