Loading...
1/*
2 * linux/fs/minix/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include "minix.h"
8
9static int add_nondir(struct dentry *dentry, struct inode *inode)
10{
11 int err = minix_add_link(dentry, inode);
12 if (!err) {
13 d_instantiate(dentry, inode);
14 return 0;
15 }
16 inode_dec_link_count(inode);
17 iput(inode);
18 return err;
19}
20
21static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
22{
23 struct inode * inode = NULL;
24 ino_t ino;
25
26 if (dentry->d_name.len > minix_sb(dir->i_sb)->s_namelen)
27 return ERR_PTR(-ENAMETOOLONG);
28
29 ino = minix_inode_by_name(dentry);
30 if (ino) {
31 inode = minix_iget(dir->i_sb, ino);
32 if (IS_ERR(inode))
33 return ERR_CAST(inode);
34 }
35 d_add(dentry, inode);
36 return NULL;
37}
38
39static int minix_mknod(struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
40{
41 int error;
42 struct inode *inode;
43
44 if (!old_valid_dev(rdev))
45 return -EINVAL;
46
47 inode = minix_new_inode(dir, mode, &error);
48
49 if (inode) {
50 minix_set_inode(inode, rdev);
51 mark_inode_dirty(inode);
52 error = add_nondir(dentry, inode);
53 }
54 return error;
55}
56
57static int minix_create(struct inode *dir, struct dentry *dentry, umode_t mode,
58 struct nameidata *nd)
59{
60 return minix_mknod(dir, dentry, mode, 0);
61}
62
63static int minix_symlink(struct inode * dir, struct dentry *dentry,
64 const char * symname)
65{
66 int err = -ENAMETOOLONG;
67 int i = strlen(symname)+1;
68 struct inode * inode;
69
70 if (i > dir->i_sb->s_blocksize)
71 goto out;
72
73 inode = minix_new_inode(dir, S_IFLNK | 0777, &err);
74 if (!inode)
75 goto out;
76
77 minix_set_inode(inode, 0);
78 err = page_symlink(inode, symname, i);
79 if (err)
80 goto out_fail;
81
82 err = add_nondir(dentry, inode);
83out:
84 return err;
85
86out_fail:
87 inode_dec_link_count(inode);
88 iput(inode);
89 goto out;
90}
91
92static int minix_link(struct dentry * old_dentry, struct inode * dir,
93 struct dentry *dentry)
94{
95 struct inode *inode = old_dentry->d_inode;
96
97 inode->i_ctime = CURRENT_TIME_SEC;
98 inode_inc_link_count(inode);
99 ihold(inode);
100 return add_nondir(dentry, inode);
101}
102
103static int minix_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
104{
105 struct inode * inode;
106 int err;
107
108 inode_inc_link_count(dir);
109
110 inode = minix_new_inode(dir, S_IFDIR | mode, &err);
111 if (!inode)
112 goto out_dir;
113
114 minix_set_inode(inode, 0);
115
116 inode_inc_link_count(inode);
117
118 err = minix_make_empty(inode, dir);
119 if (err)
120 goto out_fail;
121
122 err = minix_add_link(dentry, inode);
123 if (err)
124 goto out_fail;
125
126 d_instantiate(dentry, inode);
127out:
128 return err;
129
130out_fail:
131 inode_dec_link_count(inode);
132 inode_dec_link_count(inode);
133 iput(inode);
134out_dir:
135 inode_dec_link_count(dir);
136 goto out;
137}
138
139static int minix_unlink(struct inode * dir, struct dentry *dentry)
140{
141 int err = -ENOENT;
142 struct inode * inode = dentry->d_inode;
143 struct page * page;
144 struct minix_dir_entry * de;
145
146 de = minix_find_entry(dentry, &page);
147 if (!de)
148 goto end_unlink;
149
150 err = minix_delete_entry(de, page);
151 if (err)
152 goto end_unlink;
153
154 inode->i_ctime = dir->i_ctime;
155 inode_dec_link_count(inode);
156end_unlink:
157 return err;
158}
159
160static int minix_rmdir(struct inode * dir, struct dentry *dentry)
161{
162 struct inode * inode = dentry->d_inode;
163 int err = -ENOTEMPTY;
164
165 if (minix_empty_dir(inode)) {
166 err = minix_unlink(dir, dentry);
167 if (!err) {
168 inode_dec_link_count(dir);
169 inode_dec_link_count(inode);
170 }
171 }
172 return err;
173}
174
175static int minix_rename(struct inode * old_dir, struct dentry *old_dentry,
176 struct inode * new_dir, struct dentry *new_dentry)
177{
178 struct inode * old_inode = old_dentry->d_inode;
179 struct inode * new_inode = new_dentry->d_inode;
180 struct page * dir_page = NULL;
181 struct minix_dir_entry * dir_de = NULL;
182 struct page * old_page;
183 struct minix_dir_entry * old_de;
184 int err = -ENOENT;
185
186 old_de = minix_find_entry(old_dentry, &old_page);
187 if (!old_de)
188 goto out;
189
190 if (S_ISDIR(old_inode->i_mode)) {
191 err = -EIO;
192 dir_de = minix_dotdot(old_inode, &dir_page);
193 if (!dir_de)
194 goto out_old;
195 }
196
197 if (new_inode) {
198 struct page * new_page;
199 struct minix_dir_entry * new_de;
200
201 err = -ENOTEMPTY;
202 if (dir_de && !minix_empty_dir(new_inode))
203 goto out_dir;
204
205 err = -ENOENT;
206 new_de = minix_find_entry(new_dentry, &new_page);
207 if (!new_de)
208 goto out_dir;
209 minix_set_link(new_de, new_page, old_inode);
210 new_inode->i_ctime = CURRENT_TIME_SEC;
211 if (dir_de)
212 drop_nlink(new_inode);
213 inode_dec_link_count(new_inode);
214 } else {
215 err = minix_add_link(new_dentry, old_inode);
216 if (err)
217 goto out_dir;
218 if (dir_de)
219 inode_inc_link_count(new_dir);
220 }
221
222 minix_delete_entry(old_de, old_page);
223 mark_inode_dirty(old_inode);
224
225 if (dir_de) {
226 minix_set_link(dir_de, dir_page, new_dir);
227 inode_dec_link_count(old_dir);
228 }
229 return 0;
230
231out_dir:
232 if (dir_de) {
233 kunmap(dir_page);
234 page_cache_release(dir_page);
235 }
236out_old:
237 kunmap(old_page);
238 page_cache_release(old_page);
239out:
240 return err;
241}
242
243/*
244 * directories can handle most operations...
245 */
246const struct inode_operations minix_dir_inode_operations = {
247 .create = minix_create,
248 .lookup = minix_lookup,
249 .link = minix_link,
250 .unlink = minix_unlink,
251 .symlink = minix_symlink,
252 .mkdir = minix_mkdir,
253 .rmdir = minix_rmdir,
254 .mknod = minix_mknod,
255 .rename = minix_rename,
256 .getattr = minix_getattr,
257};
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/fs/minix/namei.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8#include "minix.h"
9
10static int add_nondir(struct dentry *dentry, struct inode *inode)
11{
12 int err = minix_add_link(dentry, inode);
13 if (!err) {
14 d_instantiate(dentry, inode);
15 return 0;
16 }
17 inode_dec_link_count(inode);
18 iput(inode);
19 return err;
20}
21
22static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
23{
24 struct inode * inode = NULL;
25 ino_t ino;
26
27 if (dentry->d_name.len > minix_sb(dir->i_sb)->s_namelen)
28 return ERR_PTR(-ENAMETOOLONG);
29
30 ino = minix_inode_by_name(dentry);
31 if (ino) {
32 inode = minix_iget(dir->i_sb, ino);
33 if (IS_ERR(inode))
34 return ERR_CAST(inode);
35 }
36 d_add(dentry, inode);
37 return NULL;
38}
39
40static int minix_mknod(struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
41{
42 int error;
43 struct inode *inode;
44
45 if (!old_valid_dev(rdev))
46 return -EINVAL;
47
48 inode = minix_new_inode(dir, mode, &error);
49
50 if (inode) {
51 minix_set_inode(inode, rdev);
52 mark_inode_dirty(inode);
53 error = add_nondir(dentry, inode);
54 }
55 return error;
56}
57
58static int minix_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
59{
60 int error;
61 struct inode *inode = minix_new_inode(dir, mode, &error);
62 if (inode) {
63 minix_set_inode(inode, 0);
64 mark_inode_dirty(inode);
65 d_tmpfile(dentry, inode);
66 }
67 return error;
68}
69
70static int minix_create(struct inode *dir, struct dentry *dentry, umode_t mode,
71 bool excl)
72{
73 return minix_mknod(dir, dentry, mode, 0);
74}
75
76static int minix_symlink(struct inode * dir, struct dentry *dentry,
77 const char * symname)
78{
79 int err = -ENAMETOOLONG;
80 int i = strlen(symname)+1;
81 struct inode * inode;
82
83 if (i > dir->i_sb->s_blocksize)
84 goto out;
85
86 inode = minix_new_inode(dir, S_IFLNK | 0777, &err);
87 if (!inode)
88 goto out;
89
90 minix_set_inode(inode, 0);
91 err = page_symlink(inode, symname, i);
92 if (err)
93 goto out_fail;
94
95 err = add_nondir(dentry, inode);
96out:
97 return err;
98
99out_fail:
100 inode_dec_link_count(inode);
101 iput(inode);
102 goto out;
103}
104
105static int minix_link(struct dentry * old_dentry, struct inode * dir,
106 struct dentry *dentry)
107{
108 struct inode *inode = d_inode(old_dentry);
109
110 inode->i_ctime = current_time(inode);
111 inode_inc_link_count(inode);
112 ihold(inode);
113 return add_nondir(dentry, inode);
114}
115
116static int minix_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
117{
118 struct inode * inode;
119 int err;
120
121 inode_inc_link_count(dir);
122
123 inode = minix_new_inode(dir, S_IFDIR | mode, &err);
124 if (!inode)
125 goto out_dir;
126
127 minix_set_inode(inode, 0);
128
129 inode_inc_link_count(inode);
130
131 err = minix_make_empty(inode, dir);
132 if (err)
133 goto out_fail;
134
135 err = minix_add_link(dentry, inode);
136 if (err)
137 goto out_fail;
138
139 d_instantiate(dentry, inode);
140out:
141 return err;
142
143out_fail:
144 inode_dec_link_count(inode);
145 inode_dec_link_count(inode);
146 iput(inode);
147out_dir:
148 inode_dec_link_count(dir);
149 goto out;
150}
151
152static int minix_unlink(struct inode * dir, struct dentry *dentry)
153{
154 int err = -ENOENT;
155 struct inode * inode = d_inode(dentry);
156 struct page * page;
157 struct minix_dir_entry * de;
158
159 de = minix_find_entry(dentry, &page);
160 if (!de)
161 goto end_unlink;
162
163 err = minix_delete_entry(de, page);
164 if (err)
165 goto end_unlink;
166
167 inode->i_ctime = dir->i_ctime;
168 inode_dec_link_count(inode);
169end_unlink:
170 return err;
171}
172
173static int minix_rmdir(struct inode * dir, struct dentry *dentry)
174{
175 struct inode * inode = d_inode(dentry);
176 int err = -ENOTEMPTY;
177
178 if (minix_empty_dir(inode)) {
179 err = minix_unlink(dir, dentry);
180 if (!err) {
181 inode_dec_link_count(dir);
182 inode_dec_link_count(inode);
183 }
184 }
185 return err;
186}
187
188static int minix_rename(struct inode * old_dir, struct dentry *old_dentry,
189 struct inode * new_dir, struct dentry *new_dentry,
190 unsigned int flags)
191{
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;
198 int err = -ENOENT;
199
200 if (flags & ~RENAME_NOREPLACE)
201 return -EINVAL;
202
203 old_de = minix_find_entry(old_dentry, &old_page);
204 if (!old_de)
205 goto out;
206
207 if (S_ISDIR(old_inode->i_mode)) {
208 err = -EIO;
209 dir_de = minix_dotdot(old_inode, &dir_page);
210 if (!dir_de)
211 goto out_old;
212 }
213
214 if (new_inode) {
215 struct page * new_page;
216 struct minix_dir_entry * new_de;
217
218 err = -ENOTEMPTY;
219 if (dir_de && !minix_empty_dir(new_inode))
220 goto out_dir;
221
222 err = -ENOENT;
223 new_de = minix_find_entry(new_dentry, &new_page);
224 if (!new_de)
225 goto out_dir;
226 minix_set_link(new_de, new_page, old_inode);
227 new_inode->i_ctime = current_time(new_inode);
228 if (dir_de)
229 drop_nlink(new_inode);
230 inode_dec_link_count(new_inode);
231 } else {
232 err = minix_add_link(new_dentry, old_inode);
233 if (err)
234 goto out_dir;
235 if (dir_de)
236 inode_inc_link_count(new_dir);
237 }
238
239 minix_delete_entry(old_de, old_page);
240 mark_inode_dirty(old_inode);
241
242 if (dir_de) {
243 minix_set_link(dir_de, dir_page, new_dir);
244 inode_dec_link_count(old_dir);
245 }
246 return 0;
247
248out_dir:
249 if (dir_de) {
250 kunmap(dir_page);
251 put_page(dir_page);
252 }
253out_old:
254 kunmap(old_page);
255 put_page(old_page);
256out:
257 return err;
258}
259
260/*
261 * directories can handle most operations...
262 */
263const struct inode_operations minix_dir_inode_operations = {
264 .create = minix_create,
265 .lookup = minix_lookup,
266 .link = minix_link,
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,
275};