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;
209 inode_inc_link_count(inode);
211 err = ufs_make_empty(inode, dir);
215 err = ufs_add_link(dentry, inode);
220 d_instantiate(dentry, inode);
225 inode_dec_link_count(inode);
226 inode_dec_link_count(inode);
229 inode_dec_link_count(dir);
234 static int ufs_unlink(struct inode * dir, struct dentry *dentry)
236 struct inode * inode = dentry->d_inode;
237 struct buffer_head * bh;
238 struct ufs_dir_entry * de;
242 de = ufs_find_entry (dentry, &bh);
246 err = ufs_delete_entry (dir, de, bh);
250 inode->i_ctime = dir->i_ctime;
251 inode_dec_link_count(inode);
258 static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
260 struct inode * inode = dentry->d_inode;
264 if (ufs_empty_dir (inode)) {
265 err = ufs_unlink(dir, dentry);
268 inode_dec_link_count(inode);
269 inode_dec_link_count(dir);
276 static int ufs_rename (struct inode * old_dir, struct dentry * old_dentry,
277 struct inode * new_dir, struct dentry * new_dentry )
279 struct inode *old_inode = old_dentry->d_inode;
280 struct inode *new_inode = new_dentry->d_inode;
281 struct buffer_head *dir_bh = NULL;
282 struct ufs_dir_entry *dir_de = NULL;
283 struct buffer_head *old_bh;
284 struct ufs_dir_entry *old_de;
288 old_de = ufs_find_entry (old_dentry, &old_bh);
292 if (S_ISDIR(old_inode->i_mode)) {
294 dir_de = ufs_dotdot(old_inode, &dir_bh);
300 struct buffer_head *new_bh;
301 struct ufs_dir_entry *new_de;
304 if (dir_de && !ufs_empty_dir (new_inode))
307 new_de = ufs_find_entry (new_dentry, &new_bh);
310 inode_inc_link_count(old_inode);
311 ufs_set_link(new_dir, new_de, new_bh, old_inode);
312 new_inode->i_ctime = CURRENT_TIME_SEC;
314 new_inode->i_nlink--;
315 inode_dec_link_count(new_inode);
319 if (new_dir->i_nlink >= UFS_LINK_MAX)
322 inode_inc_link_count(old_inode);
323 err = ufs_add_link(new_dentry, old_inode);
325 inode_dec_link_count(old_inode);
329 inode_inc_link_count(new_dir);
332 ufs_delete_entry (old_dir, old_de, old_bh);
334 inode_dec_link_count(old_inode);
337 ufs_set_link(old_inode, dir_de, dir_bh, new_dir);
338 inode_dec_link_count(old_dir);
353 struct inode_operations ufs_dir_inode_operations = {
354 .create = ufs_create,
355 .lookup = ufs_lookup,
357 .unlink = ufs_unlink,
358 .symlink = ufs_symlink,
362 .rename = ufs_rename,