glock.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/delay.h>
  15. #include <linux/sort.h>
  16. #include <linux/jhash.h>
  17. #include <linux/kref.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <asm/semaphore.h>
  20. #include <asm/uaccess.h>
  21. #include "gfs2.h"
  22. #include "lm_interface.h"
  23. #include "incore.h"
  24. #include "glock.h"
  25. #include "glops.h"
  26. #include "inode.h"
  27. #include "lm.h"
  28. #include "lops.h"
  29. #include "meta_io.h"
  30. #include "quota.h"
  31. #include "super.h"
  32. #include "util.h"
  33. /* Must be kept in sync with the beginning of struct gfs2_glock */
  34. struct glock_plug {
  35. struct list_head gl_list;
  36. unsigned long gl_flags;
  37. };
  38. struct greedy {
  39. struct gfs2_holder gr_gh;
  40. struct work_struct gr_work;
  41. };
  42. typedef void (*glock_examiner) (struct gfs2_glock * gl);
  43. /**
  44. * relaxed_state_ok - is a requested lock compatible with the current lock mode?
  45. * @actual: the current state of the lock
  46. * @requested: the lock state that was requested by the caller
  47. * @flags: the modifier flags passed in by the caller
  48. *
  49. * Returns: 1 if the locks are compatible, 0 otherwise
  50. */
  51. static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
  52. int flags)
  53. {
  54. if (actual == requested)
  55. return 1;
  56. if (flags & GL_EXACT)
  57. return 0;
  58. if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
  59. return 1;
  60. if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
  61. return 1;
  62. return 0;
  63. }
  64. /**
  65. * gl_hash() - Turn glock number into hash bucket number
  66. * @lock: The glock number
  67. *
  68. * Returns: The number of the corresponding hash bucket
  69. */
  70. static unsigned int gl_hash(struct lm_lockname *name)
  71. {
  72. unsigned int h;
  73. h = jhash(&name->ln_number, sizeof(uint64_t), 0);
  74. h = jhash(&name->ln_type, sizeof(unsigned int), h);
  75. h &= GFS2_GL_HASH_MASK;
  76. return h;
  77. }
  78. /**
  79. * glock_free() - Perform a few checks and then release struct gfs2_glock
  80. * @gl: The glock to release
  81. *
  82. * Also calls lock module to release its internal structure for this glock.
  83. *
  84. */
  85. static void glock_free(struct gfs2_glock *gl)
  86. {
  87. struct gfs2_sbd *sdp = gl->gl_sbd;
  88. struct inode *aspace = gl->gl_aspace;
  89. gfs2_lm_put_lock(sdp, gl->gl_lock);
  90. if (aspace)
  91. gfs2_aspace_put(aspace);
  92. kmem_cache_free(gfs2_glock_cachep, gl);
  93. }
  94. /**
  95. * gfs2_glock_hold() - increment reference count on glock
  96. * @gl: The glock to hold
  97. *
  98. */
  99. void gfs2_glock_hold(struct gfs2_glock *gl)
  100. {
  101. kref_get(&gl->gl_ref);
  102. }
  103. /* All work is done after the return from kref_put() so we
  104. can release the write_lock before the free. */
  105. static void kill_glock(struct kref *kref)
  106. {
  107. struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
  108. struct gfs2_sbd *sdp = gl->gl_sbd;
  109. gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
  110. gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
  111. gfs2_assert(sdp, list_empty(&gl->gl_holders));
  112. gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
  113. gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
  114. gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
  115. }
  116. /**
  117. * gfs2_glock_put() - Decrement reference count on glock
  118. * @gl: The glock to put
  119. *
  120. */
  121. int gfs2_glock_put(struct gfs2_glock *gl)
  122. {
  123. struct gfs2_sbd *sdp = gl->gl_sbd;
  124. struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
  125. int rv = 0;
  126. mutex_lock(&sdp->sd_invalidate_inodes_mutex);
  127. write_lock(&bucket->hb_lock);
  128. if (kref_put(&gl->gl_ref, kill_glock)) {
  129. list_del_init(&gl->gl_list);
  130. write_unlock(&bucket->hb_lock);
  131. glock_free(gl);
  132. rv = 1;
  133. goto out;
  134. }
  135. write_unlock(&bucket->hb_lock);
  136. out:
  137. mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
  138. return rv;
  139. }
  140. /**
  141. * queue_empty - check to see if a glock's queue is empty
  142. * @gl: the glock
  143. * @head: the head of the queue to check
  144. *
  145. * This function protects the list in the event that a process already
  146. * has a holder on the list and is adding a second holder for itself.
  147. * The glmutex lock is what generally prevents processes from working
  148. * on the same glock at once, but the special case of adding a second
  149. * holder for yourself ("recursive" locking) doesn't involve locking
  150. * glmutex, making the spin lock necessary.
  151. *
  152. * Returns: 1 if the queue is empty
  153. */
  154. static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
  155. {
  156. int empty;
  157. spin_lock(&gl->gl_spin);
  158. empty = list_empty(head);
  159. spin_unlock(&gl->gl_spin);
  160. return empty;
  161. }
  162. /**
  163. * search_bucket() - Find struct gfs2_glock by lock number
  164. * @bucket: the bucket to search
  165. * @name: The lock name
  166. *
  167. * Returns: NULL, or the struct gfs2_glock with the requested number
  168. */
  169. static struct gfs2_glock *search_bucket(struct gfs2_gl_hash_bucket *bucket,
  170. struct lm_lockname *name)
  171. {
  172. struct gfs2_glock *gl;
  173. list_for_each_entry(gl, &bucket->hb_list, gl_list) {
  174. if (test_bit(GLF_PLUG, &gl->gl_flags))
  175. continue;
  176. if (!lm_name_equal(&gl->gl_name, name))
  177. continue;
  178. kref_get(&gl->gl_ref);
  179. return gl;
  180. }
  181. return NULL;
  182. }
  183. /**
  184. * gfs2_glock_find() - Find glock by lock number
  185. * @sdp: The GFS2 superblock
  186. * @name: The lock name
  187. *
  188. * Returns: NULL, or the struct gfs2_glock with the requested number
  189. */
  190. struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
  191. struct lm_lockname *name)
  192. {
  193. struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)];
  194. struct gfs2_glock *gl;
  195. read_lock(&bucket->hb_lock);
  196. gl = search_bucket(bucket, name);
  197. read_unlock(&bucket->hb_lock);
  198. return gl;
  199. }
  200. /**
  201. * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
  202. * @sdp: The GFS2 superblock
  203. * @number: the lock number
  204. * @glops: The glock_operations to use
  205. * @create: If 0, don't create the glock if it doesn't exist
  206. * @glp: the glock is returned here
  207. *
  208. * This does not lock a glock, just finds/creates structures for one.
  209. *
  210. * Returns: errno
  211. */
  212. int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number,
  213. struct gfs2_glock_operations *glops, int create,
  214. struct gfs2_glock **glp)
  215. {
  216. struct lm_lockname name;
  217. struct gfs2_glock *gl, *tmp;
  218. struct gfs2_gl_hash_bucket *bucket;
  219. int error;
  220. name.ln_number = number;
  221. name.ln_type = glops->go_type;
  222. bucket = &sdp->sd_gl_hash[gl_hash(&name)];
  223. read_lock(&bucket->hb_lock);
  224. gl = search_bucket(bucket, &name);
  225. read_unlock(&bucket->hb_lock);
  226. if (gl || !create) {
  227. *glp = gl;
  228. return 0;
  229. }
  230. gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
  231. if (!gl)
  232. return -ENOMEM;
  233. memset(gl, 0, sizeof(struct gfs2_glock));
  234. INIT_LIST_HEAD(&gl->gl_list);
  235. gl->gl_name = name;
  236. kref_init(&gl->gl_ref);
  237. spin_lock_init(&gl->gl_spin);
  238. gl->gl_state = LM_ST_UNLOCKED;
  239. INIT_LIST_HEAD(&gl->gl_holders);
  240. INIT_LIST_HEAD(&gl->gl_waiters1);
  241. INIT_LIST_HEAD(&gl->gl_waiters2);
  242. INIT_LIST_HEAD(&gl->gl_waiters3);
  243. gl->gl_ops = glops;
  244. gl->gl_bucket = bucket;
  245. INIT_LIST_HEAD(&gl->gl_reclaim);
  246. gl->gl_sbd = sdp;
  247. lops_init_le(&gl->gl_le, &gfs2_glock_lops);
  248. INIT_LIST_HEAD(&gl->gl_ail_list);
  249. /* If this glock protects actual on-disk data or metadata blocks,
  250. create a VFS inode to manage the pages/buffers holding them. */
  251. if (glops == &gfs2_inode_glops ||
  252. glops == &gfs2_rgrp_glops ||
  253. glops == &gfs2_meta_glops) {
  254. gl->gl_aspace = gfs2_aspace_get(sdp);
  255. if (!gl->gl_aspace) {
  256. error = -ENOMEM;
  257. goto fail;
  258. }
  259. }
  260. error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
  261. if (error)
  262. goto fail_aspace;
  263. write_lock(&bucket->hb_lock);
  264. tmp = search_bucket(bucket, &name);
  265. if (tmp) {
  266. write_unlock(&bucket->hb_lock);
  267. glock_free(gl);
  268. gl = tmp;
  269. } else {
  270. list_add_tail(&gl->gl_list, &bucket->hb_list);
  271. write_unlock(&bucket->hb_lock);
  272. }
  273. *glp = gl;
  274. return 0;
  275. fail_aspace:
  276. if (gl->gl_aspace)
  277. gfs2_aspace_put(gl->gl_aspace);
  278. fail:
  279. kmem_cache_free(gfs2_glock_cachep, gl);
  280. return error;
  281. }
  282. /**
  283. * gfs2_holder_init - initialize a struct gfs2_holder in the default way
  284. * @gl: the glock
  285. * @state: the state we're requesting
  286. * @flags: the modifier flags
  287. * @gh: the holder structure
  288. *
  289. */
  290. void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, int flags,
  291. struct gfs2_holder *gh)
  292. {
  293. INIT_LIST_HEAD(&gh->gh_list);
  294. gh->gh_gl = gl;
  295. gh->gh_owner = (flags & GL_NEVER_RECURSE) ? NULL : current;
  296. gh->gh_state = state;
  297. gh->gh_flags = flags;
  298. gh->gh_error = 0;
  299. gh->gh_iflags = 0;
  300. init_completion(&gh->gh_wait);
  301. if (gh->gh_state == LM_ST_EXCLUSIVE)
  302. gh->gh_flags |= GL_LOCAL_EXCL;
  303. gfs2_glock_hold(gl);
  304. }
  305. /**
  306. * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
  307. * @state: the state we're requesting
  308. * @flags: the modifier flags
  309. * @gh: the holder structure
  310. *
  311. * Don't mess with the glock.
  312. *
  313. */
  314. void gfs2_holder_reinit(unsigned int state, int flags, struct gfs2_holder *gh)
  315. {
  316. gh->gh_state = state;
  317. gh->gh_flags = flags;
  318. if (gh->gh_state == LM_ST_EXCLUSIVE)
  319. gh->gh_flags |= GL_LOCAL_EXCL;
  320. gh->gh_iflags &= 1 << HIF_ALLOCED;
  321. }
  322. /**
  323. * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
  324. * @gh: the holder structure
  325. *
  326. */
  327. void gfs2_holder_uninit(struct gfs2_holder *gh)
  328. {
  329. gfs2_glock_put(gh->gh_gl);
  330. gh->gh_gl = NULL;
  331. }
  332. /**
  333. * gfs2_holder_get - get a struct gfs2_holder structure
  334. * @gl: the glock
  335. * @state: the state we're requesting
  336. * @flags: the modifier flags
  337. * @gfp_flags: __GFP_NOFAIL
  338. *
  339. * Figure out how big an impact this function has. Either:
  340. * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
  341. * 2) Leave it like it is
  342. *
  343. * Returns: the holder structure, NULL on ENOMEM
  344. */
  345. struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl, unsigned int state,
  346. int flags, gfp_t gfp_flags)
  347. {
  348. struct gfs2_holder *gh;
  349. gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
  350. if (!gh)
  351. return NULL;
  352. gfs2_holder_init(gl, state, flags, gh);
  353. set_bit(HIF_ALLOCED, &gh->gh_iflags);
  354. return gh;
  355. }
  356. /**
  357. * gfs2_holder_put - get rid of a struct gfs2_holder structure
  358. * @gh: the holder structure
  359. *
  360. */
  361. void gfs2_holder_put(struct gfs2_holder *gh)
  362. {
  363. gfs2_holder_uninit(gh);
  364. kfree(gh);
  365. }
  366. /**
  367. * handle_recurse - put other holder structures (marked recursive)
  368. * into the holders list
  369. * @gh: the holder structure
  370. *
  371. */
  372. static void handle_recurse(struct gfs2_holder *gh)
  373. {
  374. struct gfs2_glock *gl = gh->gh_gl;
  375. struct gfs2_sbd *sdp = gl->gl_sbd;
  376. struct gfs2_holder *tmp_gh, *safe;
  377. int found = 0;
  378. if (gfs2_assert_warn(sdp, gh->gh_owner))
  379. return;
  380. list_for_each_entry_safe(tmp_gh, safe, &gl->gl_waiters3, gh_list) {
  381. if (tmp_gh->gh_owner != gh->gh_owner)
  382. continue;
  383. gfs2_assert_warn(sdp,
  384. test_bit(HIF_RECURSE, &tmp_gh->gh_iflags));
  385. list_move_tail(&tmp_gh->gh_list, &gl->gl_holders);
  386. tmp_gh->gh_error = 0;
  387. set_bit(HIF_HOLDER, &tmp_gh->gh_iflags);
  388. complete(&tmp_gh->gh_wait);
  389. found = 1;
  390. }
  391. gfs2_assert_warn(sdp, found);
  392. }
  393. /**
  394. * do_unrecurse - a recursive holder was just dropped of the waiters3 list
  395. * @gh: the holder
  396. *
  397. * If there is only one other recursive holder, clear its HIF_RECURSE bit.
  398. * If there is more than one, leave them alone.
  399. *
  400. */
  401. static void do_unrecurse(struct gfs2_holder *gh)
  402. {
  403. struct gfs2_glock *gl = gh->gh_gl;
  404. struct gfs2_sbd *sdp = gl->gl_sbd;
  405. struct gfs2_holder *tmp_gh, *last_gh = NULL;
  406. int found = 0;
  407. if (gfs2_assert_warn(sdp, gh->gh_owner))
  408. return;
  409. list_for_each_entry(tmp_gh, &gl->gl_waiters3, gh_list) {
  410. if (tmp_gh->gh_owner != gh->gh_owner)
  411. continue;
  412. gfs2_assert_warn(sdp,
  413. test_bit(HIF_RECURSE, &tmp_gh->gh_iflags));
  414. if (found)
  415. return;
  416. found = 1;
  417. last_gh = tmp_gh;
  418. }
  419. if (!gfs2_assert_warn(sdp, found))
  420. clear_bit(HIF_RECURSE, &last_gh->gh_iflags);
  421. }
  422. /**
  423. * rq_mutex - process a mutex request in the queue
  424. * @gh: the glock holder
  425. *
  426. * Returns: 1 if the queue is blocked
  427. */
  428. static int rq_mutex(struct gfs2_holder *gh)
  429. {
  430. struct gfs2_glock *gl = gh->gh_gl;
  431. list_del_init(&gh->gh_list);
  432. /* gh->gh_error never examined. */
  433. set_bit(GLF_LOCK, &gl->gl_flags);
  434. complete(&gh->gh_wait);
  435. return 1;
  436. }
  437. /**
  438. * rq_promote - process a promote request in the queue
  439. * @gh: the glock holder
  440. *
  441. * Acquire a new inter-node lock, or change a lock state to more restrictive.
  442. *
  443. * Returns: 1 if the queue is blocked
  444. */
  445. static int rq_promote(struct gfs2_holder *gh)
  446. {
  447. struct gfs2_glock *gl = gh->gh_gl;
  448. struct gfs2_sbd *sdp = gl->gl_sbd;
  449. struct gfs2_glock_operations *glops = gl->gl_ops;
  450. int recurse;
  451. if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
  452. if (list_empty(&gl->gl_holders)) {
  453. gl->gl_req_gh = gh;
  454. set_bit(GLF_LOCK, &gl->gl_flags);
  455. spin_unlock(&gl->gl_spin);
  456. if (atomic_read(&sdp->sd_reclaim_count) >
  457. gfs2_tune_get(sdp, gt_reclaim_limit) &&
  458. !(gh->gh_flags & LM_FLAG_PRIORITY)) {
  459. gfs2_reclaim_glock(sdp);
  460. gfs2_reclaim_glock(sdp);
  461. }
  462. glops->go_xmote_th(gl, gh->gh_state,
  463. gh->gh_flags);
  464. spin_lock(&gl->gl_spin);
  465. }
  466. return 1;
  467. }
  468. if (list_empty(&gl->gl_holders)) {
  469. set_bit(HIF_FIRST, &gh->gh_iflags);
  470. set_bit(GLF_LOCK, &gl->gl_flags);
  471. recurse = 0;
  472. } else {
  473. struct gfs2_holder *next_gh;
  474. if (gh->gh_flags & GL_LOCAL_EXCL)
  475. return 1;
  476. next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
  477. gh_list);
  478. if (next_gh->gh_flags & GL_LOCAL_EXCL)
  479. return 1;
  480. recurse = test_bit(HIF_RECURSE, &gh->gh_iflags);
  481. }
  482. list_move_tail(&gh->gh_list, &gl->gl_holders);
  483. gh->gh_error = 0;
  484. set_bit(HIF_HOLDER, &gh->gh_iflags);
  485. if (recurse)
  486. handle_recurse(gh);
  487. complete(&gh->gh_wait);
  488. return 0;
  489. }
  490. /**
  491. * rq_demote - process a demote request in the queue
  492. * @gh: the glock holder
  493. *
  494. * Returns: 1 if the queue is blocked
  495. */
  496. static int rq_demote(struct gfs2_holder *gh)
  497. {
  498. struct gfs2_glock *gl = gh->gh_gl;
  499. struct gfs2_glock_operations *glops = gl->gl_ops;
  500. if (!list_empty(&gl->gl_holders))
  501. return 1;
  502. if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
  503. list_del_init(&gh->gh_list);
  504. gh->gh_error = 0;
  505. spin_unlock(&gl->gl_spin);
  506. if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
  507. gfs2_holder_put(gh);
  508. else
  509. complete(&gh->gh_wait);
  510. spin_lock(&gl->gl_spin);
  511. } else {
  512. gl->gl_req_gh = gh;
  513. set_bit(GLF_LOCK, &gl->gl_flags);
  514. spin_unlock(&gl->gl_spin);
  515. if (gh->gh_state == LM_ST_UNLOCKED ||
  516. gl->gl_state != LM_ST_EXCLUSIVE)
  517. glops->go_drop_th(gl);
  518. else
  519. glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
  520. spin_lock(&gl->gl_spin);
  521. }
  522. return 0;
  523. }
  524. /**
  525. * rq_greedy - process a queued request to drop greedy status
  526. * @gh: the glock holder
  527. *
  528. * Returns: 1 if the queue is blocked
  529. */
  530. static int rq_greedy(struct gfs2_holder *gh)
  531. {
  532. struct gfs2_glock *gl = gh->gh_gl;
  533. list_del_init(&gh->gh_list);
  534. /* gh->gh_error never examined. */
  535. clear_bit(GLF_GREEDY, &gl->gl_flags);
  536. spin_unlock(&gl->gl_spin);
  537. gfs2_holder_uninit(gh);
  538. kfree(container_of(gh, struct greedy, gr_gh));
  539. spin_lock(&gl->gl_spin);
  540. return 0;
  541. }
  542. /**
  543. * run_queue - process holder structures on a glock
  544. * @gl: the glock
  545. *
  546. */
  547. static void run_queue(struct gfs2_glock *gl)
  548. {
  549. struct gfs2_holder *gh;
  550. int blocked = 1;
  551. for (;;) {
  552. if (test_bit(GLF_LOCK, &gl->gl_flags))
  553. break;
  554. if (!list_empty(&gl->gl_waiters1)) {
  555. gh = list_entry(gl->gl_waiters1.next,
  556. struct gfs2_holder, gh_list);
  557. if (test_bit(HIF_MUTEX, &gh->gh_iflags))
  558. blocked = rq_mutex(gh);
  559. else
  560. gfs2_assert_warn(gl->gl_sbd, 0);
  561. } else if (!list_empty(&gl->gl_waiters2) &&
  562. !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
  563. gh = list_entry(gl->gl_waiters2.next,
  564. struct gfs2_holder, gh_list);
  565. if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
  566. blocked = rq_demote(gh);
  567. else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
  568. blocked = rq_greedy(gh);
  569. else
  570. gfs2_assert_warn(gl->gl_sbd, 0);
  571. } else if (!list_empty(&gl->gl_waiters3)) {
  572. gh = list_entry(gl->gl_waiters3.next,
  573. struct gfs2_holder, gh_list);
  574. if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
  575. blocked = rq_promote(gh);
  576. else
  577. gfs2_assert_warn(gl->gl_sbd, 0);
  578. } else
  579. break;
  580. if (blocked)
  581. break;
  582. }
  583. }
  584. /**
  585. * gfs2_glmutex_lock - acquire a local lock on a glock
  586. * @gl: the glock
  587. *
  588. * Gives caller exclusive access to manipulate a glock structure.
  589. */
  590. void gfs2_glmutex_lock(struct gfs2_glock *gl)
  591. {
  592. struct gfs2_holder gh;
  593. gfs2_holder_init(gl, 0, 0, &gh);
  594. set_bit(HIF_MUTEX, &gh.gh_iflags);
  595. spin_lock(&gl->gl_spin);
  596. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
  597. list_add_tail(&gh.gh_list, &gl->gl_waiters1);
  598. else
  599. complete(&gh.gh_wait);
  600. spin_unlock(&gl->gl_spin);
  601. wait_for_completion(&gh.gh_wait);
  602. gfs2_holder_uninit(&gh);
  603. }
  604. /**
  605. * gfs2_glmutex_trylock - try to acquire a local lock on a glock
  606. * @gl: the glock
  607. *
  608. * Returns: 1 if the glock is acquired
  609. */
  610. int gfs2_glmutex_trylock(struct gfs2_glock *gl)
  611. {
  612. int acquired = 1;
  613. spin_lock(&gl->gl_spin);
  614. if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
  615. acquired = 0;
  616. spin_unlock(&gl->gl_spin);
  617. return acquired;
  618. }
  619. /**
  620. * gfs2_glmutex_unlock - release a local lock on a glock
  621. * @gl: the glock
  622. *
  623. */
  624. void gfs2_glmutex_unlock(struct gfs2_glock *gl)
  625. {
  626. spin_lock(&gl->gl_spin);
  627. clear_bit(GLF_LOCK, &gl->gl_flags);
  628. run_queue(gl);
  629. spin_unlock(&gl->gl_spin);
  630. }
  631. /**
  632. * handle_callback - add a demote request to a lock's queue
  633. * @gl: the glock
  634. * @state: the state the caller wants us to change to
  635. *
  636. */
  637. static void handle_callback(struct gfs2_glock *gl, unsigned int state)
  638. {
  639. struct gfs2_holder *gh, *new_gh = NULL;
  640. restart:
  641. spin_lock(&gl->gl_spin);
  642. list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
  643. if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
  644. gl->gl_req_gh != gh) {
  645. if (gh->gh_state != state)
  646. gh->gh_state = LM_ST_UNLOCKED;
  647. goto out;
  648. }
  649. }
  650. if (new_gh) {
  651. list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
  652. new_gh = NULL;
  653. } else {
  654. spin_unlock(&gl->gl_spin);
  655. new_gh = gfs2_holder_get(gl, state,
  656. LM_FLAG_TRY | GL_NEVER_RECURSE,
  657. GFP_KERNEL | __GFP_NOFAIL),
  658. set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
  659. set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
  660. goto restart;
  661. }
  662. out:
  663. spin_unlock(&gl->gl_spin);
  664. if (new_gh)
  665. gfs2_holder_put(new_gh);
  666. }
  667. /**
  668. * state_change - record that the glock is now in a different state
  669. * @gl: the glock
  670. * @new_state the new state
  671. *
  672. */
  673. static void state_change(struct gfs2_glock *gl, unsigned int new_state)
  674. {
  675. int held1, held2;
  676. held1 = (gl->gl_state != LM_ST_UNLOCKED);
  677. held2 = (new_state != LM_ST_UNLOCKED);
  678. if (held1 != held2) {
  679. if (held2)
  680. gfs2_glock_hold(gl);
  681. else
  682. gfs2_glock_put(gl);
  683. }
  684. gl->gl_state = new_state;
  685. }
  686. /**
  687. * xmote_bh - Called after the lock module is done acquiring a lock
  688. * @gl: The glock in question
  689. * @ret: the int returned from the lock module
  690. *
  691. */
  692. static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
  693. {
  694. struct gfs2_sbd *sdp = gl->gl_sbd;
  695. struct gfs2_glock_operations *glops = gl->gl_ops;
  696. struct gfs2_holder *gh = gl->gl_req_gh;
  697. int prev_state = gl->gl_state;
  698. int op_done = 1;
  699. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  700. gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
  701. gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
  702. state_change(gl, ret & LM_OUT_ST_MASK);
  703. if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
  704. if (glops->go_inval)
  705. glops->go_inval(gl, DIO_METADATA | DIO_DATA);
  706. } else if (gl->gl_state == LM_ST_DEFERRED) {
  707. /* We might not want to do this here.
  708. Look at moving to the inode glops. */
  709. if (glops->go_inval)
  710. glops->go_inval(gl, DIO_DATA);
  711. }
  712. /* Deal with each possible exit condition */
  713. if (!gh)
  714. gl->gl_stamp = jiffies;
  715. else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  716. spin_lock(&gl->gl_spin);
  717. list_del_init(&gh->gh_list);
  718. gh->gh_error = -EIO;
  719. if (test_bit(HIF_RECURSE, &gh->gh_iflags))
  720. do_unrecurse(gh);
  721. spin_unlock(&gl->gl_spin);
  722. } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
  723. spin_lock(&gl->gl_spin);
  724. list_del_init(&gh->gh_list);
  725. if (gl->gl_state == gh->gh_state ||
  726. gl->gl_state == LM_ST_UNLOCKED)
  727. gh->gh_error = 0;
  728. else {
  729. if (gfs2_assert_warn(sdp, gh->gh_flags &
  730. (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
  731. fs_warn(sdp, "ret = 0x%.8X\n", ret);
  732. gh->gh_error = GLR_TRYFAILED;
  733. }
  734. spin_unlock(&gl->gl_spin);
  735. if (ret & LM_OUT_CANCELED)
  736. handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
  737. } else if (ret & LM_OUT_CANCELED) {
  738. spin_lock(&gl->gl_spin);
  739. list_del_init(&gh->gh_list);
  740. gh->gh_error = GLR_CANCELED;
  741. if (test_bit(HIF_RECURSE, &gh->gh_iflags))
  742. do_unrecurse(gh);
  743. spin_unlock(&gl->gl_spin);
  744. } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
  745. spin_lock(&gl->gl_spin);
  746. list_move_tail(&gh->gh_list, &gl->gl_holders);
  747. gh->gh_error = 0;
  748. set_bit(HIF_HOLDER, &gh->gh_iflags);
  749. spin_unlock(&gl->gl_spin);
  750. set_bit(HIF_FIRST, &gh->gh_iflags);
  751. op_done = 0;
  752. } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  753. spin_lock(&gl->gl_spin);
  754. list_del_init(&gh->gh_list);
  755. gh->gh_error = GLR_TRYFAILED;
  756. if (test_bit(HIF_RECURSE, &gh->gh_iflags))
  757. do_unrecurse(gh);
  758. spin_unlock(&gl->gl_spin);
  759. } else {
  760. if (gfs2_assert_withdraw(sdp, 0) == -1)
  761. fs_err(sdp, "ret = 0x%.8X\n", ret);
  762. }
  763. if (glops->go_xmote_bh)
  764. glops->go_xmote_bh(gl);
  765. if (op_done) {
  766. spin_lock(&gl->gl_spin);
  767. gl->gl_req_gh = NULL;
  768. gl->gl_req_bh = NULL;
  769. clear_bit(GLF_LOCK, &gl->gl_flags);
  770. run_queue(gl);
  771. spin_unlock(&gl->gl_spin);
  772. }
  773. gfs2_glock_put(gl);
  774. if (gh) {
  775. if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
  776. gfs2_holder_put(gh);
  777. else
  778. complete(&gh->gh_wait);
  779. }
  780. }
  781. /**
  782. * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
  783. * @gl: The glock in question
  784. * @state: the requested state
  785. * @flags: modifier flags to the lock call
  786. *
  787. */
  788. void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
  789. {
  790. struct gfs2_sbd *sdp = gl->gl_sbd;
  791. struct gfs2_glock_operations *glops = gl->gl_ops;
  792. int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
  793. LM_FLAG_NOEXP | LM_FLAG_ANY |
  794. LM_FLAG_PRIORITY);
  795. unsigned int lck_ret;
  796. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  797. gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
  798. gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
  799. gfs2_assert_warn(sdp, state != gl->gl_state);
  800. if (gl->gl_state == LM_ST_EXCLUSIVE) {
  801. if (glops->go_sync)
  802. glops->go_sync(gl,
  803. DIO_METADATA | DIO_DATA | DIO_RELEASE);
  804. }
  805. gfs2_glock_hold(gl);
  806. gl->gl_req_bh = xmote_bh;
  807. lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
  808. lck_flags);
  809. if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
  810. return;
  811. if (lck_ret & LM_OUT_ASYNC)
  812. gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
  813. else
  814. xmote_bh(gl, lck_ret);
  815. }
  816. /**
  817. * drop_bh - Called after a lock module unlock completes
  818. * @gl: the glock
  819. * @ret: the return status
  820. *
  821. * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
  822. * Doesn't drop the reference on the glock the top half took out
  823. *
  824. */
  825. static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
  826. {
  827. struct gfs2_sbd *sdp = gl->gl_sbd;
  828. struct gfs2_glock_operations *glops = gl->gl_ops;
  829. struct gfs2_holder *gh = gl->gl_req_gh;
  830. clear_bit(GLF_PREFETCH, &gl->gl_flags);
  831. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  832. gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
  833. gfs2_assert_warn(sdp, !ret);
  834. state_change(gl, LM_ST_UNLOCKED);
  835. if (glops->go_inval)
  836. glops->go_inval(gl, DIO_METADATA | DIO_DATA);
  837. if (gh) {
  838. spin_lock(&gl->gl_spin);
  839. list_del_init(&gh->gh_list);
  840. gh->gh_error = 0;
  841. spin_unlock(&gl->gl_spin);
  842. }
  843. if (glops->go_drop_bh)
  844. glops->go_drop_bh(gl);
  845. spin_lock(&gl->gl_spin);
  846. gl->gl_req_gh = NULL;
  847. gl->gl_req_bh = NULL;
  848. clear_bit(GLF_LOCK, &gl->gl_flags);
  849. run_queue(gl);
  850. spin_unlock(&gl->gl_spin);
  851. gfs2_glock_put(gl);
  852. if (gh) {
  853. if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
  854. gfs2_holder_put(gh);
  855. else
  856. complete(&gh->gh_wait);
  857. }
  858. }
  859. /**
  860. * gfs2_glock_drop_th - call into the lock module to unlock a lock
  861. * @gl: the glock
  862. *
  863. */
  864. void gfs2_glock_drop_th(struct gfs2_glock *gl)
  865. {
  866. struct gfs2_sbd *sdp = gl->gl_sbd;
  867. struct gfs2_glock_operations *glops = gl->gl_ops;
  868. unsigned int ret;
  869. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  870. gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
  871. gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
  872. if (gl->gl_state == LM_ST_EXCLUSIVE) {
  873. if (glops->go_sync)
  874. glops->go_sync(gl,
  875. DIO_METADATA | DIO_DATA | DIO_RELEASE);
  876. }
  877. gfs2_glock_hold(gl);
  878. gl->gl_req_bh = drop_bh;
  879. ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
  880. if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
  881. return;
  882. if (!ret)
  883. drop_bh(gl, ret);
  884. else
  885. gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
  886. }
  887. /**
  888. * do_cancels - cancel requests for locks stuck waiting on an expire flag
  889. * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
  890. *
  891. * Don't cancel GL_NOCANCEL requests.
  892. */
  893. static void do_cancels(struct gfs2_holder *gh)
  894. {
  895. struct gfs2_glock *gl = gh->gh_gl;
  896. spin_lock(&gl->gl_spin);
  897. while (gl->gl_req_gh != gh &&
  898. !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
  899. !list_empty(&gh->gh_list)) {
  900. if (gl->gl_req_bh &&
  901. !(gl->gl_req_gh &&
  902. (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
  903. spin_unlock(&gl->gl_spin);
  904. gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
  905. msleep(100);
  906. spin_lock(&gl->gl_spin);
  907. } else {
  908. spin_unlock(&gl->gl_spin);
  909. msleep(100);
  910. spin_lock(&gl->gl_spin);
  911. }
  912. }
  913. spin_unlock(&gl->gl_spin);
  914. }
  915. /**
  916. * glock_wait_internal - wait on a glock acquisition
  917. * @gh: the glock holder
  918. *
  919. * Returns: 0 on success
  920. */
  921. static int glock_wait_internal(struct gfs2_holder *gh)
  922. {
  923. struct gfs2_glock *gl = gh->gh_gl;
  924. struct gfs2_sbd *sdp = gl->gl_sbd;
  925. struct gfs2_glock_operations *glops = gl->gl_ops;
  926. if (test_bit(HIF_ABORTED, &gh->gh_iflags))
  927. return -EIO;
  928. if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
  929. spin_lock(&gl->gl_spin);
  930. if (gl->gl_req_gh != gh &&
  931. !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
  932. !list_empty(&gh->gh_list)) {
  933. list_del_init(&gh->gh_list);
  934. gh->gh_error = GLR_TRYFAILED;
  935. if (test_bit(HIF_RECURSE, &gh->gh_iflags))
  936. do_unrecurse(gh);
  937. run_queue(gl);
  938. spin_unlock(&gl->gl_spin);
  939. return gh->gh_error;
  940. }
  941. spin_unlock(&gl->gl_spin);
  942. }
  943. if (gh->gh_flags & LM_FLAG_PRIORITY)
  944. do_cancels(gh);
  945. wait_for_completion(&gh->gh_wait);
  946. if (gh->gh_error)
  947. return gh->gh_error;
  948. gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
  949. gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
  950. gh->gh_state,
  951. gh->gh_flags));
  952. if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
  953. gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
  954. if (glops->go_lock) {
  955. gh->gh_error = glops->go_lock(gh);
  956. if (gh->gh_error) {
  957. spin_lock(&gl->gl_spin);
  958. list_del_init(&gh->gh_list);
  959. if (test_and_clear_bit(HIF_RECURSE,
  960. &gh->gh_iflags))
  961. do_unrecurse(gh);
  962. spin_unlock(&gl->gl_spin);
  963. }
  964. }
  965. spin_lock(&gl->gl_spin);
  966. gl->gl_req_gh = NULL;
  967. gl->gl_req_bh = NULL;
  968. clear_bit(GLF_LOCK, &gl->gl_flags);
  969. if (test_bit(HIF_RECURSE, &gh->gh_iflags))
  970. handle_recurse(gh);
  971. run_queue(gl);
  972. spin_unlock(&gl->gl_spin);
  973. }
  974. return gh->gh_error;
  975. }
  976. static inline struct gfs2_holder *
  977. find_holder_by_owner(struct list_head *head, struct task_struct *owner)
  978. {
  979. struct gfs2_holder *gh;
  980. list_for_each_entry(gh, head, gh_list) {
  981. if (gh->gh_owner == owner)
  982. return gh;
  983. }
  984. return NULL;
  985. }
  986. /**
  987. * recurse_check -
  988. *
  989. * Make sure the new holder is compatible with the pre-existing one.
  990. *
  991. */
  992. static int recurse_check(struct gfs2_holder *existing, struct gfs2_holder *new,
  993. unsigned int state)
  994. {
  995. struct gfs2_sbd *sdp = existing->gh_gl->gl_sbd;
  996. if (gfs2_assert_warn(sdp, (new->gh_flags & LM_FLAG_ANY) ||
  997. !(existing->gh_flags & LM_FLAG_ANY)))
  998. goto fail;
  999. if (gfs2_assert_warn(sdp, (existing->gh_flags & GL_LOCAL_EXCL) ||
  1000. !(new->gh_flags & GL_LOCAL_EXCL)))
  1001. goto fail;
  1002. if (gfs2_assert_warn(sdp, relaxed_state_ok(state, new->gh_state,
  1003. new->gh_flags)))
  1004. goto fail;
  1005. return 0;
  1006. fail:
  1007. set_bit(HIF_ABORTED, &new->gh_iflags);
  1008. return -EINVAL;
  1009. }
  1010. /**
  1011. * add_to_queue - Add a holder to the wait queue (but look for recursion)
  1012. * @gh: the holder structure to add
  1013. *
  1014. */
  1015. static void add_to_queue(struct gfs2_holder *gh)
  1016. {
  1017. struct gfs2_glock *gl = gh->gh_gl;
  1018. struct gfs2_holder *existing;
  1019. if (!gh->gh_owner)
  1020. goto out;
  1021. existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
  1022. if (existing) {
  1023. if (recurse_check(existing, gh, gl->gl_state))
  1024. return;
  1025. list_add_tail(&gh->gh_list, &gl->gl_holders);
  1026. set_bit(HIF_HOLDER, &gh->gh_iflags);
  1027. gh->gh_error = 0;
  1028. complete(&gh->gh_wait);
  1029. return;
  1030. }
  1031. existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
  1032. if (existing) {
  1033. if (recurse_check(existing, gh, existing->gh_state))
  1034. return;
  1035. set_bit(HIF_RECURSE, &gh->gh_iflags);
  1036. set_bit(HIF_RECURSE, &existing->gh_iflags);
  1037. list_add_tail(&gh->gh_list, &gl->gl_waiters3);
  1038. return;
  1039. }
  1040. out:
  1041. if (gh->gh_flags & LM_FLAG_PRIORITY)
  1042. list_add(&gh->gh_list, &gl->gl_waiters3);
  1043. else
  1044. list_add_tail(&gh->gh_list, &gl->gl_waiters3);
  1045. }
  1046. /**
  1047. * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
  1048. * @gh: the holder structure
  1049. *
  1050. * if (gh->gh_flags & GL_ASYNC), this never returns an error
  1051. *
  1052. * Returns: 0, GLR_TRYFAILED, or errno on failure
  1053. */
  1054. int gfs2_glock_nq(struct gfs2_holder *gh)
  1055. {
  1056. struct gfs2_glock *gl = gh->gh_gl;
  1057. struct gfs2_sbd *sdp = gl->gl_sbd;
  1058. int error = 0;
  1059. restart:
  1060. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
  1061. set_bit(HIF_ABORTED, &gh->gh_iflags);
  1062. return -EIO;
  1063. }
  1064. set_bit(HIF_PROMOTE, &gh->gh_iflags);
  1065. spin_lock(&gl->gl_spin);
  1066. add_to_queue(gh);
  1067. run_queue(gl);
  1068. spin_unlock(&gl->gl_spin);
  1069. if (!(gh->gh_flags & GL_ASYNC)) {
  1070. error = glock_wait_internal(gh);
  1071. if (error == GLR_CANCELED) {
  1072. msleep(1000);
  1073. goto restart;
  1074. }
  1075. }
  1076. clear_bit(GLF_PREFETCH, &gl->gl_flags);
  1077. return error;
  1078. }
  1079. /**
  1080. * gfs2_glock_poll - poll to see if an async request has been completed
  1081. * @gh: the holder
  1082. *
  1083. * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
  1084. */
  1085. int gfs2_glock_poll(struct gfs2_holder *gh)
  1086. {
  1087. struct gfs2_glock *gl = gh->gh_gl;
  1088. int ready = 0;
  1089. spin_lock(&gl->gl_spin);
  1090. if (test_bit(HIF_HOLDER, &gh->gh_iflags))
  1091. ready = 1;
  1092. else if (list_empty(&gh->gh_list)) {
  1093. if (gh->gh_error == GLR_CANCELED) {
  1094. spin_unlock(&gl->gl_spin);
  1095. msleep(1000);
  1096. if (gfs2_glock_nq(gh))
  1097. return 1;
  1098. return 0;
  1099. } else
  1100. ready = 1;
  1101. }
  1102. spin_unlock(&gl->gl_spin);
  1103. return ready;
  1104. }
  1105. /**
  1106. * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
  1107. * @gh: the holder structure
  1108. *
  1109. * Returns: 0, GLR_TRYFAILED, or errno on failure
  1110. */
  1111. int gfs2_glock_wait(struct gfs2_holder *gh)
  1112. {
  1113. int error;
  1114. error = glock_wait_internal(gh);
  1115. if (error == GLR_CANCELED) {
  1116. msleep(1000);
  1117. gh->gh_flags &= ~GL_ASYNC;
  1118. error = gfs2_glock_nq(gh);
  1119. }
  1120. return error;
  1121. }
  1122. /**
  1123. * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
  1124. * @gh: the glock holder
  1125. *
  1126. */
  1127. void gfs2_glock_dq(struct gfs2_holder *gh)
  1128. {
  1129. struct gfs2_glock *gl = gh->gh_gl;
  1130. struct gfs2_glock_operations *glops = gl->gl_ops;
  1131. if (gh->gh_flags & GL_SYNC)
  1132. set_bit(GLF_SYNC, &gl->gl_flags);
  1133. if (gh->gh_flags & GL_NOCACHE)
  1134. handle_callback(gl, LM_ST_UNLOCKED);
  1135. gfs2_glmutex_lock(gl);
  1136. spin_lock(&gl->gl_spin);
  1137. list_del_init(&gh->gh_list);
  1138. if (list_empty(&gl->gl_holders)) {
  1139. spin_unlock(&gl->gl_spin);
  1140. if (glops->go_unlock)
  1141. glops->go_unlock(gh);
  1142. if (test_bit(GLF_SYNC, &gl->gl_flags)) {
  1143. if (glops->go_sync)
  1144. glops->go_sync(gl, DIO_METADATA | DIO_DATA);
  1145. }
  1146. gl->gl_stamp = jiffies;
  1147. spin_lock(&gl->gl_spin);
  1148. }
  1149. clear_bit(GLF_LOCK, &gl->gl_flags);
  1150. run_queue(gl);
  1151. spin_unlock(&gl->gl_spin);
  1152. }
  1153. /**
  1154. * gfs2_glock_prefetch - Try to prefetch a glock
  1155. * @gl: the glock
  1156. * @state: the state to prefetch in
  1157. * @flags: flags passed to go_xmote_th()
  1158. *
  1159. */
  1160. void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state, int flags)
  1161. {
  1162. struct gfs2_glock_operations *glops = gl->gl_ops;
  1163. spin_lock(&gl->gl_spin);
  1164. if (test_bit(GLF_LOCK, &gl->gl_flags) ||
  1165. !list_empty(&gl->gl_holders) ||
  1166. !list_empty(&gl->gl_waiters1) ||
  1167. !list_empty(&gl->gl_waiters2) ||
  1168. !list_empty(&gl->gl_waiters3) ||
  1169. relaxed_state_ok(gl->gl_state, state, flags)) {
  1170. spin_unlock(&gl->gl_spin);
  1171. return;
  1172. }
  1173. set_bit(GLF_PREFETCH, &gl->gl_flags);
  1174. set_bit(GLF_LOCK, &gl->gl_flags);
  1175. spin_unlock(&gl->gl_spin);
  1176. glops->go_xmote_th(gl, state, flags);
  1177. }
  1178. /**
  1179. * gfs2_glock_force_drop - Force a glock to be uncached
  1180. * @gl: the glock
  1181. *
  1182. */
  1183. void gfs2_glock_force_drop(struct gfs2_glock *gl)
  1184. {
  1185. struct gfs2_holder gh;
  1186. gfs2_holder_init(gl, LM_ST_UNLOCKED, GL_NEVER_RECURSE, &gh);
  1187. set_bit(HIF_DEMOTE, &gh.gh_iflags);
  1188. spin_lock(&gl->gl_spin);
  1189. list_add_tail(&gh.gh_list, &gl->gl_waiters2);
  1190. run_queue(gl);
  1191. spin_unlock(&gl->gl_spin);
  1192. wait_for_completion(&gh.gh_wait);
  1193. gfs2_holder_uninit(&gh);
  1194. }
  1195. static void greedy_work(void *data)
  1196. {
  1197. struct greedy *gr = (struct greedy *)data;
  1198. struct gfs2_holder *gh = &gr->gr_gh;
  1199. struct gfs2_glock *gl = gh->gh_gl;
  1200. struct gfs2_glock_operations *glops = gl->gl_ops;
  1201. clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
  1202. if (glops->go_greedy)
  1203. glops->go_greedy(gl);
  1204. spin_lock(&gl->gl_spin);
  1205. if (list_empty(&gl->gl_waiters2)) {
  1206. clear_bit(GLF_GREEDY, &gl->gl_flags);
  1207. spin_unlock(&gl->gl_spin);
  1208. gfs2_holder_uninit(gh);
  1209. kfree(gr);
  1210. } else {
  1211. gfs2_glock_hold(gl);
  1212. list_add_tail(&gh->gh_list, &gl->gl_waiters2);
  1213. run_queue(gl);
  1214. spin_unlock(&gl->gl_spin);
  1215. gfs2_glock_put(gl);
  1216. }
  1217. }
  1218. /**
  1219. * gfs2_glock_be_greedy -
  1220. * @gl:
  1221. * @time:
  1222. *
  1223. * Returns: 0 if go_greedy will be called, 1 otherwise
  1224. */
  1225. int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
  1226. {
  1227. struct greedy *gr;
  1228. struct gfs2_holder *gh;
  1229. if (!time ||
  1230. gl->gl_sbd->sd_args.ar_localcaching ||
  1231. test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
  1232. return 1;
  1233. gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
  1234. if (!gr) {
  1235. clear_bit(GLF_GREEDY, &gl->gl_flags);
  1236. return 1;
  1237. }
  1238. gh = &gr->gr_gh;
  1239. gfs2_holder_init(gl, 0, GL_NEVER_RECURSE, gh);
  1240. set_bit(HIF_GREEDY, &gh->gh_iflags);
  1241. INIT_WORK(&gr->gr_work, greedy_work, gr);
  1242. set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
  1243. schedule_delayed_work(&gr->gr_work, time);
  1244. return 0;
  1245. }
  1246. /**
  1247. * gfs2_glock_nq_init - intialize a holder and enqueue it on a glock
  1248. * @gl: the glock
  1249. * @state: the state we're requesting
  1250. * @flags: the modifier flags
  1251. * @gh: the holder structure
  1252. *
  1253. * Returns: 0, GLR_*, or errno
  1254. */
  1255. int gfs2_glock_nq_init(struct gfs2_glock *gl, unsigned int state, int flags,
  1256. struct gfs2_holder *gh)
  1257. {
  1258. int error;
  1259. gfs2_holder_init(gl, state, flags, gh);
  1260. error = gfs2_glock_nq(gh);
  1261. if (error)
  1262. gfs2_holder_uninit(gh);
  1263. return error;
  1264. }
  1265. /**
  1266. * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
  1267. * @gh: the holder structure
  1268. *
  1269. */
  1270. void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
  1271. {
  1272. gfs2_glock_dq(gh);
  1273. gfs2_holder_uninit(gh);
  1274. }
  1275. /**
  1276. * gfs2_glock_nq_num - acquire a glock based on lock number
  1277. * @sdp: the filesystem
  1278. * @number: the lock number
  1279. * @glops: the glock operations for the type of glock
  1280. * @state: the state to acquire the glock in
  1281. * @flags: modifier flags for the aquisition
  1282. * @gh: the struct gfs2_holder
  1283. *
  1284. * Returns: errno
  1285. */
  1286. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
  1287. struct gfs2_glock_operations *glops, unsigned int state,
  1288. int flags, struct gfs2_holder *gh)
  1289. {
  1290. struct gfs2_glock *gl;
  1291. int error;
  1292. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1293. if (!error) {
  1294. error = gfs2_glock_nq_init(gl, state, flags, gh);
  1295. gfs2_glock_put(gl);
  1296. }
  1297. return error;
  1298. }
  1299. /**
  1300. * glock_compare - Compare two struct gfs2_glock structures for sorting
  1301. * @arg_a: the first structure
  1302. * @arg_b: the second structure
  1303. *
  1304. */
  1305. static int glock_compare(const void *arg_a, const void *arg_b)
  1306. {
  1307. struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
  1308. struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
  1309. struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1310. struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1311. int ret = 0;
  1312. if (a->ln_number > b->ln_number)
  1313. ret = 1;
  1314. else if (a->ln_number < b->ln_number)
  1315. ret = -1;
  1316. else {
  1317. if (gh_a->gh_state == LM_ST_SHARED &&
  1318. gh_b->gh_state == LM_ST_EXCLUSIVE)
  1319. ret = 1;
  1320. else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
  1321. (gh_b->gh_flags & GL_LOCAL_EXCL))
  1322. ret = 1;
  1323. }
  1324. return ret;
  1325. }
  1326. /**
  1327. * nq_m_sync - synchonously acquire more than one glock in deadlock free order
  1328. * @num_gh: the number of structures
  1329. * @ghs: an array of struct gfs2_holder structures
  1330. *
  1331. * Returns: 0 on success (all glocks acquired),
  1332. * errno on failure (no glocks acquired)
  1333. */
  1334. static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
  1335. struct gfs2_holder **p)
  1336. {
  1337. unsigned int x;
  1338. int error = 0;
  1339. for (x = 0; x < num_gh; x++)
  1340. p[x] = &ghs[x];
  1341. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
  1342. for (x = 0; x < num_gh; x++) {
  1343. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1344. error = gfs2_glock_nq(p[x]);
  1345. if (error) {
  1346. while (x--)
  1347. gfs2_glock_dq(p[x]);
  1348. break;
  1349. }
  1350. }
  1351. return error;
  1352. }
  1353. /**
  1354. * gfs2_glock_nq_m - acquire multiple glocks
  1355. * @num_gh: the number of structures
  1356. * @ghs: an array of struct gfs2_holder structures
  1357. *
  1358. * Figure out how big an impact this function has. Either:
  1359. * 1) Replace this code with code that calls gfs2_glock_prefetch()
  1360. * 2) Forget async stuff and just call nq_m_sync()
  1361. * 3) Leave it like it is
  1362. *
  1363. * Returns: 0 on success (all glocks acquired),
  1364. * errno on failure (no glocks acquired)
  1365. */
  1366. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1367. {
  1368. int *e;
  1369. unsigned int x;
  1370. int borked = 0, serious = 0;
  1371. int error = 0;
  1372. if (!num_gh)
  1373. return 0;
  1374. if (num_gh == 1) {
  1375. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1376. return gfs2_glock_nq(ghs);
  1377. }
  1378. e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
  1379. if (!e)
  1380. return -ENOMEM;
  1381. for (x = 0; x < num_gh; x++) {
  1382. ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
  1383. error = gfs2_glock_nq(&ghs[x]);
  1384. if (error) {
  1385. borked = 1;
  1386. serious = error;
  1387. num_gh = x;
  1388. break;
  1389. }
  1390. }
  1391. for (x = 0; x < num_gh; x++) {
  1392. error = e[x] = glock_wait_internal(&ghs[x]);
  1393. if (error) {
  1394. borked = 1;
  1395. if (error != GLR_TRYFAILED && error != GLR_CANCELED)
  1396. serious = error;
  1397. }
  1398. }
  1399. if (!borked) {
  1400. kfree(e);
  1401. return 0;
  1402. }
  1403. for (x = 0; x < num_gh; x++)
  1404. if (!e[x])
  1405. gfs2_glock_dq(&ghs[x]);
  1406. if (serious)
  1407. error = serious;
  1408. else {
  1409. for (x = 0; x < num_gh; x++)
  1410. gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
  1411. &ghs[x]);
  1412. error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
  1413. }
  1414. kfree(e);
  1415. return error;
  1416. }
  1417. /**
  1418. * gfs2_glock_dq_m - release multiple glocks
  1419. * @num_gh: the number of structures
  1420. * @ghs: an array of struct gfs2_holder structures
  1421. *
  1422. */
  1423. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1424. {
  1425. unsigned int x;
  1426. for (x = 0; x < num_gh; x++)
  1427. gfs2_glock_dq(&ghs[x]);
  1428. }
  1429. /**
  1430. * gfs2_glock_dq_uninit_m - release multiple glocks
  1431. * @num_gh: the number of structures
  1432. * @ghs: an array of struct gfs2_holder structures
  1433. *
  1434. */
  1435. void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
  1436. {
  1437. unsigned int x;
  1438. for (x = 0; x < num_gh; x++)
  1439. gfs2_glock_dq_uninit(&ghs[x]);
  1440. }
  1441. /**
  1442. * gfs2_glock_prefetch_num - prefetch a glock based on lock number
  1443. * @sdp: the filesystem
  1444. * @number: the lock number
  1445. * @glops: the glock operations for the type of glock
  1446. * @state: the state to acquire the glock in
  1447. * @flags: modifier flags for the aquisition
  1448. *
  1449. * Returns: errno
  1450. */
  1451. void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
  1452. struct gfs2_glock_operations *glops,
  1453. unsigned int state, int flags)
  1454. {
  1455. struct gfs2_glock *gl;
  1456. int error;
  1457. if (atomic_read(&sdp->sd_reclaim_count) <
  1458. gfs2_tune_get(sdp, gt_reclaim_limit)) {
  1459. error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
  1460. if (!error) {
  1461. gfs2_glock_prefetch(gl, state, flags);
  1462. gfs2_glock_put(gl);
  1463. }
  1464. }
  1465. }
  1466. /**
  1467. * gfs2_lvb_hold - attach a LVB from a glock
  1468. * @gl: The glock in question
  1469. *
  1470. */
  1471. int gfs2_lvb_hold(struct gfs2_glock *gl)
  1472. {
  1473. int error;
  1474. gfs2_glmutex_lock(gl);
  1475. if (!atomic_read(&gl->gl_lvb_count)) {
  1476. error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
  1477. if (error) {
  1478. gfs2_glmutex_unlock(gl);
  1479. return error;
  1480. }
  1481. gfs2_glock_hold(gl);
  1482. }
  1483. atomic_inc(&gl->gl_lvb_count);
  1484. gfs2_glmutex_unlock(gl);
  1485. return 0;
  1486. }
  1487. /**
  1488. * gfs2_lvb_unhold - detach a LVB from a glock
  1489. * @gl: The glock in question
  1490. *
  1491. */
  1492. void gfs2_lvb_unhold(struct gfs2_glock *gl)
  1493. {
  1494. gfs2_glock_hold(gl);
  1495. gfs2_glmutex_lock(gl);
  1496. gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
  1497. if (atomic_dec_and_test(&gl->gl_lvb_count)) {
  1498. gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
  1499. gl->gl_lvb = NULL;
  1500. gfs2_glock_put(gl);
  1501. }
  1502. gfs2_glmutex_unlock(gl);
  1503. gfs2_glock_put(gl);
  1504. }
  1505. void gfs2_lvb_sync(struct gfs2_glock *gl)
  1506. {
  1507. gfs2_glmutex_lock(gl);
  1508. gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
  1509. if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
  1510. gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
  1511. gfs2_glmutex_unlock(gl);
  1512. }
  1513. static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
  1514. unsigned int state)
  1515. {
  1516. struct gfs2_glock *gl;
  1517. gl = gfs2_glock_find(sdp, name);
  1518. if (!gl)
  1519. return;
  1520. if (gl->gl_ops->go_callback)
  1521. gl->gl_ops->go_callback(gl, state);
  1522. handle_callback(gl, state);
  1523. spin_lock(&gl->gl_spin);
  1524. run_queue(gl);
  1525. spin_unlock(&gl->gl_spin);
  1526. gfs2_glock_put(gl);
  1527. }
  1528. /**
  1529. * gfs2_glock_cb - Callback used by locking module
  1530. * @fsdata: Pointer to the superblock
  1531. * @type: Type of callback
  1532. * @data: Type dependent data pointer
  1533. *
  1534. * Called by the locking module when it wants to tell us something.
  1535. * Either we need to drop a lock, one of our ASYNC requests completed, or
  1536. * a journal from another client needs to be recovered.
  1537. */
  1538. void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
  1539. {
  1540. struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
  1541. switch (type) {
  1542. case LM_CB_NEED_E:
  1543. blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_UNLOCKED);
  1544. return;
  1545. case LM_CB_NEED_D:
  1546. blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_DEFERRED);
  1547. return;
  1548. case LM_CB_NEED_S:
  1549. blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_SHARED);
  1550. return;
  1551. case LM_CB_ASYNC: {
  1552. struct lm_async_cb *async = (struct lm_async_cb *)data;
  1553. struct gfs2_glock *gl;
  1554. gl = gfs2_glock_find(sdp, &async->lc_name);
  1555. if (gfs2_assert_warn(sdp, gl))
  1556. return;
  1557. if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
  1558. gl->gl_req_bh(gl, async->lc_ret);
  1559. gfs2_glock_put(gl);
  1560. return;
  1561. }
  1562. case LM_CB_NEED_RECOVERY:
  1563. gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
  1564. if (sdp->sd_recoverd_process)
  1565. wake_up_process(sdp->sd_recoverd_process);
  1566. return;
  1567. case LM_CB_DROPLOCKS:
  1568. gfs2_gl_hash_clear(sdp, NO_WAIT);
  1569. gfs2_quota_scan(sdp);
  1570. return;
  1571. default:
  1572. gfs2_assert_warn(sdp, 0);
  1573. return;
  1574. }
  1575. }
  1576. /**
  1577. * gfs2_try_toss_inode - try to remove a particular inode struct from cache
  1578. * sdp: the filesystem
  1579. * inum: the inode number
  1580. *
  1581. */
  1582. void gfs2_try_toss_inode(struct gfs2_sbd *sdp, struct gfs2_inum *inum)
  1583. {
  1584. struct gfs2_glock *gl;
  1585. struct gfs2_inode *ip;
  1586. int error;
  1587. error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops,
  1588. NO_CREATE, &gl);
  1589. if (error || !gl)
  1590. return;
  1591. if (!gfs2_glmutex_trylock(gl))
  1592. goto out;
  1593. ip = gl->gl_object;
  1594. if (!ip)
  1595. goto out_unlock;
  1596. if (atomic_read(&ip->i_count))
  1597. goto out_unlock;
  1598. gfs2_inode_destroy(ip);
  1599. out_unlock:
  1600. gfs2_glmutex_unlock(gl);
  1601. out:
  1602. gfs2_glock_put(gl);
  1603. }
  1604. /**
  1605. * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
  1606. * iopen glock from memory
  1607. * @io_gl: the iopen glock
  1608. * @state: the state into which the glock should be put
  1609. *
  1610. */
  1611. void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
  1612. {
  1613. struct gfs2_glock *i_gl;
  1614. if (state != LM_ST_UNLOCKED)
  1615. return;
  1616. spin_lock(&io_gl->gl_spin);
  1617. i_gl = io_gl->gl_object;
  1618. if (i_gl) {
  1619. gfs2_glock_hold(i_gl);
  1620. spin_unlock(&io_gl->gl_spin);
  1621. } else {
  1622. spin_unlock(&io_gl->gl_spin);
  1623. return;
  1624. }
  1625. if (gfs2_glmutex_trylock(i_gl)) {
  1626. struct gfs2_inode *ip = i_gl->gl_object;
  1627. if (ip) {
  1628. gfs2_try_toss_vnode(ip);
  1629. gfs2_glmutex_unlock(i_gl);
  1630. gfs2_glock_schedule_for_reclaim(i_gl);
  1631. goto out;
  1632. }
  1633. gfs2_glmutex_unlock(i_gl);
  1634. }
  1635. out:
  1636. gfs2_glock_put(i_gl);
  1637. }
  1638. /**
  1639. * demote_ok - Check to see if it's ok to unlock a glock
  1640. * @gl: the glock
  1641. *
  1642. * Returns: 1 if it's ok
  1643. */
  1644. static int demote_ok(struct gfs2_glock *gl)
  1645. {
  1646. struct gfs2_sbd *sdp = gl->gl_sbd;
  1647. struct gfs2_glock_operations *glops = gl->gl_ops;
  1648. int demote = 1;
  1649. if (test_bit(GLF_STICKY, &gl->gl_flags))
  1650. demote = 0;
  1651. else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
  1652. demote = time_after_eq(jiffies,
  1653. gl->gl_stamp +
  1654. gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
  1655. else if (glops->go_demote_ok)
  1656. demote = glops->go_demote_ok(gl);
  1657. return demote;
  1658. }
  1659. /**
  1660. * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
  1661. * @gl: the glock
  1662. *
  1663. */
  1664. void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
  1665. {
  1666. struct gfs2_sbd *sdp = gl->gl_sbd;
  1667. spin_lock(&sdp->sd_reclaim_lock);
  1668. if (list_empty(&gl->gl_reclaim)) {
  1669. gfs2_glock_hold(gl);
  1670. list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
  1671. atomic_inc(&sdp->sd_reclaim_count);
  1672. }
  1673. spin_unlock(&sdp->sd_reclaim_lock);
  1674. wake_up(&sdp->sd_reclaim_wq);
  1675. }
  1676. /**
  1677. * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
  1678. * @sdp: the filesystem
  1679. *
  1680. * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
  1681. * different glock and we notice that there are a lot of glocks in the
  1682. * reclaim list.
  1683. *
  1684. */
  1685. void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
  1686. {
  1687. struct gfs2_glock *gl;
  1688. spin_lock(&sdp->sd_reclaim_lock);
  1689. if (list_empty(&sdp->sd_reclaim_list)) {
  1690. spin_unlock(&sdp->sd_reclaim_lock);
  1691. return;
  1692. }
  1693. gl = list_entry(sdp->sd_reclaim_list.next,
  1694. struct gfs2_glock, gl_reclaim);
  1695. list_del_init(&gl->gl_reclaim);
  1696. spin_unlock(&sdp->sd_reclaim_lock);
  1697. atomic_dec(&sdp->sd_reclaim_count);
  1698. atomic_inc(&sdp->sd_reclaimed);
  1699. if (gfs2_glmutex_trylock(gl)) {
  1700. if (gl->gl_ops == &gfs2_inode_glops) {
  1701. struct gfs2_inode *ip = gl->gl_object;
  1702. if (ip && !atomic_read(&ip->i_count))
  1703. gfs2_inode_destroy(ip);
  1704. }
  1705. if (queue_empty(gl, &gl->gl_holders) &&
  1706. gl->gl_state != LM_ST_UNLOCKED &&
  1707. demote_ok(gl))
  1708. handle_callback(gl, LM_ST_UNLOCKED);
  1709. gfs2_glmutex_unlock(gl);
  1710. }
  1711. gfs2_glock_put(gl);
  1712. }
  1713. /**
  1714. * examine_bucket - Call a function for glock in a hash bucket
  1715. * @examiner: the function
  1716. * @sdp: the filesystem
  1717. * @bucket: the bucket
  1718. *
  1719. * Returns: 1 if the bucket has entries
  1720. */
  1721. static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
  1722. struct gfs2_gl_hash_bucket *bucket)
  1723. {
  1724. struct glock_plug plug;
  1725. struct list_head *tmp;
  1726. struct gfs2_glock *gl;
  1727. int entries;
  1728. /* Add "plug" to end of bucket list, work back up list from there */
  1729. memset(&plug.gl_flags, 0, sizeof(unsigned long));
  1730. set_bit(GLF_PLUG, &plug.gl_flags);
  1731. write_lock(&bucket->hb_lock);
  1732. list_add(&plug.gl_list, &bucket->hb_list);
  1733. write_unlock(&bucket->hb_lock);
  1734. for (;;) {
  1735. write_lock(&bucket->hb_lock);
  1736. for (;;) {
  1737. tmp = plug.gl_list.next;
  1738. if (tmp == &bucket->hb_list) {
  1739. list_del(&plug.gl_list);
  1740. entries = !list_empty(&bucket->hb_list);
  1741. write_unlock(&bucket->hb_lock);
  1742. return entries;
  1743. }
  1744. gl = list_entry(tmp, struct gfs2_glock, gl_list);
  1745. /* Move plug up list */
  1746. list_move(&plug.gl_list, &gl->gl_list);
  1747. if (test_bit(GLF_PLUG, &gl->gl_flags))
  1748. continue;
  1749. /* examiner() must glock_put() */
  1750. gfs2_glock_hold(gl);
  1751. break;
  1752. }
  1753. write_unlock(&bucket->hb_lock);
  1754. examiner(gl);
  1755. }
  1756. }
  1757. /**
  1758. * scan_glock - look at a glock and see if we can reclaim it
  1759. * @gl: the glock to look at
  1760. *
  1761. */
  1762. static void scan_glock(struct gfs2_glock *gl)
  1763. {
  1764. if (gfs2_glmutex_trylock(gl)) {
  1765. if (gl->gl_ops == &gfs2_inode_glops) {
  1766. struct gfs2_inode *ip = gl->gl_object;
  1767. if (ip && !atomic_read(&ip->i_count))
  1768. goto out_schedule;
  1769. }
  1770. if (queue_empty(gl, &gl->gl_holders) &&
  1771. gl->gl_state != LM_ST_UNLOCKED &&
  1772. demote_ok(gl))
  1773. goto out_schedule;
  1774. gfs2_glmutex_unlock(gl);
  1775. }
  1776. gfs2_glock_put(gl);
  1777. return;
  1778. out_schedule:
  1779. gfs2_glmutex_unlock(gl);
  1780. gfs2_glock_schedule_for_reclaim(gl);
  1781. gfs2_glock_put(gl);
  1782. }
  1783. /**
  1784. * gfs2_scand_internal - Look for glocks and inodes to toss from memory
  1785. * @sdp: the filesystem
  1786. *
  1787. */
  1788. void gfs2_scand_internal(struct gfs2_sbd *sdp)
  1789. {
  1790. unsigned int x;
  1791. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  1792. examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
  1793. cond_resched();
  1794. }
  1795. }
  1796. /**
  1797. * clear_glock - look at a glock and see if we can free it from glock cache
  1798. * @gl: the glock to look at
  1799. *
  1800. */
  1801. static void clear_glock(struct gfs2_glock *gl)
  1802. {
  1803. struct gfs2_sbd *sdp = gl->gl_sbd;
  1804. int released;
  1805. spin_lock(&sdp->sd_reclaim_lock);
  1806. if (!list_empty(&gl->gl_reclaim)) {
  1807. list_del_init(&gl->gl_reclaim);
  1808. atomic_dec(&sdp->sd_reclaim_count);
  1809. released = gfs2_glock_put(gl);
  1810. gfs2_assert(sdp, !released);
  1811. }
  1812. spin_unlock(&sdp->sd_reclaim_lock);
  1813. if (gfs2_glmutex_trylock(gl)) {
  1814. if (gl->gl_ops == &gfs2_inode_glops) {
  1815. struct gfs2_inode *ip = gl->gl_object;
  1816. if (ip && !atomic_read(&ip->i_count))
  1817. gfs2_inode_destroy(ip);
  1818. }
  1819. if (queue_empty(gl, &gl->gl_holders) &&
  1820. gl->gl_state != LM_ST_UNLOCKED)
  1821. handle_callback(gl, LM_ST_UNLOCKED);
  1822. gfs2_glmutex_unlock(gl);
  1823. }
  1824. gfs2_glock_put(gl);
  1825. }
  1826. /**
  1827. * gfs2_gl_hash_clear - Empty out the glock hash table
  1828. * @sdp: the filesystem
  1829. * @wait: wait until it's all gone
  1830. *
  1831. * Called when unmounting the filesystem, or when inter-node lock manager
  1832. * requests DROPLOCKS because it is running out of capacity.
  1833. */
  1834. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
  1835. {
  1836. unsigned long t;
  1837. unsigned int x;
  1838. int cont;
  1839. t = jiffies;
  1840. for (;;) {
  1841. cont = 0;
  1842. for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
  1843. if (examine_bucket(clear_glock, sdp,
  1844. &sdp->sd_gl_hash[x]))
  1845. cont = 1;
  1846. if (!wait || !cont)
  1847. break;
  1848. if (time_after_eq(jiffies,
  1849. t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
  1850. fs_warn(sdp, "Unmount seems to be stalled. "
  1851. "Dumping lock state...\n");
  1852. gfs2_dump_lockstate(sdp);
  1853. t = jiffies;
  1854. }
  1855. /* invalidate_inodes() requires that the sb inodes list
  1856. not change, but an async completion callback for an
  1857. unlock can occur which does glock_put() which
  1858. can call iput() which will change the sb inodes list.
  1859. invalidate_inodes_mutex prevents glock_put()'s during
  1860. an invalidate_inodes() */
  1861. mutex_lock(&sdp->sd_invalidate_inodes_mutex);
  1862. invalidate_inodes(sdp->sd_vfs);
  1863. mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
  1864. yield();
  1865. }
  1866. }
  1867. /*
  1868. * Diagnostic routines to help debug distributed deadlock
  1869. */
  1870. /**
  1871. * dump_holder - print information about a glock holder
  1872. * @str: a string naming the type of holder
  1873. * @gh: the glock holder
  1874. *
  1875. * Returns: 0 on success, -ENOBUFS when we run out of space
  1876. */
  1877. static int dump_holder(char *str, struct gfs2_holder *gh)
  1878. {
  1879. unsigned int x;
  1880. int error = -ENOBUFS;
  1881. printk(KERN_INFO " %s\n", str);
  1882. printk(KERN_INFO " owner = %ld\n",
  1883. (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
  1884. printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
  1885. printk(KERN_INFO " gh_flags =");
  1886. for (x = 0; x < 32; x++)
  1887. if (gh->gh_flags & (1 << x))
  1888. printk(" %u", x);
  1889. printk(" \n");
  1890. printk(KERN_INFO " error = %d\n", gh->gh_error);
  1891. printk(KERN_INFO " gh_iflags =");
  1892. for (x = 0; x < 32; x++)
  1893. if (test_bit(x, &gh->gh_iflags))
  1894. printk(" %u", x);
  1895. printk(" \n");
  1896. error = 0;
  1897. return error;
  1898. }
  1899. /**
  1900. * dump_inode - print information about an inode
  1901. * @ip: the inode
  1902. *
  1903. * Returns: 0 on success, -ENOBUFS when we run out of space
  1904. */
  1905. static int dump_inode(struct gfs2_inode *ip)
  1906. {
  1907. unsigned int x;
  1908. int error = -ENOBUFS;
  1909. printk(KERN_INFO " Inode:\n");
  1910. printk(KERN_INFO " num = %llu %llu\n",
  1911. ip->i_num.no_formal_ino, ip->i_num.no_addr);
  1912. printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
  1913. printk(KERN_INFO " i_count = %d\n", atomic_read(&ip->i_count));
  1914. printk(KERN_INFO " i_flags =");
  1915. for (x = 0; x < 32; x++)
  1916. if (test_bit(x, &ip->i_flags))
  1917. printk(" %u", x);
  1918. printk(" \n");
  1919. printk(KERN_INFO " vnode = %s\n", (ip->i_vnode) ? "yes" : "no");
  1920. error = 0;
  1921. return error;
  1922. }
  1923. /**
  1924. * dump_glock - print information about a glock
  1925. * @gl: the glock
  1926. * @count: where we are in the buffer
  1927. *
  1928. * Returns: 0 on success, -ENOBUFS when we run out of space
  1929. */
  1930. static int dump_glock(struct gfs2_glock *gl)
  1931. {
  1932. struct gfs2_holder *gh;
  1933. unsigned int x;
  1934. int error = -ENOBUFS;
  1935. spin_lock(&gl->gl_spin);
  1936. printk(KERN_INFO "Glock (%u, %llu)\n",
  1937. gl->gl_name.ln_type,
  1938. gl->gl_name.ln_number);
  1939. printk(KERN_INFO " gl_flags =");
  1940. for (x = 0; x < 32; x++)
  1941. if (test_bit(x, &gl->gl_flags))
  1942. printk(" %u", x);
  1943. printk(" \n");
  1944. printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
  1945. printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
  1946. printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
  1947. printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
  1948. printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
  1949. printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
  1950. printk(KERN_INFO " le = %s\n",
  1951. (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
  1952. printk(KERN_INFO " reclaim = %s\n",
  1953. (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
  1954. if (gl->gl_aspace)
  1955. printk(KERN_INFO " aspace = %lu\n",
  1956. gl->gl_aspace->i_mapping->nrpages);
  1957. else
  1958. printk(KERN_INFO " aspace = no\n");
  1959. printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
  1960. if (gl->gl_req_gh) {
  1961. error = dump_holder("Request", gl->gl_req_gh);
  1962. if (error)
  1963. goto out;
  1964. }
  1965. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  1966. error = dump_holder("Holder", gh);
  1967. if (error)
  1968. goto out;
  1969. }
  1970. list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
  1971. error = dump_holder("Waiter1", gh);
  1972. if (error)
  1973. goto out;
  1974. }
  1975. list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
  1976. error = dump_holder("Waiter2", gh);
  1977. if (error)
  1978. goto out;
  1979. }
  1980. list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
  1981. error = dump_holder("Waiter3", gh);
  1982. if (error)
  1983. goto out;
  1984. }
  1985. if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
  1986. if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
  1987. list_empty(&gl->gl_holders)) {
  1988. error = dump_inode(gl->gl_object);
  1989. if (error)
  1990. goto out;
  1991. } else {
  1992. error = -ENOBUFS;
  1993. printk(KERN_INFO " Inode: busy\n");
  1994. }
  1995. }
  1996. error = 0;
  1997. out:
  1998. spin_unlock(&gl->gl_spin);
  1999. return error;
  2000. }
  2001. /**
  2002. * gfs2_dump_lockstate - print out the current lockstate
  2003. * @sdp: the filesystem
  2004. * @ub: the buffer to copy the information into
  2005. *
  2006. * If @ub is NULL, dump the lockstate to the console.
  2007. *
  2008. */
  2009. int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
  2010. {
  2011. struct gfs2_gl_hash_bucket *bucket;
  2012. struct gfs2_glock *gl;
  2013. unsigned int x;
  2014. int error = 0;
  2015. for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
  2016. bucket = &sdp->sd_gl_hash[x];
  2017. read_lock(&bucket->hb_lock);
  2018. list_for_each_entry(gl, &bucket->hb_list, gl_list) {
  2019. if (test_bit(GLF_PLUG, &gl->gl_flags))
  2020. continue;
  2021. error = dump_glock(gl);
  2022. if (error)
  2023. break;
  2024. }
  2025. read_unlock(&bucket->hb_lock);
  2026. if (error)
  2027. break;
  2028. }
  2029. return error;
  2030. }