nfs4state.c 109 KB

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