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
);
34 return ERR_CAST(inode
);
40 static int minix_mknod(struct inode
* dir
, struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
45 if (!old_valid_dev(rdev
))
48 inode
= minix_new_inode(dir
, mode
, &error
);
51 minix_set_inode(inode
, rdev
);
52 mark_inode_dirty(inode
);
53 error
= add_nondir(dentry
, inode
);
58 static int minix_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
61 struct inode
*inode
= minix_new_inode(dir
, mode
, &error
);
63 minix_set_inode(inode
, 0);
64 mark_inode_dirty(inode
);
65 d_tmpfile(dentry
, inode
);
70 static int minix_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
73 return minix_mknod(dir
, dentry
, mode
, 0);
76 static int minix_symlink(struct inode
* dir
, struct dentry
*dentry
,
79 int err
= -ENAMETOOLONG
;
80 int i
= strlen(symname
)+1;
83 if (i
> dir
->i_sb
->s_blocksize
)
86 inode
= minix_new_inode(dir
, S_IFLNK
| 0777, &err
);
90 minix_set_inode(inode
, 0);
91 err
= page_symlink(inode
, symname
, i
);
95 err
= add_nondir(dentry
, inode
);
100 inode_dec_link_count(inode
);
105 static int minix_link(struct dentry
* old_dentry
, struct inode
* dir
,
106 struct dentry
*dentry
)
108 struct inode
*inode
= d_inode(old_dentry
);
110 inode
->i_ctime
= current_time(inode
);
111 inode_inc_link_count(inode
);
113 return add_nondir(dentry
, inode
);
116 static int minix_mkdir(struct inode
* dir
, struct dentry
*dentry
, umode_t mode
)
118 struct inode
* inode
;
121 inode_inc_link_count(dir
);
123 inode
= minix_new_inode(dir
, S_IFDIR
| mode
, &err
);
127 minix_set_inode(inode
, 0);
129 inode_inc_link_count(inode
);
131 err
= minix_make_empty(inode
, dir
);
135 err
= minix_add_link(dentry
, inode
);
139 d_instantiate(dentry
, inode
);
144 inode_dec_link_count(inode
);
145 inode_dec_link_count(inode
);
148 inode_dec_link_count(dir
);
152 static int minix_unlink(struct inode
* dir
, struct dentry
*dentry
)
155 struct inode
* inode
= d_inode(dentry
);
157 struct minix_dir_entry
* de
;
159 de
= minix_find_entry(dentry
, &page
);
163 err
= minix_delete_entry(de
, page
);
167 inode
->i_ctime
= dir
->i_ctime
;
168 inode_dec_link_count(inode
);
173 static int minix_rmdir(struct inode
* dir
, struct dentry
*dentry
)
175 struct inode
* inode
= d_inode(dentry
);
176 int err
= -ENOTEMPTY
;
178 if (minix_empty_dir(inode
)) {
179 err
= minix_unlink(dir
, dentry
);
181 inode_dec_link_count(dir
);
182 inode_dec_link_count(inode
);
188 static int minix_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
189 struct inode
* new_dir
, struct dentry
*new_dentry
,
192 struct inode
* old_inode
= d_inode(old_dentry
);
193 struct inode
* new_inode
= d_inode(new_dentry
);
194 struct page
* dir_page
= NULL
;
195 struct minix_dir_entry
* dir_de
= NULL
;
196 struct page
* old_page
;
197 struct minix_dir_entry
* old_de
;
200 if (flags
& ~RENAME_NOREPLACE
)
203 old_de
= minix_find_entry(old_dentry
, &old_page
);
207 if (S_ISDIR(old_inode
->i_mode
)) {
209 dir_de
= minix_dotdot(old_inode
, &dir_page
);
215 struct page
* new_page
;
216 struct minix_dir_entry
* new_de
;
219 if (dir_de
&& !minix_empty_dir(new_inode
))
223 new_de
= minix_find_entry(new_dentry
, &new_page
);
226 minix_set_link(new_de
, new_page
, old_inode
);
227 new_inode
->i_ctime
= current_time(new_inode
);
229 drop_nlink(new_inode
);
230 inode_dec_link_count(new_inode
);
232 err
= minix_add_link(new_dentry
, old_inode
);
236 inode_inc_link_count(new_dir
);
239 minix_delete_entry(old_de
, old_page
);
240 mark_inode_dirty(old_inode
);
243 minix_set_link(dir_de
, dir_page
, new_dir
);
244 inode_dec_link_count(old_dir
);
261 * directories can handle most operations...
263 const struct inode_operations minix_dir_inode_operations
= {
264 .create
= minix_create
,
265 .lookup
= minix_lookup
,
267 .unlink
= minix_unlink
,
268 .symlink
= minix_symlink
,
269 .mkdir
= minix_mkdir
,
270 .rmdir
= minix_rmdir
,
271 .mknod
= minix_mknod
,
272 .rename
= minix_rename
,
273 .getattr
= minix_getattr
,
274 .tmpfile
= minix_tmpfile
,