UPSTREAM: power_supply: fix return value of get_property
[firefly-linux-kernel-4.4.55.git] / init / do_mounts_dm.c
1 /* do_mounts_dm.c
2  * Copyright (C) 2010 The Chromium OS Authors <chromium-os-dev@chromium.org>
3  *                    All Rights Reserved.
4  * Based on do_mounts_md.c
5  *
6  * This file is released under the GPL.
7  */
8 #include <linux/device-mapper.h>
9 #include <linux/fs.h>
10 #include <linux/string.h>
11
12 #include "do_mounts.h"
13 #include "../drivers/md/dm.h"
14
15 #define DM_MAX_NAME 32
16 #define DM_MAX_UUID 129
17 #define DM_NO_UUID "none"
18
19 #define DM_MSG_PREFIX "init"
20
21 /* Separators used for parsing the dm= argument. */
22 #define DM_FIELD_SEP ' '
23 #define DM_LINE_SEP ','
24
25 /*
26  * When the device-mapper and any targets are compiled into the kernel
27  * (not a module), one target may be created and used as the root device at
28  * boot time with the parameters given with the boot line dm=...
29  * The code for that is here.
30  */
31
32 struct dm_setup_target {
33         sector_t begin;
34         sector_t length;
35         char *type;
36         char *params;
37         /* simple singly linked list */
38         struct dm_setup_target *next;
39 };
40
41 static struct {
42         int minor;
43         int ro;
44         char name[DM_MAX_NAME];
45         char uuid[DM_MAX_UUID];
46         char *targets;
47         struct dm_setup_target *target;
48         int target_count;
49 } dm_setup_args __initdata;
50
51 static __initdata int dm_early_setup;
52
53 static size_t __init get_dm_option(char *str, char **next, char sep)
54 {
55         size_t len = 0;
56         char *endp = NULL;
57
58         if (!str)
59                 return 0;
60
61         endp = strchr(str, sep);
62         if (!endp) {  /* act like strchrnul */
63                 len = strlen(str);
64                 endp = str + len;
65         } else {
66                 len = endp - str;
67         }
68
69         if (endp == str)
70                 return 0;
71
72         if (!next)
73                 return len;
74
75         if (*endp == 0) {
76                 /* Don't advance past the nul. */
77                 *next = endp;
78         } else {
79                 *next = endp + 1;
80         }
81         return len;
82 }
83
84 static int __init dm_setup_args_init(void)
85 {
86         dm_setup_args.minor = 0;
87         dm_setup_args.ro = 0;
88         dm_setup_args.target = NULL;
89         dm_setup_args.target_count = 0;
90         return 0;
91 }
92
93 static int __init dm_setup_cleanup(void)
94 {
95         struct dm_setup_target *target = dm_setup_args.target;
96         struct dm_setup_target *old_target = NULL;
97         while (target) {
98                 kfree(target->type);
99                 kfree(target->params);
100                 old_target = target;
101                 target = target->next;
102                 kfree(old_target);
103                 dm_setup_args.target_count--;
104         }
105         BUG_ON(dm_setup_args.target_count);
106         return 0;
107 }
108
109 static char * __init dm_setup_parse_device_args(char *str)
110 {
111         char *next = NULL;
112         size_t len = 0;
113
114         /* Grab the logical name of the device to be exported to udev */
115         len = get_dm_option(str, &next, DM_FIELD_SEP);
116         if (!len) {
117                 DMERR("failed to parse device name");
118                 goto parse_fail;
119         }
120         len = min(len + 1, sizeof(dm_setup_args.name));
121         strlcpy(dm_setup_args.name, str, len);  /* includes nul */
122         str = skip_spaces(next);
123
124         /* Grab the UUID value or "none" */
125         len = get_dm_option(str, &next, DM_FIELD_SEP);
126         if (!len) {
127                 DMERR("failed to parse device uuid");
128                 goto parse_fail;
129         }
130         len = min(len + 1, sizeof(dm_setup_args.uuid));
131         strlcpy(dm_setup_args.uuid, str, len);
132         str = skip_spaces(next);
133
134         /* Determine if the table/device will be read only or read-write */
135         if (!strncmp("ro,", str, 3)) {
136                 dm_setup_args.ro = 1;
137         } else if (!strncmp("rw,", str, 3)) {
138                 dm_setup_args.ro = 0;
139         } else {
140                 DMERR("failed to parse table mode");
141                 goto parse_fail;
142         }
143         str = skip_spaces(str + 3);
144
145         return str;
146
147 parse_fail:
148         return NULL;
149 }
150
151 static void __init dm_substitute_devices(char *str, size_t str_len)
152 {
153         char *candidate = str;
154         char *candidate_end = str;
155         char old_char;
156         size_t len = 0;
157         dev_t dev;
158
159         if (str_len < 3)
160                 return;
161
162         while (str && *str) {
163                 candidate = strchr(str, '/');
164                 if (!candidate)
165                         break;
166
167                 /* Avoid embedded slashes */
168                 if (candidate != str && *(candidate - 1) != DM_FIELD_SEP) {
169                         str = strchr(candidate, DM_FIELD_SEP);
170                         continue;
171                 }
172
173                 len = get_dm_option(candidate, &candidate_end, DM_FIELD_SEP);
174                 str = skip_spaces(candidate_end);
175                 if (len < 3 || len > 37)  /* name_to_dev_t max; maj:mix min */
176                         continue;
177
178                 /* Temporarily terminate with a nul */
179                 if (*candidate_end)
180                         candidate_end--;
181                 old_char = *candidate_end;
182                 *candidate_end = '\0';
183
184                 DMDEBUG("converting candidate device '%s' to dev_t", candidate);
185                 /* Use the boot-time specific device naming */
186                 dev = name_to_dev_t(candidate);
187                 *candidate_end = old_char;
188
189                 DMDEBUG(" -> %u", dev);
190                 /* No suitable replacement found */
191                 if (!dev)
192                         continue;
193
194                 /* Rewrite the /dev/path as a major:minor */
195                 len = snprintf(candidate, len, "%u:%u", MAJOR(dev), MINOR(dev));
196                 if (!len) {
197                         DMERR("error substituting device major/minor.");
198                         break;
199                 }
200                 candidate += len;
201                 /* Pad out with spaces (fixing our nul) */
202                 while (candidate < candidate_end)
203                         *(candidate++) = DM_FIELD_SEP;
204         }
205 }
206
207 static int __init dm_setup_parse_targets(char *str)
208 {
209         char *next = NULL;
210         size_t len = 0;
211         struct dm_setup_target **target = NULL;
212
213         /* Targets are defined as per the table format but with a
214          * comma as a newline separator. */
215         target = &dm_setup_args.target;
216         while (str && *str) {
217                 *target = kzalloc(sizeof(struct dm_setup_target), GFP_KERNEL);
218                 if (!*target) {
219                         DMERR("failed to allocate memory for target %d",
220                               dm_setup_args.target_count);
221                         goto parse_fail;
222                 }
223                 dm_setup_args.target_count++;
224
225                 (*target)->begin = simple_strtoull(str, &next, 10);
226                 if (!next || *next != DM_FIELD_SEP) {
227                         DMERR("failed to parse starting sector for target %d",
228                               dm_setup_args.target_count - 1);
229                         goto parse_fail;
230                 }
231                 str = skip_spaces(next + 1);
232
233                 (*target)->length = simple_strtoull(str, &next, 10);
234                 if (!next || *next != DM_FIELD_SEP) {
235                         DMERR("failed to parse length for target %d",
236                               dm_setup_args.target_count - 1);
237                         goto parse_fail;
238                 }
239                 str = skip_spaces(next + 1);
240
241                 len = get_dm_option(str, &next, DM_FIELD_SEP);
242                 if (!len ||
243                     !((*target)->type = kstrndup(str, len, GFP_KERNEL))) {
244                         DMERR("failed to parse type for target %d",
245                               dm_setup_args.target_count - 1);
246                         goto parse_fail;
247                 }
248                 str = skip_spaces(next);
249
250                 len = get_dm_option(str, &next, DM_LINE_SEP);
251                 if (!len ||
252                     !((*target)->params = kstrndup(str, len, GFP_KERNEL))) {
253                         DMERR("failed to parse params for target %d",
254                               dm_setup_args.target_count - 1);
255                         goto parse_fail;
256                 }
257                 str = skip_spaces(next);
258
259                 /* Before moving on, walk through the copied target and
260                  * attempt to replace all /dev/xxx with the major:minor number.
261                  * It may not be possible to resolve them traditionally at
262                  * boot-time. */
263                 dm_substitute_devices((*target)->params, len);
264
265                 target = &((*target)->next);
266         }
267         DMDEBUG("parsed %d targets", dm_setup_args.target_count);
268
269         return 0;
270
271 parse_fail:
272         return 1;
273 }
274
275 /*
276  * Parse the command-line parameters given our kernel, but do not
277  * actually try to invoke the DM device now; that is handled by
278  * dm_setup_drive after the low-level disk drivers have initialised.
279  * dm format is as follows:
280  *  dm="name uuid fmode,[table line 1],[table line 2],..."
281  * May be used with root=/dev/dm-0 as it always uses the first dm minor.
282  */
283
284 static int __init dm_setup(char *str)
285 {
286         dm_setup_args_init();
287
288         str = dm_setup_parse_device_args(str);
289         if (!str) {
290                 DMDEBUG("str is NULL");
291                 goto parse_fail;
292         }
293
294         /* Target parsing is delayed until we have dynamic memory */
295         dm_setup_args.targets = str;
296
297         printk(KERN_INFO "dm: will configure '%s' on dm-%d\n",
298                dm_setup_args.name, dm_setup_args.minor);
299
300         dm_early_setup = 1;
301         return 1;
302
303 parse_fail:
304         printk(KERN_WARNING "dm: Invalid arguments supplied to dm=.\n");
305         return 0;
306 }
307
308
309 static void __init dm_setup_drive(void)
310 {
311         struct mapped_device *md = NULL;
312         struct dm_table *table = NULL;
313         struct dm_setup_target *target;
314         char *uuid = dm_setup_args.uuid;
315         fmode_t fmode = FMODE_READ;
316
317         /* Finish parsing the targets. */
318         if (dm_setup_parse_targets(dm_setup_args.targets))
319                 goto parse_fail;
320
321         if (dm_create(dm_setup_args.minor, &md)) {
322                 DMDEBUG("failed to create the device");
323                 goto dm_create_fail;
324         }
325         DMDEBUG("created device '%s'", dm_device_name(md));
326
327         /* In addition to flagging the table below, the disk must be
328          * set explicitly ro/rw. */
329         set_disk_ro(dm_disk(md), dm_setup_args.ro);
330
331         if (!dm_setup_args.ro)
332                 fmode |= FMODE_WRITE;
333         if (dm_table_create(&table, fmode, dm_setup_args.target_count, md)) {
334                 DMDEBUG("failed to create the table");
335                 goto dm_table_create_fail;
336         }
337
338         dm_lock_md_type(md);
339         target = dm_setup_args.target;
340         while (target) {
341                 DMINFO("adding target '%llu %llu %s %s'",
342                        (unsigned long long) target->begin,
343                        (unsigned long long) target->length, target->type,
344                        target->params);
345                 if (dm_table_add_target(table, target->type, target->begin,
346                                         target->length, target->params)) {
347                         DMDEBUG("failed to add the target to the table");
348                         goto add_target_fail;
349                 }
350                 target = target->next;
351         }
352
353         if (dm_table_complete(table)) {
354                 DMDEBUG("failed to complete the table");
355                 goto table_complete_fail;
356         }
357
358         if (dm_get_md_type(md) == DM_TYPE_NONE) {
359                 dm_set_md_type(md, dm_table_get_type(table));
360                 if (dm_setup_md_queue(md)) {
361                         DMWARN("unable to set up device queue for new table.");
362                         goto setup_md_queue_fail;
363                 }
364         } else if (dm_get_md_type(md) != dm_table_get_type(table)) {
365                 DMWARN("can't change device type after initial table load.");
366                 goto setup_md_queue_fail;
367         }
368
369         /* Suspend the device so that we can bind it to the table. */
370         if (dm_suspend(md, 0)) {
371                 DMDEBUG("failed to suspend the device pre-bind");
372                 goto suspend_fail;
373         }
374
375         /* Bind the table to the device. This is the only way to associate
376          * md->map with the table and set the disk capacity directly. */
377         if (dm_swap_table(md, table)) {  /* should return NULL. */
378                 DMDEBUG("failed to bind the device to the table");
379                 goto table_bind_fail;
380         }
381
382         /* Finally, resume and the device should be ready. */
383         if (dm_resume(md)) {
384                 DMDEBUG("failed to resume the device");
385                 goto resume_fail;
386         }
387
388         /* Export the dm device via the ioctl interface */
389         if (!strcmp(DM_NO_UUID, dm_setup_args.uuid))
390                 uuid = NULL;
391         if (dm_ioctl_export(md, dm_setup_args.name, uuid)) {
392                 DMDEBUG("failed to export device with given name and uuid");
393                 goto export_fail;
394         }
395         printk(KERN_INFO "dm: dm-%d is ready\n", dm_setup_args.minor);
396
397         dm_unlock_md_type(md);
398         dm_setup_cleanup();
399         return;
400
401 export_fail:
402 resume_fail:
403 table_bind_fail:
404 suspend_fail:
405 setup_md_queue_fail:
406 table_complete_fail:
407 add_target_fail:
408         dm_unlock_md_type(md);
409 dm_table_create_fail:
410         dm_put(md);
411 dm_create_fail:
412         dm_setup_cleanup();
413 parse_fail:
414         printk(KERN_WARNING "dm: starting dm-%d (%s) failed\n",
415                dm_setup_args.minor, dm_setup_args.name);
416 }
417
418 __setup("dm=", dm_setup);
419
420 void __init dm_run_setup(void)
421 {
422         if (!dm_early_setup)
423                 return;
424         printk(KERN_INFO "dm: attempting early device configuration.\n");
425         dm_setup_drive();
426 }