nfs4state.c 87 KB

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