nfs4state.c 114 KB

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