nfs4state.c 87 KB

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