nfs4state.c 88 KB

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