nfs4state.c 109 KB

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