glock.c 50 KB

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