nfs4state.c 85 KB

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