nfs4state.c 113 KB

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