Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/sysv/namei.c |
| 3 | * |
| 4 | * minix/namei.c |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | * |
| 7 | * coh/namei.c |
| 8 | * Copyright (C) 1993 Pascal Haible, Bruno Haible |
| 9 | * |
| 10 | * sysv/namei.c |
| 11 | * Copyright (C) 1993 Bruno Haible |
| 12 | * Copyright (C) 1997, 1998 Krzysztof G. Baranowski |
| 13 | */ |
| 14 | |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/smp_lock.h> |
| 17 | #include "sysv.h" |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | static int add_nondir(struct dentry *dentry, struct inode *inode) |
| 20 | { |
| 21 | int err = sysv_add_link(dentry, inode); |
| 22 | if (!err) { |
| 23 | d_instantiate(dentry, inode); |
| 24 | return 0; |
| 25 | } |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 26 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | iput(inode); |
| 28 | return err; |
| 29 | } |
| 30 | |
| 31 | static int sysv_hash(struct dentry *dentry, struct qstr *qstr) |
| 32 | { |
| 33 | /* Truncate the name in place, avoids having to define a compare |
| 34 | function. */ |
| 35 | if (qstr->len > SYSV_NAMELEN) { |
| 36 | qstr->len = SYSV_NAMELEN; |
| 37 | qstr->hash = full_name_hash(qstr->name, qstr->len); |
| 38 | } |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | struct dentry_operations sysv_dentry_operations = { |
| 43 | .d_hash = sysv_hash, |
| 44 | }; |
| 45 | |
| 46 | static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd) |
| 47 | { |
| 48 | struct inode * inode = NULL; |
| 49 | ino_t ino; |
| 50 | |
| 51 | dentry->d_op = dir->i_sb->s_root->d_op; |
| 52 | if (dentry->d_name.len > SYSV_NAMELEN) |
| 53 | return ERR_PTR(-ENAMETOOLONG); |
| 54 | ino = sysv_inode_by_name(dentry); |
| 55 | |
| 56 | if (ino) { |
| 57 | inode = iget(dir->i_sb, ino); |
| 58 | if (!inode) |
| 59 | return ERR_PTR(-EACCES); |
| 60 | } |
| 61 | d_add(dentry, inode); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | static int sysv_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev) |
| 66 | { |
| 67 | struct inode * inode; |
| 68 | int err; |
| 69 | |
| 70 | if (!old_valid_dev(rdev)) |
| 71 | return -EINVAL; |
| 72 | |
| 73 | inode = sysv_new_inode(dir, mode); |
| 74 | err = PTR_ERR(inode); |
| 75 | |
| 76 | if (!IS_ERR(inode)) { |
| 77 | sysv_set_inode(inode, rdev); |
| 78 | mark_inode_dirty(inode); |
| 79 | err = add_nondir(dentry, inode); |
| 80 | } |
| 81 | return err; |
| 82 | } |
| 83 | |
| 84 | static int sysv_create(struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd) |
| 85 | { |
| 86 | return sysv_mknod(dir, dentry, mode, 0); |
| 87 | } |
| 88 | |
| 89 | static int sysv_symlink(struct inode * dir, struct dentry * dentry, |
| 90 | const char * symname) |
| 91 | { |
| 92 | int err = -ENAMETOOLONG; |
| 93 | int l = strlen(symname)+1; |
| 94 | struct inode * inode; |
| 95 | |
| 96 | if (l > dir->i_sb->s_blocksize) |
| 97 | goto out; |
| 98 | |
| 99 | inode = sysv_new_inode(dir, S_IFLNK|0777); |
| 100 | err = PTR_ERR(inode); |
| 101 | if (IS_ERR(inode)) |
| 102 | goto out; |
| 103 | |
| 104 | sysv_set_inode(inode, 0); |
| 105 | err = page_symlink(inode, symname, l); |
| 106 | if (err) |
| 107 | goto out_fail; |
| 108 | |
| 109 | mark_inode_dirty(inode); |
| 110 | err = add_nondir(dentry, inode); |
| 111 | out: |
| 112 | return err; |
| 113 | |
| 114 | out_fail: |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 115 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | iput(inode); |
| 117 | goto out; |
| 118 | } |
| 119 | |
| 120 | static int sysv_link(struct dentry * old_dentry, struct inode * dir, |
| 121 | struct dentry * dentry) |
| 122 | { |
| 123 | struct inode *inode = old_dentry->d_inode; |
| 124 | |
| 125 | if (inode->i_nlink >= SYSV_SB(inode->i_sb)->s_link_max) |
| 126 | return -EMLINK; |
| 127 | |
| 128 | inode->i_ctime = CURRENT_TIME_SEC; |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 129 | inode_inc_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | atomic_inc(&inode->i_count); |
| 131 | |
| 132 | return add_nondir(dentry, inode); |
| 133 | } |
| 134 | |
| 135 | static int sysv_mkdir(struct inode * dir, struct dentry *dentry, int mode) |
| 136 | { |
| 137 | struct inode * inode; |
| 138 | int err = -EMLINK; |
| 139 | |
| 140 | if (dir->i_nlink >= SYSV_SB(dir->i_sb)->s_link_max) |
| 141 | goto out; |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 142 | inode_inc_link_count(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | inode = sysv_new_inode(dir, S_IFDIR|mode); |
| 145 | err = PTR_ERR(inode); |
| 146 | if (IS_ERR(inode)) |
| 147 | goto out_dir; |
| 148 | |
| 149 | sysv_set_inode(inode, 0); |
| 150 | |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 151 | inode_inc_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | err = sysv_make_empty(inode, dir); |
| 154 | if (err) |
| 155 | goto out_fail; |
| 156 | |
| 157 | err = sysv_add_link(dentry, inode); |
| 158 | if (err) |
| 159 | goto out_fail; |
| 160 | |
| 161 | d_instantiate(dentry, inode); |
| 162 | out: |
| 163 | return err; |
| 164 | |
| 165 | out_fail: |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 166 | inode_dec_link_count(inode); |
| 167 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | iput(inode); |
| 169 | out_dir: |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 170 | inode_dec_link_count(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | goto out; |
| 172 | } |
| 173 | |
| 174 | static int sysv_unlink(struct inode * dir, struct dentry * dentry) |
| 175 | { |
| 176 | struct inode * inode = dentry->d_inode; |
| 177 | struct page * page; |
| 178 | struct sysv_dir_entry * de; |
| 179 | int err = -ENOENT; |
| 180 | |
| 181 | de = sysv_find_entry(dentry, &page); |
| 182 | if (!de) |
| 183 | goto out; |
| 184 | |
| 185 | err = sysv_delete_entry (de, page); |
| 186 | if (err) |
| 187 | goto out; |
| 188 | |
| 189 | inode->i_ctime = dir->i_ctime; |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 190 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | out: |
| 192 | return err; |
| 193 | } |
| 194 | |
| 195 | static int sysv_rmdir(struct inode * dir, struct dentry * dentry) |
| 196 | { |
| 197 | struct inode *inode = dentry->d_inode; |
| 198 | int err = -ENOTEMPTY; |
| 199 | |
| 200 | if (sysv_empty_dir(inode)) { |
| 201 | err = sysv_unlink(dir, dentry); |
| 202 | if (!err) { |
| 203 | inode->i_size = 0; |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 204 | inode_dec_link_count(inode); |
| 205 | inode_dec_link_count(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | return err; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Anybody can rename anything with this: the permission checks are left to the |
| 213 | * higher-level routines. |
| 214 | */ |
| 215 | static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry, |
| 216 | struct inode * new_dir, struct dentry * new_dentry) |
| 217 | { |
| 218 | struct inode * old_inode = old_dentry->d_inode; |
| 219 | struct inode * new_inode = new_dentry->d_inode; |
| 220 | struct page * dir_page = NULL; |
| 221 | struct sysv_dir_entry * dir_de = NULL; |
| 222 | struct page * old_page; |
| 223 | struct sysv_dir_entry * old_de; |
| 224 | int err = -ENOENT; |
| 225 | |
| 226 | old_de = sysv_find_entry(old_dentry, &old_page); |
| 227 | if (!old_de) |
| 228 | goto out; |
| 229 | |
| 230 | if (S_ISDIR(old_inode->i_mode)) { |
| 231 | err = -EIO; |
| 232 | dir_de = sysv_dotdot(old_inode, &dir_page); |
| 233 | if (!dir_de) |
| 234 | goto out_old; |
| 235 | } |
| 236 | |
| 237 | if (new_inode) { |
| 238 | struct page * new_page; |
| 239 | struct sysv_dir_entry * new_de; |
| 240 | |
| 241 | err = -ENOTEMPTY; |
| 242 | if (dir_de && !sysv_empty_dir(new_inode)) |
| 243 | goto out_dir; |
| 244 | |
| 245 | err = -ENOENT; |
| 246 | new_de = sysv_find_entry(new_dentry, &new_page); |
| 247 | if (!new_de) |
| 248 | goto out_dir; |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 249 | inode_inc_link_count(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | sysv_set_link(new_de, new_page, old_inode); |
| 251 | new_inode->i_ctime = CURRENT_TIME_SEC; |
| 252 | if (dir_de) |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 253 | drop_nlink(new_inode); |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 254 | inode_dec_link_count(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } else { |
| 256 | if (dir_de) { |
| 257 | err = -EMLINK; |
| 258 | if (new_dir->i_nlink >= SYSV_SB(new_dir->i_sb)->s_link_max) |
| 259 | goto out_dir; |
| 260 | } |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 261 | inode_inc_link_count(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | err = sysv_add_link(new_dentry, old_inode); |
| 263 | if (err) { |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 264 | inode_dec_link_count(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | goto out_dir; |
| 266 | } |
| 267 | if (dir_de) |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 268 | inode_inc_link_count(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | sysv_delete_entry(old_de, old_page); |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 272 | inode_dec_link_count(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | if (dir_de) { |
| 275 | sysv_set_link(dir_de, dir_page, new_dir); |
Alexey Dobriyan | 4e907c3 | 2006-03-23 03:00:52 -0800 | [diff] [blame] | 276 | inode_dec_link_count(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | return 0; |
| 279 | |
| 280 | out_dir: |
| 281 | if (dir_de) { |
| 282 | kunmap(dir_page); |
| 283 | page_cache_release(dir_page); |
| 284 | } |
| 285 | out_old: |
| 286 | kunmap(old_page); |
| 287 | page_cache_release(old_page); |
| 288 | out: |
| 289 | return err; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * directories can handle most operations... |
| 294 | */ |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 295 | const struct inode_operations sysv_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | .create = sysv_create, |
| 297 | .lookup = sysv_lookup, |
| 298 | .link = sysv_link, |
| 299 | .unlink = sysv_unlink, |
| 300 | .symlink = sysv_symlink, |
| 301 | .mkdir = sysv_mkdir, |
| 302 | .rmdir = sysv_rmdir, |
| 303 | .mknod = sysv_mknod, |
| 304 | .rename = sysv_rename, |
| 305 | .getattr = sysv_getattr, |
| 306 | }; |