]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/minix/namei.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
7 | #include "minix.h" | |
8 | ||
1da177e4 LT |
9 | static 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 | } | |
78ec7b69 | 16 | inode_dec_link_count(inode); |
1da177e4 LT |
17 | iput(inode); |
18 | return err; | |
19 | } | |
20 | ||
00cd8dd3 | 21 | static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags) |
1da177e4 LT |
22 | { |
23 | struct inode * inode = NULL; | |
24 | ino_t ino; | |
25 | ||
1da177e4 LT |
26 | if (dentry->d_name.len > minix_sb(dir->i_sb)->s_namelen) |
27 | return ERR_PTR(-ENAMETOOLONG); | |
28 | ||
29 | ino = minix_inode_by_name(dentry); | |
30 | if (ino) { | |
a90a0880 DH |
31 | inode = minix_iget(dir->i_sb, ino); |
32 | if (IS_ERR(inode)) | |
33 | return ERR_CAST(inode); | |
1da177e4 LT |
34 | } |
35 | d_add(dentry, inode); | |
36 | return NULL; | |
37 | } | |
38 | ||
1a67aafb | 39 | static int minix_mknod(struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev) |
1da177e4 LT |
40 | { |
41 | int error; | |
42 | struct inode *inode; | |
43 | ||
44 | if (!old_valid_dev(rdev)) | |
45 | return -EINVAL; | |
46 | ||
9eed1fb7 | 47 | inode = minix_new_inode(dir, mode, &error); |
1da177e4 LT |
48 | |
49 | if (inode) { | |
1da177e4 LT |
50 | minix_set_inode(inode, rdev); |
51 | mark_inode_dirty(inode); | |
52 | error = add_nondir(dentry, inode); | |
53 | } | |
54 | return error; | |
55 | } | |
56 | ||
60545d0d AV |
57 | static int minix_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
58 | { | |
59 | int error; | |
60 | struct inode *inode = minix_new_inode(dir, mode, &error); | |
61 | if (inode) { | |
62 | minix_set_inode(inode, 0); | |
63 | mark_inode_dirty(inode); | |
64 | d_tmpfile(dentry, inode); | |
65 | } | |
66 | return error; | |
67 | } | |
68 | ||
4acdaf27 | 69 | static int minix_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
ebfc3b49 | 70 | bool excl) |
1da177e4 LT |
71 | { |
72 | return minix_mknod(dir, dentry, mode, 0); | |
73 | } | |
74 | ||
75 | static int minix_symlink(struct inode * dir, struct dentry *dentry, | |
76 | const char * symname) | |
77 | { | |
78 | int err = -ENAMETOOLONG; | |
79 | int i = strlen(symname)+1; | |
80 | struct inode * inode; | |
81 | ||
82 | if (i > dir->i_sb->s_blocksize) | |
83 | goto out; | |
84 | ||
9eed1fb7 | 85 | inode = minix_new_inode(dir, S_IFLNK | 0777, &err); |
1da177e4 LT |
86 | if (!inode) |
87 | goto out; | |
88 | ||
1da177e4 LT |
89 | minix_set_inode(inode, 0); |
90 | err = page_symlink(inode, symname, i); | |
91 | if (err) | |
92 | goto out_fail; | |
93 | ||
94 | err = add_nondir(dentry, inode); | |
95 | out: | |
96 | return err; | |
97 | ||
98 | out_fail: | |
78ec7b69 | 99 | inode_dec_link_count(inode); |
1da177e4 LT |
100 | iput(inode); |
101 | goto out; | |
102 | } | |
103 | ||
104 | static int minix_link(struct dentry * old_dentry, struct inode * dir, | |
105 | struct dentry *dentry) | |
106 | { | |
2b0143b5 | 107 | struct inode *inode = d_inode(old_dentry); |
1da177e4 | 108 | |
02027d42 | 109 | inode->i_ctime = current_time(inode); |
78ec7b69 | 110 | inode_inc_link_count(inode); |
7de9c6ee | 111 | ihold(inode); |
1da177e4 LT |
112 | return add_nondir(dentry, inode); |
113 | } | |
114 | ||
18bb1db3 | 115 | static int minix_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode) |
1da177e4 LT |
116 | { |
117 | struct inode * inode; | |
8de52778 | 118 | int err; |
1da177e4 | 119 | |
78ec7b69 | 120 | inode_inc_link_count(dir); |
1da177e4 | 121 | |
eee743fd | 122 | inode = minix_new_inode(dir, S_IFDIR | mode, &err); |
1da177e4 LT |
123 | if (!inode) |
124 | goto out_dir; | |
125 | ||
1da177e4 LT |
126 | minix_set_inode(inode, 0); |
127 | ||
78ec7b69 | 128 | inode_inc_link_count(inode); |
1da177e4 LT |
129 | |
130 | err = minix_make_empty(inode, dir); | |
131 | if (err) | |
132 | goto out_fail; | |
133 | ||
134 | err = minix_add_link(dentry, inode); | |
135 | if (err) | |
136 | goto out_fail; | |
137 | ||
138 | d_instantiate(dentry, inode); | |
139 | out: | |
140 | return err; | |
141 | ||
142 | out_fail: | |
78ec7b69 AD |
143 | inode_dec_link_count(inode); |
144 | inode_dec_link_count(inode); | |
1da177e4 LT |
145 | iput(inode); |
146 | out_dir: | |
78ec7b69 | 147 | inode_dec_link_count(dir); |
1da177e4 LT |
148 | goto out; |
149 | } | |
150 | ||
151 | static int minix_unlink(struct inode * dir, struct dentry *dentry) | |
152 | { | |
153 | int err = -ENOENT; | |
2b0143b5 | 154 | struct inode * inode = d_inode(dentry); |
1da177e4 LT |
155 | struct page * page; |
156 | struct minix_dir_entry * de; | |
157 | ||
158 | de = minix_find_entry(dentry, &page); | |
159 | if (!de) | |
160 | goto end_unlink; | |
161 | ||
162 | err = minix_delete_entry(de, page); | |
163 | if (err) | |
164 | goto end_unlink; | |
165 | ||
166 | inode->i_ctime = dir->i_ctime; | |
78ec7b69 | 167 | inode_dec_link_count(inode); |
1da177e4 LT |
168 | end_unlink: |
169 | return err; | |
170 | } | |
171 | ||
172 | static int minix_rmdir(struct inode * dir, struct dentry *dentry) | |
173 | { | |
2b0143b5 | 174 | struct inode * inode = d_inode(dentry); |
1da177e4 LT |
175 | int err = -ENOTEMPTY; |
176 | ||
177 | if (minix_empty_dir(inode)) { | |
178 | err = minix_unlink(dir, dentry); | |
179 | if (!err) { | |
78ec7b69 AD |
180 | inode_dec_link_count(dir); |
181 | inode_dec_link_count(inode); | |
1da177e4 LT |
182 | } |
183 | } | |
184 | return err; | |
185 | } | |
186 | ||
187 | static int minix_rename(struct inode * old_dir, struct dentry *old_dentry, | |
f03b8ad8 MS |
188 | struct inode * new_dir, struct dentry *new_dentry, |
189 | unsigned int flags) | |
1da177e4 | 190 | { |
2b0143b5 DH |
191 | struct inode * old_inode = d_inode(old_dentry); |
192 | struct inode * new_inode = d_inode(new_dentry); | |
1da177e4 LT |
193 | struct page * dir_page = NULL; |
194 | struct minix_dir_entry * dir_de = NULL; | |
195 | struct page * old_page; | |
196 | struct minix_dir_entry * old_de; | |
197 | int err = -ENOENT; | |
198 | ||
f03b8ad8 MS |
199 | if (flags & ~RENAME_NOREPLACE) |
200 | return -EINVAL; | |
201 | ||
1da177e4 LT |
202 | old_de = minix_find_entry(old_dentry, &old_page); |
203 | if (!old_de) | |
204 | goto out; | |
205 | ||
206 | if (S_ISDIR(old_inode->i_mode)) { | |
207 | err = -EIO; | |
208 | dir_de = minix_dotdot(old_inode, &dir_page); | |
209 | if (!dir_de) | |
210 | goto out_old; | |
211 | } | |
212 | ||
213 | if (new_inode) { | |
214 | struct page * new_page; | |
215 | struct minix_dir_entry * new_de; | |
216 | ||
217 | err = -ENOTEMPTY; | |
218 | if (dir_de && !minix_empty_dir(new_inode)) | |
219 | goto out_dir; | |
220 | ||
221 | err = -ENOENT; | |
222 | new_de = minix_find_entry(new_dentry, &new_page); | |
223 | if (!new_de) | |
224 | goto out_dir; | |
1da177e4 | 225 | minix_set_link(new_de, new_page, old_inode); |
02027d42 | 226 | new_inode->i_ctime = current_time(new_inode); |
1da177e4 | 227 | if (dir_de) |
9a53c3a7 | 228 | drop_nlink(new_inode); |
78ec7b69 | 229 | inode_dec_link_count(new_inode); |
1da177e4 | 230 | } else { |
1da177e4 | 231 | err = minix_add_link(new_dentry, old_inode); |
6f88049c | 232 | if (err) |
1da177e4 | 233 | goto out_dir; |
1da177e4 | 234 | if (dir_de) |
78ec7b69 | 235 | inode_inc_link_count(new_dir); |
1da177e4 LT |
236 | } |
237 | ||
238 | minix_delete_entry(old_de, old_page); | |
6f88049c | 239 | mark_inode_dirty(old_inode); |
1da177e4 LT |
240 | |
241 | if (dir_de) { | |
242 | minix_set_link(dir_de, dir_page, new_dir); | |
78ec7b69 | 243 | inode_dec_link_count(old_dir); |
1da177e4 LT |
244 | } |
245 | return 0; | |
246 | ||
247 | out_dir: | |
248 | if (dir_de) { | |
249 | kunmap(dir_page); | |
09cbfeaf | 250 | put_page(dir_page); |
1da177e4 LT |
251 | } |
252 | out_old: | |
253 | kunmap(old_page); | |
09cbfeaf | 254 | put_page(old_page); |
1da177e4 LT |
255 | out: |
256 | return err; | |
257 | } | |
258 | ||
259 | /* | |
260 | * directories can handle most operations... | |
261 | */ | |
92e1d5be | 262 | const struct inode_operations minix_dir_inode_operations = { |
1da177e4 LT |
263 | .create = minix_create, |
264 | .lookup = minix_lookup, | |
265 | .link = minix_link, | |
266 | .unlink = minix_unlink, | |
267 | .symlink = minix_symlink, | |
268 | .mkdir = minix_mkdir, | |
269 | .rmdir = minix_rmdir, | |
270 | .mknod = minix_mknod, | |
271 | .rename = minix_rename, | |
272 | .getattr = minix_getattr, | |
60545d0d | 273 | .tmpfile = minix_tmpfile, |
1da177e4 | 274 | }; |