Merge master.kernel.org:/pub/scm/linux/kernel/git/perex/alsa
[wrt350n-kernel.git] / fs / sysv / namei.c
blobb8a73f716fbe35c5a0683017db96fc3c2d1136cd
1 /*
2 * linux/fs/sysv/namei.c
4 * minix/namei.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * coh/namei.c
8 * Copyright (C) 1993 Pascal Haible, Bruno Haible
10 * sysv/namei.c
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>
17 #include "sysv.h"
19 static int add_nondir(struct dentry *dentry, struct inode *inode)
21 int err = sysv_add_link(dentry, inode);
22 if (!err) {
23 d_instantiate(dentry, inode);
24 return 0;
26 inode_dec_link_count(inode);
27 iput(inode);
28 return err;
31 static int sysv_hash(struct dentry *dentry, struct qstr *qstr)
33 /* Truncate the name in place, avoids having to define a compare
34 function. */
35 if (qstr->len > SYSV_NAMELEN) {
36 qstr->len = SYSV_NAMELEN;
37 qstr->hash = full_name_hash(qstr->name, qstr->len);
39 return 0;
42 struct dentry_operations sysv_dentry_operations = {
43 .d_hash = sysv_hash,
46 static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
48 struct inode * inode = NULL;
49 ino_t ino;
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);
56 if (ino) {
57 inode = iget(dir->i_sb, ino);
58 if (!inode)
59 return ERR_PTR(-EACCES);
61 d_add(dentry, inode);
62 return NULL;
65 static int sysv_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev)
67 struct inode * inode;
68 int err;
70 if (!old_valid_dev(rdev))
71 return -EINVAL;
73 inode = sysv_new_inode(dir, mode);
74 err = PTR_ERR(inode);
76 if (!IS_ERR(inode)) {
77 sysv_set_inode(inode, rdev);
78 mark_inode_dirty(inode);
79 err = add_nondir(dentry, inode);
81 return err;
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,
90 const char * symname)
92 int err = -ENAMETOOLONG;
93 int l = strlen(symname)+1;
94 struct inode * inode;
96 if (l > dir->i_sb->s_blocksize)
97 goto out;
99 inode = sysv_new_inode(dir, S_IFLNK|0777);
100 err = PTR_ERR(inode);
101 if (IS_ERR(inode))
102 goto out;
104 sysv_set_inode(inode, 0);
105 err = page_symlink(inode, symname, l);
106 if (err)
107 goto out_fail;
109 mark_inode_dirty(inode);
110 err = add_nondir(dentry, inode);
111 out:
112 return err;
114 out_fail:
115 inode_dec_link_count(inode);
116 iput(inode);
117 goto out;
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)
126 return -EMLINK;
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;
138 int err = -EMLINK;
140 if (dir->i_nlink >= SYSV_SB(dir->i_sb)->s_link_max)
141 goto out;
142 inode_inc_link_count(dir);
144 inode = sysv_new_inode(dir, S_IFDIR|mode);
145 err = PTR_ERR(inode);
146 if (IS_ERR(inode))
147 goto out_dir;
149 sysv_set_inode(inode, 0);
151 inode_inc_link_count(inode);
153 err = sysv_make_empty(inode, dir);
154 if (err)
155 goto out_fail;
157 err = sysv_add_link(dentry, inode);
158 if (err)
159 goto out_fail;
161 d_instantiate(dentry, inode);
162 out:
163 return err;
165 out_fail:
166 inode_dec_link_count(inode);
167 inode_dec_link_count(inode);
168 iput(inode);
169 out_dir:
170 inode_dec_link_count(dir);
171 goto out;
174 static int sysv_unlink(struct inode * dir, struct dentry * dentry)
176 struct inode * inode = dentry->d_inode;
177 struct page * page;
178 struct sysv_dir_entry * de;
179 int err = -ENOENT;
181 de = sysv_find_entry(dentry, &page);
182 if (!de)
183 goto out;
185 err = sysv_delete_entry (de, page);
186 if (err)
187 goto out;
189 inode->i_ctime = dir->i_ctime;
190 inode_dec_link_count(inode);
191 out:
192 return err;
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);
202 if (!err) {
203 inode->i_size = 0;
204 inode_dec_link_count(inode);
205 inode_dec_link_count(dir);
208 return err;
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;
224 int err = -ENOENT;
226 old_de = sysv_find_entry(old_dentry, &old_page);
227 if (!old_de)
228 goto out;
230 if (S_ISDIR(old_inode->i_mode)) {
231 err = -EIO;
232 dir_de = sysv_dotdot(old_inode, &dir_page);
233 if (!dir_de)
234 goto out_old;
237 if (new_inode) {
238 struct page * new_page;
239 struct sysv_dir_entry * new_de;
241 err = -ENOTEMPTY;
242 if (dir_de && !sysv_empty_dir(new_inode))
243 goto out_dir;
245 err = -ENOENT;
246 new_de = sysv_find_entry(new_dentry, &new_page);
247 if (!new_de)
248 goto out_dir;
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;
252 if (dir_de)
253 new_inode->i_nlink--;
254 inode_dec_link_count(new_inode);
255 } else {
256 if (dir_de) {
257 err = -EMLINK;
258 if (new_dir->i_nlink >= SYSV_SB(new_dir->i_sb)->s_link_max)
259 goto out_dir;
261 inode_inc_link_count(old_inode);
262 err = sysv_add_link(new_dentry, old_inode);
263 if (err) {
264 inode_dec_link_count(old_inode);
265 goto out_dir;
267 if (dir_de)
268 inode_inc_link_count(new_dir);
271 sysv_delete_entry(old_de, old_page);
272 inode_dec_link_count(old_inode);
274 if (dir_de) {
275 sysv_set_link(dir_de, dir_page, new_dir);
276 inode_dec_link_count(old_dir);
278 return 0;
280 out_dir:
281 if (dir_de) {
282 kunmap(dir_page);
283 page_cache_release(dir_page);
285 out_old:
286 kunmap(old_page);
287 page_cache_release(old_page);
288 out:
289 return err;
293 * directories can handle most operations...
295 struct inode_operations sysv_dir_inode_operations = {
296 .create = sysv_create,
297 .lookup = sysv_lookup,
298 .link = sysv_link,
299 .unlink = sysv_unlink,
300 .symlink = sysv_symlink,
301 .mkdir = sysv_mkdir,
302 .rmdir = sysv_rmdir,
303 .mknod = sysv_mknod,
304 .rename = sysv_rename,
305 .getattr = sysv_getattr,