2 * parse_opt.c -- mount option string parsing helpers
4 * Copyright (C) 2007 Oracle. All rights reserved.
5 * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 021110-1307, USA.
25 * Converting a C string containing mount options to a data object
26 * and manipulating that object is cleaner in C than manipulating
27 * the C string itself. This is similar to the way Python handles
28 * string manipulation.
30 * The current implementation uses a linked list as the data object
31 * since lists are simple, and we don't need to worry about more
32 * than ten or twenty options at a time.
34 * Hopefully the interface is abstract enough that the underlying
35 * data structure can be replaced if needed without changing the API.
50 #include "parse_opt.h"
55 struct mount_option
*next
, *prev
;
60 struct mount_options
{
61 struct mount_option
*head
, *tail
;
65 static struct mount_option
*option_create(char *str
)
67 struct mount_option
*option
;
73 option
= malloc(sizeof(*option
));
80 opteq
= strchr(str
, '=');
82 option
->keyword
= strndup(str
, opteq
- str
);
85 option
->value
= strdup(opteq
+ 1);
87 free(option
->keyword
);
91 option
->keyword
= strdup(str
);
104 static void option_destroy(struct mount_option
*option
)
106 free(option
->keyword
);
111 static void options_init(struct mount_options
*options
)
113 options
->head
= options
->tail
= NULL
;
117 static struct mount_options
*options_create(void)
119 struct mount_options
*options
;
121 options
= malloc(sizeof(*options
));
123 options_init(options
);
128 static int options_empty(struct mount_options
*options
)
130 return options
->count
== 0;
133 static void options_tail_insert(struct mount_options
*options
,
134 struct mount_option
*option
)
136 struct mount_option
*prev
= options
->tail
;
144 options
->head
= option
;
145 options
->tail
= option
;
150 static void options_delete(struct mount_options
*options
,
151 struct mount_option
*option
)
153 struct mount_option
*prev
= option
->prev
;
154 struct mount_option
*next
= option
->next
;
156 if (!options_empty(options
)) {
158 prev
->next
= option
->next
;
160 next
->prev
= option
->prev
;
162 if (options
->head
== option
)
163 options
->head
= option
->next
;
164 if (options
->tail
== option
)
165 options
->tail
= prev
;
169 option_destroy(option
);
175 * po_destroy - deallocate a group of mount options
176 * @options: pointer to mount options to free
179 void po_destroy(struct mount_options
*options
)
182 while (!options_empty(options
))
183 options_delete(options
, options
->head
);
189 * po_split - split options string into group of options
190 * @options: pointer to C string containing zero or more comma-delimited options
192 * Convert our mount options string to a list to make it easier
193 * to adjust the options as we go. This is just an exercise in
194 * lexical parsing -- this function doesn't pay attention to the
195 * meaning of the options themselves.
197 * Returns a new group of mount options if successful; otherwise NULL
198 * is returned if some failure occurred.
200 struct mount_options
*po_split(char *str
)
202 struct mount_options
*options
;
203 struct tokenizer_state
*tstate
;
207 return options_create();
209 options
= options_create();
211 tstate
= init_tokenizer(str
, ',');
212 for (opt
= next_token(tstate
); opt
; opt
= next_token(tstate
)) {
213 struct mount_option
*option
= option_create(opt
);
217 options_tail_insert(options
, option
);
219 if (tokenizer_error(tstate
))
221 end_tokenizer(tstate
);
226 end_tokenizer(tstate
);
232 * po_replace - replace mount options in one mount_options object with another
233 * @target: pointer to previously instantiated object to replace
234 * @source: pointer to object containing source mount options
236 * Side effect: the object referred to by source is emptied.
238 void po_replace(struct mount_options
*target
, struct mount_options
*source
)
241 while (!options_empty(target
))
242 options_delete(target
, target
->head
);
245 target
->head
= source
->head
;
246 target
->tail
= source
->tail
;
247 target
->count
= source
->count
;
249 options_init(source
);
255 * po_join - recombine group of mount options into a C string
256 * @options: pointer to mount options to recombine
257 * @str: handle on string to replace (input and output)
259 * Convert our mount options object back into a string that the
260 * rest of the world can use.
262 * Upon return, @string contains the address of a replacement
263 * C string containing a comma-delimited list of mount options
264 * and values; or the passed-in string is freed and NULL is
265 * returned if some failure occurred.
267 po_return_t
po_join(struct mount_options
*options
, char **str
)
270 struct mount_option
*option
;
272 if (!str
|| !options
)
278 if (options_empty(options
)) {
280 return *str
? PO_SUCCEEDED
: PO_FAILED
;
283 for (option
= options
->head
; option
; option
= option
->next
) {
284 len
+= strlen(option
->keyword
);
286 len
+=strlen(option
->value
) + 1; /* equals sign */
291 len
++; /* NULL on the end */
298 for (option
= options
->head
; option
; option
= option
->next
) {
299 strcat(*str
, option
->keyword
);
302 strcat(*str
, option
->value
);
312 * po_append - concatenate an option onto a group of options
313 * @options: pointer to mount options
314 * @option: pointer to a C string containing the option to add
317 po_return_t
po_append(struct mount_options
*options
, char *str
)
319 struct mount_option
*option
= option_create(str
);
322 options_tail_insert(options
, option
);
329 * po_contains - check for presense of an option in a group
330 * @options: pointer to mount options
331 * @keyword: pointer to a C string containing option keyword for which to search
334 po_found_t
po_contains(struct mount_options
*options
, char *keyword
)
336 struct mount_option
*option
;
338 if (options
&& keyword
) {
339 for (option
= options
->head
; option
; option
= option
->next
)
340 if (strcmp(option
->keyword
, keyword
) == 0)
348 * po_get - return the value of the rightmost instance of an option
349 * @options: pointer to mount options
350 * @keyword: pointer to a C string containing option keyword for which to search
352 * If multiple instances of the same option are present in a mount option
353 * list, the rightmost instance is always the effective one.
355 * Returns pointer to C string containing the value of the option.
356 * Returns NULL if the option isn't found, or if the option doesn't
359 char *po_get(struct mount_options
*options
, char *keyword
)
361 struct mount_option
*option
;
363 if (options
&& keyword
) {
364 for (option
= options
->tail
; option
; option
= option
->prev
)
365 if (strcmp(option
->keyword
, keyword
) == 0)
366 return option
->value
;
373 * po_get_numeric - return numeric value of rightmost instance of keyword option
374 * @options: pointer to mount options
375 * @keyword: pointer to a C string containing option keyword for which to search
376 * @value: OUT: set to the value of the keyword
378 * This is specifically for parsing keyword options that take only a numeric
379 * value. If multiple instances of the same option are present in a mount
380 * option list, the rightmost instance is always the effective one.
383 * * PO_FOUND if the keyword was found and the value is numeric; @value is
384 * set to the keyword's value
385 * * PO_NOT_FOUND if the keyword was not found
386 * * PO_BAD_VALUE if the keyword was found, but the value is not numeric
388 * These last two are separate in case the caller wants to warn about bad mount
389 * options instead of silently using a default.
392 po_found_t
po_get_numeric(struct mount_options
*options
, char *keyword
, long *value
)
394 char *option
, *endptr
;
397 option
= po_get(options
, keyword
);
402 tmp
= strtol(option
, &endptr
, 10);
403 if (errno
== 0 && endptr
!= option
) {
409 #else /* HAVE_STRTOL */
410 po_found_t
po_get_numeric(struct mount_options
*options
, char *keyword
, long *value
)
414 option
= po_get(options
, keyword
);
418 *value
= atoi(option
);
421 #endif /* HAVE_STRTOL */
424 * po_rightmost - determine the relative position of several options
425 * @options: pointer to mount options
426 * @keys: pointer to an array of C strings containing option keywords
428 * This function can be used to determine which of several similar
429 * options will be the one to take effect.
431 * The kernel parses the mount option string from left to right.
432 * If an option is specified more than once (for example, "intr"
433 * and "nointr", the rightmost option is the last to be parsed,
434 * and it therefore takes precedence over previous similar options.
436 * This can also distinguish among multiple synonymous options, such
437 * as "proto=," "udp" and "tcp."
439 * Returns the index into @keys of the option that is rightmost.
440 * If none of the options listed in @keys is present in @options, or
441 * if @options is NULL, returns -1.
443 int po_rightmost(struct mount_options
*options
, const char *keys
[])
445 struct mount_option
*option
;
449 for (option
= options
->tail
; option
; option
= option
->prev
) {
450 for (i
= 0; keys
[i
] != NULL
; i
++)
451 if (strcmp(option
->keyword
, keys
[i
]) == 0)
460 * po_remove_all - remove instances of an option from a group
461 * @options: pointer to mount options
462 * @keyword: pointer to a C string containing an option keyword to remove
464 * Side-effect: the passed-in list is truncated on success.
466 po_found_t
po_remove_all(struct mount_options
*options
, char *keyword
)
468 struct mount_option
*option
, *next
;
469 int found
= PO_NOT_FOUND
;
471 if (options
&& keyword
) {
472 for (option
= options
->head
; option
; option
= next
) {
474 if (strcmp(option
->keyword
, keyword
) == 0) {
475 options_delete(options
, option
);