nfs4state.c 87 KB

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