nfs4state.c 87 KB

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