RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / udf / namei.c
blobf7204efeb47524241a3ba699e5d8aead48f12c8b
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>
34 #include <linux/exportfs.h>
36 static inline int udf_match(int len1, const char *name1, int len2, const char *name2)
38 if (len1 != len2)
39 return 0;
40 return !memcmp(name1, name2, len1);
43 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
44 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
45 uint8_t *impuse, uint8_t *fileident)
47 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
48 uint16_t crc;
49 uint8_t checksum = 0;
50 int i;
51 int offset;
52 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
53 uint8_t lfi = cfi->lengthFileIdent;
54 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
55 sizeof(struct fileIdentDesc);
56 int adinicb = 0;
58 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
59 adinicb = 1;
61 offset = fibh->soffset + sizeof(struct fileIdentDesc);
63 if (impuse)
65 if (adinicb || (offset + liu < 0))
66 memcpy((uint8_t *)sfi->impUse, impuse, liu);
67 else if (offset >= 0)
68 memcpy(fibh->ebh->b_data + offset, impuse, liu);
69 else
71 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
72 memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);
76 offset += liu;
78 if (fileident)
80 if (adinicb || (offset + lfi < 0))
81 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
82 else if (offset >= 0)
83 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
84 else
86 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, -offset);
87 memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);
91 offset += lfi;
93 if (adinicb || (offset + padlen < 0))
94 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
95 else if (offset >= 0)
96 memset(fibh->ebh->b_data + offset, 0x00, padlen);
97 else
99 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
100 memset(fibh->ebh->b_data, 0x00, padlen + offset);
103 crc = udf_crc((uint8_t *)cfi + sizeof(tag), sizeof(struct fileIdentDesc) -
104 sizeof(tag), 0);
106 if (fibh->sbh == fibh->ebh)
107 crc = udf_crc((uint8_t *)sfi->impUse,
108 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
109 else if (sizeof(struct fileIdentDesc) >= -fibh->soffset)
110 crc = udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) + fibh->soffset,
111 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
112 else
114 crc = udf_crc((uint8_t *)sfi->impUse,
115 -fibh->soffset - sizeof(struct fileIdentDesc), crc);
116 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
119 cfi->descTag.descCRC = cpu_to_le16(crc);
120 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
122 for (i=0; i<16; i++)
123 if (i != 4)
124 checksum += ((uint8_t *)&cfi->descTag)[i];
126 cfi->descTag.tagChecksum = checksum;
127 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset))
128 memcpy((uint8_t *)sfi, (uint8_t *)cfi, sizeof(struct fileIdentDesc));
129 else
131 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
132 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
133 sizeof(struct fileIdentDesc) + fibh->soffset);
136 if (adinicb)
137 mark_inode_dirty(inode);
138 else
140 if (fibh->sbh != fibh->ebh)
141 mark_buffer_dirty_inode(fibh->ebh, inode);
142 mark_buffer_dirty_inode(fibh->sbh, inode);
144 return 0;
147 static struct fileIdentDesc *
148 udf_find_entry(struct inode *dir, struct dentry *dentry,
149 struct udf_fileident_bh *fibh,
150 struct fileIdentDesc *cfi)
152 struct fileIdentDesc *fi=NULL;
153 loff_t f_pos;
154 int block, flen;
155 char fname[UDF_NAME_LEN];
156 char *nameptr;
157 uint8_t lfi;
158 uint16_t liu;
159 loff_t size;
160 kernel_lb_addr eloc;
161 uint32_t elen;
162 sector_t offset;
163 struct extent_position epos = { NULL, 0, { 0, 0}};
164 int isdotdot = child->len == 2 &&
165 child->name[0] == '.' && child->name[1] == '.';
167 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
168 f_pos = (udf_ext0_offset(dir) >> 2);
170 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
171 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
172 fibh->sbh = fibh->ebh = NULL;
173 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
174 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
176 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
177 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
179 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
180 epos.offset -= sizeof(short_ad);
181 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
182 epos.offset -= sizeof(long_ad);
184 else
185 offset = 0;
187 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
189 brelse(epos.bh);
190 return NULL;
193 else
195 brelse(epos.bh);
196 return NULL;
199 while ( (f_pos < size) )
201 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc, &elen, &offset);
203 if (!fi)
205 if (fibh->sbh != fibh->ebh)
206 brelse(fibh->ebh);
207 brelse(fibh->sbh);
208 brelse(epos.bh);
209 return NULL;
212 liu = le16_to_cpu(cfi->lengthOfImpUse);
213 lfi = cfi->lengthFileIdent;
215 if (fibh->sbh == fibh->ebh)
217 nameptr = fi->fileIdent + liu;
219 else
221 int poffset; /* Unpaded ending offset */
223 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
225 if (poffset >= lfi)
226 nameptr = (uint8_t *)(fibh->ebh->b_data + poffset - lfi);
227 else
229 nameptr = fname;
230 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
231 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
235 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
237 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE) )
238 continue;
241 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0 )
243 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE) )
244 continue;
247 if ((cfi->fileCharacteristics & FID_FILE_CHAR_PARENT) &&
248 isdotdot) {
249 brelse(epos.bh);
250 return fi;
253 if (!lfi)
254 continue;
256 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)))
258 if (udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
260 brelse(epos.bh);
261 return fi;
265 if (fibh->sbh != fibh->ebh)
266 brelse(fibh->ebh);
267 brelse(fibh->sbh);
268 brelse(epos.bh);
269 return NULL;
273 * udf_lookup
275 * PURPOSE
276 * Look-up the inode for a given name.
278 * DESCRIPTION
279 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
280 * available for a directory. The filesystem is useless if this routine is
281 * not available for at least the filesystem's root directory.
283 * This routine is passed an incomplete dentry - it must be completed by
284 * calling d_add(dentry, inode). If the name does not exist, then the
285 * specified inode must be set to null. An error should only be returned
286 * when the lookup fails for a reason other than the name not existing.
287 * Note that the directory inode semaphore is held during the call.
289 * Refer to lookup_dentry() in fs/namei.c
290 * lookup_dentry() -> lookup() -> real_lookup() -> .
292 * PRE-CONDITIONS
293 * dir Pointer to inode of parent directory.
294 * dentry Pointer to dentry to complete.
295 * nd Pointer to lookup nameidata
297 * POST-CONDITIONS
298 * <return> Zero on success.
300 * HISTORY
301 * July 1, 1997 - Andrew E. Mileski
302 * Written, tested, and released.
305 static struct dentry *
306 udf_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
308 struct inode *inode = NULL;
309 struct fileIdentDesc cfi;
310 struct udf_fileident_bh fibh;
312 if (dentry->d_name.len > UDF_NAME_LEN-2)
313 return ERR_PTR(-ENAMETOOLONG);
315 lock_kernel();
316 #ifdef UDF_RECOVERY
317 /* temporary shorthand for specifying files by inode number */
318 if (!strncmp(dentry->d_name.name, ".B=", 3) )
320 kernel_lb_addr lb = { 0, simple_strtoul(dentry->d_name.name+3, NULL, 0) };
321 inode = udf_iget(dir->i_sb, lb);
322 if (!inode)
324 unlock_kernel();
325 return ERR_PTR(-EACCES);
328 else
329 #endif /* UDF_RECOVERY */
331 if (udf_find_entry(dir, dentry, &fibh, &cfi))
333 if (fibh.sbh != fibh.ebh)
334 brelse(fibh.ebh);
335 brelse(fibh.sbh);
337 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
338 if ( !inode )
340 unlock_kernel();
341 return ERR_PTR(-EACCES);
344 unlock_kernel();
345 return d_splice_alias(inode, dentry);
348 static struct fileIdentDesc *
349 udf_add_entry(struct inode *dir, struct dentry *dentry,
350 struct udf_fileident_bh *fibh,
351 struct fileIdentDesc *cfi, int *err)
353 struct super_block *sb;
354 struct fileIdentDesc *fi=NULL;
355 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
356 int namelen;
357 loff_t f_pos;
358 int flen;
359 char *nameptr;
360 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
361 int nfidlen;
362 uint8_t lfi;
363 uint16_t liu;
364 int block;
365 kernel_lb_addr eloc;
366 uint32_t elen;
367 sector_t offset;
368 struct extent_position epos = { NULL, 0, { 0, 0 }};
370 sb = dir->i_sb;
372 if (dentry)
374 if (!dentry->d_name.len)
376 *err = -EINVAL;
377 return NULL;
380 if ( !(namelen = udf_put_filename(sb, dentry->d_name.name, name, dentry->d_name.len)))
382 *err = -ENAMETOOLONG;
383 return NULL;
386 else
387 namelen = 0;
389 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
391 f_pos = (udf_ext0_offset(dir) >> 2);
393 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
394 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
395 fibh->sbh = fibh->ebh = NULL;
396 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
397 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
399 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
400 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
402 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
403 epos.offset -= sizeof(short_ad);
404 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
405 epos.offset -= sizeof(long_ad);
407 else
408 offset = 0;
410 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
412 brelse(epos.bh);
413 *err = -EIO;
414 return NULL;
417 block = UDF_I_LOCATION(dir).logicalBlockNum;
420 else
422 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
423 fibh->sbh = fibh->ebh = NULL;
424 fibh->soffset = fibh->eoffset = sb->s_blocksize;
425 goto add;
428 while ( (f_pos < size) )
430 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc, &elen, &offset);
432 if (!fi)
434 if (fibh->sbh != fibh->ebh)
435 brelse(fibh->ebh);
436 brelse(fibh->sbh);
437 brelse(epos.bh);
438 *err = -EIO;
439 return NULL;
442 liu = le16_to_cpu(cfi->lengthOfImpUse);
443 lfi = cfi->lengthFileIdent;
445 if (fibh->sbh == fibh->ebh)
446 nameptr = fi->fileIdent + liu;
447 else
449 int poffset; /* Unpaded ending offset */
451 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
453 if (poffset >= lfi)
454 nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
455 else
457 nameptr = fname;
458 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
459 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
463 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
465 if (((sizeof(struct fileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen)
467 brelse(epos.bh);
468 cfi->descTag.tagSerialNum = cpu_to_le16(1);
469 cfi->fileVersionNum = cpu_to_le16(1);
470 cfi->fileCharacteristics = 0;
471 cfi->lengthFileIdent = namelen;
472 cfi->lengthOfImpUse = cpu_to_le16(0);
473 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
474 return fi;
475 else
477 *err = -EIO;
478 return NULL;
483 if (!lfi || !dentry)
484 continue;
486 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
487 udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
489 if (fibh->sbh != fibh->ebh)
490 brelse(fibh->ebh);
491 brelse(fibh->sbh);
492 brelse(epos.bh);
493 *err = -EEXIST;
494 return NULL;
498 add:
499 f_pos += nfidlen;
501 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
502 sb->s_blocksize - fibh->eoffset < nfidlen)
504 brelse(epos.bh);
505 epos.bh = NULL;
506 fibh->soffset -= udf_ext0_offset(dir);
507 fibh->eoffset -= udf_ext0_offset(dir);
508 f_pos -= (udf_ext0_offset(dir) >> 2);
509 if (fibh->sbh != fibh->ebh)
510 brelse(fibh->ebh);
511 brelse(fibh->sbh);
512 if (!(fibh->sbh = fibh->ebh = udf_expand_dir_adinicb(dir, &block, err)))
513 return NULL;
514 epos.block = UDF_I_LOCATION(dir);
515 eloc.logicalBlockNum = block;
516 eloc.partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
517 elen = dir->i_sb->s_blocksize;
518 epos.offset = udf_file_entry_alloc_offset(dir);
519 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
520 epos.offset += sizeof(short_ad);
521 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
522 epos.offset += sizeof(long_ad);
525 if (sb->s_blocksize - fibh->eoffset >= nfidlen)
527 fibh->soffset = fibh->eoffset;
528 fibh->eoffset += nfidlen;
529 if (fibh->sbh != fibh->ebh)
531 brelse(fibh->sbh);
532 fibh->sbh = fibh->ebh;
535 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
537 block = UDF_I_LOCATION(dir).logicalBlockNum;
538 fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) + fibh->soffset - udf_ext0_offset(dir) + UDF_I_LENEATTR(dir));
540 else
542 block = eloc.logicalBlockNum + ((elen - 1) >>
543 dir->i_sb->s_blocksize_bits);
544 fi = (struct fileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
547 else
549 fibh->soffset = fibh->eoffset - sb->s_blocksize;
550 fibh->eoffset += nfidlen - sb->s_blocksize;
551 if (fibh->sbh != fibh->ebh)
553 brelse(fibh->sbh);
554 fibh->sbh = fibh->ebh;
557 block = eloc.logicalBlockNum + ((elen - 1) >>
558 dir->i_sb->s_blocksize_bits);
560 if (!(fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err)))
562 brelse(epos.bh);
563 brelse(fibh->sbh);
564 return NULL;
567 if (!(fibh->soffset))
569 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
570 (EXT_RECORDED_ALLOCATED >> 30))
572 block = eloc.logicalBlockNum + ((elen - 1) >>
573 dir->i_sb->s_blocksize_bits);
575 else
576 block ++;
578 brelse(fibh->sbh);
579 fibh->sbh = fibh->ebh;
580 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
582 else
584 fi = (struct fileIdentDesc *)
585 (fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
589 memset(cfi, 0, sizeof(struct fileIdentDesc));
590 if (UDF_SB_UDFREV(sb) >= 0x0200)
591 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, sizeof(tag));
592 else
593 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, sizeof(tag));
594 cfi->fileVersionNum = cpu_to_le16(1);
595 cfi->lengthFileIdent = namelen;
596 cfi->lengthOfImpUse = cpu_to_le16(0);
597 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
599 brelse(epos.bh);
600 dir->i_size += nfidlen;
601 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
602 UDF_I_LENALLOC(dir) += nfidlen;
603 mark_inode_dirty(dir);
604 return fi;
606 else
608 brelse(epos.bh);
609 if (fibh->sbh != fibh->ebh)
610 brelse(fibh->ebh);
611 brelse(fibh->sbh);
612 *err = -EIO;
613 return NULL;
617 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
618 struct udf_fileident_bh *fibh, struct fileIdentDesc *cfi)
620 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
621 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
622 memset(&(cfi->icb), 0x00, sizeof(long_ad));
623 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
626 static int udf_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
628 struct udf_fileident_bh fibh;
629 struct inode *inode;
630 struct fileIdentDesc cfi, *fi;
631 int err;
633 lock_kernel();
634 inode = udf_new_inode(dir, mode, &err);
635 if (!inode)
637 unlock_kernel();
638 return err;
641 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
642 inode->i_data.a_ops = &udf_adinicb_aops;
643 else
644 inode->i_data.a_ops = &udf_aops;
645 inode->i_op = &udf_file_inode_operations;
646 inode->i_fop = &udf_file_operations;
647 inode->i_mode = mode;
648 mark_inode_dirty(inode);
650 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
652 inode->i_nlink --;
653 mark_inode_dirty(inode);
654 iput(inode);
655 unlock_kernel();
656 return err;
658 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
659 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
660 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
661 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
662 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
663 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
665 mark_inode_dirty(dir);
667 if (fibh.sbh != fibh.ebh)
668 brelse(fibh.ebh);
669 brelse(fibh.sbh);
670 unlock_kernel();
671 d_instantiate(dentry, inode);
672 return 0;
675 static int udf_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev)
677 struct inode * inode;
678 struct udf_fileident_bh fibh;
679 struct fileIdentDesc cfi, *fi;
680 int err;
682 if (!old_valid_dev(rdev))
683 return -EINVAL;
685 lock_kernel();
686 err = -EIO;
687 inode = udf_new_inode(dir, mode, &err);
688 if (!inode)
689 goto out;
691 inode->i_uid = current->fsuid;
692 init_special_inode(inode, mode, rdev);
693 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
695 inode->i_nlink --;
696 mark_inode_dirty(inode);
697 iput(inode);
698 unlock_kernel();
699 return err;
701 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
702 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
703 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
704 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
705 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
706 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
708 mark_inode_dirty(dir);
710 mark_inode_dirty(inode);
712 if (fibh.sbh != fibh.ebh)
713 brelse(fibh.ebh);
714 brelse(fibh.sbh);
715 d_instantiate(dentry, inode);
716 err = 0;
717 out:
718 unlock_kernel();
719 return err;
722 static int udf_mkdir(struct inode * dir, struct dentry * dentry, int mode)
724 struct inode * inode;
725 struct udf_fileident_bh fibh;
726 struct fileIdentDesc cfi, *fi;
727 int err;
729 lock_kernel();
730 err = -EMLINK;
731 if (dir->i_nlink >= (256<<sizeof(dir->i_nlink))-1)
732 goto out;
734 err = -EIO;
735 inode = udf_new_inode(dir, S_IFDIR, &err);
736 if (!inode)
737 goto out;
739 inode->i_op = &udf_dir_inode_operations;
740 inode->i_fop = &udf_dir_operations;
741 if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err)))
743 inode->i_nlink--;
744 mark_inode_dirty(inode);
745 iput(inode);
746 goto out;
748 inode->i_nlink = 2;
749 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
750 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
751 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
752 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
753 cfi.fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
754 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
755 brelse(fibh.sbh);
756 inode->i_mode = S_IFDIR | mode;
757 if (dir->i_mode & S_ISGID)
758 inode->i_mode |= S_ISGID;
759 mark_inode_dirty(inode);
761 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
763 inode->i_nlink = 0;
764 mark_inode_dirty(inode);
765 iput(inode);
766 goto out;
768 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
769 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
770 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
771 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
772 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
773 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
774 inc_nlink(dir);
775 mark_inode_dirty(dir);
776 d_instantiate(dentry, inode);
777 if (fibh.sbh != fibh.ebh)
778 brelse(fibh.ebh);
779 brelse(fibh.sbh);
780 err = 0;
781 out:
782 unlock_kernel();
783 return err;
786 static int empty_dir(struct inode *dir)
788 struct fileIdentDesc *fi, cfi;
789 struct udf_fileident_bh fibh;
790 loff_t f_pos;
791 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
792 int block;
793 kernel_lb_addr eloc;
794 uint32_t elen;
795 sector_t offset;
796 struct extent_position epos = { NULL, 0, { 0, 0}};
798 f_pos = (udf_ext0_offset(dir) >> 2);
800 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
802 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
803 fibh.sbh = fibh.ebh = NULL;
804 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
805 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
807 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
808 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
810 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
811 epos.offset -= sizeof(short_ad);
812 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
813 epos.offset -= sizeof(long_ad);
815 else
816 offset = 0;
818 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block)))
820 brelse(epos.bh);
821 return 0;
824 else
826 brelse(epos.bh);
827 return 0;
831 while ( (f_pos < size) )
833 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc, &elen, &offset);
835 if (!fi)
837 if (fibh.sbh != fibh.ebh)
838 brelse(fibh.ebh);
839 brelse(fibh.sbh);
840 brelse(epos.bh);
841 return 0;
844 if (cfi.lengthFileIdent && (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0)
846 if (fibh.sbh != fibh.ebh)
847 brelse(fibh.ebh);
848 brelse(fibh.sbh);
849 brelse(epos.bh);
850 return 0;
853 if (fibh.sbh != fibh.ebh)
854 brelse(fibh.ebh);
855 brelse(fibh.sbh);
856 brelse(epos.bh);
857 return 1;
860 static int udf_rmdir(struct inode * dir, struct dentry * dentry)
862 int retval;
863 struct inode * inode = dentry->d_inode;
864 struct udf_fileident_bh fibh;
865 struct fileIdentDesc *fi, cfi;
866 kernel_lb_addr tloc;
868 retval = -ENOENT;
869 lock_kernel();
870 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
871 if (!fi)
872 goto out;
874 retval = -EIO;
875 tloc = lelb_to_cpu(cfi.icb.extLocation);
876 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
877 goto end_rmdir;
878 retval = -ENOTEMPTY;
879 if (!empty_dir(inode))
880 goto end_rmdir;
881 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
882 if (retval)
883 goto end_rmdir;
884 if (inode->i_nlink != 2)
885 udf_warning(inode->i_sb, "udf_rmdir",
886 "empty directory has nlink != 2 (%d)",
887 inode->i_nlink);
888 clear_nlink(inode);
889 inode->i_size = 0;
890 inode_dec_link_count(dir);
891 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
892 mark_inode_dirty(dir);
894 end_rmdir:
895 if (fibh.sbh != fibh.ebh)
896 brelse(fibh.ebh);
897 brelse(fibh.sbh);
898 out:
899 unlock_kernel();
900 return retval;
903 static int udf_unlink(struct inode * dir, struct dentry * dentry)
905 int retval;
906 struct inode * inode = dentry->d_inode;
907 struct udf_fileident_bh fibh;
908 struct fileIdentDesc *fi;
909 struct fileIdentDesc cfi;
910 kernel_lb_addr tloc;
912 retval = -ENOENT;
913 lock_kernel();
914 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
915 if (!fi)
916 goto out;
918 retval = -EIO;
919 tloc = lelb_to_cpu(cfi.icb.extLocation);
920 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
921 goto end_unlink;
923 if (!inode->i_nlink)
925 udf_debug("Deleting nonexistent file (%lu), %d\n",
926 inode->i_ino, inode->i_nlink);
927 inode->i_nlink = 1;
929 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
930 if (retval)
931 goto end_unlink;
932 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
933 mark_inode_dirty(dir);
934 inode_dec_link_count(inode);
935 inode->i_ctime = dir->i_ctime;
936 retval = 0;
938 end_unlink:
939 if (fibh.sbh != fibh.ebh)
940 brelse(fibh.ebh);
941 brelse(fibh.sbh);
942 out:
943 unlock_kernel();
944 return retval;
947 static int udf_symlink(struct inode * dir, struct dentry * dentry, const char * symname)
949 struct inode * inode;
950 struct pathComponent *pc;
951 char *compstart;
952 struct udf_fileident_bh fibh;
953 struct extent_position epos = { NULL, 0, {0, 0}};
954 int eoffset, elen = 0;
955 struct fileIdentDesc *fi;
956 struct fileIdentDesc cfi;
957 char *ea;
958 int err;
959 int block;
960 char name[UDF_NAME_LEN];
961 int namelen;
963 lock_kernel();
964 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
965 goto out;
967 inode->i_mode = S_IFLNK | S_IRWXUGO;
968 inode->i_data.a_ops = &udf_symlink_aops;
969 inode->i_op = &page_symlink_inode_operations;
971 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB)
973 kernel_lb_addr eloc;
974 uint32_t elen;
976 block = udf_new_block(inode->i_sb, inode,
977 UDF_I_LOCATION(inode).partitionReferenceNum,
978 UDF_I_LOCATION(inode).logicalBlockNum, &err);
979 if (!block)
980 goto out_no_entry;
981 epos.block = UDF_I_LOCATION(inode);
982 epos.offset = udf_file_entry_alloc_offset(inode);
983 epos.bh = NULL;
984 eloc.logicalBlockNum = block;
985 eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
986 elen = inode->i_sb->s_blocksize;
987 UDF_I_LENEXTENTS(inode) = elen;
988 udf_add_aext(inode, &epos, eloc, elen, 0);
989 brelse(epos.bh);
991 block = udf_get_pblock(inode->i_sb, block,
992 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
993 epos.bh = udf_tread(inode->i_sb, block);
994 lock_buffer(epos.bh);
995 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
996 set_buffer_uptodate(epos.bh);
997 unlock_buffer(epos.bh);
998 mark_buffer_dirty_inode(epos.bh, inode);
999 ea = epos.bh->b_data + udf_ext0_offset(inode);
1001 else
1002 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
1004 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
1005 pc = (struct pathComponent *)ea;
1007 if (*symname == '/')
1011 symname++;
1012 } while (*symname == '/');
1014 pc->componentType = 1;
1015 pc->lengthComponentIdent = 0;
1016 pc->componentFileVersionNum = 0;
1017 pc += sizeof(struct pathComponent);
1018 elen += sizeof(struct pathComponent);
1021 err = -ENAMETOOLONG;
1023 while (*symname)
1025 if (elen + sizeof(struct pathComponent) > eoffset)
1026 goto out_no_entry;
1028 pc = (struct pathComponent *)(ea + elen);
1030 compstart = (char *)symname;
1034 symname++;
1035 } while (*symname && *symname != '/');
1037 pc->componentType = 5;
1038 pc->lengthComponentIdent = 0;
1039 pc->componentFileVersionNum = 0;
1040 if (compstart[0] == '.')
1042 if ((symname-compstart) == 1)
1043 pc->componentType = 4;
1044 else if ((symname-compstart) == 2 && compstart[1] == '.')
1045 pc->componentType = 3;
1048 if (pc->componentType == 5)
1050 if ( !(namelen = udf_put_filename(inode->i_sb, compstart, name, symname-compstart)))
1051 goto out_no_entry;
1053 if (elen + sizeof(struct pathComponent) + namelen > eoffset)
1054 goto out_no_entry;
1055 else
1056 pc->lengthComponentIdent = namelen;
1058 memcpy(pc->componentIdent, name, namelen);
1061 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1063 if (*symname)
1067 symname++;
1068 } while (*symname == '/');
1072 brelse(epos.bh);
1073 inode->i_size = elen;
1074 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1075 UDF_I_LENALLOC(inode) = inode->i_size;
1076 mark_inode_dirty(inode);
1078 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1079 goto out_no_entry;
1080 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1081 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1082 if (UDF_SB_LVIDBH(inode->i_sb))
1084 struct logicalVolHeaderDesc *lvhd;
1085 uint64_t uniqueID;
1086 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1087 uniqueID = le64_to_cpu(lvhd->uniqueID);
1088 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1089 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1090 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1091 uniqueID += 16;
1092 lvhd->uniqueID = cpu_to_le64(uniqueID);
1093 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1095 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1096 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1098 mark_inode_dirty(dir);
1100 if (fibh.sbh != fibh.ebh)
1101 brelse(fibh.ebh);
1102 brelse(fibh.sbh);
1103 d_instantiate(dentry, inode);
1104 err = 0;
1106 out:
1107 unlock_kernel();
1108 return err;
1110 out_no_entry:
1111 inode_dec_link_count(inode);
1112 iput(inode);
1113 goto out;
1116 static 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 struct fileIdentDesc cfi, *fi;
1122 int err;
1124 lock_kernel();
1125 if (inode->i_nlink >= (256<<sizeof(inode->i_nlink))-1)
1127 unlock_kernel();
1128 return -EMLINK;
1131 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1133 unlock_kernel();
1134 return err;
1136 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1137 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1138 if (UDF_SB_LVIDBH(inode->i_sb))
1140 struct logicalVolHeaderDesc *lvhd;
1141 uint64_t uniqueID;
1142 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1143 uniqueID = le64_to_cpu(lvhd->uniqueID);
1144 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1145 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1146 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1147 uniqueID += 16;
1148 lvhd->uniqueID = cpu_to_le64(uniqueID);
1149 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1151 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1152 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1154 mark_inode_dirty(dir);
1156 if (fibh.sbh != fibh.ebh)
1157 brelse(fibh.ebh);
1158 brelse(fibh.sbh);
1159 inc_nlink(inode);
1160 inode->i_ctime = current_fs_time(inode->i_sb);
1161 mark_inode_dirty(inode);
1162 atomic_inc(&inode->i_count);
1163 d_instantiate(dentry, inode);
1164 unlock_kernel();
1165 return 0;
1168 /* Anybody can rename anything with this: the permission checks are left to the
1169 * higher-level routines.
1171 static int udf_rename (struct inode * old_dir, struct dentry * old_dentry,
1172 struct inode * new_dir, struct dentry * new_dentry)
1174 struct inode * old_inode = old_dentry->d_inode;
1175 struct inode * new_inode = new_dentry->d_inode;
1176 struct udf_fileident_bh ofibh, nfibh;
1177 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
1178 struct buffer_head *dir_bh = NULL;
1179 int retval = -ENOENT;
1180 kernel_lb_addr tloc;
1182 lock_kernel();
1183 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi)))
1185 if (ofibh.sbh != ofibh.ebh)
1186 brelse(ofibh.ebh);
1187 brelse(ofibh.sbh);
1189 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1190 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1191 != old_inode->i_ino)
1192 goto end_rename;
1194 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1195 if (nfi)
1197 if (!new_inode)
1199 if (nfibh.sbh != nfibh.ebh)
1200 brelse(nfibh.ebh);
1201 brelse(nfibh.sbh);
1202 nfi = NULL;
1205 if (S_ISDIR(old_inode->i_mode))
1207 uint32_t offset = udf_ext0_offset(old_inode);
1209 if (new_inode)
1211 retval = -ENOTEMPTY;
1212 if (!empty_dir(new_inode))
1213 goto end_rename;
1215 retval = -EIO;
1216 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1218 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
1219 (UDF_I_EFE(old_inode) ?
1220 sizeof(struct extendedFileEntry) :
1221 sizeof(struct fileEntry)),
1222 old_inode->i_sb->s_blocksize, &offset);
1224 else
1226 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1227 if (!dir_bh)
1228 goto end_rename;
1229 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
1231 if (!dir_fi)
1232 goto end_rename;
1233 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1234 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0)
1235 != old_dir->i_ino)
1236 goto end_rename;
1238 retval = -EMLINK;
1239 if (!new_inode && new_dir->i_nlink >= (256<<sizeof(new_dir->i_nlink))-1)
1240 goto end_rename;
1242 if (!nfi)
1244 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1245 if (!nfi)
1246 goto end_rename;
1250 * Like most other Unix systems, set the ctime for inodes on a
1251 * rename.
1253 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1254 mark_inode_dirty(old_inode);
1257 * ok, that's it
1259 ncfi.fileVersionNum = ocfi.fileVersionNum;
1260 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1261 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1262 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1264 /* The old fid may have moved - find it again */
1265 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1266 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1268 if (new_inode)
1270 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1271 inode_dec_link_count(new_inode);
1273 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1274 mark_inode_dirty(old_dir);
1276 if (dir_fi)
1278 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1279 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
1280 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1281 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1283 mark_inode_dirty(old_inode);
1285 else
1286 mark_buffer_dirty_inode(dir_bh, old_inode);
1287 inode_dec_link_count(old_dir);
1288 if (new_inode)
1290 inode_dec_link_count(new_inode);
1292 else
1294 inc_nlink(new_dir);
1295 mark_inode_dirty(new_dir);
1299 if (ofi)
1301 if (ofibh.sbh != ofibh.ebh)
1302 brelse(ofibh.ebh);
1303 brelse(ofibh.sbh);
1306 retval = 0;
1308 end_rename:
1309 brelse(dir_bh);
1310 if (nfi)
1312 if (nfibh.sbh != nfibh.ebh)
1313 brelse(nfibh.ebh);
1314 brelse(nfibh.sbh);
1316 unlock_kernel();
1317 return retval;
1320 static struct dentry *udf_get_parent(struct dentry *child)
1322 struct dentry *parent;
1323 struct kernel_lb_addr tloc;
1324 struct inode *inode = NULL;
1325 struct qstr dotdot = {.name = "..", .len = 2};
1326 struct fileIdentDesc cfi;
1327 struct udf_fileident_bh fibh;
1329 lock_kernel();
1330 if (!udf_find_entry(child->d_inode, &dotdot, &fibh, &cfi))
1331 goto out_unlock;
1333 if (fibh.sbh != fibh.ebh)
1334 brelse(fibh.ebh);
1335 brelse(fibh.sbh);
1337 tloc = lelb_to_cpu(cfi.icb.extLocation);
1338 inode = udf_iget(child->d_inode->i_sb, &tloc);
1339 if (!inode)
1340 goto out_unlock;
1341 unlock_kernel();
1343 parent = d_alloc_anon(inode);
1344 if (!parent) {
1345 iput(inode);
1346 parent = ERR_PTR(-ENOMEM);
1349 return parent;
1350 out_unlock:
1351 unlock_kernel();
1352 return ERR_PTR(-EACCES);
1356 static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
1357 u16 partref, __u32 generation)
1359 struct inode *inode;
1360 struct dentry *result;
1361 struct kernel_lb_addr loc;
1363 if (block == 0)
1364 return ERR_PTR(-ESTALE);
1366 loc.logicalBlockNum = block;
1367 loc.partitionReferenceNum = partref;
1368 inode = udf_iget(sb, &loc);
1370 if (inode == NULL)
1371 return ERR_PTR(-ENOMEM);
1373 if (generation && inode->i_generation != generation) {
1374 iput(inode);
1375 return ERR_PTR(-ESTALE);
1377 result = d_alloc_anon(inode);
1378 if (!result) {
1379 iput(inode);
1380 return ERR_PTR(-ENOMEM);
1382 return result;
1385 static struct dentry *udf_fh_to_dentry(struct super_block *sb,
1386 struct fid *fid, int fh_len, int fh_type)
1388 if ((fh_len != 3 && fh_len != 5) ||
1389 (fh_type != FILEID_UDF_WITH_PARENT &&
1390 fh_type != FILEID_UDF_WITHOUT_PARENT))
1391 return NULL;
1393 return udf_nfs_get_inode(sb, fid->udf.block, fid->udf.partref,
1394 fid->udf.generation);
1397 static struct dentry *udf_fh_to_parent(struct super_block *sb,
1398 struct fid *fid, int fh_len, int fh_type)
1400 if (fh_len != 5 || fh_type != FILEID_UDF_WITH_PARENT)
1401 return NULL;
1403 return udf_nfs_get_inode(sb, fid->udf.parent_block,
1404 fid->udf.parent_partref,
1405 fid->udf.parent_generation);
1407 static int udf_encode_fh(struct dentry *de, __u32 *fh, int *lenp,
1408 int connectable)
1410 int len = *lenp;
1411 struct inode *inode = de->d_inode;
1412 struct kernel_lb_addr location = UDF_I(inode)->i_location;
1413 struct fid *fid = (struct fid *)fh;
1414 int type = FILEID_UDF_WITHOUT_PARENT;
1416 if (len < 3 || (connectable && len < 5))
1417 return 255;
1419 *lenp = 3;
1420 fid->udf.block = location.logicalBlockNum;
1421 fid->udf.partref = location.partitionReferenceNum;
1422 fid->udf.generation = inode->i_generation;
1424 if (connectable && !S_ISDIR(inode->i_mode)) {
1425 spin_lock(&de->d_lock);
1426 inode = de->d_parent->d_inode;
1427 location = UDF_I(inode)->i_location;
1428 fid->udf.parent_block = location.logicalBlockNum;
1429 fid->udf.parent_partref = location.partitionReferenceNum;
1430 fid->udf.parent_generation = inode->i_generation;
1431 spin_unlock(&de->d_lock);
1432 *lenp = 5;
1433 type = FILEID_UDF_WITH_PARENT;
1436 return type;
1439 const struct export_operations udf_export_ops = {
1440 .encode_fh = udf_encode_fh,
1441 .fh_to_dentry = udf_fh_to_dentry,
1442 .fh_to_parent = udf_fh_to_parent,
1443 .get_parent = udf_get_parent,
1446 const struct inode_operations udf_dir_inode_operations = {
1447 .lookup = udf_lookup,
1448 .create = udf_create,
1449 .link = udf_link,
1450 .unlink = udf_unlink,
1451 .symlink = udf_symlink,
1452 .mkdir = udf_mkdir,
1453 .rmdir = udf_rmdir,
1454 .mknod = udf_mknod,
1455 .rename = udf_rename,