Remove advertising header from share/
[dragonfly.git] / share / doc / psd / 05.sysman / 2.2.t
1 .\" Copyright (c) 1983, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)2.2.t       8.1 (Berkeley) 6/8/93
29 .\"
30 .sh "File system
31 .NH 3
32 Overview
33 .PP
34 The file system abstraction provides access to a hierarchical
35 file system structure.
36 The file system contains directories (each of which may contain
37 other sub-directories) as well as files and references to other
38 objects such as devices and inter-process communications sockets.
39 .PP
40 Each file is organized as a linear array of bytes.  No record
41 boundaries or system related information is present in
42 a file.
43 Files may be read and written in a random-access fashion.
44 The user may read the data in a directory as though
45 it were an ordinary file to determine the names of the contained files,
46 but only the system may write into the directories.
47 The file system stores only a small amount of ownership, protection and usage
48 information with a file.
49 .NH 3
50 Naming
51 .PP
52 The file system calls take \fIpath name\fP arguments.
53 These consist of a zero or more component \fIfile names\fP
54 separated by ``/\^'' characters, where each file name
55 is up to 255 ASCII characters excluding null and ``/\^''.
56 .PP
57 Each process always has two naming contexts: one for the
58 root directory of the file system and one for the
59 current working directory.  These are used
60 by the system in the filename translation process.
61 If a path name begins with a ``/\^'', it is called
62 a full path name and interpreted relative to the root directory context.
63 If the path name does not begin with a ``/\^'' it is called
64 a relative path name and interpreted relative to the current directory
65 context.
66 .PP
67 The system limits
68 the total length of a path name to 1024 characters.
69 .PP
70 The file name ``..'' in each directory refers to
71 the parent directory of that directory.
72 The parent directory of the root of the file system is always that directory.
73 .PP
74 The calls
75 .DS
76 chdir(path);
77 char *path;
78
79 chroot(path)
80 char *path;
81 .DE
82 change the current working directory and root directory context of a process.
83 Only the super-user can change the root directory context of a process.
84 .NH 3
85 Creation and removal
86 .PP
87 The file system allows directories, files, special devices,
88 and ``portals'' to be created and removed from the file system.
89 .NH 4
90 Directory creation and removal
91 .PP
92 A directory is created with the \fImkdir\fP system call:
93 .DS
94 mkdir(path, mode);
95 char *path; int mode;
96 .DE
97 where the mode is defined as for files (see below).
98 Directories are removed with the \fIrmdir\fP system call:
99 .DS
100 rmdir(path);
101 char *path;
102 .DE
103 A directory must be empty if it is to be deleted.
104 .NH 4
105 File creation
106 .PP
107 Files are created with the \fIopen\fP system call,
108 .DS
109 fd = open(path, oflag, mode);
110 result int fd; char *path; int oflag, mode;
111 .DE
112 The \fIpath\fP parameter specifies the name of the
113 file to be created.  The \fIoflag\fP parameter must
114 include O_CREAT from below to cause the file to be created.
115 Bits for \fIoflag\fP are
116 defined in \fI<sys/file.h>\fP:
117 .DS
118 ._d
119 #define O_RDONLY        000     /* open for reading */
120 #define O_WRONLY        001     /* open for writing */
121 #define O_RDWR  002     /* open for read & write */
122 #define O_NDELAY        004     /* non-blocking open */
123 #define O_APPEND        010     /* append on each write */
124 #define O_CREAT 01000   /* open with file create */
125 #define O_TRUNC 02000   /* open with truncation */
126 #define O_EXCL  04000   /* error on create if file exists */
127 .DE
128 .PP
129 One of O_RDONLY, O_WRONLY and O_RDWR should be specified,
130 indicating what types of operations are desired to be performed
131 on the open file.  The operations will be checked against the user's
132 access rights to the file before allowing the \fIopen\fP to succeed.
133 Specifying O_APPEND causes writes to automatically append to the
134 file.
135 The flag O_CREAT causes the file to be created if it does not
136 exist, owned by the current user
137 and the group of the containing directory.
138 The protection for the new file is specified in \fImode\fP.
139 The file mode is used as a three digit octal number.
140 Each digit encodes read access as 4, write access as 2 and execute
141 access as 1, or'ed together.  The 0700 bits describe owner
142 access, the 070 bits describe the access rights for processes in the same
143 group as the file, and the 07 bits describe the access rights
144 for other processes.
145 .PP
146 If the open specifies to create the file with O_EXCL
147 and the file already exists, then the \fIopen\fP will fail
148 without affecting the file in any way.  This provides a
149 simple exclusive access facility.
150 If the file exists but is a symbolic link, the open will fail
151 regardless of the existence of the file specified by the link.
152 .NH 4
153 Creating references to devices
154 .PP
155 The file system allows entries which reference peripheral devices.
156 Peripherals are distinguished as \fIblock\fP or \fIcharacter\fP
157 devices according by their ability to support block-oriented
158 operations.
159 Devices are identified by their ``major'' and ``minor''
160 device numbers.  The major device number determines the kind
161 of peripheral it is, while the minor device number indicates
162 one of possibly many peripherals of that kind.
163 Structured devices have all operations performed internally
164 in ``block'' quantities while
165 unstructured devices often have a number of
166 special \fIioctl\fP operations, and may have input and output
167 performed in varying units.
168 The \fImknod\fP call creates special entries:
169 .DS
170 mknod(path, mode, dev);
171 char *path; int mode, dev;
172 .DE
173 where \fImode\fP is formed from the object type
174 and access permissions.  The parameter \fIdev\fP is a configuration
175 dependent parameter used to identify specific character or
176 block I/O devices.
177 .NH 4
178 Portal creation\(dg
179 .PP
180 .FS
181 \(dg The \fIportal\fP call is not implemented in 4.3BSD.
182 .FE
183 The call
184 .DS
185 fd = portal(name, server, param, dtype, protocol, domain, socktype)
186 result int fd; char *name, *server, *param; int dtype, protocol;
187 int domain, socktype;
188 .DE
189 places a \fIname\fP in the file system name space that causes connection to a
190 server process when the name is used.
191 The portal call returns an active portal in \fIfd\fP as though an
192 access had occurred to activate an inactive portal, as now described.
193 .PP
194 When an inactive portal is accessed, the system sets up a socket
195 of the specified \fIsocktype\fP in the specified communications
196 \fIdomain\fP (see section 2.3), and creates the \fIserver\fP process,
197 giving it the specified \fIparam\fP as argument to help it identify
198 the portal, and also giving it the newly created socket as descriptor
199 number 0.  The accessor of the portal will create a socket in the same
200 \fIdomain\fP and \fIconnect\fP to the server.  The user will then
201 \fIwrap\fP the socket in the specified \fIprotocol\fP to create an object of
202 the required descriptor type \fIdtype\fP and proceed with the
203 operation which was in progress before the portal was encountered.
204 .PP
205 While the server process holds the socket (which it received as \fIfd\fP
206 from the \fIportal\fP call on descriptor 0 at activation) further references
207 will result in connections being made to the same socket.
208 .NH 4
209 File, device, and portal removal
210 .PP
211 A reference to a file, special device or portal may be removed with the
212 \fIunlink\fP call,
213 .DS
214 unlink(path);
215 char *path;
216 .DE
217 The caller must have write access to the directory in which
218 the file is located for this call to be successful.
219 .NH 3
220 Reading and modifying file attributes
221 .PP
222 Detailed information about the attributes of a file
223 may be obtained with the calls:
224 .DS
225 #include <sys/stat.h>
226
227 stat(path, stb);
228 char *path; result struct stat *stb;
229
230 fstat(fd, stb);
231 int fd; result struct stat *stb;
232 .DE
233 The \fIstat\fP structure includes the file
234 type, protection, ownership, access times,
235 size, and a count of hard links.
236 If the file is a symbolic link, then the status of the link
237 itself (rather than the file the link references)
238 may be found using the \fIlstat\fP call:
239 .DS
240 lstat(path, stb);
241 char *path; result struct stat *stb;
242 .DE
243 .PP
244 Newly created files are assigned the user id of the
245 process that created it and the group id of the directory
246 in which it was created.  The ownership of a file may
247 be changed by either of the calls
248 .DS
249 chown(path, owner, group);
250 char *path; int owner, group;
251
252 fchown(fd, owner, group);
253 int fd, owner, group;
254 .DE
255 .PP
256 In addition to ownership, each file has three levels of access
257 protection associated with it.  These levels are owner relative,
258 group relative, and global (all users and groups).  Each level
259 of access has separate indicators for read permission, write
260 permission, and execute permission.
261 The protection bits associated with a file may be set by either
262 of the calls:
263 .DS
264 chmod(path, mode);
265 char *path; int mode;
266
267 fchmod(fd, mode);
268 int fd, mode;
269 .DE
270 where \fImode\fP is a value indicating the new protection
271 of the file, as listed in section 2.2.3.2.
272 .PP
273 Finally, the access and modify times on a file may be set by the call:
274 .DS
275 utimes(path, tvp)
276 char *path; struct timeval *tvp[2];
277 .DE
278 This is particularly useful when moving files between media, to
279 preserve relationships between the times the file was modified.
280 .NH 3
281 Links and renaming
282 .PP
283 Links allow multiple names for a file
284 to exist.  Links exist independently of the file linked to.
285 .PP
286 Two types of links exist, \fIhard\fP links and \fIsymbolic\fP
287 links.  A hard link is a reference counting mechanism that
288 allows a file to have multiple names within the same file
289 system.  Symbolic links cause string substitution
290 during the pathname interpretation process.
291 .PP
292 Hard links and symbolic links have different
293 properties.  A hard link insures the target
294 file will always be accessible, even after its original
295 directory entry is removed; no such guarantee exists for a symbolic link.
296 Symbolic links can span file systems boundaries.
297 .PP
298 The following calls create a new link, named \fIpath2\fP,
299 to \fIpath1\fP:
300 .DS
301 link(path1, path2);
302 char *path1, *path2;
303
304 symlink(path1, path2);
305 char *path1, *path2;
306 .DE
307 The \fIunlink\fP primitive may be used to remove
308 either type of link. 
309 .PP
310 If a file is a symbolic link, the ``value'' of the
311 link may be read with the \fIreadlink\fP call,
312 .DS
313 len = readlink(path, buf, bufsize);
314 result int len; result char *path, *buf; int bufsize;
315 .DE
316 This call returns, in \fIbuf\fP, the null-terminated string
317 substituted into pathnames passing through \fIpath\fP\|.
318 .PP
319 Atomic renaming of file system resident objects is possible
320 with the \fIrename\fP call:
321 .DS
322 rename(oldname, newname);
323 char *oldname, *newname;
324 .DE
325 where both \fIoldname\fP and \fInewname\fP must be
326 in the same file system.
327 If \fInewname\fP exists and is a directory, then it must be empty.
328 .NH 3
329 Extension and truncation
330 .PP
331 Files are created with zero length and may be extended
332 simply by writing or appending to them.  While a file is
333 open the system maintains a pointer into the file
334 indicating the current location in the file associated with
335 the descriptor.  This pointer may be moved about in the
336 file in a random access fashion.
337 To set the current offset into a file, the \fIlseek\fP
338 call may be used,
339 .DS
340 oldoffset = lseek(fd, offset, type);
341 result off_t oldoffset; int fd; off_t offset; int type;
342 .DE
343 where \fItype\fP is given in \fI<sys/file.h>\fP as one of:
344 .DS
345 ._d
346 #define L_SET   0       /* set absolute file offset */
347 #define L_INCR  1       /* set file offset relative to current position */
348 #define L_XTND  2       /* set offset relative to end-of-file */
349 .DE
350 The call ``lseek(fd, 0, L_INCR)''
351 returns the current offset into the file.
352 .PP
353 Files may have ``holes'' in them.  Holes are void areas in the
354 linear extent of the file where data has never been
355 written.  These may be created by seeking to
356 a location in a file past the current end-of-file and writing.
357 Holes are treated by the system as zero valued bytes.
358 .PP
359 A file may be truncated with either of the calls:
360 .DS
361 truncate(path, length);
362 char *path; int length;
363
364 ftruncate(fd, length);
365 int fd, length;
366 .DE
367 reducing the size of the specified file to \fIlength\fP bytes.
368 .NH 3
369 Checking accessibility
370 .PP
371 A process running with
372 different real and effective user ids
373 may interrogate the accessibility of a file to the
374 real user by using
375 the \fIaccess\fP call:
376 .DS
377 accessible = access(path, how);
378 result int accessible; char *path; int how;
379 .DE
380 Here \fIhow\fP is constructed by or'ing the following bits, defined
381 in \fI<sys/file.h>\fP:
382 .DS
383 ._d
384 #define F_OK    0       /* file exists */
385 #define X_OK    1       /* file is executable */
386 #define W_OK    2       /* file is writable */
387 #define R_OK    4       /* file is readable */
388 .DE
389 The presence or absence of advisory locks does not affect the
390 result of \fIaccess\fP\|.
391 .NH 3
392 Locking
393 .PP
394 The file system provides basic facilities that allow cooperating processes
395 to synchronize their access to shared files.  A process may
396 place an advisory \fIread\fP or \fIwrite\fP lock on a file,
397 so that other cooperating processes may avoid interfering
398 with the process' access.  This simple mechanism
399 provides locking with file granularity.  More granular
400 locking can be built using the IPC facilities to provide a lock
401 manager.
402 The system does not force processes to obey the locks;
403 they are of an advisory nature only.
404 .PP
405 Locking is performed after an \fIopen\fP call by applying the
406 \fIflock\fP primitive,
407 .DS
408 flock(fd, how);
409 int fd, how;
410 .DE
411 where the \fIhow\fP parameter is formed from bits defined in \fI<sys/file.h>\fP:
412 .DS
413 ._d
414 #define LOCK_SH 1       /* shared lock */
415 #define LOCK_EX 2       /* exclusive lock */
416 #define LOCK_NB 4       /* don't block when locking */
417 #define LOCK_UN 8       /* unlock */
418 .DE
419 Successive lock calls may be used to increase or
420 decrease the level of locking.  If an object is currently
421 locked by another process when a \fIflock\fP call is made,
422 the caller will be blocked until the current lock owner
423 releases the lock; this may be avoided by including LOCK_NB
424 in the \fIhow\fP parameter.
425 Specifying LOCK_UN removes all locks associated with the descriptor.
426 Advisory locks held by a process are automatically deleted when
427 the process terminates.
428 .NH 3
429 Disk quotas
430 .PP
431 As an optional facility, each file system may be requested to
432 impose limits on a user's disk usage.
433 Two quantities are limited: the total amount of disk space which
434 a user may allocate in a file system and the total number of files
435 a user may create in a file system.  Quotas are expressed as
436 \fIhard\fP limits and \fIsoft\fP limits.  A hard limit is
437 always imposed; if a user would exceed a hard limit, the operation
438 which caused the resource request will fail.  A soft limit results
439 in the user receiving a warning message, but with allocation succeeding.
440 Facilities are provided to turn soft limits into hard limits if a
441 user has exceeded a soft limit for an unreasonable period of time.
442 .PP
443 To enable disk quotas on a file system the \fIsetquota\fP call
444 is used:
445 .DS
446 setquota(special, file)
447 char *special, *file;
448 .DE
449 where \fIspecial\fP refers to a structured device file where
450 a mounted file system exists, and
451 \fIfile\fP refers to a disk quota file (residing on the file
452 system associated with \fIspecial\fP) from which user quotas
453 should be obtained.  The format of the disk quota file is 
454 implementation dependent.
455 .PP
456 To manipulate disk quotas the \fIquota\fP call is provided:
457 .DS
458 #include <sys/quota.h>
459
460 quota(cmd, uid, arg, addr)
461 int cmd, uid, arg; caddr_t addr;
462 .DE
463 The indicated \fIcmd\fP is applied to the user ID \fIuid\fP.
464 The parameters \fIarg\fP and \fIaddr\fP are command specific.
465 The file \fI<sys/quota.h>\fP contains definitions pertinent to the
466 use of this call.