nfs4state.c 87 KB

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