blob: 118556243e7ab60bd2f58aa1f809ee26336f107e [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>
Christoph Hellwig907f4552010-03-03 09:05:06 -050033#include <linux/quotaops.h>
Mike Frysingere5420592008-02-08 04:21:31 -080034
35#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070036#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "util.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
40{
41 int err = ufs_add_link(dentry, inode);
42 if (!err) {
43 d_instantiate(dentry, inode);
44 return 0;
45 }
Alexey Dobriyan32575452006-03-23 03:00:53 -080046 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 iput(inode);
48 return err;
49}
50
51static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
52{
53 struct inode * inode = NULL;
54 ino_t ino;
55
56 if (dentry->d_name.len > UFS_MAXNAMLEN)
57 return ERR_PTR(-ENAMETOOLONG);
58
59 lock_kernel();
Alexey Dobriyan08049702009-12-15 16:46:50 -080060 ino = ufs_inode_by_name(dir, &dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (ino) {
David Howellsb55c4602008-02-07 00:15:48 -080062 inode = ufs_iget(dir->i_sb, ino);
63 if (IS_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 unlock_kernel();
David Howellsb55c4602008-02-07 00:15:48 -080065 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67 }
68 unlock_kernel();
69 d_add(dentry, inode);
70 return NULL;
71}
72
73/*
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.
77 *
78 * If the create succeeds, we fill in the inode information
79 * with d_instantiate().
80 */
81static int ufs_create (struct inode * dir, struct dentry * dentry, int mode,
82 struct nameidata *nd)
83{
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070084 struct inode *inode;
85 int err;
86
87 UFSD("BEGIN\n");
Christoph Hellwig907f4552010-03-03 09:05:06 -050088
Christoph Hellwig871a2932010-03-03 09:05:07 -050089 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -050090
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070091 inode = ufs_new_inode(dir, mode);
92 err = PTR_ERR(inode);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 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();
102 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700103 UFSD("END: err=%d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return err;
105}
106
107static int ufs_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
108{
109 struct inode *inode;
110 int err;
111
112 if (!old_valid_dev(rdev))
113 return -EINVAL;
Christoph Hellwig907f4552010-03-03 09:05:06 -0500114
Christoph Hellwig871a2932010-03-03 09:05:07 -0500115 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 inode = ufs_new_inode(dir, mode);
118 err = PTR_ERR(inode);
119 if (!IS_ERR(inode)) {
120 init_special_inode(inode, mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 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();
126 }
127 return err;
128}
129
130static int ufs_symlink (struct inode * dir, struct dentry * dentry,
131 const char * symname)
132{
133 struct super_block * sb = dir->i_sb;
134 int err = -ENAMETOOLONG;
135 unsigned l = strlen(symname)+1;
136 struct inode * inode;
137
138 if (l > sb->s_blocksize)
Josh Triplett344fe782006-07-30 03:03:59 -0700139 goto out_notlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Christoph Hellwig871a2932010-03-03 09:05:07 -0500141 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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;
148
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;
Duane Griffin723be1f2009-01-08 22:43:51 +0000159 memcpy(UFS_I(inode)->i_u1.i_symlink, symname, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 inode->i_size = l-1;
161 }
162 mark_inode_dirty(inode);
163
164 err = ufs_add_nondir(dentry, inode);
165out:
166 unlock_kernel();
Josh Triplett344fe782006-07-30 03:03:59 -0700167out_notlocked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return err;
169
170out_fail:
Alexey Dobriyan32575452006-03-23 03:00:53 -0800171 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 iput(inode);
173 goto out;
174}
175
176static int ufs_link (struct dentry * old_dentry, struct inode * dir,
177 struct dentry *dentry)
178{
179 struct inode *inode = old_dentry->d_inode;
180 int error;
181
182 lock_kernel();
183 if (inode->i_nlink >= UFS_LINK_MAX) {
184 unlock_kernel();
185 return -EMLINK;
186 }
187
Christoph Hellwig871a2932010-03-03 09:05:07 -0500188 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 inode->i_ctime = CURRENT_TIME_SEC;
Alexey Dobriyan32575452006-03-23 03:00:53 -0800191 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 atomic_inc(&inode->i_count);
193
194 error = ufs_add_nondir(dentry, inode);
195 unlock_kernel();
196 return error;
197}
198
199static int ufs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
200{
201 struct inode * inode;
202 int err = -EMLINK;
203
204 if (dir->i_nlink >= UFS_LINK_MAX)
205 goto out;
206
Christoph Hellwig871a2932010-03-03 09:05:07 -0500207 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 lock_kernel();
Alexey Dobriyan32575452006-03-23 03:00:53 -0800210 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 inode = ufs_new_inode(dir, S_IFDIR|mode);
213 err = PTR_ERR(inode);
214 if (IS_ERR(inode))
215 goto out_dir;
216
217 inode->i_op = &ufs_dir_inode_operations;
218 inode->i_fop = &ufs_dir_operations;
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700219 inode->i_mapping->a_ops = &ufs_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Alexey Dobriyan32575452006-03-23 03:00:53 -0800221 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 err = ufs_make_empty(inode, dir);
224 if (err)
225 goto out_fail;
226
227 err = ufs_add_link(dentry, inode);
228 if (err)
229 goto out_fail;
230 unlock_kernel();
231
232 d_instantiate(dentry, inode);
233out:
234 return err;
235
236out_fail:
Alexey Dobriyan32575452006-03-23 03:00:53 -0800237 inode_dec_link_count(inode);
238 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 iput (inode);
240out_dir:
Alexey Dobriyan32575452006-03-23 03:00:53 -0800241 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unlock_kernel();
243 goto out;
244}
245
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700246static int ufs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct inode * inode = dentry->d_inode;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700249 struct ufs_dir_entry *de;
250 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int err = -ENOENT;
252
Christoph Hellwig871a2932010-03-03 09:05:07 -0500253 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500254
Alexey Dobriyan08049702009-12-15 16:46:50 -0800255 de = ufs_find_entry(dir, &dentry->d_name, &page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!de)
257 goto out;
258
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700259 err = ufs_delete_entry(dir, de, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (err)
261 goto out;
262
263 inode->i_ctime = dir->i_ctime;
Alexey Dobriyan32575452006-03-23 03:00:53 -0800264 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 err = 0;
266out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return err;
268}
269
270static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
271{
272 struct inode * inode = dentry->d_inode;
273 int err= -ENOTEMPTY;
274
275 lock_kernel();
276 if (ufs_empty_dir (inode)) {
277 err = ufs_unlink(dir, dentry);
278 if (!err) {
279 inode->i_size = 0;
Alexey Dobriyan32575452006-03-23 03:00:53 -0800280 inode_dec_link_count(inode);
281 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 }
284 unlock_kernel();
285 return err;
286}
287
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700288static int ufs_rename(struct inode *old_dir, struct dentry *old_dentry,
289 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct inode *old_inode = old_dentry->d_inode;
292 struct inode *new_inode = new_dentry->d_inode;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700293 struct page *dir_page = NULL;
294 struct ufs_dir_entry * dir_de = NULL;
295 struct page *old_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct ufs_dir_entry *old_de;
297 int err = -ENOENT;
298
Christoph Hellwig871a2932010-03-03 09:05:07 -0500299 dquot_initialize(old_dir);
300 dquot_initialize(new_dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500301
Alexey Dobriyan08049702009-12-15 16:46:50 -0800302 old_de = ufs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!old_de)
304 goto out;
305
306 if (S_ISDIR(old_inode->i_mode)) {
307 err = -EIO;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700308 dir_de = ufs_dotdot(old_inode, &dir_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!dir_de)
310 goto out_old;
311 }
312
313 if (new_inode) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700314 struct page *new_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct ufs_dir_entry *new_de;
316
317 err = -ENOTEMPTY;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700318 if (dir_de && !ufs_empty_dir(new_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto out_dir;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 err = -ENOENT;
Alexey Dobriyan08049702009-12-15 16:46:50 -0800322 new_de = ufs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (!new_de)
324 goto out_dir;
Alexey Dobriyan32575452006-03-23 03:00:53 -0800325 inode_inc_link_count(old_inode);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700326 ufs_set_link(new_dir, new_de, new_page, old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 new_inode->i_ctime = CURRENT_TIME_SEC;
328 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700329 drop_nlink(new_inode);
Alexey Dobriyan32575452006-03-23 03:00:53 -0800330 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 } else {
332 if (dir_de) {
333 err = -EMLINK;
334 if (new_dir->i_nlink >= UFS_LINK_MAX)
335 goto out_dir;
336 }
Alexey Dobriyan32575452006-03-23 03:00:53 -0800337 inode_inc_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 err = ufs_add_link(new_dentry, old_inode);
339 if (err) {
Alexey Dobriyan32575452006-03-23 03:00:53 -0800340 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto out_dir;
342 }
343 if (dir_de)
Alexey Dobriyan32575452006-03-23 03:00:53 -0800344 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700347 /*
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.
351 */
352 old_inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700354 ufs_delete_entry(old_dir, old_de, old_page);
Alexey Dobriyan32575452006-03-23 03:00:53 -0800355 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (dir_de) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700358 ufs_set_link(old_inode, dir_de, dir_page, new_dir);
Alexey Dobriyan32575452006-03-23 03:00:53 -0800359 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364out_dir:
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700365 if (dir_de) {
366 kunmap(dir_page);
367 page_cache_release(dir_page);
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369out_old:
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700370 kunmap(old_page);
371 page_cache_release(old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return err;
374}
375
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800376const struct inode_operations ufs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 .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,
386};