nfs4state.c 88 KB

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