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