1433d6423SLionel Sambuc /* Created (MFS based):
2433d6423SLionel Sambuc * February 2010 (Evgeniy Ivanov)
3433d6423SLionel Sambuc */
4433d6423SLionel Sambuc
5433d6423SLionel Sambuc #include "fs.h"
6433d6423SLionel Sambuc #include <sys/stat.h>
7433d6423SLionel Sambuc #include <string.h>
8433d6423SLionel Sambuc #include <minix/com.h>
9433d6423SLionel Sambuc #include "buf.h"
10433d6423SLionel Sambuc #include "inode.h"
11433d6423SLionel Sambuc #include "super.h"
12433d6423SLionel Sambuc #include <minix/vfsif.h>
13433d6423SLionel Sambuc #include <sys/param.h>
14433d6423SLionel Sambuc
15433d6423SLionel Sambuc #define SAME 1000
16433d6423SLionel Sambuc
17433d6423SLionel Sambuc static int freesp_inode(struct inode *rip, off_t st, off_t end);
18970d95ecSDavid van Moolenbroek static int remove_dir(struct inode *rldirp, struct inode *rip,
19970d95ecSDavid van Moolenbroek const char *dir_name);
20970d95ecSDavid van Moolenbroek static int unlink_file(struct inode *dirp, struct inode *rip,
21970d95ecSDavid van Moolenbroek const char *file_name);
22433d6423SLionel Sambuc static off_t nextblock(off_t pos, int blocksize);
23433d6423SLionel Sambuc static void zeroblock_half(struct inode *i, off_t p, int l);
24433d6423SLionel Sambuc static void zeroblock_range(struct inode *i, off_t p, off_t h);
25433d6423SLionel Sambuc
26433d6423SLionel Sambuc /* Args to zeroblock_half() */
27433d6423SLionel Sambuc #define FIRST_HALF 0
28433d6423SLionel Sambuc #define LAST_HALF 1
29433d6423SLionel Sambuc
30433d6423SLionel Sambuc
31433d6423SLionel Sambuc /*===========================================================================*
32433d6423SLionel Sambuc * fs_link *
33433d6423SLionel Sambuc *===========================================================================*/
fs_link(ino_t dir_nr,char * name,ino_t ino_nr)34970d95ecSDavid van Moolenbroek int fs_link(ino_t dir_nr, char *name, ino_t ino_nr)
35433d6423SLionel Sambuc {
36433d6423SLionel Sambuc /* Perform the link(name1, name2) system call. */
37433d6423SLionel Sambuc
38433d6423SLionel Sambuc struct inode *ip, *rip;
39433d6423SLionel Sambuc register int r;
40433d6423SLionel Sambuc struct inode *new_ip;
41433d6423SLionel Sambuc
42433d6423SLionel Sambuc /* Temporarily open the file. */
43970d95ecSDavid van Moolenbroek if( (rip = get_inode(fs_dev, ino_nr)) == NULL)
44433d6423SLionel Sambuc return(EINVAL);
45433d6423SLionel Sambuc
46433d6423SLionel Sambuc /* Check to see if the file has maximum number of links already. */
47433d6423SLionel Sambuc r = OK;
48433d6423SLionel Sambuc if (rip->i_links_count >= USHRT_MAX)
49433d6423SLionel Sambuc r = EMLINK;
50433d6423SLionel Sambuc if(rip->i_links_count >= LINK_MAX)
51433d6423SLionel Sambuc r = EMLINK;
52433d6423SLionel Sambuc
53970d95ecSDavid van Moolenbroek /* Linking to directories is too dangerous to allow. */
54433d6423SLionel Sambuc if(r == OK)
55970d95ecSDavid van Moolenbroek if( (rip->i_mode & I_TYPE) == I_DIRECTORY)
56433d6423SLionel Sambuc r = EPERM;
57433d6423SLionel Sambuc
58433d6423SLionel Sambuc /* If error with 'name', return the inode. */
59433d6423SLionel Sambuc if (r != OK) {
60433d6423SLionel Sambuc put_inode(rip);
61433d6423SLionel Sambuc return(r);
62433d6423SLionel Sambuc }
63433d6423SLionel Sambuc
64433d6423SLionel Sambuc /* Temporarily open the last dir */
65970d95ecSDavid van Moolenbroek if( (ip = get_inode(fs_dev, dir_nr)) == NULL) {
66433d6423SLionel Sambuc put_inode(rip);
67433d6423SLionel Sambuc return(EINVAL);
68433d6423SLionel Sambuc }
69433d6423SLionel Sambuc
70433d6423SLionel Sambuc if (ip->i_links_count == NO_LINK) { /* Dir does not actually exist */
71433d6423SLionel Sambuc put_inode(rip);
72433d6423SLionel Sambuc put_inode(ip);
73433d6423SLionel Sambuc return(ENOENT);
74433d6423SLionel Sambuc }
75433d6423SLionel Sambuc
76433d6423SLionel Sambuc /* If 'name2' exists in full (even if no space) set 'r' to error. */
77970d95ecSDavid van Moolenbroek if ((new_ip = advance(ip, name)) == NULL) {
78433d6423SLionel Sambuc r = err_code;
79433d6423SLionel Sambuc if(r == ENOENT)
80433d6423SLionel Sambuc r = OK;
81433d6423SLionel Sambuc } else {
82433d6423SLionel Sambuc put_inode(new_ip);
83433d6423SLionel Sambuc r = EEXIST;
84433d6423SLionel Sambuc }
85433d6423SLionel Sambuc
86433d6423SLionel Sambuc /* Try to link. */
87433d6423SLionel Sambuc if(r == OK)
88970d95ecSDavid van Moolenbroek r = search_dir(ip, name, &rip->i_num, ENTER, rip->i_mode & I_TYPE);
89433d6423SLionel Sambuc
90433d6423SLionel Sambuc /* If success, register the linking. */
91433d6423SLionel Sambuc if(r == OK) {
92433d6423SLionel Sambuc rip->i_links_count++;
93433d6423SLionel Sambuc rip->i_update |= CTIME;
94433d6423SLionel Sambuc rip->i_dirt = IN_DIRTY;
95433d6423SLionel Sambuc }
96433d6423SLionel Sambuc
97433d6423SLionel Sambuc /* Done. Release both inodes. */
98433d6423SLionel Sambuc put_inode(rip);
99433d6423SLionel Sambuc put_inode(ip);
100433d6423SLionel Sambuc return(r);
101433d6423SLionel Sambuc }
102433d6423SLionel Sambuc
103433d6423SLionel Sambuc
104433d6423SLionel Sambuc /*===========================================================================*
105433d6423SLionel Sambuc * fs_unlink *
106433d6423SLionel Sambuc *===========================================================================*/
fs_unlink(ino_t dir_nr,char * name,int call)107970d95ecSDavid van Moolenbroek int fs_unlink(ino_t dir_nr, char *name, int call)
108433d6423SLionel Sambuc {
109433d6423SLionel Sambuc /* Perform the unlink(name) or rmdir(name) system call. The code for these two
110970d95ecSDavid van Moolenbroek * is almost the same. They differ only in some condition testing.
111433d6423SLionel Sambuc */
112433d6423SLionel Sambuc register struct inode *rip;
113433d6423SLionel Sambuc struct inode *rldirp;
114433d6423SLionel Sambuc int r;
115433d6423SLionel Sambuc
116433d6423SLionel Sambuc /* Temporarily open the dir. */
117970d95ecSDavid van Moolenbroek if((rldirp = get_inode(fs_dev, dir_nr)) == NULL)
118433d6423SLionel Sambuc return(EINVAL);
119433d6423SLionel Sambuc
120433d6423SLionel Sambuc /* The last directory exists. Does the file also exist? */
121970d95ecSDavid van Moolenbroek rip = advance(rldirp, name);
122433d6423SLionel Sambuc r = err_code;
123433d6423SLionel Sambuc
124433d6423SLionel Sambuc /* If error, return inode. */
125433d6423SLionel Sambuc if(r != OK) {
126433d6423SLionel Sambuc put_inode(rldirp);
127433d6423SLionel Sambuc return(r);
128433d6423SLionel Sambuc }
129970d95ecSDavid van Moolenbroek if (rip->i_mountpoint) {
130970d95ecSDavid van Moolenbroek put_inode(rip);
131970d95ecSDavid van Moolenbroek put_inode(rldirp);
132970d95ecSDavid van Moolenbroek return(EBUSY);
133970d95ecSDavid van Moolenbroek }
134433d6423SLionel Sambuc
135433d6423SLionel Sambuc /* Now test if the call is allowed, separately for unlink() and rmdir(). */
136970d95ecSDavid van Moolenbroek if (call == FSC_UNLINK) {
137433d6423SLionel Sambuc if( (rip->i_mode & I_TYPE) == I_DIRECTORY) r = EPERM;
138433d6423SLionel Sambuc
139433d6423SLionel Sambuc /* Actually try to unlink the file; fails if parent is mode 0 etc. */
140970d95ecSDavid van Moolenbroek if (r == OK) r = unlink_file(rldirp, rip, name);
141433d6423SLionel Sambuc } else {
142970d95ecSDavid van Moolenbroek r = remove_dir(rldirp, rip, name); /* call is RMDIR */
143433d6423SLionel Sambuc }
144433d6423SLionel Sambuc
145433d6423SLionel Sambuc /* If unlink was possible, it has been done, otherwise it has not. */
146433d6423SLionel Sambuc put_inode(rip);
147433d6423SLionel Sambuc put_inode(rldirp);
148433d6423SLionel Sambuc return(r);
149433d6423SLionel Sambuc }
150433d6423SLionel Sambuc
151433d6423SLionel Sambuc
152433d6423SLionel Sambuc /*===========================================================================*
153433d6423SLionel Sambuc * fs_rdlink *
154433d6423SLionel Sambuc *===========================================================================*/
fs_rdlink(ino_t ino_nr,struct fsdriver_data * data,size_t bytes)155970d95ecSDavid van Moolenbroek ssize_t fs_rdlink(ino_t ino_nr, struct fsdriver_data *data, size_t bytes)
156433d6423SLionel Sambuc {
157433d6423SLionel Sambuc struct buf *bp = NULL; /* buffer containing link text */
158433d6423SLionel Sambuc char* link_text; /* either bp->b_data or rip->i_block */
159433d6423SLionel Sambuc register struct inode *rip; /* target inode */
160433d6423SLionel Sambuc register int r; /* return value */
161433d6423SLionel Sambuc
162433d6423SLionel Sambuc /* Temporarily open the file. */
163970d95ecSDavid van Moolenbroek if( (rip = get_inode(fs_dev, ino_nr)) == NULL)
164433d6423SLionel Sambuc return(EINVAL);
165433d6423SLionel Sambuc
166433d6423SLionel Sambuc if (rip->i_size >= MAX_FAST_SYMLINK_LENGTH) {
167433d6423SLionel Sambuc /* normal symlink */
168433d6423SLionel Sambuc if(!(bp = get_block_map(rip, 0))) {
169433d6423SLionel Sambuc r = EIO;
170433d6423SLionel Sambuc } else {
171433d6423SLionel Sambuc link_text = b_data(bp);
172433d6423SLionel Sambuc r = OK;
173433d6423SLionel Sambuc }
174433d6423SLionel Sambuc } else {
175433d6423SLionel Sambuc /* fast symlink, stored in inode */
176433d6423SLionel Sambuc link_text = (char*) rip->i_block;
177433d6423SLionel Sambuc r = OK;
178433d6423SLionel Sambuc }
179433d6423SLionel Sambuc if (r == OK) {
180433d6423SLionel Sambuc /* Passed all checks */
181970d95ecSDavid van Moolenbroek if (bytes > rip->i_size)
182970d95ecSDavid van Moolenbroek bytes = rip->i_size;
183970d95ecSDavid van Moolenbroek r = fsdriver_copyout(data, 0, link_text, bytes);
184*0314acfbSDavid van Moolenbroek put_block(bp);
185433d6423SLionel Sambuc if (r == OK)
186970d95ecSDavid van Moolenbroek r = bytes;
187433d6423SLionel Sambuc }
188433d6423SLionel Sambuc
189433d6423SLionel Sambuc put_inode(rip);
190433d6423SLionel Sambuc return(r);
191433d6423SLionel Sambuc }
192433d6423SLionel Sambuc
193433d6423SLionel Sambuc
194433d6423SLionel Sambuc /*===========================================================================*
195433d6423SLionel Sambuc * remove_dir *
196433d6423SLionel Sambuc *===========================================================================*/
remove_dir(rldirp,rip,dir_name)197433d6423SLionel Sambuc static int remove_dir(rldirp, rip, dir_name)
198433d6423SLionel Sambuc struct inode *rldirp; /* parent directory */
199433d6423SLionel Sambuc struct inode *rip; /* directory to be removed */
200970d95ecSDavid van Moolenbroek const char *dir_name; /* name of directory to be removed */
201433d6423SLionel Sambuc {
202433d6423SLionel Sambuc /* A directory file has to be removed. Five conditions have to met:
203433d6423SLionel Sambuc * - The file must be a directory
204433d6423SLionel Sambuc * - The directory must be empty (except for . and ..)
205433d6423SLionel Sambuc * - The final component of the path must not be . or ..
206433d6423SLionel Sambuc * - The directory must not be the root of a mounted file system (VFS)
207433d6423SLionel Sambuc * - The directory must not be anybody's root/working directory (VFS)
208433d6423SLionel Sambuc */
209433d6423SLionel Sambuc int r;
210433d6423SLionel Sambuc
211433d6423SLionel Sambuc /* search_dir checks that rip is a directory too. */
212970d95ecSDavid van Moolenbroek if ((r = search_dir(rip, "", NULL, IS_EMPTY, 0)) != OK)
213433d6423SLionel Sambuc return r;
214433d6423SLionel Sambuc
215433d6423SLionel Sambuc if (rip->i_num == ROOT_INODE) return(EBUSY); /* can't remove 'root' */
216433d6423SLionel Sambuc
217433d6423SLionel Sambuc /* Actually try to unlink the file; fails if parent is mode 0 etc. */
218433d6423SLionel Sambuc if ((r = unlink_file(rldirp, rip, dir_name)) != OK) return r;
219433d6423SLionel Sambuc
220433d6423SLionel Sambuc /* Unlink . and .. from the dir. The super user can link and unlink any dir,
221433d6423SLionel Sambuc * so don't make too many assumptions about them.
222433d6423SLionel Sambuc */
223970d95ecSDavid van Moolenbroek (void) unlink_file(rip, NULL, ".");
224970d95ecSDavid van Moolenbroek (void) unlink_file(rip, NULL, "..");
225433d6423SLionel Sambuc return(OK);
226433d6423SLionel Sambuc }
227433d6423SLionel Sambuc
228433d6423SLionel Sambuc
229433d6423SLionel Sambuc /*===========================================================================*
230433d6423SLionel Sambuc * unlink_file *
231433d6423SLionel Sambuc *===========================================================================*/
unlink_file(dirp,rip,file_name)232433d6423SLionel Sambuc static int unlink_file(dirp, rip, file_name)
233433d6423SLionel Sambuc struct inode *dirp; /* parent directory of file */
234433d6423SLionel Sambuc struct inode *rip; /* inode of file, may be NULL too. */
235970d95ecSDavid van Moolenbroek const char *file_name; /* name of file to be removed */
236433d6423SLionel Sambuc {
237433d6423SLionel Sambuc /* Unlink 'file_name'; rip must be the inode of 'file_name' or NULL. */
238433d6423SLionel Sambuc
239433d6423SLionel Sambuc ino_t numb; /* inode number */
240433d6423SLionel Sambuc int r;
241433d6423SLionel Sambuc
242433d6423SLionel Sambuc /* If rip is not NULL, it is used to get faster access to the inode. */
243433d6423SLionel Sambuc if (rip == NULL) {
244433d6423SLionel Sambuc /* Search for file in directory and try to get its inode. */
245970d95ecSDavid van Moolenbroek err_code = search_dir(dirp, file_name, &numb, LOOK_UP, 0);
246433d6423SLionel Sambuc if (err_code == OK) rip = get_inode(dirp->i_dev, (int) numb);
247433d6423SLionel Sambuc if (err_code != OK || rip == NULL) return(err_code);
248433d6423SLionel Sambuc } else {
249433d6423SLionel Sambuc dup_inode(rip); /* inode will be returned with put_inode */
250433d6423SLionel Sambuc }
251433d6423SLionel Sambuc
252970d95ecSDavid van Moolenbroek r = search_dir(dirp, file_name, NULL, DELETE, 0);
253433d6423SLionel Sambuc
254433d6423SLionel Sambuc if (r == OK) {
255433d6423SLionel Sambuc rip->i_links_count--; /* entry deleted from parent's dir */
256433d6423SLionel Sambuc rip->i_update |= CTIME;
257433d6423SLionel Sambuc rip->i_dirt = IN_DIRTY;
258433d6423SLionel Sambuc }
259433d6423SLionel Sambuc
260433d6423SLionel Sambuc put_inode(rip);
261433d6423SLionel Sambuc return(r);
262433d6423SLionel Sambuc }
263433d6423SLionel Sambuc
264433d6423SLionel Sambuc
265433d6423SLionel Sambuc /*===========================================================================*
266433d6423SLionel Sambuc * fs_rename *
267433d6423SLionel Sambuc *===========================================================================*/
fs_rename(ino_t old_dir_nr,char * old_name,ino_t new_dir_nr,char * new_name)268970d95ecSDavid van Moolenbroek int fs_rename(ino_t old_dir_nr, char *old_name, ino_t new_dir_nr,
269970d95ecSDavid van Moolenbroek char *new_name)
270433d6423SLionel Sambuc {
271433d6423SLionel Sambuc /* Perform the rename(name1, name2) system call. */
272433d6423SLionel Sambuc struct inode *old_dirp, *old_ip; /* ptrs to old dir, file inodes */
273433d6423SLionel Sambuc struct inode *new_dirp, *new_ip; /* ptrs to new dir, file inodes */
274433d6423SLionel Sambuc struct inode *new_superdirp, *next_new_superdirp;
275433d6423SLionel Sambuc int r = OK; /* error flag; initially no error */
276433d6423SLionel Sambuc int odir, ndir; /* TRUE iff {old|new} file is dir */
277433d6423SLionel Sambuc int same_pdir = 0; /* TRUE iff parent dirs are the same */
278433d6423SLionel Sambuc ino_t numb;
279433d6423SLionel Sambuc
280433d6423SLionel Sambuc /* Get old dir inode */
281970d95ecSDavid van Moolenbroek if( (old_dirp = get_inode(fs_dev, old_dir_nr)) == NULL)
282433d6423SLionel Sambuc return(err_code);
283433d6423SLionel Sambuc
284970d95ecSDavid van Moolenbroek old_ip = advance(old_dirp, old_name);
285433d6423SLionel Sambuc r = err_code;
286433d6423SLionel Sambuc
287970d95ecSDavid van Moolenbroek if (old_ip == NULL) {
288970d95ecSDavid van Moolenbroek put_inode(old_dirp);
289970d95ecSDavid van Moolenbroek return(r);
290970d95ecSDavid van Moolenbroek }
291970d95ecSDavid van Moolenbroek
292970d95ecSDavid van Moolenbroek if (old_ip->i_mountpoint) {
293433d6423SLionel Sambuc put_inode(old_ip);
294970d95ecSDavid van Moolenbroek put_inode(old_dirp);
295970d95ecSDavid van Moolenbroek return(EBUSY);
296433d6423SLionel Sambuc }
297433d6423SLionel Sambuc
298433d6423SLionel Sambuc /* Get new dir inode */
299970d95ecSDavid van Moolenbroek if ((new_dirp = get_inode(fs_dev, new_dir_nr)) == NULL){
300433d6423SLionel Sambuc put_inode(old_ip);
301433d6423SLionel Sambuc put_inode(old_dirp);
302433d6423SLionel Sambuc return(err_code);
303433d6423SLionel Sambuc } else {
304433d6423SLionel Sambuc if (new_dirp->i_links_count == NO_LINK) { /* Dir does not exist */
305433d6423SLionel Sambuc put_inode(old_ip);
306433d6423SLionel Sambuc put_inode(old_dirp);
307433d6423SLionel Sambuc put_inode(new_dirp);
308433d6423SLionel Sambuc return(ENOENT);
309433d6423SLionel Sambuc }
310433d6423SLionel Sambuc }
311433d6423SLionel Sambuc
312970d95ecSDavid van Moolenbroek new_ip = advance(new_dirp, new_name); /* not required to exist */
313433d6423SLionel Sambuc
314970d95ecSDavid van Moolenbroek /* If the node does exist, make sure it's not a mountpoint. */
315970d95ecSDavid van Moolenbroek if (new_ip != NULL && new_ip->i_mountpoint) {
316433d6423SLionel Sambuc put_inode(new_ip);
317433d6423SLionel Sambuc new_ip = NULL;
318433d6423SLionel Sambuc r = EBUSY;
319433d6423SLionel Sambuc }
320433d6423SLionel Sambuc
321433d6423SLionel Sambuc if(old_ip != NULL)
322433d6423SLionel Sambuc odir = ((old_ip->i_mode & I_TYPE) == I_DIRECTORY); /* TRUE iff dir */
323433d6423SLionel Sambuc else
324433d6423SLionel Sambuc odir = FALSE;
325433d6423SLionel Sambuc
326433d6423SLionel Sambuc /* If it is ok, check for a variety of possible errors. */
327433d6423SLionel Sambuc if(r == OK) {
328433d6423SLionel Sambuc same_pdir = (old_dirp == new_dirp);
329433d6423SLionel Sambuc
330433d6423SLionel Sambuc /* The old inode must not be a superdirectory of the new last dir. */
331433d6423SLionel Sambuc if (odir && !same_pdir) {
332433d6423SLionel Sambuc dup_inode(new_superdirp = new_dirp);
333433d6423SLionel Sambuc while (TRUE) { /* may hang in a file system loop */
334433d6423SLionel Sambuc if (new_superdirp == old_ip) {
335433d6423SLionel Sambuc put_inode(new_superdirp);
336433d6423SLionel Sambuc r = EINVAL;
337433d6423SLionel Sambuc break;
338433d6423SLionel Sambuc }
339970d95ecSDavid van Moolenbroek next_new_superdirp = advance(new_superdirp, "..");
340433d6423SLionel Sambuc
341433d6423SLionel Sambuc put_inode(new_superdirp);
342433d6423SLionel Sambuc if(next_new_superdirp == new_superdirp) {
343433d6423SLionel Sambuc put_inode(new_superdirp);
344433d6423SLionel Sambuc break;
345433d6423SLionel Sambuc }
346970d95ecSDavid van Moolenbroek if(next_new_superdirp->i_num == ROOT_INODE) {
347433d6423SLionel Sambuc /* imitate that we are back at the root,
348433d6423SLionel Sambuc * cross device checked already on VFS */
349433d6423SLionel Sambuc put_inode(next_new_superdirp);
350433d6423SLionel Sambuc err_code = OK;
351433d6423SLionel Sambuc break;
352433d6423SLionel Sambuc }
353433d6423SLionel Sambuc new_superdirp = next_new_superdirp;
354433d6423SLionel Sambuc if(new_superdirp == NULL) {
355433d6423SLionel Sambuc /* Missing ".." entry. Assume the worst. */
356433d6423SLionel Sambuc r = EINVAL;
357433d6423SLionel Sambuc break;
358433d6423SLionel Sambuc }
359433d6423SLionel Sambuc }
360433d6423SLionel Sambuc }
361433d6423SLionel Sambuc
362433d6423SLionel Sambuc /* Some tests apply only if the new path exists. */
363433d6423SLionel Sambuc if(new_ip == NULL) {
364433d6423SLionel Sambuc if(odir && (new_dirp->i_links_count >= SHRT_MAX ||
365433d6423SLionel Sambuc new_dirp->i_links_count >= LINK_MAX) &&
366433d6423SLionel Sambuc !same_pdir && r == OK) {
367433d6423SLionel Sambuc r = EMLINK;
368433d6423SLionel Sambuc }
369433d6423SLionel Sambuc } else {
370433d6423SLionel Sambuc if(old_ip == new_ip) r = SAME; /* old=new */
371433d6423SLionel Sambuc
372433d6423SLionel Sambuc ndir = ((new_ip->i_mode & I_TYPE) == I_DIRECTORY);/* dir ? */
373433d6423SLionel Sambuc if(odir == TRUE && ndir == FALSE) r = ENOTDIR;
374433d6423SLionel Sambuc if(odir == FALSE && ndir == TRUE) r = EISDIR;
375433d6423SLionel Sambuc }
376433d6423SLionel Sambuc }
377433d6423SLionel Sambuc
378433d6423SLionel Sambuc /* If a process has another root directory than the system root, we might
379433d6423SLionel Sambuc * "accidently" be moving it's working directory to a place where it's
380433d6423SLionel Sambuc * root directory isn't a super directory of it anymore. This can make
381433d6423SLionel Sambuc * the function chroot useless. If chroot will be used often we should
382433d6423SLionel Sambuc * probably check for it here. */
383433d6423SLionel Sambuc
384433d6423SLionel Sambuc /* The rename will probably work. Only two things can go wrong now:
385433d6423SLionel Sambuc * 1. being unable to remove the new file. (when new file already exists)
386433d6423SLionel Sambuc * 2. being unable to make the new directory entry. (new file doesn't exists)
387433d6423SLionel Sambuc * [directory has to grow by one block and cannot because the disk
388433d6423SLionel Sambuc * is completely full].
389433d6423SLionel Sambuc */
390433d6423SLionel Sambuc if(r == OK) {
391433d6423SLionel Sambuc if(new_ip != NULL) {
392433d6423SLionel Sambuc /* There is already an entry for 'new'. Try to remove it. */
393433d6423SLionel Sambuc if(odir)
394433d6423SLionel Sambuc r = remove_dir(new_dirp, new_ip, new_name);
395433d6423SLionel Sambuc else
396433d6423SLionel Sambuc r = unlink_file(new_dirp, new_ip, new_name);
397433d6423SLionel Sambuc }
398433d6423SLionel Sambuc /* if r is OK, the rename will succeed, while there is now an
399433d6423SLionel Sambuc * unused entry in the new parent directory. */
400433d6423SLionel Sambuc }
401433d6423SLionel Sambuc
402433d6423SLionel Sambuc if(r == OK) {
403433d6423SLionel Sambuc /* If the new name will be in the same parent directory as the old
404433d6423SLionel Sambuc * one, first remove the old name to free an entry for the new name,
405433d6423SLionel Sambuc * otherwise first try to create the new name entry to make sure
406433d6423SLionel Sambuc * the rename will succeed.
407433d6423SLionel Sambuc */
408433d6423SLionel Sambuc numb = old_ip->i_num; /* inode number of old file */
409433d6423SLionel Sambuc
410433d6423SLionel Sambuc if(same_pdir) {
411970d95ecSDavid van Moolenbroek r = search_dir(old_dirp,old_name, NULL, DELETE, 0);
412433d6423SLionel Sambuc /* shouldn't go wrong. */
413433d6423SLionel Sambuc if(r == OK)
414970d95ecSDavid van Moolenbroek (void) search_dir(old_dirp, new_name, &numb, ENTER,
415433d6423SLionel Sambuc old_ip->i_mode & I_TYPE);
416433d6423SLionel Sambuc } else {
417970d95ecSDavid van Moolenbroek r = search_dir(new_dirp, new_name, &numb, ENTER,
418433d6423SLionel Sambuc old_ip->i_mode & I_TYPE);
419433d6423SLionel Sambuc if(r == OK) {
420433d6423SLionel Sambuc (void) search_dir(old_dirp, old_name, NULL,
421970d95ecSDavid van Moolenbroek DELETE, 0);
422433d6423SLionel Sambuc }
423433d6423SLionel Sambuc }
424433d6423SLionel Sambuc }
425433d6423SLionel Sambuc /* If r is OK, the ctime and mtime of old_dirp and new_dirp have been marked
426433d6423SLionel Sambuc * for update in search_dir. */
427433d6423SLionel Sambuc
428433d6423SLionel Sambuc if(r == OK && odir && !same_pdir) {
429433d6423SLionel Sambuc /* Update the .. entry in the directory (still points to old_dirp).*/
430433d6423SLionel Sambuc numb = new_dirp->i_num;
431970d95ecSDavid van Moolenbroek (void) unlink_file(old_ip, NULL, "..");
432970d95ecSDavid van Moolenbroek if(search_dir(old_ip, "..", &numb, ENTER, I_DIRECTORY) == OK) {
433433d6423SLionel Sambuc /* New link created. */
434433d6423SLionel Sambuc new_dirp->i_links_count++;
435433d6423SLionel Sambuc new_dirp->i_dirt = IN_DIRTY;
436433d6423SLionel Sambuc }
437433d6423SLionel Sambuc }
438433d6423SLionel Sambuc
439433d6423SLionel Sambuc /* Release the inodes. */
440433d6423SLionel Sambuc put_inode(old_dirp);
441433d6423SLionel Sambuc put_inode(old_ip);
442433d6423SLionel Sambuc put_inode(new_dirp);
443433d6423SLionel Sambuc put_inode(new_ip);
444433d6423SLionel Sambuc return(r == SAME ? OK : r);
445433d6423SLionel Sambuc }
446433d6423SLionel Sambuc
447433d6423SLionel Sambuc
448433d6423SLionel Sambuc /*===========================================================================*
449970d95ecSDavid van Moolenbroek * fs_trunc *
450433d6423SLionel Sambuc *===========================================================================*/
fs_trunc(ino_t ino_nr,off_t start,off_t end)451970d95ecSDavid van Moolenbroek int fs_trunc(ino_t ino_nr, off_t start, off_t end)
452433d6423SLionel Sambuc {
453433d6423SLionel Sambuc struct inode *rip;
454433d6423SLionel Sambuc int r;
455433d6423SLionel Sambuc
456970d95ecSDavid van Moolenbroek if( (rip = find_inode(fs_dev, ino_nr)) == NULL)
457433d6423SLionel Sambuc return(EINVAL);
458433d6423SLionel Sambuc
459433d6423SLionel Sambuc if (end == 0)
460433d6423SLionel Sambuc r = truncate_inode(rip, start);
461433d6423SLionel Sambuc else
462433d6423SLionel Sambuc r = freesp_inode(rip, start, end);
463433d6423SLionel Sambuc
464433d6423SLionel Sambuc return(r);
465433d6423SLionel Sambuc }
466433d6423SLionel Sambuc
467433d6423SLionel Sambuc
468433d6423SLionel Sambuc /*===========================================================================*
469433d6423SLionel Sambuc * truncate_inode *
470433d6423SLionel Sambuc *===========================================================================*/
truncate_inode(rip,newsize)471433d6423SLionel Sambuc int truncate_inode(rip, newsize)
472433d6423SLionel Sambuc register struct inode *rip; /* pointer to inode to be truncated */
473433d6423SLionel Sambuc off_t newsize; /* inode must become this size */
474433d6423SLionel Sambuc {
475433d6423SLionel Sambuc /* Set inode to a certain size, freeing any blocks no longer referenced
476433d6423SLionel Sambuc * and updating the size in the inode. If the inode is extended, the
477433d6423SLionel Sambuc * extra space is a hole that reads as zeroes.
478433d6423SLionel Sambuc *
479433d6423SLionel Sambuc * Nothing special has to happen to file pointers if inode is opened in
480433d6423SLionel Sambuc * O_APPEND mode, as this is different per fd and is checked when
481433d6423SLionel Sambuc * writing is done.
482433d6423SLionel Sambuc */
483433d6423SLionel Sambuc int r;
484433d6423SLionel Sambuc mode_t file_type;
485433d6423SLionel Sambuc
486433d6423SLionel Sambuc discard_preallocated_blocks(rip);
487433d6423SLionel Sambuc
488433d6423SLionel Sambuc file_type = rip->i_mode & I_TYPE; /* check to see if file is special */
489433d6423SLionel Sambuc if (file_type == I_CHAR_SPECIAL || file_type == I_BLOCK_SPECIAL)
490433d6423SLionel Sambuc return(EINVAL);
491433d6423SLionel Sambuc if (newsize > rip->i_sp->s_max_size) /* don't let inode grow too big */
492433d6423SLionel Sambuc return(EFBIG);
493433d6423SLionel Sambuc
494433d6423SLionel Sambuc /* Free the actual space if truncating. */
495433d6423SLionel Sambuc if (newsize < rip->i_size) {
496433d6423SLionel Sambuc if ((r = freesp_inode(rip, newsize, rip->i_size)) != OK)
497433d6423SLionel Sambuc return(r);
498433d6423SLionel Sambuc }
499433d6423SLionel Sambuc
500433d6423SLionel Sambuc /* Clear the rest of the last block if expanding. */
501433d6423SLionel Sambuc if (newsize > rip->i_size) zeroblock_half(rip, rip->i_size, LAST_HALF);
502433d6423SLionel Sambuc
503433d6423SLionel Sambuc /* Next correct the inode size. */
504433d6423SLionel Sambuc rip->i_size = newsize;
505433d6423SLionel Sambuc rip->i_update |= CTIME | MTIME;
506433d6423SLionel Sambuc rip->i_dirt = IN_DIRTY;
507433d6423SLionel Sambuc
508433d6423SLionel Sambuc return(OK);
509433d6423SLionel Sambuc }
510433d6423SLionel Sambuc
511433d6423SLionel Sambuc
512433d6423SLionel Sambuc /*===========================================================================*
513433d6423SLionel Sambuc * freesp_inode *
514433d6423SLionel Sambuc *===========================================================================*/
freesp_inode(rip,start,end)515433d6423SLionel Sambuc static int freesp_inode(rip, start, end)
516433d6423SLionel Sambuc register struct inode *rip; /* pointer to inode to be partly freed */
517433d6423SLionel Sambuc off_t start, end; /* range of bytes to free (end uninclusive) */
518433d6423SLionel Sambuc {
519433d6423SLionel Sambuc /* Cut an arbitrary hole in an inode. The caller is responsible for checking
520433d6423SLionel Sambuc * the reasonableness of the inode type of rip. The reason is this is that
521433d6423SLionel Sambuc * this function can be called for different reasons, for which different
522433d6423SLionel Sambuc * sets of inode types are reasonable. Adjusting the final size of the inode
523433d6423SLionel Sambuc * is to be done by the caller too, if wished.
524433d6423SLionel Sambuc *
525433d6423SLionel Sambuc * Consumers of this function currently are truncate_inode() (used to
526433d6423SLionel Sambuc * free indirect and data blocks for any type of inode, but also to
527433d6423SLionel Sambuc * implement the ftruncate() and truncate() system calls) and the F_FREESP
528433d6423SLionel Sambuc * fcntl().
529433d6423SLionel Sambuc */
530433d6423SLionel Sambuc off_t p, e;
531433d6423SLionel Sambuc int r;
532433d6423SLionel Sambuc unsigned short block_size = rip->i_sp->s_block_size;
533433d6423SLionel Sambuc int zero_last, zero_first;
534433d6423SLionel Sambuc
535433d6423SLionel Sambuc discard_preallocated_blocks(rip);
536433d6423SLionel Sambuc
537433d6423SLionel Sambuc if (rip->i_blocks == 0) {
538433d6423SLionel Sambuc /* Either hole or symlink. Freeing fast symlink using
539433d6423SLionel Sambuc * write_map() causes segfaults since it doesn't use any
540433d6423SLionel Sambuc * blocks, but uses i_block[] to store target.
541433d6423SLionel Sambuc */
542433d6423SLionel Sambuc return(OK);
543433d6423SLionel Sambuc }
544433d6423SLionel Sambuc
545433d6423SLionel Sambuc if(end > rip->i_size) /* freeing beyond end makes no sense */
546433d6423SLionel Sambuc end = rip->i_size;
547433d6423SLionel Sambuc if(end <= start) /* end is uninclusive, so start<end */
548433d6423SLionel Sambuc return(EINVAL);
549433d6423SLionel Sambuc
550433d6423SLionel Sambuc /* If freeing doesn't cross a block boundary, then we may only zero
551433d6423SLionel Sambuc * a range of the block.
552433d6423SLionel Sambuc */
553433d6423SLionel Sambuc zero_last = start % block_size;
554433d6423SLionel Sambuc zero_first = end % block_size && end < rip->i_size;
555433d6423SLionel Sambuc if (start/block_size == (end-1)/block_size && (zero_last || zero_first)) {
556433d6423SLionel Sambuc zeroblock_range(rip, start, end-start);
557433d6423SLionel Sambuc } else {
558433d6423SLionel Sambuc /* First zero unused part of partly used blocks. */
559433d6423SLionel Sambuc if (zero_last)
560433d6423SLionel Sambuc zeroblock_half(rip, start, LAST_HALF);
561433d6423SLionel Sambuc if (zero_first)
562433d6423SLionel Sambuc zeroblock_half(rip, end, FIRST_HALF);
563433d6423SLionel Sambuc
564433d6423SLionel Sambuc /* Now completely free the completely unused blocks.
565433d6423SLionel Sambuc * write_map() will free unused indirect
566433d6423SLionel Sambuc * blocks too. Converting the range to block numbers avoids
567433d6423SLionel Sambuc * overflow on p when doing e.g. 'p += block_size'.
568433d6423SLionel Sambuc */
569433d6423SLionel Sambuc e = end / block_size;
570433d6423SLionel Sambuc if (end == rip->i_size && (end % block_size))
571433d6423SLionel Sambuc e++;
572433d6423SLionel Sambuc for (p = nextblock(start, block_size)/block_size; p < e; p++) {
573433d6423SLionel Sambuc if ((r = write_map(rip, p*block_size, NO_BLOCK, WMAP_FREE)) != OK)
574433d6423SLionel Sambuc return(r);
575433d6423SLionel Sambuc }
576433d6423SLionel Sambuc }
577433d6423SLionel Sambuc
578433d6423SLionel Sambuc rip->i_update |= CTIME | MTIME;
579433d6423SLionel Sambuc rip->i_dirt = IN_DIRTY;
580433d6423SLionel Sambuc
581433d6423SLionel Sambuc return(OK);
582433d6423SLionel Sambuc }
583433d6423SLionel Sambuc
584433d6423SLionel Sambuc
585433d6423SLionel Sambuc /*===========================================================================*
586433d6423SLionel Sambuc * nextblock *
587433d6423SLionel Sambuc *===========================================================================*/
nextblock(pos,block_size)588433d6423SLionel Sambuc static off_t nextblock(pos, block_size)
589433d6423SLionel Sambuc off_t pos;
590433d6423SLionel Sambuc unsigned short block_size;
591433d6423SLionel Sambuc {
592433d6423SLionel Sambuc /* Return the first position in the next block after position 'pos'
593433d6423SLionel Sambuc * (unless this is the first position in the current block).
594433d6423SLionel Sambuc * This can be done in one expression, but that can overflow pos.
595433d6423SLionel Sambuc */
596433d6423SLionel Sambuc off_t p;
597433d6423SLionel Sambuc p = (pos / block_size) * block_size;
598433d6423SLionel Sambuc if (pos % block_size) p += block_size; /* Round up. */
599433d6423SLionel Sambuc return(p);
600433d6423SLionel Sambuc }
601433d6423SLionel Sambuc
602433d6423SLionel Sambuc
603433d6423SLionel Sambuc /*===========================================================================*
604433d6423SLionel Sambuc * zeroblock_half *
605433d6423SLionel Sambuc *===========================================================================*/
zeroblock_half(rip,pos,half)606433d6423SLionel Sambuc static void zeroblock_half(rip, pos, half)
607433d6423SLionel Sambuc struct inode *rip;
608433d6423SLionel Sambuc off_t pos;
609433d6423SLionel Sambuc int half;
610433d6423SLionel Sambuc {
611433d6423SLionel Sambuc /* Zero the upper or lower 'half' of a block that holds position 'pos'.
612433d6423SLionel Sambuc * half can be FIRST_HALF or LAST_HALF.
613433d6423SLionel Sambuc *
614433d6423SLionel Sambuc * FIRST_HALF: 0..pos-1 will be zeroed
615433d6423SLionel Sambuc * LAST_HALF: pos..blocksize-1 will be zeroed
616433d6423SLionel Sambuc */
617433d6423SLionel Sambuc off_t offset, len;
618433d6423SLionel Sambuc
619433d6423SLionel Sambuc /* Offset of zeroing boundary. */
620433d6423SLionel Sambuc offset = pos % rip->i_sp->s_block_size;
621433d6423SLionel Sambuc
622433d6423SLionel Sambuc if(half == LAST_HALF) {
623433d6423SLionel Sambuc len = rip->i_sp->s_block_size - offset;
624433d6423SLionel Sambuc } else {
625433d6423SLionel Sambuc len = offset;
626433d6423SLionel Sambuc pos -= offset;
627433d6423SLionel Sambuc }
628433d6423SLionel Sambuc
629433d6423SLionel Sambuc zeroblock_range(rip, pos, len);
630433d6423SLionel Sambuc }
631433d6423SLionel Sambuc
632433d6423SLionel Sambuc
633433d6423SLionel Sambuc /*===========================================================================*
634433d6423SLionel Sambuc * zeroblock_range *
635433d6423SLionel Sambuc *===========================================================================*/
zeroblock_range(rip,pos,len)636433d6423SLionel Sambuc static void zeroblock_range(rip, pos, len)
637433d6423SLionel Sambuc struct inode *rip;
638433d6423SLionel Sambuc off_t pos;
639433d6423SLionel Sambuc off_t len;
640433d6423SLionel Sambuc {
641433d6423SLionel Sambuc /* Zero a range in a block.
642433d6423SLionel Sambuc * This function is used to zero a segment of a block.
643433d6423SLionel Sambuc */
644433d6423SLionel Sambuc struct buf *bp;
645433d6423SLionel Sambuc off_t offset;
646433d6423SLionel Sambuc
647433d6423SLionel Sambuc if (!len) return; /* no zeroing to be done. */
648433d6423SLionel Sambuc if (!(bp = get_block_map(rip, rounddown(pos, rip->i_sp->s_block_size))))
649970d95ecSDavid van Moolenbroek return; /* skip holes */
650433d6423SLionel Sambuc offset = pos % rip->i_sp->s_block_size;
651433d6423SLionel Sambuc if (offset + len > rip->i_sp->s_block_size)
652433d6423SLionel Sambuc panic("zeroblock_range: len too long: %lld", len);
653433d6423SLionel Sambuc memset(b_data(bp) + offset, 0, len);
654433d6423SLionel Sambuc lmfs_markdirty(bp);
655*0314acfbSDavid van Moolenbroek put_block(bp);
656433d6423SLionel Sambuc }
657