* better
[mascara-docs.git] / i386 / linux-2.3.21 / fs / udf / namei.c
blob01b3979c91a6535a65fa7e2be34ffbccfad76c47
1 /*
2 * namei.c
4 * PURPOSE
5 * Inode name handling routines for the OSTA-UDF(tm) filesystem.
7 * CONTACTS
8 * E-mail regarding any portion of the Linux UDF file system should be
9 * directed to the development team mailing list (run by majordomo):
10 * linux_udf@hootie.lvld.hp.com
12 * COPYRIGHT
13 * This file is distributed under the terms of the GNU General Public
14 * License (GPL). Copies of the GPL can be obtained from:
15 * ftp://prep.ai.mit.edu/pub/gnu/GPL
16 * Each contributing author retains all rights to their own work.
18 * (C) 1998-1999 Ben Fennema
19 * (C) 1999 Stelias Computing Inc
21 * HISTORY
23 * 12/12/98 blf Created. Split out the lookup code from dir.c
24 * 04/19/99 blf link, mknod, symlink support
28 #if defined(__linux__) && defined(__KERNEL__)
29 #include <linux/version.h>
30 #include "udf_i.h"
31 #include "udf_sb.h"
32 #include <linux/string.h>
33 #include <linux/errno.h>
34 #include <linux/mm.h>
35 #include <linux/malloc.h>
36 #include <linux/udf_fs.h>
37 #endif
39 #include "udfdecl.h"
41 static inline int udf_match(int len, const char * const name, struct qstr *qs)
43 if (len != qs->len)
44 return 0;
45 return !memcmp(name, qs->name, len);
48 int udf_write_fi(struct FileIdentDesc *cfi, struct FileIdentDesc *sfi,
49 struct udf_fileident_bh *fibh,
50 Uint8 *impuse, Uint8 *fileident)
52 struct FileIdentDesc *efi;
53 Uint16 crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
54 Uint16 crc;
55 Uint8 checksum = 0;
56 int i;
57 int offset, len;
58 int padlen = fibh->eoffset - fibh->soffset - cfi->lengthOfImpUse - cfi->lengthFileIdent -
59 sizeof(struct FileIdentDesc);
61 crc = udf_crc((Uint8 *)cfi + sizeof(tag), sizeof(struct FileIdentDesc) -
62 sizeof(tag), 0);
63 efi = (struct FileIdentDesc *)(fibh->ebh->b_data + fibh->soffset);
64 if (fibh->sbh == fibh->ebh ||
65 (!fileident &&
66 (sizeof(struct FileIdentDesc) + (impuse ? cfi->lengthOfImpUse : 0))
67 <= -fibh->soffset))
69 memcpy((Uint8 *)sfi, (Uint8 *)cfi, sizeof(struct FileIdentDesc));
71 if (impuse)
72 memcpy(sfi->impUse, impuse, cfi->lengthOfImpUse);
74 if (fileident)
75 memcpy(sfi->fileIdent + cfi->lengthOfImpUse, fileident,
76 cfi->lengthFileIdent);
78 /* Zero padding */
79 memset(sfi->fileIdent + cfi->lengthOfImpUse + cfi->lengthFileIdent, 0,
80 padlen);
82 if (fibh->sbh == fibh->ebh)
83 crc = udf_crc((Uint8 *)sfi + sizeof(tag), crclen, 0);
84 else
86 crc = udf_crc((Uint8 *)sfi + sizeof(tag), crclen - fibh->eoffset, 0);
87 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
90 sfi->descTag.descCRC = cpu_to_le32(crc);
91 sfi->descTag.descCRCLength = cpu_to_le16(crclen);
93 for (i=0; i<16; i++)
94 if (i != 4)
95 checksum += ((Uint8 *)&sfi->descTag)[i];
97 sfi->descTag.tagChecksum = checksum;
99 mark_buffer_dirty(fibh->sbh, 1);
101 else
103 offset = -fibh->soffset;
104 len = sizeof(struct FileIdentDesc);
106 if (len <= offset)
107 memcpy((Uint8 *)sfi, (Uint8 *)cfi, len);
108 else
110 memcpy((Uint8 *)sfi, (Uint8 *)cfi, offset);
111 memcpy(fibh->ebh->b_data, (Uint8 *)cfi + offset, len - offset);
114 offset -= len;
115 len = cfi->lengthOfImpUse;
117 if (impuse)
119 if (offset <= 0)
120 memcpy(efi->impUse, impuse, len);
121 else if (sizeof(struct FileIdentDesc) + len <= -fibh->soffset)
122 memcpy(sfi->impUse, impuse, len);
123 else
125 memcpy(sfi->impUse, impuse, offset);
126 memcpy(efi->impUse + offset, impuse + offset, len - offset);
130 offset -= len;
131 len = cfi->lengthFileIdent;
133 if (fileident)
135 if (offset <= 0)
136 memcpy(efi->fileIdent + cfi->lengthOfImpUse, fileident, len);
137 else
139 memcpy(sfi->fileIdent + cfi->lengthOfImpUse, fileident, offset);
140 memcpy(efi->fileIdent + cfi->lengthOfImpUse + offset,
141 fileident + offset, len - offset);
145 /* Zero padding */
146 memset(efi->fileIdent + cfi->lengthOfImpUse + cfi->lengthFileIdent, 0x00,
147 padlen);
149 if (sizeof(tag) < -fibh->soffset)
151 crc = udf_crc((Uint8 *)sfi + sizeof(tag), crclen - fibh->eoffset, 0);
152 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
154 else
155 crc = udf_crc((Uint8 *)fibh->ebh->b_data + fibh->eoffset - crclen, crclen, 0);
157 if (&(efi->descTag.descCRC) < (Uint16 *)fibh->ebh->b_data)
159 sfi->descTag.descCRC = cpu_to_le16(crc);
160 sfi->descTag.descCRCLength = cpu_to_le16(crclen);
162 else
164 efi->descTag.descCRC = cpu_to_le16(crc);
165 efi->descTag.descCRCLength = cpu_to_le16(crclen);
168 for (i=0; i<16; i++)
170 if (i != 4)
172 if (&(((Uint8 *)&efi->descTag)[i]) < (Uint8 *)fibh->ebh->b_data)
173 checksum += ((Uint8 *)&sfi->descTag)[i];
174 else
175 checksum += ((Uint8 *)&efi->descTag)[i];
179 if (&(cfi->descTag.tagChecksum) < (Uint8 *)fibh->ebh->b_data)
180 sfi->descTag.tagChecksum = checksum;
181 else
182 efi->descTag.tagChecksum = checksum;
184 mark_buffer_dirty(fibh->sbh, 1);
185 mark_buffer_dirty(fibh->ebh, 1);
187 return 0;
190 static struct FileIdentDesc *
191 udf_find_entry(struct inode *dir, struct dentry *dentry,
192 struct udf_fileident_bh *fibh,
193 struct FileIdentDesc *cfi)
195 struct FileIdentDesc *fi=NULL;
196 int f_pos, block;
197 int flen;
198 char fname[255];
199 char *nameptr;
200 Uint8 lfi;
201 Uint16 liu;
202 int size = (UDF_I_EXT0OFFS(dir) + dir->i_size) >> 2;
203 lb_addr bloc, eloc;
204 Uint32 extoffset, elen, offset;
205 struct buffer_head *bh = NULL;
207 if (!dir)
208 return NULL;
210 f_pos = (UDF_I_EXT0OFFS(dir) >> 2);
212 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
213 if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
214 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)
216 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
217 if (++offset < (elen >> dir->i_sb->s_blocksize_bits))
219 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
220 extoffset -= sizeof(short_ad);
221 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
222 extoffset -= sizeof(long_ad);
224 else
225 offset = 0;
227 else
229 udf_release_data(bh);
230 return NULL;
233 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block, dir->i_sb->s_blocksize)))
235 udf_debug("udf_tread failed: block=%d\n", block);
236 udf_release_data(bh);
237 return NULL;
240 while ( (f_pos < size) )
242 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &offset, &bh);
243 liu = le16_to_cpu(cfi->lengthOfImpUse);
244 lfi = cfi->lengthFileIdent;
246 if (!fi)
248 if (fibh->sbh != fibh->ebh)
249 udf_release_data(fibh->ebh);
250 udf_release_data(fibh->sbh);
251 udf_release_data(bh);
252 return NULL;
255 if (fibh->sbh == fibh->ebh)
257 nameptr = fi->fileIdent + liu;
259 else
261 int poffset; /* Unpaded ending offset */
263 poffset = fibh->soffset + sizeof(struct FileIdentDesc) + liu + lfi;
265 if (poffset >= lfi)
266 nameptr = (Uint8 *)(fibh->ebh->b_data + poffset - lfi);
267 else
269 nameptr = fname;
270 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
271 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
275 if ( (cfi->fileCharacteristics & FILE_DELETED) != 0 )
277 if ( !IS_UNDELETE(dir->i_sb) )
278 continue;
281 if ( (cfi->fileCharacteristics & FILE_HIDDEN) != 0 )
283 if ( !IS_UNHIDE(dir->i_sb) )
284 continue;
287 if (!lfi)
288 continue;
290 if ((flen = udf_get_filename(nameptr, fname, lfi)))
292 if (udf_match(flen, fname, &(dentry->d_name)))
294 udf_release_data(bh);
295 return fi;
299 if (fibh->sbh != fibh->ebh)
300 udf_release_data(fibh->ebh);
301 udf_release_data(fibh->sbh);
302 udf_release_data(bh);
303 return NULL;
307 * udf_lookup
309 * PURPOSE
310 * Look-up the inode for a given name.
312 * DESCRIPTION
313 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
314 * available for a directory. The filesystem is useless if this routine is
315 * not available for at least the filesystem's root directory.
317 * This routine is passed an incomplete dentry - it must be completed by
318 * calling d_add(dentry, inode). If the name does not exist, then the
319 * specified inode must be set to null. An error should only be returned
320 * when the lookup fails for a reason other than the name not existing.
321 * Note that the directory inode semaphore is held during the call.
323 * Refer to lookup_dentry() in fs/namei.c
324 * lookup_dentry() -> lookup() -> real_lookup() -> .
326 * PRE-CONDITIONS
327 * dir Pointer to inode of parent directory.
328 * dentry Pointer to dentry to complete.
330 * POST-CONDITIONS
331 * <return> Zero on success.
333 * HISTORY
334 * July 1, 1997 - Andrew E. Mileski
335 * Written, tested, and released.
338 struct dentry *
339 udf_lookup(struct inode *dir, struct dentry *dentry)
341 struct inode *inode = NULL;
342 struct FileIdentDesc cfi, *fi;
343 struct udf_fileident_bh fibh;
345 #ifdef UDF_RECOVERY
346 /* temporary shorthand for specifying files by inode number */
347 if (!strncmp(dentry->d_name.name, ".B=", 3) )
349 lb_addr lb = { 0, simple_strtoul(dentry->d_name.name+3, NULL, 0) };
350 inode = udf_iget(dir->i_sb, lb);
351 if (!inode)
352 return ERR_PTR(-EACCES);
354 else
355 #endif /* UDF_RECOVERY */
357 if ((fi = udf_find_entry(dir, dentry, &fibh, &cfi)))
359 if (fibh.sbh != fibh.ebh)
360 udf_release_data(fibh.ebh);
361 udf_release_data(fibh.sbh);
363 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
364 if ( !inode )
365 return ERR_PTR(-EACCES);
367 d_add(dentry, inode);
368 return NULL;
371 static struct FileIdentDesc *
372 udf_add_entry(struct inode *dir, struct dentry *dentry,
373 struct udf_fileident_bh *fibh,
374 struct FileIdentDesc *cfi, int *err)
376 struct super_block *sb;
377 struct FileIdentDesc *fi=NULL;
378 struct ustr unifilename;
379 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
380 int namelen;
381 int f_pos;
382 int flen;
383 char *nameptr;
384 int size = (UDF_I_EXT0OFFS(dir) + dir->i_size) >> 2;
385 int nfidlen;
386 Uint8 lfi;
387 Uint16 liu;
388 int block;
389 lb_addr bloc, eloc;
390 Uint32 extoffset, elen, offset;
391 struct buffer_head *bh = NULL;
393 *err = -EINVAL;
394 if (!dir || !dir->i_nlink)
395 return NULL;
396 sb = dir->i_sb;
398 if (!dentry->d_name.len)
399 return NULL;
401 if (dir->i_size == 0)
403 *err = -ENOENT;
404 return NULL;
407 if ( !(udf_char_to_ustr(&unifilename, dentry->d_name.name, dentry->d_name.len)) )
409 *err = -ENAMETOOLONG;
410 return NULL;
413 if ( !(namelen = udf_UTF8toCS0(name, &unifilename, UDF_NAME_LEN)) )
414 return 0;
416 nfidlen = (sizeof(struct FileIdentDesc) + 0 + namelen + 3) & ~3;
418 f_pos = (UDF_I_EXT0OFFS(dir) >> 2);
420 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
421 if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
422 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)
424 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
425 if (++offset < (elen >> dir->i_sb->s_blocksize_bits))
427 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
428 extoffset -= sizeof(short_ad);
429 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
430 extoffset -= sizeof(long_ad);
432 else
433 offset = 0;
435 else
437 udf_release_data(bh);
438 return NULL;
441 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block, dir->i_sb->s_blocksize)))
442 return NULL;
444 block = UDF_I_LOCATION(dir).logicalBlockNum;
446 while ( (f_pos < size) )
448 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &offset, &bh);
449 liu = le16_to_cpu(cfi->lengthOfImpUse);
450 lfi = cfi->lengthFileIdent;
452 if (!fi)
454 if (fibh->sbh != fibh->ebh)
455 udf_release_data(fibh->ebh);
456 udf_release_data(fibh->sbh);
457 udf_release_data(bh);
458 return NULL;
461 if (fibh->sbh == fibh->ebh)
462 nameptr = fi->fileIdent + liu;
463 else
465 int poffset; /* Unpaded ending offset */
467 poffset = fibh->soffset + sizeof(struct FileIdentDesc) + liu + lfi;
469 if (poffset >= lfi)
470 nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
471 else
473 nameptr = fname;
474 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
475 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
479 if ( (cfi->fileCharacteristics & FILE_DELETED) != 0 )
481 if (((sizeof(struct FileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen)
483 udf_release_data(bh);
484 cfi->descTag.tagSerialNum = cpu_to_le16(1);
485 cfi->fileVersionNum = cpu_to_le16(1);
486 cfi->fileCharacteristics = 0;
487 cfi->lengthFileIdent = namelen;
488 cfi->lengthOfImpUse = cpu_to_le16(0);
489 if (!udf_write_fi(cfi, fi, fibh, NULL, name))
490 return fi;
491 else
492 return NULL;
496 if (!lfi)
497 continue;
499 if ((flen = udf_get_filename(nameptr, fname, lfi)))
501 if (udf_match(flen, fname, &(dentry->d_name)))
503 if (fibh->sbh != fibh->ebh)
504 udf_release_data(fibh->ebh);
505 udf_release_data(fibh->sbh);
506 udf_release_data(bh);
507 *err = -EEXIST;
508 return NULL;
513 f_pos += nfidlen;
515 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB &&
516 sb->s_blocksize - fibh->eoffset < nfidlen)
518 udf_release_data(bh);
519 bh = NULL;
520 fibh->soffset -= UDF_I_EXT0OFFS(dir);
521 fibh->eoffset -= UDF_I_EXT0OFFS(dir);
522 f_pos -= (UDF_I_EXT0OFFS(dir) >> 2);
523 udf_release_data(fibh->sbh);
524 if (!(fibh->sbh = fibh->ebh = udf_expand_adinicb(dir, &block, 1, err)))
525 return NULL;
526 bloc = UDF_I_LOCATION(dir);
527 extoffset = udf_file_entry_alloc_offset(dir);
529 else
531 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
532 extoffset -= sizeof(short_ad);
533 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
534 extoffset -= sizeof(long_ad);
537 dir->i_size += nfidlen;
538 if (sb->s_blocksize - fibh->eoffset >= nfidlen)
540 fibh->soffset = fibh->eoffset;
541 fibh->eoffset += nfidlen;
542 if (fibh->sbh != fibh->ebh)
544 udf_release_data(fibh->sbh);
545 fibh->sbh = fibh->ebh;
548 if (UDF_I_ALLOCTYPE(dir) != ICB_FLAG_AD_IN_ICB)
550 Uint32 lextoffset = extoffset;
551 if (udf_next_aext(dir, &bloc, &extoffset, &eloc, &elen, &bh, 1) !=
552 EXTENT_RECORDED_ALLOCATED)
554 udf_release_data(bh);
555 udf_release_data(fibh->sbh);
556 return NULL;
558 else
560 elen += nfidlen;
561 elen = (EXTENT_RECORDED_ALLOCATED << 30) | elen;
562 udf_write_aext(dir, bloc, &lextoffset, eloc, elen, &bh, 1);
563 block = eloc.logicalBlockNum + (elen >> dir->i_sb->s_blocksize_bits);
566 else
567 block = UDF_I_LOCATION(dir).logicalBlockNum;
569 fi = (struct FileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
571 else
573 Uint32 lextoffset = extoffset;
575 fibh->soffset = fibh->eoffset - sb->s_blocksize;
576 fibh->eoffset += nfidlen - sb->s_blocksize;
577 if (fibh->sbh != fibh->ebh)
579 udf_release_data(fibh->sbh);
580 fibh->sbh = fibh->ebh;
583 if (udf_next_aext(dir, &bloc, &extoffset, &eloc, &elen, &bh, 1) !=
584 EXTENT_RECORDED_ALLOCATED)
586 udf_release_data(bh);
587 udf_release_data(fibh->sbh);
588 return NULL;
590 else
591 block = eloc.logicalBlockNum + (elen >> dir->i_sb->s_blocksize_bits);
593 *err = -ENOSPC;
594 if (!(fibh->ebh = udf_getblk(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err)))
596 udf_release_data(bh);
597 udf_release_data(fibh->sbh);
598 return NULL;
600 if (!(fibh->soffset))
602 if (udf_next_aext(dir, &bloc, &lextoffset, &eloc, &elen, &bh, 1) ==
603 EXTENT_RECORDED_ALLOCATED)
605 block = eloc.logicalBlockNum + (elen >> dir->i_sb->s_blocksize_bits);
607 else
608 block ++;
611 fi = (struct FileIdentDesc *)(fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
614 memset(cfi, 0, sizeof(struct FileIdentDesc));
615 udf_new_tag((char *)cfi, TID_FILE_IDENT_DESC, 2, 1, block, sizeof(tag));
616 cfi->fileVersionNum = cpu_to_le16(1);
617 cfi->lengthFileIdent = namelen;
618 cfi->lengthOfImpUse = cpu_to_le16(0);
619 if (!udf_write_fi(cfi, fi, fibh, NULL, name))
621 udf_release_data(bh);
622 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
623 UDF_I_LENALLOC(dir) += nfidlen;
624 dir->i_version = ++event;
625 mark_inode_dirty(dir);
626 return fi;
628 else
630 udf_release_data(bh);
631 dir->i_size -= nfidlen;
632 if (fibh->sbh != fibh->ebh)
633 udf_release_data(fibh->ebh);
634 udf_release_data(fibh->sbh);
635 return NULL;
639 static int udf_delete_entry(struct FileIdentDesc *fi,
640 struct udf_fileident_bh *fibh,
641 struct FileIdentDesc *cfi)
643 cfi->fileCharacteristics |= FILE_DELETED;
644 return udf_write_fi(cfi, fi, fibh, NULL, NULL);
647 int udf_create(struct inode *dir, struct dentry *dentry, int mode)
649 struct udf_fileident_bh fibh;
650 struct inode *inode;
651 struct FileIdentDesc cfi, *fi;
652 int err;
654 inode = udf_new_inode(dir, mode, &err);
655 if (!inode)
656 return err;
658 inode->i_op = &udf_file_inode_operations;
659 inode->i_mode = mode;
660 mark_inode_dirty(inode);
662 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
664 udf_debug("udf_add_entry failure!\n");
665 inode->i_nlink --;
666 mark_inode_dirty(inode);
667 iput(inode);
668 return err;
670 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
671 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
672 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
673 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
674 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
675 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
677 mark_inode_dirty(dir);
678 dir->i_version = ++event;
680 if (fibh.sbh != fibh.ebh)
681 udf_release_data(fibh.ebh);
682 udf_release_data(fibh.sbh);
683 d_instantiate(dentry, inode);
684 return 0;
687 int udf_mknod(struct inode * dir, struct dentry * dentry, int mode, int rdev)
689 struct inode * inode;
690 struct udf_fileident_bh fibh;
691 int err;
692 struct FileIdentDesc cfi, *fi;
694 err = -EIO;
695 inode = udf_new_inode(dir, mode, &err);
696 if (!inode)
697 goto out;
699 inode->i_uid = current->fsuid;
700 inode->i_mode = mode;
701 inode->i_op = NULL;
702 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
704 udf_debug("udf_add_entry failure!\n");
705 inode->i_nlink --;
706 mark_inode_dirty(inode);
707 iput(inode);
708 return err;
710 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
711 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
712 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
713 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
714 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
715 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
717 mark_inode_dirty(dir);
718 dir->i_version = ++event;
720 if (S_ISREG(inode->i_mode))
722 inode->i_op = &udf_file_inode_operations;
724 else if (S_ISCHR(inode->i_mode))
726 inode->i_op = &chrdev_inode_operations;
728 else if (S_ISBLK(inode->i_mode))
730 inode->i_op = &blkdev_inode_operations;
732 else if (S_ISFIFO(inode->i_mode))
734 init_fifo(inode);
736 if (S_ISBLK(mode) || S_ISCHR(mode))
737 inode->i_rdev = to_kdev_t(rdev);
738 mark_inode_dirty(inode);
740 if (fibh.sbh != fibh.ebh)
741 udf_release_data(fibh.ebh);
742 udf_release_data(fibh.sbh);
743 d_instantiate(dentry, inode);
744 err = 0;
745 out:
746 return err;
749 int udf_mkdir(struct inode * dir, struct dentry * dentry, int mode)
751 struct inode * inode;
752 struct udf_fileident_bh fibh;
753 int err;
754 struct FileEntry *fe;
755 struct FileIdentDesc cfi, *fi;
756 Uint32 loc;
758 err = -EMLINK;
759 if (dir->i_nlink >= (256<<sizeof(dir->i_nlink))-1)
760 goto out;
762 err = -EIO;
763 inode = udf_new_inode(dir, S_IFDIR, &err);
764 if (!inode)
765 goto out;
767 inode->i_op = &udf_dir_inode_operations;
768 inode->i_size = (sizeof(struct FileIdentDesc) + 3) & ~3;
769 if (UDF_I_ALLOCTYPE(inode) == ICB_FLAG_AD_IN_ICB)
771 UDF_I_EXT0LEN(inode) = inode->i_size;
772 UDF_I_EXT0LOC(inode) = UDF_I_LOCATION(inode);
773 UDF_I_LENALLOC(inode) = inode->i_size;
774 loc = UDF_I_LOCATION(inode).logicalBlockNum;
775 fibh.sbh = udf_tread(inode->i_sb, inode->i_ino, inode->i_sb->s_blocksize);
777 else
779 fibh.sbh = udf_bread (inode, 0, 1, &err);
780 loc = UDF_I_EXT0LOC(inode).logicalBlockNum;
783 if (!fibh.sbh)
785 inode->i_nlink--;
786 mark_inode_dirty(inode);
787 iput(inode);
788 goto out;
790 inode->i_nlink = 2;
791 fe = (struct FileEntry *)fibh.sbh->b_data;
792 fi = (struct FileIdentDesc *)&(fe->extendedAttr[UDF_I_LENEATTR(inode)]);
793 udf_new_tag((char *)&cfi, TID_FILE_IDENT_DESC, 2, 1, loc,
794 sizeof(struct FileIdentDesc));
795 cfi.fileVersionNum = cpu_to_le16(1);
796 cfi.fileCharacteristics = FILE_DIRECTORY | FILE_PARENT;
797 cfi.lengthFileIdent = 0;
798 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
799 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
800 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
801 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
802 cfi.lengthOfImpUse = cpu_to_le16(0);
803 fibh.ebh = fibh.sbh;
804 fibh.soffset = sizeof(struct FileEntry);
805 fibh.eoffset = sizeof(struct FileEntry) + inode->i_size;
806 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
807 udf_release_data(fibh.sbh);
808 inode->i_mode = S_IFDIR | (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask);
809 if (dir->i_mode & S_ISGID)
810 inode->i_mode |= S_ISGID;
811 mark_inode_dirty(inode);
813 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
815 udf_debug("udf_add_entry failure!\n");
816 inode->i_nlink = 0;
817 mark_inode_dirty(inode);
818 iput(inode);
819 goto out;
821 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
822 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
823 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
824 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
825 cfi.fileCharacteristics |= FILE_DIRECTORY;
826 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
827 dir->i_version = ++event;
828 dir->i_nlink++;
829 mark_inode_dirty(dir);
830 d_instantiate(dentry, inode);
831 if (fibh.sbh != fibh.ebh)
832 udf_release_data(fibh.ebh);
833 udf_release_data(fibh.sbh);
834 err = 0;
835 out:
836 return err;
839 static int empty_dir(struct inode *dir)
841 struct FileIdentDesc *fi, cfi;
842 struct udf_fileident_bh fibh;
843 int f_pos;
844 int size = (UDF_I_EXT0OFFS(dir) + dir->i_size) >> 2;
845 int block;
846 lb_addr bloc, eloc;
847 Uint32 extoffset, elen, offset;
848 struct buffer_head *bh = NULL;
850 f_pos = (UDF_I_EXT0OFFS(dir) >> 2);
852 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
853 if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
854 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)
856 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
857 if (++offset < (elen >> dir->i_sb->s_blocksize_bits))
859 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
860 extoffset -= sizeof(short_ad);
861 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
862 extoffset -= sizeof(long_ad);
864 else
865 offset = 0;
867 else
869 udf_release_data(bh);
870 return 0;
873 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block, dir->i_sb->s_blocksize)))
874 return 0;
876 while ( (f_pos < size) )
878 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &bloc, &extoffset, &offset, &bh);
880 if (!fi)
882 if (fibh.sbh != fibh.ebh)
883 udf_release_data(fibh.ebh);
884 udf_release_data(fibh.sbh);
885 udf_release_data(bh);
886 return 0;
889 if (cfi.lengthFileIdent && (cfi.fileCharacteristics & FILE_DELETED) == 0)
891 udf_release_data(bh);
892 return 0;
895 if (fibh.sbh != fibh.ebh)
896 udf_release_data(fibh.ebh);
897 udf_release_data(fibh.sbh);
898 udf_release_data(bh);
899 return 1;
902 int udf_rmdir(struct inode * dir, struct dentry * dentry)
904 int retval;
905 struct inode * inode;
906 struct udf_fileident_bh fibh;
907 struct FileIdentDesc *fi, cfi;
909 retval = -ENOENT;
910 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
911 if (!fi)
912 goto out;
914 inode = dentry->d_inode;
916 retval = -EIO;
917 if (udf_get_lb_pblock(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation), 0) != inode->i_ino)
918 goto end_rmdir;
919 retval = -ENOTEMPTY;
920 if (!empty_dir(inode))
921 goto end_rmdir;
922 retval = udf_delete_entry(fi, &fibh, &cfi);
923 dir->i_version = ++event;
924 if (retval)
925 goto end_rmdir;
926 if (inode->i_nlink != 2)
927 udf_warning(inode->i_sb, "udf_rmdir",
928 "empty directory has nlink != 2 (%d)",
929 inode->i_nlink);
930 inode->i_version = ++event;
931 inode->i_nlink = 0;
932 inode->i_size = 0;
933 mark_inode_dirty(inode);
934 dir->i_nlink --;
935 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
936 UDF_I_UCTIME(inode) = UDF_I_UCTIME(dir) = UDF_I_UMTIME(dir) = CURRENT_UTIME;
937 mark_inode_dirty(dir);
938 d_delete(dentry);
940 end_rmdir:
941 if (fibh.sbh != fibh.ebh)
942 udf_release_data(fibh.ebh);
943 udf_release_data(fibh.sbh);
944 out:
945 return retval;
948 int udf_unlink(struct inode * dir, struct dentry * dentry)
950 int retval;
951 struct inode * inode;
952 struct udf_fileident_bh fibh;
953 struct FileIdentDesc *fi;
954 struct FileIdentDesc cfi;
956 retval = -ENOENT;
957 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
958 if (!fi)
959 goto out;
961 inode = dentry->d_inode;
963 retval = -EIO;
965 if (udf_get_lb_pblock(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation), 0) !=
966 inode->i_ino)
968 goto end_unlink;
971 if (!inode->i_nlink)
973 udf_debug("Deleting nonexistent file (%lu), %d\n",
974 inode->i_ino, inode->i_nlink);
975 inode->i_nlink = 1;
977 retval = udf_delete_entry(fi, &fibh, &cfi);
978 if (retval)
979 goto end_unlink;
980 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
981 UDF_I_UCTIME(dir) = UDF_I_UMTIME(dir) = CURRENT_UTIME;
982 mark_inode_dirty(dir);
983 inode->i_nlink--;
984 mark_inode_dirty(inode);
985 inode->i_ctime = dir->i_ctime;
986 retval = 0;
987 d_delete(dentry); /* This also frees the inode */
989 end_unlink:
990 if (fibh.sbh != fibh.ebh)
991 udf_release_data(fibh.ebh);
992 udf_release_data(fibh.sbh);
993 out:
994 return retval;
997 int udf_symlink(struct inode * dir, struct dentry * dentry, const char * symname)
999 struct inode * inode;
1000 struct PathComponent *pc;
1001 struct udf_fileident_bh fibh;
1002 struct buffer_head *bh = NULL;
1003 int eoffset, elen = 0;
1004 struct FileIdentDesc *fi;
1005 struct FileIdentDesc cfi;
1006 char *ea;
1007 int err;
1009 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
1010 goto out;
1012 inode->i_mode = S_IFLNK | S_IRWXUGO;
1013 inode->i_op = &udf_symlink_inode_operations;
1015 bh = udf_tread(inode->i_sb, inode->i_ino, inode->i_sb->s_blocksize);
1016 ea = bh->b_data + udf_file_entry_alloc_offset(inode);
1018 eoffset = inode->i_sb->s_blocksize - (ea - bh->b_data);
1019 pc = (struct PathComponent *)ea;
1021 if (*symname == '/')
1025 symname++;
1026 } while (*symname == '/');
1028 pc->componentType = 1;
1029 pc->lengthComponentIdent = 0;
1030 pc->componentFileVersionNum = 0;
1031 pc += sizeof(struct PathComponent);
1032 elen += sizeof(struct PathComponent);
1035 while (*symname && eoffset > elen + sizeof(struct PathComponent))
1037 char *compstart;
1038 pc = (struct PathComponent *)(ea + elen);
1040 compstart = (char *)symname;
1044 symname++;
1045 } while (*symname && *symname != '/');
1047 pc->componentType = 5;
1048 pc->lengthComponentIdent = 0;
1049 pc->componentFileVersionNum = 0;
1050 if (pc->componentIdent[0] == '.')
1052 if (pc->lengthComponentIdent == 1)
1053 pc->componentType = 4;
1054 else if (pc->lengthComponentIdent == 2 && pc->componentIdent[1] == '.')
1055 pc->componentType = 3;
1058 if (pc->componentType == 5)
1060 if (elen + sizeof(struct PathComponent) + symname - compstart > eoffset)
1061 pc->lengthComponentIdent = eoffset - elen - sizeof(struct PathComponent);
1062 else
1063 pc->lengthComponentIdent = symname - compstart;
1065 memcpy(pc->componentIdent, compstart, pc->lengthComponentIdent);
1068 elen += sizeof(struct PathComponent) + pc->lengthComponentIdent;
1070 if (*symname)
1074 symname++;
1075 } while (*symname == '/');
1079 udf_release_data(bh);
1080 UDF_I_LENALLOC(inode) = inode->i_size = elen;
1081 mark_inode_dirty(inode);
1083 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1084 goto out;
1085 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1086 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1087 if (UDF_SB_LVIDBH(inode->i_sb))
1089 struct LogicalVolHeaderDesc *lvhd;
1090 Uint64 uniqueID;
1091 lvhd = (struct LogicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1092 uniqueID = le64_to_cpu(lvhd->uniqueID);
1093 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
1094 le32_to_cpu(uniqueID & 0x00000000FFFFFFFFUL);
1095 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1096 uniqueID += 16;
1097 lvhd->uniqueID = cpu_to_le64(uniqueID);
1098 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb), 1);
1100 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
1101 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
1103 mark_inode_dirty(dir);
1104 dir->i_version = ++event;
1106 if (fibh.sbh != fibh.ebh)
1107 udf_release_data(fibh.ebh);
1108 udf_release_data(fibh.sbh);
1109 d_instantiate(dentry, inode);
1110 err = 0;
1112 out:
1113 return err;
1116 int udf_link(struct dentry * old_dentry, struct inode * dir,
1117 struct dentry *dentry)
1119 struct inode *inode = old_dentry->d_inode;
1120 struct udf_fileident_bh fibh;
1121 int err;
1122 struct FileIdentDesc cfi, *fi;
1124 if (S_ISDIR(inode->i_mode))
1125 return -EPERM;
1127 if (inode->i_nlink >= (256<<sizeof(inode->i_nlink))-1)
1128 return -EMLINK;
1130 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1131 return err;
1132 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1133 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1134 if (UDF_SB_LVIDBH(inode->i_sb))
1136 struct LogicalVolHeaderDesc *lvhd;
1137 Uint64 uniqueID;
1138 lvhd = (struct LogicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1139 uniqueID = le64_to_cpu(lvhd->uniqueID);
1140 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
1141 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1142 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1143 uniqueID += 16;
1144 lvhd->uniqueID = cpu_to_le64(uniqueID);
1145 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb), 1);
1147 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
1148 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
1150 mark_inode_dirty(dir);
1151 dir->i_version = ++event;
1153 if (fibh.sbh != fibh.ebh)
1154 udf_release_data(fibh.ebh);
1155 udf_release_data(fibh.sbh);
1156 inode->i_nlink ++;
1157 inode->i_ctime = CURRENT_TIME;
1158 UDF_I_UCTIME(inode) = CURRENT_UTIME;
1159 mark_inode_dirty(inode);
1160 inode->i_count ++;
1161 d_instantiate(dentry, inode);
1162 return 0;
1165 /* Anybody can rename anything with this: the permission checks are left to the
1166 * higher-level routines.
1168 int udf_rename (struct inode * old_dir, struct dentry * old_dentry,
1169 struct inode * new_dir, struct dentry * new_dentry)
1171 struct inode * old_inode, * new_inode;
1172 struct udf_fileident_bh ofibh, nfibh;
1173 struct FileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
1174 struct buffer_head *dir_bh = NULL;
1175 int retval = -ENOENT;
1177 old_inode = old_dentry->d_inode;
1178 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1179 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, lelb_to_cpu(ocfi.icb.extLocation), 0) !=
1180 old_inode->i_ino)
1182 goto end_rename;
1185 new_inode = new_dentry->d_inode;
1186 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1187 if (nfi)
1189 if (!new_inode)
1191 if (nfibh.sbh != nfibh.ebh)
1192 udf_release_data(nfibh.ebh);
1193 udf_release_data(nfibh.sbh);
1194 nfi = NULL;
1196 else
1199 DQUOT_INIT(new_inode);
1203 if (S_ISDIR(old_inode->i_mode))
1205 Uint32 offset = UDF_I_EXT0OFFS(old_inode);
1207 if (new_inode)
1209 retval = -ENOTEMPTY;
1210 if (!empty_dir(new_inode))
1211 goto end_rename;
1213 retval = -EIO;
1214 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1215 if (!dir_bh)
1216 goto end_rename;
1217 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
1218 if (!dir_fi)
1219 goto end_rename;
1220 if (udf_get_lb_pblock(old_inode->i_sb, cpu_to_lelb(dir_fi->icb.extLocation), 0) !=
1221 old_dir->i_ino)
1223 goto end_rename;
1225 retval = -EMLINK;
1226 if (!new_inode && new_dir->i_nlink >= (256<<sizeof(new_dir->i_nlink))-1)
1227 goto end_rename;
1229 if (!nfi)
1231 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1232 if (!nfi)
1233 goto end_rename;
1235 new_dir->i_version = ++event;
1238 * ok, that's it
1240 ncfi.fileVersionNum = ocfi.fileVersionNum;
1241 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1242 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1243 udf_write_fi(&ncfi, nfi, &nfibh, NULL, NULL);
1245 udf_delete_entry(ofi, &ofibh, &ocfi);
1247 old_dir->i_version = ++event;
1248 if (new_inode)
1250 new_inode->i_nlink--;
1251 new_inode->i_ctime = CURRENT_TIME;
1252 UDF_I_UCTIME(new_inode) = CURRENT_UTIME;
1253 mark_inode_dirty(new_inode);
1255 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1256 UDF_I_UCTIME(old_dir) = UDF_I_UMTIME(old_dir) = CURRENT_UTIME;
1257 mark_inode_dirty(old_dir);
1259 if (dir_bh)
1261 dir_fi->icb.extLocation = lelb_to_cpu(UDF_I_LOCATION(new_dir));
1262 udf_update_tag((char *)dir_fi, sizeof(struct FileIdentDesc) +
1263 cpu_to_le16(dir_fi->lengthOfImpUse));
1264 if (UDF_I_ALLOCTYPE(old_inode) == ICB_FLAG_AD_IN_ICB)
1266 mark_inode_dirty(old_inode);
1267 old_inode->i_version = ++event;
1269 else
1270 mark_buffer_dirty(dir_bh, 1);
1271 old_dir->i_nlink --;
1272 mark_inode_dirty(old_dir);
1273 if (new_inode)
1275 new_inode->i_nlink --;
1276 mark_inode_dirty(new_inode);
1278 else
1280 new_dir->i_nlink ++;
1281 mark_inode_dirty(new_dir);
1285 retval = 0;
1287 end_rename:
1288 udf_release_data(dir_bh);
1289 if (ofi)
1291 if (ofibh.sbh != ofibh.ebh)
1292 udf_release_data(ofibh.ebh);
1293 udf_release_data(ofibh.sbh);
1295 if (nfi)
1297 if (nfibh.sbh != nfibh.ebh)
1298 udf_release_data(nfibh.ebh);
1299 udf_release_data(nfibh.sbh);
1301 return retval;