nfs4state.c 114 KB

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