nfs4state.c 119 KB

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