xref: /minix3/minix/fs/ext2/path.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
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  * Created (MFS based):
5433d6423SLionel Sambuc  *   February 2010 (Evgeniy Ivanov)
6433d6423SLionel Sambuc  */
7433d6423SLionel Sambuc 
8433d6423SLionel Sambuc #include "fs.h"
9433d6423SLionel Sambuc #include <assert.h>
10433d6423SLionel Sambuc #include <string.h>
11433d6423SLionel Sambuc #include <sys/param.h>
12433d6423SLionel Sambuc #include "buf.h"
13433d6423SLionel Sambuc #include "inode.h"
14433d6423SLionel Sambuc #include "super.h"
15433d6423SLionel Sambuc 
16433d6423SLionel Sambuc 
17433d6423SLionel Sambuc /*===========================================================================*
18433d6423SLionel Sambuc  *                             fs_lookup				     *
19433d6423SLionel Sambuc  *===========================================================================*/
fs_lookup(ino_t dir_nr,char * name,struct fsdriver_node * node,int * is_mountpt)20970d95ecSDavid van Moolenbroek int fs_lookup(ino_t dir_nr, char *name, struct fsdriver_node *node,
21970d95ecSDavid van Moolenbroek 	int *is_mountpt)
22433d6423SLionel Sambuc {
23970d95ecSDavid van Moolenbroek   struct inode *dirp, *rip;
24433d6423SLionel Sambuc 
25970d95ecSDavid van Moolenbroek   /* Find the starting inode. */
26970d95ecSDavid van Moolenbroek   if ((dirp = find_inode(fs_dev, dir_nr)) == NULL)
27970d95ecSDavid van Moolenbroek 	return EINVAL;
28433d6423SLionel Sambuc 
29970d95ecSDavid van Moolenbroek   /* Look up the directory entry. */
30970d95ecSDavid van Moolenbroek   if ((rip = advance(dirp, name)) == NULL)
31970d95ecSDavid van Moolenbroek 	return err_code;
32433d6423SLionel Sambuc 
33970d95ecSDavid van Moolenbroek   /* On success, leave the resulting inode open and return its details. */
34970d95ecSDavid van Moolenbroek   node->fn_ino_nr = rip->i_num;
35970d95ecSDavid van Moolenbroek   node->fn_mode = rip->i_mode;
36970d95ecSDavid van Moolenbroek   node->fn_size = rip->i_size;
37970d95ecSDavid van Moolenbroek   node->fn_uid = rip->i_uid;
38970d95ecSDavid van Moolenbroek   node->fn_gid = rip->i_gid;
39433d6423SLionel Sambuc   /* This is only valid for block and character specials. But it doesn't
40433d6423SLionel Sambuc    * cause any harm to always set the device field. */
41970d95ecSDavid van Moolenbroek   node->fn_dev = (dev_t) rip->i_block[0];
42433d6423SLionel Sambuc 
43970d95ecSDavid van Moolenbroek   *is_mountpt = rip->i_mountpoint;
44433d6423SLionel Sambuc 
45970d95ecSDavid van Moolenbroek   return OK;
46433d6423SLionel Sambuc }
47433d6423SLionel Sambuc 
48433d6423SLionel Sambuc 
49433d6423SLionel Sambuc /*===========================================================================*
50433d6423SLionel Sambuc  *				advance					     *
51433d6423SLionel Sambuc  *===========================================================================*/
advance(dirp,string)52970d95ecSDavid van Moolenbroek struct inode *advance(dirp, string)
53433d6423SLionel Sambuc struct inode *dirp;		/* inode for directory to be searched */
54970d95ecSDavid van Moolenbroek const char *string;		/* component name to look for */
55433d6423SLionel Sambuc {
56433d6423SLionel Sambuc /* Given a directory and a component of a path, look up the component in
57433d6423SLionel Sambuc  * the directory, find the inode, open it, and return a pointer to its inode
58433d6423SLionel Sambuc  * slot.
59433d6423SLionel Sambuc  */
60433d6423SLionel Sambuc   ino_t numb;
61433d6423SLionel Sambuc   struct inode *rip;
62433d6423SLionel Sambuc 
63970d95ecSDavid van Moolenbroek   assert(dirp != NULL);
64970d95ecSDavid van Moolenbroek 
65433d6423SLionel Sambuc   /* If 'string' is empty, return an error. */
66433d6423SLionel Sambuc   if (string[0] == '\0') {
67433d6423SLionel Sambuc 	err_code = ENOENT;
68433d6423SLionel Sambuc 	return(NULL);
69433d6423SLionel Sambuc   }
70433d6423SLionel Sambuc 
71970d95ecSDavid van Moolenbroek   /* If dir has been removed return ENOENT. */
72970d95ecSDavid van Moolenbroek   if (dirp->i_links_count == NO_LINK) {
73970d95ecSDavid van Moolenbroek 	err_code = ENOENT;
74970d95ecSDavid van Moolenbroek 	return(NULL);
75970d95ecSDavid van Moolenbroek   }
76433d6423SLionel Sambuc 
77433d6423SLionel Sambuc   /* If 'string' is not present in the directory, signal error. */
78970d95ecSDavid van Moolenbroek   if ( (err_code = search_dir(dirp, string, &numb, LOOK_UP, 0)) != OK) {
79433d6423SLionel Sambuc 	return(NULL);
80433d6423SLionel Sambuc   }
81433d6423SLionel Sambuc 
82433d6423SLionel Sambuc   /* The component has been found in the directory.  Get inode. */
83433d6423SLionel Sambuc   if ( (rip = get_inode(dirp->i_dev, (int) numb)) == NULL)  {
84970d95ecSDavid van Moolenbroek 	assert(err_code != OK);
85433d6423SLionel Sambuc 	return(NULL);
86433d6423SLionel Sambuc   }
87433d6423SLionel Sambuc 
88433d6423SLionel Sambuc   return(rip);
89433d6423SLionel Sambuc }
90433d6423SLionel Sambuc 
91433d6423SLionel Sambuc 
92433d6423SLionel Sambuc /*===========================================================================*
93433d6423SLionel Sambuc  *				search_dir				     *
94433d6423SLionel Sambuc  *===========================================================================*/
search_dir(ldir_ptr,string,numb,flag,ftype)95970d95ecSDavid van Moolenbroek int search_dir(ldir_ptr, string, numb, flag, ftype)
96433d6423SLionel Sambuc register struct inode *ldir_ptr; /* ptr to inode for dir to search */
97970d95ecSDavid van Moolenbroek const char *string;		 /* component to search for */
98433d6423SLionel Sambuc ino_t *numb;			 /* pointer to inode number */
99433d6423SLionel Sambuc int flag;			 /* LOOK_UP, ENTER, DELETE or IS_EMPTY */
100970d95ecSDavid van Moolenbroek int ftype;			 /* used when ENTER and INCOMPAT_FILETYPE */
101433d6423SLionel Sambuc {
102433d6423SLionel Sambuc /* This function searches the directory whose inode is pointed to by 'ldip':
103433d6423SLionel Sambuc  * if (flag == ENTER)  enter 'string' in the directory with inode # '*numb';
104433d6423SLionel Sambuc  * if (flag == DELETE) delete 'string' from the directory;
105433d6423SLionel Sambuc  * if (flag == LOOK_UP) search for 'string' and return inode # in 'numb';
106433d6423SLionel Sambuc  * if (flag == IS_EMPTY) return OK if only . and .. in dir else ENOTEMPTY;
107433d6423SLionel Sambuc  */
108433d6423SLionel Sambuc   register struct ext2_disk_dir_desc  *dp = NULL;
109433d6423SLionel Sambuc   register struct ext2_disk_dir_desc  *prev_dp = NULL;
110433d6423SLionel Sambuc   register struct buf *bp = NULL;
111433d6423SLionel Sambuc   int i, r, e_hit, t, match;
112433d6423SLionel Sambuc   off_t pos;
113433d6423SLionel Sambuc   unsigned new_slots;
114433d6423SLionel Sambuc   int extended = 0;
115433d6423SLionel Sambuc   int required_space = 0;
116433d6423SLionel Sambuc   int string_len = 0;
117433d6423SLionel Sambuc 
118433d6423SLionel Sambuc   /* If 'ldir_ptr' is not a pointer to a dir inode, error. */
119433d6423SLionel Sambuc   if ( (ldir_ptr->i_mode & I_TYPE) != I_DIRECTORY)  {
120433d6423SLionel Sambuc 	return(ENOTDIR);
121433d6423SLionel Sambuc   }
122433d6423SLionel Sambuc 
123433d6423SLionel Sambuc   new_slots = 0;
124433d6423SLionel Sambuc   e_hit = FALSE;
125433d6423SLionel Sambuc   match = 0;    	/* set when a string match occurs */
126433d6423SLionel Sambuc   pos = 0;
127433d6423SLionel Sambuc 
128970d95ecSDavid van Moolenbroek   if ((string_len = strlen(string)) > EXT2_NAME_MAX)
129970d95ecSDavid van Moolenbroek 	return(ENAMETOOLONG);
130970d95ecSDavid van Moolenbroek 
131433d6423SLionel Sambuc   if (flag == ENTER) {
132433d6423SLionel Sambuc 	required_space = MIN_DIR_ENTRY_SIZE + string_len;
133433d6423SLionel Sambuc 	required_space += (required_space & 0x03) == 0 ? 0 :
134433d6423SLionel Sambuc 			     (DIR_ENTRY_ALIGN - (required_space & 0x03) );
135433d6423SLionel Sambuc 
136433d6423SLionel Sambuc 	if (ldir_ptr->i_last_dpos < ldir_ptr->i_size &&
137433d6423SLionel Sambuc 	    ldir_ptr->i_last_dentry_size <= required_space)
138433d6423SLionel Sambuc 		pos = ldir_ptr->i_last_dpos;
139433d6423SLionel Sambuc   }
140433d6423SLionel Sambuc 
141433d6423SLionel Sambuc   for (; pos < ldir_ptr->i_size; pos += ldir_ptr->i_sp->s_block_size) {
142433d6423SLionel Sambuc 	/* Since directories don't have holes, 'b' cannot be NO_BLOCK. */
143433d6423SLionel Sambuc 	if(!(bp = get_block_map(ldir_ptr,
144433d6423SLionel Sambuc 	   rounddown(pos, ldir_ptr->i_sp->s_block_size))))
145433d6423SLionel Sambuc 		panic("get_block returned NO_BLOCK");
146433d6423SLionel Sambuc 
147433d6423SLionel Sambuc 	prev_dp = NULL; /* New block - new first dentry, so no prev. */
148433d6423SLionel Sambuc 
149433d6423SLionel Sambuc 	/* Search a directory block.
150433d6423SLionel Sambuc 	 * Note, we set prev_dp at the end of the loop.
151433d6423SLionel Sambuc 	 */
152433d6423SLionel Sambuc 	for (dp = (struct ext2_disk_dir_desc*) &b_data(bp);
153433d6423SLionel Sambuc 	     CUR_DISC_DIR_POS(dp, &b_data(bp)) < ldir_ptr->i_sp->s_block_size;
154433d6423SLionel Sambuc 	     dp = NEXT_DISC_DIR_DESC(dp) ) {
155433d6423SLionel Sambuc 		/* Match occurs if string found. */
156433d6423SLionel Sambuc 		if (flag != ENTER && dp->d_ino != NO_ENTRY) {
157433d6423SLionel Sambuc 			if (flag == IS_EMPTY) {
158433d6423SLionel Sambuc 				/* If this test succeeds, dir is not empty. */
159433d6423SLionel Sambuc 				if (ansi_strcmp(dp->d_name, ".", dp->d_name_len) != 0 &&
160433d6423SLionel Sambuc 				    ansi_strcmp(dp->d_name, "..", dp->d_name_len) != 0) match = 1;
161433d6423SLionel Sambuc 			} else {
162433d6423SLionel Sambuc 				if (ansi_strcmp(dp->d_name, string, dp->d_name_len) == 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 				if (dp->d_name_len >= sizeof(ino_t)) {
174433d6423SLionel Sambuc 					/* Save d_ino for recovery. */
175433d6423SLionel Sambuc 					t = dp->d_name_len - sizeof(ino_t);
176*0a6a1f1dSLionel Sambuc 					memcpy(&dp->d_name[t], &dp->d_ino, sizeof(dp->d_ino));
177433d6423SLionel Sambuc 				}
178433d6423SLionel Sambuc 				dp->d_ino = NO_ENTRY;	/* erase entry */
179433d6423SLionel Sambuc 				lmfs_markdirty(bp);
180433d6423SLionel Sambuc 
181433d6423SLionel Sambuc 				/* If we don't support HTree (directory index),
182433d6423SLionel Sambuc 				 * which is fully compatible ext2 feature,
183433d6423SLionel Sambuc 				 * we should reset EXT2_INDEX_FL, when modify
184433d6423SLionel Sambuc 				 * linked directory structure.
185433d6423SLionel Sambuc 				 *
186433d6423SLionel Sambuc 				 * @TODO: actually we could just reset it for
187433d6423SLionel Sambuc 				 * each directory, but I added if() to not
188433d6423SLionel Sambuc 				 * forget about it later, when add HTree
189433d6423SLionel Sambuc 				 * support.
190433d6423SLionel Sambuc 				 */
191433d6423SLionel Sambuc 				if (!HAS_COMPAT_FEATURE(ldir_ptr->i_sp,
192433d6423SLionel Sambuc 							COMPAT_DIR_INDEX))
193433d6423SLionel Sambuc 					ldir_ptr->i_flags &= ~EXT2_INDEX_FL;
194433d6423SLionel Sambuc 				if (pos < ldir_ptr->i_last_dpos) {
195433d6423SLionel Sambuc 					ldir_ptr->i_last_dpos = pos;
196433d6423SLionel Sambuc 					ldir_ptr->i_last_dentry_size =
197433d6423SLionel Sambuc 						conv2(le_CPU, dp->d_rec_len);
198433d6423SLionel Sambuc 				}
199433d6423SLionel Sambuc 				ldir_ptr->i_update |= CTIME | MTIME;
200433d6423SLionel Sambuc 				ldir_ptr->i_dirt = IN_DIRTY;
201433d6423SLionel Sambuc 				/* Now we have cleared dentry, if it's not
202433d6423SLionel Sambuc 				 * the first one, merge it with previous one.
203433d6423SLionel Sambuc 				 * Since we assume, that existing dentry must be
204433d6423SLionel Sambuc 				 * correct, there is no way to spann a data block.
205433d6423SLionel Sambuc 				 */
206433d6423SLionel Sambuc 				if (prev_dp) {
207433d6423SLionel Sambuc 					u16_t temp = conv2(le_CPU,
208433d6423SLionel Sambuc 							prev_dp->d_rec_len);
209433d6423SLionel Sambuc 					temp += conv2(le_CPU,
210433d6423SLionel Sambuc 							dp->d_rec_len);
211433d6423SLionel Sambuc 					prev_dp->d_rec_len = conv2(le_CPU,
212433d6423SLionel Sambuc 							temp);
213433d6423SLionel Sambuc 				}
214433d6423SLionel Sambuc 			} else {
215433d6423SLionel Sambuc 				/* 'flag' is LOOK_UP */
216433d6423SLionel Sambuc 				*numb = (ino_t) conv4(le_CPU, dp->d_ino);
217433d6423SLionel Sambuc 			}
2180314acfbSDavid van Moolenbroek 			put_block(bp);
219433d6423SLionel Sambuc 			return(r);
220433d6423SLionel Sambuc 		}
221433d6423SLionel Sambuc 
222433d6423SLionel Sambuc 		/* Check for free slot for the benefit of ENTER. */
223433d6423SLionel Sambuc 		if (flag == ENTER && dp->d_ino == NO_ENTRY) {
224433d6423SLionel Sambuc 			/* we found a free slot, check if it has enough space */
225433d6423SLionel Sambuc 			if (required_space <= conv2(le_CPU, dp->d_rec_len)) {
226433d6423SLionel Sambuc 				e_hit = TRUE;	/* we found a free slot */
227433d6423SLionel Sambuc 				break;
228433d6423SLionel Sambuc 			}
229433d6423SLionel Sambuc 		}
230433d6423SLionel Sambuc 		/* Can we shrink dentry? */
231433d6423SLionel Sambuc 		if (flag == ENTER && required_space <= DIR_ENTRY_SHRINK(dp)) {
232433d6423SLionel Sambuc 			/* Shrink directory and create empty slot, now
233433d6423SLionel Sambuc 			 * dp->d_rec_len = DIR_ENTRY_ACTUAL_SIZE + DIR_ENTRY_SHRINK.
234433d6423SLionel Sambuc 			 */
235433d6423SLionel Sambuc 			int new_slot_size = conv2(le_CPU, dp->d_rec_len);
236433d6423SLionel Sambuc 			int actual_size = DIR_ENTRY_ACTUAL_SIZE(dp);
237433d6423SLionel Sambuc 			new_slot_size -= actual_size;
238433d6423SLionel Sambuc 			dp->d_rec_len = conv2(le_CPU, actual_size);
239433d6423SLionel Sambuc 			dp = NEXT_DISC_DIR_DESC(dp);
240433d6423SLionel Sambuc 			dp->d_rec_len = conv2(le_CPU, new_slot_size);
241433d6423SLionel Sambuc 			/* if we fail before writing real ino */
242433d6423SLionel Sambuc 			dp->d_ino = NO_ENTRY;
243433d6423SLionel Sambuc 			lmfs_markdirty(bp);
244433d6423SLionel Sambuc 			e_hit = TRUE;	/* we found a free slot */
245433d6423SLionel Sambuc 			break;
246433d6423SLionel Sambuc 		}
247433d6423SLionel Sambuc 
248433d6423SLionel Sambuc 		prev_dp = dp;
249433d6423SLionel Sambuc 	}
250433d6423SLionel Sambuc 
251433d6423SLionel Sambuc 	/* The whole block has been searched or ENTER has a free slot. */
252433d6423SLionel Sambuc 	if (e_hit) break;	/* e_hit set if ENTER can be performed now */
2530314acfbSDavid van Moolenbroek 	put_block(bp);		 /* otherwise, continue searching dir */
254433d6423SLionel Sambuc   }
255433d6423SLionel Sambuc 
256433d6423SLionel Sambuc   /* The whole directory has now been searched. */
257433d6423SLionel Sambuc   if (flag != ENTER) {
258433d6423SLionel Sambuc 	return(flag == IS_EMPTY ? OK : ENOENT);
259433d6423SLionel Sambuc   }
260433d6423SLionel Sambuc 
261433d6423SLionel Sambuc   /* When ENTER next time, start searching for free slot from
262433d6423SLionel Sambuc    * i_last_dpos. It gives solid performance improvement.
263433d6423SLionel Sambuc    */
264433d6423SLionel Sambuc   ldir_ptr->i_last_dpos = pos;
265433d6423SLionel Sambuc   ldir_ptr->i_last_dentry_size = required_space;
266433d6423SLionel Sambuc 
267433d6423SLionel Sambuc   /* This call is for ENTER.  If no free slot has been found so far, try to
268433d6423SLionel Sambuc    * extend directory.
269433d6423SLionel Sambuc    */
270433d6423SLionel Sambuc   if (e_hit == FALSE) { /* directory is full and no room left in last block */
271433d6423SLionel Sambuc 	new_slots++;		/* increase directory size by 1 entry */
272433d6423SLionel Sambuc 	if ( (bp = new_block(ldir_ptr, ldir_ptr->i_size)) == NULL)
273433d6423SLionel Sambuc 		return(err_code);
274433d6423SLionel Sambuc 	dp = (struct ext2_disk_dir_desc*) &b_data(bp);
275433d6423SLionel Sambuc 	dp->d_rec_len = conv2(le_CPU, ldir_ptr->i_sp->s_block_size);
276433d6423SLionel Sambuc 	dp->d_name_len = DIR_ENTRY_MAX_NAME_LEN(dp); /* for failure */
277433d6423SLionel Sambuc 	extended = 1;
278433d6423SLionel Sambuc   }
279433d6423SLionel Sambuc 
280433d6423SLionel Sambuc   /* 'bp' now points to a directory block with space. 'dp' points to slot. */
281433d6423SLionel Sambuc   dp->d_name_len = string_len;
282433d6423SLionel Sambuc   for (i = 0; i < NAME_MAX && i < dp->d_name_len && string[i]; i++)
283433d6423SLionel Sambuc 	dp->d_name[i] = string[i];
284433d6423SLionel Sambuc   dp->d_ino = (int) conv4(le_CPU, *numb);
285433d6423SLionel Sambuc   if (HAS_INCOMPAT_FEATURE(ldir_ptr->i_sp, INCOMPAT_FILETYPE)) {
286433d6423SLionel Sambuc 	/* Convert ftype (from inode.i_mode) to dp->d_file_type */
287433d6423SLionel Sambuc 	if (ftype == I_REGULAR)
288433d6423SLionel Sambuc 		dp->d_file_type = EXT2_FT_REG_FILE;
289433d6423SLionel Sambuc 	else if (ftype == I_DIRECTORY)
290433d6423SLionel Sambuc 		dp->d_file_type = EXT2_FT_DIR;
291433d6423SLionel Sambuc 	else if (ftype == I_SYMBOLIC_LINK)
292433d6423SLionel Sambuc 		dp->d_file_type = EXT2_FT_SYMLINK;
293433d6423SLionel Sambuc 	else if (ftype == I_BLOCK_SPECIAL)
294433d6423SLionel Sambuc 		dp->d_file_type = EXT2_FT_BLKDEV;
295433d6423SLionel Sambuc 	else if (ftype == I_CHAR_SPECIAL)
296433d6423SLionel Sambuc 		dp->d_file_type = EXT2_FT_CHRDEV;
297433d6423SLionel Sambuc 	else if (ftype == I_NAMED_PIPE)
298433d6423SLionel Sambuc 		dp->d_file_type = EXT2_FT_FIFO;
299433d6423SLionel Sambuc 	else
300433d6423SLionel Sambuc 		dp->d_file_type = EXT2_FT_UNKNOWN;
301433d6423SLionel Sambuc   }
302433d6423SLionel Sambuc   lmfs_markdirty(bp);
3030314acfbSDavid van Moolenbroek   put_block(bp);
304433d6423SLionel Sambuc   ldir_ptr->i_update |= CTIME | MTIME;	/* mark mtime for update later */
305433d6423SLionel Sambuc   ldir_ptr->i_dirt = IN_DIRTY;
306433d6423SLionel Sambuc 
307433d6423SLionel Sambuc   if (new_slots == 1) {
308433d6423SLionel Sambuc 	ldir_ptr->i_size += (off_t) conv2(le_CPU, dp->d_rec_len);
309433d6423SLionel Sambuc 	/* Send the change to disk if the directory is extended. */
310433d6423SLionel Sambuc 	if (extended) rw_inode(ldir_ptr, WRITING);
311433d6423SLionel Sambuc   }
312433d6423SLionel Sambuc   return(OK);
313433d6423SLionel Sambuc 
314433d6423SLionel Sambuc }
315