nfs4state.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287
  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_verifier, &unconf->cl_verifier)) &&
  746. (!same_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
  747. /*
  748. * RFC 3530 14.2.34 CASE 1:
  749. * callback update
  750. */
  751. if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
  752. status = nfserr_clid_inuse;
  753. else {
  754. /* XXX: We just turn off callbacks until we can handle
  755. * change request correctly. */
  756. atomic_set(&conf->cl_callback.cb_set, 0);
  757. gen_confirm(conf);
  758. nfsd4_remove_clid_dir(unconf);
  759. expire_client(unconf);
  760. status = nfs_ok;
  761. }
  762. } else if ((conf && !unconf) ||
  763. ((conf && unconf) &&
  764. !same_verf(&conf->cl_verifier, &unconf->cl_verifier))) {
  765. /*
  766. * RFC 3530 14.2.34 CASE 2:
  767. * probable retransmitted request; play it safe and
  768. * do nothing.
  769. */
  770. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
  771. status = nfserr_clid_inuse;
  772. else
  773. status = nfs_ok;
  774. } else if (!conf && unconf
  775. && same_verf(&unconf->cl_confirm, &confirm)) {
  776. /*
  777. * RFC 3530 14.2.34 CASE 3:
  778. * Normal case; new or rebooted client:
  779. */
  780. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
  781. status = nfserr_clid_inuse;
  782. } else {
  783. unsigned int hash =
  784. clientstr_hashval(unconf->cl_recdir);
  785. conf = find_confirmed_client_by_str(unconf->cl_recdir,
  786. hash);
  787. if (conf) {
  788. nfsd4_remove_clid_dir(conf);
  789. expire_client(conf);
  790. }
  791. move_to_confirmed(unconf);
  792. conf = unconf;
  793. nfsd4_probe_callback(conf);
  794. status = nfs_ok;
  795. }
  796. } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
  797. && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
  798. &confirm)))) {
  799. /*
  800. * RFC 3530 14.2.34 CASE 4:
  801. * Client probably hasn't noticed that we rebooted yet.
  802. */
  803. status = nfserr_stale_clientid;
  804. } else {
  805. /* check that we have hit one of the cases...*/
  806. status = nfserr_clid_inuse;
  807. }
  808. out:
  809. nfs4_unlock_state();
  810. return status;
  811. }
  812. /* OPEN Share state helper functions */
  813. static inline struct nfs4_file *
  814. alloc_init_file(struct inode *ino)
  815. {
  816. struct nfs4_file *fp;
  817. unsigned int hashval = file_hashval(ino);
  818. fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
  819. if (fp) {
  820. kref_init(&fp->fi_ref);
  821. INIT_LIST_HEAD(&fp->fi_hash);
  822. INIT_LIST_HEAD(&fp->fi_stateids);
  823. INIT_LIST_HEAD(&fp->fi_delegations);
  824. list_add(&fp->fi_hash, &file_hashtbl[hashval]);
  825. fp->fi_inode = igrab(ino);
  826. fp->fi_id = current_fileid++;
  827. fp->fi_had_conflict = false;
  828. return fp;
  829. }
  830. return NULL;
  831. }
  832. static void
  833. nfsd4_free_slab(struct kmem_cache **slab)
  834. {
  835. if (*slab == NULL)
  836. return;
  837. kmem_cache_destroy(*slab);
  838. *slab = NULL;
  839. }
  840. void
  841. nfsd4_free_slabs(void)
  842. {
  843. nfsd4_free_slab(&stateowner_slab);
  844. nfsd4_free_slab(&file_slab);
  845. nfsd4_free_slab(&stateid_slab);
  846. nfsd4_free_slab(&deleg_slab);
  847. }
  848. static int
  849. nfsd4_init_slabs(void)
  850. {
  851. stateowner_slab = kmem_cache_create("nfsd4_stateowners",
  852. sizeof(struct nfs4_stateowner), 0, 0, NULL);
  853. if (stateowner_slab == NULL)
  854. goto out_nomem;
  855. file_slab = kmem_cache_create("nfsd4_files",
  856. sizeof(struct nfs4_file), 0, 0, NULL);
  857. if (file_slab == NULL)
  858. goto out_nomem;
  859. stateid_slab = kmem_cache_create("nfsd4_stateids",
  860. sizeof(struct nfs4_stateid), 0, 0, NULL);
  861. if (stateid_slab == NULL)
  862. goto out_nomem;
  863. deleg_slab = kmem_cache_create("nfsd4_delegations",
  864. sizeof(struct nfs4_delegation), 0, 0, NULL);
  865. if (deleg_slab == NULL)
  866. goto out_nomem;
  867. return 0;
  868. out_nomem:
  869. nfsd4_free_slabs();
  870. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  871. return -ENOMEM;
  872. }
  873. void
  874. nfs4_free_stateowner(struct kref *kref)
  875. {
  876. struct nfs4_stateowner *sop =
  877. container_of(kref, struct nfs4_stateowner, so_ref);
  878. kfree(sop->so_owner.data);
  879. kmem_cache_free(stateowner_slab, sop);
  880. }
  881. static inline struct nfs4_stateowner *
  882. alloc_stateowner(struct xdr_netobj *owner)
  883. {
  884. struct nfs4_stateowner *sop;
  885. if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
  886. if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
  887. memcpy(sop->so_owner.data, owner->data, owner->len);
  888. sop->so_owner.len = owner->len;
  889. kref_init(&sop->so_ref);
  890. return sop;
  891. }
  892. kmem_cache_free(stateowner_slab, sop);
  893. }
  894. return NULL;
  895. }
  896. static struct nfs4_stateowner *
  897. alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
  898. struct nfs4_stateowner *sop;
  899. struct nfs4_replay *rp;
  900. unsigned int idhashval;
  901. if (!(sop = alloc_stateowner(&open->op_owner)))
  902. return NULL;
  903. idhashval = ownerid_hashval(current_ownerid);
  904. INIT_LIST_HEAD(&sop->so_idhash);
  905. INIT_LIST_HEAD(&sop->so_strhash);
  906. INIT_LIST_HEAD(&sop->so_perclient);
  907. INIT_LIST_HEAD(&sop->so_stateids);
  908. INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
  909. INIT_LIST_HEAD(&sop->so_close_lru);
  910. sop->so_time = 0;
  911. list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
  912. list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
  913. list_add(&sop->so_perclient, &clp->cl_openowners);
  914. sop->so_is_open_owner = 1;
  915. sop->so_id = current_ownerid++;
  916. sop->so_client = clp;
  917. sop->so_seqid = open->op_seqid;
  918. sop->so_confirmed = 0;
  919. rp = &sop->so_replay;
  920. rp->rp_status = nfserr_serverfault;
  921. rp->rp_buflen = 0;
  922. rp->rp_buf = rp->rp_ibuf;
  923. return sop;
  924. }
  925. static void
  926. release_stateid_lockowners(struct nfs4_stateid *open_stp)
  927. {
  928. struct nfs4_stateowner *lock_sop;
  929. while (!list_empty(&open_stp->st_lockowners)) {
  930. lock_sop = list_entry(open_stp->st_lockowners.next,
  931. struct nfs4_stateowner, so_perstateid);
  932. /* list_del(&open_stp->st_lockowners); */
  933. BUG_ON(lock_sop->so_is_open_owner);
  934. release_stateowner(lock_sop);
  935. }
  936. }
  937. static void
  938. unhash_stateowner(struct nfs4_stateowner *sop)
  939. {
  940. struct nfs4_stateid *stp;
  941. list_del(&sop->so_idhash);
  942. list_del(&sop->so_strhash);
  943. if (sop->so_is_open_owner)
  944. list_del(&sop->so_perclient);
  945. list_del(&sop->so_perstateid);
  946. while (!list_empty(&sop->so_stateids)) {
  947. stp = list_entry(sop->so_stateids.next,
  948. struct nfs4_stateid, st_perstateowner);
  949. if (sop->so_is_open_owner)
  950. release_stateid(stp, OPEN_STATE);
  951. else
  952. release_stateid(stp, LOCK_STATE);
  953. }
  954. }
  955. static void
  956. release_stateowner(struct nfs4_stateowner *sop)
  957. {
  958. unhash_stateowner(sop);
  959. list_del(&sop->so_close_lru);
  960. nfs4_put_stateowner(sop);
  961. }
  962. static inline void
  963. init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  964. struct nfs4_stateowner *sop = open->op_stateowner;
  965. unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
  966. INIT_LIST_HEAD(&stp->st_hash);
  967. INIT_LIST_HEAD(&stp->st_perstateowner);
  968. INIT_LIST_HEAD(&stp->st_lockowners);
  969. INIT_LIST_HEAD(&stp->st_perfile);
  970. list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
  971. list_add(&stp->st_perstateowner, &sop->so_stateids);
  972. list_add(&stp->st_perfile, &fp->fi_stateids);
  973. stp->st_stateowner = sop;
  974. get_nfs4_file(fp);
  975. stp->st_file = fp;
  976. stp->st_stateid.si_boot = boot_time;
  977. stp->st_stateid.si_stateownerid = sop->so_id;
  978. stp->st_stateid.si_fileid = fp->fi_id;
  979. stp->st_stateid.si_generation = 0;
  980. stp->st_access_bmap = 0;
  981. stp->st_deny_bmap = 0;
  982. __set_bit(open->op_share_access, &stp->st_access_bmap);
  983. __set_bit(open->op_share_deny, &stp->st_deny_bmap);
  984. stp->st_openstp = NULL;
  985. }
  986. static void
  987. release_stateid(struct nfs4_stateid *stp, int flags)
  988. {
  989. struct file *filp = stp->st_vfs_file;
  990. list_del(&stp->st_hash);
  991. list_del(&stp->st_perfile);
  992. list_del(&stp->st_perstateowner);
  993. if (flags & OPEN_STATE) {
  994. release_stateid_lockowners(stp);
  995. stp->st_vfs_file = NULL;
  996. nfsd_close(filp);
  997. } else if (flags & LOCK_STATE)
  998. locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
  999. put_nfs4_file(stp->st_file);
  1000. kmem_cache_free(stateid_slab, stp);
  1001. }
  1002. static void
  1003. move_to_close_lru(struct nfs4_stateowner *sop)
  1004. {
  1005. dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
  1006. list_move_tail(&sop->so_close_lru, &close_lru);
  1007. sop->so_time = get_seconds();
  1008. }
  1009. static int
  1010. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
  1011. clientid_t *clid)
  1012. {
  1013. return (sop->so_owner.len == owner->len) &&
  1014. 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
  1015. (sop->so_client->cl_clientid.cl_id == clid->cl_id);
  1016. }
  1017. static struct nfs4_stateowner *
  1018. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
  1019. {
  1020. struct nfs4_stateowner *so = NULL;
  1021. list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
  1022. if (same_owner_str(so, &open->op_owner, &open->op_clientid))
  1023. return so;
  1024. }
  1025. return NULL;
  1026. }
  1027. /* search file_hashtbl[] for file */
  1028. static struct nfs4_file *
  1029. find_file(struct inode *ino)
  1030. {
  1031. unsigned int hashval = file_hashval(ino);
  1032. struct nfs4_file *fp;
  1033. list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  1034. if (fp->fi_inode == ino) {
  1035. get_nfs4_file(fp);
  1036. return fp;
  1037. }
  1038. }
  1039. return NULL;
  1040. }
  1041. static int access_valid(u32 x)
  1042. {
  1043. return (x > 0 && x < 4);
  1044. }
  1045. static int deny_valid(u32 x)
  1046. {
  1047. return (x >= 0 && x < 5);
  1048. }
  1049. static void
  1050. set_access(unsigned int *access, unsigned long bmap) {
  1051. int i;
  1052. *access = 0;
  1053. for (i = 1; i < 4; i++) {
  1054. if (test_bit(i, &bmap))
  1055. *access |= i;
  1056. }
  1057. }
  1058. static void
  1059. set_deny(unsigned int *deny, unsigned long bmap) {
  1060. int i;
  1061. *deny = 0;
  1062. for (i = 0; i < 4; i++) {
  1063. if (test_bit(i, &bmap))
  1064. *deny |= i ;
  1065. }
  1066. }
  1067. static int
  1068. test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
  1069. unsigned int access, deny;
  1070. set_access(&access, stp->st_access_bmap);
  1071. set_deny(&deny, stp->st_deny_bmap);
  1072. if ((access & open->op_share_deny) || (deny & open->op_share_access))
  1073. return 0;
  1074. return 1;
  1075. }
  1076. /*
  1077. * Called to check deny when READ with all zero stateid or
  1078. * WRITE with all zero or all one stateid
  1079. */
  1080. static __be32
  1081. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  1082. {
  1083. struct inode *ino = current_fh->fh_dentry->d_inode;
  1084. struct nfs4_file *fp;
  1085. struct nfs4_stateid *stp;
  1086. __be32 ret;
  1087. dprintk("NFSD: nfs4_share_conflict\n");
  1088. fp = find_file(ino);
  1089. if (!fp)
  1090. return nfs_ok;
  1091. ret = nfserr_locked;
  1092. /* Search for conflicting share reservations */
  1093. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  1094. if (test_bit(deny_type, &stp->st_deny_bmap) ||
  1095. test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
  1096. goto out;
  1097. }
  1098. ret = nfs_ok;
  1099. out:
  1100. put_nfs4_file(fp);
  1101. return ret;
  1102. }
  1103. static inline void
  1104. nfs4_file_downgrade(struct file *filp, unsigned int share_access)
  1105. {
  1106. if (share_access & NFS4_SHARE_ACCESS_WRITE) {
  1107. put_write_access(filp->f_path.dentry->d_inode);
  1108. filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
  1109. }
  1110. }
  1111. /*
  1112. * Recall a delegation
  1113. */
  1114. static int
  1115. do_recall(void *__dp)
  1116. {
  1117. struct nfs4_delegation *dp = __dp;
  1118. dp->dl_file->fi_had_conflict = true;
  1119. nfsd4_cb_recall(dp);
  1120. return 0;
  1121. }
  1122. /*
  1123. * Spawn a thread to perform a recall on the delegation represented
  1124. * by the lease (file_lock)
  1125. *
  1126. * Called from break_lease() with lock_kernel() held.
  1127. * Note: we assume break_lease will only call this *once* for any given
  1128. * lease.
  1129. */
  1130. static
  1131. void nfsd_break_deleg_cb(struct file_lock *fl)
  1132. {
  1133. struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
  1134. struct task_struct *t;
  1135. dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
  1136. if (!dp)
  1137. return;
  1138. /* We're assuming the state code never drops its reference
  1139. * without first removing the lease. Since we're in this lease
  1140. * callback (and since the lease code is serialized by the kernel
  1141. * lock) we know the server hasn't removed the lease yet, we know
  1142. * it's safe to take a reference: */
  1143. atomic_inc(&dp->dl_count);
  1144. atomic_inc(&dp->dl_client->cl_count);
  1145. spin_lock(&recall_lock);
  1146. list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
  1147. spin_unlock(&recall_lock);
  1148. /* only place dl_time is set. protected by lock_kernel*/
  1149. dp->dl_time = get_seconds();
  1150. /*
  1151. * We don't want the locks code to timeout the lease for us;
  1152. * we'll remove it ourself if the delegation isn't returned
  1153. * in time.
  1154. */
  1155. fl->fl_break_time = 0;
  1156. t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
  1157. if (IS_ERR(t)) {
  1158. struct nfs4_client *clp = dp->dl_client;
  1159. printk(KERN_INFO "NFSD: Callback thread failed for "
  1160. "for client (clientid %08x/%08x)\n",
  1161. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1162. put_nfs4_client(dp->dl_client);
  1163. nfs4_put_delegation(dp);
  1164. }
  1165. }
  1166. /*
  1167. * The file_lock is being reapd.
  1168. *
  1169. * Called by locks_free_lock() with lock_kernel() held.
  1170. */
  1171. static
  1172. void nfsd_release_deleg_cb(struct file_lock *fl)
  1173. {
  1174. struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
  1175. dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
  1176. if (!(fl->fl_flags & FL_LEASE) || !dp)
  1177. return;
  1178. dp->dl_flock = NULL;
  1179. }
  1180. /*
  1181. * Set the delegation file_lock back pointer.
  1182. *
  1183. * Called from setlease() with lock_kernel() held.
  1184. */
  1185. static
  1186. void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
  1187. {
  1188. struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
  1189. dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
  1190. if (!dp)
  1191. return;
  1192. dp->dl_flock = new;
  1193. }
  1194. /*
  1195. * Called from setlease() with lock_kernel() held
  1196. */
  1197. static
  1198. int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
  1199. {
  1200. struct nfs4_delegation *onlistd =
  1201. (struct nfs4_delegation *)onlist->fl_owner;
  1202. struct nfs4_delegation *tryd =
  1203. (struct nfs4_delegation *)try->fl_owner;
  1204. if (onlist->fl_lmops != try->fl_lmops)
  1205. return 0;
  1206. return onlistd->dl_client == tryd->dl_client;
  1207. }
  1208. static
  1209. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  1210. {
  1211. if (arg & F_UNLCK)
  1212. return lease_modify(onlist, arg);
  1213. else
  1214. return -EAGAIN;
  1215. }
  1216. static struct lock_manager_operations nfsd_lease_mng_ops = {
  1217. .fl_break = nfsd_break_deleg_cb,
  1218. .fl_release_private = nfsd_release_deleg_cb,
  1219. .fl_copy_lock = nfsd_copy_lock_deleg_cb,
  1220. .fl_mylease = nfsd_same_client_deleg_cb,
  1221. .fl_change = nfsd_change_deleg_cb,
  1222. };
  1223. __be32
  1224. nfsd4_process_open1(struct nfsd4_open *open)
  1225. {
  1226. clientid_t *clientid = &open->op_clientid;
  1227. struct nfs4_client *clp = NULL;
  1228. unsigned int strhashval;
  1229. struct nfs4_stateowner *sop = NULL;
  1230. if (!check_name(open->op_owner))
  1231. return nfserr_inval;
  1232. if (STALE_CLIENTID(&open->op_clientid))
  1233. return nfserr_stale_clientid;
  1234. strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
  1235. sop = find_openstateowner_str(strhashval, open);
  1236. open->op_stateowner = sop;
  1237. if (!sop) {
  1238. /* Make sure the client's lease hasn't expired. */
  1239. clp = find_confirmed_client(clientid);
  1240. if (clp == NULL)
  1241. return nfserr_expired;
  1242. goto renew;
  1243. }
  1244. if (!sop->so_confirmed) {
  1245. /* Replace unconfirmed owners without checking for replay. */
  1246. clp = sop->so_client;
  1247. release_stateowner(sop);
  1248. open->op_stateowner = NULL;
  1249. goto renew;
  1250. }
  1251. if (open->op_seqid == sop->so_seqid - 1) {
  1252. if (sop->so_replay.rp_buflen)
  1253. return nfserr_replay_me;
  1254. /* The original OPEN failed so spectacularly
  1255. * that we don't even have replay data saved!
  1256. * Therefore, we have no choice but to continue
  1257. * processing this OPEN; presumably, we'll
  1258. * fail again for the same reason.
  1259. */
  1260. dprintk("nfsd4_process_open1: replay with no replay cache\n");
  1261. goto renew;
  1262. }
  1263. if (open->op_seqid != sop->so_seqid)
  1264. return nfserr_bad_seqid;
  1265. renew:
  1266. if (open->op_stateowner == NULL) {
  1267. sop = alloc_init_open_stateowner(strhashval, clp, open);
  1268. if (sop == NULL)
  1269. return nfserr_resource;
  1270. open->op_stateowner = sop;
  1271. }
  1272. list_del_init(&sop->so_close_lru);
  1273. renew_client(sop->so_client);
  1274. return nfs_ok;
  1275. }
  1276. static inline __be32
  1277. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  1278. {
  1279. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  1280. return nfserr_openmode;
  1281. else
  1282. return nfs_ok;
  1283. }
  1284. static struct nfs4_delegation *
  1285. find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
  1286. {
  1287. struct nfs4_delegation *dp;
  1288. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
  1289. if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
  1290. return dp;
  1291. }
  1292. return NULL;
  1293. }
  1294. static __be32
  1295. nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
  1296. struct nfs4_delegation **dp)
  1297. {
  1298. int flags;
  1299. __be32 status = nfserr_bad_stateid;
  1300. *dp = find_delegation_file(fp, &open->op_delegate_stateid);
  1301. if (*dp == NULL)
  1302. goto out;
  1303. flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
  1304. RD_STATE : WR_STATE;
  1305. status = nfs4_check_delegmode(*dp, flags);
  1306. if (status)
  1307. *dp = NULL;
  1308. out:
  1309. if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
  1310. return nfs_ok;
  1311. if (status)
  1312. return status;
  1313. open->op_stateowner->so_confirmed = 1;
  1314. return nfs_ok;
  1315. }
  1316. static __be32
  1317. nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
  1318. {
  1319. struct nfs4_stateid *local;
  1320. __be32 status = nfserr_share_denied;
  1321. struct nfs4_stateowner *sop = open->op_stateowner;
  1322. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  1323. /* ignore lock owners */
  1324. if (local->st_stateowner->so_is_open_owner == 0)
  1325. continue;
  1326. /* remember if we have seen this open owner */
  1327. if (local->st_stateowner == sop)
  1328. *stpp = local;
  1329. /* check for conflicting share reservations */
  1330. if (!test_share(local, open))
  1331. goto out;
  1332. }
  1333. status = 0;
  1334. out:
  1335. return status;
  1336. }
  1337. static inline struct nfs4_stateid *
  1338. nfs4_alloc_stateid(void)
  1339. {
  1340. return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
  1341. }
  1342. static __be32
  1343. nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
  1344. struct nfs4_delegation *dp,
  1345. struct svc_fh *cur_fh, int flags)
  1346. {
  1347. struct nfs4_stateid *stp;
  1348. stp = nfs4_alloc_stateid();
  1349. if (stp == NULL)
  1350. return nfserr_resource;
  1351. if (dp) {
  1352. get_file(dp->dl_vfs_file);
  1353. stp->st_vfs_file = dp->dl_vfs_file;
  1354. } else {
  1355. __be32 status;
  1356. status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
  1357. &stp->st_vfs_file);
  1358. if (status) {
  1359. if (status == nfserr_dropit)
  1360. status = nfserr_jukebox;
  1361. kmem_cache_free(stateid_slab, stp);
  1362. return status;
  1363. }
  1364. }
  1365. *stpp = stp;
  1366. return 0;
  1367. }
  1368. static inline __be32
  1369. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  1370. struct nfsd4_open *open)
  1371. {
  1372. struct iattr iattr = {
  1373. .ia_valid = ATTR_SIZE,
  1374. .ia_size = 0,
  1375. };
  1376. if (!open->op_truncate)
  1377. return 0;
  1378. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  1379. return nfserr_inval;
  1380. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  1381. }
  1382. static __be32
  1383. nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
  1384. {
  1385. struct file *filp = stp->st_vfs_file;
  1386. struct inode *inode = filp->f_path.dentry->d_inode;
  1387. unsigned int share_access, new_writer;
  1388. __be32 status;
  1389. set_access(&share_access, stp->st_access_bmap);
  1390. new_writer = (~share_access) & open->op_share_access
  1391. & NFS4_SHARE_ACCESS_WRITE;
  1392. if (new_writer) {
  1393. int err = get_write_access(inode);
  1394. if (err)
  1395. return nfserrno(err);
  1396. }
  1397. status = nfsd4_truncate(rqstp, cur_fh, open);
  1398. if (status) {
  1399. if (new_writer)
  1400. put_write_access(inode);
  1401. return status;
  1402. }
  1403. /* remember the open */
  1404. filp->f_mode |= open->op_share_access;
  1405. set_bit(open->op_share_access, &stp->st_access_bmap);
  1406. set_bit(open->op_share_deny, &stp->st_deny_bmap);
  1407. return nfs_ok;
  1408. }
  1409. static void
  1410. nfs4_set_claim_prev(struct nfsd4_open *open)
  1411. {
  1412. open->op_stateowner->so_confirmed = 1;
  1413. open->op_stateowner->so_client->cl_firststate = 1;
  1414. }
  1415. /*
  1416. * Attempt to hand out a delegation.
  1417. */
  1418. static void
  1419. nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
  1420. {
  1421. struct nfs4_delegation *dp;
  1422. struct nfs4_stateowner *sop = stp->st_stateowner;
  1423. struct nfs4_callback *cb = &sop->so_client->cl_callback;
  1424. struct file_lock fl, *flp = &fl;
  1425. int status, flag = 0;
  1426. flag = NFS4_OPEN_DELEGATE_NONE;
  1427. open->op_recall = 0;
  1428. switch (open->op_claim_type) {
  1429. case NFS4_OPEN_CLAIM_PREVIOUS:
  1430. if (!atomic_read(&cb->cb_set))
  1431. open->op_recall = 1;
  1432. flag = open->op_delegate_type;
  1433. if (flag == NFS4_OPEN_DELEGATE_NONE)
  1434. goto out;
  1435. break;
  1436. case NFS4_OPEN_CLAIM_NULL:
  1437. /* Let's not give out any delegations till everyone's
  1438. * had the chance to reclaim theirs.... */
  1439. if (nfs4_in_grace())
  1440. goto out;
  1441. if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
  1442. goto out;
  1443. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  1444. flag = NFS4_OPEN_DELEGATE_WRITE;
  1445. else
  1446. flag = NFS4_OPEN_DELEGATE_READ;
  1447. break;
  1448. default:
  1449. goto out;
  1450. }
  1451. dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
  1452. if (dp == NULL) {
  1453. flag = NFS4_OPEN_DELEGATE_NONE;
  1454. goto out;
  1455. }
  1456. locks_init_lock(&fl);
  1457. fl.fl_lmops = &nfsd_lease_mng_ops;
  1458. fl.fl_flags = FL_LEASE;
  1459. fl.fl_end = OFFSET_MAX;
  1460. fl.fl_owner = (fl_owner_t)dp;
  1461. fl.fl_file = stp->st_vfs_file;
  1462. fl.fl_pid = current->tgid;
  1463. /* vfs_setlease checks to see if delegation should be handed out.
  1464. * the lock_manager callbacks fl_mylease and fl_change are used
  1465. */
  1466. if ((status = vfs_setlease(stp->st_vfs_file,
  1467. flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
  1468. dprintk("NFSD: setlease failed [%d], no delegation\n", status);
  1469. unhash_delegation(dp);
  1470. flag = NFS4_OPEN_DELEGATE_NONE;
  1471. goto out;
  1472. }
  1473. memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
  1474. dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
  1475. dp->dl_stateid.si_boot,
  1476. dp->dl_stateid.si_stateownerid,
  1477. dp->dl_stateid.si_fileid,
  1478. dp->dl_stateid.si_generation);
  1479. out:
  1480. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
  1481. && flag == NFS4_OPEN_DELEGATE_NONE
  1482. && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
  1483. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  1484. open->op_delegate_type = flag;
  1485. }
  1486. /*
  1487. * called with nfs4_lock_state() held.
  1488. */
  1489. __be32
  1490. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  1491. {
  1492. struct nfs4_file *fp = NULL;
  1493. struct inode *ino = current_fh->fh_dentry->d_inode;
  1494. struct nfs4_stateid *stp = NULL;
  1495. struct nfs4_delegation *dp = NULL;
  1496. __be32 status;
  1497. status = nfserr_inval;
  1498. if (!access_valid(open->op_share_access)
  1499. || !deny_valid(open->op_share_deny))
  1500. goto out;
  1501. /*
  1502. * Lookup file; if found, lookup stateid and check open request,
  1503. * and check for delegations in the process of being recalled.
  1504. * If not found, create the nfs4_file struct
  1505. */
  1506. fp = find_file(ino);
  1507. if (fp) {
  1508. if ((status = nfs4_check_open(fp, open, &stp)))
  1509. goto out;
  1510. status = nfs4_check_deleg(fp, open, &dp);
  1511. if (status)
  1512. goto out;
  1513. } else {
  1514. status = nfserr_bad_stateid;
  1515. if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
  1516. goto out;
  1517. status = nfserr_resource;
  1518. fp = alloc_init_file(ino);
  1519. if (fp == NULL)
  1520. goto out;
  1521. }
  1522. /*
  1523. * OPEN the file, or upgrade an existing OPEN.
  1524. * If truncate fails, the OPEN fails.
  1525. */
  1526. if (stp) {
  1527. /* Stateid was found, this is an OPEN upgrade */
  1528. status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
  1529. if (status)
  1530. goto out;
  1531. update_stateid(&stp->st_stateid);
  1532. } else {
  1533. /* Stateid was not found, this is a new OPEN */
  1534. int flags = 0;
  1535. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  1536. flags |= MAY_READ;
  1537. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  1538. flags |= MAY_WRITE;
  1539. status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
  1540. if (status)
  1541. goto out;
  1542. init_stateid(stp, fp, open);
  1543. status = nfsd4_truncate(rqstp, current_fh, open);
  1544. if (status) {
  1545. release_stateid(stp, OPEN_STATE);
  1546. goto out;
  1547. }
  1548. }
  1549. memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
  1550. /*
  1551. * Attempt to hand out a delegation. No error return, because the
  1552. * OPEN succeeds even if we fail.
  1553. */
  1554. nfs4_open_delegation(current_fh, open, stp);
  1555. status = nfs_ok;
  1556. dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
  1557. stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
  1558. stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
  1559. out:
  1560. if (fp)
  1561. put_nfs4_file(fp);
  1562. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  1563. nfs4_set_claim_prev(open);
  1564. /*
  1565. * To finish the open response, we just need to set the rflags.
  1566. */
  1567. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  1568. if (!open->op_stateowner->so_confirmed)
  1569. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  1570. return status;
  1571. }
  1572. static struct workqueue_struct *laundry_wq;
  1573. static void laundromat_main(struct work_struct *);
  1574. static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
  1575. __be32
  1576. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1577. clientid_t *clid)
  1578. {
  1579. struct nfs4_client *clp;
  1580. __be32 status;
  1581. nfs4_lock_state();
  1582. dprintk("process_renew(%08x/%08x): starting\n",
  1583. clid->cl_boot, clid->cl_id);
  1584. status = nfserr_stale_clientid;
  1585. if (STALE_CLIENTID(clid))
  1586. goto out;
  1587. clp = find_confirmed_client(clid);
  1588. status = nfserr_expired;
  1589. if (clp == NULL) {
  1590. /* We assume the client took too long to RENEW. */
  1591. dprintk("nfsd4_renew: clientid not found!\n");
  1592. goto out;
  1593. }
  1594. renew_client(clp);
  1595. status = nfserr_cb_path_down;
  1596. if (!list_empty(&clp->cl_delegations)
  1597. && !atomic_read(&clp->cl_callback.cb_set))
  1598. goto out;
  1599. status = nfs_ok;
  1600. out:
  1601. nfs4_unlock_state();
  1602. return status;
  1603. }
  1604. static void
  1605. end_grace(void)
  1606. {
  1607. dprintk("NFSD: end of grace period\n");
  1608. nfsd4_recdir_purge_old();
  1609. in_grace = 0;
  1610. }
  1611. static time_t
  1612. nfs4_laundromat(void)
  1613. {
  1614. struct nfs4_client *clp;
  1615. struct nfs4_stateowner *sop;
  1616. struct nfs4_delegation *dp;
  1617. struct list_head *pos, *next, reaplist;
  1618. time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
  1619. time_t t, clientid_val = NFSD_LEASE_TIME;
  1620. time_t u, test_val = NFSD_LEASE_TIME;
  1621. nfs4_lock_state();
  1622. dprintk("NFSD: laundromat service - starting\n");
  1623. if (in_grace)
  1624. end_grace();
  1625. list_for_each_safe(pos, next, &client_lru) {
  1626. clp = list_entry(pos, struct nfs4_client, cl_lru);
  1627. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  1628. t = clp->cl_time - cutoff;
  1629. if (clientid_val > t)
  1630. clientid_val = t;
  1631. break;
  1632. }
  1633. dprintk("NFSD: purging unused client (clientid %08x)\n",
  1634. clp->cl_clientid.cl_id);
  1635. nfsd4_remove_clid_dir(clp);
  1636. expire_client(clp);
  1637. }
  1638. INIT_LIST_HEAD(&reaplist);
  1639. spin_lock(&recall_lock);
  1640. list_for_each_safe(pos, next, &del_recall_lru) {
  1641. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  1642. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  1643. u = dp->dl_time - cutoff;
  1644. if (test_val > u)
  1645. test_val = u;
  1646. break;
  1647. }
  1648. dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
  1649. dp, dp->dl_flock);
  1650. list_move(&dp->dl_recall_lru, &reaplist);
  1651. }
  1652. spin_unlock(&recall_lock);
  1653. list_for_each_safe(pos, next, &reaplist) {
  1654. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  1655. list_del_init(&dp->dl_recall_lru);
  1656. unhash_delegation(dp);
  1657. }
  1658. test_val = NFSD_LEASE_TIME;
  1659. list_for_each_safe(pos, next, &close_lru) {
  1660. sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
  1661. if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
  1662. u = sop->so_time - cutoff;
  1663. if (test_val > u)
  1664. test_val = u;
  1665. break;
  1666. }
  1667. dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
  1668. sop->so_id);
  1669. release_stateowner(sop);
  1670. }
  1671. if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
  1672. clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
  1673. nfs4_unlock_state();
  1674. return clientid_val;
  1675. }
  1676. void
  1677. laundromat_main(struct work_struct *not_used)
  1678. {
  1679. time_t t;
  1680. t = nfs4_laundromat();
  1681. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  1682. queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
  1683. }
  1684. static struct nfs4_stateowner *
  1685. search_close_lru(u32 st_id, int flags)
  1686. {
  1687. struct nfs4_stateowner *local = NULL;
  1688. if (flags & CLOSE_STATE) {
  1689. list_for_each_entry(local, &close_lru, so_close_lru) {
  1690. if (local->so_id == st_id)
  1691. return local;
  1692. }
  1693. }
  1694. return NULL;
  1695. }
  1696. static inline int
  1697. nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
  1698. {
  1699. return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
  1700. }
  1701. static int
  1702. STALE_STATEID(stateid_t *stateid)
  1703. {
  1704. if (stateid->si_boot == boot_time)
  1705. return 0;
  1706. dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
  1707. stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
  1708. stateid->si_generation);
  1709. return 1;
  1710. }
  1711. static inline int
  1712. access_permit_read(unsigned long access_bmap)
  1713. {
  1714. return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
  1715. test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
  1716. test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
  1717. }
  1718. static inline int
  1719. access_permit_write(unsigned long access_bmap)
  1720. {
  1721. return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
  1722. test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
  1723. }
  1724. static
  1725. __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
  1726. {
  1727. __be32 status = nfserr_openmode;
  1728. if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
  1729. goto out;
  1730. if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
  1731. goto out;
  1732. status = nfs_ok;
  1733. out:
  1734. return status;
  1735. }
  1736. static inline __be32
  1737. check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
  1738. {
  1739. /* Trying to call delegreturn with a special stateid? Yuch: */
  1740. if (!(flags & (RD_STATE | WR_STATE)))
  1741. return nfserr_bad_stateid;
  1742. else if (ONE_STATEID(stateid) && (flags & RD_STATE))
  1743. return nfs_ok;
  1744. else if (nfs4_in_grace()) {
  1745. /* Answer in remaining cases depends on existance of
  1746. * conflicting state; so we must wait out the grace period. */
  1747. return nfserr_grace;
  1748. } else if (flags & WR_STATE)
  1749. return nfs4_share_conflict(current_fh,
  1750. NFS4_SHARE_DENY_WRITE);
  1751. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  1752. return nfs4_share_conflict(current_fh,
  1753. NFS4_SHARE_DENY_READ);
  1754. }
  1755. /*
  1756. * Allow READ/WRITE during grace period on recovered state only for files
  1757. * that are not able to provide mandatory locking.
  1758. */
  1759. static inline int
  1760. io_during_grace_disallowed(struct inode *inode, int flags)
  1761. {
  1762. return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
  1763. && mandatory_lock(inode);
  1764. }
  1765. /*
  1766. * Checks for stateid operations
  1767. */
  1768. __be32
  1769. nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
  1770. {
  1771. struct nfs4_stateid *stp = NULL;
  1772. struct nfs4_delegation *dp = NULL;
  1773. stateid_t *stidp;
  1774. struct inode *ino = current_fh->fh_dentry->d_inode;
  1775. __be32 status;
  1776. dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
  1777. stateid->si_boot, stateid->si_stateownerid,
  1778. stateid->si_fileid, stateid->si_generation);
  1779. if (filpp)
  1780. *filpp = NULL;
  1781. if (io_during_grace_disallowed(ino, flags))
  1782. return nfserr_grace;
  1783. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  1784. return check_special_stateids(current_fh, stateid, flags);
  1785. /* STALE STATEID */
  1786. status = nfserr_stale_stateid;
  1787. if (STALE_STATEID(stateid))
  1788. goto out;
  1789. /* BAD STATEID */
  1790. status = nfserr_bad_stateid;
  1791. if (!stateid->si_fileid) { /* delegation stateid */
  1792. if(!(dp = find_delegation_stateid(ino, stateid))) {
  1793. dprintk("NFSD: delegation stateid not found\n");
  1794. goto out;
  1795. }
  1796. stidp = &dp->dl_stateid;
  1797. } else { /* open or lock stateid */
  1798. if (!(stp = find_stateid(stateid, flags))) {
  1799. dprintk("NFSD: open or lock stateid not found\n");
  1800. goto out;
  1801. }
  1802. if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
  1803. goto out;
  1804. if (!stp->st_stateowner->so_confirmed)
  1805. goto out;
  1806. stidp = &stp->st_stateid;
  1807. }
  1808. if (stateid->si_generation > stidp->si_generation)
  1809. goto out;
  1810. /* OLD STATEID */
  1811. status = nfserr_old_stateid;
  1812. if (stateid->si_generation < stidp->si_generation)
  1813. goto out;
  1814. if (stp) {
  1815. if ((status = nfs4_check_openmode(stp,flags)))
  1816. goto out;
  1817. renew_client(stp->st_stateowner->so_client);
  1818. if (filpp)
  1819. *filpp = stp->st_vfs_file;
  1820. } else if (dp) {
  1821. if ((status = nfs4_check_delegmode(dp, flags)))
  1822. goto out;
  1823. renew_client(dp->dl_client);
  1824. if (flags & DELEG_RET)
  1825. unhash_delegation(dp);
  1826. if (filpp)
  1827. *filpp = dp->dl_vfs_file;
  1828. }
  1829. status = nfs_ok;
  1830. out:
  1831. return status;
  1832. }
  1833. static inline int
  1834. setlkflg (int type)
  1835. {
  1836. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  1837. RD_STATE : WR_STATE;
  1838. }
  1839. /*
  1840. * Checks for sequence id mutating operations.
  1841. */
  1842. static __be32
  1843. 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)
  1844. {
  1845. struct nfs4_stateid *stp;
  1846. struct nfs4_stateowner *sop;
  1847. dprintk("NFSD: preprocess_seqid_op: seqid=%d "
  1848. "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
  1849. stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
  1850. stateid->si_generation);
  1851. *stpp = NULL;
  1852. *sopp = NULL;
  1853. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
  1854. dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
  1855. return nfserr_bad_stateid;
  1856. }
  1857. if (STALE_STATEID(stateid))
  1858. return nfserr_stale_stateid;
  1859. /*
  1860. * We return BAD_STATEID if filehandle doesn't match stateid,
  1861. * the confirmed flag is incorrecly set, or the generation
  1862. * number is incorrect.
  1863. */
  1864. stp = find_stateid(stateid, flags);
  1865. if (stp == NULL) {
  1866. /*
  1867. * Also, we should make sure this isn't just the result of
  1868. * a replayed close:
  1869. */
  1870. sop = search_close_lru(stateid->si_stateownerid, flags);
  1871. if (sop == NULL)
  1872. return nfserr_bad_stateid;
  1873. *sopp = sop;
  1874. goto check_replay;
  1875. }
  1876. if (lock) {
  1877. struct nfs4_stateowner *sop = stp->st_stateowner;
  1878. clientid_t *lockclid = &lock->v.new.clientid;
  1879. struct nfs4_client *clp = sop->so_client;
  1880. int lkflg = 0;
  1881. __be32 status;
  1882. lkflg = setlkflg(lock->lk_type);
  1883. if (lock->lk_is_new) {
  1884. if (!sop->so_is_open_owner)
  1885. return nfserr_bad_stateid;
  1886. if (!same_clid(&clp->cl_clientid, lockclid))
  1887. return nfserr_bad_stateid;
  1888. /* stp is the open stateid */
  1889. status = nfs4_check_openmode(stp, lkflg);
  1890. if (status)
  1891. return status;
  1892. } else {
  1893. /* stp is the lock stateid */
  1894. status = nfs4_check_openmode(stp->st_openstp, lkflg);
  1895. if (status)
  1896. return status;
  1897. }
  1898. }
  1899. if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
  1900. dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
  1901. return nfserr_bad_stateid;
  1902. }
  1903. *stpp = stp;
  1904. *sopp = sop = stp->st_stateowner;
  1905. /*
  1906. * We now validate the seqid and stateid generation numbers.
  1907. * For the moment, we ignore the possibility of
  1908. * generation number wraparound.
  1909. */
  1910. if (seqid != sop->so_seqid)
  1911. goto check_replay;
  1912. if (sop->so_confirmed && flags & CONFIRM) {
  1913. dprintk("NFSD: preprocess_seqid_op: expected"
  1914. " unconfirmed stateowner!\n");
  1915. return nfserr_bad_stateid;
  1916. }
  1917. if (!sop->so_confirmed && !(flags & CONFIRM)) {
  1918. dprintk("NFSD: preprocess_seqid_op: stateowner not"
  1919. " confirmed yet!\n");
  1920. return nfserr_bad_stateid;
  1921. }
  1922. if (stateid->si_generation > stp->st_stateid.si_generation) {
  1923. dprintk("NFSD: preprocess_seqid_op: future stateid?!\n");
  1924. return nfserr_bad_stateid;
  1925. }
  1926. if (stateid->si_generation < stp->st_stateid.si_generation) {
  1927. dprintk("NFSD: preprocess_seqid_op: old stateid!\n");
  1928. return nfserr_old_stateid;
  1929. }
  1930. renew_client(sop->so_client);
  1931. return nfs_ok;
  1932. check_replay:
  1933. if (seqid == sop->so_seqid - 1) {
  1934. dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
  1935. /* indicate replay to calling function */
  1936. return nfserr_replay_me;
  1937. }
  1938. dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
  1939. sop->so_seqid, seqid);
  1940. *sopp = NULL;
  1941. return nfserr_bad_seqid;
  1942. }
  1943. __be32
  1944. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1945. struct nfsd4_open_confirm *oc)
  1946. {
  1947. __be32 status;
  1948. struct nfs4_stateowner *sop;
  1949. struct nfs4_stateid *stp;
  1950. dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
  1951. (int)cstate->current_fh.fh_dentry->d_name.len,
  1952. cstate->current_fh.fh_dentry->d_name.name);
  1953. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  1954. if (status)
  1955. return status;
  1956. nfs4_lock_state();
  1957. if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
  1958. oc->oc_seqid, &oc->oc_req_stateid,
  1959. CHECK_FH | CONFIRM | OPEN_STATE,
  1960. &oc->oc_stateowner, &stp, NULL)))
  1961. goto out;
  1962. sop = oc->oc_stateowner;
  1963. sop->so_confirmed = 1;
  1964. update_stateid(&stp->st_stateid);
  1965. memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
  1966. dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
  1967. "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
  1968. stp->st_stateid.si_boot,
  1969. stp->st_stateid.si_stateownerid,
  1970. stp->st_stateid.si_fileid,
  1971. stp->st_stateid.si_generation);
  1972. nfsd4_create_clid_dir(sop->so_client);
  1973. out:
  1974. if (oc->oc_stateowner) {
  1975. nfs4_get_stateowner(oc->oc_stateowner);
  1976. cstate->replay_owner = oc->oc_stateowner;
  1977. }
  1978. nfs4_unlock_state();
  1979. return status;
  1980. }
  1981. /*
  1982. * unset all bits in union bitmap (bmap) that
  1983. * do not exist in share (from successful OPEN_DOWNGRADE)
  1984. */
  1985. static void
  1986. reset_union_bmap_access(unsigned long access, unsigned long *bmap)
  1987. {
  1988. int i;
  1989. for (i = 1; i < 4; i++) {
  1990. if ((i & access) != i)
  1991. __clear_bit(i, bmap);
  1992. }
  1993. }
  1994. static void
  1995. reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
  1996. {
  1997. int i;
  1998. for (i = 0; i < 4; i++) {
  1999. if ((i & deny) != i)
  2000. __clear_bit(i, bmap);
  2001. }
  2002. }
  2003. __be32
  2004. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  2005. struct nfsd4_compound_state *cstate,
  2006. struct nfsd4_open_downgrade *od)
  2007. {
  2008. __be32 status;
  2009. struct nfs4_stateid *stp;
  2010. unsigned int share_access;
  2011. dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
  2012. (int)cstate->current_fh.fh_dentry->d_name.len,
  2013. cstate->current_fh.fh_dentry->d_name.name);
  2014. if (!access_valid(od->od_share_access)
  2015. || !deny_valid(od->od_share_deny))
  2016. return nfserr_inval;
  2017. nfs4_lock_state();
  2018. if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
  2019. od->od_seqid,
  2020. &od->od_stateid,
  2021. CHECK_FH | OPEN_STATE,
  2022. &od->od_stateowner, &stp, NULL)))
  2023. goto out;
  2024. status = nfserr_inval;
  2025. if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
  2026. dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
  2027. stp->st_access_bmap, od->od_share_access);
  2028. goto out;
  2029. }
  2030. if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
  2031. dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
  2032. stp->st_deny_bmap, od->od_share_deny);
  2033. goto out;
  2034. }
  2035. set_access(&share_access, stp->st_access_bmap);
  2036. nfs4_file_downgrade(stp->st_vfs_file,
  2037. share_access & ~od->od_share_access);
  2038. reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
  2039. reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
  2040. update_stateid(&stp->st_stateid);
  2041. memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
  2042. status = nfs_ok;
  2043. out:
  2044. if (od->od_stateowner) {
  2045. nfs4_get_stateowner(od->od_stateowner);
  2046. cstate->replay_owner = od->od_stateowner;
  2047. }
  2048. nfs4_unlock_state();
  2049. return status;
  2050. }
  2051. /*
  2052. * nfs4_unlock_state() called after encode
  2053. */
  2054. __be32
  2055. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2056. struct nfsd4_close *close)
  2057. {
  2058. __be32 status;
  2059. struct nfs4_stateid *stp;
  2060. dprintk("NFSD: nfsd4_close on file %.*s\n",
  2061. (int)cstate->current_fh.fh_dentry->d_name.len,
  2062. cstate->current_fh.fh_dentry->d_name.name);
  2063. nfs4_lock_state();
  2064. /* check close_lru for replay */
  2065. if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
  2066. close->cl_seqid,
  2067. &close->cl_stateid,
  2068. CHECK_FH | OPEN_STATE | CLOSE_STATE,
  2069. &close->cl_stateowner, &stp, NULL)))
  2070. goto out;
  2071. status = nfs_ok;
  2072. update_stateid(&stp->st_stateid);
  2073. memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
  2074. /* release_stateid() calls nfsd_close() if needed */
  2075. release_stateid(stp, OPEN_STATE);
  2076. /* place unused nfs4_stateowners on so_close_lru list to be
  2077. * released by the laundromat service after the lease period
  2078. * to enable us to handle CLOSE replay
  2079. */
  2080. if (list_empty(&close->cl_stateowner->so_stateids))
  2081. move_to_close_lru(close->cl_stateowner);
  2082. out:
  2083. if (close->cl_stateowner) {
  2084. nfs4_get_stateowner(close->cl_stateowner);
  2085. cstate->replay_owner = close->cl_stateowner;
  2086. }
  2087. nfs4_unlock_state();
  2088. return status;
  2089. }
  2090. __be32
  2091. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2092. struct nfsd4_delegreturn *dr)
  2093. {
  2094. __be32 status;
  2095. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  2096. goto out;
  2097. nfs4_lock_state();
  2098. status = nfs4_preprocess_stateid_op(&cstate->current_fh,
  2099. &dr->dr_stateid, DELEG_RET, NULL);
  2100. nfs4_unlock_state();
  2101. out:
  2102. return status;
  2103. }
  2104. /*
  2105. * Lock owner state (byte-range locks)
  2106. */
  2107. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  2108. #define LOCK_HASH_BITS 8
  2109. #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
  2110. #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
  2111. #define lockownerid_hashval(id) \
  2112. ((id) & LOCK_HASH_MASK)
  2113. static inline unsigned int
  2114. lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
  2115. struct xdr_netobj *ownername)
  2116. {
  2117. return (file_hashval(inode) + cl_id
  2118. + opaque_hashval(ownername->data, ownername->len))
  2119. & LOCK_HASH_MASK;
  2120. }
  2121. static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
  2122. static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
  2123. static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
  2124. static struct nfs4_stateid *
  2125. find_stateid(stateid_t *stid, int flags)
  2126. {
  2127. struct nfs4_stateid *local = NULL;
  2128. u32 st_id = stid->si_stateownerid;
  2129. u32 f_id = stid->si_fileid;
  2130. unsigned int hashval;
  2131. dprintk("NFSD: find_stateid flags 0x%x\n",flags);
  2132. if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
  2133. hashval = stateid_hashval(st_id, f_id);
  2134. list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
  2135. if ((local->st_stateid.si_stateownerid == st_id) &&
  2136. (local->st_stateid.si_fileid == f_id))
  2137. return local;
  2138. }
  2139. }
  2140. if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
  2141. hashval = stateid_hashval(st_id, f_id);
  2142. list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
  2143. if ((local->st_stateid.si_stateownerid == st_id) &&
  2144. (local->st_stateid.si_fileid == f_id))
  2145. return local;
  2146. }
  2147. }
  2148. return NULL;
  2149. }
  2150. static struct nfs4_delegation *
  2151. find_delegation_stateid(struct inode *ino, stateid_t *stid)
  2152. {
  2153. struct nfs4_file *fp;
  2154. struct nfs4_delegation *dl;
  2155. dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
  2156. stid->si_boot, stid->si_stateownerid,
  2157. stid->si_fileid, stid->si_generation);
  2158. fp = find_file(ino);
  2159. if (!fp)
  2160. return NULL;
  2161. dl = find_delegation_file(fp, stid);
  2162. put_nfs4_file(fp);
  2163. return dl;
  2164. }
  2165. /*
  2166. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  2167. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  2168. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  2169. * locking, this prevents us from being completely protocol-compliant. The
  2170. * real solution to this problem is to start using unsigned file offsets in
  2171. * the VFS, but this is a very deep change!
  2172. */
  2173. static inline void
  2174. nfs4_transform_lock_offset(struct file_lock *lock)
  2175. {
  2176. if (lock->fl_start < 0)
  2177. lock->fl_start = OFFSET_MAX;
  2178. if (lock->fl_end < 0)
  2179. lock->fl_end = OFFSET_MAX;
  2180. }
  2181. /* Hack!: For now, we're defining this just so we can use a pointer to it
  2182. * as a unique cookie to identify our (NFSv4's) posix locks. */
  2183. static struct lock_manager_operations nfsd_posix_mng_ops = {
  2184. };
  2185. static inline void
  2186. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  2187. {
  2188. struct nfs4_stateowner *sop;
  2189. unsigned int hval;
  2190. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  2191. sop = (struct nfs4_stateowner *) fl->fl_owner;
  2192. hval = lockownerid_hashval(sop->so_id);
  2193. kref_get(&sop->so_ref);
  2194. deny->ld_sop = sop;
  2195. deny->ld_clientid = sop->so_client->cl_clientid;
  2196. } else {
  2197. deny->ld_sop = NULL;
  2198. deny->ld_clientid.cl_boot = 0;
  2199. deny->ld_clientid.cl_id = 0;
  2200. }
  2201. deny->ld_start = fl->fl_start;
  2202. deny->ld_length = ~(u64)0;
  2203. if (fl->fl_end != ~(u64)0)
  2204. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  2205. deny->ld_type = NFS4_READ_LT;
  2206. if (fl->fl_type != F_RDLCK)
  2207. deny->ld_type = NFS4_WRITE_LT;
  2208. }
  2209. static struct nfs4_stateowner *
  2210. find_lockstateowner_str(struct inode *inode, clientid_t *clid,
  2211. struct xdr_netobj *owner)
  2212. {
  2213. unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
  2214. struct nfs4_stateowner *op;
  2215. list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
  2216. if (same_owner_str(op, owner, clid))
  2217. return op;
  2218. }
  2219. return NULL;
  2220. }
  2221. /*
  2222. * Alloc a lock owner structure.
  2223. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  2224. * occured.
  2225. *
  2226. * strhashval = lock_ownerstr_hashval
  2227. */
  2228. static struct nfs4_stateowner *
  2229. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
  2230. struct nfs4_stateowner *sop;
  2231. struct nfs4_replay *rp;
  2232. unsigned int idhashval;
  2233. if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
  2234. return NULL;
  2235. idhashval = lockownerid_hashval(current_ownerid);
  2236. INIT_LIST_HEAD(&sop->so_idhash);
  2237. INIT_LIST_HEAD(&sop->so_strhash);
  2238. INIT_LIST_HEAD(&sop->so_perclient);
  2239. INIT_LIST_HEAD(&sop->so_stateids);
  2240. INIT_LIST_HEAD(&sop->so_perstateid);
  2241. INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
  2242. sop->so_time = 0;
  2243. list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
  2244. list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
  2245. list_add(&sop->so_perstateid, &open_stp->st_lockowners);
  2246. sop->so_is_open_owner = 0;
  2247. sop->so_id = current_ownerid++;
  2248. sop->so_client = clp;
  2249. /* It is the openowner seqid that will be incremented in encode in the
  2250. * case of new lockowners; so increment the lock seqid manually: */
  2251. sop->so_seqid = lock->lk_new_lock_seqid + 1;
  2252. sop->so_confirmed = 1;
  2253. rp = &sop->so_replay;
  2254. rp->rp_status = nfserr_serverfault;
  2255. rp->rp_buflen = 0;
  2256. rp->rp_buf = rp->rp_ibuf;
  2257. return sop;
  2258. }
  2259. static struct nfs4_stateid *
  2260. alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
  2261. {
  2262. struct nfs4_stateid *stp;
  2263. unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
  2264. stp = nfs4_alloc_stateid();
  2265. if (stp == NULL)
  2266. goto out;
  2267. INIT_LIST_HEAD(&stp->st_hash);
  2268. INIT_LIST_HEAD(&stp->st_perfile);
  2269. INIT_LIST_HEAD(&stp->st_perstateowner);
  2270. INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
  2271. list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
  2272. list_add(&stp->st_perfile, &fp->fi_stateids);
  2273. list_add(&stp->st_perstateowner, &sop->so_stateids);
  2274. stp->st_stateowner = sop;
  2275. get_nfs4_file(fp);
  2276. stp->st_file = fp;
  2277. stp->st_stateid.si_boot = boot_time;
  2278. stp->st_stateid.si_stateownerid = sop->so_id;
  2279. stp->st_stateid.si_fileid = fp->fi_id;
  2280. stp->st_stateid.si_generation = 0;
  2281. stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
  2282. stp->st_access_bmap = open_stp->st_access_bmap;
  2283. stp->st_deny_bmap = open_stp->st_deny_bmap;
  2284. stp->st_openstp = open_stp;
  2285. out:
  2286. return stp;
  2287. }
  2288. static int
  2289. check_lock_length(u64 offset, u64 length)
  2290. {
  2291. return ((length == 0) || ((length != ~(u64)0) &&
  2292. LOFF_OVERFLOW(offset, length)));
  2293. }
  2294. /*
  2295. * LOCK operation
  2296. */
  2297. __be32
  2298. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2299. struct nfsd4_lock *lock)
  2300. {
  2301. struct nfs4_stateowner *open_sop = NULL;
  2302. struct nfs4_stateowner *lock_sop = NULL;
  2303. struct nfs4_stateid *lock_stp;
  2304. struct file *filp;
  2305. struct file_lock file_lock;
  2306. struct file_lock conflock;
  2307. __be32 status = 0;
  2308. unsigned int strhashval;
  2309. unsigned int cmd;
  2310. int err;
  2311. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  2312. (long long) lock->lk_offset,
  2313. (long long) lock->lk_length);
  2314. if (check_lock_length(lock->lk_offset, lock->lk_length))
  2315. return nfserr_inval;
  2316. if ((status = fh_verify(rqstp, &cstate->current_fh,
  2317. S_IFREG, MAY_LOCK))) {
  2318. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  2319. return status;
  2320. }
  2321. nfs4_lock_state();
  2322. if (lock->lk_is_new) {
  2323. /*
  2324. * Client indicates that this is a new lockowner.
  2325. * Use open owner and open stateid to create lock owner and
  2326. * lock stateid.
  2327. */
  2328. struct nfs4_stateid *open_stp = NULL;
  2329. struct nfs4_file *fp;
  2330. status = nfserr_stale_clientid;
  2331. if (STALE_CLIENTID(&lock->lk_new_clientid))
  2332. goto out;
  2333. /* validate and update open stateid and open seqid */
  2334. status = nfs4_preprocess_seqid_op(&cstate->current_fh,
  2335. lock->lk_new_open_seqid,
  2336. &lock->lk_new_open_stateid,
  2337. CHECK_FH | OPEN_STATE,
  2338. &lock->lk_replay_owner, &open_stp,
  2339. lock);
  2340. if (status)
  2341. goto out;
  2342. open_sop = lock->lk_replay_owner;
  2343. /* create lockowner and lock stateid */
  2344. fp = open_stp->st_file;
  2345. strhashval = lock_ownerstr_hashval(fp->fi_inode,
  2346. open_sop->so_client->cl_clientid.cl_id,
  2347. &lock->v.new.owner);
  2348. /* XXX: Do we need to check for duplicate stateowners on
  2349. * the same file, or should they just be allowed (and
  2350. * create new stateids)? */
  2351. status = nfserr_resource;
  2352. lock_sop = alloc_init_lock_stateowner(strhashval,
  2353. open_sop->so_client, open_stp, lock);
  2354. if (lock_sop == NULL)
  2355. goto out;
  2356. lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
  2357. if (lock_stp == NULL)
  2358. goto out;
  2359. } else {
  2360. /* lock (lock owner + lock stateid) already exists */
  2361. status = nfs4_preprocess_seqid_op(&cstate->current_fh,
  2362. lock->lk_old_lock_seqid,
  2363. &lock->lk_old_lock_stateid,
  2364. CHECK_FH | LOCK_STATE,
  2365. &lock->lk_replay_owner, &lock_stp, lock);
  2366. if (status)
  2367. goto out;
  2368. lock_sop = lock->lk_replay_owner;
  2369. }
  2370. /* lock->lk_replay_owner and lock_stp have been created or found */
  2371. filp = lock_stp->st_vfs_file;
  2372. status = nfserr_grace;
  2373. if (nfs4_in_grace() && !lock->lk_reclaim)
  2374. goto out;
  2375. status = nfserr_no_grace;
  2376. if (!nfs4_in_grace() && lock->lk_reclaim)
  2377. goto out;
  2378. locks_init_lock(&file_lock);
  2379. switch (lock->lk_type) {
  2380. case NFS4_READ_LT:
  2381. case NFS4_READW_LT:
  2382. file_lock.fl_type = F_RDLCK;
  2383. cmd = F_SETLK;
  2384. break;
  2385. case NFS4_WRITE_LT:
  2386. case NFS4_WRITEW_LT:
  2387. file_lock.fl_type = F_WRLCK;
  2388. cmd = F_SETLK;
  2389. break;
  2390. default:
  2391. status = nfserr_inval;
  2392. goto out;
  2393. }
  2394. file_lock.fl_owner = (fl_owner_t)lock_sop;
  2395. file_lock.fl_pid = current->tgid;
  2396. file_lock.fl_file = filp;
  2397. file_lock.fl_flags = FL_POSIX;
  2398. file_lock.fl_lmops = &nfsd_posix_mng_ops;
  2399. file_lock.fl_start = lock->lk_offset;
  2400. if ((lock->lk_length == ~(u64)0) ||
  2401. LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
  2402. file_lock.fl_end = ~(u64)0;
  2403. else
  2404. file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
  2405. nfs4_transform_lock_offset(&file_lock);
  2406. /*
  2407. * Try to lock the file in the VFS.
  2408. * Note: locks.c uses the BKL to protect the inode's lock list.
  2409. */
  2410. /* XXX?: Just to divert the locks_release_private at the start of
  2411. * locks_copy_lock: */
  2412. locks_init_lock(&conflock);
  2413. err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
  2414. switch (-err) {
  2415. case 0: /* success! */
  2416. update_stateid(&lock_stp->st_stateid);
  2417. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
  2418. sizeof(stateid_t));
  2419. status = 0;
  2420. break;
  2421. case (EAGAIN): /* conflock holds conflicting lock */
  2422. status = nfserr_denied;
  2423. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  2424. nfs4_set_lock_denied(&conflock, &lock->lk_denied);
  2425. break;
  2426. case (EDEADLK):
  2427. status = nfserr_deadlock;
  2428. break;
  2429. default:
  2430. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  2431. status = nfserr_resource;
  2432. break;
  2433. }
  2434. out:
  2435. if (status && lock->lk_is_new && lock_sop)
  2436. release_stateowner(lock_sop);
  2437. if (lock->lk_replay_owner) {
  2438. nfs4_get_stateowner(lock->lk_replay_owner);
  2439. cstate->replay_owner = lock->lk_replay_owner;
  2440. }
  2441. nfs4_unlock_state();
  2442. return status;
  2443. }
  2444. /*
  2445. * LOCKT operation
  2446. */
  2447. __be32
  2448. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2449. struct nfsd4_lockt *lockt)
  2450. {
  2451. struct inode *inode;
  2452. struct file file;
  2453. struct file_lock file_lock;
  2454. int error;
  2455. __be32 status;
  2456. if (nfs4_in_grace())
  2457. return nfserr_grace;
  2458. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  2459. return nfserr_inval;
  2460. lockt->lt_stateowner = NULL;
  2461. nfs4_lock_state();
  2462. status = nfserr_stale_clientid;
  2463. if (STALE_CLIENTID(&lockt->lt_clientid))
  2464. goto out;
  2465. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
  2466. dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
  2467. if (status == nfserr_symlink)
  2468. status = nfserr_inval;
  2469. goto out;
  2470. }
  2471. inode = cstate->current_fh.fh_dentry->d_inode;
  2472. locks_init_lock(&file_lock);
  2473. switch (lockt->lt_type) {
  2474. case NFS4_READ_LT:
  2475. case NFS4_READW_LT:
  2476. file_lock.fl_type = F_RDLCK;
  2477. break;
  2478. case NFS4_WRITE_LT:
  2479. case NFS4_WRITEW_LT:
  2480. file_lock.fl_type = F_WRLCK;
  2481. break;
  2482. default:
  2483. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  2484. status = nfserr_inval;
  2485. goto out;
  2486. }
  2487. lockt->lt_stateowner = find_lockstateowner_str(inode,
  2488. &lockt->lt_clientid, &lockt->lt_owner);
  2489. if (lockt->lt_stateowner)
  2490. file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
  2491. file_lock.fl_pid = current->tgid;
  2492. file_lock.fl_flags = FL_POSIX;
  2493. file_lock.fl_lmops = &nfsd_posix_mng_ops;
  2494. file_lock.fl_start = lockt->lt_offset;
  2495. if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
  2496. file_lock.fl_end = ~(u64)0;
  2497. else
  2498. file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
  2499. nfs4_transform_lock_offset(&file_lock);
  2500. /* vfs_test_lock uses the struct file _only_ to resolve the inode.
  2501. * since LOCKT doesn't require an OPEN, and therefore a struct
  2502. * file may not exist, pass vfs_test_lock a struct file with
  2503. * only the dentry:inode set.
  2504. */
  2505. memset(&file, 0, sizeof (struct file));
  2506. file.f_path.dentry = cstate->current_fh.fh_dentry;
  2507. status = nfs_ok;
  2508. error = vfs_test_lock(&file, &file_lock);
  2509. if (error) {
  2510. status = nfserrno(error);
  2511. goto out;
  2512. }
  2513. if (file_lock.fl_type != F_UNLCK) {
  2514. status = nfserr_denied;
  2515. nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
  2516. }
  2517. out:
  2518. nfs4_unlock_state();
  2519. return status;
  2520. }
  2521. __be32
  2522. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2523. struct nfsd4_locku *locku)
  2524. {
  2525. struct nfs4_stateid *stp;
  2526. struct file *filp = NULL;
  2527. struct file_lock file_lock;
  2528. __be32 status;
  2529. int err;
  2530. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  2531. (long long) locku->lu_offset,
  2532. (long long) locku->lu_length);
  2533. if (check_lock_length(locku->lu_offset, locku->lu_length))
  2534. return nfserr_inval;
  2535. nfs4_lock_state();
  2536. if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
  2537. locku->lu_seqid,
  2538. &locku->lu_stateid,
  2539. CHECK_FH | LOCK_STATE,
  2540. &locku->lu_stateowner, &stp, NULL)))
  2541. goto out;
  2542. filp = stp->st_vfs_file;
  2543. BUG_ON(!filp);
  2544. locks_init_lock(&file_lock);
  2545. file_lock.fl_type = F_UNLCK;
  2546. file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
  2547. file_lock.fl_pid = current->tgid;
  2548. file_lock.fl_file = filp;
  2549. file_lock.fl_flags = FL_POSIX;
  2550. file_lock.fl_lmops = &nfsd_posix_mng_ops;
  2551. file_lock.fl_start = locku->lu_offset;
  2552. if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
  2553. file_lock.fl_end = ~(u64)0;
  2554. else
  2555. file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
  2556. nfs4_transform_lock_offset(&file_lock);
  2557. /*
  2558. * Try to unlock the file in the VFS.
  2559. */
  2560. err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
  2561. if (err) {
  2562. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  2563. goto out_nfserr;
  2564. }
  2565. /*
  2566. * OK, unlock succeeded; the only thing left to do is update the stateid.
  2567. */
  2568. update_stateid(&stp->st_stateid);
  2569. memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
  2570. out:
  2571. if (locku->lu_stateowner) {
  2572. nfs4_get_stateowner(locku->lu_stateowner);
  2573. cstate->replay_owner = locku->lu_stateowner;
  2574. }
  2575. nfs4_unlock_state();
  2576. return status;
  2577. out_nfserr:
  2578. status = nfserrno(err);
  2579. goto out;
  2580. }
  2581. /*
  2582. * returns
  2583. * 1: locks held by lockowner
  2584. * 0: no locks held by lockowner
  2585. */
  2586. static int
  2587. check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
  2588. {
  2589. struct file_lock **flpp;
  2590. struct inode *inode = filp->f_path.dentry->d_inode;
  2591. int status = 0;
  2592. lock_kernel();
  2593. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  2594. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  2595. status = 1;
  2596. goto out;
  2597. }
  2598. }
  2599. out:
  2600. unlock_kernel();
  2601. return status;
  2602. }
  2603. __be32
  2604. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  2605. struct nfsd4_compound_state *cstate,
  2606. struct nfsd4_release_lockowner *rlockowner)
  2607. {
  2608. clientid_t *clid = &rlockowner->rl_clientid;
  2609. struct nfs4_stateowner *sop;
  2610. struct nfs4_stateid *stp;
  2611. struct xdr_netobj *owner = &rlockowner->rl_owner;
  2612. struct list_head matches;
  2613. int i;
  2614. __be32 status;
  2615. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  2616. clid->cl_boot, clid->cl_id);
  2617. /* XXX check for lease expiration */
  2618. status = nfserr_stale_clientid;
  2619. if (STALE_CLIENTID(clid))
  2620. return status;
  2621. nfs4_lock_state();
  2622. status = nfserr_locks_held;
  2623. /* XXX: we're doing a linear search through all the lockowners.
  2624. * Yipes! For now we'll just hope clients aren't really using
  2625. * release_lockowner much, but eventually we have to fix these
  2626. * data structures. */
  2627. INIT_LIST_HEAD(&matches);
  2628. for (i = 0; i < LOCK_HASH_SIZE; i++) {
  2629. list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
  2630. if (!same_owner_str(sop, owner, clid))
  2631. continue;
  2632. list_for_each_entry(stp, &sop->so_stateids,
  2633. st_perstateowner) {
  2634. if (check_for_locks(stp->st_vfs_file, sop))
  2635. goto out;
  2636. /* Note: so_perclient unused for lockowners,
  2637. * so it's OK to fool with here. */
  2638. list_add(&sop->so_perclient, &matches);
  2639. }
  2640. }
  2641. }
  2642. /* Clients probably won't expect us to return with some (but not all)
  2643. * of the lockowner state released; so don't release any until all
  2644. * have been checked. */
  2645. status = nfs_ok;
  2646. while (!list_empty(&matches)) {
  2647. sop = list_entry(matches.next, struct nfs4_stateowner,
  2648. so_perclient);
  2649. /* unhash_stateowner deletes so_perclient only
  2650. * for openowners. */
  2651. list_del(&sop->so_perclient);
  2652. release_stateowner(sop);
  2653. }
  2654. out:
  2655. nfs4_unlock_state();
  2656. return status;
  2657. }
  2658. static inline struct nfs4_client_reclaim *
  2659. alloc_reclaim(void)
  2660. {
  2661. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  2662. }
  2663. int
  2664. nfs4_has_reclaimed_state(const char *name)
  2665. {
  2666. unsigned int strhashval = clientstr_hashval(name);
  2667. struct nfs4_client *clp;
  2668. clp = find_confirmed_client_by_str(name, strhashval);
  2669. return clp ? 1 : 0;
  2670. }
  2671. /*
  2672. * failure => all reset bets are off, nfserr_no_grace...
  2673. */
  2674. int
  2675. nfs4_client_to_reclaim(const char *name)
  2676. {
  2677. unsigned int strhashval;
  2678. struct nfs4_client_reclaim *crp = NULL;
  2679. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  2680. crp = alloc_reclaim();
  2681. if (!crp)
  2682. return 0;
  2683. strhashval = clientstr_hashval(name);
  2684. INIT_LIST_HEAD(&crp->cr_strhash);
  2685. list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
  2686. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  2687. reclaim_str_hashtbl_size++;
  2688. return 1;
  2689. }
  2690. static void
  2691. nfs4_release_reclaim(void)
  2692. {
  2693. struct nfs4_client_reclaim *crp = NULL;
  2694. int i;
  2695. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  2696. while (!list_empty(&reclaim_str_hashtbl[i])) {
  2697. crp = list_entry(reclaim_str_hashtbl[i].next,
  2698. struct nfs4_client_reclaim, cr_strhash);
  2699. list_del(&crp->cr_strhash);
  2700. kfree(crp);
  2701. reclaim_str_hashtbl_size--;
  2702. }
  2703. }
  2704. BUG_ON(reclaim_str_hashtbl_size);
  2705. }
  2706. /*
  2707. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  2708. static struct nfs4_client_reclaim *
  2709. nfs4_find_reclaim_client(clientid_t *clid)
  2710. {
  2711. unsigned int strhashval;
  2712. struct nfs4_client *clp;
  2713. struct nfs4_client_reclaim *crp = NULL;
  2714. /* find clientid in conf_id_hashtbl */
  2715. clp = find_confirmed_client(clid);
  2716. if (clp == NULL)
  2717. return NULL;
  2718. dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
  2719. clp->cl_name.len, clp->cl_name.data,
  2720. clp->cl_recdir);
  2721. /* find clp->cl_name in reclaim_str_hashtbl */
  2722. strhashval = clientstr_hashval(clp->cl_recdir);
  2723. list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
  2724. if (same_name(crp->cr_recdir, clp->cl_recdir)) {
  2725. return crp;
  2726. }
  2727. }
  2728. return NULL;
  2729. }
  2730. /*
  2731. * Called from OPEN. Look for clientid in reclaim list.
  2732. */
  2733. __be32
  2734. nfs4_check_open_reclaim(clientid_t *clid)
  2735. {
  2736. return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
  2737. }
  2738. /* initialization to perform at module load time: */
  2739. int
  2740. nfs4_state_init(void)
  2741. {
  2742. int i, status;
  2743. status = nfsd4_init_slabs();
  2744. if (status)
  2745. return status;
  2746. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  2747. INIT_LIST_HEAD(&conf_id_hashtbl[i]);
  2748. INIT_LIST_HEAD(&conf_str_hashtbl[i]);
  2749. INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
  2750. INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
  2751. }
  2752. for (i = 0; i < FILE_HASH_SIZE; i++) {
  2753. INIT_LIST_HEAD(&file_hashtbl[i]);
  2754. }
  2755. for (i = 0; i < OWNER_HASH_SIZE; i++) {
  2756. INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
  2757. INIT_LIST_HEAD(&ownerid_hashtbl[i]);
  2758. }
  2759. for (i = 0; i < STATEID_HASH_SIZE; i++) {
  2760. INIT_LIST_HEAD(&stateid_hashtbl[i]);
  2761. INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
  2762. }
  2763. for (i = 0; i < LOCK_HASH_SIZE; i++) {
  2764. INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
  2765. INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
  2766. }
  2767. memset(&onestateid, ~0, sizeof(stateid_t));
  2768. INIT_LIST_HEAD(&close_lru);
  2769. INIT_LIST_HEAD(&client_lru);
  2770. INIT_LIST_HEAD(&del_recall_lru);
  2771. for (i = 0; i < CLIENT_HASH_SIZE; i++)
  2772. INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
  2773. reclaim_str_hashtbl_size = 0;
  2774. return 0;
  2775. }
  2776. static void
  2777. nfsd4_load_reboot_recovery_data(void)
  2778. {
  2779. int status;
  2780. nfs4_lock_state();
  2781. nfsd4_init_recdir(user_recovery_dirname);
  2782. status = nfsd4_recdir_load();
  2783. nfs4_unlock_state();
  2784. if (status)
  2785. printk("NFSD: Failure reading reboot recovery data\n");
  2786. }
  2787. unsigned long
  2788. get_nfs4_grace_period(void)
  2789. {
  2790. return max(user_lease_time, lease_time) * HZ;
  2791. }
  2792. /*
  2793. * Since the lifetime of a delegation isn't limited to that of an open, a
  2794. * client may quite reasonably hang on to a delegation as long as it has
  2795. * the inode cached. This becomes an obvious problem the first time a
  2796. * client's inode cache approaches the size of the server's total memory.
  2797. *
  2798. * For now we avoid this problem by imposing a hard limit on the number
  2799. * of delegations, which varies according to the server's memory size.
  2800. */
  2801. static void
  2802. set_max_delegations(void)
  2803. {
  2804. /*
  2805. * Allow at most 4 delegations per megabyte of RAM. Quick
  2806. * estimates suggest that in the worst case (where every delegation
  2807. * is for a different inode), a delegation could take about 1.5K,
  2808. * giving a worst case usage of about 6% of memory.
  2809. */
  2810. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  2811. }
  2812. /* initialization to perform when the nfsd service is started: */
  2813. static void
  2814. __nfs4_state_start(void)
  2815. {
  2816. unsigned long grace_time;
  2817. boot_time = get_seconds();
  2818. grace_time = get_nfs_grace_period();
  2819. lease_time = user_lease_time;
  2820. in_grace = 1;
  2821. printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
  2822. grace_time/HZ);
  2823. laundry_wq = create_singlethread_workqueue("nfsd4");
  2824. queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
  2825. set_max_delegations();
  2826. }
  2827. void
  2828. nfs4_state_start(void)
  2829. {
  2830. if (nfs4_init)
  2831. return;
  2832. nfsd4_load_reboot_recovery_data();
  2833. __nfs4_state_start();
  2834. nfs4_init = 1;
  2835. return;
  2836. }
  2837. int
  2838. nfs4_in_grace(void)
  2839. {
  2840. return in_grace;
  2841. }
  2842. time_t
  2843. nfs4_lease_time(void)
  2844. {
  2845. return lease_time;
  2846. }
  2847. static void
  2848. __nfs4_state_shutdown(void)
  2849. {
  2850. int i;
  2851. struct nfs4_client *clp = NULL;
  2852. struct nfs4_delegation *dp = NULL;
  2853. struct list_head *pos, *next, reaplist;
  2854. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  2855. while (!list_empty(&conf_id_hashtbl[i])) {
  2856. clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  2857. expire_client(clp);
  2858. }
  2859. while (!list_empty(&unconf_str_hashtbl[i])) {
  2860. clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
  2861. expire_client(clp);
  2862. }
  2863. }
  2864. INIT_LIST_HEAD(&reaplist);
  2865. spin_lock(&recall_lock);
  2866. list_for_each_safe(pos, next, &del_recall_lru) {
  2867. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2868. list_move(&dp->dl_recall_lru, &reaplist);
  2869. }
  2870. spin_unlock(&recall_lock);
  2871. list_for_each_safe(pos, next, &reaplist) {
  2872. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2873. list_del_init(&dp->dl_recall_lru);
  2874. unhash_delegation(dp);
  2875. }
  2876. nfsd4_shutdown_recdir();
  2877. nfs4_init = 0;
  2878. }
  2879. void
  2880. nfs4_state_shutdown(void)
  2881. {
  2882. cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
  2883. destroy_workqueue(laundry_wq);
  2884. nfs4_lock_state();
  2885. nfs4_release_reclaim();
  2886. __nfs4_state_shutdown();
  2887. nfs4_unlock_state();
  2888. }
  2889. static void
  2890. nfs4_set_recdir(char *recdir)
  2891. {
  2892. nfs4_lock_state();
  2893. strcpy(user_recovery_dirname, recdir);
  2894. nfs4_unlock_state();
  2895. }
  2896. /*
  2897. * Change the NFSv4 recovery directory to recdir.
  2898. */
  2899. int
  2900. nfs4_reset_recoverydir(char *recdir)
  2901. {
  2902. int status;
  2903. struct nameidata nd;
  2904. status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
  2905. if (status)
  2906. return status;
  2907. status = -ENOTDIR;
  2908. if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
  2909. nfs4_set_recdir(recdir);
  2910. status = 0;
  2911. }
  2912. path_release(&nd);
  2913. return status;
  2914. }
  2915. /*
  2916. * Called when leasetime is changed.
  2917. *
  2918. * The only way the protocol gives us to handle on-the-fly lease changes is to
  2919. * simulate a reboot. Instead of doing that, we just wait till the next time
  2920. * we start to register any changes in lease time. If the administrator
  2921. * really wants to change the lease time *now*, they can go ahead and bring
  2922. * nfsd down and then back up again after changing the lease time.
  2923. */
  2924. void
  2925. nfs4_reset_lease(time_t leasetime)
  2926. {
  2927. lock_kernel();
  2928. user_lease_time = leasetime;
  2929. unlock_kernel();
  2930. }