blob: 1a6084d2b02e7520642f7fc4f19182075fffde2e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/minix/namei.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8#include "minix.h"
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010static 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 }
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -080017 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 iput(inode);
19 return err;
20}
21
Al Viro00cd8dd2012-06-10 17:13:09 -040022static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 struct inode * inode = NULL;
25 ino_t ino;
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 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);
Al Virob0149512018-04-30 19:57:34 -040031 if (ino)
David Howellsa90a08802008-02-07 00:15:44 -080032 inode = minix_iget(dir->i_sb, ino);
Al Virob0149512018-04-30 19:57:34 -040033 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
Al Viro1a67aaf2011-07-26 01:52:52 -040036static int minix_mknod(struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 int error;
39 struct inode *inode;
40
41 if (!old_valid_dev(rdev))
42 return -EINVAL;
43
Dmitry Monakhov9eed1fb2010-03-04 17:32:14 +030044 inode = minix_new_inode(dir, mode, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 if (inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 minix_set_inode(inode, rdev);
48 mark_inode_dirty(inode);
49 error = add_nondir(dentry, inode);
50 }
51 return error;
52}
53
Al Viro60545d02013-06-07 01:20:27 -040054static int minix_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
55{
56 int error;
57 struct inode *inode = minix_new_inode(dir, mode, &error);
58 if (inode) {
59 minix_set_inode(inode, 0);
60 mark_inode_dirty(inode);
61 d_tmpfile(dentry, inode);
62 }
63 return error;
64}
65
Al Viro4acdaf22011-07-26 01:42:34 -040066static int minix_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040067 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 return minix_mknod(dir, dentry, mode, 0);
70}
71
72static int minix_symlink(struct inode * dir, struct dentry *dentry,
73 const char * symname)
74{
75 int err = -ENAMETOOLONG;
76 int i = strlen(symname)+1;
77 struct inode * inode;
78
79 if (i > dir->i_sb->s_blocksize)
80 goto out;
81
Dmitry Monakhov9eed1fb2010-03-04 17:32:14 +030082 inode = minix_new_inode(dir, S_IFLNK | 0777, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (!inode)
84 goto out;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 minix_set_inode(inode, 0);
87 err = page_symlink(inode, symname, i);
88 if (err)
89 goto out_fail;
90
91 err = add_nondir(dentry, inode);
92out:
93 return err;
94
95out_fail:
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -080096 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 iput(inode);
98 goto out;
99}
100
101static int minix_link(struct dentry * old_dentry, struct inode * dir,
102 struct dentry *dentry)
103{
David Howells2b0143b2015-03-17 22:25:59 +0000104 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Deepa Dinamani02027d42016-09-14 07:48:05 -0700106 inode->i_ctime = current_time(inode);
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800107 inode_inc_link_count(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400108 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return add_nondir(dentry, inode);
110}
111
Al Viro18bb1db2011-07-26 01:41:39 -0400112static int minix_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct inode * inode;
Al Viro8de52772012-02-06 12:45:27 -0500115 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800117 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jorge Boncompte [DTI2]eee743f2010-09-09 16:38:19 -0700119 inode = minix_new_inode(dir, S_IFDIR | mode, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (!inode)
121 goto out_dir;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 minix_set_inode(inode, 0);
124
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800125 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 err = minix_make_empty(inode, dir);
128 if (err)
129 goto out_fail;
130
131 err = minix_add_link(dentry, inode);
132 if (err)
133 goto out_fail;
134
135 d_instantiate(dentry, inode);
136out:
137 return err;
138
139out_fail:
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800140 inode_dec_link_count(inode);
141 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 iput(inode);
143out_dir:
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800144 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto out;
146}
147
148static int minix_unlink(struct inode * dir, struct dentry *dentry)
149{
150 int err = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +0000151 struct inode * inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct page * page;
153 struct minix_dir_entry * de;
154
155 de = minix_find_entry(dentry, &page);
156 if (!de)
157 goto end_unlink;
158
159 err = minix_delete_entry(de, page);
160 if (err)
161 goto end_unlink;
162
163 inode->i_ctime = dir->i_ctime;
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800164 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165end_unlink:
166 return err;
167}
168
169static int minix_rmdir(struct inode * dir, struct dentry *dentry)
170{
David Howells2b0143b2015-03-17 22:25:59 +0000171 struct inode * inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 int err = -ENOTEMPTY;
173
174 if (minix_empty_dir(inode)) {
175 err = minix_unlink(dir, dentry);
176 if (!err) {
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800177 inode_dec_link_count(dir);
178 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 }
181 return err;
182}
183
184static int minix_rename(struct inode * old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200185 struct inode * new_dir, struct dentry *new_dentry,
186 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
David Howells2b0143b2015-03-17 22:25:59 +0000188 struct inode * old_inode = d_inode(old_dentry);
189 struct inode * new_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 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;
194 int err = -ENOENT;
195
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200196 if (flags & ~RENAME_NOREPLACE)
197 return -EINVAL;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 old_de = minix_find_entry(old_dentry, &old_page);
200 if (!old_de)
201 goto out;
202
203 if (S_ISDIR(old_inode->i_mode)) {
204 err = -EIO;
205 dir_de = minix_dotdot(old_inode, &dir_page);
206 if (!dir_de)
207 goto out_old;
208 }
209
210 if (new_inode) {
211 struct page * new_page;
212 struct minix_dir_entry * new_de;
213
214 err = -ENOTEMPTY;
215 if (dir_de && !minix_empty_dir(new_inode))
216 goto out_dir;
217
218 err = -ENOENT;
219 new_de = minix_find_entry(new_dentry, &new_page);
220 if (!new_de)
221 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 minix_set_link(new_de, new_page, old_inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700223 new_inode->i_ctime = current_time(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700225 drop_nlink(new_inode);
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800226 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 err = minix_add_link(new_dentry, old_inode);
Al Viro6f880492011-03-02 09:41:38 -0500229 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (dir_de)
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800232 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 minix_delete_entry(old_de, old_page);
Al Viro6f880492011-03-02 09:41:38 -0500236 mark_inode_dirty(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (dir_de) {
239 minix_set_link(dir_de, dir_page, new_dir);
Alexey Dobriyan78ec7b62006-03-23 03:00:51 -0800240 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 return 0;
243
244out_dir:
245 if (dir_de) {
246 kunmap(dir_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300247 put_page(dir_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249out_old:
250 kunmap(old_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300251 put_page(old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252out:
253 return err;
254}
255
256/*
257 * directories can handle most operations...
258 */
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800259const struct inode_operations minix_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .create = minix_create,
261 .lookup = minix_lookup,
262 .link = minix_link,
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,
Al Viro60545d02013-06-07 01:20:27 -0400270 .tmpfile = minix_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};