1 /* 2 * JFFS2 -- Journalling Flash File System, Version 2. 3 * 4 * Copyright (C) 2001-2003 Free Software Foundation, Inc. 5 * 6 * Created by David Woodhouse <[email protected]> 7 * 8 * For licensing information, see the file 'LICENCE' in this directory. 9 * 10 * $Id: dir-ecos.c,v 1.11 2005/02/08 19:36:27 lunn Exp $ 11 * 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/crc32.h> 16 #include "nodelist.h" 17 18 /***********************************************************************/ 19 20 /* Takes length argument because it can be either NUL-terminated or '/'-terminated */ 21 struct _inode *jffs2_lookup(struct _inode *dir_i, const unsigned char *d_name, int namelen) 22 { 23 struct jffs2_inode_info *dir_f; 24 struct jffs2_full_dirent *fd = NULL, *fd_list; 25 uint32_t ino = 0; 26 uint32_t hash = full_name_hash(d_name, namelen); 27 struct _inode *inode = NULL; 28 29 D1(printk("jffs2_lookup()\n")); 30 31 dir_f = JFFS2_INODE_INFO(dir_i); 32 33 down(&dir_f->sem); 34 35 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */ 36 for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= hash; fd_list = fd_list->next) { 37 if (fd_list->nhash == hash && 38 (!fd || fd_list->version > fd->version) && 39 strlen((char *)fd_list->name) == namelen && 40 !strncmp((char *)fd_list->name, (char *)d_name, namelen)) { 41 fd = fd_list; 42 } 43 } 44 if (fd) 45 ino = fd->ino; 46 up(&dir_f->sem); 47 if (ino) { 48 inode = jffs2_iget(dir_i->i_sb, ino); 49 if (IS_ERR(inode)) { 50 printk("jffs2_iget() failed for ino #%u\n", ino); 51 return inode; 52 } 53 } 54 55 return inode; 56 } 57 58 /***********************************************************************/ 59 60 61 62 int jffs2_create(struct _inode *dir_i, const unsigned char *d_name, int mode, 63 struct _inode **new_i) 64 { 65 struct jffs2_raw_inode *ri; 66 struct jffs2_inode_info *f, *dir_f; 67 struct jffs2_sb_info *c; 68 struct _inode *inode; 69 int ret; 70 71 ri = jffs2_alloc_raw_inode(); 72 if (!ri) 73 return -ENOMEM; 74 75 c = JFFS2_SB_INFO(dir_i->i_sb); 76 77 D1(printk(KERN_DEBUG "jffs2_create()\n")); 78 79 inode = jffs2_new_inode(dir_i, mode, ri); 80 81 if (IS_ERR(inode)) { 82 D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n")); 83 jffs2_free_raw_inode(ri); 84 return PTR_ERR(inode); 85 } 86 87 f = JFFS2_INODE_INFO(inode); 88 dir_f = JFFS2_INODE_INFO(dir_i); 89 90 ret = jffs2_do_create(c, dir_f, f, ri, 91 (const char *)d_name, 92 strlen((char *)d_name)); 93 94 if (ret) { 95 inode->i_nlink = 0; 96 jffs2_iput(inode); 97 jffs2_free_raw_inode(ri); 98 return ret; 99 } 100 101 jffs2_free_raw_inode(ri); 102 103 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d)\n", 104 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink)); 105 *new_i = inode; 106 return 0; 107 } 108 109 /***********************************************************************/ 110 111 112 int jffs2_unlink(struct _inode *dir_i, struct _inode *d_inode, const unsigned char *d_name) 113 { 114 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb); 115 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i); 116 struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(d_inode); 117 int ret; 118 119 ret = jffs2_do_unlink(c, dir_f, (const char *)d_name, 120 strlen((char *)d_name), dead_f); 121 if (dead_f->inocache) 122 d_inode->i_nlink = dead_f->inocache->nlink; 123 return ret; 124 } 125 /***********************************************************************/ 126 127 128 int jffs2_link (struct _inode *old_d_inode, struct _inode *dir_i, const unsigned char *d_name) 129 { 130 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_d_inode->i_sb); 131 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_d_inode); 132 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i); 133 int ret; 134 135 /* XXX: This is ugly */ 136 uint8_t type = (old_d_inode->i_mode & S_IFMT) >> 12; 137 if (!type) type = DT_REG; 138 139 ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, 140 (const char * )d_name, 141 strlen((char *)d_name)); 142 143 if (!ret) { 144 down(&f->sem); 145 old_d_inode->i_nlink = ++f->inocache->nlink; 146 up(&f->sem); 147 } 148 return ret; 149 } 150 151 int jffs2_mkdir (struct _inode *dir_i, const unsigned char *d_name, int mode) 152 { 153 struct jffs2_inode_info *f, *dir_f; 154 struct jffs2_sb_info *c; 155 struct _inode *inode; 156 struct jffs2_raw_inode *ri; 157 struct jffs2_raw_dirent *rd; 158 struct jffs2_full_dnode *fn; 159 struct jffs2_full_dirent *fd; 160 int namelen; 161 uint32_t alloclen, phys_ofs; 162 int ret; 163 164 mode |= S_IFDIR; 165 166 ri = jffs2_alloc_raw_inode(); 167 if (!ri) 168 return -ENOMEM; 169 170 c = JFFS2_SB_INFO(dir_i->i_sb); 171 172 /* Try to reserve enough space for both node and dirent. 173 * Just the node will do for now, though 174 */ 175 namelen = strlen((char *)d_name); 176 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL); 177 178 if (ret) { 179 jffs2_free_raw_inode(ri); 180 return ret; 181 } 182 183 inode = jffs2_new_inode(dir_i, mode, ri); 184 185 if (IS_ERR(inode)) { 186 jffs2_free_raw_inode(ri); 187 jffs2_complete_reservation(c); 188 return PTR_ERR(inode); 189 } 190 191 f = JFFS2_INODE_INFO(inode); 192 193 ri->data_crc = cpu_to_je32(0); 194 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 195 196 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL); 197 198 jffs2_free_raw_inode(ri); 199 200 if (IS_ERR(fn)) { 201 /* Eeek. Wave bye bye */ 202 up(&f->sem); 203 jffs2_complete_reservation(c); 204 inode->i_nlink = 0; 205 jffs2_iput(inode); 206 return PTR_ERR(fn); 207 } 208 /* No data here. Only a metadata node, which will be 209 obsoleted by the first data write 210 */ 211 f->metadata = fn; 212 up(&f->sem); 213 214 jffs2_complete_reservation(c); 215 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL); 216 if (ret) { 217 /* Eep. */ 218 inode->i_nlink = 0; 219 jffs2_iput(inode); 220 return ret; 221 } 222 223 rd = jffs2_alloc_raw_dirent(); 224 if (!rd) { 225 /* Argh. Now we treat it like a normal delete */ 226 jffs2_complete_reservation(c); 227 inode->i_nlink = 0; 228 jffs2_iput(inode); 229 return -ENOMEM; 230 } 231 232 dir_f = JFFS2_INODE_INFO(dir_i); 233 down(&dir_f->sem); 234 235 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 236 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 237 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); 238 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 239 240 rd->pino = cpu_to_je32(dir_i->i_ino); 241 rd->version = cpu_to_je32(++dir_f->highest_version); 242 rd->ino = cpu_to_je32(inode->i_ino); 243 rd->mctime = cpu_to_je32(jffs2_get_timestamp()); 244 rd->nsize = namelen; 245 rd->type = DT_DIR; 246 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 247 rd->name_crc = cpu_to_je32(crc32(0, d_name, namelen)); 248 249 fd = jffs2_write_dirent(c, dir_f, rd, d_name, namelen, phys_ofs, ALLOC_NORMAL); 250 251 jffs2_complete_reservation(c); 252 jffs2_free_raw_dirent(rd); 253 254 if (IS_ERR(fd)) { 255 /* dirent failed to write. Delete the inode normally 256 as if it were the final unlink() */ 257 up(&dir_f->sem); 258 inode->i_nlink = 0; 259 jffs2_iput(inode); 260 return PTR_ERR(fd); 261 } 262 263 /* Link the fd into the inode's list, obsoleting an old 264 one if necessary. */ 265 jffs2_add_fd_to_list(c, fd, &dir_f->dents); 266 up(&dir_f->sem); 267 268 jffs2_iput(inode); 269 return 0; 270 } 271 272 int jffs2_rmdir (struct _inode *dir_i, struct _inode *d_inode, const unsigned char *d_name) 273 { 274 struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode); 275 struct jffs2_full_dirent *fd; 276 277 for (fd = f->dents ; fd; fd = fd->next) { 278 if (fd->ino) 279 return EPERM; //-ENOTEMPTY; 280 } 281 return jffs2_unlink(dir_i, d_inode, d_name); 282 } 283 284 int jffs2_rename (struct _inode *old_dir_i, struct _inode *d_inode, const unsigned char *old_d_name, 285 struct _inode *new_dir_i, const unsigned char *new_d_name) 286 { 287 int ret; 288 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb); 289 struct jffs2_inode_info *victim_f = NULL; 290 uint8_t type; 291 292 #if 0 /* FIXME -- this really doesn't belong in individual file systems. 293 The fileio code ought to do this for us, or at least part of it */ 294 if (new_dentry->d_inode) { 295 if (S_ISDIR(d_inode->i_mode) && 296 !S_ISDIR(new_dentry->d_inode->i_mode)) { 297 /* Cannot rename directory over non-directory */ 298 return -EINVAL; 299 } 300 301 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode); 302 303 if (S_ISDIR(new_dentry->d_inode->i_mode)) { 304 struct jffs2_full_dirent *fd; 305 306 if (!S_ISDIR(d_inode->i_mode)) { 307 /* Cannot rename non-directory over directory */ 308 return -EINVAL; 309 } 310 down(&victim_f->sem); 311 for (fd = victim_f->dents; fd; fd = fd->next) { 312 if (fd->ino) { 313 up(&victim_f->sem); 314 return -ENOTEMPTY; 315 } 316 } 317 up(&victim_f->sem); 318 } 319 } 320 #endif 321 322 /* XXX: We probably ought to alloc enough space for 323 both nodes at the same time. Writing the new link, 324 then getting -ENOSPC, is quite bad :) 325 */ 326 327 /* Make a hard link */ 328 329 /* XXX: This is ugly */ 330 type = (d_inode->i_mode & S_IFMT) >> 12; 331 if (!type) type = DT_REG; 332 333 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i), 334 d_inode->i_ino, type, 335 (const char *)new_d_name, 336 strlen((char *)new_d_name)); 337 338 if (ret) 339 return ret; 340 341 if (victim_f) { 342 /* There was a victim. Kill it off nicely */ 343 /* Don't oops if the victim was a dirent pointing to an 344 inode which didn't exist. */ 345 if (victim_f->inocache) { 346 down(&victim_f->sem); 347 victim_f->inocache->nlink--; 348 up(&victim_f->sem); 349 } 350 } 351 352 /* Unlink the original */ 353 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i), 354 (const char *)old_d_name, 355 strlen((char *)old_d_name), NULL); 356 357 if (ret) { 358 /* Oh shit. We really ought to make a single node which can do both atomically */ 359 struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode); 360 down(&f->sem); 361 if (f->inocache) 362 d_inode->i_nlink = f->inocache->nlink++; 363 up(&f->sem); 364 365 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret); 366 } 367 return ret; 368 } 369 370