@@ -362,35 +362,21 @@ int allocate_resource(struct resource *root, struct resource *new,
362362
363363EXPORT_SYMBOL (allocate_resource );
364364
365- /**
366- * insert_resource - Inserts a resource in the resource tree
367- * @parent: parent of the new resource
368- * @new: new resource to insert
369- *
370- * Returns 0 on success, -EBUSY if the resource can't be inserted.
371- *
372- * This function is equivalent to request_resource when no conflict
373- * happens. If a conflict happens, and the conflicting resources
374- * entirely fit within the range of the new resource, then the new
375- * resource is inserted and the conflicting resources become children of
376- * the new resource.
365+ /*
366+ * Insert a resource into the resource tree. If successful, return NULL,
367+ * otherwise return the conflicting resource (compare to __request_resource())
377368 */
378- int insert_resource (struct resource * parent , struct resource * new )
369+ static struct resource * __insert_resource (struct resource * parent , struct resource * new )
379370{
380- int result ;
381371 struct resource * first , * next ;
382372
383- write_lock (& resource_lock );
384-
385373 for (;; parent = first ) {
386- result = 0 ;
387374 first = __request_resource (parent , new );
388375 if (!first )
389- goto out ;
376+ return first ;
390377
391- result = - EBUSY ;
392378 if (first == parent )
393- goto out ;
379+ return first ;
394380
395381 if ((first -> start > new -> start ) || (first -> end < new -> end ))
396382 break ;
@@ -401,15 +387,13 @@ int insert_resource(struct resource *parent, struct resource *new)
401387 for (next = first ; ; next = next -> sibling ) {
402388 /* Partial overlap? Bad, and unfixable */
403389 if (next -> start < new -> start || next -> end > new -> end )
404- goto out ;
390+ return next ;
405391 if (!next -> sibling )
406392 break ;
407393 if (next -> sibling -> start > new -> end )
408394 break ;
409395 }
410396
411- result = 0 ;
412-
413397 new -> parent = parent ;
414398 new -> sibling = next -> sibling ;
415399 new -> child = first ;
@@ -426,10 +410,64 @@ int insert_resource(struct resource *parent, struct resource *new)
426410 next = next -> sibling ;
427411 next -> sibling = new ;
428412 }
413+ return NULL ;
414+ }
429415
430- out :
416+ /**
417+ * insert_resource - Inserts a resource in the resource tree
418+ * @parent: parent of the new resource
419+ * @new: new resource to insert
420+ *
421+ * Returns 0 on success, -EBUSY if the resource can't be inserted.
422+ *
423+ * This function is equivalent to request_resource when no conflict
424+ * happens. If a conflict happens, and the conflicting resources
425+ * entirely fit within the range of the new resource, then the new
426+ * resource is inserted and the conflicting resources become children of
427+ * the new resource.
428+ */
429+ int insert_resource (struct resource * parent , struct resource * new )
430+ {
431+ struct resource * conflict ;
432+
433+ write_lock (& resource_lock );
434+ conflict = __insert_resource (parent , new );
435+ write_unlock (& resource_lock );
436+ return conflict ? - EBUSY : 0 ;
437+ }
438+
439+ /**
440+ * insert_resource_expand_to_fit - Insert a resource into the resource tree
441+ * @parent: parent of the new resource
442+ * @new: new resource to insert
443+ *
444+ * Insert a resource into the resource tree, possibly expanding it in order
445+ * to make it encompass any conflicting resources.
446+ */
447+ void insert_resource_expand_to_fit (struct resource * root , struct resource * new )
448+ {
449+ if (new -> parent )
450+ return ;
451+
452+ write_lock (& resource_lock );
453+ for (;;) {
454+ struct resource * conflict ;
455+
456+ conflict = __insert_resource (root , new );
457+ if (!conflict )
458+ break ;
459+ if (conflict == root )
460+ break ;
461+
462+ /* Ok, expand resource to cover the conflict, then try again .. */
463+ if (conflict -> start < new -> start )
464+ new -> start = conflict -> start ;
465+ if (conflict -> end > new -> end )
466+ new -> end = conflict -> end ;
467+
468+ printk ("Expanded resource %s due to conflict with %s\n" , new -> name , conflict -> name );
469+ }
431470 write_unlock (& resource_lock );
432- return result ;
433471}
434472
435473/**
0 commit comments