nfs4state.c 107 KB

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