v2.6.22.24-op1
[linux-2.6.22.y-op.git] / fs / udf / namei.c
blob51fe307dc0ec026a7d85e203659261bdb3f3912b
1 /*
2 * namei.c
4 * PURPOSE
5 * Inode name handling routines for the OSTA-UDF(tm) filesystem.
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.
13 * (C) 1998-2004 Ben Fennema
14 * (C) 1999-2000 Stelias Computing Inc
16 * HISTORY
18 * 12/12/98 blf Created. Split out the lookup code from dir.c
19 * 04/19/99 blf link, mknod, symlink support
22 #include "udfdecl.h"
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>
35 static inline int udf_match(int len1, const char *name1, int len2, const char *name2)
37 if (len1 != len2)
38 return 0;
39 return !memcmp(name1, name2, len1);
42 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
43 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
44 uint8_t *impuse, uint8_t *fileident)
46 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
47 uint16_t crc;
48 uint8_t checksum = 0;
49 int i;
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;
57 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
58 adinicb = 1;
60 offset = fibh->soffset + sizeof(struct fileIdentDesc);
62 if (impuse)
64 if (adinicb || (offset + liu < 0))
65 memcpy((uint8_t *)sfi->impUse, impuse, liu);
66 else if (offset >= 0)
67 memcpy(fibh->ebh->b_data + offset, impuse, liu);
68 else
70 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
71 memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);
75 offset += liu;
77 if (fileident)
79 if (adinicb || (offset + lfi < 0))
80 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
81 else if (offset >= 0)
82 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
83 else
85 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, -offset);
86 memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);
90 offset += lfi;
92 if (adinicb || (offset + padlen < 0))
93 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
94 else if (offset >= 0)
95 memset(fibh->ebh->b_data + offset, 0x00, padlen);
96 else
98 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
99 memset(fibh->ebh->b_data, 0x00, padlen + offset);
102 crc = udf_crc((uint8_t *)cfi + sizeof(tag), sizeof(struct fileIdentDesc) -
103 sizeof(tag), 0);
105 if (fibh->sbh == fibh->ebh)
106 crc = udf_crc((uint8_t *)sfi->impUse,
107 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
108 else if (sizeof(struct fileIdentDesc) >= -fibh->soffset)
109 crc = udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) + fibh->soffset,
110 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
111 else
113 crc = udf_crc((uint8_t *)sfi->impUse,
114 -fibh->soffset - sizeof(struct fileIdentDesc), crc);
115 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
118 cfi->descTag.descCRC = cpu_to_le16(crc);
119 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
121 for (i=0; i<16; i++)
122 if (i != 4)
123 checksum += ((uint8_t *)&cfi->descTag)[i];
125 cfi->descTag.tagChecksum = checksum;
126 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset))
127 memcpy((uint8_t *)sfi, (uint8_t *)cfi, sizeof(struct fileIdentDesc));
128 else
130 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
131 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
132 sizeof(struct fileIdentDesc) + fibh->soffset);
135 if (adinicb)
136 mark_inode_dirty(inode);
137 else
139 if (fibh->sbh != fibh->ebh)
140 mark_buffer_dirty_inode(fibh->ebh, inode);
141 mark_buffer_dirty_inode(fibh->sbh, inode);
143 return 0;
146 static struct fileIdentDesc *
147 udf_find_entry(struct inode *dir, struct dentry *dentry,
148 struct udf_fileident_bh *fibh,
149 struct fileIdentDesc *cfi)
151 struct fileIdentDesc *fi=NULL;
152 loff_t f_pos;
153 int block, flen;
154 char fname[UDF_NAME_LEN];
155 char *nameptr;
156 uint8_t lfi;
157 uint16_t liu;
158 loff_t size;
159 kernel_lb_addr eloc;
160 uint32_t elen;
161 sector_t offset;
162 struct extent_position epos = { NULL, 0, { 0, 0}};
164 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
165 f_pos = (udf_ext0_offset(dir) >> 2);
167 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
168 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
169 fibh->sbh = fibh->ebh = NULL;
170 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
171 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
173 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
174 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
176 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
177 epos.offset -= sizeof(short_ad);
178 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
179 epos.offset -= sizeof(long_ad);
181 else
182 offset = 0;
184 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
186 brelse(epos.bh);
187 return NULL;
190 else
192 brelse(epos.bh);
193 return NULL;
196 while ( (f_pos < size) )
198 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc, &elen, &offset);
200 if (!fi)
202 if (fibh->sbh != fibh->ebh)
203 brelse(fibh->ebh);
204 brelse(fibh->sbh);
205 brelse(epos.bh);
206 return NULL;
209 liu = le16_to_cpu(cfi->lengthOfImpUse);
210 lfi = cfi->lengthFileIdent;
212 if (fibh->sbh == fibh->ebh)
214 nameptr = fi->fileIdent + liu;
216 else
218 int poffset; /* Unpaded ending offset */
220 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
222 if (poffset >= lfi)
223 nameptr = (uint8_t *)(fibh->ebh->b_data + poffset - lfi);
224 else
226 nameptr = fname;
227 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
228 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
232 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
234 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE) )
235 continue;
238 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0 )
240 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE) )
241 continue;
244 if (!lfi)
245 continue;
247 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)))
249 if (udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
251 brelse(epos.bh);
252 return fi;
256 if (fibh->sbh != fibh->ebh)
257 brelse(fibh->ebh);
258 brelse(fibh->sbh);
259 brelse(epos.bh);
260 return NULL;
264 * udf_lookup
266 * PURPOSE
267 * Look-up the inode for a given name.
269 * DESCRIPTION
270 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
271 * available for a directory. The filesystem is useless if this routine is
272 * not available for at least the filesystem's root directory.
274 * This routine is passed an incomplete dentry - it must be completed by
275 * calling d_add(dentry, inode). If the name does not exist, then the
276 * specified inode must be set to null. An error should only be returned
277 * when the lookup fails for a reason other than the name not existing.
278 * Note that the directory inode semaphore is held during the call.
280 * Refer to lookup_dentry() in fs/namei.c
281 * lookup_dentry() -> lookup() -> real_lookup() -> .
283 * PRE-CONDITIONS
284 * dir Pointer to inode of parent directory.
285 * dentry Pointer to dentry to complete.
286 * nd Pointer to lookup nameidata
288 * POST-CONDITIONS
289 * <return> Zero on success.
291 * HISTORY
292 * July 1, 1997 - Andrew E. Mileski
293 * Written, tested, and released.
296 static struct dentry *
297 udf_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
299 struct inode *inode = NULL;
300 struct fileIdentDesc cfi;
301 struct udf_fileident_bh fibh;
303 if (dentry->d_name.len > UDF_NAME_LEN-2)
304 return ERR_PTR(-ENAMETOOLONG);
306 lock_kernel();
307 #ifdef UDF_RECOVERY
308 /* temporary shorthand for specifying files by inode number */
309 if (!strncmp(dentry->d_name.name, ".B=", 3) )
311 kernel_lb_addr lb = { 0, simple_strtoul(dentry->d_name.name+3, NULL, 0) };
312 inode = udf_iget(dir->i_sb, lb);
313 if (!inode)
315 unlock_kernel();
316 return ERR_PTR(-EACCES);
319 else
320 #endif /* UDF_RECOVERY */
322 if (udf_find_entry(dir, dentry, &fibh, &cfi))
324 if (fibh.sbh != fibh.ebh)
325 brelse(fibh.ebh);
326 brelse(fibh.sbh);
328 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
329 if ( !inode )
331 unlock_kernel();
332 return ERR_PTR(-EACCES);
335 unlock_kernel();
336 d_add(dentry, inode);
337 return NULL;
340 static struct fileIdentDesc *
341 udf_add_entry(struct inode *dir, struct dentry *dentry,
342 struct udf_fileident_bh *fibh,
343 struct fileIdentDesc *cfi, int *err)
345 struct super_block *sb;
346 struct fileIdentDesc *fi=NULL;
347 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
348 int namelen;
349 loff_t f_pos;
350 int flen;
351 char *nameptr;
352 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
353 int nfidlen;
354 uint8_t lfi;
355 uint16_t liu;
356 int block;
357 kernel_lb_addr eloc;
358 uint32_t elen;
359 sector_t offset;
360 struct extent_position epos = { NULL, 0, { 0, 0 }};
362 sb = dir->i_sb;
364 if (dentry)
366 if (!dentry->d_name.len)
368 *err = -EINVAL;
369 return NULL;
372 if ( !(namelen = udf_put_filename(sb, dentry->d_name.name, name, dentry->d_name.len)))
374 *err = -ENAMETOOLONG;
375 return NULL;
378 else
379 namelen = 0;
381 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
383 f_pos = (udf_ext0_offset(dir) >> 2);
385 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
386 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
387 fibh->sbh = fibh->ebh = NULL;
388 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
389 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
391 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
392 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
394 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
395 epos.offset -= sizeof(short_ad);
396 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
397 epos.offset -= sizeof(long_ad);
399 else
400 offset = 0;
402 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
404 brelse(epos.bh);
405 *err = -EIO;
406 return NULL;
409 block = UDF_I_LOCATION(dir).logicalBlockNum;
412 else
414 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
415 fibh->sbh = fibh->ebh = NULL;
416 fibh->soffset = fibh->eoffset = sb->s_blocksize;
417 goto add;
420 while ( (f_pos < size) )
422 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc, &elen, &offset);
424 if (!fi)
426 if (fibh->sbh != fibh->ebh)
427 brelse(fibh->ebh);
428 brelse(fibh->sbh);
429 brelse(epos.bh);
430 *err = -EIO;
431 return NULL;
434 liu = le16_to_cpu(cfi->lengthOfImpUse);
435 lfi = cfi->lengthFileIdent;
437 if (fibh->sbh == fibh->ebh)
438 nameptr = fi->fileIdent + liu;
439 else
441 int poffset; /* Unpaded ending offset */
443 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
445 if (poffset >= lfi)
446 nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
447 else
449 nameptr = fname;
450 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
451 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
455 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
457 if (((sizeof(struct fileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen)
459 brelse(epos.bh);
460 cfi->descTag.tagSerialNum = cpu_to_le16(1);
461 cfi->fileVersionNum = cpu_to_le16(1);
462 cfi->fileCharacteristics = 0;
463 cfi->lengthFileIdent = namelen;
464 cfi->lengthOfImpUse = cpu_to_le16(0);
465 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
466 return fi;
467 else
469 *err = -EIO;
470 return NULL;
475 if (!lfi || !dentry)
476 continue;
478 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
479 udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
481 if (fibh->sbh != fibh->ebh)
482 brelse(fibh->ebh);
483 brelse(fibh->sbh);
484 brelse(epos.bh);
485 *err = -EEXIST;
486 return NULL;
490 add:
491 f_pos += nfidlen;
493 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
494 sb->s_blocksize - fibh->eoffset < nfidlen)
496 brelse(epos.bh);
497 epos.bh = NULL;
498 fibh->soffset -= udf_ext0_offset(dir);
499 fibh->eoffset -= udf_ext0_offset(dir);
500 f_pos -= (udf_ext0_offset(dir) >> 2);
501 if (fibh->sbh != fibh->ebh)
502 brelse(fibh->ebh);
503 brelse(fibh->sbh);
504 if (!(fibh->sbh = fibh->ebh = udf_expand_dir_adinicb(dir, &block, err)))
505 return NULL;
506 epos.block = UDF_I_LOCATION(dir);
507 eloc.logicalBlockNum = block;
508 eloc.partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
509 elen = dir->i_sb->s_blocksize;
510 epos.offset = udf_file_entry_alloc_offset(dir);
511 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
512 epos.offset += sizeof(short_ad);
513 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
514 epos.offset += sizeof(long_ad);
517 if (sb->s_blocksize - fibh->eoffset >= nfidlen)
519 fibh->soffset = fibh->eoffset;
520 fibh->eoffset += nfidlen;
521 if (fibh->sbh != fibh->ebh)
523 brelse(fibh->sbh);
524 fibh->sbh = fibh->ebh;
527 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
529 block = UDF_I_LOCATION(dir).logicalBlockNum;
530 fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) + fibh->soffset - udf_ext0_offset(dir) + UDF_I_LENEATTR(dir));
532 else
534 block = eloc.logicalBlockNum + ((elen - 1) >>
535 dir->i_sb->s_blocksize_bits);
536 fi = (struct fileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
539 else
541 fibh->soffset = fibh->eoffset - sb->s_blocksize;
542 fibh->eoffset += nfidlen - sb->s_blocksize;
543 if (fibh->sbh != fibh->ebh)
545 brelse(fibh->sbh);
546 fibh->sbh = fibh->ebh;
549 block = eloc.logicalBlockNum + ((elen - 1) >>
550 dir->i_sb->s_blocksize_bits);
552 if (!(fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err)))
554 brelse(epos.bh);
555 brelse(fibh->sbh);
556 return NULL;
559 if (!(fibh->soffset))
561 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
562 (EXT_RECORDED_ALLOCATED >> 30))
564 block = eloc.logicalBlockNum + ((elen - 1) >>
565 dir->i_sb->s_blocksize_bits);
567 else
568 block ++;
570 brelse(fibh->sbh);
571 fibh->sbh = fibh->ebh;
572 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
574 else
576 fi = (struct fileIdentDesc *)
577 (fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
581 memset(cfi, 0, sizeof(struct fileIdentDesc));
582 if (UDF_SB_UDFREV(sb) >= 0x0200)
583 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, sizeof(tag));
584 else
585 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, sizeof(tag));
586 cfi->fileVersionNum = cpu_to_le16(1);
587 cfi->lengthFileIdent = namelen;
588 cfi->lengthOfImpUse = cpu_to_le16(0);
589 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
591 brelse(epos.bh);
592 dir->i_size += nfidlen;
593 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
594 UDF_I_LENALLOC(dir) += nfidlen;
595 mark_inode_dirty(dir);
596 return fi;
598 else
600 brelse(epos.bh);
601 if (fibh->sbh != fibh->ebh)
602 brelse(fibh->ebh);
603 brelse(fibh->sbh);
604 *err = -EIO;
605 return NULL;
609 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
610 struct udf_fileident_bh *fibh, struct fileIdentDesc *cfi)
612 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
613 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
614 memset(&(cfi->icb), 0x00, sizeof(long_ad));
615 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
618 static int udf_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
620 struct udf_fileident_bh fibh;
621 struct inode *inode;
622 struct fileIdentDesc cfi, *fi;
623 int err;
625 lock_kernel();
626 inode = udf_new_inode(dir, mode, &err);
627 if (!inode)
629 unlock_kernel();
630 return err;
633 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
634 inode->i_data.a_ops = &udf_adinicb_aops;
635 else
636 inode->i_data.a_ops = &udf_aops;
637 inode->i_op = &udf_file_inode_operations;
638 inode->i_fop = &udf_file_operations;
639 inode->i_mode = mode;
640 mark_inode_dirty(inode);
642 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
644 inode->i_nlink --;
645 mark_inode_dirty(inode);
646 iput(inode);
647 unlock_kernel();
648 return err;
650 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
651 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
652 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
653 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
654 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
655 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
657 mark_inode_dirty(dir);
659 if (fibh.sbh != fibh.ebh)
660 brelse(fibh.ebh);
661 brelse(fibh.sbh);
662 unlock_kernel();
663 d_instantiate(dentry, inode);
664 return 0;
667 static int udf_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev)
669 struct inode * inode;
670 struct udf_fileident_bh fibh;
671 struct fileIdentDesc cfi, *fi;
672 int err;
674 if (!old_valid_dev(rdev))
675 return -EINVAL;
677 lock_kernel();
678 err = -EIO;
679 inode = udf_new_inode(dir, mode, &err);
680 if (!inode)
681 goto out;
683 inode->i_uid = current->fsuid;
684 init_special_inode(inode, mode, rdev);
685 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
687 inode->i_nlink --;
688 mark_inode_dirty(inode);
689 iput(inode);
690 unlock_kernel();
691 return err;
693 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
694 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
695 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
696 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
697 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
698 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
700 mark_inode_dirty(dir);
702 mark_inode_dirty(inode);
704 if (fibh.sbh != fibh.ebh)
705 brelse(fibh.ebh);
706 brelse(fibh.sbh);
707 d_instantiate(dentry, inode);
708 err = 0;
709 out:
710 unlock_kernel();
711 return err;
714 static int udf_mkdir(struct inode * dir, struct dentry * dentry, int mode)
716 struct inode * inode;
717 struct udf_fileident_bh fibh;
718 struct fileIdentDesc cfi, *fi;
719 int err;
721 lock_kernel();
722 err = -EMLINK;
723 if (dir->i_nlink >= (256<<sizeof(dir->i_nlink))-1)
724 goto out;
726 err = -EIO;
727 inode = udf_new_inode(dir, S_IFDIR, &err);
728 if (!inode)
729 goto out;
731 inode->i_op = &udf_dir_inode_operations;
732 inode->i_fop = &udf_dir_operations;
733 if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err)))
735 inode->i_nlink--;
736 mark_inode_dirty(inode);
737 iput(inode);
738 goto out;
740 inode->i_nlink = 2;
741 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
742 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
743 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
744 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
745 cfi.fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
746 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
747 brelse(fibh.sbh);
748 inode->i_mode = S_IFDIR | mode;
749 if (dir->i_mode & S_ISGID)
750 inode->i_mode |= S_ISGID;
751 mark_inode_dirty(inode);
753 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
755 inode->i_nlink = 0;
756 mark_inode_dirty(inode);
757 iput(inode);
758 goto out;
760 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
761 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
762 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
763 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
764 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
765 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
766 inc_nlink(dir);
767 mark_inode_dirty(dir);
768 d_instantiate(dentry, inode);
769 if (fibh.sbh != fibh.ebh)
770 brelse(fibh.ebh);
771 brelse(fibh.sbh);
772 err = 0;
773 out:
774 unlock_kernel();
775 return err;
778 static int empty_dir(struct inode *dir)
780 struct fileIdentDesc *fi, cfi;
781 struct udf_fileident_bh fibh;
782 loff_t f_pos;
783 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
784 int block;
785 kernel_lb_addr eloc;
786 uint32_t elen;
787 sector_t offset;
788 struct extent_position epos = { NULL, 0, { 0, 0}};
790 f_pos = (udf_ext0_offset(dir) >> 2);
792 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
794 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
795 fibh.sbh = fibh.ebh = NULL;
796 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
797 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
799 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
800 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
802 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
803 epos.offset -= sizeof(short_ad);
804 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
805 epos.offset -= sizeof(long_ad);
807 else
808 offset = 0;
810 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block)))
812 brelse(epos.bh);
813 return 0;
816 else
818 brelse(epos.bh);
819 return 0;
823 while ( (f_pos < size) )
825 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc, &elen, &offset);
827 if (!fi)
829 if (fibh.sbh != fibh.ebh)
830 brelse(fibh.ebh);
831 brelse(fibh.sbh);
832 brelse(epos.bh);
833 return 0;
836 if (cfi.lengthFileIdent && (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0)
838 if (fibh.sbh != fibh.ebh)
839 brelse(fibh.ebh);
840 brelse(fibh.sbh);
841 brelse(epos.bh);
842 return 0;
845 if (fibh.sbh != fibh.ebh)
846 brelse(fibh.ebh);
847 brelse(fibh.sbh);
848 brelse(epos.bh);
849 return 1;
852 static int udf_rmdir(struct inode * dir, struct dentry * dentry)
854 int retval;
855 struct inode * inode = dentry->d_inode;
856 struct udf_fileident_bh fibh;
857 struct fileIdentDesc *fi, cfi;
858 kernel_lb_addr tloc;
860 retval = -ENOENT;
861 lock_kernel();
862 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
863 if (!fi)
864 goto out;
866 retval = -EIO;
867 tloc = lelb_to_cpu(cfi.icb.extLocation);
868 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
869 goto end_rmdir;
870 retval = -ENOTEMPTY;
871 if (!empty_dir(inode))
872 goto end_rmdir;
873 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
874 if (retval)
875 goto end_rmdir;
876 if (inode->i_nlink != 2)
877 udf_warning(inode->i_sb, "udf_rmdir",
878 "empty directory has nlink != 2 (%d)",
879 inode->i_nlink);
880 clear_nlink(inode);
881 inode->i_size = 0;
882 inode_dec_link_count(dir);
883 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
884 mark_inode_dirty(dir);
886 end_rmdir:
887 if (fibh.sbh != fibh.ebh)
888 brelse(fibh.ebh);
889 brelse(fibh.sbh);
890 out:
891 unlock_kernel();
892 return retval;
895 static int udf_unlink(struct inode * dir, struct dentry * dentry)
897 int retval;
898 struct inode * inode = dentry->d_inode;
899 struct udf_fileident_bh fibh;
900 struct fileIdentDesc *fi;
901 struct fileIdentDesc cfi;
902 kernel_lb_addr tloc;
904 retval = -ENOENT;
905 lock_kernel();
906 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
907 if (!fi)
908 goto out;
910 retval = -EIO;
911 tloc = lelb_to_cpu(cfi.icb.extLocation);
912 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
913 goto end_unlink;
915 if (!inode->i_nlink)
917 udf_debug("Deleting nonexistent file (%lu), %d\n",
918 inode->i_ino, inode->i_nlink);
919 inode->i_nlink = 1;
921 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
922 if (retval)
923 goto end_unlink;
924 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
925 mark_inode_dirty(dir);
926 inode_dec_link_count(inode);
927 inode->i_ctime = dir->i_ctime;
928 retval = 0;
930 end_unlink:
931 if (fibh.sbh != fibh.ebh)
932 brelse(fibh.ebh);
933 brelse(fibh.sbh);
934 out:
935 unlock_kernel();
936 return retval;
939 static int udf_symlink(struct inode * dir, struct dentry * dentry, const char * symname)
941 struct inode * inode;
942 struct pathComponent *pc;
943 char *compstart;
944 struct udf_fileident_bh fibh;
945 struct extent_position epos = { NULL, 0, {0, 0}};
946 int eoffset, elen = 0;
947 struct fileIdentDesc *fi;
948 struct fileIdentDesc cfi;
949 char *ea;
950 int err;
951 int block;
952 char name[UDF_NAME_LEN];
953 int namelen;
955 lock_kernel();
956 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
957 goto out;
959 inode->i_mode = S_IFLNK | S_IRWXUGO;
960 inode->i_data.a_ops = &udf_symlink_aops;
961 inode->i_op = &page_symlink_inode_operations;
963 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB)
965 kernel_lb_addr eloc;
966 uint32_t elen;
968 block = udf_new_block(inode->i_sb, inode,
969 UDF_I_LOCATION(inode).partitionReferenceNum,
970 UDF_I_LOCATION(inode).logicalBlockNum, &err);
971 if (!block)
972 goto out_no_entry;
973 epos.block = UDF_I_LOCATION(inode);
974 epos.offset = udf_file_entry_alloc_offset(inode);
975 epos.bh = NULL;
976 eloc.logicalBlockNum = block;
977 eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
978 elen = inode->i_sb->s_blocksize;
979 UDF_I_LENEXTENTS(inode) = elen;
980 udf_add_aext(inode, &epos, eloc, elen, 0);
981 brelse(epos.bh);
983 block = udf_get_pblock(inode->i_sb, block,
984 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
985 epos.bh = udf_tread(inode->i_sb, block);
986 lock_buffer(epos.bh);
987 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
988 set_buffer_uptodate(epos.bh);
989 unlock_buffer(epos.bh);
990 mark_buffer_dirty_inode(epos.bh, inode);
991 ea = epos.bh->b_data + udf_ext0_offset(inode);
993 else
994 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
996 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
997 pc = (struct pathComponent *)ea;
999 if (*symname == '/')
1003 symname++;
1004 } while (*symname == '/');
1006 pc->componentType = 1;
1007 pc->lengthComponentIdent = 0;
1008 pc->componentFileVersionNum = 0;
1009 pc += sizeof(struct pathComponent);
1010 elen += sizeof(struct pathComponent);
1013 err = -ENAMETOOLONG;
1015 while (*symname)
1017 if (elen + sizeof(struct pathComponent) > eoffset)
1018 goto out_no_entry;
1020 pc = (struct pathComponent *)(ea + elen);
1022 compstart = (char *)symname;
1026 symname++;
1027 } while (*symname && *symname != '/');
1029 pc->componentType = 5;
1030 pc->lengthComponentIdent = 0;
1031 pc->componentFileVersionNum = 0;
1032 if (compstart[0] == '.')
1034 if ((symname-compstart) == 1)
1035 pc->componentType = 4;
1036 else if ((symname-compstart) == 2 && compstart[1] == '.')
1037 pc->componentType = 3;
1040 if (pc->componentType == 5)
1042 if ( !(namelen = udf_put_filename(inode->i_sb, compstart, name, symname-compstart)))
1043 goto out_no_entry;
1045 if (elen + sizeof(struct pathComponent) + namelen > eoffset)
1046 goto out_no_entry;
1047 else
1048 pc->lengthComponentIdent = namelen;
1050 memcpy(pc->componentIdent, name, namelen);
1053 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1055 if (*symname)
1059 symname++;
1060 } while (*symname == '/');
1064 brelse(epos.bh);
1065 inode->i_size = elen;
1066 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1067 UDF_I_LENALLOC(inode) = inode->i_size;
1068 mark_inode_dirty(inode);
1070 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1071 goto out_no_entry;
1072 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1073 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1074 if (UDF_SB_LVIDBH(inode->i_sb))
1076 struct logicalVolHeaderDesc *lvhd;
1077 uint64_t uniqueID;
1078 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1079 uniqueID = le64_to_cpu(lvhd->uniqueID);
1080 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1081 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1082 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1083 uniqueID += 16;
1084 lvhd->uniqueID = cpu_to_le64(uniqueID);
1085 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1087 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1088 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1090 mark_inode_dirty(dir);
1092 if (fibh.sbh != fibh.ebh)
1093 brelse(fibh.ebh);
1094 brelse(fibh.sbh);
1095 d_instantiate(dentry, inode);
1096 err = 0;
1098 out:
1099 unlock_kernel();
1100 return err;
1102 out_no_entry:
1103 inode_dec_link_count(inode);
1104 iput(inode);
1105 goto out;
1108 static int udf_link(struct dentry * old_dentry, struct inode * dir,
1109 struct dentry *dentry)
1111 struct inode *inode = old_dentry->d_inode;
1112 struct udf_fileident_bh fibh;
1113 struct fileIdentDesc cfi, *fi;
1114 int err;
1116 lock_kernel();
1117 if (inode->i_nlink >= (256<<sizeof(inode->i_nlink))-1)
1119 unlock_kernel();
1120 return -EMLINK;
1123 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1125 unlock_kernel();
1126 return err;
1128 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1129 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1130 if (UDF_SB_LVIDBH(inode->i_sb))
1132 struct logicalVolHeaderDesc *lvhd;
1133 uint64_t uniqueID;
1134 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1135 uniqueID = le64_to_cpu(lvhd->uniqueID);
1136 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1137 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1138 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1139 uniqueID += 16;
1140 lvhd->uniqueID = cpu_to_le64(uniqueID);
1141 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1143 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1144 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1146 mark_inode_dirty(dir);
1148 if (fibh.sbh != fibh.ebh)
1149 brelse(fibh.ebh);
1150 brelse(fibh.sbh);
1151 inc_nlink(inode);
1152 inode->i_ctime = current_fs_time(inode->i_sb);
1153 mark_inode_dirty(inode);
1154 atomic_inc(&inode->i_count);
1155 d_instantiate(dentry, inode);
1156 unlock_kernel();
1157 return 0;
1160 /* Anybody can rename anything with this: the permission checks are left to the
1161 * higher-level routines.
1163 static int udf_rename (struct inode * old_dir, struct dentry * old_dentry,
1164 struct inode * new_dir, struct dentry * new_dentry)
1166 struct inode * old_inode = old_dentry->d_inode;
1167 struct inode * new_inode = new_dentry->d_inode;
1168 struct udf_fileident_bh ofibh, nfibh;
1169 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
1170 struct buffer_head *dir_bh = NULL;
1171 int retval = -ENOENT;
1172 kernel_lb_addr tloc;
1174 lock_kernel();
1175 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi)))
1177 if (ofibh.sbh != ofibh.ebh)
1178 brelse(ofibh.ebh);
1179 brelse(ofibh.sbh);
1181 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1182 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1183 != old_inode->i_ino)
1184 goto end_rename;
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 brelse(nfibh.ebh);
1193 brelse(nfibh.sbh);
1194 nfi = NULL;
1197 if (S_ISDIR(old_inode->i_mode))
1199 uint32_t offset = udf_ext0_offset(old_inode);
1201 if (new_inode)
1203 retval = -ENOTEMPTY;
1204 if (!empty_dir(new_inode))
1205 goto end_rename;
1207 retval = -EIO;
1208 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1210 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
1211 (UDF_I_EFE(old_inode) ?
1212 sizeof(struct extendedFileEntry) :
1213 sizeof(struct fileEntry)),
1214 old_inode->i_sb->s_blocksize, &offset);
1216 else
1218 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1219 if (!dir_bh)
1220 goto end_rename;
1221 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
1223 if (!dir_fi)
1224 goto end_rename;
1225 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1226 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0)
1227 != old_dir->i_ino)
1228 goto end_rename;
1230 retval = -EMLINK;
1231 if (!new_inode && new_dir->i_nlink >= (256<<sizeof(new_dir->i_nlink))-1)
1232 goto end_rename;
1234 if (!nfi)
1236 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1237 if (!nfi)
1238 goto end_rename;
1242 * Like most other Unix systems, set the ctime for inodes on a
1243 * rename.
1245 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1246 mark_inode_dirty(old_inode);
1249 * ok, that's it
1251 ncfi.fileVersionNum = ocfi.fileVersionNum;
1252 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1253 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1254 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1256 /* The old fid may have moved - find it again */
1257 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1258 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1260 if (new_inode)
1262 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1263 inode_dec_link_count(new_inode);
1265 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1266 mark_inode_dirty(old_dir);
1268 if (dir_fi)
1270 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1271 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
1272 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1273 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1275 mark_inode_dirty(old_inode);
1277 else
1278 mark_buffer_dirty_inode(dir_bh, old_inode);
1279 inode_dec_link_count(old_dir);
1280 if (new_inode)
1282 inode_dec_link_count(new_inode);
1284 else
1286 inc_nlink(new_dir);
1287 mark_inode_dirty(new_dir);
1291 if (ofi)
1293 if (ofibh.sbh != ofibh.ebh)
1294 brelse(ofibh.ebh);
1295 brelse(ofibh.sbh);
1298 retval = 0;
1300 end_rename:
1301 brelse(dir_bh);
1302 if (nfi)
1304 if (nfibh.sbh != nfibh.ebh)
1305 brelse(nfibh.ebh);
1306 brelse(nfibh.sbh);
1308 unlock_kernel();
1309 return retval;
1312 const struct inode_operations udf_dir_inode_operations = {
1313 .lookup = udf_lookup,
1314 .create = udf_create,
1315 .link = udf_link,
1316 .unlink = udf_unlink,
1317 .symlink = udf_symlink,
1318 .mkdir = udf_mkdir,
1319 .rmdir = udf_rmdir,
1320 .mknod = udf_mknod,
1321 .rename = udf_rename,