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