nfs4state.c 88 KB

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