]>
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) { | |
1e2e547a | 44 | d_instantiate_new(dentry, inode); |
1da177e4 LT |
45 | return 0; |
46 | } | |
a513b035 | 47 | inode_dec_link_count(inode); |
2e5afe54 | 48 | discard_new_inode(inode); |
1da177e4 LT |
49 | return err; |
50 | } | |
51 | ||
52 | /* | |
53 | * Methods themselves. | |
54 | */ | |
55 | ||
00cd8dd3 | 56 | static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags) |
1da177e4 LT |
57 | { |
58 | struct inode * inode; | |
b4962091 | 59 | ino_t ino = 0; |
60 | int res; | |
1da177e4 LT |
61 | |
62 | if (dentry->d_name.len > EXT2_NAME_LEN) | |
63 | return ERR_PTR(-ENAMETOOLONG); | |
64 | ||
b4962091 | 65 | res = ext2_inode_by_name(dir, &dentry->d_name, &ino); |
66 | if (res) | |
67 | return ERR_PTR(res); | |
1da177e4 LT |
68 | inode = NULL; |
69 | if (ino) { | |
52fcf703 | 70 | inode = ext2_iget(dir->i_sb, ino); |
a9049376 AV |
71 | if (inode == ERR_PTR(-ESTALE)) { |
72 | ext2_error(dir->i_sb, __func__, | |
73 | "deleted inode referenced: %lu", | |
74 | (unsigned long) ino); | |
75 | return ERR_PTR(-EIO); | |
4d6c13f8 | 76 | } |
1da177e4 | 77 | } |
082a05c6 | 78 | return d_splice_alias(inode, dentry); |
1da177e4 LT |
79 | } |
80 | ||
81 | struct dentry *ext2_get_parent(struct dentry *child) | |
82 | { | |
26fe5750 | 83 | struct qstr dotdot = QSTR_INIT("..", 2); |
b4962091 | 84 | ino_t ino = 0; |
85 | int res; | |
86 | ||
87 | res = ext2_inode_by_name(d_inode(child), &dotdot, &ino); | |
88 | if (res) | |
89 | return ERR_PTR(res); | |
1da177e4 LT |
90 | if (!ino) |
91 | return ERR_PTR(-ENOENT); | |
fc64005c | 92 | return d_obtain_alias(ext2_iget(child->d_sb, ino)); |
1da177e4 LT |
93 | } |
94 | ||
95 | /* | |
96 | * By the time this is called, we already have created | |
97 | * the directory cache entry for the new file, but it | |
98 | * is so far negative - it has no inode. | |
99 | * | |
100 | * If the create succeeds, we fill in the inode information | |
101 | * with d_instantiate(). | |
102 | */ | |
ebfc3b49 | 103 | static int ext2_create (struct inode * dir, struct dentry * dentry, umode_t mode, bool excl) |
1da177e4 | 104 | { |
907f4554 | 105 | struct inode *inode; |
c2edb305 | 106 | int err; |
907f4554 | 107 | |
c2edb305 JK |
108 | err = dquot_initialize(dir); |
109 | if (err) | |
110 | return err; | |
907f4554 | 111 | |
2a7dba39 | 112 | inode = ext2_new_inode(dir, mode, &dentry->d_name); |
907f4554 CH |
113 | if (IS_ERR(inode)) |
114 | return PTR_ERR(inode); | |
115 | ||
fb094c90 | 116 | ext2_set_file_ops(inode); |
907f4554 CH |
117 | mark_inode_dirty(inode); |
118 | return ext2_add_nondir(dentry, inode); | |
1da177e4 LT |
119 | } |
120 | ||
60545d0d AV |
121 | static int ext2_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
122 | { | |
123 | struct inode *inode = ext2_new_inode(dir, mode, NULL); | |
124 | if (IS_ERR(inode)) | |
125 | return PTR_ERR(inode); | |
126 | ||
fb094c90 | 127 | ext2_set_file_ops(inode); |
60545d0d AV |
128 | mark_inode_dirty(inode); |
129 | d_tmpfile(dentry, inode); | |
130 | unlock_new_inode(inode); | |
131 | return 0; | |
132 | } | |
133 | ||
1a67aafb | 134 | static int ext2_mknod (struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev) |
1da177e4 LT |
135 | { |
136 | struct inode * inode; | |
137 | int err; | |
138 | ||
c2edb305 JK |
139 | err = dquot_initialize(dir); |
140 | if (err) | |
141 | return err; | |
907f4554 | 142 | |
2a7dba39 | 143 | inode = ext2_new_inode (dir, mode, &dentry->d_name); |
1da177e4 LT |
144 | err = PTR_ERR(inode); |
145 | if (!IS_ERR(inode)) { | |
146 | init_special_inode(inode, inode->i_mode, rdev); | |
1da177e4 | 147 | inode->i_op = &ext2_special_inode_operations; |
1da177e4 LT |
148 | mark_inode_dirty(inode); |
149 | err = ext2_add_nondir(dentry, inode); | |
150 | } | |
151 | return err; | |
152 | } | |
153 | ||
154 | static int ext2_symlink (struct inode * dir, struct dentry * dentry, | |
155 | const char * symname) | |
156 | { | |
157 | struct super_block * sb = dir->i_sb; | |
158 | int err = -ENAMETOOLONG; | |
159 | unsigned l = strlen(symname)+1; | |
160 | struct inode * inode; | |
161 | ||
162 | if (l > sb->s_blocksize) | |
163 | goto out; | |
164 | ||
c2edb305 JK |
165 | err = dquot_initialize(dir); |
166 | if (err) | |
167 | goto out; | |
907f4554 | 168 | |
2a7dba39 | 169 | inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name); |
1da177e4 LT |
170 | err = PTR_ERR(inode); |
171 | if (IS_ERR(inode)) | |
172 | goto out; | |
173 | ||
174 | if (l > sizeof (EXT2_I(inode)->i_data)) { | |
175 | /* slow symlink */ | |
176 | inode->i_op = &ext2_symlink_inode_operations; | |
21fc61c7 | 177 | inode_nohighmem(inode); |
1da177e4 LT |
178 | if (test_opt(inode->i_sb, NOBH)) |
179 | inode->i_mapping->a_ops = &ext2_nobh_aops; | |
180 | else | |
181 | inode->i_mapping->a_ops = &ext2_aops; | |
182 | err = page_symlink(inode, symname, l); | |
183 | if (err) | |
184 | goto out_fail; | |
185 | } else { | |
186 | /* fast symlink */ | |
187 | inode->i_op = &ext2_fast_symlink_inode_operations; | |
cbe0fa38 AV |
188 | inode->i_link = (char*)EXT2_I(inode)->i_data; |
189 | memcpy(inode->i_link, symname, l); | |
1da177e4 LT |
190 | inode->i_size = l-1; |
191 | } | |
192 | mark_inode_dirty(inode); | |
193 | ||
194 | err = ext2_add_nondir(dentry, inode); | |
195 | out: | |
196 | return err; | |
197 | ||
198 | out_fail: | |
a513b035 | 199 | inode_dec_link_count(inode); |
2e5afe54 | 200 | discard_new_inode(inode); |
1da177e4 LT |
201 | goto out; |
202 | } | |
203 | ||
204 | static int ext2_link (struct dentry * old_dentry, struct inode * dir, | |
205 | struct dentry *dentry) | |
206 | { | |
2b0143b5 | 207 | struct inode *inode = d_inode(old_dentry); |
41080b5a | 208 | int err; |
1da177e4 | 209 | |
c2edb305 JK |
210 | err = dquot_initialize(dir); |
211 | if (err) | |
212 | return err; | |
907f4554 | 213 | |
02027d42 | 214 | inode->i_ctime = current_time(inode); |
a513b035 | 215 | inode_inc_link_count(inode); |
7de9c6ee | 216 | ihold(inode); |
1da177e4 | 217 | |
41080b5a AV |
218 | err = ext2_add_link(dentry, inode); |
219 | if (!err) { | |
220 | d_instantiate(dentry, inode); | |
221 | return 0; | |
222 | } | |
223 | inode_dec_link_count(inode); | |
224 | iput(inode); | |
225 | return err; | |
1da177e4 LT |
226 | } |
227 | ||
18bb1db3 | 228 | static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode) |
1da177e4 LT |
229 | { |
230 | struct inode * inode; | |
8de52778 | 231 | int err; |
1da177e4 | 232 | |
c2edb305 JK |
233 | err = dquot_initialize(dir); |
234 | if (err) | |
235 | return err; | |
907f4554 | 236 | |
a513b035 | 237 | inode_inc_link_count(dir); |
1da177e4 | 238 | |
2a7dba39 | 239 | inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name); |
1da177e4 LT |
240 | err = PTR_ERR(inode); |
241 | if (IS_ERR(inode)) | |
242 | goto out_dir; | |
243 | ||
244 | inode->i_op = &ext2_dir_inode_operations; | |
245 | inode->i_fop = &ext2_dir_operations; | |
246 | if (test_opt(inode->i_sb, NOBH)) | |
247 | inode->i_mapping->a_ops = &ext2_nobh_aops; | |
248 | else | |
249 | inode->i_mapping->a_ops = &ext2_aops; | |
250 | ||
a513b035 | 251 | inode_inc_link_count(inode); |
1da177e4 LT |
252 | |
253 | err = ext2_make_empty(inode, dir); | |
254 | if (err) | |
255 | goto out_fail; | |
256 | ||
257 | err = ext2_add_link(dentry, inode); | |
258 | if (err) | |
259 | goto out_fail; | |
260 | ||
1e2e547a | 261 | d_instantiate_new(dentry, inode); |
1da177e4 LT |
262 | out: |
263 | return err; | |
264 | ||
265 | out_fail: | |
a513b035 AD |
266 | inode_dec_link_count(inode); |
267 | inode_dec_link_count(inode); | |
2e5afe54 | 268 | discard_new_inode(inode); |
1da177e4 | 269 | out_dir: |
a513b035 | 270 | inode_dec_link_count(dir); |
1da177e4 LT |
271 | goto out; |
272 | } | |
273 | ||
274 | static int ext2_unlink(struct inode * dir, struct dentry *dentry) | |
275 | { | |
2b0143b5 | 276 | struct inode * inode = d_inode(dentry); |
1da177e4 LT |
277 | struct ext2_dir_entry_2 * de; |
278 | struct page * page; | |
c2edb305 | 279 | int err; |
1da177e4 | 280 | |
c2edb305 JK |
281 | err = dquot_initialize(dir); |
282 | if (err) | |
283 | goto out; | |
907f4554 | 284 | |
b4962091 | 285 | de = ext2_find_entry(dir, &dentry->d_name, &page); |
286 | if (IS_ERR(de)) { | |
287 | err = PTR_ERR(de); | |
288 | goto out; | |
289 | } | |
c2edb305 JK |
290 | if (!de) { |
291 | err = -ENOENT; | |
1da177e4 | 292 | goto out; |
c2edb305 | 293 | } |
1da177e4 LT |
294 | |
295 | err = ext2_delete_entry (de, page); | |
296 | if (err) | |
297 | goto out; | |
298 | ||
299 | inode->i_ctime = dir->i_ctime; | |
a513b035 | 300 | inode_dec_link_count(inode); |
1da177e4 LT |
301 | err = 0; |
302 | out: | |
303 | return err; | |
304 | } | |
305 | ||
306 | static int ext2_rmdir (struct inode * dir, struct dentry *dentry) | |
307 | { | |
2b0143b5 | 308 | struct inode * inode = d_inode(dentry); |
1da177e4 LT |
309 | int err = -ENOTEMPTY; |
310 | ||
311 | if (ext2_empty_dir(inode)) { | |
312 | err = ext2_unlink(dir, dentry); | |
313 | if (!err) { | |
314 | inode->i_size = 0; | |
a513b035 AD |
315 | inode_dec_link_count(inode); |
316 | inode_dec_link_count(dir); | |
1da177e4 LT |
317 | } |
318 | } | |
319 | return err; | |
320 | } | |
321 | ||
322 | static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry, | |
f03b8ad8 MS |
323 | struct inode * new_dir, struct dentry * new_dentry, |
324 | unsigned int flags) | |
1da177e4 | 325 | { |
2b0143b5 DH |
326 | struct inode * old_inode = d_inode(old_dentry); |
327 | struct inode * new_inode = d_inode(new_dentry); | |
1da177e4 LT |
328 | struct page * dir_page = NULL; |
329 | struct ext2_dir_entry_2 * dir_de = NULL; | |
330 | struct page * old_page; | |
331 | struct ext2_dir_entry_2 * old_de; | |
c2edb305 | 332 | int err; |
1da177e4 | 333 | |
f03b8ad8 MS |
334 | if (flags & ~RENAME_NOREPLACE) |
335 | return -EINVAL; | |
336 | ||
c2edb305 JK |
337 | err = dquot_initialize(old_dir); |
338 | if (err) | |
339 | goto out; | |
340 | ||
341 | err = dquot_initialize(new_dir); | |
342 | if (err) | |
343 | goto out; | |
907f4554 | 344 | |
b4962091 | 345 | old_de = ext2_find_entry(old_dir, &old_dentry->d_name, &old_page); |
346 | if (IS_ERR(old_de)) { | |
347 | err = PTR_ERR(old_de); | |
348 | goto out; | |
349 | } | |
c2edb305 JK |
350 | if (!old_de) { |
351 | err = -ENOENT; | |
1da177e4 | 352 | goto out; |
c2edb305 | 353 | } |
1da177e4 LT |
354 | |
355 | if (S_ISDIR(old_inode->i_mode)) { | |
356 | err = -EIO; | |
357 | dir_de = ext2_dotdot(old_inode, &dir_page); | |
358 | if (!dir_de) | |
359 | goto out_old; | |
360 | } | |
361 | ||
362 | if (new_inode) { | |
363 | struct page *new_page; | |
364 | struct ext2_dir_entry_2 *new_de; | |
365 | ||
366 | err = -ENOTEMPTY; | |
367 | if (dir_de && !ext2_empty_dir (new_inode)) | |
368 | goto out_dir; | |
369 | ||
370 | err = -ENOENT; | |
b4962091 | 371 | new_de = ext2_find_entry(new_dir, &new_dentry->d_name, &new_page); |
372 | if (IS_ERR(new_de)) { | |
373 | err = PTR_ERR(new_de); | |
374 | goto out_dir; | |
375 | } | |
1da177e4 LT |
376 | if (!new_de) |
377 | goto out_dir; | |
39fe7557 | 378 | ext2_set_link(new_dir, new_de, new_page, old_inode, 1); |
02027d42 | 379 | new_inode->i_ctime = current_time(new_inode); |
1da177e4 | 380 | if (dir_de) |
9a53c3a7 | 381 | drop_nlink(new_inode); |
a513b035 | 382 | inode_dec_link_count(new_inode); |
1da177e4 | 383 | } else { |
1da177e4 | 384 | err = ext2_add_link(new_dentry, old_inode); |
e8a80c6f | 385 | if (err) |
1da177e4 | 386 | goto out_dir; |
1da177e4 | 387 | if (dir_de) |
a513b035 | 388 | inode_inc_link_count(new_dir); |
1da177e4 LT |
389 | } |
390 | ||
391 | /* | |
392 | * Like most other Unix systems, set the ctime for inodes on a | |
393 | * rename. | |
1da177e4 | 394 | */ |
02027d42 | 395 | old_inode->i_ctime = current_time(old_inode); |
e8a80c6f | 396 | mark_inode_dirty(old_inode); |
1da177e4 LT |
397 | |
398 | ext2_delete_entry (old_de, old_page); | |
1da177e4 LT |
399 | |
400 | if (dir_de) { | |
39fe7557 JK |
401 | if (old_dir != new_dir) |
402 | ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0); | |
9de6886e NP |
403 | else { |
404 | kunmap(dir_page); | |
09cbfeaf | 405 | put_page(dir_page); |
9de6886e | 406 | } |
a513b035 | 407 | inode_dec_link_count(old_dir); |
1da177e4 LT |
408 | } |
409 | return 0; | |
410 | ||
411 | ||
412 | out_dir: | |
413 | if (dir_de) { | |
414 | kunmap(dir_page); | |
09cbfeaf | 415 | put_page(dir_page); |
1da177e4 LT |
416 | } |
417 | out_old: | |
418 | kunmap(old_page); | |
09cbfeaf | 419 | put_page(old_page); |
1da177e4 LT |
420 | out: |
421 | return err; | |
422 | } | |
423 | ||
754661f1 | 424 | const struct inode_operations ext2_dir_inode_operations = { |
1da177e4 LT |
425 | .create = ext2_create, |
426 | .lookup = ext2_lookup, | |
427 | .link = ext2_link, | |
428 | .unlink = ext2_unlink, | |
429 | .symlink = ext2_symlink, | |
430 | .mkdir = ext2_mkdir, | |
431 | .rmdir = ext2_rmdir, | |
432 | .mknod = ext2_mknod, | |
433 | .rename = ext2_rename, | |
1da177e4 | 434 | .listxattr = ext2_listxattr, |
93bc420e | 435 | .getattr = ext2_getattr, |
1da177e4 | 436 | .setattr = ext2_setattr, |
4e34e719 | 437 | .get_acl = ext2_get_acl, |
64e178a7 | 438 | .set_acl = ext2_set_acl, |
60545d0d | 439 | .tmpfile = ext2_tmpfile, |
1da177e4 LT |
440 | }; |
441 | ||
754661f1 | 442 | const struct inode_operations ext2_special_inode_operations = { |
1da177e4 | 443 | .listxattr = ext2_listxattr, |
93bc420e | 444 | .getattr = ext2_getattr, |
1da177e4 | 445 | .setattr = ext2_setattr, |
4e34e719 | 446 | .get_acl = ext2_get_acl, |
64e178a7 | 447 | .set_acl = ext2_set_acl, |
1da177e4 | 448 | }; |