2 * linux/fs/sysv/namei.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Copyright (C) 1993 Pascal Haible, Bruno Haible
11 * Copyright (C) 1993 Bruno Haible
12 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
16 #include <linux/sysv_fs.h>
17 #include <linux/pagemap.h>
19 static inline void inc_count(struct inode *inode)
22 mark_inode_dirty(inode);
25 static inline void dec_count(struct inode *inode)
28 mark_inode_dirty(inode);
31 static int add_nondir(struct dentry *dentry, struct inode *inode)
33 int err = sysv_add_link(dentry, inode);
35 d_instantiate(dentry, inode);
43 static int sysv_hash(struct dentry *dentry, struct qstr *qstr)
47 const unsigned char *name;
52 /* Truncate the name in place, avoids having to define a compare
56 hash = init_name_hash();
58 hash = partial_name_hash(*name++, hash);
59 qstr->hash = end_name_hash(hash);
63 struct dentry_operations sysv_dentry_operations = {
67 static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry)
69 struct inode * inode = NULL;
72 dentry->d_op = dir->i_sb->s_root->d_op;
73 if (dentry->d_name.len > SYSV_NAMELEN)
74 return ERR_PTR(-ENAMETOOLONG);
75 ino = sysv_inode_by_name(dentry);
78 inode = iget(dir->i_sb, ino);
80 return ERR_PTR(-EACCES);
86 static int sysv_mknod(struct inode * dir, struct dentry * dentry, int mode, int rdev)
88 struct inode * inode = sysv_new_inode(dir, mode);
89 int err = PTR_ERR(inode);
92 sysv_set_inode(inode, rdev);
93 mark_inode_dirty(inode);
94 err = add_nondir(dentry, inode);
99 static int sysv_create(struct inode * dir, struct dentry * dentry, int mode)
101 return sysv_mknod(dir, dentry, mode, 0);
104 static int sysv_symlink(struct inode * dir, struct dentry * dentry,
105 const char * symname)
107 int err = -ENAMETOOLONG;
108 int l = strlen(symname)+1;
109 struct inode * inode;
111 if (l > dir->i_sb->s_blocksize)
114 inode = sysv_new_inode(dir, S_IFLNK|0777);
115 err = PTR_ERR(inode);
119 sysv_set_inode(inode, 0);
120 err = block_symlink(inode, symname, l);
124 mark_inode_dirty(inode);
125 err = add_nondir(dentry, inode);
135 static int sysv_link(struct dentry * old_dentry, struct inode * dir,
136 struct dentry * dentry)
138 struct inode *inode = old_dentry->d_inode;
140 if (S_ISDIR(inode->i_mode))
143 if (inode->i_nlink >= inode->i_sb->sv_link_max)
146 inode->i_ctime = CURRENT_TIME;
148 atomic_inc(&inode->i_count);
150 return add_nondir(dentry, inode);
153 static int sysv_mkdir(struct inode * dir, struct dentry *dentry, int mode)
155 struct inode * inode;
158 if (dir->i_nlink >= dir->i_sb->sv_link_max)
162 inode = sysv_new_inode(dir, S_IFDIR|mode);
163 err = PTR_ERR(inode);
167 sysv_set_inode(inode, 0);
171 err = sysv_make_empty(inode, dir);
175 err = sysv_add_link(dentry, inode);
179 d_instantiate(dentry, inode);
192 static int sysv_unlink(struct inode * dir, struct dentry * dentry)
194 struct inode * inode = dentry->d_inode;
196 struct sysv_dir_entry * de;
199 de = sysv_find_entry(dentry, &page);
203 err = sysv_delete_entry (de, page);
207 inode->i_ctime = dir->i_ctime;
213 static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
215 struct inode *inode = dentry->d_inode;
216 int err = -ENOTEMPTY;
218 if (sysv_empty_dir(inode)) {
219 err = sysv_unlink(dir, dentry);
230 * Anybody can rename anything with this: the permission checks are left to the
231 * higher-level routines.
233 static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry,
234 struct inode * new_dir, struct dentry * new_dentry)
236 struct inode * old_inode = old_dentry->d_inode;
237 struct inode * new_inode = new_dentry->d_inode;
238 struct page * dir_page = NULL;
239 struct sysv_dir_entry * dir_de = NULL;
240 struct page * old_page;
241 struct sysv_dir_entry * old_de;
244 old_de = sysv_find_entry(old_dentry, &old_page);
248 if (S_ISDIR(old_inode->i_mode)) {
250 dir_de = sysv_dotdot(old_inode, &dir_page);
256 struct page * new_page;
257 struct sysv_dir_entry * new_de;
260 if (dir_de && !sysv_empty_dir(new_inode))
264 new_de = sysv_find_entry(new_dentry, &new_page);
267 inc_count(old_inode);
268 sysv_set_link(new_de, new_page, old_inode);
269 new_inode->i_ctime = CURRENT_TIME;
271 new_inode->i_nlink--;
272 dec_count(new_inode);
276 if (new_dir->i_nlink >= new_dir->i_sb->sv_link_max)
279 inc_count(old_inode);
280 err = sysv_add_link(new_dentry, old_inode);
282 dec_count(old_inode);
289 sysv_delete_entry(old_de, old_page);
290 dec_count(old_inode);
293 sysv_set_link(dir_de, dir_page, new_dir);
301 page_cache_release(dir_page);
305 page_cache_release(old_page);
311 * directories can handle most operations...
313 struct inode_operations sysv_dir_inode_operations = {
318 symlink: sysv_symlink,