nfs4state.c 89 KB

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