nfs4state.c 87 KB

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