glock.c 46 KB

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