nfs4state.c 112 KB

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