nfs4state.c 120 KB

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