nfs4state.c 87 KB

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