nfs4state.c 88 KB

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