5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
10 * linux/fs/ext2/namei.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/namei.c
21 * Copyright (C) 1991, 1992 Linus Torvalds
23 * Big-endian to little-endian byte-swapping/bitmaps by
24 * David S. Miller (davem@caip.rutgers.edu), 1995
27 #include <linux/sched.h>
29 #include <linux/ufs_fs.h>
31 #undef UFS_NAMEI_DEBUG
33 #ifdef UFS_NAMEI_DEBUG
34 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
39 static inline void ufs_inc_count(struct inode *inode)
42 mark_inode_dirty(inode);
45 static inline void ufs_dec_count(struct inode *inode)
48 mark_inode_dirty(inode);
51 static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
53 int err = ufs_add_link(dentry, inode);
55 d_instantiate(dentry, inode);
63 static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry)
65 struct inode * inode = NULL;
68 if (dentry->d_name.len > UFS_MAXNAMLEN)
69 return ERR_PTR(-ENAMETOOLONG);
71 ino = ufs_inode_by_name(dir, dentry);
73 inode = iget(dir->i_sb, ino);
75 return ERR_PTR(-EACCES);
82 * By the time this is called, we already have created
83 * the directory cache entry for the new file, but it
84 * is so far negative - it has no inode.
86 * If the create succeeds, we fill in the inode information
87 * with d_instantiate().
89 static int ufs_create (struct inode * dir, struct dentry * dentry, int mode)
91 struct inode * inode = ufs_new_inode(dir, mode);
92 int err = PTR_ERR(inode);
94 inode->i_op = &ufs_file_inode_operations;
95 inode->i_fop = &ufs_file_operations;
96 inode->i_mapping->a_ops = &ufs_aops;
97 mark_inode_dirty(inode);
98 err = ufs_add_nondir(dentry, inode);
103 static int ufs_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
105 struct inode * inode = ufs_new_inode(dir, mode);
106 int err = PTR_ERR(inode);
107 if (!IS_ERR(inode)) {
108 init_special_inode(inode, mode, rdev);
109 mark_inode_dirty(inode);
110 err = ufs_add_nondir(dentry, inode);
115 static int ufs_symlink (struct inode * dir, struct dentry * dentry,
116 const char * symname)
118 struct super_block * sb = dir->i_sb;
119 int err = -ENAMETOOLONG;
120 unsigned l = strlen(symname)+1;
121 struct inode * inode;
123 if (l > sb->s_blocksize)
126 inode = ufs_new_inode(dir, S_IFLNK | S_IRWXUGO);
127 err = PTR_ERR(inode);
131 if (l > sb->u.ufs_sb.s_uspi->s_maxsymlinklen) {
133 inode->i_op = &page_symlink_inode_operations;
134 inode->i_mapping->a_ops = &ufs_aops;
135 err = block_symlink(inode, symname, l);
140 inode->i_op = &ufs_fast_symlink_inode_operations;
141 memcpy((char*)&inode->u.ufs_i.i_u1.i_data,symname,l);
144 mark_inode_dirty(inode);
146 err = ufs_add_nondir(dentry, inode);
151 ufs_dec_count(inode);
156 static int ufs_link (struct dentry * old_dentry, struct inode * dir,
157 struct dentry *dentry)
159 struct inode *inode = old_dentry->d_inode;
161 if (S_ISDIR(inode->i_mode))
164 if (inode->i_nlink >= UFS_LINK_MAX)
167 inode->i_ctime = CURRENT_TIME;
168 ufs_inc_count(inode);
169 atomic_inc(&inode->i_count);
171 return ufs_add_nondir(dentry, inode);
174 static int ufs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
176 struct inode * inode;
179 if (dir->i_nlink >= UFS_LINK_MAX)
184 inode = ufs_new_inode(dir, S_IFDIR|mode);
185 err = PTR_ERR(inode);
189 inode->i_op = &ufs_dir_inode_operations;
190 inode->i_fop = &ufs_dir_operations;
192 ufs_inc_count(inode);
194 err = ufs_make_empty(inode, dir);
198 err = ufs_add_link(dentry, inode);
202 d_instantiate(dentry, inode);
207 ufs_dec_count(inode);
208 ufs_dec_count(inode);
215 static int ufs_unlink(struct inode * dir, struct dentry *dentry)
217 struct inode * inode = dentry->d_inode;
218 struct buffer_head * bh;
219 struct ufs_dir_entry * de;
222 de = ufs_find_entry (dentry, &bh);
226 err = ufs_delete_entry (dir, de, bh);
230 inode->i_ctime = dir->i_ctime;
231 ufs_dec_count(inode);
237 static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
239 struct inode * inode = dentry->d_inode;
242 if (ufs_empty_dir (inode)) {
243 err = ufs_unlink(dir, dentry);
246 ufs_dec_count(inode);
253 static int ufs_rename (struct inode * old_dir, struct dentry * old_dentry,
254 struct inode * new_dir, struct dentry * new_dentry )
256 struct inode *old_inode = old_dentry->d_inode;
257 struct inode *new_inode = new_dentry->d_inode;
258 struct buffer_head *dir_bh = NULL;
259 struct ufs_dir_entry *dir_de = NULL;
260 struct buffer_head *old_bh;
261 struct ufs_dir_entry *old_de;
264 old_de = ufs_find_entry (old_dentry, &old_bh);
268 if (S_ISDIR(old_inode->i_mode)) {
270 dir_de = ufs_dotdot(old_inode, &dir_bh);
276 struct buffer_head *new_bh;
277 struct ufs_dir_entry *new_de;
280 if (dir_de && !ufs_empty_dir (new_inode))
283 new_de = ufs_find_entry (new_dentry, &new_bh);
286 ufs_inc_count(old_inode);
287 ufs_set_link(new_dir, new_de, new_bh, old_inode);
288 new_inode->i_ctime = CURRENT_TIME;
290 new_inode->i_nlink--;
291 ufs_dec_count(new_inode);
295 if (new_dir->i_nlink >= UFS_LINK_MAX)
298 ufs_inc_count(old_inode);
299 err = ufs_add_link(new_dentry, old_inode);
301 ufs_dec_count(old_inode);
305 ufs_inc_count(new_dir);
308 ufs_delete_entry (old_dir, old_de, old_bh);
310 ufs_dec_count(old_inode);
313 ufs_set_link(old_inode, dir_de, dir_bh, new_dir);
314 ufs_dec_count(old_dir);
327 struct inode_operations ufs_dir_inode_operations = {
332 symlink: ufs_symlink,