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