nfs4state.c 116 KB

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