1 | /* This file handles the LINK and UNLINK system calls. It also deals with
|
---|
2 | * deallocating the storage used by a file when the last UNLINK is done to a
|
---|
3 | * file and the blocks must be returned to the free block pool.
|
---|
4 | *
|
---|
5 | * The entry points into this file are
|
---|
6 | * do_link: perform the LINK system call
|
---|
7 | * do_unlink: perform the UNLINK and RMDIR system calls
|
---|
8 | * do_rename: perform the RENAME system call
|
---|
9 | * do_truncate: perform the TRUNCATE system call
|
---|
10 | * do_ftruncate: perform the FTRUNCATE system call
|
---|
11 | * truncate_inode: release the blocks associated with an inode up to a size
|
---|
12 | * freesp_inode: release a range of blocks without setting the size
|
---|
13 | */
|
---|
14 |
|
---|
15 | #include "fs.h"
|
---|
16 | #include <sys/stat.h>
|
---|
17 | #include <string.h>
|
---|
18 | #include <minix/com.h>
|
---|
19 | #include <minix/callnr.h>
|
---|
20 | #include "buf.h"
|
---|
21 | #include "file.h"
|
---|
22 | #include "fproc.h"
|
---|
23 | #include "inode.h"
|
---|
24 | #include "param.h"
|
---|
25 | #include "super.h"
|
---|
26 |
|
---|
27 | #define SAME 1000
|
---|
28 |
|
---|
29 | FORWARD _PROTOTYPE( int remove_dir, (struct inode *rldirp, struct inode *rip,
|
---|
30 | char dir_name[NAME_MAX]) );
|
---|
31 | FORWARD _PROTOTYPE( int unlink_file, (struct inode *dirp, struct inode *rip,
|
---|
32 | char file_name[NAME_MAX]) );
|
---|
33 | FORWARD _PROTOTYPE( off_t nextblock, (off_t pos, int zonesize) );
|
---|
34 | FORWARD _PROTOTYPE( void zeroblock_half, (struct inode *i, off_t p, int l));
|
---|
35 | FORWARD _PROTOTYPE( void zeroblock_range, (struct inode *i, off_t p, off_t h));
|
---|
36 |
|
---|
37 | /* Args to zeroblock_half() */
|
---|
38 | #define FIRST_HALF 0
|
---|
39 | #define LAST_HALF 1
|
---|
40 |
|
---|
41 | /*===========================================================================*
|
---|
42 | * do_link *
|
---|
43 | *===========================================================================*/
|
---|
44 | PUBLIC int do_link()
|
---|
45 | {
|
---|
46 | /* Perform the link(name1, name2) system call. */
|
---|
47 |
|
---|
48 | struct inode *ip, *rip;
|
---|
49 | register int r;
|
---|
50 | char string[NAME_MAX];
|
---|
51 | struct inode *new_ip;
|
---|
52 |
|
---|
53 | /* See if 'name' (file to be linked) exists. */
|
---|
54 | if (fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code);
|
---|
55 | if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code);
|
---|
56 |
|
---|
57 | /* Check to see if the file has maximum number of links already. */
|
---|
58 | r = OK;
|
---|
59 | if (rip->i_nlinks >= (rip->i_sp->s_version == V1 ? CHAR_MAX : SHRT_MAX))
|
---|
60 | r = EMLINK;
|
---|
61 |
|
---|
62 | /* Only super_user may link to directories. */
|
---|
63 | if (r == OK)
|
---|
64 | if ( (rip->i_mode & I_TYPE) == I_DIRECTORY && !super_user) r = EPERM;
|
---|
65 |
|
---|
66 | /* If error with 'name', return the inode. */
|
---|
67 | if (r != OK) {
|
---|
68 | put_inode(rip);
|
---|
69 | return(r);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* Does the final directory of 'name2' exist? */
|
---|
73 | if (fetch_name(m_in.name2, m_in.name2_length, M1) != OK) {
|
---|
74 | put_inode(rip);
|
---|
75 | return(err_code);
|
---|
76 | }
|
---|
77 | if ( (ip = last_dir(user_path, string)) == NIL_INODE) r = err_code;
|
---|
78 |
|
---|
79 | /* If 'name2' exists in full (even if no space) set 'r' to error. */
|
---|
80 | if (r == OK) {
|
---|
81 | if ( (new_ip = advance(&ip, string)) == NIL_INODE) {
|
---|
82 | r = err_code;
|
---|
83 | if (r == ENOENT) r = OK;
|
---|
84 | } else {
|
---|
85 | put_inode(new_ip);
|
---|
86 | r = EEXIST;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | /* Check for links across devices. */
|
---|
91 | if (r == OK)
|
---|
92 | if (rip->i_dev != ip->i_dev) r = EXDEV;
|
---|
93 |
|
---|
94 | /* Try to link. */
|
---|
95 | if (r == OK)
|
---|
96 | r = search_dir(ip, string, &rip->i_num, ENTER);
|
---|
97 |
|
---|
98 | /* If success, register the linking. */
|
---|
99 | if (r == OK) {
|
---|
100 | rip->i_nlinks++;
|
---|
101 | rip->i_update |= CTIME;
|
---|
102 | rip->i_dirt = DIRTY;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /* Done. Release both inodes. */
|
---|
106 | put_inode(rip);
|
---|
107 | put_inode(ip);
|
---|
108 | return(r);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /*===========================================================================*
|
---|
112 | * do_unlink *
|
---|
113 | *===========================================================================*/
|
---|
114 | PUBLIC int do_unlink()
|
---|
115 | {
|
---|
116 | /* Perform the unlink(name) or rmdir(name) system call. The code for these two
|
---|
117 | * is almost the same. They differ only in some condition testing. Unlink()
|
---|
118 | * may be used by the superuser to do dangerous things; rmdir() may not.
|
---|
119 | */
|
---|
120 |
|
---|
121 | register struct inode *rip;
|
---|
122 | struct inode *rldirp;
|
---|
123 | int r;
|
---|
124 | char string[NAME_MAX];
|
---|
125 |
|
---|
126 | /* Get the last directory in the path. */
|
---|
127 | if (fetch_name(m_in.name, m_in.name_length, M3) != OK) return(err_code);
|
---|
128 | if ( (rldirp = last_dir(user_path, string)) == NIL_INODE)
|
---|
129 | return(err_code);
|
---|
130 |
|
---|
131 | /* The last directory exists. Does the file also exist? */
|
---|
132 | r = OK;
|
---|
133 | if ( (rip = advance(&rldirp, string)) == NIL_INODE) r = err_code;
|
---|
134 |
|
---|
135 | /* If error, return inode. */
|
---|
136 | if (r != OK) {
|
---|
137 | put_inode(rldirp);
|
---|
138 | return(r);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /* Do not remove a mount point. */
|
---|
142 | if (rip->i_num == ROOT_INODE) {
|
---|
143 | put_inode(rldirp);
|
---|
144 | put_inode(rip);
|
---|
145 | return(EBUSY);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* Now test if the call is allowed, separately for unlink() and rmdir(). */
|
---|
149 | if (call_nr == UNLINK) {
|
---|
150 | /* Only the su may unlink directories, but the su can unlink any dir.*/
|
---|
151 | if ( (rip->i_mode & I_TYPE) == I_DIRECTORY && !super_user) r = EPERM;
|
---|
152 |
|
---|
153 | /* Don't unlink a file if it is the root of a mounted file system. */
|
---|
154 | if (rip->i_num == ROOT_INODE) r = EBUSY;
|
---|
155 |
|
---|
156 | /* Actually try to unlink the file; fails if parent is mode 0 etc. */
|
---|
157 | if (r == OK) r = unlink_file(rldirp, rip, string);
|
---|
158 |
|
---|
159 | } else {
|
---|
160 | r = remove_dir(rldirp, rip, string); /* call is RMDIR */
|
---|
161 | }
|
---|
162 |
|
---|
163 | /* If unlink was possible, it has been done, otherwise it has not. */
|
---|
164 | put_inode(rip);
|
---|
165 | put_inode(rldirp);
|
---|
166 | return(r);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /*===========================================================================*
|
---|
170 | * do_rename *
|
---|
171 | *===========================================================================*/
|
---|
172 | PUBLIC int do_rename()
|
---|
173 | {
|
---|
174 | /* Perform the rename(name1, name2) system call. */
|
---|
175 |
|
---|
176 | struct inode *old_dirp, *old_ip; /* ptrs to old dir, file inodes */
|
---|
177 | struct inode *new_dirp, *new_ip; /* ptrs to new dir, file inodes */
|
---|
178 | struct inode *new_superdirp, *next_new_superdirp;
|
---|
179 | int r = OK; /* error flag; initially no error */
|
---|
180 | int odir, ndir; /* TRUE iff {old|new} file is dir */
|
---|
181 | int same_pdir; /* TRUE iff parent dirs are the same */
|
---|
182 | char old_name[NAME_MAX], new_name[NAME_MAX];
|
---|
183 | ino_t numb;
|
---|
184 | int r1;
|
---|
185 |
|
---|
186 | /* See if 'name1' (existing file) exists. Get dir and file inodes. */
|
---|
187 | if (fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code);
|
---|
188 | if ( (old_dirp = last_dir(user_path, old_name))==NIL_INODE) return(err_code);
|
---|
189 |
|
---|
190 | if ( (old_ip = advance(&old_dirp, old_name)) == NIL_INODE) r = err_code;
|
---|
191 |
|
---|
192 | /* See if 'name2' (new name) exists. Get dir and file inodes. */
|
---|
193 | if (fetch_name(m_in.name2, m_in.name2_length, M1) != OK) r = err_code;
|
---|
194 | if ( (new_dirp = last_dir(user_path, new_name)) == NIL_INODE) r = err_code;
|
---|
195 | new_ip = advance(&new_dirp, new_name); /* not required to exist */
|
---|
196 |
|
---|
197 | if (old_ip != NIL_INODE)
|
---|
198 | odir = ((old_ip->i_mode & I_TYPE) == I_DIRECTORY); /* TRUE iff dir */
|
---|
199 |
|
---|
200 | /* If it is ok, check for a variety of possible errors. */
|
---|
201 | if (r == OK) {
|
---|
202 | same_pdir = (old_dirp == new_dirp);
|
---|
203 |
|
---|
204 | /* The old inode must not be a superdirectory of the new last dir. */
|
---|
205 | if (odir && !same_pdir) {
|
---|
206 | dup_inode(new_superdirp = new_dirp);
|
---|
207 | while (TRUE) { /* may hang in a file system loop */
|
---|
208 | if (new_superdirp == old_ip) {
|
---|
209 | r = EINVAL;
|
---|
210 | break;
|
---|
211 | }
|
---|
212 | next_new_superdirp = advance(&new_superdirp, dot2);
|
---|
213 | put_inode(new_superdirp);
|
---|
214 | if (next_new_superdirp == new_superdirp)
|
---|
215 | break; /* back at system root directory */
|
---|
216 | new_superdirp = next_new_superdirp;
|
---|
217 | if (new_superdirp == NIL_INODE) {
|
---|
218 | /* Missing ".." entry. Assume the worst. */
|
---|
219 | r = EINVAL;
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | put_inode(new_superdirp);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /* The old or new name must not be . or .. */
|
---|
227 | if (strcmp(old_name, ".")==0 || strcmp(old_name, "..")==0 ||
|
---|
228 | strcmp(new_name, ".")==0 || strcmp(new_name, "..")==0) r = EINVAL;
|
---|
229 |
|
---|
230 | /* Both parent directories must be on the same device. */
|
---|
231 | if (old_dirp->i_dev != new_dirp->i_dev) r = EXDEV;
|
---|
232 |
|
---|
233 | /* Parent dirs must be writable, searchable and on a writable device */
|
---|
234 | if ((r1 = forbidden(old_dirp, W_BIT | X_BIT)) != OK ||
|
---|
235 | (r1 = forbidden(new_dirp, W_BIT | X_BIT)) != OK) r = r1;
|
---|
236 |
|
---|
237 | /* Some tests apply only if the new path exists. */
|
---|
238 | if (new_ip == NIL_INODE) {
|
---|
239 | /* don't rename a file with a file system mounted on it. */
|
---|
240 | if (old_ip->i_dev != old_dirp->i_dev) r = EXDEV;
|
---|
241 | if (odir && new_dirp->i_nlinks >=
|
---|
242 | (new_dirp->i_sp->s_version == V1 ? CHAR_MAX : SHRT_MAX) &&
|
---|
243 | !same_pdir && r == OK) r = EMLINK;
|
---|
244 | } else {
|
---|
245 | if (old_ip == new_ip) r = SAME; /* old=new */
|
---|
246 |
|
---|
247 | /* has the old file or new file a file system mounted on it? */
|
---|
248 | if (old_ip->i_dev != new_ip->i_dev) r = EXDEV;
|
---|
249 |
|
---|
250 | ndir = ((new_ip->i_mode & I_TYPE) == I_DIRECTORY); /* dir ? */
|
---|
251 | if (odir == TRUE && ndir == FALSE) r = ENOTDIR;
|
---|
252 | if (odir == FALSE && ndir == TRUE) r = EISDIR;
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | /* If a process has another root directory than the system root, we might
|
---|
257 | * "accidently" be moving it's working directory to a place where it's
|
---|
258 | * root directory isn't a super directory of it anymore. This can make
|
---|
259 | * the function chroot useless. If chroot will be used often we should
|
---|
260 | * probably check for it here.
|
---|
261 | */
|
---|
262 |
|
---|
263 | /* The rename will probably work. Only two things can go wrong now:
|
---|
264 | * 1. being unable to remove the new file. (when new file already exists)
|
---|
265 | * 2. being unable to make the new directory entry. (new file doesn't exists)
|
---|
266 | * [directory has to grow by one block and cannot because the disk
|
---|
267 | * is completely full].
|
---|
268 | */
|
---|
269 | if (r == OK) {
|
---|
270 | if (new_ip != NIL_INODE) {
|
---|
271 | /* There is already an entry for 'new'. Try to remove it. */
|
---|
272 | if (odir)
|
---|
273 | r = remove_dir(new_dirp, new_ip, new_name);
|
---|
274 | else
|
---|
275 | r = unlink_file(new_dirp, new_ip, new_name);
|
---|
276 | }
|
---|
277 | /* if r is OK, the rename will succeed, while there is now an
|
---|
278 | * unused entry in the new parent directory.
|
---|
279 | */
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (r == OK) {
|
---|
283 | /* If the new name will be in the same parent directory as the old one,
|
---|
284 | * first remove the old name to free an entry for the new name,
|
---|
285 | * otherwise first try to create the new name entry to make sure
|
---|
286 | * the rename will succeed.
|
---|
287 | */
|
---|
288 | numb = old_ip->i_num; /* inode number of old file */
|
---|
289 |
|
---|
290 | if (same_pdir) {
|
---|
291 | r = search_dir(old_dirp, old_name, (ino_t *) 0, DELETE);
|
---|
292 | /* shouldn't go wrong. */
|
---|
293 | if (r==OK) (void) search_dir(old_dirp, new_name, &numb, ENTER);
|
---|
294 | } else {
|
---|
295 | r = search_dir(new_dirp, new_name, &numb, ENTER);
|
---|
296 | if (r == OK)
|
---|
297 | (void) search_dir(old_dirp, old_name, (ino_t *) 0, DELETE);
|
---|
298 | }
|
---|
299 | }
|
---|
300 | /* If r is OK, the ctime and mtime of old_dirp and new_dirp have been marked
|
---|
301 | * for update in search_dir.
|
---|
302 | */
|
---|
303 |
|
---|
304 | if (r == OK && odir && !same_pdir) {
|
---|
305 | /* Update the .. entry in the directory (still points to old_dirp). */
|
---|
306 | numb = new_dirp->i_num;
|
---|
307 | (void) unlink_file(old_ip, NIL_INODE, dot2);
|
---|
308 | if (search_dir(old_ip, dot2, &numb, ENTER) == OK) {
|
---|
309 | /* New link created. */
|
---|
310 | new_dirp->i_nlinks++;
|
---|
311 | new_dirp->i_dirt = DIRTY;
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | /* Release the inodes. */
|
---|
316 | put_inode(old_dirp);
|
---|
317 | put_inode(old_ip);
|
---|
318 | put_inode(new_dirp);
|
---|
319 | put_inode(new_ip);
|
---|
320 | return(r == SAME ? OK : r);
|
---|
321 | }
|
---|
322 |
|
---|
323 | /*===========================================================================*
|
---|
324 | * do_truncate *
|
---|
325 | *===========================================================================*/
|
---|
326 | PUBLIC int do_truncate()
|
---|
327 | {
|
---|
328 | /* truncate_inode() does the actual work of do_truncate() and do_ftruncate().
|
---|
329 | * do_truncate() and do_ftruncate() have to get hold of the inode, either
|
---|
330 | * by name or fd, do checks on it, and call truncate_inode() to do the
|
---|
331 | * work.
|
---|
332 | */
|
---|
333 | int r;
|
---|
334 | struct inode *rip; /* pointer to inode to be truncated */
|
---|
335 |
|
---|
336 | if (fetch_name(m_in.m2_p1, m_in.m2_i1, M1) != OK)
|
---|
337 | return err_code;
|
---|
338 | if( (rip = eat_path(user_path)) == NIL_INODE)
|
---|
339 | return err_code;
|
---|
340 | if ( (rip->i_mode & I_TYPE) != I_REGULAR)
|
---|
341 | r = EINVAL;
|
---|
342 | else
|
---|
343 | r = truncate_inode(rip, m_in.m2_l1);
|
---|
344 | put_inode(rip);
|
---|
345 |
|
---|
346 | return r;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /*===========================================================================*
|
---|
350 | * do_ftruncate *
|
---|
351 | *===========================================================================*/
|
---|
352 | PUBLIC int do_ftruncate()
|
---|
353 | {
|
---|
354 | /* As with do_truncate(), truncate_inode() does the actual work. */
|
---|
355 | struct filp *rfilp;
|
---|
356 | if ( (rfilp = get_filp(m_in.m2_i1)) == NIL_FILP)
|
---|
357 | return err_code;
|
---|
358 | if ( (rfilp->filp_ino->i_mode & I_TYPE) != I_REGULAR)
|
---|
359 | return EINVAL;
|
---|
360 | return truncate_inode(rfilp->filp_ino, m_in.m2_l1);
|
---|
361 | }
|
---|
362 |
|
---|
363 | /*===========================================================================*
|
---|
364 | * truncate_inode *
|
---|
365 | *===========================================================================*/
|
---|
366 | PUBLIC int truncate_inode(rip, newsize)
|
---|
367 | register struct inode *rip; /* pointer to inode to be truncated */
|
---|
368 | off_t newsize; /* inode must become this size */
|
---|
369 | {
|
---|
370 | /* Set inode to a certain size, freeing any zones no longer referenced
|
---|
371 | * and updating the size in the inode. If the inode is extended, the
|
---|
372 | * extra space is a hole that reads as zeroes.
|
---|
373 | *
|
---|
374 | * Nothing special has to happen to file pointers if inode is opened in
|
---|
375 | * O_APPEND mode, as this is different per fd and is checked when
|
---|
376 | * writing is done.
|
---|
377 | */
|
---|
378 | zone_t zone_size;
|
---|
379 | int scale, file_type, waspipe;
|
---|
380 | dev_t dev;
|
---|
381 |
|
---|
382 | file_type = rip->i_mode & I_TYPE; /* check to see if file is special */
|
---|
383 | if (file_type == I_CHAR_SPECIAL || file_type == I_BLOCK_SPECIAL)
|
---|
384 | return EINVAL;
|
---|
385 | if(newsize > rip->i_sp->s_max_size) /* don't let inode grow too big */
|
---|
386 | return EFBIG;
|
---|
387 |
|
---|
388 | dev = rip->i_dev; /* device on which inode resides */
|
---|
389 | scale = rip->i_sp->s_log_zone_size;
|
---|
390 | zone_size = (zone_t) rip->i_sp->s_block_size << scale;
|
---|
391 |
|
---|
392 | /* Pipes can shrink, so adjust size to make sure all zones are removed. */
|
---|
393 | waspipe = rip->i_pipe == I_PIPE; /* TRUE if this was a pipe */
|
---|
394 | if (waspipe) {
|
---|
395 | if(newsize != 0)
|
---|
396 | return EINVAL; /* Only truncate pipes to 0. */
|
---|
397 | rip->i_size = PIPE_SIZE(rip->i_sp->s_block_size);
|
---|
398 | }
|
---|
399 |
|
---|
400 | /* Free the actual space if relevant. */
|
---|
401 | if(newsize < rip->i_size)
|
---|
402 | freesp_inode(rip, newsize, rip->i_size);
|
---|
403 |
|
---|
404 | /* Next correct the inode size. */
|
---|
405 | if(!waspipe) rip->i_size = newsize;
|
---|
406 | else wipe_inode(rip); /* Pipes can only be truncated to 0. */
|
---|
407 | rip->i_dirt = DIRTY;
|
---|
408 |
|
---|
409 | return OK;
|
---|
410 | }
|
---|
411 |
|
---|
412 | /*===========================================================================*
|
---|
413 | * freesp_inode *
|
---|
414 | *===========================================================================*/
|
---|
415 | PUBLIC int freesp_inode(rip, start, end)
|
---|
416 | register struct inode *rip; /* pointer to inode to be partly freed */
|
---|
417 | off_t start, end; /* range of bytes to free (end uninclusive) */
|
---|
418 | {
|
---|
419 | /* Cut an arbitrary hole in an inode. The caller is responsible for checking
|
---|
420 | * the reasonableness of the inode type of rip. The reason is this is that
|
---|
421 | * this function can be called for different reasons, for which different
|
---|
422 | * sets of inode types are reasonable. Adjusting the final size of the inode
|
---|
423 | * is to be done by the caller too, if wished.
|
---|
424 | *
|
---|
425 | * Consumers of this function currently are truncate_inode() (used to
|
---|
426 | * free indirect and data blocks for any type of inode, but also to
|
---|
427 | * implement the ftruncate() and truncate() system calls) and the F_FREESP
|
---|
428 | * fcntl().
|
---|
429 | */
|
---|
430 | off_t p, e;
|
---|
431 | int zone_size, dev;
|
---|
432 |
|
---|
433 | if(end > rip->i_size) /* freeing beyond end makes no sense */
|
---|
434 | end = rip->i_size;
|
---|
435 | if(end <= start) /* end is uninclusive, so start<end */
|
---|
436 | return EINVAL;
|
---|
437 | zone_size = rip->i_sp->s_block_size << rip->i_sp->s_log_zone_size;
|
---|
438 | dev = rip->i_dev; /* device on which inode resides */
|
---|
439 |
|
---|
440 | /* If freeing doesn't cross a zone boundary, then we may only zero
|
---|
441 | * a range of the block.
|
---|
442 | */
|
---|
443 | if(start/zone_size == (end-1)/zone_size) {
|
---|
444 | zeroblock_range(rip, start, end-start);
|
---|
445 | } else {
|
---|
446 | /* First zero unused part of partly used blocks. */
|
---|
447 | if(start%zone_size)
|
---|
448 | zeroblock_half(rip, start, LAST_HALF);
|
---|
449 | if(end%zone_size && end < rip->i_size)
|
---|
450 | zeroblock_half(rip, end, FIRST_HALF);
|
---|
451 | }
|
---|
452 |
|
---|
453 | /* Now completely free the completely unused blocks.
|
---|
454 | * write_map() will free unused (double) indirect
|
---|
455 | * blocks too. Converting the range to zone numbers avoids
|
---|
456 | * overflow on p when doing e.g. 'p += zone_size'.
|
---|
457 | */
|
---|
458 | e = end/zone_size;
|
---|
459 | if(end == rip->i_size && (end % zone_size)) e++;
|
---|
460 | for(p = nextblock(start, zone_size)/zone_size; p < e; p ++)
|
---|
461 | write_map(rip, p*zone_size, NO_ZONE, WMAP_FREE);
|
---|
462 |
|
---|
463 | return OK;
|
---|
464 | }
|
---|
465 |
|
---|
466 | /*===========================================================================*
|
---|
467 | * nextblock *
|
---|
468 | *===========================================================================*/
|
---|
469 | PRIVATE off_t nextblock(pos, zone_size)
|
---|
470 | off_t pos;
|
---|
471 | int zone_size;
|
---|
472 | {
|
---|
473 | /* Return the first position in the next block after position 'pos'
|
---|
474 | * (unless this is the first position in the current block).
|
---|
475 | * This can be done in one expression, but that can overflow pos.
|
---|
476 | */
|
---|
477 | off_t p;
|
---|
478 | p = (pos/zone_size)*zone_size;
|
---|
479 | if((pos % zone_size)) p += zone_size; /* Round up. */
|
---|
480 | return p;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /*===========================================================================*
|
---|
484 | * zeroblock_half *
|
---|
485 | *===========================================================================*/
|
---|
486 | PRIVATE void zeroblock_half(rip, pos, half)
|
---|
487 | struct inode *rip;
|
---|
488 | off_t pos;
|
---|
489 | int half;
|
---|
490 | {
|
---|
491 | /* Zero the upper or lower 'half' of a block that holds position 'pos'.
|
---|
492 | * half can be FIRST_HALF or LAST_HALF.
|
---|
493 | *
|
---|
494 | * FIRST_HALF: 0..pos-1 will be zeroed
|
---|
495 | * LAST_HALF: pos..blocksize-1 will be zeroed
|
---|
496 | */
|
---|
497 | int offset, len;
|
---|
498 |
|
---|
499 | /* Offset of zeroing boundary. */
|
---|
500 | offset = pos % rip->i_sp->s_block_size;
|
---|
501 |
|
---|
502 | if(half == LAST_HALF) {
|
---|
503 | len = rip->i_sp->s_block_size - offset;
|
---|
504 | } else {
|
---|
505 | len = offset;
|
---|
506 | pos -= offset;
|
---|
507 | offset = 0;
|
---|
508 | }
|
---|
509 |
|
---|
510 | zeroblock_range(rip, pos, len);
|
---|
511 | }
|
---|
512 |
|
---|
513 | /*===========================================================================*
|
---|
514 | * zeroblock_range *
|
---|
515 | *===========================================================================*/
|
---|
516 | PRIVATE void zeroblock_range(rip, pos, len)
|
---|
517 | struct inode *rip;
|
---|
518 | off_t pos;
|
---|
519 | off_t len;
|
---|
520 | {
|
---|
521 | /* Zero a range in a block.
|
---|
522 | * This function is used to zero a segment of a block, either
|
---|
523 | * FIRST_HALF of LAST_HALF.
|
---|
524 | *
|
---|
525 | */
|
---|
526 | block_t b;
|
---|
527 | struct buf *bp;
|
---|
528 | off_t offset;
|
---|
529 |
|
---|
530 | if(!len) return; /* no zeroing to be done. */
|
---|
531 | if( (b = read_map(rip, pos)) == NO_BLOCK) return;
|
---|
532 | if( (bp = get_block(rip->i_dev, b, NORMAL)) == NIL_BUF)
|
---|
533 | panic(__FILE__, "zeroblock_range: no block", NO_NUM);
|
---|
534 | offset = pos % rip->i_sp->s_block_size;
|
---|
535 | if(offset + len > rip->i_sp->s_block_size)
|
---|
536 | panic(__FILE__, "zeroblock_range: len too long", len);
|
---|
537 | memset(bp->b_data + offset, 0, len);
|
---|
538 | bp->b_dirt = DIRTY;
|
---|
539 | put_block(bp, FULL_DATA_BLOCK);
|
---|
540 | }
|
---|
541 |
|
---|
542 | /*===========================================================================*
|
---|
543 | * remove_dir *
|
---|
544 | *===========================================================================*/
|
---|
545 | PRIVATE int remove_dir(rldirp, rip, dir_name)
|
---|
546 | struct inode *rldirp; /* parent directory */
|
---|
547 | struct inode *rip; /* directory to be removed */
|
---|
548 | char dir_name[NAME_MAX]; /* name of directory to be removed */
|
---|
549 | {
|
---|
550 | /* A directory file has to be removed. Five conditions have to met:
|
---|
551 | * - The file must be a directory
|
---|
552 | * - The directory must be empty (except for . and ..)
|
---|
553 | * - The final component of the path must not be . or ..
|
---|
554 | * - The directory must not be the root of a mounted file system
|
---|
555 | * - The directory must not be anybody's root/working directory
|
---|
556 | */
|
---|
557 |
|
---|
558 | int r;
|
---|
559 | register struct fproc *rfp;
|
---|
560 |
|
---|
561 | /* search_dir checks that rip is a directory too. */
|
---|
562 | if ((r = search_dir(rip, "", (ino_t *) 0, IS_EMPTY)) != OK) return r;
|
---|
563 |
|
---|
564 | if (strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0)return(EINVAL);
|
---|
565 | if (rip->i_num == ROOT_INODE) return(EBUSY); /* can't remove 'root' */
|
---|
566 |
|
---|
567 | for (rfp = &fproc[INIT_PROC_NR + 1]; rfp < &fproc[NR_PROCS]; rfp++)
|
---|
568 | if (rfp->fp_pid != PID_FREE &&
|
---|
569 | (rfp->fp_workdir == rip || rfp->fp_rootdir == rip))
|
---|
570 | return(EBUSY); /* can't remove anybody's working dir */
|
---|
571 |
|
---|
572 | /* Actually try to unlink the file; fails if parent is mode 0 etc. */
|
---|
573 | if ((r = unlink_file(rldirp, rip, dir_name)) != OK) return r;
|
---|
574 |
|
---|
575 | /* Unlink . and .. from the dir. The super user can link and unlink any dir,
|
---|
576 | * so don't make too many assumptions about them.
|
---|
577 | */
|
---|
578 | (void) unlink_file(rip, NIL_INODE, dot1);
|
---|
579 | (void) unlink_file(rip, NIL_INODE, dot2);
|
---|
580 | return(OK);
|
---|
581 | }
|
---|
582 |
|
---|
583 | /*===========================================================================*
|
---|
584 | * unlink_file *
|
---|
585 | *===========================================================================*/
|
---|
586 | PRIVATE int unlink_file(dirp, rip, file_name)
|
---|
587 | struct inode *dirp; /* parent directory of file */
|
---|
588 | struct inode *rip; /* inode of file, may be NIL_INODE too. */
|
---|
589 | char file_name[NAME_MAX]; /* name of file to be removed */
|
---|
590 | {
|
---|
591 | /* Unlink 'file_name'; rip must be the inode of 'file_name' or NIL_INODE. */
|
---|
592 |
|
---|
593 | ino_t numb; /* inode number */
|
---|
594 | int r;
|
---|
595 |
|
---|
596 | /* If rip is not NIL_INODE, it is used to get faster access to the inode. */
|
---|
597 | if (rip == NIL_INODE) {
|
---|
598 | /* Search for file in directory and try to get its inode. */
|
---|
599 | err_code = search_dir(dirp, file_name, &numb, LOOK_UP);
|
---|
600 | if (err_code == OK) rip = get_inode(dirp->i_dev, (int) numb);
|
---|
601 | if (err_code != OK || rip == NIL_INODE) return(err_code);
|
---|
602 | } else {
|
---|
603 | dup_inode(rip); /* inode will be returned with put_inode */
|
---|
604 | }
|
---|
605 |
|
---|
606 | r = search_dir(dirp, file_name, (ino_t *) 0, DELETE);
|
---|
607 |
|
---|
608 | if (r == OK) {
|
---|
609 | rip->i_nlinks--; /* entry deleted from parent's dir */
|
---|
610 | rip->i_update |= CTIME;
|
---|
611 | rip->i_dirt = DIRTY;
|
---|
612 | }
|
---|
613 |
|
---|
614 | put_inode(rip);
|
---|
615 | return(r);
|
---|
616 | }
|
---|