nfs4state.c 88 KB

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