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