Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ufs/namei.c |
| 3 | * |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 4 | * Migration to usage of "page cache" on May 2006 by |
| 5 | * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base. |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Copyright (C) 1998 |
| 8 | * Daniel Pirkl <daniel.pirkl@email.cz> |
| 9 | * Charles University, Faculty of Mathematics and Physics |
| 10 | * |
| 11 | * from |
| 12 | * |
| 13 | * linux/fs/ext2/namei.c |
| 14 | * |
| 15 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 16 | * Remy Card (card@masi.ibp.fr) |
| 17 | * Laboratoire MASI - Institut Blaise Pascal |
| 18 | * Universite Pierre et Marie Curie (Paris VI) |
| 19 | * |
| 20 | * from |
| 21 | * |
| 22 | * linux/fs/minix/namei.c |
| 23 | * |
| 24 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 25 | * |
| 26 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 27 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 28 | */ |
| 29 | |
| 30 | #include <linux/time.h> |
| 31 | #include <linux/fs.h> |
| 32 | #include <linux/ufs_fs.h> |
| 33 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "swab.h" /* will go away - see comment in mknod() */ |
| 35 | #include "util.h" |
| 36 | |
| 37 | /* |
| 38 | #undef UFS_NAMEI_DEBUG |
| 39 | */ |
| 40 | #define UFS_NAMEI_DEBUG |
| 41 | |
| 42 | #ifdef UFS_NAMEI_DEBUG |
| 43 | #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x; |
| 44 | #else |
| 45 | #define UFSD(x) |
| 46 | #endif |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode) |
| 49 | { |
| 50 | int err = ufs_add_link(dentry, inode); |
| 51 | if (!err) { |
| 52 | d_instantiate(dentry, inode); |
| 53 | return 0; |
| 54 | } |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 55 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | iput(inode); |
| 57 | return err; |
| 58 | } |
| 59 | |
| 60 | static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd) |
| 61 | { |
| 62 | struct inode * inode = NULL; |
| 63 | ino_t ino; |
| 64 | |
| 65 | if (dentry->d_name.len > UFS_MAXNAMLEN) |
| 66 | return ERR_PTR(-ENAMETOOLONG); |
| 67 | |
| 68 | lock_kernel(); |
| 69 | ino = ufs_inode_by_name(dir, dentry); |
| 70 | if (ino) { |
| 71 | inode = iget(dir->i_sb, ino); |
| 72 | if (!inode) { |
| 73 | unlock_kernel(); |
| 74 | return ERR_PTR(-EACCES); |
| 75 | } |
| 76 | } |
| 77 | unlock_kernel(); |
| 78 | d_add(dentry, inode); |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * By the time this is called, we already have created |
| 84 | * the directory cache entry for the new file, but it |
| 85 | * is so far negative - it has no inode. |
| 86 | * |
| 87 | * If the create succeeds, we fill in the inode information |
| 88 | * with d_instantiate(). |
| 89 | */ |
| 90 | static int ufs_create (struct inode * dir, struct dentry * dentry, int mode, |
| 91 | struct nameidata *nd) |
| 92 | { |
| 93 | struct inode * inode = ufs_new_inode(dir, mode); |
| 94 | int err = PTR_ERR(inode); |
| 95 | if (!IS_ERR(inode)) { |
| 96 | inode->i_op = &ufs_file_inode_operations; |
| 97 | inode->i_fop = &ufs_file_operations; |
| 98 | inode->i_mapping->a_ops = &ufs_aops; |
| 99 | mark_inode_dirty(inode); |
| 100 | lock_kernel(); |
| 101 | err = ufs_add_nondir(dentry, inode); |
| 102 | unlock_kernel(); |
| 103 | } |
| 104 | return err; |
| 105 | } |
| 106 | |
| 107 | static int ufs_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev) |
| 108 | { |
| 109 | struct inode *inode; |
| 110 | int err; |
| 111 | |
| 112 | if (!old_valid_dev(rdev)) |
| 113 | return -EINVAL; |
| 114 | inode = ufs_new_inode(dir, mode); |
| 115 | err = PTR_ERR(inode); |
| 116 | if (!IS_ERR(inode)) { |
| 117 | init_special_inode(inode, mode, rdev); |
| 118 | /* NOTE: that'll go when we get wide dev_t */ |
| 119 | ufs_set_inode_dev(inode->i_sb, UFS_I(inode), rdev); |
| 120 | mark_inode_dirty(inode); |
| 121 | lock_kernel(); |
| 122 | err = ufs_add_nondir(dentry, inode); |
| 123 | unlock_kernel(); |
| 124 | } |
| 125 | return err; |
| 126 | } |
| 127 | |
| 128 | static int ufs_symlink (struct inode * dir, struct dentry * dentry, |
| 129 | const char * symname) |
| 130 | { |
| 131 | struct super_block * sb = dir->i_sb; |
| 132 | int err = -ENAMETOOLONG; |
| 133 | unsigned l = strlen(symname)+1; |
| 134 | struct inode * inode; |
| 135 | |
| 136 | if (l > sb->s_blocksize) |
| 137 | goto out; |
| 138 | |
| 139 | lock_kernel(); |
| 140 | inode = ufs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 141 | err = PTR_ERR(inode); |
| 142 | if (IS_ERR(inode)) |
| 143 | goto out; |
| 144 | |
| 145 | if (l > UFS_SB(sb)->s_uspi->s_maxsymlinklen) { |
| 146 | /* slow symlink */ |
| 147 | inode->i_op = &page_symlink_inode_operations; |
| 148 | inode->i_mapping->a_ops = &ufs_aops; |
| 149 | err = page_symlink(inode, symname, l); |
| 150 | if (err) |
| 151 | goto out_fail; |
| 152 | } else { |
| 153 | /* fast symlink */ |
| 154 | inode->i_op = &ufs_fast_symlink_inode_operations; |
| 155 | memcpy((char*)&UFS_I(inode)->i_u1.i_data,symname,l); |
| 156 | inode->i_size = l-1; |
| 157 | } |
| 158 | mark_inode_dirty(inode); |
| 159 | |
| 160 | err = ufs_add_nondir(dentry, inode); |
| 161 | out: |
| 162 | unlock_kernel(); |
| 163 | return err; |
| 164 | |
| 165 | out_fail: |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 166 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | iput(inode); |
| 168 | goto out; |
| 169 | } |
| 170 | |
| 171 | static int ufs_link (struct dentry * old_dentry, struct inode * dir, |
| 172 | struct dentry *dentry) |
| 173 | { |
| 174 | struct inode *inode = old_dentry->d_inode; |
| 175 | int error; |
| 176 | |
| 177 | lock_kernel(); |
| 178 | if (inode->i_nlink >= UFS_LINK_MAX) { |
| 179 | unlock_kernel(); |
| 180 | return -EMLINK; |
| 181 | } |
| 182 | |
| 183 | inode->i_ctime = CURRENT_TIME_SEC; |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 184 | inode_inc_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | atomic_inc(&inode->i_count); |
| 186 | |
| 187 | error = ufs_add_nondir(dentry, inode); |
| 188 | unlock_kernel(); |
| 189 | return error; |
| 190 | } |
| 191 | |
| 192 | static int ufs_mkdir(struct inode * dir, struct dentry * dentry, int mode) |
| 193 | { |
| 194 | struct inode * inode; |
| 195 | int err = -EMLINK; |
| 196 | |
| 197 | if (dir->i_nlink >= UFS_LINK_MAX) |
| 198 | goto out; |
| 199 | |
| 200 | lock_kernel(); |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 201 | inode_inc_link_count(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | inode = ufs_new_inode(dir, S_IFDIR|mode); |
| 204 | err = PTR_ERR(inode); |
| 205 | if (IS_ERR(inode)) |
| 206 | goto out_dir; |
| 207 | |
| 208 | inode->i_op = &ufs_dir_inode_operations; |
| 209 | inode->i_fop = &ufs_dir_operations; |
Evgeniy Dushistov | 826843a | 2006-06-25 05:47:21 -0700 | [diff] [blame] | 210 | inode->i_mapping->a_ops = &ufs_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 212 | inode_inc_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | err = ufs_make_empty(inode, dir); |
| 215 | if (err) |
| 216 | goto out_fail; |
| 217 | |
| 218 | err = ufs_add_link(dentry, inode); |
| 219 | if (err) |
| 220 | goto out_fail; |
| 221 | unlock_kernel(); |
| 222 | |
| 223 | d_instantiate(dentry, inode); |
| 224 | out: |
| 225 | return err; |
| 226 | |
| 227 | out_fail: |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 228 | inode_dec_link_count(inode); |
| 229 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | iput (inode); |
| 231 | out_dir: |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 232 | inode_dec_link_count(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | unlock_kernel(); |
| 234 | goto out; |
| 235 | } |
| 236 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 237 | static int ufs_unlink(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | struct inode * inode = dentry->d_inode; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 240 | struct ufs_dir_entry *de; |
| 241 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | int err = -ENOENT; |
| 243 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 244 | de = ufs_find_entry(dir, dentry, &page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (!de) |
| 246 | goto out; |
| 247 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 248 | err = ufs_delete_entry(dir, de, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | if (err) |
| 250 | goto out; |
| 251 | |
| 252 | inode->i_ctime = dir->i_ctime; |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 253 | inode_dec_link_count(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | err = 0; |
| 255 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return err; |
| 257 | } |
| 258 | |
| 259 | static int ufs_rmdir (struct inode * dir, struct dentry *dentry) |
| 260 | { |
| 261 | struct inode * inode = dentry->d_inode; |
| 262 | int err= -ENOTEMPTY; |
| 263 | |
| 264 | lock_kernel(); |
| 265 | if (ufs_empty_dir (inode)) { |
| 266 | err = ufs_unlink(dir, dentry); |
| 267 | if (!err) { |
| 268 | inode->i_size = 0; |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 269 | inode_dec_link_count(inode); |
| 270 | inode_dec_link_count(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | unlock_kernel(); |
| 274 | return err; |
| 275 | } |
| 276 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 277 | static int ufs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 278 | struct inode *new_dir, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
| 280 | struct inode *old_inode = old_dentry->d_inode; |
| 281 | struct inode *new_inode = new_dentry->d_inode; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 282 | struct page *dir_page = NULL; |
| 283 | struct ufs_dir_entry * dir_de = NULL; |
| 284 | struct page *old_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | struct ufs_dir_entry *old_de; |
| 286 | int err = -ENOENT; |
| 287 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 288 | old_de = ufs_find_entry(old_dir, old_dentry, &old_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | if (!old_de) |
| 290 | goto out; |
| 291 | |
| 292 | if (S_ISDIR(old_inode->i_mode)) { |
| 293 | err = -EIO; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 294 | dir_de = ufs_dotdot(old_inode, &dir_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (!dir_de) |
| 296 | goto out_old; |
| 297 | } |
| 298 | |
| 299 | if (new_inode) { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 300 | struct page *new_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | struct ufs_dir_entry *new_de; |
| 302 | |
| 303 | err = -ENOTEMPTY; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 304 | if (dir_de && !ufs_empty_dir(new_inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | goto out_dir; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | err = -ENOENT; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 308 | new_de = ufs_find_entry(new_dir, new_dentry, &new_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (!new_de) |
| 310 | goto out_dir; |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 311 | inode_inc_link_count(old_inode); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 312 | ufs_set_link(new_dir, new_de, new_page, old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | new_inode->i_ctime = CURRENT_TIME_SEC; |
| 314 | if (dir_de) |
| 315 | new_inode->i_nlink--; |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 316 | inode_dec_link_count(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } else { |
| 318 | if (dir_de) { |
| 319 | err = -EMLINK; |
| 320 | if (new_dir->i_nlink >= UFS_LINK_MAX) |
| 321 | goto out_dir; |
| 322 | } |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 323 | inode_inc_link_count(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | err = ufs_add_link(new_dentry, old_inode); |
| 325 | if (err) { |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 326 | inode_dec_link_count(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | goto out_dir; |
| 328 | } |
| 329 | if (dir_de) |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 330 | inode_inc_link_count(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 333 | /* |
| 334 | * Like most other Unix systems, set the ctime for inodes on a |
| 335 | * rename. |
| 336 | * inode_dec_link_count() will mark the inode dirty. |
| 337 | */ |
| 338 | old_inode->i_ctime = CURRENT_TIME_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 340 | ufs_delete_entry(old_dir, old_de, old_page); |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 341 | inode_dec_link_count(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | if (dir_de) { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 344 | ufs_set_link(old_inode, dir_de, dir_page, new_dir); |
Alexey Dobriyan | 3257545 | 2006-03-23 03:00:53 -0800 | [diff] [blame] | 345 | inode_dec_link_count(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return 0; |
| 348 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | out_dir: |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 351 | if (dir_de) { |
| 352 | kunmap(dir_page); |
| 353 | page_cache_release(dir_page); |
| 354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | out_old: |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 356 | kunmap(old_page); |
| 357 | page_cache_release(old_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | return err; |
| 360 | } |
| 361 | |
| 362 | struct inode_operations ufs_dir_inode_operations = { |
| 363 | .create = ufs_create, |
| 364 | .lookup = ufs_lookup, |
| 365 | .link = ufs_link, |
| 366 | .unlink = ufs_unlink, |
| 367 | .symlink = ufs_symlink, |
| 368 | .mkdir = ufs_mkdir, |
| 369 | .rmdir = ufs_rmdir, |
| 370 | .mknod = ufs_mknod, |
| 371 | .rename = ufs_rename, |
| 372 | }; |