nfs4state.c 88 KB

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