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/time.h>
29 #include <linux/ufs_fs.h>
30 #include <linux/smp_lock.h>
31 #include <linux/buffer_head.h>
32 #include "swab.h" /* will go away - see comment in mknod() */
36 #undef UFS_NAMEI_DEBUG
38 #define UFS_NAMEI_DEBUG
40 #ifdef UFS_NAMEI_DEBUG
41 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
46 static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
48 int err = ufs_add_link(dentry, inode);
50 d_instantiate(dentry, inode);
53 inode_dec_link_count(inode);
58 static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
60 struct inode * inode = NULL;
63 if (dentry->d_name.len > UFS_MAXNAMLEN)
64 return ERR_PTR(-ENAMETOOLONG);
67 ino = ufs_inode_by_name(dir, dentry);
69 inode = iget(dir->i_sb, ino);
72 return ERR_PTR(-EACCES);
81 * By the time this is called, we already have created
82 * the directory cache entry for the new file, but it
83 * is so far negative - it has no inode.
85 * If the create succeeds, we fill in the inode information
86 * with d_instantiate().
88 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);
99 err = ufs_add_nondir(dentry, inode);
105 static int ufs_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
110 if (!old_valid_dev(rdev))
112 inode = ufs_new_inode(dir, mode);
113 err = PTR_ERR(inode);
114 if (!IS_ERR(inode)) {
115 init_special_inode(inode, mode, rdev);
116 /* NOTE: that'll go when we get wide dev_t */
117 ufs_set_inode_dev(inode->i_sb, UFS_I(inode), rdev);
118 mark_inode_dirty(inode);
120 err = ufs_add_nondir(dentry, inode);
126 static int ufs_symlink (struct inode * dir, struct dentry * dentry,
127 const char * symname)
129 struct super_block * sb = dir->i_sb;
130 int err = -ENAMETOOLONG;
131 unsigned l = strlen(symname)+1;
132 struct inode * inode;
134 if (l > sb->s_blocksize)
138 inode = ufs_new_inode(dir, S_IFLNK | S_IRWXUGO);
139 err = PTR_ERR(inode);
143 if (l > UFS_SB(sb)->s_uspi->s_maxsymlinklen) {
145 inode->i_op = &page_symlink_inode_operations;
146 inode->i_mapping->a_ops = &ufs_aops;
147 err = page_symlink(inode, symname, l);
152 inode->i_op = &ufs_fast_symlink_inode_operations;
153 memcpy((char*)&UFS_I(inode)->i_u1.i_data,symname,l);
156 mark_inode_dirty(inode);
158 err = ufs_add_nondir(dentry, inode);
164 inode_dec_link_count(inode);
169 static int ufs_link (struct dentry * old_dentry, struct inode * dir,
170 struct dentry *dentry)
172 struct inode *inode = old_dentry->d_inode;
176 if (inode->i_nlink >= UFS_LINK_MAX) {
181 inode->i_ctime = CURRENT_TIME_SEC;
182 inode_inc_link_count(inode);
183 atomic_inc(&inode->i_count);
185 error = ufs_add_nondir(dentry, inode);
190 static int ufs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
192 struct inode * inode;
195 if (dir->i_nlink >= UFS_LINK_MAX)
199 inode_inc_link_count(dir);
201 inode = ufs_new_inode(dir, S_IFDIR|mode);
202 err = PTR_ERR(inode);
206 inode->i_op = &ufs_dir_inode_operations;
207 inode->i_fop = &ufs_dir_operations;
208 inode->i_mapping->a_ops = &ufs_aops;
210 inode_inc_link_count(inode);
212 err = ufs_make_empty(inode, dir);
216 err = ufs_add_link(dentry, inode);
221 d_instantiate(dentry, inode);
226 inode_dec_link_count(inode);
227 inode_dec_link_count(inode);
230 inode_dec_link_count(dir);
235 static int ufs_unlink(struct inode * dir, struct dentry *dentry)
237 struct inode * inode = dentry->d_inode;
238 struct buffer_head * bh;
239 struct ufs_dir_entry * de;
243 de = ufs_find_entry (dentry, &bh);
247 err = ufs_delete_entry (dir, de, bh);
251 inode->i_ctime = dir->i_ctime;
252 inode_dec_link_count(inode);
259 static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
261 struct inode * inode = dentry->d_inode;
265 if (ufs_empty_dir (inode)) {
266 err = ufs_unlink(dir, dentry);
269 inode_dec_link_count(inode);
270 inode_dec_link_count(dir);
277 static int ufs_rename (struct inode * old_dir, struct dentry * old_dentry,
278 struct inode * new_dir, struct dentry * new_dentry )
280 struct inode *old_inode = old_dentry->d_inode;
281 struct inode *new_inode = new_dentry->d_inode;
282 struct buffer_head *dir_bh = NULL;
283 struct ufs_dir_entry *dir_de = NULL;
284 struct buffer_head *old_bh;
285 struct ufs_dir_entry *old_de;
289 old_de = ufs_find_entry (old_dentry, &old_bh);
293 if (S_ISDIR(old_inode->i_mode)) {
295 dir_de = ufs_dotdot(old_inode, &dir_bh);
301 struct buffer_head *new_bh;
302 struct ufs_dir_entry *new_de;
305 if (dir_de && !ufs_empty_dir (new_inode))
308 new_de = ufs_find_entry (new_dentry, &new_bh);
311 inode_inc_link_count(old_inode);
312 ufs_set_link(new_dir, new_de, new_bh, old_inode);
313 new_inode->i_ctime = CURRENT_TIME_SEC;
315 new_inode->i_nlink--;
316 inode_dec_link_count(new_inode);
320 if (new_dir->i_nlink >= UFS_LINK_MAX)
323 inode_inc_link_count(old_inode);
324 err = ufs_add_link(new_dentry, old_inode);
326 inode_dec_link_count(old_inode);
330 inode_inc_link_count(new_dir);
333 ufs_delete_entry (old_dir, old_de, old_bh);
335 inode_dec_link_count(old_inode);
338 ufs_set_link(old_inode, dir_de, dir_bh, new_dir);
339 inode_dec_link_count(old_dir);
354 struct inode_operations ufs_dir_inode_operations = {
355 .create = ufs_create,
356 .lookup = ufs_lookup,
358 .unlink = ufs_unlink,
359 .symlink = ufs_symlink,
363 .rename = ufs_rename,