nfs4state.c 87 KB

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