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