nfs4state.c 85 KB

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