blob: e20ee85955d1c77c3a410da2c82893cd38acce8e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/minix/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include "minix.h"
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009static int add_nondir(struct dentry *dentry, struct inode *inode)
10{
11 int err = minix_add_link(dentry, inode);
12 if (!err) {
13 d_instantiate(dentry, inode);
14 return 0;
15 }
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -080016 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 iput(inode);
18 return err;
19}
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
22{
23 struct inode * inode = NULL;
24 ino_t ino;
25
26 dentry->d_op = dir->i_sb->s_root->d_op;
27
28 if (dentry->d_name.len > minix_sb(dir->i_sb)->s_namelen)
29 return ERR_PTR(-ENAMETOOLONG);
30
31 ino = minix_inode_by_name(dentry);
32 if (ino) {
David Howellsa90a08802008-02-07 00:15:44 -080033 inode = minix_iget(dir->i_sb, ino);
34 if (IS_ERR(inode))
35 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 }
37 d_add(dentry, inode);
38 return NULL;
39}
40
41static int minix_mknod(struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
42{
43 int error;
44 struct inode *inode;
45
46 if (!old_valid_dev(rdev))
47 return -EINVAL;
48
Dmitry Monakhov9eed1fb2010-03-04 17:32:14 +030049 inode = minix_new_inode(dir, mode, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 if (inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 minix_set_inode(inode, rdev);
53 mark_inode_dirty(inode);
54 error = add_nondir(dentry, inode);
55 }
56 return error;
57}
58
59static int minix_create(struct inode * dir, struct dentry *dentry, int mode,
60 struct nameidata *nd)
61{
62 return minix_mknod(dir, dentry, mode, 0);
63}
64
65static int minix_symlink(struct inode * dir, struct dentry *dentry,
66 const char * symname)
67{
68 int err = -ENAMETOOLONG;
69 int i = strlen(symname)+1;
70 struct inode * inode;
71
72 if (i > dir->i_sb->s_blocksize)
73 goto out;
74
Dmitry Monakhov9eed1fb2010-03-04 17:32:14 +030075 inode = minix_new_inode(dir, S_IFLNK | 0777, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (!inode)
77 goto out;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 minix_set_inode(inode, 0);
80 err = page_symlink(inode, symname, i);
81 if (err)
82 goto out_fail;
83
84 err = add_nondir(dentry, inode);
85out:
86 return err;
87
88out_fail:
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -080089 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 iput(inode);
91 goto out;
92}
93
94static int minix_link(struct dentry * old_dentry, struct inode * dir,
95 struct dentry *dentry)
96{
97 struct inode *inode = old_dentry->d_inode;
98
99 if (inode->i_nlink >= minix_sb(inode->i_sb)->s_link_max)
100 return -EMLINK;
101
102 inode->i_ctime = CURRENT_TIME_SEC;
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800103 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 atomic_inc(&inode->i_count);
105 return add_nondir(dentry, inode);
106}
107
108static int minix_mkdir(struct inode * dir, struct dentry *dentry, int mode)
109{
110 struct inode * inode;
111 int err = -EMLINK;
112
113 if (dir->i_nlink >= minix_sb(dir->i_sb)->s_link_max)
114 goto out;
115
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800116 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Dmitry Monakhov9eed1fb2010-03-04 17:32:14 +0300118 inode = minix_new_inode(dir, mode, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (!inode)
120 goto out_dir;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 minix_set_inode(inode, 0);
123
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800124 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 err = minix_make_empty(inode, dir);
127 if (err)
128 goto out_fail;
129
130 err = minix_add_link(dentry, inode);
131 if (err)
132 goto out_fail;
133
134 d_instantiate(dentry, inode);
135out:
136 return err;
137
138out_fail:
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800139 inode_dec_link_count(inode);
140 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 iput(inode);
142out_dir:
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800143 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 goto out;
145}
146
147static int minix_unlink(struct inode * dir, struct dentry *dentry)
148{
149 int err = -ENOENT;
150 struct inode * inode = dentry->d_inode;
151 struct page * page;
152 struct minix_dir_entry * de;
153
154 de = minix_find_entry(dentry, &page);
155 if (!de)
156 goto end_unlink;
157
158 err = minix_delete_entry(de, page);
159 if (err)
160 goto end_unlink;
161
162 inode->i_ctime = dir->i_ctime;
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800163 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164end_unlink:
165 return err;
166}
167
168static int minix_rmdir(struct inode * dir, struct dentry *dentry)
169{
170 struct inode * inode = dentry->d_inode;
171 int err = -ENOTEMPTY;
172
173 if (minix_empty_dir(inode)) {
174 err = minix_unlink(dir, dentry);
175 if (!err) {
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800176 inode_dec_link_count(dir);
177 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 }
180 return err;
181}
182
183static int minix_rename(struct inode * old_dir, struct dentry *old_dentry,
184 struct inode * new_dir, struct dentry *new_dentry)
185{
186 struct minix_sb_info * info = minix_sb(old_dir->i_sb);
187 struct inode * old_inode = old_dentry->d_inode;
188 struct inode * new_inode = new_dentry->d_inode;
189 struct page * dir_page = NULL;
190 struct minix_dir_entry * dir_de = NULL;
191 struct page * old_page;
192 struct minix_dir_entry * old_de;
193 int err = -ENOENT;
194
195 old_de = minix_find_entry(old_dentry, &old_page);
196 if (!old_de)
197 goto out;
198
199 if (S_ISDIR(old_inode->i_mode)) {
200 err = -EIO;
201 dir_de = minix_dotdot(old_inode, &dir_page);
202 if (!dir_de)
203 goto out_old;
204 }
205
206 if (new_inode) {
207 struct page * new_page;
208 struct minix_dir_entry * new_de;
209
210 err = -ENOTEMPTY;
211 if (dir_de && !minix_empty_dir(new_inode))
212 goto out_dir;
213
214 err = -ENOENT;
215 new_de = minix_find_entry(new_dentry, &new_page);
216 if (!new_de)
217 goto out_dir;
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800218 inode_inc_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 minix_set_link(new_de, new_page, old_inode);
220 new_inode->i_ctime = CURRENT_TIME_SEC;
221 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700222 drop_nlink(new_inode);
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800223 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 } else {
225 if (dir_de) {
226 err = -EMLINK;
227 if (new_dir->i_nlink >= info->s_link_max)
228 goto out_dir;
229 }
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800230 inode_inc_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 err = minix_add_link(new_dentry, old_inode);
232 if (err) {
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800233 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto out_dir;
235 }
236 if (dir_de)
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800237 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
240 minix_delete_entry(old_de, old_page);
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800241 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if (dir_de) {
244 minix_set_link(dir_de, dir_page, new_dir);
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800245 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 return 0;
248
249out_dir:
250 if (dir_de) {
251 kunmap(dir_page);
252 page_cache_release(dir_page);
253 }
254out_old:
255 kunmap(old_page);
256 page_cache_release(old_page);
257out:
258 return err;
259}
260
261/*
262 * directories can handle most operations...
263 */
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800264const struct inode_operations minix_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .create = minix_create,
266 .lookup = minix_lookup,
267 .link = minix_link,
268 .unlink = minix_unlink,
269 .symlink = minix_symlink,
270 .mkdir = minix_mkdir,
271 .rmdir = minix_rmdir,
272 .mknod = minix_mknod,
273 .rename = minix_rename,
274 .getattr = minix_getattr,
275};