1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext2/namei.c
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 * viro@parcelfarce.linux.theplanet.co.uk
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.
15 * The only non-static object here is ext2_dir_inode_operations.
17 * TODO: get rid of kmap() use, add readahead.
19 * Copyright (C) 1992, 1993, 1994, 1995
20 * Remy Card (card@masi.ibp.fr)
21 * Laboratoire MASI - Institut Blaise Pascal
22 * Universite Pierre et Marie Curie (Paris VI)
26 * linux/fs/minix/namei.c
28 * Copyright (C) 1991, 1992 Linus Torvalds
30 * Big-endian to little-endian byte-swapping/bitmaps by
31 * David S. Miller (davem@caip.rutgers.edu), 1995
34 #include <linux/pagemap.h>
35 #include <linux/quotaops.h>
40 static inline int ext2_add_nondir(struct dentry
*dentry
, struct inode
*inode
)
42 int err
= ext2_add_link(dentry
, inode
);
44 unlock_new_inode(inode
);
45 d_instantiate(dentry
, inode
);
48 inode_dec_link_count(inode
);
49 unlock_new_inode(inode
);
58 static struct dentry
*ext2_lookup(struct inode
* dir
, struct dentry
*dentry
, unsigned int flags
)
63 if (dentry
->d_name
.len
> EXT2_NAME_LEN
)
64 return ERR_PTR(-ENAMETOOLONG
);
66 ino
= ext2_inode_by_name(dir
, &dentry
->d_name
);
69 inode
= ext2_iget(dir
->i_sb
, ino
);
70 if (inode
== ERR_PTR(-ESTALE
)) {
71 ext2_error(dir
->i_sb
, __func__
,
72 "deleted inode referenced: %lu",
77 return d_splice_alias(inode
, dentry
);
80 struct dentry
*ext2_get_parent(struct dentry
*child
)
82 struct qstr dotdot
= QSTR_INIT("..", 2);
83 unsigned long ino
= ext2_inode_by_name(d_inode(child
), &dotdot
);
85 return ERR_PTR(-ENOENT
);
86 return d_obtain_alias(ext2_iget(child
->d_sb
, ino
));
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.
94 * If the create succeeds, we fill in the inode information
95 * with d_instantiate().
97 static int ext2_create (struct inode
* dir
, struct dentry
* dentry
, umode_t mode
, bool excl
)
102 err
= dquot_initialize(dir
);
106 inode
= ext2_new_inode(dir
, mode
, &dentry
->d_name
);
108 return PTR_ERR(inode
);
110 inode
->i_op
= &ext2_file_inode_operations
;
111 if (test_opt(inode
->i_sb
, NOBH
)) {
112 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
113 inode
->i_fop
= &ext2_file_operations
;
115 inode
->i_mapping
->a_ops
= &ext2_aops
;
116 inode
->i_fop
= &ext2_file_operations
;
118 mark_inode_dirty(inode
);
119 return ext2_add_nondir(dentry
, inode
);
122 static int ext2_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
124 struct inode
*inode
= ext2_new_inode(dir
, mode
, NULL
);
126 return PTR_ERR(inode
);
128 inode
->i_op
= &ext2_file_inode_operations
;
129 if (test_opt(inode
->i_sb
, NOBH
)) {
130 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
131 inode
->i_fop
= &ext2_file_operations
;
133 inode
->i_mapping
->a_ops
= &ext2_aops
;
134 inode
->i_fop
= &ext2_file_operations
;
136 mark_inode_dirty(inode
);
137 d_tmpfile(dentry
, inode
);
138 unlock_new_inode(inode
);
142 static int ext2_mknod (struct inode
* dir
, struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
144 struct inode
* inode
;
147 err
= dquot_initialize(dir
);
151 inode
= ext2_new_inode (dir
, mode
, &dentry
->d_name
);
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
;
158 mark_inode_dirty(inode
);
159 err
= ext2_add_nondir(dentry
, inode
);
164 static int ext2_symlink (struct inode
* dir
, struct dentry
* dentry
,
165 const char * symname
)
167 struct super_block
* sb
= dir
->i_sb
;
168 int err
= -ENAMETOOLONG
;
169 unsigned l
= strlen(symname
)+1;
170 struct inode
* inode
;
172 if (l
> sb
->s_blocksize
)
175 err
= dquot_initialize(dir
);
179 inode
= ext2_new_inode (dir
, S_IFLNK
| S_IRWXUGO
, &dentry
->d_name
);
180 err
= PTR_ERR(inode
);
184 if (l
> sizeof (EXT2_I(inode
)->i_data
)) {
186 inode
->i_op
= &ext2_symlink_inode_operations
;
187 inode_nohighmem(inode
);
188 if (test_opt(inode
->i_sb
, NOBH
))
189 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
191 inode
->i_mapping
->a_ops
= &ext2_aops
;
192 err
= page_symlink(inode
, symname
, l
);
197 inode
->i_op
= &ext2_fast_symlink_inode_operations
;
198 inode
->i_link
= (char*)EXT2_I(inode
)->i_data
;
199 memcpy(inode
->i_link
, symname
, l
);
202 mark_inode_dirty(inode
);
204 err
= ext2_add_nondir(dentry
, inode
);
209 inode_dec_link_count(inode
);
210 unlock_new_inode(inode
);
215 static int ext2_link (struct dentry
* old_dentry
, struct inode
* dir
,
216 struct dentry
*dentry
)
218 struct inode
*inode
= d_inode(old_dentry
);
221 err
= dquot_initialize(dir
);
225 inode
->i_ctime
= current_time(inode
);
226 inode_inc_link_count(inode
);
229 err
= ext2_add_link(dentry
, inode
);
231 d_instantiate(dentry
, inode
);
234 inode_dec_link_count(inode
);
239 static int ext2_mkdir(struct inode
* dir
, struct dentry
* dentry
, umode_t mode
)
241 struct inode
* inode
;
244 err
= dquot_initialize(dir
);
248 inode_inc_link_count(dir
);
250 inode
= ext2_new_inode(dir
, S_IFDIR
| mode
, &dentry
->d_name
);
251 err
= PTR_ERR(inode
);
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
;
260 inode
->i_mapping
->a_ops
= &ext2_aops
;
262 inode_inc_link_count(inode
);
264 err
= ext2_make_empty(inode
, dir
);
268 err
= ext2_add_link(dentry
, inode
);
272 unlock_new_inode(inode
);
273 d_instantiate(dentry
, inode
);
278 inode_dec_link_count(inode
);
279 inode_dec_link_count(inode
);
280 unlock_new_inode(inode
);
283 inode_dec_link_count(dir
);
287 static int ext2_unlink(struct inode
* dir
, struct dentry
*dentry
)
289 struct inode
* inode
= d_inode(dentry
);
290 struct ext2_dir_entry_2
* de
;
294 err
= dquot_initialize(dir
);
298 de
= ext2_find_entry (dir
, &dentry
->d_name
, &page
);
304 err
= ext2_delete_entry (de
, page
);
308 inode
->i_ctime
= dir
->i_ctime
;
309 inode_dec_link_count(inode
);
315 static int ext2_rmdir (struct inode
* dir
, struct dentry
*dentry
)
317 struct inode
* inode
= d_inode(dentry
);
318 int err
= -ENOTEMPTY
;
320 if (ext2_empty_dir(inode
)) {
321 err
= ext2_unlink(dir
, dentry
);
324 inode_dec_link_count(inode
);
325 inode_dec_link_count(dir
);
331 static int ext2_rename (struct inode
* old_dir
, struct dentry
* old_dentry
,
332 struct inode
* new_dir
, struct dentry
* new_dentry
,
335 struct inode
* old_inode
= d_inode(old_dentry
);
336 struct inode
* new_inode
= d_inode(new_dentry
);
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
;
343 if (flags
& ~RENAME_NOREPLACE
)
346 err
= dquot_initialize(old_dir
);
350 err
= dquot_initialize(new_dir
);
354 old_de
= ext2_find_entry (old_dir
, &old_dentry
->d_name
, &old_page
);
360 if (S_ISDIR(old_inode
->i_mode
)) {
362 dir_de
= ext2_dotdot(old_inode
, &dir_page
);
368 struct page
*new_page
;
369 struct ext2_dir_entry_2
*new_de
;
372 if (dir_de
&& !ext2_empty_dir (new_inode
))
376 new_de
= ext2_find_entry (new_dir
, &new_dentry
->d_name
, &new_page
);
379 ext2_set_link(new_dir
, new_de
, new_page
, old_inode
, 1);
380 new_inode
->i_ctime
= current_time(new_inode
);
382 drop_nlink(new_inode
);
383 inode_dec_link_count(new_inode
);
385 err
= ext2_add_link(new_dentry
, old_inode
);
389 inode_inc_link_count(new_dir
);
393 * Like most other Unix systems, set the ctime for inodes on a
396 old_inode
->i_ctime
= current_time(old_inode
);
397 mark_inode_dirty(old_inode
);
399 ext2_delete_entry (old_de
, old_page
);
402 if (old_dir
!= new_dir
)
403 ext2_set_link(old_inode
, dir_de
, dir_page
, new_dir
, 0);
408 inode_dec_link_count(old_dir
);
425 const struct inode_operations ext2_dir_inode_operations
= {
426 .create
= ext2_create
,
427 .lookup
= ext2_lookup
,
429 .unlink
= ext2_unlink
,
430 .symlink
= ext2_symlink
,
434 .rename
= ext2_rename
,
435 #ifdef CONFIG_EXT2_FS_XATTR
436 .listxattr
= ext2_listxattr
,
438 .setattr
= ext2_setattr
,
439 .get_acl
= ext2_get_acl
,
440 .set_acl
= ext2_set_acl
,
441 .tmpfile
= ext2_tmpfile
,
444 const struct inode_operations ext2_special_inode_operations
= {
445 #ifdef CONFIG_EXT2_FS_XATTR
446 .listxattr
= ext2_listxattr
,
448 .setattr
= ext2_setattr
,
449 .get_acl
= ext2_get_acl
,
450 .set_acl
= ext2_set_acl
,