---
name: "cgwrite1"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/cgget/3"
---

# cgwrite1(3)

read/write cylinder groups of UFS disks

Part of [cgget(3)](/cgget/3.md).

## NAME

**cgget**, **cgput**, **cgread**, **cgread1**, **cgwrite**, **cgwrite1** – read/write cylinder groups of UFS disks

## LIBRARY

library “libufs”

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/mount.h\>**

**#include \<ufs/ufs/ufsmount.h\>**

**#include \<ufs/ufs/dinode.h\>**

**#include \<ufs/ffs/fs.h\>**

**#include \<libufs.h\>**

*int*\

**cgget**(*int devfd*, *struct fs \*fs*, *int cg*, *struct cg \*cgp*);

*int*\

**cgput**(*int devfd*, *struct fs \*fs*, *struct cg \*cgp*);

*int*\

**cgread**(*struct uufsd \*disk*);

*int*\

**cgread1**(*struct uufsd \*disk*, *int cg*);

*int*\

**cgwrite**(*struct uufsd \*disk*);

*int*\

**cgwrite1**(*struct uufsd \*disk*, *int cg*);

## DESCRIPTION

The **cgget**(), **cgread**(), and **cgread1**() functions provide cylinder group reads for [libufs(3)](/libufs/3.md) consumers. The **cgput**(), **cgwrite**(), and **cgwrite1**() functions provide cylinder group writes for [libufs(3)](/libufs/3.md) consumers.

The **cgget**() function reads the cylinder group specified by *cg* into the buffer pointed to by *cgp* from the filesystem described by the *fs* superblock using the *devfd* file descriptor that references the filesystem disk. The **cgget**() function is the only cylinder group read function that is safe to use in threaded applications.

The **cgput**() function writes the cylinder group specified by *cgp* to the filesystem described by the *fs* superblock using the *devfd* file descriptor that references the filesystem disk. The **cgput**() function is the only cylinder group write function that is safe to use in threaded applications. Note that the **cgput**() function needs to be called only if the cylinder group has been modified and the on-disk copy needs to be updated.

The **cgread1**() function reads from the cylinder group specified by *cg* into the *d\_cg* cylinder-group structure in a user-land UFS-disk structure. It sets the *d\_lcg* field to the cylinder group number *cg*.

The **cgread**() function operates on sequential cylinder groups. Calling the **cgread**() function is equivalent to calling **cgread1**() with a cylinder group specifier equivalent to the value of the current *d\_ccg* field, and then incrementing the *d\_ccg* field.

The **cgwrite**() function stores on disk the cylinder group held in the *d\_cg* cylinder-group structure in a user-land UFS-disk structure.

The **cgwrite1**() function provides no additional functionality over the **cgwrite**() function as there is only one place that a given cylinder group can correctly be written. If the caller gets the *cg* parameter wrong, the function fails with the error EDOOFUS. This function remains only to provide backward compatibility.

## RETURN VALUES

The **cgread**() function returns 0 if there are no more cylinder groups to read, 1 if there are more cylinder groups, and -1 on error. The **cgread1**() function returns 1 on success and -1 on error. The other functions return 0 on success and -1 on error.

## ERRORS

The **cgget**(), **cgread**(), and **cgread1**() functions may fail and set *errno* for any of the errors specified for the library function [bread(3)](/bread/3.md).

The **cgput**(), **cgwrite**(), and **cgwrite1**() functions may fail and set *errno* for any of the errors specified for the library function [bwrite(3)](/bwrite/3.md). Additionally the **cgwrite1**() will return the EDOOFUS error if the cylinder group specified does not match the cylinder group that it is requesting to write.

## SEE ALSO

[bread(3)](/bread/3.md), [bwrite(3)](/bwrite/3.md), [libufs(3)](/libufs/3.md)

## HISTORY

These functions first appeared as part of [libufs(3)](/libufs/3.md) in FreeBSD 5.1.

## AUTHORS

Juli Mallett ⟨*jmallett@FreeBSD.org*⟩ Marshall Kirk McKusick ⟨*mckusick@FreeBSD.org*⟩
