nfs4state.c 87 KB

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