2 * linux/fs/ext2/namei.c
4 * Rewrite to pagecache. Almost all code had been changed, so blame me
5 * if the things go wrong. Please, send bug reports to viro@math.psu.edu
7 * Stuff here is basically a glue between the VFS and generic UNIXish
8 * filesystem that keeps everything in pagecache. All knowledge of the
9 * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable
10 * and it's easier to debug that way. In principle we might want to
11 * generalize that a bit and turn it into a library. Or not.
13 * The only non-static object here is ext2_dir_inode_operations.
15 * TODO: get rid of kmap() use, add readahead.
17 * Copyright (C) 1992, 1993, 1994, 1995
18 * Remy Card (card@masi.ibp.fr)
19 * Laboratoire MASI - Institut Blaise Pascal
20 * Universite Pierre et Marie Curie (Paris VI)
24 * linux/fs/minix/namei.c
26 * Copyright (C) 1991, 1992 Linus Torvalds
28 * Big-endian to little-endian byte-swapping/bitmaps by
29 * David S. Miller (davem@caip.rutgers.edu), 1995
33 #include <linux/ext2_fs.h>
34 #include <linux/pagemap.h>
37 * Couple of helper functions - make the code slightly cleaner.
40 static inline void ext2_inc_count(struct inode *inode)
43 mark_inode_dirty(inode);
46 static inline void ext2_dec_count(struct inode *inode)
49 mark_inode_dirty(inode);
52 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
54 int err = ext2_add_link(dentry, inode);
56 d_instantiate(dentry, inode);
59 ext2_dec_count(inode);
68 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry)
73 if (dentry->d_name.len > EXT2_NAME_LEN)
74 return ERR_PTR(-ENAMETOOLONG);
76 ino = ext2_inode_by_name(dir, dentry);
79 inode = iget(dir->i_sb, ino);
81 return ERR_PTR(-EACCES);
88 * By the time this is called, we already have created
89 * the directory cache entry for the new file, but it
90 * is so far negative - it has no inode.
92 * If the create succeeds, we fill in the inode information
93 * with d_instantiate().
95 static int ext2_create (struct inode * dir, struct dentry * dentry, int mode)
97 struct inode * inode = ext2_new_inode (dir, mode);
98 int err = PTR_ERR(inode);
100 inode->i_op = &ext2_file_inode_operations;
101 inode->i_fop = &ext2_file_operations;
102 inode->i_mapping->a_ops = &ext2_aops;
103 mark_inode_dirty(inode);
104 err = ext2_add_nondir(dentry, inode);
109 static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
111 struct inode * inode = ext2_new_inode (dir, mode);
112 int err = PTR_ERR(inode);
113 if (!IS_ERR(inode)) {
114 init_special_inode(inode, mode, rdev);
115 mark_inode_dirty(inode);
116 err = ext2_add_nondir(dentry, inode);
121 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
122 const char * symname)
124 struct super_block * sb = dir->i_sb;
125 int err = -ENAMETOOLONG;
126 unsigned l = strlen(symname)+1;
127 struct inode * inode;
129 if (l > sb->s_blocksize)
132 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
133 err = PTR_ERR(inode);
137 if (l > sizeof (inode->u.ext2_i.i_data)) {
139 inode->i_op = &page_symlink_inode_operations;
140 inode->i_mapping->a_ops = &ext2_aops;
141 err = block_symlink(inode, symname, l);
146 inode->i_op = &ext2_fast_symlink_inode_operations;
147 memcpy((char*)&inode->u.ext2_i.i_data,symname,l);
150 mark_inode_dirty(inode);
152 err = ext2_add_nondir(dentry, inode);
157 ext2_dec_count(inode);
162 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
163 struct dentry *dentry)
165 struct inode *inode = old_dentry->d_inode;
167 if (S_ISDIR(inode->i_mode))
170 if (inode->i_nlink >= EXT2_LINK_MAX)
173 inode->i_ctime = CURRENT_TIME;
174 ext2_inc_count(inode);
175 atomic_inc(&inode->i_count);
177 return ext2_add_nondir(dentry, inode);
180 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
182 struct inode * inode;
185 if (dir->i_nlink >= EXT2_LINK_MAX)
190 inode = ext2_new_inode (dir, S_IFDIR | mode);
191 err = PTR_ERR(inode);
195 inode->i_op = &ext2_dir_inode_operations;
196 inode->i_fop = &ext2_dir_operations;
197 inode->i_mapping->a_ops = &ext2_aops;
199 ext2_inc_count(inode);
201 err = ext2_make_empty(inode, dir);
205 err = ext2_add_link(dentry, inode);
209 d_instantiate(dentry, inode);
214 ext2_dec_count(inode);
215 ext2_dec_count(inode);
222 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
224 struct inode * inode = dentry->d_inode;
225 struct ext2_dir_entry_2 * de;
229 de = ext2_find_entry (dir, dentry, &page);
233 err = ext2_delete_entry (de, page);
237 inode->i_ctime = dir->i_ctime;
238 ext2_dec_count(inode);
244 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
246 struct inode * inode = dentry->d_inode;
247 int err = -ENOTEMPTY;
249 if (ext2_empty_dir(inode)) {
250 err = ext2_unlink(dir, dentry);
253 ext2_dec_count(inode);
260 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
261 struct inode * new_dir, struct dentry * new_dentry )
263 struct inode * old_inode = old_dentry->d_inode;
264 struct inode * new_inode = new_dentry->d_inode;
265 struct page * dir_page = NULL;
266 struct ext2_dir_entry_2 * dir_de = NULL;
267 struct page * old_page;
268 struct ext2_dir_entry_2 * old_de;
271 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
275 if (S_ISDIR(old_inode->i_mode)) {
277 dir_de = ext2_dotdot(old_inode, &dir_page);
283 struct page *new_page;
284 struct ext2_dir_entry_2 *new_de;
287 if (dir_de && !ext2_empty_dir (new_inode))
291 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
294 ext2_inc_count(old_inode);
295 ext2_set_link(new_dir, new_de, new_page, old_inode);
296 new_inode->i_ctime = CURRENT_TIME;
298 new_inode->i_nlink--;
299 ext2_dec_count(new_inode);
303 if (new_dir->i_nlink >= EXT2_LINK_MAX)
306 ext2_inc_count(old_inode);
307 err = ext2_add_link(new_dentry, old_inode);
309 ext2_dec_count(old_inode);
313 ext2_inc_count(new_dir);
317 * Like most other Unix systems, set the ctime for inodes on a
318 * rename. Note that other 2.4 FS as well as EXT2 on 2.2 and 2.6
319 * do this. ext2_dec_count() will mark the inode dirty.
321 old_inode->i_ctime = CURRENT_TIME;
323 ext2_delete_entry (old_de, old_page);
324 ext2_dec_count(old_inode);
327 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
328 ext2_dec_count(old_dir);
336 page_cache_release(dir_page);
340 page_cache_release(old_page);
345 struct inode_operations ext2_dir_inode_operations = {
350 symlink: ext2_symlink,