1 /* 2 * namei.c 3 * 4 * PURPOSE 5 * Inode name handling routines for the OSTA-UDF(tm) filesystem. 6 * 7 * COPYRIGHT 8 * This file is distributed under the terms of the GNU General Public 9 * License (GPL). Copies of the GPL can be obtained from: 10 * ftp://prep.ai.mit.edu/pub/gnu/GPL 11 * Each contributing author retains all rights to their own work. 12 * 13 * (C) 1998-2004 Ben Fennema 14 * (C) 1999-2000 Stelias Computing Inc 15 * 16 * HISTORY 17 * 18 * 12/12/98 blf Created. Split out the lookup code from dir.c 19 * 04/19/99 blf link, mknod, symlink support 20 */ 21 22 #include "udfdecl.h" 23 24 #include "udf_i.h" 25 #include "udf_sb.h" 26 #include <linux/string.h> 27 #include <linux/errno.h> 28 #include <linux/mm.h> 29 #include <linux/slab.h> 30 #include <linux/quotaops.h> 31 #include <linux/smp_lock.h> 32 #include <linux/buffer_head.h> 33 #include <linux/sched.h> 34 35 static inline int udf_match(int len1, const char *name1, int len2, 36 const char *name2) 37 { 38 if (len1 != len2) 39 return 0; 40 41 return !memcmp(name1, name2, len1); 42 } 43 44 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi, 45 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh, 46 uint8_t *impuse, uint8_t *fileident) 47 { 48 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag); 49 uint16_t crc; 50 int offset; 51 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse); 52 uint8_t lfi = cfi->lengthFileIdent; 53 int padlen = fibh->eoffset - fibh->soffset - liu - lfi - 54 sizeof(struct fileIdentDesc); 55 int adinicb = 0; 56 57 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 58 adinicb = 1; 59 60 offset = fibh->soffset + sizeof(struct fileIdentDesc); 61 62 if (impuse) { 63 if (adinicb || (offset + liu < 0)) { 64 memcpy((uint8_t *)sfi->impUse, impuse, liu); 65 } else if (offset >= 0) { 66 memcpy(fibh->ebh->b_data + offset, impuse, liu); 67 } else { 68 memcpy((uint8_t *)sfi->impUse, impuse, -offset); 69 memcpy(fibh->ebh->b_data, impuse - offset, 70 liu + offset); 71 } 72 } 73 74 offset += liu; 75 76 if (fileident) { 77 if (adinicb || (offset + lfi < 0)) { 78 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi); 79 } else if (offset >= 0) { 80 memcpy(fibh->ebh->b_data + offset, fileident, lfi); 81 } else { 82 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, 83 -offset); 84 memcpy(fibh->ebh->b_data, fileident - offset, 85 lfi + offset); 86 } 87 } 88 89 offset += lfi; 90 91 if (adinicb || (offset + padlen < 0)) { 92 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen); 93 } else if (offset >= 0) { 94 memset(fibh->ebh->b_data + offset, 0x00, padlen); 95 } else { 96 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset); 97 memset(fibh->ebh->b_data, 0x00, padlen + offset); 98 } 99 100 crc = udf_crc((uint8_t *)cfi + sizeof(tag), 101 sizeof(struct fileIdentDesc) - sizeof(tag), 0); 102 103 if (fibh->sbh == fibh->ebh) { 104 crc = udf_crc((uint8_t *)sfi->impUse, 105 crclen + sizeof(tag) - 106 sizeof(struct fileIdentDesc), crc); 107 } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) { 108 crc = udf_crc(fibh->ebh->b_data + 109 sizeof(struct fileIdentDesc) + 110 fibh->soffset, 111 crclen + sizeof(tag) - 112 sizeof(struct fileIdentDesc), 113 crc); 114 } else { 115 crc = udf_crc((uint8_t *)sfi->impUse, 116 -fibh->soffset - sizeof(struct fileIdentDesc), 117 crc); 118 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc); 119 } 120 121 cfi->descTag.descCRC = cpu_to_le16(crc); 122 cfi->descTag.descCRCLength = cpu_to_le16(crclen); 123 cfi->descTag.tagChecksum = udf_tag_checksum(&cfi->descTag); 124 125 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) { 126 memcpy((uint8_t *)sfi, (uint8_t *)cfi, 127 sizeof(struct fileIdentDesc)); 128 } else { 129 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset); 130 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset, 131 sizeof(struct fileIdentDesc) + fibh->soffset); 132 } 133 134 if (adinicb) { 135 mark_inode_dirty(inode); 136 } else { 137 if (fibh->sbh != fibh->ebh) 138 mark_buffer_dirty_inode(fibh->ebh, inode); 139 mark_buffer_dirty_inode(fibh->sbh, inode); 140 } 141 return 0; 142 } 143 144 static struct fileIdentDesc *udf_find_entry(struct inode *dir, 145 struct dentry *dentry, 146 struct udf_fileident_bh *fibh, 147 struct fileIdentDesc *cfi) 148 { 149 struct fileIdentDesc *fi = NULL; 150 loff_t f_pos; 151 int block, flen; 152 char *fname = NULL; 153 char *nameptr; 154 uint8_t lfi; 155 uint16_t liu; 156 loff_t size; 157 kernel_lb_addr eloc; 158 uint32_t elen; 159 sector_t offset; 160 struct extent_position epos = {}; 161 struct udf_inode_info *dinfo = UDF_I(dir); 162 163 size = udf_ext0_offset(dir) + dir->i_size; 164 f_pos = udf_ext0_offset(dir); 165 166 fibh->sbh = fibh->ebh = NULL; 167 fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1); 168 if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) { 169 if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, &epos, 170 &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) 171 goto out_err; 172 block = udf_get_lb_pblock(dir->i_sb, eloc, offset); 173 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) { 174 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 175 epos.offset -= sizeof(short_ad); 176 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 177 epos.offset -= sizeof(long_ad); 178 } else 179 offset = 0; 180 181 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block); 182 if (!fibh->sbh) 183 goto out_err; 184 } 185 186 fname = kmalloc(UDF_NAME_LEN, GFP_NOFS); 187 if (!fname) 188 goto out_err; 189 190 while (f_pos < size) { 191 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc, 192 &elen, &offset); 193 if (!fi) 194 goto out_err; 195 196 liu = le16_to_cpu(cfi->lengthOfImpUse); 197 lfi = cfi->lengthFileIdent; 198 199 if (fibh->sbh == fibh->ebh) { 200 nameptr = fi->fileIdent + liu; 201 } else { 202 int poffset; /* Unpaded ending offset */ 203 204 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + 205 liu + lfi; 206 207 if (poffset >= lfi) 208 nameptr = (uint8_t *)(fibh->ebh->b_data + 209 poffset - lfi); 210 else { 211 nameptr = fname; 212 memcpy(nameptr, fi->fileIdent + liu, 213 lfi - poffset); 214 memcpy(nameptr + lfi - poffset, 215 fibh->ebh->b_data, poffset); 216 } 217 } 218 219 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) { 220 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE)) 221 continue; 222 } 223 224 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) { 225 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE)) 226 continue; 227 } 228 229 if (!lfi) 230 continue; 231 232 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi); 233 if (flen && udf_match(flen, fname, dentry->d_name.len, 234 dentry->d_name.name)) 235 goto out_ok; 236 } 237 238 out_err: 239 fi = NULL; 240 if (fibh->sbh != fibh->ebh) 241 brelse(fibh->ebh); 242 brelse(fibh->sbh); 243 out_ok: 244 brelse(epos.bh); 245 kfree(fname); 246 247 return fi; 248 } 249 250 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry, 251 struct nameidata *nd) 252 { 253 struct inode *inode = NULL; 254 struct fileIdentDesc cfi; 255 struct udf_fileident_bh fibh; 256 257 if (dentry->d_name.len > UDF_NAME_LEN - 2) 258 return ERR_PTR(-ENAMETOOLONG); 259 260 lock_kernel(); 261 #ifdef UDF_RECOVERY 262 /* temporary shorthand for specifying files by inode number */ 263 if (!strncmp(dentry->d_name.name, ".B=", 3)) { 264 kernel_lb_addr lb = { 265 .logicalBlockNum = 0, 266 .partitionReferenceNum = 267 simple_strtoul(dentry->d_name.name + 3, 268 NULL, 0), 269 }; 270 inode = udf_iget(dir->i_sb, lb); 271 if (!inode) { 272 unlock_kernel(); 273 return ERR_PTR(-EACCES); 274 } 275 } else 276 #endif /* UDF_RECOVERY */ 277 278 if (udf_find_entry(dir, dentry, &fibh, &cfi)) { 279 if (fibh.sbh != fibh.ebh) 280 brelse(fibh.ebh); 281 brelse(fibh.sbh); 282 283 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation)); 284 if (!inode) { 285 unlock_kernel(); 286 return ERR_PTR(-EACCES); 287 } 288 } 289 unlock_kernel(); 290 d_add(dentry, inode); 291 292 return NULL; 293 } 294 295 static struct fileIdentDesc *udf_add_entry(struct inode *dir, 296 struct dentry *dentry, 297 struct udf_fileident_bh *fibh, 298 struct fileIdentDesc *cfi, int *err) 299 { 300 struct super_block *sb = dir->i_sb; 301 struct fileIdentDesc *fi = NULL; 302 char *name = NULL; 303 int namelen; 304 loff_t f_pos; 305 loff_t size = udf_ext0_offset(dir) + dir->i_size; 306 int nfidlen; 307 uint8_t lfi; 308 uint16_t liu; 309 int block; 310 kernel_lb_addr eloc; 311 uint32_t elen; 312 sector_t offset; 313 struct extent_position epos = {}; 314 struct udf_inode_info *dinfo; 315 316 fibh->sbh = fibh->ebh = NULL; 317 name = kmalloc(UDF_NAME_LEN, GFP_NOFS); 318 if (!name) { 319 *err = -ENOMEM; 320 goto out_err; 321 } 322 323 if (dentry) { 324 if (!dentry->d_name.len) { 325 *err = -EINVAL; 326 goto out_err; 327 } 328 namelen = udf_put_filename(sb, dentry->d_name.name, name, 329 dentry->d_name.len); 330 if (!namelen) { 331 *err = -ENAMETOOLONG; 332 goto out_err; 333 } 334 } else { 335 namelen = 0; 336 } 337 338 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3; 339 340 f_pos = udf_ext0_offset(dir); 341 342 fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1); 343 dinfo = UDF_I(dir); 344 if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) { 345 if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, &epos, 346 &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) { 347 block = udf_get_lb_pblock(dir->i_sb, 348 dinfo->i_location, 0); 349 fibh->soffset = fibh->eoffset = sb->s_blocksize; 350 goto add; 351 } 352 block = udf_get_lb_pblock(dir->i_sb, eloc, offset); 353 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) { 354 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 355 epos.offset -= sizeof(short_ad); 356 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 357 epos.offset -= sizeof(long_ad); 358 } else 359 offset = 0; 360 361 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block); 362 if (!fibh->sbh) { 363 *err = -EIO; 364 goto out_err; 365 } 366 367 block = dinfo->i_location.logicalBlockNum; 368 } 369 370 while (f_pos < size) { 371 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc, 372 &elen, &offset); 373 374 if (!fi) { 375 *err = -EIO; 376 goto out_err; 377 } 378 379 liu = le16_to_cpu(cfi->lengthOfImpUse); 380 lfi = cfi->lengthFileIdent; 381 382 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) { 383 if (((sizeof(struct fileIdentDesc) + 384 liu + lfi + 3) & ~3) == nfidlen) { 385 cfi->descTag.tagSerialNum = cpu_to_le16(1); 386 cfi->fileVersionNum = cpu_to_le16(1); 387 cfi->fileCharacteristics = 0; 388 cfi->lengthFileIdent = namelen; 389 cfi->lengthOfImpUse = cpu_to_le16(0); 390 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, 391 name)) 392 goto out_ok; 393 else { 394 *err = -EIO; 395 goto out_err; 396 } 397 } 398 } 399 } 400 401 add: 402 if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) { 403 elen = (elen + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1); 404 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 405 epos.offset -= sizeof(short_ad); 406 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 407 epos.offset -= sizeof(long_ad); 408 udf_write_aext(dir, &epos, eloc, elen, 1); 409 } 410 f_pos += nfidlen; 411 412 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB && 413 sb->s_blocksize - fibh->eoffset < nfidlen) { 414 brelse(epos.bh); 415 epos.bh = NULL; 416 fibh->soffset -= udf_ext0_offset(dir); 417 fibh->eoffset -= udf_ext0_offset(dir); 418 f_pos -= udf_ext0_offset(dir); 419 if (fibh->sbh != fibh->ebh) 420 brelse(fibh->ebh); 421 brelse(fibh->sbh); 422 fibh->sbh = fibh->ebh = 423 udf_expand_dir_adinicb(dir, &block, err); 424 if (!fibh->sbh) 425 goto out_err; 426 epos.block = dinfo->i_location; 427 epos.offset = udf_file_entry_alloc_offset(dir); 428 /* Load extent udf_expand_dir_adinicb() has created */ 429 udf_current_aext(dir, &epos, &eloc, &elen, 1); 430 } 431 432 if (sb->s_blocksize - fibh->eoffset >= nfidlen) { 433 fibh->soffset = fibh->eoffset; 434 fibh->eoffset += nfidlen; 435 if (fibh->sbh != fibh->ebh) { 436 brelse(fibh->sbh); 437 fibh->sbh = fibh->ebh; 438 } 439 440 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { 441 block = dinfo->i_location.logicalBlockNum; 442 fi = (struct fileIdentDesc *) 443 (dinfo->i_ext.i_data + 444 fibh->soffset - 445 udf_ext0_offset(dir) + 446 dinfo->i_lenEAttr); 447 } else { 448 block = eloc.logicalBlockNum + 449 ((elen - 1) >> 450 dir->i_sb->s_blocksize_bits); 451 fi = (struct fileIdentDesc *) 452 (fibh->sbh->b_data + fibh->soffset); 453 } 454 } else { 455 fibh->soffset = fibh->eoffset - sb->s_blocksize; 456 fibh->eoffset += nfidlen - sb->s_blocksize; 457 if (fibh->sbh != fibh->ebh) { 458 brelse(fibh->sbh); 459 fibh->sbh = fibh->ebh; 460 } 461 462 block = eloc.logicalBlockNum + ((elen - 1) >> 463 dir->i_sb->s_blocksize_bits); 464 fibh->ebh = udf_bread(dir, 465 f_pos >> dir->i_sb->s_blocksize_bits, 1, err); 466 if (!fibh->ebh) 467 goto out_err; 468 469 if (!fibh->soffset) { 470 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) == 471 (EXT_RECORDED_ALLOCATED >> 30)) { 472 block = eloc.logicalBlockNum + ((elen - 1) >> 473 dir->i_sb->s_blocksize_bits); 474 } else 475 block++; 476 477 brelse(fibh->sbh); 478 fibh->sbh = fibh->ebh; 479 fi = (struct fileIdentDesc *)(fibh->sbh->b_data); 480 } else { 481 fi = (struct fileIdentDesc *) 482 (fibh->sbh->b_data + sb->s_blocksize + 483 fibh->soffset); 484 } 485 } 486 487 memset(cfi, 0, sizeof(struct fileIdentDesc)); 488 if (UDF_SB(sb)->s_udfrev >= 0x0200) 489 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, 490 sizeof(tag)); 491 else 492 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, 493 sizeof(tag)); 494 cfi->fileVersionNum = cpu_to_le16(1); 495 cfi->lengthFileIdent = namelen; 496 cfi->lengthOfImpUse = cpu_to_le16(0); 497 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) { 498 dir->i_size += nfidlen; 499 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 500 dinfo->i_lenAlloc += nfidlen; 501 mark_inode_dirty(dir); 502 goto out_ok; 503 } else { 504 *err = -EIO; 505 goto out_err; 506 } 507 508 out_err: 509 fi = NULL; 510 if (fibh->sbh != fibh->ebh) 511 brelse(fibh->ebh); 512 brelse(fibh->sbh); 513 out_ok: 514 brelse(epos.bh); 515 kfree(name); 516 return fi; 517 } 518 519 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi, 520 struct udf_fileident_bh *fibh, 521 struct fileIdentDesc *cfi) 522 { 523 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED; 524 525 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)) 526 memset(&(cfi->icb), 0x00, sizeof(long_ad)); 527 528 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL); 529 } 530 531 static int udf_create(struct inode *dir, struct dentry *dentry, int mode, 532 struct nameidata *nd) 533 { 534 struct udf_fileident_bh fibh; 535 struct inode *inode; 536 struct fileIdentDesc cfi, *fi; 537 int err; 538 struct udf_inode_info *iinfo; 539 540 lock_kernel(); 541 inode = udf_new_inode(dir, mode, &err); 542 if (!inode) { 543 unlock_kernel(); 544 return err; 545 } 546 547 iinfo = UDF_I(inode); 548 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 549 inode->i_data.a_ops = &udf_adinicb_aops; 550 else 551 inode->i_data.a_ops = &udf_aops; 552 inode->i_op = &udf_file_inode_operations; 553 inode->i_fop = &udf_file_operations; 554 inode->i_mode = mode; 555 mark_inode_dirty(inode); 556 557 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err); 558 if (!fi) { 559 inode->i_nlink--; 560 mark_inode_dirty(inode); 561 iput(inode); 562 unlock_kernel(); 563 return err; 564 } 565 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize); 566 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location); 567 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse = 568 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL); 569 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL); 570 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 571 mark_inode_dirty(dir); 572 if (fibh.sbh != fibh.ebh) 573 brelse(fibh.ebh); 574 brelse(fibh.sbh); 575 unlock_kernel(); 576 d_instantiate(dentry, inode); 577 578 return 0; 579 } 580 581 static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode, 582 dev_t rdev) 583 { 584 struct inode *inode; 585 struct udf_fileident_bh fibh; 586 struct fileIdentDesc cfi, *fi; 587 int err; 588 struct udf_inode_info *iinfo; 589 590 if (!old_valid_dev(rdev)) 591 return -EINVAL; 592 593 lock_kernel(); 594 err = -EIO; 595 inode = udf_new_inode(dir, mode, &err); 596 if (!inode) 597 goto out; 598 599 iinfo = UDF_I(inode); 600 inode->i_uid = current->fsuid; 601 init_special_inode(inode, mode, rdev); 602 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err); 603 if (!fi) { 604 inode->i_nlink--; 605 mark_inode_dirty(inode); 606 iput(inode); 607 unlock_kernel(); 608 return err; 609 } 610 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize); 611 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location); 612 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse = 613 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL); 614 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL); 615 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 616 mark_inode_dirty(dir); 617 mark_inode_dirty(inode); 618 619 if (fibh.sbh != fibh.ebh) 620 brelse(fibh.ebh); 621 brelse(fibh.sbh); 622 d_instantiate(dentry, inode); 623 err = 0; 624 625 out: 626 unlock_kernel(); 627 return err; 628 } 629 630 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode) 631 { 632 struct inode *inode; 633 struct udf_fileident_bh fibh; 634 struct fileIdentDesc cfi, *fi; 635 int err; 636 struct udf_inode_info *dinfo = UDF_I(dir); 637 struct udf_inode_info *iinfo; 638 639 lock_kernel(); 640 err = -EMLINK; 641 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1) 642 goto out; 643 644 err = -EIO; 645 inode = udf_new_inode(dir, S_IFDIR, &err); 646 if (!inode) 647 goto out; 648 649 iinfo = UDF_I(inode); 650 inode->i_op = &udf_dir_inode_operations; 651 inode->i_fop = &udf_dir_operations; 652 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err); 653 if (!fi) { 654 inode->i_nlink--; 655 mark_inode_dirty(inode); 656 iput(inode); 657 goto out; 658 } 659 inode->i_nlink = 2; 660 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize); 661 cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location); 662 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse = 663 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL); 664 cfi.fileCharacteristics = 665 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT; 666 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL); 667 brelse(fibh.sbh); 668 inode->i_mode = S_IFDIR | mode; 669 if (dir->i_mode & S_ISGID) 670 inode->i_mode |= S_ISGID; 671 mark_inode_dirty(inode); 672 673 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err); 674 if (!fi) { 675 inode->i_nlink = 0; 676 mark_inode_dirty(inode); 677 iput(inode); 678 goto out; 679 } 680 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize); 681 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location); 682 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse = 683 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL); 684 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY; 685 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL); 686 inc_nlink(dir); 687 mark_inode_dirty(dir); 688 d_instantiate(dentry, inode); 689 if (fibh.sbh != fibh.ebh) 690 brelse(fibh.ebh); 691 brelse(fibh.sbh); 692 err = 0; 693 694 out: 695 unlock_kernel(); 696 return err; 697 } 698 699 static int empty_dir(struct inode *dir) 700 { 701 struct fileIdentDesc *fi, cfi; 702 struct udf_fileident_bh fibh; 703 loff_t f_pos; 704 loff_t size = udf_ext0_offset(dir) + dir->i_size; 705 int block; 706 kernel_lb_addr eloc; 707 uint32_t elen; 708 sector_t offset; 709 struct extent_position epos = {}; 710 struct udf_inode_info *dinfo = UDF_I(dir); 711 712 f_pos = udf_ext0_offset(dir); 713 fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1); 714 715 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 716 fibh.sbh = fibh.ebh = NULL; 717 else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, 718 &epos, &eloc, &elen, &offset) == 719 (EXT_RECORDED_ALLOCATED >> 30)) { 720 block = udf_get_lb_pblock(dir->i_sb, eloc, offset); 721 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) { 722 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 723 epos.offset -= sizeof(short_ad); 724 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 725 epos.offset -= sizeof(long_ad); 726 } else 727 offset = 0; 728 729 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block); 730 if (!fibh.sbh) { 731 brelse(epos.bh); 732 return 0; 733 } 734 } else { 735 brelse(epos.bh); 736 return 0; 737 } 738 739 while (f_pos < size) { 740 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc, 741 &elen, &offset); 742 if (!fi) { 743 if (fibh.sbh != fibh.ebh) 744 brelse(fibh.ebh); 745 brelse(fibh.sbh); 746 brelse(epos.bh); 747 return 0; 748 } 749 750 if (cfi.lengthFileIdent && 751 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) { 752 if (fibh.sbh != fibh.ebh) 753 brelse(fibh.ebh); 754 brelse(fibh.sbh); 755 brelse(epos.bh); 756 return 0; 757 } 758 } 759 760 if (fibh.sbh != fibh.ebh) 761 brelse(fibh.ebh); 762 brelse(fibh.sbh); 763 brelse(epos.bh); 764 765 return 1; 766 } 767 768 static int udf_rmdir(struct inode *dir, struct dentry *dentry) 769 { 770 int retval; 771 struct inode *inode = dentry->d_inode; 772 struct udf_fileident_bh fibh; 773 struct fileIdentDesc *fi, cfi; 774 kernel_lb_addr tloc; 775 776 retval = -ENOENT; 777 lock_kernel(); 778 fi = udf_find_entry(dir, dentry, &fibh, &cfi); 779 if (!fi) 780 goto out; 781 782 retval = -EIO; 783 tloc = lelb_to_cpu(cfi.icb.extLocation); 784 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino) 785 goto end_rmdir; 786 retval = -ENOTEMPTY; 787 if (!empty_dir(inode)) 788 goto end_rmdir; 789 retval = udf_delete_entry(dir, fi, &fibh, &cfi); 790 if (retval) 791 goto end_rmdir; 792 if (inode->i_nlink != 2) 793 udf_warning(inode->i_sb, "udf_rmdir", 794 "empty directory has nlink != 2 (%d)", 795 inode->i_nlink); 796 clear_nlink(inode); 797 inode->i_size = 0; 798 inode_dec_link_count(dir); 799 inode->i_ctime = dir->i_ctime = dir->i_mtime = 800 current_fs_time(dir->i_sb); 801 mark_inode_dirty(dir); 802 803 end_rmdir: 804 if (fibh.sbh != fibh.ebh) 805 brelse(fibh.ebh); 806 brelse(fibh.sbh); 807 808 out: 809 unlock_kernel(); 810 return retval; 811 } 812 813 static int udf_unlink(struct inode *dir, struct dentry *dentry) 814 { 815 int retval; 816 struct inode *inode = dentry->d_inode; 817 struct udf_fileident_bh fibh; 818 struct fileIdentDesc *fi; 819 struct fileIdentDesc cfi; 820 kernel_lb_addr tloc; 821 822 retval = -ENOENT; 823 lock_kernel(); 824 fi = udf_find_entry(dir, dentry, &fibh, &cfi); 825 if (!fi) 826 goto out; 827 828 retval = -EIO; 829 tloc = lelb_to_cpu(cfi.icb.extLocation); 830 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino) 831 goto end_unlink; 832 833 if (!inode->i_nlink) { 834 udf_debug("Deleting nonexistent file (%lu), %d\n", 835 inode->i_ino, inode->i_nlink); 836 inode->i_nlink = 1; 837 } 838 retval = udf_delete_entry(dir, fi, &fibh, &cfi); 839 if (retval) 840 goto end_unlink; 841 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb); 842 mark_inode_dirty(dir); 843 inode_dec_link_count(inode); 844 inode->i_ctime = dir->i_ctime; 845 retval = 0; 846 847 end_unlink: 848 if (fibh.sbh != fibh.ebh) 849 brelse(fibh.ebh); 850 brelse(fibh.sbh); 851 852 out: 853 unlock_kernel(); 854 return retval; 855 } 856 857 static int udf_symlink(struct inode *dir, struct dentry *dentry, 858 const char *symname) 859 { 860 struct inode *inode; 861 struct pathComponent *pc; 862 char *compstart; 863 struct udf_fileident_bh fibh; 864 struct extent_position epos = {}; 865 int eoffset, elen = 0; 866 struct fileIdentDesc *fi; 867 struct fileIdentDesc cfi; 868 char *ea; 869 int err; 870 int block; 871 char *name = NULL; 872 int namelen; 873 struct buffer_head *bh; 874 struct udf_inode_info *iinfo; 875 876 lock_kernel(); 877 inode = udf_new_inode(dir, S_IFLNK, &err); 878 if (!inode) 879 goto out; 880 881 name = kmalloc(UDF_NAME_LEN, GFP_NOFS); 882 if (!name) { 883 err = -ENOMEM; 884 goto out_no_entry; 885 } 886 887 iinfo = UDF_I(inode); 888 inode->i_mode = S_IFLNK | S_IRWXUGO; 889 inode->i_data.a_ops = &udf_symlink_aops; 890 inode->i_op = &page_symlink_inode_operations; 891 892 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) { 893 kernel_lb_addr eloc; 894 uint32_t elen; 895 896 block = udf_new_block(inode->i_sb, inode, 897 iinfo->i_location.partitionReferenceNum, 898 iinfo->i_location.logicalBlockNum, &err); 899 if (!block) 900 goto out_no_entry; 901 epos.block = iinfo->i_location; 902 epos.offset = udf_file_entry_alloc_offset(inode); 903 epos.bh = NULL; 904 eloc.logicalBlockNum = block; 905 eloc.partitionReferenceNum = 906 iinfo->i_location.partitionReferenceNum; 907 elen = inode->i_sb->s_blocksize; 908 iinfo->i_lenExtents = elen; 909 udf_add_aext(inode, &epos, eloc, elen, 0); 910 brelse(epos.bh); 911 912 block = udf_get_pblock(inode->i_sb, block, 913 iinfo->i_location.partitionReferenceNum, 914 0); 915 epos.bh = udf_tread(inode->i_sb, block); 916 lock_buffer(epos.bh); 917 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize); 918 set_buffer_uptodate(epos.bh); 919 unlock_buffer(epos.bh); 920 mark_buffer_dirty_inode(epos.bh, inode); 921 ea = epos.bh->b_data + udf_ext0_offset(inode); 922 } else 923 ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr; 924 925 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode); 926 pc = (struct pathComponent *)ea; 927 928 if (*symname == '/') { 929 do { 930 symname++; 931 } while (*symname == '/'); 932 933 pc->componentType = 1; 934 pc->lengthComponentIdent = 0; 935 pc->componentFileVersionNum = 0; 936 pc += sizeof(struct pathComponent); 937 elen += sizeof(struct pathComponent); 938 } 939 940 err = -ENAMETOOLONG; 941 942 while (*symname) { 943 if (elen + sizeof(struct pathComponent) > eoffset) 944 goto out_no_entry; 945 946 pc = (struct pathComponent *)(ea + elen); 947 948 compstart = (char *)symname; 949 950 do { 951 symname++; 952 } while (*symname && *symname != '/'); 953 954 pc->componentType = 5; 955 pc->lengthComponentIdent = 0; 956 pc->componentFileVersionNum = 0; 957 if (compstart[0] == '.') { 958 if ((symname - compstart) == 1) 959 pc->componentType = 4; 960 else if ((symname - compstart) == 2 && 961 compstart[1] == '.') 962 pc->componentType = 3; 963 } 964 965 if (pc->componentType == 5) { 966 namelen = udf_put_filename(inode->i_sb, compstart, name, 967 symname - compstart); 968 if (!namelen) 969 goto out_no_entry; 970 971 if (elen + sizeof(struct pathComponent) + namelen > 972 eoffset) 973 goto out_no_entry; 974 else 975 pc->lengthComponentIdent = namelen; 976 977 memcpy(pc->componentIdent, name, namelen); 978 } 979 980 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent; 981 982 if (*symname) { 983 do { 984 symname++; 985 } while (*symname == '/'); 986 } 987 } 988 989 brelse(epos.bh); 990 inode->i_size = elen; 991 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 992 iinfo->i_lenAlloc = inode->i_size; 993 mark_inode_dirty(inode); 994 995 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err); 996 if (!fi) 997 goto out_no_entry; 998 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize); 999 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location); 1000 bh = UDF_SB(inode->i_sb)->s_lvid_bh; 1001 if (bh) { 1002 struct logicalVolIntegrityDesc *lvid = 1003 (struct logicalVolIntegrityDesc *)bh->b_data; 1004 struct logicalVolHeaderDesc *lvhd; 1005 uint64_t uniqueID; 1006 lvhd = (struct logicalVolHeaderDesc *) 1007 lvid->logicalVolContentsUse; 1008 uniqueID = le64_to_cpu(lvhd->uniqueID); 1009 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse = 1010 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL); 1011 if (!(++uniqueID & 0x00000000FFFFFFFFUL)) 1012 uniqueID += 16; 1013 lvhd->uniqueID = cpu_to_le64(uniqueID); 1014 mark_buffer_dirty(bh); 1015 } 1016 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL); 1017 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 1018 mark_inode_dirty(dir); 1019 if (fibh.sbh != fibh.ebh) 1020 brelse(fibh.ebh); 1021 brelse(fibh.sbh); 1022 d_instantiate(dentry, inode); 1023 err = 0; 1024 1025 out: 1026 kfree(name); 1027 unlock_kernel(); 1028 return err; 1029 1030 out_no_entry: 1031 inode_dec_link_count(inode); 1032 iput(inode); 1033 goto out; 1034 } 1035 1036 static int udf_link(struct dentry *old_dentry, struct inode *dir, 1037 struct dentry *dentry) 1038 { 1039 struct inode *inode = old_dentry->d_inode; 1040 struct udf_fileident_bh fibh; 1041 struct fileIdentDesc cfi, *fi; 1042 int err; 1043 struct buffer_head *bh; 1044 1045 lock_kernel(); 1046 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) { 1047 unlock_kernel(); 1048 return -EMLINK; 1049 } 1050 1051 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err); 1052 if (!fi) { 1053 unlock_kernel(); 1054 return err; 1055 } 1056 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize); 1057 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location); 1058 bh = UDF_SB(inode->i_sb)->s_lvid_bh; 1059 if (bh) { 1060 struct logicalVolIntegrityDesc *lvid = 1061 (struct logicalVolIntegrityDesc *)bh->b_data; 1062 struct logicalVolHeaderDesc *lvhd; 1063 uint64_t uniqueID; 1064 lvhd = (struct logicalVolHeaderDesc *) 1065 (lvid->logicalVolContentsUse); 1066 uniqueID = le64_to_cpu(lvhd->uniqueID); 1067 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse = 1068 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL); 1069 if (!(++uniqueID & 0x00000000FFFFFFFFUL)) 1070 uniqueID += 16; 1071 lvhd->uniqueID = cpu_to_le64(uniqueID); 1072 mark_buffer_dirty(bh); 1073 } 1074 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL); 1075 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 1076 mark_inode_dirty(dir); 1077 1078 if (fibh.sbh != fibh.ebh) 1079 brelse(fibh.ebh); 1080 brelse(fibh.sbh); 1081 inc_nlink(inode); 1082 inode->i_ctime = current_fs_time(inode->i_sb); 1083 mark_inode_dirty(inode); 1084 atomic_inc(&inode->i_count); 1085 d_instantiate(dentry, inode); 1086 unlock_kernel(); 1087 1088 return 0; 1089 } 1090 1091 /* Anybody can rename anything with this: the permission checks are left to the 1092 * higher-level routines. 1093 */ 1094 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry, 1095 struct inode *new_dir, struct dentry *new_dentry) 1096 { 1097 struct inode *old_inode = old_dentry->d_inode; 1098 struct inode *new_inode = new_dentry->d_inode; 1099 struct udf_fileident_bh ofibh, nfibh; 1100 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL; 1101 struct fileIdentDesc ocfi, ncfi; 1102 struct buffer_head *dir_bh = NULL; 1103 int retval = -ENOENT; 1104 kernel_lb_addr tloc; 1105 struct udf_inode_info *old_iinfo = UDF_I(old_inode); 1106 1107 lock_kernel(); 1108 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi); 1109 if (ofi) { 1110 if (ofibh.sbh != ofibh.ebh) 1111 brelse(ofibh.ebh); 1112 brelse(ofibh.sbh); 1113 } 1114 tloc = lelb_to_cpu(ocfi.icb.extLocation); 1115 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0) 1116 != old_inode->i_ino) 1117 goto end_rename; 1118 1119 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi); 1120 if (nfi) { 1121 if (!new_inode) { 1122 if (nfibh.sbh != nfibh.ebh) 1123 brelse(nfibh.ebh); 1124 brelse(nfibh.sbh); 1125 nfi = NULL; 1126 } 1127 } 1128 if (S_ISDIR(old_inode->i_mode)) { 1129 int offset = udf_ext0_offset(old_inode); 1130 1131 if (new_inode) { 1132 retval = -ENOTEMPTY; 1133 if (!empty_dir(new_inode)) 1134 goto end_rename; 1135 } 1136 retval = -EIO; 1137 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { 1138 dir_fi = udf_get_fileident( 1139 old_iinfo->i_ext.i_data - 1140 (old_iinfo->i_efe ? 1141 sizeof(struct extendedFileEntry) : 1142 sizeof(struct fileEntry)), 1143 old_inode->i_sb->s_blocksize, &offset); 1144 } else { 1145 dir_bh = udf_bread(old_inode, 0, 0, &retval); 1146 if (!dir_bh) 1147 goto end_rename; 1148 dir_fi = udf_get_fileident(dir_bh->b_data, 1149 old_inode->i_sb->s_blocksize, &offset); 1150 } 1151 if (!dir_fi) 1152 goto end_rename; 1153 tloc = lelb_to_cpu(dir_fi->icb.extLocation); 1154 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) != 1155 old_dir->i_ino) 1156 goto end_rename; 1157 1158 retval = -EMLINK; 1159 if (!new_inode && 1160 new_dir->i_nlink >= 1161 (256 << sizeof(new_dir->i_nlink)) - 1) 1162 goto end_rename; 1163 } 1164 if (!nfi) { 1165 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, 1166 &retval); 1167 if (!nfi) 1168 goto end_rename; 1169 } 1170 1171 /* 1172 * Like most other Unix systems, set the ctime for inodes on a 1173 * rename. 1174 */ 1175 old_inode->i_ctime = current_fs_time(old_inode->i_sb); 1176 mark_inode_dirty(old_inode); 1177 1178 /* 1179 * ok, that's it 1180 */ 1181 ncfi.fileVersionNum = ocfi.fileVersionNum; 1182 ncfi.fileCharacteristics = ocfi.fileCharacteristics; 1183 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad)); 1184 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL); 1185 1186 /* The old fid may have moved - find it again */ 1187 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi); 1188 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi); 1189 1190 if (new_inode) { 1191 new_inode->i_ctime = current_fs_time(new_inode->i_sb); 1192 inode_dec_link_count(new_inode); 1193 } 1194 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb); 1195 mark_inode_dirty(old_dir); 1196 1197 if (dir_fi) { 1198 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location); 1199 udf_update_tag((char *)dir_fi, 1200 (sizeof(struct fileIdentDesc) + 1201 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3); 1202 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 1203 mark_inode_dirty(old_inode); 1204 else 1205 mark_buffer_dirty_inode(dir_bh, old_inode); 1206 1207 inode_dec_link_count(old_dir); 1208 if (new_inode) 1209 inode_dec_link_count(new_inode); 1210 else { 1211 inc_nlink(new_dir); 1212 mark_inode_dirty(new_dir); 1213 } 1214 } 1215 1216 if (ofi) { 1217 if (ofibh.sbh != ofibh.ebh) 1218 brelse(ofibh.ebh); 1219 brelse(ofibh.sbh); 1220 } 1221 1222 retval = 0; 1223 1224 end_rename: 1225 brelse(dir_bh); 1226 if (nfi) { 1227 if (nfibh.sbh != nfibh.ebh) 1228 brelse(nfibh.ebh); 1229 brelse(nfibh.sbh); 1230 } 1231 unlock_kernel(); 1232 1233 return retval; 1234 } 1235 1236 const struct inode_operations udf_dir_inode_operations = { 1237 .lookup = udf_lookup, 1238 .create = udf_create, 1239 .link = udf_link, 1240 .unlink = udf_unlink, 1241 .symlink = udf_symlink, 1242 .mkdir = udf_mkdir, 1243 .rmdir = udf_rmdir, 1244 .mknod = udf_mknod, 1245 .rename = udf_rename, 1246 }; 1247