nfs4state.c 87 KB

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