dm-ioctl.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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_put(hc->md);
  279. up_write(&_hash_lock);
  280. kfree(old_name);
  281. return 0;
  282. }
  283. /*-----------------------------------------------------------------
  284. * Implementation of the ioctl commands
  285. *---------------------------------------------------------------*/
  286. /*
  287. * All the ioctl commands get dispatched to functions with this
  288. * prototype.
  289. */
  290. typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
  291. static int remove_all(struct dm_ioctl *param, size_t param_size)
  292. {
  293. dm_hash_remove_all(1);
  294. param->data_size = 0;
  295. return 0;
  296. }
  297. /*
  298. * Round up the ptr to an 8-byte boundary.
  299. */
  300. #define ALIGN_MASK 7
  301. static inline void *align_ptr(void *ptr)
  302. {
  303. return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
  304. }
  305. /*
  306. * Retrieves the data payload buffer from an already allocated
  307. * struct dm_ioctl.
  308. */
  309. static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
  310. size_t *len)
  311. {
  312. param->data_start = align_ptr(param + 1) - (void *) param;
  313. if (param->data_start < param_size)
  314. *len = param_size - param->data_start;
  315. else
  316. *len = 0;
  317. return ((void *) param) + param->data_start;
  318. }
  319. static int list_devices(struct dm_ioctl *param, size_t param_size)
  320. {
  321. unsigned int i;
  322. struct hash_cell *hc;
  323. size_t len, needed = 0;
  324. struct gendisk *disk;
  325. struct dm_name_list *nl, *old_nl = NULL;
  326. down_write(&_hash_lock);
  327. /*
  328. * Loop through all the devices working out how much
  329. * space we need.
  330. */
  331. for (i = 0; i < NUM_BUCKETS; i++) {
  332. list_for_each_entry (hc, _name_buckets + i, name_list) {
  333. needed += sizeof(struct dm_name_list);
  334. needed += strlen(hc->name) + 1;
  335. needed += ALIGN_MASK;
  336. }
  337. }
  338. /*
  339. * Grab our output buffer.
  340. */
  341. nl = get_result_buffer(param, param_size, &len);
  342. if (len < needed) {
  343. param->flags |= DM_BUFFER_FULL_FLAG;
  344. goto out;
  345. }
  346. param->data_size = param->data_start + needed;
  347. nl->dev = 0; /* Flags no data */
  348. /*
  349. * Now loop through filling out the names.
  350. */
  351. for (i = 0; i < NUM_BUCKETS; i++) {
  352. list_for_each_entry (hc, _name_buckets + i, name_list) {
  353. if (old_nl)
  354. old_nl->next = (uint32_t) ((void *) nl -
  355. (void *) old_nl);
  356. disk = dm_disk(hc->md);
  357. nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
  358. nl->next = 0;
  359. strcpy(nl->name, hc->name);
  360. old_nl = nl;
  361. nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
  362. }
  363. }
  364. out:
  365. up_write(&_hash_lock);
  366. return 0;
  367. }
  368. static void list_version_get_needed(struct target_type *tt, void *needed_param)
  369. {
  370. size_t *needed = needed_param;
  371. *needed += sizeof(struct dm_target_versions);
  372. *needed += strlen(tt->name);
  373. *needed += ALIGN_MASK;
  374. }
  375. static void list_version_get_info(struct target_type *tt, void *param)
  376. {
  377. struct vers_iter *info = param;
  378. /* Check space - it might have changed since the first iteration */
  379. if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
  380. info->end) {
  381. info->flags = DM_BUFFER_FULL_FLAG;
  382. return;
  383. }
  384. if (info->old_vers)
  385. info->old_vers->next = (uint32_t) ((void *)info->vers -
  386. (void *)info->old_vers);
  387. info->vers->version[0] = tt->version[0];
  388. info->vers->version[1] = tt->version[1];
  389. info->vers->version[2] = tt->version[2];
  390. info->vers->next = 0;
  391. strcpy(info->vers->name, tt->name);
  392. info->old_vers = info->vers;
  393. info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
  394. }
  395. static int list_versions(struct dm_ioctl *param, size_t param_size)
  396. {
  397. size_t len, needed = 0;
  398. struct dm_target_versions *vers;
  399. struct vers_iter iter_info;
  400. /*
  401. * Loop through all the devices working out how much
  402. * space we need.
  403. */
  404. dm_target_iterate(list_version_get_needed, &needed);
  405. /*
  406. * Grab our output buffer.
  407. */
  408. vers = get_result_buffer(param, param_size, &len);
  409. if (len < needed) {
  410. param->flags |= DM_BUFFER_FULL_FLAG;
  411. goto out;
  412. }
  413. param->data_size = param->data_start + needed;
  414. iter_info.param_size = param_size;
  415. iter_info.old_vers = NULL;
  416. iter_info.vers = vers;
  417. iter_info.flags = 0;
  418. iter_info.end = (char *)vers+len;
  419. /*
  420. * Now loop through filling out the names & versions.
  421. */
  422. dm_target_iterate(list_version_get_info, &iter_info);
  423. param->flags |= iter_info.flags;
  424. out:
  425. return 0;
  426. }
  427. static int check_name(const char *name)
  428. {
  429. if (strchr(name, '/')) {
  430. DMWARN("invalid device name");
  431. return -EINVAL;
  432. }
  433. return 0;
  434. }
  435. /*
  436. * Fills in a dm_ioctl structure, ready for sending back to
  437. * userland.
  438. */
  439. static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
  440. {
  441. struct gendisk *disk = dm_disk(md);
  442. struct dm_table *table;
  443. param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
  444. DM_ACTIVE_PRESENT_FLAG);
  445. if (dm_suspended(md))
  446. param->flags |= DM_SUSPEND_FLAG;
  447. param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
  448. /*
  449. * Yes, this will be out of date by the time it gets back
  450. * to userland, but it is still very useful for
  451. * debugging.
  452. */
  453. param->open_count = dm_open_count(md);
  454. if (disk->policy)
  455. param->flags |= DM_READONLY_FLAG;
  456. param->event_nr = dm_get_event_nr(md);
  457. table = dm_get_table(md);
  458. if (table) {
  459. param->flags |= DM_ACTIVE_PRESENT_FLAG;
  460. param->target_count = dm_table_get_num_targets(table);
  461. dm_table_put(table);
  462. } else
  463. param->target_count = 0;
  464. return 0;
  465. }
  466. static int dev_create(struct dm_ioctl *param, size_t param_size)
  467. {
  468. int r, m = DM_ANY_MINOR;
  469. struct mapped_device *md;
  470. r = check_name(param->name);
  471. if (r)
  472. return r;
  473. if (param->flags & DM_PERSISTENT_DEV_FLAG)
  474. m = MINOR(huge_decode_dev(param->dev));
  475. r = dm_create(m, &md);
  476. if (r)
  477. return r;
  478. r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
  479. if (r) {
  480. dm_put(md);
  481. return r;
  482. }
  483. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  484. r = __dev_status(md, param);
  485. dm_put(md);
  486. return r;
  487. }
  488. /*
  489. * Always use UUID for lookups if it's present, otherwise use name or dev.
  490. */
  491. static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
  492. {
  493. struct mapped_device *md;
  494. void *mdptr = NULL;
  495. if (*param->uuid)
  496. return __get_uuid_cell(param->uuid);
  497. if (*param->name)
  498. return __get_name_cell(param->name);
  499. md = dm_get_md(huge_decode_dev(param->dev));
  500. if (!md)
  501. goto out;
  502. mdptr = dm_get_mdptr(md);
  503. if (!mdptr)
  504. dm_put(md);
  505. out:
  506. return mdptr;
  507. }
  508. static struct mapped_device *find_device(struct dm_ioctl *param)
  509. {
  510. struct hash_cell *hc;
  511. struct mapped_device *md = NULL;
  512. down_read(&_hash_lock);
  513. hc = __find_device_hash_cell(param);
  514. if (hc) {
  515. md = hc->md;
  516. /*
  517. * Sneakily write in both the name and the uuid
  518. * while we have the cell.
  519. */
  520. strncpy(param->name, hc->name, sizeof(param->name));
  521. if (hc->uuid)
  522. strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
  523. else
  524. param->uuid[0] = '\0';
  525. if (hc->new_map)
  526. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  527. else
  528. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  529. }
  530. up_read(&_hash_lock);
  531. return md;
  532. }
  533. static int dev_remove(struct dm_ioctl *param, size_t param_size)
  534. {
  535. struct hash_cell *hc;
  536. struct mapped_device *md;
  537. int r;
  538. down_write(&_hash_lock);
  539. hc = __find_device_hash_cell(param);
  540. if (!hc) {
  541. DMWARN("device doesn't appear to be in the dev hash table.");
  542. up_write(&_hash_lock);
  543. return -ENXIO;
  544. }
  545. md = hc->md;
  546. /*
  547. * Ensure the device is not open and nothing further can open it.
  548. */
  549. r = dm_lock_for_deletion(md);
  550. if (r) {
  551. DMWARN("unable to remove open device %s", hc->name);
  552. up_write(&_hash_lock);
  553. dm_put(md);
  554. return r;
  555. }
  556. __hash_remove(hc);
  557. up_write(&_hash_lock);
  558. dm_put(md);
  559. param->data_size = 0;
  560. return 0;
  561. }
  562. /*
  563. * Check a string doesn't overrun the chunk of
  564. * memory we copied from userland.
  565. */
  566. static int invalid_str(char *str, void *end)
  567. {
  568. while ((void *) str < end)
  569. if (!*str++)
  570. return 0;
  571. return -EINVAL;
  572. }
  573. static int dev_rename(struct dm_ioctl *param, size_t param_size)
  574. {
  575. int r;
  576. char *new_name = (char *) param + param->data_start;
  577. if (new_name < (char *) param->data ||
  578. invalid_str(new_name, (void *) param + param_size)) {
  579. DMWARN("Invalid new logical volume name supplied.");
  580. return -EINVAL;
  581. }
  582. r = check_name(new_name);
  583. if (r)
  584. return r;
  585. param->data_size = 0;
  586. return dm_hash_rename(param->name, new_name);
  587. }
  588. static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
  589. {
  590. int r = -EINVAL, x;
  591. struct mapped_device *md;
  592. struct hd_geometry geometry;
  593. unsigned long indata[4];
  594. char *geostr = (char *) param + param->data_start;
  595. md = find_device(param);
  596. if (!md)
  597. return -ENXIO;
  598. if (geostr < (char *) param->data ||
  599. invalid_str(geostr, (void *) param + param_size)) {
  600. DMWARN("Invalid geometry supplied.");
  601. goto out;
  602. }
  603. x = sscanf(geostr, "%lu %lu %lu %lu", indata,
  604. indata + 1, indata + 2, indata + 3);
  605. if (x != 4) {
  606. DMWARN("Unable to interpret geometry settings.");
  607. goto out;
  608. }
  609. if (indata[0] > 65535 || indata[1] > 255 ||
  610. indata[2] > 255 || indata[3] > ULONG_MAX) {
  611. DMWARN("Geometry exceeds range limits.");
  612. goto out;
  613. }
  614. geometry.cylinders = indata[0];
  615. geometry.heads = indata[1];
  616. geometry.sectors = indata[2];
  617. geometry.start = indata[3];
  618. r = dm_set_geometry(md, &geometry);
  619. if (!r)
  620. r = __dev_status(md, param);
  621. param->data_size = 0;
  622. out:
  623. dm_put(md);
  624. return r;
  625. }
  626. static int do_suspend(struct dm_ioctl *param)
  627. {
  628. int r = 0;
  629. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  630. struct mapped_device *md;
  631. md = find_device(param);
  632. if (!md)
  633. return -ENXIO;
  634. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  635. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  636. if (param->flags & DM_NOFLUSH_FLAG)
  637. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  638. if (!dm_suspended(md))
  639. r = dm_suspend(md, suspend_flags);
  640. if (!r)
  641. r = __dev_status(md, param);
  642. dm_put(md);
  643. return r;
  644. }
  645. static int do_resume(struct dm_ioctl *param)
  646. {
  647. int r = 0;
  648. unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
  649. struct hash_cell *hc;
  650. struct mapped_device *md;
  651. struct dm_table *new_map;
  652. down_write(&_hash_lock);
  653. hc = __find_device_hash_cell(param);
  654. if (!hc) {
  655. DMWARN("device doesn't appear to be in the dev hash table.");
  656. up_write(&_hash_lock);
  657. return -ENXIO;
  658. }
  659. md = hc->md;
  660. new_map = hc->new_map;
  661. hc->new_map = NULL;
  662. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  663. up_write(&_hash_lock);
  664. /* Do we need to load a new map ? */
  665. if (new_map) {
  666. /* Suspend if it isn't already suspended */
  667. if (param->flags & DM_SKIP_LOCKFS_FLAG)
  668. suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
  669. if (param->flags & DM_NOFLUSH_FLAG)
  670. suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
  671. if (!dm_suspended(md))
  672. dm_suspend(md, suspend_flags);
  673. r = dm_swap_table(md, new_map);
  674. if (r) {
  675. dm_put(md);
  676. dm_table_put(new_map);
  677. return r;
  678. }
  679. if (dm_table_get_mode(new_map) & FMODE_WRITE)
  680. set_disk_ro(dm_disk(md), 0);
  681. else
  682. set_disk_ro(dm_disk(md), 1);
  683. dm_table_put(new_map);
  684. }
  685. if (dm_suspended(md))
  686. r = dm_resume(md);
  687. if (!r)
  688. r = __dev_status(md, param);
  689. dm_put(md);
  690. return r;
  691. }
  692. /*
  693. * Set or unset the suspension state of a device.
  694. * If the device already is in the requested state we just return its status.
  695. */
  696. static int dev_suspend(struct dm_ioctl *param, size_t param_size)
  697. {
  698. if (param->flags & DM_SUSPEND_FLAG)
  699. return do_suspend(param);
  700. return do_resume(param);
  701. }
  702. /*
  703. * Copies device info back to user space, used by
  704. * the create and info ioctls.
  705. */
  706. static int dev_status(struct dm_ioctl *param, size_t param_size)
  707. {
  708. int r;
  709. struct mapped_device *md;
  710. md = find_device(param);
  711. if (!md)
  712. return -ENXIO;
  713. r = __dev_status(md, param);
  714. dm_put(md);
  715. return r;
  716. }
  717. /*
  718. * Build up the status struct for each target
  719. */
  720. static void retrieve_status(struct dm_table *table,
  721. struct dm_ioctl *param, size_t param_size)
  722. {
  723. unsigned int i, num_targets;
  724. struct dm_target_spec *spec;
  725. char *outbuf, *outptr;
  726. status_type_t type;
  727. size_t remaining, len, used = 0;
  728. outptr = outbuf = get_result_buffer(param, param_size, &len);
  729. if (param->flags & DM_STATUS_TABLE_FLAG)
  730. type = STATUSTYPE_TABLE;
  731. else
  732. type = STATUSTYPE_INFO;
  733. /* Get all the target info */
  734. num_targets = dm_table_get_num_targets(table);
  735. for (i = 0; i < num_targets; i++) {
  736. struct dm_target *ti = dm_table_get_target(table, i);
  737. remaining = len - (outptr - outbuf);
  738. if (remaining <= sizeof(struct dm_target_spec)) {
  739. param->flags |= DM_BUFFER_FULL_FLAG;
  740. break;
  741. }
  742. spec = (struct dm_target_spec *) outptr;
  743. spec->status = 0;
  744. spec->sector_start = ti->begin;
  745. spec->length = ti->len;
  746. strncpy(spec->target_type, ti->type->name,
  747. sizeof(spec->target_type));
  748. outptr += sizeof(struct dm_target_spec);
  749. remaining = len - (outptr - outbuf);
  750. if (remaining <= 0) {
  751. param->flags |= DM_BUFFER_FULL_FLAG;
  752. break;
  753. }
  754. /* Get the status/table string from the target driver */
  755. if (ti->type->status) {
  756. if (ti->type->status(ti, type, outptr, remaining)) {
  757. param->flags |= DM_BUFFER_FULL_FLAG;
  758. break;
  759. }
  760. } else
  761. outptr[0] = '\0';
  762. outptr += strlen(outptr) + 1;
  763. used = param->data_start + (outptr - outbuf);
  764. outptr = align_ptr(outptr);
  765. spec->next = outptr - outbuf;
  766. }
  767. if (used)
  768. param->data_size = used;
  769. param->target_count = num_targets;
  770. }
  771. /*
  772. * Wait for a device to report an event
  773. */
  774. static int dev_wait(struct dm_ioctl *param, size_t param_size)
  775. {
  776. int r;
  777. struct mapped_device *md;
  778. struct dm_table *table;
  779. md = find_device(param);
  780. if (!md)
  781. return -ENXIO;
  782. /*
  783. * Wait for a notification event
  784. */
  785. if (dm_wait_event(md, param->event_nr)) {
  786. r = -ERESTARTSYS;
  787. goto out;
  788. }
  789. /*
  790. * The userland program is going to want to know what
  791. * changed to trigger the event, so we may as well tell
  792. * him and save an ioctl.
  793. */
  794. r = __dev_status(md, param);
  795. if (r)
  796. goto out;
  797. table = dm_get_table(md);
  798. if (table) {
  799. retrieve_status(table, param, param_size);
  800. dm_table_put(table);
  801. }
  802. out:
  803. dm_put(md);
  804. return r;
  805. }
  806. static inline int get_mode(struct dm_ioctl *param)
  807. {
  808. int mode = FMODE_READ | FMODE_WRITE;
  809. if (param->flags & DM_READONLY_FLAG)
  810. mode = FMODE_READ;
  811. return mode;
  812. }
  813. static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
  814. struct dm_target_spec **spec, char **target_params)
  815. {
  816. *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
  817. *target_params = (char *) (*spec + 1);
  818. if (*spec < (last + 1))
  819. return -EINVAL;
  820. return invalid_str(*target_params, end);
  821. }
  822. static int populate_table(struct dm_table *table,
  823. struct dm_ioctl *param, size_t param_size)
  824. {
  825. int r;
  826. unsigned int i = 0;
  827. struct dm_target_spec *spec = (struct dm_target_spec *) param;
  828. uint32_t next = param->data_start;
  829. void *end = (void *) param + param_size;
  830. char *target_params;
  831. if (!param->target_count) {
  832. DMWARN("populate_table: no targets specified");
  833. return -EINVAL;
  834. }
  835. for (i = 0; i < param->target_count; i++) {
  836. r = next_target(spec, next, end, &spec, &target_params);
  837. if (r) {
  838. DMWARN("unable to find target");
  839. return r;
  840. }
  841. r = dm_table_add_target(table, spec->target_type,
  842. (sector_t) spec->sector_start,
  843. (sector_t) spec->length,
  844. target_params);
  845. if (r) {
  846. DMWARN("error adding target to table");
  847. return r;
  848. }
  849. next = spec->next;
  850. }
  851. return dm_table_complete(table);
  852. }
  853. static int table_load(struct dm_ioctl *param, size_t param_size)
  854. {
  855. int r;
  856. struct hash_cell *hc;
  857. struct dm_table *t;
  858. struct mapped_device *md;
  859. md = find_device(param);
  860. if (!md)
  861. return -ENXIO;
  862. r = dm_table_create(&t, get_mode(param), param->target_count, md);
  863. if (r)
  864. goto out;
  865. r = populate_table(t, param, param_size);
  866. if (r) {
  867. dm_table_put(t);
  868. goto out;
  869. }
  870. down_write(&_hash_lock);
  871. hc = dm_get_mdptr(md);
  872. if (!hc || hc->md != md) {
  873. DMWARN("device has been removed from the dev hash table.");
  874. dm_table_put(t);
  875. up_write(&_hash_lock);
  876. r = -ENXIO;
  877. goto out;
  878. }
  879. if (hc->new_map)
  880. dm_table_put(hc->new_map);
  881. hc->new_map = t;
  882. up_write(&_hash_lock);
  883. param->flags |= DM_INACTIVE_PRESENT_FLAG;
  884. r = __dev_status(md, param);
  885. out:
  886. dm_put(md);
  887. return r;
  888. }
  889. static int table_clear(struct dm_ioctl *param, size_t param_size)
  890. {
  891. int r;
  892. struct hash_cell *hc;
  893. struct mapped_device *md;
  894. down_write(&_hash_lock);
  895. hc = __find_device_hash_cell(param);
  896. if (!hc) {
  897. DMWARN("device doesn't appear to be in the dev hash table.");
  898. up_write(&_hash_lock);
  899. return -ENXIO;
  900. }
  901. if (hc->new_map) {
  902. dm_table_put(hc->new_map);
  903. hc->new_map = NULL;
  904. }
  905. param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
  906. r = __dev_status(hc->md, param);
  907. md = hc->md;
  908. up_write(&_hash_lock);
  909. dm_put(md);
  910. return r;
  911. }
  912. /*
  913. * Retrieves a list of devices used by a particular dm device.
  914. */
  915. static void retrieve_deps(struct dm_table *table,
  916. struct dm_ioctl *param, size_t param_size)
  917. {
  918. unsigned int count = 0;
  919. struct list_head *tmp;
  920. size_t len, needed;
  921. struct dm_dev *dd;
  922. struct dm_target_deps *deps;
  923. deps = get_result_buffer(param, param_size, &len);
  924. /*
  925. * Count the devices.
  926. */
  927. list_for_each (tmp, dm_table_get_devices(table))
  928. count++;
  929. /*
  930. * Check we have enough space.
  931. */
  932. needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
  933. if (len < needed) {
  934. param->flags |= DM_BUFFER_FULL_FLAG;
  935. return;
  936. }
  937. /*
  938. * Fill in the devices.
  939. */
  940. deps->count = count;
  941. count = 0;
  942. list_for_each_entry (dd, dm_table_get_devices(table), list)
  943. deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
  944. param->data_size = param->data_start + needed;
  945. }
  946. static int table_deps(struct dm_ioctl *param, size_t param_size)
  947. {
  948. int r = 0;
  949. struct mapped_device *md;
  950. struct dm_table *table;
  951. md = find_device(param);
  952. if (!md)
  953. return -ENXIO;
  954. r = __dev_status(md, param);
  955. if (r)
  956. goto out;
  957. table = dm_get_table(md);
  958. if (table) {
  959. retrieve_deps(table, param, param_size);
  960. dm_table_put(table);
  961. }
  962. out:
  963. dm_put(md);
  964. return r;
  965. }
  966. /*
  967. * Return the status of a device as a text string for each
  968. * target.
  969. */
  970. static int table_status(struct dm_ioctl *param, size_t param_size)
  971. {
  972. int r;
  973. struct mapped_device *md;
  974. struct dm_table *table;
  975. md = find_device(param);
  976. if (!md)
  977. return -ENXIO;
  978. r = __dev_status(md, param);
  979. if (r)
  980. goto out;
  981. table = dm_get_table(md);
  982. if (table) {
  983. retrieve_status(table, param, param_size);
  984. dm_table_put(table);
  985. }
  986. out:
  987. dm_put(md);
  988. return r;
  989. }
  990. /*
  991. * Pass a message to the target that's at the supplied device offset.
  992. */
  993. static int target_message(struct dm_ioctl *param, size_t param_size)
  994. {
  995. int r, argc;
  996. char **argv;
  997. struct mapped_device *md;
  998. struct dm_table *table;
  999. struct dm_target *ti;
  1000. struct dm_target_msg *tmsg = (void *) param + param->data_start;
  1001. md = find_device(param);
  1002. if (!md)
  1003. return -ENXIO;
  1004. r = __dev_status(md, param);
  1005. if (r)
  1006. goto out;
  1007. if (tmsg < (struct dm_target_msg *) param->data ||
  1008. invalid_str(tmsg->message, (void *) param + param_size)) {
  1009. DMWARN("Invalid target message parameters.");
  1010. r = -EINVAL;
  1011. goto out;
  1012. }
  1013. r = dm_split_args(&argc, &argv, tmsg->message);
  1014. if (r) {
  1015. DMWARN("Failed to split target message parameters");
  1016. goto out;
  1017. }
  1018. table = dm_get_table(md);
  1019. if (!table)
  1020. goto out_argv;
  1021. if (tmsg->sector >= dm_table_get_size(table)) {
  1022. DMWARN("Target message sector outside device.");
  1023. r = -EINVAL;
  1024. goto out_table;
  1025. }
  1026. ti = dm_table_find_target(table, tmsg->sector);
  1027. if (ti->type->message)
  1028. r = ti->type->message(ti, argc, argv);
  1029. else {
  1030. DMWARN("Target type does not support messages");
  1031. r = -EINVAL;
  1032. }
  1033. out_table:
  1034. dm_table_put(table);
  1035. out_argv:
  1036. kfree(argv);
  1037. out:
  1038. param->data_size = 0;
  1039. dm_put(md);
  1040. return r;
  1041. }
  1042. /*-----------------------------------------------------------------
  1043. * Implementation of open/close/ioctl on the special char
  1044. * device.
  1045. *---------------------------------------------------------------*/
  1046. static ioctl_fn lookup_ioctl(unsigned int cmd)
  1047. {
  1048. static struct {
  1049. int cmd;
  1050. ioctl_fn fn;
  1051. } _ioctls[] = {
  1052. {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
  1053. {DM_REMOVE_ALL_CMD, remove_all},
  1054. {DM_LIST_DEVICES_CMD, list_devices},
  1055. {DM_DEV_CREATE_CMD, dev_create},
  1056. {DM_DEV_REMOVE_CMD, dev_remove},
  1057. {DM_DEV_RENAME_CMD, dev_rename},
  1058. {DM_DEV_SUSPEND_CMD, dev_suspend},
  1059. {DM_DEV_STATUS_CMD, dev_status},
  1060. {DM_DEV_WAIT_CMD, dev_wait},
  1061. {DM_TABLE_LOAD_CMD, table_load},
  1062. {DM_TABLE_CLEAR_CMD, table_clear},
  1063. {DM_TABLE_DEPS_CMD, table_deps},
  1064. {DM_TABLE_STATUS_CMD, table_status},
  1065. {DM_LIST_VERSIONS_CMD, list_versions},
  1066. {DM_TARGET_MSG_CMD, target_message},
  1067. {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
  1068. };
  1069. return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
  1070. }
  1071. /*
  1072. * As well as checking the version compatibility this always
  1073. * copies the kernel interface version out.
  1074. */
  1075. static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
  1076. {
  1077. uint32_t version[3];
  1078. int r = 0;
  1079. if (copy_from_user(version, user->version, sizeof(version)))
  1080. return -EFAULT;
  1081. if ((DM_VERSION_MAJOR != version[0]) ||
  1082. (DM_VERSION_MINOR < version[1])) {
  1083. DMWARN("ioctl interface mismatch: "
  1084. "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
  1085. DM_VERSION_MAJOR, DM_VERSION_MINOR,
  1086. DM_VERSION_PATCHLEVEL,
  1087. version[0], version[1], version[2], cmd);
  1088. r = -EINVAL;
  1089. }
  1090. /*
  1091. * Fill in the kernel version.
  1092. */
  1093. version[0] = DM_VERSION_MAJOR;
  1094. version[1] = DM_VERSION_MINOR;
  1095. version[2] = DM_VERSION_PATCHLEVEL;
  1096. if (copy_to_user(user->version, version, sizeof(version)))
  1097. return -EFAULT;
  1098. return r;
  1099. }
  1100. static void free_params(struct dm_ioctl *param)
  1101. {
  1102. vfree(param);
  1103. }
  1104. static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
  1105. {
  1106. struct dm_ioctl tmp, *dmi;
  1107. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1108. return -EFAULT;
  1109. if (tmp.data_size < sizeof(tmp))
  1110. return -EINVAL;
  1111. dmi = vmalloc(tmp.data_size);
  1112. if (!dmi)
  1113. return -ENOMEM;
  1114. if (copy_from_user(dmi, user, tmp.data_size)) {
  1115. vfree(dmi);
  1116. return -EFAULT;
  1117. }
  1118. *param = dmi;
  1119. return 0;
  1120. }
  1121. static int validate_params(uint cmd, struct dm_ioctl *param)
  1122. {
  1123. /* Always clear this flag */
  1124. param->flags &= ~DM_BUFFER_FULL_FLAG;
  1125. /* Ignores parameters */
  1126. if (cmd == DM_REMOVE_ALL_CMD ||
  1127. cmd == DM_LIST_DEVICES_CMD ||
  1128. cmd == DM_LIST_VERSIONS_CMD)
  1129. return 0;
  1130. if ((cmd == DM_DEV_CREATE_CMD)) {
  1131. if (!*param->name) {
  1132. DMWARN("name not supplied when creating device");
  1133. return -EINVAL;
  1134. }
  1135. } else if ((*param->uuid && *param->name)) {
  1136. DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
  1137. return -EINVAL;
  1138. }
  1139. /* Ensure strings are terminated */
  1140. param->name[DM_NAME_LEN - 1] = '\0';
  1141. param->uuid[DM_UUID_LEN - 1] = '\0';
  1142. return 0;
  1143. }
  1144. static int ctl_ioctl(struct inode *inode, struct file *file,
  1145. uint command, ulong u)
  1146. {
  1147. int r = 0;
  1148. unsigned int cmd;
  1149. struct dm_ioctl *param;
  1150. struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
  1151. ioctl_fn fn = NULL;
  1152. size_t param_size;
  1153. /* only root can play with this */
  1154. if (!capable(CAP_SYS_ADMIN))
  1155. return -EACCES;
  1156. if (_IOC_TYPE(command) != DM_IOCTL)
  1157. return -ENOTTY;
  1158. cmd = _IOC_NR(command);
  1159. /*
  1160. * Check the interface version passed in. This also
  1161. * writes out the kernel's interface version.
  1162. */
  1163. r = check_version(cmd, user);
  1164. if (r)
  1165. return r;
  1166. /*
  1167. * Nothing more to do for the version command.
  1168. */
  1169. if (cmd == DM_VERSION_CMD)
  1170. return 0;
  1171. fn = lookup_ioctl(cmd);
  1172. if (!fn) {
  1173. DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
  1174. return -ENOTTY;
  1175. }
  1176. /*
  1177. * Trying to avoid low memory issues when a device is
  1178. * suspended.
  1179. */
  1180. current->flags |= PF_MEMALLOC;
  1181. /*
  1182. * Copy the parameters into kernel space.
  1183. */
  1184. r = copy_params(user, &param);
  1185. current->flags &= ~PF_MEMALLOC;
  1186. if (r)
  1187. return r;
  1188. r = validate_params(cmd, param);
  1189. if (r)
  1190. goto out;
  1191. param_size = param->data_size;
  1192. param->data_size = sizeof(*param);
  1193. r = fn(param, param_size);
  1194. /*
  1195. * Copy the results back to userland.
  1196. */
  1197. if (!r && copy_to_user(user, param, param->data_size))
  1198. r = -EFAULT;
  1199. out:
  1200. free_params(param);
  1201. return r;
  1202. }
  1203. static const struct file_operations _ctl_fops = {
  1204. .ioctl = ctl_ioctl,
  1205. .owner = THIS_MODULE,
  1206. };
  1207. static struct miscdevice _dm_misc = {
  1208. .minor = MISC_DYNAMIC_MINOR,
  1209. .name = DM_NAME,
  1210. .fops = &_ctl_fops
  1211. };
  1212. /*
  1213. * Create misc character device and link to DM_DIR/control.
  1214. */
  1215. int __init dm_interface_init(void)
  1216. {
  1217. int r;
  1218. r = dm_hash_init();
  1219. if (r)
  1220. return r;
  1221. r = misc_register(&_dm_misc);
  1222. if (r) {
  1223. DMERR("misc_register failed for control device");
  1224. dm_hash_exit();
  1225. return r;
  1226. }
  1227. DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
  1228. DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
  1229. DM_DRIVER_EMAIL);
  1230. return 0;
  1231. }
  1232. void dm_interface_exit(void)
  1233. {
  1234. if (misc_deregister(&_dm_misc) < 0)
  1235. DMERR("misc_deregister failed for control device");
  1236. dm_hash_exit();
  1237. }
  1238. /**
  1239. * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
  1240. * @md: Pointer to mapped_device
  1241. * @name: Buffer (size DM_NAME_LEN) for name
  1242. * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
  1243. */
  1244. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
  1245. {
  1246. int r = 0;
  1247. struct hash_cell *hc;
  1248. if (!md)
  1249. return -ENXIO;
  1250. dm_get(md);
  1251. down_read(&_hash_lock);
  1252. hc = dm_get_mdptr(md);
  1253. if (!hc || hc->md != md) {
  1254. r = -ENXIO;
  1255. goto out;
  1256. }
  1257. strcpy(name, hc->name);
  1258. strcpy(uuid, hc->uuid ? : "");
  1259. out:
  1260. up_read(&_hash_lock);
  1261. dm_put(md);
  1262. return r;
  1263. }