nfs4state.c 87 KB

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