Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/minix/namei.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | */ |
| 7 | |
| 8 | #include "minix.h" |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | static 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 Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 17 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | iput(inode); |
| 19 | return err; |
| 20 | } |
| 21 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 22 | static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
| 24 | struct inode * inode = NULL; |
| 25 | ino_t ino; |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
David Howells | a90a0880 | 2008-02-07 00:15:44 -0800 | [diff] [blame] | 32 | inode = minix_iget(dir->i_sb, ino); |
| 33 | if (IS_ERR(inode)) |
| 34 | return ERR_CAST(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } |
| 36 | d_add(dentry, inode); |
| 37 | return NULL; |
| 38 | } |
| 39 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 40 | static int minix_mknod(struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | int error; |
| 43 | struct inode *inode; |
| 44 | |
| 45 | if (!old_valid_dev(rdev)) |
| 46 | return -EINVAL; |
| 47 | |
Dmitry Monakhov | 9eed1fb | 2010-03-04 17:32:14 +0300 | [diff] [blame] | 48 | inode = minix_new_inode(dir, mode, &error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | if (inode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | minix_set_inode(inode, rdev); |
| 52 | mark_inode_dirty(inode); |
| 53 | error = add_nondir(dentry, inode); |
| 54 | } |
| 55 | return error; |
| 56 | } |
| 57 | |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 58 | static 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 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 70 | static int minix_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 71 | bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | return minix_mknod(dir, dentry, mode, 0); |
| 74 | } |
| 75 | |
| 76 | static 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 | |
Dmitry Monakhov | 9eed1fb | 2010-03-04 17:32:14 +0300 | [diff] [blame] | 86 | inode = minix_new_inode(dir, S_IFLNK | 0777, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (!inode) |
| 88 | goto out; |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 96 | out: |
| 97 | return err; |
| 98 | |
| 99 | out_fail: |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 100 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | iput(inode); |
| 102 | goto out; |
| 103 | } |
| 104 | |
| 105 | static int minix_link(struct dentry * old_dentry, struct inode * dir, |
| 106 | struct dentry *dentry) |
| 107 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 108 | struct inode *inode = d_inode(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 110 | inode->i_ctime = current_time(inode); |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 111 | inode_inc_link_count(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 112 | ihold(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return add_nondir(dentry, inode); |
| 114 | } |
| 115 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 116 | static int minix_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | struct inode * inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 119 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 121 | inode_inc_link_count(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Jorge Boncompte [DTI2] | eee743f | 2010-09-09 16:38:19 -0700 | [diff] [blame] | 123 | inode = minix_new_inode(dir, S_IFDIR | mode, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (!inode) |
| 125 | goto out_dir; |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | minix_set_inode(inode, 0); |
| 128 | |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 129 | inode_inc_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 140 | out: |
| 141 | return err; |
| 142 | |
| 143 | out_fail: |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 144 | inode_dec_link_count(inode); |
| 145 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | iput(inode); |
| 147 | out_dir: |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 148 | inode_dec_link_count(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | static int minix_unlink(struct inode * dir, struct dentry *dentry) |
| 153 | { |
| 154 | int err = -ENOENT; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 155 | struct inode * inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 168 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | end_unlink: |
| 170 | return err; |
| 171 | } |
| 172 | |
| 173 | static int minix_rmdir(struct inode * dir, struct dentry *dentry) |
| 174 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 175 | struct inode * inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | int err = -ENOTEMPTY; |
| 177 | |
| 178 | if (minix_empty_dir(inode)) { |
| 179 | err = minix_unlink(dir, dentry); |
| 180 | if (!err) { |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 181 | inode_dec_link_count(dir); |
| 182 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | return err; |
| 186 | } |
| 187 | |
| 188 | static int minix_rename(struct inode * old_dir, struct dentry *old_dentry, |
Miklos Szeredi | f03b8ad | 2016-09-27 11:03:57 +0200 | [diff] [blame] | 189 | struct inode * new_dir, struct dentry *new_dentry, |
| 190 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 192 | struct inode * old_inode = d_inode(old_dentry); |
| 193 | struct inode * new_inode = d_inode(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Miklos Szeredi | f03b8ad | 2016-09-27 11:03:57 +0200 | [diff] [blame] | 200 | if (flags & ~RENAME_NOREPLACE) |
| 201 | return -EINVAL; |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | minix_set_link(new_de, new_page, old_inode); |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 227 | new_inode->i_ctime = current_time(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (dir_de) |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 229 | drop_nlink(new_inode); |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 230 | inode_dec_link_count(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | err = minix_add_link(new_dentry, old_inode); |
Al Viro | 6f88049 | 2011-03-02 09:41:38 -0500 | [diff] [blame] | 233 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | goto out_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | if (dir_de) |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 236 | inode_inc_link_count(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | minix_delete_entry(old_de, old_page); |
Al Viro | 6f88049 | 2011-03-02 09:41:38 -0500 | [diff] [blame] | 240 | mark_inode_dirty(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | if (dir_de) { |
| 243 | minix_set_link(dir_de, dir_page, new_dir); |
Alexey Dobriyan | 78ec7b6 | 2006-03-23 03:00:51 -0800 | [diff] [blame] | 244 | inode_dec_link_count(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | return 0; |
| 247 | |
| 248 | out_dir: |
| 249 | if (dir_de) { |
| 250 | kunmap(dir_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 251 | put_page(dir_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | out_old: |
| 254 | kunmap(old_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 255 | put_page(old_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | out: |
| 257 | return err; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * directories can handle most operations... |
| 262 | */ |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 263 | const struct inode_operations minix_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 274 | .tmpfile = minix_tmpfile, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | }; |