nfs4state.c 85 KB

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