xref: /minix3/minix/fs/mfs/path.c (revision 4472b590c74fed661146a925d8dff2ed1b04256e)
1433d6423SLionel Sambuc /* This file contains the procedures that look up path names in the directory
2433d6423SLionel Sambuc  * system and determine the inode number that goes with a given path name.
3433d6423SLionel Sambuc  */
4433d6423SLionel Sambuc 
5433d6423SLionel Sambuc #include "fs.h"
6ccaeedb2SDavid van Moolenbroek #include <assert.h>
7433d6423SLionel Sambuc #include <string.h>
8433d6423SLionel Sambuc #include "buf.h"
9433d6423SLionel Sambuc #include "inode.h"
10433d6423SLionel Sambuc #include "super.h"
11433d6423SLionel Sambuc 
12433d6423SLionel Sambuc 
13433d6423SLionel Sambuc /*===========================================================================*
14433d6423SLionel Sambuc  *                             fs_lookup				     *
15433d6423SLionel Sambuc  *===========================================================================*/
fs_lookup(ino_t dir_nr,char * name,struct fsdriver_node * node,int * is_mountpt)16ccaeedb2SDavid van Moolenbroek int fs_lookup(ino_t dir_nr, char *name, struct fsdriver_node *node,
17ccaeedb2SDavid van Moolenbroek 	int *is_mountpt)
18433d6423SLionel Sambuc {
19ccaeedb2SDavid van Moolenbroek   struct inode *dirp, *rip;
20433d6423SLionel Sambuc 
21ccaeedb2SDavid van Moolenbroek   /* Find the starting inode. */
22ccaeedb2SDavid van Moolenbroek   if ((dirp = find_inode(fs_dev, dir_nr)) == NULL)
23ccaeedb2SDavid van Moolenbroek 	return EINVAL;
24433d6423SLionel Sambuc 
25ccaeedb2SDavid van Moolenbroek   /* Look up the directory entry. */
26ccaeedb2SDavid van Moolenbroek   if ((rip = advance(dirp, name)) == NULL)
27ccaeedb2SDavid van Moolenbroek 	return err_code;
28433d6423SLionel Sambuc 
29ccaeedb2SDavid van Moolenbroek   /* On success, leave the resulting inode open and return its details. */
30ccaeedb2SDavid van Moolenbroek   node->fn_ino_nr = rip->i_num;
31ccaeedb2SDavid van Moolenbroek   node->fn_mode = rip->i_mode;
32ccaeedb2SDavid van Moolenbroek   node->fn_size = rip->i_size;
33ccaeedb2SDavid van Moolenbroek   node->fn_uid = rip->i_uid;
34ccaeedb2SDavid van Moolenbroek   node->fn_gid = rip->i_gid;
35433d6423SLionel Sambuc   /* This is only valid for block and character specials. But it doesn't
36433d6423SLionel Sambuc    * cause any harm to always set the device field. */
37ccaeedb2SDavid van Moolenbroek   node->fn_dev = (dev_t) rip->i_zone[0];
38433d6423SLionel Sambuc 
39ccaeedb2SDavid van Moolenbroek   *is_mountpt = rip->i_mountpoint;
40433d6423SLionel Sambuc 
41ccaeedb2SDavid van Moolenbroek   return OK;
42433d6423SLionel Sambuc }
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc 
45433d6423SLionel Sambuc /*===========================================================================*
46433d6423SLionel Sambuc  *				advance					     *
47433d6423SLionel Sambuc  *===========================================================================*/
advance(dirp,string)48ccaeedb2SDavid van Moolenbroek struct inode *advance(dirp, string)
49433d6423SLionel Sambuc struct inode *dirp;		/* inode for directory to be searched */
50ccaeedb2SDavid van Moolenbroek const char *string;		/* component name to look for */
51433d6423SLionel Sambuc {
52433d6423SLionel Sambuc /* Given a directory and a component of a path, look up the component in
53433d6423SLionel Sambuc  * the directory, find the inode, open it, and return a pointer to its inode
54433d6423SLionel Sambuc  * slot.
55433d6423SLionel Sambuc  */
56433d6423SLionel Sambuc   ino_t numb;
57433d6423SLionel Sambuc   struct inode *rip;
58433d6423SLionel Sambuc 
59ccaeedb2SDavid van Moolenbroek   assert(dirp != NULL);
60ccaeedb2SDavid van Moolenbroek 
61433d6423SLionel Sambuc   /* If 'string' is empty, return an error. */
62433d6423SLionel Sambuc   if (string[0] == '\0') {
63433d6423SLionel Sambuc   	err_code = ENOENT;
64433d6423SLionel Sambuc 	return(NULL);
65433d6423SLionel Sambuc   }
66433d6423SLionel Sambuc 
67ccaeedb2SDavid van Moolenbroek   /* If dir has been removed return ENOENT. */
68ccaeedb2SDavid van Moolenbroek   if (dirp->i_nlinks == NO_LINK) {
69ccaeedb2SDavid van Moolenbroek 	err_code = ENOENT;
70ccaeedb2SDavid van Moolenbroek 	return(NULL);
71ccaeedb2SDavid van Moolenbroek   }
72433d6423SLionel Sambuc 
73433d6423SLionel Sambuc   /* If 'string' is not present in the directory, signal error. */
74ccaeedb2SDavid van Moolenbroek   if ( (err_code = search_dir(dirp, string, &numb, LOOK_UP)) != OK) {
75433d6423SLionel Sambuc 	return(NULL);
76433d6423SLionel Sambuc   }
77433d6423SLionel Sambuc 
78433d6423SLionel Sambuc   /* The component has been found in the directory.  Get inode. */
79433d6423SLionel Sambuc   if ( (rip = get_inode(dirp->i_dev, (int) numb)) == NULL)  {
80ccaeedb2SDavid van Moolenbroek 	assert(err_code != OK);
81433d6423SLionel Sambuc 	return(NULL);
82433d6423SLionel Sambuc   }
83433d6423SLionel Sambuc 
84ccaeedb2SDavid van Moolenbroek   assert(err_code == OK);
85433d6423SLionel Sambuc   return(rip);
86433d6423SLionel Sambuc }
87433d6423SLionel Sambuc 
88433d6423SLionel Sambuc 
89433d6423SLionel Sambuc /*===========================================================================*
90433d6423SLionel Sambuc  *				search_dir				     *
91433d6423SLionel Sambuc  *===========================================================================*/
search_dir(ldir_ptr,string,numb,flag)92ccaeedb2SDavid van Moolenbroek int search_dir(ldir_ptr, string, numb, flag)
93433d6423SLionel Sambuc register struct inode *ldir_ptr; /* ptr to inode for dir to search */
94ccaeedb2SDavid van Moolenbroek const char *string;		 /* component to search for */
95433d6423SLionel Sambuc ino_t *numb;			 /* pointer to inode number */
96433d6423SLionel Sambuc int flag;			 /* LOOK_UP, ENTER, DELETE or IS_EMPTY */
97433d6423SLionel Sambuc {
98433d6423SLionel Sambuc /* This function searches the directory whose inode is pointed to by 'ldip':
99433d6423SLionel Sambuc  * if (flag == ENTER)  enter 'string' in the directory with inode # '*numb';
100433d6423SLionel Sambuc  * if (flag == DELETE) delete 'string' from the directory;
101433d6423SLionel Sambuc  * if (flag == LOOK_UP) search for 'string' and return inode # in 'numb';
102433d6423SLionel Sambuc  * if (flag == IS_EMPTY) return OK if only . and .. in dir else ENOTEMPTY;
103433d6423SLionel Sambuc  *
104ccaeedb2SDavid van Moolenbroek  * This function, and this function alone, implements name truncation,
105ccaeedb2SDavid van Moolenbroek  * by simply considering only the first MFS_NAME_MAX bytes from 'string'.
106433d6423SLionel Sambuc  */
107433d6423SLionel Sambuc   register struct direct *dp = NULL;
108433d6423SLionel Sambuc   register struct buf *bp = NULL;
109433d6423SLionel Sambuc   int i, r, e_hit, t, match;
110433d6423SLionel Sambuc   off_t pos;
111433d6423SLionel Sambuc   unsigned new_slots, old_slots;
112433d6423SLionel Sambuc   struct super_block *sp;
113433d6423SLionel Sambuc   int extended = 0;
114433d6423SLionel Sambuc 
115433d6423SLionel Sambuc   /* If 'ldir_ptr' is not a pointer to a dir inode, error. */
116433d6423SLionel Sambuc   if ( (ldir_ptr->i_mode & I_TYPE) != I_DIRECTORY)  {
117433d6423SLionel Sambuc 	return(ENOTDIR);
118433d6423SLionel Sambuc    }
119433d6423SLionel Sambuc 
120433d6423SLionel Sambuc   if((flag == DELETE || flag == ENTER) && ldir_ptr->i_sp->s_rd_only)
121433d6423SLionel Sambuc 	return EROFS;
122433d6423SLionel Sambuc 
123433d6423SLionel Sambuc   /* Step through the directory one block at a time. */
124433d6423SLionel Sambuc   old_slots = (unsigned) (ldir_ptr->i_size/DIR_ENTRY_SIZE);
125433d6423SLionel Sambuc   new_slots = 0;
126433d6423SLionel Sambuc   e_hit = FALSE;
127433d6423SLionel Sambuc   match = 0;			/* set when a string match occurs */
128433d6423SLionel Sambuc 
129433d6423SLionel Sambuc   pos = 0;
130433d6423SLionel Sambuc   if (flag == ENTER && ldir_ptr->i_last_dpos < ldir_ptr->i_size) {
131433d6423SLionel Sambuc 	pos = ldir_ptr->i_last_dpos;
132433d6423SLionel Sambuc 	new_slots = (unsigned) (pos/DIR_ENTRY_SIZE);
133433d6423SLionel Sambuc   }
134433d6423SLionel Sambuc 
135433d6423SLionel Sambuc   for (; pos < ldir_ptr->i_size; pos += ldir_ptr->i_sp->s_block_size) {
136433d6423SLionel Sambuc 	assert(ldir_ptr->i_dev != NO_DEV);
137433d6423SLionel Sambuc 
138433d6423SLionel Sambuc 	/* Since directories don't have holes, 'b' cannot be NO_BLOCK. */
139433d6423SLionel Sambuc 	bp = get_block_map(ldir_ptr, pos);
140433d6423SLionel Sambuc 
141433d6423SLionel Sambuc 	assert(ldir_ptr->i_dev != NO_DEV);
142433d6423SLionel Sambuc 	assert(bp != NULL);
143433d6423SLionel Sambuc 
144433d6423SLionel Sambuc 	/* Search a directory block. */
145433d6423SLionel Sambuc 	for (dp = &b_dir(bp)[0];
146433d6423SLionel Sambuc 		dp < &b_dir(bp)[NR_DIR_ENTRIES(ldir_ptr->i_sp->s_block_size)];
147433d6423SLionel Sambuc 		dp++) {
148433d6423SLionel Sambuc 		if (++new_slots > old_slots) { /* not found, but room left */
149433d6423SLionel Sambuc 			if (flag == ENTER) e_hit = TRUE;
150433d6423SLionel Sambuc 			break;
151433d6423SLionel Sambuc 		}
152433d6423SLionel Sambuc 
153433d6423SLionel Sambuc 		/* Match occurs if string found. */
154433d6423SLionel Sambuc 		if (flag != ENTER && dp->mfs_d_ino != NO_ENTRY) {
155433d6423SLionel Sambuc 			if (flag == IS_EMPTY) {
156433d6423SLionel Sambuc 				/* If this test succeeds, dir is not empty. */
157433d6423SLionel Sambuc 				if (strcmp(dp->mfs_d_name, "." ) != 0 &&
158ccaeedb2SDavid van Moolenbroek 				    strcmp(dp->mfs_d_name, "..") != 0)
159ccaeedb2SDavid van Moolenbroek 					match = 1;
160433d6423SLionel Sambuc 			} else {
161433d6423SLionel Sambuc 				if (strncmp(dp->mfs_d_name, string,
162433d6423SLionel Sambuc 					sizeof(dp->mfs_d_name)) == 0){
163433d6423SLionel Sambuc 					match = 1;
164433d6423SLionel Sambuc 				}
165433d6423SLionel Sambuc 			}
166433d6423SLionel Sambuc 		}
167433d6423SLionel Sambuc 
168433d6423SLionel Sambuc 		if (match) {
169433d6423SLionel Sambuc 			/* LOOK_UP or DELETE found what it wanted. */
170433d6423SLionel Sambuc 			r = OK;
171433d6423SLionel Sambuc 			if (flag == IS_EMPTY) r = ENOTEMPTY;
172433d6423SLionel Sambuc 			else if (flag == DELETE) {
173433d6423SLionel Sambuc 				/* Save d_ino for recovery. */
174433d6423SLionel Sambuc 				t = MFS_NAME_MAX - sizeof(ino_t);
175433d6423SLionel Sambuc 				*((ino_t *) &dp->mfs_d_name[t]) = dp->mfs_d_ino;
176433d6423SLionel Sambuc 				dp->mfs_d_ino = NO_ENTRY; /* erase entry */
177433d6423SLionel Sambuc 				MARKDIRTY(bp);
178433d6423SLionel Sambuc 				ldir_ptr->i_update |= CTIME | MTIME;
179433d6423SLionel Sambuc 				IN_MARKDIRTY(ldir_ptr);
180433d6423SLionel Sambuc 				if (pos < ldir_ptr->i_last_dpos)
181433d6423SLionel Sambuc 					ldir_ptr->i_last_dpos = pos;
182433d6423SLionel Sambuc 			} else {
183433d6423SLionel Sambuc 				sp = ldir_ptr->i_sp;	/* 'flag' is LOOK_UP */
184433d6423SLionel Sambuc 				*numb = (ino_t) conv4(sp->s_native,
185433d6423SLionel Sambuc 						      (int) dp->mfs_d_ino);
186433d6423SLionel Sambuc 			}
187*0314acfbSDavid van Moolenbroek 			put_block(bp);
188433d6423SLionel Sambuc 			return(r);
189433d6423SLionel Sambuc 		}
190433d6423SLionel Sambuc 
191433d6423SLionel Sambuc 		/* Check for free slot for the benefit of ENTER. */
192433d6423SLionel Sambuc 		if (flag == ENTER && dp->mfs_d_ino == 0) {
193433d6423SLionel Sambuc 			e_hit = TRUE;	/* we found a free slot */
194433d6423SLionel Sambuc 			break;
195433d6423SLionel Sambuc 		}
196433d6423SLionel Sambuc 	}
197433d6423SLionel Sambuc 
198433d6423SLionel Sambuc 	/* The whole block has been searched or ENTER has a free slot. */
199433d6423SLionel Sambuc 	if (e_hit) break;	/* e_hit set if ENTER can be performed now */
200*0314acfbSDavid van Moolenbroek 	put_block(bp);		/* otherwise, continue searching dir */
201433d6423SLionel Sambuc   }
202433d6423SLionel Sambuc 
203433d6423SLionel Sambuc   /* The whole directory has now been searched. */
204433d6423SLionel Sambuc   if (flag != ENTER) {
205433d6423SLionel Sambuc   	return(flag == IS_EMPTY ? OK : ENOENT);
206433d6423SLionel Sambuc   }
207433d6423SLionel Sambuc 
208433d6423SLionel Sambuc   /* When ENTER next time, start searching for free slot from
209433d6423SLionel Sambuc    * i_last_dpos. It gives some performance improvement (3-5%).
210433d6423SLionel Sambuc    */
211433d6423SLionel Sambuc   ldir_ptr->i_last_dpos = pos;
212433d6423SLionel Sambuc 
213433d6423SLionel Sambuc   /* This call is for ENTER.  If no free slot has been found so far, try to
214433d6423SLionel Sambuc    * extend directory.
215433d6423SLionel Sambuc    */
216433d6423SLionel Sambuc   if (e_hit == FALSE) { /* directory is full and no room left in last block */
217433d6423SLionel Sambuc 	new_slots++;		/* increase directory size by 1 entry */
218433d6423SLionel Sambuc 	if (new_slots == 0) return(EFBIG); /* dir size limited by slot count */
219433d6423SLionel Sambuc 	if ( (bp = new_block(ldir_ptr, ldir_ptr->i_size)) == NULL)
220433d6423SLionel Sambuc 		return(err_code);
221433d6423SLionel Sambuc 	dp = &b_dir(bp)[0];
222433d6423SLionel Sambuc 	extended = 1;
223433d6423SLionel Sambuc   }
224433d6423SLionel Sambuc 
225433d6423SLionel Sambuc   /* 'bp' now points to a directory block with space. 'dp' points to slot. */
226433d6423SLionel Sambuc   (void) memset(dp->mfs_d_name, 0, (size_t) MFS_NAME_MAX); /* clear entry */
227433d6423SLionel Sambuc   for (i = 0; i < MFS_NAME_MAX && string[i]; i++) dp->mfs_d_name[i] = string[i];
228433d6423SLionel Sambuc   sp = ldir_ptr->i_sp;
229433d6423SLionel Sambuc   dp->mfs_d_ino = conv4(sp->s_native, (int) *numb);
230433d6423SLionel Sambuc   MARKDIRTY(bp);
231*0314acfbSDavid van Moolenbroek   put_block(bp);
232433d6423SLionel Sambuc   ldir_ptr->i_update |= CTIME | MTIME;	/* mark mtime for update later */
233433d6423SLionel Sambuc   IN_MARKDIRTY(ldir_ptr);
234433d6423SLionel Sambuc   if (new_slots > old_slots) {
235433d6423SLionel Sambuc 	ldir_ptr->i_size = (off_t) new_slots * DIR_ENTRY_SIZE;
236433d6423SLionel Sambuc 	/* Send the change to disk if the directory is extended. */
237433d6423SLionel Sambuc 	if (extended) rw_inode(ldir_ptr, WRITING);
238433d6423SLionel Sambuc   }
239433d6423SLionel Sambuc   return(OK);
240433d6423SLionel Sambuc }
241433d6423SLionel Sambuc 
242