nfs4state.c 111 KB

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