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