glock.c 50 KB

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