1 /* This file contains the procedures for creating, opening, closing, and
2 * seeking on files.
3 *
4 * The entry points into this file are
5 * do_creat: perform the CREAT system call
6 * do_open: perform the OPEN system call
7 * do_mknod: perform the MKNOD system call
8 * do_mkdir: perform the MKDIR system call
9 * do_close: perform the CLOSE system call
10 * do_lseek: perform the LSEEK system call
11 */
12
13 #include "fs.h"
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <minix/callnr.h>
17 #include <minix/com.h>
18 #include "buf.h"
19 #include "dev.h"
20 #include "file.h"
21 #include "fproc.h"
22 #include "inode.h"
23 #include "lock.h"
24 #include "param.h"
25
26 PRIVATE message dev_mess;
27 PRIVATE char mode_map[] = {R_BIT, W_BIT, R_BIT|W_BIT, 0};
28
29 FORWARD _PROTOTYPE( int common_open, (int oflags, Mode_t omode) );
30 FORWARD _PROTOTYPE( int pipe_open, (struct inode *rip,Mode_t bits,int oflags));
31 FORWARD _PROTOTYPE( struct inode *new_node, (char *path, Mode_t bits,
32 zone_t z0) );
33
34
35 /*===========================================================================*
36 * do_creat *
37 *===========================================================================*/
38 PUBLIC int do_creat()
39 {
40 /* Perform the creat(name, mode) system call. */
41 int r;
42
43 if (fetch_name(name, name_length, M3) != OK) return(err_code);
44 r = common_open(O_WRONLY | O_CREAT | O_TRUNC, (mode_t) mode);
45 return(r);
46 }
47
48
49 /*===========================================================================*
50 * do_open *
51 *===========================================================================*/
52 PUBLIC int do_open()
53 {
54 /* Perform the open(name, flags,...) system call. */
55
56 int create_mode = 0; /* is really mode_t but this gives problems */
57 int r;
58
59 /* If O_CREAT is set, open has three parameters, otherwise two. */
60 if (mode & O_CREAT) {
61 create_mode = c_mode;
62 r = fetch_name(c_name, name1_length, M1);
63 } else {
64 r = fetch_name(name, name_length, M3);
65 }
66
67 if (r != OK) return(err_code); /* name was bad */
68 r = common_open(mode, create_mode);
69 return(r);
70 }
71
72
73 /*===========================================================================*
74 * common_open *
75 *===========================================================================*/
76 PRIVATE int common_open(oflags, omode)
77 register int oflags;
78 mode_t omode;
79 {
80 /* Common code from do_creat and do_open. */
81
82 register struct inode *rip;
83 int r, b, major, task, exist = TRUE;
84 dev_t dev;
85 mode_t bits;
86 off_t pos;
87 struct filp *fil_ptr, *filp2;
88
89 /* Remap the bottom two bits of oflags. */
90 bits = (mode_t) mode_map[oflags & O_ACCMODE];
91
92 /* See if file descriptor and filp slots are available. */
93 if ( (r = get_fd(0, bits, &fd, &fil_ptr)) != OK) return(r);
94
95 /* If O_CREATE is set, try to make the file. */
96 if (oflags & O_CREAT) {
97 /* Create a new inode by calling new_node(). */
98 omode = I_REGULAR | (omode & ALL_MODES & fp->fp_umask);
99 rip = new_node(user_path, omode, NO_ZONE);
100 r = err_code;
101 if (r == OK) exist = FALSE; /* we just created the file */
102 else if (r != EEXIST) return(r); /* other error */
103 else exist = !(oflags & O_EXCL); /* file exists, if the O_EXCL
104 flag is set this is an error */
105 } else {
106 /* Scan path name. */
107 if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code);
108 }
109
110 /* Claim the file descriptor and filp slot and fill them in. */
111 fp->fp_filp[fd] = fil_ptr;
112 fil_ptr->filp_count = 1;
113 fil_ptr->filp_ino = rip;
114 fil_ptr->filp_flags = oflags;
115
116 /* Only do the normal open code if we didn't just create the file. */
117 if (exist) {
118 /* Check protections. */
119 if ((r = forbidden(rip, bits)) == OK) {
120 /* Opening reg. files directories and special files differ. */
121 switch (rip->i_mode & I_TYPE) {
122 case I_REGULAR:
123 /* Truncate regular file if O_TRUNC. */
124 if (oflags & O_TRUNC) {
125 if ((r = forbidden(rip, W_BIT)) !=OK) break;
126 truncate(rip);
127 wipe_inode(rip);
128 /* Send the inode from the inode cache to the
129 * block cache, so it gets written on the next
130 * cache flush.
131 */
132 rw_inode(rip, WRITING);
133 }
134 break;
135
136 case I_DIRECTORY:
137 /* Directories may be read but not written. */
138 r = (bits & W_BIT ? EISDIR : OK);
139 break;
140
141 case I_CHAR_SPECIAL:
142 case I_BLOCK_SPECIAL:
143 /* Invoke the driver for special processing. */
144 dev_mess.m_type = DEV_OPEN;
145 dev = (dev_t) rip->i_zone[0];
146 dev_mess.DEVICE = dev;
147 dev_mess.COUNT = bits | (oflags & ~O_ACCMODE);
148 major = (dev >> MAJOR) & BYTE; /* major device nr */
149 if (major <= 0 || major >= max_major) {
150 r = ENODEV;
151 break;
152 }
153 task = dmap[major].dmap_task; /* device task nr */
154 (*dmap[major].dmap_open)(task, &dev_mess);
155 r = dev_mess.REP_STATUS;
156 break;
157
158 case I_NAMED_PIPE:
159 oflags |= O_APPEND; /* force append mode */
160 fil_ptr->filp_flags = oflags;
161 r = pipe_open(rip, bits, oflags);
162 if (r == OK) {
163 /* See if someone else is doing a rd or wt on
164 * the FIFO. If so, use its filp entry so the
165 * file position will be automatically shared.
166 */
167 b = (bits & R_BIT ? R_BIT : W_BIT);
168 fil_ptr->filp_count = 0; /* don't find self */
169 if ((filp2 = find_filp(rip, b)) != NIL_FILP) {
170 /* Co-reader or writer found. Use it.*/
171 fp->fp_filp[fd] = filp2;
172 filp2->filp_count++;
173 filp2->filp_ino = rip;
174 filp2->filp_flags = oflags;
175
176 /* i_count was incremented incorrectly
177 * by eatpath above, not knowing that
178 * we were going to use an existing
179 * filp entry. Correct this error.
180 */
181 rip->i_count--;
182 } else {
183 /* Nobody else found. Restore filp. */
184 fil_ptr->filp_count = 1;
185 if (b == R_BIT)
186 pos = rip->i_zone[V2_NR_DZONES+1];
187 else
188 pos = rip->i_zone[V2_NR_DZONES+2];
189 fil_ptr->filp_pos = pos;
190 }
191 }
192 break;
193 }
194 }
195 }
196
197 /* If error, release inode. */
198 if (r != OK) {
199 fp->fp_filp[fd] = NIL_FILP;
200 fil_ptr->filp_count= 0;
201 put_inode(rip);
202 return(r);
203 }
204
205 return(fd);
206 }
207
208
209 /*===========================================================================*
210 * new_node *
211 *===========================================================================*/
212 PRIVATE struct inode *new_node(path, bits, z0)
213 char *path; /* pointer to path name */
214 mode_t bits; /* mode of the new inode */
215 zone_t z0; /* zone number 0 for new inode */
216 {
217 /* New_node() is called by common_open(), do_mknod(), and do_mkdir().
218 * In all cases it allocates a new inode, makes a directory entry for it on
219 * the path 'path', and initializes it. It returns a pointer to the inode if
220 * it can do this; otherwise it returns NIL_INODE. It always sets 'err_code'
221 * to an appropriate value (OK or an error code).
222 */
223
224 register struct inode *rlast_dir_ptr, *rip;
225 register int r;
226 char string[NAME_MAX];
227
228 /* See if the path can be opened down to the last directory. */
229 if ((rlast_dir_ptr = last_dir(path, string)) == NIL_INODE) return(NIL_INODE);
230
231 /* The final directory is accessible. Get final component of the path. */
232 rip = advance(rlast_dir_ptr, string);
233 if ( rip == NIL_INODE && err_code == ENOENT) {
234 /* Last path component does not exist. Make new directory entry. */
235 if ( (rip = alloc_inode(rlast_dir_ptr->i_dev, bits)) == NIL_INODE) {
236 /* Can't creat new inode: out of inodes. */
237 put_inode(rlast_dir_ptr);
238 return(NIL_INODE);
239 }
240
241 /* Force inode to the disk before making directory entry to make
242 * the system more robust in the face of a crash: an inode with
243 * no directory entry is much better than the opposite.
244 */
245 rip->i_nlinks++;
246 rip->i_zone[0] = z0; /* major/minor device numbers */
247 rw_inode(rip, WRITING); /* force inode to disk now */
248
249 /* New inode acquired. Try to make directory entry. */
250 if ((r = search_dir(rlast_dir_ptr, string, &rip->i_num,ENTER)) != OK) {
251 put_inode(rlast_dir_ptr);
252 rip->i_nlinks--; /* pity, have to free disk inode */
253 rip->i_dirt = DIRTY; /* dirty inodes are written out */
254 put_inode(rip); /* this call frees the inode */
255 err_code = r;
256 return(NIL_INODE);
257 }
258
259 } else {
260 /* Either last component exists, or there is some problem. */
261 if (rip != NIL_INODE)
262 r = EEXIST;
263 else
264 r = err_code;
265 }
266
267 /* Return the directory inode and exit. */
268 put_inode(rlast_dir_ptr);
269 err_code = r;
270 return(rip);
271 }
272
273
274 /*===========================================================================*
275 * pipe_open *
276 *===========================================================================*/
277 PRIVATE int pipe_open(rip, bits, oflags)
278 register struct inode *rip;
279 register mode_t bits;
280 register int oflags;
281 {
282 /* This function is called from common_open. It checks if
283 * there is at least one reader/writer pair for the pipe, if not
284 * it suspends the caller, otherwise it revives all other blocked
285 * processes hanging on the pipe.
286 */
287
288 if (find_filp(rip, bits & W_BIT ? R_BIT : W_BIT) == NIL_FILP) {
289 if (oflags & O_NONBLOCK) {
290 if (bits & W_BIT) return(ENXIO);
291 } else
292 suspend(XPOPEN); /* suspend caller */
293 } else if (susp_count > 0) {/* revive blocked processes */
294 release(rip, OPEN, susp_count);
295 release(rip, CREAT, susp_count);
296 }
297 rip->i_pipe = I_PIPE;
298
299 return(OK);
300 }
301
302
303 /*===========================================================================*
304 * do_mknod *
305 *===========================================================================*/
306 PUBLIC int do_mknod()
307 {
308 /* Perform the mknod(name, mode, addr) system call. */
309
310 register mode_t bits, mode_bits;
311 struct inode *ip;
312
313 /* Only the super_user may make nodes other than fifos. */
314 mode_bits = (mode_t) m.m1_i2; /* mode of the inode */
315 if (!super_user && ((mode_bits & I_TYPE) != I_NAMED_PIPE)) return(EPERM);
316 if (fetch_name(m.m1_p1, m.m1_i1, M1) != OK) return(err_code);
317 bits = (mode_bits & I_TYPE) | (mode_bits & ALL_MODES & fp->fp_umask);
318 ip = new_node(user_path, bits, (zone_t) m.m1_i3);
319 put_inode(ip);
320 return(err_code);
321 }
322
323
324 /*===========================================================================*
325 * do_mkdir *
326 *===========================================================================*/
327 PUBLIC int do_mkdir()
328 {
329 /* Perform the mkdir(name, mode) system call. */
330
331 int r1, r2; /* status codes */
332 ino_t dot, dotdot; /* inode numbers for . and .. */
333 mode_t bits; /* mode bits for the new inode */
334 char string[NAME_MAX]; /* last component of the new dir's path name */
335 register struct inode *rip, *ldirp;
336
337 /* Check to see if it is possible to make another link in the parent dir. */
338 if (fetch_name(name1, name1_length, M1) != OK) return(err_code);
339 ldirp = last_dir(user_path, string); /* pointer to new dir's parent */
340 if (ldirp == NIL_INODE) return(err_code);
341 if ( (ldirp->i_nlinks & BYTE) >= LINK_MAX) {
342 put_inode(ldirp); /* return parent */
343 return(EMLINK);
344 }
345
346 /* Next make the inode. If that fails, return error code. */
347 bits = I_DIRECTORY | (mode & RWX_MODES & fp->fp_umask);
348 rip = new_node(user_path, bits, (zone_t) 0);
349 if (rip == NIL_INODE || err_code == EEXIST) {
350 put_inode(rip); /* can't make dir: it already exists */
351 put_inode(ldirp); /* return parent too */
352 return(err_code);
353 }
354
355 /* Get the inode numbers for . and .. to enter in the directory. */
356 dotdot = ldirp->i_num; /* parent's inode number */
357 dot = rip->i_num; /* inode number of the new dir itself */
358
359 /* Now make dir entries for . and .. unless the disk is completely full. */
360 /* Use dot1 and dot2, so the mode of the directory isn't important. */
361 rip->i_mode = bits; /* set mode */
362 r1 = search_dir(rip, dot1, &dot, ENTER); /* enter . in the new dir */
363 r2 = search_dir(rip, dot2, &dotdot, ENTER); /* enter .. in the new dir */
364
365 /* If both . and .. were successfully entered, increment the link counts. */
366 if (r1 == OK && r2 == OK) {
367 /* Normal case. It was possible to enter . and .. in the new dir. */
368 rip->i_nlinks++; /* this accounts for . */
369 ldirp->i_nlinks++; /* this accounts for .. */
370 ldirp->i_dirt = DIRTY; /* mark parent's inode as dirty */
371 } else {
372 /* It was not possible to enter . or .. probably disk was full. */
373 (void) search_dir(ldirp, string, (ino_t *) 0, DELETE);
374 rip->i_nlinks--; /* undo the increment done in new_node() */
375 }
376 rip->i_dirt = DIRTY; /* either way, i_nlinks has changed */
377
378 put_inode(ldirp); /* return the inode of the parent dir */
379 put_inode(rip); /* return the inode of the newly made dir */
380 return(err_code); /* new_node() always sets 'err_code' */
381 }
382
383
384 /*===========================================================================*
385 * do_close *
386 *===========================================================================*/
387 PUBLIC int do_close()
388 {
389 /* Perform the close(fd) system call. */
390
391 register struct filp *rfilp;
392 register struct inode *rip;
393 struct file_lock *flp;
394 int rw, mode_word, major, task, lock_count;
395 dev_t dev;
396
397 /* First locate the inode that belongs to the file descriptor. */
398 if ( (rfilp = get_filp(fd)) == NIL_FILP) return(err_code);
399 rip = rfilp->filp_ino; /* 'rip' points to the inode */
400
401 if (rfilp->filp_count - 1 == 0 && rfilp->filp_mode != FILP_CLOSED) {
402 /* Check to see if the file is special. */
403 mode_word = rip->i_mode & I_TYPE;
404 if (mode_word == I_CHAR_SPECIAL || mode_word == I_BLOCK_SPECIAL) {
405 dev = (dev_t) rip->i_zone[0];
406 if (mode_word == I_BLOCK_SPECIAL) {
407 /* Invalidate cache entries unless special is mounted
408 * or ROOT
409 */
410 if (!mounted(rip)) {
411 (void) do_sync(); /* purge cache */
412 invalidate(dev);
413 }
414 }
415 /* Use the dmap_close entry to do any special processing
416 * required.
417 */
418 dev_mess.m_type = DEV_CLOSE;
419 dev_mess.DEVICE = dev;
420 major = (dev >> MAJOR) & BYTE; /* major device nr */
421 task = dmap[major].dmap_task; /* device task nr */
422 (*dmap[major].dmap_close)(task, &dev_mess);
423 }
424 }
425
426 /* If the inode being closed is a pipe, release everyone hanging on it. */
427 if (rip->i_pipe == I_PIPE) {
428 rw = (rfilp->filp_mode & R_BIT ? WRITE : READ);
429 release(rip, rw, NR_PROCS);
430 }
431
432 /* If a write has been done, the inode is already marked as DIRTY. */
433 if (--rfilp->filp_count == 0) {
434 if (rip->i_pipe == I_PIPE && rip->i_count > 1) {
435 /* Save the file position in the i-node in case needed later.
436 * The read and write positions are saved separately. The
437 * last 3 zones in the i-node are not used for (named) pipes.
438 */
439 if (rfilp->filp_mode == R_BIT)
440 rip->i_zone[V2_NR_DZONES+1] = (zone_t) rfilp->filp_pos;
441 else
442 rip->i_zone[V2_NR_DZONES+2] = (zone_t) rfilp->filp_pos;
443 }
444 put_inode(rip);
445 }
446
447 fp->fp_cloexec &= ~(1L << fd); /* turn off close-on-exec bit */
448 fp->fp_filp[fd] = NIL_FILP;
449
450 /* Check to see if the file is locked. If so, release all locks. */
451 if (nr_locks == 0) return(OK);
452 lock_count = nr_locks; /* save count of locks */
453 for (flp = &file_lock[0]; flp < &file_lock[NR_LOCKS]; flp++) {
454 if (flp->lock_type == 0) continue; /* slot not in use */
455 if (flp->lock_inode == rip && flp->lock_pid == fp->fp_pid) {
456 flp->lock_type = 0;
457 nr_locks--;
458 }
459 }
460 if (nr_locks < lock_count) lock_revive(); /* lock released */
461 return(OK);
462 }
463
464
465 /*===========================================================================*
466 * do_lseek *
467 *===========================================================================*/
468 PUBLIC int do_lseek()
469 {
470 /* Perform the lseek(ls_fd, offset, whence) system call. */
471
472 register struct filp *rfilp;
473 register off_t pos;
474
475 /* Check to see if the file descriptor is valid. */
476 if ( (rfilp = get_filp(ls_fd)) == NIL_FILP) return(err_code);
477
478 /* No lseek on pipes. */
479 if (rfilp->filp_ino->i_pipe == I_PIPE) return(ESPIPE);
480
481 /* The value of 'whence' determines the start position to use. */
482 switch(whence) {
483 case 0: pos = 0; break;
484 case 1: pos = rfilp->filp_pos; break;
485 case 2: pos = rfilp->filp_ino->i_size; break;
486 default: return(EINVAL);
487 }
488
489 /* Check for overflow. */
490 if (((long)offset > 0) && ((long)(pos + offset) < (long)pos)) return(EINVAL);
491 if (((long)offset < 0) && ((long)(pos + offset) > (long)pos)) return(EINVAL);
492 pos = pos + offset;
493
494 if (pos != rfilp->filp_pos)
495 rfilp->filp_ino->i_seek = ISEEK; /* inhibit read ahead */
496 rfilp->filp_pos = pos;
497 reply_l1 = pos; /* insert the long into the output message */
498 return(OK);
499 }
500
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.