nfs4state.c 88 KB

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