nfs4state.c 87 KB

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