nfs4state.c 87 KB

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