nfs4state.c 88 KB

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