Blackfin: bfin_sport: unify & standardize SPORT masks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ufs / namei.c
blob118556243e7ab60bd2f58aa1f809ee26336f107e
1 /*
2 * linux/fs/ufs/namei.c
4 * Migration to usage of "page cache" on May 2006 by
5 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
7 * Copyright (C) 1998
8 * Daniel Pirkl <daniel.pirkl@email.cz>
9 * Charles University, Faculty of Mathematics and Physics
11 * from
13 * linux/fs/ext2/namei.c
15 * Copyright (C) 1992, 1993, 1994, 1995
16 * Remy Card (card@masi.ibp.fr)
17 * Laboratoire MASI - Institut Blaise Pascal
18 * Universite Pierre et Marie Curie (Paris VI)
20 * from
22 * linux/fs/minix/namei.c
24 * Copyright (C) 1991, 1992 Linus Torvalds
26 * Big-endian to little-endian byte-swapping/bitmaps by
27 * David S. Miller (davem@caip.rutgers.edu), 1995
30 #include <linux/time.h>
31 #include <linux/fs.h>
32 #include <linux/smp_lock.h>
33 #include <linux/quotaops.h>
35 #include "ufs_fs.h"
36 #include "ufs.h"
37 #include "util.h"
39 static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
41 int err = ufs_add_link(dentry, inode);
42 if (!err) {
43 d_instantiate(dentry, inode);
44 return 0;
46 inode_dec_link_count(inode);
47 iput(inode);
48 return err;
51 static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
53 struct inode * inode = NULL;
54 ino_t ino;
56 if (dentry->d_name.len > UFS_MAXNAMLEN)
57 return ERR_PTR(-ENAMETOOLONG);
59 lock_kernel();
60 ino = ufs_inode_by_name(dir, &dentry->d_name);
61 if (ino) {
62 inode = ufs_iget(dir->i_sb, ino);
63 if (IS_ERR(inode)) {
64 unlock_kernel();
65 return ERR_CAST(inode);
68 unlock_kernel();
69 d_add(dentry, inode);
70 return NULL;
74 * By the time this is called, we already have created
75 * the directory cache entry for the new file, but it
76 * is so far negative - it has no inode.
78 * If the create succeeds, we fill in the inode information
79 * with d_instantiate().
81 static int ufs_create (struct inode * dir, struct dentry * dentry, int mode,
82 struct nameidata *nd)
84 struct inode *inode;
85 int err;
87 UFSD("BEGIN\n");
89 dquot_initialize(dir);
91 inode = ufs_new_inode(dir, mode);
92 err = PTR_ERR(inode);
94 if (!IS_ERR(inode)) {
95 inode->i_op = &ufs_file_inode_operations;
96 inode->i_fop = &ufs_file_operations;
97 inode->i_mapping->a_ops = &ufs_aops;
98 mark_inode_dirty(inode);
99 lock_kernel();
100 err = ufs_add_nondir(dentry, inode);
101 unlock_kernel();
103 UFSD("END: err=%d\n", err);
104 return err;
107 static int ufs_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
109 struct inode *inode;
110 int err;
112 if (!old_valid_dev(rdev))
113 return -EINVAL;
115 dquot_initialize(dir);
117 inode = ufs_new_inode(dir, mode);
118 err = PTR_ERR(inode);
119 if (!IS_ERR(inode)) {
120 init_special_inode(inode, mode, rdev);
121 ufs_set_inode_dev(inode->i_sb, UFS_I(inode), rdev);
122 mark_inode_dirty(inode);
123 lock_kernel();
124 err = ufs_add_nondir(dentry, inode);
125 unlock_kernel();
127 return err;
130 static int ufs_symlink (struct inode * dir, struct dentry * dentry,
131 const char * symname)
133 struct super_block * sb = dir->i_sb;
134 int err = -ENAMETOOLONG;
135 unsigned l = strlen(symname)+1;
136 struct inode * inode;
138 if (l > sb->s_blocksize)
139 goto out_notlocked;
141 dquot_initialize(dir);
143 lock_kernel();
144 inode = ufs_new_inode(dir, S_IFLNK | S_IRWXUGO);
145 err = PTR_ERR(inode);
146 if (IS_ERR(inode))
147 goto out;
149 if (l > UFS_SB(sb)->s_uspi->s_maxsymlinklen) {
150 /* slow symlink */
151 inode->i_op = &page_symlink_inode_operations;
152 inode->i_mapping->a_ops = &ufs_aops;
153 err = page_symlink(inode, symname, l);
154 if (err)
155 goto out_fail;
156 } else {
157 /* fast symlink */
158 inode->i_op = &ufs_fast_symlink_inode_operations;
159 memcpy(UFS_I(inode)->i_u1.i_symlink, symname, l);
160 inode->i_size = l-1;
162 mark_inode_dirty(inode);
164 err = ufs_add_nondir(dentry, inode);
165 out:
166 unlock_kernel();
167 out_notlocked:
168 return err;
170 out_fail:
171 inode_dec_link_count(inode);
172 iput(inode);
173 goto out;
176 static int ufs_link (struct dentry * old_dentry, struct inode * dir,
177 struct dentry *dentry)
179 struct inode *inode = old_dentry->d_inode;
180 int error;
182 lock_kernel();
183 if (inode->i_nlink >= UFS_LINK_MAX) {
184 unlock_kernel();
185 return -EMLINK;
188 dquot_initialize(dir);
190 inode->i_ctime = CURRENT_TIME_SEC;
191 inode_inc_link_count(inode);
192 atomic_inc(&inode->i_count);
194 error = ufs_add_nondir(dentry, inode);
195 unlock_kernel();
196 return error;
199 static int ufs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
201 struct inode * inode;
202 int err = -EMLINK;
204 if (dir->i_nlink >= UFS_LINK_MAX)
205 goto out;
207 dquot_initialize(dir);
209 lock_kernel();
210 inode_inc_link_count(dir);
212 inode = ufs_new_inode(dir, S_IFDIR|mode);
213 err = PTR_ERR(inode);
214 if (IS_ERR(inode))
215 goto out_dir;
217 inode->i_op = &ufs_dir_inode_operations;
218 inode->i_fop = &ufs_dir_operations;
219 inode->i_mapping->a_ops = &ufs_aops;
221 inode_inc_link_count(inode);
223 err = ufs_make_empty(inode, dir);
224 if (err)
225 goto out_fail;
227 err = ufs_add_link(dentry, inode);
228 if (err)
229 goto out_fail;
230 unlock_kernel();
232 d_instantiate(dentry, inode);
233 out:
234 return err;
236 out_fail:
237 inode_dec_link_count(inode);
238 inode_dec_link_count(inode);
239 iput (inode);
240 out_dir:
241 inode_dec_link_count(dir);
242 unlock_kernel();
243 goto out;
246 static int ufs_unlink(struct inode *dir, struct dentry *dentry)
248 struct inode * inode = dentry->d_inode;
249 struct ufs_dir_entry *de;
250 struct page *page;
251 int err = -ENOENT;
253 dquot_initialize(dir);
255 de = ufs_find_entry(dir, &dentry->d_name, &page);
256 if (!de)
257 goto out;
259 err = ufs_delete_entry(dir, de, page);
260 if (err)
261 goto out;
263 inode->i_ctime = dir->i_ctime;
264 inode_dec_link_count(inode);
265 err = 0;
266 out:
267 return err;
270 static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
272 struct inode * inode = dentry->d_inode;
273 int err= -ENOTEMPTY;
275 lock_kernel();
276 if (ufs_empty_dir (inode)) {
277 err = ufs_unlink(dir, dentry);
278 if (!err) {
279 inode->i_size = 0;
280 inode_dec_link_count(inode);
281 inode_dec_link_count(dir);
284 unlock_kernel();
285 return err;
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 page *dir_page = NULL;
294 struct ufs_dir_entry * dir_de = NULL;
295 struct page *old_page;
296 struct ufs_dir_entry *old_de;
297 int err = -ENOENT;
299 dquot_initialize(old_dir);
300 dquot_initialize(new_dir);
302 old_de = ufs_find_entry(old_dir, &old_dentry->d_name, &old_page);
303 if (!old_de)
304 goto out;
306 if (S_ISDIR(old_inode->i_mode)) {
307 err = -EIO;
308 dir_de = ufs_dotdot(old_inode, &dir_page);
309 if (!dir_de)
310 goto out_old;
313 if (new_inode) {
314 struct page *new_page;
315 struct ufs_dir_entry *new_de;
317 err = -ENOTEMPTY;
318 if (dir_de && !ufs_empty_dir(new_inode))
319 goto out_dir;
321 err = -ENOENT;
322 new_de = ufs_find_entry(new_dir, &new_dentry->d_name, &new_page);
323 if (!new_de)
324 goto out_dir;
325 inode_inc_link_count(old_inode);
326 ufs_set_link(new_dir, new_de, new_page, old_inode);
327 new_inode->i_ctime = CURRENT_TIME_SEC;
328 if (dir_de)
329 drop_nlink(new_inode);
330 inode_dec_link_count(new_inode);
331 } else {
332 if (dir_de) {
333 err = -EMLINK;
334 if (new_dir->i_nlink >= UFS_LINK_MAX)
335 goto out_dir;
337 inode_inc_link_count(old_inode);
338 err = ufs_add_link(new_dentry, old_inode);
339 if (err) {
340 inode_dec_link_count(old_inode);
341 goto out_dir;
343 if (dir_de)
344 inode_inc_link_count(new_dir);
348 * Like most other Unix systems, set the ctime for inodes on a
349 * rename.
350 * inode_dec_link_count() will mark the inode dirty.
352 old_inode->i_ctime = CURRENT_TIME_SEC;
354 ufs_delete_entry(old_dir, old_de, old_page);
355 inode_dec_link_count(old_inode);
357 if (dir_de) {
358 ufs_set_link(old_inode, dir_de, dir_page, new_dir);
359 inode_dec_link_count(old_dir);
361 return 0;
364 out_dir:
365 if (dir_de) {
366 kunmap(dir_page);
367 page_cache_release(dir_page);
369 out_old:
370 kunmap(old_page);
371 page_cache_release(old_page);
372 out:
373 return err;
376 const struct inode_operations ufs_dir_inode_operations = {
377 .create = ufs_create,
378 .lookup = ufs_lookup,
379 .link = ufs_link,
380 .unlink = ufs_unlink,
381 .symlink = ufs_symlink,
382 .mkdir = ufs_mkdir,
383 .rmdir = ufs_rmdir,
384 .mknod = ufs_mknod,
385 .rename = ufs_rename,