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 void ufs_inc_count(struct inode *inode)
49 mark_inode_dirty(inode);
52 static inline void ufs_dec_count(struct inode *inode)
55 mark_inode_dirty(inode);
58 static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
60 int err = ufs_add_link(dentry, inode);
62 d_instantiate(dentry, inode);
70 static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
72 struct inode * inode = NULL;
75 if (dentry->d_name.len > UFS_MAXNAMLEN)
76 return ERR_PTR(-ENAMETOOLONG);
79 ino = ufs_inode_by_name(dir, dentry);
81 inode = iget(dir->i_sb, ino);
84 return ERR_PTR(-EACCES);
93 * By the time this is called, we already have created
94 * the directory cache entry for the new file, but it
95 * is so far negative - it has no inode.
97 * If the create succeeds, we fill in the inode information
98 * with d_instantiate().
100 static int ufs_create (struct inode * dir, struct dentry * dentry, int mode,
101 struct nameidata *nd)
103 struct inode * inode = ufs_new_inode(dir, mode);
104 int err = PTR_ERR(inode);
105 if (!IS_ERR(inode)) {
106 inode->i_op = &ufs_file_inode_operations;
107 inode->i_fop = &ufs_file_operations;
108 inode->i_mapping->a_ops = &ufs_aops;
109 mark_inode_dirty(inode);
111 err = ufs_add_nondir(dentry, inode);
117 static int ufs_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
122 if (!old_valid_dev(rdev))
124 inode = ufs_new_inode(dir, mode);
125 err = PTR_ERR(inode);
126 if (!IS_ERR(inode)) {
127 init_special_inode(inode, mode, rdev);
128 /* NOTE: that'll go when we get wide dev_t */
129 ufs_set_inode_dev(inode->i_sb, UFS_I(inode), rdev);
130 mark_inode_dirty(inode);
132 err = ufs_add_nondir(dentry, inode);
138 static int ufs_symlink (struct inode * dir, struct dentry * dentry,
139 const char * symname)
141 struct super_block * sb = dir->i_sb;
142 int err = -ENAMETOOLONG;
143 unsigned l = strlen(symname)+1;
144 struct inode * inode;
146 if (l > sb->s_blocksize)
150 inode = ufs_new_inode(dir, S_IFLNK | S_IRWXUGO);
151 err = PTR_ERR(inode);
155 if (l > UFS_SB(sb)->s_uspi->s_maxsymlinklen) {
157 inode->i_op = &page_symlink_inode_operations;
158 inode->i_mapping->a_ops = &ufs_aops;
159 err = page_symlink(inode, symname, l);
164 inode->i_op = &ufs_fast_symlink_inode_operations;
165 memcpy((char*)&UFS_I(inode)->i_u1.i_data,symname,l);
168 mark_inode_dirty(inode);
170 err = ufs_add_nondir(dentry, inode);
176 ufs_dec_count(inode);
181 static int ufs_link (struct dentry * old_dentry, struct inode * dir,
182 struct dentry *dentry)
184 struct inode *inode = old_dentry->d_inode;
188 if (inode->i_nlink >= UFS_LINK_MAX) {
193 inode->i_ctime = CURRENT_TIME_SEC;
194 ufs_inc_count(inode);
195 atomic_inc(&inode->i_count);
197 error = ufs_add_nondir(dentry, inode);
202 static int ufs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
204 struct inode * inode;
207 if (dir->i_nlink >= UFS_LINK_MAX)
213 inode = ufs_new_inode(dir, S_IFDIR|mode);
214 err = PTR_ERR(inode);
218 inode->i_op = &ufs_dir_inode_operations;
219 inode->i_fop = &ufs_dir_operations;
221 ufs_inc_count(inode);
223 err = ufs_make_empty(inode, dir);
227 err = ufs_add_link(dentry, inode);
232 d_instantiate(dentry, inode);
237 ufs_dec_count(inode);
238 ufs_dec_count(inode);
246 static int ufs_unlink(struct inode * dir, struct dentry *dentry)
248 struct inode * inode = dentry->d_inode;
249 struct buffer_head * bh;
250 struct ufs_dir_entry * de;
254 de = ufs_find_entry (dentry, &bh);
258 err = ufs_delete_entry (dir, de, bh);
262 inode->i_ctime = dir->i_ctime;
263 ufs_dec_count(inode);
270 static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
272 struct inode * inode = dentry->d_inode;
276 if (ufs_empty_dir (inode)) {
277 err = ufs_unlink(dir, dentry);
280 ufs_dec_count(inode);
288 static int ufs_rename (struct inode * old_dir, struct dentry * old_dentry,
289 struct inode * new_dir, struct dentry * new_dentry )
291 struct inode *old_inode = old_dentry->d_inode;
292 struct inode *new_inode = new_dentry->d_inode;
293 struct buffer_head *dir_bh = NULL;
294 struct ufs_dir_entry *dir_de = NULL;
295 struct buffer_head *old_bh;
296 struct ufs_dir_entry *old_de;
300 old_de = ufs_find_entry (old_dentry, &old_bh);
304 if (S_ISDIR(old_inode->i_mode)) {
306 dir_de = ufs_dotdot(old_inode, &dir_bh);
312 struct buffer_head *new_bh;
313 struct ufs_dir_entry *new_de;
316 if (dir_de && !ufs_empty_dir (new_inode))
319 new_de = ufs_find_entry (new_dentry, &new_bh);
322 ufs_inc_count(old_inode);
323 ufs_set_link(new_dir, new_de, new_bh, old_inode);
324 new_inode->i_ctime = CURRENT_TIME_SEC;
326 new_inode->i_nlink--;
327 ufs_dec_count(new_inode);
331 if (new_dir->i_nlink >= UFS_LINK_MAX)
334 ufs_inc_count(old_inode);
335 err = ufs_add_link(new_dentry, old_inode);
337 ufs_dec_count(old_inode);
341 ufs_inc_count(new_dir);
344 ufs_delete_entry (old_dir, old_de, old_bh);
346 ufs_dec_count(old_inode);
349 ufs_set_link(old_inode, dir_de, dir_bh, new_dir);
350 ufs_dec_count(old_dir);
365 struct inode_operations ufs_dir_inode_operations = {
366 .create = ufs_create,
367 .lookup = ufs_lookup,
369 .unlink = ufs_unlink,
370 .symlink = ufs_symlink,
374 .rename = ufs_rename,