nfs4state.c 88 KB

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