nfs4state.c 88 KB

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