Import 2.3.26pre2
[davej-history.git] / fs / udf / namei.c
blob712cf09fb69e341788c1c8737e753c062f637672
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 Uint16 crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
53 Uint16 crc;
54 Uint8 checksum = 0;
55 int i;
56 int offset;
57 Uint16 liu = le16_to_cpu(cfi->lengthOfImpUse);
58 Uint8 lfi = cfi->lengthFileIdent;
59 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
60 sizeof(struct FileIdentDesc);
63 offset = fibh->soffset + sizeof(struct FileIdentDesc);
65 if (impuse)
67 if (offset + liu < 0)
68 memcpy((Uint8 *)sfi->impUse, impuse, liu);
69 else if (offset >= 0)
70 memcpy(fibh->ebh->b_data + offset, impuse, liu);
71 else
73 memcpy((Uint8 *)sfi->impUse, impuse, -offset);
74 memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);
78 offset += liu;
80 if (fileident)
82 if (offset + lfi < 0)
83 memcpy((Uint8 *)sfi->fileIdent + liu, fileident, lfi);
84 else if (offset >= 0)
85 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
86 else
88 memcpy((Uint8 *)sfi->fileIdent + liu, fileident, -offset);
89 memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);
93 offset += lfi;
95 if (offset + padlen < 0)
96 memset((Uint8 *)sfi->padding + liu + lfi, 0x00, padlen);
97 else if (offset >= 0)
98 memset(fibh->ebh->b_data + offset, 0x00, padlen);
99 else
101 memset((Uint8 *)sfi->padding + liu + lfi, 0x00, -offset);
102 memset(fibh->ebh->b_data, 0x00, padlen + offset);
105 crc = udf_crc((Uint8 *)cfi + sizeof(tag), sizeof(struct FileIdentDesc) -
106 sizeof(tag), 0);
108 if (fibh->sbh == fibh->ebh)
109 crc = udf_crc((Uint8 *)sfi->impUse,
110 crclen + sizeof(tag) - sizeof(struct FileIdentDesc), crc);
111 else if (sizeof(struct FileIdentDesc) >= -fibh->soffset)
112 crc = udf_crc(fibh->ebh->b_data + sizeof(struct FileIdentDesc) + fibh->soffset,
113 crclen + sizeof(tag) - sizeof(struct FileIdentDesc), crc);
114 else
116 crc = udf_crc((Uint8 *)sfi->impUse,
117 -fibh->soffset - sizeof(struct FileIdentDesc), crc);
118 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
121 cfi->descTag.descCRC = cpu_to_le32(crc);
122 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
124 for (i=0; i<16; i++)
125 if (i != 4)
126 checksum += ((Uint8 *)&cfi->descTag)[i];
128 cfi->descTag.tagChecksum = checksum;
129 if (sizeof(struct FileIdentDesc) <= -fibh->soffset)
130 memcpy((Uint8 *)sfi, (Uint8 *)cfi, sizeof(struct FileIdentDesc));
131 else
133 memcpy((Uint8 *)sfi, (Uint8 *)cfi, -fibh->soffset);
134 memcpy(fibh->ebh->b_data, (Uint8 *)cfi - fibh->soffset,
135 sizeof(struct FileIdentDesc) + fibh->soffset);
138 if (fibh->sbh != fibh->ebh)
139 mark_buffer_dirty(fibh->ebh, 1);
140 mark_buffer_dirty(fibh->sbh, 1);
141 return 0;
144 static struct FileIdentDesc *
145 udf_find_entry(struct inode *dir, struct dentry *dentry,
146 struct udf_fileident_bh *fibh,
147 struct FileIdentDesc *cfi)
149 struct FileIdentDesc *fi=NULL;
150 int f_pos, block;
151 int flen;
152 char fname[255];
153 char *nameptr;
154 Uint8 lfi;
155 Uint16 liu;
156 int size = (UDF_I_EXT0OFFS(dir) + dir->i_size) >> 2;
157 lb_addr bloc, eloc;
158 Uint32 extoffset, elen, offset;
159 struct buffer_head *bh = NULL;
161 if (!dir)
162 return NULL;
164 f_pos = (UDF_I_EXT0OFFS(dir) >> 2);
166 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
167 if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
168 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)
170 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
171 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
173 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
174 extoffset -= sizeof(short_ad);
175 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
176 extoffset -= sizeof(long_ad);
178 else
179 offset = 0;
181 else
183 udf_release_data(bh);
184 return NULL;
187 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block, dir->i_sb->s_blocksize)))
189 udf_debug("udf_tread failed: block=%d\n", block);
190 udf_release_data(bh);
191 return NULL;
194 while ( (f_pos < size) )
196 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &offset, &bh);
198 if (!fi)
200 if (fibh->sbh != fibh->ebh)
201 udf_release_data(fibh->ebh);
202 udf_release_data(fibh->sbh);
203 udf_release_data(bh);
204 return NULL;
207 liu = le16_to_cpu(cfi->lengthOfImpUse);
208 lfi = cfi->lengthFileIdent;
210 if (fibh->sbh == fibh->ebh)
212 nameptr = fi->fileIdent + liu;
214 else
216 int poffset; /* Unpaded ending offset */
218 poffset = fibh->soffset + sizeof(struct FileIdentDesc) + liu + lfi;
220 if (poffset >= lfi)
221 nameptr = (Uint8 *)(fibh->ebh->b_data + poffset - lfi);
222 else
224 nameptr = fname;
225 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
226 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
230 if ( (cfi->fileCharacteristics & FILE_DELETED) != 0 )
232 if ( !IS_UNDELETE(dir->i_sb) )
233 continue;
236 if ( (cfi->fileCharacteristics & FILE_HIDDEN) != 0 )
238 if ( !IS_UNHIDE(dir->i_sb) )
239 continue;
242 if (!lfi)
243 continue;
245 if ((flen = udf_get_filename(nameptr, fname, lfi)))
247 if (udf_match(flen, fname, &(dentry->d_name)))
249 udf_release_data(bh);
250 return fi;
254 if (fibh->sbh != fibh->ebh)
255 udf_release_data(fibh->ebh);
256 udf_release_data(fibh->sbh);
257 udf_release_data(bh);
258 return NULL;
262 * udf_lookup
264 * PURPOSE
265 * Look-up the inode for a given name.
267 * DESCRIPTION
268 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
269 * available for a directory. The filesystem is useless if this routine is
270 * not available for at least the filesystem's root directory.
272 * This routine is passed an incomplete dentry - it must be completed by
273 * calling d_add(dentry, inode). If the name does not exist, then the
274 * specified inode must be set to null. An error should only be returned
275 * when the lookup fails for a reason other than the name not existing.
276 * Note that the directory inode semaphore is held during the call.
278 * Refer to lookup_dentry() in fs/namei.c
279 * lookup_dentry() -> lookup() -> real_lookup() -> .
281 * PRE-CONDITIONS
282 * dir Pointer to inode of parent directory.
283 * dentry Pointer to dentry to complete.
285 * POST-CONDITIONS
286 * <return> Zero on success.
288 * HISTORY
289 * July 1, 1997 - Andrew E. Mileski
290 * Written, tested, and released.
293 struct dentry *
294 udf_lookup(struct inode *dir, struct dentry *dentry)
296 struct inode *inode = NULL;
297 struct FileIdentDesc cfi, *fi;
298 struct udf_fileident_bh fibh;
300 #ifdef UDF_RECOVERY
301 /* temporary shorthand for specifying files by inode number */
302 if (!strncmp(dentry->d_name.name, ".B=", 3) )
304 lb_addr lb = { 0, simple_strtoul(dentry->d_name.name+3, NULL, 0) };
305 inode = udf_iget(dir->i_sb, lb);
306 if (!inode)
307 return ERR_PTR(-EACCES);
309 else
310 #endif /* UDF_RECOVERY */
312 if ((fi = udf_find_entry(dir, dentry, &fibh, &cfi)))
314 if (fibh.sbh != fibh.ebh)
315 udf_release_data(fibh.ebh);
316 udf_release_data(fibh.sbh);
318 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
319 if ( !inode )
320 return ERR_PTR(-EACCES);
322 d_add(dentry, inode);
323 return NULL;
326 static struct FileIdentDesc *
327 udf_add_entry(struct inode *dir, struct dentry *dentry,
328 struct udf_fileident_bh *fibh,
329 struct FileIdentDesc *cfi, int *err)
331 struct super_block *sb;
332 struct FileIdentDesc *fi=NULL;
333 struct ustr unifilename;
334 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
335 int namelen;
336 int f_pos;
337 int flen;
338 char *nameptr;
339 int size = (UDF_I_EXT0OFFS(dir) + dir->i_size) >> 2;
340 int nfidlen;
341 Uint8 lfi;
342 Uint16 liu;
343 int block;
344 lb_addr bloc, eloc;
345 Uint32 extoffset, elen, offset;
346 struct buffer_head *bh = NULL;
348 *err = -EINVAL;
349 if (!dir || !dir->i_nlink)
350 return NULL;
351 sb = dir->i_sb;
353 if (!dentry->d_name.len)
354 return NULL;
356 if (dir->i_size == 0)
358 *err = -ENOENT;
359 return NULL;
362 if ( !(udf_char_to_ustr(&unifilename, dentry->d_name.name, dentry->d_name.len)) )
364 *err = -ENAMETOOLONG;
365 return NULL;
368 if ( !(namelen = udf_UTF8toCS0(name, &unifilename, UDF_NAME_LEN)) )
369 return 0;
371 nfidlen = (sizeof(struct FileIdentDesc) + 0 + namelen + 3) & ~3;
373 f_pos = (UDF_I_EXT0OFFS(dir) >> 2);
375 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
376 if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
377 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)
379 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
380 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
382 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
383 extoffset -= sizeof(short_ad);
384 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
385 extoffset -= sizeof(long_ad);
387 else
388 offset = 0;
390 else
392 udf_release_data(bh);
393 return NULL;
396 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block, dir->i_sb->s_blocksize)))
397 return NULL;
399 block = UDF_I_LOCATION(dir).logicalBlockNum;
401 while ( (f_pos < size) )
403 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &offset, &bh);
405 if (!fi)
407 if (fibh->sbh != fibh->ebh)
408 udf_release_data(fibh->ebh);
409 udf_release_data(fibh->sbh);
410 udf_release_data(bh);
411 return NULL;
414 liu = le16_to_cpu(cfi->lengthOfImpUse);
415 lfi = cfi->lengthFileIdent;
417 if (fibh->sbh == fibh->ebh)
418 nameptr = fi->fileIdent + liu;
419 else
421 int poffset; /* Unpaded ending offset */
423 poffset = fibh->soffset + sizeof(struct FileIdentDesc) + liu + lfi;
425 if (poffset >= lfi)
426 nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
427 else
429 nameptr = fname;
430 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
431 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
435 if ( (cfi->fileCharacteristics & FILE_DELETED) != 0 )
437 if (((sizeof(struct FileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen)
439 udf_release_data(bh);
440 cfi->descTag.tagSerialNum = cpu_to_le16(1);
441 cfi->fileVersionNum = cpu_to_le16(1);
442 cfi->fileCharacteristics = 0;
443 cfi->lengthFileIdent = namelen;
444 cfi->lengthOfImpUse = cpu_to_le16(0);
445 if (!udf_write_fi(cfi, fi, fibh, NULL, name))
446 return fi;
447 else
448 return NULL;
452 if (!lfi)
453 continue;
455 if ((flen = udf_get_filename(nameptr, fname, lfi)))
457 if (udf_match(flen, fname, &(dentry->d_name)))
459 if (fibh->sbh != fibh->ebh)
460 udf_release_data(fibh->ebh);
461 udf_release_data(fibh->sbh);
462 udf_release_data(bh);
463 *err = -EEXIST;
464 return NULL;
469 f_pos += nfidlen;
471 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB &&
472 sb->s_blocksize - fibh->eoffset < nfidlen)
474 udf_release_data(bh);
475 bh = NULL;
476 fibh->soffset -= UDF_I_EXT0OFFS(dir);
477 fibh->eoffset -= UDF_I_EXT0OFFS(dir);
478 f_pos -= (UDF_I_EXT0OFFS(dir) >> 2);
479 if (fibh->sbh != fibh->ebh)
480 udf_release_data(fibh->ebh);
481 udf_release_data(fibh->sbh);
482 if (!(fibh->sbh = fibh->ebh = udf_expand_adinicb(dir, &block, 1, err)))
483 return NULL;
484 bloc = UDF_I_LOCATION(dir);
485 extoffset = udf_file_entry_alloc_offset(dir);
487 else
489 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
490 extoffset -= sizeof(short_ad);
491 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
492 extoffset -= sizeof(long_ad);
495 dir->i_size += nfidlen;
496 if (sb->s_blocksize - fibh->eoffset >= nfidlen)
498 fibh->soffset = fibh->eoffset;
499 fibh->eoffset += nfidlen;
500 if (fibh->sbh != fibh->ebh)
502 udf_release_data(fibh->sbh);
503 fibh->sbh = fibh->ebh;
506 if (UDF_I_ALLOCTYPE(dir) != ICB_FLAG_AD_IN_ICB)
508 Uint32 lextoffset = extoffset;
509 if (udf_next_aext(dir, &bloc, &extoffset, &eloc, &elen, &bh, 1) !=
510 EXTENT_RECORDED_ALLOCATED)
512 udf_release_data(bh);
513 udf_release_data(fibh->sbh);
514 return NULL;
516 else
518 if (elen & (sb->s_blocksize - 1))
519 elen += nfidlen;
520 block = eloc.logicalBlockNum + ((elen - 1) >>
521 dir->i_sb->s_blocksize_bits);
522 elen = (EXTENT_RECORDED_ALLOCATED << 30) | elen;
523 udf_write_aext(dir, bloc, &lextoffset, eloc, elen, &bh, 1);
526 else
527 block = UDF_I_LOCATION(dir).logicalBlockNum;
529 fi = (struct FileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
531 else
533 Uint32 lextoffset = extoffset;
535 fibh->soffset = fibh->eoffset - sb->s_blocksize;
536 fibh->eoffset += nfidlen - sb->s_blocksize;
537 if (fibh->sbh != fibh->ebh)
539 udf_release_data(fibh->sbh);
540 fibh->sbh = fibh->ebh;
543 if (udf_next_aext(dir, &bloc, &extoffset, &eloc, &elen, &bh, 1) !=
544 EXTENT_RECORDED_ALLOCATED)
546 udf_release_data(bh);
547 udf_release_data(fibh->sbh);
548 return NULL;
550 else
552 elen = ((elen + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
553 block = eloc.logicalBlockNum + ((elen - 1) >>
554 dir->i_sb->s_blocksize_bits);
555 elen = (EXTENT_RECORDED_ALLOCATED << 30) | elen;
556 udf_write_aext(dir, bloc, &lextoffset, eloc, elen, &bh, 0);
559 *err = -ENOSPC;
560 if (!(fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err)))
562 udf_release_data(bh);
563 udf_release_data(fibh->sbh);
564 return NULL;
566 if (!(fibh->soffset))
568 if (udf_next_aext(dir, &bloc, &lextoffset, &eloc, &elen, &bh, 1) ==
569 EXTENT_RECORDED_ALLOCATED)
571 block = eloc.logicalBlockNum + ((elen - 1) >>
572 dir->i_sb->s_blocksize_bits);
574 else
575 block ++;
578 fi = (struct FileIdentDesc *)(fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
581 memset(cfi, 0, sizeof(struct FileIdentDesc));
582 udf_new_tag((char *)cfi, TID_FILE_IDENT_DESC, 2, 1, block, sizeof(tag));
583 cfi->fileVersionNum = cpu_to_le16(1);
584 cfi->lengthFileIdent = namelen;
585 cfi->lengthOfImpUse = cpu_to_le16(0);
586 if (!udf_write_fi(cfi, fi, fibh, NULL, name))
588 udf_release_data(bh);
589 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
590 UDF_I_LENALLOC(dir) += nfidlen;
591 dir->i_version = ++event;
592 mark_inode_dirty(dir);
593 return fi;
595 else
597 udf_release_data(bh);
598 dir->i_size -= nfidlen;
599 if (fibh->sbh != fibh->ebh)
600 udf_release_data(fibh->ebh);
601 udf_release_data(fibh->sbh);
602 return NULL;
606 static int udf_delete_entry(struct FileIdentDesc *fi,
607 struct udf_fileident_bh *fibh,
608 struct FileIdentDesc *cfi)
610 cfi->fileCharacteristics |= FILE_DELETED;
611 return udf_write_fi(cfi, fi, fibh, NULL, NULL);
614 int udf_create(struct inode *dir, struct dentry *dentry, int mode)
616 struct udf_fileident_bh fibh;
617 struct inode *inode;
618 struct FileIdentDesc cfi, *fi;
619 int err;
621 inode = udf_new_inode(dir, mode, &err);
622 if (!inode)
623 return err;
625 inode->i_op = &udf_file_inode_operations;
626 inode->i_mode = mode;
627 mark_inode_dirty(inode);
629 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
631 udf_debug("udf_add_entry failure!\n");
632 inode->i_nlink --;
633 mark_inode_dirty(inode);
634 iput(inode);
635 return err;
637 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
638 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
639 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
640 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
641 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
642 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
644 mark_inode_dirty(dir);
645 dir->i_version = ++event;
647 if (fibh.sbh != fibh.ebh)
648 udf_release_data(fibh.ebh);
649 udf_release_data(fibh.sbh);
650 d_instantiate(dentry, inode);
651 return 0;
654 int udf_mknod(struct inode * dir, struct dentry * dentry, int mode, int rdev)
656 struct inode * inode;
657 struct udf_fileident_bh fibh;
658 int err;
659 struct FileIdentDesc cfi, *fi;
661 err = -EIO;
662 inode = udf_new_inode(dir, mode, &err);
663 if (!inode)
664 goto out;
666 inode->i_uid = current->fsuid;
667 inode->i_mode = mode;
668 inode->i_op = NULL;
669 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
671 udf_debug("udf_add_entry failure!\n");
672 inode->i_nlink --;
673 mark_inode_dirty(inode);
674 iput(inode);
675 return err;
677 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
678 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
679 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
680 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
681 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
682 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
684 mark_inode_dirty(dir);
685 dir->i_version = ++event;
687 if (S_ISREG(inode->i_mode))
689 inode->i_op = &udf_file_inode_operations;
691 else if (S_ISCHR(inode->i_mode))
693 inode->i_op = &chrdev_inode_operations;
695 else if (S_ISBLK(inode->i_mode))
697 inode->i_op = &blkdev_inode_operations;
699 else if (S_ISFIFO(inode->i_mode))
701 init_fifo(inode);
703 if (S_ISBLK(mode) || S_ISCHR(mode))
704 inode->i_rdev = to_kdev_t(rdev);
705 mark_inode_dirty(inode);
707 if (fibh.sbh != fibh.ebh)
708 udf_release_data(fibh.ebh);
709 udf_release_data(fibh.sbh);
710 d_instantiate(dentry, inode);
711 err = 0;
712 out:
713 return err;
716 int udf_mkdir(struct inode * dir, struct dentry * dentry, int mode)
718 struct inode * inode;
719 struct udf_fileident_bh fibh;
720 int err;
721 struct FileEntry *fe;
722 struct FileIdentDesc cfi, *fi;
723 Uint32 loc;
725 err = -EMLINK;
726 if (dir->i_nlink >= (256<<sizeof(dir->i_nlink))-1)
727 goto out;
729 err = -EIO;
730 inode = udf_new_inode(dir, S_IFDIR, &err);
731 if (!inode)
732 goto out;
734 inode->i_op = &udf_dir_inode_operations;
735 inode->i_size = (sizeof(struct FileIdentDesc) + 3) & ~3;
736 if (UDF_I_ALLOCTYPE(inode) == ICB_FLAG_AD_IN_ICB)
738 UDF_I_EXT0LEN(inode) = inode->i_size;
739 UDF_I_EXT0LOC(inode) = UDF_I_LOCATION(inode);
740 UDF_I_LENALLOC(inode) = inode->i_size;
741 loc = UDF_I_LOCATION(inode).logicalBlockNum;
742 fibh.sbh = udf_tread(inode->i_sb, inode->i_ino, inode->i_sb->s_blocksize);
744 else
746 fibh.sbh = udf_bread (inode, 0, 1, &err);
747 loc = UDF_I_EXT0LOC(inode).logicalBlockNum;
750 if (!fibh.sbh)
752 inode->i_nlink--;
753 mark_inode_dirty(inode);
754 iput(inode);
755 goto out;
757 inode->i_nlink = 2;
758 fe = (struct FileEntry *)fibh.sbh->b_data;
759 fi = (struct FileIdentDesc *)&(fe->extendedAttr[UDF_I_LENEATTR(inode)]);
760 udf_new_tag((char *)&cfi, TID_FILE_IDENT_DESC, 2, 1, loc,
761 sizeof(struct FileIdentDesc));
762 cfi.fileVersionNum = cpu_to_le16(1);
763 cfi.fileCharacteristics = FILE_DIRECTORY | FILE_PARENT;
764 cfi.lengthFileIdent = 0;
765 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
766 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
767 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
768 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
769 cfi.lengthOfImpUse = cpu_to_le16(0);
770 fibh.ebh = fibh.sbh;
771 fibh.soffset = sizeof(struct FileEntry);
772 fibh.eoffset = sizeof(struct FileEntry) + inode->i_size;
773 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
774 udf_release_data(fibh.sbh);
775 inode->i_mode = S_IFDIR | (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask);
776 if (dir->i_mode & S_ISGID)
777 inode->i_mode |= S_ISGID;
778 mark_inode_dirty(inode);
780 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
782 udf_debug("udf_add_entry failure!\n");
783 inode->i_nlink = 0;
784 mark_inode_dirty(inode);
785 iput(inode);
786 goto out;
788 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
789 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
790 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
791 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
792 cfi.fileCharacteristics |= FILE_DIRECTORY;
793 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
794 dir->i_version = ++event;
795 dir->i_nlink++;
796 mark_inode_dirty(dir);
797 d_instantiate(dentry, inode);
798 if (fibh.sbh != fibh.ebh)
799 udf_release_data(fibh.ebh);
800 udf_release_data(fibh.sbh);
801 err = 0;
802 out:
803 return err;
806 static int empty_dir(struct inode *dir)
808 struct FileIdentDesc *fi, cfi;
809 struct udf_fileident_bh fibh;
810 int f_pos;
811 int size = (UDF_I_EXT0OFFS(dir) + dir->i_size) >> 2;
812 int block;
813 lb_addr bloc, eloc;
814 Uint32 extoffset, elen, offset;
815 struct buffer_head *bh = NULL;
817 f_pos = (UDF_I_EXT0OFFS(dir) >> 2);
819 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
820 if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
821 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)
823 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
824 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
826 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
827 extoffset -= sizeof(short_ad);
828 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
829 extoffset -= sizeof(long_ad);
831 else
832 offset = 0;
834 else
836 udf_release_data(bh);
837 return 0;
840 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block, dir->i_sb->s_blocksize)))
841 return 0;
843 while ( (f_pos < size) )
845 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &bloc, &extoffset, &offset, &bh);
847 if (!fi)
849 if (fibh.sbh != fibh.ebh)
850 udf_release_data(fibh.ebh);
851 udf_release_data(fibh.sbh);
852 udf_release_data(bh);
853 return 0;
856 if (cfi.lengthFileIdent && (cfi.fileCharacteristics & FILE_DELETED) == 0)
858 udf_release_data(bh);
859 return 0;
862 if (fibh.sbh != fibh.ebh)
863 udf_release_data(fibh.ebh);
864 udf_release_data(fibh.sbh);
865 udf_release_data(bh);
866 return 1;
869 int udf_rmdir(struct inode * dir, struct dentry * dentry)
871 int retval;
872 struct inode * inode;
873 struct udf_fileident_bh fibh;
874 struct FileIdentDesc *fi, cfi;
876 retval = -ENOENT;
877 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
878 if (!fi)
879 goto out;
881 inode = dentry->d_inode;
883 retval = -EIO;
884 if (udf_get_lb_pblock(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation), 0) != inode->i_ino)
885 goto end_rmdir;
886 retval = -ENOTEMPTY;
887 if (!empty_dir(inode))
888 goto end_rmdir;
889 retval = udf_delete_entry(fi, &fibh, &cfi);
890 dir->i_version = ++event;
891 if (retval)
892 goto end_rmdir;
893 if (inode->i_nlink != 2)
894 udf_warning(inode->i_sb, "udf_rmdir",
895 "empty directory has nlink != 2 (%d)",
896 inode->i_nlink);
897 inode->i_version = ++event;
898 inode->i_nlink = 0;
899 inode->i_size = 0;
900 mark_inode_dirty(inode);
901 dir->i_nlink --;
902 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
903 UDF_I_UCTIME(inode) = UDF_I_UCTIME(dir) = UDF_I_UMTIME(dir) = CURRENT_UTIME;
904 mark_inode_dirty(dir);
905 d_delete(dentry);
907 end_rmdir:
908 if (fibh.sbh != fibh.ebh)
909 udf_release_data(fibh.ebh);
910 udf_release_data(fibh.sbh);
911 out:
912 return retval;
915 int udf_unlink(struct inode * dir, struct dentry * dentry)
917 int retval;
918 struct inode * inode;
919 struct udf_fileident_bh fibh;
920 struct FileIdentDesc *fi;
921 struct FileIdentDesc cfi;
923 retval = -ENOENT;
924 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
925 if (!fi)
926 goto out;
928 inode = dentry->d_inode;
930 retval = -EIO;
932 if (udf_get_lb_pblock(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation), 0) !=
933 inode->i_ino)
935 goto end_unlink;
938 if (!inode->i_nlink)
940 udf_debug("Deleting nonexistent file (%lu), %d\n",
941 inode->i_ino, inode->i_nlink);
942 inode->i_nlink = 1;
944 retval = udf_delete_entry(fi, &fibh, &cfi);
945 if (retval)
946 goto end_unlink;
947 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
948 UDF_I_UCTIME(dir) = UDF_I_UMTIME(dir) = CURRENT_UTIME;
949 mark_inode_dirty(dir);
950 inode->i_nlink--;
951 mark_inode_dirty(inode);
952 inode->i_ctime = dir->i_ctime;
953 retval = 0;
954 d_delete(dentry); /* This also frees the inode */
956 end_unlink:
957 if (fibh.sbh != fibh.ebh)
958 udf_release_data(fibh.ebh);
959 udf_release_data(fibh.sbh);
960 out:
961 return retval;
964 int udf_symlink(struct inode * dir, struct dentry * dentry, const char * symname)
966 struct inode * inode;
967 struct PathComponent *pc;
968 struct udf_fileident_bh fibh;
969 struct buffer_head *bh = NULL;
970 int eoffset, elen = 0;
971 struct FileIdentDesc *fi;
972 struct FileIdentDesc cfi;
973 char *ea;
974 int err;
976 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
977 goto out;
979 inode->i_mode = S_IFLNK | S_IRWXUGO;
980 inode->i_op = &udf_symlink_inode_operations;
982 bh = udf_tread(inode->i_sb, inode->i_ino, inode->i_sb->s_blocksize);
983 ea = bh->b_data + udf_file_entry_alloc_offset(inode);
985 eoffset = inode->i_sb->s_blocksize - (ea - bh->b_data);
986 pc = (struct PathComponent *)ea;
988 if (*symname == '/')
992 symname++;
993 } while (*symname == '/');
995 pc->componentType = 1;
996 pc->lengthComponentIdent = 0;
997 pc->componentFileVersionNum = 0;
998 pc += sizeof(struct PathComponent);
999 elen += sizeof(struct PathComponent);
1002 while (*symname && eoffset > elen + sizeof(struct PathComponent))
1004 char *compstart;
1005 pc = (struct PathComponent *)(ea + elen);
1007 compstart = (char *)symname;
1011 symname++;
1012 } while (*symname && *symname != '/');
1014 pc->componentType = 5;
1015 pc->lengthComponentIdent = 0;
1016 pc->componentFileVersionNum = 0;
1017 if (pc->componentIdent[0] == '.')
1019 if (pc->lengthComponentIdent == 1)
1020 pc->componentType = 4;
1021 else if (pc->lengthComponentIdent == 2 && pc->componentIdent[1] == '.')
1022 pc->componentType = 3;
1025 if (pc->componentType == 5)
1027 if (elen + sizeof(struct PathComponent) + symname - compstart > eoffset)
1028 pc->lengthComponentIdent = eoffset - elen - sizeof(struct PathComponent);
1029 else
1030 pc->lengthComponentIdent = symname - compstart;
1032 memcpy(pc->componentIdent, compstart, pc->lengthComponentIdent);
1035 elen += sizeof(struct PathComponent) + pc->lengthComponentIdent;
1037 if (*symname)
1041 symname++;
1042 } while (*symname == '/');
1046 udf_release_data(bh);
1047 UDF_I_LENALLOC(inode) = inode->i_size = elen;
1048 mark_inode_dirty(inode);
1050 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1051 goto out;
1052 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1053 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1054 if (UDF_SB_LVIDBH(inode->i_sb))
1056 struct LogicalVolHeaderDesc *lvhd;
1057 Uint64 uniqueID;
1058 lvhd = (struct LogicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1059 uniqueID = le64_to_cpu(lvhd->uniqueID);
1060 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
1061 le32_to_cpu(uniqueID & 0x00000000FFFFFFFFUL);
1062 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1063 uniqueID += 16;
1064 lvhd->uniqueID = cpu_to_le64(uniqueID);
1065 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb), 1);
1067 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
1068 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
1070 mark_inode_dirty(dir);
1071 dir->i_version = ++event;
1073 if (fibh.sbh != fibh.ebh)
1074 udf_release_data(fibh.ebh);
1075 udf_release_data(fibh.sbh);
1076 d_instantiate(dentry, inode);
1077 err = 0;
1079 out:
1080 return err;
1083 int udf_link(struct dentry * old_dentry, struct inode * dir,
1084 struct dentry *dentry)
1086 struct inode *inode = old_dentry->d_inode;
1087 struct udf_fileident_bh fibh;
1088 int err;
1089 struct FileIdentDesc cfi, *fi;
1091 if (S_ISDIR(inode->i_mode))
1092 return -EPERM;
1094 if (inode->i_nlink >= (256<<sizeof(inode->i_nlink))-1)
1095 return -EMLINK;
1097 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1098 return err;
1099 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1100 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1101 if (UDF_SB_LVIDBH(inode->i_sb))
1103 struct LogicalVolHeaderDesc *lvhd;
1104 Uint64 uniqueID;
1105 lvhd = (struct LogicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1106 uniqueID = le64_to_cpu(lvhd->uniqueID);
1107 *(Uint32 *)((struct ADImpUse *)cfi.icb.impUse)->impUse =
1108 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1109 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1110 uniqueID += 16;
1111 lvhd->uniqueID = cpu_to_le64(uniqueID);
1112 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb), 1);
1114 udf_write_fi(&cfi, fi, &fibh, NULL, NULL);
1115 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_IN_ICB)
1117 mark_inode_dirty(dir);
1118 dir->i_version = ++event;
1120 if (fibh.sbh != fibh.ebh)
1121 udf_release_data(fibh.ebh);
1122 udf_release_data(fibh.sbh);
1123 inode->i_nlink ++;
1124 inode->i_ctime = CURRENT_TIME;
1125 UDF_I_UCTIME(inode) = CURRENT_UTIME;
1126 mark_inode_dirty(inode);
1127 inode->i_count ++;
1128 d_instantiate(dentry, inode);
1129 return 0;
1132 /* Anybody can rename anything with this: the permission checks are left to the
1133 * higher-level routines.
1135 int udf_rename (struct inode * old_dir, struct dentry * old_dentry,
1136 struct inode * new_dir, struct dentry * new_dentry)
1138 struct inode * old_inode, * new_inode;
1139 struct udf_fileident_bh ofibh, nfibh;
1140 struct FileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
1141 struct buffer_head *dir_bh = NULL;
1142 int retval = -ENOENT;
1144 old_inode = old_dentry->d_inode;
1145 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1146 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, lelb_to_cpu(ocfi.icb.extLocation), 0) !=
1147 old_inode->i_ino)
1149 goto end_rename;
1152 new_inode = new_dentry->d_inode;
1153 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1154 if (nfi)
1156 if (!new_inode)
1158 if (nfibh.sbh != nfibh.ebh)
1159 udf_release_data(nfibh.ebh);
1160 udf_release_data(nfibh.sbh);
1161 nfi = NULL;
1163 else
1166 DQUOT_INIT(new_inode);
1170 if (S_ISDIR(old_inode->i_mode))
1172 Uint32 offset = UDF_I_EXT0OFFS(old_inode);
1174 if (new_inode)
1176 retval = -ENOTEMPTY;
1177 if (!empty_dir(new_inode))
1178 goto end_rename;
1180 retval = -EIO;
1181 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1182 if (!dir_bh)
1183 goto end_rename;
1184 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
1185 if (!dir_fi)
1186 goto end_rename;
1187 if (udf_get_lb_pblock(old_inode->i_sb, cpu_to_lelb(dir_fi->icb.extLocation), 0) !=
1188 old_dir->i_ino)
1190 goto end_rename;
1192 retval = -EMLINK;
1193 if (!new_inode && new_dir->i_nlink >= (256<<sizeof(new_dir->i_nlink))-1)
1194 goto end_rename;
1196 if (!nfi)
1198 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1199 if (!nfi)
1200 goto end_rename;
1202 new_dir->i_version = ++event;
1205 * ok, that's it
1207 ncfi.fileVersionNum = ocfi.fileVersionNum;
1208 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1209 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1210 udf_write_fi(&ncfi, nfi, &nfibh, NULL, NULL);
1212 udf_delete_entry(ofi, &ofibh, &ocfi);
1214 old_dir->i_version = ++event;
1215 if (new_inode)
1217 new_inode->i_nlink--;
1218 new_inode->i_ctime = CURRENT_TIME;
1219 UDF_I_UCTIME(new_inode) = CURRENT_UTIME;
1220 mark_inode_dirty(new_inode);
1222 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1223 UDF_I_UCTIME(old_dir) = UDF_I_UMTIME(old_dir) = CURRENT_UTIME;
1224 mark_inode_dirty(old_dir);
1226 if (dir_bh)
1228 dir_fi->icb.extLocation = lelb_to_cpu(UDF_I_LOCATION(new_dir));
1229 udf_update_tag((char *)dir_fi, sizeof(struct FileIdentDesc) +
1230 cpu_to_le16(dir_fi->lengthOfImpUse));
1231 if (UDF_I_ALLOCTYPE(old_inode) == ICB_FLAG_AD_IN_ICB)
1233 mark_inode_dirty(old_inode);
1234 old_inode->i_version = ++event;
1236 else
1237 mark_buffer_dirty(dir_bh, 1);
1238 old_dir->i_nlink --;
1239 mark_inode_dirty(old_dir);
1240 if (new_inode)
1242 new_inode->i_nlink --;
1243 mark_inode_dirty(new_inode);
1245 else
1247 new_dir->i_nlink ++;
1248 mark_inode_dirty(new_dir);
1252 retval = 0;
1254 end_rename:
1255 udf_release_data(dir_bh);
1256 if (ofi)
1258 if (ofibh.sbh != ofibh.ebh)
1259 udf_release_data(ofibh.ebh);
1260 udf_release_data(ofibh.sbh);
1262 if (nfi)
1264 if (nfibh.sbh != nfibh.ebh)
1265 udf_release_data(nfibh.ebh);
1266 udf_release_data(nfibh.sbh);
1268 return retval;