1  
       2  #include "c.h"
       3  #include "strutils.h"
       4  
       5  #ifdef HAVE_LIBBLKID
       6  # include <blkid.h>
       7  #endif
       8  
       9  #include "fdiskP.h"
      10  
      11  /**
      12   * SECTION: partition
      13   * @title: Partition
      14   * @short_description: generic label independent partition abstraction
      15   *
      16   * The fdisk_partition provides label independent abstraction. The partitions
      17   * are not directly connected with partition table (label) data. Any change to
      18   * fdisk_partition does not affects in-memory or on-disk label data.
      19   *
      20   * The fdisk_partition is possible to use as a template for
      21   * fdisk_add_partition() or fdisk_set_partition() operations.
      22   */
      23  
      24  static void init_partition(struct fdisk_partition *pa)
      25  {
      26  	FDISK_INIT_UNDEF(pa->size);
      27  	FDISK_INIT_UNDEF(pa->start);
      28  	FDISK_INIT_UNDEF(pa->partno);
      29  	FDISK_INIT_UNDEF(pa->parent_partno);
      30  	FDISK_INIT_UNDEF(pa->boot);
      31  
      32  	INIT_LIST_HEAD(&pa->parts);
      33  }
      34  
      35  /**
      36   * fdisk_new_partition:
      37   *
      38   * Returns: new instance.
      39   */
      40  struct fdisk_partition *fdisk_new_partition(void)
      41  {
      42  	struct fdisk_partition *pa = calloc(1, sizeof(*pa));
      43  
      44  	pa->refcount = 1;
      45  	init_partition(pa);
      46  	DBG(PART, ul_debugobj(pa, "alloc"));
      47  	return pa;
      48  }
      49  
      50  /**
      51   * fdisk_reset_partition:
      52   * @pa: partition
      53   *
      54   * Resets partition content.
      55   */
      56  void fdisk_reset_partition(struct fdisk_partition *pa)
      57  {
      58  	int ref;
      59  
      60  	if (!pa)
      61  		return;
      62  
      63  	DBG(PART, ul_debugobj(pa, "reset"));
      64  	ref = pa->refcount;
      65  
      66  	fdisk_unref_parttype(pa->type);
      67  	free(pa->name);
      68  	free(pa->uuid);
      69  	free(pa->attrs);
      70  	free(pa->fstype);
      71  	free(pa->fsuuid);
      72  	free(pa->fslabel);
      73  	free(pa->start_chs);
      74  	free(pa->end_chs);
      75  
      76  	memset(pa, 0, sizeof(*pa));
      77  	pa->refcount = ref;
      78  
      79  	init_partition(pa);
      80  }
      81  
      82  static struct fdisk_partition *__copy_partition(struct fdisk_partition *o)
      83  {
      84  	struct fdisk_partition *n = fdisk_new_partition();
      85  	int rc;
      86  
      87  	if (!n)
      88  		return NULL;
      89  
      90  	memcpy(n, o, sizeof(*n));
      91  
      92  	/* do not copy reference to lists, etc.*/
      93  	n->refcount = 1;
      94  	INIT_LIST_HEAD(&n->parts);
      95  
      96  	if (n->type)
      97  		fdisk_ref_parttype(n->type);
      98  
      99  	/* note that strdup_between_structs() deallocates destination pointer,
     100  	 * so make sure it's NULL as we call memcpy() before ... */
     101  	n->name = NULL;
     102  	rc = strdup_between_structs(n, o, name);
     103  
     104  	n->uuid = NULL;
     105  	if (!rc)
     106  		rc = strdup_between_structs(n, o, uuid);
     107  	n->attrs = NULL;
     108  	if (!rc)
     109  		rc = strdup_between_structs(n, o, attrs);
     110  	n->fstype = NULL;
     111  	if (!rc)
     112  		rc = strdup_between_structs(n, o, fstype);
     113  	n->fsuuid = NULL;
     114  	if (!rc)
     115  		rc = strdup_between_structs(n, o, fsuuid);
     116  	n->fslabel = NULL;
     117  	if (!rc)
     118  		rc = strdup_between_structs(n, o, fslabel);
     119  	n->start_chs = NULL;
     120  	if (!rc)
     121  		rc = strdup_between_structs(n, o, start_chs);
     122  	n->end_chs = NULL;
     123  	if (!rc)
     124  		rc = strdup_between_structs(n, o, end_chs);
     125  
     126  	if (rc) {
     127  		fdisk_unref_partition(n);
     128  		n = NULL;
     129  	}
     130  	return n;
     131  }
     132  
     133  /**
     134   * fdisk_ref_partition:
     135   * @pa: partition pointer
     136   *
     137   * Increments reference counter.
     138   */
     139  void fdisk_ref_partition(struct fdisk_partition *pa)
     140  {
     141  	if (pa)
     142  		pa->refcount++;
     143  }
     144  
     145  /**
     146   * fdisk_unref_partition:
     147   * @pa: partition pointer
     148   *
     149   * Decrements reference counter, on zero the @pa is automatically
     150   * deallocated.
     151   */
     152  void fdisk_unref_partition(struct fdisk_partition *pa)
     153  {
     154  	if (!pa)
     155  		return;
     156  
     157  	pa->refcount--;
     158  	if (pa->refcount <= 0) {
     159  		list_del(&pa->parts);
     160  		fdisk_reset_partition(pa);
     161  		DBG(PART, ul_debugobj(pa, "free"));
     162  		free(pa);
     163  	}
     164  }
     165  
     166  /**
     167   * fdisk_partition_set_start:
     168   * @pa: partition
     169   * @off: offset in sectors, maximal is UINT64_MAX-1
     170   *
     171   * Note that zero is valid offset too. Use fdisk_partition_unset_start() to
     172   * undefine the offset.
     173   *
     174   * Returns: 0 on success, <0 on error.
     175   */
     176  int fdisk_partition_set_start(struct fdisk_partition *pa, fdisk_sector_t off)
     177  {
     178  	if (!pa)
     179  		return -EINVAL;
     180  	if (FDISK_IS_UNDEF(off))
     181  		return -ERANGE;
     182  	pa->start = off;
     183  	pa->fs_probed = 0;
     184  	return 0;
     185  }
     186  
     187  /**
     188   * fdisk_partition_unset_start:
     189   * @pa: partition
     190   *
     191   * Sets the size as undefined. See fdisk_partition_has_start().
     192   *
     193   * Returns: 0 on success, <0 on error.
     194   */
     195  int fdisk_partition_unset_start(struct fdisk_partition *pa)
     196  {
     197  	if (!pa)
     198  		return -EINVAL;
     199  	FDISK_INIT_UNDEF(pa->start);
     200  	pa->fs_probed = 0;
     201  	return 0;
     202  }
     203  
     204  /**
     205   * fdisk_partition_get_start:
     206   * @pa: partition
     207   *
     208   * The zero is also valid offset. The function may return random undefined
     209   * value when start offset is undefined (for example after
     210   * fdisk_partition_unset_start()). Always use fdisk_partition_has_start() to be
     211   * sure that you work with valid numbers.
     212   *
     213   * Returns: start offset in sectors
     214   */
     215  fdisk_sector_t fdisk_partition_get_start(struct fdisk_partition *pa)
     216  {
     217  	return pa->start;
     218  }
     219  
     220  /**
     221   * fdisk_partition_has_start:
     222   * @pa: partition
     223   *
     224   * Returns: 1 or 0
     225   */
     226  int fdisk_partition_has_start(struct fdisk_partition *pa)
     227  {
     228  	return pa && !FDISK_IS_UNDEF(pa->start);
     229  }
     230  
     231  
     232  /**
     233   * fdisk_partition_cmp_start:
     234   * @a: partition
     235   * @b: partition
     236   *
     237   * Compares partitions according to start offset, See fdisk_table_sort_partitions().
     238   *
     239   * Return: 0 if the same, <0 if @b greater, >0 if @a greater.
     240   */
     241  int fdisk_partition_cmp_start(struct fdisk_partition *a,
     242  			      struct fdisk_partition *b)
     243  {
     244  	int no_a = FDISK_IS_UNDEF(a->start),
     245  	    no_b = FDISK_IS_UNDEF(b->start);
     246  
     247  	if (no_a && no_b)
     248  		return 0;
     249  	if (no_a)
     250  		return -1;
     251  	if (no_b)
     252  		return 1;
     253  
     254  	return cmp_numbers(a->start, b->start);
     255  }
     256  
     257  /**
     258   * fdisk_partition_start_follow_default
     259   * @pa: partition
     260   * @enable: 0|1
     261   *
     262   * When @pa used as a template for fdisk_add_partition() when force label driver
     263   * to use the first possible space for the new partition.
     264   *
     265   * Returns: 0 on success, <0 on error.
     266   */
     267  int fdisk_partition_start_follow_default(struct fdisk_partition *pa, int enable)
     268  {
     269  	if (!pa)
     270  		return -EINVAL;
     271  	pa->start_follow_default = enable ? 1 : 0;
     272  	return 0;
     273  }
     274  
     275  /**
     276   * fdisk_partition_start_is_default:
     277   * @pa: partition
     278   *
     279   * See fdisk_partition_start_follow_default().
     280   *
     281   * Returns: 1 if the partition follows default
     282   */
     283  int fdisk_partition_start_is_default(struct fdisk_partition *pa)
     284  {
     285  	assert(pa);
     286  	return pa->start_follow_default;
     287  }
     288  
     289  /**
     290   * fdisk_partition_set_size:
     291   * @pa: partition
     292   * @sz: size in sectors, maximal is UIN64_MAX-1
     293   *
     294   * Note that zero is valid size too. Use fdisk_partition_unset_size() to
     295   * undefine the size.
     296   *
     297   * Returns: 0 on success, <0 on error.
     298   */
     299  int fdisk_partition_set_size(struct fdisk_partition *pa, fdisk_sector_t sz)
     300  {
     301  	if (!pa)
     302  		return -EINVAL;
     303  	if (FDISK_IS_UNDEF(sz))
     304  		return -ERANGE;
     305  	pa->size = sz;
     306  	pa->fs_probed = 0;
     307  	return 0;
     308  }
     309  
     310  /**
     311   * fdisk_partition_unset_size:
     312   * @pa: partition
     313   *
     314   * Sets the size as undefined. See fdisk_partition_has_size().
     315   *
     316   * Returns: 0 on success, <0 on error.
     317   */
     318  int fdisk_partition_unset_size(struct fdisk_partition *pa)
     319  {
     320  	if (!pa)
     321  		return -EINVAL;
     322  	FDISK_INIT_UNDEF(pa->size);
     323  	pa->fs_probed = 0;
     324  	return 0;
     325  }
     326  
     327  /**
     328   * fdisk_partition_get_size:
     329   * @pa: partition
     330   *
     331   * The zero is also valid size. The function may return random undefined
     332   * value when size is undefined (for example after fdisk_partition_unset_size()).
     333   * Always use fdisk_partition_has_size() to be sure that you work with valid
     334   * numbers.
     335   *
     336   * Returns: size offset in sectors
     337   */
     338  fdisk_sector_t fdisk_partition_get_size(struct fdisk_partition *pa)
     339  {
     340  	return pa->size;
     341  }
     342  
     343  /**
     344   * fdisk_partition_has_size:
     345   * @pa: partition
     346   *
     347   * Returns: 1 or 0
     348   */
     349  int fdisk_partition_has_size(struct fdisk_partition *pa)
     350  {
     351  	return pa && !FDISK_IS_UNDEF(pa->size);
     352  }
     353  
     354  /**
     355   * fdisk_partition_size_explicit:
     356   * @pa: partition
     357   * @enable: 0|1
     358   *
     359   * By default libfdisk aligns the size when add the new partition (by
     360   * fdisk_add_partition()). If you want to disable this functionality use
     361   * @enable = 1.
     362   *
     363   * Returns: 0 on success, <0 on error.
     364   */
     365  int fdisk_partition_size_explicit(struct fdisk_partition *pa, int enable)
     366  {
     367  	if (!pa)
     368  		return -EINVAL;
     369  	pa->size_explicit = enable ? 1 : 0;
     370  	return 0;
     371  }
     372  
     373  /**
     374   * fdisk_partition_set_partno:
     375   * @pa: partition
     376   * @num: partition number (0 is the first partition, maximal is SIZE_MAX-1)
     377   *
     378   * Note that zero is valid partno too. Use fdisk_partition_unset_partno() to
     379   * undefine the partno.
     380   *
     381   * Returns: 0 on success, <0 on error.
     382   */
     383  int fdisk_partition_set_partno(struct fdisk_partition *pa, size_t num)
     384  {
     385  	if (!pa)
     386  		return -EINVAL;
     387  	if (FDISK_IS_UNDEF(num))
     388  		return -ERANGE;
     389  	pa->partno = num;
     390  	return 0;
     391  }
     392  
     393  /**
     394   * fdisk_partition_unset_partno:
     395   * @pa: partition
     396   *
     397   * Sets the partno as undefined. See fdisk_partition_has_partno().
     398   *
     399   * Returns: 0 on success, <0 on error.
     400   */
     401  int fdisk_partition_unset_partno(struct fdisk_partition *pa)
     402  {
     403  	if (!pa)
     404  		return -EINVAL;
     405  	FDISK_INIT_UNDEF(pa->partno);
     406  	return 0;
     407  }
     408  
     409  /**
     410   * fdisk_partition_get_partno:
     411   * @pa: partition
     412   *
     413   * The zero is also valid partition number. The function may return random
     414   * value when partno is undefined (for example after fdisk_partition_unset_partno()).
     415   * Always use fdisk_partition_has_partno() to be sure that you work with valid
     416   * numbers.
     417   *
     418   * Returns: partition number (0 is the first partition)
     419   */
     420  size_t fdisk_partition_get_partno(struct fdisk_partition *pa)
     421  {
     422  	return pa->partno;
     423  }
     424  
     425  /**
     426   * fdisk_partition_has_partno:
     427   * @pa: partition
     428   *
     429   * Returns: 1 or 0
     430   */
     431  int fdisk_partition_has_partno(struct fdisk_partition *pa)
     432  {
     433  	return pa && !FDISK_IS_UNDEF(pa->partno);
     434  }
     435  
     436  
     437  /**
     438   * fdisk_partition_cmp_partno:
     439   * @a: partition
     440   * @b: partition
     441   *
     442   * Compares partitions according to partition number See fdisk_table_sort_partitions().
     443   *
     444   * Return: 0 if the same, <0 if @b greater, >0 if @a greater.
     445   */
     446  int fdisk_partition_cmp_partno(struct fdisk_partition *a,
     447  			       struct fdisk_partition *b)
     448  {
     449  	return a->partno - b->partno;
     450  }
     451  
     452  /**
     453   * fdisk_partition_partno_follow_default
     454   * @pa: partition
     455   * @enable: 0|1
     456   *
     457   * When @pa used as a template for fdisk_add_partition() when force label driver
     458   * to add a new partition to the default (next) position.
     459   *
     460   * Returns: 0 on success, <0 on error.
     461   */
     462  int fdisk_partition_partno_follow_default(struct fdisk_partition *pa, int enable)
     463  {
     464  	if (!pa)
     465  		return -EINVAL;
     466  	pa->partno_follow_default = enable ? 1 : 0;
     467  	return 0;
     468  }
     469  
     470  /**
     471   * fdisk_partition_set_type:
     472   * @pa: partition
     473   * @type: partition type
     474   *
     475   * Sets partition type.
     476   *
     477   * Returns: 0 on success, <0 on error.
     478   */
     479  int fdisk_partition_set_type(struct fdisk_partition *pa,
     480  			     struct fdisk_parttype *type)
     481  {
     482  	if (!pa)
     483  		return -EINVAL;
     484  
     485  	fdisk_ref_parttype(type);
     486  	fdisk_unref_parttype(pa->type);
     487  	pa->type = type;
     488  
     489  	return 0;
     490  }
     491  
     492  /**
     493   * fdisk_partition_get_type:
     494   * @pa: partition
     495   *
     496   * Returns: pointer to partition type.
     497   */
     498  struct fdisk_parttype *fdisk_partition_get_type(struct fdisk_partition *pa)
     499  {
     500  	return pa ? pa->type : NULL;
     501  }
     502  
     503  /**
     504   * fdisk_partition_set_name:
     505   * @pa: partition
     506   * @name: partition name
     507   *
     508   * Returns: 0 on success, <0 on error.
     509   */
     510  int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name)
     511  {
     512  	if (!pa)
     513  		return -EINVAL;
     514  	return strdup_to_struct_member(pa, name, name);
     515  }
     516  
     517  /**
     518   * fdisk_partition_get_name:
     519   * @pa: partition
     520   *
     521   * Returns: partition name
     522   */
     523  const char *fdisk_partition_get_name(struct fdisk_partition *pa)
     524  {
     525  	return pa ? pa->name : NULL;
     526  }
     527  
     528  /**
     529   * fdisk_partition_set_uuid:
     530   * @pa: partition
     531   * @uuid: UUID of the partition
     532   *
     533   * Returns: 0 on success, <0 on error.
     534   */
     535  int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid)
     536  {
     537  	if (!pa)
     538  		return -EINVAL;
     539  	return strdup_to_struct_member(pa, uuid, uuid);
     540  }
     541  
     542  /**
     543   * fdisk_partition_has_end:
     544   * @pa: partition
     545   *
     546   * Returns: 1 if the partition has defined last sector
     547   */
     548  int fdisk_partition_has_end(struct fdisk_partition *pa)
     549  {
     550  	return pa && !FDISK_IS_UNDEF(pa->start) && !FDISK_IS_UNDEF(pa->size);
     551  }
     552  
     553  /**
     554   * fdisk_partition_get_end:
     555   * @pa: partition
     556   *
     557   * This function may returns absolute non-sense, always check
     558   * fdisk_partition_has_end().
     559   *
     560   * Note that partition end is defined by fdisk_partition_set_start() and
     561   * fdisk_partition_set_size().
     562   *
     563   * Returns: last partition sector LBA.
     564   */
     565  fdisk_sector_t fdisk_partition_get_end(struct fdisk_partition *pa)
     566  {
     567  	return pa->start + pa->size - (pa->size == 0 ? 0 : 1);
     568  }
     569  
     570  /**
     571   * fdisk_partition_end_follow_default
     572   * @pa: partition
     573   * @enable: 0|1
     574   *
     575   * When @pa used as a template for fdisk_add_partition() when force label
     576   * driver to use all the possible space for the new partition.
     577   *
     578   * Returns: 0 on success, <0 on error.
     579   */
     580  int fdisk_partition_end_follow_default(struct fdisk_partition *pa, int enable)
     581  {
     582  	if (!pa)
     583  		return -EINVAL;
     584  	pa->end_follow_default = enable ? 1 : 0;
     585  	return 0;
     586  }
     587  
     588  /**
     589   * fdisk_partition_end_is_default:
     590   * @pa: partition
     591   *
     592   * Returns: 1 if the partition follows default
     593   */
     594  int fdisk_partition_end_is_default(struct fdisk_partition *pa)
     595  {
     596  	assert(pa);
     597  	return pa->end_follow_default;
     598  }
     599  
     600  /**
     601   * fdisk_partition_get_uuid:
     602   * @pa: partition
     603   *
     604   * Returns: partition UUID as string
     605   */
     606  const char *fdisk_partition_get_uuid(struct fdisk_partition *pa)
     607  {
     608  	return pa ? pa->uuid : NULL;
     609  }
     610  
     611  /**
     612   * fdisk_partition_get_attrs:
     613   * @pa: partition
     614   *
     615   * Returns: partition attributes in string format
     616   */
     617  const char *fdisk_partition_get_attrs(struct fdisk_partition *pa)
     618  {
     619  	return pa ? pa->attrs : NULL;
     620  }
     621  
     622  /**
     623   * fdisk_partition_set_attrs:
     624   * @pa: partition
     625   * @attrs: attributes
     626   *
     627   * Sets @attrs to @pa.
     628   *
     629   * Return: 0 on success, <0 on error.
     630   */
     631  int fdisk_partition_set_attrs(struct fdisk_partition *pa, const char *attrs)
     632  {
     633  	if (!pa)
     634  		return -EINVAL;
     635  	return strdup_to_struct_member(pa, attrs, attrs);
     636  }
     637  
     638  /**
     639   * fdisk_partition_is_nested:
     640   * @pa: partition
     641   *
     642   * Returns: 1 if the partition is nested (e.g. MBR logical partition)
     643   */
     644  int fdisk_partition_is_nested(struct fdisk_partition *pa)
     645  {
     646  	return pa && !FDISK_IS_UNDEF(pa->parent_partno);
     647  }
     648  
     649  /**
     650   * fdisk_partition_is_container:
     651   * @pa: partition
     652   *
     653   * Returns: 1 if the partition is container (e.g. MBR extended partition)
     654   */
     655  int fdisk_partition_is_container(struct fdisk_partition *pa)
     656  {
     657  	return pa && pa->container;
     658  }
     659  
     660  /**
     661   * fdisk_partition_get_parent:
     662   * @pa: partition
     663   * @parent: parent parno
     664   *
     665   * Returns: returns devno of the parent
     666   */
     667  int fdisk_partition_get_parent(struct fdisk_partition *pa, size_t *parent)
     668  {
     669  	if (pa && parent)
     670  		*parent = pa->parent_partno;
     671  	else
     672  		return -EINVAL;
     673  	return 0;
     674  }
     675  
     676  /**
     677   * fdisk_partition_is_used:
     678   * @pa: partition
     679   *
     680   * Returns: 1 if the partition points to some area
     681   */
     682  int fdisk_partition_is_used(struct fdisk_partition *pa)
     683  {
     684  	return pa && pa->used;
     685  }
     686  
     687  /**
     688   * fdisk_partition_is_bootable:
     689   * @pa: partition
     690   *
     691   * Returns: 1 if the partition has enabled boot flag
     692   */
     693  int fdisk_partition_is_bootable(struct fdisk_partition *pa)
     694  {
     695  	return pa && pa->boot == 1;
     696  }
     697  
     698  /**
     699   * fdisk_partition_is_freespace:
     700   * @pa: partition
     701   *
     702   * Returns: 1 if @pa points to freespace
     703   */
     704  int fdisk_partition_is_freespace(struct fdisk_partition *pa)
     705  {
     706  	return pa && pa->freespace;
     707  }
     708  
     709  /**
     710   * fdisk_partition_is_wholedisk:
     711   * @pa: partition
     712   *
     713   * Returns: 1 if the partition is special whole-disk (e.g. SUN) partition
     714   */
     715  int fdisk_partition_is_wholedisk(struct fdisk_partition *pa)
     716  {
     717  	return pa && pa->wholedisk;
     718  }
     719  
     720  /**
     721   * fdisk_partition_next_partno:
     722   * @pa: partition
     723   * @cxt: context
     724   * @n: returns partition number
     725   *
     726   * If @pa specified and partno-follow-default (see fdisk_partition_partno_follow_default())
     727   * enabled then returns next expected partno or -ERANGE on error.
     728   *
     729   * If @pa is NULL, or @pa does not specify any semantic for the next partno
     730   * then use Ask API to ask user for the next partno. In this case returns 1 if
     731   * no free partition available. If fdisk dialogs are disabled then returns -EINVAL.
     732   *
     733   * Returns: 0 on success, <0 on error, or 1 for non-free partno by Ask API.
     734   */
     735  int fdisk_partition_next_partno(
     736  		struct fdisk_partition *pa,
     737  		struct fdisk_context *cxt,
     738  		size_t *n)
     739  {
     740  	if (!cxt || !n)
     741  		return -EINVAL;
     742  
     743  	if (pa && pa->partno_follow_default) {
     744  		size_t i;
     745  
     746  		DBG(PART, ul_debugobj(pa, "next partno (follow default)"));
     747  
     748  		for (i = 0; i < cxt->label->nparts_max; i++) {
     749  			if (!fdisk_is_partition_used(cxt, i)) {
     750  				*n = i;
     751  				return 0;
     752  			}
     753  		}
     754  		return -ERANGE;
     755  
     756  	}
     757  
     758  	if (pa && fdisk_partition_has_partno(pa)) {
     759  
     760  		DBG(PART, ul_debugobj(pa, "next partno (specified=%zu)", pa->partno));
     761  
     762  		if (pa->partno >= cxt->label->nparts_max ||
     763  		    fdisk_is_partition_used(cxt, pa->partno))
     764  			return -ERANGE;
     765  		*n = pa->partno;
     766  		return 0;
     767  
     768  	}
     769  
     770  	if (fdisk_has_dialogs(cxt))
     771  		return fdisk_ask_partnum(cxt, n, 1);
     772  
     773  	return -EINVAL;
     774  }
     775  
     776  static int probe_partition_content(struct fdisk_context *cxt, struct fdisk_partition *pa)
     777  {
     778  	int rc = 1;	/* nothing */
     779  
     780  	DBG(PART, ul_debugobj(pa, "start probe #%zu partition [cxt %p] >>>", pa->partno, cxt));
     781  
     782  	/* zeroize the current setting */
     783  	strdup_to_struct_member(pa, fstype, NULL);
     784  	strdup_to_struct_member(pa, fsuuid, NULL);
     785  	strdup_to_struct_member(pa, fslabel, NULL);
     786  
     787  	if (!fdisk_partition_has_start(pa) ||
     788  	    !fdisk_partition_has_size(pa))
     789  		goto done;
     790  
     791  #ifdef HAVE_LIBBLKID
     792  	else {
     793  		uintmax_t start, size;
     794  
     795  		blkid_probe pr = blkid_new_probe();
     796  		if (!pr)
     797  			goto done;
     798  
     799  		DBG(PART, ul_debugobj(pa, "blkid prober: %p", pr));
     800  
     801  		blkid_probe_enable_superblocks(pr, 1);
     802  		blkid_probe_set_superblocks_flags(pr,
     803  				BLKID_SUBLKS_MAGIC |
     804  				BLKID_SUBLKS_TYPE |
     805  				BLKID_SUBLKS_LABEL |
     806  				BLKID_SUBLKS_UUID |
     807  				BLKID_SUBLKS_BADCSUM);
     808  
     809  		start = fdisk_partition_get_start(pa) * fdisk_get_sector_size(cxt);
     810  		size = fdisk_partition_get_size(pa) * fdisk_get_sector_size(cxt);
     811  
     812  		if (blkid_probe_set_device(pr, cxt->dev_fd, start, size) == 0
     813  		    && blkid_do_fullprobe(pr) == 0) {
     814  
     815  			const char *data;
     816  			rc = 0;
     817  
     818  			if (!blkid_probe_lookup_value(pr, "TYPE",  &data, NULL))
     819  				rc = strdup_to_struct_member(pa, fstype, data);
     820  
     821  			if (!rc && !blkid_probe_lookup_value(pr, "LABEL", &data, NULL))
     822  				rc = strdup_to_struct_member(pa, fslabel, data);
     823  
     824  			if (!rc && !blkid_probe_lookup_value(pr, "UUID",  &data, NULL))
     825  				rc = strdup_to_struct_member(pa, fsuuid, data);
     826  		}
     827  
     828  		blkid_free_probe(pr);
     829  		pa->fs_probed = 1;
     830  	}
     831  #endif /* HAVE_LIBBLKID */
     832  
     833  done:
     834  	DBG(PART, ul_debugobj(pa, "<<< end probe #%zu partition[cxt %p, rc=%d]", pa->partno, cxt, rc));
     835  	return rc;
     836  }
     837  
     838  /**
     839   * fdisk_partition_to_string:
     840   * @pa: partition
     841   * @cxt: context
     842   * @id: field (FDISK_FIELD_*)
     843   * @data: returns string with allocated data
     844   *
     845   * Returns info about partition converted to printable string.
     846   *
     847   * For example
     848   * <informalexample>
     849   *   <programlisting>
     850   *      struct fdisk_partition *pa;
     851   *
     852   *      fdisk_get_partition(cxt, 0, &pa);
     853   *	fdisk_partition_to_string(pa, FDISK_FIELD_UUID, &data);
     854   *	printf("first partition uuid: %s\n", data);
     855   *	free(data);
     856   *	fdisk_unref_partition(pa);
     857   *   </programlisting>
     858   * </informalexample>
     859   *
     860   * returns UUID for the first partition.
     861   *
     862   * Returns: 0 on success, otherwise, a corresponding error.
     863   */
     864  int fdisk_partition_to_string(struct fdisk_partition *pa,
     865  			      struct fdisk_context *cxt,
     866  			      int id,
     867  			      char **data)
     868  {
     869  	char *p = NULL;
     870  	int rc = 0;
     871  	uint64_t x;
     872  
     873  	if (!pa || !cxt || !data)
     874  		return -EINVAL;
     875  
     876  	switch (id) {
     877  	case FDISK_FIELD_DEVICE:
     878  		if (pa->freespace)
     879  			p = strdup(_("Free space"));
     880  		else if (fdisk_partition_has_partno(pa) && cxt->dev_path) {
     881  			if (cxt->label->flags & FDISK_LABEL_FL_INCHARS_PARTNO)
     882  				rc = asprintf(&p, "%c", (int) pa->partno + 'a');
     883  			else
     884  				p = fdisk_partname(cxt->dev_path, pa->partno + 1);
     885  		}
     886  		break;
     887  	case FDISK_FIELD_BOOT:
     888  		p = fdisk_partition_is_bootable(pa) ? strdup("*") : NULL;
     889  		break;
     890  	case FDISK_FIELD_START:
     891  		if (fdisk_partition_has_start(pa)) {
     892  			x = fdisk_cround(cxt, pa->start);
     893  			rc = pa->start_post ?
     894  				asprintf(&p, "%"PRIu64"%c", x, pa->start_post) :
     895  				asprintf(&p, "%"PRIu64, x);
     896  		}
     897  		break;
     898  	case FDISK_FIELD_END:
     899  		if (fdisk_partition_has_end(pa)) {
     900  			x = fdisk_cround(cxt, fdisk_partition_get_end(pa));
     901  			rc = pa->end_post ?
     902  					asprintf(&p, "%"PRIu64"%c", x, pa->end_post) :
     903  					asprintf(&p, "%"PRIu64, x);
     904  		}
     905  		break;
     906  	case FDISK_FIELD_SIZE:
     907  		if (fdisk_partition_has_size(pa)) {
     908  			uint64_t sz = pa->size * cxt->sector_size;
     909  
     910  			switch (cxt->sizeunit) {
     911  			case FDISK_SIZEUNIT_BYTES:
     912  				rc = asprintf(&p, "%"PRIu64"", sz);
     913  				break;
     914  			case FDISK_SIZEUNIT_HUMAN:
     915  				if (fdisk_is_details(cxt))
     916  					rc = pa->size_post ?
     917  							asprintf(&p, "%"PRIu64"%c", sz, pa->size_post) :
     918  							asprintf(&p, "%"PRIu64, sz);
     919  				else {
     920  					p = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
     921  					if (!p)
     922  						rc = -ENOMEM;
     923  				}
     924  				break;
     925  			}
     926  		}
     927  		break;
     928  	case FDISK_FIELD_CYLINDERS:
     929  	{
     930  		uintmax_t sz = fdisk_partition_has_size(pa) ? pa->size : 0;
     931  		if (sz)
     932  			/* Why we need to cast that to uintmax_t? */
     933  			rc = asprintf(&p, "%ju", (uintmax_t)(sz / (cxt->geom.heads * cxt->geom.sectors)) + 1);
     934  		break;
     935  	}
     936  	case FDISK_FIELD_SECTORS:
     937  		rc = asprintf(&p, "%ju",
     938  			fdisk_partition_has_size(pa) ? (uintmax_t) pa->size : 0);
     939  		break;
     940  	case FDISK_FIELD_BSIZE:
     941  		rc = asprintf(&p, "%"PRIu64, pa->bsize);
     942  		break;
     943  	case FDISK_FIELD_FSIZE:
     944  		rc = asprintf(&p, "%"PRIu64, pa->fsize);
     945  		break;
     946  	case FDISK_FIELD_CPG:
     947  		rc = asprintf(&p, "%"PRIu64, pa->cpg);
     948  		break;
     949  	case FDISK_FIELD_TYPE:
     950  		p = pa->type && pa->type->name ? strdup(_(pa->type->name)) : NULL;
     951  		break;
     952  	case FDISK_FIELD_TYPEID:
     953  		if (pa->type && fdisk_parttype_get_string(pa->type))
     954  			rc = asprintf(&p, "%s", fdisk_parttype_get_string(pa->type));
     955  		else if (pa->type)
     956  			rc = asprintf(&p, "%x", fdisk_parttype_get_code(pa->type));
     957  		break;
     958  	case FDISK_FIELD_UUID:
     959  		p = pa->uuid && *pa->uuid? strdup(pa->uuid) : NULL;
     960  		break;
     961  	case FDISK_FIELD_NAME:
     962  		p = pa->name && *pa->name ? strdup(pa->name) : NULL;
     963  		break;
     964  	case FDISK_FIELD_ATTR:
     965  		p = pa->attrs && *pa->attrs ? strdup(pa->attrs) : NULL;
     966  		break;
     967  	case FDISK_FIELD_SADDR:
     968  		p = pa->start_chs && *pa->start_chs ? strdup(pa->start_chs) : NULL;
     969  		break;
     970  	case FDISK_FIELD_EADDR:
     971  		p = pa->end_chs && *pa->end_chs? strdup(pa->end_chs) : NULL;
     972  		break;
     973  	case FDISK_FIELD_FSUUID:
     974  		if (pa->fs_probed || probe_partition_content(cxt, pa) == 0)
     975  			p = pa->fsuuid && *pa->fsuuid ? strdup(pa->fsuuid) : NULL;
     976  		break;
     977  	case FDISK_FIELD_FSLABEL:
     978  		if (pa->fs_probed || probe_partition_content(cxt, pa) == 0)
     979  			p = pa->fslabel && *pa->fslabel ? strdup(pa->fslabel) : NULL;
     980  		break;
     981  	case FDISK_FIELD_FSTYPE:
     982  		if (pa->fs_probed || probe_partition_content(cxt, pa) == 0)
     983  			p = pa->fstype && *pa->fstype ? strdup(pa->fstype) : NULL;
     984  		break;
     985  	default:
     986  		return -EINVAL;
     987  	}
     988  
     989  	if (rc < 0) {
     990  		rc = -ENOMEM;
     991  		free(p);
     992  		p = NULL;
     993  
     994  	} else if (rc > 0)
     995  		rc = 0;
     996  
     997  	*data = p;
     998  
     999  	return rc;
    1000  }
    1001  
    1002  
    1003  /**
    1004   * fdisk_get_partition:
    1005   * @cxt: context
    1006   * @partno: partition number (0 is the first partition)
    1007   * @pa: returns data about partition
    1008   *
    1009   * Reads disklabel and fills in @pa with data about partition @n.
    1010   *
    1011   * Note that partno may address unused partition and then this function does
    1012   * not fill anything to @pa.  See fdisk_is_partition_used(). If @pa points to
    1013   * NULL then the function allocates a newly allocated fdisk_partition struct,
    1014   * use fdisk_unref_partition() to deallocate.
    1015   *
    1016   * Returns: 0 on success, otherwise, a corresponding error.
    1017   */
    1018  int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
    1019  			struct fdisk_partition **pa)
    1020  {
    1021  	int rc;
    1022  	struct fdisk_partition *np = NULL;
    1023  
    1024  	if (!cxt || !cxt->label || !pa)
    1025  		return -EINVAL;
    1026  	if (!cxt->label->op->get_part)
    1027  		return -ENOSYS;
    1028  	if (!fdisk_is_partition_used(cxt, partno))
    1029  		return -EINVAL;
    1030  
    1031  	if (!*pa) {
    1032  		np = *pa = fdisk_new_partition();
    1033  		if (!*pa)
    1034  			return -ENOMEM;
    1035  	} else
    1036  		fdisk_reset_partition(*pa);
    1037  
    1038  	(*pa)->partno = partno;
    1039  	rc = cxt->label->op->get_part(cxt, partno, *pa);
    1040  
    1041  	if (rc) {
    1042  		if (np) {
    1043  			fdisk_unref_partition(np);
    1044  			*pa = NULL;
    1045  		} else
    1046  			fdisk_reset_partition(*pa);
    1047  	} else
    1048  		(*pa)->size_explicit = 1;
    1049  	return rc;
    1050  }
    1051  
    1052  static struct fdisk_partition *area_by_offset(
    1053  			struct fdisk_table *tb,
    1054  			struct fdisk_partition *cur,
    1055  			fdisk_sector_t off)
    1056  {
    1057  	struct fdisk_partition *pa = NULL;
    1058  	struct fdisk_iter itr;
    1059  
    1060  	fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
    1061  
    1062  	while (fdisk_table_next_partition(tb, &itr, &pa) == 0) {
    1063  		if (!fdisk_partition_has_start(pa) || !fdisk_partition_has_size(pa))
    1064  			continue;
    1065  		if (fdisk_partition_is_nested(cur) &&
    1066  		    pa->parent_partno != cur->parent_partno)
    1067  			continue;
    1068  		if (off >= pa->start && off < pa->start + pa->size)
    1069  			return pa;
    1070  	}
    1071  
    1072  	return NULL;
    1073  }
    1074  
    1075  static int resize_get_first_possible(
    1076  			struct fdisk_table *tb,
    1077  			struct fdisk_partition *cur,
    1078  			fdisk_sector_t *start)
    1079  {
    1080  	struct fdisk_partition *pa = NULL, *first = NULL;
    1081  	struct fdisk_iter itr;
    1082  
    1083  	fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
    1084  
    1085  	*start = 0;
    1086  	DBG(TAB, ul_debugobj(tb, "checking first possible before start=%ju", (uintmax_t) cur->start));
    1087  
    1088  
    1089  	while (fdisk_table_next_partition(tb, &itr, &pa) == 0) {
    1090  
    1091  		if (pa->start > cur->start || pa == cur)
    1092  			break;
    1093  
    1094  		DBG(TAB, ul_debugobj(tb, " checking entry %p [partno=%zu start=%ju, end=%ju, size=%ju%s%s%s]",
    1095  			pa,
    1096  			fdisk_partition_get_partno(pa),
    1097  			(uintmax_t) fdisk_partition_get_start(pa),
    1098  			(uintmax_t) fdisk_partition_get_end(pa),
    1099  			(uintmax_t) fdisk_partition_get_size(pa),
    1100  			fdisk_partition_is_freespace(pa) ? " freespace" : "",
    1101  			fdisk_partition_is_nested(pa)    ? " nested"    : "",
    1102  			fdisk_partition_is_container(pa) ? " container" : ""));
    1103  
    1104  
    1105  		if (!fdisk_partition_is_freespace(pa)) {
    1106  			DBG(TAB, ul_debugobj(tb, "  ignored (no freespace)"));
    1107  			first = NULL;
    1108  			continue;
    1109  		}
    1110  		if (!fdisk_partition_has_start(pa) || !fdisk_partition_has_size(pa)) {
    1111  			DBG(TAB, ul_debugobj(tb, "  ignored (no start/size)"));
    1112  			first = NULL;
    1113  			continue;
    1114  		}
    1115  		/* The current is nested, free space has to be nested within the same parent */
    1116  		if (fdisk_partition_is_nested(cur)
    1117  		    && pa->parent_partno != cur->parent_partno) {
    1118  			DBG(TAB, ul_debugobj(tb, "  ignore (nested required)"));
    1119  			first = NULL;
    1120  			continue;
    1121  		}
    1122  		if (pa->start + pa->size <= cur->start) {
    1123  			first = pa;
    1124  			DBG(TAB, ul_debugobj(tb, "  entry usable"));
    1125  		}
    1126  	}
    1127  
    1128  	if (first)
    1129  		*start = first->start;
    1130  	else
    1131  		DBG(PART, ul_debugobj(cur, "resize: nothing usable before %ju", (uintmax_t) cur->start));
    1132  
    1133  	return first ? 0 : -1;
    1134  }
    1135  
    1136  /*
    1137   * Verify that area addressed by @start is freespace or the @cur[rent]
    1138   * partition and continue to the next table entries until it's freespace, and
    1139   * counts size of all this space.
    1140   *
    1141   * This is core of the partition start offset move operation. We can move the
    1142   * start within the current partition of to the another free space. It's
    1143   * forbidden to move start of the partition to another already defined
    1144   * partition.
    1145   */
    1146  static int resize_get_last_possible(
    1147  			struct fdisk_table *tb,
    1148  			struct fdisk_partition *cur,
    1149  			fdisk_sector_t start,
    1150  			fdisk_sector_t *maxsz)
    1151  {
    1152  	struct fdisk_partition *pa = NULL, *last = NULL;
    1153  	struct fdisk_iter itr;
    1154  
    1155  	fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
    1156  
    1157  	*maxsz = 0;
    1158  	DBG(TAB, ul_debugobj(tb, "checking last possible for start=%ju", (uintmax_t) start));
    1159  
    1160  
    1161  	while (fdisk_table_next_partition(tb, &itr, &pa) == 0) {
    1162  
    1163  		DBG(TAB, ul_debugobj(tb, " checking entry %p [partno=%zu start=%ju, end=%ju, size=%ju%s%s%s]",
    1164  			pa,
    1165  			fdisk_partition_get_partno(pa),
    1166  			(uintmax_t) fdisk_partition_get_start(pa),
    1167  			(uintmax_t) fdisk_partition_get_end(pa),
    1168  			(uintmax_t) fdisk_partition_get_size(pa),
    1169  			fdisk_partition_is_freespace(pa) ? " freespace" : "",
    1170  			fdisk_partition_is_nested(pa)    ? " nested"    : "",
    1171  			fdisk_partition_is_container(pa) ? " container" : ""));
    1172  
    1173  		if (!fdisk_partition_has_start(pa) ||
    1174  		    !fdisk_partition_has_size(pa) ||
    1175  		    (fdisk_partition_is_container(pa) && pa != cur)) {
    1176  			DBG(TAB, ul_debugobj(tb, "  ignored (no start/size or container)"));
    1177  			continue;
    1178  		}
    1179  
    1180  		if (fdisk_partition_is_nested(pa)
    1181  		    && fdisk_partition_is_container(cur)
    1182  		    && pa->parent_partno == cur->partno) {
    1183  			DBG(TAB, ul_debugobj(tb, "  ignore (nested child of the current partition)"));
    1184  			continue;
    1185  		}
    1186  
    1187  		/* The current is nested, free space has to be nested within the same parent */
    1188  		if (fdisk_partition_is_nested(cur)
    1189  		    && pa->parent_partno != cur->parent_partno) {
    1190  			DBG(TAB, ul_debugobj(tb, "  ignore (nested required)"));
    1191  			continue;
    1192  		}
    1193  
    1194  		if (!last) {
    1195  			if (start >= pa->start &&  start < pa->start + pa->size) {
    1196  				if (fdisk_partition_is_freespace(pa) || pa == cur) {
    1197  					DBG(TAB, ul_debugobj(tb, "  accepted as last"));
    1198  					last = pa;
    1199  				} else {
    1200  					DBG(TAB, ul_debugobj(tb, "  failed to set last"));
    1201  					break;
    1202  				}
    1203  
    1204  
    1205  				*maxsz = pa->size - (start - pa->start);
    1206  				DBG(TAB, ul_debugobj(tb, "  new max=%ju", (uintmax_t) *maxsz));
    1207  			}
    1208  		} else if (!fdisk_partition_is_freespace(pa) && pa != cur) {
    1209  			DBG(TAB, ul_debugobj(tb, "  no free space behind current"));
    1210  			break;
    1211  		} else {
    1212  			last = pa;
    1213  			*maxsz = pa->size - (start - pa->start);
    1214  			DBG(TAB, ul_debugobj(tb, "  new max=%ju (last updated)", (uintmax_t) *maxsz));
    1215  		}
    1216  	}
    1217  
    1218  	if (last)
    1219  		DBG(PART, ul_debugobj(cur, "resize: max size=%ju", (uintmax_t) *maxsz));
    1220  	else
    1221  		DBG(PART, ul_debugobj(cur, "resize: nothing usable after %ju", (uintmax_t) start));
    1222  
    1223  	return last ? 0 : -1;
    1224  }
    1225  
    1226  /*
    1227   * Uses template @tpl to recount start and size change of the partition @res. The
    1228   * @tpl->size and @tpl->start are interpreted as relative to the current setting.
    1229   */
    1230  static int recount_resize(
    1231  			struct fdisk_context *cxt, size_t partno,
    1232  			struct fdisk_partition *res, struct fdisk_partition *tpl)
    1233  {
    1234  	fdisk_sector_t start, size, xsize;
    1235  	struct fdisk_partition *cur = NULL;
    1236  	struct fdisk_table *tb = NULL;
    1237  	int rc;
    1238  
    1239  	DBG(PART, ul_debugobj(tpl, ">>> resize requested"));
    1240  
    1241  	FDISK_INIT_UNDEF(start);
    1242  	FDISK_INIT_UNDEF(size);
    1243  
    1244  	rc = fdisk_get_partitions(cxt, &tb);
    1245  	if (!rc) {
    1246  		/* For resize we do not follow grain to detect free-space, but
    1247  		 * we support to resize with very small granulation. */
    1248  		unsigned long org = cxt->grain;
    1249  
    1250  		cxt->grain = cxt->sector_size;
    1251  		rc = fdisk_get_freespaces(cxt, &tb);
    1252  		cxt->grain = org;
    1253  	}
    1254  	if (rc) {
    1255  		fdisk_unref_table(tb);
    1256  		return rc;
    1257  	}
    1258  
    1259  	fdisk_table_sort_partitions(tb, fdisk_partition_cmp_start);
    1260  
    1261  	DBG(PART, ul_debugobj(tpl, "resize partition partno=%zu in table:", partno));
    1262  	ON_DBG(PART, fdisk_debug_print_table(tb));
    1263  
    1264  	cur = fdisk_table_get_partition_by_partno(tb, partno);
    1265  	if (!cur) {
    1266  		fdisk_unref_table(tb);
    1267  		return -EINVAL;
    1268  	}
    1269  
    1270  	/* 1a) set new start - change relative to the current on-disk setting */
    1271  	if (tpl->movestart && fdisk_partition_has_start(tpl)) {
    1272  		start = fdisk_partition_get_start(cur);
    1273  		if (tpl->movestart == FDISK_MOVE_DOWN) {
    1274  			if (fdisk_partition_get_start(tpl) > start)
    1275  				goto erange;
    1276  			start -= fdisk_partition_get_start(tpl);
    1277  		} else
    1278  			start += fdisk_partition_get_start(tpl);
    1279  
    1280  		DBG(PART, ul_debugobj(tpl, "resize: moving start %s relative, new start: %ju",
    1281  				tpl->movestart == FDISK_MOVE_DOWN  ? "DOWN" : "UP", (uintmax_t)start));
    1282  
    1283  	/* 1b) set new start - try freespace before the curret partition */
    1284  	} else if (tpl->movestart == FDISK_MOVE_DOWN) {
    1285  
    1286  		if (resize_get_first_possible(tb, cur, &start) != 0)
    1287  			goto erange;
    1288  
    1289  		DBG(PART, ul_debugobj(tpl, "resize: moving start DOWN (first possible), new start: %ju",
    1290  				(uintmax_t)start));
    1291  
    1292  	/* 1c) set new start - absolute number */
    1293  	} else if (fdisk_partition_has_start(tpl)) {
    1294  		start = fdisk_partition_get_start(tpl);
    1295  		DBG(PART, ul_debugobj(tpl, "resize: moving start to absolute offset: %ju",
    1296  		                      (uintmax_t)start));
    1297  	}
    1298  
    1299  	/* 2) verify that start is within the current partition or any freespace area */
    1300  	if (!FDISK_IS_UNDEF(start)) {
    1301  		struct fdisk_partition *area = area_by_offset(tb, cur, start);
    1302  
    1303  		if (area == cur)
    1304  			DBG(PART, ul_debugobj(tpl, "resize: start points to the current partition"));
    1305  		else if (area && fdisk_partition_is_freespace(area))
    1306  			DBG(PART, ul_debugobj(tpl, "resize: start points to freespace"));
    1307  		else if (!area && start >= cxt->first_lba && start < cxt->first_lba + (cxt->grain / cxt->sector_size))
    1308  			DBG(PART, ul_debugobj(tpl, "resize: start points before first partition"));
    1309  		else {
    1310  			DBG(PART, ul_debugobj(tpl, "resize: start verification failed"));
    1311  			goto erange;
    1312  		}
    1313  	} else {
    1314  		/* no change, start points to the current partition */
    1315  		DBG(PART, ul_debugobj(tpl, "resize: start unchanged"));
    1316  		start = fdisk_partition_get_start(cur);
    1317  	}
    1318  
    1319  	/* 3a) set new size -- reduce */
    1320  	if (tpl->resize == FDISK_RESIZE_REDUCE && fdisk_partition_has_size(tpl)) {
    1321  		DBG(PART, ul_debugobj(tpl, "resize: reduce"));
    1322  		size = fdisk_partition_get_size(cur);
    1323  		if (fdisk_partition_get_size(tpl) > size)
    1324  			goto erange;
    1325  		size -= fdisk_partition_get_size(tpl);
    1326  
    1327  	/* 3b) set new size -- enlarge */
    1328  	} else if (tpl->resize == FDISK_RESIZE_ENLARGE && fdisk_partition_has_size(tpl)) {
    1329  		DBG(PART, ul_debugobj(tpl, "resize: enlarge"));
    1330  		size = fdisk_partition_get_size(cur);
    1331  		size += fdisk_partition_get_size(tpl);
    1332  
    1333  	/* 3c) set new size -- no size specified, enlarge to all freespace */
    1334  	} else if (tpl->resize == FDISK_RESIZE_ENLARGE) {
    1335  		DBG(PART, ul_debugobj(tpl, "resize: enlarge to all possible"));
    1336  		if (resize_get_last_possible(tb, cur, start, &size))
    1337  			goto erange;
    1338  
    1339  	/* 3d) set new size -- absolute number */
    1340  	} else if (fdisk_partition_has_size(tpl)) {
    1341  		DBG(PART, ul_debugobj(tpl, "resize: new absolute size"));
    1342  		size = fdisk_partition_get_size(tpl);
    1343  	}
    1344  
    1345  	/* 4) verify that size is within the current partition or next free space */
    1346  	xsize = !FDISK_IS_UNDEF(size) ? size : fdisk_partition_get_size(cur);
    1347  
    1348  	if (fdisk_partition_has_size(cur)) {
    1349  		fdisk_sector_t maxsz;
    1350  
    1351  		if (resize_get_last_possible(tb, cur, start, &maxsz))
    1352  			goto erange;
    1353  		DBG(PART, ul_debugobj(tpl, "resize: size=%ju, max=%ju",
    1354  					(uintmax_t) xsize, (uintmax_t) maxsz));
    1355  		if (xsize > maxsz)
    1356  			goto erange;
    1357  	}
    1358  
    1359  	if (FDISK_IS_UNDEF(size)) {
    1360  		DBG(PART, ul_debugobj(tpl, "resize: size unchanged (undefined)"));
    1361  	}
    1362  
    1363  
    1364  	DBG(PART, ul_debugobj(tpl, "<<< resize: SUCCESS: start %ju->%ju; size %ju->%ju",
    1365  			(uintmax_t) fdisk_partition_get_start(cur), (uintmax_t) start,
    1366  			(uintmax_t) fdisk_partition_get_size(cur), (uintmax_t) size));
    1367  	res->start = start;
    1368  	res->size = size;
    1369  	fdisk_unref_table(tb);
    1370  	return 0;
    1371  erange:
    1372  	DBG(PART, ul_debugobj(tpl, "<<< resize: FAILED"));
    1373  	fdisk_warnx(cxt, _("Failed to resize partition #%zu."), partno + 1);
    1374  	fdisk_unref_table(tb);
    1375  	return -ERANGE;
    1376  
    1377  }
    1378  
    1379  /**
    1380   * fdisk_set_partition:
    1381   * @cxt: context
    1382   * @partno: partition number (0 is the first partition)
    1383   * @pa: new partition setting
    1384   *
    1385   * Modifies disklabel according to setting with in @pa.
    1386   *
    1387   * Returns: 0 on success, <0 on error.
    1388   */
    1389  int fdisk_set_partition(struct fdisk_context *cxt, size_t partno,
    1390  			struct fdisk_partition *pa)
    1391  {
    1392  	struct fdisk_partition *xpa = pa, *tmp = NULL;
    1393  	int rc, wipe = 0;
    1394  
    1395  	if (!cxt || !cxt->label || !pa)
    1396  		return -EINVAL;
    1397  	if (!cxt->label->op->set_part)
    1398  		return -ENOSYS;
    1399  
    1400  	pa->fs_probed = 0;
    1401  
    1402  	if (!fdisk_is_partition_used(cxt, partno)) {
    1403  		pa->partno = partno;
    1404  		return fdisk_add_partition(cxt, pa, NULL);
    1405  	}
    1406  
    1407  	if (pa->resize || pa->movestart
    1408  	    || fdisk_partition_has_start(pa) || fdisk_partition_has_size(pa)) {
    1409  		xpa = __copy_partition(pa);
    1410  		if (!xpa) {
    1411  			rc = -ENOMEM;
    1412  			goto done;
    1413  		}
    1414  		xpa->movestart = 0;
    1415  		xpa->resize = 0;
    1416  		FDISK_INIT_UNDEF(xpa->size);
    1417  		FDISK_INIT_UNDEF(xpa->start);
    1418  
    1419  		rc = recount_resize(cxt, partno, xpa, pa);
    1420  		if (rc)
    1421  			goto done;
    1422  	}
    1423  
    1424  	DBG(CXT, ul_debugobj(cxt, "setting partition %zu %p (start=%ju, end=%ju, size=%ju)",
    1425  		    partno, xpa,
    1426  		    (uintmax_t) fdisk_partition_get_start(xpa),
    1427  		    (uintmax_t) fdisk_partition_get_end(xpa),
    1428  		    (uintmax_t) fdisk_partition_get_size(xpa)));
    1429  
    1430  	/* disable wipe for old offset/size setting */
    1431  	if (fdisk_get_partition(cxt, partno, &tmp) == 0 && tmp) {
    1432  		wipe = fdisk_set_wipe_area(cxt, fdisk_partition_get_start(tmp),
    1433  						fdisk_partition_get_size(tmp), FALSE);
    1434  		fdisk_unref_partition(tmp);
    1435  	}
    1436  
    1437  	/* call label driver */
    1438  	rc = cxt->label->op->set_part(cxt, partno, xpa);
    1439  
    1440  	/* enable wipe for new offset/size */
    1441  	if (!rc && wipe)
    1442  		fdisk_wipe_partition(cxt, partno, TRUE);
    1443  done:
    1444  	DBG(CXT, ul_debugobj(cxt, "set_partition() rc=%d", rc));
    1445  	if (xpa != pa)
    1446  		fdisk_unref_partition(xpa);
    1447  	return rc;
    1448  }
    1449  
    1450  /**
    1451   * fdisk_wipe_partition:
    1452   * @cxt: fdisk context
    1453   * @partno: partition number
    1454   * @enable: 0 or 1
    1455   *
    1456   * Enable/disable filesystems/RAIDs wiping in area defined by partition start and size.
    1457   *
    1458   * Returns: <0 in case of error, 0 on success
    1459   * Since: 2.29
    1460   */
    1461  int fdisk_wipe_partition(struct fdisk_context *cxt, size_t partno, int enable)
    1462  {
    1463  	struct fdisk_partition *pa = NULL;
    1464  	int rc;
    1465  
    1466  	rc = fdisk_get_partition(cxt, partno, &pa);
    1467  	if (rc)
    1468  		return rc;
    1469  
    1470  	rc = fdisk_set_wipe_area(cxt, fdisk_partition_get_start(pa),
    1471  				      fdisk_partition_get_size(pa), enable);
    1472  	fdisk_unref_partition(pa);
    1473  	return rc < 0 ? rc : 0;
    1474  }
    1475  
    1476  /**
    1477   * fdisk_partition_has_wipe:
    1478   * @cxt: fdisk context
    1479   * @pa: partition
    1480   *
    1481   * Since: 2.30
    1482   *
    1483   * Returns: 1 if the area specified by @pa will be wiped by write command, or 0.
    1484   */
    1485  int fdisk_partition_has_wipe(struct fdisk_context *cxt, struct fdisk_partition *pa)
    1486  {
    1487  	return fdisk_has_wipe_area(cxt, fdisk_partition_get_start(pa),
    1488  					fdisk_partition_get_size(pa));
    1489  }
    1490  
    1491  
    1492  /**
    1493   * fdisk_add_partition:
    1494   * @cxt: fdisk context
    1495   * @pa: template for the partition (or NULL)
    1496   * @partno: NULL or returns new partition number
    1497   *
    1498   * If @pa is not specified or any @pa item is missing the libfdisk will ask by
    1499   * fdisk_ask_ API.
    1500   *
    1501   * The @pa template is important for non-interactive partitioning,
    1502   * especially for MBR where is necessary to differentiate between
    1503   * primary/logical; this is done by start offset or/and partno.
    1504   * The rules for MBR:
    1505   *
    1506   *   A) template specifies start within extended partition: add logical
    1507   *   B) template specifies start out of extended partition: add primary
    1508   *   C) template specifies start (or default), partno < 4: add primary
    1509   *   D) template specifies default start, partno >= 4: add logical
    1510   *
    1511   * otherwise MBR driver uses Ask-API to get missing information.
    1512   *
    1513   * Adds a new partition to disklabel.
    1514   *
    1515   * Returns: 0 on success, <0 on error.
    1516   */
    1517  int fdisk_add_partition(struct fdisk_context *cxt,
    1518  			struct fdisk_partition *pa,
    1519  			size_t *partno)
    1520  {
    1521  	int rc;
    1522  
    1523  	if (!cxt || !cxt->label)
    1524  		return -EINVAL;
    1525  	if (!cxt->label->op->add_part)
    1526  		return -ENOSYS;
    1527  	if (fdisk_missing_geometry(cxt))
    1528  		return -EINVAL;
    1529  
    1530  	if (pa) {
    1531  		pa->fs_probed = 0;
    1532  		DBG(CXT, ul_debugobj(cxt, "adding new partition %p", pa));
    1533  		if (fdisk_partition_has_start(pa))
    1534  			DBG(CXT, ul_debugobj(cxt, "     start: %ju", (uintmax_t) fdisk_partition_get_start(pa)));
    1535  		if (fdisk_partition_has_end(pa))
    1536  			DBG(CXT, ul_debugobj(cxt, "       end: %ju", (uintmax_t) fdisk_partition_get_end(pa)));
    1537  		if (fdisk_partition_has_size(pa))
    1538  			DBG(CXT, ul_debugobj(cxt, "      size: %ju", (uintmax_t) fdisk_partition_get_size(pa)));
    1539  
    1540  		DBG(CXT, ul_debugobj(cxt,         "  defaults: start=%s, end=%s, partno=%s",
    1541  			    pa->start_follow_default ? "yes" : "no",
    1542  			    pa->end_follow_default ? "yes" : "no",
    1543  			    pa->partno_follow_default ? "yes" : "no"));
    1544  	} else
    1545  		DBG(CXT, ul_debugobj(cxt, "adding partition"));
    1546  
    1547  	rc = cxt->label->op->add_part(cxt, pa, partno);
    1548  
    1549  	DBG(CXT, ul_debugobj(cxt, "add partition done (rc=%d)", rc));
    1550  	return rc;
    1551  }
    1552  
    1553  /**
    1554   * fdisk_delete_partition:
    1555   * @cxt: fdisk context
    1556   * @partno: partition number to delete (0 is the first partition)
    1557   *
    1558   * Deletes a @partno partition from disklabel.
    1559   *
    1560   * Returns: 0 on success, <0 on error
    1561   */
    1562  int fdisk_delete_partition(struct fdisk_context *cxt, size_t partno)
    1563  {
    1564  	if (!cxt || !cxt->label)
    1565  		return -EINVAL;
    1566  	if (!cxt->label->op->del_part)
    1567  		return -ENOSYS;
    1568  
    1569  	fdisk_wipe_partition(cxt, partno, 0);
    1570  
    1571  	DBG(CXT, ul_debugobj(cxt, "deleting %s partition number %zd",
    1572  				cxt->label->name, partno));
    1573  	return cxt->label->op->del_part(cxt, partno);
    1574  }
    1575  
    1576  /**
    1577   * fdisk_delete_all_partitions:
    1578   * @cxt: fdisk context
    1579   *
    1580   * Delete all used partitions from disklabel.
    1581   *
    1582   * Returns: 0 on success, otherwise, a corresponding error.
    1583   */
    1584  int fdisk_delete_all_partitions(struct fdisk_context *cxt)
    1585  {
    1586  	size_t i;
    1587  	int rc = 0;
    1588  
    1589  	if (!cxt || !cxt->label)
    1590  		return -EINVAL;
    1591  
    1592  	for (i = 0; i < cxt->label->nparts_max; i++) {
    1593  
    1594  		if (!fdisk_is_partition_used(cxt, i))
    1595  			continue;
    1596  		rc = fdisk_delete_partition(cxt, i);
    1597  		if (rc)
    1598  			break;
    1599  	}
    1600  
    1601  	return rc;
    1602  }
    1603  
    1604  /**
    1605   * fdisk_is_partition_used:
    1606   * @cxt: context
    1607   * @n: partition number (0 is the first partition)
    1608   *
    1609   * Check if the partition number @n is used by partition table. This function
    1610   * does not check if the device is used (e.g. mounted) by system!
    1611   *
    1612   * This is faster than fdisk_get_partition() + fdisk_partition_is_used().
    1613   *
    1614   * Returns: 0 or 1
    1615   */
    1616  int fdisk_is_partition_used(struct fdisk_context *cxt, size_t n)
    1617  {
    1618  	if (!cxt || !cxt->label)
    1619  		return -EINVAL;
    1620  	if (!cxt->label->op->part_is_used)
    1621  		return -ENOSYS;
    1622  
    1623  	return cxt->label->op->part_is_used(cxt, n);
    1624  }
    1625