]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/ext2/namei.c | |
3 | * | |
4 | * Rewrite to pagecache. Almost all code had been changed, so blame me | |
5 | * if the things go wrong. Please, send bug reports to | |
6 | * [email protected] | |
7 | * | |
8 | * Stuff here is basically a glue between the VFS and generic UNIXish | |
9 | * filesystem that keeps everything in pagecache. All knowledge of the | |
10 | * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable | |
11 | * and it's easier to debug that way. In principle we might want to | |
12 | * generalize that a bit and turn it into a library. Or not. | |
13 | * | |
14 | * The only non-static object here is ext2_dir_inode_operations. | |
15 | * | |
16 | * TODO: get rid of kmap() use, add readahead. | |
17 | * | |
18 | * Copyright (C) 1992, 1993, 1994, 1995 | |
19 | * Remy Card ([email protected]) | |
20 | * Laboratoire MASI - Institut Blaise Pascal | |
21 | * Universite Pierre et Marie Curie (Paris VI) | |
22 | * | |
23 | * from | |
24 | * | |
25 | * linux/fs/minix/namei.c | |
26 | * | |
27 | * Copyright (C) 1991, 1992 Linus Torvalds | |
28 | * | |
29 | * Big-endian to little-endian byte-swapping/bitmaps by | |
30 | * David S. Miller ([email protected]), 1995 | |
31 | */ | |
32 | ||
33 | #include <linux/pagemap.h> | |
34 | #include "ext2.h" | |
35 | #include "xattr.h" | |
36 | #include "acl.h" | |
6d79125b | 37 | #include "xip.h" |
1da177e4 LT |
38 | |
39 | /* | |
40 | * Couple of helper functions - make the code slightly cleaner. | |
41 | */ | |
42 | ||
43 | static inline void ext2_inc_count(struct inode *inode) | |
44 | { | |
45 | inode->i_nlink++; | |
46 | mark_inode_dirty(inode); | |
47 | } | |
48 | ||
49 | static inline void ext2_dec_count(struct inode *inode) | |
50 | { | |
51 | inode->i_nlink--; | |
52 | mark_inode_dirty(inode); | |
53 | } | |
54 | ||
55 | static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode) | |
56 | { | |
57 | int err = ext2_add_link(dentry, inode); | |
58 | if (!err) { | |
59 | d_instantiate(dentry, inode); | |
60 | return 0; | |
61 | } | |
62 | ext2_dec_count(inode); | |
63 | iput(inode); | |
64 | return err; | |
65 | } | |
66 | ||
67 | /* | |
68 | * Methods themselves. | |
69 | */ | |
70 | ||
71 | static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd) | |
72 | { | |
73 | struct inode * inode; | |
74 | ino_t ino; | |
75 | ||
76 | if (dentry->d_name.len > EXT2_NAME_LEN) | |
77 | return ERR_PTR(-ENAMETOOLONG); | |
78 | ||
79 | ino = ext2_inode_by_name(dir, dentry); | |
80 | inode = NULL; | |
81 | if (ino) { | |
82 | inode = iget(dir->i_sb, ino); | |
83 | if (!inode) | |
84 | return ERR_PTR(-EACCES); | |
85 | } | |
86 | if (inode) | |
87 | return d_splice_alias(inode, dentry); | |
88 | d_add(dentry, inode); | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | struct dentry *ext2_get_parent(struct dentry *child) | |
93 | { | |
94 | unsigned long ino; | |
95 | struct dentry *parent; | |
96 | struct inode *inode; | |
97 | struct dentry dotdot; | |
98 | ||
99 | dotdot.d_name.name = ".."; | |
100 | dotdot.d_name.len = 2; | |
101 | ||
102 | ino = ext2_inode_by_name(child->d_inode, &dotdot); | |
103 | if (!ino) | |
104 | return ERR_PTR(-ENOENT); | |
105 | inode = iget(child->d_inode->i_sb, ino); | |
106 | ||
107 | if (!inode) | |
108 | return ERR_PTR(-EACCES); | |
109 | parent = d_alloc_anon(inode); | |
110 | if (!parent) { | |
111 | iput(inode); | |
112 | parent = ERR_PTR(-ENOMEM); | |
113 | } | |
114 | return parent; | |
115 | } | |
116 | ||
117 | /* | |
118 | * By the time this is called, we already have created | |
119 | * the directory cache entry for the new file, but it | |
120 | * is so far negative - it has no inode. | |
121 | * | |
122 | * If the create succeeds, we fill in the inode information | |
123 | * with d_instantiate(). | |
124 | */ | |
125 | static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd) | |
126 | { | |
127 | struct inode * inode = ext2_new_inode (dir, mode); | |
128 | int err = PTR_ERR(inode); | |
129 | if (!IS_ERR(inode)) { | |
130 | inode->i_op = &ext2_file_inode_operations; | |
6d79125b CO |
131 | if (ext2_use_xip(inode->i_sb)) { |
132 | inode->i_mapping->a_ops = &ext2_aops_xip; | |
133 | inode->i_fop = &ext2_xip_file_operations; | |
134 | } else if (test_opt(inode->i_sb, NOBH)) { | |
1da177e4 | 135 | inode->i_mapping->a_ops = &ext2_nobh_aops; |
6d79125b CO |
136 | inode->i_fop = &ext2_file_operations; |
137 | } else { | |
1da177e4 | 138 | inode->i_mapping->a_ops = &ext2_aops; |
6d79125b CO |
139 | inode->i_fop = &ext2_file_operations; |
140 | } | |
1da177e4 LT |
141 | mark_inode_dirty(inode); |
142 | err = ext2_add_nondir(dentry, inode); | |
143 | } | |
144 | return err; | |
145 | } | |
146 | ||
147 | static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev) | |
148 | { | |
149 | struct inode * inode; | |
150 | int err; | |
151 | ||
152 | if (!new_valid_dev(rdev)) | |
153 | return -EINVAL; | |
154 | ||
155 | inode = ext2_new_inode (dir, mode); | |
156 | err = PTR_ERR(inode); | |
157 | if (!IS_ERR(inode)) { | |
158 | init_special_inode(inode, inode->i_mode, rdev); | |
159 | #ifdef CONFIG_EXT2_FS_XATTR | |
160 | inode->i_op = &ext2_special_inode_operations; | |
161 | #endif | |
162 | mark_inode_dirty(inode); | |
163 | err = ext2_add_nondir(dentry, inode); | |
164 | } | |
165 | return err; | |
166 | } | |
167 | ||
168 | static int ext2_symlink (struct inode * dir, struct dentry * dentry, | |
169 | const char * symname) | |
170 | { | |
171 | struct super_block * sb = dir->i_sb; | |
172 | int err = -ENAMETOOLONG; | |
173 | unsigned l = strlen(symname)+1; | |
174 | struct inode * inode; | |
175 | ||
176 | if (l > sb->s_blocksize) | |
177 | goto out; | |
178 | ||
179 | inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO); | |
180 | err = PTR_ERR(inode); | |
181 | if (IS_ERR(inode)) | |
182 | goto out; | |
183 | ||
184 | if (l > sizeof (EXT2_I(inode)->i_data)) { | |
185 | /* slow symlink */ | |
186 | inode->i_op = &ext2_symlink_inode_operations; | |
187 | if (test_opt(inode->i_sb, NOBH)) | |
188 | inode->i_mapping->a_ops = &ext2_nobh_aops; | |
189 | else | |
190 | inode->i_mapping->a_ops = &ext2_aops; | |
191 | err = page_symlink(inode, symname, l); | |
192 | if (err) | |
193 | goto out_fail; | |
194 | } else { | |
195 | /* fast symlink */ | |
196 | inode->i_op = &ext2_fast_symlink_inode_operations; | |
197 | memcpy((char*)(EXT2_I(inode)->i_data),symname,l); | |
198 | inode->i_size = l-1; | |
199 | } | |
200 | mark_inode_dirty(inode); | |
201 | ||
202 | err = ext2_add_nondir(dentry, inode); | |
203 | out: | |
204 | return err; | |
205 | ||
206 | out_fail: | |
207 | ext2_dec_count(inode); | |
208 | iput (inode); | |
209 | goto out; | |
210 | } | |
211 | ||
212 | static int ext2_link (struct dentry * old_dentry, struct inode * dir, | |
213 | struct dentry *dentry) | |
214 | { | |
215 | struct inode *inode = old_dentry->d_inode; | |
216 | ||
217 | if (inode->i_nlink >= EXT2_LINK_MAX) | |
218 | return -EMLINK; | |
219 | ||
220 | inode->i_ctime = CURRENT_TIME_SEC; | |
221 | ext2_inc_count(inode); | |
222 | atomic_inc(&inode->i_count); | |
223 | ||
224 | return ext2_add_nondir(dentry, inode); | |
225 | } | |
226 | ||
227 | static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode) | |
228 | { | |
229 | struct inode * inode; | |
230 | int err = -EMLINK; | |
231 | ||
232 | if (dir->i_nlink >= EXT2_LINK_MAX) | |
233 | goto out; | |
234 | ||
235 | ext2_inc_count(dir); | |
236 | ||
237 | inode = ext2_new_inode (dir, S_IFDIR | mode); | |
238 | err = PTR_ERR(inode); | |
239 | if (IS_ERR(inode)) | |
240 | goto out_dir; | |
241 | ||
242 | inode->i_op = &ext2_dir_inode_operations; | |
243 | inode->i_fop = &ext2_dir_operations; | |
244 | if (test_opt(inode->i_sb, NOBH)) | |
245 | inode->i_mapping->a_ops = &ext2_nobh_aops; | |
246 | else | |
247 | inode->i_mapping->a_ops = &ext2_aops; | |
248 | ||
249 | ext2_inc_count(inode); | |
250 | ||
251 | err = ext2_make_empty(inode, dir); | |
252 | if (err) | |
253 | goto out_fail; | |
254 | ||
255 | err = ext2_add_link(dentry, inode); | |
256 | if (err) | |
257 | goto out_fail; | |
258 | ||
259 | d_instantiate(dentry, inode); | |
260 | out: | |
261 | return err; | |
262 | ||
263 | out_fail: | |
264 | ext2_dec_count(inode); | |
265 | ext2_dec_count(inode); | |
266 | iput(inode); | |
267 | out_dir: | |
268 | ext2_dec_count(dir); | |
269 | goto out; | |
270 | } | |
271 | ||
272 | static int ext2_unlink(struct inode * dir, struct dentry *dentry) | |
273 | { | |
274 | struct inode * inode = dentry->d_inode; | |
275 | struct ext2_dir_entry_2 * de; | |
276 | struct page * page; | |
277 | int err = -ENOENT; | |
278 | ||
279 | de = ext2_find_entry (dir, dentry, &page); | |
280 | if (!de) | |
281 | goto out; | |
282 | ||
283 | err = ext2_delete_entry (de, page); | |
284 | if (err) | |
285 | goto out; | |
286 | ||
287 | inode->i_ctime = dir->i_ctime; | |
288 | ext2_dec_count(inode); | |
289 | err = 0; | |
290 | out: | |
291 | return err; | |
292 | } | |
293 | ||
294 | static int ext2_rmdir (struct inode * dir, struct dentry *dentry) | |
295 | { | |
296 | struct inode * inode = dentry->d_inode; | |
297 | int err = -ENOTEMPTY; | |
298 | ||
299 | if (ext2_empty_dir(inode)) { | |
300 | err = ext2_unlink(dir, dentry); | |
301 | if (!err) { | |
302 | inode->i_size = 0; | |
303 | ext2_dec_count(inode); | |
304 | ext2_dec_count(dir); | |
305 | } | |
306 | } | |
307 | return err; | |
308 | } | |
309 | ||
310 | static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry, | |
311 | struct inode * new_dir, struct dentry * new_dentry ) | |
312 | { | |
313 | struct inode * old_inode = old_dentry->d_inode; | |
314 | struct inode * new_inode = new_dentry->d_inode; | |
315 | struct page * dir_page = NULL; | |
316 | struct ext2_dir_entry_2 * dir_de = NULL; | |
317 | struct page * old_page; | |
318 | struct ext2_dir_entry_2 * old_de; | |
319 | int err = -ENOENT; | |
320 | ||
321 | old_de = ext2_find_entry (old_dir, old_dentry, &old_page); | |
322 | if (!old_de) | |
323 | goto out; | |
324 | ||
325 | if (S_ISDIR(old_inode->i_mode)) { | |
326 | err = -EIO; | |
327 | dir_de = ext2_dotdot(old_inode, &dir_page); | |
328 | if (!dir_de) | |
329 | goto out_old; | |
330 | } | |
331 | ||
332 | if (new_inode) { | |
333 | struct page *new_page; | |
334 | struct ext2_dir_entry_2 *new_de; | |
335 | ||
336 | err = -ENOTEMPTY; | |
337 | if (dir_de && !ext2_empty_dir (new_inode)) | |
338 | goto out_dir; | |
339 | ||
340 | err = -ENOENT; | |
341 | new_de = ext2_find_entry (new_dir, new_dentry, &new_page); | |
342 | if (!new_de) | |
343 | goto out_dir; | |
344 | ext2_inc_count(old_inode); | |
345 | ext2_set_link(new_dir, new_de, new_page, old_inode); | |
346 | new_inode->i_ctime = CURRENT_TIME_SEC; | |
347 | if (dir_de) | |
348 | new_inode->i_nlink--; | |
349 | ext2_dec_count(new_inode); | |
350 | } else { | |
351 | if (dir_de) { | |
352 | err = -EMLINK; | |
353 | if (new_dir->i_nlink >= EXT2_LINK_MAX) | |
354 | goto out_dir; | |
355 | } | |
356 | ext2_inc_count(old_inode); | |
357 | err = ext2_add_link(new_dentry, old_inode); | |
358 | if (err) { | |
359 | ext2_dec_count(old_inode); | |
360 | goto out_dir; | |
361 | } | |
362 | if (dir_de) | |
363 | ext2_inc_count(new_dir); | |
364 | } | |
365 | ||
366 | /* | |
367 | * Like most other Unix systems, set the ctime for inodes on a | |
368 | * rename. | |
369 | * ext2_dec_count() will mark the inode dirty. | |
370 | */ | |
371 | old_inode->i_ctime = CURRENT_TIME_SEC; | |
372 | ||
373 | ext2_delete_entry (old_de, old_page); | |
374 | ext2_dec_count(old_inode); | |
375 | ||
376 | if (dir_de) { | |
377 | ext2_set_link(old_inode, dir_de, dir_page, new_dir); | |
378 | ext2_dec_count(old_dir); | |
379 | } | |
380 | return 0; | |
381 | ||
382 | ||
383 | out_dir: | |
384 | if (dir_de) { | |
385 | kunmap(dir_page); | |
386 | page_cache_release(dir_page); | |
387 | } | |
388 | out_old: | |
389 | kunmap(old_page); | |
390 | page_cache_release(old_page); | |
391 | out: | |
392 | return err; | |
393 | } | |
394 | ||
395 | struct inode_operations ext2_dir_inode_operations = { | |
396 | .create = ext2_create, | |
397 | .lookup = ext2_lookup, | |
398 | .link = ext2_link, | |
399 | .unlink = ext2_unlink, | |
400 | .symlink = ext2_symlink, | |
401 | .mkdir = ext2_mkdir, | |
402 | .rmdir = ext2_rmdir, | |
403 | .mknod = ext2_mknod, | |
404 | .rename = ext2_rename, | |
405 | #ifdef CONFIG_EXT2_FS_XATTR | |
406 | .setxattr = generic_setxattr, | |
407 | .getxattr = generic_getxattr, | |
408 | .listxattr = ext2_listxattr, | |
409 | .removexattr = generic_removexattr, | |
410 | #endif | |
411 | .setattr = ext2_setattr, | |
412 | .permission = ext2_permission, | |
413 | }; | |
414 | ||
415 | struct inode_operations ext2_special_inode_operations = { | |
416 | #ifdef CONFIG_EXT2_FS_XATTR | |
417 | .setxattr = generic_setxattr, | |
418 | .getxattr = generic_getxattr, | |
419 | .listxattr = ext2_listxattr, | |
420 | .removexattr = generic_removexattr, | |
421 | #endif | |
422 | .setattr = ext2_setattr, | |
423 | .permission = ext2_permission, | |
424 | }; |