xref: /netbsd-src/lib/libquota/libquota.3 (revision 2dd295436a0082eb4f8d294f4aa73c223413d0f2)
1.\"	$NetBSD: libquota.3,v 1.5 2012/02/13 13:23:29 wiz Exp $
2.\"
3.\" Copyright (c) 2012 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by David A. Holland.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.Dd February 13, 2012
30.Dt LIBQUOTA 3
31.Os
32.Sh NAME
33.Nm libquota ,
34.Nm quota_open ,
35.Nm quota_close ,
36.Nm quota_getmountdevice ,
37.Nm quota_getmountpoint ,
38.Nm quota_getimplname ,
39.Nm quota_getrestrictions ,
40.Nm quota_getnumidtypes ,
41.Nm quota_getnumobjtypes ,
42.Nm quota_idtype_getname ,
43.Nm quota_objtype_getname ,
44.Nm quota_objtype_isbytes ,
45.Nm quota_get ,
46.Nm quota_put ,
47.Nm quota_delete ,
48.Nm quota_opencursor ,
49.Nm quotacursor_close ,
50.Nm quotacursor_skipidtype ,
51.Nm quotacursor_get ,
52.Nm quotacursor_getn ,
53.Nm quotacursor_atend ,
54.Nm quotacursor_rewind ,
55.Nm quota_quotaon ,
56.Nm quota_quotaoff ,
57.Nm quotaval_clear
58.Nd disk quota access and control library
59.Sh LIBRARY
60.Lb libquota
61.Sh SYNOPSIS
62.In quota.h
63.Ft struct quotahandle *
64.Fn quota_open "const char *path"
65.Ft void
66.Fn quota_close "struct quotahandle *qh"
67.Ft const char *
68.Fn quota_getmountdevice "struct quotahandle *qh"
69.Ft const char *
70.Fn quota_getmountpoint "struct quotahandle *qh"
71.Ft const char *
72.Fn quota_getimplname "struct quotahandle *qh"
73.Ft unsigned
74.Fn quota_getrestrictions "struct quotahandle *qh"
75.Ft int
76.Fn quota_getnumidtypes "struct quotahandle *qh"
77.Ft int
78.Fn quota_getnumobjtypes "struct quotahandle *qh"
79.Ft const char *
80.Fn quota_idtype_getname "struct quotahandle *qh" "int idtype"
81.Ft const char *
82.Fn quota_objtype_getname "struct quotahandle *qh" "int objtype"
83.Ft int
84.Fn quota_objtype_isbytes "struct quotahandle *qh" "int objtype"
85.Ft int
86.Fn quota_get "struct quotahandle *qh" "const struct quotakey *key" "struct quotaval *val"
87.Ft int
88.Fn quota_put "struct quotahandle *qh" "const struct quotakey *key" "const struct quotaval *val"
89.Ft int
90.Fn quota_delete "struct quotahandle *qh" "const struct quotakey *key"
91.Ft struct quotacursor *
92.Fn quota_opencursor "struct quotahandle *qh"
93.Ft void
94.Fn quotacursor_close "struct quotacursor *qc"
95.Ft int
96.Fn quotacursor_skipidtype "struct quotacursor *qc" "int idtype"
97.Ft int
98.Fn quotacursor_get "struct quotacursor *qc" "struct quotakey *key" "const struct quotaval *val"
99.Ft int
100.Fn quotacursor_getn "struct quotacursor *qc" "struct quotakey *keys" "const struct quotaval *vals" "unsigned maxnum"
101.Ft int
102.Fn quotacursor_atend "struct quotacursor *qc"
103.Ft int
104.Fn quotacursor_rewind "struct quotacursor *qc"
105.Ft int
106.Fn quota_quotaon "struct quotahandle *qh" "int idtype"
107.Ft int
108.Fn quota_quotaoff "struct quotahandle *qh" "int idtype"
109.Ft void
110.Fn quotaval_clear "struct quotaval *qv"
111.Sh DESCRIPTION
112The
113.Nm
114library provides uniform access to disk quota functionality across all
115file systems and file system types.
116Programs should be linked with
117.Fl lquota lrpcsvc .
118.Pp
119Quota information is organized as a key/value store, where the key
120names a particular limit and the value contains information about that
121limit.
122The information includes a configured
123.Em soft limit ,
124.Em hard limit ,
125and
126.Em grace time ,
127as well as the current usage and the expire time of any pending grace
128period.
129The soft limit may be exceeded temporarily, but only for the length of
130time specified; after that further usage is rejected.
131The hard limit may not be exceeded.
132.Pp
133Each mounted file system that supports quotas contains its own
134key/value store for quota information.
135.Pq The underlying representation may vary.
136The library supports get, put, and delete operations, as well as a
137cursor interface for iterating an entire store.
138It also provides functions for inspecting the properties of a
139particular file system's quota implementation.
140.Pp
141All functionality is accessed by first calling
142.Fn quota_open
143on a particular volume to get a handle for that volume's quota
144information.
145Other operations can be called at this point.
146The
147.Fn quota_close
148function should be called when done to release internal resources.
149.Ss Data Structures
150The key part of the key/value schema is defined as
151.Dv struct quotakey ,
152which contains the following members:
153.Bl -tag -width 4n
154.It int qk_idtype
155The type of principal (user, group, etc.) to retrieve quota
156information for.
157.It id_t qk_id
158The ID number (uid, gid, etc.) to retrieve quota information for.
159.It int qk_objtype
160The type of file system resource (blocks, files, etc.) to retrieve
161quota information for.
162.El
163The value part of the key/value schema is defined as
164.Dv struct quotaval ,
165which contains the following members:
166.Bl -tag -width 4n
167.It uint64_t qv_softlimit
168The soft limit.
169.It uint64_t qv_hardlimit
170The hard limit.
171.It uint64_t qv_usage
172The current usage.
173.It int64_t qv_expiretime
174The time
175.Pq in time_t terms
176at which the current grace period, if any, expires.
177.It int64_t qv_grace
178The configured length of grace period.
179.El
180.Ss Constants
181The basic ID and object types are predefined.
182.Dv QUOTA_IDTYPE_USER
183is the code number for quotas on users;
184.Dv QUOTA_IDTYPE_GROUP
185is the code number for quotas on groups.
186Similarly,
187.Dv QUOTA_OBJTYPE_BLOCKS
188retrieves limits on file system blocks, while
189.Dv QUOTA_OBJTYPE_FILES
190retrieves limits on the number of existing files.
191.Pp
192Some backends support a default configuration; this can be accessed by
193using
194.Dv QUOTA_DEFAULTID
195as the ID number.
196.Pp
197When no limit is in place, the value
198.Dv QUOTA_NOLIMIT
199appears in the limit fields of struct quotaval, and if no time is
200indicated the value
201.Dv QUOTA_NOTIME
202appears in the time fields.
203.Ss Quota v1
204The historic BSD quota implementation for FFS, known as
205.Dq quota v1 ,
206has additional restrictions and requirements.
207All file systems to be mounted with v1 quotas
208.Em must
209be listed in
210.Xr fstab 5
211with the
212.Dv userquota
213and/or
214.Dv groupquota
215mount options specified.
216The tools
217.Xr quotacheck 8
218and
219.Xr quotaon 8
220must be used on quota v1 volumes before quotas become fully
221operational, and
222.Xr quotaoff 8
223must be used at system shutdown time.
224The
225.Nm
226library provides access to quota v1 data even before
227.Xr quotaon 8
228is called by direct access to the on-disk quota information.
229However, this method is not recommended.
230Note that the
231.Dv userquota
232and
233.Dv groupquota
234mount options are read and interpreted at quotaon time, not
235.Xr mount 8
236time.
237This allowed historic implementations to avoid storing the path in the
238kernel.
239.Ss Semantic Restrictions
240Some quota implementations are restricted in their functionality or
241semantics.
242The following restriction codes are defined to allow
243.Nm libquota
244client code to adapt or to provide more helpful diagnostic messages.
245.Bl -tag -width 4n
246.It QUOTA_RESTRICT_NEEDSQUOTACHECK
247The quota implementation is a quota v1 system and requires the
248old-style quota check and mount process as described in the
249previous section.
250.It QUOTA_RESTRICT_UNIFORMGRACE
251The grace period for how long a quota can be over the soft limit can
252be specified only once, globally, for all principals.
253It is set via the default
254.Pq Dv QUOTA_DEFAULTID
255quota entry.
256.It QUOTA_RESTRICT_32BIT
257The values in struct quotaval are limited to 32 bits wide.
258Larger values will be treated as
259.Dv QUOTA_NOLIMIT .
260.It QUOTA_RESTRICT_READONLY
261The quota information is read-only.
262Attempts to update it using
263.Fn quota_put
264or other functions will fail.
265.El
266.Ss Function Descriptions
267.Bl -tag -width 4n
268.It Fn quota_open
269Open a volume for access with the quota library.
270The
271.Fa path
272may be any file or file system object on the desired volume.
273On success, returns a quota handle for further use.
274On failure, returns
275.Dv NULL
276and sets
277.Dv errno .
278.It Fn quota_close
279Close a quota handle previously created with
280.Fn quota_open .
281.It Fn quota_getmountdevice
282Return the path of the device the target volume is mounted from.
283This is retrieved with
284.Xr statvfs 2 .
285.It Fn quota_getmountpoint
286Return the path in the directory tree where the target volume is
287mounted.
288This is retrieved with
289.Xr statvfs 2 .
290.It Fn quota_getimplname
291Return a human-readable string describing the underlying quota
292implementation.
293Client programs should not attempt to interpret this string.
294.It Fn quota_getrestrictions
295Return the semantic restriction flags for the underlying quota
296implementation.
297The possible flags are described above.
298.It Fn quota_getnumidtypes
299Return the number of ID types supported by this volume.
300Will ordinarily be two; ideally code using this library should be
301prepared for other values, including possibly one.
302However, as of this writing it is difficult to foresee any other
303likely ID types beyond users and groups.
304.It Fn quota_getnumobjtypes
305Return the number of object types supported by this volume.
306Will ordinarily be two; ideally code using this library should be
307prepared for larger values.
308As of this writing there are deployed file systems
309.Pq though not in Nx
310that support quotas for more than two object types.
311.It Fn quota_idtype_getname
312Return a printable name for an ID type.
313.It Fn quota_objtype_getname
314Return a printable name for an object type.
315.It Fn quota_objtype_isbytes
316Return true if the object type refers to something measured in bytes.
317.Pq This can be used for calling Xr humanize_number 3 .
318.It Fn quota_get
319Return, in
320.Fa val ,
321the quota information associated with the quota key
322.Fa key .
323On failure, returns \-1 and sets
324.Dv errno .
325.It Fn quota_put
326Update the quota information associated with the quota key
327.Fa key
328from the value
329.Fa val .
330Note that the current usage
331.Pq which is maintained by the file system
332cannot be updated via
333.Nm .
334If it becomes incorrect or corrupted,
335.Xr quotacheck 8
336or
337.Xr fsck 8
338must be used.
339Also note that sufficient privilege is required.
340On failure, returns \-1 and sets
341.Dv errno .
342.It Fn quota_delete
343Remove the quota information associated with the quota key
344.Fa key .
345Depending on the backend implementation this might just blank it out
346or might physically remove the quota record from disk.
347Note that sufficient privilege is required.
348On failure, returns \-1 and sets
349.Dv errno .
350.It Fn quota_opencursor
351Prepare to iterate the store by creating a cursor.
352The cursor starts at the beginning of the store.
353On success, returns a pointer to a cursor object that can be used with
354the quotacursor calls.
355On failure, returns
356.Dv NULL
357and sets
358.Dv errno .
359.It Fn quotacursor_close
360Destroy a cursor previously created with
361.Fn quota_opencursor .
362This releases internal storage.
363.It Fn quotacursor_skipidtype
364Hint to the implementation that the caller is not interested in
365retrieving records with ID type
366.Fa idtype .
367As this is a hint, the implementation may ignore it; the caller should
368still be prepared to receive and ignore such records.
369On failure, returns \-1 and sets
370.Dv errno .
371.It Fn quotacursor_get
372Retrieve the next record
373.Pq key and value
374from a cursor.
375Note that records are not guaranteed to be returned in any particular
376order.
377On failure, returns \-1 and sets
378.Dv errno .
379.It Fn quotacursor_getn
380Retrieve the next several keys and values from a cursor.
381Up to
382.Fa maxnum
383keys and values will be stored in the arrays pointed to by the
384.Fa keys
385and
386.Fa vals
387arguments.
388Returns the number of records retrieved.
389On failure, returns \-1 and sets
390.Dv errno .
391.It Fn quotacursor_atend
392Returns true if the cursor has reached the end of the quota store.
393.It Fn quotacursor_rewind
394Resets a cursor to point to the beginning of the quota store, allowing
395another pass over the data.
396.It Fn quota_quotaon
397For old-style quota v1 implementations, this function enables quotas
398for the specified ID type.
399To ensure that the quota files are consistent with the file system
400state,
401.Xr quotacheck 8
402must have been run beforehand.
403As described above, the file system volume must be listed in
404.Xr fstab 5
405and the corresponding old-style mount option,
406.Dv userquota
407or
408.Dv groupquota ,
409must be set therein.
410The path name for the quota file is retrieved from
411.Xr fstab 5
412and passed to the kernel.
413This function will fail if used on newer quota implementations with
414in-file-system quotas.
415.It Fn quota_quotaoff
416For old-style quotas, this function disables quotas for the specified
417ID type.
418This function will fail if used on newer quota implementations with
419in-file-system quotas.
420.It Fn quotaval_clear
421A convenience function for initializing a struct quotaval instance
422to the default empty state.
423.El
424.\" .Sh EXAMPLES
425.\" XXX would be nice to have an example, particularly of iteration.
426.Sh ERRORS
427.\" XXX this is woefully incomplete, particularly about errors that
428.\" can be generated inside file systems.
429Error conditions include:
430.Bl -tag -width Er
431.\" .It Bq Er EBUSY
432.\" .Fn quota_quotaon
433.\" was attempted on a volume that is not a quota v1 volume.
434.It Bq Er EDEADLK
435An inconsistency was detected during
436.Fn quotacursor_get
437or
438.Fn quotacursor_getn .
439The application should discard information collected so far and use
440.Fn quotacursor_rewind
441to start the iteration over.
442.It Bq Er ENOENT
443The quota information requested from
444.Fn quota_get
445does not exist.
446.It Bq Er ENXIO
447The
448.Fa path
449passed to
450.Fn quota_open
451was on a volume whose quota support is not enabled.
452.It Bq Er EOPNOTSUPP
453The
454.Fa path
455passed to
456.Fn quota_open
457was on a volume that has no quota support.
458Or, the iterator functions,
459.Fn quota_put ,
460or other unsupported operations were attempted on an NFS volume,
461or on some other volume type that does not support the full
462semantic range of quota information.
463.El
464.Sh SEE ALSO
465.Xr quota 1 ,
466.Xr edquota 8 ,
467.Xr mount_ffs 8 ,
468.Xr mount_nfs 8 ,
469.Xr quotacheck 8 ,
470.Xr quotaon 8 ,
471.Xr repquota 8 ,
472.Xr rpc.rquotad 8
473.Sh HISTORY
474The
475.Nm
476library first appeared in
477.Nx 6.0 .
478.Sh AUTHORS
479The
480.Nm
481library was written by
482.An David A. Holland .
483