1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/minix/namei.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
10 static int add_nondir(struct dentry
*dentry
, struct inode
*inode
)
12 int err
= minix_add_link(dentry
, inode
);
14 d_instantiate(dentry
, inode
);
17 inode_dec_link_count(inode
);
22 static struct dentry
*minix_lookup(struct inode
* dir
, struct dentry
*dentry
, unsigned int flags
)
24 struct inode
* inode
= NULL
;
27 if (dentry
->d_name
.len
> minix_sb(dir
->i_sb
)->s_namelen
)
28 return ERR_PTR(-ENAMETOOLONG
);
30 ino
= minix_inode_by_name(dentry
);
32 inode
= minix_iget(dir
->i_sb
, ino
);
33 return d_splice_alias(inode
, dentry
);
36 static int minix_mknod(struct inode
* dir
, struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
41 if (!old_valid_dev(rdev
))
44 inode
= minix_new_inode(dir
, mode
, &error
);
47 minix_set_inode(inode
, rdev
);
48 mark_inode_dirty(inode
);
49 error
= add_nondir(dentry
, inode
);
54 static int minix_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
57 struct inode
*inode
= minix_new_inode(dir
, mode
, &error
);
59 minix_set_inode(inode
, 0);
60 mark_inode_dirty(inode
);
61 d_tmpfile(dentry
, inode
);
66 static int minix_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
69 return minix_mknod(dir
, dentry
, mode
, 0);
72 static int minix_symlink(struct inode
* dir
, struct dentry
*dentry
,
75 int err
= -ENAMETOOLONG
;
76 int i
= strlen(symname
)+1;
79 if (i
> dir
->i_sb
->s_blocksize
)
82 inode
= minix_new_inode(dir
, S_IFLNK
| 0777, &err
);
86 minix_set_inode(inode
, 0);
87 err
= page_symlink(inode
, symname
, i
);
91 err
= add_nondir(dentry
, inode
);
96 inode_dec_link_count(inode
);
101 static int minix_link(struct dentry
* old_dentry
, struct inode
* dir
,
102 struct dentry
*dentry
)
104 struct inode
*inode
= d_inode(old_dentry
);
106 inode
->i_ctime
= current_time(inode
);
107 inode_inc_link_count(inode
);
109 return add_nondir(dentry
, inode
);
112 static int minix_mkdir(struct inode
* dir
, struct dentry
*dentry
, umode_t mode
)
114 struct inode
* inode
;
117 inode_inc_link_count(dir
);
119 inode
= minix_new_inode(dir
, S_IFDIR
| mode
, &err
);
123 minix_set_inode(inode
, 0);
125 inode_inc_link_count(inode
);
127 err
= minix_make_empty(inode
, dir
);
131 err
= minix_add_link(dentry
, inode
);
135 d_instantiate(dentry
, inode
);
140 inode_dec_link_count(inode
);
141 inode_dec_link_count(inode
);
144 inode_dec_link_count(dir
);
148 static int minix_unlink(struct inode
* dir
, struct dentry
*dentry
)
151 struct inode
* inode
= d_inode(dentry
);
153 struct minix_dir_entry
* de
;
155 de
= minix_find_entry(dentry
, &page
);
159 err
= minix_delete_entry(de
, page
);
163 inode
->i_ctime
= dir
->i_ctime
;
164 inode_dec_link_count(inode
);
169 static int minix_rmdir(struct inode
* dir
, struct dentry
*dentry
)
171 struct inode
* inode
= d_inode(dentry
);
172 int err
= -ENOTEMPTY
;
174 if (minix_empty_dir(inode
)) {
175 err
= minix_unlink(dir
, dentry
);
177 inode_dec_link_count(dir
);
178 inode_dec_link_count(inode
);
184 static int minix_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
185 struct inode
* new_dir
, struct dentry
*new_dentry
,
188 struct inode
* old_inode
= d_inode(old_dentry
);
189 struct inode
* new_inode
= d_inode(new_dentry
);
190 struct page
* dir_page
= NULL
;
191 struct minix_dir_entry
* dir_de
= NULL
;
192 struct page
* old_page
;
193 struct minix_dir_entry
* old_de
;
196 if (flags
& ~RENAME_NOREPLACE
)
199 old_de
= minix_find_entry(old_dentry
, &old_page
);
203 if (S_ISDIR(old_inode
->i_mode
)) {
205 dir_de
= minix_dotdot(old_inode
, &dir_page
);
211 struct page
* new_page
;
212 struct minix_dir_entry
* new_de
;
215 if (dir_de
&& !minix_empty_dir(new_inode
))
219 new_de
= minix_find_entry(new_dentry
, &new_page
);
222 minix_set_link(new_de
, new_page
, old_inode
);
223 new_inode
->i_ctime
= current_time(new_inode
);
225 drop_nlink(new_inode
);
226 inode_dec_link_count(new_inode
);
228 err
= minix_add_link(new_dentry
, old_inode
);
232 inode_inc_link_count(new_dir
);
235 minix_delete_entry(old_de
, old_page
);
236 mark_inode_dirty(old_inode
);
239 minix_set_link(dir_de
, dir_page
, new_dir
);
240 inode_dec_link_count(old_dir
);
257 * directories can handle most operations...
259 const struct inode_operations minix_dir_inode_operations
= {
260 .create
= minix_create
,
261 .lookup
= minix_lookup
,
263 .unlink
= minix_unlink
,
264 .symlink
= minix_symlink
,
265 .mkdir
= minix_mkdir
,
266 .rmdir
= minix_rmdir
,
267 .mknod
= minix_mknod
,
268 .rename
= minix_rename
,
269 .getattr
= minix_getattr
,
270 .tmpfile
= minix_tmpfile
,