blob: b056f02b1fb306c810f4b429ae99417a8f6238e5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ufs/namei.c
3 *
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -07004 * Migration to usage of "page cache" on May 2006 by
5 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 1998
8 * Daniel Pirkl <daniel.pirkl@email.cz>
9 * Charles University, Faculty of Mathematics and Physics
10 *
11 * from
12 *
13 * linux/fs/ext2/namei.c
14 *
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)
19 *
20 * from
21 *
22 * linux/fs/minix/namei.c
23 *
24 * Copyright (C) 1991, 1992 Linus Torvalds
25 *
26 * Big-endian to little-endian byte-swapping/bitmaps by
27 * David S. Miller (davem@caip.rutgers.edu), 1995
28 */
29
30#include <linux/time.h>
31#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/smp_lock.h>
Mike Frysingere5420592008-02-08 04:21:31 -080033
34#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070035#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "util.h"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
39{
40 int err = ufs_add_link(dentry, inode);
41 if (!err) {
42 d_instantiate(dentry, inode);
43 return 0;
44 }
Alexey Dobriyan32575452006-03-23 03:00:53 -080045 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 iput(inode);
47 return err;
48}
49
50static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
51{
52 struct inode * inode = NULL;
53 ino_t ino;
54
55 if (dentry->d_name.len > UFS_MAXNAMLEN)
56 return ERR_PTR(-ENAMETOOLONG);
57
58 lock_kernel();
Alexey Dobriyan08049702009-12-15 16:46:50 -080059 ino = ufs_inode_by_name(dir, &dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (ino) {
David Howellsb55c4602008-02-07 00:15:48 -080061 inode = ufs_iget(dir->i_sb, ino);
62 if (IS_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 unlock_kernel();
David Howellsb55c4602008-02-07 00:15:48 -080064 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66 }
67 unlock_kernel();
68 d_add(dentry, inode);
69 return NULL;
70}
71
72/*
73 * By the time this is called, we already have created
74 * the directory cache entry for the new file, but it
75 * is so far negative - it has no inode.
76 *
77 * If the create succeeds, we fill in the inode information
78 * with d_instantiate().
79 */
80static int ufs_create (struct inode * dir, struct dentry * dentry, int mode,
81 struct nameidata *nd)
82{
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070083 struct inode *inode;
84 int err;
85
86 UFSD("BEGIN\n");
Christoph Hellwig907f4552010-03-03 09:05:06 -050087
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070088 inode = ufs_new_inode(dir, mode);
89 err = PTR_ERR(inode);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (!IS_ERR(inode)) {
92 inode->i_op = &ufs_file_inode_operations;
93 inode->i_fop = &ufs_file_operations;
94 inode->i_mapping->a_ops = &ufs_aops;
95 mark_inode_dirty(inode);
96 lock_kernel();
97 err = ufs_add_nondir(dentry, inode);
98 unlock_kernel();
99 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700100 UFSD("END: err=%d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return err;
102}
103
104static int ufs_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
105{
106 struct inode *inode;
107 int err;
108
109 if (!old_valid_dev(rdev))
110 return -EINVAL;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 inode = ufs_new_inode(dir, mode);
113 err = PTR_ERR(inode);
114 if (!IS_ERR(inode)) {
115 init_special_inode(inode, mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ufs_set_inode_dev(inode->i_sb, UFS_I(inode), rdev);
117 mark_inode_dirty(inode);
118 lock_kernel();
119 err = ufs_add_nondir(dentry, inode);
120 unlock_kernel();
121 }
122 return err;
123}
124
125static int ufs_symlink (struct inode * dir, struct dentry * dentry,
126 const char * symname)
127{
128 struct super_block * sb = dir->i_sb;
129 int err = -ENAMETOOLONG;
130 unsigned l = strlen(symname)+1;
131 struct inode * inode;
132
133 if (l > sb->s_blocksize)
Josh Triplett344fe782006-07-30 03:03:59 -0700134 goto out_notlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 lock_kernel();
137 inode = ufs_new_inode(dir, S_IFLNK | S_IRWXUGO);
138 err = PTR_ERR(inode);
139 if (IS_ERR(inode))
140 goto out;
141
142 if (l > UFS_SB(sb)->s_uspi->s_maxsymlinklen) {
143 /* slow symlink */
Dmitry Monakhov311b9542010-04-15 00:56:58 +0200144 inode->i_op = &ufs_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 inode->i_mapping->a_ops = &ufs_aops;
146 err = page_symlink(inode, symname, l);
147 if (err)
148 goto out_fail;
149 } else {
150 /* fast symlink */
151 inode->i_op = &ufs_fast_symlink_inode_operations;
Duane Griffin723be1f2009-01-08 22:43:51 +0000152 memcpy(UFS_I(inode)->i_u1.i_symlink, symname, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 inode->i_size = l-1;
154 }
155 mark_inode_dirty(inode);
156
157 err = ufs_add_nondir(dentry, inode);
158out:
159 unlock_kernel();
Josh Triplett344fe782006-07-30 03:03:59 -0700160out_notlocked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return err;
162
163out_fail:
Alexey Dobriyan32575452006-03-23 03:00:53 -0800164 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 iput(inode);
166 goto out;
167}
168
169static int ufs_link (struct dentry * old_dentry, struct inode * dir,
170 struct dentry *dentry)
171{
172 struct inode *inode = old_dentry->d_inode;
173 int error;
174
175 lock_kernel();
176 if (inode->i_nlink >= UFS_LINK_MAX) {
177 unlock_kernel();
178 return -EMLINK;
179 }
180
181 inode->i_ctime = CURRENT_TIME_SEC;
Alexey Dobriyan32575452006-03-23 03:00:53 -0800182 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 atomic_inc(&inode->i_count);
184
185 error = ufs_add_nondir(dentry, inode);
186 unlock_kernel();
187 return error;
188}
189
190static int ufs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
191{
192 struct inode * inode;
193 int err = -EMLINK;
194
195 if (dir->i_nlink >= UFS_LINK_MAX)
196 goto out;
197
198 lock_kernel();
Alexey Dobriyan32575452006-03-23 03:00:53 -0800199 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 inode = ufs_new_inode(dir, S_IFDIR|mode);
202 err = PTR_ERR(inode);
203 if (IS_ERR(inode))
204 goto out_dir;
205
206 inode->i_op = &ufs_dir_inode_operations;
207 inode->i_fop = &ufs_dir_operations;
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700208 inode->i_mapping->a_ops = &ufs_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Alexey Dobriyan32575452006-03-23 03:00:53 -0800210 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 err = ufs_make_empty(inode, dir);
213 if (err)
214 goto out_fail;
215
216 err = ufs_add_link(dentry, inode);
217 if (err)
218 goto out_fail;
219 unlock_kernel();
220
221 d_instantiate(dentry, inode);
222out:
223 return err;
224
225out_fail:
Alexey Dobriyan32575452006-03-23 03:00:53 -0800226 inode_dec_link_count(inode);
227 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 iput (inode);
229out_dir:
Alexey Dobriyan32575452006-03-23 03:00:53 -0800230 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unlock_kernel();
232 goto out;
233}
234
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700235static int ufs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct inode * inode = dentry->d_inode;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700238 struct ufs_dir_entry *de;
239 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 int err = -ENOENT;
241
Alexey Dobriyan08049702009-12-15 16:46:50 -0800242 de = ufs_find_entry(dir, &dentry->d_name, &page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!de)
244 goto out;
245
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700246 err = ufs_delete_entry(dir, de, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (err)
248 goto out;
249
250 inode->i_ctime = dir->i_ctime;
Alexey Dobriyan32575452006-03-23 03:00:53 -0800251 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 err = 0;
253out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return err;
255}
256
257static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
258{
259 struct inode * inode = dentry->d_inode;
260 int err= -ENOTEMPTY;
261
262 lock_kernel();
263 if (ufs_empty_dir (inode)) {
264 err = ufs_unlink(dir, dentry);
265 if (!err) {
266 inode->i_size = 0;
Alexey Dobriyan32575452006-03-23 03:00:53 -0800267 inode_dec_link_count(inode);
268 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 }
271 unlock_kernel();
272 return err;
273}
274
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700275static int ufs_rename(struct inode *old_dir, struct dentry *old_dentry,
276 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct inode *old_inode = old_dentry->d_inode;
279 struct inode *new_inode = new_dentry->d_inode;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700280 struct page *dir_page = NULL;
281 struct ufs_dir_entry * dir_de = NULL;
282 struct page *old_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct ufs_dir_entry *old_de;
284 int err = -ENOENT;
285
Alexey Dobriyan08049702009-12-15 16:46:50 -0800286 old_de = ufs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!old_de)
288 goto out;
289
290 if (S_ISDIR(old_inode->i_mode)) {
291 err = -EIO;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700292 dir_de = ufs_dotdot(old_inode, &dir_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (!dir_de)
294 goto out_old;
295 }
296
297 if (new_inode) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700298 struct page *new_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct ufs_dir_entry *new_de;
300
301 err = -ENOTEMPTY;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700302 if (dir_de && !ufs_empty_dir(new_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 goto out_dir;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 err = -ENOENT;
Alexey Dobriyan08049702009-12-15 16:46:50 -0800306 new_de = ufs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!new_de)
308 goto out_dir;
Alexey Dobriyan32575452006-03-23 03:00:53 -0800309 inode_inc_link_count(old_inode);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700310 ufs_set_link(new_dir, new_de, new_page, old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 new_inode->i_ctime = CURRENT_TIME_SEC;
312 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700313 drop_nlink(new_inode);
Alexey Dobriyan32575452006-03-23 03:00:53 -0800314 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 } else {
316 if (dir_de) {
317 err = -EMLINK;
318 if (new_dir->i_nlink >= UFS_LINK_MAX)
319 goto out_dir;
320 }
Alexey Dobriyan32575452006-03-23 03:00:53 -0800321 inode_inc_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 err = ufs_add_link(new_dentry, old_inode);
323 if (err) {
Alexey Dobriyan32575452006-03-23 03:00:53 -0800324 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto out_dir;
326 }
327 if (dir_de)
Alexey Dobriyan32575452006-03-23 03:00:53 -0800328 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700331 /*
332 * Like most other Unix systems, set the ctime for inodes on a
333 * rename.
334 * inode_dec_link_count() will mark the inode dirty.
335 */
336 old_inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700338 ufs_delete_entry(old_dir, old_de, old_page);
Alexey Dobriyan32575452006-03-23 03:00:53 -0800339 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (dir_de) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700342 ufs_set_link(old_inode, dir_de, dir_page, new_dir);
Alexey Dobriyan32575452006-03-23 03:00:53 -0800343 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348out_dir:
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700349 if (dir_de) {
350 kunmap(dir_page);
351 page_cache_release(dir_page);
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353out_old:
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700354 kunmap(old_page);
355 page_cache_release(old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return err;
358}
359
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800360const struct inode_operations ufs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 .create = ufs_create,
362 .lookup = ufs_lookup,
363 .link = ufs_link,
364 .unlink = ufs_unlink,
365 .symlink = ufs_symlink,
366 .mkdir = ufs_mkdir,
367 .rmdir = ufs_rmdir,
368 .mknod = ufs_mknod,
369 .rename = ufs_rename,
370};