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