dm-ioctl.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /*
  2. * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm.h"
  8. #include <linux/module.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/init.h>
  12. #include <linux/wait.h>
  13. #include <linux/slab.h>
  14. #include <linux/dm-ioctl.h>
  15. #include <linux/hdreg.h>
  16. #include <asm/uaccess.h>
  17. #define DM_MSG_PREFIX "ioctl"
  18. #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
  19. /*-----------------------------------------------------------------
  20. * The ioctl interface needs to be able to look up devices by
  21. * name or uuid.
  22. *---------------------------------------------------------------*/
  23. struct hash_cell {
  24. struct list_head name_list;
  25. struct list_head uuid_list;
  26. char *name;
  27. char *uuid;
  28. struct mapped_device *md;
  29. struct dm_table *new_map;
  30. };
  31. struct vers_iter {
  32. size_t param_size;
  33. struct dm_target_versions *vers, *old_vers;
  34. char *end;
  35. uint32_t flags;
  36. };
  37. #define NUM_BUCKETS 64
  38. #define MASK_BUCKETS (NUM_BUCKETS - 1)
  39. static struct list_head _name_buckets[NUM_BUCKETS];
  40. static struct list_head _uuid_buckets[NUM_BUCKETS];
  41. static void dm_hash_remove_all(int keep_open_devices);
  42. /*
  43. * Guards access to both hash tables.
  44. */
  45. static DECLARE_RWSEM(_hash_lock);
  46. static void init_buckets(struct list_head *buckets)
  47. {
  48. unsigned int i;
  49. for (i = 0; i < NUM_BUCKETS; i++)
  50. INIT_LIST_HEAD(buckets + i);
  51. }
  52. static int dm_hash_init(void)
  53. {
  54. init_buckets(_name_buckets);
  55. init_buckets(_uuid_buckets);
  56. return 0;
  57. }
  58. static void dm_hash_exit(void)
  59. {
  60. dm_hash_remove_all(0);
  61. }
  62. /*-----------------------------------------------------------------
  63. * Hash function:
  64. * We're not really concerned with the str hash function being
  65. * fast since it's only used by the ioctl interface.
  66. *---------------------------------------------------------------*/
  67. static unsigned int hash_str(const char *str)
  68. {
  69. const unsigned int hash_mult = 2654435387U;
  70. unsigned int h = 0;
  71. while (*str)
  72. h = (h + (unsigned int) *str++) * hash_mult;
  73. return h & MASK_BUCKETS;
  74. }
  75. /*-----------------------------------------------------------------
  76. * Code for looking up a device by name
  77. *---------------------------------------------------------------*/
  78. static struct hash_cell *__get_name_cell(const char *str)
  79. {
  80. struct hash_cell *hc;
  81. unsigned int h = hash_str(str);
  82. list_for_each_entry (hc, _name_buckets + h, name_list)
  83. if (!strcmp(hc->name, str)) {
  84. dm_get(hc->md);
  85. return hc;
  86. }
  87. return NULL;
  88. }
  89. static struct hash_cell *__get_uuid_cell(const char *str)
  90. {
  91. struct hash_cell *hc;
  92. unsigned int h = hash_str(str);
  93. list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
  94. if (!strcmp(hc->uuid, str)) {
  95. dm_get(hc->md);
  96. return hc;
  97. }
  98. return NULL;
  99. }
  100. /*-----------------------------------------------------------------
  101. * Inserting, removing and renaming a device.
  102. *---------------------------------------------------------------*/
  103. static struct hash_cell *alloc_cell(const char *name, const char *uuid,
  104. struct mapped_device *md)
  105. {
  106. struct hash_cell *hc;
  107. hc = kmalloc(sizeof(*hc), GFP_KERNEL);
  108. if (!hc)
  109. return NULL;
  110. hc->name = kstrdup(name, GFP_KERNEL);
  111. if (!hc->name) {
  112. kfree(hc);
  113. return NULL;
  114. }
  115. if (!uuid)
  116. hc->uuid = NULL;
  117. else {
  118. hc->uuid = kstrdup(uuid, GFP_KERNEL);
  119. if (!hc->uuid) {
  120. kfree(hc->name);
  121. kfree(hc);
  122. return NULL;
  123. }
  124. }
  125. INIT_LIST_HEAD(&hc->name_list);
  126. INIT_LIST_HEAD(&hc->uuid_list);
  127. hc->md = md;
  128. hc->new_map = NULL;
  129. return hc;
  130. }
  131. static void free_cell(struct hash_cell *hc)
  132. {
  133. if (hc) {
  134. kfree(hc->name);
  135. kfree(hc->uuid);
  136. kfree(hc);
  137. }
  138. }
  139. /*
  140. * The kdev_t and uuid of a device can never change once it is
  141. * initially inserted.
  142. */
  143. static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
  144. {
  145. struct hash_cell *cell, *hc;
  146. /*
  147. * Allocate the new cells.
  148. */
  149. cell = alloc_cell(name, uuid, md);
  150. if (!cell)
  151. return -ENOMEM;
  152. /*
  153. * Insert the cell into both hash tables.
  154. */
  155. down_write(&_hash_lock);
  156. hc = __get_name_cell(name);
  157. if (hc) {
  158. dm_put(hc->md);
  159. goto bad;
  160. }
  161. list_add(&cell->name_list, _name_buckets + hash_str(name));
  162. if (uuid) {
  163. hc = __get_uuid_cell(uuid);
  164. if (hc) {
  165. list_del(&cell->name_list);
  166. dm_put(hc->md);
  167. goto bad;
  168. }
  169. list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
  170. }
  171. dm_get(md);
  172. dm_set_mdptr(md, cell);
  173. up_write(&_hash_lock);
  174. return 0;
  175. bad:
  176. up_write(&_hash_lock);
  177. free_cell(cell);
  178. return -EBUSY;
  179. }
  180. static void __hash_remove(struct hash_cell *hc)
  181. {
  182. struct dm_table *table;
  183. /* remove from the dev hash */
  184. list_del(&hc->uuid_list);
  185. list_del(&hc->name_list);
  186. dm_set_mdptr(hc->md, NULL);
  187. table = dm_get_table(hc->md);
  188. if (table) {
  189. dm_table_event(table);
  190. dm_table_put(table);
  191. }
  192. if (hc->new_map)
  193. dm_table_put(hc->new_map);
  194. dm_put(hc->md);
  195. free_cell(hc);
  196. }
  197. static void dm_hash_remove_all(int keep_open_devices)
  198. {
  199. int i, dev_skipped, dev_removed;
  200. struct hash_cell *hc;
  201. struct list_head *tmp, *n;
  202. down_write(&_hash_lock);
  203. retry:
  204. dev_skipped = dev_removed = 0;
  205. for (i = 0; i < NUM_BUCKETS; i++) {
  206. list_for_each_safe (tmp, n, _name_buckets + i) {
  207. hc = list_entry(tmp, struct hash_cell, name_list);
  208. if (keep_open_devices &&
  209. dm_lock_for_deletion(hc->md)) {
  210. dev_skipped++;
  211. continue;
  212. }
  213. __hash_remove(hc);
  214. dev_removed = 1;
  215. }
  216. }
  217. /*
  218. * Some mapped devices may be using other mapped devices, so if any
  219. * still exist, repeat until we make no further progress.
  220. */
  221. if (dev_skipped) {
  222. if (dev_removed)
  223. goto retry;
  224. DMWARN("remove_all left %d open device(s)", dev_skipped);
  225. }
  226. up_write(&_hash_lock);
  227. }
  228. static int dm_hash_rename(const char *old, const char *new)
  229. {
  230. char *new_name, *old_name;
  231. struct hash_cell *hc;
  232. struct dm_table *table;
  233. /*
  234. * duplicate new.
  235. */
  236. new_name = kstrdup(new, GFP_KERNEL);
  237. if (!new_name)
  238. return -ENOMEM;
  239. down_write(&_hash_lock);
  240. /*
  241. * Is new free ?
  242. */
  243. hc = __get_name_cell(new);
  244. if (hc) {
  245. DMWARN("asked to rename to an already existing name %s -> %s",
  246. old, new);
  247. dm_put(hc->md);
  248. up_write(&_hash_lock);
  249. kfree(new_name);
  250. return -EBUSY;
  251. }
  252. /*
  253. * Is there such a device as 'old' ?
  254. */
  255. hc = __get_name_cell(old);
  256. if (!hc) {
  257. DMWARN("asked to rename a non existent device %s -> %s",
  258. old, new);
  259. up_write(&_hash_lock);
  260. kfree(new_name);
  261. return -ENXIO;
  262. }
  263. /*
  264. * rename and move the name cell.
  265. */
  266. list_del(&hc->name_list);
  267. old_name = hc->name;
  268. hc->name = new_name;
  269. list_add(&hc->name_list, _name_buckets + hash_str(new_name));
  270. /*
  271. * Wake up any dm event waiters.
  272. */
  273. table = dm_get_table(hc->md);
  274. if (table) {
  275. dm_table_event(table);
  276. dm_table_put(table);
  277. }
  278. dm_kobject_uevent(hc->md);
  279. dm_put(hc->md);
  280. up_write(&_hash_lock);
  281. kfree(old_name);
  282. return 0;
  283. }
  284. /*-----------------------------------------------------------------
  285. * Implementation of the ioctl commands
  286. *---------------------------------------------------------------*/
  287. /*
  288. * All the ioctl commands get dispatched to functions with this
  289. * prototype.
  290. */
  291. typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
  292. static int remove_all(struct dm_ioctl *param, size_t param_size)
  293. {
  294. dm_hash_remove_all(1);
  295. param->data_size = 0;
  296. return 0;
  297. }
  298. /*
  299. * Round up the ptr to an 8-byte boundary.
  300. */
  301. #define ALIGN_MASK 7
  302. static inline void *align_ptr(void *ptr)
  303. {
  304. return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
  305. }
  306. /*
  307. * Retrieves the data payload buffer from an already allocated
  308. * struct dm_ioctl.
  309. */
  310. static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
  311. size_t *len)
  312. {
  313. param->data_start = align_ptr(param + 1) - (void *) param;
  314. if (param->data_start < param_size)
  315. *len = param_size - param->data_start;
  316. else
  317. *len = 0;
  318. return ((void *) param) + param->data_start;
  319. }
  320. static int list_devices(struct dm_ioctl *param, size_t param_size)
  321. {
  322. unsigned int i;
  323. struct hash_cell *hc;
  324. size_t len, needed = 0;
  325. struct gendisk *disk;
  326. struct dm_name_list *nl, *old_nl = NULL;
  327. down_write(&_hash_lock);
  328. /*
  329. * Loop through all the devices working out how much
  330. * space we need.
  331. */
  332. for (i = 0; i < NUM_BUCKETS; i++) {
  333. list_for_each_entry (hc, _name_buckets + i, name_list) {
  334. needed += sizeof(struct dm_name_list);
  335. needed += strlen(hc->name) + 1;
  336. needed += ALIGN_MASK;
  337. }
  338. }
  339. /*
  340. * Grab our output buffer.
  341. */
  342. nl = get_result_buffer(param, param_size, &len);
  343. if (len < needed) {
  344. param->flags |= DM_BUFFER_FULL_FLAG;
  345. goto out;
  346. }
  347. param->data_size = param->data_start + needed;
  348. nl->dev = 0; /* Flags no data */
  349. /*
  350. * Now loop through filling out the names.
  351. */
  352. for (i = 0; i < NUM_BUCKETS; i++) {
  353. list_for_each_entry (hc, _name_buckets + i, name_list) {
  354. if (old_nl)
  355. old_nl->next = (uint32_t) ((void *) nl -
  356. (void *) old_nl);
  357. disk = dm_disk(hc->md);
  358. nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
  359. nl->next = 0;
  360. strcpy(nl->name, hc->name);
  361. old_nl = nl;
  362. nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
  363. }
  364. }
  365. out:
  366. up_write(&_hash_lock);
  367. return 0;
  368. }
  369. static void list_version_get_needed(struct target_type *tt, void *needed_param)
  370. {
  371. size_t *needed = needed_param;
  372. *needed += sizeof(struct dm_target_versions);
  373. *needed += strlen(tt->name);
  374. *needed += ALIGN_MASK;
  375. }
  376. static void list_version_get_info(struct target_type *tt, void *param)
  377. {
  378. struct vers_iter *info = param;
  379. /* Check space - it might have changed since the first iteration */
  380. if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
  381. info->end) {
  382. info->flags = DM_BUFFER_FULL_FLAG;
  383. return;
  384. }
  385. if (info->old_vers)
  386. info->old_vers->next = (uint32_t) ((void *)info->vers -
  387. (void *)info->old_vers);
  388. info->vers->version[0] = tt->version[0];
  389. info->vers->version[1] = tt->version[1];
  390. info->vers->version[2] = tt->version[2];
  391. info->vers->next = 0;
  392. strcpy(info->vers->name, tt->name);
  393. info->old_vers = info->vers;
  394. info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
  395. }
  396. static int list_versions(struct dm_ioctl *param, size_t param_size)
  397. {
  398. size_t len, needed = 0;
  399. struct dm_target_versions *vers;
  400. struct vers_iter iter_info;
  401. /*
  402. * Loop through all the devices working out how much
  403. * space we need.
  404. */
  405. dm_target_iterate(list_version_get_needed, &needed);
  406. /*
  407. * Grab our output buffer.
  408. */
  409. vers = get_result_buffer(param, param_size, &len);
  410. if (len < needed) {
  411. param->flags |= DM_BUFFER_FULL_FLAG;
  412. goto out;
  413. }
  414. param->data_size = param->data_start + needed;
  415. iter_info.param_size = param_size;
  416. iter_info.old_vers = NULL;
  417. iter_info.vers = vers;
  418. iter_info.flags = 0;
  419. iter_info.end = (char *)vers+len;
  420. /*
  421. * Now loop through filling out the names & versions.
  422. */
  423. dm_target_iterate(list_version_get_info, &iter_info);
  424. param->flags |= iter_info.flags;
  425. out:
  426. return 0;
  427. }
  428. static int check_name(const char *name)
  429. {
  430. if (strchr(name, '/')) {
  431. DMWARN("invalid device name");
  432. return -EINVAL;
  433. }
  434. return 0;
  435. }
  436. /*
  437. * Fills in a dm_ioctl structure, ready for sending back to
  438. * userland.
  439. */
  440. static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
  441. {
  442. struct gendisk *disk = dm_disk(md);
  443. struct dm_table *table;
  444. param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
  445. DM_ACTIVE_PRESENT_FLAG);
  446. if (dm_suspended(md))
  447. param->flags |= DM_SUSPEND_FLAG;
  448. param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
  449. /*
  450. * Yes, this will be out of date by the time it gets back
  451. * to userland, but it is still very useful for
  452. * debugging.
  453. */
  454. param->open_count = dm_open_count(md);
  455. if (disk->policy)
  456. param->flags |= DM_READONLY_FLAG;
  457. param->event_nr = dm_get_event_nr(md);
  458. table = dm_get_table(md);
  459. if (table) {
  460. param->flags |= DM_ACTIVE_PRESENT_FLAG;
  461. param->target_count = dm_table_get_num_targets(table);
  462. dm_table_put(table);
  463. } else
  464. param->target_count = 0;
  465. return 0;
  466. }
  467. static int dev_create(struct dm_ioctl *param, size_t param_size)
  468. {
  469. int r, m = DM_ANY_MINOR;
  470. struct mapped_device *md;
  471. r = check_name(param->name);
  472. if (r)
  473. return r;
  474. if (param->flags & DM_PERSISTENT_DEV_FLAG)
  475. m = MINOR(huge_decode_dev(param->dev));
  476. r = dm_create(m, &md);
  477. if (r)
  478. return r;
  479. r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
  480. if (r) {
  481. dm_put(md);
  482. return r;
  483. }
  484. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  485. r = __dev_status(md, param);
  486. dm_put(md);
  487. return r;
  488. }
  489. /*
  490. * Always use UUID for lookups if it's present, otherwise use name or dev.
  491. */
  492. static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
  493. {
  494. struct mapped_device *md;
  495. void *mdptr = NULL;
  496. if (*param->uuid)
  497. return __get_uuid_cell(param->uuid);
  498. if (*param->name)
  499. return __get_name_cell(param->name);
  500. md = dm_get_md(huge_decode_dev(param->dev));
  501. if (!md)
  502. goto out;
  503. mdptr = dm_get_mdptr(md);
  504. if (!mdptr)
  505. dm_put(md);
  506. out:
  507. return mdptr;
  508. }
  509. static struct mapped_device *find_device(struct dm_ioctl *param)
  510. {
  511. struct hash_cell *hc;
  512. struct mapped_device *md = NULL;
  513. down_read(&_hash_lock);
  514. hc = __find_device_hash_cell(param);
  515. if (hc) {
  516. md = hc->md;
  517. /*
  518. * Sneakily write in both the name and the uuid
  519. * while we have the cell.
  520. */
  521. strncpy(param->name, hc->name, sizeof(param->name));
  522. if (hc->uuid)
  523. strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
  524. else
  525. param->uuid[0] = '\0';
  526. if (hc->new_map)
  527. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  528. else
  529. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  530. }
  531. up_read(&_hash_lock);
  532. return md;
  533. }
  534. static int dev_remove(struct dm_ioctl *param, size_t param_size)
  535. {
  536. struct hash_cell *hc;
  537. struct mapped_device *md;
  538. int r;
  539. down_write(&_hash_lock);
  540. hc = __find_device_hash_cell(param);
  541. if (!hc) {
  542. DMWARN("device doesn't appear to be in the dev hash table.");
  543. up_write(&_hash_lock);
  544. return -ENXIO;
  545. }
  546. md = hc->md;
  547. /*
  548. * Ensure the device is not open and nothing further can open it.
  549. */
  550. r = dm_lock_for_deletion(md);
  551. if (r) {
  552. DMWARN("unable to remove open device %s", hc->name);
  553. up_write(&_hash_lock);
  554. dm_put(md);
  555. return r;
  556. }
  557. __hash_remove(hc);
  558. up_write(&_hash_lock);
  559. dm_put(md);
  560. param->data_size = 0;
  561. return 0;
  562. }
  563. /*
  564. * Check a string doesn't overrun the chunk of
  565. * memory we copied from userland.
  566. */
  567. static int invalid_str(char *str, void *end)
  568. {
  569. while ((void *) str < end)
  570. if (!*str++)
  571. return 0;
  572. return -EINVAL;
  573. }
  574. static int dev_rename(struct dm_ioctl *param, size_t param_size)
  575. {
  576. int r;
  577. char *new_name = (char *) param + param->data_start;
  578. if (new_name < (char *) param->data ||
  579. invalid_str(new_name, (void *) param + param_size)) {
  580. DMWARN("Invalid new logical volume name supplied.");
  581. return -EINVAL;
  582. }
  583. r = check_name(new_name);
  584. if (r)
  585. return r;
  586. param->data_size = 0;
  587. return dm_hash_rename(param->name, new_name);
  588. }
  589. static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
  590. {
  591. int r = -EINVAL, x;
  592. struct mapped_device *md;
  593. struct hd_geometry geometry;
  594. unsigned long indata[4];
  595. char *geostr = (char *) param + param->data_start;
  596. md = find_device(param);
  597. if (!md)
  598. return -ENXIO;
  599. if (geostr < (char *) param->data ||
  600. invalid_str(geostr, (void *) param + param_size)) {
  601. DMWARN("Invalid geometry supplied.");
  602. goto out;
  603. }
  604. x = sscanf(geostr, "%lu %lu %lu %lu", indata,
  605. indata + 1, indata + 2, indata + 3);
  606. if (x != 4) {
  607. DMWARN("Unable to interpret geometry settings.");
  608. goto out;
  609. }
  610. if (indata[0] > 65535 || indata[1] > 255 ||
  611. indata[2] > 255 || indata[3] > ULONG_MAX) {
  612. DMWARN("Geometry exceeds range limits.");
  613. goto out;
  614. }
  615. geometry.cylinders = indata[0];
  616. geometry.heads = indata[1];
  617. geometry.sectors = indata[2];
  618. geometry.start = indata[3];
  619. r = dm_set_geometry(md, &geometry);
  620. if (!r)
  621. r = __dev_status(md, param);
  622. param->data_size = 0;
  623. out:
  624. dm_put(md);
  625. return r;
  626. }
  627. static int do_suspend(struct dm_ioctl *param)
  628. {
  629. int r = 0;
  630. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  631. struct mapped_device *md;
  632. md = find_device(param);
  633. if (!md)
  634. return -ENXIO;
  635. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  636. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  637. if (param->flags & DM_NOFLUSH_FLAG)
  638. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  639. if (!dm_suspended(md))
  640. r = dm_suspend(md, suspend_flags);
  641. if (!r)
  642. r = __dev_status(md, param);
  643. dm_put(md);
  644. return r;
  645. }
  646. static int do_resume(struct dm_ioctl *param)
  647. {
  648. int r = 0;
  649. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  650. struct hash_cell *hc;
  651. struct mapped_device *md;
  652. struct dm_table *new_map;
  653. down_write(&_hash_lock);
  654. hc = __find_device_hash_cell(param);
  655. if (!hc) {
  656. DMWARN("device doesn't appear to be in the dev hash table.");
  657. up_write(&_hash_lock);
  658. return -ENXIO;
  659. }
  660. md = hc->md;
  661. new_map = hc->new_map;
  662. hc->new_map = NULL;
  663. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  664. up_write(&_hash_lock);
  665. /* Do we need to load a new map ? */
  666. if (new_map) {
  667. /* Suspend if it isn't already suspended */
  668. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  669. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  670. if (param->flags & DM_NOFLUSH_FLAG)
  671. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  672. if (!dm_suspended(md))
  673. dm_suspend(md, suspend_flags);
  674. r = dm_swap_table(md, new_map);
  675. if (r) {
  676. dm_put(md);
  677. dm_table_put(new_map);
  678. return r;
  679. }
  680. if (dm_table_get_mode(new_map) & FMODE_WRITE)
  681. set_disk_ro(dm_disk(md), 0);
  682. else
  683. set_disk_ro(dm_disk(md), 1);
  684. dm_table_put(new_map);
  685. }
  686. if (dm_suspended(md))
  687. r = dm_resume(md);
  688. if (!r)
  689. r = __dev_status(md, param);
  690. dm_put(md);
  691. return r;
  692. }
  693. /*
  694. * Set or unset the suspension state of a device.
  695. * If the device already is in the requested state we just return its status.
  696. */
  697. static int dev_suspend(struct dm_ioctl *param, size_t param_size)
  698. {
  699. if (param->flags & DM_SUSPEND_FLAG)
  700. return do_suspend(param);
  701. return do_resume(param);
  702. }
  703. /*
  704. * Copies device info back to user space, used by
  705. * the create and info ioctls.
  706. */
  707. static int dev_status(struct dm_ioctl *param, size_t param_size)
  708. {
  709. int r;
  710. struct mapped_device *md;
  711. md = find_device(param);
  712. if (!md)
  713. return -ENXIO;
  714. r = __dev_status(md, param);
  715. dm_put(md);
  716. return r;
  717. }
  718. /*
  719. * Build up the status struct for each target
  720. */
  721. static void retrieve_status(struct dm_table *table,
  722. struct dm_ioctl *param, size_t param_size)
  723. {
  724. unsigned int i, num_targets;
  725. struct dm_target_spec *spec;
  726. char *outbuf, *outptr;
  727. status_type_t type;
  728. size_t remaining, len, used = 0;
  729. outptr = outbuf = get_result_buffer(param, param_size, &len);
  730. if (param->flags & DM_STATUS_TABLE_FLAG)
  731. type = STATUSTYPE_TABLE;
  732. else
  733. type = STATUSTYPE_INFO;
  734. /* Get all the target info */
  735. num_targets = dm_table_get_num_targets(table);
  736. for (i = 0; i < num_targets; i++) {
  737. struct dm_target *ti = dm_table_get_target(table, i);
  738. remaining = len - (outptr - outbuf);
  739. if (remaining <= sizeof(struct dm_target_spec)) {
  740. param->flags |= DM_BUFFER_FULL_FLAG;
  741. break;
  742. }
  743. spec = (struct dm_target_spec *) outptr;
  744. spec->status = 0;
  745. spec->sector_start = ti->begin;
  746. spec->length = ti->len;
  747. strncpy(spec->target_type, ti->type->name,
  748. sizeof(spec->target_type));
  749. outptr += sizeof(struct dm_target_spec);
  750. remaining = len - (outptr - outbuf);
  751. if (remaining <= 0) {
  752. param->flags |= DM_BUFFER_FULL_FLAG;
  753. break;
  754. }
  755. /* Get the status/table string from the target driver */
  756. if (ti->type->status) {
  757. if (ti->type->status(ti, type, outptr, remaining)) {
  758. param->flags |= DM_BUFFER_FULL_FLAG;
  759. break;
  760. }
  761. } else
  762. outptr[0] = '\0';
  763. outptr += strlen(outptr) + 1;
  764. used = param->data_start + (outptr - outbuf);
  765. outptr = align_ptr(outptr);
  766. spec->next = outptr - outbuf;
  767. }
  768. if (used)
  769. param->data_size = used;
  770. param->target_count = num_targets;
  771. }
  772. /*
  773. * Wait for a device to report an event
  774. */
  775. static int dev_wait(struct dm_ioctl *param, size_t param_size)
  776. {
  777. int r;
  778. struct mapped_device *md;
  779. struct dm_table *table;
  780. md = find_device(param);
  781. if (!md)
  782. return -ENXIO;
  783. /*
  784. * Wait for a notification event
  785. */
  786. if (dm_wait_event(md, param->event_nr)) {
  787. r = -ERESTARTSYS;
  788. goto out;
  789. }
  790. /*
  791. * The userland program is going to want to know what
  792. * changed to trigger the event, so we may as well tell
  793. * him and save an ioctl.
  794. */
  795. r = __dev_status(md, param);
  796. if (r)
  797. goto out;
  798. table = dm_get_table(md);
  799. if (table) {
  800. retrieve_status(table, param, param_size);
  801. dm_table_put(table);
  802. }
  803. out:
  804. dm_put(md);
  805. return r;
  806. }
  807. static inline int get_mode(struct dm_ioctl *param)
  808. {
  809. int mode = FMODE_READ | FMODE_WRITE;
  810. if (param->flags & DM_READONLY_FLAG)
  811. mode = FMODE_READ;
  812. return mode;
  813. }
  814. static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
  815. struct dm_target_spec **spec, char **target_params)
  816. {
  817. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  818. *target_params = (char *) (*spec + 1);
  819. if (*spec < (last + 1))
  820. return -EINVAL;
  821. return invalid_str(*target_params, end);
  822. }
  823. static int populate_table(struct dm_table *table,
  824. struct dm_ioctl *param, size_t param_size)
  825. {
  826. int r;
  827. unsigned int i = 0;
  828. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  829. uint32_t next = param->data_start;
  830. void *end = (void *) param + param_size;
  831. char *target_params;
  832. if (!param->target_count) {
  833. DMWARN("populate_table: no targets specified");
  834. return -EINVAL;
  835. }
  836. for (i = 0; i < param->target_count; i++) {
  837. r = next_target(spec, next, end, &spec, &target_params);
  838. if (r) {
  839. DMWARN("unable to find target");
  840. return r;
  841. }
  842. r = dm_table_add_target(table, spec->target_type,
  843. (sector_t) spec->sector_start,
  844. (sector_t) spec->length,
  845. target_params);
  846. if (r) {
  847. DMWARN("error adding target to table");
  848. return r;
  849. }
  850. next = spec->next;
  851. }
  852. return dm_table_complete(table);
  853. }
  854. static int table_load(struct dm_ioctl *param, size_t param_size)
  855. {
  856. int r;
  857. struct hash_cell *hc;
  858. struct dm_table *t;
  859. struct mapped_device *md;
  860. md = find_device(param);
  861. if (!md)
  862. return -ENXIO;
  863. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  864. if (r)
  865. goto out;
  866. r = populate_table(t, param, param_size);
  867. if (r) {
  868. dm_table_put(t);
  869. goto out;
  870. }
  871. down_write(&_hash_lock);
  872. hc = dm_get_mdptr(md);
  873. if (!hc || hc->md != md) {
  874. DMWARN("device has been removed from the dev hash table.");
  875. dm_table_put(t);
  876. up_write(&_hash_lock);
  877. r = -ENXIO;
  878. goto out;
  879. }
  880. if (hc->new_map)
  881. dm_table_put(hc->new_map);
  882. hc->new_map = t;
  883. up_write(&_hash_lock);
  884. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  885. r = __dev_status(md, param);
  886. out:
  887. dm_put(md);
  888. return r;
  889. }
  890. static int table_clear(struct dm_ioctl *param, size_t param_size)
  891. {
  892. int r;
  893. struct hash_cell *hc;
  894. struct mapped_device *md;
  895. down_write(&_hash_lock);
  896. hc = __find_device_hash_cell(param);
  897. if (!hc) {
  898. DMWARN("device doesn't appear to be in the dev hash table.");
  899. up_write(&_hash_lock);
  900. return -ENXIO;
  901. }
  902. if (hc->new_map) {
  903. dm_table_put(hc->new_map);
  904. hc->new_map = NULL;
  905. }
  906. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  907. r = __dev_status(hc->md, param);
  908. md = hc->md;
  909. up_write(&_hash_lock);
  910. dm_put(md);
  911. return r;
  912. }
  913. /*
  914. * Retrieves a list of devices used by a particular dm device.
  915. */
  916. static void retrieve_deps(struct dm_table *table,
  917. struct dm_ioctl *param, size_t param_size)
  918. {
  919. unsigned int count = 0;
  920. struct list_head *tmp;
  921. size_t len, needed;
  922. struct dm_dev *dd;
  923. struct dm_target_deps *deps;
  924. deps = get_result_buffer(param, param_size, &len);
  925. /*
  926. * Count the devices.
  927. */
  928. list_for_each (tmp, dm_table_get_devices(table))
  929. count++;
  930. /*
  931. * Check we have enough space.
  932. */
  933. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  934. if (len < needed) {
  935. param->flags |= DM_BUFFER_FULL_FLAG;
  936. return;
  937. }
  938. /*
  939. * Fill in the devices.
  940. */
  941. deps->count = count;
  942. count = 0;
  943. list_for_each_entry (dd, dm_table_get_devices(table), list)
  944. deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
  945. param->data_size = param->data_start + needed;
  946. }
  947. static int table_deps(struct dm_ioctl *param, size_t param_size)
  948. {
  949. int r = 0;
  950. struct mapped_device *md;
  951. struct dm_table *table;
  952. md = find_device(param);
  953. if (!md)
  954. return -ENXIO;
  955. r = __dev_status(md, param);
  956. if (r)
  957. goto out;
  958. table = dm_get_table(md);
  959. if (table) {
  960. retrieve_deps(table, param, param_size);
  961. dm_table_put(table);
  962. }
  963. out:
  964. dm_put(md);
  965. return r;
  966. }
  967. /*
  968. * Return the status of a device as a text string for each
  969. * target.
  970. */
  971. static int table_status(struct dm_ioctl *param, size_t param_size)
  972. {
  973. int r;
  974. struct mapped_device *md;
  975. struct dm_table *table;
  976. md = find_device(param);
  977. if (!md)
  978. return -ENXIO;
  979. r = __dev_status(md, param);
  980. if (r)
  981. goto out;
  982. table = dm_get_table(md);
  983. if (table) {
  984. retrieve_status(table, param, param_size);
  985. dm_table_put(table);
  986. }
  987. out:
  988. dm_put(md);
  989. return r;
  990. }
  991. /*
  992. * Pass a message to the target that's at the supplied device offset.
  993. */
  994. static int target_message(struct dm_ioctl *param, size_t param_size)
  995. {
  996. int r, argc;
  997. char **argv;
  998. struct mapped_device *md;
  999. struct dm_table *table;
  1000. struct dm_target *ti;
  1001. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  1002. md = find_device(param);
  1003. if (!md)
  1004. return -ENXIO;
  1005. r = __dev_status(md, param);
  1006. if (r)
  1007. goto out;
  1008. if (tmsg < (struct dm_target_msg *) param->data ||
  1009. invalid_str(tmsg->message, (void *) param + param_size)) {
  1010. DMWARN("Invalid target message parameters.");
  1011. r = -EINVAL;
  1012. goto out;
  1013. }
  1014. r = dm_split_args(&argc, &argv, tmsg->message);
  1015. if (r) {
  1016. DMWARN("Failed to split target message parameters");
  1017. goto out;
  1018. }
  1019. table = dm_get_table(md);
  1020. if (!table)
  1021. goto out_argv;
  1022. ti = dm_table_find_target(table, tmsg->sector);
  1023. if (!dm_target_is_valid(ti)) {
  1024. DMWARN("Target message sector outside device.");
  1025. r = -EINVAL;
  1026. } else if (ti->type->message)
  1027. r = ti->type->message(ti, argc, argv);
  1028. else {
  1029. DMWARN("Target type does not support messages");
  1030. r = -EINVAL;
  1031. }
  1032. dm_table_put(table);
  1033. out_argv:
  1034. kfree(argv);
  1035. out:
  1036. param->data_size = 0;
  1037. dm_put(md);
  1038. return r;
  1039. }
  1040. /*-----------------------------------------------------------------
  1041. * Implementation of open/close/ioctl on the special char
  1042. * device.
  1043. *---------------------------------------------------------------*/
  1044. static ioctl_fn lookup_ioctl(unsigned int cmd)
  1045. {
  1046. static struct {
  1047. int cmd;
  1048. ioctl_fn fn;
  1049. } _ioctls[] = {
  1050. {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
  1051. {DM_REMOVE_ALL_CMD, remove_all},
  1052. {DM_LIST_DEVICES_CMD, list_devices},
  1053. {DM_DEV_CREATE_CMD, dev_create},
  1054. {DM_DEV_REMOVE_CMD, dev_remove},
  1055. {DM_DEV_RENAME_CMD, dev_rename},
  1056. {DM_DEV_SUSPEND_CMD, dev_suspend},
  1057. {DM_DEV_STATUS_CMD, dev_status},
  1058. {DM_DEV_WAIT_CMD, dev_wait},
  1059. {DM_TABLE_LOAD_CMD, table_load},
  1060. {DM_TABLE_CLEAR_CMD, table_clear},
  1061. {DM_TABLE_DEPS_CMD, table_deps},
  1062. {DM_TABLE_STATUS_CMD, table_status},
  1063. {DM_LIST_VERSIONS_CMD, list_versions},
  1064. {DM_TARGET_MSG_CMD, target_message},
  1065. {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
  1066. };
  1067. return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
  1068. }
  1069. /*
  1070. * As well as checking the version compatibility this always
  1071. * copies the kernel interface version out.
  1072. */
  1073. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1074. {
  1075. uint32_t version[3];
  1076. int r = 0;
  1077. if (copy_from_user(version, user->version, sizeof(version)))
  1078. return -EFAULT;
  1079. if ((DM_VERSION_MAJOR != version[0]) ||
  1080. (DM_VERSION_MINOR < version[1])) {
  1081. DMWARN("ioctl interface mismatch: "
  1082. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1083. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1084. DM_VERSION_PATCHLEVEL,
  1085. version[0], version[1], version[2], cmd);
  1086. r = -EINVAL;
  1087. }
  1088. /*
  1089. * Fill in the kernel version.
  1090. */
  1091. version[0] = DM_VERSION_MAJOR;
  1092. version[1] = DM_VERSION_MINOR;
  1093. version[2] = DM_VERSION_PATCHLEVEL;
  1094. if (copy_to_user(user->version, version, sizeof(version)))
  1095. return -EFAULT;
  1096. return r;
  1097. }
  1098. static void free_params(struct dm_ioctl *param)
  1099. {
  1100. vfree(param);
  1101. }
  1102. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
  1103. {
  1104. struct dm_ioctl tmp, *dmi;
  1105. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1106. return -EFAULT;
  1107. if (tmp.data_size < sizeof(tmp))
  1108. return -EINVAL;
  1109. dmi = vmalloc(tmp.data_size);
  1110. if (!dmi)
  1111. return -ENOMEM;
  1112. if (copy_from_user(dmi, user, tmp.data_size)) {
  1113. vfree(dmi);
  1114. return -EFAULT;
  1115. }
  1116. *param = dmi;
  1117. return 0;
  1118. }
  1119. static int validate_params(uint cmd, struct dm_ioctl *param)
  1120. {
  1121. /* Always clear this flag */
  1122. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1123. /* Ignores parameters */
  1124. if (cmd == DM_REMOVE_ALL_CMD ||
  1125. cmd == DM_LIST_DEVICES_CMD ||
  1126. cmd == DM_LIST_VERSIONS_CMD)
  1127. return 0;
  1128. if ((cmd == DM_DEV_CREATE_CMD)) {
  1129. if (!*param->name) {
  1130. DMWARN("name not supplied when creating device");
  1131. return -EINVAL;
  1132. }
  1133. } else if ((*param->uuid && *param->name)) {
  1134. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1135. return -EINVAL;
  1136. }
  1137. /* Ensure strings are terminated */
  1138. param->name[DM_NAME_LEN - 1] = '\0';
  1139. param->uuid[DM_UUID_LEN - 1] = '\0';
  1140. return 0;
  1141. }
  1142. static int ctl_ioctl(struct inode *inode, struct file *file,
  1143. uint command, ulong u)
  1144. {
  1145. int r = 0;
  1146. unsigned int cmd;
  1147. struct dm_ioctl *param;
  1148. struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
  1149. ioctl_fn fn = NULL;
  1150. size_t param_size;
  1151. /* only root can play with this */
  1152. if (!capable(CAP_SYS_ADMIN))
  1153. return -EACCES;
  1154. if (_IOC_TYPE(command) != DM_IOCTL)
  1155. return -ENOTTY;
  1156. cmd = _IOC_NR(command);
  1157. /*
  1158. * Check the interface version passed in. This also
  1159. * writes out the kernel's interface version.
  1160. */
  1161. r = check_version(cmd, user);
  1162. if (r)
  1163. return r;
  1164. /*
  1165. * Nothing more to do for the version command.
  1166. */
  1167. if (cmd == DM_VERSION_CMD)
  1168. return 0;
  1169. fn = lookup_ioctl(cmd);
  1170. if (!fn) {
  1171. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1172. return -ENOTTY;
  1173. }
  1174. /*
  1175. * Trying to avoid low memory issues when a device is
  1176. * suspended.
  1177. */
  1178. current->flags |= PF_MEMALLOC;
  1179. /*
  1180. * Copy the parameters into kernel space.
  1181. */
  1182. r = copy_params(user, &param);
  1183. current->flags &= ~PF_MEMALLOC;
  1184. if (r)
  1185. return r;
  1186. r = validate_params(cmd, param);
  1187. if (r)
  1188. goto out;
  1189. param_size = param->data_size;
  1190. param->data_size = sizeof(*param);
  1191. r = fn(param, param_size);
  1192. /*
  1193. * Copy the results back to userland.
  1194. */
  1195. if (!r && copy_to_user(user, param, param->data_size))
  1196. r = -EFAULT;
  1197. out:
  1198. free_params(param);
  1199. return r;
  1200. }
  1201. static const struct file_operations _ctl_fops = {
  1202. .ioctl = ctl_ioctl,
  1203. .owner = THIS_MODULE,
  1204. };
  1205. static struct miscdevice _dm_misc = {
  1206. .minor = MISC_DYNAMIC_MINOR,
  1207. .name = DM_NAME,
  1208. .fops = &_ctl_fops
  1209. };
  1210. /*
  1211. * Create misc character device and link to DM_DIR/control.
  1212. */
  1213. int __init dm_interface_init(void)
  1214. {
  1215. int r;
  1216. r = dm_hash_init();
  1217. if (r)
  1218. return r;
  1219. r = misc_register(&_dm_misc);
  1220. if (r) {
  1221. DMERR("misc_register failed for control device");
  1222. dm_hash_exit();
  1223. return r;
  1224. }
  1225. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1226. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1227. DM_DRIVER_EMAIL);
  1228. return 0;
  1229. }
  1230. void dm_interface_exit(void)
  1231. {
  1232. if (misc_deregister(&_dm_misc) < 0)
  1233. DMERR("misc_deregister failed for control device");
  1234. dm_hash_exit();
  1235. }
  1236. /**
  1237. * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
  1238. * @md: Pointer to mapped_device
  1239. * @name: Buffer (size DM_NAME_LEN) for name
  1240. * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
  1241. */
  1242. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
  1243. {
  1244. int r = 0;
  1245. struct hash_cell *hc;
  1246. if (!md)
  1247. return -ENXIO;
  1248. dm_get(md);
  1249. down_read(&_hash_lock);
  1250. hc = dm_get_mdptr(md);
  1251. if (!hc || hc->md != md) {
  1252. r = -ENXIO;
  1253. goto out;
  1254. }
  1255. strcpy(name, hc->name);
  1256. strcpy(uuid, hc->uuid ? : "");
  1257. out:
  1258. up_read(&_hash_lock);
  1259. dm_put(md);
  1260. return r;
  1261. }