nfs4state.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207
  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. /*
  1088. * Set bit when the owner id and verifier map to an already
  1089. * confirmed client id (18.35.3).
  1090. */
  1091. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1092. /*
  1093. * Falling into 18.35.4 case 2, possible router replay.
  1094. * Leave confirmed record intact and return same result.
  1095. */
  1096. copy_verf(conf, &verf);
  1097. new = conf;
  1098. goto out_copy;
  1099. }
  1100. /* 18.35.4 case 7 */
  1101. if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
  1102. status = nfserr_noent;
  1103. goto out;
  1104. }
  1105. unconf = find_unconfirmed_client_by_str(dname, strhashval, true);
  1106. if (unconf) {
  1107. /*
  1108. * Possible retry or client restart. Per 18.35.4 case 4,
  1109. * a new unconfirmed record should be generated regardless
  1110. * of whether any properties have changed.
  1111. */
  1112. expire_client(unconf);
  1113. }
  1114. out_new:
  1115. /* Normal case */
  1116. new = create_client(exid->clname, dname);
  1117. if (new == NULL) {
  1118. status = nfserr_resource;
  1119. goto out;
  1120. }
  1121. copy_verf(new, &verf);
  1122. copy_cred(&new->cl_cred, &rqstp->rq_cred);
  1123. new->cl_addr = ip_addr;
  1124. gen_clid(new);
  1125. gen_confirm(new);
  1126. add_to_unconfirmed(new, strhashval);
  1127. out_copy:
  1128. exid->clientid.cl_boot = new->cl_clientid.cl_boot;
  1129. exid->clientid.cl_id = new->cl_clientid.cl_id;
  1130. new->cl_slot.sl_seqid = 0;
  1131. exid->seqid = 1;
  1132. nfsd4_set_ex_flags(new, exid);
  1133. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  1134. new->cl_slot.sl_seqid, new->cl_exchange_flags);
  1135. status = nfs_ok;
  1136. out:
  1137. nfs4_unlock_state();
  1138. error:
  1139. dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
  1140. return status;
  1141. }
  1142. static int
  1143. check_slot_seqid(u32 seqid, struct nfsd4_slot *slot)
  1144. {
  1145. dprintk("%s enter. seqid %d slot->sl_seqid %d\n", __func__, seqid,
  1146. slot->sl_seqid);
  1147. /* The slot is in use, and no response has been sent. */
  1148. if (slot->sl_inuse) {
  1149. if (seqid == slot->sl_seqid)
  1150. return nfserr_jukebox;
  1151. else
  1152. return nfserr_seq_misordered;
  1153. }
  1154. /* Normal */
  1155. if (likely(seqid == slot->sl_seqid + 1))
  1156. return nfs_ok;
  1157. /* Replay */
  1158. if (seqid == slot->sl_seqid)
  1159. return nfserr_replay_cache;
  1160. /* Wraparound */
  1161. if (seqid == 1 && (slot->sl_seqid + 1) == 0)
  1162. return nfs_ok;
  1163. /* Misordered replay or misordered new request */
  1164. return nfserr_seq_misordered;
  1165. }
  1166. __be32
  1167. nfsd4_create_session(struct svc_rqst *rqstp,
  1168. struct nfsd4_compound_state *cstate,
  1169. struct nfsd4_create_session *cr_ses)
  1170. {
  1171. u32 ip_addr = svc_addr_in(rqstp)->sin_addr.s_addr;
  1172. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1173. struct nfs4_client *conf, *unconf;
  1174. struct nfsd4_slot *slot = NULL;
  1175. int status = 0;
  1176. nfs4_lock_state();
  1177. unconf = find_unconfirmed_client(&cr_ses->clientid);
  1178. conf = find_confirmed_client(&cr_ses->clientid);
  1179. if (conf) {
  1180. slot = &conf->cl_slot;
  1181. status = check_slot_seqid(cr_ses->seqid, slot);
  1182. if (status == nfserr_replay_cache) {
  1183. dprintk("Got a create_session replay! seqid= %d\n",
  1184. slot->sl_seqid);
  1185. cstate->slot = slot;
  1186. cstate->status = status;
  1187. /* Return the cached reply status */
  1188. status = nfsd4_replay_cache_entry(resp, NULL);
  1189. goto out;
  1190. } else if (cr_ses->seqid != conf->cl_slot.sl_seqid + 1) {
  1191. status = nfserr_seq_misordered;
  1192. dprintk("Sequence misordered!\n");
  1193. dprintk("Expected seqid= %d but got seqid= %d\n",
  1194. slot->sl_seqid, cr_ses->seqid);
  1195. goto out;
  1196. }
  1197. conf->cl_slot.sl_seqid++;
  1198. } else if (unconf) {
  1199. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  1200. (ip_addr != unconf->cl_addr)) {
  1201. status = nfserr_clid_inuse;
  1202. goto out;
  1203. }
  1204. slot = &unconf->cl_slot;
  1205. status = check_slot_seqid(cr_ses->seqid, slot);
  1206. if (status) {
  1207. /* an unconfirmed replay returns misordered */
  1208. status = nfserr_seq_misordered;
  1209. goto out;
  1210. }
  1211. slot->sl_seqid++; /* from 0 to 1 */
  1212. move_to_confirmed(unconf);
  1213. /*
  1214. * We do not support RDMA or persistent sessions
  1215. */
  1216. cr_ses->flags &= ~SESSION4_PERSIST;
  1217. cr_ses->flags &= ~SESSION4_RDMA;
  1218. conf = unconf;
  1219. } else {
  1220. status = nfserr_stale_clientid;
  1221. goto out;
  1222. }
  1223. status = alloc_init_session(rqstp, conf, cr_ses);
  1224. if (status)
  1225. goto out;
  1226. memcpy(cr_ses->sessionid.data, conf->cl_sessionid.data,
  1227. NFS4_MAX_SESSIONID_LEN);
  1228. cr_ses->seqid = slot->sl_seqid;
  1229. slot->sl_inuse = true;
  1230. cstate->slot = slot;
  1231. /* Ensure a page is used for the cache */
  1232. slot->sl_cache_entry.ce_cachethis = 1;
  1233. out:
  1234. nfs4_unlock_state();
  1235. dprintk("%s returns %d\n", __func__, ntohl(status));
  1236. return status;
  1237. }
  1238. __be32
  1239. nfsd4_destroy_session(struct svc_rqst *r,
  1240. struct nfsd4_compound_state *cstate,
  1241. struct nfsd4_destroy_session *sessionid)
  1242. {
  1243. struct nfsd4_session *ses;
  1244. u32 status = nfserr_badsession;
  1245. /* Notes:
  1246. * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
  1247. * - Should we return nfserr_back_chan_busy if waiting for
  1248. * callbacks on to-be-destroyed session?
  1249. * - Do we need to clear any callback info from previous session?
  1250. */
  1251. dump_sessionid(__func__, &sessionid->sessionid);
  1252. spin_lock(&sessionid_lock);
  1253. ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
  1254. if (!ses) {
  1255. spin_unlock(&sessionid_lock);
  1256. goto out;
  1257. }
  1258. unhash_session(ses);
  1259. spin_unlock(&sessionid_lock);
  1260. /* wait for callbacks */
  1261. shutdown_callback_client(ses->se_client);
  1262. nfsd4_put_session(ses);
  1263. status = nfs_ok;
  1264. out:
  1265. dprintk("%s returns %d\n", __func__, ntohl(status));
  1266. return status;
  1267. }
  1268. __be32
  1269. nfsd4_sequence(struct svc_rqst *rqstp,
  1270. struct nfsd4_compound_state *cstate,
  1271. struct nfsd4_sequence *seq)
  1272. {
  1273. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1274. struct nfsd4_session *session;
  1275. struct nfsd4_slot *slot;
  1276. int status;
  1277. if (resp->opcnt != 1)
  1278. return nfserr_sequence_pos;
  1279. spin_lock(&sessionid_lock);
  1280. status = nfserr_badsession;
  1281. session = find_in_sessionid_hashtbl(&seq->sessionid);
  1282. if (!session)
  1283. goto out;
  1284. status = nfserr_badslot;
  1285. if (seq->slotid >= session->se_fchannel.maxreqs)
  1286. goto out;
  1287. slot = &session->se_slots[seq->slotid];
  1288. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  1289. status = check_slot_seqid(seq->seqid, slot);
  1290. if (status == nfserr_replay_cache) {
  1291. cstate->slot = slot;
  1292. cstate->session = session;
  1293. /* Return the cached reply status and set cstate->status
  1294. * for nfsd4_svc_encode_compoundres processing */
  1295. status = nfsd4_replay_cache_entry(resp, seq);
  1296. cstate->status = nfserr_replay_cache;
  1297. goto replay_cache;
  1298. }
  1299. if (status)
  1300. goto out;
  1301. /* Success! bump slot seqid */
  1302. slot->sl_inuse = true;
  1303. slot->sl_seqid = seq->seqid;
  1304. slot->sl_cache_entry.ce_cachethis = seq->cachethis;
  1305. /* Always set the cache entry cachethis for solo sequence */
  1306. if (nfsd4_is_solo_sequence(resp))
  1307. slot->sl_cache_entry.ce_cachethis = 1;
  1308. cstate->slot = slot;
  1309. cstate->session = session;
  1310. replay_cache:
  1311. /* Renew the clientid on success and on replay.
  1312. * Hold a session reference until done processing the compound:
  1313. * nfsd4_put_session called only if the cstate slot is set.
  1314. */
  1315. renew_client(session->se_client);
  1316. nfsd4_get_session(session);
  1317. out:
  1318. spin_unlock(&sessionid_lock);
  1319. dprintk("%s: return %d\n", __func__, ntohl(status));
  1320. return status;
  1321. }
  1322. __be32
  1323. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1324. struct nfsd4_setclientid *setclid)
  1325. {
  1326. struct sockaddr_in *sin = svc_addr_in(rqstp);
  1327. struct xdr_netobj clname = {
  1328. .len = setclid->se_namelen,
  1329. .data = setclid->se_name,
  1330. };
  1331. nfs4_verifier clverifier = setclid->se_verf;
  1332. unsigned int strhashval;
  1333. struct nfs4_client *conf, *unconf, *new;
  1334. __be32 status;
  1335. char *princ;
  1336. char dname[HEXDIR_LEN];
  1337. if (!check_name(clname))
  1338. return nfserr_inval;
  1339. status = nfs4_make_rec_clidname(dname, &clname);
  1340. if (status)
  1341. return status;
  1342. /*
  1343. * XXX The Duplicate Request Cache (DRC) has been checked (??)
  1344. * We get here on a DRC miss.
  1345. */
  1346. strhashval = clientstr_hashval(dname);
  1347. nfs4_lock_state();
  1348. conf = find_confirmed_client_by_str(dname, strhashval, false);
  1349. if (conf) {
  1350. /* RFC 3530 14.2.33 CASE 0: */
  1351. status = nfserr_clid_inuse;
  1352. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  1353. dprintk("NFSD: setclientid: string in use by client"
  1354. " at %pI4\n", &conf->cl_addr);
  1355. goto out;
  1356. }
  1357. }
  1358. /*
  1359. * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
  1360. * has a description of SETCLIENTID request processing consisting
  1361. * of 5 bullet points, labeled as CASE0 - CASE4 below.
  1362. */
  1363. unconf = find_unconfirmed_client_by_str(dname, strhashval, false);
  1364. status = nfserr_resource;
  1365. if (!conf) {
  1366. /*
  1367. * RFC 3530 14.2.33 CASE 4:
  1368. * placed first, because it is the normal case
  1369. */
  1370. if (unconf)
  1371. expire_client(unconf);
  1372. new = create_client(clname, dname);
  1373. if (new == NULL)
  1374. goto out;
  1375. gen_clid(new);
  1376. } else if (same_verf(&conf->cl_verifier, &clverifier)) {
  1377. /*
  1378. * RFC 3530 14.2.33 CASE 1:
  1379. * probable callback update
  1380. */
  1381. if (unconf) {
  1382. /* Note this is removing unconfirmed {*x***},
  1383. * which is stronger than RFC recommended {vxc**}.
  1384. * This has the advantage that there is at most
  1385. * one {*x***} in either list at any time.
  1386. */
  1387. expire_client(unconf);
  1388. }
  1389. new = create_client(clname, dname);
  1390. if (new == NULL)
  1391. goto out;
  1392. copy_clid(new, conf);
  1393. } else if (!unconf) {
  1394. /*
  1395. * RFC 3530 14.2.33 CASE 2:
  1396. * probable client reboot; state will be removed if
  1397. * confirmed.
  1398. */
  1399. new = create_client(clname, dname);
  1400. if (new == NULL)
  1401. goto out;
  1402. gen_clid(new);
  1403. } else {
  1404. /*
  1405. * RFC 3530 14.2.33 CASE 3:
  1406. * probable client reboot; state will be removed if
  1407. * confirmed.
  1408. */
  1409. expire_client(unconf);
  1410. new = create_client(clname, dname);
  1411. if (new == NULL)
  1412. goto out;
  1413. gen_clid(new);
  1414. }
  1415. copy_verf(new, &clverifier);
  1416. new->cl_addr = sin->sin_addr.s_addr;
  1417. new->cl_flavor = rqstp->rq_flavor;
  1418. princ = svc_gss_principal(rqstp);
  1419. if (princ) {
  1420. new->cl_principal = kstrdup(princ, GFP_KERNEL);
  1421. if (new->cl_principal == NULL) {
  1422. free_client(new);
  1423. goto out;
  1424. }
  1425. }
  1426. copy_cred(&new->cl_cred, &rqstp->rq_cred);
  1427. gen_confirm(new);
  1428. gen_callback(new, setclid);
  1429. add_to_unconfirmed(new, strhashval);
  1430. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  1431. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  1432. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  1433. status = nfs_ok;
  1434. out:
  1435. nfs4_unlock_state();
  1436. return status;
  1437. }
  1438. /*
  1439. * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
  1440. * a description of SETCLIENTID_CONFIRM request processing consisting of 4
  1441. * bullets, labeled as CASE1 - CASE4 below.
  1442. */
  1443. __be32
  1444. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  1445. struct nfsd4_compound_state *cstate,
  1446. struct nfsd4_setclientid_confirm *setclientid_confirm)
  1447. {
  1448. struct sockaddr_in *sin = svc_addr_in(rqstp);
  1449. struct nfs4_client *conf, *unconf;
  1450. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  1451. clientid_t * clid = &setclientid_confirm->sc_clientid;
  1452. __be32 status;
  1453. if (STALE_CLIENTID(clid))
  1454. return nfserr_stale_clientid;
  1455. /*
  1456. * XXX The Duplicate Request Cache (DRC) has been checked (??)
  1457. * We get here on a DRC miss.
  1458. */
  1459. nfs4_lock_state();
  1460. conf = find_confirmed_client(clid);
  1461. unconf = find_unconfirmed_client(clid);
  1462. status = nfserr_clid_inuse;
  1463. if (conf && conf->cl_addr != sin->sin_addr.s_addr)
  1464. goto out;
  1465. if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
  1466. goto out;
  1467. /*
  1468. * section 14.2.34 of RFC 3530 has a description of
  1469. * SETCLIENTID_CONFIRM request processing consisting
  1470. * of 4 bullet points, labeled as CASE1 - CASE4 below.
  1471. */
  1472. if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
  1473. /*
  1474. * RFC 3530 14.2.34 CASE 1:
  1475. * callback update
  1476. */
  1477. if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
  1478. status = nfserr_clid_inuse;
  1479. else {
  1480. /* XXX: We just turn off callbacks until we can handle
  1481. * change request correctly. */
  1482. atomic_set(&conf->cl_cb_conn.cb_set, 0);
  1483. expire_client(unconf);
  1484. status = nfs_ok;
  1485. }
  1486. } else if (conf && !unconf) {
  1487. /*
  1488. * RFC 3530 14.2.34 CASE 2:
  1489. * probable retransmitted request; play it safe and
  1490. * do nothing.
  1491. */
  1492. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
  1493. status = nfserr_clid_inuse;
  1494. else
  1495. status = nfs_ok;
  1496. } else if (!conf && unconf
  1497. && same_verf(&unconf->cl_confirm, &confirm)) {
  1498. /*
  1499. * RFC 3530 14.2.34 CASE 3:
  1500. * Normal case; new or rebooted client:
  1501. */
  1502. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
  1503. status = nfserr_clid_inuse;
  1504. } else {
  1505. unsigned int hash =
  1506. clientstr_hashval(unconf->cl_recdir);
  1507. conf = find_confirmed_client_by_str(unconf->cl_recdir,
  1508. hash, false);
  1509. if (conf) {
  1510. nfsd4_remove_clid_dir(conf);
  1511. expire_client(conf);
  1512. }
  1513. move_to_confirmed(unconf);
  1514. conf = unconf;
  1515. nfsd4_probe_callback(conf);
  1516. status = nfs_ok;
  1517. }
  1518. } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
  1519. && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
  1520. &confirm)))) {
  1521. /*
  1522. * RFC 3530 14.2.34 CASE 4:
  1523. * Client probably hasn't noticed that we rebooted yet.
  1524. */
  1525. status = nfserr_stale_clientid;
  1526. } else {
  1527. /* check that we have hit one of the cases...*/
  1528. status = nfserr_clid_inuse;
  1529. }
  1530. out:
  1531. nfs4_unlock_state();
  1532. return status;
  1533. }
  1534. /* OPEN Share state helper functions */
  1535. static inline struct nfs4_file *
  1536. alloc_init_file(struct inode *ino)
  1537. {
  1538. struct nfs4_file *fp;
  1539. unsigned int hashval = file_hashval(ino);
  1540. fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
  1541. if (fp) {
  1542. atomic_set(&fp->fi_ref, 1);
  1543. INIT_LIST_HEAD(&fp->fi_hash);
  1544. INIT_LIST_HEAD(&fp->fi_stateids);
  1545. INIT_LIST_HEAD(&fp->fi_delegations);
  1546. spin_lock(&recall_lock);
  1547. list_add(&fp->fi_hash, &file_hashtbl[hashval]);
  1548. spin_unlock(&recall_lock);
  1549. fp->fi_inode = igrab(ino);
  1550. fp->fi_id = current_fileid++;
  1551. fp->fi_had_conflict = false;
  1552. return fp;
  1553. }
  1554. return NULL;
  1555. }
  1556. static void
  1557. nfsd4_free_slab(struct kmem_cache **slab)
  1558. {
  1559. if (*slab == NULL)
  1560. return;
  1561. kmem_cache_destroy(*slab);
  1562. *slab = NULL;
  1563. }
  1564. void
  1565. nfsd4_free_slabs(void)
  1566. {
  1567. nfsd4_free_slab(&stateowner_slab);
  1568. nfsd4_free_slab(&file_slab);
  1569. nfsd4_free_slab(&stateid_slab);
  1570. nfsd4_free_slab(&deleg_slab);
  1571. }
  1572. static int
  1573. nfsd4_init_slabs(void)
  1574. {
  1575. stateowner_slab = kmem_cache_create("nfsd4_stateowners",
  1576. sizeof(struct nfs4_stateowner), 0, 0, NULL);
  1577. if (stateowner_slab == NULL)
  1578. goto out_nomem;
  1579. file_slab = kmem_cache_create("nfsd4_files",
  1580. sizeof(struct nfs4_file), 0, 0, NULL);
  1581. if (file_slab == NULL)
  1582. goto out_nomem;
  1583. stateid_slab = kmem_cache_create("nfsd4_stateids",
  1584. sizeof(struct nfs4_stateid), 0, 0, NULL);
  1585. if (stateid_slab == NULL)
  1586. goto out_nomem;
  1587. deleg_slab = kmem_cache_create("nfsd4_delegations",
  1588. sizeof(struct nfs4_delegation), 0, 0, NULL);
  1589. if (deleg_slab == NULL)
  1590. goto out_nomem;
  1591. return 0;
  1592. out_nomem:
  1593. nfsd4_free_slabs();
  1594. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  1595. return -ENOMEM;
  1596. }
  1597. void
  1598. nfs4_free_stateowner(struct kref *kref)
  1599. {
  1600. struct nfs4_stateowner *sop =
  1601. container_of(kref, struct nfs4_stateowner, so_ref);
  1602. kfree(sop->so_owner.data);
  1603. kmem_cache_free(stateowner_slab, sop);
  1604. }
  1605. static inline struct nfs4_stateowner *
  1606. alloc_stateowner(struct xdr_netobj *owner)
  1607. {
  1608. struct nfs4_stateowner *sop;
  1609. if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
  1610. if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
  1611. memcpy(sop->so_owner.data, owner->data, owner->len);
  1612. sop->so_owner.len = owner->len;
  1613. kref_init(&sop->so_ref);
  1614. return sop;
  1615. }
  1616. kmem_cache_free(stateowner_slab, sop);
  1617. }
  1618. return NULL;
  1619. }
  1620. static struct nfs4_stateowner *
  1621. alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
  1622. struct nfs4_stateowner *sop;
  1623. struct nfs4_replay *rp;
  1624. unsigned int idhashval;
  1625. if (!(sop = alloc_stateowner(&open->op_owner)))
  1626. return NULL;
  1627. idhashval = ownerid_hashval(current_ownerid);
  1628. INIT_LIST_HEAD(&sop->so_idhash);
  1629. INIT_LIST_HEAD(&sop->so_strhash);
  1630. INIT_LIST_HEAD(&sop->so_perclient);
  1631. INIT_LIST_HEAD(&sop->so_stateids);
  1632. INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
  1633. INIT_LIST_HEAD(&sop->so_close_lru);
  1634. sop->so_time = 0;
  1635. list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
  1636. list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
  1637. list_add(&sop->so_perclient, &clp->cl_openowners);
  1638. sop->so_is_open_owner = 1;
  1639. sop->so_id = current_ownerid++;
  1640. sop->so_client = clp;
  1641. sop->so_seqid = open->op_seqid;
  1642. sop->so_confirmed = 0;
  1643. rp = &sop->so_replay;
  1644. rp->rp_status = nfserr_serverfault;
  1645. rp->rp_buflen = 0;
  1646. rp->rp_buf = rp->rp_ibuf;
  1647. return sop;
  1648. }
  1649. static inline void
  1650. init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  1651. struct nfs4_stateowner *sop = open->op_stateowner;
  1652. unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
  1653. INIT_LIST_HEAD(&stp->st_hash);
  1654. INIT_LIST_HEAD(&stp->st_perstateowner);
  1655. INIT_LIST_HEAD(&stp->st_lockowners);
  1656. INIT_LIST_HEAD(&stp->st_perfile);
  1657. list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
  1658. list_add(&stp->st_perstateowner, &sop->so_stateids);
  1659. list_add(&stp->st_perfile, &fp->fi_stateids);
  1660. stp->st_stateowner = sop;
  1661. get_nfs4_file(fp);
  1662. stp->st_file = fp;
  1663. stp->st_stateid.si_boot = get_seconds();
  1664. stp->st_stateid.si_stateownerid = sop->so_id;
  1665. stp->st_stateid.si_fileid = fp->fi_id;
  1666. stp->st_stateid.si_generation = 0;
  1667. stp->st_access_bmap = 0;
  1668. stp->st_deny_bmap = 0;
  1669. __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
  1670. &stp->st_access_bmap);
  1671. __set_bit(open->op_share_deny, &stp->st_deny_bmap);
  1672. stp->st_openstp = NULL;
  1673. }
  1674. static void
  1675. move_to_close_lru(struct nfs4_stateowner *sop)
  1676. {
  1677. dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
  1678. list_move_tail(&sop->so_close_lru, &close_lru);
  1679. sop->so_time = get_seconds();
  1680. }
  1681. static int
  1682. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
  1683. clientid_t *clid)
  1684. {
  1685. return (sop->so_owner.len == owner->len) &&
  1686. 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
  1687. (sop->so_client->cl_clientid.cl_id == clid->cl_id);
  1688. }
  1689. static struct nfs4_stateowner *
  1690. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
  1691. {
  1692. struct nfs4_stateowner *so = NULL;
  1693. list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
  1694. if (same_owner_str(so, &open->op_owner, &open->op_clientid))
  1695. return so;
  1696. }
  1697. return NULL;
  1698. }
  1699. /* search file_hashtbl[] for file */
  1700. static struct nfs4_file *
  1701. find_file(struct inode *ino)
  1702. {
  1703. unsigned int hashval = file_hashval(ino);
  1704. struct nfs4_file *fp;
  1705. spin_lock(&recall_lock);
  1706. list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  1707. if (fp->fi_inode == ino) {
  1708. get_nfs4_file(fp);
  1709. spin_unlock(&recall_lock);
  1710. return fp;
  1711. }
  1712. }
  1713. spin_unlock(&recall_lock);
  1714. return NULL;
  1715. }
  1716. static inline int access_valid(u32 x, u32 minorversion)
  1717. {
  1718. if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
  1719. return 0;
  1720. if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
  1721. return 0;
  1722. x &= ~NFS4_SHARE_ACCESS_MASK;
  1723. if (minorversion && x) {
  1724. if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
  1725. return 0;
  1726. if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
  1727. return 0;
  1728. x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
  1729. }
  1730. if (x)
  1731. return 0;
  1732. return 1;
  1733. }
  1734. static inline int deny_valid(u32 x)
  1735. {
  1736. /* Note: unlike access bits, deny bits may be zero. */
  1737. return x <= NFS4_SHARE_DENY_BOTH;
  1738. }
  1739. /*
  1740. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  1741. * st_{access,deny}_bmap field of the stateid, in order to track not
  1742. * only what share bits are currently in force, but also what
  1743. * combinations of share bits previous opens have used. This allows us
  1744. * to enforce the recommendation of rfc 3530 14.2.19 that the server
  1745. * return an error if the client attempt to downgrade to a combination
  1746. * of share bits not explicable by closing some of its previous opens.
  1747. *
  1748. * XXX: This enforcement is actually incomplete, since we don't keep
  1749. * track of access/deny bit combinations; so, e.g., we allow:
  1750. *
  1751. * OPEN allow read, deny write
  1752. * OPEN allow both, deny none
  1753. * DOWNGRADE allow read, deny none
  1754. *
  1755. * which we should reject.
  1756. */
  1757. static void
  1758. set_access(unsigned int *access, unsigned long bmap) {
  1759. int i;
  1760. *access = 0;
  1761. for (i = 1; i < 4; i++) {
  1762. if (test_bit(i, &bmap))
  1763. *access |= i;
  1764. }
  1765. }
  1766. static void
  1767. set_deny(unsigned int *deny, unsigned long bmap) {
  1768. int i;
  1769. *deny = 0;
  1770. for (i = 0; i < 4; i++) {
  1771. if (test_bit(i, &bmap))
  1772. *deny |= i ;
  1773. }
  1774. }
  1775. static int
  1776. test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
  1777. unsigned int access, deny;
  1778. set_access(&access, stp->st_access_bmap);
  1779. set_deny(&deny, stp->st_deny_bmap);
  1780. if ((access & open->op_share_deny) || (deny & open->op_share_access))
  1781. return 0;
  1782. return 1;
  1783. }
  1784. /*
  1785. * Called to check deny when READ with all zero stateid or
  1786. * WRITE with all zero or all one stateid
  1787. */
  1788. static __be32
  1789. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  1790. {
  1791. struct inode *ino = current_fh->fh_dentry->d_inode;
  1792. struct nfs4_file *fp;
  1793. struct nfs4_stateid *stp;
  1794. __be32 ret;
  1795. dprintk("NFSD: nfs4_share_conflict\n");
  1796. fp = find_file(ino);
  1797. if (!fp)
  1798. return nfs_ok;
  1799. ret = nfserr_locked;
  1800. /* Search for conflicting share reservations */
  1801. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  1802. if (test_bit(deny_type, &stp->st_deny_bmap) ||
  1803. test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
  1804. goto out;
  1805. }
  1806. ret = nfs_ok;
  1807. out:
  1808. put_nfs4_file(fp);
  1809. return ret;
  1810. }
  1811. static inline void
  1812. nfs4_file_downgrade(struct file *filp, unsigned int share_access)
  1813. {
  1814. if (share_access & NFS4_SHARE_ACCESS_WRITE) {
  1815. drop_file_write_access(filp);
  1816. filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
  1817. }
  1818. }
  1819. /*
  1820. * Spawn a thread to perform a recall on the delegation represented
  1821. * by the lease (file_lock)
  1822. *
  1823. * Called from break_lease() with lock_kernel() held.
  1824. * Note: we assume break_lease will only call this *once* for any given
  1825. * lease.
  1826. */
  1827. static
  1828. void nfsd_break_deleg_cb(struct file_lock *fl)
  1829. {
  1830. struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
  1831. dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
  1832. if (!dp)
  1833. return;
  1834. /* We're assuming the state code never drops its reference
  1835. * without first removing the lease. Since we're in this lease
  1836. * callback (and since the lease code is serialized by the kernel
  1837. * lock) we know the server hasn't removed the lease yet, we know
  1838. * it's safe to take a reference: */
  1839. atomic_inc(&dp->dl_count);
  1840. atomic_inc(&dp->dl_client->cl_count);
  1841. spin_lock(&recall_lock);
  1842. list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
  1843. spin_unlock(&recall_lock);
  1844. /* only place dl_time is set. protected by lock_kernel*/
  1845. dp->dl_time = get_seconds();
  1846. /*
  1847. * We don't want the locks code to timeout the lease for us;
  1848. * we'll remove it ourself if the delegation isn't returned
  1849. * in time.
  1850. */
  1851. fl->fl_break_time = 0;
  1852. dp->dl_file->fi_had_conflict = true;
  1853. nfsd4_cb_recall(dp);
  1854. }
  1855. /*
  1856. * The file_lock is being reapd.
  1857. *
  1858. * Called by locks_free_lock() with lock_kernel() held.
  1859. */
  1860. static
  1861. void nfsd_release_deleg_cb(struct file_lock *fl)
  1862. {
  1863. struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
  1864. dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
  1865. if (!(fl->fl_flags & FL_LEASE) || !dp)
  1866. return;
  1867. dp->dl_flock = NULL;
  1868. }
  1869. /*
  1870. * Set the delegation file_lock back pointer.
  1871. *
  1872. * Called from setlease() with lock_kernel() held.
  1873. */
  1874. static
  1875. void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
  1876. {
  1877. struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
  1878. dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
  1879. if (!dp)
  1880. return;
  1881. dp->dl_flock = new;
  1882. }
  1883. /*
  1884. * Called from setlease() with lock_kernel() held
  1885. */
  1886. static
  1887. int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
  1888. {
  1889. struct nfs4_delegation *onlistd =
  1890. (struct nfs4_delegation *)onlist->fl_owner;
  1891. struct nfs4_delegation *tryd =
  1892. (struct nfs4_delegation *)try->fl_owner;
  1893. if (onlist->fl_lmops != try->fl_lmops)
  1894. return 0;
  1895. return onlistd->dl_client == tryd->dl_client;
  1896. }
  1897. static
  1898. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  1899. {
  1900. if (arg & F_UNLCK)
  1901. return lease_modify(onlist, arg);
  1902. else
  1903. return -EAGAIN;
  1904. }
  1905. static struct lock_manager_operations nfsd_lease_mng_ops = {
  1906. .fl_break = nfsd_break_deleg_cb,
  1907. .fl_release_private = nfsd_release_deleg_cb,
  1908. .fl_copy_lock = nfsd_copy_lock_deleg_cb,
  1909. .fl_mylease = nfsd_same_client_deleg_cb,
  1910. .fl_change = nfsd_change_deleg_cb,
  1911. };
  1912. __be32
  1913. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  1914. struct nfsd4_open *open)
  1915. {
  1916. clientid_t *clientid = &open->op_clientid;
  1917. struct nfs4_client *clp = NULL;
  1918. unsigned int strhashval;
  1919. struct nfs4_stateowner *sop = NULL;
  1920. if (!check_name(open->op_owner))
  1921. return nfserr_inval;
  1922. if (STALE_CLIENTID(&open->op_clientid))
  1923. return nfserr_stale_clientid;
  1924. strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
  1925. sop = find_openstateowner_str(strhashval, open);
  1926. open->op_stateowner = sop;
  1927. if (!sop) {
  1928. /* Make sure the client's lease hasn't expired. */
  1929. clp = find_confirmed_client(clientid);
  1930. if (clp == NULL)
  1931. return nfserr_expired;
  1932. goto renew;
  1933. }
  1934. /* When sessions are used, skip open sequenceid processing */
  1935. if (nfsd4_has_session(cstate))
  1936. goto renew;
  1937. if (!sop->so_confirmed) {
  1938. /* Replace unconfirmed owners without checking for replay. */
  1939. clp = sop->so_client;
  1940. release_openowner(sop);
  1941. open->op_stateowner = NULL;
  1942. goto renew;
  1943. }
  1944. if (open->op_seqid == sop->so_seqid - 1) {
  1945. if (sop->so_replay.rp_buflen)
  1946. return nfserr_replay_me;
  1947. /* The original OPEN failed so spectacularly
  1948. * that we don't even have replay data saved!
  1949. * Therefore, we have no choice but to continue
  1950. * processing this OPEN; presumably, we'll
  1951. * fail again for the same reason.
  1952. */
  1953. dprintk("nfsd4_process_open1: replay with no replay cache\n");
  1954. goto renew;
  1955. }
  1956. if (open->op_seqid != sop->so_seqid)
  1957. return nfserr_bad_seqid;
  1958. renew:
  1959. if (open->op_stateowner == NULL) {
  1960. sop = alloc_init_open_stateowner(strhashval, clp, open);
  1961. if (sop == NULL)
  1962. return nfserr_resource;
  1963. open->op_stateowner = sop;
  1964. }
  1965. list_del_init(&sop->so_close_lru);
  1966. renew_client(sop->so_client);
  1967. return nfs_ok;
  1968. }
  1969. static inline __be32
  1970. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  1971. {
  1972. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  1973. return nfserr_openmode;
  1974. else
  1975. return nfs_ok;
  1976. }
  1977. static struct nfs4_delegation *
  1978. find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
  1979. {
  1980. struct nfs4_delegation *dp;
  1981. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
  1982. if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
  1983. return dp;
  1984. }
  1985. return NULL;
  1986. }
  1987. static __be32
  1988. nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
  1989. struct nfs4_delegation **dp)
  1990. {
  1991. int flags;
  1992. __be32 status = nfserr_bad_stateid;
  1993. *dp = find_delegation_file(fp, &open->op_delegate_stateid);
  1994. if (*dp == NULL)
  1995. goto out;
  1996. flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
  1997. RD_STATE : WR_STATE;
  1998. status = nfs4_check_delegmode(*dp, flags);
  1999. if (status)
  2000. *dp = NULL;
  2001. out:
  2002. if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
  2003. return nfs_ok;
  2004. if (status)
  2005. return status;
  2006. open->op_stateowner->so_confirmed = 1;
  2007. return nfs_ok;
  2008. }
  2009. static __be32
  2010. nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
  2011. {
  2012. struct nfs4_stateid *local;
  2013. __be32 status = nfserr_share_denied;
  2014. struct nfs4_stateowner *sop = open->op_stateowner;
  2015. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  2016. /* ignore lock owners */
  2017. if (local->st_stateowner->so_is_open_owner == 0)
  2018. continue;
  2019. /* remember if we have seen this open owner */
  2020. if (local->st_stateowner == sop)
  2021. *stpp = local;
  2022. /* check for conflicting share reservations */
  2023. if (!test_share(local, open))
  2024. goto out;
  2025. }
  2026. status = 0;
  2027. out:
  2028. return status;
  2029. }
  2030. static inline struct nfs4_stateid *
  2031. nfs4_alloc_stateid(void)
  2032. {
  2033. return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
  2034. }
  2035. static __be32
  2036. nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
  2037. struct nfs4_delegation *dp,
  2038. struct svc_fh *cur_fh, int flags)
  2039. {
  2040. struct nfs4_stateid *stp;
  2041. stp = nfs4_alloc_stateid();
  2042. if (stp == NULL)
  2043. return nfserr_resource;
  2044. if (dp) {
  2045. get_file(dp->dl_vfs_file);
  2046. stp->st_vfs_file = dp->dl_vfs_file;
  2047. } else {
  2048. __be32 status;
  2049. status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
  2050. &stp->st_vfs_file);
  2051. if (status) {
  2052. if (status == nfserr_dropit)
  2053. status = nfserr_jukebox;
  2054. kmem_cache_free(stateid_slab, stp);
  2055. return status;
  2056. }
  2057. }
  2058. *stpp = stp;
  2059. return 0;
  2060. }
  2061. static inline __be32
  2062. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  2063. struct nfsd4_open *open)
  2064. {
  2065. struct iattr iattr = {
  2066. .ia_valid = ATTR_SIZE,
  2067. .ia_size = 0,
  2068. };
  2069. if (!open->op_truncate)
  2070. return 0;
  2071. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  2072. return nfserr_inval;
  2073. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  2074. }
  2075. static __be32
  2076. nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
  2077. {
  2078. struct file *filp = stp->st_vfs_file;
  2079. struct inode *inode = filp->f_path.dentry->d_inode;
  2080. unsigned int share_access, new_writer;
  2081. __be32 status;
  2082. set_access(&share_access, stp->st_access_bmap);
  2083. new_writer = (~share_access) & open->op_share_access
  2084. & NFS4_SHARE_ACCESS_WRITE;
  2085. if (new_writer) {
  2086. int err = get_write_access(inode);
  2087. if (err)
  2088. return nfserrno(err);
  2089. err = mnt_want_write(cur_fh->fh_export->ex_path.mnt);
  2090. if (err)
  2091. return nfserrno(err);
  2092. file_take_write(filp);
  2093. }
  2094. status = nfsd4_truncate(rqstp, cur_fh, open);
  2095. if (status) {
  2096. if (new_writer)
  2097. put_write_access(inode);
  2098. return status;
  2099. }
  2100. /* remember the open */
  2101. filp->f_mode |= open->op_share_access;
  2102. __set_bit(open->op_share_access, &stp->st_access_bmap);
  2103. __set_bit(open->op_share_deny, &stp->st_deny_bmap);
  2104. return nfs_ok;
  2105. }
  2106. static void
  2107. nfs4_set_claim_prev(struct nfsd4_open *open)
  2108. {
  2109. open->op_stateowner->so_confirmed = 1;
  2110. open->op_stateowner->so_client->cl_firststate = 1;
  2111. }
  2112. /*
  2113. * Attempt to hand out a delegation.
  2114. */
  2115. static void
  2116. nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
  2117. {
  2118. struct nfs4_delegation *dp;
  2119. struct nfs4_stateowner *sop = stp->st_stateowner;
  2120. struct nfs4_cb_conn *cb = &sop->so_client->cl_cb_conn;
  2121. struct file_lock fl, *flp = &fl;
  2122. int status, flag = 0;
  2123. flag = NFS4_OPEN_DELEGATE_NONE;
  2124. open->op_recall = 0;
  2125. switch (open->op_claim_type) {
  2126. case NFS4_OPEN_CLAIM_PREVIOUS:
  2127. if (!atomic_read(&cb->cb_set))
  2128. open->op_recall = 1;
  2129. flag = open->op_delegate_type;
  2130. if (flag == NFS4_OPEN_DELEGATE_NONE)
  2131. goto out;
  2132. break;
  2133. case NFS4_OPEN_CLAIM_NULL:
  2134. /* Let's not give out any delegations till everyone's
  2135. * had the chance to reclaim theirs.... */
  2136. if (locks_in_grace())
  2137. goto out;
  2138. if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
  2139. goto out;
  2140. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  2141. flag = NFS4_OPEN_DELEGATE_WRITE;
  2142. else
  2143. flag = NFS4_OPEN_DELEGATE_READ;
  2144. break;
  2145. default:
  2146. goto out;
  2147. }
  2148. dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
  2149. if (dp == NULL) {
  2150. flag = NFS4_OPEN_DELEGATE_NONE;
  2151. goto out;
  2152. }
  2153. locks_init_lock(&fl);
  2154. fl.fl_lmops = &nfsd_lease_mng_ops;
  2155. fl.fl_flags = FL_LEASE;
  2156. fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  2157. fl.fl_end = OFFSET_MAX;
  2158. fl.fl_owner = (fl_owner_t)dp;
  2159. fl.fl_file = stp->st_vfs_file;
  2160. fl.fl_pid = current->tgid;
  2161. /* vfs_setlease checks to see if delegation should be handed out.
  2162. * the lock_manager callbacks fl_mylease and fl_change are used
  2163. */
  2164. if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) {
  2165. dprintk("NFSD: setlease failed [%d], no delegation\n", status);
  2166. unhash_delegation(dp);
  2167. flag = NFS4_OPEN_DELEGATE_NONE;
  2168. goto out;
  2169. }
  2170. memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
  2171. dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
  2172. dp->dl_stateid.si_boot,
  2173. dp->dl_stateid.si_stateownerid,
  2174. dp->dl_stateid.si_fileid,
  2175. dp->dl_stateid.si_generation);
  2176. out:
  2177. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
  2178. && flag == NFS4_OPEN_DELEGATE_NONE
  2179. && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
  2180. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  2181. open->op_delegate_type = flag;
  2182. }
  2183. /*
  2184. * called with nfs4_lock_state() held.
  2185. */
  2186. __be32
  2187. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  2188. {
  2189. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2190. struct nfs4_file *fp = NULL;
  2191. struct inode *ino = current_fh->fh_dentry->d_inode;
  2192. struct nfs4_stateid *stp = NULL;
  2193. struct nfs4_delegation *dp = NULL;
  2194. __be32 status;
  2195. status = nfserr_inval;
  2196. if (!access_valid(open->op_share_access, resp->cstate.minorversion)
  2197. || !deny_valid(open->op_share_deny))
  2198. goto out;
  2199. /*
  2200. * Lookup file; if found, lookup stateid and check open request,
  2201. * and check for delegations in the process of being recalled.
  2202. * If not found, create the nfs4_file struct
  2203. */
  2204. fp = find_file(ino);
  2205. if (fp) {
  2206. if ((status = nfs4_check_open(fp, open, &stp)))
  2207. goto out;
  2208. status = nfs4_check_deleg(fp, open, &dp);
  2209. if (status)
  2210. goto out;
  2211. } else {
  2212. status = nfserr_bad_stateid;
  2213. if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
  2214. goto out;
  2215. status = nfserr_resource;
  2216. fp = alloc_init_file(ino);
  2217. if (fp == NULL)
  2218. goto out;
  2219. }
  2220. /*
  2221. * OPEN the file, or upgrade an existing OPEN.
  2222. * If truncate fails, the OPEN fails.
  2223. */
  2224. if (stp) {
  2225. /* Stateid was found, this is an OPEN upgrade */
  2226. status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
  2227. if (status)
  2228. goto out;
  2229. update_stateid(&stp->st_stateid);
  2230. } else {
  2231. /* Stateid was not found, this is a new OPEN */
  2232. int flags = 0;
  2233. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  2234. flags |= NFSD_MAY_READ;
  2235. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  2236. flags |= NFSD_MAY_WRITE;
  2237. status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
  2238. if (status)
  2239. goto out;
  2240. init_stateid(stp, fp, open);
  2241. status = nfsd4_truncate(rqstp, current_fh, open);
  2242. if (status) {
  2243. release_open_stateid(stp);
  2244. goto out;
  2245. }
  2246. if (nfsd4_has_session(&resp->cstate))
  2247. update_stateid(&stp->st_stateid);
  2248. }
  2249. memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
  2250. if (nfsd4_has_session(&resp->cstate))
  2251. open->op_stateowner->so_confirmed = 1;
  2252. /*
  2253. * Attempt to hand out a delegation. No error return, because the
  2254. * OPEN succeeds even if we fail.
  2255. */
  2256. nfs4_open_delegation(current_fh, open, stp);
  2257. status = nfs_ok;
  2258. dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
  2259. stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
  2260. stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
  2261. out:
  2262. if (fp)
  2263. put_nfs4_file(fp);
  2264. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  2265. nfs4_set_claim_prev(open);
  2266. /*
  2267. * To finish the open response, we just need to set the rflags.
  2268. */
  2269. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  2270. if (!open->op_stateowner->so_confirmed &&
  2271. !nfsd4_has_session(&resp->cstate))
  2272. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  2273. return status;
  2274. }
  2275. __be32
  2276. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2277. clientid_t *clid)
  2278. {
  2279. struct nfs4_client *clp;
  2280. __be32 status;
  2281. nfs4_lock_state();
  2282. dprintk("process_renew(%08x/%08x): starting\n",
  2283. clid->cl_boot, clid->cl_id);
  2284. status = nfserr_stale_clientid;
  2285. if (STALE_CLIENTID(clid))
  2286. goto out;
  2287. clp = find_confirmed_client(clid);
  2288. status = nfserr_expired;
  2289. if (clp == NULL) {
  2290. /* We assume the client took too long to RENEW. */
  2291. dprintk("nfsd4_renew: clientid not found!\n");
  2292. goto out;
  2293. }
  2294. renew_client(clp);
  2295. status = nfserr_cb_path_down;
  2296. if (!list_empty(&clp->cl_delegations)
  2297. && !atomic_read(&clp->cl_cb_conn.cb_set))
  2298. goto out;
  2299. status = nfs_ok;
  2300. out:
  2301. nfs4_unlock_state();
  2302. return status;
  2303. }
  2304. struct lock_manager nfsd4_manager = {
  2305. };
  2306. static void
  2307. nfsd4_end_grace(void)
  2308. {
  2309. dprintk("NFSD: end of grace period\n");
  2310. nfsd4_recdir_purge_old();
  2311. locks_end_grace(&nfsd4_manager);
  2312. }
  2313. static time_t
  2314. nfs4_laundromat(void)
  2315. {
  2316. struct nfs4_client *clp;
  2317. struct nfs4_stateowner *sop;
  2318. struct nfs4_delegation *dp;
  2319. struct list_head *pos, *next, reaplist;
  2320. time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
  2321. time_t t, clientid_val = NFSD_LEASE_TIME;
  2322. time_t u, test_val = NFSD_LEASE_TIME;
  2323. nfs4_lock_state();
  2324. dprintk("NFSD: laundromat service - starting\n");
  2325. if (locks_in_grace())
  2326. nfsd4_end_grace();
  2327. list_for_each_safe(pos, next, &client_lru) {
  2328. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2329. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  2330. t = clp->cl_time - cutoff;
  2331. if (clientid_val > t)
  2332. clientid_val = t;
  2333. break;
  2334. }
  2335. dprintk("NFSD: purging unused client (clientid %08x)\n",
  2336. clp->cl_clientid.cl_id);
  2337. nfsd4_remove_clid_dir(clp);
  2338. expire_client(clp);
  2339. }
  2340. INIT_LIST_HEAD(&reaplist);
  2341. spin_lock(&recall_lock);
  2342. list_for_each_safe(pos, next, &del_recall_lru) {
  2343. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2344. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  2345. u = dp->dl_time - cutoff;
  2346. if (test_val > u)
  2347. test_val = u;
  2348. break;
  2349. }
  2350. dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
  2351. dp, dp->dl_flock);
  2352. list_move(&dp->dl_recall_lru, &reaplist);
  2353. }
  2354. spin_unlock(&recall_lock);
  2355. list_for_each_safe(pos, next, &reaplist) {
  2356. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2357. list_del_init(&dp->dl_recall_lru);
  2358. unhash_delegation(dp);
  2359. }
  2360. test_val = NFSD_LEASE_TIME;
  2361. list_for_each_safe(pos, next, &close_lru) {
  2362. sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
  2363. if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
  2364. u = sop->so_time - cutoff;
  2365. if (test_val > u)
  2366. test_val = u;
  2367. break;
  2368. }
  2369. dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
  2370. sop->so_id);
  2371. release_openowner(sop);
  2372. }
  2373. if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
  2374. clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
  2375. nfs4_unlock_state();
  2376. return clientid_val;
  2377. }
  2378. static struct workqueue_struct *laundry_wq;
  2379. static void laundromat_main(struct work_struct *);
  2380. static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
  2381. static void
  2382. laundromat_main(struct work_struct *not_used)
  2383. {
  2384. time_t t;
  2385. t = nfs4_laundromat();
  2386. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  2387. queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
  2388. }
  2389. static struct nfs4_stateowner *
  2390. search_close_lru(u32 st_id, int flags)
  2391. {
  2392. struct nfs4_stateowner *local = NULL;
  2393. if (flags & CLOSE_STATE) {
  2394. list_for_each_entry(local, &close_lru, so_close_lru) {
  2395. if (local->so_id == st_id)
  2396. return local;
  2397. }
  2398. }
  2399. return NULL;
  2400. }
  2401. static inline int
  2402. nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
  2403. {
  2404. return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
  2405. }
  2406. static int
  2407. STALE_STATEID(stateid_t *stateid)
  2408. {
  2409. if (time_after((unsigned long)boot_time,
  2410. (unsigned long)stateid->si_boot)) {
  2411. dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
  2412. stateid->si_boot, stateid->si_stateownerid,
  2413. stateid->si_fileid, stateid->si_generation);
  2414. return 1;
  2415. }
  2416. return 0;
  2417. }
  2418. static int
  2419. EXPIRED_STATEID(stateid_t *stateid)
  2420. {
  2421. if (time_before((unsigned long)boot_time,
  2422. ((unsigned long)stateid->si_boot)) &&
  2423. time_before((unsigned long)(stateid->si_boot + lease_time), get_seconds())) {
  2424. dprintk("NFSD: expired stateid (%08x/%08x/%08x/%08x)!\n",
  2425. stateid->si_boot, stateid->si_stateownerid,
  2426. stateid->si_fileid, stateid->si_generation);
  2427. return 1;
  2428. }
  2429. return 0;
  2430. }
  2431. static __be32
  2432. stateid_error_map(stateid_t *stateid)
  2433. {
  2434. if (STALE_STATEID(stateid))
  2435. return nfserr_stale_stateid;
  2436. if (EXPIRED_STATEID(stateid))
  2437. return nfserr_expired;
  2438. dprintk("NFSD: bad stateid (%08x/%08x/%08x/%08x)!\n",
  2439. stateid->si_boot, stateid->si_stateownerid,
  2440. stateid->si_fileid, stateid->si_generation);
  2441. return nfserr_bad_stateid;
  2442. }
  2443. static inline int
  2444. access_permit_read(unsigned long access_bmap)
  2445. {
  2446. return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
  2447. test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
  2448. test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
  2449. }
  2450. static inline int
  2451. access_permit_write(unsigned long access_bmap)
  2452. {
  2453. return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
  2454. test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
  2455. }
  2456. static
  2457. __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
  2458. {
  2459. __be32 status = nfserr_openmode;
  2460. if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
  2461. goto out;
  2462. if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
  2463. goto out;
  2464. status = nfs_ok;
  2465. out:
  2466. return status;
  2467. }
  2468. static inline __be32
  2469. check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
  2470. {
  2471. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  2472. return nfs_ok;
  2473. else if (locks_in_grace()) {
  2474. /* Answer in remaining cases depends on existance of
  2475. * conflicting state; so we must wait out the grace period. */
  2476. return nfserr_grace;
  2477. } else if (flags & WR_STATE)
  2478. return nfs4_share_conflict(current_fh,
  2479. NFS4_SHARE_DENY_WRITE);
  2480. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  2481. return nfs4_share_conflict(current_fh,
  2482. NFS4_SHARE_DENY_READ);
  2483. }
  2484. /*
  2485. * Allow READ/WRITE during grace period on recovered state only for files
  2486. * that are not able to provide mandatory locking.
  2487. */
  2488. static inline int
  2489. grace_disallows_io(struct inode *inode)
  2490. {
  2491. return locks_in_grace() && mandatory_lock(inode);
  2492. }
  2493. static int check_stateid_generation(stateid_t *in, stateid_t *ref, int flags)
  2494. {
  2495. /*
  2496. * When sessions are used the stateid generation number is ignored
  2497. * when it is zero.
  2498. */
  2499. if ((flags & HAS_SESSION) && in->si_generation == 0)
  2500. goto out;
  2501. /* If the client sends us a stateid from the future, it's buggy: */
  2502. if (in->si_generation > ref->si_generation)
  2503. return nfserr_bad_stateid;
  2504. /*
  2505. * The following, however, can happen. For example, if the
  2506. * client sends an open and some IO at the same time, the open
  2507. * may bump si_generation while the IO is still in flight.
  2508. * Thanks to hard links and renames, the client never knows what
  2509. * file an open will affect. So it could avoid that situation
  2510. * only by serializing all opens and IO from the same open
  2511. * owner. To recover from the old_stateid error, the client
  2512. * will just have to retry the IO:
  2513. */
  2514. if (in->si_generation < ref->si_generation)
  2515. return nfserr_old_stateid;
  2516. out:
  2517. return nfs_ok;
  2518. }
  2519. static int is_delegation_stateid(stateid_t *stateid)
  2520. {
  2521. return stateid->si_fileid == 0;
  2522. }
  2523. /*
  2524. * Checks for stateid operations
  2525. */
  2526. __be32
  2527. nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
  2528. stateid_t *stateid, int flags, struct file **filpp)
  2529. {
  2530. struct nfs4_stateid *stp = NULL;
  2531. struct nfs4_delegation *dp = NULL;
  2532. struct svc_fh *current_fh = &cstate->current_fh;
  2533. struct inode *ino = current_fh->fh_dentry->d_inode;
  2534. __be32 status;
  2535. if (filpp)
  2536. *filpp = NULL;
  2537. if (grace_disallows_io(ino))
  2538. return nfserr_grace;
  2539. if (nfsd4_has_session(cstate))
  2540. flags |= HAS_SESSION;
  2541. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  2542. return check_special_stateids(current_fh, stateid, flags);
  2543. status = nfserr_stale_stateid;
  2544. if (STALE_STATEID(stateid))
  2545. goto out;
  2546. status = nfserr_bad_stateid;
  2547. if (is_delegation_stateid(stateid)) {
  2548. dp = find_delegation_stateid(ino, stateid);
  2549. if (!dp) {
  2550. status = stateid_error_map(stateid);
  2551. goto out;
  2552. }
  2553. status = check_stateid_generation(stateid, &dp->dl_stateid,
  2554. flags);
  2555. if (status)
  2556. goto out;
  2557. status = nfs4_check_delegmode(dp, flags);
  2558. if (status)
  2559. goto out;
  2560. renew_client(dp->dl_client);
  2561. if (filpp)
  2562. *filpp = dp->dl_vfs_file;
  2563. } else { /* open or lock stateid */
  2564. stp = find_stateid(stateid, flags);
  2565. if (!stp) {
  2566. status = stateid_error_map(stateid);
  2567. goto out;
  2568. }
  2569. if (nfs4_check_fh(current_fh, stp))
  2570. goto out;
  2571. if (!stp->st_stateowner->so_confirmed)
  2572. goto out;
  2573. status = check_stateid_generation(stateid, &stp->st_stateid,
  2574. flags);
  2575. if (status)
  2576. goto out;
  2577. status = nfs4_check_openmode(stp, flags);
  2578. if (status)
  2579. goto out;
  2580. renew_client(stp->st_stateowner->so_client);
  2581. if (filpp)
  2582. *filpp = stp->st_vfs_file;
  2583. }
  2584. status = nfs_ok;
  2585. out:
  2586. return status;
  2587. }
  2588. static inline int
  2589. setlkflg (int type)
  2590. {
  2591. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  2592. RD_STATE : WR_STATE;
  2593. }
  2594. /*
  2595. * Checks for sequence id mutating operations.
  2596. */
  2597. static __be32
  2598. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  2599. stateid_t *stateid, int flags,
  2600. struct nfs4_stateowner **sopp,
  2601. struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
  2602. {
  2603. struct nfs4_stateid *stp;
  2604. struct nfs4_stateowner *sop;
  2605. struct svc_fh *current_fh = &cstate->current_fh;
  2606. __be32 status;
  2607. dprintk("NFSD: preprocess_seqid_op: seqid=%d "
  2608. "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
  2609. stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
  2610. stateid->si_generation);
  2611. *stpp = NULL;
  2612. *sopp = NULL;
  2613. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
  2614. dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
  2615. return nfserr_bad_stateid;
  2616. }
  2617. if (STALE_STATEID(stateid))
  2618. return nfserr_stale_stateid;
  2619. if (nfsd4_has_session(cstate))
  2620. flags |= HAS_SESSION;
  2621. /*
  2622. * We return BAD_STATEID if filehandle doesn't match stateid,
  2623. * the confirmed flag is incorrecly set, or the generation
  2624. * number is incorrect.
  2625. */
  2626. stp = find_stateid(stateid, flags);
  2627. if (stp == NULL) {
  2628. /*
  2629. * Also, we should make sure this isn't just the result of
  2630. * a replayed close:
  2631. */
  2632. sop = search_close_lru(stateid->si_stateownerid, flags);
  2633. if (sop == NULL)
  2634. return stateid_error_map(stateid);
  2635. *sopp = sop;
  2636. goto check_replay;
  2637. }
  2638. *stpp = stp;
  2639. *sopp = sop = stp->st_stateowner;
  2640. if (lock) {
  2641. clientid_t *lockclid = &lock->v.new.clientid;
  2642. struct nfs4_client *clp = sop->so_client;
  2643. int lkflg = 0;
  2644. __be32 status;
  2645. lkflg = setlkflg(lock->lk_type);
  2646. if (lock->lk_is_new) {
  2647. if (!sop->so_is_open_owner)
  2648. return nfserr_bad_stateid;
  2649. if (!(flags & HAS_SESSION) &&
  2650. !same_clid(&clp->cl_clientid, lockclid))
  2651. return nfserr_bad_stateid;
  2652. /* stp is the open stateid */
  2653. status = nfs4_check_openmode(stp, lkflg);
  2654. if (status)
  2655. return status;
  2656. } else {
  2657. /* stp is the lock stateid */
  2658. status = nfs4_check_openmode(stp->st_openstp, lkflg);
  2659. if (status)
  2660. return status;
  2661. }
  2662. }
  2663. if (nfs4_check_fh(current_fh, stp)) {
  2664. dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
  2665. return nfserr_bad_stateid;
  2666. }
  2667. /*
  2668. * We now validate the seqid and stateid generation numbers.
  2669. * For the moment, we ignore the possibility of
  2670. * generation number wraparound.
  2671. */
  2672. if (!(flags & HAS_SESSION) && seqid != sop->so_seqid)
  2673. goto check_replay;
  2674. if (sop->so_confirmed && flags & CONFIRM) {
  2675. dprintk("NFSD: preprocess_seqid_op: expected"
  2676. " unconfirmed stateowner!\n");
  2677. return nfserr_bad_stateid;
  2678. }
  2679. if (!sop->so_confirmed && !(flags & CONFIRM)) {
  2680. dprintk("NFSD: preprocess_seqid_op: stateowner not"
  2681. " confirmed yet!\n");
  2682. return nfserr_bad_stateid;
  2683. }
  2684. status = check_stateid_generation(stateid, &stp->st_stateid, flags);
  2685. if (status)
  2686. return status;
  2687. renew_client(sop->so_client);
  2688. return nfs_ok;
  2689. check_replay:
  2690. if (seqid == sop->so_seqid - 1) {
  2691. dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
  2692. /* indicate replay to calling function */
  2693. return nfserr_replay_me;
  2694. }
  2695. dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
  2696. sop->so_seqid, seqid);
  2697. *sopp = NULL;
  2698. return nfserr_bad_seqid;
  2699. }
  2700. __be32
  2701. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2702. struct nfsd4_open_confirm *oc)
  2703. {
  2704. __be32 status;
  2705. struct nfs4_stateowner *sop;
  2706. struct nfs4_stateid *stp;
  2707. dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
  2708. (int)cstate->current_fh.fh_dentry->d_name.len,
  2709. cstate->current_fh.fh_dentry->d_name.name);
  2710. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  2711. if (status)
  2712. return status;
  2713. nfs4_lock_state();
  2714. if ((status = nfs4_preprocess_seqid_op(cstate,
  2715. oc->oc_seqid, &oc->oc_req_stateid,
  2716. CONFIRM | OPEN_STATE,
  2717. &oc->oc_stateowner, &stp, NULL)))
  2718. goto out;
  2719. sop = oc->oc_stateowner;
  2720. sop->so_confirmed = 1;
  2721. update_stateid(&stp->st_stateid);
  2722. memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
  2723. dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
  2724. "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
  2725. stp->st_stateid.si_boot,
  2726. stp->st_stateid.si_stateownerid,
  2727. stp->st_stateid.si_fileid,
  2728. stp->st_stateid.si_generation);
  2729. nfsd4_create_clid_dir(sop->so_client);
  2730. out:
  2731. if (oc->oc_stateowner) {
  2732. nfs4_get_stateowner(oc->oc_stateowner);
  2733. cstate->replay_owner = oc->oc_stateowner;
  2734. }
  2735. nfs4_unlock_state();
  2736. return status;
  2737. }
  2738. /*
  2739. * unset all bits in union bitmap (bmap) that
  2740. * do not exist in share (from successful OPEN_DOWNGRADE)
  2741. */
  2742. static void
  2743. reset_union_bmap_access(unsigned long access, unsigned long *bmap)
  2744. {
  2745. int i;
  2746. for (i = 1; i < 4; i++) {
  2747. if ((i & access) != i)
  2748. __clear_bit(i, bmap);
  2749. }
  2750. }
  2751. static void
  2752. reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
  2753. {
  2754. int i;
  2755. for (i = 0; i < 4; i++) {
  2756. if ((i & deny) != i)
  2757. __clear_bit(i, bmap);
  2758. }
  2759. }
  2760. __be32
  2761. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  2762. struct nfsd4_compound_state *cstate,
  2763. struct nfsd4_open_downgrade *od)
  2764. {
  2765. __be32 status;
  2766. struct nfs4_stateid *stp;
  2767. unsigned int share_access;
  2768. dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
  2769. (int)cstate->current_fh.fh_dentry->d_name.len,
  2770. cstate->current_fh.fh_dentry->d_name.name);
  2771. if (!access_valid(od->od_share_access, cstate->minorversion)
  2772. || !deny_valid(od->od_share_deny))
  2773. return nfserr_inval;
  2774. nfs4_lock_state();
  2775. if ((status = nfs4_preprocess_seqid_op(cstate,
  2776. od->od_seqid,
  2777. &od->od_stateid,
  2778. OPEN_STATE,
  2779. &od->od_stateowner, &stp, NULL)))
  2780. goto out;
  2781. status = nfserr_inval;
  2782. if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
  2783. dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
  2784. stp->st_access_bmap, od->od_share_access);
  2785. goto out;
  2786. }
  2787. if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
  2788. dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
  2789. stp->st_deny_bmap, od->od_share_deny);
  2790. goto out;
  2791. }
  2792. set_access(&share_access, stp->st_access_bmap);
  2793. nfs4_file_downgrade(stp->st_vfs_file,
  2794. share_access & ~od->od_share_access);
  2795. reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
  2796. reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
  2797. update_stateid(&stp->st_stateid);
  2798. memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
  2799. status = nfs_ok;
  2800. out:
  2801. if (od->od_stateowner) {
  2802. nfs4_get_stateowner(od->od_stateowner);
  2803. cstate->replay_owner = od->od_stateowner;
  2804. }
  2805. nfs4_unlock_state();
  2806. return status;
  2807. }
  2808. /*
  2809. * nfs4_unlock_state() called after encode
  2810. */
  2811. __be32
  2812. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2813. struct nfsd4_close *close)
  2814. {
  2815. __be32 status;
  2816. struct nfs4_stateid *stp;
  2817. dprintk("NFSD: nfsd4_close on file %.*s\n",
  2818. (int)cstate->current_fh.fh_dentry->d_name.len,
  2819. cstate->current_fh.fh_dentry->d_name.name);
  2820. nfs4_lock_state();
  2821. /* check close_lru for replay */
  2822. if ((status = nfs4_preprocess_seqid_op(cstate,
  2823. close->cl_seqid,
  2824. &close->cl_stateid,
  2825. OPEN_STATE | CLOSE_STATE,
  2826. &close->cl_stateowner, &stp, NULL)))
  2827. goto out;
  2828. status = nfs_ok;
  2829. update_stateid(&stp->st_stateid);
  2830. memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
  2831. /* release_stateid() calls nfsd_close() if needed */
  2832. release_open_stateid(stp);
  2833. /* place unused nfs4_stateowners on so_close_lru list to be
  2834. * released by the laundromat service after the lease period
  2835. * to enable us to handle CLOSE replay
  2836. */
  2837. if (list_empty(&close->cl_stateowner->so_stateids))
  2838. move_to_close_lru(close->cl_stateowner);
  2839. out:
  2840. if (close->cl_stateowner) {
  2841. nfs4_get_stateowner(close->cl_stateowner);
  2842. cstate->replay_owner = close->cl_stateowner;
  2843. }
  2844. nfs4_unlock_state();
  2845. return status;
  2846. }
  2847. __be32
  2848. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2849. struct nfsd4_delegreturn *dr)
  2850. {
  2851. struct nfs4_delegation *dp;
  2852. stateid_t *stateid = &dr->dr_stateid;
  2853. struct inode *inode;
  2854. __be32 status;
  2855. int flags = 0;
  2856. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  2857. return status;
  2858. inode = cstate->current_fh.fh_dentry->d_inode;
  2859. if (nfsd4_has_session(cstate))
  2860. flags |= HAS_SESSION;
  2861. nfs4_lock_state();
  2862. status = nfserr_bad_stateid;
  2863. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  2864. goto out;
  2865. status = nfserr_stale_stateid;
  2866. if (STALE_STATEID(stateid))
  2867. goto out;
  2868. status = nfserr_bad_stateid;
  2869. if (!is_delegation_stateid(stateid))
  2870. goto out;
  2871. dp = find_delegation_stateid(inode, stateid);
  2872. if (!dp) {
  2873. status = stateid_error_map(stateid);
  2874. goto out;
  2875. }
  2876. status = check_stateid_generation(stateid, &dp->dl_stateid, flags);
  2877. if (status)
  2878. goto out;
  2879. renew_client(dp->dl_client);
  2880. unhash_delegation(dp);
  2881. out:
  2882. nfs4_unlock_state();
  2883. return status;
  2884. }
  2885. /*
  2886. * Lock owner state (byte-range locks)
  2887. */
  2888. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  2889. #define LOCK_HASH_BITS 8
  2890. #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
  2891. #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
  2892. static inline u64
  2893. end_offset(u64 start, u64 len)
  2894. {
  2895. u64 end;
  2896. end = start + len;
  2897. return end >= start ? end: NFS4_MAX_UINT64;
  2898. }
  2899. /* last octet in a range */
  2900. static inline u64
  2901. last_byte_offset(u64 start, u64 len)
  2902. {
  2903. u64 end;
  2904. BUG_ON(!len);
  2905. end = start + len;
  2906. return end > start ? end - 1: NFS4_MAX_UINT64;
  2907. }
  2908. #define lockownerid_hashval(id) \
  2909. ((id) & LOCK_HASH_MASK)
  2910. static inline unsigned int
  2911. lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
  2912. struct xdr_netobj *ownername)
  2913. {
  2914. return (file_hashval(inode) + cl_id
  2915. + opaque_hashval(ownername->data, ownername->len))
  2916. & LOCK_HASH_MASK;
  2917. }
  2918. static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
  2919. static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
  2920. static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
  2921. static struct nfs4_stateid *
  2922. find_stateid(stateid_t *stid, int flags)
  2923. {
  2924. struct nfs4_stateid *local;
  2925. u32 st_id = stid->si_stateownerid;
  2926. u32 f_id = stid->si_fileid;
  2927. unsigned int hashval;
  2928. dprintk("NFSD: find_stateid flags 0x%x\n",flags);
  2929. if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
  2930. hashval = stateid_hashval(st_id, f_id);
  2931. list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
  2932. if ((local->st_stateid.si_stateownerid == st_id) &&
  2933. (local->st_stateid.si_fileid == f_id))
  2934. return local;
  2935. }
  2936. }
  2937. if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
  2938. hashval = stateid_hashval(st_id, f_id);
  2939. list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
  2940. if ((local->st_stateid.si_stateownerid == st_id) &&
  2941. (local->st_stateid.si_fileid == f_id))
  2942. return local;
  2943. }
  2944. }
  2945. return NULL;
  2946. }
  2947. static struct nfs4_delegation *
  2948. find_delegation_stateid(struct inode *ino, stateid_t *stid)
  2949. {
  2950. struct nfs4_file *fp;
  2951. struct nfs4_delegation *dl;
  2952. dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
  2953. stid->si_boot, stid->si_stateownerid,
  2954. stid->si_fileid, stid->si_generation);
  2955. fp = find_file(ino);
  2956. if (!fp)
  2957. return NULL;
  2958. dl = find_delegation_file(fp, stid);
  2959. put_nfs4_file(fp);
  2960. return dl;
  2961. }
  2962. /*
  2963. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  2964. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  2965. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  2966. * locking, this prevents us from being completely protocol-compliant. The
  2967. * real solution to this problem is to start using unsigned file offsets in
  2968. * the VFS, but this is a very deep change!
  2969. */
  2970. static inline void
  2971. nfs4_transform_lock_offset(struct file_lock *lock)
  2972. {
  2973. if (lock->fl_start < 0)
  2974. lock->fl_start = OFFSET_MAX;
  2975. if (lock->fl_end < 0)
  2976. lock->fl_end = OFFSET_MAX;
  2977. }
  2978. /* Hack!: For now, we're defining this just so we can use a pointer to it
  2979. * as a unique cookie to identify our (NFSv4's) posix locks. */
  2980. static struct lock_manager_operations nfsd_posix_mng_ops = {
  2981. };
  2982. static inline void
  2983. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  2984. {
  2985. struct nfs4_stateowner *sop;
  2986. unsigned int hval;
  2987. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  2988. sop = (struct nfs4_stateowner *) fl->fl_owner;
  2989. hval = lockownerid_hashval(sop->so_id);
  2990. kref_get(&sop->so_ref);
  2991. deny->ld_sop = sop;
  2992. deny->ld_clientid = sop->so_client->cl_clientid;
  2993. } else {
  2994. deny->ld_sop = NULL;
  2995. deny->ld_clientid.cl_boot = 0;
  2996. deny->ld_clientid.cl_id = 0;
  2997. }
  2998. deny->ld_start = fl->fl_start;
  2999. deny->ld_length = NFS4_MAX_UINT64;
  3000. if (fl->fl_end != NFS4_MAX_UINT64)
  3001. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  3002. deny->ld_type = NFS4_READ_LT;
  3003. if (fl->fl_type != F_RDLCK)
  3004. deny->ld_type = NFS4_WRITE_LT;
  3005. }
  3006. static struct nfs4_stateowner *
  3007. find_lockstateowner_str(struct inode *inode, clientid_t *clid,
  3008. struct xdr_netobj *owner)
  3009. {
  3010. unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
  3011. struct nfs4_stateowner *op;
  3012. list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
  3013. if (same_owner_str(op, owner, clid))
  3014. return op;
  3015. }
  3016. return NULL;
  3017. }
  3018. /*
  3019. * Alloc a lock owner structure.
  3020. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  3021. * occured.
  3022. *
  3023. * strhashval = lock_ownerstr_hashval
  3024. */
  3025. static struct nfs4_stateowner *
  3026. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
  3027. struct nfs4_stateowner *sop;
  3028. struct nfs4_replay *rp;
  3029. unsigned int idhashval;
  3030. if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
  3031. return NULL;
  3032. idhashval = lockownerid_hashval(current_ownerid);
  3033. INIT_LIST_HEAD(&sop->so_idhash);
  3034. INIT_LIST_HEAD(&sop->so_strhash);
  3035. INIT_LIST_HEAD(&sop->so_perclient);
  3036. INIT_LIST_HEAD(&sop->so_stateids);
  3037. INIT_LIST_HEAD(&sop->so_perstateid);
  3038. INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
  3039. sop->so_time = 0;
  3040. list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
  3041. list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
  3042. list_add(&sop->so_perstateid, &open_stp->st_lockowners);
  3043. sop->so_is_open_owner = 0;
  3044. sop->so_id = current_ownerid++;
  3045. sop->so_client = clp;
  3046. /* It is the openowner seqid that will be incremented in encode in the
  3047. * case of new lockowners; so increment the lock seqid manually: */
  3048. sop->so_seqid = lock->lk_new_lock_seqid + 1;
  3049. sop->so_confirmed = 1;
  3050. rp = &sop->so_replay;
  3051. rp->rp_status = nfserr_serverfault;
  3052. rp->rp_buflen = 0;
  3053. rp->rp_buf = rp->rp_ibuf;
  3054. return sop;
  3055. }
  3056. static struct nfs4_stateid *
  3057. alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
  3058. {
  3059. struct nfs4_stateid *stp;
  3060. unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
  3061. stp = nfs4_alloc_stateid();
  3062. if (stp == NULL)
  3063. goto out;
  3064. INIT_LIST_HEAD(&stp->st_hash);
  3065. INIT_LIST_HEAD(&stp->st_perfile);
  3066. INIT_LIST_HEAD(&stp->st_perstateowner);
  3067. INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
  3068. list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
  3069. list_add(&stp->st_perfile, &fp->fi_stateids);
  3070. list_add(&stp->st_perstateowner, &sop->so_stateids);
  3071. stp->st_stateowner = sop;
  3072. get_nfs4_file(fp);
  3073. stp->st_file = fp;
  3074. stp->st_stateid.si_boot = get_seconds();
  3075. stp->st_stateid.si_stateownerid = sop->so_id;
  3076. stp->st_stateid.si_fileid = fp->fi_id;
  3077. stp->st_stateid.si_generation = 0;
  3078. stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
  3079. stp->st_access_bmap = open_stp->st_access_bmap;
  3080. stp->st_deny_bmap = open_stp->st_deny_bmap;
  3081. stp->st_openstp = open_stp;
  3082. out:
  3083. return stp;
  3084. }
  3085. static int
  3086. check_lock_length(u64 offset, u64 length)
  3087. {
  3088. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  3089. LOFF_OVERFLOW(offset, length)));
  3090. }
  3091. /*
  3092. * LOCK operation
  3093. */
  3094. __be32
  3095. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3096. struct nfsd4_lock *lock)
  3097. {
  3098. struct nfs4_stateowner *open_sop = NULL;
  3099. struct nfs4_stateowner *lock_sop = NULL;
  3100. struct nfs4_stateid *lock_stp;
  3101. struct file *filp;
  3102. struct file_lock file_lock;
  3103. struct file_lock conflock;
  3104. __be32 status = 0;
  3105. unsigned int strhashval;
  3106. unsigned int cmd;
  3107. int err;
  3108. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  3109. (long long) lock->lk_offset,
  3110. (long long) lock->lk_length);
  3111. if (check_lock_length(lock->lk_offset, lock->lk_length))
  3112. return nfserr_inval;
  3113. if ((status = fh_verify(rqstp, &cstate->current_fh,
  3114. S_IFREG, NFSD_MAY_LOCK))) {
  3115. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  3116. return status;
  3117. }
  3118. nfs4_lock_state();
  3119. if (lock->lk_is_new) {
  3120. /*
  3121. * Client indicates that this is a new lockowner.
  3122. * Use open owner and open stateid to create lock owner and
  3123. * lock stateid.
  3124. */
  3125. struct nfs4_stateid *open_stp = NULL;
  3126. struct nfs4_file *fp;
  3127. status = nfserr_stale_clientid;
  3128. if (!nfsd4_has_session(cstate) &&
  3129. STALE_CLIENTID(&lock->lk_new_clientid))
  3130. goto out;
  3131. /* validate and update open stateid and open seqid */
  3132. status = nfs4_preprocess_seqid_op(cstate,
  3133. lock->lk_new_open_seqid,
  3134. &lock->lk_new_open_stateid,
  3135. OPEN_STATE,
  3136. &lock->lk_replay_owner, &open_stp,
  3137. lock);
  3138. if (status)
  3139. goto out;
  3140. open_sop = lock->lk_replay_owner;
  3141. /* create lockowner and lock stateid */
  3142. fp = open_stp->st_file;
  3143. strhashval = lock_ownerstr_hashval(fp->fi_inode,
  3144. open_sop->so_client->cl_clientid.cl_id,
  3145. &lock->v.new.owner);
  3146. /* XXX: Do we need to check for duplicate stateowners on
  3147. * the same file, or should they just be allowed (and
  3148. * create new stateids)? */
  3149. status = nfserr_resource;
  3150. lock_sop = alloc_init_lock_stateowner(strhashval,
  3151. open_sop->so_client, open_stp, lock);
  3152. if (lock_sop == NULL)
  3153. goto out;
  3154. lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
  3155. if (lock_stp == NULL)
  3156. goto out;
  3157. } else {
  3158. /* lock (lock owner + lock stateid) already exists */
  3159. status = nfs4_preprocess_seqid_op(cstate,
  3160. lock->lk_old_lock_seqid,
  3161. &lock->lk_old_lock_stateid,
  3162. LOCK_STATE,
  3163. &lock->lk_replay_owner, &lock_stp, lock);
  3164. if (status)
  3165. goto out;
  3166. lock_sop = lock->lk_replay_owner;
  3167. }
  3168. /* lock->lk_replay_owner and lock_stp have been created or found */
  3169. filp = lock_stp->st_vfs_file;
  3170. status = nfserr_grace;
  3171. if (locks_in_grace() && !lock->lk_reclaim)
  3172. goto out;
  3173. status = nfserr_no_grace;
  3174. if (!locks_in_grace() && lock->lk_reclaim)
  3175. goto out;
  3176. locks_init_lock(&file_lock);
  3177. switch (lock->lk_type) {
  3178. case NFS4_READ_LT:
  3179. case NFS4_READW_LT:
  3180. file_lock.fl_type = F_RDLCK;
  3181. cmd = F_SETLK;
  3182. break;
  3183. case NFS4_WRITE_LT:
  3184. case NFS4_WRITEW_LT:
  3185. file_lock.fl_type = F_WRLCK;
  3186. cmd = F_SETLK;
  3187. break;
  3188. default:
  3189. status = nfserr_inval;
  3190. goto out;
  3191. }
  3192. file_lock.fl_owner = (fl_owner_t)lock_sop;
  3193. file_lock.fl_pid = current->tgid;
  3194. file_lock.fl_file = filp;
  3195. file_lock.fl_flags = FL_POSIX;
  3196. file_lock.fl_lmops = &nfsd_posix_mng_ops;
  3197. file_lock.fl_start = lock->lk_offset;
  3198. file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  3199. nfs4_transform_lock_offset(&file_lock);
  3200. /*
  3201. * Try to lock the file in the VFS.
  3202. * Note: locks.c uses the BKL to protect the inode's lock list.
  3203. */
  3204. err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
  3205. switch (-err) {
  3206. case 0: /* success! */
  3207. update_stateid(&lock_stp->st_stateid);
  3208. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
  3209. sizeof(stateid_t));
  3210. status = 0;
  3211. break;
  3212. case (EAGAIN): /* conflock holds conflicting lock */
  3213. status = nfserr_denied;
  3214. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  3215. nfs4_set_lock_denied(&conflock, &lock->lk_denied);
  3216. break;
  3217. case (EDEADLK):
  3218. status = nfserr_deadlock;
  3219. break;
  3220. default:
  3221. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  3222. status = nfserr_resource;
  3223. break;
  3224. }
  3225. out:
  3226. if (status && lock->lk_is_new && lock_sop)
  3227. release_lockowner(lock_sop);
  3228. if (lock->lk_replay_owner) {
  3229. nfs4_get_stateowner(lock->lk_replay_owner);
  3230. cstate->replay_owner = lock->lk_replay_owner;
  3231. }
  3232. nfs4_unlock_state();
  3233. return status;
  3234. }
  3235. /*
  3236. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  3237. * so we do a temporary open here just to get an open file to pass to
  3238. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  3239. * inode operation.)
  3240. */
  3241. static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  3242. {
  3243. struct file *file;
  3244. int err;
  3245. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  3246. if (err)
  3247. return err;
  3248. err = vfs_test_lock(file, lock);
  3249. nfsd_close(file);
  3250. return err;
  3251. }
  3252. /*
  3253. * LOCKT operation
  3254. */
  3255. __be32
  3256. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3257. struct nfsd4_lockt *lockt)
  3258. {
  3259. struct inode *inode;
  3260. struct file_lock file_lock;
  3261. int error;
  3262. __be32 status;
  3263. if (locks_in_grace())
  3264. return nfserr_grace;
  3265. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  3266. return nfserr_inval;
  3267. lockt->lt_stateowner = NULL;
  3268. nfs4_lock_state();
  3269. status = nfserr_stale_clientid;
  3270. if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
  3271. goto out;
  3272. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
  3273. dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
  3274. if (status == nfserr_symlink)
  3275. status = nfserr_inval;
  3276. goto out;
  3277. }
  3278. inode = cstate->current_fh.fh_dentry->d_inode;
  3279. locks_init_lock(&file_lock);
  3280. switch (lockt->lt_type) {
  3281. case NFS4_READ_LT:
  3282. case NFS4_READW_LT:
  3283. file_lock.fl_type = F_RDLCK;
  3284. break;
  3285. case NFS4_WRITE_LT:
  3286. case NFS4_WRITEW_LT:
  3287. file_lock.fl_type = F_WRLCK;
  3288. break;
  3289. default:
  3290. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  3291. status = nfserr_inval;
  3292. goto out;
  3293. }
  3294. lockt->lt_stateowner = find_lockstateowner_str(inode,
  3295. &lockt->lt_clientid, &lockt->lt_owner);
  3296. if (lockt->lt_stateowner)
  3297. file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
  3298. file_lock.fl_pid = current->tgid;
  3299. file_lock.fl_flags = FL_POSIX;
  3300. file_lock.fl_start = lockt->lt_offset;
  3301. file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  3302. nfs4_transform_lock_offset(&file_lock);
  3303. status = nfs_ok;
  3304. error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
  3305. if (error) {
  3306. status = nfserrno(error);
  3307. goto out;
  3308. }
  3309. if (file_lock.fl_type != F_UNLCK) {
  3310. status = nfserr_denied;
  3311. nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
  3312. }
  3313. out:
  3314. nfs4_unlock_state();
  3315. return status;
  3316. }
  3317. __be32
  3318. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3319. struct nfsd4_locku *locku)
  3320. {
  3321. struct nfs4_stateid *stp;
  3322. struct file *filp = NULL;
  3323. struct file_lock file_lock;
  3324. __be32 status;
  3325. int err;
  3326. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  3327. (long long) locku->lu_offset,
  3328. (long long) locku->lu_length);
  3329. if (check_lock_length(locku->lu_offset, locku->lu_length))
  3330. return nfserr_inval;
  3331. nfs4_lock_state();
  3332. if ((status = nfs4_preprocess_seqid_op(cstate,
  3333. locku->lu_seqid,
  3334. &locku->lu_stateid,
  3335. LOCK_STATE,
  3336. &locku->lu_stateowner, &stp, NULL)))
  3337. goto out;
  3338. filp = stp->st_vfs_file;
  3339. BUG_ON(!filp);
  3340. locks_init_lock(&file_lock);
  3341. file_lock.fl_type = F_UNLCK;
  3342. file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
  3343. file_lock.fl_pid = current->tgid;
  3344. file_lock.fl_file = filp;
  3345. file_lock.fl_flags = FL_POSIX;
  3346. file_lock.fl_lmops = &nfsd_posix_mng_ops;
  3347. file_lock.fl_start = locku->lu_offset;
  3348. file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
  3349. nfs4_transform_lock_offset(&file_lock);
  3350. /*
  3351. * Try to unlock the file in the VFS.
  3352. */
  3353. err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
  3354. if (err) {
  3355. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  3356. goto out_nfserr;
  3357. }
  3358. /*
  3359. * OK, unlock succeeded; the only thing left to do is update the stateid.
  3360. */
  3361. update_stateid(&stp->st_stateid);
  3362. memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
  3363. out:
  3364. if (locku->lu_stateowner) {
  3365. nfs4_get_stateowner(locku->lu_stateowner);
  3366. cstate->replay_owner = locku->lu_stateowner;
  3367. }
  3368. nfs4_unlock_state();
  3369. return status;
  3370. out_nfserr:
  3371. status = nfserrno(err);
  3372. goto out;
  3373. }
  3374. /*
  3375. * returns
  3376. * 1: locks held by lockowner
  3377. * 0: no locks held by lockowner
  3378. */
  3379. static int
  3380. check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
  3381. {
  3382. struct file_lock **flpp;
  3383. struct inode *inode = filp->f_path.dentry->d_inode;
  3384. int status = 0;
  3385. lock_kernel();
  3386. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  3387. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  3388. status = 1;
  3389. goto out;
  3390. }
  3391. }
  3392. out:
  3393. unlock_kernel();
  3394. return status;
  3395. }
  3396. __be32
  3397. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  3398. struct nfsd4_compound_state *cstate,
  3399. struct nfsd4_release_lockowner *rlockowner)
  3400. {
  3401. clientid_t *clid = &rlockowner->rl_clientid;
  3402. struct nfs4_stateowner *sop;
  3403. struct nfs4_stateid *stp;
  3404. struct xdr_netobj *owner = &rlockowner->rl_owner;
  3405. struct list_head matches;
  3406. int i;
  3407. __be32 status;
  3408. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  3409. clid->cl_boot, clid->cl_id);
  3410. /* XXX check for lease expiration */
  3411. status = nfserr_stale_clientid;
  3412. if (STALE_CLIENTID(clid))
  3413. return status;
  3414. nfs4_lock_state();
  3415. status = nfserr_locks_held;
  3416. /* XXX: we're doing a linear search through all the lockowners.
  3417. * Yipes! For now we'll just hope clients aren't really using
  3418. * release_lockowner much, but eventually we have to fix these
  3419. * data structures. */
  3420. INIT_LIST_HEAD(&matches);
  3421. for (i = 0; i < LOCK_HASH_SIZE; i++) {
  3422. list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
  3423. if (!same_owner_str(sop, owner, clid))
  3424. continue;
  3425. list_for_each_entry(stp, &sop->so_stateids,
  3426. st_perstateowner) {
  3427. if (check_for_locks(stp->st_vfs_file, sop))
  3428. goto out;
  3429. /* Note: so_perclient unused for lockowners,
  3430. * so it's OK to fool with here. */
  3431. list_add(&sop->so_perclient, &matches);
  3432. }
  3433. }
  3434. }
  3435. /* Clients probably won't expect us to return with some (but not all)
  3436. * of the lockowner state released; so don't release any until all
  3437. * have been checked. */
  3438. status = nfs_ok;
  3439. while (!list_empty(&matches)) {
  3440. sop = list_entry(matches.next, struct nfs4_stateowner,
  3441. so_perclient);
  3442. /* unhash_stateowner deletes so_perclient only
  3443. * for openowners. */
  3444. list_del(&sop->so_perclient);
  3445. release_lockowner(sop);
  3446. }
  3447. out:
  3448. nfs4_unlock_state();
  3449. return status;
  3450. }
  3451. static inline struct nfs4_client_reclaim *
  3452. alloc_reclaim(void)
  3453. {
  3454. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  3455. }
  3456. int
  3457. nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
  3458. {
  3459. unsigned int strhashval = clientstr_hashval(name);
  3460. struct nfs4_client *clp;
  3461. clp = find_confirmed_client_by_str(name, strhashval, use_exchange_id);
  3462. return clp ? 1 : 0;
  3463. }
  3464. /*
  3465. * failure => all reset bets are off, nfserr_no_grace...
  3466. */
  3467. int
  3468. nfs4_client_to_reclaim(const char *name)
  3469. {
  3470. unsigned int strhashval;
  3471. struct nfs4_client_reclaim *crp = NULL;
  3472. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  3473. crp = alloc_reclaim();
  3474. if (!crp)
  3475. return 0;
  3476. strhashval = clientstr_hashval(name);
  3477. INIT_LIST_HEAD(&crp->cr_strhash);
  3478. list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
  3479. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  3480. reclaim_str_hashtbl_size++;
  3481. return 1;
  3482. }
  3483. static void
  3484. nfs4_release_reclaim(void)
  3485. {
  3486. struct nfs4_client_reclaim *crp = NULL;
  3487. int i;
  3488. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  3489. while (!list_empty(&reclaim_str_hashtbl[i])) {
  3490. crp = list_entry(reclaim_str_hashtbl[i].next,
  3491. struct nfs4_client_reclaim, cr_strhash);
  3492. list_del(&crp->cr_strhash);
  3493. kfree(crp);
  3494. reclaim_str_hashtbl_size--;
  3495. }
  3496. }
  3497. BUG_ON(reclaim_str_hashtbl_size);
  3498. }
  3499. /*
  3500. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  3501. static struct nfs4_client_reclaim *
  3502. nfs4_find_reclaim_client(clientid_t *clid)
  3503. {
  3504. unsigned int strhashval;
  3505. struct nfs4_client *clp;
  3506. struct nfs4_client_reclaim *crp = NULL;
  3507. /* find clientid in conf_id_hashtbl */
  3508. clp = find_confirmed_client(clid);
  3509. if (clp == NULL)
  3510. return NULL;
  3511. dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
  3512. clp->cl_name.len, clp->cl_name.data,
  3513. clp->cl_recdir);
  3514. /* find clp->cl_name in reclaim_str_hashtbl */
  3515. strhashval = clientstr_hashval(clp->cl_recdir);
  3516. list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
  3517. if (same_name(crp->cr_recdir, clp->cl_recdir)) {
  3518. return crp;
  3519. }
  3520. }
  3521. return NULL;
  3522. }
  3523. /*
  3524. * Called from OPEN. Look for clientid in reclaim list.
  3525. */
  3526. __be32
  3527. nfs4_check_open_reclaim(clientid_t *clid)
  3528. {
  3529. return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
  3530. }
  3531. /* initialization to perform at module load time: */
  3532. int
  3533. nfs4_state_init(void)
  3534. {
  3535. int i, status;
  3536. status = nfsd4_init_slabs();
  3537. if (status)
  3538. return status;
  3539. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  3540. INIT_LIST_HEAD(&conf_id_hashtbl[i]);
  3541. INIT_LIST_HEAD(&conf_str_hashtbl[i]);
  3542. INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
  3543. INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
  3544. INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
  3545. }
  3546. for (i = 0; i < SESSION_HASH_SIZE; i++)
  3547. INIT_LIST_HEAD(&sessionid_hashtbl[i]);
  3548. for (i = 0; i < FILE_HASH_SIZE; i++) {
  3549. INIT_LIST_HEAD(&file_hashtbl[i]);
  3550. }
  3551. for (i = 0; i < OWNER_HASH_SIZE; i++) {
  3552. INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
  3553. INIT_LIST_HEAD(&ownerid_hashtbl[i]);
  3554. }
  3555. for (i = 0; i < STATEID_HASH_SIZE; i++) {
  3556. INIT_LIST_HEAD(&stateid_hashtbl[i]);
  3557. INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
  3558. }
  3559. for (i = 0; i < LOCK_HASH_SIZE; i++) {
  3560. INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
  3561. INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
  3562. }
  3563. memset(&onestateid, ~0, sizeof(stateid_t));
  3564. INIT_LIST_HEAD(&close_lru);
  3565. INIT_LIST_HEAD(&client_lru);
  3566. INIT_LIST_HEAD(&del_recall_lru);
  3567. reclaim_str_hashtbl_size = 0;
  3568. return 0;
  3569. }
  3570. static void
  3571. nfsd4_load_reboot_recovery_data(void)
  3572. {
  3573. int status;
  3574. nfs4_lock_state();
  3575. nfsd4_init_recdir(user_recovery_dirname);
  3576. status = nfsd4_recdir_load();
  3577. nfs4_unlock_state();
  3578. if (status)
  3579. printk("NFSD: Failure reading reboot recovery data\n");
  3580. }
  3581. unsigned long
  3582. get_nfs4_grace_period(void)
  3583. {
  3584. return max(user_lease_time, lease_time) * HZ;
  3585. }
  3586. /*
  3587. * Since the lifetime of a delegation isn't limited to that of an open, a
  3588. * client may quite reasonably hang on to a delegation as long as it has
  3589. * the inode cached. This becomes an obvious problem the first time a
  3590. * client's inode cache approaches the size of the server's total memory.
  3591. *
  3592. * For now we avoid this problem by imposing a hard limit on the number
  3593. * of delegations, which varies according to the server's memory size.
  3594. */
  3595. static void
  3596. set_max_delegations(void)
  3597. {
  3598. /*
  3599. * Allow at most 4 delegations per megabyte of RAM. Quick
  3600. * estimates suggest that in the worst case (where every delegation
  3601. * is for a different inode), a delegation could take about 1.5K,
  3602. * giving a worst case usage of about 6% of memory.
  3603. */
  3604. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  3605. }
  3606. /* initialization to perform when the nfsd service is started: */
  3607. static void
  3608. __nfs4_state_start(void)
  3609. {
  3610. unsigned long grace_time;
  3611. boot_time = get_seconds();
  3612. grace_time = get_nfs4_grace_period();
  3613. lease_time = user_lease_time;
  3614. locks_start_grace(&nfsd4_manager);
  3615. printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
  3616. grace_time/HZ);
  3617. laundry_wq = create_singlethread_workqueue("nfsd4");
  3618. queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
  3619. set_max_delegations();
  3620. }
  3621. void
  3622. nfs4_state_start(void)
  3623. {
  3624. if (nfs4_init)
  3625. return;
  3626. nfsd4_load_reboot_recovery_data();
  3627. __nfs4_state_start();
  3628. nfs4_init = 1;
  3629. return;
  3630. }
  3631. time_t
  3632. nfs4_lease_time(void)
  3633. {
  3634. return lease_time;
  3635. }
  3636. static void
  3637. __nfs4_state_shutdown(void)
  3638. {
  3639. int i;
  3640. struct nfs4_client *clp = NULL;
  3641. struct nfs4_delegation *dp = NULL;
  3642. struct list_head *pos, *next, reaplist;
  3643. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  3644. while (!list_empty(&conf_id_hashtbl[i])) {
  3645. clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  3646. expire_client(clp);
  3647. }
  3648. while (!list_empty(&unconf_str_hashtbl[i])) {
  3649. clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
  3650. expire_client(clp);
  3651. }
  3652. }
  3653. INIT_LIST_HEAD(&reaplist);
  3654. spin_lock(&recall_lock);
  3655. list_for_each_safe(pos, next, &del_recall_lru) {
  3656. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  3657. list_move(&dp->dl_recall_lru, &reaplist);
  3658. }
  3659. spin_unlock(&recall_lock);
  3660. list_for_each_safe(pos, next, &reaplist) {
  3661. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  3662. list_del_init(&dp->dl_recall_lru);
  3663. unhash_delegation(dp);
  3664. }
  3665. nfsd4_shutdown_recdir();
  3666. nfs4_init = 0;
  3667. }
  3668. void
  3669. nfs4_state_shutdown(void)
  3670. {
  3671. cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
  3672. destroy_workqueue(laundry_wq);
  3673. locks_end_grace(&nfsd4_manager);
  3674. nfs4_lock_state();
  3675. nfs4_release_reclaim();
  3676. __nfs4_state_shutdown();
  3677. nfs4_unlock_state();
  3678. }
  3679. /*
  3680. * user_recovery_dirname is protected by the nfsd_mutex since it's only
  3681. * accessed when nfsd is starting.
  3682. */
  3683. static void
  3684. nfs4_set_recdir(char *recdir)
  3685. {
  3686. strcpy(user_recovery_dirname, recdir);
  3687. }
  3688. /*
  3689. * Change the NFSv4 recovery directory to recdir.
  3690. */
  3691. int
  3692. nfs4_reset_recoverydir(char *recdir)
  3693. {
  3694. int status;
  3695. struct path path;
  3696. status = kern_path(recdir, LOOKUP_FOLLOW, &path);
  3697. if (status)
  3698. return status;
  3699. status = -ENOTDIR;
  3700. if (S_ISDIR(path.dentry->d_inode->i_mode)) {
  3701. nfs4_set_recdir(recdir);
  3702. status = 0;
  3703. }
  3704. path_put(&path);
  3705. return status;
  3706. }
  3707. char *
  3708. nfs4_recoverydir(void)
  3709. {
  3710. return user_recovery_dirname;
  3711. }
  3712. /*
  3713. * Called when leasetime is changed.
  3714. *
  3715. * The only way the protocol gives us to handle on-the-fly lease changes is to
  3716. * simulate a reboot. Instead of doing that, we just wait till the next time
  3717. * we start to register any changes in lease time. If the administrator
  3718. * really wants to change the lease time *now*, they can go ahead and bring
  3719. * nfsd down and then back up again after changing the lease time.
  3720. *
  3721. * user_lease_time is protected by nfsd_mutex since it's only really accessed
  3722. * when nfsd is starting
  3723. */
  3724. void
  3725. nfs4_reset_lease(time_t leasetime)
  3726. {
  3727. user_lease_time = leasetime;
  3728. }