genhd.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * gendisk handling
  4. */
  5. #include <linux/module.h>
  6. #include <linux/ctype.h>
  7. #include <linux/fs.h>
  8. #include <linux/genhd.h>
  9. #include <linux/kdev_t.h>
  10. #include <linux/kernel.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/backing-dev.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/slab.h>
  18. #include <linux/kmod.h>
  19. #include <linux/kobj_map.h>
  20. #include <linux/mutex.h>
  21. #include <linux/idr.h>
  22. #include <linux/log2.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/badblocks.h>
  25. #include "blk.h"
  26. static DEFINE_MUTEX(block_class_lock);
  27. static struct kobject *block_depr;
  28. /* for extended dynamic devt allocation, currently only one major is used */
  29. #define NR_EXT_DEVT (1 << MINORBITS)
  30. /* For extended devt allocation. ext_devt_lock prevents look up
  31. * results from going away underneath its user.
  32. */
  33. static DEFINE_SPINLOCK(ext_devt_lock);
  34. static DEFINE_IDR(ext_devt_idr);
  35. static void disk_check_events(struct disk_events *ev,
  36. unsigned int *clearing_ptr);
  37. static void disk_alloc_events(struct gendisk *disk);
  38. static void disk_add_events(struct gendisk *disk);
  39. static void disk_del_events(struct gendisk *disk);
  40. static void disk_release_events(struct gendisk *disk);
  41. /*
  42. * Set disk capacity and notify if the size is not currently
  43. * zero and will not be set to zero
  44. */
  45. bool set_capacity_revalidate_and_notify(struct gendisk *disk, sector_t size,
  46. bool update_bdev)
  47. {
  48. sector_t capacity = get_capacity(disk);
  49. set_capacity(disk, size);
  50. if (update_bdev)
  51. revalidate_disk_size(disk, true);
  52. if (capacity != size && capacity != 0 && size != 0) {
  53. char *envp[] = { "RESIZE=1", NULL };
  54. kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
  55. return true;
  56. }
  57. return false;
  58. }
  59. EXPORT_SYMBOL_GPL(set_capacity_revalidate_and_notify);
  60. /*
  61. * Format the device name of the indicated disk into the supplied buffer and
  62. * return a pointer to that same buffer for convenience.
  63. */
  64. char *disk_name(struct gendisk *hd, int partno, char *buf)
  65. {
  66. if (!partno)
  67. snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
  68. else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
  69. snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
  70. else
  71. snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
  72. return buf;
  73. }
  74. const char *bdevname(struct block_device *bdev, char *buf)
  75. {
  76. return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
  77. }
  78. EXPORT_SYMBOL(bdevname);
  79. static void part_stat_read_all(struct hd_struct *part, struct disk_stats *stat)
  80. {
  81. int cpu;
  82. memset(stat, 0, sizeof(struct disk_stats));
  83. for_each_possible_cpu(cpu) {
  84. struct disk_stats *ptr = per_cpu_ptr(part->dkstats, cpu);
  85. int group;
  86. for (group = 0; group < NR_STAT_GROUPS; group++) {
  87. stat->nsecs[group] += ptr->nsecs[group];
  88. stat->sectors[group] += ptr->sectors[group];
  89. stat->ios[group] += ptr->ios[group];
  90. stat->merges[group] += ptr->merges[group];
  91. }
  92. stat->io_ticks += ptr->io_ticks;
  93. }
  94. }
  95. static unsigned int part_in_flight(struct hd_struct *part)
  96. {
  97. unsigned int inflight = 0;
  98. int cpu;
  99. for_each_possible_cpu(cpu) {
  100. inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
  101. part_stat_local_read_cpu(part, in_flight[1], cpu);
  102. }
  103. if ((int)inflight < 0)
  104. inflight = 0;
  105. return inflight;
  106. }
  107. static void part_in_flight_rw(struct hd_struct *part, unsigned int inflight[2])
  108. {
  109. int cpu;
  110. inflight[0] = 0;
  111. inflight[1] = 0;
  112. for_each_possible_cpu(cpu) {
  113. inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
  114. inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
  115. }
  116. if ((int)inflight[0] < 0)
  117. inflight[0] = 0;
  118. if ((int)inflight[1] < 0)
  119. inflight[1] = 0;
  120. }
  121. struct hd_struct *__disk_get_part(struct gendisk *disk, int partno)
  122. {
  123. struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
  124. if (unlikely(partno < 0 || partno >= ptbl->len))
  125. return NULL;
  126. return rcu_dereference(ptbl->part[partno]);
  127. }
  128. /**
  129. * disk_get_part - get partition
  130. * @disk: disk to look partition from
  131. * @partno: partition number
  132. *
  133. * Look for partition @partno from @disk. If found, increment
  134. * reference count and return it.
  135. *
  136. * CONTEXT:
  137. * Don't care.
  138. *
  139. * RETURNS:
  140. * Pointer to the found partition on success, NULL if not found.
  141. */
  142. struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
  143. {
  144. struct hd_struct *part;
  145. rcu_read_lock();
  146. part = __disk_get_part(disk, partno);
  147. if (part)
  148. get_device(part_to_dev(part));
  149. rcu_read_unlock();
  150. return part;
  151. }
  152. /**
  153. * disk_part_iter_init - initialize partition iterator
  154. * @piter: iterator to initialize
  155. * @disk: disk to iterate over
  156. * @flags: DISK_PITER_* flags
  157. *
  158. * Initialize @piter so that it iterates over partitions of @disk.
  159. *
  160. * CONTEXT:
  161. * Don't care.
  162. */
  163. void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
  164. unsigned int flags)
  165. {
  166. struct disk_part_tbl *ptbl;
  167. rcu_read_lock();
  168. ptbl = rcu_dereference(disk->part_tbl);
  169. piter->disk = disk;
  170. piter->part = NULL;
  171. if (flags & DISK_PITER_REVERSE)
  172. piter->idx = ptbl->len - 1;
  173. else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
  174. piter->idx = 0;
  175. else
  176. piter->idx = 1;
  177. piter->flags = flags;
  178. rcu_read_unlock();
  179. }
  180. EXPORT_SYMBOL_GPL(disk_part_iter_init);
  181. /**
  182. * disk_part_iter_next - proceed iterator to the next partition and return it
  183. * @piter: iterator of interest
  184. *
  185. * Proceed @piter to the next partition and return it.
  186. *
  187. * CONTEXT:
  188. * Don't care.
  189. */
  190. struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
  191. {
  192. struct disk_part_tbl *ptbl;
  193. int inc, end;
  194. /* put the last partition */
  195. disk_put_part(piter->part);
  196. piter->part = NULL;
  197. /* get part_tbl */
  198. rcu_read_lock();
  199. ptbl = rcu_dereference(piter->disk->part_tbl);
  200. /* determine iteration parameters */
  201. if (piter->flags & DISK_PITER_REVERSE) {
  202. inc = -1;
  203. if (piter->flags & (DISK_PITER_INCL_PART0 |
  204. DISK_PITER_INCL_EMPTY_PART0))
  205. end = -1;
  206. else
  207. end = 0;
  208. } else {
  209. inc = 1;
  210. end = ptbl->len;
  211. }
  212. /* iterate to the next partition */
  213. for (; piter->idx != end; piter->idx += inc) {
  214. struct hd_struct *part;
  215. part = rcu_dereference(ptbl->part[piter->idx]);
  216. if (!part)
  217. continue;
  218. get_device(part_to_dev(part));
  219. piter->part = part;
  220. if (!part_nr_sects_read(part) &&
  221. !(piter->flags & DISK_PITER_INCL_EMPTY) &&
  222. !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
  223. piter->idx == 0)) {
  224. put_device(part_to_dev(part));
  225. piter->part = NULL;
  226. continue;
  227. }
  228. piter->idx += inc;
  229. break;
  230. }
  231. rcu_read_unlock();
  232. return piter->part;
  233. }
  234. EXPORT_SYMBOL_GPL(disk_part_iter_next);
  235. /**
  236. * disk_part_iter_exit - finish up partition iteration
  237. * @piter: iter of interest
  238. *
  239. * Called when iteration is over. Cleans up @piter.
  240. *
  241. * CONTEXT:
  242. * Don't care.
  243. */
  244. void disk_part_iter_exit(struct disk_part_iter *piter)
  245. {
  246. disk_put_part(piter->part);
  247. piter->part = NULL;
  248. }
  249. EXPORT_SYMBOL_GPL(disk_part_iter_exit);
  250. static inline int sector_in_part(struct hd_struct *part, sector_t sector)
  251. {
  252. return part->start_sect <= sector &&
  253. sector < part->start_sect + part_nr_sects_read(part);
  254. }
  255. /**
  256. * disk_map_sector_rcu - map sector to partition
  257. * @disk: gendisk of interest
  258. * @sector: sector to map
  259. *
  260. * Find out which partition @sector maps to on @disk. This is
  261. * primarily used for stats accounting.
  262. *
  263. * CONTEXT:
  264. * RCU read locked. The returned partition pointer is always valid
  265. * because its refcount is grabbed except for part0, which lifetime
  266. * is same with the disk.
  267. *
  268. * RETURNS:
  269. * Found partition on success, part0 is returned if no partition matches
  270. * or the matched partition is being deleted.
  271. */
  272. struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
  273. {
  274. struct disk_part_tbl *ptbl;
  275. struct hd_struct *part;
  276. int i;
  277. rcu_read_lock();
  278. ptbl = rcu_dereference(disk->part_tbl);
  279. part = rcu_dereference(ptbl->last_lookup);
  280. if (part && sector_in_part(part, sector) && hd_struct_try_get(part))
  281. goto out_unlock;
  282. for (i = 1; i < ptbl->len; i++) {
  283. part = rcu_dereference(ptbl->part[i]);
  284. if (part && sector_in_part(part, sector)) {
  285. /*
  286. * only live partition can be cached for lookup,
  287. * so use-after-free on cached & deleting partition
  288. * can be avoided
  289. */
  290. if (!hd_struct_try_get(part))
  291. break;
  292. rcu_assign_pointer(ptbl->last_lookup, part);
  293. goto out_unlock;
  294. }
  295. }
  296. part = &disk->part0;
  297. out_unlock:
  298. rcu_read_unlock();
  299. return part;
  300. }
  301. /**
  302. * disk_has_partitions
  303. * @disk: gendisk of interest
  304. *
  305. * Walk through the partition table and check if valid partition exists.
  306. *
  307. * CONTEXT:
  308. * Don't care.
  309. *
  310. * RETURNS:
  311. * True if the gendisk has at least one valid non-zero size partition.
  312. * Otherwise false.
  313. */
  314. bool disk_has_partitions(struct gendisk *disk)
  315. {
  316. struct disk_part_tbl *ptbl;
  317. int i;
  318. bool ret = false;
  319. rcu_read_lock();
  320. ptbl = rcu_dereference(disk->part_tbl);
  321. /* Iterate partitions skipping the whole device at index 0 */
  322. for (i = 1; i < ptbl->len; i++) {
  323. if (rcu_dereference(ptbl->part[i])) {
  324. ret = true;
  325. break;
  326. }
  327. }
  328. rcu_read_unlock();
  329. return ret;
  330. }
  331. EXPORT_SYMBOL_GPL(disk_has_partitions);
  332. /*
  333. * Can be deleted altogether. Later.
  334. *
  335. */
  336. #define BLKDEV_MAJOR_HASH_SIZE 255
  337. static struct blk_major_name {
  338. struct blk_major_name *next;
  339. int major;
  340. char name[16];
  341. } *major_names[BLKDEV_MAJOR_HASH_SIZE];
  342. /* index in the above - for now: assume no multimajor ranges */
  343. static inline int major_to_index(unsigned major)
  344. {
  345. return major % BLKDEV_MAJOR_HASH_SIZE;
  346. }
  347. #ifdef CONFIG_PROC_FS
  348. void blkdev_show(struct seq_file *seqf, off_t offset)
  349. {
  350. struct blk_major_name *dp;
  351. mutex_lock(&block_class_lock);
  352. for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
  353. if (dp->major == offset)
  354. seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
  355. mutex_unlock(&block_class_lock);
  356. }
  357. #endif /* CONFIG_PROC_FS */
  358. /**
  359. * register_blkdev - register a new block device
  360. *
  361. * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
  362. * @major = 0, try to allocate any unused major number.
  363. * @name: the name of the new block device as a zero terminated string
  364. *
  365. * The @name must be unique within the system.
  366. *
  367. * The return value depends on the @major input parameter:
  368. *
  369. * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
  370. * then the function returns zero on success, or a negative error code
  371. * - if any unused major number was requested with @major = 0 parameter
  372. * then the return value is the allocated major number in range
  373. * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
  374. *
  375. * See Documentation/admin-guide/devices.txt for the list of allocated
  376. * major numbers.
  377. */
  378. int register_blkdev(unsigned int major, const char *name)
  379. {
  380. struct blk_major_name **n, *p;
  381. int index, ret = 0;
  382. mutex_lock(&block_class_lock);
  383. /* temporary */
  384. if (major == 0) {
  385. for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
  386. if (major_names[index] == NULL)
  387. break;
  388. }
  389. if (index == 0) {
  390. printk("%s: failed to get major for %s\n",
  391. __func__, name);
  392. ret = -EBUSY;
  393. goto out;
  394. }
  395. major = index;
  396. ret = major;
  397. }
  398. if (major >= BLKDEV_MAJOR_MAX) {
  399. pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
  400. __func__, major, BLKDEV_MAJOR_MAX-1, name);
  401. ret = -EINVAL;
  402. goto out;
  403. }
  404. p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
  405. if (p == NULL) {
  406. ret = -ENOMEM;
  407. goto out;
  408. }
  409. p->major = major;
  410. strlcpy(p->name, name, sizeof(p->name));
  411. p->next = NULL;
  412. index = major_to_index(major);
  413. for (n = &major_names[index]; *n; n = &(*n)->next) {
  414. if ((*n)->major == major)
  415. break;
  416. }
  417. if (!*n)
  418. *n = p;
  419. else
  420. ret = -EBUSY;
  421. if (ret < 0) {
  422. printk("register_blkdev: cannot get major %u for %s\n",
  423. major, name);
  424. kfree(p);
  425. }
  426. out:
  427. mutex_unlock(&block_class_lock);
  428. return ret;
  429. }
  430. EXPORT_SYMBOL(register_blkdev);
  431. void unregister_blkdev(unsigned int major, const char *name)
  432. {
  433. struct blk_major_name **n;
  434. struct blk_major_name *p = NULL;
  435. int index = major_to_index(major);
  436. mutex_lock(&block_class_lock);
  437. for (n = &major_names[index]; *n; n = &(*n)->next)
  438. if ((*n)->major == major)
  439. break;
  440. if (!*n || strcmp((*n)->name, name)) {
  441. WARN_ON(1);
  442. } else {
  443. p = *n;
  444. *n = p->next;
  445. }
  446. mutex_unlock(&block_class_lock);
  447. kfree(p);
  448. }
  449. EXPORT_SYMBOL(unregister_blkdev);
  450. static struct kobj_map *bdev_map;
  451. /**
  452. * blk_mangle_minor - scatter minor numbers apart
  453. * @minor: minor number to mangle
  454. *
  455. * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
  456. * is enabled. Mangling twice gives the original value.
  457. *
  458. * RETURNS:
  459. * Mangled value.
  460. *
  461. * CONTEXT:
  462. * Don't care.
  463. */
  464. static int blk_mangle_minor(int minor)
  465. {
  466. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  467. int i;
  468. for (i = 0; i < MINORBITS / 2; i++) {
  469. int low = minor & (1 << i);
  470. int high = minor & (1 << (MINORBITS - 1 - i));
  471. int distance = MINORBITS - 1 - 2 * i;
  472. minor ^= low | high; /* clear both bits */
  473. low <<= distance; /* swap the positions */
  474. high >>= distance;
  475. minor |= low | high; /* and set */
  476. }
  477. #endif
  478. return minor;
  479. }
  480. /**
  481. * blk_alloc_devt - allocate a dev_t for a partition
  482. * @part: partition to allocate dev_t for
  483. * @devt: out parameter for resulting dev_t
  484. *
  485. * Allocate a dev_t for block device.
  486. *
  487. * RETURNS:
  488. * 0 on success, allocated dev_t is returned in *@devt. -errno on
  489. * failure.
  490. *
  491. * CONTEXT:
  492. * Might sleep.
  493. */
  494. int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
  495. {
  496. struct gendisk *disk = part_to_disk(part);
  497. int idx;
  498. /* in consecutive minor range? */
  499. if (part->partno < disk->minors) {
  500. *devt = MKDEV(disk->major, disk->first_minor + part->partno);
  501. return 0;
  502. }
  503. /* allocate ext devt */
  504. idr_preload(GFP_KERNEL);
  505. spin_lock_bh(&ext_devt_lock);
  506. idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
  507. spin_unlock_bh(&ext_devt_lock);
  508. idr_preload_end();
  509. if (idx < 0)
  510. return idx == -ENOSPC ? -EBUSY : idx;
  511. *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
  512. return 0;
  513. }
  514. /**
  515. * blk_free_devt - free a dev_t
  516. * @devt: dev_t to free
  517. *
  518. * Free @devt which was allocated using blk_alloc_devt().
  519. *
  520. * CONTEXT:
  521. * Might sleep.
  522. */
  523. void blk_free_devt(dev_t devt)
  524. {
  525. if (devt == MKDEV(0, 0))
  526. return;
  527. if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
  528. spin_lock_bh(&ext_devt_lock);
  529. idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
  530. spin_unlock_bh(&ext_devt_lock);
  531. }
  532. }
  533. /*
  534. * We invalidate devt by assigning NULL pointer for devt in idr.
  535. */
  536. void blk_invalidate_devt(dev_t devt)
  537. {
  538. if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
  539. spin_lock_bh(&ext_devt_lock);
  540. idr_replace(&ext_devt_idr, NULL, blk_mangle_minor(MINOR(devt)));
  541. spin_unlock_bh(&ext_devt_lock);
  542. }
  543. }
  544. static char *bdevt_str(dev_t devt, char *buf)
  545. {
  546. if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
  547. char tbuf[BDEVT_SIZE];
  548. snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
  549. snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
  550. } else
  551. snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
  552. return buf;
  553. }
  554. /*
  555. * Register device numbers dev..(dev+range-1)
  556. * range must be nonzero
  557. * The hash chain is sorted on range, so that subranges can override.
  558. */
  559. void blk_register_region(dev_t devt, unsigned long range, struct module *module,
  560. struct kobject *(*probe)(dev_t, int *, void *),
  561. int (*lock)(dev_t, void *), void *data)
  562. {
  563. kobj_map(bdev_map, devt, range, module, probe, lock, data);
  564. }
  565. EXPORT_SYMBOL(blk_register_region);
  566. void blk_unregister_region(dev_t devt, unsigned long range)
  567. {
  568. kobj_unmap(bdev_map, devt, range);
  569. }
  570. EXPORT_SYMBOL(blk_unregister_region);
  571. static struct kobject *exact_match(dev_t devt, int *partno, void *data)
  572. {
  573. struct gendisk *p = data;
  574. return &disk_to_dev(p)->kobj;
  575. }
  576. static int exact_lock(dev_t devt, void *data)
  577. {
  578. struct gendisk *p = data;
  579. if (!get_disk_and_module(p))
  580. return -1;
  581. return 0;
  582. }
  583. static void disk_scan_partitions(struct gendisk *disk)
  584. {
  585. struct block_device *bdev;
  586. if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
  587. return;
  588. set_bit(GD_NEED_PART_SCAN, &disk->state);
  589. bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
  590. if (!IS_ERR(bdev))
  591. blkdev_put(bdev, FMODE_READ);
  592. }
  593. static void register_disk(struct device *parent, struct gendisk *disk,
  594. const struct attribute_group **groups)
  595. {
  596. struct device *ddev = disk_to_dev(disk);
  597. struct disk_part_iter piter;
  598. struct hd_struct *part;
  599. int err;
  600. ddev->parent = parent;
  601. dev_set_name(ddev, "%s", disk->disk_name);
  602. /* delay uevents, until we scanned partition table */
  603. dev_set_uevent_suppress(ddev, 1);
  604. if (groups) {
  605. WARN_ON(ddev->groups);
  606. ddev->groups = groups;
  607. }
  608. if (device_add(ddev))
  609. return;
  610. if (!sysfs_deprecated) {
  611. err = sysfs_create_link(block_depr, &ddev->kobj,
  612. kobject_name(&ddev->kobj));
  613. if (err) {
  614. device_del(ddev);
  615. return;
  616. }
  617. }
  618. /*
  619. * avoid probable deadlock caused by allocating memory with
  620. * GFP_KERNEL in runtime_resume callback of its all ancestor
  621. * devices
  622. */
  623. pm_runtime_set_memalloc_noio(ddev, true);
  624. disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
  625. disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
  626. if (disk->flags & GENHD_FL_HIDDEN)
  627. return;
  628. disk_scan_partitions(disk);
  629. /* announce disk after possible partitions are created */
  630. dev_set_uevent_suppress(ddev, 0);
  631. kobject_uevent(&ddev->kobj, KOBJ_ADD);
  632. /* announce possible partitions */
  633. disk_part_iter_init(&piter, disk, 0);
  634. while ((part = disk_part_iter_next(&piter)))
  635. kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
  636. disk_part_iter_exit(&piter);
  637. if (disk->queue->backing_dev_info->dev) {
  638. err = sysfs_create_link(&ddev->kobj,
  639. &disk->queue->backing_dev_info->dev->kobj,
  640. "bdi");
  641. WARN_ON(err);
  642. }
  643. }
  644. /**
  645. * __device_add_disk - add disk information to kernel list
  646. * @parent: parent device for the disk
  647. * @disk: per-device partitioning information
  648. * @groups: Additional per-device sysfs groups
  649. * @register_queue: register the queue if set to true
  650. *
  651. * This function registers the partitioning information in @disk
  652. * with the kernel.
  653. *
  654. * FIXME: error handling
  655. */
  656. static void __device_add_disk(struct device *parent, struct gendisk *disk,
  657. const struct attribute_group **groups,
  658. bool register_queue)
  659. {
  660. dev_t devt;
  661. int retval;
  662. /*
  663. * The disk queue should now be all set with enough information about
  664. * the device for the elevator code to pick an adequate default
  665. * elevator if one is needed, that is, for devices requesting queue
  666. * registration.
  667. */
  668. if (register_queue)
  669. elevator_init_mq(disk->queue);
  670. /* minors == 0 indicates to use ext devt from part0 and should
  671. * be accompanied with EXT_DEVT flag. Make sure all
  672. * parameters make sense.
  673. */
  674. WARN_ON(disk->minors && !(disk->major || disk->first_minor));
  675. WARN_ON(!disk->minors &&
  676. !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
  677. disk->flags |= GENHD_FL_UP;
  678. retval = blk_alloc_devt(&disk->part0, &devt);
  679. if (retval) {
  680. WARN_ON(1);
  681. return;
  682. }
  683. disk->major = MAJOR(devt);
  684. disk->first_minor = MINOR(devt);
  685. disk_alloc_events(disk);
  686. if (disk->flags & GENHD_FL_HIDDEN) {
  687. /*
  688. * Don't let hidden disks show up in /proc/partitions,
  689. * and don't bother scanning for partitions either.
  690. */
  691. disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
  692. disk->flags |= GENHD_FL_NO_PART_SCAN;
  693. } else {
  694. struct backing_dev_info *bdi = disk->queue->backing_dev_info;
  695. struct device *dev = disk_to_dev(disk);
  696. int ret;
  697. /* Register BDI before referencing it from bdev */
  698. dev->devt = devt;
  699. ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
  700. WARN_ON(ret);
  701. bdi_set_owner(bdi, dev);
  702. blk_register_region(disk_devt(disk), disk->minors, NULL,
  703. exact_match, exact_lock, disk);
  704. }
  705. register_disk(parent, disk, groups);
  706. if (register_queue)
  707. blk_register_queue(disk);
  708. /*
  709. * Take an extra ref on queue which will be put on disk_release()
  710. * so that it sticks around as long as @disk is there.
  711. */
  712. WARN_ON_ONCE(!blk_get_queue(disk->queue));
  713. disk_add_events(disk);
  714. blk_integrity_add(disk);
  715. }
  716. void device_add_disk(struct device *parent, struct gendisk *disk,
  717. const struct attribute_group **groups)
  718. {
  719. __device_add_disk(parent, disk, groups, true);
  720. }
  721. EXPORT_SYMBOL(device_add_disk);
  722. void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
  723. {
  724. __device_add_disk(parent, disk, NULL, false);
  725. }
  726. EXPORT_SYMBOL(device_add_disk_no_queue_reg);
  727. static void invalidate_partition(struct gendisk *disk, int partno)
  728. {
  729. struct block_device *bdev;
  730. bdev = bdget_disk(disk, partno);
  731. if (!bdev)
  732. return;
  733. fsync_bdev(bdev);
  734. __invalidate_device(bdev, true);
  735. /*
  736. * Unhash the bdev inode for this device so that it gets evicted as soon
  737. * as last inode reference is dropped.
  738. */
  739. remove_inode_hash(bdev->bd_inode);
  740. bdput(bdev);
  741. }
  742. /**
  743. * del_gendisk - remove the gendisk
  744. * @disk: the struct gendisk to remove
  745. *
  746. * Removes the gendisk and all its associated resources. This deletes the
  747. * partitions associated with the gendisk, and unregisters the associated
  748. * request_queue.
  749. *
  750. * This is the counter to the respective __device_add_disk() call.
  751. *
  752. * The final removal of the struct gendisk happens when its refcount reaches 0
  753. * with put_disk(), which should be called after del_gendisk(), if
  754. * __device_add_disk() was used.
  755. *
  756. * Drivers exist which depend on the release of the gendisk to be synchronous,
  757. * it should not be deferred.
  758. *
  759. * Context: can sleep
  760. */
  761. void del_gendisk(struct gendisk *disk)
  762. {
  763. struct disk_part_iter piter;
  764. struct hd_struct *part;
  765. might_sleep();
  766. blk_integrity_del(disk);
  767. disk_del_events(disk);
  768. /*
  769. * Block lookups of the disk until all bdevs are unhashed and the
  770. * disk is marked as dead (GENHD_FL_UP cleared).
  771. */
  772. down_write(&disk->lookup_sem);
  773. /* invalidate stuff */
  774. disk_part_iter_init(&piter, disk,
  775. DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
  776. while ((part = disk_part_iter_next(&piter))) {
  777. invalidate_partition(disk, part->partno);
  778. delete_partition(part);
  779. }
  780. disk_part_iter_exit(&piter);
  781. invalidate_partition(disk, 0);
  782. set_capacity(disk, 0);
  783. disk->flags &= ~GENHD_FL_UP;
  784. up_write(&disk->lookup_sem);
  785. if (!(disk->flags & GENHD_FL_HIDDEN))
  786. sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
  787. if (disk->queue) {
  788. /*
  789. * Unregister bdi before releasing device numbers (as they can
  790. * get reused and we'd get clashes in sysfs).
  791. */
  792. if (!(disk->flags & GENHD_FL_HIDDEN))
  793. bdi_unregister(disk->queue->backing_dev_info);
  794. blk_unregister_queue(disk);
  795. } else {
  796. WARN_ON(1);
  797. }
  798. if (!(disk->flags & GENHD_FL_HIDDEN))
  799. blk_unregister_region(disk_devt(disk), disk->minors);
  800. /*
  801. * Remove gendisk pointer from idr so that it cannot be looked up
  802. * while RCU period before freeing gendisk is running to prevent
  803. * use-after-free issues. Note that the device number stays
  804. * "in-use" until we really free the gendisk.
  805. */
  806. blk_invalidate_devt(disk_devt(disk));
  807. kobject_put(disk->part0.holder_dir);
  808. kobject_put(disk->slave_dir);
  809. part_stat_set_all(&disk->part0, 0);
  810. disk->part0.stamp = 0;
  811. if (!sysfs_deprecated)
  812. sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
  813. pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
  814. device_del(disk_to_dev(disk));
  815. }
  816. EXPORT_SYMBOL(del_gendisk);
  817. /* sysfs access to bad-blocks list. */
  818. static ssize_t disk_badblocks_show(struct device *dev,
  819. struct device_attribute *attr,
  820. char *page)
  821. {
  822. struct gendisk *disk = dev_to_disk(dev);
  823. if (!disk->bb)
  824. return sprintf(page, "\n");
  825. return badblocks_show(disk->bb, page, 0);
  826. }
  827. static ssize_t disk_badblocks_store(struct device *dev,
  828. struct device_attribute *attr,
  829. const char *page, size_t len)
  830. {
  831. struct gendisk *disk = dev_to_disk(dev);
  832. if (!disk->bb)
  833. return -ENXIO;
  834. return badblocks_store(disk->bb, page, len, 0);
  835. }
  836. /**
  837. * get_gendisk - get partitioning information for a given device
  838. * @devt: device to get partitioning information for
  839. * @partno: returned partition index
  840. *
  841. * This function gets the structure containing partitioning
  842. * information for the given device @devt.
  843. *
  844. * Context: can sleep
  845. */
  846. struct gendisk *get_gendisk(dev_t devt, int *partno)
  847. {
  848. struct gendisk *disk = NULL;
  849. might_sleep();
  850. if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
  851. struct kobject *kobj;
  852. kobj = kobj_lookup(bdev_map, devt, partno);
  853. if (kobj)
  854. disk = dev_to_disk(kobj_to_dev(kobj));
  855. } else {
  856. struct hd_struct *part;
  857. spin_lock_bh(&ext_devt_lock);
  858. part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
  859. if (part && get_disk_and_module(part_to_disk(part))) {
  860. *partno = part->partno;
  861. disk = part_to_disk(part);
  862. }
  863. spin_unlock_bh(&ext_devt_lock);
  864. }
  865. if (!disk)
  866. return NULL;
  867. /*
  868. * Synchronize with del_gendisk() to not return disk that is being
  869. * destroyed.
  870. */
  871. down_read(&disk->lookup_sem);
  872. if (unlikely((disk->flags & GENHD_FL_HIDDEN) ||
  873. !(disk->flags & GENHD_FL_UP))) {
  874. up_read(&disk->lookup_sem);
  875. put_disk_and_module(disk);
  876. disk = NULL;
  877. } else {
  878. up_read(&disk->lookup_sem);
  879. }
  880. return disk;
  881. }
  882. /**
  883. * bdget_disk - do bdget() by gendisk and partition number
  884. * @disk: gendisk of interest
  885. * @partno: partition number
  886. *
  887. * Find partition @partno from @disk, do bdget() on it.
  888. *
  889. * CONTEXT:
  890. * Don't care.
  891. *
  892. * RETURNS:
  893. * Resulting block_device on success, NULL on failure.
  894. */
  895. struct block_device *bdget_disk(struct gendisk *disk, int partno)
  896. {
  897. struct hd_struct *part;
  898. struct block_device *bdev = NULL;
  899. part = disk_get_part(disk, partno);
  900. if (part)
  901. bdev = bdget_part(part);
  902. disk_put_part(part);
  903. return bdev;
  904. }
  905. EXPORT_SYMBOL(bdget_disk);
  906. /*
  907. * print a full list of all partitions - intended for places where the root
  908. * filesystem can't be mounted and thus to give the victim some idea of what
  909. * went wrong
  910. */
  911. void __init printk_all_partitions(void)
  912. {
  913. struct class_dev_iter iter;
  914. struct device *dev;
  915. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  916. while ((dev = class_dev_iter_next(&iter))) {
  917. struct gendisk *disk = dev_to_disk(dev);
  918. struct disk_part_iter piter;
  919. struct hd_struct *part;
  920. char name_buf[BDEVNAME_SIZE];
  921. char devt_buf[BDEVT_SIZE];
  922. /*
  923. * Don't show empty devices or things that have been
  924. * suppressed
  925. */
  926. if (get_capacity(disk) == 0 ||
  927. (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
  928. continue;
  929. /*
  930. * Note, unlike /proc/partitions, I am showing the
  931. * numbers in hex - the same format as the root=
  932. * option takes.
  933. */
  934. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  935. while ((part = disk_part_iter_next(&piter))) {
  936. bool is_part0 = part == &disk->part0;
  937. printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
  938. bdevt_str(part_devt(part), devt_buf),
  939. (unsigned long long)part_nr_sects_read(part) >> 1
  940. , disk_name(disk, part->partno, name_buf),
  941. part->info ? part->info->uuid : "");
  942. if (is_part0) {
  943. if (dev->parent && dev->parent->driver)
  944. printk(" driver: %s\n",
  945. dev->parent->driver->name);
  946. else
  947. printk(" (driver?)\n");
  948. } else
  949. printk("\n");
  950. }
  951. disk_part_iter_exit(&piter);
  952. }
  953. class_dev_iter_exit(&iter);
  954. }
  955. #ifdef CONFIG_PROC_FS
  956. /* iterator */
  957. static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
  958. {
  959. loff_t skip = *pos;
  960. struct class_dev_iter *iter;
  961. struct device *dev;
  962. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  963. if (!iter)
  964. return ERR_PTR(-ENOMEM);
  965. seqf->private = iter;
  966. class_dev_iter_init(iter, &block_class, NULL, &disk_type);
  967. do {
  968. dev = class_dev_iter_next(iter);
  969. if (!dev)
  970. return NULL;
  971. } while (skip--);
  972. return dev_to_disk(dev);
  973. }
  974. static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
  975. {
  976. struct device *dev;
  977. (*pos)++;
  978. dev = class_dev_iter_next(seqf->private);
  979. if (dev)
  980. return dev_to_disk(dev);
  981. return NULL;
  982. }
  983. static void disk_seqf_stop(struct seq_file *seqf, void *v)
  984. {
  985. struct class_dev_iter *iter = seqf->private;
  986. /* stop is called even after start failed :-( */
  987. if (iter) {
  988. class_dev_iter_exit(iter);
  989. kfree(iter);
  990. seqf->private = NULL;
  991. }
  992. }
  993. static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
  994. {
  995. void *p;
  996. p = disk_seqf_start(seqf, pos);
  997. if (!IS_ERR_OR_NULL(p) && !*pos)
  998. seq_puts(seqf, "major minor #blocks name\n\n");
  999. return p;
  1000. }
  1001. static int show_partition(struct seq_file *seqf, void *v)
  1002. {
  1003. struct gendisk *sgp = v;
  1004. struct disk_part_iter piter;
  1005. struct hd_struct *part;
  1006. char buf[BDEVNAME_SIZE];
  1007. /* Don't show non-partitionable removeable devices or empty devices */
  1008. if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
  1009. (sgp->flags & GENHD_FL_REMOVABLE)))
  1010. return 0;
  1011. if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
  1012. return 0;
  1013. /* show the full disk and all non-0 size partitions of it */
  1014. disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
  1015. while ((part = disk_part_iter_next(&piter)))
  1016. seq_printf(seqf, "%4d %7d %10llu %s\n",
  1017. MAJOR(part_devt(part)), MINOR(part_devt(part)),
  1018. (unsigned long long)part_nr_sects_read(part) >> 1,
  1019. disk_name(sgp, part->partno, buf));
  1020. disk_part_iter_exit(&piter);
  1021. return 0;
  1022. }
  1023. static const struct seq_operations partitions_op = {
  1024. .start = show_partition_start,
  1025. .next = disk_seqf_next,
  1026. .stop = disk_seqf_stop,
  1027. .show = show_partition
  1028. };
  1029. #endif
  1030. static struct kobject *base_probe(dev_t devt, int *partno, void *data)
  1031. {
  1032. if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
  1033. /* Make old-style 2.4 aliases work */
  1034. request_module("block-major-%d", MAJOR(devt));
  1035. return NULL;
  1036. }
  1037. static int __init genhd_device_init(void)
  1038. {
  1039. int error;
  1040. block_class.dev_kobj = sysfs_dev_block_kobj;
  1041. error = class_register(&block_class);
  1042. if (unlikely(error))
  1043. return error;
  1044. bdev_map = kobj_map_init(base_probe, &block_class_lock);
  1045. blk_dev_init();
  1046. register_blkdev(BLOCK_EXT_MAJOR, "blkext");
  1047. /* create top-level block dir */
  1048. if (!sysfs_deprecated)
  1049. block_depr = kobject_create_and_add("block", NULL);
  1050. return 0;
  1051. }
  1052. subsys_initcall(genhd_device_init);
  1053. static ssize_t disk_range_show(struct device *dev,
  1054. struct device_attribute *attr, char *buf)
  1055. {
  1056. struct gendisk *disk = dev_to_disk(dev);
  1057. return sprintf(buf, "%d\n", disk->minors);
  1058. }
  1059. static ssize_t disk_ext_range_show(struct device *dev,
  1060. struct device_attribute *attr, char *buf)
  1061. {
  1062. struct gendisk *disk = dev_to_disk(dev);
  1063. return sprintf(buf, "%d\n", disk_max_parts(disk));
  1064. }
  1065. static ssize_t disk_removable_show(struct device *dev,
  1066. struct device_attribute *attr, char *buf)
  1067. {
  1068. struct gendisk *disk = dev_to_disk(dev);
  1069. return sprintf(buf, "%d\n",
  1070. (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
  1071. }
  1072. static ssize_t disk_hidden_show(struct device *dev,
  1073. struct device_attribute *attr, char *buf)
  1074. {
  1075. struct gendisk *disk = dev_to_disk(dev);
  1076. return sprintf(buf, "%d\n",
  1077. (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
  1078. }
  1079. static ssize_t disk_ro_show(struct device *dev,
  1080. struct device_attribute *attr, char *buf)
  1081. {
  1082. struct gendisk *disk = dev_to_disk(dev);
  1083. return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
  1084. }
  1085. ssize_t part_size_show(struct device *dev,
  1086. struct device_attribute *attr, char *buf)
  1087. {
  1088. struct hd_struct *p = dev_to_part(dev);
  1089. return sprintf(buf, "%llu\n",
  1090. (unsigned long long)part_nr_sects_read(p));
  1091. }
  1092. ssize_t part_stat_show(struct device *dev,
  1093. struct device_attribute *attr, char *buf)
  1094. {
  1095. struct hd_struct *p = dev_to_part(dev);
  1096. struct request_queue *q = part_to_disk(p)->queue;
  1097. struct disk_stats stat;
  1098. unsigned int inflight;
  1099. part_stat_read_all(p, &stat);
  1100. if (queue_is_mq(q))
  1101. inflight = blk_mq_in_flight(q, p);
  1102. else
  1103. inflight = part_in_flight(p);
  1104. return sprintf(buf,
  1105. "%8lu %8lu %8llu %8u "
  1106. "%8lu %8lu %8llu %8u "
  1107. "%8u %8u %8u "
  1108. "%8lu %8lu %8llu %8u "
  1109. "%8lu %8u"
  1110. "\n",
  1111. stat.ios[STAT_READ],
  1112. stat.merges[STAT_READ],
  1113. (unsigned long long)stat.sectors[STAT_READ],
  1114. (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
  1115. stat.ios[STAT_WRITE],
  1116. stat.merges[STAT_WRITE],
  1117. (unsigned long long)stat.sectors[STAT_WRITE],
  1118. (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
  1119. inflight,
  1120. jiffies_to_msecs(stat.io_ticks),
  1121. (unsigned int)div_u64(stat.nsecs[STAT_READ] +
  1122. stat.nsecs[STAT_WRITE] +
  1123. stat.nsecs[STAT_DISCARD] +
  1124. stat.nsecs[STAT_FLUSH],
  1125. NSEC_PER_MSEC),
  1126. stat.ios[STAT_DISCARD],
  1127. stat.merges[STAT_DISCARD],
  1128. (unsigned long long)stat.sectors[STAT_DISCARD],
  1129. (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
  1130. stat.ios[STAT_FLUSH],
  1131. (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
  1132. }
  1133. ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
  1134. char *buf)
  1135. {
  1136. struct hd_struct *p = dev_to_part(dev);
  1137. struct request_queue *q = part_to_disk(p)->queue;
  1138. unsigned int inflight[2];
  1139. if (queue_is_mq(q))
  1140. blk_mq_in_flight_rw(q, p, inflight);
  1141. else
  1142. part_in_flight_rw(p, inflight);
  1143. return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
  1144. }
  1145. static ssize_t disk_capability_show(struct device *dev,
  1146. struct device_attribute *attr, char *buf)
  1147. {
  1148. struct gendisk *disk = dev_to_disk(dev);
  1149. return sprintf(buf, "%x\n", disk->flags);
  1150. }
  1151. static ssize_t disk_alignment_offset_show(struct device *dev,
  1152. struct device_attribute *attr,
  1153. char *buf)
  1154. {
  1155. struct gendisk *disk = dev_to_disk(dev);
  1156. return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
  1157. }
  1158. static ssize_t disk_discard_alignment_show(struct device *dev,
  1159. struct device_attribute *attr,
  1160. char *buf)
  1161. {
  1162. struct gendisk *disk = dev_to_disk(dev);
  1163. return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
  1164. }
  1165. static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
  1166. static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
  1167. static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
  1168. static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
  1169. static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
  1170. static DEVICE_ATTR(size, 0444, part_size_show, NULL);
  1171. static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
  1172. static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
  1173. static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
  1174. static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
  1175. static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
  1176. static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
  1177. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1178. ssize_t part_fail_show(struct device *dev,
  1179. struct device_attribute *attr, char *buf)
  1180. {
  1181. struct hd_struct *p = dev_to_part(dev);
  1182. return sprintf(buf, "%d\n", p->make_it_fail);
  1183. }
  1184. ssize_t part_fail_store(struct device *dev,
  1185. struct device_attribute *attr,
  1186. const char *buf, size_t count)
  1187. {
  1188. struct hd_struct *p = dev_to_part(dev);
  1189. int i;
  1190. if (count > 0 && sscanf(buf, "%d", &i) > 0)
  1191. p->make_it_fail = (i == 0) ? 0 : 1;
  1192. return count;
  1193. }
  1194. static struct device_attribute dev_attr_fail =
  1195. __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
  1196. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  1197. #ifdef CONFIG_FAIL_IO_TIMEOUT
  1198. static struct device_attribute dev_attr_fail_timeout =
  1199. __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
  1200. #endif
  1201. static struct attribute *disk_attrs[] = {
  1202. &dev_attr_range.attr,
  1203. &dev_attr_ext_range.attr,
  1204. &dev_attr_removable.attr,
  1205. &dev_attr_hidden.attr,
  1206. &dev_attr_ro.attr,
  1207. &dev_attr_size.attr,
  1208. &dev_attr_alignment_offset.attr,
  1209. &dev_attr_discard_alignment.attr,
  1210. &dev_attr_capability.attr,
  1211. &dev_attr_stat.attr,
  1212. &dev_attr_inflight.attr,
  1213. &dev_attr_badblocks.attr,
  1214. #ifdef CONFIG_FAIL_MAKE_REQUEST
  1215. &dev_attr_fail.attr,
  1216. #endif
  1217. #ifdef CONFIG_FAIL_IO_TIMEOUT
  1218. &dev_attr_fail_timeout.attr,
  1219. #endif
  1220. NULL
  1221. };
  1222. static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
  1223. {
  1224. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  1225. struct gendisk *disk = dev_to_disk(dev);
  1226. if (a == &dev_attr_badblocks.attr && !disk->bb)
  1227. return 0;
  1228. return a->mode;
  1229. }
  1230. static struct attribute_group disk_attr_group = {
  1231. .attrs = disk_attrs,
  1232. .is_visible = disk_visible,
  1233. };
  1234. static const struct attribute_group *disk_attr_groups[] = {
  1235. &disk_attr_group,
  1236. NULL
  1237. };
  1238. /**
  1239. * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
  1240. * @disk: disk to replace part_tbl for
  1241. * @new_ptbl: new part_tbl to install
  1242. *
  1243. * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
  1244. * original ptbl is freed using RCU callback.
  1245. *
  1246. * LOCKING:
  1247. * Matching bd_mutex locked or the caller is the only user of @disk.
  1248. */
  1249. static void disk_replace_part_tbl(struct gendisk *disk,
  1250. struct disk_part_tbl *new_ptbl)
  1251. {
  1252. struct disk_part_tbl *old_ptbl =
  1253. rcu_dereference_protected(disk->part_tbl, 1);
  1254. rcu_assign_pointer(disk->part_tbl, new_ptbl);
  1255. if (old_ptbl) {
  1256. rcu_assign_pointer(old_ptbl->last_lookup, NULL);
  1257. kfree_rcu(old_ptbl, rcu_head);
  1258. }
  1259. }
  1260. /**
  1261. * disk_expand_part_tbl - expand disk->part_tbl
  1262. * @disk: disk to expand part_tbl for
  1263. * @partno: expand such that this partno can fit in
  1264. *
  1265. * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
  1266. * uses RCU to allow unlocked dereferencing for stats and other stuff.
  1267. *
  1268. * LOCKING:
  1269. * Matching bd_mutex locked or the caller is the only user of @disk.
  1270. * Might sleep.
  1271. *
  1272. * RETURNS:
  1273. * 0 on success, -errno on failure.
  1274. */
  1275. int disk_expand_part_tbl(struct gendisk *disk, int partno)
  1276. {
  1277. struct disk_part_tbl *old_ptbl =
  1278. rcu_dereference_protected(disk->part_tbl, 1);
  1279. struct disk_part_tbl *new_ptbl;
  1280. int len = old_ptbl ? old_ptbl->len : 0;
  1281. int i, target;
  1282. /*
  1283. * check for int overflow, since we can get here from blkpg_ioctl()
  1284. * with a user passed 'partno'.
  1285. */
  1286. target = partno + 1;
  1287. if (target < 0)
  1288. return -EINVAL;
  1289. /* disk_max_parts() is zero during initialization, ignore if so */
  1290. if (disk_max_parts(disk) && target > disk_max_parts(disk))
  1291. return -EINVAL;
  1292. if (target <= len)
  1293. return 0;
  1294. new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
  1295. disk->node_id);
  1296. if (!new_ptbl)
  1297. return -ENOMEM;
  1298. new_ptbl->len = target;
  1299. for (i = 0; i < len; i++)
  1300. rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
  1301. disk_replace_part_tbl(disk, new_ptbl);
  1302. return 0;
  1303. }
  1304. /**
  1305. * disk_release - releases all allocated resources of the gendisk
  1306. * @dev: the device representing this disk
  1307. *
  1308. * This function releases all allocated resources of the gendisk.
  1309. *
  1310. * The struct gendisk refcount is incremented with get_gendisk() or
  1311. * get_disk_and_module(), and its refcount is decremented with
  1312. * put_disk_and_module() or put_disk(). Once the refcount reaches 0 this
  1313. * function is called.
  1314. *
  1315. * Drivers which used __device_add_disk() have a gendisk with a request_queue
  1316. * assigned. Since the request_queue sits on top of the gendisk for these
  1317. * drivers we also call blk_put_queue() for them, and we expect the
  1318. * request_queue refcount to reach 0 at this point, and so the request_queue
  1319. * will also be freed prior to the disk.
  1320. *
  1321. * Context: can sleep
  1322. */
  1323. static void disk_release(struct device *dev)
  1324. {
  1325. struct gendisk *disk = dev_to_disk(dev);
  1326. might_sleep();
  1327. blk_free_devt(dev->devt);
  1328. disk_release_events(disk);
  1329. kfree(disk->random);
  1330. disk_replace_part_tbl(disk, NULL);
  1331. hd_free_part(&disk->part0);
  1332. if (disk->queue)
  1333. blk_put_queue(disk->queue);
  1334. kfree(disk);
  1335. }
  1336. struct class block_class = {
  1337. .name = "block",
  1338. };
  1339. static char *block_devnode(struct device *dev, umode_t *mode,
  1340. kuid_t *uid, kgid_t *gid)
  1341. {
  1342. struct gendisk *disk = dev_to_disk(dev);
  1343. if (disk->fops->devnode)
  1344. return disk->fops->devnode(disk, mode);
  1345. return NULL;
  1346. }
  1347. const struct device_type disk_type = {
  1348. .name = "disk",
  1349. .groups = disk_attr_groups,
  1350. .release = disk_release,
  1351. .devnode = block_devnode,
  1352. };
  1353. #ifdef CONFIG_PROC_FS
  1354. /*
  1355. * aggregate disk stat collector. Uses the same stats that the sysfs
  1356. * entries do, above, but makes them available through one seq_file.
  1357. *
  1358. * The output looks suspiciously like /proc/partitions with a bunch of
  1359. * extra fields.
  1360. */
  1361. static int diskstats_show(struct seq_file *seqf, void *v)
  1362. {
  1363. struct gendisk *gp = v;
  1364. struct disk_part_iter piter;
  1365. struct hd_struct *hd;
  1366. char buf[BDEVNAME_SIZE];
  1367. unsigned int inflight;
  1368. struct disk_stats stat;
  1369. /*
  1370. if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
  1371. seq_puts(seqf, "major minor name"
  1372. " rio rmerge rsect ruse wio wmerge "
  1373. "wsect wuse running use aveq"
  1374. "\n\n");
  1375. */
  1376. disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
  1377. while ((hd = disk_part_iter_next(&piter))) {
  1378. part_stat_read_all(hd, &stat);
  1379. if (queue_is_mq(gp->queue))
  1380. inflight = blk_mq_in_flight(gp->queue, hd);
  1381. else
  1382. inflight = part_in_flight(hd);
  1383. seq_printf(seqf, "%4d %7d %s "
  1384. "%lu %lu %lu %u "
  1385. "%lu %lu %lu %u "
  1386. "%u %u %u "
  1387. "%lu %lu %lu %u "
  1388. "%lu %u"
  1389. "\n",
  1390. MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
  1391. disk_name(gp, hd->partno, buf),
  1392. stat.ios[STAT_READ],
  1393. stat.merges[STAT_READ],
  1394. stat.sectors[STAT_READ],
  1395. (unsigned int)div_u64(stat.nsecs[STAT_READ],
  1396. NSEC_PER_MSEC),
  1397. stat.ios[STAT_WRITE],
  1398. stat.merges[STAT_WRITE],
  1399. stat.sectors[STAT_WRITE],
  1400. (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
  1401. NSEC_PER_MSEC),
  1402. inflight,
  1403. jiffies_to_msecs(stat.io_ticks),
  1404. (unsigned int)div_u64(stat.nsecs[STAT_READ] +
  1405. stat.nsecs[STAT_WRITE] +
  1406. stat.nsecs[STAT_DISCARD] +
  1407. stat.nsecs[STAT_FLUSH],
  1408. NSEC_PER_MSEC),
  1409. stat.ios[STAT_DISCARD],
  1410. stat.merges[STAT_DISCARD],
  1411. stat.sectors[STAT_DISCARD],
  1412. (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
  1413. NSEC_PER_MSEC),
  1414. stat.ios[STAT_FLUSH],
  1415. (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
  1416. NSEC_PER_MSEC)
  1417. );
  1418. }
  1419. disk_part_iter_exit(&piter);
  1420. return 0;
  1421. }
  1422. static const struct seq_operations diskstats_op = {
  1423. .start = disk_seqf_start,
  1424. .next = disk_seqf_next,
  1425. .stop = disk_seqf_stop,
  1426. .show = diskstats_show
  1427. };
  1428. static int __init proc_genhd_init(void)
  1429. {
  1430. proc_create_seq("diskstats", 0, NULL, &diskstats_op);
  1431. proc_create_seq("partitions", 0, NULL, &partitions_op);
  1432. return 0;
  1433. }
  1434. module_init(proc_genhd_init);
  1435. #endif /* CONFIG_PROC_FS */
  1436. dev_t blk_lookup_devt(const char *name, int partno)
  1437. {
  1438. dev_t devt = MKDEV(0, 0);
  1439. struct class_dev_iter iter;
  1440. struct device *dev;
  1441. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  1442. while ((dev = class_dev_iter_next(&iter))) {
  1443. struct gendisk *disk = dev_to_disk(dev);
  1444. struct hd_struct *part;
  1445. if (strcmp(dev_name(dev), name))
  1446. continue;
  1447. if (partno < disk->minors) {
  1448. /* We need to return the right devno, even
  1449. * if the partition doesn't exist yet.
  1450. */
  1451. devt = MKDEV(MAJOR(dev->devt),
  1452. MINOR(dev->devt) + partno);
  1453. break;
  1454. }
  1455. part = disk_get_part(disk, partno);
  1456. if (part) {
  1457. devt = part_devt(part);
  1458. disk_put_part(part);
  1459. break;
  1460. }
  1461. disk_put_part(part);
  1462. }
  1463. class_dev_iter_exit(&iter);
  1464. return devt;
  1465. }
  1466. struct gendisk *__alloc_disk_node(int minors, int node_id)
  1467. {
  1468. struct gendisk *disk;
  1469. struct disk_part_tbl *ptbl;
  1470. if (minors > DISK_MAX_PARTS) {
  1471. printk(KERN_ERR
  1472. "block: can't allocate more than %d partitions\n",
  1473. DISK_MAX_PARTS);
  1474. minors = DISK_MAX_PARTS;
  1475. }
  1476. disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
  1477. if (!disk)
  1478. return NULL;
  1479. disk->part0.dkstats = alloc_percpu(struct disk_stats);
  1480. if (!disk->part0.dkstats)
  1481. goto out_free_disk;
  1482. init_rwsem(&disk->lookup_sem);
  1483. disk->node_id = node_id;
  1484. if (disk_expand_part_tbl(disk, 0)) {
  1485. free_percpu(disk->part0.dkstats);
  1486. goto out_free_disk;
  1487. }
  1488. ptbl = rcu_dereference_protected(disk->part_tbl, 1);
  1489. rcu_assign_pointer(ptbl->part[0], &disk->part0);
  1490. /*
  1491. * set_capacity() and get_capacity() currently don't use
  1492. * seqcounter to read/update the part0->nr_sects. Still init
  1493. * the counter as we can read the sectors in IO submission
  1494. * patch using seqence counters.
  1495. *
  1496. * TODO: Ideally set_capacity() and get_capacity() should be
  1497. * converted to make use of bd_mutex and sequence counters.
  1498. */
  1499. hd_sects_seq_init(&disk->part0);
  1500. if (hd_ref_init(&disk->part0))
  1501. goto out_free_part0;
  1502. disk->minors = minors;
  1503. rand_initialize_disk(disk);
  1504. disk_to_dev(disk)->class = &block_class;
  1505. disk_to_dev(disk)->type = &disk_type;
  1506. device_initialize(disk_to_dev(disk));
  1507. return disk;
  1508. out_free_part0:
  1509. hd_free_part(&disk->part0);
  1510. out_free_disk:
  1511. kfree(disk);
  1512. return NULL;
  1513. }
  1514. EXPORT_SYMBOL(__alloc_disk_node);
  1515. /**
  1516. * get_disk_and_module - increments the gendisk and gendisk fops module refcount
  1517. * @disk: the struct gendisk to increment the refcount for
  1518. *
  1519. * This increments the refcount for the struct gendisk, and the gendisk's
  1520. * fops module owner.
  1521. *
  1522. * Context: Any context.
  1523. */
  1524. struct kobject *get_disk_and_module(struct gendisk *disk)
  1525. {
  1526. struct module *owner;
  1527. struct kobject *kobj;
  1528. if (!disk->fops)
  1529. return NULL;
  1530. owner = disk->fops->owner;
  1531. if (owner && !try_module_get(owner))
  1532. return NULL;
  1533. kobj = kobject_get_unless_zero(&disk_to_dev(disk)->kobj);
  1534. if (kobj == NULL) {
  1535. module_put(owner);
  1536. return NULL;
  1537. }
  1538. return kobj;
  1539. }
  1540. EXPORT_SYMBOL(get_disk_and_module);
  1541. /**
  1542. * put_disk - decrements the gendisk refcount
  1543. * @disk: the struct gendisk to decrement the refcount for
  1544. *
  1545. * This decrements the refcount for the struct gendisk. When this reaches 0
  1546. * we'll have disk_release() called.
  1547. *
  1548. * Context: Any context, but the last reference must not be dropped from
  1549. * atomic context.
  1550. */
  1551. void put_disk(struct gendisk *disk)
  1552. {
  1553. if (disk)
  1554. kobject_put(&disk_to_dev(disk)->kobj);
  1555. }
  1556. EXPORT_SYMBOL(put_disk);
  1557. /**
  1558. * put_disk_and_module - decrements the module and gendisk refcount
  1559. * @disk: the struct gendisk to decrement the refcount for
  1560. *
  1561. * This is a counterpart of get_disk_and_module() and thus also of
  1562. * get_gendisk().
  1563. *
  1564. * Context: Any context, but the last reference must not be dropped from
  1565. * atomic context.
  1566. */
  1567. void put_disk_and_module(struct gendisk *disk)
  1568. {
  1569. if (disk) {
  1570. struct module *owner = disk->fops->owner;
  1571. put_disk(disk);
  1572. module_put(owner);
  1573. }
  1574. }
  1575. EXPORT_SYMBOL(put_disk_and_module);
  1576. static void set_disk_ro_uevent(struct gendisk *gd, int ro)
  1577. {
  1578. char event[] = "DISK_RO=1";
  1579. char *envp[] = { event, NULL };
  1580. if (!ro)
  1581. event[8] = '0';
  1582. kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
  1583. }
  1584. void set_device_ro(struct block_device *bdev, int flag)
  1585. {
  1586. bdev->bd_part->policy = flag;
  1587. }
  1588. EXPORT_SYMBOL(set_device_ro);
  1589. void set_disk_ro(struct gendisk *disk, int flag)
  1590. {
  1591. struct disk_part_iter piter;
  1592. struct hd_struct *part;
  1593. if (disk->part0.policy != flag) {
  1594. set_disk_ro_uevent(disk, flag);
  1595. disk->part0.policy = flag;
  1596. }
  1597. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
  1598. while ((part = disk_part_iter_next(&piter)))
  1599. part->policy = flag;
  1600. disk_part_iter_exit(&piter);
  1601. }
  1602. EXPORT_SYMBOL(set_disk_ro);
  1603. int bdev_read_only(struct block_device *bdev)
  1604. {
  1605. if (!bdev)
  1606. return 0;
  1607. return bdev->bd_part->policy;
  1608. }
  1609. EXPORT_SYMBOL(bdev_read_only);
  1610. /*
  1611. * Disk events - monitor disk events like media change and eject request.
  1612. */
  1613. struct disk_events {
  1614. struct list_head node; /* all disk_event's */
  1615. struct gendisk *disk; /* the associated disk */
  1616. spinlock_t lock;
  1617. struct mutex block_mutex; /* protects blocking */
  1618. int block; /* event blocking depth */
  1619. unsigned int pending; /* events already sent out */
  1620. unsigned int clearing; /* events being cleared */
  1621. long poll_msecs; /* interval, -1 for default */
  1622. struct delayed_work dwork;
  1623. };
  1624. static const char *disk_events_strs[] = {
  1625. [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
  1626. [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
  1627. };
  1628. static char *disk_uevents[] = {
  1629. [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
  1630. [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
  1631. };
  1632. /* list of all disk_events */
  1633. static DEFINE_MUTEX(disk_events_mutex);
  1634. static LIST_HEAD(disk_events);
  1635. /* disable in-kernel polling by default */
  1636. static unsigned long disk_events_dfl_poll_msecs;
  1637. static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
  1638. {
  1639. struct disk_events *ev = disk->ev;
  1640. long intv_msecs = 0;
  1641. /*
  1642. * If device-specific poll interval is set, always use it. If
  1643. * the default is being used, poll if the POLL flag is set.
  1644. */
  1645. if (ev->poll_msecs >= 0)
  1646. intv_msecs = ev->poll_msecs;
  1647. else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
  1648. intv_msecs = disk_events_dfl_poll_msecs;
  1649. return msecs_to_jiffies(intv_msecs);
  1650. }
  1651. /**
  1652. * disk_block_events - block and flush disk event checking
  1653. * @disk: disk to block events for
  1654. *
  1655. * On return from this function, it is guaranteed that event checking
  1656. * isn't in progress and won't happen until unblocked by
  1657. * disk_unblock_events(). Events blocking is counted and the actual
  1658. * unblocking happens after the matching number of unblocks are done.
  1659. *
  1660. * Note that this intentionally does not block event checking from
  1661. * disk_clear_events().
  1662. *
  1663. * CONTEXT:
  1664. * Might sleep.
  1665. */
  1666. void disk_block_events(struct gendisk *disk)
  1667. {
  1668. struct disk_events *ev = disk->ev;
  1669. unsigned long flags;
  1670. bool cancel;
  1671. if (!ev)
  1672. return;
  1673. /*
  1674. * Outer mutex ensures that the first blocker completes canceling
  1675. * the event work before further blockers are allowed to finish.
  1676. */
  1677. mutex_lock(&ev->block_mutex);
  1678. spin_lock_irqsave(&ev->lock, flags);
  1679. cancel = !ev->block++;
  1680. spin_unlock_irqrestore(&ev->lock, flags);
  1681. if (cancel)
  1682. cancel_delayed_work_sync(&disk->ev->dwork);
  1683. mutex_unlock(&ev->block_mutex);
  1684. }
  1685. static void __disk_unblock_events(struct gendisk *disk, bool check_now)
  1686. {
  1687. struct disk_events *ev = disk->ev;
  1688. unsigned long intv;
  1689. unsigned long flags;
  1690. spin_lock_irqsave(&ev->lock, flags);
  1691. if (WARN_ON_ONCE(ev->block <= 0))
  1692. goto out_unlock;
  1693. if (--ev->block)
  1694. goto out_unlock;
  1695. intv = disk_events_poll_jiffies(disk);
  1696. if (check_now)
  1697. queue_delayed_work(system_freezable_power_efficient_wq,
  1698. &ev->dwork, 0);
  1699. else if (intv)
  1700. queue_delayed_work(system_freezable_power_efficient_wq,
  1701. &ev->dwork, intv);
  1702. out_unlock:
  1703. spin_unlock_irqrestore(&ev->lock, flags);
  1704. }
  1705. /**
  1706. * disk_unblock_events - unblock disk event checking
  1707. * @disk: disk to unblock events for
  1708. *
  1709. * Undo disk_block_events(). When the block count reaches zero, it
  1710. * starts events polling if configured.
  1711. *
  1712. * CONTEXT:
  1713. * Don't care. Safe to call from irq context.
  1714. */
  1715. void disk_unblock_events(struct gendisk *disk)
  1716. {
  1717. if (disk->ev)
  1718. __disk_unblock_events(disk, false);
  1719. }
  1720. /**
  1721. * disk_flush_events - schedule immediate event checking and flushing
  1722. * @disk: disk to check and flush events for
  1723. * @mask: events to flush
  1724. *
  1725. * Schedule immediate event checking on @disk if not blocked. Events in
  1726. * @mask are scheduled to be cleared from the driver. Note that this
  1727. * doesn't clear the events from @disk->ev.
  1728. *
  1729. * CONTEXT:
  1730. * If @mask is non-zero must be called with bdev->bd_mutex held.
  1731. */
  1732. void disk_flush_events(struct gendisk *disk, unsigned int mask)
  1733. {
  1734. struct disk_events *ev = disk->ev;
  1735. if (!ev)
  1736. return;
  1737. spin_lock_irq(&ev->lock);
  1738. ev->clearing |= mask;
  1739. if (!ev->block)
  1740. mod_delayed_work(system_freezable_power_efficient_wq,
  1741. &ev->dwork, 0);
  1742. spin_unlock_irq(&ev->lock);
  1743. }
  1744. /**
  1745. * disk_clear_events - synchronously check, clear and return pending events
  1746. * @disk: disk to fetch and clear events from
  1747. * @mask: mask of events to be fetched and cleared
  1748. *
  1749. * Disk events are synchronously checked and pending events in @mask
  1750. * are cleared and returned. This ignores the block count.
  1751. *
  1752. * CONTEXT:
  1753. * Might sleep.
  1754. */
  1755. static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
  1756. {
  1757. struct disk_events *ev = disk->ev;
  1758. unsigned int pending;
  1759. unsigned int clearing = mask;
  1760. if (!ev)
  1761. return 0;
  1762. disk_block_events(disk);
  1763. /*
  1764. * store the union of mask and ev->clearing on the stack so that the
  1765. * race with disk_flush_events does not cause ambiguity (ev->clearing
  1766. * can still be modified even if events are blocked).
  1767. */
  1768. spin_lock_irq(&ev->lock);
  1769. clearing |= ev->clearing;
  1770. ev->clearing = 0;
  1771. spin_unlock_irq(&ev->lock);
  1772. disk_check_events(ev, &clearing);
  1773. /*
  1774. * if ev->clearing is not 0, the disk_flush_events got called in the
  1775. * middle of this function, so we want to run the workfn without delay.
  1776. */
  1777. __disk_unblock_events(disk, ev->clearing ? true : false);
  1778. /* then, fetch and clear pending events */
  1779. spin_lock_irq(&ev->lock);
  1780. pending = ev->pending & mask;
  1781. ev->pending &= ~mask;
  1782. spin_unlock_irq(&ev->lock);
  1783. WARN_ON_ONCE(clearing & mask);
  1784. return pending;
  1785. }
  1786. /**
  1787. * bdev_check_media_change - check if a removable media has been changed
  1788. * @bdev: block device to check
  1789. *
  1790. * Check whether a removable media has been changed, and attempt to free all
  1791. * dentries and inodes and invalidates all block device page cache entries in
  1792. * that case.
  1793. *
  1794. * Returns %true if the block device changed, or %false if not.
  1795. */
  1796. bool bdev_check_media_change(struct block_device *bdev)
  1797. {
  1798. unsigned int events;
  1799. events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
  1800. DISK_EVENT_EJECT_REQUEST);
  1801. if (!(events & DISK_EVENT_MEDIA_CHANGE))
  1802. return false;
  1803. if (__invalidate_device(bdev, true))
  1804. pr_warn("VFS: busy inodes on changed media %s\n",
  1805. bdev->bd_disk->disk_name);
  1806. set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
  1807. return true;
  1808. }
  1809. EXPORT_SYMBOL(bdev_check_media_change);
  1810. /*
  1811. * Separate this part out so that a different pointer for clearing_ptr can be
  1812. * passed in for disk_clear_events.
  1813. */
  1814. static void disk_events_workfn(struct work_struct *work)
  1815. {
  1816. struct delayed_work *dwork = to_delayed_work(work);
  1817. struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
  1818. disk_check_events(ev, &ev->clearing);
  1819. }
  1820. static void disk_check_events(struct disk_events *ev,
  1821. unsigned int *clearing_ptr)
  1822. {
  1823. struct gendisk *disk = ev->disk;
  1824. char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
  1825. unsigned int clearing = *clearing_ptr;
  1826. unsigned int events;
  1827. unsigned long intv;
  1828. int nr_events = 0, i;
  1829. /* check events */
  1830. events = disk->fops->check_events(disk, clearing);
  1831. /* accumulate pending events and schedule next poll if necessary */
  1832. spin_lock_irq(&ev->lock);
  1833. events &= ~ev->pending;
  1834. ev->pending |= events;
  1835. *clearing_ptr &= ~clearing;
  1836. intv = disk_events_poll_jiffies(disk);
  1837. if (!ev->block && intv)
  1838. queue_delayed_work(system_freezable_power_efficient_wq,
  1839. &ev->dwork, intv);
  1840. spin_unlock_irq(&ev->lock);
  1841. /*
  1842. * Tell userland about new events. Only the events listed in
  1843. * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
  1844. * is set. Otherwise, events are processed internally but never
  1845. * get reported to userland.
  1846. */
  1847. for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
  1848. if ((events & disk->events & (1 << i)) &&
  1849. (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
  1850. envp[nr_events++] = disk_uevents[i];
  1851. if (nr_events)
  1852. kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
  1853. }
  1854. /*
  1855. * A disk events enabled device has the following sysfs nodes under
  1856. * its /sys/block/X/ directory.
  1857. *
  1858. * events : list of all supported events
  1859. * events_async : list of events which can be detected w/o polling
  1860. * (always empty, only for backwards compatibility)
  1861. * events_poll_msecs : polling interval, 0: disable, -1: system default
  1862. */
  1863. static ssize_t __disk_events_show(unsigned int events, char *buf)
  1864. {
  1865. const char *delim = "";
  1866. ssize_t pos = 0;
  1867. int i;
  1868. for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
  1869. if (events & (1 << i)) {
  1870. pos += sprintf(buf + pos, "%s%s",
  1871. delim, disk_events_strs[i]);
  1872. delim = " ";
  1873. }
  1874. if (pos)
  1875. pos += sprintf(buf + pos, "\n");
  1876. return pos;
  1877. }
  1878. static ssize_t disk_events_show(struct device *dev,
  1879. struct device_attribute *attr, char *buf)
  1880. {
  1881. struct gendisk *disk = dev_to_disk(dev);
  1882. if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
  1883. return 0;
  1884. return __disk_events_show(disk->events, buf);
  1885. }
  1886. static ssize_t disk_events_async_show(struct device *dev,
  1887. struct device_attribute *attr, char *buf)
  1888. {
  1889. return 0;
  1890. }
  1891. static ssize_t disk_events_poll_msecs_show(struct device *dev,
  1892. struct device_attribute *attr,
  1893. char *buf)
  1894. {
  1895. struct gendisk *disk = dev_to_disk(dev);
  1896. if (!disk->ev)
  1897. return sprintf(buf, "-1\n");
  1898. return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
  1899. }
  1900. static ssize_t disk_events_poll_msecs_store(struct device *dev,
  1901. struct device_attribute *attr,
  1902. const char *buf, size_t count)
  1903. {
  1904. struct gendisk *disk = dev_to_disk(dev);
  1905. long intv;
  1906. if (!count || !sscanf(buf, "%ld", &intv))
  1907. return -EINVAL;
  1908. if (intv < 0 && intv != -1)
  1909. return -EINVAL;
  1910. if (!disk->ev)
  1911. return -ENODEV;
  1912. disk_block_events(disk);
  1913. disk->ev->poll_msecs = intv;
  1914. __disk_unblock_events(disk, true);
  1915. return count;
  1916. }
  1917. static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
  1918. static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
  1919. static const DEVICE_ATTR(events_poll_msecs, 0644,
  1920. disk_events_poll_msecs_show,
  1921. disk_events_poll_msecs_store);
  1922. static const struct attribute *disk_events_attrs[] = {
  1923. &dev_attr_events.attr,
  1924. &dev_attr_events_async.attr,
  1925. &dev_attr_events_poll_msecs.attr,
  1926. NULL,
  1927. };
  1928. /*
  1929. * The default polling interval can be specified by the kernel
  1930. * parameter block.events_dfl_poll_msecs which defaults to 0
  1931. * (disable). This can also be modified runtime by writing to
  1932. * /sys/module/block/parameters/events_dfl_poll_msecs.
  1933. */
  1934. static int disk_events_set_dfl_poll_msecs(const char *val,
  1935. const struct kernel_param *kp)
  1936. {
  1937. struct disk_events *ev;
  1938. int ret;
  1939. ret = param_set_ulong(val, kp);
  1940. if (ret < 0)
  1941. return ret;
  1942. mutex_lock(&disk_events_mutex);
  1943. list_for_each_entry(ev, &disk_events, node)
  1944. disk_flush_events(ev->disk, 0);
  1945. mutex_unlock(&disk_events_mutex);
  1946. return 0;
  1947. }
  1948. static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
  1949. .set = disk_events_set_dfl_poll_msecs,
  1950. .get = param_get_ulong,
  1951. };
  1952. #undef MODULE_PARAM_PREFIX
  1953. #define MODULE_PARAM_PREFIX "block."
  1954. module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
  1955. &disk_events_dfl_poll_msecs, 0644);
  1956. /*
  1957. * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
  1958. */
  1959. static void disk_alloc_events(struct gendisk *disk)
  1960. {
  1961. struct disk_events *ev;
  1962. if (!disk->fops->check_events || !disk->events)
  1963. return;
  1964. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  1965. if (!ev) {
  1966. pr_warn("%s: failed to initialize events\n", disk->disk_name);
  1967. return;
  1968. }
  1969. INIT_LIST_HEAD(&ev->node);
  1970. ev->disk = disk;
  1971. spin_lock_init(&ev->lock);
  1972. mutex_init(&ev->block_mutex);
  1973. ev->block = 1;
  1974. ev->poll_msecs = -1;
  1975. INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
  1976. disk->ev = ev;
  1977. }
  1978. static void disk_add_events(struct gendisk *disk)
  1979. {
  1980. /* FIXME: error handling */
  1981. if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
  1982. pr_warn("%s: failed to create sysfs files for events\n",
  1983. disk->disk_name);
  1984. if (!disk->ev)
  1985. return;
  1986. mutex_lock(&disk_events_mutex);
  1987. list_add_tail(&disk->ev->node, &disk_events);
  1988. mutex_unlock(&disk_events_mutex);
  1989. /*
  1990. * Block count is initialized to 1 and the following initial
  1991. * unblock kicks it into action.
  1992. */
  1993. __disk_unblock_events(disk, true);
  1994. }
  1995. static void disk_del_events(struct gendisk *disk)
  1996. {
  1997. if (disk->ev) {
  1998. disk_block_events(disk);
  1999. mutex_lock(&disk_events_mutex);
  2000. list_del_init(&disk->ev->node);
  2001. mutex_unlock(&disk_events_mutex);
  2002. }
  2003. sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
  2004. }
  2005. static void disk_release_events(struct gendisk *disk)
  2006. {
  2007. /* the block count should be 1 from disk_del_events() */
  2008. WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
  2009. kfree(disk->ev);
  2010. }