nfs4state.c 87 KB

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