nfs4state.c 108 KB

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