nfs4state.c 87 KB

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