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 mnt_idmap
*idmap
, struct inode
*dir
,
37 struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
41 if (!old_valid_dev(rdev
))
44 inode
= minix_new_inode(dir
, mode
);
46 return PTR_ERR(inode
);
48 minix_set_inode(inode
, rdev
);
49 mark_inode_dirty(inode
);
50 return add_nondir(dentry
, inode
);
53 static int minix_tmpfile(struct mnt_idmap
*idmap
, struct inode
*dir
,
54 struct file
*file
, umode_t mode
)
56 struct inode
*inode
= minix_new_inode(dir
, mode
);
59 return finish_open_simple(file
, PTR_ERR(inode
));
60 minix_set_inode(inode
, 0);
61 mark_inode_dirty(inode
);
62 d_tmpfile(file
, inode
);
63 return finish_open_simple(file
, 0);
66 static int minix_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
67 struct dentry
*dentry
, umode_t mode
, bool excl
)
69 return minix_mknod(&nop_mnt_idmap
, dir
, dentry
, mode
, 0);
72 static int minix_symlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
73 struct dentry
*dentry
, const char *symname
)
75 int i
= strlen(symname
)+1;
79 if (i
> dir
->i_sb
->s_blocksize
)
82 inode
= minix_new_inode(dir
, S_IFLNK
| 0777);
84 return PTR_ERR(inode
);
86 minix_set_inode(inode
, 0);
87 err
= page_symlink(inode
, symname
, i
);
89 inode_dec_link_count(inode
);
93 return add_nondir(dentry
, inode
);
96 static int minix_link(struct dentry
* old_dentry
, struct inode
* dir
,
97 struct dentry
*dentry
)
99 struct inode
*inode
= d_inode(old_dentry
);
101 inode_set_ctime_current(inode
);
102 inode_inc_link_count(inode
);
104 return add_nondir(dentry
, inode
);
107 static int minix_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
108 struct dentry
*dentry
, umode_t mode
)
110 struct inode
* inode
;
113 inode
= minix_new_inode(dir
, S_IFDIR
| mode
);
115 return PTR_ERR(inode
);
117 inode_inc_link_count(dir
);
118 minix_set_inode(inode
, 0);
119 inode_inc_link_count(inode
);
121 err
= minix_make_empty(inode
, dir
);
125 err
= minix_add_link(dentry
, inode
);
129 d_instantiate(dentry
, inode
);
134 inode_dec_link_count(inode
);
135 inode_dec_link_count(inode
);
137 inode_dec_link_count(dir
);
141 static int minix_unlink(struct inode
* dir
, struct dentry
*dentry
)
143 struct inode
* inode
= d_inode(dentry
);
145 struct minix_dir_entry
* de
;
148 de
= minix_find_entry(dentry
, &folio
);
151 err
= minix_delete_entry(de
, folio
);
152 folio_release_kmap(folio
, de
);
156 inode_set_ctime_to_ts(inode
, inode_get_ctime(dir
));
157 inode_dec_link_count(inode
);
161 static int minix_rmdir(struct inode
* dir
, struct dentry
*dentry
)
163 struct inode
* inode
= d_inode(dentry
);
164 int err
= -ENOTEMPTY
;
166 if (minix_empty_dir(inode
)) {
167 err
= minix_unlink(dir
, dentry
);
169 inode_dec_link_count(dir
);
170 inode_dec_link_count(inode
);
176 static int minix_rename(struct mnt_idmap
*idmap
,
177 struct inode
*old_dir
, struct dentry
*old_dentry
,
178 struct inode
*new_dir
, struct dentry
*new_dentry
,
181 struct inode
* old_inode
= d_inode(old_dentry
);
182 struct inode
* new_inode
= d_inode(new_dentry
);
183 struct folio
* dir_folio
= NULL
;
184 struct minix_dir_entry
* dir_de
= NULL
;
185 struct folio
*old_folio
;
186 struct minix_dir_entry
* old_de
;
189 if (flags
& ~RENAME_NOREPLACE
)
192 old_de
= minix_find_entry(old_dentry
, &old_folio
);
196 if (S_ISDIR(old_inode
->i_mode
)) {
198 dir_de
= minix_dotdot(old_inode
, &dir_folio
);
204 struct folio
*new_folio
;
205 struct minix_dir_entry
* new_de
;
208 if (dir_de
&& !minix_empty_dir(new_inode
))
212 new_de
= minix_find_entry(new_dentry
, &new_folio
);
215 err
= minix_set_link(new_de
, new_folio
, old_inode
);
216 folio_release_kmap(new_folio
, new_de
);
219 inode_set_ctime_current(new_inode
);
221 drop_nlink(new_inode
);
222 inode_dec_link_count(new_inode
);
224 err
= minix_add_link(new_dentry
, old_inode
);
228 inode_inc_link_count(new_dir
);
231 err
= minix_delete_entry(old_de
, old_folio
);
235 mark_inode_dirty(old_inode
);
238 err
= minix_set_link(dir_de
, dir_folio
, new_dir
);
240 inode_dec_link_count(old_dir
);
244 folio_release_kmap(dir_folio
, dir_de
);
246 folio_release_kmap(old_folio
, old_de
);
252 * directories can handle most operations...
254 const struct inode_operations minix_dir_inode_operations
= {
255 .create
= minix_create
,
256 .lookup
= minix_lookup
,
258 .unlink
= minix_unlink
,
259 .symlink
= minix_symlink
,
260 .mkdir
= minix_mkdir
,
261 .rmdir
= minix_rmdir
,
262 .mknod
= minix_mknod
,
263 .rename
= minix_rename
,
264 .getattr
= minix_getattr
,
265 .tmpfile
= minix_tmpfile
,