glock.c 50 KB

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