nfs4state.c 87 KB

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