nfs4state.c 87 KB

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