nfs4state.c 88 KB

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