xref: /minix/minix/fs/mfs/path.c (revision 83133719)
1 /* This file contains the procedures that look up path names in the directory
2  * system and determine the inode number that goes with a given path name.
3  */
4 
5 #include "fs.h"
6 #include <assert.h>
7 #include <string.h>
8 #include "buf.h"
9 #include "inode.h"
10 #include "super.h"
11 
12 
13 /*===========================================================================*
14  *                             fs_lookup				     *
15  *===========================================================================*/
16 int fs_lookup(ino_t dir_nr, char *name, struct fsdriver_node *node,
17 	int *is_mountpt)
18 {
19   struct inode *dirp, *rip;
20 
21   /* Find the starting inode. */
22   if ((dirp = find_inode(fs_dev, dir_nr)) == NULL)
23 	return EINVAL;
24 
25   /* Look up the directory entry. */
26   if ((rip = advance(dirp, name)) == NULL)
27 	return err_code;
28 
29   /* On success, leave the resulting inode open and return its details. */
30   node->fn_ino_nr = rip->i_num;
31   node->fn_mode = rip->i_mode;
32   node->fn_size = rip->i_size;
33   node->fn_uid = rip->i_uid;
34   node->fn_gid = rip->i_gid;
35   /* This is only valid for block and character specials. But it doesn't
36    * cause any harm to always set the device field. */
37   node->fn_dev = (dev_t) rip->i_zone[0];
38 
39   *is_mountpt = rip->i_mountpoint;
40 
41   return OK;
42 }
43 
44 
45 /*===========================================================================*
46  *				advance					     *
47  *===========================================================================*/
48 struct inode *advance(dirp, string)
49 struct inode *dirp;		/* inode for directory to be searched */
50 const char *string;		/* component name to look for */
51 {
52 /* Given a directory and a component of a path, look up the component in
53  * the directory, find the inode, open it, and return a pointer to its inode
54  * slot.
55  */
56   ino_t numb;
57   struct inode *rip;
58 
59   assert(dirp != NULL);
60 
61   /* If 'string' is empty, return an error. */
62   if (string[0] == '\0') {
63   	err_code = ENOENT;
64 	return(NULL);
65   }
66 
67   /* If dir has been removed return ENOENT. */
68   if (dirp->i_nlinks == NO_LINK) {
69 	err_code = ENOENT;
70 	return(NULL);
71   }
72 
73   /* If 'string' is not present in the directory, signal error. */
74   if ( (err_code = search_dir(dirp, string, &numb, LOOK_UP)) != OK) {
75 	return(NULL);
76   }
77 
78   /* The component has been found in the directory.  Get inode. */
79   if ( (rip = get_inode(dirp->i_dev, (int) numb)) == NULL)  {
80 	assert(err_code != OK);
81 	return(NULL);
82   }
83 
84   assert(err_code == OK);
85   return(rip);
86 }
87 
88 
89 /*===========================================================================*
90  *				search_dir				     *
91  *===========================================================================*/
92 int search_dir(ldir_ptr, string, numb, flag)
93 register struct inode *ldir_ptr; /* ptr to inode for dir to search */
94 const char *string;		 /* component to search for */
95 ino_t *numb;			 /* pointer to inode number */
96 int flag;			 /* LOOK_UP, ENTER, DELETE or IS_EMPTY */
97 {
98 /* This function searches the directory whose inode is pointed to by 'ldip':
99  * if (flag == ENTER)  enter 'string' in the directory with inode # '*numb';
100  * if (flag == DELETE) delete 'string' from the directory;
101  * if (flag == LOOK_UP) search for 'string' and return inode # in 'numb';
102  * if (flag == IS_EMPTY) return OK if only . and .. in dir else ENOTEMPTY;
103  *
104  * This function, and this function alone, implements name truncation,
105  * by simply considering only the first MFS_NAME_MAX bytes from 'string'.
106  */
107   register struct direct *dp = NULL;
108   register struct buf *bp = NULL;
109   int i, r, e_hit, t, match;
110   off_t pos;
111   unsigned new_slots, old_slots;
112   struct super_block *sp;
113   int extended = 0;
114 
115   /* If 'ldir_ptr' is not a pointer to a dir inode, error. */
116   if ( (ldir_ptr->i_mode & I_TYPE) != I_DIRECTORY)  {
117 	return(ENOTDIR);
118    }
119 
120   if((flag == DELETE || flag == ENTER) && ldir_ptr->i_sp->s_rd_only)
121 	return EROFS;
122 
123   /* Step through the directory one block at a time. */
124   old_slots = (unsigned) (ldir_ptr->i_size/DIR_ENTRY_SIZE);
125   new_slots = 0;
126   e_hit = FALSE;
127   match = 0;			/* set when a string match occurs */
128 
129   pos = 0;
130   if (flag == ENTER && ldir_ptr->i_last_dpos < ldir_ptr->i_size) {
131 	pos = ldir_ptr->i_last_dpos;
132 	new_slots = (unsigned) (pos/DIR_ENTRY_SIZE);
133   }
134 
135   for (; pos < ldir_ptr->i_size; pos += ldir_ptr->i_sp->s_block_size) {
136 	assert(ldir_ptr->i_dev != NO_DEV);
137 
138 	/* Since directories don't have holes, 'b' cannot be NO_BLOCK. */
139 	bp = get_block_map(ldir_ptr, pos);
140 
141 	assert(ldir_ptr->i_dev != NO_DEV);
142 	assert(bp != NULL);
143 	assert(lmfs_dev(bp) != NO_DEV);
144 
145 	/* Search a directory block. */
146 	for (dp = &b_dir(bp)[0];
147 		dp < &b_dir(bp)[NR_DIR_ENTRIES(ldir_ptr->i_sp->s_block_size)];
148 		dp++) {
149 		if (++new_slots > old_slots) { /* not found, but room left */
150 			if (flag == ENTER) e_hit = TRUE;
151 			break;
152 		}
153 
154 		/* Match occurs if string found. */
155 		if (flag != ENTER && dp->mfs_d_ino != NO_ENTRY) {
156 			if (flag == IS_EMPTY) {
157 				/* If this test succeeds, dir is not empty. */
158 				if (strcmp(dp->mfs_d_name, "." ) != 0 &&
159 				    strcmp(dp->mfs_d_name, "..") != 0)
160 					match = 1;
161 			} else {
162 				if (strncmp(dp->mfs_d_name, string,
163 					sizeof(dp->mfs_d_name)) == 0){
164 					match = 1;
165 				}
166 			}
167 		}
168 
169 		if (match) {
170 			/* LOOK_UP or DELETE found what it wanted. */
171 			r = OK;
172 			if (flag == IS_EMPTY) r = ENOTEMPTY;
173 			else if (flag == DELETE) {
174 				/* Save d_ino for recovery. */
175 				t = MFS_NAME_MAX - sizeof(ino_t);
176 				*((ino_t *) &dp->mfs_d_name[t]) = dp->mfs_d_ino;
177 				dp->mfs_d_ino = NO_ENTRY; /* erase entry */
178 				MARKDIRTY(bp);
179 				ldir_ptr->i_update |= CTIME | MTIME;
180 				IN_MARKDIRTY(ldir_ptr);
181 				if (pos < ldir_ptr->i_last_dpos)
182 					ldir_ptr->i_last_dpos = pos;
183 			} else {
184 				sp = ldir_ptr->i_sp;	/* 'flag' is LOOK_UP */
185 				*numb = (ino_t) conv4(sp->s_native,
186 						      (int) dp->mfs_d_ino);
187 			}
188 			assert(lmfs_dev(bp) != NO_DEV);
189 			put_block(bp, DIRECTORY_BLOCK);
190 			return(r);
191 		}
192 
193 		/* Check for free slot for the benefit of ENTER. */
194 		if (flag == ENTER && dp->mfs_d_ino == 0) {
195 			e_hit = TRUE;	/* we found a free slot */
196 			break;
197 		}
198 	}
199 
200 	/* The whole block has been searched or ENTER has a free slot. */
201 	if (e_hit) break;	/* e_hit set if ENTER can be performed now */
202 	assert(lmfs_dev(bp) != NO_DEV);
203 	put_block(bp, DIRECTORY_BLOCK);	/* otherwise, continue searching dir */
204   }
205 
206   /* The whole directory has now been searched. */
207   if (flag != ENTER) {
208   	return(flag == IS_EMPTY ? OK : ENOENT);
209   }
210 
211   /* When ENTER next time, start searching for free slot from
212    * i_last_dpos. It gives some performance improvement (3-5%).
213    */
214   ldir_ptr->i_last_dpos = pos;
215 
216   /* This call is for ENTER.  If no free slot has been found so far, try to
217    * extend directory.
218    */
219   if (e_hit == FALSE) { /* directory is full and no room left in last block */
220 	new_slots++;		/* increase directory size by 1 entry */
221 	if (new_slots == 0) return(EFBIG); /* dir size limited by slot count */
222 	if ( (bp = new_block(ldir_ptr, ldir_ptr->i_size)) == NULL)
223 		return(err_code);
224 	dp = &b_dir(bp)[0];
225 	extended = 1;
226   }
227 
228   /* 'bp' now points to a directory block with space. 'dp' points to slot. */
229   (void) memset(dp->mfs_d_name, 0, (size_t) MFS_NAME_MAX); /* clear entry */
230   for (i = 0; i < MFS_NAME_MAX && string[i]; i++) dp->mfs_d_name[i] = string[i];
231   sp = ldir_ptr->i_sp;
232   dp->mfs_d_ino = conv4(sp->s_native, (int) *numb);
233   MARKDIRTY(bp);
234   put_block(bp, DIRECTORY_BLOCK);
235   ldir_ptr->i_update |= CTIME | MTIME;	/* mark mtime for update later */
236   IN_MARKDIRTY(ldir_ptr);
237   if (new_slots > old_slots) {
238 	ldir_ptr->i_size = (off_t) new_slots * DIR_ENTRY_SIZE;
239 	/* Send the change to disk if the directory is extended. */
240 	if (extended) rw_inode(ldir_ptr, WRITING);
241   }
242   return(OK);
243 }
244 
245