glock.c 50 KB

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