nfs4state.c 88 KB

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