nfs4state.c 110 KB

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