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