nfs4state.c 117 KB

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