nfs4state.c 88 KB

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