nfs4state.c 85 KB

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