nfs4state.c 88 KB

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