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
15 #include <linux/pagemap.h>
16 #include <linux/smp_lock.h>
19 static int add_nondir(struct dentry *dentry, struct inode *inode)
21 int err = sysv_add_link(dentry, inode);
23 d_instantiate(dentry, inode);
26 inode_dec_link_count(inode);
31 static int sysv_hash(struct dentry *dentry, struct qstr *qstr)
33 /* Truncate the name in place, avoids having to define a compare
35 if (qstr->len > SYSV_NAMELEN) {
36 qstr->len = SYSV_NAMELEN;
37 qstr->hash = full_name_hash(qstr->name, qstr->len);
42 struct dentry_operations sysv_dentry_operations = {
46 static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
48 struct inode * inode = NULL;
51 dentry->d_op = dir->i_sb->s_root->d_op;
52 if (dentry->d_name.len > SYSV_NAMELEN)
53 return ERR_PTR(-ENAMETOOLONG);
54 ino = sysv_inode_by_name(dentry);
57 inode = iget(dir->i_sb, ino);
59 return ERR_PTR(-EACCES);
65 static int sysv_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev)
70 if (!old_valid_dev(rdev))
73 inode = sysv_new_inode(dir, mode);
77 sysv_set_inode(inode, rdev);
78 mark_inode_dirty(inode);
79 err = add_nondir(dentry, inode);
84 static int sysv_create(struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
86 return sysv_mknod(dir, dentry, mode, 0);
89 static int sysv_symlink(struct inode * dir, struct dentry * dentry,
92 int err = -ENAMETOOLONG;
93 int l = strlen(symname)+1;
96 if (l > dir->i_sb->s_blocksize)
99 inode = sysv_new_inode(dir, S_IFLNK|0777);
100 err = PTR_ERR(inode);
104 sysv_set_inode(inode, 0);
105 err = page_symlink(inode, symname, l);
109 mark_inode_dirty(inode);
110 err = add_nondir(dentry, inode);
115 inode_dec_link_count(inode);
120 static int sysv_link(struct dentry * old_dentry, struct inode * dir,
121 struct dentry * dentry)
123 struct inode *inode = old_dentry->d_inode;
125 if (inode->i_nlink >= SYSV_SB(inode->i_sb)->s_link_max)
128 inode->i_ctime = CURRENT_TIME_SEC;
129 inode_inc_link_count(inode);
130 atomic_inc(&inode->i_count);
132 return add_nondir(dentry, inode);
135 static int sysv_mkdir(struct inode * dir, struct dentry *dentry, int mode)
137 struct inode * inode;
140 if (dir->i_nlink >= SYSV_SB(dir->i_sb)->s_link_max)
142 inode_inc_link_count(dir);
144 inode = sysv_new_inode(dir, S_IFDIR|mode);
145 err = PTR_ERR(inode);
149 sysv_set_inode(inode, 0);
151 inode_inc_link_count(inode);
153 err = sysv_make_empty(inode, dir);
157 err = sysv_add_link(dentry, inode);
161 d_instantiate(dentry, inode);
166 inode_dec_link_count(inode);
167 inode_dec_link_count(inode);
170 inode_dec_link_count(dir);
174 static int sysv_unlink(struct inode * dir, struct dentry * dentry)
176 struct inode * inode = dentry->d_inode;
178 struct sysv_dir_entry * de;
181 de = sysv_find_entry(dentry, &page);
185 err = sysv_delete_entry (de, page);
189 inode->i_ctime = dir->i_ctime;
190 inode_dec_link_count(inode);
195 static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
197 struct inode *inode = dentry->d_inode;
198 int err = -ENOTEMPTY;
200 if (sysv_empty_dir(inode)) {
201 err = sysv_unlink(dir, dentry);
204 inode_dec_link_count(inode);
205 inode_dec_link_count(dir);
212 * Anybody can rename anything with this: the permission checks are left to the
213 * higher-level routines.
215 static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry,
216 struct inode * new_dir, struct dentry * new_dentry)
218 struct inode * old_inode = old_dentry->d_inode;
219 struct inode * new_inode = new_dentry->d_inode;
220 struct page * dir_page = NULL;
221 struct sysv_dir_entry * dir_de = NULL;
222 struct page * old_page;
223 struct sysv_dir_entry * old_de;
226 old_de = sysv_find_entry(old_dentry, &old_page);
230 if (S_ISDIR(old_inode->i_mode)) {
232 dir_de = sysv_dotdot(old_inode, &dir_page);
238 struct page * new_page;
239 struct sysv_dir_entry * new_de;
242 if (dir_de && !sysv_empty_dir(new_inode))
246 new_de = sysv_find_entry(new_dentry, &new_page);
249 inode_inc_link_count(old_inode);
250 sysv_set_link(new_de, new_page, old_inode);
251 new_inode->i_ctime = CURRENT_TIME_SEC;
253 drop_nlink(new_inode);
254 inode_dec_link_count(new_inode);
258 if (new_dir->i_nlink >= SYSV_SB(new_dir->i_sb)->s_link_max)
261 inode_inc_link_count(old_inode);
262 err = sysv_add_link(new_dentry, old_inode);
264 inode_dec_link_count(old_inode);
268 inode_inc_link_count(new_dir);
271 sysv_delete_entry(old_de, old_page);
272 inode_dec_link_count(old_inode);
275 sysv_set_link(dir_de, dir_page, new_dir);
276 inode_dec_link_count(old_dir);
283 page_cache_release(dir_page);
287 page_cache_release(old_page);
293 * directories can handle most operations...
295 const struct inode_operations sysv_dir_inode_operations = {
296 .create = sysv_create,
297 .lookup = sysv_lookup,
299 .unlink = sysv_unlink,
300 .symlink = sysv_symlink,
304 .rename = sysv_rename,
305 .getattr = sysv_getattr,