nfs4state.c 87 KB

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